Skip to content

fix(ast-grep): route subcommands by the grammar ast-grep actually has - #4150

Merged
KuSh merged 1 commit into
rtk-ai:developfrom
KuSh:fix/4121-followup-subcommand-grammar
Oct 7, 2026
Merged

KuSh merged 1 commit into
rtk-ai:developfrom
KuSh:fix/4121-followup-subcommand-grammar

Conversation

@KuSh

@KuSh KuSh commented Sep 20, 2026 •

Copy link
Copy Markdown
Collaborator

rtk ast-grep routes by subcommand: anything that is not a run goes to the passthrough, because only run's plain output has the path:line:content shape the filter parses. The subcommand list and the table of options that may precede one had been written from recall, and do not match the tool.

Subcommands

ast-grep --help lists run scan test new lsp outline completions help. docs is not among them -- ast-grep docs answers error: unrecognized subcommand 'docs' -- so that entry only ever matched a path named docs and sent an ordinary search to the passthrough. Measured against the real binary on a docs/ directory with 40 matches:

output
before 40 lines, 2053 B -- raw ast-grep output
after 7 lines, 330 B (-84%), ending in [full output: rtk recall …]

outline and help were missing in the other direction: both were captured and run through the filter, and only the unparsed-output guard kept their output intact. They are in the list now.

Options

Ahead of a subcommand ast-grep accepts exactly three options -- -c/--config, -h/--help, -V/--version -- and only the first takes a value. --color is a subcommand option, and it moves out of the global table into run's, where the binary puts it. That move is inert, not a fix: on every line where the two spellings disagree, ast-grep rejects the invocation outright (ast-grep --color always lsp → error: unexpected argument '--color' found, rc=2). It is here because the table's job is to state ast-grep's grammar, and an entry the binary does not have makes the next reader trust the rest less.

run's table now carries the full arity from ast-grep run --help, gaining --kind/-k, --no-ignore, --inspect, --heading and -j. --json and --debug-query stay out of it: ast-grep takes their value only attached with =, so the tokenizer splits it off without an entry, and an entry would make them swallow the next argument -- including a --stdin, which has to force a passthrough.

Interactive runs

That same help text lists -i/--interactive, which needs the terminal while it runs for the reason --stdin does. It drives a full-screen session -- one prompt per match with a rewrite, a match browser without one -- which it writes to stdout while reading the answers from /dev/tty. Captured, the whole session including the prompt never reached the terminal, and ast-grep sat waiting for an answer to a question nothing had displayed. Measured under a pty, rtk ast-grep run -i -p 'compute($A)' -r 'calc($A)' docs had printed nothing while ast-grep was at Accept? [y]es/[↵]…; it now streams like the raw command. -U/--update-all applies without asking and is unaffected.

Recovery hint

The comment above the store decision said the decision was taken on the text the hint is already part of. It is not: writing the archive is what produces the path the hint names, so the margin is weighed against a fixed 256 B reserve before any hint exists. The comment now says that, and names the case the reserve does not cover -- a tee_directory long enough to outgrow it. The same paragraph appeared twice; the copy on the margin check stays.

docs/usage/FEATURES.md listed the phantom subcommand too, and said the count of cut lines stands alone when no hint is available, where the filter in fact always appends a fallback route. Both corrected.

Verification

Both tables are transcribed from ast-grep 0.45.3 (app-x86_64-unknown-linux-gnu release binary, run in Docker), not from recall. cargo fmt --all, cargo clippy --all-targets and cargo test --all are clean. The new arity test is mutation-checked: dropping an entry from either table makes it fail.

🤖 Generated with Claude Code

@KuSh
KuSh force-pushed the fix/4121-followup-subcommand-grammar branch from f725c6f to 9fd09f0 Compare September 20, 2026 18:53
@rtk-wshm-sync-bot rtk-wshm-sync-bot Bot added ast-grep bug Something isn't working cli labels Sep 20, 2026
@rtk-wshm-sync-bot

Copy link
Copy Markdown

wshm · Automated triage by AI

📊 Automated PR Analysis

🐛 Type bug-fix
🟡 Risk medium

Summary

Fixes rtk's ast-grep subcommand routing to match the real ast-grep 0.45.3 grammar: removes the phantom docs subcommand, adds missing outline/help subcommands, moves --color from global to run's option table, expands run's value-taking options, and forces passthrough for interactive -i runs (in addition to existing --stdin handling) since ast-grep prompts via /dev/tty. Also corrects an inaccurate code comment about the recovery-hint size decision and updates FEATURES.md docs to m…

