Skip to content

feat: rotate a pool of Command Code keys on credential failures - #9

Merged
JoshRob297 merged 1 commit into
mainfrom
feat/key-rotation
Oct 31, 2026
Merged

JoshRob297 merged 1 commit into
mainfrom
feat/key-rotation

Conversation

@JoshRob297

Copy link
Copy Markdown
Owner

Stacked on #8 (ZDR). Rebase to main once #8 merges.

Context

A Command Code plan can hold several API keys (one per account). Today a key that hits 402 insufficient credits or a rate-limit window keeps failing every request until the user edits the key file, which is what happened with the $0.07 account.

Change

  • New src/auth/pool.ts: pool loading (COMMANDCODE_API_KEYS, COMMANDCODE_KEYS_FILE, ~/.config/commandcode/keys.txt with label=key lines), selection, cooldown mapping, state persistence.
  • Rotation activates only with two or more keys. Single-key mode keeps the current behaviour and never overrides a host Authorization header.
  • In rotation mode the plugin owns the Authorization header on the strict Command Code host, because the host-injected key may be the one that just returned 402.
  • Cooldowns: 402 -> 24h, 401/403 -> 24h, 429 -> Retry-After (cap 6h) or 5m, 5xx -> none. When every key is cooling down the soonest recovery is used.
  • State is labels-only (commandcode-keys-state.json, 0600) and each cooldown is appended to commandcode-rotation.log without secrets.

Verify

  • bun test: 120 pass, 3 skip, 0 fail (new tests/pool.test.ts covers parsing, selection, cooldown mapping, Retry-After, rotation through the real plugin hooks, lookalike host, single-key regression).
  • bunx tsc --noEmit: clean.

@JoshRob297
JoshRob297 changed the base branch from feat/zdr-passthrough to main October 31, 2026 18:28
A plan can hold several API keys. With two or more (COMMANDCODE_API_KEYS,
COMMANDCODE_KEYS_FILE or ~/.config/commandcode/keys.txt) the plugin now
picks the first key that is not cooling down, records 24h cooldowns for
402/401/403, honours Retry-After up to 6h for 429, and never blames the
key for 5xx. State is labels-only and logged without secrets; single-key
mode keeps the previous behaviour and never overrides a host header.
@JoshRob297
JoshRob297 marked this pull request as ready for review October 31, 2026 18:29
@JoshRob297
JoshRob297 merged commit 6643275 into main Oct 31, 2026
1 check passed
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