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.
C aiderastclaude-codecode-analysiscode-intelligencecodexcursorcypherdeveloper-toolsgemini-cligraph-visualizationkilocodeknowledge-graphmcpmcp-servermodel-context-protocolopencodesqlitetree-sitterwindsurf
Merge origin/main into codex/bt-240-truthful-index-freshness
Martin Vogelc1f30fdcc3c6
diff --cc src/pipeline/pipeline.cindex 7016dfe62,64bc57dba..e492c2b8d--- a/src/pipeline/pipeline.c+++ b/src/pipeline/pipeline.c@@@ -3694,7 -4163,8 +4167,9 @@@ static int dump_and_persist_hashes(cbm_.ignored_files_total = p->ignored_total,.coverage_version = CBM_SEMANTIC_INDEX_VERSION,.hash_records_complete = true,+ .unresolved_calls_complete =+ pipeline_unresolved_capture_complete(p, coverage_rows_available),+ .indexed_checkout_sha = p->git_ctx.head_sha,},.surface_rows = p->surface_rows,.surface_row_count = p->surface_row_count,diff --cc src/pipeline/pipeline_incremental.cindex 9383c647b,ac2c253af..4e96e732a--- a/src/pipeline/pipeline_incremental.c+++ b/src/pipeline/pipeline_incremental.c@@@ -3695,7 -3856,8 +3856,9 @@@ static int run_closure_delta(cbm_pipeli.ignored_files_total = run_ignored_total,.coverage_version = CBM_SEMANTIC_INDEX_VERSION,.hash_records_complete = true,+ .unresolved_calls_complete = incremental_capture_complete(+ coverage_rows_available, unresolved_complete, retained_unresolved_complete),+ .indexed_checkout_sha = cbm_pipeline_indexed_checkout_sha(p),},.surface_rows = NULL,.surface_row_count = 0,@@@ -4402,7 -4541,8 +4542,9 @@@ int cbm_pipeline_run_incremental(cbm_pi.ignored_files_total = run_ignored_total,.coverage_version = CBM_SEMANTIC_INDEX_VERSION,.hash_records_complete = true,+ .unresolved_calls_complete = incremental_capture_complete(+ coverage_rows_available, unresolved_complete, retained_unresolved_complete),+ .indexed_checkout_sha = cbm_pipeline_indexed_checkout_sha(p),};/* Publish surfaces: the surviving previous rows plus this run's fresh* ones (closure route). The legacy test route publishes none — itsdiff --cc src/store/store.cindex ed8462d5a,ed06236a9..c76fd3625--- a/src/store/store.c+++ b/src/store/store.c@@@ -483,15 -493,8 +513,16 @@@ static int init_schema(cbm_store_t *s)" ignored_files_stored INTEGER NOT NULL DEFAULT 0,"" ignored_files_total INTEGER NOT NULL DEFAULT 0,"" coverage_version INTEGER NOT NULL DEFAULT 1,"- " hash_records_complete INTEGER NOT NULL DEFAULT 0"+ " hash_records_complete INTEGER NOT NULL DEFAULT 0,"+ " unresolved_calls_complete INTEGER NOT NULL DEFAULT 0"+ ");"+ /* BT-240: the checkout an index was built from. A table of its own,+ * not an index_coverage_meta column: it is source identity, which the+ * pinned graph-digest tables deliberately do not bind, and a new+ * table needs no migration of existing databases. */+ "CREATE TABLE IF NOT EXISTS index_checkout ("+ " project TEXT PRIMARY KEY REFERENCES projects(name) ON DELETE CASCADE,"+ " indexed_checkout_sha TEXT NOT NULL"");";int rc = exec_sql(s, ddl);@@@ -4764,34 -4838,114 +4867,142 @@@ static int cov_rebuild_shadow_graph(cbmreturn CBM_STORE_OK;}+/* Record (or, with no sha, clear) the checkout a project's index was built+ * from. Runs inside the caller's coverage transaction, so a failed replace+ * keeps the previous identity. */+static int index_checkout_write(cbm_store_t *s, const char *project, const char *sha) {+ bool record = sha && sha[0];+ sqlite3_stmt *stmt = NULL;+ if (sqlite3_prepare_v2(s->db,+ record ? "INSERT INTO index_checkout (project, indexed_checkout_sha) "+ "VALUES (?1, ?2) ON CONFLICT(project) DO UPDATE SET "+ "indexed_checkout_sha=?2;"+ : "DELETE FROM index_checkout WHERE project = ?1;",+ CBM_NOT_FOUND, &stmt, NULL) != SQLITE_OK) {+ store_set_error_sqlite(s, "index checkout write prepare");+ return CBM_STORE_ERR;+ }+ bind_text(stmt, SKIP_ONE, project);+ if (record) {+ bind_text(stmt, ST_COL_2, sha);+ }+ int rc = sqlite3_step(stmt);+ sqlite3_finalize(stmt);+ if (rc != SQLITE_DONE) {+ store_set_error_sqlite(s, "index checkout write");+ return CBM_STORE_ERR;+ }+ return CBM_STORE_OK;+}++ /* Materialize once at publication, after replacement and deleted-file pruning.+ * json_each sees a safe marker array for malformed/legacy non-array payloads.+ * The side table cascades with its authoritative coverage rows. */+ static int coverage_index_unresolved_candidates(cbm_store_t *s, const char *project) {+ static const char sql[] =+ "INSERT OR IGNORE INTO index_unresolved_candidates(project, candidate, rel_path) "+ "SELECT c.project, CASE WHEN j.type != 'object' OR "+ "json_extract(CASE WHEN j.type = 'object' THEN j.value ELSE '{}' END, '$.truncated') = 1 "+ "THEN '' "+ "ELSE json_extract(CASE WHEN j.type = 'object' THEN j.value ELSE '{}' END, '$.candidate') "+ "END, c.rel_path "+ "FROM index_coverage c, json_each(CASE "+ "WHEN json_valid(c.detail) THEN CASE WHEN json_type(c.detail) = 'array' "+ "THEN c.detail ELSE '[{\"truncated\":true}]' END "+ "ELSE '[{\"truncated\":true}]' END) j "+ "WHERE c.project = ?1 AND c.kind = 'unresolved_calls' AND "+ "(j.type != 'object' OR json_extract(CASE WHEN j.type = 'object' THEN j.value ELSE '{}' "+ "END, '$.truncated') = 1 OR "+ "(json_type(CASE WHEN j.type = 'object' THEN j.value ELSE '{}' END, '$.candidate') = "+ "'text' AND "+ "length(json_extract(CASE WHEN j.type = 'object' THEN j.value ELSE '{}' END, "+ "'$.candidate')) > 0));";+ sqlite3_stmt *stmt = NULL;+ if (sqlite3_prepare_v2(s->db, sql, CBM_NOT_FOUND, &stmt, NULL) != SQLITE_OK) {+ store_set_error_sqlite(s, "unresolved candidate index prepare");+ return CBM_STORE_ERR;+ }+ bind_text(stmt, SKIP_ONE, project);+ int rc = sqlite3_step(stmt);+ sqlite3_finalize(stmt);+ if (rc != SQLITE_DONE) {+ store_set_error_sqlite(s, "unresolved candidate index");+ return CBM_STORE_ERR;+ }+ return CBM_STORE_OK;+ }++ /* The caller owns the coverage transaction and rolls it back on failure. */+ static int coverage_write_metadata(cbm_store_t *s, const char *project,+ const cbm_coverage_meta_t *meta) {+ enum { COVERAGE_META_INITIAL_VERSION = 1 };+ if (!meta) {+ sqlite3_stmt *del_meta = NULL;+ if (sqlite3_prepare_v2(s->db, "DELETE FROM index_coverage_meta WHERE project = ?1;",+ CBM_NOT_FOUND, &del_meta, NULL) != SQLITE_OK) {+ store_set_error_sqlite(s, "coverage meta delete prepare");+ return CBM_STORE_ERR;+ }+ bind_text(del_meta, SKIP_ONE, project);+ int meta_rc = sqlite3_step(del_meta);+ sqlite3_finalize(del_meta);+ if (meta_rc != SQLITE_DONE) {+ store_set_error_sqlite(s, "coverage meta delete");+ return CBM_STORE_ERR;+ }- return CBM_STORE_OK;++ return index_checkout_write(s, project, NULL);+ }+ char recorded_at[CBM_SZ_64];+ if (meta->recorded_at && meta->recorded_at[0]) {+ (void)snprintf(recorded_at, sizeof(recorded_at), "%s", meta->recorded_at);+ } else {+ iso_now(recorded_at, sizeof(recorded_at));+ }+ const char *generation =+ meta->generation && meta->generation[0] ? meta->generation : recorded_at;+ const char *index_mode = meta->index_mode && meta->index_mode[0] ? meta->index_mode : "unknown";+ const char *recording_status = meta->recording_status && meta->recording_status[0]+ ? meta->recording_status+ : "unavailable";+ int ignored_stored = meta->ignored_files_stored > 0 ? meta->ignored_files_stored : 0;+ int ignored_total = meta->ignored_files_total > 0 ? meta->ignored_files_total : 0;+ int coverage_version =+ meta->coverage_version > 0 ? meta->coverage_version : COVERAGE_META_INITIAL_VERSION;++ sqlite3_stmt *up_meta = NULL;+ if (sqlite3_prepare_v2(s->db,+ "INSERT INTO index_coverage_meta "+ "(project, generation, index_mode, recorded_at, recording_status, "+ " ignored_files_stored, ignored_files_total, coverage_version, "+ " hash_records_complete, unresolved_calls_complete) "+ "VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10) "+ "ON CONFLICT(project) DO UPDATE SET generation=?2, index_mode=?3, "+ "recorded_at=?4, recording_status=?5, ignored_files_stored=?6, "+ "ignored_files_total=?7, coverage_version=?8, hash_records_complete=?9, "+ "unresolved_calls_complete=?10;",+ CBM_NOT_FOUND, &up_meta, NULL) != SQLITE_OK) {+ store_set_error_sqlite(s, "coverage meta upsert prepare");+ return CBM_STORE_ERR;+ }+ bind_text(up_meta, SKIP_ONE, project);+ bind_text(up_meta, ST_COL_2, generation);+ bind_text(up_meta, ST_COL_3, index_mode);+ bind_text(up_meta, CBM_SZ_4, recorded_at);+ bind_text(up_meta, CBM_SZ_5, recording_status);+ sqlite3_bind_int(up_meta, ST_COL_6, ignored_stored);+ sqlite3_bind_int(up_meta, ST_COL_7, ignored_total);+ sqlite3_bind_int(up_meta, ST_COL_8, coverage_version);+ sqlite3_bind_int(up_meta, ST_COL_9, meta->hash_records_complete);+ sqlite3_bind_int(up_meta, ST_COL_10, meta->unresolved_calls_complete);+ int meta_rc = sqlite3_step(up_meta);+ sqlite3_finalize(up_meta);+ if (meta_rc != SQLITE_DONE) {+ store_set_error_sqlite(s, "coverage meta upsert");+ return CBM_STORE_ERR;+ }- return CBM_STORE_OK;++ return index_checkout_write(s, project, meta->indexed_checkout_sha);+ }+int cbm_store_coverage_replace_ex(cbm_store_t *s, const char *project,const cbm_coverage_row_t *rows, int count,const cbm_coverage_meta_t *meta) {diff --cc src/store/store.hindex 43f1a21f0,eed6f746d..c3dc5613c--- a/src/store/store.h+++ b/src/store/store.h@@@ -790,11 -804,7 +804,12 @@@ typedef structint ignored_files_total;int coverage_version;bool hash_records_complete;+ bool unresolved_calls_complete;+ /* Git checkout identity frozen at the successful staged-generation+ * boundary (BT-240), stored in the index_checkout table. NULL/"" when the+ * run had no git context (non-git repo) or the DB predates that table.+ * Owned by the row: meta_get allocates it, meta_clear frees it. */+ const char *indexed_checkout_sha;} cbm_coverage_meta_t;/* Replace the project's coverage rows in one transaction, then prune rows fordiff --cc tests/test_mcp.cindex dde95310d,cdc3c5541..1d8881e6d--- a/tests/test_mcp.c+++ b/tests/test_mcp.c@@@ -6970,466 -7839,204 +7840,660 @@@ TEST(tool_index_status_includes_git_metPASS();}-/* ══════════════════════════════════════════════════════════════════- * TOOL HANDLERS WITH DATA- * ══════════════════════════════════════════════════════════════════ */+/* BT-240 RED: a live checkout SHA is not proof of the generation that produced+ * graph content. Older databases lack an indexed-checkout identity, so verbose+ * status must fail closed as unknown while exposing graph generation+ * separately. */+TEST(tool_index_status_fails_closed_without_indexed_checkout_identity) {+ char tmp[256];+ cbm_mcp_server_t *srv = setup_snippet_server(tmp, sizeof(tmp));+ ASSERT_NOT_NULL(srv);++ char *resp = cbm_mcp_handle_tool(+ srv, "index_status",+ "{\"project\":\"test-project\",\"verbose\":true,\"format\":\"json\"}");+ ASSERT_NOT_NULL(resp);+ char *inner = extract_text_content(resp);+ ASSERT_NOT_NULL(inner);+ ASSERT_NOT_NULL(strstr(inner, "\"freshness\""));+ ASSERT_NOT_NULL(strstr(inner, "\"verdict\":\"unknown\""));+ ASSERT_NOT_NULL(strstr(inner, "\"indexed_generation\""));+ ASSERT_NOT_NULL(strstr(inner, "\"indexed_checkout_sha\":null"));+ ASSERT_NOT_NULL(strstr(inner, "indexed_checkout_unavailable"));++ free(inner);+ free(resp);+ cbm_mcp_server_free(srv);+ cleanup_snippet_dir(tmp);+ PASS();+}++/* BT-240 lean-default guard: the freshness verdict is diagnostics — a+ * report-only signal that never auto-indexes — so it must stay out of the+ * common (non-verbose) status call. */+TEST(tool_index_status_omits_freshness_by_default) {+ char tmp[256];+ cbm_mcp_server_t *srv = setup_snippet_server(tmp, sizeof(tmp));+ ASSERT_NOT_NULL(srv);++ char *resp = cbm_mcp_handle_tool(+ srv, "index_status", "{\"project\":\"test-project\",\"format\":\"json\"}");+ ASSERT_NOT_NULL(resp);+ char *inner = extract_text_content(resp);+ ASSERT_NOT_NULL(inner);+ ASSERT_NOT_NULL(strstr(inner, "\"status\""));+ ASSERT_NULL(strstr(inner, "\"freshness\""));+ ASSERT_NULL(strstr(inner, "indexed_checkout_sha"));+ /* BT-240 increment 3: the worktree-status snapshot is part of the+ * verbose-only freshness block and must not leak into the default call. */+ ASSERT_NULL(strstr(inner, "status_available"));+ ASSERT_NULL(strstr(inner, "tracked_changes"));+ ASSERT_NULL(strstr(inner, "untracked_source"));++ free(inner);+ free(resp);+ cbm_mcp_server_free(srv);+ cleanup_snippet_dir(tmp);+ PASS();+}++/* ── BT-240: freshness verdict against the recorded indexed checkout ──+ * These shell out to git via cbm_popen (the same isolated spawn production+ * git_context uses) rather than system(), so they run on every platform that+ * has git on PATH — Windows CI included. A genuinely missing git is the only+ * skip. */++static int mcp_git_run(const char *dir, const char *args) {+ char cmd[1024];+#ifdef _WIN32+ const char *null_dev = "NUL";+#else+ const char *null_dev = "/dev/null";+#endif+ snprintf(cmd, sizeof(cmd), "git -C \"%s\" %s 2>%s", dir, args, null_dev);+ FILE *fp = cbm_popen(cmd, "r");+ if (!fp) {+ return -1;+ }+ char drain[256];+ while (fgets(drain, sizeof(drain), fp)) {+ }+ return cbm_pclose(fp);+}++static int mcp_make_git_repo(const char *dir) {+ if (th_mkdir_p(dir) != 0) return -1;+ if (mcp_git_run(dir, "init -q") != 0) return -1;+ if (mcp_git_run(dir, "config user.email [email protected]") != 0) return -1;+ if (mcp_git_run(dir, "config user.name Test") != 0) return -1;+ if (th_write_file(TH_PATH(dir, ".keep"), "") != 0) return -1;+ if (mcp_git_run(dir, "add -A") != 0) return -1;+ if (mcp_git_run(dir, "commit -qm init") != 0) return -1;+ return 0;+}++/* Helper: build a server whose project root is the given git repo and whose+ * coverage metadata records indexed_checkout_sha = sha (may be NULL). */+static cbm_mcp_server_t *mcp_freshness_server(const char *repo, const char *sha) {+ cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL);+ if (!srv) return NULL;+ cbm_store_t *st = cbm_mcp_server_store(srv);+ if (!st) {+ cbm_mcp_server_free(srv);+ return NULL;+ }+ cbm_mcp_server_set_project(srv, "fresh-project");+ cbm_store_upsert_project(st, "fresh-project", repo);+ cbm_coverage_meta_t meta = {+ .generation = "fresh-generation",+ .index_mode = "fast",+ .recorded_at = "2026-08-11T00:00:00Z",+ .recording_status = "complete",+ .coverage_version = 1,+ .hash_records_complete = true,+ .indexed_checkout_sha = sha,+ };+ if (cbm_store_coverage_replace_ex(st, "fresh-project", NULL, 0, &meta) != CBM_STORE_OK) {+ cbm_mcp_server_free(srv);+ return NULL;+ }+ return srv;+}++TEST(tool_index_status_freshness_verdict_current_when_indexed_checkout_matches) {+ char *repo = th_mktempdir("cbm_fresh_git");+ if (!repo) FAIL("th_mktempdir returned NULL");+ if (mcp_make_git_repo(repo) != 0) {+ th_rmtree(repo);+ SKIP_PLATFORM("git not available to init a repo");+ }+ cbm_git_context_t ctx = {0};+ if (cbm_git_context_resolve(repo, &ctx) != 0 || !ctx.head_sha || !ctx.head_sha[0]) {+ cbm_git_context_free(&ctx);+ th_rmtree(repo);+ SKIP_PLATFORM("git head not resolvable");+ }+ char *head = strdup(ctx.head_sha);+ cbm_git_context_free(&ctx);++ cbm_mcp_server_t *srv = mcp_freshness_server(repo, head);+ if (!srv) {+ free(head);+ th_rmtree(repo);+ FAIL("mcp_freshness_server failed");+ }++ char *resp = cbm_mcp_handle_tool(+ srv, "index_status",+ "{\"project\":\"fresh-project\",\"verbose\":true,\"format\":\"json\"}");+ ASSERT_NOT_NULL(resp);+ char *inner = extract_text_content(resp);+ ASSERT_NOT_NULL(inner);+ ASSERT_NOT_NULL(strstr(inner, "\"verdict\":\"current\""));+ ASSERT_NOT_NULL(strstr(inner, "indexed_checkout_current"));+ ASSERT_NOT_NULL(strstr(inner, "\"indexed_checkout_sha\":\""));+ ASSERT_NOT_NULL(strstr(inner, "\"checkout_sha\":\""));+ ASSERT_NOT_NULL(strstr(inner, "\"recommended_action\":\"use_graph\""));+ ASSERT_NOT_NULL(strstr(inner, "\"status_available\":true"));+ ASSERT_NOT_NULL(strstr(inner, "\"tracked_changes\":"));+ ASSERT_NOT_NULL(strstr(inner, "\"untracked_source\":"));+ ASSERT_NOT_NULL(strstr(inner, "\"count\":0"));++ free(inner);+ free(resp);+ cbm_mcp_server_free(srv);+ free(head);+ th_rmtree(repo);+ PASS();+}++TEST(tool_index_status_freshness_verdict_stale_when_indexed_checkout_mismatches) {+ char *repo = th_mktempdir("cbm_fresh_git");+ if (!repo) FAIL("th_mktempdir returned NULL");+ if (mcp_make_git_repo(repo) != 0) {+ th_rmtree(repo);+ SKIP_PLATFORM("git not available to init a repo");+ }++ /* A stale recorded identity that does not equal the live HEAD. */+ cbm_mcp_server_t *srv = mcp_freshness_server(repo, "0000000000000000000000000000000000000000");+ if (!srv) {+ th_rmtree(repo);+ FAIL("mcp_freshness_server failed");+ }++ char *resp = cbm_mcp_handle_tool(+ srv, "index_status",+ "{\"project\":\"fresh-project\",\"verbose\":true,\"format\":\"json\"}");+ ASSERT_NOT_NULL(resp);+ char *inner = extract_text_content(resp);+ ASSERT_NOT_NULL(inner);+ ASSERT_NOT_NULL(strstr(inner, "\"verdict\":\"stale\""));+ ASSERT_NOT_NULL(strstr(inner, "indexed_checkout_mismatch"));+ ASSERT_NOT_NULL(strstr(inner, "\"indexed_checkout_sha\":\"0000000000000000000000000000000000000000\""));+ ASSERT_NOT_NULL(strstr(inner, "\"checkout_sha\":\""));+ ASSERT_NOT_NULL(strstr(inner, "\"recommended_action\":\"reindex_to_match_checkout\""));++ free(inner);+ free(resp);+ cbm_mcp_server_free(srv);+ th_rmtree(repo);+ PASS();+}++/* BT-240 increment 3: a modified tracked file makes the verdict stale even+ * when the indexed SHA matches the live HEAD, with the sample surfaced. */+TEST(tool_index_status_freshness_verdict_stale_when_tracked_changes) {+ char *repo = th_mktempdir("cbm_fresh_git");+ if (!repo) FAIL("th_mktempdir returned NULL");+ if (mcp_make_git_repo(repo) != 0) {+ th_rmtree(repo);+ SKIP_PLATFORM("git not available to init a repo");+ }+ if (th_write_file(TH_PATH(repo, "tracked.txt"), "v1") != 0 ||+ mcp_git_run(repo, "add -f -- tracked.txt") != 0 ||+ mcp_git_run(repo, "commit -q -m add-tracked") != 0) {+ th_rmtree(repo);+ FAIL("failed to commit tracked file");+ }++ cbm_git_context_t ctx = {0};+ if (cbm_git_context_resolve(repo, &ctx) != 0 || !ctx.head_sha || !ctx.head_sha[0]) {+ cbm_git_context_free(&ctx);+ th_rmtree(repo);+ SKIP_PLATFORM("git head not resolvable");+ }+ char *head = strdup(ctx.head_sha);+ cbm_git_context_free(&ctx);++ if (th_write_file(TH_PATH(repo, "tracked.txt"), "v2") != 0) {+ free(head);+ th_rmtree(repo);+ FAIL("failed to modify tracked file");+ }++ cbm_mcp_server_t *srv = mcp_freshness_server(repo, head);+ if (!srv) {+ free(head);+ th_rmtree(repo);+ FAIL("mcp_freshness_server failed");+ }++ char *resp = cbm_mcp_handle_tool(+ srv, "index_status",+ "{\"project\":\"fresh-project\",\"verbose\":true,\"format\":\"json\"}");+ ASSERT_NOT_NULL(resp);+ char *inner = extract_text_content(resp);+ ASSERT_NOT_NULL(inner);+ ASSERT_NOT_NULL(strstr(inner, "\"verdict\":\"stale\""));+ ASSERT_NOT_NULL(strstr(inner, "tracked_changes_present"));+ ASSERT_NOT_NULL(strstr(inner, "\"tracked_changes\":"));+ ASSERT_NOT_NULL(strstr(inner, "\"count\":1"));+ ASSERT_NOT_NULL(strstr(inner, "\"paths\":[\"tracked.txt\"]"));+ ASSERT_NOT_NULL(strstr(inner, "\"truncated\":false"));+ ASSERT_NULL(strstr(inner, "indexed_checkout_current"));++ free(inner);+ free(resp);+ cbm_mcp_server_free(srv);+ free(head);+ th_rmtree(repo);+ PASS();+}++/* BT-240 increment 3: untracked sources prevent "current" — verdict is+ * unknown (no stale reason), with the untracked_not_indexed reason. */+TEST(tool_index_status_freshness_verdict_unknown_when_untracked_only) {+ char *repo = th_mktempdir("cbm_fresh_git");+ if (!repo) FAIL("th_mktempdir returned NULL");+ if (mcp_make_git_repo(repo) != 0) {+ th_rmtree(repo);+ SKIP_PLATFORM("git not available to init a repo");+ }+ if (th_write_file(TH_PATH(repo, "newfile.txt"), "n") != 0) {+ th_rmtree(repo);+ FAIL("failed to create untracked file");+ }++ cbm_git_context_t ctx = {0};+ if (cbm_git_context_resolve(repo, &ctx) != 0 || !ctx.head_sha || !ctx.head_sha[0]) {+ cbm_git_context_free(&ctx);+ th_rmtree(repo);+ SKIP_PLATFORM("git head not resolvable");+ }+ char *head = strdup(ctx.head_sha);+ cbm_git_context_free(&ctx);++ cbm_mcp_server_t *srv = mcp_freshness_server(repo, head);+ if (!srv) {+ free(head);+ th_rmtree(repo);+ FAIL("mcp_freshness_server failed");+ }++ char *resp = cbm_mcp_handle_tool(+ srv, "index_status",+ "{\"project\":\"fresh-project\",\"verbose\":true,\"format\":\"json\"}");+ ASSERT_NOT_NULL(resp);+ char *inner = extract_text_content(resp);+ ASSERT_NOT_NULL(inner);+ ASSERT_NOT_NULL(strstr(inner, "\"verdict\":\"unknown\""));+ ASSERT_NOT_NULL(strstr(inner, "untracked_not_indexed"));+ ASSERT_NOT_NULL(strstr(inner, "\"untracked_source\":"));+ ASSERT_NOT_NULL(strstr(inner, "\"count\":1"));+ ASSERT_NULL(strstr(inner, "indexed_checkout_current"));++ free(inner);+ free(resp);+ cbm_mcp_server_free(srv);+ free(head);+ th_rmtree(repo);+ PASS();+}++/* BT-240 increment 3: tracked changes win over untracked — both reasons are+ * present but the verdict is stale, never unknown. */+TEST(tool_index_status_freshness_verdict_stale_when_tracked_and_untracked) {+ char *repo = th_mktempdir("cbm_fresh_git");+ if (!repo) FAIL("th_mktempdir returned NULL");+ if (mcp_make_git_repo(repo) != 0) {+ th_rmtree(repo);+ SKIP_PLATFORM("git not available to init a repo");+ }+ if (th_write_file(TH_PATH(repo, "tracked.txt"), "v1") != 0 ||+ mcp_git_run(repo, "add -f -- tracked.txt") != 0 ||+ mcp_git_run(repo, "commit -q -m add-tracked") != 0) {+ th_rmtree(repo);+ FAIL("failed to commit tracked file");+ }++ cbm_git_context_t ctx = {0};+ if (cbm_git_context_resolve(repo, &ctx) != 0 || !ctx.head_sha || !ctx.head_sha[0]) {+ cbm_git_context_free(&ctx);+ th_rmtree(repo);+ SKIP_PLATFORM("git head not resolvable");+ }+ char *head = strdup(ctx.head_sha);+ cbm_git_context_free(&ctx);++ if (th_write_file(TH_PATH(repo, "tracked.txt"), "v2") != 0 ||+ th_write_file(TH_PATH(repo, "untracked.txt"), "n") != 0) {+ free(head);+ th_rmtree(repo);+ FAIL("failed to prepare mixed worktree");+ }++ cbm_mcp_server_t *srv = mcp_freshness_server(repo, head);+ if (!srv) {+ free(head);+ th_rmtree(repo);+ FAIL("mcp_freshness_server failed");+ }++ char *resp = cbm_mcp_handle_tool(+ srv, "index_status",+ "{\"project\":\"fresh-project\",\"verbose\":true,\"format\":\"json\"}");+ ASSERT_NOT_NULL(resp);+ char *inner = extract_text_content(resp);+ ASSERT_NOT_NULL(inner);+ ASSERT_NOT_NULL(strstr(inner, "\"verdict\":\"stale\""));+ ASSERT_NOT_NULL(strstr(inner, "tracked_changes_present"));+ ASSERT_NOT_NULL(strstr(inner, "untracked_not_indexed"));++ free(inner);+ free(resp);+ cbm_mcp_server_free(srv);+ free(head);+ th_rmtree(repo);+ PASS();+}++/* BT-240 increment 3: a non-git root with a recorded identity cannot prove+ * current — status availability must be false and the verdict unknown. */+TEST(tool_index_status_freshness_status_unavailable_non_git) {+ char *repo = th_mktempdir("cbm_fresh_non_git");+ if (!repo) FAIL("th_mktempdir returned NULL");++ /* Record an identity even though the root is not a git repository. */+ cbm_mcp_server_t *srv = mcp_freshness_server(repo, "0123456789012345678901234567890123456789");+ if (!srv) {+ th_rmtree(repo);+ FAIL("mcp_freshness_server failed");+ }++ char *resp = cbm_mcp_handle_tool(+ srv, "index_status",+ "{\"project\":\"fresh-project\",\"verbose\":true,\"format\":\"json\"}");+ ASSERT_NOT_NULL(resp);+ char *inner = extract_text_content(resp);+ ASSERT_NOT_NULL(inner);+ ASSERT_NOT_NULL(strstr(inner, "\"status_available\":false"));+ ASSERT_NOT_NULL(strstr(inner, "status_unavailable"));+ ASSERT_NOT_NULL(strstr(inner, "\"verdict\":\"unknown\""));+ ASSERT_NULL(strstr(inner, "indexed_checkout_current"));+ ASSERT_NOT_NULL(strstr(inner, "\"tracked_changes\":"));+ ASSERT_NOT_NULL(strstr(inner, "\"untracked_source\":"));+ ASSERT_NOT_NULL(strstr(inner, "\"count\":0"));++ free(inner);+ free(resp);+ cbm_mcp_server_free(srv);+ th_rmtree(repo);+ PASS();+}++/* BT-240 increment 3: sample cap at the MCP layer — many untracked sources+ * surface the true count, a bounded paths array and truncated=true. */+TEST(tool_index_status_freshness_sample_cap_truncates) {+ char *repo = th_mktempdir("cbm_fresh_git");+ if (!repo) FAIL("th_mktempdir returned NULL");+ if (mcp_make_git_repo(repo) != 0) {+ th_rmtree(repo);+ SKIP_PLATFORM("git not available to init a repo");+ }+ enum { N_FILES = 17, MCP_SAMPLE_MAX = 16 };+ for (int i = 0; i < N_FILES; i++) {+ char path[1024];+ snprintf(path, sizeof(path), "%s/u%02d.txt", repo, i);+ if (th_write_file(path, "u") != 0) {+ th_rmtree(repo);+ FAIL("failed to create untracked file");+ }+ }++ cbm_git_context_t ctx = {0};+ if (cbm_git_context_resolve(repo, &ctx) != 0 || !ctx.head_sha || !ctx.head_sha[0]) {+ cbm_git_context_free(&ctx);+ th_rmtree(repo);+ SKIP_PLATFORM("git head not resolvable");+ }+ char *head = strdup(ctx.head_sha);+ cbm_git_context_free(&ctx);++ cbm_mcp_server_t *srv = mcp_freshness_server(repo, head);+ if (!srv) {+ free(head);+ th_rmtree(repo);+ FAIL("mcp_freshness_server failed");+ }++ char *resp = cbm_mcp_handle_tool(+ srv, "index_status",+ "{\"project\":\"fresh-project\",\"verbose\":true,\"format\":\"json\"}");+ ASSERT_NOT_NULL(resp);+ char *inner = extract_text_content(resp);+ ASSERT_NOT_NULL(inner);+ ASSERT_NOT_NULL(strstr(inner, "\"untracked_source\":"));+ ASSERT_NOT_NULL(strstr(inner, "\"count\":17"));+ ASSERT_NOT_NULL(strstr(inner, "\"truncated\":true"));+ static const char EXPECTED_PATHS[] =+ "\"paths\":[\"u00.txt\",\"u01.txt\",\"u02.txt\",\"u03.txt\",\"u04.txt\",\"u05.txt\","+ "\"u06.txt\",\"u07.txt\",\"u08.txt\",\"u09.txt\",\"u10.txt\",\"u11.txt\",\"u12.txt\","+ "\"u13.txt\",\"u14.txt\",\"u15.txt\"]";+ ASSERT_NOT_NULL(strstr(inner, EXPECTED_PATHS));++ free(inner);+ free(resp);+ cbm_mcp_server_free(srv);+ free(head);+ th_rmtree(repo);+ PASS();+}+ /* #2816: stored recursion must remain visible even when the CALLS graph has+ * no self-edges. Removing metadata emission must fail these assertions. */+ TEST(tool_trace_path_recursion_metadata_issue2816) {+ cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL);+ ASSERT_NOT_NULL(srv);+ cbm_store_t *st = cbm_mcp_server_store(srv);+ const char *proj = "recursion-proj";+ cbm_mcp_server_set_project(srv, proj);+ ASSERT_EQ(cbm_store_upsert_project(st, proj, "/tmp/recursion"), CBM_STORE_OK);+ const struct {+ const char *name;+ const char *props;+ const char *self;+ const char *recursive;+ } cases[] = {+ {"countdown", "{\"self_recursive\":true,\"recursive\":true}", "true", "true"},+ {"visit", "{\"self_recursive\":true,\"recursive\":true}", "true", "true"},+ {"mutual", "{\"self_recursive\":false,\"recursive\":true}", "false", "true"},+ {"leaf", "{\"self_recursive\":false,\"recursive\":false}", "false", "false"},+ {"legacy", "{}", "null", "null"},+ {"partial", "{\"self_recursive\":true}", "true", "null"},+ {"invalid", "{\"self_recursive\":\"true\",\"recursive\":1}", "null", "null"},+ };+ for (size_t i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) {+ char qn[128];+ snprintf(qn, sizeof(qn), "%s.%s", proj, cases[i].name);+ cbm_node_t n = {.project = proj,+ .label = i == 1 ? "Method" : "Function",+ .name = cases[i].name,+ .qualified_name = qn,+ .file_path = "recursion.ts",+ .start_line = 1,+ .end_line = 5,+ .properties_json = cases[i].props};+ ASSERT_GT(cbm_store_upsert_node(st, &n), 0);+ for (int json = 0; json < 2; json++) {+ for (int inbound = 0; inbound < 2; inbound++) {+ char args[512];+ snprintf(args, sizeof(args),+ "{\"project\":\"%s\",\"function_name\":\"%s\","+ "\"direction\":\"%s\",\"format\":\"%s\"}",+ proj, cases[i].name, inbound ? "inbound" : "outbound",+ json ? "json" : "tree");+ char *response = cbm_mcp_handle_tool(srv, "trace_path", args);+ char *inner = extract_text_content(response);+ ASSERT_NOT_NULL(inner);+ char expected[128];+ snprintf(expected, sizeof(expected),+ json ? "\"self_recursive\":%s" : "self_recursive: %s\n", cases[i].self);+ ASSERT_NOT_NULL(strstr(inner, expected));+ snprintf(expected, sizeof(expected), json ? "\"recursive\":%s" : "recursive: %s\n",+ cases[i].recursive);+ ASSERT_NOT_NULL(strstr(inner, expected));+ ASSERT_NOT_NULL(strstr(inner, json ? "\"self_call_policy\":\"omitted\""+ : "self_call_policy: omitted\n"));+ snprintf(expected, sizeof(expected), json ? "\"%s_total\":0" : "%s_total: 0\n",+ inbound ? "callers" : "callees");+ ASSERT_NOT_NULL(strstr(inner, expected));+ free(inner);+ free(response);+ }+ }+ }+ cbm_mcp_server_free(srv);+ PASS();+ }++ TEST(tool_trace_path_recursion_union_and_edges_issue2816) {+ cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL);+ ASSERT_NOT_NULL(srv);+ cbm_store_t *st = cbm_mcp_server_store(srv);+ const char *proj = "recursion-union";+ cbm_mcp_server_set_project(srv, proj);+ ASSERT_EQ(cbm_store_upsert_project(st, proj, "/tmp/recursion-union"), CBM_STORE_OK);+ cbm_node_t def = {.project = proj,+ .label = "Function",+ .name = "countdown",+ .qualified_name = "recursion-union.impl.countdown",+ .file_path = "impl.ts",+ .start_line = 1,+ .end_line = 10,+ .properties_json = "{\"self_recursive\":false,\"recursive\":false}"};+ int64_t def_id = cbm_store_upsert_node(st, &def);+ ASSERT_GT(def_id, 0);+ cbm_node_t stub = def;+ stub.qualified_name = "recursion-union.stub.countdown";+ stub.file_path = "stub.d.ts";+ stub.end_line = 1;+ stub.properties_json = "{\"self_recursive\":true,\"recursive\":true}";+ ASSERT_GT(cbm_store_upsert_node(st, &stub), 0);+ cbm_node_t leaf = def;+ leaf.name = "leaf";+ leaf.qualified_name = "recursion-union.impl.leaf";+ leaf.properties_json = "{\"self_recursive\":true,\"recursive\":true}";+ int64_t leaf_id = cbm_store_upsert_node(st, &leaf);+ ASSERT_GT(leaf_id, 0);+ cbm_edge_t edge = {.project = proj, .source_id = def_id, .target_id = leaf_id, .type = "CALLS"};+ ASSERT_GT(cbm_store_insert_edge(st, &edge), 0);+ const char *expected_flags[] = {"true", "null", "false", "true"};+ for (int state = 0; state < 4; state++) {+ def.properties_json = state == 3 ? "{\"self_recursive\":true,\"recursive\":true}"+ : "{\"self_recursive\":false,\"recursive\":false}";+ stub.properties_json = state == 0 ? "{\"self_recursive\":true,\"recursive\":true}"+ : state == 2 ? "{\"self_recursive\":false,\"recursive\":false}"+ : "{}";+ ASSERT_GT(cbm_store_upsert_node(st, &def), 0);+ ASSERT_GT(cbm_store_upsert_node(st, &stub), 0);+ for (int json = 0; json < 2; json++) {+ char args[512];+ snprintf(args, sizeof(args),+ "{\"project\":\"%s\",\"function_name\":\"countdown\","+ "\"direction\":\"outbound\",\"format\":\"%s\"}",+ proj, json ? "json" : "tree");+ char *response = cbm_mcp_handle_tool(srv, "trace_path", args);+ char *inner = extract_text_content(response);+ ASSERT_NOT_NULL(inner);+ ASSERT_NOT_NULL(strstr(inner, json ? "\"callees_total\":1" : "callees_total: 1\n"));+ ASSERT_NOT_NULL(strstr(inner, json ? "\"leaf\"" : ".leaf"));+ char expected[128];+ snprintf(expected, sizeof(expected),+ json ? "\"self_recursive\":%s" : "self_recursive: %s\n",+ expected_flags[state]);+ ASSERT_NOT_NULL(strstr(inner, expected));+ snprintf(expected, sizeof(expected), json ? "\"recursive\":%s" : "recursive: %s\n",+ expected_flags[state]);+ ASSERT_NOT_NULL(strstr(inner, expected));+ free(inner);+ free(response);+ }+ }+ /* Recursion is root metadata, but the omission policy concerns CALLS only. */+ char *response =+ cbm_mcp_handle_tool(srv, "trace_path",+ "{\"project\":\"recursion-union\",\"function_name\":\"countdown\","+ "\"edge_types\":[\"IMPORTS\"],\"format\":\"json\"}");+ char *inner = extract_text_content(response);+ ASSERT_NOT_NULL(inner);+ ASSERT_NOT_NULL(strstr(inner, "\"self_recursive\":true"));+ ASSERT_NULL(strstr(inner, "self_call_policy"));+ free(inner);+ free(response);+ cbm_mcp_server_free(srv);+ PASS();+ }++ TEST(tool_trace_path_recursion_survives_budget_floor_issue2816) {+ cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL);+ ASSERT_NOT_NULL(srv);+ cbm_store_t *st = cbm_mcp_server_store(srv);+ const char *proj = "recursion-budget";+ cbm_mcp_server_set_project(srv, proj);+ ASSERT_EQ(cbm_store_upsert_project(st, proj, "/tmp/recursion-budget"), CBM_STORE_OK);+ char huge[4096];+ memset(huge, 'x', sizeof(huge) - 1);+ huge[sizeof(huge) - 1] = '\0';+ cbm_node_t n = {.project = proj,+ .label = "Function",+ .name = "countdown",+ .qualified_name = "recursion-budget.countdown",+ .file_path = "recursion.ts",+ .start_line = 1,+ .end_line = 10,+ .properties_json = "{\"self_recursive\":true,\"recursive\":true}"};+ int64_t root_id = cbm_store_upsert_node(st, &n);+ ASSERT_GT(root_id, 0);+ n.name = huge;+ n.qualified_name = huge;+ int64_t callee_id = cbm_store_upsert_node(st, &n);+ ASSERT_GT(callee_id, 0);+ cbm_edge_t edge = {+ .project = proj, .source_id = root_id, .target_id = callee_id, .type = "CALLS"};+ ASSERT_GT(cbm_store_insert_edge(st, &edge), 0);+ for (int json = 0; json < 2; json++) {+ char args[512];+ snprintf(args, sizeof(args),+ "{\"project\":\"%s\",\"function_name\":\"countdown\","+ "\"direction\":\"outbound\",\"format\":\"%s\",\"max_output_tokens\":128}",+ proj, json ? "json" : "tree");+ char *response = cbm_mcp_handle_tool(srv, "trace_path", args);+ char *inner = extract_text_content(response);+ ASSERT_NOT_NULL(inner);+ ASSERT_NOT_NULL(strstr(inner, "output_budget"));+ ASSERT_NULL(strstr(inner, huge));+ ASSERT_NOT_NULL(strstr(inner, json ? "\"self_recursive\":true" : "self_recursive: true\n"));+ ASSERT_NOT_NULL(strstr(inner, json ? "\"recursive\":true" : "recursive: true\n"));+ ASSERT_NOT_NULL(strstr(inner, json ? "\"self_call_policy\":\"omitted\""+ : "self_call_policy: omitted\n"));+ free(inner);+ free(response);+ }+ cbm_mcp_server_free(srv);+ PASS();+ }+TEST(tool_trace_call_path_not_found) {cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL);@@@ -25541,19 -26891,19 +27348,28 @@@ SUITE(mcp)RUN_TEST(tool_check_index_coverage_rejects_stale_generation);RUN_TEST(tool_check_index_coverage_freshness_uses_indexer_mtime_source_issue1714);RUN_TEST(tool_check_index_coverage_requires_source_when_file_metadata_changed);+ RUN_TEST(tool_check_index_coverage_freshness_verifies_content_issue2585);+ RUN_TEST(tool_get_code_snippet_withholds_source_whose_content_drifted_issue2585);+ #ifdef CBM_ENABLE_TEST_SEAMS+ RUN_TEST(tool_get_code_snippet_serves_the_bytes_it_verified_issue2585);+ #endif+ RUN_TEST(tool_responses_carry_store_generation_issue2585);RUN_TEST(tool_check_index_coverage_surfaces_lookup_errors);RUN_TEST(tool_index_status_includes_git_metadata);+ RUN_TEST(tool_index_status_fails_closed_without_indexed_checkout_identity);+ RUN_TEST(tool_index_status_omits_freshness_by_default);+ RUN_TEST(tool_index_status_freshness_verdict_current_when_indexed_checkout_matches);+ RUN_TEST(tool_index_status_freshness_verdict_stale_when_indexed_checkout_mismatches);+ RUN_TEST(tool_index_status_freshness_verdict_stale_when_tracked_changes);+ RUN_TEST(tool_index_status_freshness_verdict_unknown_when_untracked_only);+ RUN_TEST(tool_index_status_freshness_verdict_stale_when_tracked_and_untracked);+ RUN_TEST(tool_index_status_freshness_status_unavailable_non_git);+ RUN_TEST(tool_index_status_freshness_sample_cap_truncates);/* Tool handlers with validation */+ RUN_TEST(tool_trace_path_recursion_metadata_issue2816);+ RUN_TEST(tool_trace_path_recursion_union_and_edges_issue2816);+ RUN_TEST(tool_trace_path_recursion_survives_budget_floor_issue2816);RUN_TEST(tool_trace_call_path_not_found);RUN_TEST(tool_call_invalid_project_name_leaves_no_corrupt_litter_issue1425);RUN_TEST(tool_trace_missing_function_name);diff --cc tests/test_store_graph_digest.cindex d2add2adf,64617a96e..1065e1339--- a/tests/test_store_graph_digest.c+++ b/tests/test_store_graph_digest.c@@@ -594,46 -683,9 +683,48 @@@ TEST(store_graph_digest_concurrent_isolcbm_store_close(a);cbm_store_close(b);PASS();}+/* BT-240 (#1561): the indexed checkout is source identity, which the digest+ * never binds, so it lives outside the digested tables. A store written through+ * the real coverage API, with and without a checkout SHA, keeps passing the+ * live-schema check (test-impact falls back to running everything otherwise),+ * and recording or changing the SHA leaves the digest unchanged. */+TEST(store_graph_digest_indexed_checkout_identity_is_not_bound) {+ cbm_store_t *s = cbm_store_open_memory();+ ASSERT_NOT_NULL(s);+ ASSERT_EQ(cbm_store_upsert_project(s, "p", "/live"), CBM_STORE_OK);+ cbm_coverage_meta_t meta = {.generation = "g",+ .index_mode = "full",+ .recorded_at = "t",+ .recording_status = "complete",+ .coverage_version = 1,+ .hash_records_complete = true};+ cbm_store_graph_digest_t none = {0}, first = {0}, second = {0};+ ASSERT_EQ(cbm_store_coverage_replace_ex(s, "p", NULL, 0, &meta), CBM_STORE_OK);+ bool none_ok = gd_p(s, &none);+ meta.indexed_checkout_sha = "0123456789abcdef0123456789abcdef01234567";+ ASSERT_EQ(cbm_store_coverage_replace_ex(s, "p", NULL, 0, &meta), CBM_STORE_OK);+ bool first_ok = gd_p(s, &first);+ meta.indexed_checkout_sha = "89abcdef0123456789abcdef0123456789abcdef";+ ASSERT_EQ(cbm_store_coverage_replace_ex(s, "p", NULL, 0, &meta), CBM_STORE_OK);+ bool second_ok = gd_p(s, &second);+ cbm_coverage_meta_t got = {0};+ int got_rc = cbm_store_coverage_meta_get(s, "p", &got);+ bool sha_kept = got_rc == CBM_STORE_OK && got.indexed_checkout_sha &&+ strcmp(got.indexed_checkout_sha, "89abcdef0123456789abcdef0123456789abcdef") == 0;+ cbm_store_coverage_meta_clear(&got);+ cbm_store_close(s);+ ASSERT_TRUE(none_ok);+ ASSERT_TRUE(first_ok);+ ASSERT_TRUE(second_ok);+ ASSERT_TRUE(sha_kept);+ ASSERT_TRUE(gd_equal(&none, &first));+ ASSERT_TRUE(gd_equal(&first, &second));+ PASS();+}+SUITE(store_graph_digest) {+ RUN_TEST(store_graph_digest_binds_unresolved_candidate_index);+ RUN_TEST(store_graph_digest_candidate_index_errors_are_not_legacy_absence);RUN_TEST(store_graph_digest_independent_canonical_golden);RUN_TEST(store_graph_digest_logical_order_and_excluded_storage_are_stable);RUN_TEST(store_graph_digest_every_known_value_and_cross_project_edge_is_bound);diff --cc tests/test_store_nodes.cindex 67e7a7e36,5d005c00e..88b37882e--- a/tests/test_store_nodes.c+++ b/tests/test_store_nodes.c@@@ -2603,75 -2603,109 +2603,178 @@@ TEST(store_coverage_replace_rolls_back_PASS();}+ /* Copy store_meta's shadow fingerprint for `project` into `out` ("" if absent). */+ static void coverage_shadow_fp(cbm_store_t *s, const char *project, char *out, size_t cap) {+ out[0] = '\0';+ char key[256];+ snprintf(key, sizeof(key), "coverage_shadow_fp:%s", project);+ sqlite3_stmt *stmt = NULL;+ if (sqlite3_prepare_v2(cbm_store_get_db(s), "SELECT v FROM store_meta WHERE k = ?1;", -1, &stmt,+ NULL) != SQLITE_OK) {+ return;+ }+ sqlite3_bind_text(stmt, 1, key, -1, SQLITE_TRANSIENT);+ if (sqlite3_step(stmt) == SQLITE_ROW && sqlite3_column_text(stmt, 0)) {+ snprintf(out, cap, "%s", (const char *)sqlite3_column_text(stmt, 0));+ }+ sqlite3_finalize(stmt);+ }++ /* #2265: unresolved_calls rows describe files that WERE indexed, so they stay+ * out of the "<project>::missed" graph like by-design not_indexed_* rows:+ * (a) a project whose only rows are unresolved_calls gets no shadow graph,+ * (b) a file with parse_partial AND unresolved_calls keeps its parse_partial+ * File node (the later unresolved_calls row used to replace it), and+ * (c) the shadow fingerprint ignores unresolved_calls rows, so the skip gate+ * and store_meta match a project without them. */+ TEST(store_coverage_shadow_excludes_unresolved_calls) {+ cbm_store_t *s = cbm_store_open_memory();+ ASSERT_NOT_NULL(s);+ const char *only = "cov-unres-only";+ const char *mixed = "cov-unres-mixed";+ const char *plain = "cov-unres-plain";+ ASSERT_EQ(cbm_store_upsert_project(s, only, "/tmp/cov-unres-only"), CBM_STORE_OK);+ ASSERT_EQ(cbm_store_upsert_project(s, mixed, "/tmp/cov-unres-mixed"), CBM_STORE_OK);+ ASSERT_EQ(cbm_store_upsert_project(s, plain, "/tmp/cov-unres-plain"), CBM_STORE_OK);+ ASSERT_EQ(cbm_store_upsert_file_hash(s, only, "src/a.go", "", 1, 1), CBM_STORE_OK);+ ASSERT_EQ(cbm_store_upsert_file_hash(s, only, "src/b.go", "", 2, 2), CBM_STORE_OK);+ ASSERT_EQ(cbm_store_upsert_file_hash(s, mixed, "src/both.go", "", 3, 3), CBM_STORE_OK);+ ASSERT_EQ(cbm_store_upsert_file_hash(s, mixed, "lib/calls.go", "", 4, 4), CBM_STORE_OK);+ ASSERT_EQ(cbm_store_upsert_file_hash(s, plain, "src/both.go", "", 3, 3), CBM_STORE_OK);++ /* (a) Only unresolved_calls (plus a by-design row): no shadow project. */+ cbm_coverage_row_t only_rows[] = {+ {.rel_path = "src/a.go", .kind = "unresolved_calls", .detail = "[{\"candidate\":\"a.f\"}]"},+ {.rel_path = "src/b.go", .kind = "unresolved_calls", .detail = "[{\"candidate\":\"b.g\"}]"},+ {.rel_path = "vendor", .kind = "not_indexed_dir", .detail = "excluded subtree"},+ };+ ASSERT_EQ(cbm_store_coverage_replace(s, only, only_rows, 3), CBM_STORE_OK);+ cbm_coverage_row_t *got = NULL;+ int n = 0;+ ASSERT_EQ(cbm_store_coverage_get(s, only, &got, &n), CBM_STORE_OK);+ ASSERT_EQ(n, 3); /* the table keeps the evidence; only the view skips it */+ cbm_store_free_coverage(got, n);+ ASSERT_EQ(cbm_store_count_nodes(s, "cov-unres-only::missed"), 0);+ cbm_project_t shadow = {0};+ ASSERT_EQ(cbm_store_get_project(s, "cov-unres-only::missed", &shadow), CBM_STORE_NOT_FOUND);+ cbm_project_free_fields(&shadow);++ /* (b) parse_partial + unresolved_calls on one path, and a second file+ * with unresolved_calls only: the shadow holds exactly the parse_partial+ * File node under src/. */+ cbm_coverage_row_t mixed_rows[] = {+ {.rel_path = "src/both.go", .kind = "parse_partial", .detail = "3-4"},+ {.rel_path = "src/both.go",+ .kind = "unresolved_calls",+ .detail = "[{\"candidate\":\"both.h\"}]"},+ {.rel_path = "lib/calls.go",+ .kind = "unresolved_calls",+ .detail = "[{\"candidate\":\"calls.k\"}]"},+ };+ ASSERT_EQ(cbm_store_coverage_replace(s, mixed, mixed_rows, 3), CBM_STORE_OK);+ cbm_node_t *nodes = NULL;+ int nc = 0;+ ASSERT_EQ(cbm_store_find_nodes_by_label(s, "cov-unres-mixed::missed", "File", &nodes, &nc),+ CBM_STORE_OK);+ ASSERT_EQ(nc, 1);+ ASSERT_STR_EQ(nodes[0].file_path, "src/both.go");+ ASSERT_NOT_NULL(strstr(nodes[0].properties_json, "\"kind\":\"parse_partial\""));+ ASSERT_NOT_NULL(strstr(nodes[0].properties_json, "\"detail\":\"3-4\""));+ ASSERT_NULL(strstr(nodes[0].properties_json, "unresolved_calls"));+ cbm_store_free_nodes(nodes, nc);+ nodes = NULL;+ nc = 0;+ ASSERT_EQ(cbm_store_find_nodes_by_label(s, "cov-unres-mixed::missed", "Folder", &nodes, &nc),+ CBM_STORE_OK);+ ASSERT_EQ(nc, 1);+ ASSERT_STR_EQ(nodes[0].name, "src");+ cbm_store_free_nodes(nodes, nc);++ /* (c) Same failure rows without the unresolved_calls evidence: the stored+ * fingerprints match. */+ cbm_coverage_row_t plain_row = {+ .rel_path = "src/both.go", .kind = "parse_partial", .detail = "3-4"};+ ASSERT_EQ(cbm_store_coverage_replace(s, plain, &plain_row, 1), CBM_STORE_OK);+ char fp_mixed[128];+ char fp_plain[128];+ coverage_shadow_fp(s, mixed, fp_mixed, sizeof(fp_mixed));+ coverage_shadow_fp(s, plain, fp_plain, sizeof(fp_plain));+ ASSERT_TRUE(fp_plain[0] != '\0');+ ASSERT_STR_EQ(fp_mixed, fp_plain);++ cbm_store_close(s);+ PASS();+ }++TEST(store_coverage_meta_indexed_checkout_sha_roundtrip_and_rollback) {+ cbm_store_t *s = cbm_store_open_memory();+ ASSERT_NOT_NULL(s);+ ASSERT_EQ(cbm_store_upsert_project(s, "coverage-sha", "/tmp/coverage-sha"), CBM_STORE_OK);+ ASSERT_EQ(cbm_store_upsert_file_hash(s, "coverage-sha", "keep.c", "", 1, 1), CBM_STORE_OK);+ cbm_coverage_row_t row = {.rel_path = "keep.c", .kind = "parse_partial", .detail = "1-2"};++ /* NULL indexed_checkout_sha (non-git run) persists as NULL and reads back+ * as no identity. */+ cbm_coverage_meta_t no_sha_meta = {+ .generation = "gen-0",+ .index_mode = "full",+ .recording_status = "complete",+ .coverage_version = 1,+ .hash_records_complete = true,+ };+ ASSERT_EQ(cbm_store_coverage_replace_ex(s, "coverage-sha", &row, 1, &no_sha_meta),+ CBM_STORE_OK);+ cbm_coverage_meta_t got = {0};+ ASSERT_EQ(cbm_store_coverage_meta_get(s, "coverage-sha", &got), CBM_STORE_OK);+ ASSERT_NULL(got.indexed_checkout_sha);+ cbm_store_coverage_meta_clear(&got);++ /* Round-trip: git-run identity persists and can be overwritten by the next+ * staged generation. */+ cbm_coverage_meta_t sha_meta = no_sha_meta;+ sha_meta.generation = "gen-1";+ sha_meta.indexed_checkout_sha = "0123456789abcdef0123456789abcdef01234567";+ ASSERT_EQ(cbm_store_coverage_replace_ex(s, "coverage-sha", &row, 1, &sha_meta),+ CBM_STORE_OK);+ got = (cbm_coverage_meta_t){0};+ ASSERT_EQ(cbm_store_coverage_meta_get(s, "coverage-sha", &got), CBM_STORE_OK);+ ASSERT_STR_EQ(got.indexed_checkout_sha, "0123456789abcdef0123456789abcdef01234567");+ ASSERT_STR_EQ(got.generation, "gen-1");+ cbm_store_coverage_meta_clear(&got);++ cbm_coverage_meta_t new_meta = sha_meta;+ new_meta.generation = "gen-2";+ new_meta.indexed_checkout_sha = "abcdef0123456789abcdef0123456789abcdef01";+ ASSERT_EQ(cbm_store_coverage_replace_ex(s, "coverage-sha", &row, 1, &new_meta),+ CBM_STORE_OK);+ got = (cbm_coverage_meta_t){0};+ ASSERT_EQ(cbm_store_coverage_meta_get(s, "coverage-sha", &got), CBM_STORE_OK);+ ASSERT_STR_EQ(got.indexed_checkout_sha, "abcdef0123456789abcdef0123456789abcdef01");+ ASSERT_STR_EQ(got.generation, "gen-2");+ cbm_store_coverage_meta_clear(&got);++ /* Rollback: a failed replace must retain the prior identity + generation. */+ ASSERT_EQ(cbm_store_exec(s, "CREATE TRIGGER fail_sha_missed_insert BEFORE INSERT ON nodes "+ "WHEN NEW.project = 'coverage-sha::missed' "+ "BEGIN SELECT RAISE(ABORT, 'forced sha shadow failure'); END;"),+ CBM_STORE_OK);+ cbm_coverage_meta_t failed_meta = new_meta;+ failed_meta.generation = "gen-must-not-commit";+ failed_meta.indexed_checkout_sha = "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef";+ cbm_coverage_row_t failed_row = row;+ failed_row.detail = "changed-to-force-shadow-rebuild";+ ASSERT_EQ(cbm_store_coverage_replace_ex(s, "coverage-sha", &failed_row, 1, &failed_meta),+ CBM_STORE_ERR);+ got = (cbm_coverage_meta_t){0};+ ASSERT_EQ(cbm_store_coverage_meta_get(s, "coverage-sha", &got), CBM_STORE_OK);+ ASSERT_STR_EQ(got.generation, "gen-2");+ ASSERT_STR_EQ(got.indexed_checkout_sha, "abcdef0123456789abcdef0123456789abcdef01");+ cbm_store_coverage_meta_clear(&got);++ cbm_store_close(s);+ PASS();+}+SUITE(store_nodes) {RUN_TEST(store_qn_suffix_is_literal);RUN_TEST(store_qn_suffix_is_unbounded);@@@ -2681,7 -2715,7 +2784,8 @@@RUN_TEST(store_coverage_meta_zero_row_truncation_and_delete);RUN_TEST(store_coverage_replace_rejects_invalid_row_arguments);RUN_TEST(store_coverage_replace_rolls_back_when_shadow_rebuild_fails);+ RUN_TEST(store_coverage_shadow_excludes_unresolved_calls);+ RUN_TEST(store_coverage_meta_indexed_checkout_sha_roundtrip_and_rollback);RUN_TEST(sql_label_allowlists_match_cbm_label_is_type_like);RUN_TEST(sql_relation_labels_match_cbm_label_is_relation);RUN_TEST(store_open_memory);