Skip to content

fix(discover): report from the rewrite's decisions - #5112

Draft
KuSh wants to merge 4 commits into
fix/report-reads-every-linefrom
fix/report-from-rewrite-decisions
Draft

KuSh wants to merge 4 commits into
fix/report-reads-every-linefrom
fix/report-from-rewrite-decisions

Conversation

@KuSh

@KuSh KuSh commented Nov 14, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

rtk discover and rtk session worked out what the hook does with a command by reading it a second time: a text diff of the rewrite's output to find what it reached, a rewrite of the command alone to estimate coverage, a shallow scan for RTK_DISABLED= and the text rebuilt without it, a classifier of the command as written, and a second lookup of its rule. Each reader could disagree with the rewrite, and the report then said something the hook does not do.

The report now asks the rewrite. A call is lexed once, and its commands, the edits that reach them, the bypass counterfactual, each command's name and class, and the rule it matched all come from the rewrite's own decision over that lex (read_call, decide_segment). rtk rewrite and the hook are unchanged.

Built on #5024.

Commits

  • refactor(discover): read the commands the rewrite reaches off its edits: a command is reached when an edit of the call's rewrite falls in it. No behaviour change.
  • fix(discover): answer the hook's coverage from the call's own rewrite: coverage and the RTK_DISABLED= counterfactual come from the call's rewrite, with the bypass run read past.
  • fix(discover): read each command with the walk the rewrite runs: each command is named, classified and sized as what the rewrite's decision settles on, and rtk session counts it the same way.
  • refactor(discover): keep the rule a command matched: the classification carries the rule's index, so the rewrite never looks it up again. No behaviour change.

Behaviour changes

  • A command behind a wrapper or keyword is the command it runs: timeout 30 cargo test, time cargo test and the body of a then or do are cargo test, where they were an unsupported timeout 30 row or no row.
  • An rtk behind assignments or wrappers is adoption: FOO=1 rtk git status, timeout 5 rtk cargo test, uv run rtk pytest.
  • A row behind plain assignments merges with the plain command's row: FOO=1 pytest is pytest.
  • An RTK_DISABLED= bypass is found behind the wrappers the rewrite reads through: RTK_DISABLED=1 timeout 5 git status, RTK_DISABLED=1 time cargo test.
  • A bypassed command the rewrite would leave raw where it sits is no longer counted as a bypass: cargo test | RTK_DISABLED=1 tail -n 5 f.log.
  • RTK_DISABLED+=1 git status is a bypass, as the rewrite's refusal reads it.
  • The commands of a line the hook cannot attest are opportunities, not missed savings: git log > out, git status; echo $(date). A logged rewrite of such a line still counts as covered.
  • Under the user's transparent_prefixes and exclude_commands, a command is read as the hook reads it: nice -n 19 ionice git status under that prefix is git status.
  • env -i git status is an env row, where it was a row named -i git.
  • rtk session counts a command as covered only where the hook would rewrite it: tail -f log 1/1 → 0/1, cat file | tail -5 2/2 → 0/2, RTK_DISABLED=1 git status 1/1 → 0/1, timeout 5 cargo test 0/1 → 1/1. It reads the same configuration as discover.

Known limits

  • uv run RTK_DISABLED=1 pytest, which the rewrite refuses, is still an rtk uv opportunity.
  • uv run bash -c 'pytest' is read as bash -c, which has no rule, like bash -c 'pytest' alone, although the rewrite edits inside the quotes.
  • Under the transparent prefix nohup, nohup RTK_DISABLED=1 git status is a bypass named as written.
  • rtk session does not read the hook's logged decisions; rtk discover does.

Test plan

  • cargo fmt --all, cargo clippy --all-targets, cargo test --all (6351 passed)
  • rtk rewrite and rtk hook claude output identical before and after on about 57,000 shapes under six configurations
  • rtk discover and rtk session compared call by call, each commit against its parent (up to 9,828 recorded and generated calls, with and without the hook, the user prefixes and the exclusions); every difference is a change listed above
  • Over about 3.5 million generated combinations of wrappers, commands, prefixes and exclusions, every edit the rewrite makes lies inside the command the report reads
  • Each guard fails a test when removed (40 mutants on the reading, plus the bypass, measured-row and rule-index guards)
  • Hook latency unchanged (hyperfine, release builds)

🤖 Generated with Claude Code

@rtk-wshm-sync-bot

Copy link
Copy Markdown

wshm · Automated triage by AI

📊 Automated PR Analysis

🐛 Type bug-fix
🟡 Risk medium

Summary

