Skip to content

fix(discover): count every line of a multi-line call - #5024

Draft
KuSh wants to merge 1 commit into
feat/4315-1-cmdline-walkerfrom
fix/report-reads-every-line
Draft

KuSh wants to merge 1 commit into
feat/4315-1-cmdline-walkerfrom
fix/report-reads-every-line

Conversation

@KuSh

@KuSh KuSh commented Nov 5, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

rtk discover and rtk session read a multi-line call as one command and classified only its first line. git status\ngit log counted one command, and cd x\ngit status offered no saving at all, although the rewrite acts on each line.

The report's commands are now the rewrite's own. One walk over the rewrite's lex, cut_line, decides every boundary, and both the rewrite and split_command_chain_parts take their segments from it. A block is read line by line as the rewrite reads it: a line is a command, a trailing operator joins the next line, and a blank or comment-only line is no command. Each command of a call is read once, for its share of the output and for its row.

Built on #4402. It uses that PR's walker.

Behaviour changes

  • total_commands and session adoption now count every line, including lines rtk doesn't handle, so a multi-line setup script lowers a session's adoption.
  • A group's own words (do, done, fi, }) and the redirects after a closing word or a subshell's ) are no command. A comment alone is no command, so a call of comments counts none.
  • Every stage after a pipe reads it, and so does every command of a group that stage opens: tail in git log | (head -5; tail -5) and in git log | while read l; do tail -1; done is a consumer. A case pattern belongs to no command.
  • A call has one measured output. Its commands now share it by category average, and the result is marked estimated. A call of one command takes it whole. A command with no rule weighs less than any category, so in git status && echo ok and cargo test; echo done the command with a rule keeps most of the measurement. On develop the last command takes it all, and the number is lost when that command is an echo.
  • The hook's whole-line checks are worked out once per call instead of once per command, so a long block costs its length rather than its square.
  • rewrite_command reads a backquote as a substitution, as it reads $( ): nothing inside one is a boundary. A heredoc with an fd number in front (cat 0<<EOF) is one text that is not rewritten. The hook's answer doesn't change, because it already leaves any substitution or heredoc alone.

Known limits

  • A block the rewrite refuses to read line by line, such as one with a subshell or a loop spread over lines, stays one command, as the rewrite leaves it whole. Its lines are counted once the rewrite reads such blocks.
  • The commands of a function body take a share of the output like any other, although defining the function runs none of them.

Test plan

  • cargo fmt --all, cargo clippy --all-targets, cargo test --all
  • Each guard fails a test when removed
  • rtk rewrite output identical before and after on 2,492 recorded calls, a 325-shape sweep and 63 hand shapes
  • rtk discover compared call by call on 2,954 recorded calls; each of the 80 differences is a change or a limit listed above, or already happens on the base

🤖 Generated with Claude Code

@KuSh
KuSh marked this pull request as draft November 5, 2026 23:42
@rtk-wshm-sync-bot

Copy link
Copy Markdown

wshm · Automated triage by AI

📊 Automated PR Analysis

🐛 Type bug-fix
🟡 Risk medium

Summary

Fixes rtk discover and rtk session to count every line of a multi-line call instead of only the first line, by sharing a new block_lines helper with the rewrite logic so lines are split the same way the hook rewrites them. Adds a joined_or_as_written lexer helper, updates total_commands/session adoption counting, and documents known limits (CRLF blocks, quoted newlines, comment-only blocks, quadratic cost on very long blocks).

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 fix/report-reads-every-line branch 2 times, most recently from 5fda834 to dd2e469 Compare November 8, 2026 17:13
@KuSh
KuSh force-pushed the fix/report-reads-every-line branch from dd2e469 to ecf9213 Compare November 13, 2026 17:09
@KuSh
KuSh changed the base branch from lex/14-grammar-tables to feat/4315-1-cmdline-walker November 13, 2026 17:09
@KuSh
KuSh added this pull request to stack #4139 November 15, 2026 23:02
@KuSh
KuSh force-pushed the fix/report-reads-every-line branch from ecf9213 to 4958129 Compare November 17, 2026 01:42
@KuSh
KuSh force-pushed the fix/report-reads-every-line branch 3 times, most recently from d686902 to 2ccb71f Compare November 21, 2026 09:57
@KuSh
KuSh force-pushed the fix/report-reads-every-line branch 2 times, most recently from 510f28a to 4443ba3 Compare November 25, 2026 02:13
@KuSh
KuSh force-pushed the fix/report-reads-every-line branch 2 times, most recently from 48f85b4 to ff8f0fb Compare November 28, 2026 01:23
`rtk discover` and `rtk session` read a multi-line call as one command, so only its
first line was classified: `git status\ngit log` counted one command, and
`cd x\ngit status` none it could offer a saving on, although the rewrite acts on each
line. A session's adoption now also counts the lines rtk does not handle.

