Skip to content

refactor(discover): rewrite a segment by lexing once and editing spans - #4400

Open
KuSh wants to merge 1 commit into
lex/12-segment-editsfrom
lex/13-span-edits
Open

KuSh wants to merge 1 commit into
lex/12-segment-editsfrom
lex/13-span-edits

Conversation

@KuSh

@KuSh KuSh commented Oct 2, 2026 •

Copy link
Copy Markdown
Collaborator

The rewrite lexes a line once and expresses each decision as edits on the original bytes.

  • One CompoundLex per line. The heredoc refusal, the already-rtk check, segments, pipeline stages and the words each decision checks all read its tokens.
  • A walk peels a segment's transparent layers in one loop, and each Layer records at peel time what later steps read.
  • A decision is a list of Edits (Insert, Replace) that apply_edits in core::cmdline::edit applies; every byte outside an edit is emitted as written.
  • The fix(uv): remove uv run from transparent prefixes to fix rewrite conflicts #2768 fall-through skips the (position, depth) pairs it already tried, so a chain of uv run stays linear.

Behaviour changes:

  • Under a user prefix that begins with a quoted shell wrapper's words (sh -c "), words after the wrapper's closing quote are no longer rewritten. Bash passes them to the script as arguments: sh -c " sh -c " exec uv run … never runs uv.
  • A comment on a line of a multi-line script behind a user prefix is given back, as on any other line.
  • head or tail with a redirect glued to a control byte (head -5 f 2>&1\x0b) is not rewritten, since that byte is part of the redirect's target.

Otherwise rtk rewrite stdout, stderr and exit code, with and without a catch-all project filter, and the hook's verdicts are byte-identical to the parent.

Test plan

  • cargo fmt --all --check && cargo clippy --all-targets -- -D warnings && cargo test --all: 6106 passing, clippy clean, at this commit
  • wincheck.sh (cfg(windows) type-check) clean
  • Hook verdicts compared with the parent commit over 1,274,141 commands: none newly allowed
  • Every allowed rewrite that differs from the parent's (184) run in bash in a container next to the original line: the same commands run, with the same output and exit code
  • Rewrite output byte-identical to the parent on the sweep corpus, except 528 cases, all the changes above

Part of stack #4139. Based on #4341. Each commit gates green on its own, so the stack is bisectable.

🤖 Generated with Claude Code

@rtk-wshm-sync-bot

Copy link
Copy Markdown

wshm · Automated triage by AI

📊 Automated PR Analysis

♻️ Type refactor
🟡 Risk medium

Summary

This PR refactors the segment rewrite logic in rtk's discover module to lex each line once into a CompoundLex and express rewrites as a list of Edit operations (Insert/Replace) applied to the original byte spans, rather than re-tokenizing and reassembling strings at each step. It also improves the #2768 fall-through logic to skip already-tried (position, depth) pairs for linear performance on chains like repeated uv run. No behavioral change is intended, verified via extensive testing includi…

Review Checklist

  • Tests present
  • Breaking change
  • Docs updated

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

