Skip to content

hook: commands inside $(...) and loop bodies are not rewritten #5110

Open

Description

@HerrmannHinz

Summary

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)

for c in '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
  echo
done
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.

Related

Activity

  1. added
    bug
    help wanted
    on Nov 13, 2026
  2. rtk-wshm-sync-bot commented on Nov 13, 2026

    @rtk-wshm-sync-bot

    wshm · Automated triage by AI

    🔍 Automated Triage

    🐛 Category bug
    🟠 Priority high
    🎯 Confidence 90%

    Summary

    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.rs
    • src/hook/claude.rs

    Suggested Actions

    1. Compare with Hook does not rewrite 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 boundary
    2. Extend the command-splitting logic to recurse into $(...) / backtick substitutions and loop/while/until bodies
    3. Check Filter the command behind an exec boundary in one place #5051 which proposes centralizing exec-boundary filtering — may share a fix point
    4. Request details on how deeply nested substitutions (e.g. $(... $(...) ...)) should be handled

    Triaged automatically by wshm · This is an automated analysis, not a human review.

  3. seragAhmed commented on Nov 13, 2026

    @seragAhmed

    I'd like to work on this.

    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.

  4. HerrmannHinz commented on Nov 14, 2026

    @HerrmannHinz
    Author

    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 😉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:clibughelp wantedpriority:high

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions