Skip to content

Commit 30f1fbf

Browse files
authored
Merge pull request DeusData#2841 from halindrome/fix/search-code-file-pattern-path
fix(mcp): match search_code file_pattern with a directory against the repo-relative path
2 parents 47474b6 + 6f63e38 commit 30f1fbf

2 files changed

Lines changed: 149 additions & 4 deletions

File tree

‎src/mcp/mcp.c‎

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,9 @@ static const tool_def_t TOOLS[] = {
729729
{"search_code",
730730
"Graph-ranked text search: compact symbols, full bounded source, or file paths.",
731731
"{\"type\":\"object\",\"properties\":{\"pattern\":{\"type\":\"string\"},\"project\":{\"type\":"
732-
"\"string\"},\"file_pattern\":{\"type\":\"string\"},\"path_filter\":{\"type\":\"string\"},"
732+
"\"string\"},\"file_pattern\":{\"type\":\"string\",\"description\":\"Glob on file name; "
733+
"with '/', on repo-relative path (** = any dirs).\"},"
734+
"\"path_filter\":{\"type\":\"string\",\"description\":\"Regex on repo-relative path.\"},"
733735
"\"mode\":{\"type\":\"string\","
734736
"\"enum\":[\"compact\",\"full\",\"files\"],\"default\":\"compact\"},"
735737
"\"context\":{\"type\":\"integer\",\"default\":0,\"minimum\":0,\"maximum\":200},"
@@ -14714,6 +14716,7 @@ typedef struct {
1471414716
uint64_t enrich_ms;
1471514717
uint64_t elapsed_ms;
1471614718
bool include_phase_timings;
14719+
bool hint_path_filter; /* a search with a '/' file_pattern returned no matches */
1471714720
} search_metrics_t;
1471814721

1471914722
#ifdef CBM_ENABLE_TEST_SEAMS
@@ -14808,6 +14811,47 @@ static int search_result_cmp(const void *a, const void *b) {
1480814811
return 0;
1480914812
}
1481014813

14814+
#define SEARCH_CODE_PATH_HINT \
14815+
"no results; a file_pattern with '/' must match the whole repo-relative path (e.g. " \
14816+
"src/**/*.c); to scope by folder you can also use path_filter (regex on the " \
14817+
"repo-relative path, e.g. ^src/cli/)"
14818+
14819+
/* A file_pattern containing '/' is matched against the whole repo-relative path, like
14820+
* search_graph's file_pattern: '*' and '**' match any run of characters including '/', a
14821+
* '**' followed by '/' also matches zero directories, '?' matches one character, and a
14822+
* pattern without wildcards is a substring match. Portable (no fnmatch on Windows) and
14823+
* linear: every '*' is equivalent, so one backtrack point suffices. */
14824+
static bool search_code_path_glob_match(const char *pat, const char *path) {
14825+
if (!strpbrk(pat, "*?")) {
14826+
return strstr(path, pat) != NULL;
14827+
}
14828+
const char *star_pat = NULL;
14829+
const char *star_path = NULL;
14830+
for (;;) {
14831+
if (*pat == '*') {
14832+
const char *run = pat;
14833+
while (*pat == '*') {
14834+
pat++;
14835+
}
14836+
if (pat - run > SKIP_ONE && *pat == '/') {
14837+
pat++;
14838+
}
14839+
star_pat = pat;
14840+
star_path = path;
14841+
} else if (*path && (*pat == '?' || *pat == *path)) {
14842+
pat++;
14843+
path++;
14844+
} else if (!*pat && !*path) {
14845+
return true;
14846+
} else if (star_pat && *star_path) {
14847+
pat = star_pat;
14848+
path = ++star_path;
14849+
} else {
14850+
return false;
14851+
}
14852+
}
14853+
}
14854+
1481114855
/* Moving an arbitrary file_pattern ahead of Select-String is not generally results-preserving:
1481214856
* the current Windows path applies PowerShell -like to the full MatchInfo.Path, while POSIX
1481314857
* delegates glob semantics to grep --include. Restrict the Windows optimization to plain suffix
@@ -15430,6 +15474,9 @@ static char *assemble_search_output_toon(search_result_t *sr, int sr_count, grep
1543015474
"(not as alternation). Pass regex=true for 'foo|bar' to mean "
1543115475
"'foo OR bar'.");
1543215476
}
15477+
if (metrics->hint_path_filter) {
15478+
cbm_tree_scalar_str(&sb, "hint", SEARCH_CODE_PATH_HINT);
15479+
}
1543315480
if (metrics->elapsed_ms >= SEARCH_SLOW_MS) {
1543415481
cbm_tree_scalar_str(&sb, "warning_slow",
1543515482
"search was slow; narrow file_pattern/path_filter or use a more "
@@ -15669,6 +15716,9 @@ static char *assemble_search_output(
1566915716
if (yyjson_mut_arr_size(warnings) > 0) {
1567015717
yyjson_mut_obj_add_val(doc, root_obj, "warnings", warnings);
1567115718
}
15719+
if (metrics->hint_path_filter) {
15720+
yyjson_mut_obj_add_str(doc, root_obj, "hint", SEARCH_CODE_PATH_HINT);
15721+
}
1567215722

1567315723
char *json = yy_doc_to_str(doc);
1567415724
if (json) {
@@ -16446,14 +16496,25 @@ static bool write_scoped_filelist(cbm_mcp_server_t *srv, const char *project, co
1644616496
continue;
1644716497
}
1644816498
}
16499+
/* A file_pattern with '/' is matched here on every platform; the basename
16500+
* filters below (and the Windows prefilter, which rejects '/') skip it. */
16501+
bool path_glob = file_pattern && strchr(file_pattern, '/');
16502+
if (path_glob) {
16503+
#ifdef _WIN32
16504+
cbm_normalize_path_sep(indexed_files[fi]);
16505+
#endif
16506+
if (!search_code_path_glob_match(file_pattern, indexed_files[fi])) {
16507+
continue;
16508+
}
16509+
}
1644916510
#ifndef _WIN32
1645016511
/* GNU grep's --include is unavailable in BusyBox grep. Filter the
1645116512
* canonical list before xargs instead, preserving grep's basename
1645216513
* glob semantics without making the shipped static binary depend
1645316514
* on GNU userland. Windows keeps its PowerShell -like filter. */
1645416515
const char *basename = strrchr(indexed_files[fi], '/');
1645516516
basename = basename ? basename + SKIP_ONE : indexed_files[fi];
16456-
if (file_pattern && fnmatch(file_pattern, basename, 0) != 0) {
16517+
if (file_pattern && !path_glob && fnmatch(file_pattern, basename, 0) != 0) {
1645716518
continue;
1645816519
}
1645916520
#endif
@@ -17471,8 +17532,8 @@ static char *handle_search_code(cbm_mcp_server_t *srv, const char *args) {
1747117532
bool scan_in_process = scoped && !srv->search_scan_command_override;
1747217533
#ifdef _WIN32
1747317534
scan_in_process =
17474-
scan_in_process &&
17475-
(!file_pattern || cbm_search_code_file_pattern_can_prefilter(file_pattern));
17535+
scan_in_process && (!file_pattern || strchr(file_pattern, '/') ||
17536+
cbm_search_code_file_pattern_can_prefilter(file_pattern));
1747617537
#endif
1747717538
mcp_scan_cause_t scan_cause;
1747817539
if (scan_in_process) {
@@ -17644,6 +17705,7 @@ static char *handle_search_code(cbm_mcp_server_t *srv, const char *args) {
1764417705
directory_output = directory_limit;
1764517706
}
1764617707
bool warn_literal_pipe = pat_has_pipe && !use_regex;
17708+
metrics.hint_path_filter = file_pattern && strchr(file_pattern, '/') && gm_count == 0;
1764717709
/* Every render below re-checks each result's file against the root; one
1764817710
* containment for the request keeps that to one resolution per file. */
1764917711
root_containment_t request_containment;

‎tests/test_mcp.c‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11670,6 +11670,88 @@ TEST(search_code_scoped_file_pattern_is_busybox_portable) {
1167011670
#endif
1167111671
}
1167211672

11673+
/* issue #2836: a file_pattern with '/' matched only the basename, so every
11674+
* directory-scoped glob silently returned 0 results. */
11675+
TEST(search_code_file_pattern_matches_repo_relative_path_issue2836) {
11676+
char tmp[CBM_SZ_512];
11677+
snprintf(tmp, sizeof(tmp), "%s/cbm-search-fp-path-XXXXXX", cbm_tmpdir());
11678+
ASSERT_NOT_NULL(cbm_mkdtemp(tmp));
11679+
char src_dir[CBM_SZ_1K];
11680+
char cli_dir[CBM_SZ_1K];
11681+
char main_path[CBM_SZ_1K];
11682+
char watcher_path[CBM_SZ_1K];
11683+
snprintf(src_dir, sizeof(src_dir), "%s/src", tmp);
11684+
snprintf(cli_dir, sizeof(cli_dir), "%s/src/cli", tmp);
11685+
snprintf(main_path, sizeof(main_path), "%s/src/main.c", tmp);
11686+
snprintf(watcher_path, sizeof(watcher_path), "%s/src/cli/watcher.c", tmp);
11687+
ASSERT_EQ(cbm_mkdir(src_dir), 0);
11688+
ASSERT_EQ(cbm_mkdir(cli_dir), 0);
11689+
ASSERT_EQ(th_write_file(main_path, "int FP_PATH_NEEDLE = 1;\n"), 0);
11690+
ASSERT_EQ(th_write_file(watcher_path, "int FP_PATH_NEEDLE = 2;\n"), 0);
11691+
11692+
cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL);
11693+
ASSERT_NOT_NULL(srv);
11694+
cbm_store_t *store = cbm_mcp_server_store(srv);
11695+
ASSERT_NOT_NULL(store);
11696+
const char *project = "search-fp-path";
11697+
cbm_mcp_server_set_project(srv, project);
11698+
ASSERT_EQ(cbm_store_upsert_project(store, project, tmp), CBM_STORE_OK);
11699+
cbm_node_t main_file = {.project = project,
11700+
.label = "File",
11701+
.name = "main.c",
11702+
.qualified_name = "search-fp-path.src.main.c",
11703+
.file_path = "src/main.c"};
11704+
cbm_node_t watcher_file = {.project = project,
11705+
.label = "File",
11706+
.name = "watcher.c",
11707+
.qualified_name = "search-fp-path.src.cli.watcher.c",
11708+
.file_path = "src/cli/watcher.c"};
11709+
ASSERT_GT(cbm_store_upsert_node(store, &main_file), 0);
11710+
ASSERT_GT(cbm_store_upsert_node(store, &watcher_file), 0);
11711+
11712+
static const struct {
11713+
const char *file_pattern;
11714+
const char *expect;
11715+
bool hint;
11716+
} cases[] = {
11717+
{"src/cli/*.c", "\"total_grep_matches\":1", false},
11718+
{"src/**/*.c", "\"total_grep_matches\":2", false}, /* ** spans zero dirs */
11719+
{"**/cli/*.c", "\"total_grep_matches\":1", false},
11720+
{"src/cli/", "\"total_grep_matches\":1", false}, /* no wildcard: substring */
11721+
{"watcher.c", "\"total_grep_matches\":1", false}, /* basename unchanged */
11722+
{"*.c", "\"total_grep_matches\":2", false},
11723+
{"lib/*.c", "\"total_grep_matches\":0", true},
11724+
};
11725+
bool ok = true;
11726+
for (size_t i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) {
11727+
char args[CBM_SZ_512];
11728+
snprintf(args, sizeof(args),
11729+
"{\"pattern\":\"FP_PATH_NEEDLE\",\"project\":\"search-fp-path\","
11730+
"\"file_pattern\":\"%s\",\"format\":\"json\"}",
11731+
cases[i].file_pattern);
11732+
char *response = cbm_mcp_handle_tool(srv, "search_code", args);
11733+
char *inner = extract_text_content(response);
11734+
bool case_ok = inner && strstr(inner, cases[i].expect) &&
11735+
(strstr(inner, "use path_filter") != NULL) == cases[i].hint;
11736+
if (!case_ok) {
11737+
fprintf(stderr, "file_pattern %s: %s\n", cases[i].file_pattern,
11738+
inner ? inner : "(null)");
11739+
ok = false;
11740+
}
11741+
free(inner);
11742+
free(response);
11743+
}
11744+
11745+
cbm_mcp_server_free(srv);
11746+
cbm_unlink(watcher_path);
11747+
cbm_unlink(main_path);
11748+
cbm_rmdir(cli_dir);
11749+
cbm_rmdir(src_dir);
11750+
cbm_rmdir(tmp);
11751+
ASSERT_TRUE(ok);
11752+
PASS();
11753+
}
11754+
1167311755
TEST(search_code_recursive_fallback_propagates_discovery_failures) {
1167411756
#ifdef _WIN32
1167511757
SKIP_PLATFORM("POSIX find/sort/xargs fallback contract");
@@ -26322,6 +26404,7 @@ SUITE(mcp) {
2632226404
RUN_TEST(search_code_fails_closed_when_complete_scan_is_impossible);
2632326405
RUN_TEST(search_code_scoped_scan_uses_canonical_file_nodes);
2632426406
RUN_TEST(search_code_scoped_file_pattern_is_busybox_portable);
26407+
RUN_TEST(search_code_file_pattern_matches_repo_relative_path_issue2836);
2632526408
RUN_TEST(search_code_recursive_fallback_propagates_discovery_failures);
2632626409
RUN_TEST(search_code_default_budget_limits_raw_rows_before_graph_results);
2632726410
RUN_TEST(search_code_raw_and_directory_remainders_are_independently_pageable);

0 commit comments

Comments
 (0)