Repository navigation
feat(rewrite): rewrite each line of multi-line Bash blocks - #3188
falsydotdev wants to merge 3 commits into
Conversation
Coding agents routinely emit multi-line Bash blocks of sequential commands. The rewriter tokenized newlines as plain whitespace, so a block was treated as one command: the rtk prefix landed on the first line and every following line ran raw and unfiltered. Split multi-line input at the newline tokens the quote-aware lexer emits (newlines inside quoted strings are never split points) and rewrite each line through the existing single-line path. Blank lines, comment lines, indentation, and CRLF separators are preserved verbatim. Per-line rewriting only applies when every line is an independent command. The whole block passes through untouched when: - a shell keyword opens control flow (for/if/while/case/...) - a list or pipeline continues across the line break (&&, ||, |, |& at a line edge) - a subshell or group spans lines - the block contains a heredoc (existing gate) Ref rtk-ai#1243 Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01TFWJoUCNkUiaAUBomN2Lb3
Hardening from adversarial review of the multi-line rewrite: - If any newline byte was swallowed by quote state (raw \n/\r count vs emitted newline tokens), pass the whole block through. The lexer has no comment awareness, so an apostrophe in a # comment opens quote state and hides subsequent lines; rewriting such a block would act on lines no permission verdict accounted for. Passthrough hands the original command to native permission handling. Quoted multi-line strings (commit messages) forgo their rewrite as the safe trade. - Bail when (( or )) sits at a line edge: arithmetic spanning lines must not get a command prefix spliced into arithmetic context. Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01TFWJoUCNkUiaAUBomN2Lb3
|
Pushed a hardening commit (aaa123c) after running an adversarial review against the branch. Two gaps closed:
Both cases have tests ( |
|
hey, i think this is addressing same enhancement as #2781 |
- Strip trailing unquoted comments before the independence checks:
'git log | # keep pipeline' continues the pipeline across the
newline even though the line ends in comment text, and rewriting
the next line would rebuild a pipeline whose final stage the
pipeline-safety guard may specifically reject (e.g. grep -f).
- Bail when a line's unquoted ()/{} don't balance: array literals
(arr=(one), function bodies (foo() {), and groups span lines, so
surrounding lines are not independent commands. Subsumes the
previous single-char edge checks.
- Bail on any block containing $'...': inside ANSI-C quoting bash
treats \' as a literal quote that does not close the string, but
the lexer thinks it does — emitting a split point bash would never
honor, the inverse of the swallowed-newline case the count check
catches.
Co-Authored-By: Claude Fable 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01TFWJoUCNkUiaAUBomN2Lb3
|
Second adversarial review round (two independent reviewers) — three more gaps closed in 882d561:
The review also re-probed everything previously fixed (comment-apostrophe swallowed newlines, cross-line arithmetic, CRLF/lone-CR byte round-trips, UTF-8 offset slicing, One divergence found during review is pre-existing and outside this branch's diff ( |
|
@aeppling You're right, thanks — same territory (#1243), and I'd missed #2781 when I searched for prior art (I grepped for "multiline", your title says "bundles" — my bad). The two PRs differ mainly in risk posture. #2781 goes for coverage: statement-level rewriting inside bundles that also contain pipes/substitutions/redirects, with the #1155 ask-clamp as the safety net (39.3% → 71.8% is a big win). #3188 is deliberately narrow: strictly line-level, and any construct that makes lines non-independent — or that the lexer can't attest — forces whole-block passthrough. Lower coverage, but the rewrite can never touch a line the splitter misjudged. Since we'd just built an adversarial corpus for #3188 (two independent review rounds), I ran it against the #2781 head (2c89559). Four inputs currently rewrite in ways worth a look — all pass
#3188 now passes all of these by bailing to passthrough (the fix commits document each one). Happy for these to just become fuzz-corpus additions to #2781 if maintainers prefer the higher-coverage direction — the tests transfer either way, and I'd rather one good implementation land than two compete. Maintainers' call. |
|
The failing |
|
Hey @revopsrocks , thanks for addressing this, here is a review, some of those case are not likely to be ran by agent, but if possible to have a solid implementation that cover most of case Narrow the $' bailOne unrelated line currently disables the whole block: Only ' inside a $'…' span is actually hazardous, the rest rewrite normally. Narrow the cross-line && / || / | baildevelop rewrites this correctly and completely today; the PR declines it: develop → rtk git status && rtk git log -3 comment_start should recognise "#" after an operatorBash starts a comment wherever # begins a word, including right after |, &&, ;, (, ) — not only after whitespace: Guard the heredoc gate against line continuationscollapse_line_continuations joins <NL> with a space where bash joins with nothing, so << stops existing and has_heredoc() misses it: = cat < <EOF / rtk git status / EOF → syntax error near '<' No real-world occurrences produced by agents, but the fix is one line: run has_heredoc() on the raw input, before collapse_line_continuations. #[test]
fn test_heredoc_split_by_line_continuation_passes_through() {
assert_eq!(
rewrite_command_no_prefixes("cat <\\\n<EOF\ngit status\nEOF", &[]),
None
);
} |
|
Thanks for building this out. The line-by-line multiline rewrite landed on develop in #3319 (merged 2026-08-03), covering the same split-at-newline-tokens approach and the same passthrough gates for control flow, cross-line lists and pipelines, grouping and heredocs, along with a few extra ones. Running the develop binary on the example from this PR gives exactly the output shown here, and the lexer test added here is present on develop under the same name, so nothing from this PR looks missing. Please comment or reopen if you find a block shape develop still mishandles. |
Summary
rtkprefix landed on the first line and every following line ran raw and unfiltered. Implements the line-by-line rewrite proposed in Feature: rewrite subcommands inside multi-line / heredoc Bash blocks #1243.tokenize_inner(_, true), already used bysplit_for_permissions), so newlines inside quoted strings — e.g. multi-line commit messages — are never split points. Each line is then rewritten through the existing single-line path; blank lines, comments, indentation, and CRLF separators are preserved verbatim.for/if/while/case/…), a list or pipeline continues across the line break (&&,||,|,|&at a line edge), a subshell/group spans lines, or the block contains a heredoc (existing gate). The heredoc output-filtering idea in Feature: rewrite subcommands inside multi-line / heredoc Bash blocks #1243 is out of scope here.Example:
Test plan
cargo fmt --all && cargo clippy --all-targets && cargo test(2559 passed)rtk hook checkoutput inspected for sequential lines, indented/comment/blank lines, CRLF, for/if blocks, cross-line&&and|/|&continuations, quoted multi-line strings, heredocsregistry::tests::multiline_blocks, lexer newline-token test) covering the rewrite and every passthrough gateRef #1243