Skip to content

bug(hook): rewrite path turns ;; into ; ;, breaking every case statement (bash syntax error) #3197

Closed

Description

@bbk1ng

Summary

The Claude PreToolUse hook rewrite path splits the command on ; without treating ;; as an atomic token, then rejoins with "; ". Every ;; becomes ; ;, which is a bash syntax error inside case:

bash: syntax error near unexpected token `;'

The rewritten command never runs. Any Bash call that mixes a case statement with any rewritable command (ls, cat, git, grep, find, …) is broken.

This is a corruption bug, not a filtering one: the rewritten string is not equivalent to the input.

Reproduction (rtk 0.38.0, Linux)

printf '%s' '{"session_id":"t","transcript_path":"/tmp/x","cwd":"/tmp","hook_event_name":"PreToolUse","tool_name":"Bash","tool_input":{"command":"ls /tmp; case x in a) echo 1;; *) echo 2;; esac"}}' \
  | rtk hook claude

Actual:

{"hookSpecificOutput":{"hookEventName":"PreToolUse","permissionDecisionReason":"RTK auto-rewrite","updatedInput":{"command":"rtk ls /tmp; case x in a) echo 1; ; *) echo 2; ; esac"}}}

Expected: ;; preserved verbatim —

rtk ls /tmp; case x in a) echo 1;; *) echo 2;; esac

Running the actual output:

$ bash -c 'rtk ls /tmp; case x in a) echo 1; ; *) echo 2; ; esac'
bash: -c: line 1: syntax error near unexpected token `;'

Trigger conditions

Only the rewrite path mangles ;;; the passthrough path is clean. Verified matrix:

Input Result
case x in a) echo 1;; *) echo 2;; esac passthrough — ;; intact
echo a;; echo b (no rewritable cmd) passthrough — ;; intact
ls /tmp; case x in a) echo 1;; *) echo 2;; esac mangled → rtk ls /tmp; … echo 1; ; …
git status; case x in a) echo 1;; *) echo 2;; esac mangled → rtk git status; …
cat /etc/hostname; case x in a) echo 1;; *) echo 2;; esac mangled → rtk read /etc/hostname; …

So: rewritable command and ;; in the same string ⇒ corruption.

How it was found

An agent session ran a routine PATH check:

echo "=== native launcher ==="; ls -la ~/.local/bin/claude 2>&1 | head -2
case ":$PATH:" in *":$HOME/.local/bin:"*) echo yes;; *) echo NO;; esac

The hook rewrote ls → rtk ls (intended) and ;; → ; ; (not intended), and the call died with the syntax error above. From the model's side this reads as "my own command had a typo", so it retries with a different formulation instead of reporting the hook — the failure mode is silent in practice.

Suggested fix

In the command splitter used by the rewrite path, treat ;; (and, for the same reason, ;& and ;;&) as a single separator token rather than two ; tokens, so it round-trips through the rejoin step. A targeted alternative: after rejoining, only reconstruct segments that were actually rewritten and splice them back into the original string by byte offset, leaving all untouched bytes — including ;; — verbatim. The second approach also fixes the general class of "rejoin loses original spacing/quoting" issues.

Related but distinct: #2262 (lexer doesn't split on newlines), #2191 (multi-line commands), #3190 (\\<newline> fusion), #3171 (safe pipeline rewriting). None cover ;;.

Workaround

exclude_commands under [hooks] in ~/.config/rtk/config.toml does suppress the rewrite on 0.38.0, verified:

[hooks]
exclude_commands = ["ls"]

but it is per-command, not per-syntax, so it costs the rewrite entirely for that command. There is no way to opt out only for strings containing ;;.

Environment

  • rtk 0.38.0
  • Claude Code 2.1.219 (native install), hook rtk hook claude on PreToolUse / matcher Bash
  • Linux 6.12.95 (Debian 13), bash

