agentgrep grep/find ignore file scope, causing unbounded workspace scans and shared-server OOM
Summary
The agentgrep tool schema accepts a file field for single-file grep and find requests, but the grep and find argument builders ignore it and only inspect path.
When file is provided and path is omitted, the search silently falls back to the session working directory. If jcode was launched from the user home directory, a request intended for one file scans the entire home directory instead.
This can exhaust memory because agentgrep v0.1.6 captures all ripgrep stdout with Command::output, then builds an in-memory map of every match and processes every matched file.
The bug is present in public v0.54.4 and current master at ccf6153e.
Safe reproduction
A tests-first reproduction is available here:
https://github.com/apierron/jcode/commit/4b8d6b5486820e5cc0e4166538728c18c977fdfe
The tests use only a temporary directory and do not perform a real home-directory scan.
Run:
git fetch https://github.com/apierron/jcode.git fix/agentgrep-file-scope-oom
git checkout 4b8d6b5486820e5cc0e4166538728c18c977fdfe
cargo test -p jcode-app-core build_grep_and_find_args_scope_file_field_to_exact_file -- --nocapture
cargo test -p jcode-app-core execute_grep_file_field_does_not_scan_sibling_files -- --nocapture
Both tests fail on the reproduction commit:
build_grep_and_find_args_scope_file_field_to_exact_file
expected parent path, got None
execute_grep_file_field_does_not_scan_sibling_files
sibling.rs was included even though file selected app.rs
Equivalent tool input:
{
"mode": "grep",
"query": "target",
"file": "src/app.rs"
}
Expected behavior
For grep and find, file should restrict the search to that exact file, as documented by the tool schema.
The search root should become the file parent and the glob should become the exact filename.
Actual behavior
The file field is deserialized but ignored by build_grep_args and build_find_args.
The search root falls back to the session working directory. A sibling file matches in the safe reproduction. With a home-directory working directory, the same behavior expands to the entire home directory.
Observed impact
On macOS a single affected search caused the shared server RSS to grow from about 300 MB to 50.7 GB before the kernel killed it.
Redacted kernel trace:
memorystatus: killing largest compressed process jcode [PID] 613542 MB
Because the killed process was the shared server, all connected clients then appeared to retry their provider requests indefinitely. Starting another jcode process recreated the server, which made all clients reconnect at once. The provider endpoint was healthy immediately before the kill.
This severity depends on workspace size and query selectivity. Users running from a project directory may see only a slow or unexpectedly broad search. Users running from their home directory can hit machine-wide memory pressure.
Root cause
The schema and deserializer support file and file_path, but grep and find call resolved_search_scope with params.path only.
Separately, agentgrep currently has no native match or output bound. Related upstream request:
https://github.com/1jehuang/agentgrep/issues/3
Reference fix branch
https://github.com/apierron/jcode/tree/fix/agentgrep-file-scope-oom
Branch structure:
-
Safe failing reproduction: https://github.com/apierron/jcode/commit/4b8d6b5486820e5cc0e4166538728c18c977fdfe
-
Fix and safety guard: https://github.com/apierron/jcode/commit/9272951c3bed6250fb8d3277df3a795a6592b2fa
The fix:
- makes file take precedence over path for grep and find scope resolution
- allows a file-only find request to count as scoped
- rejects an unscoped search only when the session working directory is exactly the user home directory
- preserves explicitly scoped searches from the home directory
- preserves existing file-valued path behavior
Separately, it might be a good idea to harden shared server recovery to stop retrying if the server entirely dies, or otherwise fix the endless retry behavior.
Validation
On the fix commit:
- all 31 agentgrep tests pass
- cargo check -p jcode-app-core --tests passes
- cargo fmt --all -- --check passes
- git diff --check origin/master...HEAD passes
- scripts/dev_cargo.sh build --profile selfdev -p jcode --bin jcode passes
The exact search that previously triggered the memory blow-up completed in 3 ms after the fix. Shared-server RSS increased by 112 KB for that call and remained stable at about 264 MB afterward.
Environment
- jcode v0.54.4 and master at ccf6153e are confirmed affected
- observed on macOS aarch64
- the file-scope bug is platform-independent
Fix is up in #553. The file field is now honored by grep/find scope resolution: when path is absent, search is scoped to the file's parent directory with an exact-filename glob, avoiding unbounded workspace scans.
Thanks @apierron for the precise repro tests. They were cherry-picked into the PR and both pass with the fix (cargo test -p jcode-app-core agentgrep: 30 passed).
— Jcode agent (automated triage), on behalf of @1jehuang