Refactors rtk discover and rtk session to derive their reported command classification, coverage, and bypass detection directly from the rewrite engine's own decisions (read_call, decide_segment) instead of re-deriving them with separate, independent logic that could disagree with what the hook actually does. This fixes numerous discrepancies where wrapped commands (timeout, time, env, assignments, wrappers) or piped commands were misclassified or miscounted relative to actual hook beha…

Review Checklist

  • Tests present
  • Breaking change
  • Docs updated

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

@KuSh
KuSh added this pull request to stack #4139 November 15, 2026 23:02
@KuSh
KuSh force-pushed the fix/report-from-rewrite-decisions branch 2 times, most recently from 2f59ab9 to 4b7e110 Compare November 18, 2026 01:06
@KuSh
KuSh force-pushed the fix/report-from-rewrite-decisions branch from 4b7e110 to f98cb90 Compare November 19, 2026 01:12
@KuSh
KuSh force-pushed the fix/report-from-rewrite-decisions branch from f98cb90 to 8c8e9e3 Compare November 21, 2026 09:57
@KuSh
KuSh force-pushed the fix/report-from-rewrite-decisions branch from 8c8e9e3 to de9a813 Compare November 23, 2026 00:14
@KuSh
KuSh force-pushed the fix/report-from-rewrite-decisions branch from de9a813 to 4c99980 Compare November 25, 2026 02:13
@KuSh
KuSh force-pushed the fix/report-from-rewrite-decisions branch from 4c99980 to a92dca7 Compare November 27, 2026 22:44
@KuSh
KuSh force-pushed the fix/report-from-rewrite-decisions branch from a92dca7 to 6298796 Compare November 28, 2026 01:23
KuSh and others added 4 commits November 29, 2026 02:22
`rtk discover` learned which commands of a call the rewrite acts on by rewriting the
whole line, cutting the rewritten text into commands again, and comparing each
command's text with the one before. That is a second reading of the call, and it gave
up on the whole line when the two cuts had a different number of commands.

The rewrite records each change it makes as an edit, a span of the line it lexed, and
the report's commands are cut by the same walk over the same lex. The report now asks
the rewrite once, over that lex, and a command is reached when an edit falls in it. An
edit is placed by where it starts among the commands' own offsets, not by its rank:
a command the rewrite passes over has no edit, and nor has a stage in the middle of a
pipeline. A call that could not be joined is not rewritten and reaches nothing, as the
hook leaves it.

`rewrite_command` and the hook are unchanged: they run the same rewrite over the same
lex and tag nothing. No answer of the report changes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
`rtk discover` decided whether the hook covers a command by rewriting text it had
rebuilt: the command alone for the estimate, and for a command behind `RTK_DISABLED=`
the command with the bypass words cut out of its text. Each is a second reading of the
call, run outside it, so its answer could disagree with what the hook does with the
call as a whole.

The report now asks the rewrite it already runs over the call's one lex.

- The estimate of whether the hook covers a command is that an edit of the call's
  rewrite falls in it and the permission checks of the whole line leave the hook free to
  rewrite. A pipeline consumer the rewrite leaves raw no longer estimates as covered;
  no row moves from that alone, as the report already left such a consumer out.
  The measured `hook_decisions` path is unchanged.
- The walk's `Stop::Disabled` carries the word the `RTK_DISABLED=` run starts at. A
  call with a command the rewrite refused there, in its first walk or in a walk of the
  search it fell back to, gets one more rewrite over the same lex, with every run
  the call's own walks stopped at read past, and that command would have been covered
  without the bypass when an edit then falls in it, in its own place in the call. A
  quoted script keeps its own runs, as the hook would meet them. A run is read
  as the words left when its `RTK_DISABLED` ones are taken out: none when it held no
  others, so the walk goes on from where it stood in front of it
  (`RTK_DISABLED=1 time -f %e cargo test` reads `time` as the reserved word), and an
  ordinary assignment layer when it did. A call with no such run is rewritten once;
  `rtk rewrite` and the hook are unchanged in output, and the only work they add is
  noting where a refused command's run starts.
- `disposition` no longer takes whether the command is bypassed. A bypassed command
  that is not counted as a bypass is judged like the same command without the
  assignment, with the rewrite's answer for it without the bypass, and as not covered:
  it ran raw, whatever a measured `hook_decisions` row says of the call.
- `rewrite_command_precompiled`, `estimate_hook_coverage` and
  `would_be_covered_without_bypass` lose their last caller and go.

What the report answers differently:

- A bypassed command the rewrite would leave raw where it sits is no longer counted as
  a bypass: a consumer at the end of a pipeline (`cargo test | RTK_DISABLED=1 tail -n 5
  f.log`), and a producer when the rewrite takes the last stage (`RTK_DISABLED=1 cat
  f.log | grep ERROR`), when a consumer is not safe to leave (`RTK_DISABLED=1 git
  status | tee out.txt | head`) or when a consumer carries a bypass of its own.
