fix(mcp): normalize root-ish path filters in codegraph_files (#426) (#466)

The agent (opencode/Gemini Flash on Windows) called codegraph_files with
path="/" and got "No files found matching the criteria.", which pushed it
straight back to Read/Glob. Indexed file paths are stored as
project-relative POSIX (e.g. "src/foo.py"), and the old startsWith filter
matched nothing for any of the root-ish or platform-flavored shapes an
agent might guess: "/", ".", "./", "", "\\", leading-slash and
leading-./ subpaths, or Windows backslash subpaths.

Normalize the filter (strip leading "/", "./", "\", bare "."; convert
"\" to "/"; trim trailing "/"), then match by exact equal or "<filter>/"
boundary — which also kills a sibling-prefix bleed where filter "src"
used to match "src-utils/...".

Validated on macOS + Linux (Docker) + Windows (Parallels) with 13 new
unit tests plus the existing mcp-input-limits/concurrent-locking
suites, and end-to-end through opencode in tmux (Big Pickle/OpenCode
Zen): codegraph_files [path=/] now returns the project tree and the
agent answers directly instead of falling back to Read.

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Colby Mchenry
2026-05-26 15:39:48 -05:00
committed by GitHub
co-authored by Claude Opus 4.7
parent c0cf9c1e7d
commit e1eb13cf9b
3 changed files with 128 additions and 3 deletions
+14 -3
View File
@@ -2248,9 +2248,20 @@ export class ToolHandler {
return this.textResult('No files indexed. Run `codegraph index` first.');
}
// Filter by path prefix
let files = pathFilter
? allFiles.filter(f => f.path.startsWith(pathFilter) || f.path.startsWith('./' + pathFilter))
// Filter by path prefix. Stored paths are project-relative POSIX (e.g.
// "src/foo.ts"), but agents commonly pass project-root variants like "/",
// ".", "./", "" or Windows-style "src\foo" — and prefixes with leading
// "/", "./" or "\". Normalize all of those before matching so the agent
// gets results instead of falling back to Read/Glob (see #426).
const normalizedFilter = pathFilter
? pathFilter
.replace(/\\/g, '/')
.replace(/^(?:\.?\/+)+/, '')
.replace(/^\.$/, '')
.replace(/\/+$/, '')
: '';
let files = normalizedFilter
? allFiles.filter(f => f.path === normalizedFilter || f.path.startsWith(normalizedFilter + '/'))
: allFiles;
// Filter by glob pattern