Skip to content

Commit 266e281

Browse files
authored
Merge pull request DeusData#2518 from DeusData/fix/search-code-scratch-isolation-v2
test(mcp): isolate search scratch cleanup fixtures
2 parents 5e6fa65 + c38bfff commit 266e281

3 files changed

Lines changed: 130 additions & 15 deletions

File tree

‎src/mcp/mcp.c‎

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1782,6 +1782,7 @@ struct cbm_mcp_server {
17821782
#endif
17831783
size_t search_output_limit_override;
17841784
const char *search_scan_command_override;
1785+
const char *search_scratch_dir_override; /* NULL = cbm_tmpdir() */
17851786
uint64_t search_scan_timeout_override_ms;
17861787
bool search_scan_timeout_override_set;
17871788
cbm_thread_t autoindex_tid;
@@ -2120,6 +2121,12 @@ void cbm_mcp_server_set_search_scan_timeout_for_test(cbm_mcp_server_t *srv, uint
21202121
srv->search_scan_timeout_override_set = override_set;
21212122
}
21222123
}
2124+
2125+
void cbm_mcp_server_set_search_scratch_dir_for_test(cbm_mcp_server_t *srv, const char *dir) {
2126+
if (srv) {
2127+
srv->search_scratch_dir_override = dir;
2128+
}
2129+
}
21232130
#ifdef CBM_ENABLE_TEST_SEAMS
21242131
void cbm_mcp_server_set_snapshot_read_test_hook(cbm_mcp_server_t *srv,
21252132
cbm_mcp_snapshot_read_test_hook_fn hook,
@@ -14944,14 +14951,14 @@ static void search_scratch_close(search_scratch_t *scratch) {
1494414951
/* Open the scratch directory, write `pattern` to the grep -f file, and leave the
1494514952
* file list open for write_scoped_filelist. Returns true on success; on failure
1494614953
* everything already created is removed before returning. */
14947-
static bool search_scratch_open(search_scratch_t *scratch, const char *pattern) {
14954+
static bool search_scratch_open(search_scratch_t *scratch, const char *parent,
14955+
const char *pattern) {
1494814956
scratch->dir[0] = '\0';
1494914957
scratch->pattern_path[0] = '\0';
1495014958
scratch->filelist_path[0] = '\0';
1495114959
scratch->filelist = NULL;
1495214960

14953-
int written =
14954-
snprintf(scratch->dir, sizeof(scratch->dir), "%s/cbm-search-XXXXXX", cbm_tmpdir());
14961+
int written = snprintf(scratch->dir, sizeof(scratch->dir), "%s/cbm-search-XXXXXX", parent);
1495514962
if (written <= 0 || (size_t)written >= sizeof(scratch->dir) || !cbm_mkdtemp(scratch->dir)) {
1495614963
scratch->dir[0] = '\0';
1495714964
return false;
@@ -15142,13 +15149,19 @@ static char *handle_search_code(cbm_mcp_server_t *srv, const char *args) {
1514215149
path_filter = NULL;
1514315150

1514415151
if (!pattern) {
15152+
if (has_path_filter) {
15153+
cbm_regfree(&path_regex);
15154+
}
1514515155
free(project);
1514615156
free(file_pattern);
1514715157
return cbm_mcp_text_result("pattern is required", true);
1514815158
}
1514915159

1515015160
/* Project is required */
1515115161
if (!project) {
15162+
if (has_path_filter) {
15163+
cbm_regfree(&path_regex);
15164+
}
1515215165
free(pattern);
1515315166
free(file_pattern);
1515415167
char *_err = build_project_list_error("project is required");
@@ -15159,6 +15172,9 @@ static char *handle_search_code(cbm_mcp_server_t *srv, const char *args) {
1515915172

1516015173
char *root_path = get_project_root(srv, project);
1516115174
if (!root_path) {
15175+
if (has_path_filter) {
15176+
cbm_regfree(&path_regex);
15177+
}
1516215178
free(pattern);
1516315179
free(project);
1516415180
free(file_pattern);
@@ -15246,12 +15262,17 @@ static char *handle_search_code(cbm_mcp_server_t *srv, const char *args) {
1524615262
: scan_started_ms + scan_budget_ms;
1524715263
bool scan_deadline_latched = false;
1524815264
search_scratch_t scratch;
15249-
if (!search_scratch_open(&scratch, pattern)) {
15265+
const char *scratch_parent =
15266+
srv->search_scratch_dir_override ? srv->search_scratch_dir_override : cbm_tmpdir();
15267+
if (!search_scratch_open(&scratch, scratch_parent, pattern)) {
1525015268
bool scan_cancelled = mcp_request_cancelled(srv);
1525115269
bool scan_timed_out = cbm_now_ms() >= scan_deadline_ms;
1525215270
char errmsg[CBM_SZ_256];
1525315271
snprintf(errmsg, sizeof(errmsg), "search failed: cannot create temp file (%s)",
1525415272
strerror(errno));
15273+
if (has_path_filter) {
15274+
cbm_regfree(&path_regex);
15275+
}
1525515276
free(root_path);
1525615277
free(pattern);
1525715278
free(project);

‎src/mcp/mcp_internal.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ void cbm_mcp_server_set_auto_index_count_test_hook(cbm_mcp_server_t *srv,
2929
void cbm_mcp_server_set_search_scan_command_for_test(cbm_mcp_server_t *srv, const char *command);
3030
void cbm_mcp_server_set_search_scan_timeout_for_test(cbm_mcp_server_t *srv, uint64_t timeout_ms,
3131
bool override_set);
32+
/* Parent directory for search_code's private cbm-search-XXXXXX scratch
33+
* (NULL restores cbm_tmpdir()). Tests point it at a private directory so a
34+
* scratch count cannot see other processes' entries in the shared temp dir.
35+
* The server borrows dir until it is reset or the server is freed. */
36+
void cbm_mcp_server_set_search_scratch_dir_for_test(cbm_mcp_server_t *srv, const char *dir);
3237

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

‎tests/test_mcp.c‎

Lines changed: 100 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,14 @@ typedef struct {
174174
bool output_filled;
175175
int calls;
176176
char command[CBM_SZ_4K];
177+
/* When set, the hook counts cbm-search-* entries here at spawn time, which
178+
* proves the scan's scratch really lives in this private directory. */
179+
const char *scratch_directory;
180+
int scratch_seen;
177181
} mcp_search_command_probe_t;
178182

183+
static int mcp_count_directory_entries_with_prefix(const char *directory, const char *prefix);
184+
179185
typedef struct {
180186
char path[512];
181187
char *saved_cache;
@@ -213,6 +219,10 @@ static bool mcp_search_command_hook_probe(void *context, const char *command) {
213219
}
214220
probe->calls++;
215221
snprintf(probe->command, sizeof(probe->command), "%s", command);
222+
if (probe->scratch_directory) {
223+
probe->scratch_seen =
224+
mcp_count_directory_entries_with_prefix(probe->scratch_directory, "cbm-search-");
225+
}
216226
if (probe->delay_ms > 0) {
217227
cbm_usleep(probe->delay_ms * 1000U);
218228
}
@@ -9228,6 +9238,41 @@ TEST(tool_search_code_no_project) {
92289238
PASS();
92299239
}
92309240

9241+
/* search_code compiles path_filter into a regex before it checks pattern,
9242+
* project and the project root. Every one of those early error returns must
9243+
* release the compiled regex. The leak itself is only visible to a leak
9244+
* detector such as LeakSanitizer, so this test drives each return with a valid
9245+
* path_filter and asserts the error taken. */
9246+
TEST(search_code_early_errors_release_path_filter_regex) {
9247+
cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL);
9248+
ASSERT_NOT_NULL(srv);
9249+
9250+
/* No pattern. */
9251+
char *resp = cbm_mcp_handle_tool(
9252+
srv, "search_code", "{\"project\":\"nonexistent\",\"path_filter\":\"^cbm_leak_probe/\"}");
9253+
bool pattern_error = resp && strstr(resp, "pattern is required") != NULL;
9254+
free(resp);
9255+
9256+
/* No project. */
9257+
resp = cbm_mcp_handle_tool(srv, "search_code",
9258+
"{\"pattern\":\"main\",\"path_filter\":\"^cbm_leak_probe/\"}");
9259+
bool project_error = resp && strstr(resp, "project is required") != NULL;
9260+
free(resp);
9261+
9262+
/* Unknown project, with a file_pattern alongside the path filter. */
9263+
resp = cbm_mcp_handle_tool(srv, "search_code",
9264+
"{\"pattern\":\"main\",\"project\":\"nonexistent\","
9265+
"\"file_pattern\":\"*.c\",\"path_filter\":\"^cbm_leak_probe/\"}");
9266+
bool root_error = resp && strstr(resp, "project not found or not indexed") != NULL;
9267+
free(resp);
9268+
9269+
cbm_mcp_server_free(srv);
9270+
ASSERT_TRUE(pattern_error);
9271+
ASSERT_TRUE(project_error);
9272+
ASSERT_TRUE(root_error);
9273+
PASS();
9274+
}
9275+
92319276
TEST(search_code_multi_word) {
92329277
char tmp[512];
92339278
cbm_mcp_server_t *srv = setup_snippet_server(tmp, sizeof(tmp));
@@ -11293,13 +11338,14 @@ TEST(search_code_output_limit_fails_closed_and_cleans_scan) {
1129311338
TEST(search_code_scan_deadline_fails_closed_and_resets) {
1129411339
mcp_search_cache_t cache;
1129511340
ASSERT_TRUE(mcp_search_cache_open(&cache, "cbm-search-deadline"));
11296-
int scratch_before = mcp_count_directory_entries_with_prefix(cbm_tmpdir(), "cbm-search-");
11297-
ASSERT_TRUE(scratch_before >= 0);
1129811341

1129911342
char tmp[512], src_path[768], vendor_path[768];
1130011343
cbm_mcp_server_t *srv = setup_prefilter_server(tmp, sizeof(tmp), src_path, sizeof(src_path),
1130111344
vendor_path, sizeof(vendor_path));
1130211345
ASSERT_NOT_NULL(srv);
11346+
/* Scratch goes to the private cache dir: the shared temp dir is visible to
11347+
* every other process on the machine, so a count there is not exact. */
11348+
cbm_mcp_server_set_search_scratch_dir_for_test(srv, cache.path);
1130311349
cbm_mcp_server_set_search_scan_timeout_for_test(srv, 0, true);
1130411350

1130511351
char *response =
@@ -11323,7 +11369,7 @@ TEST(search_code_scan_deadline_fails_closed_and_resets) {
1132311369
/* An immediate deadline can return before the log directory is created;
1132411370
* both a missing directory (-1) and an empty one (0) prove no artifact. */
1132511371
ASSERT_TRUE(mcp_count_directory_entries_with_prefix(logs, ".mcp-command-") <= 0);
11326-
ASSERT_EQ(mcp_count_directory_entries_with_prefix(cbm_tmpdir(), "cbm-search-"), scratch_before);
11372+
ASSERT_EQ(mcp_count_directory_entries_with_prefix(cache.path, "cbm-search-"), 0);
1132711373
free(response);
1132811374

1132911375
cbm_mcp_server_set_search_scan_timeout_for_test(srv, 0, false);
@@ -11424,13 +11470,14 @@ TEST(search_code_scan_setup_failures_respect_cause_precedence) {
1142411470
TEST(search_code_scan_live_child_deadline_is_bounded_and_fails_closed) {
1142511471
mcp_search_cache_t cache;
1142611472
ASSERT_TRUE(mcp_search_cache_open(&cache, "cbm-search-live-deadline"));
11427-
int scratch_before = mcp_count_directory_entries_with_prefix(cbm_tmpdir(), "cbm-search-");
11428-
ASSERT_TRUE(scratch_before >= 0);
1142911473

1143011474
char tmp[512], src_path[768], vendor_path[768];
1143111475
cbm_mcp_server_t *srv = setup_prefilter_server(tmp, sizeof(tmp), src_path, sizeof(src_path),
1143211476
vendor_path, sizeof(vendor_path));
1143311477
ASSERT_NOT_NULL(srv);
11478+
/* Count only this server's scratch. The hook-rejection test below observes
11479+
* creation directly, without requiring this short deadline to reach spawn. */
11480+
cbm_mcp_server_set_search_scratch_dir_for_test(srv, cache.path);
1143411481
#ifdef _WIN32
1143511482
const char *slow_command =
1143611483
"echo deadline-partial-output & powershell.exe -NoProfile -Command \"Start-Sleep -Seconds "
@@ -11453,7 +11500,7 @@ TEST(search_code_scan_live_child_deadline_is_bounded_and_fails_closed) {
1145311500
char logs[640];
1145411501
snprintf(logs, sizeof(logs), "%s/logs", cache.path);
1145511502
int command_artifacts = mcp_count_directory_entries_with_prefix(logs, ".mcp-command-");
11456-
int scratch_after = mcp_count_directory_entries_with_prefix(cbm_tmpdir(), "cbm-search-");
11503+
int scratch_after = mcp_count_directory_entries_with_prefix(cache.path, "cbm-search-");
1145711504

1145811505
free(response);
1145911506
cbm_mcp_server_set_search_scan_command_for_test(srv, NULL);
@@ -11465,7 +11512,7 @@ TEST(search_code_scan_live_child_deadline_is_bounded_and_fails_closed) {
1146511512
ASSERT_TRUE(timed_out);
1146611513
ASSERT_TRUE(partial_hidden);
1146711514
ASSERT_EQ(command_artifacts, 0);
11468-
ASSERT_EQ(scratch_after, scratch_before);
11515+
ASSERT_EQ(scratch_after, 0);
1146911516
PASS();
1147011517
}
1147111518

@@ -11528,14 +11575,13 @@ TEST(search_code_scan_deadline_precedes_output_limit) {
1152811575
TEST(search_code_scan_hook_rejection_is_contained_and_cleans_up) {
1152911576
mcp_search_cache_t cache;
1153011577
ASSERT_TRUE(mcp_search_cache_open(&cache, "cbm-search-reject"));
11531-
int scratch_before = mcp_count_directory_entries_with_prefix(cbm_tmpdir(), "cbm-search-");
11532-
ASSERT_TRUE(scratch_before >= 0);
1153311578

1153411579
char tmp[512], src_path[768], vendor_path[768];
1153511580
cbm_mcp_server_t *srv = setup_prefilter_server(tmp, sizeof(tmp), src_path, sizeof(src_path),
1153611581
vendor_path, sizeof(vendor_path));
1153711582
ASSERT_NOT_NULL(srv);
11538-
mcp_search_command_probe_t probe = {.reject = true};
11583+
cbm_mcp_server_set_search_scratch_dir_for_test(srv, cache.path);
11584+
mcp_search_command_probe_t probe = {.reject = true, .scratch_directory = cache.path};
1153911585
cbm_mcp_server_set_command_test_hook(srv, mcp_search_command_hook_probe, &probe);
1154011586

1154111587
char *response = cbm_mcp_handle_tool(srv, "search_code",
@@ -11550,14 +11596,55 @@ TEST(search_code_scan_hook_rejection_is_contained_and_cleans_up) {
1155011596
char logs[640];
1155111597
snprintf(logs, sizeof(logs), "%s/logs", cache.path);
1155211598
ASSERT_EQ(mcp_count_directory_entries_with_prefix(logs, ".mcp-command-"), 0);
11553-
ASSERT_EQ(mcp_count_directory_entries_with_prefix(cbm_tmpdir(), "cbm-search-"), scratch_before);
11599+
ASSERT_EQ(probe.scratch_seen, 1);
11600+
ASSERT_EQ(mcp_count_directory_entries_with_prefix(cache.path, "cbm-search-"), 0);
1155411601
free(response);
1155511602
cbm_mcp_server_free(srv);
1155611603
cleanup_prefilter_dir(tmp, src_path, vendor_path);
1155711604
ASSERT_TRUE(mcp_search_cache_close(&cache));
1155811605
PASS();
1155911606
}
1156011607

11608+
/* The private parent seam also pins scratch creation failure without changing
11609+
* process-wide temp settings. A valid path filter exercises its early cleanup
11610+
* under leak sanitizers; no command should be launched. */
11611+
TEST(search_code_scratch_creation_error_releases_path_filter) {
11612+
mcp_search_cache_t cache;
11613+
ASSERT_TRUE(mcp_search_cache_open(&cache, "cbm-search-scratch-error"));
11614+
char blocker_path[640];
11615+
snprintf(blocker_path, sizeof(blocker_path), "%s/not-a-directory", cache.path);
11616+
FILE *blocker = cbm_fopen(blocker_path, "wb");
11617+
ASSERT_NOT_NULL(blocker);
11618+
ASSERT_EQ(fclose(blocker), 0);
11619+
11620+
char tmp[512], src_path[768], vendor_path[768];
11621+
cbm_mcp_server_t *srv = setup_prefilter_server(tmp, sizeof(tmp), src_path, sizeof(src_path),
11622+
vendor_path, sizeof(vendor_path));
11623+
ASSERT_NOT_NULL(srv);
11624+
cbm_mcp_server_set_search_scratch_dir_for_test(srv, blocker_path);
11625+
mcp_search_command_probe_t probe = {0};
11626+
cbm_mcp_server_set_command_test_hook(srv, mcp_search_command_hook_probe, &probe);
11627+
char *response =
11628+
cbm_mcp_handle_tool(srv, "search_code",
11629+
"{\"pattern\":\"HandleRequest\",\"project\":\"prefilter-search\","
11630+
"\"path_filter\":\"^cbm_leak_probe/\"}");
11631+
bool setup_error = response && strstr(response, "\"isError\":true") != NULL &&
11632+
strstr(response, "cannot create temp file") != NULL;
11633+
int scratch_after = mcp_count_directory_entries_with_prefix(cache.path, "cbm-search-");
11634+
free(response);
11635+
cbm_mcp_server_set_search_scratch_dir_for_test(srv, NULL);
11636+
cbm_mcp_server_free(srv);
11637+
cleanup_prefilter_dir(tmp, src_path, vendor_path);
11638+
int removed = cbm_unlink(blocker_path);
11639+
bool cache_closed = mcp_search_cache_close(&cache);
11640+
ASSERT_TRUE(setup_error);
11641+
ASSERT_EQ(probe.calls, 0);
11642+
ASSERT_EQ(scratch_after, 0);
11643+
ASSERT_EQ(removed, 0);
11644+
ASSERT_TRUE(cache_closed);
11645+
PASS();
11646+
}
11647+
1156111648
TEST(search_code_scoped_exit_one_is_not_no_match) {
1156211649
char tmp[512], src_path[768], vendor_path[768];
1156311650
cbm_mcp_server_t *srv = setup_prefilter_server(tmp, sizeof(tmp), src_path, sizeof(src_path),
@@ -21575,6 +21662,7 @@ SUITE(mcp) {
2157521662
RUN_TEST(tool_search_code_limit_declares_a_minimum_issue1511);
2157621663
RUN_TEST(tool_search_code_declares_independent_result_and_raw_content_paging);
2157721664
RUN_TEST(tool_search_code_no_project);
21665+
RUN_TEST(search_code_early_errors_release_path_filter_regex);
2157821666
RUN_TEST(search_code_multi_word);
2157921667
RUN_TEST(search_code_full_preserves_utf8_source);
2158021668
RUN_TEST(search_code_raw_match_preserves_utf8_content);
@@ -21613,6 +21701,7 @@ SUITE(mcp) {
2161321701
RUN_TEST(search_code_scan_cancellation_precedes_zero_deadline);
2161421702
RUN_TEST(search_code_scan_deadline_precedes_output_limit);
2161521703
RUN_TEST(search_code_scan_hook_rejection_is_contained_and_cleans_up);
21704+
RUN_TEST(search_code_scratch_creation_error_releases_path_filter);
2161621705
RUN_TEST(search_code_scoped_exit_one_is_not_no_match);
2161721706
RUN_TEST(search_code_no_match_is_empty_for_direct_and_scoped_routes);
2161821707
RUN_TEST(search_code_scoped_scan_skips_non_regular_indexed_paths);

0 commit comments

Comments
 (0)