Activity

  1. added
    bug
    good first issue
    on Jul 24, 2026
  2. pszymkowiak commented on Jul 24, 2026

    @pszymkowiak
    Collaborator

    wshm · Automated triage by AI

    🔍 Automated Triage

    🐛 Category bug
    🟠 Priority high
    🎯 Confidence 97%

    Summary

    PreToolUse hook rewrite path splits commands on ; without treating ;;/;&/;;& as atomic tokens, turning ;; into ; ; and breaking every case statement combined with a rewritable command; fix the splitter to treat these as single tokens (or splice rewrites back by byte offset).

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


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

  3. nightcityblade commented on Jul 25, 2026

    @nightcityblade

    Hi, I'd like to work on this. I'll submit a PR shortly.

  4. nightcityblade commented on Jul 25, 2026

    @nightcityblade

    I need to step back from this one because I can't complete the repository's contributor-agreement requirement on behalf of the account owner. This issue is available for someone else to take—sorry for the churn.

  5. mgwedd commented on Jul 25, 2026

    @mgwedd

    I'd grab this. Root cause is in the discover lexer: the ; branch of tokenize_inner never peeks ahead, so ;; becomes two ; operator tokens, and rewrite_compound rejoins each with "; "

  6. mgwedd commented on Jul 26, 2026

    @mgwedd

    Fix PR submitted: #3202

  7. SoftSolid commented on Aug 4, 2026

    @SoftSolid

    Still reproducible on v0.44.2 (released 2026-08-01), Linux x86_64, musl build installed via install.sh.

    Since the rewrite engine can be called directly, the hook payload isn't needed to show it:

    $ rtk --version
    rtk 0.44.2
    
    $ rtk rewrite 'ls -d /tmp; case "x" in x) echo A;; esac'
    rtk ls -d /tmp; case "x" in x) echo A; ; esac
    
    $ LC_ALL=C bash -c 'rtk ls -d /tmp; case "x" in x) echo A; ; esac'
    bash: -c: line 1: syntax error near unexpected token `;'
    bash: -c: line 1: `rtk ls -d /tmp; case "x" in x) echo A; ; esac'

    The trigger boundary reported in the original matrix still holds on 0.44.2: the mangling happens only when the line contains at least one rewritable segment. With case, for or if leading the command, rtk rewrite exits 1 and leaves the input alone; ;; only breaks once something like ls/git/rg sits in front of it.

    @mgwedd's root-cause analysis matches what the current master still shows: the ';' arm in the lexer emits an operator token without peeking at a following ; (unlike the '&' arm, which peeks and builds &&), and rewrite_compound then appends a space after every ;.

    Since #3202 is still open and unmerged, everyone on the current stable release keeps hitting this. Our workaround remains "never mix a case statement with a rewritable command in one Bash call", which is a real papercut for agent-generated shell.

  8. HetCreep commented on Sep 23, 2026

    @HetCreep

    Thanks for the clear write-up, and to @mgwedd and @KuSh for the fixes in #3202 and #4130.

    This still reproduces on v0.49.0 (the latest stable), this time on Windows 11 with Git Bash, through rtk hook claude under Claude Code. Two data points that may help the regression tests.

    1. It also reaches a case inside a one-line for loop, which is how we hit it:

    $ rtk rewrite 'for f in a.mjs b.test.mjs; do case "$f" in *.test.mjs) continue;; esac; grep -q x README.md || echo miss; done'
    for f in a.mjs b.test.mjs; do case "$f" in *.test.mjs) continue; ; esac; rtk grep -q x README.md || echo miss; done

    bash -n accepts the input and rejects the output with syntax error near unexpected token `;', so the loop never runs.

    2. On 0.49.0 a leading case, if or for does not keep the line out of the rewrite; any rewritable segment anywhere in the string is enough:

    Input rtk rewrite exit Output
    case x in x) echo A;; esac; ls /tmp 3 case x in x) echo A; ; esac; rtk ls /tmp
    if true; then case x in x) echo a;; esac; ls /tmp; fi 3 if true; then case x in x) echo a; ; esac; rtk ls /tmp; fi
    for f in a b; do case "$f" in a) echo A;; esac; done 1 unchanged (nothing to rewrite)

    The cause matches the analysis above: src/discover/lexer.rs still emits one ; operator per character on develop, and rewrite_compound re-emits each as "; ".

    Happy to re-run these on a build with either fix if that helps.

  9. KuSh commented on Sep 25, 2026

    @KuSh
    Collaborator

    Thanks — the one-line for loop was the useful part, and both data points are now regression tests.

    All three of your shapes reproduce, and #4130 fixes all three

    Reproduced on develop at feac25d1, with output byte-identical to yours. Rather than compare strings I let bash judge: bash -n on the input and on the output, then executing both with a recording stub for rtk on PATH.

    shape develop with #4130
    for … case … esac; grep …; done rc 0→2 same stdout, same exit code
    case x in x) echo A;; esac; ls /tmp rc 0→2 same
    if true; then case …; esac; ls /tmp; fi rc 0→2 same

    rc 0→2 is the part that matters: the input runs, the rewritten line is rejected by bash, so it never runs at all.

    Two notes:

    • fix(hook): keep case terminators intact when rewriting (#3197) #4130 alone is enough. I built each stacked branch separately; nothing later in the stack contributes to this fix, so that is the only PR to watch for it.
    • while and until were affected too, in the same way as your for:
      $ rtk rewrite 'while read -r l; do case "$l" in a) continue;; esac; ls /tmp; done'
      while read -r l; do case "$l" in a) continue; ; esac; rtk ls /tmp; done
      Also fixed, and covered.

    On your point 2 — a leading case/if/for still does not keep the line out of the rewrite, and I left it that way. Your own example is the reason it is safe to: rtk grep -q preserves both the exit code and the silence that -q implies, in the match and no-match cases, so grep -q x README.md || echo miss behaves identically before and after.

    Your shapes are now pinned

    The existing test only covered ls /tmp; case … — the rewritable command before the case. Every shape you reported has it after, which is a different path, so none of them was actually covered. They are now, as test_case_terminators_survive_inside_a_block on #4130, together with while and until. I checked the test fails if the ;; lexing is reverted, so it is pinning something.

    One more in the same family

    Looking for neighbours of this bug I found that a bracketed case pattern broke the same way — bash accepts the input and rejects the output:

    $ rtk rewrite 'case x in (ls) echo 1;; esac'
    case x in (rtk ls) echo 1;; esac     # syntax error near unexpected token `ls'

    The leading ( in a case pattern is optional in bash, and it was being read as a subshell. This one is not on develop — it only appeared in a later branch of the same stack — so it is fixed there rather than filed separately.

    Happy to take you up on the re-run offer: #4130 is the branch to build for the three you reported. Windows/Git Bash is the gap in what I verified — everything above was checked on Linux.

  10. HetCreep commented on Sep 26, 2026

    @HetCreep

    Following up on the re-run I offered above: @KuSh noted that Windows/Git Bash was the gap in the Linux check, so I ran #4130 there. On Windows 11 with Git for Windows' bash (GNU bash 5.3.15) it fixes the three shapes from my earlier comment, while too, and the reproduction in the issue body. (The installed release is 0.50.0 now and it still reproduces; my earlier comment was on 0.49.0.)

    Provenance: AI-assisted, human-directed. I built these from source and ran everything below on the machine; nothing here is a read-only check.

    What I ran. Four binaries: the installed release rtk 0.50.0, and debug builds of develop at c75f159, of c7f76b7 (#4129, the commit #4130 sits on) and of #4130's head 6a9fbaf. Same approach as the Linux check: bash -n on the input and on the rtk rewrite output, then run both with an rtk pass-through stub (a shell function, not a recording stub) in a fixture directory (/tmp → .) and compare stdout and exit code. rc 0→2 = the input runs, the rewritten line is rejected by bash.

    shape 0.50.0 develop c75f159 #4129 c7f76b7 #4130 6a9fbaf
    for … case … esac; grep -q …; done rc 0→2 rc 0→2 rc 0→2 same stdout, same rc
    case x in x) echo A;; esac; ls /tmp rc 0→2 rc 0→2 rc 0→2 same
    if true; then case …; esac; ls /tmp; fi rc 0→2 rc 0→2 rc 0→2 same
    while read -r l; do case …; esac; ls /tmp; done rc 0→2 rc 0→2 rc 0→2 same
    ls /tmp; case x in a) echo 1;& *) echo 2;; esac rc 0→2 rc 0→2 rc 0→2 same
    ls /tmp; case x in a) echo 1;;& *) echo 2;; esac rc 0→2 rc 0→2 rc 0→2 same
    case x in x) echo 1;;esac; ls /tmp rc 0→2 rc 0→2 rc 0→2 same
    f() { case x in x) echo a;; esac; }; f; ls /tmp rc 0→2 rc 0→2 rc 0→2 same

    The issue's own reproduction. With the exact hook JSON from the issue body, ls /tmp; case x in a) echo 1;; *) echo 2;; esac comes back from 0.50.0, develop and #4129 as rtk ls /tmp; case x in a) echo 1; ; *) echo 2; ; esac, as reported. From #4130 it comes back as rtk ls /tmp; case x in a) echo 1;; *) echo 2;; esac, byte for byte the report's "Expected", and it runs the same as the original under the stub. The git status and cat rows of the report's table behave the same way, and its two passthrough rows are still passed through by all four.

    End to end through the hook. I fed rtk hook claude the PreToolUse JSON and ran the updatedInput.command it returned in Git Bash, with the same rtk.exe first on PATH. On 0.50.0 all five shapes I ran this way come back rc 2 where the originals return 0. On #4130 all five return 0, with output identical apart from rtk ls's own formatting. It also reproduces live through Claude Code's Bash tool on this machine: I typed case x in x) echo A;; esac; ls . and bash received case x in x) echo A; ; esac; rtk ls ., giving syntax error near unexpected token `;'.

    KuSh's rtk grep -q argument for leaving a leading case/if/for in the rewrite also holds on Windows: exit code and silence match /usr/bin/grep -q for both a match (0) and a miss (1).

    Attribution. #4129 alone (c7f76b7) still breaks every row, so the fix is 6a9fbaf, consistent with "#4130 alone is enough". I didn't build #4138/#4139, so I can't confirm that nothing later contributes, or the (ls) fix; what I can say is that case x in (ls) echo 1;; esac is left unrewritten by all four of my binaries, matching the note that it isn't on develop.

    Regression check. c7f76b7 and 6a9fbaf differ by exactly this commit, so I diffed their rtk rewrite output. On 1,528 real commands from my rtk history (all 28 that contain ;; or ;&, plus 1,500 others) nothing changes except 3 of those 28. On 1,462 generated one-line inputs that bash accepts (;;/;&/;;&, spaced or glued to esac, inside for/while/if/function/subshell/brace groups), #4129 gives bash-rejected output for 761 and #4130 for none, and all 944 that get rewritten run under the stub with the original's stdout and exit code.

    Tests on Windows (C:\Program Files\Git\usr\bin first on PATH, since some core::stream tests call true/echo/sh). The three new tests plus discover:: and hooks::permissions: 710 pass. cargo test --all --no-fail-fast at 6a9fbaf: 3,737 unit and 100 integration tests pass, 1 fails: cmds::system::find_cmd::tests::run_from_args_propagates_find_exit_status (find_cmd.rs:1370, left 1 / right 2). It fails identically on develop at c75f159 and the PR doesn't touch find_cmd, so #4130 isn't the cause; I haven't looked into why it fails here.

    Not covered: PowerShell/cmd as the shell (Git Bash only), release-profile builds, macOS, the #4138/#4139 tips. One scoping note: the two-line command in the report's "How it was found" section is not rewritten at all by any of the four binaries, so it no longer triggers on 0.50.0. In my runs a case in a command that also has a newline or a pipe is left alone, while a newline or a pipe without a case is rewritten as usual. What still breaks on 0.50.0 is the single-line, pipe-free form, which is what every shape above is.

  11. KuSh commented on Sep 26, 2026

    @KuSh
    Collaborator

    Thanks for the Windows run — that was the gap, and the 1,462 generated inputs are a stronger check than anything I had.

    Two of your shapes weren't pinned by any test; they are now, and one of them turned out to need two homes.

    case x in x) echo 1;;esac; ls /tmp — at #4130 the rewrite returns ;; esac, with a space: the terminator is kept (the fix), but the emitter at that point still rebuilds the text around each operator, so the gap is normalised. That's why your execution check said "same" — bash reads both alike — while a byte comparison would not. Byte preservation is what #4132 adds ("rewrite commands without rewriting the text between them"), and the tip keeps ;;esac glued. So the lexer fact is pinned at #4130 (;;esac lexes as two tokens) and the byte-exact emission at #4132, where a case group now runs each of ;;/;&/;;& against every gap width including none.

    f() { case x in x) echo a;; esac; }; f; ls /tmp — pinned at #4130, with the f () spacing too. I also ran it through the stack's tip, since a later commit (#4135) makes ( and ) command boundaries and f() is the one place a () pair defines rather than groups: it rewrites the trailing ls and parses, and the original and rewritten lines run identically under a recording stub.

    Your scoping note — a case sharing a line with a pipe is left alone — is right, and it's sharper than it looks: git status | cat; case x in x) echo 1;; esac isn't rewritten either, even though the pipeline and the case are separate commands. The rewriter bails on the whole line when it sees a | and any bracket, and a case pattern's ) counts. Nothing corrupts, so it isn't this issue; filed as #4283 with the repro.

  12. added 2 commits that reference this issue on Oct 9, 2026
  13. KuSh commented on Oct 10, 2026

    @KuSh
    Collaborator

    Fixed on develop by #4130: rtk keeps the case terminators ;;, ;& and ;;& intact when it rewrites a command line, including inside for and while bodies. That change is not in a stable release yet, so labeling this resolved-pending-close; it will be closed once a release containing it ships. If it still happens on that release, please comment here.

  14. added
    resolved-pending-close
    on Oct 10, 2026
  15. KuSh commented on Oct 18, 2026

    @KuSh
    Collaborator

    Fixed by #4130, which shipped in v0.52.0: rtk now keeps the case terminators ;;, ;& and ;;& intact when it rewrites a command line, including inside for and while bodies. The hook payload from the report now comes back as rtk ls /tmp; case x in a) echo 1;; *) echo 2;; esac. If you still see this on v0.52.0 or later, please comment here and we will 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:clibuggood first issuepriority:highresolved-pending-close

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions