fff icon indicating copy to clipboard operation
fff copied to clipboard

ffgrep: path constraint targeting an unindexed file silently widens to a repo-wide fuzzy search

Open bfzha opened this issue 3 months ago • 2 comments

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.json exists on disk and actually contains jsx-runtime in its exports.

Reproduction

All commands run from the repo root:

  1. ffgrep { pattern: "jsx-runtime", path: "node_modules/react/package.json" } -> [0 exact matches. Maybe you meant this?] then matches from package-lock.json and tests/ — never from the pinned file (or anything under node_modules).

  2. ffgrep { pattern: "version", path: "node_modules/react/package.json" } -> first result is e2e/file-link-repro.spec.ts:50: version: 3, — completely unrelated to react.

  3. ffgrep { pattern: "sideEffects", path: "node_modules/react/package.json" } -> returns src/renderer/** files whose top hit is a useEffect line — fuzzy noise with zero relationship to either the query or the pinned path. (Because results are frecency-ranked, the App.tsx family shows up first for generic words.)

Control cases for contrast:

  • path: "node_modules/react/" (directory constraint, no trailing extension) + jsx-runtime -> clean No matches found (no leak — directory constraints keep the constrained query).
  • path: "this-dir-does-not-exist-xyz/" -> clean No 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) returns null for paths without a ../ prefix, so the query runs against the workspace finder, whose index excludes node_modules. Exact grep => 0 items.
  • On 0 items, pathTargetsFile is computed from the trailing extension, and when true the fuzzy fallback uses fuzzyQuery = pattern without 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

  1. Don't broaden when the target isn't in the index. If pathTargetsFile and the pinned path is not part of the picker's index (gitignored/excluded), return a clear message such as path not indexed (gitignored or excluded): no matches — or, better, read/match that single file on disk so the query actually answers the question.
  2. 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.
  3. The fuzzy fallback for common/generic patterns degrades into pure noise (sideEffects -> useEffect files). 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 routePathConstraint refuses workspace-relative paths even when they point outside the indexed set (e.g. node_modules/react/...).

bfzha avatar Aug 28 '26 03:08 bfzha

[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 🪿

gustav-fff avatar Aug 28 '26 03:08 gustav-fff

AI-generated report. Everything below was produced by an AI coding agent (pi coding agent 0.85.1, model deepseek-flash via the deepseek provider) 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-fff 0.10.6, @ff-labs/fff-node 0.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). No node_modules needed.

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:

  1. Return No matches found when a FilePath constraint yields 0 hits, and let any deliberate widening stay explicit and labeled; or
  2. 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.

yeshengxiao04-star avatar Sep 18 '26 18:09 yeshengxiao04-star