@KuSh
KuSh force-pushed the lex/13-span-edits branch 2 times, most recently from e1f4173 to 384e98b Compare October 5, 2026 22:36
@KuSh
KuSh force-pushed the lex/13-span-edits branch from 384e98b to dfa62c8 Compare October 7, 2026 07:16
@KuSh
KuSh force-pushed the lex/13-span-edits branch 2 times, most recently from c3b901a to 502b8e2 Compare October 9, 2026 16:27
@KuSh
KuSh force-pushed the lex/13-span-edits branch from 502b8e2 to 6855789 Compare October 9, 2026 20:29
@KuSh
KuSh force-pushed the lex/13-span-edits branch from 6855789 to e789e06 Compare October 15, 2026 00:24
@KuSh
KuSh force-pushed the lex/13-span-edits branch 2 times, most recently from 454a6b0 to ee36440 Compare October 19, 2026 16:38
@KuSh
KuSh force-pushed the lex/13-span-edits branch from ee36440 to 4907c60 Compare October 21, 2026 00:03
@KuSh
KuSh force-pushed the lex/13-span-edits branch 2 times, most recently from 59d6cd8 to 25788d7 Compare October 29, 2026 13:16
@KuSh
KuSh force-pushed the lex/13-span-edits branch from 25788d7 to 19b5338 Compare October 31, 2026 16:42
@KuSh
KuSh force-pushed the lex/13-span-edits branch from 19b5338 to 9a1df5d Compare November 1, 2026 16:42
@KuSh
KuSh force-pushed the lex/13-span-edits branch from ef1d545 to c2950bb Compare November 3, 2026 00:58
@KuSh
KuSh force-pushed the lex/13-span-edits branch from c2950bb to 793bcb4 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/13-span-edits branch from 793bcb4 to 8f05cdc Compare November 5, 2026 11:48
@KuSh
KuSh force-pushed the lex/13-span-edits branch from 8f05cdc to 59cfe26 Compare November 5, 2026 12:12
@KuSh
KuSh marked this pull request as ready for review November 5, 2026 12:22
@KuSh
KuSh force-pushed the lex/13-span-edits branch from 59cfe26 to bd6972e Compare November 17, 2026 01:42
@KuSh
KuSh force-pushed the lex/13-span-edits branch from bd6972e to d3e1063 Compare November 18, 2026 01:06
@KuSh
KuSh force-pushed the lex/13-span-edits branch 2 times, most recently from 2a7e011 to e2d0353 Compare November 21, 2026 09:57
@KuSh
KuSh force-pushed the lex/13-span-edits branch from e2d0353 to 5aad4cc Compare November 23, 2026 00:14
@KuSh
KuSh force-pushed the lex/13-span-edits branch 2 times, most recently from 66fd71d to 70fa994 Compare November 27, 2026 22:44
@KuSh
KuSh force-pushed the lex/13-span-edits branch from 70fa994 to df453b9 Compare November 28, 2026 01:23
The rewrite lexes a command line once, into a `CompoundLex`, and reads
everything else from those tokens: the heredoc refusal, whether a line
already runs through rtk, each line of a multi-line block, each segment and
pipeline stage, and the words and argv the decision checks (the program
word, golangci's global flags, cat's options, a pipeline consumer's flags,
grep's and rg's pattern files). A `Slice` is one command's run of tokens,
from its first word to the end of its last, so an escaped or quoted blank
that ends the last word stays in the command. The lexer exposes
`resolve_words` to the crate, so a command resolves its own words, and keeps
`resolve_word_text` to the `cmdline` module, where `rtk_invocation` reads a
command's first words with it. A line runs through rtk when
`rtk_invocation` says its words run rtk and no operator joins another
command to it.

A walk peels a segment's transparent layers left to right in one loop: an
env run, a built-in wrapper, a process wrapper, a user prefix, in that order
at each position, within the depth bound. The segment's words are found
once, and each step reads them from the position the walk reached. Each
`Layer` records at peel time what a later step reads: where it starts, and
for a routable wrapper, the span of the command it wraps past any
assignments, which is what its exclusion check matches. A prefix matched as
text can end inside a token, as a user prefix `x 'a` ends inside the quote
it opens; from there the walk reads a fresh lex of the rest of the command.
Every other layer ends where a token starts, and a token starts where the
lexer holds no quote, escape or word open, so the walk reads on through the
segment's own tokens and words.

When nothing is decided about the command under the layers, the #2768
fall-through retries each routable wrapper, innermost first, as a walk of
its own from the wrapper's position and depth. A walk depends on nothing
but that position and depth, so a pair already tried is skipped, which
keeps the search over a `uv run uv run ... uv run` chain linear in its
depth.

A decision is one edit against the line, from `core::cmdline::edit`:
`Insert { at, text }` puts `rtk ` in front of a command, and
`Replace { span, text }` renames one. Every byte outside an edit is emitted
as written. `apply_edits` takes the edits in position order, sizes its output
from the line and the edit texts, and returns `None` for an overlapping,
reversed, out-of-range or off-boundary span rather than panic.

The `RTK_DISABLED` warning looks for a heredoc in the command as written.

The rewrite output is byte-identical to its parent: `rtk rewrite` stdout,
stderr and exit code, with and without a catch-all project filter, and the
hook's verdicts.

A quoted `sh|bash|zsh|fish -c` script is rewritten the same way, one wrapper
deep: when the command a walk reaches is such a wrapper, its script is lexed and
rewritten into edits of its own, and the wrapper's bytes around it are kept.
A wrapper is the command wherever the walk reaches one, before any layer is
peeled there, as the parent read it first: a user prefix that begins with its
words (`sh -c "`) is not peeled off it. A wrapper is still reached at the
prefix-depth limit, where no further layer is peeled.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@KuSh
KuSh force-pushed the lex/13-span-edits branch from df453b9 to 15dc3c3 Compare November 29, 2026 01:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant