Skip to content

Commit 36d467d

Browse files
committed
fix(mcp): fail closed when indexed-checkout identity is absent
A live checkout SHA is not proof of the generation that produced graph content, so the freshness verdict must come from the indexed-checkout identity recorded with the DB. Legacy DBs record no such identity: verbose index_status now exposes the graph generation separately (indexed_generation) and fails closed with verdict=unknown, indexed_checkout_sha=null and reason=indexed_checkout_unavailable. The default status call stays lean and the freshness block is report-only — it never triggers indexing. Add focused coverage for the fail-closed verdict and for its omission from the default output. Signed-off-by: Leonardo trindade miranda <tmonestudio@gmail.com>
1 parent 7f23a66 commit 36d467d

2 files changed

Lines changed: 76 additions & 3 deletions

File tree

‎src/mcp/mcp.c‎

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4456,13 +4456,33 @@ 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. */
4465+
static void add_index_freshness_json(yyjson_mut_doc *doc, yyjson_mut_val *root,
4466+
const char *indexed_generation) {
4467+
yyjson_mut_val *freshness = yyjson_mut_obj(doc);
4468+
if (indexed_generation && indexed_generation[0]) {
4469+
yyjson_mut_obj_add_strcpy(doc, freshness, "indexed_generation", indexed_generation);
4470+
} else {
4471+
yyjson_mut_obj_add_str(doc, freshness, "indexed_generation", "");
4472+
}
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");
4476+
yyjson_mut_obj_add_val(doc, root, "freshness", freshness);
4477+
}
4478+
44594479
static char *handle_index_status(cbm_mcp_server_t *srv, const char *args) {
44604480
char *project = get_project_arg(args);
44614481
cbm_store_t *store = resolve_store(srv, project);
44624482
REQUIRE_STORE(store, project);
4463-
/* The git context block (worktree/shadow path variants) only matters when
4464-
* debugging index-location issues — gate it so the common status call
4465-
* stays lean. */
4483+
/* The git context block (worktree/shadow path variants) and the freshness
4484+
* verdict only matter when debugging index-location issues — gate them so
4485+
* the common status call stays lean. */
44664486
bool verbose = cbm_mcp_get_bool_arg(args, "verbose");
44674487

44684488
yyjson_mut_doc *doc = yyjson_mut_doc_new(NULL);
@@ -4482,6 +4502,7 @@ static char *handle_index_status(cbm_mcp_server_t *srv, const char *args) {
44824502
proj_info.root_path ? proj_info.root_path : "");
44834503
if (verbose) {
44844504
add_git_context_json(doc, root, proj_info.root_path);
4505+
add_index_freshness_json(doc, root, proj_info.indexed_at);
44854506
}
44864507
safe_str_free(&proj_info.name);
44874508
safe_str_free(&proj_info.indexed_at);

‎tests/test_mcp.c‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2642,6 +2642,56 @@ TEST(tool_index_status_includes_git_metadata) {
26422642
PASS();
26432643
}
26442644

2645+
/* BT-240 RED: a live checkout SHA is not proof of the generation that produced
2646+
* graph content. Older databases lack an indexed-checkout identity, so verbose
2647+
* status must fail closed as unknown while exposing graph generation
2648+
* separately. */
2649+
TEST(tool_index_status_fails_closed_without_indexed_checkout_identity) {
2650+
char tmp[256];
2651+
cbm_mcp_server_t *srv = setup_snippet_server(tmp, sizeof(tmp));
2652+
ASSERT_NOT_NULL(srv);
2653+
2654+
char *resp = cbm_mcp_handle_tool(
2655+
srv, "index_status", "{\"project\":\"test-project\",\"verbose\":true}");
2656+
ASSERT_NOT_NULL(resp);
2657+
char *inner = extract_text_content(resp);
2658+
ASSERT_NOT_NULL(inner);
2659+
ASSERT_NOT_NULL(strstr(inner, "\"freshness\""));
2660+
ASSERT_NOT_NULL(strstr(inner, "\"verdict\":\"unknown\""));
2661+
ASSERT_NOT_NULL(strstr(inner, "\"indexed_generation\""));
2662+
ASSERT_NOT_NULL(strstr(inner, "\"indexed_checkout_sha\":null"));
2663+
ASSERT_NOT_NULL(strstr(inner, "indexed_checkout_unavailable"));
2664+
2665+
free(inner);
2666+
free(resp);
2667+
cbm_mcp_server_free(srv);
2668+
cleanup_snippet_dir(tmp);
2669+
PASS();
2670+
}
2671+
2672+
/* BT-240 lean-default guard: the freshness verdict is diagnostics — a
2673+
* report-only signal that never auto-indexes — so it must stay out of the
2674+
* common (non-verbose) status call. */
2675+
TEST(tool_index_status_omits_freshness_by_default) {
2676+
char tmp[256];
2677+
cbm_mcp_server_t *srv = setup_snippet_server(tmp, sizeof(tmp));
2678+
ASSERT_NOT_NULL(srv);
2679+
2680+
char *resp = cbm_mcp_handle_tool(srv, "index_status", "{\"project\":\"test-project\"}");
2681+
ASSERT_NOT_NULL(resp);
2682+
char *inner = extract_text_content(resp);
2683+
ASSERT_NOT_NULL(inner);
2684+
ASSERT_NOT_NULL(strstr(inner, "\"status\""));
2685+
ASSERT_NULL(strstr(inner, "\"freshness\""));
2686+
ASSERT_NULL(strstr(inner, "indexed_checkout_sha"));
2687+
2688+
free(inner);
2689+
free(resp);
2690+
cbm_mcp_server_free(srv);
2691+
cleanup_snippet_dir(tmp);
2692+
PASS();
2693+
}
2694+
26452695
/* ══════════════════════════════════════════════════════════════════
26462696
* TOOL HANDLERS WITH DATA
26472697
* ══════════════════════════════════════════════════════════════════ */
@@ -10541,6 +10591,8 @@ SUITE(mcp) {
1054110591
RUN_TEST(tool_check_index_coverage_requires_source_when_file_metadata_changed);
1054210592
RUN_TEST(tool_check_index_coverage_surfaces_lookup_errors);
1054310593
RUN_TEST(tool_index_status_includes_git_metadata);
10594+
RUN_TEST(tool_index_status_fails_closed_without_indexed_checkout_identity);
10595+
RUN_TEST(tool_index_status_omits_freshness_by_default);
1054410596

1054510597
/* Tool handlers with validation */
1054610598
RUN_TEST(tool_trace_call_path_not_found);

0 commit comments

Comments
 (0)