Skip to content

feat(tools): optional grep context lines (#76) - #82

Merged
jamiesun merged 1 commit into
mainfrom
feat/i76-grep-context
Jun 18, 2026
Merged

feat(tools): optional grep context lines (#76)#82
jamiesun merged 1 commit into
mainfrom
feat/i76-grep-context

Conversation

@jamiesun

Copy link
Copy Markdown
Collaborator

Summary

Closes #76.

grep previously returned only the matching line. To understand a hit (surrounding code, why it matched), the agent had to issue a second file_read of the whole file — extra ReACT turns and a large token spend. This adds an opt-in context parameter so a single grep can return the lines around each hit, the same ergonomics as grep -C.

Changes

  • src/tools/search.zig
    • ContextBlock (start_line, lines, hit_mask), grepTextContext / grepFileContext.
    • Each hit expands to ±N lines; adjacent/overlapping windows merge into one block.
    • context clamped to [0, max_grep_context = 20] to bound memory and observation budget.
  • src/agent.zig
    • GrepArgs gains optional context.
    • Shared grepObservation helper used by both execTool and the parallel worker:
      • context == 0 → existing formatGrepHits (behavior unchanged).
      • context > 0 → new formatGrepBlocks: hit lines lineno:text, context lines lineno-text, blocks separated by --, header notes ±N.
    • Updated the grep action description to document {"context":N}.
  • docs: tools chapter (en + zh) documents the option.

Compatibility

Fully backward compatible — omitting context (or 0) 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-level grepObservation: plain vs context formatting)
  • mdBook en/zh build ✅

…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>
@jamiesun
jamiesun merged commit 62c9908 into main Jun 18, 2026
4 checks passed
@jamiesun
jamiesun deleted the feat/i76-grep-context branch June 18, 2026 07:50
@jamiesun jamiesun mentioned this pull request Jun 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[tools] grep 只回命中行,无上下文行(±N),常触发紧随其后的整文件读

1 participant