Repository navigation
Description
rtk hook claude silently skips rewriting any command containing a pipe (|)
Environment
- rtk 0.45.0 (
rtk --version) - Hook config (
~/.claude/settings.json):"hooks": { "PreToolUse": [ { "matcher": "Bash", "hooks": [ { "type": "command", "command": "rtk hook claude" } ] } ] }
Repro
echo '{"tool_name":"Bash","tool_input":{"command":"git status | head -20"}}' | rtk hook claudeExpected: a hookSpecificOutput.updatedInput.command rewrite to rtk git status | head -20 (or similar), matching how the same command rewrites without the pipe.
Actual: empty stdout, exit code 0, no stderr. No hookSpecificOutput at all, so Claude Code applies no rewrite and runs the original unmodified command.
Minimal matrix
Same three command families, same four connector shapes. Bare, &&, and ; all rewrite correctly. Every case with a | anywhere in the command is a silent no-op — same failure, no error surfaced anywhere.
| Command family | bare | && |
; |
| |
|---|---|---|---|---|
git status |
rewritten | rewritten | rewritten | silent no-op |
grep -n foo file.txt |
rewritten | rewritten | rewritten | silent no-op |
find . -name '*.rs' |
rewritten | rewritten | rewritten | silent no-op |
Exact commands tested for the | column:
echo '{"tool_name":"Bash","tool_input":{"command":"git status | head -20"}}' | rtk hook claude
echo '{"tool_name":"Bash","tool_input":{"command":"grep -n foo file.txt | head -5"}}' | rtk hook claude
echo '{"tool_name":"Bash","tool_input":{"command":"find . -name \"*.rs\" | wc -l"}}' | rtk hook claudeAll three: empty output, exit 0.
For contrast, the &&/; cases that work correctly:
echo '{"tool_name":"Bash","tool_input":{"command":"cd /tmp && git status"}}' | rtk hook claude
# -> {"hookSpecificOutput":{...,"updatedInput":{"command":"cd /tmp && rtk git status"}}}
echo '{"tool_name":"Bash","tool_input":{"command":"git status; echo done"}}' | rtk hook claude
# -> {"hookSpecificOutput":{...,"updatedInput":{"command":"rtk git status; echo done"}}}Why this matters
Piping into head/tail/wc -l/grep etc. is one of the most common shapes for an agent-issued shell command. Since the hook fails silently (no error, exit 0) rather than falling back or warning, there's no signal that compression didn't happen — the token savings are just quietly lost. Ran rtk discover against 30 days / 49 sessions of real usage and it found 598 raw git status invocations alone that never got routed through RTK despite RTK having a handler for it (~83.2K tokens left on the table just for that one command), which is consistent with this being the cause for a large share of the misses.
Activity
pszymkowiak commentedon Aug 15, 2026
🔍 Automated Triage
bughighSummary
rtk hook claude silently emits no hookSpecificOutput for any command containing a pipe, losing rewrite/compression for a very common command shape; add | handling alongside existing && and ; connector support.
📁 Relevant files
src/hook/claude.rssrc/hook/rewrite.rsTriaged automatically by wshm · This is an automated analysis, not a human review.
sigma67 commentedon Aug 20, 2026
Same issue here. Unfortunately many interactions with Gitlab require glab api calls and rtk almost never intercepts these. For example:
funkadelic commentedon Aug 28, 2026
There is a second cluster of open issues asking for the exact opposite of this one, and I think the two are the same knob at two settings.
#1560, #2962, #3558, #2487 and #1282 all report the producing side of a pipe being rewritten and corrupting the downstream consumer. On 0.46.0 those repros no longer fire:
So this issue looks like the cost of fixing those, since the hook now bails on every pipe rather than only the unsafe ones. Flipping it back would just reopen the other cluster.
One caution on the asks in the Cluster A issues. #3722 asks for
| wc -lto be rewritten too, and that is the unsafe direction:A pipe carries two different intentions and they need separating.
| head -20is display trimming, and the output is still read by the model.| wc -l,| jq,| xargs,| grep -cconsume the output as data, and reformatting changes the answer.Suggested discriminator
Rewrite the producer only when both of these hold.
Rule 1, the consumer is only trimming for display. Every stage after the first is a bare
head,tail,cat,lessormore. Any other consumer, pass through as today.Rule 2, the producer is not emitting machine-readable output. No
--format,--pretty,--porcelain,--json,--name-only,--name-status,--raw,-zor--nullon the producing command.Rule 2 is not implied by rule 1.
git log --format=%H | head -1satisfies rule 1, since its consumer is a barehead, but thatheadis selecting a commit SHA rather than shortening output for a human to read. Rewriting the producer would feed rtk's reformatted output to whatever consumes the SHA, which is #2487 again.Size of the safe subset
Across 685 local Claude Code sessions, 38917 Bash commands:
Top unsafe tails were
grep(2805),python3(488),sed(429),cut(280),jq(214),wc(195). Those stay passed through under this rule, so the recovered share is the 27%, not the 59%.Reference implementation
I tested the criterion in a wrapper around the hook rather than in rtk itself, so I have not read your rewrite engine and this may not map cleanly onto it. The wrapper splits on top-level
|withshlex, applies both rules, delegates the first segment tortk hook claude, reattaches the tail, and falls through to plain delegation on any error. Adds about 20ms per call.Test matrix it passes, if it is useful as a starting set:
git log --oneline -5 | head -3rtk git log --oneline -5 | head -3ls -la | headrtk ls -la | headcd /tmp && git log -5 | tail -2cd /tmp && rtk git log -5 | tail -2grep -rn foo . | head -20 | tail -5rtk grep -rn foo . | head -20 | tail -5ls -la | wc -lgit log --format=%H | xargs echogh pr list | jq '.[].number'git log --format=%H | head -1--format)git diff --name-only | head -20--name-only)SHA=$(git log --oneline | head -1)grep -rn 'a|b' .rtk grep -rn 'a|b' .(quoted pipe is not a pipeline)otzpt commentedon Aug 29, 2026
Traced this to one specific function, based on reading the current
mastersource (not a built/tested patch — just pinpointing where the behavior comes from):decide_from_verdict(src/hooks/hook_cmd.rs) maps "no rewrite available" straight toHookDecision::Defer, andDeferis what produces the empty-stdout/exit-0 silent no-op. The question is why "no rewrite available" fires for these pipes at all.That traces down through
get_rewritten→rewrite_command→rewrite_compound→rewrite_pipeline_final_stage(src/discover/registry.rs:1007):This only ever tries to rewrite the last stage (that's the existing, correctly-tested behavior —
cargo test | grep FAILED→cargo test | rtk grep FAILED, sincegrephas a handler). When the last stage ishead,tail, or similar — every repro command in this issue — there's no handler for it,rewrite_segment_innerreturnsNone, and the whole function gives up withNone. There's no fallback that tries the producer (first) stage instead. ThatNoneis what turns into the silentDefer.This lines up with @funkadelic's discriminator above. A minimal fix is a fallback branch inside
rewrite_pipeline_final_stage: when the final-stage rewrite attempt fails, and the final stage is a barehead/tail/cat/less/more(Rule 1), and the producer segment's own flags don't look like machine-readable output (Rule 2), attemptrewrite_segment_inneron the producer segment instead and prefix it onto the untouched final stage.analyze_pipelinealready locates the final stage boundary, so the producer segment is already sliced out — the fallback doesn't need new parsing.One correctness note against the three repros in the issue body specifically:
find . -name '*.rs' | wc -lshould stay unchanged even after this fix —wcisn't in the safe trimmer set (per @funkadelic's ownls -la | wc -lcounter-example above), so only the| head -Ncases (git status | head -20,grep -n foo file.txt | head -5) actually flip from silent no-op to a real rewrite. That's still correct, just worth calling out so the fix isn't graded against the wrong expectation.Haven't built/tested this — happy to open a PR with it if useful, just wanted to get the localized root cause down first since the thread so far has been discussing this as more of an open design question than a five-line bug.
KuSh commentedon Sep 13, 2026
Closing as a duplicate of #3722, which tracks the same problem with the more complete discussion. Please follow up there; if your case differs in a way that issue does not cover, comment here and it can be reopened.