Skip to content

Commit e49f7e5

Browse files
committed
feat(store): persist indexed-checkout sha at staged-generation boundary
Record the frozen git HEAD at the same successful staged-generation boundary as graph coverage. cbm_coverage_meta_t gains indexed_checkout_sha; new writable DBs create a nullable indexed_checkout_sha column and existing writable DBs are migrated idempotently in init_schema, while read-only legacy DBs without the column still open, read and report no identity instead of erroring. coverage_replace_ex writes/updates the SHA inside its existing transaction, so failure/rollback retains the prior identity. Full, delta-incremental and legacy-incremental generation templates set it from the pipeline's refreshed git context (NULL/empty for non-git repos). index_status freshness now derives from the recorded identity: no indexed SHA -> unknown/indexed_checkout_unavailable; differs from the live git HEAD -> stale/indexed_checkout_mismatch; equal -> current. Emits indexed_generation, indexed_checkout_sha, checkout_sha, a stable reasons array and recommended_action; stays verbose-only and read-only. Add store round-trip/rollback coverage and MCP verdict tests for matching current and mismatched stale (git fixtures, platform-skipped on Windows). Signed-off-by: Leonardo trindade miranda <tmonestudio@gmail.com>
1 parent 36d467d commit e49f7e5

7 files changed

Lines changed: 345 additions & 23 deletions

File tree

‎src/mcp/mcp.c‎

Lines changed: 63 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4456,24 +4456,75 @@ static char *handle_check_index_coverage(cbm_mcp_server_t *srv, const char *args
44564456
return result;
44574457
}
44584458

4459-
/* BT-240: fail-closed freshness verdict. A live checkout SHA is not proof of
4460-
* the generation that produced graph content, so the verdict must come from
4461-
* the indexed-checkout identity recorded with the DB. Legacy DBs record no
4462-
* such identity: expose the graph generation separately and fail closed as
4463-
* unknown rather than pretending the live checkout produced the graph. This
4464-
* is a report-only signal — it never triggers indexing. */
4459+
/* BT-240: fail-closed freshness verdict against the indexed-checkout identity
4460+
* recorded with the DB at the successful staged-generation boundary. A live
4461+
* checkout SHA is not proof of the generation that produced graph content, so
4462+
* the verdict comes from that recorded identity:
4463+
* - no indexed SHA (legacy DB or non-git run) → unknown / unavailable
4464+
* - indexed SHA differs from the live git HEAD → stale / mismatch
4465+
* - equal → current
4466+
* Report-only: it never triggers indexing. Verbose-only and read-only; the
4467+
* live checkout is resolved fresh from the project root (never mutated). */
44654468
static void add_index_freshness_json(yyjson_mut_doc *doc, yyjson_mut_val *root,
4466-
const char *indexed_generation) {
4469+
cbm_store_t *store, const char *project,
4470+
const char *root_path, const char *indexed_generation) {
44674471
yyjson_mut_val *freshness = yyjson_mut_obj(doc);
44684472
if (indexed_generation && indexed_generation[0]) {
44694473
yyjson_mut_obj_add_strcpy(doc, freshness, "indexed_generation", indexed_generation);
44704474
} else {
44714475
yyjson_mut_obj_add_str(doc, freshness, "indexed_generation", "");
44724476
}
4473-
yyjson_mut_obj_add_null(doc, freshness, "indexed_checkout_sha");
4474-
yyjson_mut_obj_add_str(doc, freshness, "verdict", "unknown");
4475-
yyjson_mut_obj_add_str(doc, freshness, "reason", "indexed_checkout_unavailable");
4477+
4478+
cbm_coverage_meta_t meta = {0};
4479+
bool have_meta = store && cbm_store_coverage_meta_get(store, project, &meta) == CBM_STORE_OK;
4480+
const char *indexed_sha = NULL;
4481+
if (have_meta && meta.indexed_checkout_sha && meta.indexed_checkout_sha[0]) {
4482+
indexed_sha = meta.indexed_checkout_sha;
4483+
}
4484+
4485+
cbm_git_context_t ctx = {0};
4486+
(void)cbm_git_context_resolve(root_path, &ctx);
4487+
const char *checkout_sha = ctx.head_sha && ctx.head_sha[0] ? ctx.head_sha : NULL;
4488+
4489+
if (indexed_sha) {
4490+
yyjson_mut_obj_add_strcpy(doc, freshness, "indexed_checkout_sha", indexed_sha);
4491+
} else {
4492+
yyjson_mut_obj_add_null(doc, freshness, "indexed_checkout_sha");
4493+
}
4494+
if (checkout_sha) {
4495+
yyjson_mut_obj_add_strcpy(doc, freshness, "checkout_sha", checkout_sha);
4496+
} else {
4497+
yyjson_mut_obj_add_null(doc, freshness, "checkout_sha");
4498+
}
4499+
4500+
const char *verdict;
4501+
const char *reason;
4502+
const char *recommended_action;
4503+
if (!indexed_sha) {
4504+
verdict = "unknown";
4505+
reason = "indexed_checkout_unavailable";
4506+
recommended_action = "reindex_to_record_indexed_checkout";
4507+
} else if (!checkout_sha || strcmp(indexed_sha, checkout_sha) != 0) {
4508+
verdict = "stale";
4509+
reason = "indexed_checkout_mismatch";
4510+
recommended_action = "reindex_to_match_checkout";
4511+
} else {
4512+
verdict = "current";
4513+
reason = "indexed_checkout_current";
4514+
recommended_action = "use_graph";
4515+
}
4516+
yyjson_mut_obj_add_str(doc, freshness, "verdict", verdict);
4517+
yyjson_mut_obj_add_str(doc, freshness, "reason", reason);
4518+
yyjson_mut_val *reasons = yyjson_mut_arr(doc);
4519+
yyjson_mut_arr_add_str(doc, reasons, reason);
4520+
yyjson_mut_obj_add_val(doc, freshness, "reasons", reasons);
4521+
yyjson_mut_obj_add_str(doc, freshness, "recommended_action", recommended_action);
44764522
yyjson_mut_obj_add_val(doc, root, "freshness", freshness);
4523+
4524+
if (have_meta) {
4525+
cbm_store_coverage_meta_clear(&meta);
4526+
}
4527+
cbm_git_context_free(&ctx);
44774528
}
44784529

44794530
static char *handle_index_status(cbm_mcp_server_t *srv, const char *args) {
@@ -4502,7 +4553,8 @@ static char *handle_index_status(cbm_mcp_server_t *srv, const char *args) {
45024553
proj_info.root_path ? proj_info.root_path : "");
45034554
if (verbose) {
45044555
add_git_context_json(doc, root, proj_info.root_path);
4505-
add_index_freshness_json(doc, root, proj_info.indexed_at);
4556+
add_index_freshness_json(doc, root, store, project, proj_info.root_path,
4557+
proj_info.indexed_at);
45064558
}
45074559
safe_str_free(&proj_info.name);
45084560
safe_str_free(&proj_info.indexed_at);

‎src/pipeline/pipeline.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1961,6 +1961,7 @@ static int dump_and_persist_hashes(cbm_pipeline_t *p, const cbm_file_hash_t *bas
19611961
.ignored_files_total = p->ignored_total,
19621962
.coverage_version = CBM_SEMANTIC_INDEX_VERSION,
19631963
.hash_records_complete = true,
1964+
.indexed_checkout_sha = p->git_ctx.head_sha,
19641965
},
19651966
.surface_rows = p->surface_rows,
19661967
.surface_row_count = p->surface_row_count,

‎src/pipeline/pipeline_incremental.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2295,6 +2295,7 @@ static int run_closure_delta(cbm_pipeline_t *p, const char *db_path, const char
22952295
.ignored_files_total = run_ignored_total,
22962296
.coverage_version = CBM_SEMANTIC_INDEX_VERSION,
22972297
.hash_records_complete = true,
2298+
.indexed_checkout_sha = p->git_ctx.head_sha,
22982299
},
22992300
.surface_rows = NULL,
23002301
.surface_row_count = 0,
@@ -2842,6 +2843,7 @@ int cbm_pipeline_run_incremental(cbm_pipeline_t *p, const char *db_path, cbm_fil
28422843
.ignored_files_total = run_ignored_total,
28432844
.coverage_version = CBM_SEMANTIC_INDEX_VERSION,
28442845
.hash_records_complete = true,
2846+
.indexed_checkout_sha = p->git_ctx.head_sha,
28452847
};
28462848
/* Publish surfaces: the surviving previous rows plus this run's fresh
28472849
* ones (closure route). The legacy test route publishes none — its

‎src/store/store.c‎

Lines changed: 68 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,33 @@ static void iso_now(char *buf, size_t sz) {
225225

226226
/* ── Schema ─────────────────────────────────────────────────────── */
227227

228+
/* Best-effort probe for the BT-240 indexed_checkout_sha column. Returns true
229+
* only when the index_coverage_meta table exists AND exposes the column.
230+
* Writable opens run init_schema (which adds it), so false is expected only on
231+
* read-only legacy DBs opened via the query path — those must keep working and
232+
* simply report no indexed-checkout identity. */
233+
static bool coverage_meta_has_checkout_column(cbm_store_t *s) {
234+
if (!s || !s->db) {
235+
return false;
236+
}
237+
sqlite3_stmt *stmt = NULL;
238+
if (sqlite3_prepare_v2(s->db, "PRAGMA table_info(index_coverage_meta);", CBM_NOT_FOUND, &stmt,
239+
NULL) != SQLITE_OK) {
240+
return false;
241+
}
242+
bool has = false;
243+
int rc;
244+
while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
245+
const char *name = (const char *)sqlite3_column_text(stmt, SKIP_ONE);
246+
if (name && strcmp(name, "indexed_checkout_sha") == 0) {
247+
has = true;
248+
break;
249+
}
250+
}
251+
sqlite3_finalize(stmt);
252+
return has;
253+
}
254+
228255
static int init_schema(cbm_store_t *s) {
229256
const char *ddl =
230257
"CREATE TABLE IF NOT EXISTS projects ("
@@ -320,14 +347,25 @@ static int init_schema(cbm_store_t *s) {
320347
" ignored_files_stored INTEGER NOT NULL DEFAULT 0,"
321348
" ignored_files_total INTEGER NOT NULL DEFAULT 0,"
322349
" coverage_version INTEGER NOT NULL DEFAULT 1,"
323-
" hash_records_complete INTEGER NOT NULL DEFAULT 0"
350+
" hash_records_complete INTEGER NOT NULL DEFAULT 0,"
351+
" indexed_checkout_sha TEXT"
324352
");";
325353

326354
int rc = exec_sql(s, ddl);
327355
if (rc != CBM_STORE_OK) {
328356
return rc;
329357
}
330358

359+
/* BT-240 migration: CREATE TABLE IF NOT EXISTS cannot add a column to an
360+
* existing legacy index_coverage_meta, so add it idempotently for any
361+
* writable DB that predates it. Read-only query opens skip init_schema
362+
* entirely and meta_get probes the column separately. */
363+
if (!coverage_meta_has_checkout_column(s) &&
364+
exec_sql(s, "ALTER TABLE index_coverage_meta ADD COLUMN indexed_checkout_sha TEXT;") !=
365+
CBM_STORE_OK) {
366+
return CBM_STORE_ERR;
367+
}
368+
331369
/* Schema-compat probe (#768): DBs created before the local_name_gen
332370
* discriminator still enforce UNIQUE(source_id,target_id,type) and lack
333371
* the column — the widened upsert in cbm_store_insert_edge can neither
@@ -2952,11 +2990,12 @@ int cbm_store_coverage_replace_ex(cbm_store_t *s, const char *project,
29522990
"INSERT INTO index_coverage_meta "
29532991
"(project, generation, index_mode, recorded_at, recording_status, "
29542992
" ignored_files_stored, ignored_files_total, coverage_version, "
2955-
" hash_records_complete) "
2956-
"VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9) "
2993+
" hash_records_complete, indexed_checkout_sha) "
2994+
"VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10) "
29572995
"ON CONFLICT(project) DO UPDATE SET generation=?2, index_mode=?3, "
29582996
"recorded_at=?4, recording_status=?5, ignored_files_stored=?6, "
2959-
"ignored_files_total=?7, coverage_version=?8, hash_records_complete=?9;",
2997+
"ignored_files_total=?7, coverage_version=?8, hash_records_complete=?9, "
2998+
"indexed_checkout_sha=?10;",
29602999
CBM_NOT_FOUND, &up_meta, NULL) != SQLITE_OK) {
29613000
store_set_error_sqlite(s, "coverage meta upsert prepare");
29623001
(void)exec_sql(s, "ROLLBACK;");
@@ -2971,6 +3010,11 @@ int cbm_store_coverage_replace_ex(cbm_store_t *s, const char *project,
29713010
sqlite3_bind_int(up_meta, 7, ignored_total);
29723011
sqlite3_bind_int(up_meta, 8, coverage_version);
29733012
sqlite3_bind_int(up_meta, 9, meta->hash_records_complete ? 1 : 0);
3013+
if (meta->indexed_checkout_sha && meta->indexed_checkout_sha[0]) {
3014+
bind_text(up_meta, 10, meta->indexed_checkout_sha);
3015+
} else {
3016+
sqlite3_bind_null(up_meta, 10);
3017+
}
29743018
int meta_rc = sqlite3_step(up_meta);
29753019
sqlite3_finalize(up_meta);
29763020
if (meta_rc != SQLITE_DONE) {
@@ -3098,6 +3142,7 @@ void cbm_store_coverage_meta_clear(cbm_coverage_meta_t *meta) {
30983142
free((char *)meta->index_mode);
30993143
free((char *)meta->recorded_at);
31003144
free((char *)meta->recording_status);
3145+
free((char *)meta->indexed_checkout_sha);
31013146
memset(meta, 0, sizeof(*meta));
31023147
}
31033148

@@ -3110,11 +3155,21 @@ int cbm_store_coverage_meta_get(cbm_store_t *s, const char *project, cbm_coverag
31103155
return CBM_STORE_ERR;
31113156
}
31123157
sqlite3_stmt *stmt = NULL;
3113-
if (sqlite3_prepare_v2(s->db,
3114-
"SELECT project, generation, index_mode, recorded_at, recording_status, "
3115-
"ignored_files_stored, ignored_files_total, coverage_version, "
3116-
"hash_records_complete FROM index_coverage_meta WHERE project = ?1;",
3117-
CBM_NOT_FOUND, &stmt, NULL) != SQLITE_OK) {
3158+
/* Legacy read-only DBs (query opens skip init_schema) lack the
3159+
* indexed_checkout_sha column — probe and fall back so they still read and
3160+
* report no identity instead of erroring. */
3161+
static const char SQL_FULL[] =
3162+
"SELECT project, generation, index_mode, recorded_at, recording_status, "
3163+
"ignored_files_stored, ignored_files_total, coverage_version, "
3164+
"hash_records_complete, indexed_checkout_sha FROM index_coverage_meta "
3165+
"WHERE project = ?1;";
3166+
static const char SQL_LEGACY[] =
3167+
"SELECT project, generation, index_mode, recorded_at, recording_status, "
3168+
"ignored_files_stored, ignored_files_total, coverage_version, "
3169+
"hash_records_complete FROM index_coverage_meta WHERE project = ?1;";
3170+
bool has_checkout_column = coverage_meta_has_checkout_column(s);
3171+
if (sqlite3_prepare_v2(s->db, has_checkout_column ? SQL_FULL : SQL_LEGACY, CBM_NOT_FOUND,
3172+
&stmt, NULL) != SQLITE_OK) {
31183173
store_set_error_sqlite(s, "coverage meta get prepare");
31193174
return CBM_STORE_ERR;
31203175
}
@@ -3130,6 +3185,10 @@ int cbm_store_coverage_meta_get(cbm_store_t *s, const char *project, cbm_coverag
31303185
out->ignored_files_total = sqlite3_column_int(stmt, 6);
31313186
out->coverage_version = sqlite3_column_int(stmt, 7);
31323187
out->hash_records_complete = sqlite3_column_int(stmt, 8) != 0;
3188+
if (has_checkout_column) {
3189+
const char *sha = (const char *)sqlite3_column_text(stmt, 9);
3190+
out->indexed_checkout_sha = sha && sha[0] ? heap_strdup(sha) : NULL;
3191+
}
31333192
sqlite3_finalize(stmt);
31343193
if (!out->project || !out->generation || !out->index_mode || !out->recorded_at ||
31353194
!out->recording_status) {

‎src/store/store.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,11 @@ typedef struct {
546546
int ignored_files_total;
547547
int coverage_version;
548548
bool hash_records_complete;
549+
/* Git checkout identity frozen at the successful staged-generation
550+
* boundary (BT-240). NULL/"" when the run had no git context (non-git
551+
* repo) or the DB predates the column. Owned by the row: meta_get
552+
* allocates it, meta_clear frees it. */
553+
const char *indexed_checkout_sha;
549554
} cbm_coverage_meta_t;
550555

551556
/* Replace the project's coverage rows in one transaction, then prune rows for

0 commit comments

Comments
 (0)