Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

fix(pipeline): route a first index as no_existing_db, not invalid_existing_db

Martin Vogel7b4eb4a00ac6
diff --git a/src/pipeline/pipeline.c b/src/pipeline/pipeline.c
index 3d600034e..5d21a1bde 100644
--- a/src/pipeline/pipeline.c
+++ b/src/pipeline/pipeline.c
@@ -196,6 +196,15 @@ struct cbm_pipeline {
* full rebuild, so the MCP response can surface the migration. */
bool format_migration;
+ /* Recorded by cbm_pipeline_run for the staged run beneath it: whether
+ * the destination existed, and whether it was copied into the stage so
+ * that an incremental route has a real previous generation to work
+ * from. Without a copy the stage is the run's empty placeholder, and
+ * probing THAT for integrity is what reported every first index as
+ * "invalid_existing_db" (#1864). */
+ bool final_existed;
+ bool existing_generation;
+
/* ADR (project_summaries) captured before a full-reindex DB delete, so it
* can be restored after the rebuild. NULL when no ADR existed. Issue #516. */
char *saved_adr;
@@ -1403,6 +1412,16 @@ static int capture_existing_adr(cbm_pipeline_t *p, const char *db_path) {
static int try_incremental_or_delete_db(cbm_pipeline_t *p, cbm_file_info_t *files, int file_count,
const cbm_file_hash_t *baseline_manifest,
int baseline_count, bool force_full_on_mismatch) {
+ if (!p->existing_generation) {
+ /* Nothing to be incremental against: a first index, or a
+ * destination that could not be copied (already reported as
+ * backup_failed_full_rebuild). The stage is an empty placeholder,
+ * not a database, so it is not probed -- "invalid_existing_db"
+ * stays reserved for a real copy that fails its integrity check. */
+ cbm_log_info("pipeline.route", "path", "full", "reason",
+ p->final_existed ? "existing_db_backup_failed" : "no_existing_db");
+ return CBM_PIPELINE_FORCE_FULL_REINDEX;
+ }
char *db_path = resolve_db_path(p);
if (!db_path) {
return CBM_PIPELINE_FORCE_FULL_REINDEX;
@@ -2956,6 +2975,8 @@ int cbm_pipeline_run(cbm_pipeline_t *p) {
}
}
+ p->final_existed = final_existed;
+ p->existing_generation = final_existed && backup_succeeded;
char *configured_db_path = p->db_path;
p->db_path = strdup(staging_path);
if (!p->db_path) {
diff --git a/tests/test_pipeline.c b/tests/test_pipeline.c
index 9ee54b41b..0892fd696 100644
--- a/tests/test_pipeline.c
+++ b/tests/test_pipeline.c
@@ -3707,6 +3707,66 @@ TEST(pipeline_sweep_removes_dead_writer_stage_keeps_locked_stage) {
PASS();
}
+static char g_route_log_capture[8192];
+static atomic_flag g_route_log_spin = ATOMIC_FLAG_INIT;
+
+/* Keeps only the route decisions; worker threads log too, and only the
+ * appends need serialising. */
+static void capture_route_log_sink(const char *line) {
+ if (!line || !strstr(line, "pipeline.route")) {
+ return;
+ }
+ while (atomic_flag_test_and_set_explicit(&g_route_log_spin, memory_order_acquire)) {}
+ size_t used = strlen(g_route_log_capture);
+ size_t avail = sizeof(g_route_log_capture) - used;
+ if (avail > 1) {
+ int n = snprintf(g_route_log_capture + used, avail, "%s\n", line);
+ if (n < 0 || (size_t)n >= avail) {
+ g_route_log_capture[sizeof(g_route_log_capture) - 1] = '\0';
+ }
+ }
+ atomic_flag_clear_explicit(&g_route_log_spin, memory_order_release);
+}
+
+/* #1864: a first index has no previous generation. The route probe used to
+ * open the run's own EMPTY stage placeholder, fail its integrity check, and
+ * warn "reason=invalid_existing_db" on every first index of every project,
+ * which the report read as a corrupted database and a crash loop. A fresh
+ * index says what it is. */
+TEST(pipeline_fresh_index_never_reports_invalid_existing_db) {
+ char tmp[256];
+ snprintf(tmp, sizeof(tmp), "/tmp/cbm_fresh_route_XXXXXX");
+ ASSERT_NOT_NULL(cbm_mkdtemp(tmp));
+ write_temp_file(tmp, "generation.py", "def StableGeneration():\n return 1\n");
+ char db_path[512];
+ snprintf(db_path, sizeof(db_path), "%s/generation.db", tmp);
+
+ g_route_log_capture[0] = '\0';
+ CBMLogLevel previous_level = cbm_log_get_level();
+ cbm_log_set_level(CBM_LOG_DEBUG);
+ cbm_log_set_sink(capture_route_log_sink);
+ cbm_pipeline_incremental_test_reset_faults();
+ cbm_pipeline_t *fresh = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
+ int fresh_rc = fresh ? cbm_pipeline_run(fresh) : -1;
+ cbm_pipeline_free(fresh);
+ cbm_log_set_sink(NULL);
+ cbm_log_set_level(previous_level);
+
+ bool invalid_reported = strstr(g_route_log_capture, "invalid_existing_db") != NULL;
+ bool fresh_reported = strstr(g_route_log_capture, "reason=no_existing_db") != NULL;
+ bool db_present = path_exists(db_path);
+ int stage_count = count_generation_stage_artifacts(tmp, "generation.db");
+ cbm_pipeline_incremental_test_reset_faults();
+ th_rmtree(tmp);
+
+ ASSERT_EQ(fresh_rc, 0);
+ ASSERT_TRUE(!invalid_reported);
+ ASSERT_TRUE(fresh_reported);
+ ASSERT_TRUE(db_present);
+ ASSERT_EQ(stage_count, 0);
+ PASS();
+}
+
/* Discovery and extraction must describe the same immutable generation. A
* source file created after extraction is not present in the original file
* list, so merely re-hashing that list cannot detect the race. Publication
@@ -14413,6 +14473,7 @@ SUITE(pipeline_semantic_manifest_repro) {
RUN_TEST(pipeline_minted_stage_is_owned_until_released);
RUN_TEST(pipeline_stale_zero_byte_stage_beside_valid_db_routes_incremental_and_is_swept);
RUN_TEST(pipeline_sweep_removes_dead_writer_stage_keeps_locked_stage);
+ RUN_TEST(pipeline_fresh_index_never_reports_invalid_existing_db);
RUN_TEST(pipeline_source_mutation_before_publication_preserves_previous_generation);
RUN_TEST(pipeline_source_addition_before_publication_preserves_previous_generation);
RUN_TEST(pipeline_tsconfig_mutation_before_publication_preserves_previous_generation);