Skip to content

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

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

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
Contributor

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.
@ualtinok
Copy link
Copy Markdown
Contributor

@cubic-dev-ai review this

@cubic-dev-ai
Copy link
Copy Markdown

cubic-dev-ai Bot commented May 20, 2026

@cubic-dev-ai review this

@ualtinok I have started the AI code review. It will take a few minutes to complete.

Copy link
Copy Markdown

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 1 file

Re-trigger cubic

@ualtinok
Copy link
Copy Markdown
Contributor

Thanks for putting this together — the transient retry behavior is useful. I reworked it slightly on main in 0131669 so the shared OAuth helper retries transient network/5xx failures, while the OpenCode main-account path disables the helper retry and keeps its existing outer retry/backoff/file-lock layer single-sourced. That avoids multiplying refresh attempts against Anthropic's OAuth endpoint while still covering fallback/Pi/helper callers.\n\nClosing this PR as superseded by the reworked implementation on main. Appreciate the contribution!

@ualtinok
Copy link
Copy Markdown
Contributor

Closed as superseded by the reworked implementation merged on main in 0131669. Thanks again!

@ualtinok ualtinok closed this May 20, 2026
@iceteaSA
Copy link
Copy Markdown
Contributor Author

Closed as superseded by the reworked implementation merged on main in 0131669. Thanks again!

Awesome! I saw you made a more robust mechanism for refreshes. Already rebased onto my Frankenstein's Fork

@iceteaSA iceteaSA deleted the feat/token-refresh-retry branch May 21, 2026 19:41
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.

2 participants