@@ -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+
179185typedef 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+
92319276TEST(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) {
1129311338TEST(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) {
1142411470TEST(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) {
1152811575TEST(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+
1156111648TEST(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