Repository navigation
Description
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 claudeActual:
{"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;; esacThe 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 claudeonPreToolUse/ matcherBash - Linux 6.12.95 (Debian 13), bash
Activity
pszymkowiak commentedon Jul 24, 2026
🔍 Automated Triage
bughighSummary
PreToolUse hook rewrite path splits commands on
;without treating;;/;&/;;&as atomic tokens, turning;;into; ;and breaking everycasestatement combined with a rewritable command; fix the splitter to treat these as single tokens (or splice rewrites back by byte offset).Triaged automatically by wshm · This is an automated analysis, not a human review.
nightcityblade commentedon Jul 25, 2026
Hi, I'd like to work on this. I'll submit a PR shortly.
nightcityblade commentedon Jul 25, 2026
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.
mgwedd commentedon Jul 25, 2026
I'd grab this. Root cause is in the discover lexer: the
;branch oftokenize_innernever peeks ahead, so;;becomes two;operator tokens, andrewrite_compoundrejoins each with"; "test(discover): harden case-terminator coverage
mgwedd commentedon Jul 26, 2026
Fix PR submitted: #3202
SoftSolid commentedon Aug 4, 2026
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:
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,fororifleading the command,rtk rewriteexits 1 and leaves the input alone;;;only breaks once something likels/git/rgsits in front of it.@mgwedd's root-cause analysis matches what the current
masterstill shows: the';'arm in the lexer emits an operator token without peeking at a following;(unlike the'&'arm, which peeks and builds&&), andrewrite_compoundthen 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
casestatement with a rewritable command in one Bash call", which is a real papercut for agent-generated shell.HetCreep commentedon Sep 23, 2026
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 claudeunder Claude Code. Two data points that may help the regression tests.1. It also reaches a
caseinside a one-lineforloop, which is how we hit it:bash -naccepts the input and rejects the output withsyntax error near unexpected token `;', so the loop never runs.2. On 0.49.0 a leading
case,iforfordoes not keep the line out of the rewrite; any rewritable segment anywhere in the string is enough:rtk rewriteexitcase x in x) echo A;; esac; ls /tmpcase x in x) echo A; ; esac; rtk ls /tmpif true; then case x in x) echo a;; esac; ls /tmp; fiif true; then case x in x) echo a; ; esac; rtk ls /tmp; fifor f in a b; do case "$f" in a) echo A;; esac; doneThe cause matches the analysis above:
src/discover/lexer.rsstill emits one;operator per character ondevelop, andrewrite_compoundre-emits each as"; ".Happy to re-run these on a build with either fix if that helps.
fix(hook): keep `case` terminators intact when rewriting (#3197)
KuSh commentedon Sep 25, 2026
Thanks — the one-line
forloop 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
developatfeac25d1, with output byte-identical to yours. Rather than compare strings I let bash judge:bash -non the input and on the output, then executing both with a recording stub forrtkonPATH.developfor … case … esac; grep …; donecase x in x) echo A;; esac; ls /tmpif true; then case …; esac; ls /tmp; firc 0→2is the part that matters: the input runs, the rewritten line is rejected by bash, so it never runs at all.Two notes:
caseterminators 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.whileanduntilwere affected too, in the same way as yourfor:On your point 2 — a leading
case/if/forstill 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 -qpreserves both the exit code and the silence that-qimplies, in the match and no-match cases, sogrep -q x README.md || echo missbehaves identically before and after.Your shapes are now pinned
The existing test only covered
ls /tmp; case …— the rewritable command before thecase. Every shape you reported has it after, which is a different path, so none of them was actually covered. They are now, astest_case_terminators_survive_inside_a_blockon #4130, together withwhileanduntil. 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
casepattern broke the same way — bash accepts the input and rejects the output:The leading
(in acasepattern is optional in bash, and it was being read as a subshell. This one is not ondevelop— 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.
fix(hook): keep `case` terminators intact when rewriting (#3197)
HetCreep commentedon Sep 26, 2026
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,
whiletoo, 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 ofdevelopatc75f159, ofc7f76b7(#4129, the commit #4130 sits on) and of #4130's head6a9fbaf. Same approach as the Linux check:bash -non the input and on thertk rewriteoutput, then run both with anrtkpass-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.developc75f159c7f76b76a9fbaffor … case … esac; grep -q …; donecase x in x) echo A;; esac; ls /tmpif true; then case …; esac; ls /tmp; fiwhile read -r l; do case …; esac; ls /tmp; donels /tmp; case x in a) echo 1;& *) echo 2;; esacls /tmp; case x in a) echo 1;;& *) echo 2;; esaccase x in x) echo 1;;esac; ls /tmpf() { case x in x) echo a;; esac; }; f; ls /tmpThe issue's own reproduction. With the exact hook JSON from the issue body,
ls /tmp; case x in a) echo 1;; *) echo 2;; esaccomes back from 0.50.0,developand #4129 asrtk ls /tmp; case x in a) echo 1; ; *) echo 2; ; esac, as reported. From #4130 it comes back asrtk 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. Thegit statusandcatrows 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 claudethe PreToolUse JSON and ran theupdatedInput.commandit returned in Git Bash, with the samertk.exefirst onPATH. 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 fromrtk ls's own formatting. It also reproduces live through Claude Code's Bash tool on this machine: I typedcase x in x) echo A;; esac; ls .and bash receivedcase x in x) echo A; ; esac; rtk ls ., givingsyntax error near unexpected token `;'.KuSh's
rtk grep -qargument for leaving a leadingcase/if/forin the rewrite also holds on Windows: exit code and silence match/usr/bin/grep -qfor both a match (0) and a miss (1).Attribution. #4129 alone (
c7f76b7) still breaks every row, so the fix is6a9fbaf, 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 thatcase x in (ls) echo 1;; esacis left unrewritten by all four of my binaries, matching the note that it isn't ondevelop.Regression check.
c7f76b7and6a9fbafdiffer by exactly this commit, so I diffed theirrtk rewriteoutput. 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 toesac, 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\binfirst onPATH, since somecore::streamtests calltrue/echo/sh). The three new tests plusdiscover::andhooks::permissions: 710 pass.cargo test --all --no-fail-fastat6a9fbaf: 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 ondevelopatc75f159and the PR doesn't touchfind_cmd, so #4130 isn't the cause; I haven't looked into why it fails here.Not covered: PowerShell/
cmdas 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 acasein a command that also has a newline or a pipe is left alone, while a newline or a pipe without acaseis rewritten as usual. What still breaks on 0.50.0 is the single-line, pipe-free form, which is what every shape above is.fix(hook): keep `case` terminators intact when rewriting (#3197)
KuSh commentedon Sep 26, 2026
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;;esacglued. So the lexer fact is pinned at #4130 (;;esaclexes as two tokens) and the byte-exact emission at #4132, where acasegroup 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 thef ()spacing too. I also ran it through the stack's tip, since a later commit (#4135) makes(and)command boundaries andf()is the one place a()pair defines rather than groups: it rewrites the trailinglsand parses, and the original and rewritten lines run identically under a recording stub.Your scoping note — a
casesharing 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;; esacisn't rewritten either, even though the pipeline and thecaseare separate commands. The rewriter bails on the whole line when it sees a|and any bracket, and acasepattern's)counts. Nothing corrupts, so it isn't this issue; filed as #4283 with the repro.Merge pull request #4130 from rtk-ai/lex/02-case-terminators
fix(hook): keep `case` terminators intact when rewriting (rtk-ai#3197)
KuSh commentedon Oct 10, 2026
Fixed on develop by #4130: rtk keeps the
caseterminators;;,;∧;&intact when it rewrites a command line, including insideforandwhilebodies. 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.KuSh commentedon Oct 18, 2026
Fixed by #4130, which shipped in v0.52.0: rtk now keeps the case terminators
;;,;∧;&intact when it rewrites a command line, including inside for and while bodies. The hook payload from the report now comes back asrtk 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.