ffgrep: path constraint targeting an unindexed file silently widens to a repo-wide fuzzy search
Summary
ffgrep with a path targeting a file outside the index (e.g. anything under node_modules/, which is gitignore/exclusion-respected) returns zero exact matches, then silently re-runs the search repo-wide with the path constraint dropped, presenting unrelated files as if they were related to the pinned path.
Environment
- OS: Windows 11
- pi: 0.84.3
- @ff-labs/pi-fff: 0.10.5 (tools-and-ui mode)
- CWD: Node project with
node_modules/(react installed);node_modules/react/package.jsonexists on disk and actually containsjsx-runtimein itsexports.
Reproduction
All commands run from the repo root:
-
ffgrep { pattern: "jsx-runtime", path: "node_modules/react/package.json" }->[0 exact matches. Maybe you meant this?]then matches frompackage-lock.jsonandtests/— never from the pinned file (or anything undernode_modules). -
ffgrep { pattern: "version", path: "node_modules/react/package.json" }-> first result ise2e/file-link-repro.spec.ts:50: version: 3,— completely unrelated toreact. -
ffgrep { pattern: "sideEffects", path: "node_modules/react/package.json" }-> returnssrc/renderer/**files whose top hit is auseEffectline — fuzzy noise with zero relationship to either the query or the pinned path. (Because results are frecency-ranked, theApp.tsxfamily shows up first for generic words.)
Control cases for contrast:
path: "node_modules/react/"(directory constraint, no trailing extension) +jsx-runtime-> cleanNo matches found(no leak — directory constraints keep the constrained query).path: "this-dir-does-not-exist-xyz/"-> cleanNo matches found.path: "package-lock.json"(an indexed file) -> works correctly.
Root cause
In src/index.ts (the grep tool's execute):
- Workspace-relative paths never reach an aux finder:
routePathConstraint(aux-finders.ts) returnsnullfor paths without a../prefix, so the query runs against the workspace finder, whose index excludesnode_modules. Exact grep => 0 items. - On 0 items,
pathTargetsFileis computed from the trailing extension, and when true the fuzzy fallback usesfuzzyQuery = patternwithout the path constraint (the "file may just be misnamed" heuristic):
const lastSeg = params.path?.split(/[\/]/).pop() ?? "";
const pathTargetsFile = /\.[a-zA-Z][a-zA-Z0-9]{0,9}$/.test(lastSeg);
const fuzzyQuery = pathTargetsFile ? pattern : query;
const fuzzy = picker.grep(fuzzyQuery, { mode: "fuzzy", ... });
The heuristic never checks whether the pinned file is actually reachable in the picker/index. For any unindexed file path — and node_modules/... is always unindexed — every grep is guaranteed to hit this fallback and return repo-wide noise.
Why it's dangerous
- An agent pinning
path: "node_modules/<pkg>/package.json"to check a dependency's contents receives matches from arbitrary source files, plausibly presented as if they concerned that file. - Short, common patterns (
version,sideEffects, ...) amplify the problem: the fuzzy results are barely related to the pattern at all.
Expected behavior
- Don't broaden when the target isn't in the index. If
pathTargetsFileand the pinned path is not part of the picker's index (gitignored/excluded), return a clear message such aspath not indexed (gitignored or excluded): no matches— or, better, read/match that single file on disk so the query actually answers the question. - At minimum, state that the constraint was dropped. When the fallback does broaden repo-wide, the output must say:
path constraint ignored (target not indexed); results are repo-wide. - The fuzzy fallback for common/generic patterns degrades into pure noise (
sideEffects->useEffectfiles). Consider suppressing or clearly labeling fuzzy results.
Notes
- Directory constraints (no trailing extension) keep the constrained query and are safe — verified
No matches found, no leak. - Aux finders currently can't rescue this case because
routePathConstraintrefuses workspace-relative paths even when they point outside the indexed set (e.g.node_modules/react/...).
[triage-bot] REPRODUCED: Reproduced on origin/main. Branch: triage-bot/issue-830.
Report is accurate, including the root cause. packages/pi-fff/src/index.ts:930-932 decides to drop the path constraint purely from the trailing extension of params.path, never checking that the pinned file is reachable at all. Any path under an excluded dir (node_modules/**) is guaranteed to fall into it, so the tool answers a repo-wide question while the call site asked about one file.
Deterministic repro against the extension's grep tool (native picker stubbed so node_modules is excluded from the index, exactly as in reality):
# packages/pi-fff, cwd = temp project containing node_modules/react/package.json
grep({ pattern: "jsx-runtime", path: "node_modules/react/package.json" })
expected: No matches found (or: path not indexed)
actual: [0 exact matches. Maybe you meant this?]
src/renderer/App.tsx
12: useEffect(() => {
Control case confirms the asymmetry — path: "node_modules/react/" keeps the constraint and returns No matches found.
Working on fix on triage-bot/issue-830.
Honk-Honk 🪿
AI-generated report. Everything below was produced by an AI coding agent (pi coding agent 0.85.1, model
deepseek-flashvia thedeepseekprovider) running locally on the machine of the human operator who posts this. The operator reviewed the draft and authorized this post. All commands were run locally and the outputs are pasted verbatim; the harness scripts and full raw logs are available on request.
Confirming #830 on a different platform and a newer version, with one correction to the original root-cause analysis: the file-level path constraint is already dropped by the first, plain grep call — the fuzzy fallback in the wrapper never runs.
Environment
@ff-labs/pi-fff0.10.6,@ff-labs/fff-node0.10.6- pi 0.85.1 — node script install (
/opt/homebrew/bin/pi→cli.js), Node v22.23.2, i.e. not the Bun binary - macOS 26.6.2, arm64
- Repo under test:
pytest-dev/pytest@8f81c76(651 git-tracked files). Nonode_modulesneeded.
Minimal repro (no node_modules required)
src/_pytest/__init__.py genuinely contains zero occurrences:
$ rg -c pytest_addoption src/_pytest/__init__.py
(exit 1, no matches)
ffgrep { pattern: "pytest_addoption", path: "src/_pytest/__init__.py" }
→ totalMatched: 20, every displayed match under doc/ (en/changelog.rst, en/announce/release-2.3.{2,3}.rst, en/example/{markers,parametrize,simple}.rst) and none from the pinned file. No [0 exact matches. Maybe you meant this?] notice, no cursor notice.
Control — built-in grep with identical arguments:
grep { pattern: "pytest_addoption", path: "src/_pytest/__init__.py" }
→ "No matches found"
What works vs. what silently widens
path form |
Result |
|---|---|
file that contains the pattern (src/_pytest/assertion/__init__.py) |
correct — exactly 1 match, from that file |
directory (src/_pytest/) |
correct |
glob (src/_pytest/*.py, src/**/*.py) |
correct |
| file with 0 occurrences | wrong — repo-wide results |
non-existent filename (nonexistent_xyz.py) |
wrong — repo-wide results |
.git/config (unindexed, no extension → normalized to a dir prefix) |
No matches found, no leak |
Root cause sits upstream of the fuzzy fallback
I instrumented a copy of the 0.10.6 source — the installed package was not modified — with logs around picker.grep() and inside the result.items.length === 0 fallback branch:
[PROBE] query="src/_pytest/__init__.py pytest_addoption" mode=plain smartCase=true pathParam="src/_pytest/__init__.py"
[PROBE] exact items=7 nextCursor="set" totalMatched=7 files=["doc/en/changelog.rst"]
(no "[PROBE] fallback entered")
The plain, non-fuzzy call already returns out-of-scope files, and the fallback branch in src/index.ts is never reached — consistent with the absence of its [0 exact matches. Maybe you meant this?] prefix. The FilePath token produced by normalizePathConstraint() in src/query.ts is not honored by the engine query parser when that file yields no hits; it appears to degrade into a fuzzy term, and the pattern then matches repo-wide.
This also explains why the #697 fix protects directory constraints but not file constraints: that fix keeps the constrained query when pathTargetsFile === false, so directories and globs behave correctly here, while file constraints do not.
Also reproduced through a real pi session
pi -p --mode json --tools ffgrep, with the model instructed to call ffgrep once using those exact arguments → the same 20 doc/ matches. The model then retried the identical call, which I read as evidence that the output is indistinguishable from a legitimate result set.
Why it matters, and two options
An agent that explicitly pins a file receives confident-looking matches from unrelated files, with no signal that its constraint was discarded. Two changes would make this safe:
- Return
No matches foundwhen a FilePath constraint yields 0 hits, and let any deliberate widening stay explicit and labeled; or - apply the constraint as a hard filter inside the fallback as well.
Related, same family: fffind { pattern: "cacheprovider", path: "src/_pytest/cacheprovider.py" } returns testing/test_cacheprovider.py alongside the pinned file, so file constraints are not strict for fffind either.