Skip to content

hook claude: silent no-op on any command containing a pipe #3578

Closed as duplicate

Description

@otzpt

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 claude

Expected: 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 claude

All 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

  1. pszymkowiak commented on Aug 15, 2026

    @pszymkowiak
    Collaborator

    wshm · Automated triage by AI

    🔍 Automated Triage

    🐛 Category bug
    🟠 Priority high
    🎯 Confidence 93%

    Summary

    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.

    This looks like a simple fix that could be auto-resolved. Repo maintainers can use /wshm fix to attempt it.

    📁 Relevant files
    • src/hook/claude.rs
    • src/hook/rewrite.rs

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

  2. sigma67 commented on Aug 20, 2026

    @sigma67

    Same issue here. Unfortunately many interactions with Gitlab require glab api calls and rtk almost never intercepts these. For example:

    • glab api "user" | grep x → rewritten to rtk glab api "user" | rtk grep x ✓
    • glab api "user" | cat / | head / | wc -l / | jq . / | python3 -m json.tool → no output at all, pipeline runs completely raw
  3. funkadelic commented on Aug 28, 2026

    @funkadelic

    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:

    grep -n match sixty.txt | wc -l      -> NO REWRITE
    git status --porcelain | wc -l       -> NO REWRITE
    git diff main..HEAD | grep -c "^+"   -> git diff main..HEAD | rtk grep -c "^+"   (producer left alone)
    

    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 -l to be rewritten too, and that is the unsafe direction:

    $ mkdir -p /tmp/d && touch /tmp/d/a /tmp/d/b /tmp/d/c
    $ ls -la /tmp/d | wc -l        # total line, ., .., and 3 files
    6
    $ rtk ls -la /tmp/d | wc -l
    3
    

    A pipe carries two different intentions and they need separating. | head -20 is display trimming, and the output is still read by the model. | wc -l, | jq, | xargs, | grep -c consume 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, less or more. 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, -z or --null on the producing command.

    Rule 2 is not implied by rule 1. git log --format=%H | head -1 satisfies rule 1, since its consumer is a bare head, but that head is 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:

    with a pipe:                          23116  (59%)
      every stage after the first is a
      bare head/tail/cat (rule 1 holds):  10357  (27% of all commands)
      consumer does real work:            12759
    

    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 | with shlex, applies both rules, delegates the first segment to rtk 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:

    command expected
    git log --oneline -5 | head -3 rtk git log --oneline -5 | head -3
    ls -la | head rtk ls -la | head
    cd /tmp && git log -5 | tail -2 cd /tmp && rtk git log -5 | tail -2
    grep -rn foo . | head -20 | tail -5 rtk grep -rn foo . | head -20 | tail -5
    ls -la | wc -l unchanged
    git log --format=%H | xargs echo unchanged
    gh pr list | jq '.[].number' unchanged
    git log --format=%H | head -1 unchanged (rule 2: --format)
    git diff --name-only | head -20 unchanged (rule 2: --name-only)
    SHA=$(git log --oneline | head -1) unchanged
    grep -rn 'a|b' . rtk grep -rn 'a|b' . (quoted pipe is not a pipeline)
  4. otzpt commented on Aug 29, 2026

    @otzpt
    Author

    Traced this to one specific function, based on reading the current master source (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 to HookDecision::Defer, and Defer is 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):

    fn rewrite_pipeline_final_stage(
        cmd: &str,
        segment_start: usize,
        analysis: PipelineAnalysis,
        excluded: &[ExcludePattern],
        transparent_prefixes: &[String],
    ) -> Option<String> {
        let final_stage_start = analysis.final_stage_start?;
        let final_stage = cmd[final_stage_start..analysis.end_offset].trim();
    
        rewrite_segment_inner(
            final_stage,
            excluded,
            transparent_prefixes,
            RewriteContext::PipelineFinal,
            0,
        )
        .filter(|rewritten| rewritten != final_stage)
        .map(|rewritten| { /* prefix producer + rewritten final stage */ })
    }

    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, since grep has a handler). When the last stage is head, tail, or similar — every repro command in this issue — there's no handler for it, rewrite_segment_inner returns None, and the whole function gives up with None. There's no fallback that tries the producer (first) stage instead. That None is what turns into the silent Defer.

    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 bare head/tail/cat/less/more (Rule 1), and the producer segment's own flags don't look like machine-readable output (Rule 2), attempt rewrite_segment_inner on the producer segment instead and prefix it onto the untouched final stage. analyze_pipeline already 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 -l should stay unchanged even after this fix — wc isn't in the safe trimmer set (per @funkadelic's own ls -la | wc -l counter-example above), so only the | head -N cases (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.

  5. KuSh commented on Sep 13, 2026

    @KuSh
    Collaborator

    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.

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:clibugpriority:high

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions