feat(auth): OAuth Marathon - multi-account credential rotation#8590
feat(auth): OAuth Marathon - multi-account credential rotation#8590jroth1111 wants to merge 8 commits intoanomalyco:devfrom
Conversation
|
Thanks for your contribution! This PR doesn't have a linked issue. All PRs must reference an existing issue. Please:
See CONTRIBUTING.md for details. |
|
The following comment was made by an LLM, it may be inaccurate: Potential Duplicate FoundPR #5754: auth: multi-account OAuth subscription failover Why it's related: This PR appears to address the same core functionality - multi-account OAuth credential failover. Both PRs are designed to handle credential rotation when one account fails (rate limits, auth errors). The current PR (#8590) seems to be an evolution or reimplementation of this concept with the "OAuth Marathon" feature set, including automatic rotation on 429/401/403 errors and cooldown tracking. You should verify:
|
|
This supersedes #5754 with a simpler, more maintainable approach:
|
Non-network errors seen in logs: - AI_APICallError (402 deactivated_workspace) - AI_APICallError (500 server_error) - AI_LoadAPIKeyError / OpenAI API key is missing - ProviderInitError - ConfigInvalidError - ProviderAuthOauthCallbackFailed - NotFoundError - EditBuffer is destroyed
ce64eb6 to
0366353
Compare
|
Nice work on this! Following along 👀 |
|
@adamdotdevin Happy to help test this if you need early feedback! |
00637c0 to
71e0ba2
Compare
f1ae801 to
08fa7f7
Compare
OAuth Marathon 🏃
Keep running when you hit the wall. This PR adds automatic credential rotation for OAuth providers - when one account hits rate limits or auth errors, opencode seamlessly switches to your next available credential within the same provider.
Closes #8591
Works for all OAuth providers — both core providers and plugins.
The Problem
Using OAuth providers with personal subscriptions often means hitting rate limits mid-session. Currently, when this happens, your request fails and you're stuck waiting.
The Solution
Register multiple OAuth accounts for the same provider, and opencode will automatically:
How to Add Multiple Accounts
Run
opencode auth loginmultiple times for the same provider:Architecture Overview
flowchart TD A[Provider.getSDK] --> B[createOAuthRotatingFetch] B --> C{fetchFn} C -->|429 Rate Limit| D[moveToBack + notifyFailover] C -->|401/403 Auth| E[markAccessExpired + retry] C -->|Network Error| F[recordOutcome + notifyFailover] C -->|200 OK| G[recordOutcome success] D --> H[Try Next Credential] E -->|Still fails| H F --> H H --> CDemo
Configuration (Optional)
Per-provider settings in
opencode.json. Sensible defaults are used if omitted:{ "provider": { "openai": { "oauth": { "maxAttempts": 3, // default: number of accounts "rateLimitCooldownMs": 60000, // default: 30000 "authFailureCooldownMs": 300000, // default: 300000 "toastDurationMs": 5000 // default: 8000 } } } }Changes
src/auth/rotating-fetch.ts- Core rotation logicsrc/auth/context.ts- AsyncLocalStorage for request scopingsrc/auth/credential-manager.ts- Toast notificationssrc/auth/index.ts- OAuth pool management & persistencesrc/config/config.ts- Newoauthconfig schematest/auth/oauth-rotation.test.ts- 10 test casesVerification
How I tested:
Test Coverage