Skip to content

Hook rewrites piped commands: compressed output feeds downstream parsers, silently wrong results #3558

Closed

Description

@sdewell

rtk 0.43.0, macOS, Claude Code PreToolUse hook (rtk hook claude).

The hook rewrites a command even when its stdout is piped into another program:

$ echo '{"tool_name":"Bash","tool_input":{"command":"git diff main..HEAD | grep -c \"^+\""}}' | rtk hook claude
→ updatedInput.command = rtk git diff main..HEAD | grep -c "^+"

The downstream program then parses compressed output instead of the real format. Reproduced on a real repo (same two-commit range):

$ rtk git diff HEAD~2..HEAD | grep -c '^+'
0
$ /usr/bin/git diff HEAD~2..HEAD | grep -c '^+'
50

grep counting zero added lines is a silent wrong answer — in an agent session it reads as "no changes", and we traced a real misdiagnosis back to exactly this.

The 0.43 never-worse guard protects the reading consumer, but a piped consumer parses structure (^+ prefixes, column layouts) that compression removes. Compression is for the model's eyes; anything behind a pipe needs raw bytes.

Suggested fix, either layer works:

  • rtk passthrough when !isatty(stdout) (same convention as git's pager/color handling), or
  • the hook engine skips rewriting a command whose proxied stage is followed by a pipe.

Related earlier reports from this setup: #3025, #3026.

Activity

  1. pszymkowiak commented on Aug 13, 2026

    @pszymkowiak
    Collaborator

    wshm · Automated triage by AI

    🔍 Automated Triage

    🐛 Category bug
    🟠 Priority high
    🎯 Confidence 90%

    Summary

    rtk hook claude rewrites piped/chained commands to a compressed format, causing downstream parsers (e.g. grep) to consume the wrong data and silently produce incorrect results; recommend passthrough when stdout isn't a tty or skipping rewrite when the proxied command is piped.

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

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

  2. vishae commented on Sep 6, 2026

    @vishae

    Variant: the rewritten command is grep itself, and stdout is empty rather than compressed

    Same family as this issue, but worth recording because the failure presents differently. Here the piped command is the rewritten one, and nothing reaches the pipe at all.

    rtk 0.46.0, macOS, Claude Code hook install (rtk init --global). Against a 6222-line file containing 12 matches:

    grep -c "Override" main.js            → 12
    grep -n "Override" main.js            → 12 lines, correct
    grep -n "Override" main.js | wc -l    → 0
    grep -n "Override" main.js | head -2  → (nothing)
    grep -n "Override" main.js | cat      → (nothing)
    rtk proxy grep -n "Override" main.js | wc -l → 12
    

    wc -l reporting 0 rules out display truncation — no lines reach the pipe. Exit status is 0 throughout, so nothing signals a problem.

    The reason I'm adding it here rather than opening a separate issue: in the case above, a downstream parser gets malformed data and returns a wrong number. In this variant the consumer gets nothing, and for grep specifically an empty result is not a malformed answer — it's a meaningful one. It reads as "this pattern does not appear in this file," which is a conclusion. In my case an AI agent acted on that conclusion and spent several steps investigating why source code was "missing" from a repository it was in fact present in. Silent-wrong-number is bad; silent-wrong-absence from a search tool is worse, because there's nothing anomalous to notice.

    The !isatty(stdout) passthrough proposed above would fix this case too.

  3. KuSh commented on Sep 13, 2026

    @KuSh
    Collaborator

    Fixed by #3343, shipped in v0.49.0, the second of the two layers you suggested. The hook now leaves a piped producer alone and only rewrites a stage whose consumer is safe, so your command comes back as git diff main..HEAD | rtk grep -c "^+" with git's real output feeding grep; on this repo the rtk and /usr/bin/git forms both count 5402. The variant reported in the thread also behaves: grep -n ... | wc -l is not rewritten, and piping rtk grep into wc/head/cat returns all 12 matching lines. If you find another shape where compressed output reaches a parser, comment here and we'll reopen.

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