Skip to content

Comments

feat: passthrough fallback when Clap parse fails#200

Open
guillaumedeslandes wants to merge 1 commit intortk-ai:masterfrom
guillaumedeslandes:feat/passthrough-fallback
Open

feat: passthrough fallback when Clap parse fails#200
guillaumedeslandes wants to merge 1 commit intortk-ai:masterfrom
guillaumedeslandes:feat/passthrough-fallback

Conversation

@guillaumedeslandes
Copy link
Contributor

Summary

  • When RTK cannot parse a command (e.g. rtk git -C /path status), instead of exiting with Clap error code 2, it now falls back to running the raw command directly
  • Adds parse_failures SQLite table to track these events for analytics
  • Adds rtk gain --failures (-F) to view failure log with recovery rate and top commands

Motivation

Clap calls std::process::exit(2) before main() executes when it can't parse args. This blocks developer workflows for commands RTK doesn't fully model (e.g. git -C, unknown flags). The fallback ensures RTK never breaks a command that would otherwise work.

Changes

File Change
src/main.rs Cli::parse()try_parse() + run_fallback() with Stdio::inherit(), 7 tests
src/tracking.rs parse_failures table, record_parse_failure(), get_parse_failure_summary(), silent wrapper, 2 tests
src/gain.rs --failures / -F flag, show_failures() display function

Test plan

  • cargo fmt --all --check passes
  • cargo clippy --all-targets — 0 new errors
  • cargo test --all — 387 tests pass
  • rtk git -C . status — fallback works, prints [rtk: parse failed, running raw]
  • rtk --help / --version — still work normally
  • rtk gain --failures — shows failure log after fallback triggers

🤖 Generated with Claude Code

When RTK cannot parse a command (e.g. `rtk git -C /path status`),
instead of exiting with error code 2, it now falls back to running
the raw command directly. This keeps developer workflows unbroken.

- Replace Cli::parse() with try_parse() + run_fallback()
- Add parse_failures SQLite table for failure analytics
- Add `rtk gain --failures` / `-F` to view failure log
- Fallback preserves stdin/stdout/stderr via Stdio::inherit()
- --help and --version still work normally

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@pszymkowiak
Copy link
Collaborator

Tested locally — solid approach, needs rebase + 2 fixes before merge.

Testing results:

  • 387/387 unit tests pass
  • 82/83 smoke tests pass (1 fail due to branch being behind master)
  • Manual testing confirms fallback works correctly for git -C, git --no-pager, grep -E
  • Normal RTK-filtered commands (git status, git log, --help, --version) unaffected

Must fix:

  1. Rebase on master — branch is 2 commits behind (missing v0.22.0/0.22.1), cargo test count mismatch
  2. Guard RTK meta-commands from fallback — rtk gain --badtypo currently tries to execute a gain binary from $PATH before showing the Clap error. Add a check for known RTK-only subcommands
    (gain, discover, learn, init, config, proxy, hook-audit, cc-economics) and show the Clap error directly for those.

Nice to have (non-blocking):

  • Timer starts after command completes — all fallback commands show ~0ms in rtk gain --history. Move TimedExecution::start() before the .status() call.
  • parse_failures table never gets cleaned up (only record() calls cleanup_old(), not record_parse_failure())
  • Strip ANSI codes from Clap error before storing in SQLite (utils::strip_ansi already exists)

This will fix #204, #228, #229 and any future unknown-flag issues globally. Great work!

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.

2 participants