@@ -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;
0 commit comments