Skip to content

fix(gh): tokenize gh args instead of walking them by hand - #4000

Merged
KuSh merged 7 commits into
rtk-ai:developfrom
KuSh:feat/gh-arg-tokenizer
Oct 8, 2026
Merged

KuSh merged 7 commits into
rtk-ai:developfrom
KuSh:feat/gh-arg-tokenizer

Conversation

@KuSh

@KuSh KuSh commented Sep 12, 2026 •

Copy link
Copy Markdown
Collaborator

The bug

extract_identifier_and_extra_args in src/cmds/git/gh_cmd.rs picked the PR/issue/run
identifier out of the args with a hand-rolled walker: a hardcoded eight-entry
flags_with_value array (-R --repo -q --jq -t --template --job --attempt), a skip_next
bool, and arg.starts_with('-').

Three defects fell out of that:

  1. The list was incomplete, so any value-taking flag it did not know about had its value
    read as the identifier. gh pr checks's -i/--interval was missing entirely, and only the
    long spellings of --job/--attempt were listed, not -j/-a.
  2. pr_action (gh pr comment / gh pr edit) did not consult the list at all — a bare
    .find(|a| !a.starts_with('-')) — so the first flag value became the PR number in the
    confirmation line.
  3. No -- awareness anywhere, and clap's trailing_var_arg had already eaten the
    separator before gh_cmd saw the args, so it never reached gh either.

Reproducer

Verified against gh 2.46.0 (Debian) and 2.100.0 (container), LC_ALL=C, with a gh shim on
PATH that echoes its argv:

$ rtk gh pr checks --interval 30
GH-ARGV: [pr] [checks] [30] [--interval]     # 30 became the PR number

$ rtk gh pr checks -i 30
GH-ARGV: [pr] [checks] [30] [-i]

$ rtk gh run view -j 12345
GH-ARGV: [run] [view] [12345] [-j]           # the job id became the run id

$ rtk gh run view -a 2
GH-ARGV: [run] [view] [2] [-a]

$ rtk gh pr edit --add-label bug
ok edited #bug                               # the label became the PR number

$ rtk gh pr comment -b hello
ok commented #hello

After the change all of these keep the flag and its value together, and the identifier stays
empty so gh resolves the current branch's PR:

$ rtk gh pr checks --interval 30
GH-ARGV: [pr] [checks] [--interval] [30]

$ rtk gh run view -j 12345
GH-ARGV: [run] [view] [-j] [12345]

$ rtk gh pr edit --add-label bug
ok edited

What changed

  • The walker and flags_with_value are gone. split_identifier calls
    arg_tokenizer::tokenize_grammar and takes the first Token::is_free_positional, then
    rebuilds extra by excluding that token's source_index — detect and act with one rule.
  • One takes_value predicate per gh subcommand, each transcribed from that subcommand's
    own --help: view_takes_value (gh pr view / gh issue view, whose flag sets are
    byte-identical), pr_checks_takes_value, run_view_takes_value, pr_comment_takes_value,
    pr_edit_takes_value. The two that build on view_takes_value are verified strict supersets
    of it; pr comment and pr edit get their own tables. Each table is the union over gh 2.46
    (still distro-shipped) and 2.100, since gh keeps adding flags here — gh pr checks gained
    -q/--jq, --json and -t/--template between the two, and gh pr comment/gh pr edit
    gained --attach.
  • ValueSpec is value().claiming_dash_dash(): verified that gh swallows a following literal
    -- as a flag value (gh run view --job -- 1 queries job "--"), and that a value-taking
    shorthand is legal inside a cluster (gh run view -vj 1 works, as does -j1).
  • pr_action takes the same predicate from its call site, so gh pr comment and gh pr edit
    each get their own grammar rather than sharing one.
  • run() reassembles [subcommand] + args before calling args_utils::restore_double_dash:
    clap carves the subcommand positional out of the same trailing region, so restoring over
    args alone lands the -- one token off and duplicates the subcommand (rtk gh -- pr view 42
    ran gh pr pr view 42). The restored -- is then dropped, because trailing_var_arg only
    ever lets clap strip one: the one at the head of that region, before the subcommand or right
    after it. That one is rtk's own option terminator, not something the user handed gh. Every
    later -- clap passes through untouched and still reaches gh verbatim, and gh keeps reading
    positionals past the boundary (gh pr view -- 42 views PR 42), so the identifier search
    deliberately is not scoped to before_dashdash.

Behaviour change

No working invocation changes against develop. The child argv is byte-identical to develop on every -- spelling
measured with a gh shim, including rtk gh -- pr view 42, rtk gh api -- repos/o/r,
rtk gh api -- repos/o/r --jq .name, rtk gh pr view -- 42, rtk gh pr list -- --state open,
and the two--- forms. What changed is only the identifier extraction listed above.

