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 codex/bt-240-truthful-index-freshness

Martin Vogelb44a22ca6b26
diff --cc src/mcp/mcp.c
index ec8aa9d3b,f6e43b317..6a7ae02d4
--- a/src/mcp/mcp.c
+++ b/src/mcp/mcp.c
@@@ -6723,162 -7378,40 +7380,196 @@@ static char *handle_check_index_coverag
return result;
}
+ /* index_status "watch" object (#2167): is this project kept fresh by the
+ * background watcher right now, and if not, why. Auto-sync covers the active
+ * MCP session's project only; anything else needs index_repository again. */
+ static void add_watch_status_json(cbm_mcp_server_t *srv, yyjson_mut_doc *doc, yyjson_mut_val *root,
+ const char *project) {
+ cbm_mcp_watch_status_t status = {0};
+ if (srv->watch_status_fn) {
+ srv->watch_status_fn(srv->watch_status_context, project, &status);
+ } else {
+ status.reason = "no_watcher";
+ }
+ yyjson_mut_val *watch = yyjson_mut_obj(doc);
+ yyjson_mut_obj_add_bool(doc, watch, "watched", status.watched);
+ if (status.watched) {
+ yyjson_mut_obj_add_str(doc, watch, "strategy", status.strategy ? status.strategy : "");
+ yyjson_mut_obj_add_int(doc, watch, "poll_interval_ms", status.poll_interval_ms);
+ if (status.last_scan_unix_s > 0) {
+ char when[CBM_SZ_32];
+ time_t t = (time_t)status.last_scan_unix_s;
+ struct tm tm;
+ cbm_gmtime_r(&t, &tm);
+ if (strftime(when, sizeof(when), "%Y-%m-%dT%H:%M:%SZ", &tm) > 0) {
+ yyjson_mut_obj_add_strcpy(doc, watch, "last_scan_at", when);
+ }
+ }
+ } else {
+ yyjson_mut_obj_add_str(doc, watch, "reason", status.reason ? status.reason : "unknown");
+ yyjson_mut_obj_add_str(doc, watch, "hint",
+ "Auto-sync watches only the active MCP session's project; "
+ "re-run index_repository to refresh this index.");
+ }
+ yyjson_mut_obj_add_val(doc, root, "watch", watch);
+ }
+
+/* Emit the bounded worktree-status snapshot inside the freshness block.
+ * Fail-closed: status_available is false whenever git status could not run
+ * clean, in which case the API guarantees zero counts and no samples — a
+ * non-git root or missing git is NEVER presented as a clean worktree. Ignored
+ * paths are deliberately absent (no --ignored in the underlying query) and
+ * must not be confused with tracked/untracked sources. */
+static void add_worktree_status_json(yyjson_mut_doc *doc, yyjson_mut_val *freshness,
+ const cbm_worktree_status_t *st) {
+ yyjson_mut_obj_add_bool(doc, freshness, "status_available", st->available);
+
+ yyjson_mut_val *tracked = yyjson_mut_obj(doc);
+ yyjson_mut_obj_add_int(doc, tracked, "count", st->tracked_count);
+ yyjson_mut_val *tpaths = yyjson_mut_arr(doc);
+ for (int i = 0; i < st->tracked_sample_count; i++) {
+ yyjson_mut_arr_add_strcpy(doc, tpaths, st->tracked_paths[i]);
+ }
+ yyjson_mut_obj_add_val(doc, tracked, "paths", tpaths);
+ yyjson_mut_obj_add_bool(doc, tracked, "truncated", st->tracked_truncated);
+ yyjson_mut_obj_add_val(doc, freshness, "tracked_changes", tracked);
+
+ yyjson_mut_val *untracked = yyjson_mut_obj(doc);
+ yyjson_mut_obj_add_int(doc, untracked, "count", st->untracked_count);
+ yyjson_mut_val *upaths = yyjson_mut_arr(doc);
+ for (int i = 0; i < st->untracked_sample_count; i++) {
+ yyjson_mut_arr_add_strcpy(doc, upaths, st->untracked_paths[i]);
+ }
+ yyjson_mut_obj_add_val(doc, untracked, "paths", upaths);
+ yyjson_mut_obj_add_bool(doc, untracked, "truncated", st->untracked_truncated);
+ yyjson_mut_obj_add_val(doc, freshness, "untracked_source", untracked);
+}
+
+/* BT-240: fail-closed freshness verdict against the indexed-checkout identity
+ * recorded with the DB at the successful staged-generation boundary. A live
+ * checkout SHA is not proof of the generation that produced graph content, so
+ * the verdict comes from that recorded identity, the live git HEAD and the
+ * bounded worktree status (tracked/untracked deltas):
+ * - no indexed SHA → unknown / unavailable
+ * - indexed SHA differs from the live git HEAD → stale / mismatch
+ * - tracked changes present → stale / tracked
+ * - untracked sources present → prevents current
+ * - equal SHA + status available + no changes → current
+ * Report-only: it never triggers indexing. Verbose-only and read-only; the
+ * live checkout is resolved fresh from the project root (never mutated). */
+static void add_index_freshness_json(yyjson_mut_doc *doc, yyjson_mut_val *root, cbm_store_t *store,
+ const char *project, const char *root_path,
+ const char *indexed_generation) {
+ yyjson_mut_val *freshness = yyjson_mut_obj(doc);
+ if (indexed_generation && indexed_generation[0]) {
+ yyjson_mut_obj_add_strcpy(doc, freshness, "indexed_generation", indexed_generation);
+ } else {
+ yyjson_mut_obj_add_str(doc, freshness, "indexed_generation", "");
+ }
+
+ cbm_coverage_meta_t meta = {0};
+ bool have_meta = store && cbm_store_coverage_meta_get(store, project, &meta) == CBM_STORE_OK;
+ const char *indexed_sha = NULL;
+ if (have_meta && meta.indexed_checkout_sha && meta.indexed_checkout_sha[0]) {
+ indexed_sha = meta.indexed_checkout_sha;
+ }
+
+ cbm_git_context_t ctx = {0};
+ (void)cbm_git_context_resolve(root_path, &ctx);
+ const char *checkout_sha = ctx.head_sha && ctx.head_sha[0] ? ctx.head_sha : NULL;
+
+ cbm_worktree_status_t st = {0};
+ int status_rc = cbm_git_worktree_status(root_path, MCP_STATUS_SAMPLE_MAX, &st);
+ bool status_available = status_rc == 0 && st.available;
+ add_worktree_status_json(doc, freshness, &st);
+
+ if (indexed_sha) {
+ yyjson_mut_obj_add_strcpy(doc, freshness, "indexed_checkout_sha", indexed_sha);
+ } else {
+ yyjson_mut_obj_add_null(doc, freshness, "indexed_checkout_sha");
+ }
+ if (checkout_sha) {
+ yyjson_mut_obj_add_strcpy(doc, freshness, "checkout_sha", checkout_sha);
+ } else {
+ yyjson_mut_obj_add_null(doc, freshness, "checkout_sha");
+ }
+
+ /* Verdict/reasons composition. Stale reasons (identity mismatch, tracked
+ * changes) win over everything; a missing indexed identity is unknown
+ * regardless of live state; status unavailability and untracked sources
+ * both prevent "current" but only yield stale when a stale reason is
+ * already present. The reasons array keeps every applicable code in a
+ * stable order; the singular reason stays as the first (dominant) one for
+ * compatibility with consumers that read it directly. */
+ bool sha_present = indexed_sha != NULL;
+ bool live_present = checkout_sha != NULL;
+ bool sha_mismatch = sha_present && live_present && strcmp(indexed_sha, checkout_sha) != 0;
+ bool has_tracked = st.tracked_count > 0;
+ bool has_untracked = st.untracked_count > 0;
+ bool has_stale = sha_mismatch || has_tracked;
+
+ const char *verdict;
+ if (!sha_present) {
+ verdict = "unknown";
+ } else if (has_stale) {
+ verdict = "stale";
+ } else if (!live_present) {
+ verdict = "unknown";
+ } else if (!status_available) {
+ verdict = "unknown";
+ } else if (has_untracked) {
+ verdict = "unknown";
+ } else {
+ verdict = "current";
+ }
+
+ const char *reasons[MCP_REASONS_MAX];
+ int n_reasons = 0;
+ if (!sha_present) {
+ reasons[n_reasons++] = "indexed_checkout_unavailable";
+ }
+ if (sha_mismatch) {
+ reasons[n_reasons++] = "indexed_checkout_mismatch";
+ }
+ if (has_tracked) {
+ reasons[n_reasons++] = "tracked_changes_present";
+ }
+ if (!status_available) {
+ reasons[n_reasons++] = "status_unavailable";
+ }
+ if (has_untracked) {
+ reasons[n_reasons++] = "untracked_not_indexed";
+ }
+ if (n_reasons == 0) {
+ reasons[n_reasons++] = "indexed_checkout_current";
+ }
+ const char *reason = reasons[0];
+
+ const char *recommended_action;
+ if (strcmp(verdict, "current") == 0) {
+ recommended_action = "use_graph";
+ } else if (strcmp(verdict, "stale") == 0) {
+ recommended_action = "reindex_to_match_checkout";
+ } else {
+ recommended_action = "reindex_to_record_indexed_checkout";
+ }
+ yyjson_mut_obj_add_str(doc, freshness, "verdict", verdict);
+ yyjson_mut_obj_add_str(doc, freshness, "reason", reason);
+ yyjson_mut_val *reasons_val = yyjson_mut_arr(doc);
+ for (int i = 0; i < n_reasons; i++) {
+ yyjson_mut_arr_add_str(doc, reasons_val, reasons[i]);
+ }
+ yyjson_mut_obj_add_val(doc, freshness, "reasons", reasons_val);
+ yyjson_mut_obj_add_str(doc, freshness, "recommended_action", recommended_action);
+ yyjson_mut_obj_add_val(doc, root, "freshness", freshness);
+
+ if (have_meta) {
+ cbm_store_coverage_meta_clear(&meta);
+ }
+ cbm_git_worktree_status_free(&st);
+ cbm_git_context_free(&ctx);
+}
+
static char *handle_index_status(cbm_mcp_server_t *srv, const char *args) {
char *project = get_project_arg(args);
cbm_store_t *store = resolve_store(srv, project);
diff --cc src/pipeline/pipeline.c
index 44ddb0598,92e23de64..7016dfe62
--- a/src/pipeline/pipeline.c
+++ b/src/pipeline/pipeline.c
@@@ -460,10 -1194,14 +1194,18 @@@ const char *cbm_pipeline_repo_path(cons
return p ? p->repo_path : NULL;
}
+ const cbm_index_resource_policy_t *cbm_pipeline_resource_policy(const cbm_pipeline_t *p) {
+ return p && cbm_index_policy_enabled(&p->resource_policy) ? &p->resource_policy : NULL;
+ }
+
+ cbm_index_resource_violation_t *cbm_pipeline_resource_violation(cbm_pipeline_t *p) {
+ return p ? &p->resource_violation : NULL;
+ }
+
+const char *cbm_pipeline_indexed_checkout_sha(const cbm_pipeline_t *p) {
+ return p ? p->git_ctx.head_sha : NULL;
+}
+
atomic_int *cbm_pipeline_cancelled_ptr(cbm_pipeline_t *p) {
return p ? p->cancelled : NULL;
}
diff --cc src/pipeline/pipeline_internal.h
index 2983a24ca,06722aca7..6b17d5d32
--- a/src/pipeline/pipeline_internal.h
+++ b/src/pipeline/pipeline_internal.h
@@@ -834,8 -1788,11 +1788,13 @@@ const cbm_doc_candidate_t *cbm_pipeline
/* Pipeline accessors for incremental use */
const char *cbm_pipeline_repo_path(const cbm_pipeline_t *p);
+ const cbm_test_declarations_t *cbm_pipeline_test_declarations(const cbm_pipeline_t *p);
+ /* Without an active run snapshot this only validates fresh's source state. */
+ bool cbm_pipeline_project_source_matches(const cbm_pipeline_t *p, const cbm_userconfig_t *fresh);
+ const cbm_index_resource_policy_t *cbm_pipeline_resource_policy(const cbm_pipeline_t *p);
+ cbm_index_resource_violation_t *cbm_pipeline_resource_violation(cbm_pipeline_t *p);
+/* Borrowed checkout identity captured and validated for this pipeline run. */
+const char *cbm_pipeline_indexed_checkout_sha(const cbm_pipeline_t *p);
atomic_int *cbm_pipeline_cancelled_ptr(cbm_pipeline_t *p);
/* Record committed graph size (#334 gate axis) from the incremental path,
* which cannot see the opaque cbm_pipeline struct. Call before the dump. */
diff --cc src/store/store.c
index 7bb721bfd,d19e52890..2471d00d7
--- a/src/store/store.c
+++ b/src/store/store.c
@@@ -230,33 -312,18 +312,45 @@@ static void iso_now(char *buf, size_t s
/* ── Schema ─────────────────────────────────────────────────────── */
+ bool cbm_store_edges_schema_current(cbm_store_t *s) {
+ sqlite3_stmt *probe = NULL;
+ if (!s || !s->db ||
+ sqlite3_prepare_v2(s->db, "SELECT local_name_gen FROM edges LIMIT 0;", CBM_NOT_FOUND,
+ &probe, NULL) != SQLITE_OK) {
+ sqlite3_finalize(probe);
+ return false;
+ }
+ sqlite3_finalize(probe);
+ return true;
+ }
+
+/* Best-effort probe for the BT-240 indexed_checkout_sha column. Returns true
+ * only when the index_coverage_meta table exists AND exposes the column.
+ * Writable opens run init_schema (which adds it), so false is expected only on
+ * read-only legacy DBs opened via the query path — those must keep working and
+ * simply report no indexed-checkout identity. */
+static bool coverage_meta_has_checkout_column(cbm_store_t *s) {
+ if (!s || !s->db) {
+ return false;
+ }
+ sqlite3_stmt *stmt = NULL;
+ if (sqlite3_prepare_v2(s->db, "PRAGMA table_info(index_coverage_meta);", CBM_NOT_FOUND, &stmt,
+ NULL) != SQLITE_OK) {
+ return false;
+ }
+ bool has = false;
+ int rc;
+ while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
+ const char *name = (const char *)sqlite3_column_text(stmt, SKIP_ONE);
+ if (name && strcmp(name, "indexed_checkout_sha") == 0) {
+ has = true;
+ break;
+ }
+ }
+ sqlite3_finalize(stmt);
+ return has;
+}
+
static int init_schema(cbm_store_t *s) {
const char *ddl =
"CREATE TABLE IF NOT EXISTS projects ("
diff --cc tests/test_git_context.c
index 3e6ebc113,c508e25f3..b73d76328
--- a/tests/test_git_context.c
+++ b/tests/test_git_context.c
@@@ -8,10 -8,9 +8,11 @@@
* (input_path), not to worktree_root. Joining it with worktree_root and then
* string-stripping "/.git" left unresolved ".." components in the result.
*
- * These tests shell out to `git`, so the canonical_root tests SKIP_PLATFORM on
- * Windows (the CI shell there cannot init a repo via system()). The bounded
- * worktree-status tests below shell out through cbm_popen instead, so they run
- * on every platform that has git on PATH.
+ * The canonical_root tests shell out to `git`, so they SKIP_PLATFORM on
+ * Windows (the CI shell there cannot init a repo via system()). The root-path
- * existence test runs on every platform.
++ * existence test runs on every platform. The bounded worktree-status tests
++ * below shell out through cbm_popen instead, so they run on every platform
++ * that has git on PATH.
*
* Reproduce-first guard: canonical_root_subdir is the genuine RED-without-the-fix
* guard — a repo indexed from a subdirectory yields a relative --git-common-dir
@@@ -234,268 -346,155 +348,417 @@@ TEST(canonical_root_linked_worktree)
#endif /* _WIN32 */
}
+ TEST(git_context_ignores_inherited_repo_env) {
+ #ifdef _WIN32
+ SKIP_PLATFORM("git-based env-isolation test not supported on Windows CI");
+ #else
+ char root[256];
+ char *raw = th_mktempdir("cbm_gitenv");
+ if (!raw)
+ FAIL("th_mktempdir returned NULL");
+ snprintf(root, sizeof(root), "%s", raw);
+ if (make_env_isolation_tree(root) != 0) {
+ th_rmtree(root);
+ SKIP_PLATFORM("git not available to init a repo");
+ }
+ char decoy[512], fixture[512], plain[512];
+ snprintf(decoy, sizeof(decoy), "%s/decoy", root);
+ snprintf(fixture, sizeof(fixture), "%s/fixture", root);
+ snprintf(plain, sizeof(plain), "%s/plain", root);
+
+ char decoy_head[128], decoy_branch[128], fixture_head[128];
+ git_read_line(decoy, "rev-parse HEAD", decoy_head, sizeof(decoy_head));
+ git_read_line(decoy, "rev-parse --abbrev-ref HEAD", decoy_branch, sizeof(decoy_branch));
+ git_read_line(fixture, "rev-parse HEAD", fixture_head, sizeof(fixture_head));
+
+ git_env_snapshot_t snap;
+ git_env_hook_enter(&snap, decoy);
+ cbm_git_context_t plain_ctx = {0};
+ int plain_rc = cbm_git_context_resolve(plain, &plain_ctx);
+ cbm_git_context_t fix_ctx = {0};
+ int fix_rc = cbm_git_context_resolve(fixture, &fix_ctx);
+ git_env_hook_leave(&snap);
+
+ bool plain_is_git = plain_ctx.is_git;
+ char fix_head[128], fix_branch[128];
+ snprintf(fix_head, sizeof(fix_head), "%s", fix_ctx.head_sha ? fix_ctx.head_sha : "");
+ snprintf(fix_branch, sizeof(fix_branch), "%s", fix_ctx.branch ? fix_ctx.branch : "");
+ bool fix_is_git = fix_ctx.is_git;
+ cbm_git_context_free(&plain_ctx);
+ cbm_git_context_free(&fix_ctx);
+
+ char decoy_head_after[128], decoy_branch_after[128];
+ git_read_line(decoy, "rev-parse HEAD", decoy_head_after, sizeof(decoy_head_after));
+ git_read_line(decoy, "rev-parse --abbrev-ref HEAD", decoy_branch_after,
+ sizeof(decoy_branch_after));
+ th_rmtree(root);
+
+ ASSERT_EQ(plain_rc, 0);
+ ASSERT_FALSE(plain_is_git); /* a plain dir is not the decoy's repo */
+ ASSERT_EQ(fix_rc, 0);
+ ASSERT_TRUE(fix_is_git);
+ ASSERT_STR_EQ(fix_head, fixture_head); /* the fixture's HEAD, not the decoy's */
+ ASSERT_STR_EQ(fix_branch, "fixture-branch");
+ ASSERT_STR_EQ(decoy_head_after, decoy_head); /* the decoy is untouched */
+ ASSERT_STR_EQ(decoy_branch_after, decoy_branch);
+ PASS();
+ #endif /* _WIN32 */
+ }
+
+ TEST(githistory_ignores_inherited_repo_env) {
+ #ifdef _WIN32
+ SKIP_PLATFORM("git-based env-isolation test not supported on Windows CI");
+ #else
+ char root[256];
+ char *raw = th_mktempdir("cbm_gitenv_hist");
+ if (!raw)
+ FAIL("th_mktempdir returned NULL");
+ snprintf(root, sizeof(root), "%s", raw);
+ if (make_env_isolation_tree(root) != 0) {
+ th_rmtree(root);
+ SKIP_PLATFORM("git not available to init a repo");
+ }
+ char decoy[512], plain[512];
+ snprintf(decoy, sizeof(decoy), "%s/decoy", root);
+ snprintf(plain, sizeof(plain), "%s/plain", root);
+
+ git_env_snapshot_t snap;
+ git_env_hook_enter(&snap, decoy);
+ cbm_githistory_result_t result = {0};
+ (void)cbm_pipeline_githistory_compute(plain, &result);
+ git_env_hook_leave(&snap);
+
+ int commits = result.commit_count;
+ int couplings = result.count;
+ free(result.couplings);
+ free(result.file_temporal);
+ th_rmtree(root);
+
+ /* A non-git directory has no history: the decoy's 3 coupled commits
+ * must not be attributed to it. */
+ ASSERT_EQ(commits, 0);
+ ASSERT_EQ(couplings, 0);
+ PASS();
+ #endif /* _WIN32 */
+ }
+
+ /* ── Branch-only resolve (list_projects) ───────────────────────────
+ * The listing prints only the branch; cbm_git_branch_resolve gets it from the
+ * two git calls cbm_git_context_resolve uses for it, so both must agree. */
+
+ #ifndef _WIN32
+ /* The branch resolver against the full context at `path`; NULL `want` = not git. */
+ static bool branch_resolve_agrees(const char *path, const char *want) {
+ cbm_git_context_t ctx = {0};
+ (void)cbm_git_context_resolve(path, &ctx);
+ bool is_git = false;
+ char branch[CBM_GIT_BRANCH_MAX];
+ (void)cbm_git_branch_resolve(path, &is_git, branch, sizeof(branch));
+ bool agrees = is_git == ctx.is_git && is_git == (want != NULL) &&
+ (want ? (ctx.branch && strcmp(branch, want) == 0 && strcmp(ctx.branch, want) == 0)
+ : branch[0] == '\0');
+ cbm_git_context_free(&ctx);
+ return agrees;
+ }
+ #endif /* _WIN32 */
+
+ TEST(git_branch_resolve_matches_full_context) {
+ #ifdef _WIN32
+ SKIP_PLATFORM("git-based branch test not supported on Windows CI");
+ #else
+ char plain[512];
+ char *tmp = th_mktempdir("cbm_gitbranch_plain");
+ if (!tmp) FAIL("th_mktempdir returned NULL");
+ snprintf(plain, sizeof(plain), "%s", tmp);
+ tmp = th_mktempdir("cbm_gitbranch");
+ if (!tmp) FAIL("th_mktempdir returned NULL");
+ if (make_git_repo(tmp) != 0) {
+ th_rmtree(tmp);
+ th_rmtree(plain);
+ SKIP_PLATFORM("git not available to init a repo");
+ }
+ char sub[600];
+ snprintf(sub, sizeof(sub), "%s/sub", tmp);
+ ASSERT_EQ(th_mkdir_p(sub), 0);
+ char missing[600];
+ snprintf(missing, sizeof(missing), "%s/missing", plain);
+
+ ASSERT_TRUE(branch_resolve_agrees(plain, NULL));
+ ASSERT_TRUE(branch_resolve_agrees(missing, NULL));
+ ASSERT_EQ(git_run(tmp, "checkout -q -b feat/x"), 0);
+ ASSERT_TRUE(branch_resolve_agrees(tmp, "feat/x"));
+ ASSERT_TRUE(branch_resolve_agrees(sub, "feat/x"));
+ ASSERT_EQ(git_run(tmp, "checkout -q --detach"), 0);
+ ASSERT_TRUE(branch_resolve_agrees(tmp, "DETACHED"));
+
+ th_rmtree(tmp);
+ th_rmtree(plain);
+ PASS();
+ #endif /* _WIN32 */
+ }
+
+/* ── Bounded worktree status (BT-240 increment 3) ──────────────────────
+ * These run on EVERY platform, not just POSIX: they shell out through
+ * cbm_popen (the same isolated spawn production git_context uses) instead of
+ * system(), so they work whenever git is on PATH — Windows CI included. The
+ * core status parser is platform-neutral and must be exercised everywhere git
+ * is available; only a genuinely missing git skips. */
+
+static int status_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 status_make_repo(const char *dir) {
+ if (th_mkdir_p(dir) != 0) return -1;
+ if (status_git_run(dir, "init -q") != 0) return -1;
+ if (status_git_run(dir, "config user.email [email protected]") != 0) return -1;
+ if (status_git_run(dir, "config user.name Test") != 0) return -1;
+ if (th_write_file(TH_PATH(dir, ".keep"), "") != 0) return -1;
+ if (status_git_run(dir, "add -A") != 0) return -1;
+ if (status_git_run(dir, "commit -qm init") != 0) return -1;
+ return 0;
+}
+
+TEST(worktree_status_clean) {
+ char *tmp = th_mktempdir("cbm_wtstatus");
+ if (!tmp) FAIL("th_mktempdir returned NULL");
+
+ if (status_make_repo(tmp) != 0) {
+ th_rmtree(tmp);
+ SKIP_PLATFORM("git not available to init a repo");
+ }
+
+ cbm_worktree_status_t st = {0};
+ int rc = cbm_git_worktree_status(tmp, 8, &st);
+ if (rc != 0 || !st.available) {
+ cbm_git_worktree_status_free(&st);
+ th_rmtree(tmp);
+ FAIL("clean repo status not available");
+ }
+ ASSERT_EQ(st.tracked_count, 0);
+ ASSERT_EQ(st.untracked_count, 0);
+ ASSERT_EQ(st.tracked_sample_count, 0);
+ ASSERT_EQ(st.untracked_sample_count, 0);
+ ASSERT_FALSE(st.tracked_truncated);
+ ASSERT_FALSE(st.untracked_truncated);
+
+ cbm_git_worktree_status_free(&st);
+ th_rmtree(tmp);
+ PASS();
+}
+
+TEST(worktree_status_tracked_modified) {
+ char *tmp = th_mktempdir("cbm_wtstatus");
+ if (!tmp) FAIL("th_mktempdir returned NULL");
+
+ if (status_make_repo(tmp) != 0) {
+ th_rmtree(tmp);
+ SKIP_PLATFORM("git not available to init a repo");
+ }
+ if (th_write_file(TH_PATH(tmp, "tracked.txt"), "v1") != 0 ||
+ status_git_run(tmp, "add -f -- tracked.txt") != 0 ||
+ status_git_run(tmp, "commit -q -m add-tracked") != 0) {
+ th_rmtree(tmp);
+ FAIL("failed to commit tracked file");
+ }
+ if (th_write_file(TH_PATH(tmp, "tracked.txt"), "v2") != 0) {
+ th_rmtree(tmp);
+ FAIL("failed to modify tracked file");
+ }
+
+ cbm_worktree_status_t st = {0};
+ int rc = cbm_git_worktree_status(tmp, 8, &st);
+ if (rc != 0 || !st.available) {
+ cbm_git_worktree_status_free(&st);
+ th_rmtree(tmp);
+ FAIL("status not available");
+ }
+ ASSERT_EQ(st.tracked_count, 1);
+ ASSERT_EQ(st.untracked_count, 0);
+ ASSERT_EQ(st.tracked_sample_count, 1);
+ ASSERT_STR_EQ(st.tracked_paths[0], "tracked.txt");
+ ASSERT_FALSE(st.tracked_truncated);
+
+ cbm_git_worktree_status_free(&st);
+ th_rmtree(tmp);
+ PASS();
+}
+
+TEST(worktree_status_untracked) {
+ char *tmp = th_mktempdir("cbm_wtstatus");
+ if (!tmp) FAIL("th_mktempdir returned NULL");
+
+ if (status_make_repo(tmp) != 0) {
+ th_rmtree(tmp);
+ SKIP_PLATFORM("git not available to init a repo");
+ }
+ if (th_write_file(TH_PATH(tmp, "newfile.txt"), "n") != 0) {
+ th_rmtree(tmp);
+ FAIL("failed to create untracked file");
+ }
+
+ cbm_worktree_status_t st = {0};
+ int rc = cbm_git_worktree_status(tmp, 8, &st);
+ if (rc != 0 || !st.available) {
+ cbm_git_worktree_status_free(&st);
+ th_rmtree(tmp);
+ FAIL("status not available");
+ }
+ ASSERT_EQ(st.untracked_count, 1);
+ ASSERT_EQ(st.tracked_count, 0);
+ ASSERT_EQ(st.untracked_sample_count, 1);
+ ASSERT_STR_EQ(st.untracked_paths[0], "newfile.txt");
+ ASSERT_FALSE(st.untracked_truncated);
+
+ cbm_git_worktree_status_free(&st);
+ th_rmtree(tmp);
+ PASS();
+}
+
+TEST(worktree_status_rename_counts_once) {
+ char *tmp = th_mktempdir("cbm_wtstatus");
+ if (!tmp) FAIL("th_mktempdir returned NULL");
+
+ if (status_make_repo(tmp) != 0) {
+ th_rmtree(tmp);
+ SKIP_PLATFORM("git not available to init a repo");
+ }
+ if (th_write_file(TH_PATH(tmp, "old.txt"), "o") != 0 ||
+ th_write_file(TH_PATH(tmp, "other.txt"), "x") != 0 ||
+ status_git_run(tmp, "add -A") != 0 ||
+ status_git_run(tmp, "commit -qm base") != 0 ||
+ status_git_run(tmp, "mv old.txt new.txt") != 0) {
+ th_rmtree(tmp);
+ FAIL("failed to stage a rename");
+ }
+
+ cbm_worktree_status_t st = {0};
+ int rc = cbm_git_worktree_status(tmp, 8, &st);
+ if (rc != 0 || !st.available) {
+ cbm_git_worktree_status_free(&st);
+ th_rmtree(tmp);
+ FAIL("status not available");
+ }
+ ASSERT_EQ(st.tracked_count, 1); /* rename/copy counts ONCE, not twice */
+ ASSERT_EQ(st.tracked_sample_count, 1);
+ ASSERT_STR_EQ(st.tracked_paths[0], "new.txt"); /* sampled NEW path */
+ ASSERT_EQ(st.untracked_count, 0);
+
+ cbm_git_worktree_status_free(&st);
+ th_rmtree(tmp);
+ PASS();
+}
+
+TEST(worktree_status_sample_cap_truncates) {
+ char *tmp = th_mktempdir("cbm_wtstatus");
+ if (!tmp) FAIL("th_mktempdir returned NULL");
+
+ if (status_make_repo(tmp) != 0) {
+ th_rmtree(tmp);
+ SKIP_PLATFORM("git not available to init a repo");
+ }
+ enum { N_FILES = 5, SAMPLE_MAX = 2 };
+ for (int i = 0; i < N_FILES; i++) {
+ char path[1024];
+ snprintf(path, sizeof(path), "%s/u%d.txt", tmp, i);
+ if (th_write_file(path, "u") != 0) {
+ th_rmtree(tmp);
+ FAIL("failed to create untracked file");
+ }
+ }
+
+ cbm_worktree_status_t st = {0};
+ int rc = cbm_git_worktree_status(tmp, SAMPLE_MAX, &st);
+ if (rc != 0 || !st.available) {
+ cbm_git_worktree_status_free(&st);
+ th_rmtree(tmp);
+ FAIL("status not available");
+ }
+ ASSERT_EQ(st.untracked_count, N_FILES); /* full stream counted... */
+ ASSERT_EQ(st.untracked_sample_count, SAMPLE_MAX); /* ...samples bounded */
+ ASSERT_TRUE(st.untracked_truncated);
+ ASSERT_EQ(st.tracked_count, 0);
+ ASSERT_FALSE(st.tracked_truncated);
+
+ cbm_git_worktree_status_free(&st);
+ th_rmtree(tmp);
+ PASS();
+}
+
+TEST(worktree_status_mixed_tracked_and_untracked) {
+ char *tmp = th_mktempdir("cbm_wtstatus");
+ if (!tmp) FAIL("th_mktempdir returned NULL");
+
+ if (status_make_repo(tmp) != 0) {
+ th_rmtree(tmp);
+ SKIP_PLATFORM("git not available to init a repo");
+ }
+ if (th_write_file(TH_PATH(tmp, "tracked.txt"), "v1") != 0 ||
+ status_git_run(tmp, "add -f -- tracked.txt") != 0 ||
+ status_git_run(tmp, "commit -q -m add-tracked") != 0) {
+ th_rmtree(tmp);
+ FAIL("failed to commit tracked file");
+ }
+ if (th_write_file(TH_PATH(tmp, "tracked.txt"), "v2") != 0 ||
+ th_write_file(TH_PATH(tmp, "untracked.txt"), "n") != 0) {
+ th_rmtree(tmp);
+ FAIL("failed to prepare mixed worktree");
+ }
+
+ cbm_worktree_status_t st = {0};
+ int rc = cbm_git_worktree_status(tmp, 8, &st);
+ if (rc != 0 || !st.available) {
+ cbm_git_worktree_status_free(&st);
+ th_rmtree(tmp);
+ FAIL("status not available");
+ }
+ ASSERT_EQ(st.tracked_count, 1);
+ ASSERT_EQ(st.untracked_count, 1);
+ ASSERT_STR_EQ(st.tracked_paths[0], "tracked.txt");
+ ASSERT_STR_EQ(st.untracked_paths[0], "untracked.txt");
+
+ cbm_git_worktree_status_free(&st);
+ th_rmtree(tmp);
+ PASS();
+}
+
+/* Fail-closed contract: a non-repo directory must report unavailable with
+ * zero counts — never a silent "clean" tree. Does not need git on PATH. */
+TEST(worktree_status_unavailable_non_repo) {
+ char *tmp = th_mktempdir("cbm_wtstatus");
+ if (!tmp) FAIL("th_mktempdir returned NULL");
+
+ cbm_worktree_status_t st = {0};
+ int rc = cbm_git_worktree_status(tmp, 8, &st);
+ if (rc != 0) {
+ th_rmtree(tmp);
+ FAIL("status call returned an error");
+ }
+ ASSERT_FALSE(st.available);
+ ASSERT_EQ(st.tracked_count, 0);
+ ASSERT_EQ(st.untracked_count, 0);
+ ASSERT_EQ(st.tracked_sample_count, 0);
+ ASSERT_EQ(st.untracked_sample_count, 0);
+
+ cbm_git_worktree_status_free(&st);
+ th_rmtree(tmp);
+ PASS();
+}
+
/* ── Suite ──────────────────────────────────────────────────────── */
SUITE(git_context) {
@@@ -502,11 -503,6 +767,13 @@@
RUN_TEST(canonical_root_repo_root);
RUN_TEST(canonical_root_subdir);
RUN_TEST(canonical_root_linked_worktree);
+ RUN_TEST(git_context_ignores_inherited_repo_env);
+ RUN_TEST(githistory_ignores_inherited_repo_env);
+ RUN_TEST(worktree_status_clean);
+ RUN_TEST(worktree_status_tracked_modified);
+ RUN_TEST(worktree_status_untracked);
+ RUN_TEST(worktree_status_rename_counts_once);
+ RUN_TEST(worktree_status_sample_cap_truncates);
+ RUN_TEST(worktree_status_mixed_tracked_and_untracked);
+ RUN_TEST(worktree_status_unavailable_non_repo);
}
diff --cc tests/test_store_nodes.c
index 4c555db30,ccd3fc6fa..67e7a7e36
--- a/tests/test_store_nodes.c
+++ b/tests/test_store_nodes.c
@@@ -2361,76 -2603,10 +2603,79 @@@ TEST(store_coverage_replace_rolls_back_
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);
+ RUN_TEST(store_qn_base_finds_overloads_only);
RUN_TEST(store_coverage_roundtrip_prune_shadow);
RUN_TEST(store_coverage_targeted_path_and_scope_lookup);
RUN_TEST(store_coverage_meta_zero_row_truncation_and_delete);