Skip to content

feat(auth): add retry with exponential backoff to refreshClaudeOAuthToken#18

Open
iceteaSA wants to merge 1 commit into
cortexkit:mainfrom
iceteaSA:feat/token-refresh-retry
Open

feat(auth): add retry with exponential backoff to refreshClaudeOAuthToken#18
iceteaSA wants to merge 1 commit into
cortexkit:mainfrom
iceteaSA:feat/token-refresh-retry

Conversation

@iceteaSA
Copy link
Copy Markdown

Summary

Adds resilient retry logic to refreshClaudeOAuthToken:

  • Exponential backoff: Up to 3 attempts (configurable via maxRetries) with delays of 0ms, 500ms, 1000ms (configurable via baseDelayMs)
  • 5xx retry: Server errors automatically retry after canceling the response body
  • Network error retry: Retries on ECONNRESET, ECONNREFUSED, ETIMEDOUT, UND_ERR_CONNECT_TIMEOUT, and generic "fetch failed"
  • 4xx immediate throw: Client errors (including ClaudeOAuthRefreshError) are not retried
  • Non-breaking: New parameters are optional with sensible defaults. Existing callers get retry automatically.

Motivation

Currently a single transient error during token refresh (e.g., a Cloudflare 502 or a momentary network blip) causes the entire session to fail. This is especially impactful for long-running sessions where a brief infrastructure hiccup shouldn't require re-authentication.

Testing

All existing tests pass. The retry logic handles both FallbackAccountManager.refreshAccountNow() (which calls this function) and the main account refresh path in the OpenCode plugin.

…oken

Currently a single 5xx or transient network error (ECONNRESET,
ECONNREFUSED, ETIMEDOUT) during token refresh kills the session.

Adds optional maxRetries (default: 2) and baseDelayMs (default: 500)
parameters with exponential backoff. 4xx errors and
ClaudeOAuthRefreshError throw immediately without retry.

Non-breaking: callers that don't pass retry options get the new
resilient behavior by default.
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