Dropping the terminator rather than forwarding it is what real gh requires. On gh 2.46.0 and
2.100.0 alike, gh -- pr view 42 is unknown command "pr" for "gh" while gh pr view 42
works, and gh api -- repos/cli/cli --jq .name is accepts 1 arg(s), received 3 while
gh api repos/cli/cli --jq .name prints cli — gh reads everything past -- as a positional.

The identifier search reaches past a -- the user typed, because gh does too (gh pr view -- 42
views PR 42), but only where pulling the identifier forward is a no-op: the boundary escapes
exactly one token, and that token is still read as a positional once unescaped. Otherwise hoisting
the identifier in front of the boundary would promote whatever else trailed it back into flag
position — rtk gh pr checks -- --watch ran gh with --watch live and blocked on a watch the user
had escaped, and gh issue view -- --web / gh run view -- --log went the same way. gh reads
everything past the boundary as a positional (gh pr view -R o/r -- --web resolves --web as the
PR selector) and accepts at most one of them here, so an escaped region rtk leaves alone is one gh
rejects on arity whatever rtk sends. The one argv that differs from develop as a result is the
crowded case, rtk gh pr checks -- --watch 42 (develop reorders it to pr checks 42 -- --watch);
real gh rejects both spellings with accepts at most 1 arg(s), received 2.

Pre-existing and untouched: rtk gh pr view -- 42 still forwards a dangling trailing -- after
rtk's injected --json, byte-identical to develop. Also pre-existing and byte-identical to
develop: rtk gh repo view -- o/r appends rtk's --json past the boundary (run_repo appends
rather than splicing at injection_point), and rtk gh pr -- list runs the filtered list where
real gh would say unknown command "list" for "gh pr" — clap consumes that -- as rtk's own
terminator and rtk cannot tell it apart from one aimed at gh.

Tests

Nine new cases in the module's #[cfg(test)] mod tests, plus the existing identifier tests
migrated to the new entry point. tests/gh_double_dash_test.rs asserts the child argv for nine
-- positions: dropped before the subcommand, dropped after it (bare and before a following
flag), preserved interior on both a passthrough (gh api) and a filtered (gh pr list)
subcommand, an escaped flag kept behind the boundary whether it is alone there or
not, on both the pr checks and run view grammars, and a lone escaped identifier still hoisted
so rtk can inject --json. The six drop/escape cases were confirmed red before their fix and
green after; the preservation and hoisting cases stay green throughout.

Five of the identifier cases were confirmed red against the old walker and green after:
pr checks -i/--interval, run view -j/-a, the -vj cluster, pr edit --add-label, and
pr comment -b.

@rtk-wshm-sync-bot rtk-wshm-sync-bot Bot added bug Something isn't working cli-parsing gh labels Sep 12, 2026
@rtk-wshm-sync-bot

Copy link
Copy Markdown

wshm · Automated triage by AI

📊 Automated PR Analysis

🐛 Type bug-fix
🟡 Risk medium

Summary

Replaces a hand-rolled, incomplete flag-walking heuristic in gh_cmd.rs with a proper tokenizer-based approach that extracts the PR/issue/run identifier from gh subcommand args. Fixes cases where value-taking flags (e.g. -i/--interval, -j/--job, --add-label) had their values misidentified as the PR/issue number, and restores '--' handling that clap had stripped before gh_cmd saw the args.

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 feat/gh-arg-tokenizer branch 2 times, most recently from 36e1bb4 to 86ac6e3 Compare September 17, 2026 22:39

@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.

lgtm

@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.

Sorry, missed this on first check.

tests/gh_double_dash_test.rs spawns the binary through CARGO_BIN_EXE_rtk directly. This branch forked before the tests_that_spawn_rtk_isolate_their_data scan landed on develop (e0d99ab), so CI is green here — but the suite fails once merged, same as what #4005 hit (repaired in #4460).

Repro:

git checkout develop          # df39e33d
git merge <this branch>
cargo test tests_that_spawn_rtk_isolate_their_data
panicked at src/core/test_isolation/mod.rs:302:
  tests/gh_double_dash_test.rs: CARGO_BIN_EXE_rtk

Fix, same shape as #4460's glab change: mod common; + common::rtk_command() in gh_argv, and drop the HOME / XDG_CONFIG_HOME / RTK_DB_PATH envs (rtk_command() pins those plus the tee/recall paths and telemetry).

Verified locally: with that change, this branch merged onto develop + #4460 passes cargo test --all.

@KuSh
KuSh force-pushed the feat/gh-arg-tokenizer branch from 86ac6e3 to ed1c416 Compare October 6, 2026 23:21
@KuSh

KuSh commented Oct 6, 2026

Copy link
Copy Markdown
Collaborator Author

@aeppling fixed

@KuSh
KuSh requested a review from aeppling October 6, 2026 23:35

@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.

thanks lgtm :)

KuSh and others added 7 commits October 8, 2026 17:53
`extract_identifier_and_extra_args` picked the PR/issue/run identifier with a
hand-rolled walker: a hardcoded eight-entry list of value-taking flags, a
`skip_next` bool, and `arg.starts_with('-')`. Anything the list missed had its
value read as the identifier, and `pr_action` did not consult the list at all.

Verified against gh 2.46.0 and 2.100.0:

    rtk gh pr checks --interval 30   ->  gh pr checks 30 --interval
    rtk gh run view -j 12345         ->  gh run view 12345 -j
    rtk gh pr edit --add-label bug   ->  "ok edited #bug"

Replace it with `arg_tokenizer::tokenize_grammar` plus one `takes_value`
predicate per gh subcommand, transcribed from each subcommand's own `--help`,
and take the first free positional. `pr_action` now uses the same tokenization,
and `run()` restores the `--` that clap's `trailing_var_arg` consumes so gh
receives the boundary the user typed.

Co-Authored-By: Claude Opus 5 <[email protected]>
`trailing_var_arg` only ever lets clap strip one `--`, the one at the head of
the region it hands to `gh` — before the subcommand, or right after it. That
one is rtk's option terminator and was never part of the gh command line, so
restoring it fabricated an argument: `gh -- pr view 42` is `unknown command
"pr" for "gh"` and `gh api -- repos/o/r --jq .name` is `accepts 1 arg(s),
received 3` on gh 2.46.0 and 2.100.0 alike. Every later `--` clap passes
through untouched and still reaches gh verbatim.

Rebuilding the region before the drop keeps the subcommand from being
reinjected, so `gh -- pr view 42` runs `gh pr view 42`, not `gh pr pr view 42`.

Co-Authored-By: Claude Opus 5 <[email protected]>
`split_identifier` reads positionals past `--` because gh does too
(`gh pr view -- 42` views PR 42), but the callers re-emit the identifier ahead
of the boundary. When the escaped region held more than the identifier, that
promoted a flag the user had escaped back into flag position:
`rtk gh pr checks -- --watch 42` ran gh with `--watch` live, blocking on a
watch the user had asked gh to treat as a positional.

gh takes at most one positional for these subcommands, so an escaped region
holding more than one token is something gh rejects whatever rtk sends. Look
past the boundary only while it escapes a single token, and hand the rest to
gh untouched.

Also isolate HOME and RTK_DB_PATH in the argv test, which otherwise read the
developer's own config and wrote to their real tracking database.

Co-Authored-By: Claude Opus 5 <[email protected]>
Refusing to look past `--` only when two or more tokens trailed it still unescaped
the single-token case, which is the one a user is most likely to type: `rtk gh pr
checks -- --watch` hoisted `--watch` into flag position and ran the blocking watch
the user had escaped, where develop passed the command through untouched. `gh issue
view -- --web` and `gh run view -- --log` went the same way.

What makes hoisting safe is not how much the boundary escapes but whether unescaping
it is a no-op: reach past `--` only for one token that is still read as a positional
without it, so `gh pr view -- 42` keeps gaining `--json`. gh reads everything past
the boundary as a positional -- `gh pr view -R o/r -- --web` resolves `--web` as the
PR selector -- so a flag left there is a flag gh never acts on.

Co-Authored-By: Claude Opus 5 <[email protected]>
`dirs::config_dir()` prefers `XDG_CONFIG_HOME` over `HOME`, so on a machine that sets
it the argv assertions still ran against the developer's real `~/.config/rtk`.

Co-Authored-By: Claude Opus 5 <[email protected]>
The `--` tests started rtk from CARGO_BIN_EXE_rtk and pointed HOME,
XDG_CONFIG_HOME and RTK_DB_PATH at a tempdir by hand. A child spawned
that way is not covered by the isolation guard, which fails the suite for
any test file that names the binary directly, because it also writes the
tee spool and recall database under the developer's real data directory.

`common::rtk_command()` pins all of those, plus telemetry, so the manual
settings and the comment justifying them go. The PATH that puts the
argv-recording `gh` stub first is not covered by it and stays.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
@KuSh
KuSh force-pushed the feat/gh-arg-tokenizer branch from ed1c416 to 5033633 Compare October 8, 2026 17:57
@KuSh
KuSh merged commit e4f0509 into rtk-ai:develop Oct 8, 2026
12 checks passed
@rtk-release-bot rtk-release-bot Bot mentioned this pull request Oct 8, 2026
@KuSh
KuSh deleted the feat/gh-arg-tokenizer branch October 11, 2026 23:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working cli-parsing gh

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants