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/adr-reindex-race

Martin Vogel8fae39e29463
diff --cc tests/test_pipeline.c
index 2de3ad451,c8c49a468..9db2b026e
--- a/tests/test_pipeline.c
+++ b/tests/test_pipeline.c
@@@ -770,213 -770,255 +770,462 @@@ TEST(pipeline_adr_survives_full_reindex
PASS();
}
+ static int pipeline_unresolved_review_case(int padding, bool spill) {
+ char tmp[256] = "/tmp/cbm_unresolved_review_XXXXXX";
+ ASSERT_NOT_NULL(cbm_mkdtemp(tmp));
+ char db[512], path[512];
+ const char *names[] = {"cliente.js", "servicio.js", "ayudante.js",
+ "directo.js", "missing.js", "route.js"};
+ const char *sources[] = {
+ "export function crearCliente() { function buscar(id) { return id; } return { buscar }; "
+ "}\n",
+ "export function crearServicio({ cliente }) {\n"
+ " function procesar(id) {\n"
+ " cliente.buscar(id); return cliente.buscar(id + 1);\n"
+ " }\n return { procesar };\n}\n",
+ "export function ayudante(id) { return id; }\n",
+ "import { ayudante } from './ayudante.js';\n"
+ "export function usarDirecto(id) { ayudante(id); return ayudante(id + 1); }\n",
+ "import { absent } from './ayudante.js';\n"
+ "export function missing(id) { return absent(id); }\n",
+ "export function setup({ app }) { app.get('/x', handler); }\n"
+ "function handler() { return 1; }\n"};
+ for (size_t i = 0; i < sizeof(names) / sizeof(names[0]); i++) {
+ snprintf(path, sizeof(path), "%s/%s", tmp, names[i]);
+ FILE *f = fopen(path, "w");
+ ASSERT_NOT_NULL(f);
+ fputs(sources[i], f);
+ fclose(f);
+ }
+ for (int i = 0; i < padding; i++) {
+ snprintf(path, sizeof(path), "%s/pad%d.js", tmp, i);
+ FILE *f = fopen(path, "w");
+ ASSERT_NOT_NULL(f);
+ fprintf(f, "export function pad%d() { return %d; }\n", i, i);
+ fclose(f);
+ }
+ snprintf(db, sizeof(db), "%s/index.db", tmp);
+ if (spill)
+ cbm_setenv("CBM_MEM_SPILL", "1", 1);
+ cbm_pipeline_t *p = cbm_pipeline_new(tmp, db, CBM_MODE_FULL);
+ ASSERT_NOT_NULL(p);
+ int rc = cbm_pipeline_run(p);
+ if (spill)
+ cbm_unsetenv("CBM_MEM_SPILL");
+ ASSERT_EQ(rc, 0);
+ char project[256];
+ snprintf(project, sizeof(project), "%s", cbm_pipeline_project_name(p));
+ cbm_pipeline_free(p);
+ cbm_store_t *st = cbm_store_open_path(db);
+ ASSERT_NOT_NULL(st);
+ cbm_coverage_row_t *rows = NULL;
+ int count = 0;
+ ASSERT_EQ(cbm_store_coverage_get_path(st, project, "directo.js", &rows, &count), CBM_STORE_OK);
+ for (int i = 0; i < count; i++)
+ ASSERT_FALSE(strcmp(rows[i].kind, "unresolved_calls") == 0);
+ cbm_store_free_coverage(rows, count);
+ rows = NULL;
+ count = 0;
+ ASSERT_EQ(cbm_store_coverage_get_path(st, project, "route.js", &rows, &count), CBM_STORE_OK);
+ for (int i = 0; i < count; i++)
+ ASSERT_FALSE(strcmp(rows[i].kind, "unresolved_calls") == 0);
+ cbm_store_free_coverage(rows, count);
+ rows = NULL;
+ count = 0;
+ ASSERT_EQ(cbm_store_coverage_get_path(st, project, "servicio.js", &rows, &count), CBM_STORE_OK);
+ ASSERT_EQ(count, 1);
+ yyjson_doc *doc = yyjson_read(rows[0].detail, strlen(rows[0].detail), 0);
+ ASSERT_NOT_NULL(doc);
+ yyjson_val *sites = yyjson_doc_get_root(doc);
+ ASSERT_EQ(yyjson_arr_size(sites), 2);
+ for (size_t i = 0; i < 2; i++) {
+ yyjson_val *site = yyjson_arr_get(sites, i);
+ const char *caller = yyjson_get_str(yyjson_obj_get(site, "caller"));
+ ASSERT_NOT_NULL(caller);
+ ASSERT_NOT_NULL(strstr(caller, ".procesar"));
+ ASSERT_EQ(yyjson_get_int(yyjson_obj_get(site, "line")), 3);
+ ASSERT_NOT_NULL(yyjson_get_str(yyjson_obj_get(site, "candidate")));
+ }
+ yyjson_doc_free(doc);
+ cbm_store_free_coverage(rows, count);
+ rows = NULL;
+ count = 0;
+ ASSERT_EQ(cbm_store_coverage_get_path(st, project, "missing.js", &rows, &count), CBM_STORE_OK);
+ ASSERT_EQ(count, 1);
+ doc = yyjson_read(rows[0].detail, strlen(rows[0].detail), 0);
+ ASSERT_NOT_NULL(doc);
+ ASSERT_EQ(yyjson_arr_size(yyjson_doc_get_root(doc)), 1);
+ yyjson_doc_free(doc);
+ cbm_store_free_coverage(rows, count);
+ cbm_store_close(st);
+ rm_rf(tmp);
+ PASS();
+ }
+
+ /* A noisy semantic resolver must retain an explicit bounded-coverage marker,
+ * including when a single reason would exceed the entire detail budget. */
+ TEST(pipeline_unresolved_capture_bounds_count_and_json_bytes) {
+ char tmp[256] = "/tmp/cbm_unresolved_cap_XXXXXX";
+ ASSERT_NOT_NULL(cbm_mkdtemp(tmp));
+ char db[512];
+ snprintf(db, sizeof(db), "%s/index.db", tmp);
+ cbm_pipeline_t *p = cbm_pipeline_new(tmp, db, CBM_MODE_FULL);
+ ASSERT_NOT_NULL(p);
+ CBMResolvedCall *sites = calloc(1001, sizeof(*sites));
+ ASSERT_NOT_NULL(sites);
+ for (int i = 0; i < 1001; i++) {
+ sites[i] = (CBMResolvedCall){.kind = CBM_RESOLVED_INVOCATION,
+ .caller_qn = "cap.run",
+ .callee_qn = "missing",
+ .strategy = "lsp_unresolved",
+ .site_start_byte = (uint32_t)i * 2,
+ .site_end_byte = (uint32_t)i * 2 + 1};
+ }
+ CBMFileResult result = {.resolved_calls = {.items = sites, .count = 1001}};
+ cbm_pipeline_record_unresolved_calls(p, "many.js", &result, NULL);
+ cbm_coverage_row_t *rows = NULL;
+ int count = 0;
+ bool complete = false;
+ cbm_pipeline_get_unresolved_calls(p, &rows, &count, &complete);
+ ASSERT_TRUE(complete);
+ ASSERT_EQ(count, 1);
+ ASSERT_TRUE(strlen(rows[0].detail) <= 128 * 1024);
+ yyjson_doc *doc = yyjson_read(rows[0].detail, strlen(rows[0].detail), 0);
+ ASSERT_NOT_NULL(doc);
+ yyjson_val *array = yyjson_doc_get_root(doc);
+ ASSERT_TRUE(yyjson_arr_size(array) <= 1001);
+ ASSERT_TRUE(yyjson_get_bool(yyjson_obj_get(yyjson_arr_get_last(array), "truncated")));
+ yyjson_doc_free(doc);
+
+ char *reason = malloc(200000);
+ ASSERT_NOT_NULL(reason);
+ memset(reason, '"', 199999);
+ reason[199999] = '\0';
+ sites[0].reason = reason;
+ result.resolved_calls.count = 1;
+ cbm_pipeline_record_unresolved_calls(p, "huge.js", &result, NULL);
+ cbm_pipeline_get_unresolved_calls(p, &rows, &count, &complete);
+ ASSERT_TRUE(complete);
+ ASSERT_EQ(count, 2);
+ ASSERT_TRUE(strlen(rows[1].detail) <= 128 * 1024);
+ doc = yyjson_read(rows[1].detail, strlen(rows[1].detail), 0);
+ ASSERT_NOT_NULL(doc);
+ array = yyjson_doc_get_root(doc);
+ ASSERT_EQ(yyjson_arr_size(array), 1);
+ ASSERT_TRUE(yyjson_get_bool(yyjson_obj_get(yyjson_arr_get_last(array), "truncated")));
+ yyjson_doc_free(doc);
+ free(reason);
+ free(sites);
+ cbm_pipeline_free(p);
+ rm_rf(tmp);
+ PASS();
+ }
+
+ TEST(pipeline_unresolved_review_sequential) {
+ return pipeline_unresolved_review_case(0, false);
+ }
+ TEST(pipeline_unresolved_review_parallel) {
+ return pipeline_unresolved_review_case(55, false);
+ }
+ TEST(pipeline_unresolved_review_spill) {
+ return pipeline_unresolved_review_case(55, true);
+ }
+
+ TEST(pipeline_records_unresolved_injected_call_sites) {
+ char tmp[256] = "/tmp/cbm_unresolved_calls_XXXXXX";
+ ASSERT_NOT_NULL(cbm_mkdtemp(tmp));
+ char db_path[512], path[512];
+ snprintf(db_path, sizeof(db_path), "%s/test.db", tmp);
+ snprintf(path, sizeof(path), "%s/cliente.js", tmp);
+ FILE *f = fopen(path, "w");
+ ASSERT_NOT_NULL(f);
+ fputs("export function crearCliente() { function buscar(id) { return id; } "
+ "return { buscar }; }\n",
+ f);
+ fclose(f);
+ snprintf(path, sizeof(path), "%s/servicio.js", tmp);
+ f = fopen(path, "w");
+ ASSERT_NOT_NULL(f);
+ fputs("export function crearServicio({ cliente }) {\n"
+ " function procesar(id) { return cliente.buscar(id); }\n"
+ " return { procesar };\n}\n",
+ f);
+ fclose(f);
+
+ cbm_pipeline_t *p = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
+ ASSERT_NOT_NULL(p);
+ ASSERT_EQ(cbm_pipeline_run(p), 0);
+ char project[256];
+ snprintf(project, sizeof(project), "%s", cbm_pipeline_project_name(p));
+ cbm_pipeline_free(p);
+ cbm_store_t *st = cbm_store_open_path(db_path);
+ ASSERT_NOT_NULL(st);
+ cbm_coverage_row_t *rows = NULL;
+ int count = 0;
+ ASSERT_EQ(cbm_store_coverage_get_path(st, project, "servicio.js", &rows, &count), CBM_STORE_OK);
+ bool found = false;
+ for (int i = 0; i < count; i++) {
+ if (strcmp(rows[i].kind, "unresolved_calls") == 0 && strstr(rows[i].detail, "buscar") &&
+ strstr(rows[i].detail, "method_not_in_registry")) {
+ found = true;
+ }
+ }
+ ASSERT_TRUE(found);
+ cbm_store_free_coverage(rows, count);
+ cbm_store_close(st);
+
+ /* Reindexing another file must retain this file's diagnostic. */
+ snprintf(path, sizeof(path), "%s/cliente.js", tmp);
+ f = fopen(path, "a");
+ ASSERT_NOT_NULL(f);
+ fputs("\n// unrelated edit\n", f);
+ fclose(f);
+ p = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
+ ASSERT_NOT_NULL(p);
+ ASSERT_EQ(cbm_pipeline_run(p), 0);
+ cbm_pipeline_free(p);
+ st = cbm_store_open_path(db_path);
+ ASSERT_NOT_NULL(st);
+ rows = NULL;
+ count = 0;
+ ASSERT_EQ(cbm_store_coverage_get_path(st, project, "servicio.js", &rows, &count), CBM_STORE_OK);
+ found = false;
+ for (int i = 0; i < count; i++) {
+ found |= strcmp(rows[i].kind, "unresolved_calls") == 0;
+ }
+ ASSERT_TRUE(found);
+ cbm_store_free_coverage(rows, count);
+ cbm_store_close(st);
+
+ /* The edited file must lose its old diagnostic on the next generation. */
+ snprintf(path, sizeof(path), "%s/servicio.js", tmp);
+ f = fopen(path, "w");
+ ASSERT_NOT_NULL(f);
+ fputs(
+ "export function crearServicio({ cliente }) { return { procesar(id) { return id; } }; }\n",
+ f);
+ fclose(f);
+ p = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
+ ASSERT_NOT_NULL(p);
+ ASSERT_EQ(cbm_pipeline_run(p), 0);
+ cbm_pipeline_free(p);
+ st = cbm_store_open_path(db_path);
+ ASSERT_NOT_NULL(st);
+ rows = NULL;
+ count = 0;
+ ASSERT_EQ(cbm_store_coverage_get_path(st, project, "servicio.js", &rows, &count), CBM_STORE_OK);
+ for (int i = 0; i < count; i++) {
+ ASSERT_FALSE(strcmp(rows[i].kind, "unresolved_calls") == 0);
+ }
+ cbm_store_free_coverage(rows, count);
+ cbm_store_close(st);
+/* An ADR written from a test hook BETWEEN the start of a reindex and its
+ * publication survives — the write lands in the "<db>.adr.db" sidecar, which the
+ * graph-DB rebuild never touches (round-2 review item 7a, full route). */
+typedef struct {
+ char db_path[512];
+ char project[256];
+ const char *adr;
+} adr_hook_ctx_t;
+
+static void write_adr_before_final_manifest(void *userdata) {
+ adr_hook_ctx_t *c = (adr_hook_ctx_t *)userdata;
+ cbm_store_t *s = cbm_store_open_path_existing(c->db_path);
+ if (!s) {
+ s = cbm_store_open_path(c->db_path);
+ }
+ if (s) {
+ (void)cbm_store_adr_store(s, c->project, c->adr);
+ cbm_store_close(s);
+ }
+}
+
+TEST(pipeline_adr_written_during_reindex_survives) {
+ char tmp[256];
+ snprintf(tmp, sizeof(tmp), "/tmp/cbm_adr_hook_XXXXXX");
+ if (!cbm_mkdtemp(tmp)) {
+ FAIL("failed to create temp dir");
+ }
+ char db_path[512];
+ snprintf(db_path, sizeof(db_path), "%s/test.db", tmp);
+ char path[512];
+ snprintf(path, sizeof(path), "%s/main.py", tmp);
+ FILE *f = fopen(path, "w");
+ ASSERT_NOT_NULL(f);
+ fprintf(f, "def foo():\n pass\n");
+ fclose(f);
+
+ cbm_pipeline_t *p1 = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
+ ASSERT_NOT_NULL(p1);
+ ASSERT_EQ(cbm_pipeline_run(p1), 0);
+ adr_hook_ctx_t ctx = {.adr = "# Decision\nWritten mid-reindex."};
+ snprintf(ctx.db_path, sizeof(ctx.db_path), "%s", db_path);
+ snprintf(ctx.project, sizeof(ctx.project), "%s", cbm_pipeline_project_name(p1));
+ cbm_pipeline_free(p1);
+
+ /* Force a full reindex, writing the ADR from the publication hook. */
+ for (int i = 0; i < 4; i++) {
+ snprintf(path, sizeof(path), "%s/extra%d.py", tmp, i);
+ f = fopen(path, "w");
+ ASSERT_NOT_NULL(f);
+ fprintf(f, "def g%d():\n return %d\n", i, i);
+ fclose(f);
+ }
+ cbm_pipeline_incremental_test_reset_faults();
+ cbm_pipeline_incremental_test_before_final_manifest_once(write_adr_before_final_manifest, &ctx);
+ cbm_pipeline_t *p2 = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
+ ASSERT_NOT_NULL(p2);
+ ASSERT_EQ(cbm_pipeline_run(p2), 0);
+ /* Pin the route so this stays a full-rebuild test if routing changes. */
+ ASSERT_EQ(cbm_pipeline_incremental_test_last_route(), CBM_INCREMENTAL_ROUTE_FORCED_FULL);
+ cbm_pipeline_free(p2);
+ cbm_pipeline_incremental_test_reset_faults();
+
+ cbm_store_t *s = cbm_store_open_path(db_path);
+ ASSERT_NOT_NULL(s);
+ cbm_adr_t adr = {0};
+ ASSERT_EQ(cbm_store_adr_get(s, ctx.project, &adr), CBM_STORE_OK);
+ ASSERT_NOT_NULL(adr.content);
+ ASSERT_STR_EQ(adr.content, ctx.adr);
+ cbm_store_adr_free(&adr);
+ cbm_store_close(s);
+
+ rm_rf(tmp);
+ PASS();
+}
+
+/* Migrate-before-rebuild fails closed: if the sidecar cannot be opened (here a
+ * DIRECTORY sits at "<db>.adr.db"), the reindex must NOT delete the old
+ * generation — it returns CBM_PIPELINE_ABORT_PRESERVE_DB and the prior graph is
+ * left intact. Deterministic, no hook. */
+TEST(pipeline_adr_migrate_failure_preserves_generation) {
+ char tmp[256];
+ snprintf(tmp, sizeof(tmp), "/tmp/cbm_adr_migfail_XXXXXX");
+ if (!cbm_mkdtemp(tmp)) {
+ FAIL("failed to create temp dir");
+ }
+ char db_path[512];
+ snprintf(db_path, sizeof(db_path), "%s/test.db", tmp);
+ char path[512];
+ snprintf(path, sizeof(path), "%s/main.py", tmp);
+ FILE *f = fopen(path, "w");
+ ASSERT_NOT_NULL(f);
+ fprintf(f, "def foo():\n pass\n");
+ fclose(f);
+
+ cbm_pipeline_t *p1 = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
+ ASSERT_NOT_NULL(p1);
+ ASSERT_EQ(cbm_pipeline_run(p1), 0);
+ char project[256];
+ snprintf(project, sizeof(project), "%s", cbm_pipeline_project_name(p1));
+ cbm_pipeline_free(p1);
+
+ cbm_store_t *before = cbm_store_open_path(db_path);
+ ASSERT_NOT_NULL(before);
+ int nodes_before = cbm_store_count_nodes(before, project);
+ cbm_store_close(before);
+ ASSERT_TRUE(nodes_before > 0);
+
+ /* A directory at "<db>.adr.db" makes the sidecar open (and thus the
+ * migrate-before-delete step) fail deterministically. */
+ char adr_dir[600];
+ snprintf(adr_dir, sizeof(adr_dir), "%s.adr.db", db_path);
+ ASSERT_EQ(cbm_mkdir(adr_dir), 0);
+
+ /* Force a full reindex; migrate-before-delete must fail closed. */
+ for (int i = 0; i < 4; i++) {
+ snprintf(path, sizeof(path), "%s/extra%d.py", tmp, i);
+ f = fopen(path, "w");
+ ASSERT_NOT_NULL(f);
+ fprintf(f, "def g%d():\n return %d\n", i, i);
+ fclose(f);
+ }
+ cbm_pipeline_incremental_test_reset_faults();
+ cbm_pipeline_t *p2 = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
+ ASSERT_NOT_NULL(p2);
+ int rc = cbm_pipeline_run(p2);
+ cbm_pipeline_free(p2);
+ ASSERT_EQ(rc, CBM_PIPELINE_ABORT_PRESERVE_DB);
+
+ /* Old generation intact: the graph DB was not deleted or rebuilt. */
+ ASSERT_TRUE(cbm_file_exists(db_path));
+ cbm_store_t *after = cbm_store_open_path(db_path);
+ ASSERT_NOT_NULL(after);
+ ASSERT_EQ(cbm_store_count_nodes(after, project), nodes_before);
+ cbm_store_close(after);
+
+ rm_rf(tmp);
+ PASS();
+}
+
+/* Upgrade order: a legacy ADR row (pre-sidecar, in the graph DB) is migrated
+ * BEFORE a forced-full reindex deletes the old generation, with NO ADR read
+ * first — the pipeline's migrate-before-rebuild step must move it (item 7b). */
+TEST(pipeline_adr_legacy_migrates_before_forced_reindex) {
+ char tmp[256];
+ snprintf(tmp, sizeof(tmp), "/tmp/cbm_adr_upgrade_XXXXXX");
+ if (!cbm_mkdtemp(tmp)) {
+ FAIL("failed to create temp dir");
+ }
+ char db_path[512];
+ snprintf(db_path, sizeof(db_path), "%s/test.db", tmp);
+ char path[512];
+ snprintf(path, sizeof(path), "%s/main.py", tmp);
+ FILE *f = fopen(path, "w");
+ ASSERT_NOT_NULL(f);
+ fprintf(f, "def foo():\n pass\n");
+ fclose(f);
+
+ cbm_pipeline_t *p1 = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
+ ASSERT_NOT_NULL(p1);
+ ASSERT_EQ(cbm_pipeline_run(p1), 0);
+ char project[256];
+ snprintf(project, sizeof(project), "%s", cbm_pipeline_project_name(p1));
+ cbm_pipeline_free(p1);
+
+ /* Seed the ADR directly into the graph DB's project_summaries (the legacy
+ * layout), bypassing the sidecar entirely. */
+ static const char adr_text[] = "# Decision\nLegacy row, migrated before rebuild.";
+ sqlite3 *raw = NULL;
+ ASSERT_EQ(sqlite3_open(db_path, &raw), SQLITE_OK);
+ sqlite3_exec(raw,
+ "CREATE TABLE IF NOT EXISTS project_summaries (project TEXT PRIMARY KEY,"
+ " summary TEXT NOT NULL, source_hash TEXT NOT NULL, created_at TEXT NOT NULL,"
+ " updated_at TEXT NOT NULL);",
+ NULL, NULL, NULL);
+ char *sql = sqlite3_mprintf(
+ "INSERT OR REPLACE INTO project_summaries VALUES (%Q, %Q, '', '2020-01-01T00:00:00Z',"
+ " '2020-01-01T00:00:00Z');",
+ project, adr_text);
+ ASSERT_NOT_NULL(sql);
+ ASSERT_EQ(sqlite3_exec(raw, sql, NULL, NULL, NULL), SQLITE_OK);
+ sqlite3_free(sql);
+ sqlite3_close(raw);
+
+ /* No ADR read here. Force a full reindex (which deletes the old graph DB). */
+ for (int i = 0; i < 4; i++) {
+ snprintf(path, sizeof(path), "%s/extra%d.py", tmp, i);
+ f = fopen(path, "w");
+ ASSERT_NOT_NULL(f);
+ fprintf(f, "def g%d():\n return %d\n", i, i);
+ fclose(f);
+ }
+ cbm_pipeline_t *p2 = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
+ ASSERT_NOT_NULL(p2);
+ ASSERT_EQ(cbm_pipeline_run(p2), 0);
+ cbm_pipeline_free(p2);
+
+ /* The legacy ADR was migrated to the sidecar before the delete, so it is
+ * still readable. */
+ cbm_store_t *s = cbm_store_open_path(db_path);
+ ASSERT_NOT_NULL(s);
+ cbm_adr_t adr = {0};
+ ASSERT_EQ(cbm_store_adr_get(s, project, &adr), CBM_STORE_OK);
+ ASSERT_NOT_NULL(adr.content);
+ ASSERT_STR_EQ(adr.content, adr_text);
+ cbm_store_adr_free(&adr);
+ cbm_store_close(s);
+
rm_rf(tmp);
PASS();
}