mirror of
https://github.com/badlogic/pi-mono.git
synced 2026-11-05 23:37:25 +00:00
fix(coding-agent): take grep context lines from ripgrep instead of reading matched files (#11705)
When `context` is set, the grep tool read every matched file whole and split it into lines, only to print the lines around each match. Memory grew with the size of the matched files, not the output. In the issue's reproduction (8 log files of about 95 MB, one match each, `context: 2`, `--max-old-space-size=1024`), the process ran out of heap after about 10 s. ripgrep already supports context. This change passes `--context N` to rg and formats the `context` events from its JSON output. The tool no longer opens matched files. Details: - After the last allowed match (`limit`), rg still sends that match's trailing context. The tool keeps those lines, then stops rg. A match inside that window is shown as a context line, as before. - Lines past the 50 KB output cap are counted but not stored, since truncation drops them anyway. Match counts, the match-limit notice, and `details.matchLimitReached` stay as before. - rg merges overlapping context, so lines shared by nearby matches are printed once instead of repeated per match. This is the one intended output change. - Context lines are decoded by rg, so UTF-16 files with a BOM show correctly with context (#11423, closed as no-action). Non-UTF-8 lines and paths, which rg sends base64-encoded, are decoded too. Two related grep fixes in the same code: - **Exact match limit:** with exactly N matches and `limit: N`, grep said "N matches limit reached. Use limit=2N". It now sets the notice and `details.matchLimitReached` only after rg sends one more match. Cost: when no further match exists, rg finishes the search instead of being stopped at the Nth match, the same as a search with fewer than N matches. - **Lone CR:** grep removed every `\r` in a line, so `one\rtwo needle` showed as `onetwo needle` and text copied from grep failed to match in edit. It now strips only the line terminator (`\n` or `\r\n`), like the read tool. The TUI still hides CRs when rendering. This updates the expectation in the #11398 test from `beforeprogress` to `before\rprogress`. Breaking: `GrepOperations.readFile` is removed because nothing uses it anymore. rg always ran locally, so this hook never made grep work on a remote backend. Docs now say to override `execute()` for remote search, as the Gondolin example does. Code that passes `readFile` still runs, and the value is ignored. Only code that type-checks an object literal against `GrepOperations` fails to compile. Known remaining differences and limits: - Memory no longer grows with file size or match count, but a single huge matching line (tens of MB) is still buffered whole before being cut to 500 characters. That was already true for `context: 0` on main. Bounding it would mean discarding oversized rg JSON records, which is a separate change. - In a directory search, rg stops reading a file once it finds a NUL byte, so context past that point in a text-looking file that turns binary later is omitted. main reread the file and printed it. Explicit file paths are unaffected. - `details.truncation.totalLines`/`totalBytes` count formatted rows. With deduplicated context, or file names containing newlines, they can differ from main. No renderer or notice displays them. **Testing** - Issue reproduction: before, `FATAL ERROR ... JavaScript heap out of memory`. After, correct output in about 60 ms with 135 MB RSS. - `pattern: "line", context: 3, limit: 100000` over the same 770 MB of logs reports both limit notices and peaks at 154 MB RSS. - New tests in `test/tools.test.ts`: - Merged overlapping context, plus trailing context at the limit including a match inside it. Fails on main. - UTF-16 with context. Fails on main. - Truncation reported when stored output ends exactly at 50 KB. Guards the counting logic. - Matches still counted past the byte cap. Passes on main too; it guards against stopping rg early at the cap. - Huge `context` value accepted. - No limit notice with exactly `limit` matches (with and without context), notice with more. Fails before this change. - Lone CR kept and CRLF stripped, with and without context. Fails before this change. - All 94 tests in the file pass. `npm run check` passes. fixes #9276 This PR is AI-generated.
This commit is contained in:
parent
2bd2c2d0a6
commit
8da141c2ad
4 changed files with 215 additions and 69 deletions
|
|
@ -2418,6 +2418,8 @@ The `create*ToolDefinition()` factories, such as `createReadToolDefinition()`, r
|
|||
|
||||
**Operations interfaces:** `ReadOperations`, `WriteOperations`, `EditOperations`, `BashOperations`, `PowerShellOperations`, `LsOperations`, `GrepOperations`, `FindOperations`
|
||||
|
||||
`GrepOperations` only covers the directory check: grep always runs the local `rg`. To search a remote system, override grep's `execute()` instead, as the [Gondolin example](../examples/extensions/gondolin/index.ts) does.
|
||||
|
||||
For `user_bash`, extensions can reuse Pi's local shell backend via `createLocalBashOperations()` (or `createLocalPowerShellOperations()`) instead of reimplementing local process spawning, shell resolution, and process-tree termination.
|
||||
|
||||
The `bash` and `powershell` tools also support a spawn hook to adjust the command, cwd, or env before execution:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue