feat(tools): optional grep context lines (#76) - #82
Merged
Conversation
…llow-up whole-file reads (#76) grep returned only the matching line, so to understand a hit the agent had to issue a second `file_read` of the whole file — extra turns and tokens. Add an opt-in `context` parameter (like `grep -C`). - search.zig: `grepTextContext` / `grepFileContext` return `ContextBlock`s (start line + lines + hit mask); each hit is expanded to ±N lines and adjacent/overlapping windows are merged into one block. `context` is clamped to `[0, max_grep_context=20]` to bound memory/budget. - agent.zig: `GrepArgs` gains optional `context`; shared `grepObservation` helper (used by execTool and the parallel worker) routes context==0 to the existing `formatGrepHits` (unchanged) and context>0 to new `formatGrepBlocks` — hit lines `lineno:text`, context lines `lineno-text`, blocks separated by `--`. Updated the grep action description. - docs: tools chapter (en/zh) documents the `context` option. Tests: search-level (±N window, adjacent-merge, far-apart split) and agent-level grepObservation (plain vs context formatting). zig build + zig build test green; mdBook en/zh build. Closes #76 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #76.
greppreviously returned only the matching line. To understand a hit (surrounding code, why it matched), the agent had to issue a secondfile_readof the whole file — extra ReACT turns and a large token spend. This adds an opt-incontextparameter so a single grep can return the lines around each hit, the same ergonomics asgrep -C.Changes
src/tools/search.zigContextBlock(start_line,lines,hit_mask),grepTextContext/grepFileContext.contextclamped to[0, max_grep_context = 20]to bound memory and observation budget.src/agent.zigGrepArgsgains optionalcontext.grepObservationhelper used by bothexecTooland the parallel worker:context == 0→ existingformatGrepHits(behavior unchanged).context > 0→ newformatGrepBlocks: hit lineslineno:text, context lineslineno-text, blocks separated by--, header notes ±N.{"context":N}.toolschapter (en + zh) documents the option.Compatibility
Fully backward compatible — omitting
context(or0) keeps the exact previous output.Testing
zig build✅zig build test✅ (added search-level tests: ±N window with hit marking, adjacent-merge, far-apart split; agent-levelgrepObservation: plain vs context formatting)