Repository navigation
test(discover): let bash decide whether a rewrite changed what runs, and fix what it found (#3262) - #4136
Open
KuSh wants to merge 1 commit into
Open
test(discover): let bash decide whether a rewrite changed what runs, and fix what it found (#3262)#4136KuSh wants to merge 1 commit into
KuSh wants to merge 1 commit into
Conversation
📊 Automated PR Analysis
SummaryReplaces a regex-based ENV_PREFIX pattern for stripping leading Review Checklist
Analyzed automatically by wshm · This is an automated analysis, not a human review. |
2 tasks done
KuSh
marked this pull request as ready for review
September 19, 2026 16:21
KuSh
force-pushed
the
lex/08-exec-oracle
branch
from
September 19, 2026 21:14
a2f2e6e to
ce54424
Compare
KuSh
force-pushed
the
lex/08-exec-oracle
branch
from
September 20, 2026 23:23
ce54424 to
d4e455d
Compare
KuSh
force-pushed
the
lex/08-exec-oracle
branch
from
September 25, 2026 22:55
d4e455d to
4a9c682
Compare
KuSh
force-pushed
the
lex/08-exec-oracle
branch
3 times, most recently
from
September 25, 2026 23:29
17286d0 to
f842f9d
Compare
10 tasks done
KuSh
force-pushed
the
lex/08-exec-oracle
branch
from
September 26, 2026 17:56
f842f9d to
b8a305d
Compare
KuSh
added a commit
that referenced
this pull request
Sep 26, 2026
…ers (#4281) Claude Code matches a deny or ask rule past any leading assignment and past a fixed set of wrappers — `timeout`, `time`, `nice`, `nohup`, `stdbuf`, `command`, `builtin`, `noglob` and a bare `xargs` — so `Bash(git push *)` stops `HUSKY=0 git push` and `timeout 30 git push` as well as `git push`. RTK's gate matched the segment as written and nothing else, so every one of those forms fell through to Default; under a host that treats Default as its own decision, as #3909's OpenClaw does, they auto-apply. The rewrite already looked through the same prefixes: `timeout 30 git push` became `timeout 30 rtk git push` while the gate did not recognise `git push` inside it. That was two readings of which command a line runs, and the gap between them is this bug. The deny and ask loops now also try `peel_for_matching(segment)`, which applies the rewrite's own peels — its env peel, its keyword prefixes, its wrapper table — to a fixpoint, so the gate and the rewrite cannot disagree again. Two wrappers the host strips were not in the rewrite's table, for a reason that still holds: `stdbuf` exists to unbuffer the wrapped command's output, which routing through rtk would buffer, and `xargs` runs whatever it is handed. Recognising a command is not the same decision as rewriting it, so they sit in a match-only table the rewrite never consults. `xargs` is peeled only in its bare form, as the host does; any option makes the wrapper grammar give up. The line is drawn at the shell. `env`, `exec` and `nocorrect` are peeled too, though the host's list stops short of them; each runs the next word as the command. A program's own arguments are not: `git -C . push` stays `git`, as the host documents, since reading past it would mean knowing every program's option grammar. Allow is deliberately left as it was. The host matches allow past known-safe variables only, and peeling a wrapper for it would let `timeout 30 <cmd>` inherit `<cmd>`'s allow rule — so the peel can only make a verdict stricter. Because assignments go through the rewrite's env peel, the gate reads them exactly as the rewrite does, including where that peel falls short of bash: uppercase names only, and a quoted value not followed by a space (#3262). Both are fixed in that peel itself, where #4136 replaces it, and the gate follows. Fixes #4281 Co-Authored-By: Claude Opus 5.5 <[email protected]>
KuSh
force-pushed
the
lex/08-exec-oracle
branch
7 times, most recently
from
October 2, 2026 15:07
7cf771a to
434109f
Compare
KuSh
added a commit
that referenced
this pull request
Oct 5, 2026
…ers (#4281) Claude Code matches a deny or ask rule past any leading assignment and past a fixed set of wrappers — `timeout`, `time`, `nice`, `nohup`, `stdbuf`, `command`, `builtin`, `noglob` and a bare `xargs` — so `Bash(git push *)` stops `HUSKY=0 git push` and `timeout 30 git push` as well as `git push`. RTK's gate matched the segment as written and nothing else, so every one of those forms fell through to Default; under a host that treats Default as its own decision, as #3909's OpenClaw does, they auto-apply. The rewrite already looked through the same prefixes: `timeout 30 git push` became `timeout 30 rtk git push` while the gate did not recognise `git push` inside it. That was two readings of which command a line runs, and the gap between them is this bug. The deny and ask loops now also try `peel_for_matching`, which applies the rewrite's own peels — its env peel, its keyword prefixes, its wrapper table — to a fixpoint, so the gate and the rewrite cannot disagree again. It reads the segment after the split's leftover grammar is gone, since `{ HUSKY=0 git push; }` reaches the gate as `{ HUSKY=0 git push`, and the grammar-only reading stays a try of its own for a rule that names the assignment. Two wrappers the host strips were not in the rewrite's table, for a reason that still holds: `stdbuf` exists to unbuffer the wrapped command's output, which routing through rtk would buffer, and `xargs` runs whatever it is handed. Recognising a command is not the same decision as rewriting it, so they sit in a match-only table the rewrite never consults. `xargs` is peeled only in its bare form, as the host does; any option makes the wrapper grammar give up. The line is drawn at the shell. `env`, `exec` and `nocorrect` are peeled too, though the host's list stops short of them; each runs the next word as the command. A program's own arguments are not: `git -C . push` stays `git`, as the host documents, since reading past it would mean knowing every program's option grammar. Allow is deliberately left as it was. The host matches allow past known-safe variables only, and peeling a wrapper for it would let `timeout 30 <cmd>` inherit `<cmd>`'s allow rule — so the peel can only make a verdict stricter. Because assignments go through the rewrite's env peel, the gate reads them exactly as the rewrite does, including where that peel falls short of bash: uppercase names only, and a quoted value not followed by a space (#3262). Both are fixed in that peel itself, where #4136 replaces it, and the gate follows. Co-Authored-By: Claude Opus 5.5 <[email protected]>
KuSh
force-pushed
the
lex/08-exec-oracle
branch
from
October 15, 2026 12:03
54fb2d3 to
401d7ff
Compare
KuSh
force-pushed
the
lex/08-exec-oracle
branch
3 times, most recently
from
October 26, 2026 11:29
8abe23d to
413ac70
Compare
KuSh
force-pushed
the
lex/08-exec-oracle
branch
2 times, most recently
from
October 28, 2026 23:54
fdd23a6 to
05ef594
Compare
KuSh
force-pushed
the
lex/08-exec-oracle
branch
2 times, most recently
from
November 1, 2026 15:19
d1c70e0 to
a08ffff
Compare
TaKO8Ki
force-pushed
the
lex/08-exec-oracle
branch
from
November 2, 2026 11:31
a08ffff to
2cc8469
Compare
KuSh
force-pushed
the
lex/08-exec-oracle
branch
2 times, most recently
from
November 3, 2026 06:27
0f0383a to
c3b24db
Compare
KuSh
marked this pull request as draft
November 4, 2026 07:41
KuSh
force-pushed
the
lex/08-exec-oracle
branch
from
November 5, 2026 11:48
c3b24db to
6fb1053
Compare
KuSh
marked this pull request as ready for review
November 5, 2026 12:22
5 tasks done
KuSh
force-pushed
the
lex/08-exec-oracle
branch
from
November 17, 2026 01:42
6fb1053 to
acf63e4
Compare
KuSh
force-pushed
the
lex/08-exec-oracle
branch
from
November 18, 2026 01:06
acf63e4 to
5a8adf3
Compare
KuSh
force-pushed
the
lex/08-exec-oracle
branch
from
November 19, 2026 01:12
5a8adf3 to
bc71650
Compare
KuSh
force-pushed
the
lex/08-exec-oracle
branch
from
November 21, 2026 09:57
bc71650 to
3554ef3
Compare
…and fix what it found (#3262) Every test of the rewriter compares it against an expectation somebody wrote down. A mistake in the author's model of shell grammar therefore sits on both sides of the comparison and agrees with itself — which is how a rewrite that reached inside `$( )` passed a full suite, and how a bracket counted on one side but not the other passed it again. This asks bash instead. Recording stubs sit ahead of everything on `PATH`, the original command and its rewrite are each run, and what actually executed has to match: which programs, with which arguments, in which directory. The `rtk` stub runs the command it wraps and marks its output, the way the real one filters, so a rewrite at a command position changes nothing that is compared while one that reaches into a substitution changes the text the outer command was built from. Three things had to be got right, none of which reading the code would have told me: - `echo` is a shell builtin, so a stub by that name is never reached. The outer command of a substitution has to be one bash looks up on `PATH`. - The stages of a pipeline run concurrently and reach the log in whatever order they are scheduled, so the comparison is over a set. - `>( )` hands over the writing end of a pipe. It opens but never delivers, so reading it waits for a writer that is waiting for this process to finish. The oracle is held to detecting as well as passing. Alongside the equivalence cases are rewrites wrong in ways this repository has actually shipped — reaching into a substitution, into a process substitution, into a quoted assignment, and turning one command into another — each of which it has to object to. And a command RTK merely declined to rewrite runs identically, so it has to stay quiet about that, or "everything differs" would satisfy the rest. It found #3262 on its first run against the real rewriter, from a case nobody had written an expectation for. `D='# shellcheck disable=SC2034'; echo hi` came back as `D='# rtk shellcheck disable=SC2034'; echo hi`: RTK edited inside a quoted literal, so the variable held text nobody wrote. The env prefix was matched with a pattern whose value alternation is `(?:"…"|'…'|[^\s]*)` followed by `\s+`. A quoted value that is not followed by a blank fails that `\s+`, the alternation backtracks to the unquoted branch, and `[^\s]*` takes the fragment up to the first space — `D='# ` — leaving `shellcheck disable=SC2034'` to be read as the command. Three shapes share the one cause, and only the first was reported: D='# shellcheck disable=SC2034'; echo hi -> prefix D='# command shellcheck … D="x y"; echo hi -> prefix D="x command y"… D='no trailing space' -> prefix D='no command trailing… A quoted value is one word however many blanks it holds, so the prefix is now read by scanning words with quote state rather than by matching a pattern: a backslash escapes inside double quotes and is literal inside single ones, and a word ends at the first blank that is not inside either. A word is an assignment on bash's own terms: a name of letters, digits and `_` not starting with a digit, then `=` or `+=`, in any case. The pattern took uppercase names only, so `foo=bar git status` was never rewritten, and `d='# shellcheck …'` escaped the quoting fix above only by not being recognised at all. `1a=b`, `foo-bar=x` and `"foo"=x` are commands to bash and stay commands here. `sudo` is still never stripped (#146). Four call sites read that prefix and each carried its own copy of the pattern, which is why they had to change together or disagree. They now go through one `split_env_prefix`, so there is one place to be right. It also retires `strip_disabled_prefix`'s trick of recovering the prefix length by subtracting the stripped string's length from the original's, which worked only because the pattern happened to remove a prefix. `RTK_DISABLED=` (and `RTK_DISABLED+=`) is one of those words, so it is recognised as one: a message that only mentions it (`MSG='use RTK_DISABLED=1'; git status`) disables nothing and draws no warning, in `rtk rewrite`, `rtk gain` and `rtk discover` alike. The warning says a command was skipped, so an assignment with no command after it (`RTK_DISABLED=1; git status`) draws none either, and the command behind the `;` is rewritten. The permission gate reads the command behind each assignment in turn, so a deny or ask rule can name any layer of a prefix. It does so through `split_env_word`, the same word reading one word at a time, so an assignment that ends its statement is one word there too: `MSG="chore: rm -rf build dir" && git commit -m "$MSG"` is no longer denied by `Bash(rm -rf *)`. Unix only, and skipped where there is no bash. What a process substitution carries can only be compared where a bounded read is available, and `timeout` is coreutils, which macOS does not ship — so the oracle checks for it and drops the two `<( )`/`>( )` cases when it is missing, rather than comparing two empty readings and calling them equal. Every trace is also required to be non-empty, because a case whose programs are all unstubbed records nothing on either side and would otherwise pass while testing nothing. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
KuSh
force-pushed
the
lex/08-exec-oracle
branch
from
November 25, 2026 02:12
3554ef3 to
e1b1c3f
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Every test of the rewriter compares it against an expectation somebody wrote
down. A mistake in the author's model of shell grammar therefore sits on both
sides of the comparison and agrees with itself — which is how a rewrite that
reached inside
$( )passed a full suite, and how a bracket counted on one sidebut not the other passed it again.
This asks bash instead. Recording stubs sit ahead of everything on
PATH, theoriginal command and its rewrite are each run, and what actually executed has to
match: which programs, with which arguments, in which directory. The
rtkstubruns the command it wraps and marks its output, the way the real one filters, so
a rewrite at a command position changes nothing that is compared while one that
reaches into a substitution changes the text the outer command was built from.
Three things had to be got right, none of which reading the code would have
told me:
echois a shell builtin, so a stub by that name is never reached. Theouter command of a substitution has to be one bash looks up on
PATH.order they are scheduled, so the comparison is over a set.
>( )hands over the writing end of a pipe. It opens but never delivers, soreading it waits for a writer that is waiting for this process to finish.
The oracle is held to detecting as well as passing. Alongside the equivalence
cases are rewrites wrong in ways this repository has actually shipped — reaching
into a substitution, into a process substitution, into a quoted assignment, and
turning one command into another — each of which it has to object to. And a
command RTK merely declined to rewrite runs identically, so it has to stay quiet
about that, or "everything differs" would satisfy the rest.
It found #3262 on its first run against the real rewriter, from a case nobody
had written an expectation for.
D='# shellcheck disable=SC2034'; echo hicameback as
D='# rtk shellcheck disable=SC2034'; echo hi: RTK edited inside aquoted literal, so the variable held text nobody wrote.
The env prefix was matched with a pattern whose value alternation is
(?:"…"|'…'|[^\s]*)followed by\s+. A quoted value that is not followed by ablank fails that
\s+, the alternation backtracks to the unquoted branch, and[^\s]*takes the fragment up to the first space —D='#— leavingshellcheck disable=SC2034'to be read as the command. Three shapes share theone cause, and only the first was reported:
D='# shellcheck disable=SC2034'; echo hi -> prefix D='# command shellcheck …
D="x y"; echo hi -> prefix D="x command y"…
D='no trailing space' -> prefix D='no command trailing…
A quoted value is one word however many blanks it holds, so the prefix is now
read by scanning words with quote state rather than by matching a pattern: a
backslash escapes inside double quotes and is literal inside single ones, and a
word ends at the first blank that is not inside either. A word is an
assignment on bash's own terms: a name of letters, digits and
_not startingwith a digit, then
=or+=, in any case. The pattern took uppercase namesonly, so
foo=bar git statuswas never rewritten, andd='# shellcheck …'escaped the quoting fix above only by not being recognised at all.
1a=b,foo-bar=xand"foo"=xare commands to bash and stay commands here.sudois still never stripped (#146).
Four call sites read that prefix and each carried its own copy of the pattern,
which is why they had to change together or disagree. They now go through one
split_env_prefix, so there is one place to be right. It also retiresstrip_disabled_prefix's trick of recovering the prefix length by subtractingthe stripped string's length from the original's, which worked only because the
pattern happened to remove a prefix.
RTK_DISABLED=(andRTK_DISABLED+=) is one of those words, so a message that only mentions it,MSG='use RTK_DISABLED=1'; git status, disables nothing and draws no warning inrtk rewrite,rtk gainandrtk discover. An assignment with no command after it,RTK_DISABLED=1; git status, draws none either, and the command behind the;is rewritten.The permission gate reads the command behind each assignment through
split_env_word, so an assignment that ends its statement is one word there too:MSG="chore: rm -rf build dir" && git commit -m "$MSG"is no longer denied byBash(rm -rf *).Unix only, and skipped where there is no bash. What a process substitution
carries can only be compared where a bounded read is available, and
timeoutiscoreutils, which macOS does not ship — so the oracle checks for it and drops the
two
<( )/>( )cases when it is missing, rather than comparing two emptyreadings and calling them equal. Every trace is also required to be non-empty,
because a case whose programs are all unstubbed records nothing on either side
and would otherwise pass while testing nothing.
Test plan
cargo fmt --all && cargo clippy --all-targets && cargo test --all— 5913 passing, clippy clean, at this commit rather than only at the tip of the stack; the oracle also passes with notimeoutonPATH, as on macOS+=, and a lowercased='# shellcheck …', and for1a=b,=x,foo-bar=xand"foo"=xstaying commands — each checked against bash with a stub first; oracle rows show the widened rewrites run the same programs; the charset mutated narrower, looser and without+=is caught all three waysRTK_DISABLED=and a bareRTK_DISABLED=1;draw no warning fromrtk rewrite, checked on stderr; a deny rule onrm -rf *does not fire on a commit message that contains itPart of #4315 (#4129 → #4138). Based on #4135. Each commit gates green on its own, so the stack is bisectable.
🤖 Generated with Claude Code