You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
hook: commands inside $(...) and loop bodies are not rewritten #5110
rtk hook claude rewrites commands at the top level of a command line (including after |, &&, ||, ;, inside ( ... ) and with VAR=1 prefixes), but leaves commands untouched when they sit inside command substitution or a loop body. Their output then reaches the agent unfiltered.
Reproduction (rtk 1.0.1, macOS)
forcin'rg -n foo .''(cd /tmp; rg -n foo)''x=$(rg -n foo)''for f in a b; do rg -n x "$f"; done';do
jq -nc --arg c "$c"'{tool_name:"Bash",tool_input:{command:$c}}'| rtk hook claude
echodone
Input
Result
rg -n foo .
rtk rg -n foo .
(cd /tmp; rg -n foo)
(cd /tmp; rtk rg -n foo)
x=$(rg -n foo)
no rewrite
for f in a b; do rg -n x "$f"; done
no rewrite
Expected
Commands inside $(...) (and probably backticks) and inside for / while / until loop bodies are rewritten the same way as top-level commands, e.g.
x=$(rtk rg -n foo)
for f in a b; do rtk rg -n x "$f"; done
Impact
Agents use loops and $(...) a lot. rtk discover on my history lists several hundred rg -n, git show and similar calls as "missed savings" for commands rtk already supports; testing the hook suggests a large part of them come from these two forms.
Plan: extend the hook's command splitting so it also descends into for / while / until loop bodies (the do ... done part, not the loop header), using the same rewrite as for commands after ; or &&.
One question before I start: should commands inside $(...) and backticks be rewritten too? Their output is usually captured by the script (x=$(rg -n foo), for f in $(git ls-files)), so filtering it would change what the variable holds and could break the rest of the command. My suggestion would be to leave substitutions alone, or only rewrite them when the filter's output is guaranteed to match the raw output, but I'll follow whatever you prefer.
Hey, cool, thanks for taking care of this! Good idea and good thinking on the $(...) part, makes total sense to leave substitutions alone. Go for it, hopefully it saves us all a few more tokens 😉
Activity
rtk-wshm-sync-bot commentedon Nov 13, 2026
🔍 Automated Triage
bughighSummary
Hook rewriter misses commands inside $(...)/backticks and for/while/until loop bodies, letting unrewritten tool output reach the agent unfiltered.
📁 Relevant files
src/hook/command_rewrite.rssrc/hook/claude.rsSuggested Actions
go -C DIR <subcommand>(global chdir flag before the subcommand) #4970 and Filter the command behind an exec boundary in one place #5051 — same family of command-shape parsing gaps in the rewrite boundaryTriaged automatically by wshm · This is an automated analysis, not a human review.
seragAhmed commentedon Nov 13, 2026
I'd like to work on this.
Plan: extend the hook's command splitting so it also descends into
for/while/untilloop bodies (thedo ... donepart, not the loop header), using the same rewrite as for commands after;or&&.One question before I start: should commands inside
$(...)and backticks be rewritten too? Their output is usually captured by the script (x=$(rg -n foo),for f in $(git ls-files)), so filtering it would change what the variable holds and could break the rest of the command. My suggestion would be to leave substitutions alone, or only rewrite them when the filter's output is guaranteed to match the raw output, but I'll follow whatever you prefer.HerrmannHinz commentedon Nov 14, 2026
Hey, cool, thanks for taking care of this! Good idea and good thinking on the
$(...)part, makes total sense to leave substitutions alone. Go for it, hopefully it saves us all a few more tokens 😉