Skip to content

feat(tools): give grep output modes, context lines, and case-insensitive search - #328

Open
raymondginger2018-sudo wants to merge 1 commit into
HKUDS:mainfrom
raymondginger2018-sudo:pr/grep-options
Open

raymondginger2018-sudo wants to merge 1 commit into
HKUDS:mainfrom
raymondginger2018-sudo:pr/grep-options

Conversation

@raymondginger2018-sudo

Copy link
Copy Markdown
Contributor

Summary

GrepTool rendered one shape only: every matching line, at most 200. A caller that wanted the file list or per-file counts had to fetch and parse the lines, and there was no way to ask for surrounding context or a case-insensitive match.

It now follows the grep contract the model already knows:

  • output_mode — content (default), files_with_matches, count
  • context — lines before/after each match, with context_before / context_after overrides
  • case_insensitive
  • head_limit — 0 means the hard cap

Changes

  • core/harness/tools/search.py
    • _Match carries path/line/text, so rendering is separate from searching.
    • Both backends implement every mode: ripgrep uses -i, --files-with-matches, --count; the Python fallback implements the same three shapes.
    • Context output is grouped per file, merges overlapping spans, and caps total lines (_MAX_CONTENT_LINES) so a wide context cannot flood the result.
    • Sensitive-path filtering applies in every mode, unchanged.
  • tests/test_grep_options.py (new) — each mode on both backends where rg exists.
  • tests/contract/test_default_capability_compatibility.py — the shared tool contract now declares grep's new optional parameters.

Compatibility

  • content output is unchanged for ripgrep. The Python fallback now emits path:line:text like ripgrep (it used to insert a space after the second colon).
  • Defaults preserve today's behaviour: output_mode=content, no context, case-sensitive, 200 matches.
  • No new dependency or configuration.

Tests

tests/test_grep_options.py 7 passed; tests/test_shell_search_tools.py -k grep 6 passed / 6 skipped (platform-specific). pre-commit run --files … clean.

…ive search

`GrepTool` returned one shape only — every matching line, 200 at most — so a
caller that wanted the file list or per-file counts had to fetch and parse the
lines. The tool now follows the grep contract the model already knows:
`output_mode` (`content` / `files_with_matches` / `count`), `context` (with
`context_before` / `context_after` overrides), `case_insensitive`, and
`head_limit`.

Both backends implement every mode. Ripgrep gets the native flags (`-i`,
`--files-with-matches`, `--count`); the Python fallback implements the same
three shapes, and the tests pin both so a machine without `rg` behaves the same
as one with it. Context output is grouped per file, merged when spans overlap,
and capped so a wide context cannot flood the result; protected paths are
filtered in every mode exactly as before.

The fallback's content shape now matches ripgrep's (`path:line:text`, no space
after the second colon); the previous fallback-only space was an accidental
divergence between the two backends.

Tests: tests/test_grep_options.py (7 cases, both backends where rg exists);
`tests/test_shell_search_tools.py -k grep` 6 passed / 6 skipped.

This branch has not been deployed

No deployments
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.

1 participant