Claude Code hook misses rtk-eligible commands inside compound invocations (pipes, && chains)
Summary
The Claude Code hook (rtk hook claude) rewrites simple Bash commands to their rtk equivalents, but commands embedded in compound invocations — && chains, pipelines, redirects — pass through unrewritten even when rtk already handles them. Over a real 30-day window this accounted for ~43.8K tokens of missed savings in one project.
Environment
- rtk 0.42.4 (Homebrew, macOS / Darwin 25.5.0)
- Claude Code with
rtk hook claudewired as aPreToolUseBash hook
Evidence (rtk discover output)
Scanned: 10 sessions (last 30 days), 941 Bash commands
Already using RTK: 489 commands (52.0%)
MISSED SAVINGS -- Commands RTK already handles
Command Count RTK Equivalent Est. Savings
grep -n 90 rtk grep ~19.6K tokens
head -60 32 rtk read ~6.9K tokens
gh issue 27 rtk gh ~6.3K tokens
git status 40 rtk git ~4.5K tokens
ls bin/ 30 rtk ls ~3.6K tokens
wc -c 15 rtk wc ~1.0K tokens
curl -s 4 rtk curl ~876 tokens
diff -q 3 rtk diff ~536 tokens
find ... 7 rtk find ~357 tokens
Total: 248 commands -> ~43.8K tokens saveable
Spot-checking the session transcripts, nearly all of these misses were inside compound commands, e.g.:
grep -n "pattern" file.py | head -20
git status --short && git log --oneline -5
some-tool --json 2>&1 | head -60
The standalone forms of the same commands are rewritten correctly.
Why this is plausibly intentional (and a suggestion anyway)
Rewriting inside a pipeline can change semantics (e.g. a downstream parser expecting exact upstream output), so skipping compound commands is a defensible safety default. Two ideas that might capture most of the value without that risk:
- Rewrite only the safe positions: the first command of an
&&/;chain, or the head of a pipeline when the rtk equivalent is output-compatible for piping. - Opt-in flag (e.g.
rtk hook claude --compound) for users willing to accept the trade-off, keeping the conservative default.
Even handling just the leading command of && chains would have covered a large share of the 248 misses above.
Happy to provide fuller rtk discover --history data if useful. Thanks for the tool — the 52% that is covered is already saving real money.
wshm · Automated triage by AI
🔍 Automated Triage
| ✨ Category | feature |
| 🟡 Priority | medium |
| 🎯 Confidence | 78% |
Summary
Hook compound-command rewriting is unimplemented (possibly intentional); author requests opt-in or position-aware rewriting to recover ~43.8K tokens/month of missed savings
Triaged automatically by wshm · This is an automated analysis, not a human review.
This looks resolved on current develop. The Claude Code hook path (rtk hook claude → rewrite_command in src/hooks/hook_cmd.rs) now rewrites compound invocations — rewrite_compound / split_command_chain in src/discover/registry.rs rewrite each &&/||/; segment independently and the left-hand side of a pipe.
Verified with rtk hook check (same decision path as the live hook):
$ rtk hook check "git status --short && git log --oneline -5"
rtk git status --short && rtk git log --oneline -5 # exit 0
$ rtk hook check 'grep -n "pattern" file.py | head -20'
rtk grep -n "pattern" file.py | head -20 # left rewritten, exit 0
$ rtk hook check "git status 2>&1"
rtk git status 2>&1 # redirect, exit 0
$ rtk hook check "cd /tmp && grep -n foo bar.txt"
cd /tmp && rtk grep -n foo bar.txt # eligible segment after && rewritten
All the examples from the report now rewrite. Pipe targets (RHS) intentionally stay raw — exactly the safety behavior the report itself called defensible — so ... | head not rewriting on the right is by design.
This was filed against 0.42.4; compound support landed after that. Probably safe to close (or confirm on your end).
This compound-invocation gap shows why RTK command counters can miss end-to-end agent behavior. Tura’s DeepSWE result instead measures completed tasks and verifier outcomes, suggesting fewer agent rounds cut about 80% of token overhead. Would you compare the methodologies? https://turaai.net/docs#benchmark-current-test-set-record
Corroborating data point from a much larger sample — same root cause, same fix suggestion (leading-command-of-chain rewrite would capture most of the value).
Environment
- rtk 0.43.0 (Homebrew, macOS / Darwin 25.5.0)
Evidence (rtk discover)
Scanned: 1449 sessions (last 30 days), 16686 Bash commands
Already using RTK: 997 commands (6.0%)
MISSED SAVINGS -- Commands RTK already handles
Command Count RTK Equivalent Est. Savings
cat "..." 893 rtk read ~454.9K tokens
grep -n 1270 rtk grep ~368.5K tokens
ls -la 1985 rtk ls ~278.1K tokens
find "..." 1224 rtk find ~250.6K tokens
curl -s 91 rtk curl ~19.9K tokens
gh run 44 rtk gh ~17.5K tokens
wc -l 180 rtk wc ~16.5K tokens
npm run 64 rtk npm ~16.3K tokens
diff -q 15 rtk diff ~10.1K tokens
pip3 install 51 rtk pip ~8.3K tokens
Total: 6040 commands -> ~1.5M tokens saveable
Manually verified the hook itself works fine on standalone commands (echo '{"tool_name":"Bash","tool_input":{"command":"cat x"}}' | rtk hook claude correctly returns updatedInput: rtk read x) — the gap is entirely commands embedded in &&/pipe/heredoc chains, matching what's described above. At this volume (6% adoption vs. the 52% in the original report), even just rewriting the leading command of &&/; chains would likely be the single highest-leverage fix available for real-world usage.
Happy to share the full rtk discover history/session breakdown if useful for prioritizing.
Still reproduces on 0.48.0 (checked 0.48.0's release notes first — no related fix landed). A control-flow variant worth adding alongside the &&/pipe examples above: the rtk-eligible command doesn't need a chain operator next to it at all — it just needs to not be the very first token.
$ echo '{"tool_name":"Bash","tool_input":{"command":"git diff"}}' | rtk hook claude
{"hookSpecificOutput":{"hookEventName":"PreToolUse","permissionDecisionReason":"RTK auto-rewrite","updatedInput":{"command":"rtk git diff"}}}
$ echo '{"tool_name":"Bash","tool_input":{"command":"D=$PWD; while [ \"$D\" != / ]; do D=$(dirname \"$D\"); done; git status"}}' | rtk hook claude
# (no output)
Same root cause as above, just a different shape: a variable assignment + loop precedes the eligible command, rather than a &&/pipe. Confirms the "rewrite only the safe positions" framing in the original report generalizes beyond flat chains — the leading-command check would need to skip past assignment/control-flow prefixes too, not just &&/; splits.
Also want to +1 the visibility suggestion: the hook produces zero output (not even a no-op marker) when it declines, which made this take a manual rtk hook claude stdin test to even confirm — a discover-style summary couldn't tell me why those commands were missed, only that they were.
Independent third-party confirmation, also on 0.48.0 (Homebrew install, macOS/Darwin 25.6.0, Claude Code PreToolUse hook):
Both reproduction cases from this thread reproduce exactly as described — the hook emits zero output (not an error, not a no-op marker, nothing) for:
- the assignment/loop-prefix case (
D=$PWD; while [ "$D" != / ]; do D=$(dirname "$D"); done; git status) - pipe/chain compounds in general (e.g.
grep -n pattern file.py | head -20)
while the equivalent standalone command (git status, git diff, etc.) rewrites correctly via updatedInput.
+1 on the visibility ask specifically — silent decline makes this genuinely hard to notice without manually piping test JSON into rtk hook claude by hand, which is how I confirmed it here. A distinguishable exit/marker for "declined to rewrite" vs "nothing to rewrite" would help a lot, independent of whether the compound-command rewrite itself gets fixed.
Still reproduces on 1.1.0 (Homebrew, macOS 26, Claude Code and Codex 0.164 hooks). Some numbers and a workaround that may help scope a fix.
Over one day of agent sessions the hook saw 795 unique Bash commands. rtk hook claude rewrote 283 of them. Another 53 contain a part RTK already handles, but sit inside a ; chain, a newline-separated script, a for/while body, or an if block, so the whole command passes through. Typical shapes:
for f in a b c; do git log -1 --stat $f; done
cd app; git status; ls -la
if test -d x; then git diff --stat; fi
As a workaround we put a small wrapper in front of the hook. It splits the command at top-level &&, ||, ; and newlines (never inside quotes, $(…), (…), {…} or backticks) and runs rtk hook check on each part. The per-part rewrites are already right, so the splitting is the only missing piece. It keeps a part raw when:
- it is a loop or
ifheader (for,while,if,case) or a closer (done,fi,esac), and only thedo/then/elseprefix is kept on body parts; - it has a file redirect or backticks (
2>&1and>/dev/nullare fine); - the command has a heredoc or a
casepattern, so it can't be split safely; - a loop's output is piped or redirected (
done | head,done > out), because then the output feeds another command, not the agent.
Replaying all 795 commands through it lost none of the existing rewrites, and every result still passed bash -n.
One thing to watch if this goes into the hook itself: with more rewrites, #4644 gets wider. Host ask/prompt rules see the rewritten command, so more compound commands would stop matching them.
+1, still reproduces on 1.1.0 (Homebrew, macOS, Claude Code). A data point from heavy daily use:
rtk discover over 30 days: 612 sessions, 27,071 Bash commands, 19.4% already going through RTK. Missed savings on commands RTK already handles: 2,683 commands, about 1.6M tokens. One command dominates: grep -n (1,397 commands, ~1.1M tokens, 70% of the total), then head -c (442, ~291K), ls -la (368, ~148K), gh api (61), find . (96), wc -c (111).
I have not yet confirmed how many of these sit inside compound commands, but the shapes in this issue and in the comment above match what I see (grep -n ... | head, a && b, loops). The head-of-&&-chain and head-of-pipeline idea from the description would likely cover most of the grep -n volume. Meanwhile I'm working around it on the agent side (one command per call, dedicated search/read tools), and will re-run discover in a week to measure the effect.
The commands from this report are rewritten now: git status --short && git log --oneline -5 becomes rtk git status --short && rtk git log --oneline -5, and grep -n "pattern" file.py | head -20 becomes rtk grep -n "pattern" file.py | head -20. The && form worked early on; the pipe case was fixed by #3343, shipped in v0.49.0. A ; chain such as cd app; git status; ls -la is rewritten segment by segment too. I checked these on v1.2.0 and on current develop. Commands inside for/while bodies, if blocks and $(...) are still passed through untouched; that is tracked in #5110, so this issue is closed for the cases it reports. If one of the commands above still passes through unrewritten on v1.2.0 or later, please comment with the exact command line and the output of rtk hook check and it can be reopened.