Skip to content

refactor(discover): write each piece of shell knowledge once - #4138

Open
KuSh wants to merge 1 commit into
lex/09-estimate-markerfrom
lex/10-consolidation
Open

KuSh wants to merge 1 commit into
lex/09-estimate-markerfrom
lex/10-consolidation

Conversation

@KuSh

@KuSh KuSh commented Sep 19, 2026 •

Copy link
Copy Markdown
Collaborator

Three things were written down more than once, and each duplicate cost a bug.

Where a substitution begins and ends was counted separately by the segmenter
and by the emitter. They walk the same tokens and have to reach the same answer,
and twice they did not: once over an operator inside a substitution, which the
segmenter knew was not a boundary and the emitter did not, so
echo $(git status && git log) was rewritten into the string it captures; once
over a bracket nested in a substitution, which the emitter counted and the
segmenter did not, so the closing ) went missing from the segment. Both were
of the shape "the other copy learned this and I did not". SubstitutionDepth
now holds that counting, in lexer.rs with the tokens and the other low-level
scanning, and registry.rs uses it. The emitter's four guarded match arms
become one skip above the match, because "nothing inside a substitution is a
boundary and nothing inside one is rewritten" is one statement rather than four.

Quoting was tracked by QuoteScan for most callers and hand-rolled by two
others: contains_substitution with its own in_single/in_double pair, and
word_end_from with its own Option<u8>. Both re-derived the same rules and
either could drift from the shared one. They now read QuoteScan.

QuoteScan itself used to step over an escaped byte without reporting it. That
suits the six callers asking about syntax — \( is not a bracket, \# is not a
comment, \' opens nothing — and it silently failed the one asking where words
are, for which an escaped byte is text like any other. A word made only of
escapes, such as a lone \', went unreported and vanished from the split,
taking the next word with it: FOO=bar \' BAZ=1 git status read as an env
prefix running through BAZ=1, making a real assignment part of the prefix
rather than a command's environment. Rather than have the caller rebuild what
the scan withheld, the scan now reports every byte and marks which are escaped,
and significant() is the word the six say. The choice sits at each call site
instead of inside the iterator, where it was a default nobody could see.

word_spans is the helper whose absence made a second scanner look necessary:
coalesce_words merges adjacent tokens, which suits argv and not the question
of where a word ends.

The report's two savings tables had the same columns, widths, totals line
and estimate note, written out twice and differing only in heading and in what
their total means. append_savings_table takes those two differences as
arguments. A column added to one can no longer go missing from the other, and
the four total_* methods that existed to feed the duplicated footers go with
them, since the shared writer sums what it is given.

The execution oracle is hardened alongside. It passed on any command whose
programs are all unstubbed — both traces empty, empty equals empty, success
reported having run nothing. Shown with head -5 f against
rtk read f --head-lines 999, a deliberately wrong line count, which passed. A
trace is now required to be non-empty. That also names what the oracle cannot
do: head -5 f becomes a different program with reordered operands, and "the
same programs ran" is not the test for that. And timeout is coreutils, which
macOS does not ship while CI runs there — absent, the stub's bounded read of a
/dev/fd failed and recorded an empty value on both sides, so the
process-substitution cases kept passing while comparing nothing. Availability is
decided once at setup and those cases are not claimed when it is missing.

No behaviour change beyond the escaped-word fix: the rendered report is the same
report, and the differential harness over 13,020 commands agrees with the frozen
walker as before.

Test plan

  • cargo fmt --all && cargo clippy --all-targets && cargo test --all — 5920 passing, clippy clean, at this commit rather than only at the tip of the stack
  • the rendered report is unchanged; reintroducing either duplicated copy, or letting an escaped byte act as syntax, is caught by a test

Part of #4315 (#4129 → #4138). Based on #4137. 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/10-consolidation branch from a6ebc5a to 28cf256 Compare September 19, 2026 21:14
@KuSh
KuSh force-pushed the lex/10-consolidation branch from 28cf256 to 1296a25 Compare September 20, 2026 23:23
@KuSh
KuSh force-pushed the lex/10-consolidation branch from 1296a25 to e90ef60 Compare September 25, 2026 22:55
@KuSh
KuSh force-pushed the lex/10-consolidation branch from e90ef60 to 7a1a303 Compare September 25, 2026 23:15
@KuSh
KuSh force-pushed the lex/10-consolidation branch 2 times, most recently from a68e00e to a4e60f8 Compare September 25, 2026 23:29
@KuSh
KuSh force-pushed the lex/10-consolidation branch from a4e60f8 to 50faaf8 Compare September 26, 2026 17:56
@KuSh
KuSh force-pushed the lex/10-consolidation branch 2 times, most recently from 0f544ae to e72bf70 Compare October 19, 2026 16:38
@KuSh
KuSh force-pushed the lex/10-consolidation branch 2 times, most recently from 51cc76b to bce37a3 Compare October 26, 2026 11:29
@KuSh
KuSh force-pushed the lex/10-consolidation branch 3 times, most recently from 5a3fb0d to 8e72f99 Compare October 31, 2026 16:42
@KuSh
KuSh force-pushed the lex/10-consolidation branch from 8e72f99 to 4ae034f Compare November 1, 2026 15:19
@TaKO8Ki
TaKO8Ki force-pushed the lex/10-consolidation branch from 4ae034f to ae5f10a Compare November 2, 2026 11:31
@KuSh
KuSh force-pushed the lex/10-consolidation branch from ae5f10a to 656de3d Compare November 3, 2026 00:58
@KuSh
KuSh force-pushed the lex/10-consolidation branch from 656de3d to 9f0c9e5 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/10-consolidation branch from 9f0c9e5 to 849c9a7 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/10-consolidation branch from 849c9a7 to 7056177 Compare November 17, 2026 01:42
@KuSh
KuSh force-pushed the lex/10-consolidation branch 2 times, most recently from f707648 to c17363d Compare November 19, 2026 01:12
@KuSh
KuSh force-pushed the lex/10-consolidation branch from c17363d to 97b6460 Compare November 21, 2026 09:57
Three things were written down more than once, and each duplicate cost a bug.

**Where a substitution begins and ends** was counted separately by the segmenter
and by the emitter. They walk the same tokens and have to reach the same answer,
and twice they did not: once over an operator inside a substitution, which the
segmenter knew was not a boundary and the emitter did not, so
`echo $(git status && git log)` was rewritten into the string it captures; once
over a bracket nested in a substitution, which the emitter counted and the
segmenter did not, so the closing `)` went missing from the segment. Both were
of the shape "the other copy learned this and I did not". `SubstitutionDepth`
now holds that counting, in `lexer.rs` with the tokens and the other low-level
scanning, and `registry.rs` uses it. The emitter's four guarded match arms
become one skip above the match, because "nothing inside a substitution is a
boundary and nothing inside one is rewritten" is one statement rather than four.

**Quoting** was tracked by `QuoteScan` for most callers and hand-rolled by two
others: `contains_substitution` with its own `in_single`/`in_double` pair, and
`word_end_from` with its own `Option<u8>`. Both re-derived the same rules and
either could drift from the shared one. They now read `QuoteScan`.

`QuoteScan` itself used to step over an escaped byte without reporting it. That
suits the six callers asking about syntax — `\(` is not a bracket, `\#` is not a
comment, `\'` opens nothing — and it silently failed the one asking where words
are, for which an escaped byte is text like any other. A word made only of
escapes, such as a lone `\'`, went unreported and vanished from the split,
taking the next word with it: `FOO=bar \' BAZ=1 git status` read as an env
prefix running through `BAZ=1`, making a real assignment part of the prefix
rather than a command's environment. Rather than have the caller rebuild what
the scan withheld, the scan now reports every byte and marks which are escaped,
and `significant()` is the word the six say. The choice sits at each call site
instead of inside the iterator, where it was a default nobody could see.

`word_spans` is the helper whose absence made a second scanner look necessary:
`coalesce_words` merges *adjacent* tokens, which suits argv and not the question
of where a word ends.

**The report's two savings tables** had the same columns, widths, totals line
and estimate note, written out twice and differing only in heading and in what
their total means. `append_savings_table` takes those two differences as
arguments. A column added to one can no longer go missing from the other, and
the four `total_*` methods that existed to feed the duplicated footers go with
them, since the shared writer sums what it is given.

The execution oracle is hardened alongside. It passed on any command whose
programs are all unstubbed — both traces empty, empty equals empty, success
reported having run nothing. Shown with `head -5 f` against
`rtk read f --head-lines 999`, a deliberately wrong line count, which passed. A
trace is now required to be non-empty. That also names what the oracle cannot
do: `head -5 f` becomes a different program with reordered operands, and "the
same programs ran" is not the test for that. And `timeout` is coreutils, which
macOS does not ship while CI runs there — absent, the stub's bounded read of a
`/dev/fd` failed and recorded an empty value on both sides, so the
process-substitution cases kept passing while comparing nothing. Availability is
decided once at setup and those cases are not claimed when it is missing.

No behaviour change beyond the escaped-word fix: the rendered report is the same
report, and the differential harness over 13,020 commands agrees with the frozen
walker as before.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@KuSh
KuSh force-pushed the lex/10-consolidation branch from 97b6460 to 311c61e 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