Repository navigation
refacto(search): run the invoked engine instead of substituting rg for grep - #2641
Merged
Merged
Conversation
- grep runs grep, rg runs rg: drop the substitution, forced --no-ignore-vcs, and BRE-to-rg translation - add `rtk rg` command (native ripgrep, sharing the same output filter) - split rewrite rule: grep to rtk grep, rg to rtk rg - record the agent's real command in tracking (was synthesized as "grep -rn") - emit nothing on a clean no-match (never-worse parity with the shared guard) - rename grep_cmd.rs to search.rs, now hosting both engines - cover engine faithfulness, ignore semantics, and rg savings with issue-referenced tests - benchmark the grep and rg paths
aeppling
force-pushed
the
fix/grep-faithful-engine
branch
from
June 26, 2026 12:20
190b907 to
eafadce
Compare
aeppling
marked this pull request as ready for review
June 26, 2026 12:36
pszymkowiak
approved these changes
Jun 26, 2026
- line number always (the openable position); filename only when grep/rg prints one (multi-file, dir, -r, -H) - single file and stdin equal `grep -n` byte-for-byte: no synthetic filename, header, or lossy truncation - read piped stdin instead of injecting `.`; rg no longer searches the cwd on a pipe - pattern-less and format-flag commands run verbatim (rg --type-list, --files, grep -c/-o) - grouped/capped form only when capping actually shrinks the output - byte-for-byte tests vs grep -n across flags, pipes and edge cases (incl #1436)
- leading ^ anchor must stay scoped to the path (tenequm) - line ending in `:` keeps full content (BTCAlchemist) - rg surfaces a regex parse error as exit 2, not a silent 0 (jhagberg)
- grep_compress_test.rs -> search_compress_test.rs (tests the shared grep/rg filter) - binary match skipped as noise by default, -a opts back in - fix parse_flags doc: -I is an intentional binary skip, not a no-op
aeppling
force-pushed
the
fix/grep-faithful-engine
branch
from
June 26, 2026 15:49
8976db5 to
059131c
Compare
- drop the synthetic "search failed with exit code N" line (an rtk addition) - surface the engine stderr verbatim in every path, incl. partial match + error - remove the now-vestigial is_grep_error_exit (the "0 matches" it guarded is gone) - error/exit matrix tests vs real grep/rg (#2465: an error is never a silent no-match)
Merged
pidefrem
added a commit
to pidefrem/rtk
that referenced
this pull request
Jun 26, 2026
GNU/BSD grep -h is --no-filename, but rtk grep -h printed the rtk help banner: clap intercepted -h before run() saw it, and the version/help passthrough also treated -h as help. Free -h via disable_help_flag plus a long-only --help (ArgAction::Help), so rtk grep --help still shows rtk help while -h reaches the arg layer. rtk forces -H --null for NUL-delimited parsing, so a forwarded -h (last-wins) would suppress the filename at the engine, break parsing, and drop line numbers. Strip -h/--no-filename from the engine args (letter-wise in clusters like -hi) and re-apply no-filename purely at the display layer (show_file = false), so output matches grep -n -h. Replaces the obsolete GNU-arg IR layer (former brief 002): upstream rtk-ai#2641 (grep runs grep, rg runs rg) removed the flag-collision class the IR solved, leaving only this -h slice. Tests pin rtk grep -h / -hi == grep -n -h.
pidefrem
added a commit
to pidefrem/rtk
that referenced
this pull request
Jun 26, 2026
GNU/BSD grep -h is --no-filename, but rtk grep -h printed the rtk help banner: clap intercepted -h before run() saw it, and the version/help passthrough also treated -h as help. Free -h via disable_help_flag plus a long-only --help (ArgAction::Help), so rtk grep --help still shows rtk help while -h reaches the arg layer. rtk forces -H --null for NUL-delimited parsing, so a forwarded -h (last-wins) would suppress the filename at the engine, break parsing, and drop line numbers. Strip -h/--no-filename from the engine args (letter-wise in clusters like -hi) and re-apply no-filename purely at the display layer (show_file = false), so output matches grep -n -h. Replaces the obsolete GNU-arg IR layer (former brief 002): upstream rtk-ai#2641 (grep runs grep, rg runs rg) removed the flag-collision class the IR solved, leaving only this -h slice. Tests pin rtk grep -h / -hi == grep -n -h.
5 tasks done
pidefrem
added a commit
to pidefrem/rtk
that referenced
this pull request
Jun 26, 2026
GNU/BSD grep -h is --no-filename, but rtk grep -h printed the rtk help banner: clap intercepted -h before run() saw it, and the version/help passthrough also treated -h as help. Free -h via disable_help_flag plus a long-only --help (ArgAction::Help), so rtk grep --help still shows rtk help while -h reaches the arg layer. rtk forces -H --null for NUL-delimited parsing, so a forwarded -h (last-wins) would suppress the filename at the engine, break parsing, and drop line numbers. Strip -h/--no-filename from the engine args (letter-wise in clusters like -hi) and re-apply no-filename purely at the display layer (show_file = false), so output matches grep -n -h. Replaces the obsolete GNU-arg IR layer (former brief 002): upstream rtk-ai#2641 (grep runs grep, rg runs rg) removed the flag-collision class the IR solved, leaving only this -h slice. Tests pin rtk grep -h / -hi == grep -n -h.
2 of 4 tasks
pidefrem
added a commit
to pidefrem/rtk
that referenced
this pull request
Jul 18, 2026
GNU/BSD grep -h is --no-filename, but rtk grep -h printed the rtk help banner: clap intercepted -h before run() saw it, and the version/help passthrough also treated -h as help. Free -h via disable_help_flag plus a long-only --help (ArgAction::Help), so rtk grep --help still shows rtk help while -h reaches the arg layer. rtk forces -H --null for NUL-delimited parsing, so a forwarded -h (last-wins) would suppress the filename at the engine, break parsing, and drop line numbers. Strip -h/--no-filename from the engine args (letter-wise in clusters like -hi) and re-apply no-filename purely at the display layer (show_file = false), so output matches grep -n -h. Replaces the obsolete GNU-arg IR layer (former brief 002): upstream rtk-ai#2641 (grep runs grep, rg runs rg) removed the flag-collision class the IR solved, leaving only this -h slice. Tests pin rtk grep -h / -hi == grep -n -h.
pidefrem
added a commit
to pidefrem/rtk
that referenced
this pull request
Jul 18, 2026
GNU/BSD grep -h is --no-filename, but rtk grep -h printed the rtk help banner: clap intercepted -h before run() saw it, and the version/help passthrough also treated -h as help. Free -h via disable_help_flag plus a long-only --help (ArgAction::Help), so rtk grep --help still shows rtk help while -h reaches the arg layer. rtk forces -H --null for NUL-delimited parsing, so a forwarded -h (last-wins) would suppress the filename at the engine, break parsing, and drop line numbers. Strip -h/--no-filename from the engine args (letter-wise in clusters like -hi) and re-apply no-filename purely at the display layer (show_file = false), so output matches grep -n -h. Replaces the obsolete GNU-arg IR layer (former brief 002): upstream rtk-ai#2641 (grep runs grep, rg runs rg) removed the flag-collision class the IR solved, leaving only this -h slice. Tests pin rtk grep -h / -hi == grep -n -h.
pidefrem
added a commit
to pidefrem/rtk
that referenced
this pull request
Jul 18, 2026
GNU/BSD grep -h is --no-filename, but rtk grep -h printed the rtk help banner: clap intercepted -h before run() saw it, and the version/help passthrough also treated -h as help. Free -h via disable_help_flag plus a long-only --help (ArgAction::Help), so rtk grep --help still shows rtk help while -h reaches the arg layer. rtk forces -H --null for NUL-delimited parsing, so a forwarded -h (last-wins) would suppress the filename at the engine, break parsing, and drop line numbers. Strip -h/--no-filename from the engine args (letter-wise in clusters like -hi) and re-apply no-filename purely at the display layer (show_file = false), so output matches grep -n -h. Replaces the obsolete GNU-arg IR layer (former brief 002): upstream rtk-ai#2641 (grep runs grep, rg runs rg) removed the flag-collision class the IR solved, leaving only this -h slice. Tests pin rtk grep -h / -hi == grep -n -h.
pidefrem
added a commit
to pidefrem/rtk
that referenced
this pull request
Jul 18, 2026
GNU/BSD grep -h is --no-filename, but rtk grep -h printed the rtk help banner: clap intercepted -h before run() saw it, and the --version/--help passthrough also treated -h as help. Free -h via disable_help_flag plus a long-only --help (ArgAction::Help), so rtk grep --help still shows rtk help while -h reaches the search path. rtk forces -H --null for NUL-delimited parsing, so a forwarded -h (last-wins) would suppress the filename at the engine, break parsing, and drop line numbers. Strip -h/--no-filename from the engine args (letter-wise in clusters like -hi) and re-apply no-filename purely at the display layer (show_file = false), so output matches grep -n -h. Replaces the obsolete GNU-arg IR layer (former brief 002): upstream rtk-ai#2641 (grep runs grep, rg runs rg) removed the flag-collision class the IR solved, leaving only this -h slice. Tests pin rtk grep -h / -hi == grep -n -h, that --help still shows help, and that \| keeps its per-dialect meaning (literal in -E/-F, alternation in BRE) against grep -n.
pidefrem
added a commit
to pidefrem/rtk
that referenced
this pull request
Jul 18, 2026
GNU/BSD grep -h is --no-filename, but rtk grep -h printed the rtk help banner: clap intercepted -h before run() saw it, and the --version/--help passthrough also treated -h as help. Free -h via disable_help_flag plus a long-only --help (ArgAction::Help), so rtk grep --help still shows rtk help while -h reaches the search path. rtk forces -H --null for NUL-delimited parsing, so a forwarded -h (last-wins) would suppress the filename at the engine, break parsing, and drop line numbers. Strip -h/--no-filename from the engine args (letter-wise in clusters like -hi) and re-apply no-filename purely at the display layer (show_file = false), so output matches grep -n -h. Replaces the obsolete GNU-arg IR layer (former brief 002): upstream rtk-ai#2641 (grep runs grep, rg runs rg) removed the flag-collision class the IR solved, leaving only this -h slice. Tests pin rtk grep -h / -hi == grep -n -h, that --help still shows help, and that \| keeps its per-dialect meaning (literal in -E/-F, alternation in BRE) against grep -n.
pidefrem
added a commit
to pidefrem/rtk
that referenced
this pull request
Jul 18, 2026
On the rtk rg path, short -r/-E were parsed with grep semantics (boolean recursive / extended-regexp), but ripgrep reads them as value-taking --replace / --encoding. So a native-rg invocation real rg accepts broke: rtk rg -r REPL world file -> rg: world: No such file (os error 2) rtk rg -E utf-8 world -> rg: unknown encoding: -e Both now match raw rg (hello REPL / hello world). Fix makes the short-flag value-set engine-aware: Engine::Rg adds r/E to the value-taking set, Engine::Grep keeps them boolean (grep-path unchanged, pinned by test). The merged rtk-ai#2641 engine split fixed the long forms (--replace/--encoding) and the old grep -rn -> 'n' mangling; this closes the short-form residual. Fixes rtk-ai#2253.
pidefrem
added a commit
to pidefrem/rtk
that referenced
this pull request
Jul 22, 2026
On the rtk rg path, short -r/-E were parsed with grep semantics (boolean recursive / extended-regexp), but ripgrep reads them as value-taking --replace / --encoding. So a native-rg invocation real rg accepts broke: rtk rg -r REPL world file -> rg: world: No such file (os error 2) rtk rg -E utf-8 world -> rg: unknown encoding: -e Both now match raw rg (hello REPL / hello world). Fix makes the short-flag value-set engine-aware: Engine::Rg adds r/E to the value-taking set, Engine::Grep keeps them boolean (grep-path unchanged, pinned by test). The merged rtk-ai#2641 engine split fixed the long forms (--replace/--encoding) and the old grep -rn -> 'n' mangling; this closes the short-form residual. Fixes rtk-ai#2253.
pidefrem
added a commit
to pidefrem/rtk
that referenced
this pull request
Jul 22, 2026
GNU/BSD grep -h is --no-filename, but rtk grep -h printed the rtk help banner: clap intercepted -h before run() saw it, and the --version/--help passthrough also treated -h as help. Free -h via disable_help_flag plus a long-only --help (ArgAction::Help), so rtk grep --help still shows rtk help while -h reaches the search path. rtk forces -H --null for NUL-delimited parsing, so a forwarded -h (last-wins) would suppress the filename at the engine, break parsing, and drop line numbers. Strip -h/--no-filename from the engine args (letter-wise in clusters like -hi) and re-apply no-filename purely at the display layer (show_file = false), so output matches grep -n -h. Replaces the obsolete GNU-arg IR layer (former brief 002): upstream rtk-ai#2641 (grep runs grep, rg runs rg) removed the flag-collision class the IR solved, leaving only this -h slice. Tests pin rtk grep -h / -hi == grep -n -h, that --help still shows help, and that \| keeps its per-dialect meaning (literal in -E/-F, alternation in BRE) against grep -n.
pidefrem
added a commit
to pidefrem/rtk
that referenced
this pull request
Jul 22, 2026
On the rtk rg path, short -r/-E were parsed with grep semantics (boolean recursive / extended-regexp), but ripgrep reads them as value-taking --replace / --encoding. So a native-rg invocation real rg accepts broke: rtk rg -r REPL world file -> rg: world: No such file (os error 2) rtk rg -E utf-8 world -> rg: unknown encoding: -e Both now match raw rg (hello REPL / hello world). Fix makes the short-flag value-set engine-aware: Engine::Rg adds r/E to the value-taking set, Engine::Grep keeps them boolean (grep-path unchanged, pinned by test). The merged rtk-ai#2641 engine split fixed the long forms (--replace/--encoding) and the old grep -rn -> 'n' mangling; this closes the short-form residual. Fixes rtk-ai#2253.
This was referenced Sep 6, 2026
This was referenced Sep 6, 2026
This was referenced Sep 12, 2026
KuSh
pushed a commit
that referenced
this pull request
Sep 25, 2026
`rtk grep -l`, `grep -L`, `rg -l`, and `rg --files` print every path in full and record 0/0 savings. Agents search from absolute roots, so each line repeats the same long directory prefix. Fold that prefix into a `<prefix> (N files)` header and print each path's tail. The transform is lossless, keeps engine order, cuts only on whole components, and runs under `never_worse`. It applies only to a bare file list: any other shape flag (`-c`, `-Z`, `--null`, `--json`, `-q`) changes the line and leaves the list verbatim. Streaming stdin passthrough is untouched. Track the fold with real sizes so it shows in `rtk gain`; unchanged passthrough keeps recording 0/0. Follow-up to #2630, which targeted the pre-#2641 grep_cmd.rs.
This was referenced Sep 26, 2026
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.
fix(grep): run the invoked engine instead of substituting rg for grep
Goal
Make
rtk grepandrtk rga transparent output filter: run the agent's exact search command, compress only its output noise, and report savings against the real command.Stop substituting one engine for the other. That substitution fabricated savings, broke regex dialects, flooded node_modules, and mislabeled the metrics DB, and maintenance (patch on patch)
The problem
rtk grepran ripgrep internally regardless of what the agent typed, then forced flags to make rg act like grep. Every band-aid leaked:grep 'al|ga'"saved 137K" but real grep matches 0|is alternation, grep BRE|is literal, so savings were measured against rg's inflated outputrg ...rewritten tortk grepran BSD grep: wrong dialect, recursion, 0-match / stdin hangrgintortk grepgrep -rn .recorded 302M tokens "saved" (9.6 min, 1.2 GB)--no-ignore-vcsmade rg read node_modules and minified filesgrep -rn '...', so you cannot tell grep from rgWhat changed
Engine { Grep, Rg };engine_captureruns the exact binary and flags, appending only parse aids (-n -H -I --nullfor grep,-n --with-filename --nullfor rg). They shape the output for grouping; they do not change which lines match.\|to|translation, the forced--no-ignore-vcs, and the rg-only-flag stripping (strip_rg_only,drop_recursion_for_rg,RG_ONLY_LONG,strip_rall deleted).rtk rgcommand: runs ripgrep natively (keeping its.gitignoreand binary skipping and its dialect), reusing the same output filter. grep and rg emit the identicalfile:line:contentshape, so the compression is shared, not duplicated.discover/rules.rs):greptortk grep,rgtortk rg. The engine is preserved end to end.rg -i foo src/,grep -rn foo .) with its actual flags, instead of the synthesizedgrep -rn '...'.grep_cmd.rstosearch.rs: it hosts both engines now, andautomodauto-declares it, so the rename is a no-op for module wiring.scripts/benchmark.sh: the grep section now passes-r(we no longer force recursion), and a new rg section was added.Verification
Local, all CI gates green:
cargo fmt --all -- --check,cargo clippy --all-targets,cargo test --all(2247 pass),cargo build --release.Behavior:
rtk grep 'al|ga'is BRE literal (0 matches, honest);rtk rg 'al|ga'is alternation (210 matches, what rg was asked for).rtk rg 'X' .stays lean (no node_modules flood).rg fn src/coreandgrep -rn fn src/core(real engine and flags).Benchmark (
scripts/benchmark.sh):grep fn70611 to 4247 (93%),grep -c95%,grep -l 4099%.rg fn70611 to 4247 (93%);rg -landrg -cpass through at 0% (already minimal).rtk rg(0.37s) is faster thanrtk grep(0.56s) since rg stays lean.Tests
Engine faithfulness now has dedicated coverage, each test tagged with the issue it guards:
grep_and_rg_use_their_own_regex_dialect(grep/rg: matches silently rewritten ("def" → "n") — grep -rn/-E collide with ripgrep's value-taking -r/--replace and -E/--encoding #2253,rtk hook clauderewritesrg→rtk grep(BSD grep) instead ofrtk rg, so path-less ripgrep searches silently return 0 matches / hang on stdin #2301, Need fixgrepsavings to much lower #545):grep 'al|ga'stays a literal pipe (0 matches, exit 1);rg 'al|ga'is alternation (matches, exit 0).grep_dash_e_selects_extended_regex(grep/rg: matches silently rewritten ("def" → "n") — grep -rn/-E collide with ripgrep's value-taking -r/--replace and -E/--encoding #2253, rtk grep calls system grep instead of ripgrep — breaks -g/--glob flags #2167):-Ereaches grep as ERE, never read as rg--encoding.rg_glob_flag_filters_the_file_set(rg auto-rewrite to rtk grep breaks normal ripgrep flag ordering (--glob,-g,--version) #1824, rtk grep calls system grep instead of ripgrep — breaks -g/--glob flags #2167): rg-g '*.rs'filters the file set.rg_honors_ignore_files_grep_does_not(rtk rewritesrgintortk grep(recursivegrep -rn), losing ripgrep's .gitignore/binary skipping → CPU spikes on large repos #2606, grep -rn fallback causes token *expansion*: execs system grep, descends node_modules, dumps minified files #2064): rg skips ignored files (no node_modules flood) while grep-rdescends.bulky_rg_yields_token_savingsandbulky_grep_yields_token_savings(Need fixgrepsavings to much lower #545): savings measured against the real engine output.parse_match_linesuite (The rtk grep is not grep-compatible #1436, Bug:rtk grepmisparses single-file rg output when content contains colons #1613): NUL-separated parse survives content and path colons.Removed two stale unit tests that documented deleted behavior (BRE-to-rg translation, forced
--no-ignore-vcs). Adapted develop'sgrep_no_match_emits_empty_not_a_messageto pass-r, since faithful grep no longer auto-recurses a directory.Rebased onto develop, mergeable with no conflicts.
cargo fmt,clippy --all-targets,test --all(2265 pass) andbuild --releaseall green.Scope kept small (no new edge cases)
-H --nullis retained as a parse aid so the NUL-based regrouping (#1436) stays unambiguous, and the parser is untouched. Single-file output therefore keeps its small, stable filename column; stripping it would require single-versus-multi detection, a new failure mode, so it is left as-is.Issues resolved
rgintortk grep(recursivegrep -rn), losing ripgrep's .gitignore/binary skipping → CPU spikes on large repos #2606 rg tortk greploses.gitignoreand binary skipping, CPU spikes: rg now runs rg-rnfallback descends node_modules and dumps minified files (token expansion): no--no-ignore-vcs, no substitution-r/-Ecollide with rg--replace/--encoding: real engine, no flag translationrtk hook clauderewritesrg→rtk grep(BSD grep) instead ofrtk rg, so path-less ripgrep searches silently return 0 matches / hang on stdin #2301 rg tortk grep(BSD grep), path-less rg returns 0 or hangs on stdin: rg tortk rg--glob,-g,--version) #1824 rg tortk grepbreaks rg flag ordering (--glob,-g): rg tortk rgrtk grepcalls system grep, breaks-g/--glob: rg flags now go tortk rgrtk grepmisparses single-file rg output when content contains colons #1613 not grep-compatible, misparses single-file colons: real grep engine,-H --nullparse keptOpen PRs superseded (they patched the substitution; this removes it)
#2183 (respect the invoked tool), #2460 (split the rewrite rule), and #2505 (route rg to
rtk rg) are implemented in full here. #2138 (translate BRE), #2348 (PCRE-convert for-E), #2254 (strip-r/-R/-Ebefore rg), #2224 (strip rg-only flags on fallback), #2131 and #2607 (translate--includeto--glob), #2434 (silent fallback when rg flags lead), and #1899 (normalize rewrite flags) are all unnecessary once we run the real engine.