Skip to content

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
lex/07-subshell-boundaryfrom
lex/08-exec-oracle
Open

KuSh wants to merge 1 commit into
lex/07-subshell-boundaryfrom
lex/08-exec-oracle

Conversation

@KuSh

@KuSh KuSh commented Sep 19, 2026 •

Copy link
Copy Markdown
Collaborator

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 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. 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 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.

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 no timeout on PATH, as on macOS
  • the oracle is required to object to four rewrites that are wrong in ways this repository has shipped, and to stay quiet about one that merely skipped; the three shapes of bug(hook): rewrite edits inside quoted string literals — 'shellcheck' in a single-quoted sed replacement became 'rtk shellcheck' (data corruption) #3262 are pinned
  • the assignment rule is bash's: split and rewrite rows for any case, +=, and a lowercase d='# shellcheck …', and for 1a=b, =x, foo-bar=x and "foo"=x staying 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 ways
  • a message that only mentions RTK_DISABLED= and a bare RTK_DISABLED=1; draw no warning from rtk rewrite, checked on stderr; a deny rule on rm -rf * does not fire on a commit message that contains it

Part of #4315 (#4129 → #4138). Based on #4135. 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
@rtk-wshm-sync-bot rtk-wshm-sync-bot Bot added bug Something isn't working discover parsing tests labels Sep 19, 2026
@rtk-wshm-sync-bot

Copy link
Copy Markdown

wshm · Automated triage by AI

📊 Automated PR Analysis

🐛 Type bug-fix
🟡 Risk medium

Summary

Replaces a regex-based ENV_PREFIX pattern for stripping leading env/VAR=value assignments with a word-scanning parser (split_env_prefix) that correctly treats quoted values as single words, fixing a bug where quoted assignments containing ; or trailing content caused the rewriter to misparse and edit inside string literals. Adds a new exec-equivalence test suite and unit tests covering quoted-value edge cases.

Review Checklist

  • Tests present
  • Breaking change
  • Docs updated

Linked issues: #3262, #146


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

@KuSh
KuSh marked this pull request as ready for review September 19, 2026 16:21
@KuSh
KuSh force-pushed the lex/08-exec-oracle branch from a2f2e6e to ce54424 Compare September 19, 2026 21:14
@KuSh
KuSh force-pushed the lex/08-exec-oracle branch from ce54424 to d4e455d Compare September 20, 2026 23:23
@KuSh
KuSh force-pushed the lex/08-exec-oracle branch from d4e455d to 4a9c682 Compare September 25, 2026 22:55
@KuSh
KuSh force-pushed the lex/08-exec-oracle branch 3 times, most recently from 17286d0 to f842f9d Compare September 25, 2026 23:29
@KuSh
KuSh force-pushed the lex/08-exec-oracle branch from f842f9d to b8a305d Compare September 26, 2026 17:56
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
KuSh force-pushed the lex/08-exec-oracle branch 7 times, most recently from 7cf771a to 434109f Compare October 2, 2026 15:07
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
KuSh force-pushed the lex/08-exec-oracle branch from 54fb2d3 to 401d7ff Compare October 15, 2026 12:03
@KuSh
KuSh force-pushed the lex/08-exec-oracle branch 3 times, most recently from 8abe23d to 413ac70 Compare October 26, 2026 11:29
@KuSh
KuSh force-pushed the lex/08-exec-oracle branch 2 times, most recently from fdd23a6 to 05ef594 Compare October 28, 2026 23:54
@KuSh
KuSh force-pushed the lex/08-exec-oracle branch 2 times, most recently from d1c70e0 to a08ffff Compare November 1, 2026 15:19
@TaKO8Ki
TaKO8Ki force-pushed the lex/08-exec-oracle branch from a08ffff to 2cc8469 Compare November 2, 2026 11:31
@KuSh
KuSh force-pushed the lex/08-exec-oracle branch 2 times, most recently from 0f0383a to c3b24db 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/08-exec-oracle branch from c3b24db to 6fb1053 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/08-exec-oracle branch from 6fb1053 to acf63e4 Compare November 17, 2026 01:42
@KuSh
KuSh force-pushed the lex/08-exec-oracle branch from acf63e4 to 5a8adf3 Compare November 18, 2026 01:06
@KuSh
KuSh force-pushed the lex/08-exec-oracle branch from 5a8adf3 to bc71650 Compare November 19, 2026 01:12
@KuSh
KuSh force-pushed the lex/08-exec-oracle branch from bc71650 to 3554ef3 Compare November 21, 2026 09:57
…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
KuSh force-pushed the lex/08-exec-oracle branch from 3554ef3 to e1b1c3f Compare November 25, 2026 02:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working discover parsing tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant