Skip to content

Commit 00beec9

Browse files
authored
Merge pull request DeusData#1608 from ertankucukoglu/fix/search-code-cancellation
fix(mcp): bound and cancel Windows code search
2 parents 2973ab7 + 847da2f commit 00beec9

3 files changed

Lines changed: 240 additions & 4 deletions

File tree

‎src/mcp/mcp.c‎

Lines changed: 105 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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

@@ -1575,6 +1576,7 @@ struct cbm_mcp_server {
15751576
void *quarantine_test_context;
15761577
cbm_mcp_command_test_hook_fn command_test_hook;
15771578
void *command_test_context;
1579+
size_t search_output_limit_override;
15781580
cbm_thread_t autoindex_tid;
15791581
bool autoindex_active; /* true if auto-index thread was started */
15801582

@@ -1873,6 +1875,12 @@ void cbm_mcp_server_set_command_test_hook(cbm_mcp_server_t *srv, cbm_mcp_command
18731875
srv->command_test_context = context;
18741876
}
18751877

1878+
void cbm_mcp_server_set_search_output_limit_for_test(cbm_mcp_server_t *srv, size_t limit) {
1879+
if (srv) {
1880+
srv->search_output_limit_override = limit;
1881+
}
1882+
}
1883+
18761884
/* ── Cache dir + project DB path helpers ───────────────────────── */
18771885

18781886
/* Returns the cache directory. Writes to buf, returns buf for convenience. */
@@ -9779,6 +9787,32 @@ static bool compile_path_filter(const char *filter, cbm_regex_t *re) {
97799787
return cbm_regcomp(re, filter, CBM_REG_EXTENDED | CBM_REG_NOSUB) == CBM_REG_OK;
97809788
}
97819789

9790+
static int mcp_run_shell_command_cancellable_bounded(cbm_mcp_server_t *srv, const char *command,
9791+
char output_path[CBM_SZ_2K],
9792+
size_t output_limit,
9793+
bool *output_limit_exceeded,
9794+
cbm_proc_result_t *result_out);
9795+
9796+
#ifdef _WIN32
9797+
static char *search_code_scan_error(search_scratch_t *scratch, const char *output_path,
9798+
bool has_path_filter, cbm_regex_t *path_regex, char *root_path,
9799+
char *pattern, char *project, char *file_pattern,
9800+
const char *message) {
9801+
if (output_path && output_path[0]) {
9802+
(void)cbm_unlink(output_path);
9803+
}
9804+
search_scratch_close(scratch);
9805+
if (has_path_filter) {
9806+
cbm_regfree(path_regex);
9807+
}
9808+
free(root_path);
9809+
free(pattern);
9810+
free(project);
9811+
free(file_pattern);
9812+
return cbm_mcp_text_result(message, true);
9813+
}
9814+
#endif
9815+
97829816
static char *handle_search_code(cbm_mcp_server_t *srv, const char *args) {
97839817
char *pattern = cbm_mcp_get_string_arg(args, "pattern");
97849818
char *project = get_project_arg(args);
@@ -9962,6 +9996,44 @@ static char *handle_search_code(cbm_mcp_server_t *srv, const char *args) {
99629996
cbm_search_code_build_grep_cmd(cmd, sizeof(cmd), use_regex, scoped, file_pattern, tmpfile,
99639997
filelist, root_path);
99649998

9999+
#ifdef _WIN32
10000+
char output_path[CBM_SZ_2K] = {0};
10001+
cbm_proc_result_t scan_result = {0};
10002+
bool scan_output_exceeded = false;
10003+
size_t scan_output_limit = srv->search_output_limit_override
10004+
? srv->search_output_limit_override
10005+
: MCP_SEARCH_OUTPUT_MAX;
10006+
int scan_run = mcp_run_shell_command_cancellable_bounded(
10007+
srv, cmd, output_path, scan_output_limit, &scan_output_exceeded, &scan_result);
10008+
if (scan_output_exceeded) {
10009+
char message[CBM_SZ_128];
10010+
snprintf(message, sizeof(message),
10011+
"search failed: output exceeded the %zu-byte safety limit", scan_output_limit);
10012+
return search_code_scan_error(&scratch, output_path, has_path_filter, &path_regex,
10013+
root_path, pattern, project, file_pattern, message);
10014+
}
10015+
bool scan_cancelled = scan_result.cancellation_requested || mcp_request_cancelled(srv);
10016+
if (scan_cancelled) {
10017+
return search_code_scan_error(&scratch, output_path, has_path_filter, &path_regex,
10018+
root_path, pattern, project, file_pattern,
10019+
"search_code cancelled for this request");
10020+
}
10021+
if (scan_run != 0) {
10022+
return search_code_scan_error(
10023+
&scratch, output_path, has_path_filter, &path_regex, root_path, pattern, project,
10024+
file_pattern, "search failed: the contained command could not complete");
10025+
}
10026+
FILE *fp = cbm_fopen(output_path, "rb");
10027+
if (!fp) {
10028+
return search_code_scan_error(&scratch, output_path, has_path_filter, &path_regex,
10029+
root_path, pattern, project, file_pattern,
10030+
"search failed: contained output could not be read");
10031+
}
10032+
gm = collect_grep_matches(fp, root_path, strlen(root_path), has_path_filter, &path_regex,
10033+
grep_limit, &gm_count);
10034+
(void)fclose(fp);
10035+
(void)cbm_unlink(output_path);
10036+
#else
996510037
FILE *fp = cbm_popen(cmd, "r");
996610038
if (!fp) {
996710039
search_scratch_close(&scratch);
@@ -9978,6 +10050,7 @@ static char *handle_search_code(cbm_mcp_server_t *srv, const char *args) {
997810050
gm = collect_grep_matches(fp, root_path, strlen(root_path), has_path_filter, &path_regex,
997910051
grep_limit, &gm_count);
998010052
cbm_pclose(fp);
10053+
#endif
998110054
/* Both scratch files and the private directory go here — unlike the old
998210055
* code, the file list is removed even when the scan was not scoped. */
998310056
search_scratch_close(&scratch);
@@ -10138,10 +10211,16 @@ static bool mcp_resolve_windows_cmd(char out[CBM_SZ_4K]) {
1013810211
}
1013910212
#endif
1014010213

10141-
static int mcp_run_shell_command_cancellable(cbm_mcp_server_t *srv, const char *command,
10142-
char output_path[CBM_SZ_2K],
10143-
cbm_proc_result_t *result_out) {
10144-
if (!srv || !command || !output_path || !result_out || !mcp_command_output_path(output_path)) {
10214+
static int mcp_run_shell_command_cancellable_bounded(cbm_mcp_server_t *srv, const char *command,
10215+
char output_path[CBM_SZ_2K],
10216+
size_t output_limit,
10217+
bool *output_limit_exceeded,
10218+
cbm_proc_result_t *result_out) {
10219+
if (output_limit_exceeded) {
10220+
*output_limit_exceeded = false;
10221+
}
10222+
if (!srv || !command || !output_path || !result_out ||
10223+
(output_limit > 0 && !output_limit_exceeded) || !mcp_command_output_path(output_path)) {
1014510224
return -1;
1014610225
}
1014710226
/* Internal test seam: rejecting after output allocation exercises the same
@@ -10180,10 +10259,18 @@ static int mcp_run_shell_command_cancellable(cbm_mcp_server_t *srv, const char *
1018010259
}
1018110260

1018210261
cbm_proc_poll_t state;
10262+
bool limit_exceeded = false;
1018310263
for (;;) {
1018410264
if (mcp_request_cancelled(srv)) {
1018510265
(void)cbm_subprocess_request_cancel(process);
1018610266
}
10267+
if (!limit_exceeded && output_limit > 0) {
10268+
int64_t output_size = cbm_file_size(output_path);
10269+
if (output_size > 0 && (uint64_t)output_size > output_limit) {
10270+
limit_exceeded = true;
10271+
(void)cbm_subprocess_request_cancel(process);
10272+
}
10273+
}
1018710274
state = cbm_subprocess_poll(process, result_out);
1018810275
if (state != CBM_PROC_POLL_RUNNING) {
1018910276
break;
@@ -10193,9 +10280,23 @@ static int mcp_run_shell_command_cancellable(cbm_mcp_server_t *srv, const char *
1019310280
bool contained = state == CBM_PROC_POLL_TERMINAL && result_out->tree_quiesced &&
1019410281
!result_out->supervision_failed;
1019510282
cbm_subprocess_destroy(process);
10283+
if (!limit_exceeded && output_limit > 0) {
10284+
int64_t final_size = cbm_file_size(output_path);
10285+
limit_exceeded = final_size > 0 && (uint64_t)final_size > output_limit;
10286+
}
10287+
if (output_limit_exceeded) {
10288+
*output_limit_exceeded = limit_exceeded;
10289+
}
1019610290
return contained ? 0 : -1;
1019710291
}
1019810292

10293+
static int mcp_run_shell_command_cancellable(cbm_mcp_server_t *srv, const char *command,
10294+
char output_path[CBM_SZ_2K],
10295+
cbm_proc_result_t *result_out) {
10296+
return mcp_run_shell_command_cancellable_bounded(srv, command, output_path, 0, NULL,
10297+
result_out);
10298+
}
10299+
1019910300
/* Does `node`'s line range overlap any recorded hunk for `file`? Used to scope
1020010301
* seed detection to the actually-changed lines rather than the whole file.
1020110302
* Non-static (declared in mcp_internal.h) so tests can exercise the overlap

‎src/mcp/mcp_internal.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ void cbm_mcp_server_set_quarantine_test_hook(cbm_mcp_server_t *srv,
1414
cbm_mcp_quarantine_test_hook_fn hook, void *context);
1515
void cbm_mcp_server_set_command_test_hook(cbm_mcp_server_t *srv, cbm_mcp_command_test_hook_fn hook,
1616
void *context);
17+
void cbm_mcp_server_set_search_output_limit_for_test(cbm_mcp_server_t *srv, size_t limit);
1718

1819
/* Release only the constructor-created pristine in-memory store. Public
1920
* cbm_mcp_server_new(NULL) semantics remain unchanged; daemon sessions use

‎tests/test_mcp.c‎

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,21 @@ typedef struct {
162162
int merge_base_calls;
163163
} mcp_command_hook_probe_t;
164164

165+
#ifdef _WIN32
166+
typedef struct {
167+
cbm_mcp_server_t *server;
168+
bool cancel_on_call;
169+
bool cancel_accepted;
170+
int calls;
171+
char command[CBM_SZ_4K];
172+
} mcp_search_command_probe_t;
173+
174+
typedef struct {
175+
char path[512];
176+
char *saved_cache;
177+
} mcp_search_cache_t;
178+
#endif
179+
165180
static bool mcp_quarantine_hook_probe(void *context, const char *step) {
166181
mcp_quarantine_hook_probe_t *probe = context;
167182
if (!probe || !step) {
@@ -187,6 +202,45 @@ static bool mcp_command_hook_probe(void *context, const char *command) {
187202
return true;
188203
}
189204

205+
#ifdef _WIN32
206+
static bool mcp_search_command_hook_probe(void *context, const char *command) {
207+
mcp_search_command_probe_t *probe = context;
208+
if (!probe || !command) {
209+
return false;
210+
}
211+
probe->calls++;
212+
snprintf(probe->command, sizeof(probe->command), "%s", command);
213+
if (probe->cancel_on_call && probe->server) {
214+
probe->cancel_accepted = cbm_mcp_server_cancel_active(probe->server);
215+
}
216+
return true;
217+
}
218+
219+
static bool mcp_search_cache_open(mcp_search_cache_t *cache, const char *prefix) {
220+
memset(cache, 0, sizeof(*cache));
221+
snprintf(cache->path, sizeof(cache->path), "%s/%s-XXXXXX", cbm_tmpdir(), prefix);
222+
if (!cbm_mkdtemp(cache->path)) {
223+
return false;
224+
}
225+
const char *saved_cache = getenv("CBM_CACHE_DIR");
226+
cache->saved_cache = saved_cache ? strdup(saved_cache) : NULL;
227+
if ((saved_cache && !cache->saved_cache) || cbm_setenv("CBM_CACHE_DIR", cache->path, 1) != 0) {
228+
free(cache->saved_cache);
229+
cache->saved_cache = NULL;
230+
(void)th_rmtree(cache->path);
231+
return false;
232+
}
233+
return true;
234+
}
235+
236+
static bool mcp_search_cache_close(mcp_search_cache_t *cache) {
237+
restore_cache_dir(cache->saved_cache);
238+
free(cache->saved_cache);
239+
cache->saved_cache = NULL;
240+
return th_rmtree(cache->path) == 0;
241+
}
242+
#endif
243+
190244
typedef struct {
191245
const char *name;
192246
char *value;
@@ -4918,6 +4972,84 @@ TEST(search_code_windows_prefilter_precedes_content_scan) {
49184972
#endif
49194973
}
49204974

4975+
TEST(search_code_windows_cancel_cleans_supervised_scan) {
4976+
#ifdef _WIN32
4977+
mcp_search_cache_t cache;
4978+
ASSERT_TRUE(mcp_search_cache_open(&cache, "cbm-search-cancel"));
4979+
4980+
char tmp[512], src_path[768], vendor_path[768];
4981+
cbm_mcp_server_t *srv = setup_prefilter_server(tmp, sizeof(tmp), src_path, sizeof(src_path),
4982+
vendor_path, sizeof(vendor_path));
4983+
ASSERT_NOT_NULL(srv);
4984+
mcp_search_command_probe_t probe = {
4985+
.server = srv,
4986+
.cancel_on_call = true,
4987+
};
4988+
cbm_mcp_server_set_command_test_hook(srv, mcp_search_command_hook_probe, &probe);
4989+
4990+
char *response =
4991+
cbm_mcp_handle_tool(srv, "search_code",
4992+
"{\"pattern\":\"HandleRequest\",\"project\":\"prefilter-search\","
4993+
"\"file_pattern\":\"*.go\"}");
4994+
ASSERT_NOT_NULL(response);
4995+
ASSERT_TRUE(probe.cancel_accepted);
4996+
ASSERT_NOT_NULL(strstr(response, "cancelled"));
4997+
ASSERT_NOT_NULL(strstr(response, "\"isError\":true"));
4998+
4999+
char logs[640];
5000+
snprintf(logs, sizeof(logs), "%s/logs", cache.path);
5001+
ASSERT_EQ(mcp_count_directory_entries_with_prefix(logs, ".mcp-command-"), 0);
5002+
5003+
free(response);
5004+
cbm_mcp_server_free(srv);
5005+
cleanup_prefilter_dir(tmp, src_path, vendor_path);
5006+
ASSERT_TRUE(mcp_search_cache_close(&cache));
5007+
PASS();
5008+
#else
5009+
SKIP_PLATFORM("supervised Select-String cancellation runs on Windows");
5010+
#endif
5011+
}
5012+
5013+
TEST(search_code_windows_output_limit_fails_closed_and_cleans_scan) {
5014+
#ifdef _WIN32
5015+
mcp_search_cache_t cache;
5016+
ASSERT_TRUE(mcp_search_cache_open(&cache, "cbm-search-limit"));
5017+
5018+
char tmp[512], src_path[768], vendor_path[768];
5019+
cbm_mcp_server_t *srv = setup_prefilter_server(tmp, sizeof(tmp), src_path, sizeof(src_path),
5020+
vendor_path, sizeof(vendor_path));
5021+
ASSERT_NOT_NULL(srv);
5022+
cbm_mcp_server_set_search_output_limit_for_test(srv, 512);
5023+
5024+
FILE *source = cbm_fopen(src_path, "ab");
5025+
ASSERT_NOT_NULL(source);
5026+
for (int i = 0; i < 256; i++) {
5027+
ASSERT_GT(fprintf(source, "func HandleRequest%d() error { return nil }\n", i), 0);
5028+
}
5029+
ASSERT_EQ(fclose(source), 0);
5030+
5031+
char *response =
5032+
cbm_mcp_handle_tool(srv, "search_code",
5033+
"{\"pattern\":\"HandleRequest\",\"project\":\"prefilter-search\","
5034+
"\"file_pattern\":\"*.go\"}");
5035+
ASSERT_NOT_NULL(response);
5036+
ASSERT_NOT_NULL(strstr(response, "output exceeded"));
5037+
ASSERT_NOT_NULL(strstr(response, "\"isError\":true"));
5038+
5039+
char logs[640];
5040+
snprintf(logs, sizeof(logs), "%s/logs", cache.path);
5041+
ASSERT_EQ(mcp_count_directory_entries_with_prefix(logs, ".mcp-command-"), 0);
5042+
5043+
free(response);
5044+
cbm_mcp_server_free(srv);
5045+
cleanup_prefilter_dir(tmp, src_path, vendor_path);
5046+
ASSERT_TRUE(mcp_search_cache_close(&cache));
5047+
PASS();
5048+
#else
5049+
SKIP_PLATFORM("supervised Select-String output limit runs on Windows");
5050+
#endif
5051+
}
5052+
49215053
/* Windows raw scans must pin the PowerShell pipe to UTF-8: PS 5.1 otherwise
49225054
* emits stdout in the console OEM codepage and non-ASCII content degrades to
49235055
* '?' depending on the inherited console CP (seen as test_mcp raw-Русский
@@ -11249,6 +11381,8 @@ SUITE(mcp) {
1124911381
RUN_TEST(search_code_path_filter_matches_nothing);
1125011382
RUN_TEST(search_code_file_pattern_prefilter_boundaries);
1125111383
RUN_TEST(search_code_windows_prefilter_precedes_content_scan);
11384+
RUN_TEST(search_code_windows_cancel_cleans_supervised_scan);
11385+
RUN_TEST(search_code_windows_output_limit_fails_closed_and_cleans_scan);
1125211386
RUN_TEST(search_code_windows_scan_pins_utf8_output);
1125311387
RUN_TEST(search_code_invalid_regex_errors_issue283);
1125411388
RUN_TEST(search_code_literal_pipe_warns_issue282);

0 commit comments

Comments
 (0)