Skip to content

fix(discover): make a subshell a command boundary everywhere - #4135

Open
KuSh wants to merge 1 commit into
lex/06-report-every-commandfrom
lex/07-subshell-boundary
Open

KuSh wants to merge 1 commit into
lex/06-report-every-commandfrom
lex/07-subshell-boundary

Conversation

@KuSh

@KuSh KuSh commented Sep 19, 2026 •

Copy link
Copy Markdown
Collaborator

Three consumers read the same command line — the permission gate, the report's
classification, and the rewrite — and they disagreed about what a command was.
Each disagreement was a bug already happening rather than one waiting to.

(git status; cargo build) was the clearest. The gate split it, the other two
did not, so the rewrite glued the bracket to the text beside it: (git status
matched no rule while cargo build) matched, and one command of a pair was
filtered while the other silently was not. (git status) was rewritten not at
all. A subshell runs the commands it wraps, so ( and ) now end a command for
classification and for the rewrite, as they always did for the gate. Background
& was the same story and gets the same answer.

The two places that must not follow are now stated rather than assumed:

A $( ) is not a subshell for this purpose. Its body runs, so the gate
descends and holds it to the deny rules; the other two stay out, because the
text around a substitution is not a command of its own — descending invents a
git log $ nobody ran — and what it captures is a string the outer command is
built from, so filtering it would change that string rather than what reaches
anyone. Policy::descend_into_substitution carries that, and the emitter tracks
the same depth.

Staying out means every separator, not only the brackets. &&, ||, ; and
| inside a substitution separate commands whose output becomes one word of the
command being built, so none of them ends a command out here — otherwise
echo $(git status && git log) prints a line nobody wrote. Brackets nest, so a
( inside a substitution counts up as well, or its ) closes the substitution
one bracket early and the real closing ) reads as a boundary out here.

<( ) and >( ) are substitutions too. They hand the outer command a file
rather than text, but the inner command serves the outer one just the same, so
none of the three is a place to end a command or to filter output somebody else
is about to parse. Reading only $ left diff <(a) <(b) classified as four
commands — two of them the fragments diff < and < — which rtk session
counts without the unattestable-construct check that shields the hook, so its
adoption figure was being computed from commands that never ran.

A { } is not a subshell either. { opens a group only at a command position;
anywhere else it is brace expansion, and ls {a,b}.txt is one command. ( has
no such ambiguity, which is why it became a boundary and { did not. The gate
copes by stripping the bracket before matching, so nothing hides behind one.

Nor is name() one. It names a function, so the word before it runs nothing and
rtk ls() { does not parse: classification and the rewrite skip the word and the
empty pair, and read the body as usual. A ( glued to a word, as in a[(b)],
x=(a b) or @(a|b), ends no command for them either, since it runs nothing of
its own.

A case pattern is the one place a ( opens nothing. case x in (ls) echo 1;; esac is the same statement as case x in ls) echo 1;; esac, so reading that
bracket as a subshell makes the pattern a command position: the rewrite turns a
one-word pattern into (rtk ls), and bash refuses the line. CaseTracker follows
case through its in, each arm terminator and its esac, and both segmenters
consult it before reading a ( as a boundary.

A pipeline's swap runs a line in a child sh, which may read a subshell or a
compound command otherwise or not at all, and its checks read words rather than that
grammar. Now that the rewrite reaches into them, a line holding one takes the
ordinary rewrite rather than the swap.

mod segmenter_consistency becomes segmenter_agreement. It used to pin three
answers to one question and call the disagreement deliberate; it now asserts the
agreement where it exists and names the reason for each place it does not. A
divergence nobody can state a reason for is a bug.

time (cargo build) becomes time (rtk cargo build). The wrapper never could
peel a (, but it no longer has to — the subshell is a boundary, so the command
inside is rewritten where it stands and the wrapper keeps wrapping the subshell.

The differential harness keeps both halves. The gate's is unchanged — a gate
that segments differently is a gate with different holes — and the legacy walker
it compares against is untouched. Classify can no longer be compared to that
walker, which describes a policy this commit deliberately changed, so it is
compared to the gate instead: on any input without a newline, a redirect or a
substitution the two hold the same policy and must agree exactly, which anchors
classify to the frozen walker through the gate. The corpus also gains the
substitution shapes it never had, including the nested and unterminated ones.

A span invariant sits alongside it — every segment is text that was really
there, at the span it claims, in order and without overlap. That is worth having
against arithmetic slips but it is not a segmentation contract: gaps between
segments are legal, since that is where the separators live, so a boundary read
in the wrong place passes it. Its comment says so rather than implying
otherwise.

Also corrects .claude/rules/rust-patterns.md, which required argument parsing
to go through tokenize_with_options. There is no such function; it is
tokenize_grammar (src/core/arg_tokenizer.rs:296).

Test plan

  • cargo fmt --all && cargo clippy --all-targets && cargo test --all — 5899 passing, clippy clean, at this commit rather than only at the tip of the stack
  • (git status; cargo build) prefixes both commands and (git status) is rewritten, while git log $(git rev-parse HEAD~1) leaves the captured command alone

Part of #4315 (#4129 → #4138). Based on #4134. Each commit gates green on its own, so the stack is bisectable.

🤖 Generated with Claude Code

@KuSh
KuSh added this pull request to stack #4139 September 19, 2026 13:31
@KuSh
KuSh marked this pull request as ready for review September 19, 2026 16:21
@KuSh
KuSh force-pushed the lex/07-subshell-boundary branch from 1ac6bef to d3f03e5 Compare September 19, 2026 21:13
@KuSh
KuSh force-pushed the lex/07-subshell-boundary branch from d3f03e5 to 18b47d4 Compare September 20, 2026 23:23
@KuSh
KuSh force-pushed the lex/07-subshell-boundary branch from 18b47d4 to 7f5df9e Compare September 25, 2026 22:55
@KuSh
KuSh force-pushed the lex/07-subshell-boundary branch 2 times, most recently from 86ef2e0 to 33e5b49 Compare September 25, 2026 23:29
@KuSh
KuSh force-pushed the lex/07-subshell-boundary branch from 33e5b49 to 042d2b0 Compare September 26, 2026 17:56
@KuSh
KuSh force-pushed the lex/07-subshell-boundary branch 5 times, most recently from 97a5262 to 2c77878 Compare October 2, 2026 15:07
@KuSh
KuSh force-pushed the lex/07-subshell-boundary branch from 2c77878 to 47f858b Compare October 5, 2026 22:36
@KuSh
KuSh force-pushed the lex/07-subshell-boundary branch 2 times, most recently from 157fdfd to 4868dda Compare October 9, 2026 11:07
@TaKO8Ki
TaKO8Ki self-requested a review October 9, 2026 14:48
@KuSh
KuSh force-pushed the lex/07-subshell-boundary branch from 4868dda to b7c011b Compare October 9, 2026 16:16
@KuSh
KuSh force-pushed the lex/07-subshell-boundary branch 3 times, most recently from b9f1ace to d4c618f Compare October 20, 2026 23:52
@KuSh
KuSh force-pushed the lex/07-subshell-boundary branch 4 times, most recently from b563c4e to 64120f0 Compare October 31, 2026 14:53
@KuSh
KuSh force-pushed the lex/07-subshell-boundary branch 2 times, most recently from 1cc2472 to 15a4577 Compare November 1, 2026 15:19
@TaKO8Ki
TaKO8Ki force-pushed the lex/07-subshell-boundary branch from 15a4577 to bfd411a Compare November 2, 2026 11:31
@KuSh
KuSh force-pushed the lex/07-subshell-boundary branch 2 times, most recently from 4b67dee to 43aedf6 Compare November 3, 2026 06:27
@KuSh
KuSh marked this pull request as draft November 4, 2026 07:41
@KuSh
KuSh force-pushed the lex/07-subshell-boundary branch from 43aedf6 to 2f5e30e Compare November 5, 2026 11:48
@KuSh
KuSh marked this pull request as ready for review November 5, 2026 12:22
@KuSh
KuSh force-pushed the lex/07-subshell-boundary branch from 2f5e30e to 6fb8a34 Compare November 17, 2026 01:42
@KuSh
KuSh force-pushed the lex/07-subshell-boundary branch from 6fb8a34 to 91edd3e Compare November 18, 2026 01:06
@KuSh
KuSh force-pushed the lex/07-subshell-boundary branch 2 times, most recently from b268c4e to f857e44 Compare November 21, 2026 09:57
Three consumers read the same command line — the permission gate, the report's
classification, and the rewrite — and they disagreed about what a command was.
Each disagreement was a bug already happening rather than one waiting to.

`(git status; cargo build)` was the clearest. The gate split it, the other two
did not, so the rewrite glued the bracket to the text beside it: `(git status`
matched no rule while `cargo build)` matched, and one command of a pair was
filtered while the other silently was not. `(git status)` was rewritten not at
all. A subshell runs the commands it wraps, so `(` and `)` now end a command for
classification and for the rewrite, as they always did for the gate. Background
`&` was the same story and gets the same answer.

The two places that must not follow are now stated rather than assumed:

A `$( )` is not a subshell for this purpose. Its body runs, so the gate
descends and holds it to the deny rules; the other two stay out, because the
text around a substitution is not a command of its own — descending invents a
`git log $` nobody ran — and what it captures is a string the outer command is
built from, so filtering it would change that string rather than what reaches
anyone. `Policy::descend_into_substitution` carries that, and the emitter tracks
the same depth.

Staying out means every separator, not only the brackets. `&&`, `||`, `;` and
`|` inside a substitution separate commands whose output becomes one word of the
command being built, so none of them ends a command out here — otherwise
`echo $(git status && git log)` prints a line nobody wrote. Brackets nest, so a
`(` inside a substitution counts up as well, or its `)` closes the substitution
one bracket early and the real closing `)` reads as a boundary out here.

`<( )` and `>( )` are substitutions too. They hand the outer command a file
rather than text, but the inner command serves the outer one just the same, so
none of the three is a place to end a command or to filter output somebody else
is about to parse. Reading only `$` left `diff <(a) <(b)` classified as four
commands — two of them the fragments `diff <` and `<` — which `rtk session`
counts without the unattestable-construct check that shields the hook, so its
adoption figure was being computed from commands that never ran.

A `{ }` is not a subshell either. `{` opens a group only at a command position;
anywhere else it is brace expansion, and `ls {a,b}.txt` is one command. The gate
copes by stripping the bracket before matching, so nothing hides behind one.

Nor is `name()` one. It names a function, so the word before it runs nothing and
`rtk ls() {` does not parse: classification and the rewrite skip the word and the
empty pair, and read the body as usual. A `(` glued to a word, as in `a[(b)]`,
`x=(a b)` or `@(a|b)`, ends no command for them either, since it runs nothing of
its own.

A `case` pattern is the one place a `(` opens nothing. `case x in (ls) echo 1;;
esac` is the same statement as `case x in ls) echo 1;; esac`, so reading that
bracket as a subshell makes the pattern a command position: the rewrite turns a
one-word pattern into `(rtk ls)`, and bash refuses the line exactly as it refuses
a `;;` split into `; ;`. `CaseTracker` follows `case` through its `in`, each arm
terminator and its `esac`, and both segmenters consult it before reading a `(` as
a boundary. `case` counts as the keyword only in command position, so `echo case`
stays a word. The `)` still ends the pattern, so an arm's own commands are
rewritten whether or not the pattern was written with brackets.

`mod segmenter_consistency` becomes `segmenter_agreement`. It used to pin three
answers to one question and call the disagreement deliberate; it now asserts the
agreement where it exists and names the reason for each place it does not. A
divergence nobody can state a reason for is a bug.

`time (cargo build)` becomes `time (rtk cargo build)`. The wrapper never could
peel a `(`, but it no longer has to — the subshell is a boundary, so the command
inside is rewritten where it stands and the wrapper keeps wrapping the subshell.

A pipeline's swap runs a line in a child `sh`, which may read a subshell or a
compound command otherwise or not at all, and its checks read words rather than that
grammar. Now that the rewrite reaches into them, a line holding one takes the
ordinary rewrite rather than the swap.

The differential harness keeps both halves. The gate's is unchanged — a gate
that segments differently is a gate with different holes — and the legacy walker
it compares against is untouched. Classify can no longer be compared to that
walker, which describes a policy this commit deliberately changed, so it is
compared to the gate instead: on any input without a newline, a redirect or a
substitution the two hold the same policy and must agree exactly, which anchors
classify to the frozen walker through the gate. The corpus also gains the
substitution shapes it never had, including the nested and unterminated ones.

A span invariant sits alongside it — every segment is text that was really
there, at the span it claims, in order and without overlap. That is worth having
against arithmetic slips but it is not a segmentation contract: gaps between
segments are legal, since that is where the separators live, so a boundary read
in the wrong place passes it. Its comment says so rather than implying
otherwise.

Also corrects `.claude/rules/rust-patterns.md`, which required argument parsing
to go through `tokenize_with_options`. There is no such function; it is
`tokenize_grammar` (`src/core/arg_tokenizer.rs:296`).

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@KuSh
KuSh force-pushed the lex/07-subshell-boundary branch from f857e44 to 77f96a8 Compare November 25, 2026 02:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant