Repository navigation
fix(grep): accept standard grep flags — eliminate 62% of parse failures - #127
Merged
Merged
Conversation
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]>
# Conflicts: # src/main.rs
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]>
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
Fixes the worst filter in the suite (E2E review P0-grep). The
grepfilter'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.Quietroute —-q/--quiet/-iqruns rg quiet (no stdout, exit-code only); was the single biggest failure source (811).extra_args, forwarded to rg, output filtered normally.-h/-Groute to passthrough —rg -hmeans--help,rghas no-G, so they must NOT be forwarded to rg (regression caught in review).-r/-R/-E,-A/-B/-C,-c/-l/-L/-o/-Zrouting unchanged.Test Plan
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 andgrep_cmd.rs'ssplitn(3,':')misreadsline:content. Warrants its own ticket.Part of the 2026-05-22 E2E review remediation.
🤖 Generated with Claude Code