Review Checklist

  • Tests present
  • Breaking change
  • Docs updated

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

@aeppling aeppling left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approve.

Verified against develop with an argv-recording stub, two output profiles (match-shaped and prose): zero argv and zero exit-code changes across 857 cases; the docs fix restores filtering on the common case (~60-84% reduction where the filter runs), the -i passthrough fixes a real hang, and prose-shaped output confirms outline/help were already passing through develop's unparsed-output guard, so nothing is lost there. Merged suite is green.

One comment: OTHER_SUBCOMMANDS is pinned at 0.45.3 and, unlike the two arity tables, its doc comment doesn't state the rot direction — an unlisted future subcommand falls through to the filter (capture), the costly direction. One sentence saying so (and that a path named like a listed subcommand deliberately passes through) would make the next version bump deliberate instead of silent.

`OTHER_SUBCOMMANDS` and the pre-subcommand option table were written from
recall rather than read off the binary. `docs` is not an ast-grep subcommand,
so `rtk ast-grep -p X docs` -- a search over a directory named `docs` -- was
sent to the passthrough and printed uncompressed. `outline` and `help` are
subcommands and were missing, so both were captured and only rescued by the
unparsed-output guard. `--color` is a subcommand option, not a global one.

Both tables now come from `ast-grep --help` and `ast-grep run --help` (0.45.3):
seven non-`run` subcommands, `-c/--config` as the only value-taking global
option, and `run`'s full arity including `--kind`, `--no-ignore`, `--inspect`,
`--heading` and `--color`. `--json` and `--debug-query` stay out of the table
because ast-grep only accepts their value attached with `=`, and an entry would
make them swallow the next argument -- including a `--stdin`, which has to force
a passthrough.

That same help text lists `-i/--interactive`, which has to reach the terminal
as it happens for the reason `--stdin` does: it drives a full-screen session,
one prompt per match, written to stdout while the answers are read from
/dev/tty. Captured, the session never reached the terminal and ast-grep sat
waiting for an answer to a question nothing had displayed. It joins `--stdin`
in forcing a passthrough.

The hint comment claimed the store decision was taken on text the hint was
already part of; the code reserves a fixed 256 B before the hint exists, since
writing the archive is what produces the path the hint names. The comment now
says so, and its duplicate above the block is gone.

FEATURES.md named the phantom subcommand too, and tied the fallback route to
recovery being off, where the filter also falls back with recovery on when the
compaction does not save enough to pay for a hint.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@KuSh
KuSh force-pushed the fix/4121-followup-subcommand-grammar branch from 814ae66 to 507422f Compare October 7, 2026 00:18
@KuSh
KuSh merged commit e0b2e85 into rtk-ai:develop Oct 7, 2026
11 checks passed
@KuSh
KuSh deleted the fix/4121-followup-subcommand-grammar branch October 7, 2026 00:25
@rtk-release-bot rtk-release-bot Bot mentioned this pull request Oct 7, 2026
Yahiro025 added a commit to Yahiro025/rtk that referenced this pull request Oct 29, 2026
Reconcile with the grammar tables now read off the ast-grep binary
(rtk-ai#4150). With `-c/--config` the only option that can put a value ahead
of a subcommand, the first free positional is reliably the subcommand
slot, which is the precondition rtk-ai#4162 set for looking at it alone.

- Doc comment: state why checking only the first positional is safe.
- test_a_subcommand_behind_any_flag_is_still_a_subcommand: drop the
  `--color always <sub>` cases. `--color` is a `run` option that
  ast-grep rejects ahead of a subcommand, so it is not a global option
  and the line is not valid ast-grep input.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
openbotgit Bot pushed a commit to Yahiro025/rtk that referenced this pull request Nov 6, 2026
Reconcile with the grammar tables now read off the ast-grep binary
(rtk-ai#4150). With `-c/--config` the only option that can put a value ahead
of a subcommand, the first free positional is reliably the subcommand
slot, which is the precondition rtk-ai#4162 set for looking at it alone.

- Doc comment: state why checking only the first positional is safe.
- test_a_subcommand_behind_any_flag_is_still_a_subcommand: drop the
  `--color always <sub>` cases. `--color` is a `run` option that
  ast-grep rejects ahead of a subcommand, so it is not a global option
  and the line is not valid ast-grep input.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ast-grep bug Something isn't working cli

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants