@@ -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 }
@@ -11145,13 +11155,14 @@ TEST(search_code_output_limit_fails_closed_and_cleans_scan) {
1114511155TEST(search_code_scan_deadline_fails_closed_and_resets) {
1114611156 mcp_search_cache_t cache;
1114711157 ASSERT_TRUE(mcp_search_cache_open(&cache, "cbm-search-deadline"));
11148- int scratch_before = mcp_count_directory_entries_with_prefix(cbm_tmpdir(), "cbm-search-");
11149- ASSERT_TRUE(scratch_before >= 0);
1115011158
1115111159 char tmp[512], src_path[768], vendor_path[768];
1115211160 cbm_mcp_server_t *srv = setup_prefilter_server(tmp, sizeof(tmp), src_path, sizeof(src_path),
1115311161 vendor_path, sizeof(vendor_path));
1115411162 ASSERT_NOT_NULL(srv);
11163+ /* Scratch goes to the private cache dir: the shared temp dir is visible to
11164+ * every other process on the machine, so a count there is not exact. */
11165+ cbm_mcp_server_set_search_scratch_dir_for_test(srv, cache.path);
1115511166 cbm_mcp_server_set_search_scan_timeout_for_test(srv, 0, true);
1115611167
1115711168 char *response =
@@ -11175,7 +11186,7 @@ TEST(search_code_scan_deadline_fails_closed_and_resets) {
1117511186 /* An immediate deadline can return before the log directory is created;
1117611187 * both a missing directory (-1) and an empty one (0) prove no artifact. */
1117711188 ASSERT_TRUE(mcp_count_directory_entries_with_prefix(logs, ".mcp-command-") <= 0);
11178- ASSERT_EQ(mcp_count_directory_entries_with_prefix(cbm_tmpdir() , "cbm-search-"), scratch_before );
11189+ ASSERT_EQ(mcp_count_directory_entries_with_prefix(cache.path , "cbm-search-"), 0 );
1117911190 free(response);
1118011191
1118111192 cbm_mcp_server_set_search_scan_timeout_for_test(srv, 0, false);
@@ -11276,13 +11287,14 @@ TEST(search_code_scan_setup_failures_respect_cause_precedence) {
1127611287TEST(search_code_scan_live_child_deadline_is_bounded_and_fails_closed) {
1127711288 mcp_search_cache_t cache;
1127811289 ASSERT_TRUE(mcp_search_cache_open(&cache, "cbm-search-live-deadline"));
11279- int scratch_before = mcp_count_directory_entries_with_prefix(cbm_tmpdir(), "cbm-search-");
11280- ASSERT_TRUE(scratch_before >= 0);
1128111290
1128211291 char tmp[512], src_path[768], vendor_path[768];
1128311292 cbm_mcp_server_t *srv = setup_prefilter_server(tmp, sizeof(tmp), src_path, sizeof(src_path),
1128411293 vendor_path, sizeof(vendor_path));
1128511294 ASSERT_NOT_NULL(srv);
11295+ /* Count only this server's scratch. The hook-rejection test below observes
11296+ * creation directly, without requiring this short deadline to reach spawn. */
11297+ cbm_mcp_server_set_search_scratch_dir_for_test(srv, cache.path);
1128611298#ifdef _WIN32
1128711299 const char *slow_command =
1128811300 "echo deadline-partial-output & powershell.exe -NoProfile -Command \"Start-Sleep -Seconds "
@@ -11305,7 +11317,7 @@ TEST(search_code_scan_live_child_deadline_is_bounded_and_fails_closed) {
1130511317 char logs[640];
1130611318 snprintf(logs, sizeof(logs), "%s/logs", cache.path);
1130711319 int command_artifacts = mcp_count_directory_entries_with_prefix(logs, ".mcp-command-");
11308- int scratch_after = mcp_count_directory_entries_with_prefix(cbm_tmpdir() , "cbm-search-");
11320+ int scratch_after = mcp_count_directory_entries_with_prefix(cache.path , "cbm-search-");
1130911321
1131011322 free(response);
1131111323 cbm_mcp_server_set_search_scan_command_for_test(srv, NULL);
@@ -11317,7 +11329,7 @@ TEST(search_code_scan_live_child_deadline_is_bounded_and_fails_closed) {
1131711329 ASSERT_TRUE(timed_out);
1131811330 ASSERT_TRUE(partial_hidden);
1131911331 ASSERT_EQ(command_artifacts, 0);
11320- ASSERT_EQ(scratch_after, scratch_before );
11332+ ASSERT_EQ(scratch_after, 0 );
1132111333 PASS();
1132211334}
1132311335
@@ -11380,14 +11392,13 @@ TEST(search_code_scan_deadline_precedes_output_limit) {
1138011392TEST(search_code_scan_hook_rejection_is_contained_and_cleans_up) {
1138111393 mcp_search_cache_t cache;
1138211394 ASSERT_TRUE(mcp_search_cache_open(&cache, "cbm-search-reject"));
11383- int scratch_before = mcp_count_directory_entries_with_prefix(cbm_tmpdir(), "cbm-search-");
11384- ASSERT_TRUE(scratch_before >= 0);
1138511395
1138611396 char tmp[512], src_path[768], vendor_path[768];
1138711397 cbm_mcp_server_t *srv = setup_prefilter_server(tmp, sizeof(tmp), src_path, sizeof(src_path),
1138811398 vendor_path, sizeof(vendor_path));
1138911399 ASSERT_NOT_NULL(srv);
11390- mcp_search_command_probe_t probe = {.reject = true};
11400+ cbm_mcp_server_set_search_scratch_dir_for_test(srv, cache.path);
11401+ mcp_search_command_probe_t probe = {.reject = true, .scratch_directory = cache.path};
1139111402 cbm_mcp_server_set_command_test_hook(srv, mcp_search_command_hook_probe, &probe);
1139211403
1139311404 char *response = cbm_mcp_handle_tool(srv, "search_code",
@@ -11402,14 +11413,55 @@ TEST(search_code_scan_hook_rejection_is_contained_and_cleans_up) {
1140211413 char logs[640];
1140311414 snprintf(logs, sizeof(logs), "%s/logs", cache.path);
1140411415 ASSERT_EQ(mcp_count_directory_entries_with_prefix(logs, ".mcp-command-"), 0);
11405- ASSERT_EQ(mcp_count_directory_entries_with_prefix(cbm_tmpdir(), "cbm-search-"), scratch_before);
11416+ ASSERT_EQ(probe.scratch_seen, 1);
11417+ ASSERT_EQ(mcp_count_directory_entries_with_prefix(cache.path, "cbm-search-"), 0);
1140611418 free(response);
1140711419 cbm_mcp_server_free(srv);
1140811420 cleanup_prefilter_dir(tmp, src_path, vendor_path);
1140911421 ASSERT_TRUE(mcp_search_cache_close(&cache));
1141011422 PASS();
1141111423}
1141211424
11425+ /* The private parent seam also pins scratch creation failure without changing
11426+ * process-wide temp settings. A valid path filter exercises its early cleanup
11427+ * under leak sanitizers; no command should be launched. */
11428+ TEST(search_code_scratch_creation_error_releases_path_filter) {
11429+ mcp_search_cache_t cache;
11430+ ASSERT_TRUE(mcp_search_cache_open(&cache, "cbm-search-scratch-error"));
11431+ char blocker_path[640];
11432+ snprintf(blocker_path, sizeof(blocker_path), "%s/not-a-directory", cache.path);
11433+ FILE *blocker = cbm_fopen(blocker_path, "wb");
11434+ ASSERT_NOT_NULL(blocker);
11435+ ASSERT_EQ(fclose(blocker), 0);
11436+
11437+ char tmp[512], src_path[768], vendor_path[768];
11438+ cbm_mcp_server_t *srv = setup_prefilter_server(tmp, sizeof(tmp), src_path, sizeof(src_path),
11439+ vendor_path, sizeof(vendor_path));
11440+ ASSERT_NOT_NULL(srv);
11441+ cbm_mcp_server_set_search_scratch_dir_for_test(srv, blocker_path);
11442+ mcp_search_command_probe_t probe = {0};
11443+ cbm_mcp_server_set_command_test_hook(srv, mcp_search_command_hook_probe, &probe);
11444+ char *response =
11445+ cbm_mcp_handle_tool(srv, "search_code",
11446+ "{\"pattern\":\"HandleRequest\",\"project\":\"prefilter-search\","
11447+ "\"path_filter\":\"^cbm_leak_probe/\"}");
11448+ bool setup_error = response && strstr(response, "\"isError\":true") != NULL &&
11449+ strstr(response, "cannot create temp file") != NULL;
11450+ int scratch_after = mcp_count_directory_entries_with_prefix(cache.path, "cbm-search-");
11451+ free(response);
11452+ cbm_mcp_server_set_search_scratch_dir_for_test(srv, NULL);
11453+ cbm_mcp_server_free(srv);
11454+ cleanup_prefilter_dir(tmp, src_path, vendor_path);
11455+ int removed = cbm_unlink(blocker_path);
11456+ bool cache_closed = mcp_search_cache_close(&cache);
11457+ ASSERT_TRUE(setup_error);
11458+ ASSERT_EQ(probe.calls, 0);
11459+ ASSERT_EQ(scratch_after, 0);
11460+ ASSERT_EQ(removed, 0);
11461+ ASSERT_TRUE(cache_closed);
11462+ PASS();
11463+ }
11464+
1141311465TEST(search_code_scoped_exit_one_is_not_no_match) {
1141411466 char tmp[512], src_path[768], vendor_path[768];
1141511467 cbm_mcp_server_t *srv = setup_prefilter_server(tmp, sizeof(tmp), src_path, sizeof(src_path),
@@ -21277,6 +21329,7 @@ SUITE(mcp) {
2127721329 RUN_TEST(search_code_scan_cancellation_precedes_zero_deadline);
2127821330 RUN_TEST(search_code_scan_deadline_precedes_output_limit);
2127921331 RUN_TEST(search_code_scan_hook_rejection_is_contained_and_cleans_up);
21332+ RUN_TEST(search_code_scratch_creation_error_releases_path_filter);
2128021333 RUN_TEST(search_code_scoped_exit_one_is_not_no_match);
2128121334 RUN_TEST(search_code_no_match_is_empty_for_direct_and_scoped_routes);
2128221335 RUN_TEST(search_code_scoped_scan_skips_non_regular_indexed_paths);
0 commit comments