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.

Merge origin/main into fix/2276-incremental-reindex-root-path-stale

Martin Vogelfe38a8a23b61
diff --cc tests/test_pipeline.c
index 2ac38d2d7,7b868a7c1..f782ff0ba
--- a/tests/test_pipeline.c
+++ b/tests/test_pipeline.c
@@@ -3940,75 -5921,57 +5921,127 @@@ TEST(pipeline_closure_repair_removed_de
PASS();
}
- /* Publication must never write through a name another local process can
- * predict. The staging database used to be "<db>.stage.<pid>.<counter>",
- * which anyone can compute in advance; publication then unlinked that name
- * and wrote it, so a symlink planted in the gap redirects the write to a
- * target of the attacker's choosing.
- *
- * Winning that timing window is not something a test should attempt -- a
- * test that has to win a race is a coin flip, not a gate. The property that
- * REMOVES the window is deterministic: publication must not touch any
- * predictable name at all. Canaries occupy every name the old scheme could
- * have picked, and all must survive. Against the old code exactly one is
- * consumed, whichever serial the counter had reached.
- *
- * This calls cbm_pipeline_publish_generation directly. The only in-tree
- * caller sits behind CBM_INCREMENTAL_TEST_API, so going through the pipeline
- * would never reach the code under test. */
- TEST(pipeline_publication_never_uses_a_predictable_staging_path) {
++/* #2276: the closure/delta route clones the previous generation's db file
++ * byte-for-byte and patches only nodes/edges/hashes/surfaces, so a reindex
++ * that keeps the same project name but a DIFFERENT repo_path (an artifact
++ * moved or reused from another checkout) leaves the `projects` row's
++ * root_path/indexed_at pinned to whichever directory the last FULL rebuild
++ * ran from, so every later root_path-keyed read then resolves files against
++ * a directory this run never touched. */
++TEST(pipeline_incremental_root_path_refreshed_across_directories) {
++ char dir_a[256];
++ char dir_b[256];
++ snprintf(dir_a, sizeof(dir_a), "/tmp/cbm_rootpath_a_XXXXXX");
++ snprintf(dir_b, sizeof(dir_b), "/tmp/cbm_rootpath_b_XXXXXX");
++ ASSERT_NOT_NULL(cbm_mkdtemp(dir_a));
++ ASSERT_NOT_NULL(cbm_mkdtemp(dir_b));
++ closure_probe_repo(dir_a);
++ closure_probe_repo(dir_b);
++ /* Body edit only, same exported names: the shape the closure route
++ * repairs without falling back to a full rebuild (mirrors
++ * pipeline_closure_repair_body_edit_converges_with_fresh_full above). */
++ write_temp_file(dir_b, "lib.ts",
++ "export function closureProbeHelper(x: number): string {\n"
++ " const doubled = x + x;\n"
++ " return String(doubled);\n"
++ "}\n");
++
++ char db[512];
++ snprintf(db, sizeof(db), "%s/rootpath.db", dir_a);
++ static const char *project = "rootpath-probe";
++
++ cbm_pipeline_t *first = cbm_pipeline_new(dir_a, db, CBM_MODE_FULL);
++ ASSERT_NOT_NULL(first);
++ ASSERT_TRUE(cbm_pipeline_set_project_name(first, project));
++ ASSERT_EQ(cbm_pipeline_run(first), 0);
++ cbm_pipeline_free(first);
++
++ /* Reindex the SAME project name from dir_b: a valid existing generation
++ * exists, so this routes through the incremental path, not a first-time
++ * full build. */
++ cbm_pipeline_t *second = cbm_pipeline_new(dir_b, db, CBM_MODE_FULL);
++ ASSERT_NOT_NULL(second);
++ ASSERT_TRUE(cbm_pipeline_set_project_name(second, project));
++ ASSERT_EQ(cbm_pipeline_run(second), 0);
++ cbm_incremental_route_t route = cbm_pipeline_incremental_test_last_route();
++ cbm_pipeline_free(second);
++ /* The bug is specific to the closure/delta route; confirm we actually
++ * exercised it rather than a silent fall-back to a full rebuild, which
++ * would pass even with the fix reverted. */
++ ASSERT_EQ(route, CBM_INCREMENTAL_ROUTE_CLOSURE_REPAIR);
++
++ /* cbm_pipeline_repo_path()/cbm_store_upsert_project() never call
++ * realpath(): the stored root_path is whatever was passed to
++ * cbm_pipeline_new(), verbatim, on both the full-rebuild path and this
++ * one. Compare against dir_b directly, not a canonicalized form, or
++ * this assertion only holds on a platform where /tmp is not itself a
++ * symlink (it resolves to /private/tmp on macOS). */
++ cbm_project_t info = {0};
++ cbm_store_t *store = cbm_store_open_path(db);
++ ASSERT_NOT_NULL(store);
++ ASSERT_EQ(cbm_store_get_project(store, project, &info), CBM_STORE_OK);
++ ASSERT_STR_EQ(info.root_path, dir_b);
++ free((char *)info.name);
++ free((char *)info.indexed_at);
++ free((char *)info.root_path);
++ cbm_store_close(store);
++
++ th_rmtree(dir_a);
++ th_rmtree(dir_b);
++ PASS();
++}
++
+ TEST(pipeline_closure_repair_added_name_declines_to_full) {
char tmp[256];
- snprintf(tmp, sizeof(tmp), "/tmp/cbm_publish_predictable_stage_XXXXXX");
+ snprintf(tmp, sizeof(tmp), "/tmp/cbm_closure_added_XXXXXX");
ASSERT_NOT_NULL(cbm_mkdtemp(tmp));
- char db_path[512];
- snprintf(db_path, sizeof(db_path), "%s/generation.db", tmp);
+ closure_probe_repo(tmp);
+ char db[512];
+ snprintf(db, sizeof(db), "%s/closure.db", tmp);
+ cbm_pipeline_t *baseline = cbm_pipeline_new(tmp, db, CBM_MODE_FULL);
+ ASSERT_NOT_NULL(baseline);
+ ASSERT_EQ(cbm_pipeline_run(baseline), 0);
+ cbm_pipeline_free(baseline);
- enum { PREDICTABLE_CANARIES = 32 };
- static const char canary[] = "canary-must-survive\n";
- char canary_path[PREDICTABLE_CANARIES][640];
- for (int i = 0; i < PREDICTABLE_CANARIES; i++) {
- snprintf(canary_path[i], sizeof(canary_path[i]), "%s.stage.%ld.%d", db_path, (long)getpid(),
- i + 1);
- ASSERT_EQ(th_write_file(canary_path[i], canary), 0);
- }
+ /* A NEW name: yesterday's graph cannot know who would now resolve to
+ * it, so the planner must decline. */
+ write_temp_file(tmp, "lib.ts",
+ "export function closureProbeHelper(x: number): string {\n"
+ " return String(x);\n"
+ "}\n"
+ "export function closureProbeExtra(): number {\n"
+ " return 1;\n"
+ "}\n");
+ cbm_pipeline_t *incr = cbm_pipeline_new(tmp, db, CBM_MODE_FULL);
+ ASSERT_NOT_NULL(incr);
+ ASSERT_EQ(cbm_pipeline_run(incr), 0);
+ cbm_incremental_route_t route = cbm_pipeline_incremental_test_last_route();
+ cbm_pipeline_free(incr);
+ th_rmtree(tmp);
+ ASSERT_EQ(route, CBM_INCREMENTAL_ROUTE_FORCED_FULL);
+ PASS();
+ }
- cbm_gbuf_t *gb = cbm_gbuf_new("predictable-stage-proj", tmp);
- ASSERT_NOT_NULL(gb);
- cbm_pipeline_generation_t generation = {
- .gbuf = gb,
- .final_db_path = db_path,
- .project = "predictable-stage-proj",
- .cancelled = NULL,
- .manifest = NULL,
- .manifest_count = 0,
- .adr_content = NULL,
- .coverage = NULL,
- .coverage_count = 0,
- };
- int publish_rc = cbm_pipeline_publish_generation(&generation);
- cbm_gbuf_free(gb);
+ TEST(pipeline_closure_repair_new_file_declines_to_full) {
+ char tmp[256];
+ snprintf(tmp, sizeof(tmp), "/tmp/cbm_closure_newfile_XXXXXX");
+ ASSERT_NOT_NULL(cbm_mkdtemp(tmp));
+ closure_probe_repo(tmp);
+ char db[512];
+ snprintf(db, sizeof(db), "%s/closure.db", tmp);
+ cbm_pipeline_t *baseline = cbm_pipeline_new(tmp, db, CBM_MODE_FULL);
+ ASSERT_NOT_NULL(baseline);
+ ASSERT_EQ(cbm_pipeline_run(baseline), 0);
+ cbm_pipeline_free(baseline);
- int survived = 0;
- int intact = 0;
- for (int i = 0; i < PREDICTABLE_CANARIES; i++) {
- FILE *f = cbm_fopen(canary_path[i], "rb");
- if (!f) {
- continue;
- }
- survived++;
- char buf[64] = {0};
- size_t n = fread(buf, 1, sizeof(buf) - 1, f);
- (void)fclose(f);
- if (n == strlen(canary) && memcmp(buf, canary, n) == 0) {
- intact++;
- }
- }
+ write_temp_file(tmp, "newcomer.ts", "export function closureNewcomer(): void {}\n");
+ cbm_pipeline_t *incr = cbm_pipeline_new(tmp, db, CBM_MODE_FULL);
+ ASSERT_NOT_NULL(incr);
+ ASSERT_EQ(cbm_pipeline_run(incr), 0);
+ cbm_incremental_route_t route = cbm_pipeline_incremental_test_last_route();
+ cbm_pipeline_free(incr);
th_rmtree(tmp);
-
- ASSERT_EQ(publish_rc, 0);
- /* Not one may be unlinked, truncated, or written through. */
- ASSERT_EQ(survived, PREDICTABLE_CANARIES);
- ASSERT_EQ(intact, PREDICTABLE_CANARIES);
+ ASSERT_EQ(route, CBM_INCREMENTAL_ROUTE_FORCED_FULL);
PASS();
}