Skip to content

Commit 7b4eb4a

Browse files
DeusDataclaude
andcommitted
fix(pipeline): route a first index as no_existing_db, not invalid_existing_db
The route probe opened whatever resolve_db_path returned, which inside the staged run is the run's own stage. On a first index nothing was copied into it, so the probe opened a 0-byte mkstemp placeholder, failed its integrity check, and warned "pipeline.route path=full reason=invalid_existing_db" -- on every first index of every project. Read back after an OOM kill it looked like a corrupted database and a daily "corrupt -> full rebuild -> OOM" loop (DeusData#1864); the loop is "no database -> full -> OOM", and the stale 0-byte stage never influenced the next run at all (each run mints a fresh name). cbm_pipeline_run now records whether the destination existed and was copied into the stage, and the route decision returns full early with reason=no_existing_db (or existing_db_backup_failed, after the backup_failed_full_rebuild warning) without probing a placeholder. "invalid_existing_db" stays reserved for a real copy that fails its integrity check. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent b6458f9 commit 7b4eb4a

2 files changed

Lines changed: 82 additions & 0 deletions

File tree

‎src/pipeline/pipeline.c‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,15 @@ struct cbm_pipeline {
196196
* full rebuild, so the MCP response can surface the migration. */
197197
bool format_migration;
198198

199+
/* Recorded by cbm_pipeline_run for the staged run beneath it: whether
200+
* the destination existed, and whether it was copied into the stage so
201+
* that an incremental route has a real previous generation to work
202+
* from. Without a copy the stage is the run's empty placeholder, and
203+
* probing THAT for integrity is what reported every first index as
204+
* "invalid_existing_db" (#1864). */
205+
bool final_existed;
206+
bool existing_generation;
207+
199208
/* ADR (project_summaries) captured before a full-reindex DB delete, so it
200209
* can be restored after the rebuild. NULL when no ADR existed. Issue #516. */
201210
char *saved_adr;
@@ -1403,6 +1412,16 @@ static int capture_existing_adr(cbm_pipeline_t *p, const char *db_path) {
14031412
static int try_incremental_or_delete_db(cbm_pipeline_t *p, cbm_file_info_t *files, int file_count,
14041413
const cbm_file_hash_t *baseline_manifest,
14051414
int baseline_count, bool force_full_on_mismatch) {
1415+
if (!p->existing_generation) {
1416+
/* Nothing to be incremental against: a first index, or a
1417+
* destination that could not be copied (already reported as
1418+
* backup_failed_full_rebuild). The stage is an empty placeholder,
1419+
* not a database, so it is not probed -- "invalid_existing_db"
1420+
* stays reserved for a real copy that fails its integrity check. */
1421+
cbm_log_info("pipeline.route", "path", "full", "reason",
1422+
p->final_existed ? "existing_db_backup_failed" : "no_existing_db");
1423+
return CBM_PIPELINE_FORCE_FULL_REINDEX;
1424+
}
14061425
char *db_path = resolve_db_path(p);
14071426
if (!db_path) {
14081427
return CBM_PIPELINE_FORCE_FULL_REINDEX;
@@ -2956,6 +2975,8 @@ int cbm_pipeline_run(cbm_pipeline_t *p) {
29562975
}
29572976
}
29582977

2978+
p->final_existed = final_existed;
2979+
p->existing_generation = final_existed && backup_succeeded;
29592980
char *configured_db_path = p->db_path;
29602981
p->db_path = strdup(staging_path);
29612982
if (!p->db_path) {

‎tests/test_pipeline.c‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3707,6 +3707,66 @@ TEST(pipeline_sweep_removes_dead_writer_stage_keeps_locked_stage) {
37073707
PASS();
37083708
}
37093709

3710+
static char g_route_log_capture[8192];
3711+
static atomic_flag g_route_log_spin = ATOMIC_FLAG_INIT;
3712+
3713+
/* Keeps only the route decisions; worker threads log too, and only the
3714+
* appends need serialising. */
3715+
static void capture_route_log_sink(const char *line) {
3716+
if (!line || !strstr(line, "pipeline.route")) {
3717+
return;
3718+
}
3719+
while (atomic_flag_test_and_set_explicit(&g_route_log_spin, memory_order_acquire)) {}
3720+
size_t used = strlen(g_route_log_capture);
3721+
size_t avail = sizeof(g_route_log_capture) - used;
3722+
if (avail > 1) {
3723+
int n = snprintf(g_route_log_capture + used, avail, "%s\n", line);
3724+
if (n < 0 || (size_t)n >= avail) {
3725+
g_route_log_capture[sizeof(g_route_log_capture) - 1] = '\0';
3726+
}
3727+
}
3728+
atomic_flag_clear_explicit(&g_route_log_spin, memory_order_release);
3729+
}
3730+
3731+
/* #1864: a first index has no previous generation. The route probe used to
3732+
* open the run's own EMPTY stage placeholder, fail its integrity check, and
3733+
* warn "reason=invalid_existing_db" on every first index of every project,
3734+
* which the report read as a corrupted database and a crash loop. A fresh
3735+
* index says what it is. */
3736+
TEST(pipeline_fresh_index_never_reports_invalid_existing_db) {
3737+
char tmp[256];
3738+
snprintf(tmp, sizeof(tmp), "/tmp/cbm_fresh_route_XXXXXX");
3739+
ASSERT_NOT_NULL(cbm_mkdtemp(tmp));
3740+
write_temp_file(tmp, "generation.py", "def StableGeneration():\n return 1\n");
3741+
char db_path[512];
3742+
snprintf(db_path, sizeof(db_path), "%s/generation.db", tmp);
3743+
3744+
g_route_log_capture[0] = '\0';
3745+
CBMLogLevel previous_level = cbm_log_get_level();
3746+
cbm_log_set_level(CBM_LOG_DEBUG);
3747+
cbm_log_set_sink(capture_route_log_sink);
3748+
cbm_pipeline_incremental_test_reset_faults();
3749+
cbm_pipeline_t *fresh = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
3750+
int fresh_rc = fresh ? cbm_pipeline_run(fresh) : -1;
3751+
cbm_pipeline_free(fresh);
3752+
cbm_log_set_sink(NULL);
3753+
cbm_log_set_level(previous_level);
3754+
3755+
bool invalid_reported = strstr(g_route_log_capture, "invalid_existing_db") != NULL;
3756+
bool fresh_reported = strstr(g_route_log_capture, "reason=no_existing_db") != NULL;
3757+
bool db_present = path_exists(db_path);
3758+
int stage_count = count_generation_stage_artifacts(tmp, "generation.db");
3759+
cbm_pipeline_incremental_test_reset_faults();
3760+
th_rmtree(tmp);
3761+
3762+
ASSERT_EQ(fresh_rc, 0);
3763+
ASSERT_TRUE(!invalid_reported);
3764+
ASSERT_TRUE(fresh_reported);
3765+
ASSERT_TRUE(db_present);
3766+
ASSERT_EQ(stage_count, 0);
3767+
PASS();
3768+
}
3769+
37103770
/* Discovery and extraction must describe the same immutable generation. A
37113771
* source file created after extraction is not present in the original file
37123772
* list, so merely re-hashing that list cannot detect the race. Publication
@@ -14413,6 +14473,7 @@ SUITE(pipeline_semantic_manifest_repro) {
1441314473
RUN_TEST(pipeline_minted_stage_is_owned_until_released);
1441414474
RUN_TEST(pipeline_stale_zero_byte_stage_beside_valid_db_routes_incremental_and_is_swept);
1441514475
RUN_TEST(pipeline_sweep_removes_dead_writer_stage_keeps_locked_stage);
14476+
RUN_TEST(pipeline_fresh_index_never_reports_invalid_existing_db);
1441614477
RUN_TEST(pipeline_source_mutation_before_publication_preserves_previous_generation);
1441714478
RUN_TEST(pipeline_source_addition_before_publication_preserves_previous_generation);
1441814479
RUN_TEST(pipeline_tsconfig_mutation_before_publication_preserves_previous_generation);

0 commit comments

Comments
 (0)