Skip to content

feat: auto-failover — baton watch - #2

Open
ehsanjso wants to merge 3 commits into
mainfrom
feat/auto-failover
Open

ehsanjso wants to merge 3 commits into
mainfrom
feat/auto-failover

Conversation

@ehsanjso

Copy link
Copy Markdown
Contributor

What

baton watch — detect quota-dead sessions and pass the baton automatically: convert the transcript, import it into the failover target, print the ready-to-run resume command.

  • Detection — Claude Code: newest assistant event in the transcript tail, flagged isApiErrorMessage with limit-shaped text (matched against real error shapes: "You've hit your session limit · resets 5:50pm"). OpenCode: newest message per session via opencode db --format json, limit-shaped error payload. 401s / model errors / stalls do NOT fire.
  • Interactive by default — quota is account-level, so every active session dies at once; watch lists the dead and you pick which to fail over. No pre-configuration.
  • --auto — unattended mode fails over only allowlisted sessions. Allowlist from inside the conversation via new MCP tools: failover_opt_in, failover_opt_out, failover_status (session id defaults to the caller's own session = most recently active).
  • --once — single scan for wiring into agent hooks later; --to overrides the default claude-code ⇄ opencode pair.
  • Dedup: each error event fires exactly once (state file keyed by event id); a session that recovers and re-dies re-fires.

Verified

  • 7 new unit tests (detection predicates both formats, tail reading, dir scan, state round-trip); 30 total pass, clippy clean on new code.
  • End-to-end both directions against the real CLIs:
    • claude-code → opencode: fixture transcript with real-shaped limit error → baton watch --once --auto → real opencode import → opencode export confirms all 3 messages.
    • opencode → claude-code: crafted export with limit error imported into opencode's SQLite → watch detects via opencode db → transcript written under ~/.claude/projects/<encoded-cwd>/ → claude --resume <id> command printed.
    • Rerun after failover: silent (dedup works).

Notes / ceilings

  • opencode import sets the session directory to the import cwd (ignores info.directory) — hence the documented "run baton watch from the project directory" constraint.
  • Error matching is keyword-based (limit/quota/429), not a per-provider taxonomy — widen if a provider's quota error slips through.
  • Other agents (codex, gemini) are follow-ups: detection predicates per format, same pipeline.

🤖 Generated with Claude Code

ehsanjso and others added 3 commits July 12, 2026 01:18
…ses the baton

When an account hits its usage limit, every active session dies at once.
`baton watch` polls the current project's sessions for limit-shaped errors
(Claude Code: transcript tail; OpenCode: last message via `opencode db`),
then converts + imports the transcript into the failover target and prints
the resume command. Interactive mode prompts per death; --auto fails over
only sessions allowlisted via the new failover_opt_in/opt_out/status MCP
tools. --once supports hook-triggered scans; --to overrides the default
claude-code ⇄ opencode pair.

Verified end-to-end in both directions against real `opencode import`/
`opencode export` and a real-shaped Claude transcript.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
…aths, dir compare hardening

- scan() now merges handled-marks into freshly-loaded state so an opt-in
  written by the MCP tool mid-scan isn't clobbered by the stale copy.
- failover_opt_in clears the session's handled entry: opting in after a
  death re-arms it for the next --auto scan.
- opencode directory match normalizes Windows verbatim prefixes and
  trailing separators on both sides.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@ehsanjso

Copy link
Copy Markdown
Contributor Author

Post-review pass (self): fixed a mid-scan state clobber (MCP opt-in written while watch was scanning could be lost), made failover_opt_in re-arm a death that fired before the opt-in, and hardened the opencode directory comparison for Windows verbatim paths/trailing separators. 31 tests green.

@ehsanjso
ehsanjso force-pushed the feat/auto-failover branch from e5cd6ff to 6546f61 Compare July 12, 2026 19:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant