Skip to content

Commit 73c509f

Browse files
committed
fix(mcp): report an unreadable count as an error, not an empty project
The store change alone does not close DeusData#2012. handle_index_status reads the counts directly and renders status as `nodes > 0 ? "ready" : "empty"`, so a negative count from a failed read still answered "empty" — now with a bare -1 printed as the count, and with no hint at all, because the "Project is empty" branch is guarded on `nodes == 0`. The guard cited earlier lives in build_index_success_response, which is index_repository's response builder, not this path. Treat a negative node or edge count as a failed read: report status "error", suppress the negative numbers rather than emitting them as counts, and replace the re-index hint with one that names the table that could not be read. Closes DeusData#2012 Signed-off-by: kavish-19 <[email protected]>
1 parent 99c3bb4 commit 73c509f

2 files changed

Lines changed: 82 additions & 4 deletions

File tree

‎src/mcp/mcp.c‎

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6668,10 +6668,17 @@ static char *handle_index_status(cbm_mcp_server_t *srv, const char *args) {
66686668
if (project) {
66696669
int nodes = cbm_store_count_nodes(store, project);
66706670
int edges = cbm_store_count_edges(store, project);
6671+
/* A negative count is a failed read (CBM_STORE_ERR), not a small
6672+
* project. Reporting it as a count would put a bare -1 in the output,
6673+
* and `nodes > 0 ? "ready" : "empty"` would answer "empty" for an
6674+
* unreadable table — the false all-clear #2012 is about. Say the read
6675+
* failed, and name the table that could not be read. */
6676+
const bool counts_unreadable = nodes < 0 || edges < 0;
66716677
yyjson_mut_obj_add_str(doc, root, "project", project);
6672-
yyjson_mut_obj_add_int(doc, root, "nodes", nodes);
6673-
yyjson_mut_obj_add_int(doc, root, "edges", edges);
6674-
yyjson_mut_obj_add_str(doc, root, "status", nodes > 0 ? "ready" : "empty");
6678+
yyjson_mut_obj_add_int(doc, root, "nodes", counts_unreadable ? 0 : nodes);
6679+
yyjson_mut_obj_add_int(doc, root, "edges", counts_unreadable ? 0 : edges);
6680+
yyjson_mut_obj_add_str(doc, root, "status",
6681+
counts_unreadable ? "error" : (nodes > 0 ? "ready" : "empty"));
66756682
cbm_project_t proj_info = {0};
66766683
bool have_proj_info = cbm_store_get_project(store, project, &proj_info) == CBM_STORE_OK;
66776684
if (have_proj_info) {
@@ -6688,7 +6695,20 @@ static char *handle_index_status(cbm_mcp_server_t *srv, const char *args) {
66886695
safe_str_free(&proj_info.name);
66896696
safe_str_free(&proj_info.indexed_at);
66906697
safe_str_free(&proj_info.root_path);
6691-
if (nodes == 0) {
6698+
if (counts_unreadable) {
6699+
yyjson_mut_obj_add_str(
6700+
doc, root, "hint",
6701+
nodes < 0 && edges < 0
6702+
? "The nodes and edges tables could not be read; the database may be "
6703+
"corrupt. Re-run index_repository(repo_path=...) or remove the project "
6704+
"cache and re-index."
6705+
: (nodes < 0 ? "The nodes table could not be read; the database may be "
6706+
"corrupt. Re-run index_repository(repo_path=...) or remove "
6707+
"the project cache and re-index."
6708+
: "The edges table could not be read; the database may be "
6709+
"corrupt. Re-run index_repository(repo_path=...) or remove "
6710+
"the project cache and re-index."));
6711+
} else if (nodes == 0) {
66926712
yyjson_mut_obj_add_str(
66936713
doc, root, "hint",
66946714
"Project is empty. Re-run index_repository(repo_path=...) to populate.");

‎tests/test_mcp.c‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3384,6 +3384,63 @@ TEST(tool_index_status_keeps_authoritative_ignored_total_when_rows_are_sampled)
33843384
PASS();
33853385
}
33863386

3387+
/* #2012: a COUNT(*) that cannot be read must not be reported as an empty
3388+
* project. The store now returns CBM_STORE_ERR for a failed step, and
3389+
* index_status has to say the read failed rather than answering "empty" with
3390+
* a bare -1 as the count. Dropping the tables after the first call makes the
3391+
* step (not the prepare) fail deterministically, because the statements are
3392+
* cached by then. */
3393+
TEST(tool_index_status_reports_an_unreadable_count_as_an_error) {
3394+
char tmp[256];
3395+
cbm_mcp_server_t *srv = setup_snippet_server(tmp, sizeof(tmp));
3396+
ASSERT_NOT_NULL(srv);
3397+
cbm_store_t *store = cbm_mcp_server_store(srv);
3398+
ASSERT_NOT_NULL(store);
3399+
3400+
/* First call: a healthy project, and it caches the count statements. */
3401+
char *response = cbm_mcp_handle_tool(srv, "index_status",
3402+
"{\"project\":\"test-project\",\"format\":\"json\"}");
3403+
char *inner = extract_text_content(response);
3404+
ASSERT_NOT_NULL(inner);
3405+
yyjson_doc *doc = yyjson_read(inner, strlen(inner), 0);
3406+
ASSERT_NOT_NULL(doc);
3407+
ASSERT_STR_EQ(yyjson_get_str(yyjson_obj_get(yyjson_doc_get_root(doc), "status")), "ready");
3408+
yyjson_doc_free(doc);
3409+
free(inner);
3410+
free(response);
3411+
3412+
ASSERT_EQ(cbm_store_exec(store, "DROP TABLE nodes;"), 0);
3413+
ASSERT_EQ(cbm_store_exec(store, "DROP TABLE edges;"), 0);
3414+
3415+
response = cbm_mcp_handle_tool(srv, "index_status",
3416+
"{\"project\":\"test-project\",\"format\":\"json\"}");
3417+
inner = extract_text_content(response);
3418+
ASSERT_NOT_NULL(inner);
3419+
doc = yyjson_read(inner, strlen(inner), 0);
3420+
ASSERT_NOT_NULL(doc);
3421+
yyjson_val *root = yyjson_doc_get_root(doc);
3422+
3423+
/* Not "empty": that is the false all-clear the issue is about. */
3424+
ASSERT_STR_EQ(yyjson_get_str(yyjson_obj_get(root, "status")), "error");
3425+
3426+
/* A failed read is not a count, so no negative number reaches the caller. */
3427+
ASSERT_TRUE(yyjson_get_int(yyjson_obj_get(root, "nodes")) >= 0);
3428+
ASSERT_TRUE(yyjson_get_int(yyjson_obj_get(root, "edges")) >= 0);
3429+
3430+
/* The hint names the unreadable table instead of telling the user to
3431+
* re-index an "empty" project. */
3432+
const char *hint = yyjson_get_str(yyjson_obj_get(root, "hint"));
3433+
ASSERT_NOT_NULL(hint);
3434+
ASSERT_TRUE(strstr(hint, "could not be read") != NULL);
3435+
ASSERT_TRUE(strstr(hint, "Project is empty") == NULL);
3436+
3437+
yyjson_doc_free(doc);
3438+
free(inner);
3439+
free(response);
3440+
cbm_mcp_server_free(srv);
3441+
PASS();
3442+
}
3443+
33873444
TEST(tool_output_byte_budgets) {
33883445
/* GUARD: absolute byte ceilings on default tool outputs. Re-bloat (e.g.
33893446
* a property blob sneaking back into row emission — the fp field alone
@@ -19827,6 +19884,7 @@ SUITE(mcp) {
1982719884
RUN_TEST(tool_search_graph_toon_never_leaks_internal_fields);
1982819885
RUN_TEST(tool_lean_defaults_schema_and_status);
1982919886
RUN_TEST(tool_index_status_keeps_authoritative_ignored_total_when_rows_are_sampled);
19887+
RUN_TEST(tool_index_status_reports_an_unreadable_count_as_an_error);
1983019888
RUN_TEST(tool_output_regression_gate);
1983119889
RUN_TEST(tool_output_byte_budgets);
1983219890
RUN_TEST(tool_search_graph_query_honors_file_pattern_issue552);

0 commit comments

Comments
 (0)