- Such a command, and a bypassed command that `exclude_commands` names, moved from the
  missed savings to where the plain command goes: an opportunity, or a consumer the
  report drops. A bypassed command the rewrite would take, which the hook's checks of
  the line refuse or no hook is installed for, stays a missed saving, whatever a
  measured row says of the call.
- `RTK_DISABLED+=1 git status` is a bypass, as the rewrite's refusal reads it, and
  when it is no bypass its row is named `git status`: the words taken out of a bypassed
  command are the ones that refusal reads, not only those spelled `RTK_DISABLED=`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
`rtk discover` and `rtk session` classified a command as written, and found an
`RTK_DISABLED=` bypass and the command behind it with a shallow scan of their own, while
the rewrite reads a command under its assignments, keyword prefixes and process wrappers.
`timeout 30 cargo test` was an unsupported `timeout 30`, `time cargo test` and the body of
a `then` or `do` were left out, `FOO=1 rtk git status` was no adoption, and a bypass in
front of a wrapper (`RTK_DISABLED=1 timeout 5 git status`) was not found. The report
read each call twice, with a reader that could disagree with the rewrite.

The rewrite's decision about one command (the walk of its layers, the command it stops
at, the search for a fall-through behind them, refusals and exclusions) is one function,
`decide_segment`, and its outcome carries the word it settles at. The rewrite applies the
edit; the report reads the command from that word. `read_call` makes the decision once
for each command of the call, under the same `exclude_commands` and `transparent_prefixes`,
for one the rewrite never visits as well (a stage in the middle of a pipeline, a producer
behind a taken last stage). The command is therefore the one the hook acts on: under every
layer the walk reads through (`timeout 30 cargo test` is `cargo test`), the one behind a
user prefix (`nice -n 19 ionice git status` under that prefix is `git status`), a routable
wrapper the search matched whole (`uv run python x.py`), and an `rtk` the rewrite keeps
(`FOO=1 rtk git status`). Its row name, classification, output share and whether it runs
rtk all come from it. Where nothing settles (no rule, the command excluded, a bypass
refused) it is the command the walk the decision ended in stops at: the first walk's, or
the furthest one the search read behind a user prefix, when it found a command
(`nice -n 19 ionice frobnicate x` under that prefix is `frobnicate x`, as `timeout 30
frobnicate x` is). Where that is no command (an option, a redirect, grammar, or an
assignment behind a wrapper: `env -i git status`, `command -v cargo`, `exec >log 2>&1`,
`timeout 5 [[ -f x ]]`), it is the last layer's, as written, so the call keeps its row,
unless that layer is a reserved word (`time -f %e cargo test`, `if -v cargo`): that is
bash's grammar, and has no row. Assignments alone run nothing, and a walk that reaches
its depth limit files the whole part. For a command the rewrite refuses at an
`RTK_DISABLED=` run, in its walk or in a search for a fall-through, the decision is made
again with the run read past: the bypass bucket's name is the command it would run
without the bypass, and a bypass needs a rule, as before.

`rtk session` counts a command as adopted where it runs rtk, or where it has a rule and
the rewrite reaches it on a line the hook can attest, read the way discover reads it and
under the same `exclude_commands` and `transparent_prefixes`, rather than wherever a rule
matches.

`strip_disabled_prefix_for_analytics`, `without_rtk_disabled`, `is_already_rtk`, the
report's own text classifier, `ChainPart`'s `runs_rtk` and the rewrite's record of the
runs it refused lose their last caller and go. `rtk rewrite` and the hook are unchanged.

What the reports answer differently:

- A command behind a wrapper or keyword is the command it runs: `timeout 30 cargo test`,
  `time cargo test`, `nohup`, `command`, `exec`, `noglob` and the commands after `then`,
  `do` or `if` are classified, named and sized as theirs, and counted as covered where the
  rewrite reaches them, instead of an unsupported wrapper row or no row. A command behind
  a user prefix is the command behind it. `uv run pytest` is `pytest`, covered, where it
  was an `rtk uv` row; `uv run bash -c 'pytest'` is `bash -c`, which has no rule, as
  `bash -c 'pytest'` alone, so it is no longer counted covered through the wrapper's rule.
  `uv run RTK_DISABLED=1 pytest`, which the rewrite refuses, is still an `rtk uv`
  opportunity, as before.
- An `rtk` behind assignments or wrappers is adoption (`FOO=1 rtk git status`,
  `timeout 5 rtk …`, `uv run rtk pytest`, and behind `RTK_DISABLED=` too, which rtk does not
  read); `rtk bypass` still is not. Behind a wrapper (`timeout 5 rtk cargo test`, `command`,
  `nohup`, `uv run rtk …`) it weighs the average of any command in its call's output share,
  as behind assignments or `env`, and no longer the weight of a command with no rule, so
  the share of the call's other rows shifts. A command behind assignments is no longer
  exempt from the commands the report leaves out (`FOO=1 echo hi`).
- A row behind plain assignments is named by the command and merges with the row of the
  unprefixed command: `FOO=1 pytest` is `pytest`, `FOO=1 git status 2>&1 | tail -3` is
  `git status` (it was `FOO=1 git`), `FOO=1 gh pr view 1 --json title` is `gh pr` (it was
  `FOO=1 gh`).
- A command that is none keeps a row, as the layer that owns it: `env -i git status` and
  `env -u FOO git status` are an `env` row, `FOO=1 env -u BAR cargo test` a `FOO=1 env`
  row, where they were rows named `-i git`, `-u FOO` and `-u BAR`. A reserved word has
  none: `time -f %e cargo test` and `if -v cargo` are grammar, as before.
- Behind a transparent prefix of the user's, a command nothing settles is the command
  behind the prefix: `nice -n 19 ionice frobnicate x` under that prefix is a `frobnicate x`
  row, as `timeout 30 frobnicate x` is, and with `git` excluded, `nice -n 19 ionice git
  status` and `timeout 5 RTK_DISABLED=1 git status` under the prefix `timeout 5` are the
  `rtk git` opportunity a plain excluded `git status` is, where they were rows named
  `nice` and `timeout 5`.
- A bypass is found behind the wrappers the rewrite reads through (`RTK_DISABLED=1
  timeout 5 git status`, `nice`, `time`, `nohup`, `command`), and one the rewrite refuses
  through a user prefix: it was left out of the bypass bucket. `RTK_DISABLED=1 time cargo
  test` reaches it, named `cargo test`. One the walk does not read through (`sudo
  RTK_DISABLED=1 docker ps`, with `sudo` not a transparent prefix) is no bypass: it is the
  `sudo` command it is written as, row `sudo RTK_DISABLED=1` where it was `sudo docker`.
  A bypassed command with no rule is left out of the report, which now includes
  `RTK_DISABLED=1 sudo docker ps`, where it was a `sudo docker` row; one the rewrite takes
  with no rule (`RTK_DISABLED=1 stat f`, `RTK_DISABLED=1 bash -c 'git status'`) is left
  out as it was.
- The commands of a line the hook cannot attest (a substitution, a redirect that names a
  file) are opportunities, not missed savings, and so is a consumer the rewrite would take
  there; a copy or `/dev/null` names no file. A measured `hook_decisions` row that
  records a rewrite of the line still counts its commands as covered.
- `rtk session` counts a command as the hook leaves it: a wrapped command with a rule the
  rewrite reaches, `time cargo test`, is covered; one on a line the hook cannot attest,
  behind an `RTK_DISABLED=` (which counted as adopted before), or excluded is not. A
  ruled command the rewrite declines is no longer counted, a raw pipe producer
  included: `tail -f log` 1/1 → 0/1, `cargo test | tee out` 1/2 → 0/2, `git log
  --oneline | wc -l` 2/2 → 0/2, `cat file | tail -5` 2/2 → 0/2, and the `head` of `git log
  | head -5`. A command behind a transparent prefix of the user's is counted as the hook
  rewrites it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The classification of a command picked its rule by index, kept the rule's fields and
dropped the index, and the rewrite's decision then found the rule again with a scan on its
`rtk_cmd` (`decide`, and `tool_form` in the check against `exclude_commands`). For `rtk uv`
and `rtk php`, which are two rules each, that scan returned the first of the pair, which
for the second rule's commands (`uv sync`, `php -l`) is not the one that matched; it was
harmless only because a test held the pair's `rewrite_prefixes` and `pipeline_safety`
equal.

`Classification::Supported` now carries the index of the rule that matched, and `decide`
and `tool_form` read the rule from it. The rewrite no longer looks a rule up by its
`rtk_cmd`, so the test that held the siblings equal goes, and a new test pins the index: it is the
rule whose pattern matched, the last of several that do, and each of two sibling rules for
its own commands.

No answer of `rtk rewrite`, the hook, `rtk discover` or `rtk session` changes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@KuSh
KuSh force-pushed the fix/report-from-rewrite-decisions branch from 6298796 to a065a2a Compare November 29, 2026 01:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant