Skip to content

fix(grep): accept standard grep flags — eliminate 62% of parse failures - #127

Merged
thehoff merged 3 commits into
developfrom
fix/grep-arg-parser
May 22, 2026
Merged

thehoff merged 3 commits into
developfrom
fix/grep-arg-parser

Conversation

@thehoff

@thehoff thehoff commented May 22, 2026

Copy link
Copy Markdown
Owner

Summary

Fixes the worst filter in the suite (E2E review P0-grep). The grep filter's clap parser rejected standard grep flags (-q -i -w -x -v -F -P -G …) as "unexpected argument" — 1,255 parse-failures, 62% of all 2,016 in real tracked usage.

  • New Quiet route — -q/--quiet/-iq runs rg quiet (no stdout, exit-code only); was the single biggest failure source (811).
  • Recognised boolean flags are reordered behind the positional pattern so clap parses them into extra_args, forwarded to rg, output filtered normally.
  • Value-flags route to raw passthrough.
  • -h/-G route to passthrough — rg -h means --help, rg has no -G, so they must NOT be forwarded to rg (regression caught in review).
  • -r/-R/-E, -A/-B/-C, -c/-l/-L/-o/-Z routing unchanged.

Test Plan

  • 28-flag matrix: 0 parse failures (was 10+); 87 grep tests pass
  • full bin suite 2425/0
  • CI full suite

Known pre-existing follow-up (not introduced here, reproduces on develop): grep <pattern> <single-file> mis-displays — rg omits the filename for a single file and grep_cmd.rs's splitn(3,':') misreads line:content. Warrants its own ticket.

Part of the 2026-05-22 E2E review remediation.

🤖 Generated with Claude Code

thehoff and others added 3 commits May 22, 2026 11:06
The grep filter's clap `Grep` variant rejected any standard grep flag
appearing before the `<PATTERN>` positional (`unexpected argument`),
falling through to `run_fallback` and logging a parse failure every
time. Measured against 14.7k tracked commands: 1,255 grep parse
failures — 62% of all parse failures tool-wide.

The pre-clap preprocessor previously handled only `-r`/`-R`/`-E`
(strip) and `-A`/`-B`/`-C`/`-H` (passthrough). Every other common
flag (`-q`, `-i`, `-w`, `-x`, `-v`, `-n`, `-F`, `-P`, `-G`, bundles
like `-iq`) was rejected.

Changes to `preprocess_grep_args`:
- New `GrepPreprocess::Quiet` variant: `-q`/`--quiet`/`--silent`
  (811 failures — the single biggest source) routes to a quiet
  rg run; no stdout, exit code only, never a parse failure.
- Value-taking flags (`-e`/`-f`/`-m`/`--include`/…) route to
  passthrough where rg parses them natively.
- Pass 4 reorder: recognised boolean grep flags (`-i`, `-w`, `-x`,
  `-v`, `-n`, `-F`, `-P`, `-G`, … plus long forms) are moved behind
  the positional pattern/path so clap parses them into `extra_args`
  (`trailing_var_arg` + `allow_hyphen_values`). `grep_cmd::run`
  forwards `extra_args` to rg and still filters the output normally.
- `-c`/`-l`/`-L`/`-o`/`-Z` still route to documented raw passthrough.

Verified: a 28-case flag matrix that produced 10 parse failures on
`develop` now produces 0. Full bin test suite: 2425 passed, 0 failed.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@thehoff
thehoff merged commit 91809bd into develop May 22, 2026
4 checks passed
@thehoff
thehoff deleted the fix/grep-arg-parser branch May 22, 2026 04:35
thehoff added a commit that referenced this pull request Jun 5, 2026
Issue rtk-ai#193: grep is ~1/3 of command traffic but averaged ~2.6% token
savings because flagged/recursive calls fell through to raw passthrough
(0 savings). PR #127 fixed the clap parse errors for standard grep flags,
but context-flag calls (-A/-B/-C) then routed to run_grep_format_passthrough,
which inherits stdout straight to the terminal — no filtering at all. This
was the single highest-yield grep shape (861 of 1629 fallbacks used -r,
most paired with context flags).

Change:
- Add grep_cmd::filter_context_output: parses rg/grep context output by
  splitting on the `--` group separator, keeps each match-group intact
  (context lines + the `file-line-` vs `file:line:` format are preserved),
  and caps groups per-file (grep_max_per_file=25) and globally
  (grep_max_results=200) using the existing [limits] config. Truncation is
  always signalled with a "[+N more groups]" marker so matches are never
  dropped silently.
- Add run_grep_context_filtered in main.rs: for the Passthrough variant,
  route true context-flag calls (has_grep_context_flag) through rg with
  CAPTURED output, filter it, and apply the no_bloat guard so a small
  result is never inflated by the framing. -H/value-flag calls (no context
  flag) stay on the raw passthrough — single-line output, nothing to group.
- Fallback discipline: if rg is absent or capture fails, fall back to the
  raw passthrough so the user always gets their matches.

Council/Codex review fix: group_file now buckets the per-file cap on the
RAW path, not the compacted display form. The old code keyed per_file_seen
on compact_path(...), so two distinct files that collapse to the same short
display string shared one counter and wrongly truncated each other (the
emitted group text uses raw paths, so the compaction served no display
purpose here). compact_path stays for display in the non-context filter.

Measured on a real recursive context grep (grep -rn -C1 let src/hooks/):
6497 -> 966 lines, 85.1% token savings (29095 -> 4348 tokens). Previously
this path yielded 0% (raw passthrough).

Tests (tests/fixtures/grep_context_raw.txt is real `grep -rn -C1` output):
- grouping + per-file cap, global cap, empty input, file attribution
- golden-form snapshot (repo has no insta dep; deterministic assertions)
- token-savings test asserting >=60% on the recursive fixture
- raw-path bucketing: distinct long files that compact to the same display
  form get separate per-file counters and do not truncate each other
All 99 grep tests pass; no new clippy warnings. The 7 pre-existing
hooks::integrity failures are environmental (/tmp mode 775 in sandbox) and
fail identically on the unmodified tree.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
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