Skip to content

Commit d72250a

Browse files
DeusDataclaude
andcommitted
fix(mcp): name the cause when an index attempt stops before publication
index_repository rendered every non-zero pipeline code as "Pipeline failed. Check repo_path exists and contains source files", which sent people debugging a path that was fine (DeusData#2020). Three outcomes now say what actually happened: - CBM_PIPELINE_ABORT_OVER_BUDGET (DeusData#1997, DeusData#832): status "error" with reason "over_memory_budget", previous_index "preserved", the budget and peak RSS in MB, and a hint naming CBM_MEM_BUDGET_MB / CBM_WORKERS / excludes. The handler logs index.abort reason=over_memory_budget. The CLI prints the same JSON and exits 1; a supervised worker still exits 0 with the response written, so the daemon passes it through verbatim instead of entering skip-and-continue recovery. - CBM_PIPELINE_ABORT_PRESERVE_DB: status "aborted_previous_preserved" (retry; the previous index still serves). - CBM_PIPELINE_PERSIST_FAILED: status "persist_failed" (disk/permissions). The incremental test helper goes fail-closed on error responses so a transient pre-publication abort surfaces once at the source instead of cascading into dozens of downstream count failures. Test: an in-process index over 64 files at a 1 MiB budget seam returns the named error, leaves the project DB the same size on disk, and index_status still reports the previous node count. Refs DeusData#2020 DeusData#1997 DeusData#832 Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 699be6c commit d72250a

3 files changed

Lines changed: 214 additions & 1 deletion

File tree

‎src/mcp/mcp.c‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11209,6 +11209,49 @@ static char *handle_index_repository(cbm_mcp_server_t *srv, const char *args) {
1120911209
if (cbm_pipeline_had_format_migration(p)) {
1121011210
yyjson_mut_obj_add_bool(doc, root, "format_migration", true);
1121111211
}
11212+
} else if (rc == CBM_PIPELINE_ABORT_OVER_BUDGET) {
11213+
/* Decision A (#1997 #832): resident memory stayed above the budget
11214+
* after back-pressure and one confirmation cycle, so the run stopped
11215+
* before publication. Name the cause, the numbers and the fact that
11216+
* the previous index still serves — the generic "check repo_path"
11217+
* hint below sent people debugging a path that was fine. */
11218+
int budget_mb = (int)(cbm_mem_budget() / (1024 * 1024));
11219+
int peak_rss_mb = (int)(cbm_mem_peak_rss() / (1024 * 1024));
11220+
char budget_text[CBM_SZ_32];
11221+
char peak_text[CBM_SZ_32];
11222+
(void)snprintf(budget_text, sizeof(budget_text), "%d", budget_mb);
11223+
(void)snprintf(peak_text, sizeof(peak_text), "%d", peak_rss_mb);
11224+
cbm_log_error("index.abort", "reason", "over_memory_budget", "project", project_name,
11225+
"budget_mb", budget_text, "peak_rss_mb", peak_text);
11226+
yyjson_mut_obj_add_str(doc, root, "status", "error");
11227+
yyjson_mut_obj_add_str(doc, root, "reason", "over_memory_budget");
11228+
yyjson_mut_obj_add_str(doc, root, "previous_index", "preserved");
11229+
yyjson_mut_obj_add_int(doc, root, "budget_mb", budget_mb);
11230+
yyjson_mut_obj_add_int(doc, root, "peak_rss_mb", peak_rss_mb);
11231+
yyjson_mut_obj_add_str(doc, root, "hint",
11232+
"Indexing stopped: resident memory stayed above the budget after "
11233+
"backpressure; no partial graph was published and the previous "
11234+
"index still serves. Raise CBM_MEM_BUDGET_MB, lower CBM_WORKERS, "
11235+
"or exclude large subtrees.");
11236+
} else if (rc == CBM_PIPELINE_ABORT_PRESERVE_DB) {
11237+
/* The truthful abort message (#2020): the old generic "check repo_path"
11238+
* hint sent people debugging a path that was fine, when the run
11239+
* aborted pre-publication (semantic inputs changed mid-run, or a
11240+
* discovery/manifest phase failed transiently) and the previous index
11241+
* is intact. A 99-test cascade on the Windows leg traced back to
11242+
* exactly this — the response said error, but not which kind. */
11243+
yyjson_mut_obj_add_str(doc, root, "status", "aborted_previous_preserved");
11244+
yyjson_mut_obj_add_str(doc, root, "hint",
11245+
"Indexing aborted before publication; the previous index is "
11246+
"intact and still serving. Causes: files changed while the run "
11247+
"was in flight, or a discovery/manifest phase failed "
11248+
"transiently. Retry; if it repeats, check the run log.");
11249+
} else if (rc == CBM_PIPELINE_PERSIST_FAILED) {
11250+
yyjson_mut_obj_add_str(doc, root, "status", "persist_failed");
11251+
yyjson_mut_obj_add_str(doc, root, "hint",
11252+
"The validated staging database could not be published. Check "
11253+
"free disk space and permissions on the cache directory; the "
11254+
"previous index may have been rolled back.");
1121211255
} else {
1121311256
yyjson_mut_obj_add_str(doc, root, "status", "error");
1121411257
yyjson_mut_obj_add_str(doc, root, "hint",

‎tests/test_incremental.c‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,20 @@ static int reformat_files(const char *subdir, int max_files) {
107107
static char *index_repo(void) {
108108
char args[512];
109109
snprintf(args, sizeof(args), "{\"repo_path\":\"%s\"}", g_repodir);
110-
return cbm_mcp_handle_tool(g_srv, "index_repository", args);
110+
char *resp = cbm_mcp_handle_tool(g_srv, "index_repository", args);
111+
/* Fail-closed at the source: every caller asserts resp != NULL, but an
112+
* error response (e.g. a transient pre-publication abort that preserves
113+
* the previous index) used to slip through and cascade into dozens of
114+
* mysterious count/query failures downstream — a 99-test pile-up on the
115+
* Windows leg traced back to exactly this. Surface the real cause once,
116+
* here, and let the existing non-NULL asserts fail loudly. */
117+
if (resp && strstr(resp, "\"isError\":true")) {
118+
fprintf(stderr, " index_repo: index_repository returned an error response:\n %.400s\n",
119+
resp);
120+
free(resp);
121+
return NULL;
122+
}
123+
return resp;
111124
}
112125

113126
/* Timed index: returns response, sets *elapsed_ms and *peak_rss_mb */

‎tests/test_mcp.c‎

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "../src/foundation/constants.h"
1010
#include "../src/foundation/log.h"
1111
#include "../src/foundation/platform.h" /* cbm_file_size */
12+
#include "../src/foundation/mem.h" /* cbm_mem_set_budget_for_tests — over-budget seam */
1213
#include "../src/foundation/subprocess.h"
1314
#include "../src/foundation/workspace.h"
1415
#include "../src/mcp/compact_out.h"
@@ -17670,6 +17671,161 @@ static int idx823_supervised_name_override_check(const char *repo_dir, const cha
1767017671
}
1767117672
#endif
1767217673

17674+
/* Write the 64-file Go fixture (above MIN_FILES_FOR_PARALLEL, so the gated
17675+
* parallel extract path runs). generation 2 gives every file a second
17676+
* definition so a re-run must re-extract all of them — an unchanged repo is an
17677+
* incremental no-op and never reaches the memory gate. */
17678+
static bool budget_fixture_write(const char *repo, int generation) {
17679+
for (int i = 0; i < 64; i++) {
17680+
char path[CBM_SZ_1K];
17681+
char body[CBM_SZ_256];
17682+
snprintf(path, sizeof(path), "%s/f%02d.go", repo, i);
17683+
if (generation == 1) {
17684+
snprintf(body, sizeof(body), "package main\n\nfunc F%02d() int {\n\treturn %d\n}\n", i,
17685+
i);
17686+
} else {
17687+
snprintf(body, sizeof(body),
17688+
"package main\n\nfunc F%02d() int {\n\treturn %d\n}\n\n"
17689+
"func G%02d() int {\n\treturn F%02d() + 1\n}\n",
17690+
i, i, i, i);
17691+
}
17692+
if (th_write_file(path, body) != 0) {
17693+
return false;
17694+
}
17695+
}
17696+
return true;
17697+
}
17698+
17699+
/* Parse the JSON object inside an MCP tool result. NULL when absent. */
17700+
static yyjson_doc *budget_result_doc(const char *result) {
17701+
char *text = extract_text_content(result);
17702+
yyjson_doc *doc = text ? yyjson_read(text, strlen(text), 0) : NULL;
17703+
free(text);
17704+
return doc;
17705+
}
17706+
17707+
static const char *budget_doc_str(yyjson_doc *doc, const char *key) {
17708+
return doc ? yyjson_get_str(yyjson_obj_get(yyjson_doc_get_root(doc), key)) : NULL;
17709+
}
17710+
17711+
static int budget_doc_int(yyjson_doc *doc, const char *key, int missing) {
17712+
yyjson_val *val = doc ? yyjson_obj_get(yyjson_doc_get_root(doc), key) : NULL;
17713+
return val && yyjson_is_int(val) ? (int)yyjson_get_int(val) : missing;
17714+
}
17715+
17716+
/* Decision A (#1997 #832): an attempt that stays over the memory budget after
17717+
* back-pressure fails WHOLE and the response says so — reason, numbers, hint,
17718+
* and that the previous index still serves — instead of the generic "check
17719+
* repo_path" error. In-process: the runner is never a marked supervisor host,
17720+
* and the budget seam stands in for a 1 MiB machine. */
17721+
TEST(index_repository_over_budget_reports_named_reason) {
17722+
char repo[CBM_SZ_1K];
17723+
char cache[CBM_SZ_1K];
17724+
snprintf(repo, sizeof(repo), "%s/cbm-mcp-budget-repo-XXXXXX", cbm_tmpdir());
17725+
snprintf(cache, sizeof(cache), "%s/cbm-mcp-budget-cache-XXXXXX", cbm_tmpdir());
17726+
if (!cbm_mkdtemp(repo)) {
17727+
FAIL("cbm_mkdtemp repo failed");
17728+
}
17729+
if (!cbm_mkdtemp(cache)) {
17730+
th_rmtree(repo);
17731+
FAIL("cbm_mkdtemp cache failed");
17732+
}
17733+
const char *saved_cache = getenv("CBM_CACHE_DIR");
17734+
char *saved_cache_copy = saved_cache ? strdup(saved_cache) : NULL;
17735+
const char *saved_workers = getenv("CBM_WORKERS");
17736+
char *saved_workers_copy = saved_workers ? strdup(saved_workers) : NULL;
17737+
cbm_setenv("CBM_CACHE_DIR", cache, 1);
17738+
cbm_setenv("CBM_WORKERS", "4", 1);
17739+
17740+
bool files_ok = budget_fixture_write(repo, 1);
17741+
cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL);
17742+
bool had_server = srv != NULL;
17743+
char args[CBM_SZ_2K];
17744+
snprintf(args, sizeof(args), "{\"repo_path\":\"%s\"}", repo);
17745+
17746+
/* Generation 1 at the process budget publishes. */
17747+
char *first = srv && files_ok ? cbm_mcp_handle_tool(srv, "index_repository", args) : NULL;
17748+
yyjson_doc *first_doc = budget_result_doc(first);
17749+
const char *first_status = budget_doc_str(first_doc, "status");
17750+
bool first_indexed = first_status && strcmp(first_status, "indexed") == 0;
17751+
int nodes_first = budget_doc_int(first_doc, "nodes", -1);
17752+
const char *first_project = budget_doc_str(first_doc, "project");
17753+
char *project = first_project ? strdup(first_project) : NULL;
17754+
yyjson_doc_free(first_doc);
17755+
free(first);
17756+
char db_path[CBM_SZ_2K];
17757+
snprintf(db_path, sizeof(db_path), "%s/%s.db", cache, project ? project : "missing");
17758+
long db_size_before = (long)cbm_file_size(db_path);
17759+
17760+
/* Generation 2 at 1 MiB over a changed repo fails whole. */
17761+
files_ok = files_ok && budget_fixture_write(repo, 2);
17762+
size_t saved_budget = cbm_mem_budget();
17763+
cbm_mem_set_budget_for_tests((size_t)1024 * 1024);
17764+
char *second = srv && files_ok ? cbm_mcp_handle_tool(srv, "index_repository", args) : NULL;
17765+
cbm_mem_set_budget_for_tests(saved_budget);
17766+
bool second_is_error = second && strstr(second, "\"isError\":true") != NULL;
17767+
yyjson_doc *second_doc = budget_result_doc(second);
17768+
const char *second_status = budget_doc_str(second_doc, "status");
17769+
const char *second_reason = budget_doc_str(second_doc, "reason");
17770+
const char *second_previous = budget_doc_str(second_doc, "previous_index");
17771+
const char *second_hint = budget_doc_str(second_doc, "hint");
17772+
bool status_error = second_status && strcmp(second_status, "error") == 0;
17773+
bool reason_named = second_reason && strcmp(second_reason, "over_memory_budget") == 0;
17774+
bool previous_preserved = second_previous && strcmp(second_previous, "preserved") == 0;
17775+
bool hint_names_knob = second_hint && strstr(second_hint, "CBM_MEM_BUDGET_MB") != NULL;
17776+
int budget_mb = budget_doc_int(second_doc, "budget_mb", -1);
17777+
int peak_rss_mb = budget_doc_int(second_doc, "peak_rss_mb", -1);
17778+
yyjson_doc_free(second_doc);
17779+
free(second);
17780+
long db_size_after = (long)cbm_file_size(db_path);
17781+
17782+
/* The previous generation still answers with its full graph. */
17783+
char status_args[CBM_SZ_2K];
17784+
snprintf(status_args, sizeof(status_args), "{\"project\":\"%s\"}", project ? project : "");
17785+
char *status = srv && project ? cbm_mcp_handle_tool(srv, "index_status", status_args) : NULL;
17786+
/* index_status answers in the tree format ("nodes: <n>" on its own line). */
17787+
char *status_text = extract_text_content(status);
17788+
const char *nodes_line = status_text ? strstr(status_text, "\nnodes: ") : NULL;
17789+
int nodes_after = nodes_line ? (int)strtol(nodes_line + strlen("\nnodes: "), NULL, 10) : -1;
17790+
if (nodes_after != nodes_first) {
17791+
printf(" index_status after the failed attempt: %.400s\n",
17792+
status_text ? status_text : "(null)");
17793+
}
17794+
free(status_text);
17795+
free(status);
17796+
17797+
cbm_mcp_server_free(srv);
17798+
cleanup_project_db(cache, project);
17799+
free(project);
17800+
if (saved_workers_copy) {
17801+
cbm_setenv("CBM_WORKERS", saved_workers_copy, 1);
17802+
} else {
17803+
cbm_unsetenv("CBM_WORKERS");
17804+
}
17805+
free(saved_workers_copy);
17806+
restore_cache_dir(saved_cache_copy);
17807+
free(saved_cache_copy);
17808+
th_rmtree(cache);
17809+
th_rmtree(repo);
17810+
17811+
ASSERT_TRUE(files_ok);
17812+
ASSERT_TRUE(had_server);
17813+
ASSERT_TRUE(first_indexed);
17814+
ASSERT_GT(nodes_first, 0);
17815+
ASSERT_TRUE(second_is_error);
17816+
ASSERT_TRUE(status_error);
17817+
ASSERT_TRUE(reason_named);
17818+
ASSERT_TRUE(previous_preserved);
17819+
ASSERT_TRUE(hint_names_knob);
17820+
ASSERT_EQ(budget_mb, 1);
17821+
ASSERT_GT(peak_rss_mb, budget_mb);
17822+
/* Preserved on disk and still served: same file size, same node count. */
17823+
ASSERT_GT(db_size_before, 0L);
17824+
ASSERT_EQ(db_size_after, db_size_before);
17825+
ASSERT_EQ(nodes_after, nodes_first);
17826+
PASS();
17827+
}
17828+
1767317829
TEST(index_repository_cli_name_override_issue823) {
1767417830
#ifdef _WIN32
1767517831
SKIP_PLATFORM("POSIX fork harness required to isolate supervisor host mark");
@@ -20238,6 +20394,7 @@ SUITE(mcp) {
2023820394
RUN_TEST(index_repository_relative_path_uses_explicit_session_root);
2023920395
RUN_TEST(index_repository_supervisor_uses_canonical_session_path);
2024020396
RUN_TEST(index_repository_cli_name_override_issue823);
20397+
RUN_TEST(index_repository_over_budget_reports_named_reason);
2024120398
RUN_TEST(index_supervisor_unsafe_clean_is_never_fallback_or_recovery);
2024220399
RUN_TEST(index_supervisor_gate_requires_marked_host_issue845);
2024320400
RUN_TEST(index_supervisor_start_failure_is_fail_closed_in_real_host);

0 commit comments

Comments
 (0)