Skip to content

rtk rewrite mishandles newline-separated (multi-line) commands → only first line rewritten / empty output #2191

Closed

Description

@wowkaw

Summary

rtk rewrite does not treat newline (\n) as a statement separator. It appears to only parse/rewrite the first newline-delimited segment of a command, which breaks multi-statement commands written across multiple lines:

  • If the first line is not a rewritable command (cd, echo, …), rtk rewrite returns empty output with exit 1 ("no rewrite"), even when later lines contain rewritable commands (grep, tsc, …).
  • If the first line is rewritable, only that first line is rewritten; rewritable commands on subsequent lines are left untouched.

Semicolon-separated equivalents are handled correctly. So the bug is specifically newline handling.

Combined with the official Claude Code hook (rtk-rewrite.sh, which applies the rewrite when rtk rewrite exits 0/3), this surfaces as silently truncated output when an agent runs a multi-line shell snippet containing several greps — only the first section is produced. This was hit repeatedly in a real Claude Code session.

Environment

  • rtk 0.42.0 (Homebrew, macOS arm64) — also reproduced on 0.35.0
  • Claude Code PreToolUse hook rtk-rewrite.sh (# rtk-hook-version: 3)

Reproduction (deterministic, no Claude Code needed)

printf 'x\n' > /tmp/f.txt

# (1) Semicolon-separated — CORRECT (both greps rewritten):
rtk rewrite 'cd /tmp; grep -c x /tmp/f.txt; grep -c x /tmp/f.txt'
#   -> cd /tmp; rtk grep -c x /tmp/f.txt; rtk grep -c x /tmp/f.txt      (exit 0)

# (2) Same commands, newline-separated, first line is `cd` — BUG: empty + exit 1
rtk rewrite "$(printf 'cd /tmp\ngrep -c x /tmp/f.txt\ngrep -c x /tmp/f.txt')"
#   -> (empty)                                                          (exit 1)

# (3) Newline-separated, first line is grep — BUG: only FIRST line rewritten
rtk rewrite "$(printf 'grep -c x /tmp/f.txt\ngrep -c x /tmp/f.txt')"
#   -> rtk grep -c x /tmp/f.txt
#      grep -c x /tmp/f.txt          <- second grep left un-rewritten   (exit 0)

Bisection — every multiline form whose first line is non-rewritable returns exit=1, len=0:

command result
cd /tmp; grep…; grep… (semicolons) exit 0, full rewrite ✓
grep…\ngrep… (newlines, grep first) exit 0, only 1st line rewritten
cd /tmp\ngrep…\ngrep… exit 1, empty
cd /tmp\necho A\ngrep…\necho B\ngrep… exit 1, empty
echo 中文\ngrep…\ngrep… exit 1, empty
cd "/tmp/a b"\ngrep…\ngrep… exit 1, empty

Expected

rtk rewrite should treat \n as a statement separator (like ;), rewriting each rewritable statement independently and preserving every statement in the output — so a newline-separated command yields the same result as its semicolon-separated equivalent.

Impact

Multi-line shell snippets are extremely common in agent/hook usage (heredocs, multi-step verification blocks). Today they are either:

  • left with only the first statement token-optimized (mild), or
  • effectively pass-through/empty depending on the first line (and, via hooks that act on the rewrite, produce truncated/partial command execution with no error) — silent and easy to mistake for a tool/agent bug.

Suggested fix

In the rewrite parser (registry), split the command on top-level statement separators including newline (\n) in addition to ; / && / ||, rewrite each segment, and re-join preserving the original separators.

Activity

  1. added
    bug
    help wanted
    on Jun 1, 2026
  2. pszymkowiak commented on Jun 1, 2026

    @pszymkowiak
    Collaborator

    wshm · Automated triage by AI

    🔍 Automated Triage

    🐛 Category bug
    🟠 Priority high
    🎯 Confidence 94%

    Summary

    rtk rewrite fails to treat newlines as statement separators, causing silent truncation or empty output for multi-line commands — fix by extending the rewrite parser to split on \n in addition to ;/&&/||


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

  3. le12484 commented on Jun 10, 2026

    @le12484

    Additional data point — hook JSON parse failure on multiline commands

    Ran rtk discover -s 7 -a across 59 sessions and found 427 multiline commands in 3 days that failed silently at the hook layer with:

    [rtk hook] Failed to parse JSON input: control character found while parsing a string at line 2 column 0
    

    These are commands Claude Code sends as multi-line strings (for loops, heredocs, multi-statement scripts). The hook fails to parse the JSON because the command string contains literal newline characters, so the command runs as-is with no rtk rewrite.

    Environment: rtk 0.42.3, macOS arm64, rtk hook claude

    Impact: 427 genuinely missed rewrites/3 days — grep, git log, find etc. embedded in multi-line scripts. Second-largest category of missed savings after piped commands.

    Suggested fix: Pre-process control characters before JSON parsing, or fall back gracefully to rewriting just the first line when the full parse fails.

  4. KuSh commented on Jul 25, 2026

    @KuSh
    Collaborator

    Hi, thanks for your contribution.
    This should have already been handled by 952245d

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:clibughelp wantedneeds-reproductionpriority:high

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions