Repository navigation
fix(ast-grep): route subcommands by the grammar ast-grep actually has - #4150
Conversation
f725c6f to
9fd09f0
Compare
📊 Automated PR Analysis
SummaryFixes rtk's ast-grep subcommand routing to match the real ast-grep 0.45.3 grammar: removes the phantom Review Checklist
Analyzed automatically by wshm · This is an automated analysis, not a human review. |
400ee58 to
814ae66
Compare
aeppling
left a comment
There was a problem hiding this comment.
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]>
814ae66 to
507422f
Compare
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]>
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.
rtk ast-greproutes by subcommand: anything that is not arungoes to the passthrough, because onlyrun's plain output has thepath:line:contentshape 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 --helplistsrun scan test new lsp outline completions help.docsis not among them --ast-grep docsanswerserror: unrecognized subcommand 'docs'-- so that entry only ever matched a path nameddocsand sent an ordinary search to the passthrough. Measured against the real binary on adocs/directory with 40 matches:[full output: rtk recall …]outlineandhelpwere 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.--coloris a subcommand option, and it moves out of the global table intorun'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 fromast-grep run --help, gaining--kind/-k,--no-ignore,--inspect,--headingand-j.--jsonand--debug-querystay 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--stdindoes. 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)' docshad printed nothing while ast-grep was atAccept? [y]es/[↵]…; it now streams like the raw command.-U/--update-allapplies 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_directorylong enough to outgrow it. The same paragraph appeared twice; the copy on the margin check stays.docs/usage/FEATURES.mdlisted 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-gnurelease binary, run in Docker), not from recall.cargo fmt --all,cargo clippy --all-targetsandcargo test --allare clean. The new arity test is mutation-checked: dropping an entry from either table makes it fail.🤖 Generated with Claude Code