Skip to content

Hook rewrite (rtk hook claude) skips commands containing a pipe (|) #3722

Closed

Description

@javython999

Version: rtk 0.46.0 (Windows x86_64-pc-windows-msvc)

Summary:
rtk hook claude (the PreToolUse Bash hook used for Claude Code auto-rewrite) silently skips rewriting whenever the command contains a pipe (|), even when a rewritable subcommand (e.g. git status, find) appears before the pipe. No rewrite happens and no error is emitted — the hook just passes the original command through unchanged.

Repro:

echo '{"tool_name":"Bash","tool_input":{"command":"git status"}}' | rtk hook claude
# -> rewrites to: rtk git status  (correct)

echo '{"tool_name":"Bash","tool_input":{"command":"git status | cat"}}' | rtk hook claude
# -> no output / no rewrite (bug)

echo '{"tool_name":"Bash","tool_input":{"command":"git status --porcelain | head -20"}}' | rtk hook claude
# -> no output / no rewrite (bug)

echo '{"tool_name":"Bash","tool_input":{"command":"find . -name \"*.txt\" | head -20"}}' | rtk hook claude
# -> no output / no rewrite (bug)

Chained commands with && work fine and rewrite the trailing subcommand correctly:

echo '{"tool_name":"Bash","tool_input":{"command":"git rev-parse HEAD && git status"}}' | rtk hook claude
# -> rewrites to: git rev-parse HEAD && rtk git status  (correct)

Expected: Commands piped to another program (| head, | cat, | wc -l, etc.) should still have their rewritable segment(s) converted to the rtk equivalent, the same way &&-chained commands already are.

Impact: Found via rtk discover --all on real Claude Code session history — dozens of git status, find, etc. invocations went unhandled specifically because they were piped to head/wc/cat, undercutting the tool's token-savings hook for a very common shell pattern.

Activity

  1. added a commit that references this issue on Aug 27, 2026
    16ddc78
  2. jclipa-lightit commented on Sep 4, 2026

    @jclipa-lightit

    Confirming this on a different platform and an earlier version: rtk 0.45.0, macOS arm64 (Darwin 25.6.0), Claude Code. Same behavior, so this is not Windows-specific and not a 0.46.0 regression.

    One detail worth adding to the report: the hook does not always skip the whole command. When a later pipeline segment is independently rewritable, that segment gets rewritten while the leading one is silently skipped. So the failure is per-segment, not all-or-nothing:

    git show HEAD:path/File.java | grep -n foo
    # -> git show HEAD:path/File.java | rtk grep -n foo
    #    the grep segment is rewritten, git show is not
    

    Full matrix I ran against rtk hook claude:

    Command Hook result
    git status rtk git status
    git status 2>&1 rtk git status 2>&1
    git status | tail -25 not rewritten
    git diff | head -50 not rewritten
    git log --oneline -5 | cat not rewritten
    git show HEAD:File.java | grep -n foo only the grep segment rewritten

    Redirections alone (2>&1) do not block the rewrite, which narrows the trigger to the pipe specifically.

    Impact on my side, measured with rtk discover --since 7: 9217 Bash commands over 7 days, 30 of them using RTK (0.3%), around 875K tokens saveable. Top missed commands were grep -n (787), git show (680), cat -n (551), glab mr (343). The great majority of those were skipped only because they appeared inside a pipeline. rtk session reported 38% average adoption over the last 10 sessions, so the two counters disagree quite a bit, which made the problem hard to spot at first: adoption looks acceptable while the actual per-command coverage is near zero.

    The practical consequence for agent-driven use is that piping to head/tail is the single most common shell idiom an LLM produces, so this one gap accounts for most of the lost savings. Worth flagging as higher priority than the raw repro suggests.

    Possibly the same root cause as #3696, and related to #3683 (discover's stop-at-pipe truncation) since both look like the same pipeline-splitting assumption in different code paths.

  3. jclipa-lightit commented on Sep 4, 2026

    @jclipa-lightit

    Follow-up: still reproduces identically on rtk 0.47.0 (macOS arm64, installed from the homebrew-core bottle). Combined with the original report on 0.46.0/Windows and my earlier comment on 0.45.0/macOS, the bug is present across 0.45.0, 0.46.0 and 0.47.0 on both platforms, so nothing in those releases touched this path.

    Re-ran the same matrix after upgrading:

    Command 0.47.0 result
    git status rtk git status
    git status | tail -25 not rewritten
    git diff | head -50 not rewritten
    git log --oneline -5 | cat not rewritten
    git show HEAD:f.java | grep -n bar only the grep segment rewritten
    cat -n f.java rtk read -n f.java

    Unchanged from 0.45.0 in every row.

    One note for anyone hitting this before it is fixed: the | head -N and | tail -N cases have a pipe-free equivalent that keeps the rewrite working, since rtk read already exposes the flags natively.

    rtk read -m 50 file.java           # instead of  cat file.java | head -50
    rtk read --tail-lines 25 file.log  # instead of  cat file.log | tail -25
    

    That is a workaround for callers, not a fix. The hook should still handle the piped form, since an LLM writing shell commands produces | head and | tail constantly and will not reach for the native flags on its own.

  4. funkadelic commented on Sep 11, 2026

    @funkadelic

    Four open issues (this one, #3696, #3578, #3442) ask for piped commands to be rewritten. #3558 asks for the opposite, after rtk git diff HEAD~2..HEAD | grep -c '^+' returned 0 where native git returned 50.

    Refusing outright is the right answer for that latter case. It is also blunt.

    Of the three piped repro cases above, two rewrite under the rule below: git status | cat and find . -name "*.txt" | head -20. The third, git status --porcelain | head -20, is harmless in practice, but the rule declines it anyway for a reason I get to further down. The case that actually bites is | wc -l from the Expected list. On this machine ls -la /tmp | wc -l returns 2897 natively and 52 through rtk, so an agent counting lines gets a wrong number and no signal that anything happened.

    Rewrite the producer only when every segment after the first is a bare display trimmer, and never when the producer asked for machine readable output.

    Safe to rewrite:

    git log --oneline -5 | head -3     -> rtk git log --oneline -5 | head -3
    ruff check foo.py 2>&1 | tail -15  -> rtk ruff check foo.py 2>&1 | tail -15
    grep -rn foo . | head -20 | tail -5  -> rtk grep -rn foo . | head -20 | tail -5
    

    Must pass through:

    ls -la | wc -l                      2897 lines native, 52 through rtk
    gh pr list | jq '.[].number'        consumer parses structure
    git diff --name-only | head -20     rtk appends a blank line and a "Changes:" trailer
    git log --format=%H | head -1       identical through rtk today, excluded as a precaution
    SHA=$(git log --oneline | head -1)  substitution, not a top level pipe
    

    A bare display trimmer means the segment is exactly one of head, tail, cat, less, more plus flags and a count, nothing else, so | head -3; rm -rf /tmp/x does not qualify. The machine readable test is a match on --format, --pretty, --porcelain, --json, --name-only, --name-status, --raw, -z or --null anywhere in the producer.

    That exclusion is deliberately coarse, because preservation is not consistent. On rtk 0.48.0 here, git status --porcelain and git log --format=%H come back byte identical, while git diff --name-only picks up a blank line and a Changes: trailer. Since it varies by subcommand, skipping the whole class costs a few rewrites and saves auditing every one of them.

    I have been running this as a Python wrapper around rtk hook claude. It splits on top level pipes, applies both tests, delegates the producer to rtk and reattaches the tail. Anything failing either test goes to rtk untouched, and any exception falls through to plain delegation, so the worst case is today's behavior rather than a wrong rewrite.

    Measured over 84,310 Bash tool calls of real Claude Code history, replaying a paired 2000 command sample through each hook: 34.4% rewritten by stock rtk hook claude, 48.1% with the wrapper.

    One implementation note that cost me an afternoon. Splitting the pipeline with a lexer and rejoining tokens destroys redirects. shlex with punctuation_chars=True turns 2>&1 into the tokens 2, >&, 1, which rejoin as 2 >& 1, reading as a stray positional argument plus a redirect, so rtk refuses the whole command. Worse, git log -5 2>/dev/null | head -3 rejoins as git log -5 2 > /dev/null, which would have been a wrong rewrite had rtk accepted it. Slice the original string at top level pipe positions instead, and bail on $(...) and backticks so a pipe inside command substitution is never mistaken for a top level one. Recovering the 2>&1 | tail shape alone was worth about 3,100 commands per 30 days here.

    Happy to share the wrapper and its 27 case test table, or to take a run at the Rust side if that would help.

  5. PromeK-Robotics commented on Sep 22, 2026

    @PromeK-Robotics

    +1, with numbers from a real setup (rtk 0.44.2, Claude Code, rtk hook claude as PreToolUse hook).

    Tested the hook directly:

    command rewritten?
    grep -n foo file yes → rtk grep -n foo file
    cd dir && git status yes
    grep -n foo file | head -5 no
    head -c 500 file no (see also #4072)

    rtk discover over the last 30 days (248 sessions, 74,674 Bash commands) lists grep -n as the top missed command: 10,539 occurrences, ~3.1M tokens estimated. The agent's habit is grep -n … | cut -c1-200 | head, so most of these carry a pipe and never reach rtk.

    Even a narrow rule would recover most of it: when the pipeline is only grep … | head -N / | cut -c1-W | head -N, rewrite to rtk grep -m N -l W … and drop the tail of the pipe. (Blindly rewriting the first segment wouldn't be safe, since rtk's grouped output changes what wc -l and friends count.)

    Side note: discover reports "Already using RTK: 0.2%" because it reads the commands as written in the transcript, before the hook rewrites them, so the simple (rewritten) cases are also counted as missed (related: #3683).

  6. KuSh commented on Sep 26, 2026

    @KuSh
    Collaborator

    Fixed by #3343, shipped in v0.49.0: when every later segment of a pipeline is a display trimmer (head, tail, cat), the hook now rewrites the producer, so git status | cat, git status --porcelain | head -20 and find . -name "*.txt" | head -20 become rtk git status | cat and so on. Pipelines into other consumers such as wc -l or grep keep the producer raw on purpose, because rtk's condensed output would change what those consumers count or match. If a head/tail/cat pipeline is still not rewritten on the latest version, please comment or reopen with the exact command.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions