@@ -123,6 +123,7 @@ enum {
123123 * growing getline buffers without bound through ignored extension headers. */
124124#define MCP_MAX_MESSAGE_SIZE ((size_t)10U * 1024U * 1024U)
125125#define MCP_MAX_HEADER_SIZE ((size_t)8U * 1024U)
126+ #define MCP_SEARCH_OUTPUT_MAX ((size_t)64U * 1024U * 1024U)
126127
127128/* ── Helpers ────────────────────────────────────────────────────── */
128129
@@ -1599,6 +1600,7 @@ struct cbm_mcp_server {
15991600 void *quarantine_test_context;
16001601 cbm_mcp_command_test_hook_fn command_test_hook;
16011602 void *command_test_context;
1603+ size_t search_output_limit_override;
16021604 cbm_thread_t autoindex_tid;
16031605 bool autoindex_active; /* true if auto-index thread was started */
16041606
@@ -1897,6 +1899,12 @@ void cbm_mcp_server_set_command_test_hook(cbm_mcp_server_t *srv, cbm_mcp_command
18971899 srv->command_test_context = context;
18981900}
18991901
1902+ void cbm_mcp_server_set_search_output_limit_for_test(cbm_mcp_server_t *srv, size_t limit) {
1903+ if (srv) {
1904+ srv->search_output_limit_override = limit;
1905+ }
1906+ }
1907+
19001908/* ── Cache dir + project DB path helpers ───────────────────────── */
19011909
19021910/* Returns the cache directory. Writes to buf, returns buf for convenience. */
@@ -7363,6 +7371,54 @@ static void add_parse_partial_summary(yyjson_mut_doc *doc, yyjson_mut_val *root,
73637371 yyjson_mut_obj_add_val(doc, root, "parse_partial", pp);
73647372}
73657373
7374+ /* The pipeline persists the complete current coverage set before this
7375+ * response is built. Prefer that set over the per-run errors so incremental
7376+ * runs that do not revisit a flagged file, and artifact bootstraps, do not
7377+ * make existing gaps appear to have vanished. By-design exclusions have
7378+ * their own response surface and are not failures. */
7379+ static bool add_persisted_failure_summaries(yyjson_mut_doc *doc, yyjson_mut_val *root,
7380+ cbm_store_t *store, const char *project,
7381+ const char *logfile) {
7382+ cbm_coverage_row_t *rows = NULL;
7383+ int row_count = 0;
7384+ if (cbm_store_coverage_get(store, project, &rows, &row_count) != CBM_STORE_OK) {
7385+ return false;
7386+ }
7387+
7388+ int failure_count = 0;
7389+ for (int i = 0; i < row_count; i++) {
7390+ const char *kind = rows[i].kind ? rows[i].kind : "";
7391+ if (strcmp(kind, "not_indexed_dir") != 0 && strcmp(kind, "not_indexed_file") != 0) {
7392+ failure_count++;
7393+ }
7394+ }
7395+
7396+ cbm_file_error_t *failures =
7397+ failure_count > 0 ? calloc((size_t)failure_count, sizeof(*failures)) : NULL;
7398+ if (failure_count > 0 && !failures) {
7399+ cbm_store_free_coverage(rows, row_count);
7400+ return false;
7401+ }
7402+
7403+ int n = 0;
7404+ for (int i = 0; i < row_count; i++) {
7405+ const char *kind = rows[i].kind ? rows[i].kind : "";
7406+ if (strcmp(kind, "not_indexed_dir") == 0 || strcmp(kind, "not_indexed_file") == 0) {
7407+ continue;
7408+ }
7409+ failures[n].path = (char *)rows[i].rel_path;
7410+ failures[n].reason = (char *)rows[i].detail;
7411+ failures[n].phase = (char *)rows[i].kind;
7412+ n++;
7413+ }
7414+
7415+ add_skipped_summary(doc, root, failures, failure_count, logfile);
7416+ add_parse_partial_summary(doc, root, failures, failure_count);
7417+ free(failures);
7418+ cbm_store_free_coverage(rows, row_count);
7419+ return true;
7420+ }
7421+
73667422/* Write the FULL (uncapped) skip list to a per-run logfile — ONLY when >=1 file
73677423 * was skipped (no logfile on a clean run). Location:
73687424 * $CBM_INDEX_LOG (override) else <cache_dir>/logs/<project>-<epoch>.log
@@ -7422,8 +7478,6 @@ static bool build_index_success_response(cbm_mcp_server_t *srv, yyjson_mut_doc *
74227478 const cbm_file_error_t *file_errors, int file_error_count,
74237479 const char *logfile) {
74247480 add_excluded_summary(doc, root, excluded_dirs, excluded_count);
7425- add_skipped_summary(doc, root, file_errors, file_error_count, logfile);
7426- add_parse_partial_summary(doc, root, file_errors, file_error_count);
74277481 add_not_indexed_files_summary(doc, root, p);
74287482
74297483 int exp_nodes = -1;
@@ -7434,6 +7488,10 @@ static bool build_index_success_response(cbm_mcp_server_t *srv, yyjson_mut_doc *
74347488 const int min_floor = CBM_DUMP_VERIFY_MIN_FLOOR;
74357489
74367490 cbm_store_t *store = resolve_store(srv, project_name);
7491+ if (!store || !add_persisted_failure_summaries(doc, root, store, project_name, logfile)) {
7492+ add_skipped_summary(doc, root, file_errors, file_error_count, logfile);
7493+ add_parse_partial_summary(doc, root, file_errors, file_error_count);
7494+ }
74377495 int nodes = 0;
74387496 int edges = 0;
74397497 bool degraded = false;
@@ -9803,6 +9861,32 @@ static bool compile_path_filter(const char *filter, cbm_regex_t *re) {
98039861 return cbm_regcomp(re, filter, CBM_REG_EXTENDED | CBM_REG_NOSUB) == CBM_REG_OK;
98049862}
98059863
9864+ static int mcp_run_shell_command_cancellable_bounded(cbm_mcp_server_t *srv, const char *command,
9865+ char output_path[CBM_SZ_2K],
9866+ size_t output_limit,
9867+ bool *output_limit_exceeded,
9868+ cbm_proc_result_t *result_out);
9869+
9870+ #ifdef _WIN32
9871+ static char *search_code_scan_error(search_scratch_t *scratch, const char *output_path,
9872+ bool has_path_filter, cbm_regex_t *path_regex, char *root_path,
9873+ char *pattern, char *project, char *file_pattern,
9874+ const char *message) {
9875+ if (output_path && output_path[0]) {
9876+ (void)cbm_unlink(output_path);
9877+ }
9878+ search_scratch_close(scratch);
9879+ if (has_path_filter) {
9880+ cbm_regfree(path_regex);
9881+ }
9882+ free(root_path);
9883+ free(pattern);
9884+ free(project);
9885+ free(file_pattern);
9886+ return cbm_mcp_text_result(message, true);
9887+ }
9888+ #endif
9889+
98069890static char *handle_search_code(cbm_mcp_server_t *srv, const char *args) {
98079891 char *pattern = cbm_mcp_get_string_arg(args, "pattern");
98089892 char *project = get_project_arg(args);
@@ -9986,6 +10070,44 @@ static char *handle_search_code(cbm_mcp_server_t *srv, const char *args) {
998610070 cbm_search_code_build_grep_cmd(cmd, sizeof(cmd), use_regex, scoped, file_pattern, tmpfile,
998710071 filelist, root_path);
998810072
10073+ #ifdef _WIN32
10074+ char output_path[CBM_SZ_2K] = {0};
10075+ cbm_proc_result_t scan_result = {0};
10076+ bool scan_output_exceeded = false;
10077+ size_t scan_output_limit = srv->search_output_limit_override
10078+ ? srv->search_output_limit_override
10079+ : MCP_SEARCH_OUTPUT_MAX;
10080+ int scan_run = mcp_run_shell_command_cancellable_bounded(
10081+ srv, cmd, output_path, scan_output_limit, &scan_output_exceeded, &scan_result);
10082+ if (scan_output_exceeded) {
10083+ char message[CBM_SZ_128];
10084+ snprintf(message, sizeof(message),
10085+ "search failed: output exceeded the %zu-byte safety limit", scan_output_limit);
10086+ return search_code_scan_error(&scratch, output_path, has_path_filter, &path_regex,
10087+ root_path, pattern, project, file_pattern, message);
10088+ }
10089+ bool scan_cancelled = scan_result.cancellation_requested || mcp_request_cancelled(srv);
10090+ if (scan_cancelled) {
10091+ return search_code_scan_error(&scratch, output_path, has_path_filter, &path_regex,
10092+ root_path, pattern, project, file_pattern,
10093+ "search_code cancelled for this request");
10094+ }
10095+ if (scan_run != 0) {
10096+ return search_code_scan_error(
10097+ &scratch, output_path, has_path_filter, &path_regex, root_path, pattern, project,
10098+ file_pattern, "search failed: the contained command could not complete");
10099+ }
10100+ FILE *fp = cbm_fopen(output_path, "rb");
10101+ if (!fp) {
10102+ return search_code_scan_error(&scratch, output_path, has_path_filter, &path_regex,
10103+ root_path, pattern, project, file_pattern,
10104+ "search failed: contained output could not be read");
10105+ }
10106+ gm = collect_grep_matches(fp, root_path, strlen(root_path), has_path_filter, &path_regex,
10107+ grep_limit, &gm_count);
10108+ (void)fclose(fp);
10109+ (void)cbm_unlink(output_path);
10110+ #else
998910111 FILE *fp = cbm_popen(cmd, "r");
999010112 if (!fp) {
999110113 search_scratch_close(&scratch);
@@ -10002,6 +10124,7 @@ static char *handle_search_code(cbm_mcp_server_t *srv, const char *args) {
1000210124 gm = collect_grep_matches(fp, root_path, strlen(root_path), has_path_filter, &path_regex,
1000310125 grep_limit, &gm_count);
1000410126 cbm_pclose(fp);
10127+ #endif
1000510128 /* Both scratch files and the private directory go here — unlike the old
1000610129 * code, the file list is removed even when the scan was not scoped. */
1000710130 search_scratch_close(&scratch);
@@ -10162,10 +10285,16 @@ static bool mcp_resolve_windows_cmd(char out[CBM_SZ_4K]) {
1016210285}
1016310286#endif
1016410287
10165- static int mcp_run_shell_command_cancellable(cbm_mcp_server_t *srv, const char *command,
10166- char output_path[CBM_SZ_2K],
10167- cbm_proc_result_t *result_out) {
10168- if (!srv || !command || !output_path || !result_out || !mcp_command_output_path(output_path)) {
10288+ static int mcp_run_shell_command_cancellable_bounded(cbm_mcp_server_t *srv, const char *command,
10289+ char output_path[CBM_SZ_2K],
10290+ size_t output_limit,
10291+ bool *output_limit_exceeded,
10292+ cbm_proc_result_t *result_out) {
10293+ if (output_limit_exceeded) {
10294+ *output_limit_exceeded = false;
10295+ }
10296+ if (!srv || !command || !output_path || !result_out ||
10297+ (output_limit > 0 && !output_limit_exceeded) || !mcp_command_output_path(output_path)) {
1016910298 return -1;
1017010299 }
1017110300 /* Internal test seam: rejecting after output allocation exercises the same
@@ -10204,10 +10333,18 @@ static int mcp_run_shell_command_cancellable(cbm_mcp_server_t *srv, const char *
1020410333 }
1020510334
1020610335 cbm_proc_poll_t state;
10336+ bool limit_exceeded = false;
1020710337 for (;;) {
1020810338 if (mcp_request_cancelled(srv)) {
1020910339 (void)cbm_subprocess_request_cancel(process);
1021010340 }
10341+ if (!limit_exceeded && output_limit > 0) {
10342+ int64_t output_size = cbm_file_size(output_path);
10343+ if (output_size > 0 && (uint64_t)output_size > output_limit) {
10344+ limit_exceeded = true;
10345+ (void)cbm_subprocess_request_cancel(process);
10346+ }
10347+ }
1021110348 state = cbm_subprocess_poll(process, result_out);
1021210349 if (state != CBM_PROC_POLL_RUNNING) {
1021310350 break;
@@ -10217,9 +10354,23 @@ static int mcp_run_shell_command_cancellable(cbm_mcp_server_t *srv, const char *
1021710354 bool contained = state == CBM_PROC_POLL_TERMINAL && result_out->tree_quiesced &&
1021810355 !result_out->supervision_failed;
1021910356 cbm_subprocess_destroy(process);
10357+ if (!limit_exceeded && output_limit > 0) {
10358+ int64_t final_size = cbm_file_size(output_path);
10359+ limit_exceeded = final_size > 0 && (uint64_t)final_size > output_limit;
10360+ }
10361+ if (output_limit_exceeded) {
10362+ *output_limit_exceeded = limit_exceeded;
10363+ }
1022010364 return contained ? 0 : -1;
1022110365}
1022210366
10367+ static int mcp_run_shell_command_cancellable(cbm_mcp_server_t *srv, const char *command,
10368+ char output_path[CBM_SZ_2K],
10369+ cbm_proc_result_t *result_out) {
10370+ return mcp_run_shell_command_cancellable_bounded(srv, command, output_path, 0, NULL,
10371+ result_out);
10372+ }
10373+
1022310374/* Does `node`'s line range overlap any recorded hunk for `file`? Used to scope
1022410375 * seed detection to the actually-changed lines rather than the whole file.
1022510376 * Non-static (declared in mcp_internal.h) so tests can exercise the overlap
0 commit comments