Skip to content

refacto(search): run the invoked engine instead of substituting rg for grep - #2641

Merged
aeppling merged 5 commits into
developfrom
fix/grep-faithful-engine
Jun 26, 2026
Merged

aeppling merged 5 commits into
developfrom
fix/grep-faithful-engine

Conversation

@aeppling

@aeppling aeppling commented Jun 26, 2026 •

Copy link
Copy Markdown
Contributor

fix(grep): run the invoked engine instead of substituting rg for grep

Goal

Make rtk grep and rtk rg a 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 grep ran ripgrep internally regardless of what the agent typed, then forced flags to make rg act like grep. Every band-aid leaked:

Symptom Root cause
grep 'al|ga' "saved 137K" but real grep matches 0 rg | is alternation, grep BRE | is literal, so savings were measured against rg's inflated output
rg ... rewritten to rtk grep ran BSD grep: wrong dialect, recursion, 0-match / stdin hang the rewrite rule folded rg into rtk grep
one grep -rn . recorded 302M tokens "saved" (9.6 min, 1.2 GB) forced --no-ignore-vcs made rg read node_modules and minified files
the DB stamps every entry grep -rn '...', so you cannot tell grep from rg tracking synthesized the command string instead of recording it

What changed

  • Run the invoked engine, never substitute. A new Engine { Grep, Rg }; engine_capture runs the exact binary and flags, appending only parse aids (-n -H -I --null for grep, -n --with-filename --null for rg). They shape the output for grouping; they do not change which lines match.
  • Removed every substitution band-aid: the rg-for-grep fallback, the \| to | translation, the forced --no-ignore-vcs, and the rg-only-flag stripping (strip_rg_only, drop_recursion_for_rg, RG_ONLY_LONG, strip_r all deleted).
  • New rtk rg command: runs ripgrep natively (keeping its .gitignore and binary skipping and its dialect), reusing the same output filter. grep and rg emit the identical file:line:content shape, so the compression is shared, not duplicated.
  • Split the rewrite rule (discover/rules.rs): grep to rtk grep, rg to rtk rg. The engine is preserved end to end.
  • Honest tracking: record the agent's real command (rg -i foo src/, grep -rn foo .) with its actual flags, instead of the synthesized grep -rn '...'.
  • Renamed grep_cmd.rs to search.rs: it hosts both engines now, and automod auto-declares it, so the rename is a no-op for module wiring.
  • Updated 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).
  • The DB records rg fn src/core and grep -rn fn src/core (real engine and flags).

Benchmark (scripts/benchmark.sh):

  • grep fn 70611 to 4247 (93%), grep -c 95%, grep -l 40 99%.
  • rg fn 70611 to 4247 (93%); rg -l and rg -c pass through at 0% (already minimal).
  • rtk rg (0.37s) is faster than rtk grep (0.56s) since rg stays lean.

Tests

Engine faithfulness now has dedicated coverage, each test tagged with the issue it guards:

Removed two stale unit tests that documented deleted behavior (BRE-to-rg translation, forced --no-ignore-vcs). Adapted develop's grep_no_match_emits_empty_not_a_message to 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) and build --release all green.

Scope kept small (no new edge cases)

-H --null is 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

Open 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/-E before rg), #2224 (strip rg-only flags on fallback), #2131 and #2607 (translate --include to --glob), #2434 (silent fallback when rg flags lead), and #1899 (normalize rewrite flags) are all unnecessary once we run the real engine.

- 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
aeppling force-pushed the fix/grep-faithful-engine branch from 190b907 to eafadce Compare June 26, 2026 12:20
@aeppling
aeppling marked this pull request as ready for review June 26, 2026 12:36
@aeppling aeppling changed the title fix(grep): run the invoked engine instead of substituting rg for grep refacto(search): run the invoked engine instead of substituting rg for grep Jun 26, 2026
aeppling added 3 commits June 26, 2026 17:25
- 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
aeppling force-pushed the fix/grep-faithful-engine branch from 8976db5 to 059131c Compare June 26, 2026 15:49
- 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)
@aeppling
aeppling merged commit d3553fb into develop Jun 26, 2026
17 checks passed
@github-actions github-actions Bot mentioned this pull request Jun 26, 2026
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.
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.
@aeppling
aeppling deleted the fix/grep-faithful-engine branch June 30, 2026 20:07
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.
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.
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.

3 participants