The report's commands are now the rewrite's own: one walk over the rewrite's lex,
`cut_line`, decides every boundary, and both the rewrite and
`split_command_chain_parts` take their segments from it. A block is read line by line
as the rewrite reads it: a line is a command, a trailing operator joins the next
line, a blank or comment-only line is no command. The `\r` of a CRLF is the last byte
of its line, in its last word or its comment, as bash and the rewrite read it. A block
the rewrite refuses to read line by line, such as one with a subshell or a loop spread
over lines, stays one command, as the rewrite leaves it whole; its lines are not counted
one by one, a known limit until the rewrite reads such blocks. A pipe or an operator
inside `$( )`, `<( )` or `>( )` belongs to the command that substitution runs, in a
pipeline's stages too. A heredoc with an fd number in front
(`cat 0<<EOF`) is a heredoc too. Each command of a call is read once, for its share of
the output and for its row.

A group's own words are no command: neither `do` or `then` alone, nor a closing `}`,
`done`, `fi` or `esac` with the redirects after it, which belong to the commands inside
the group. Nor is a comment alone, so a call of comments counts no command. A stage
after a pipe reads it whatever stands in front of the pipe, so `tail` in
`{ git status; } 2>&1 | tail -1` is a consumer. A group a later stage opens, a subshell,
a `{ }` group or a `while`, `for`, `if` or `case`, is cut into its commands, which all
read the pipe, and the pipeline goes on past it, so `tail` in
`git log | (head -5; tail -5)` and in `git log | while read l; do tail -1; done` is a
consumer too, and a `case` pattern there belongs to no command, as anywhere. The `)` of a
subshell a pipeline stands in ends that pipeline. The rewrite leaves any line with a pipe
and a bracket as written, and the commands of a compound a pipe joins as they are, so
neither change reaches its answer.

A call has one measured output, which went to its last command and was lost when that
was an `echo`. Which command wrote what of it cannot be told apart, so the call's
commands now share it by category average, marked estimated, and a call of one command
takes it whole. A command with no rule is most often a builtin or a small tool, so it
weighs less than any category: in `git status && echo ok` and `cargo test; echo done`,
the command with a rule keeps most of the measurement. A command that runs rtk weighs
the average of any command, since the classifier leaves it aside although it runs a
tool. A function body's commands take a share like any other, although defining the
function runs none of them; that is a known limit of this estimate.

The hook's checks of a call's whole line are worked out once per call instead of once
per command, so a call of many commands costs its length rather than its square.

`rewrite_command`, from which the report reads which commands the rewrite reached,
changes in two ways. A backquote is a substitution, as `$( )` is: nothing in one is a
boundary, so `ls `a | b`` is one command, and a block with a newline inside one is no
longer read line by line. A heredoc with an fd number in front (`cat 0<<EOF`,
`cat 0<<<x`) is one text that is not rewritten, as one without it already was. The
hook's answer does not change, as it leaves any substitution or heredoc alone before it
asks for a rewrite.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@KuSh
KuSh force-pushed the fix/report-reads-every-line branch from ff8f0fb to 601eb52 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