Skip to content

fix(rewrite): apply custom TOML filters during command rewrite - #2226

Closed
mikeangstadt wants to merge 3 commits into
rtk-ai:developfrom
mikeangstadt:fix/toml-custom-filters-rewrite
Closed

mikeangstadt wants to merge 3 commits into
rtk-ai:developfrom
mikeangstadt:fix/toml-custom-filters-rewrite

Conversation

@mikeangstadt

@mikeangstadt mikeangstadt commented Jun 2, 2026 •

Copy link
Copy Markdown

Summary

Custom TOML filters (from .rtk/filters.toml or ~/.config/rtk/filters.toml) were parsed and passed rtk verify, but were never consulted by the hook rewrite path. The rewrite engine (discover/registry.rs) only checked the hardcoded RULES array, returning empty for any unrecognized command. This meant user-defined TOML filters were non-functional for their primary purpose.

Root Cause

The rtk rewrite path (used by hooks) has no knowledge of TOML filters. There are two independent systems:

System Data Source Used By Knows Custom Filters?
Rewrite Rules discover/rules.rs (hardcoded) rtk rewrite, hook NO
TOML Filter Engine .rtk/filters.toml / ~/.config/rtk/filters.toml / built-in rtk verify, run_fallback YES

When a user runs a command matching a custom TOML filter (e.g. zzprobe --foo):

  1. Hook calls rtk rewrite 'zzprobe --foo' -> hardcoded rules don't match -> returns empty -> hook passes through unchanged
  2. Command runs without rtk prefix -> TOML filter is never applied during execution

Change

In rewrite_segment_inner() in discover/registry.rs: when classify_command() returns Unsupported, check the TomlFilterRegistry before giving up. If a matching custom TOML filter is found, return Some(format!("rtk {}/{}", cmd_part, redirect_suffix)) -- which causes the hook to run the command through rtk, where run_fallback() applies the TOML filter pipeline.

This is a minimal change: 10 lines added, 1 modified. Only the Unsupported branch is affected -- built-in rules, Ignored commands, and excluded commands are unchanged.

Closes #2179


Collaborated & orchestrated by ClosedLoop.AI | GitHub

When ripgrep is not on $PATH, rtk grep falls back to system grep but
was passing ripgrep-specific flags like -g/--glob, --hidden, --pcre2,
--json, etc. that grep does not understand, causing silent failures or
wrong results.

Adds a stateful filter that removes rg-only flags (and their value
arguments) from the extra_args before passing them to grep.

Closes rtk-ai#2167
rtk proxy does not invoke a shell — it spawns the command directly via
exec. When users pass compound shell syntax (&&, ||, |, ;, $(), etc.),
those tokens end up as literal arguments to the spawned process, causing
confusion or incorrect behavior (e.g. bogus files/dirs from shell tokens).

Adds detect_shell_snippet() that validates the command name and all args
for shell control operators and command substitution syntax, returning
a clear error with a suggestion to use `sh -c` or `bash -c` instead.

Closes rtk-ai#2163
Custom TOML filters (from .rtk/filters.toml or ~/.config/rtk/filters.toml)
were parsed and passed rtk verify, but were never consulted by the hook
rewrite path. The rewrite engine (discover/registry.rs) only checked the
hardcoded RULES array, returning empty for any unrecognized command.

Now, when classify_command returns Unsupported, we check the TomlFilterRegistry
for a matching custom filter. If found, the command is rewritten to run
through "rtk <original>" so that fallback execution applies the filter pipeline.

Closes rtk-ai#2179
@aeppling aeppling self-assigned this Jun 22, 2026
@aeppling

aeppling commented Jun 22, 2026 •

Copy link
Copy Markdown
Contributor

Hey @mikeangstadt

Thanks for addressing this, will enhance all gains for RTK toml users

There are some blockers, here are some problems i've identified


The PR does not compile

Check ci


Exclude-list bypass (regression)

The Supported arm honors the user's exclude_commands (registry.rs:813); the new Unsupported+TOML arm does not. I verified is_excluded has exactly one call site and there's no upstream gate, then reproduced it with a unit test on the actual PR code:

test repro_control_gcc_rewritten_by_toml ... ok # feature works: gcc → "rtk gcc -o x x.c"
test repro_excluded_command_bypasses_exclude_list ... FAILED
assertion failed: got Some("rtk gcc -o x x.c"), right: None

With exclude_commands = ["gcc"], the PR still force-wraps the command. A user who excluded a command gets it rewritten anyway whenever any of the 58 built-in filters match. Fix: mirror the is_excluded check (with ENV_PREFIX strip) in the new arm.


Needs tests for the new behavior

This is a behavioral change and we need solid tests to ensure no futures regressions.


Notes

Fix is tested and work well, please drop unrelated grep changes, this belong to another PR. I thing you embedded this maybe by wrong base on one of your others PR, please test the code and the fix for the next review so there is no back and forth for compiling issues for example.

Thanks for this contribution , awaiting for your changes !

pull Bot pushed a commit to Mu-L/rtk that referenced this pull request Jul 3, 2026
…cation

Wire TOML-covered commands into the transparent hook rewrite path, and make
TOML truncation reversible via the shared tee hints.

Part 1 — hook wiring (rtk-ai#2179):
The hook's rewrite decision only consulted the static RULES table, so a command
covered solely by a TOML filter (jj, jq, just, ssh, ty, nx, turbo, …) was passed
through unfiltered by the hook even though `rtk <cmd>` filtered it directly.
`rewrite_segment_inner` now consults the TOML registry on a RULES miss
(Unsupported) and rewrites to `rtk <cmd>`, which re-enters via run_fallback →
the TOML tier. Guards: honors RTK_NO_TOML, respects the exclude list, never
rewrites Ignored (denylisted) commands, and a collision guard
(is_rtk_reserved_command) prevents rewriting to a name Clap would route to its
own subcommand. RTK_META_COMMANDS moved to core::constants as the shared source.

Part 2 — reversibility:
TOML truncation on success previously dropped lines with a bare "... omitted"
marker and no recovery pointer. apply_filter_with_info now reports a Lossiness
(Tail / Whole / None); run_fallback emits the line-offset tail hint
(`[see remaining: tail -n +N …]`) for a contiguous tail-drop, the full-output
hint otherwise, and — when tee is unavailable — shows the full raw rather than an
unrecoverable marker. Fixes the symmetric <500B failure-path gap and an
inaccurate MIN_TEE_SIZE comment in curl_cmd.

All hosts (claude/gemini/copilot/cursor) and `rtk rewrite` are covered via the
shared rewrite_command. No hook JSON / integrity-hash changes. The mirrored RULES
rows are kept (they feed discover/session/gain metadata). Builds on the approach
of rtk-ai#2226.

Note: the TOML registry load now lands on the Unsupported hook hot path
(~3-8ms native, one-time per process); the Supported path is unaffected. A
match-only RegexSet to cut that cost is a documented follow-up.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@KuSh

KuSh commented Sep 19, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the clear write up of the two-systems problem. Issue #2179 was fixed on develop by #2748 (merged 2026-07-03): the rewrite path now consults the TOML filter registry in the Unsupported branch, with extra guards for a disabled TOML engine and for rtk's own reserved commands, and a match only RegexSet so the hook stays fast. The grep hunk on this branch targets src/cmds/system/grep_cmd.rs, which #2641 (merged 2026-06-26) removed when rtk stopped substituting rg for grep, so it cannot be carried over. The proxy case is still open: rtk proxy spawns directly, so &&, |, ; and $() reach the child as literal arguments, and an issue or a small PR for that would be welcome, ideally one that cannot reject a literal ${ or backtick inside a quoted argument. Please comment or reopen if I missed a part of this.

@KuSh KuSh closed this Sep 19, 2026
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.

Custom TOML filters (global or trusted project-local) are never applied by the hook

3 participants