Skip to content

feat: add OAuth token keep-alive to prevent expiration - #9112

Closed
mguttmann wants to merge 1 commit into
anomalyco:devfrom
mguttmann:feat/oauth-keepalive
Closed

mguttmann wants to merge 1 commit into
anomalyco:devfrom
mguttmann:feat/oauth-keepalive

Conversation

@mguttmann

Copy link
Copy Markdown

Summary

Implements an automatic keep-alive mechanism for Anthropic OAuth tokens to prevent the "Token refresh failed: 400" error that occurs after periods of inactivity.

Problem

Users report that after several hours of inactivity (especially after sleep/overnight), the Anthropic OAuth token expires and refresh fails:

Error: Token refresh failed: 400

This forces users to re-login every morning. See related issues:

Solution

New module auth/keepalive.ts that periodically pings all logged-in Anthropic OAuth accounts using the /api/oauth/usage endpoint:

  • Zero token cost - The usage endpoint doesn't consume any tokens
  • Hourly ping - First ping after 5 minutes, then every 60 minutes
  • Multi-account support - Pings ALL logged-in Anthropic accounts (account 1, 2, 3, etc.)
  • Anthropic-only - Other providers (Copilot, OpenAI) are not affected
  • Automatic - Starts with InstanceBootstrap(), no user action required
  • Logged - All pings logged under auth.keepalive for debugging

Changes

  • packages/opencode/src/auth/keepalive.ts - New module with keep-alive logic
  • packages/opencode/src/project/bootstrap.ts - Initialize keep-alive on startup

Testing

  • Run OpenCode for extended periods
  • Monitor logs for auth.keepalive messages:
    auth.keepalive: pinging anthropic oauth accounts { count: 2 }
    auth.keepalive: keepalive ping successful { recordId: "...", label: "default" }
    
  • Verify tokens remain valid after previously problematic timeframes

Implements hourly ping for all Anthropic OAuth accounts to prevent
token expiration after periods of inactivity.

- New module: auth/keepalive.ts
- Uses /api/oauth/usage endpoint (zero token cost)
- Pings all logged-in accounts (multi-account support)
- First ping after 5 minutes, then every hour
- Integrated into InstanceBootstrap

Fixes #9111
@github-actions

Copy link
Copy Markdown
Contributor

The following comment was made by an LLM, it may be inaccurate:

No duplicate PRs found

@mguttmann

Copy link
Copy Markdown
Author

This PR implements a fix for the token expiration issues reported in #6559 and #4992.

The approach uses the existing fetchAnthropicUsage function which calls /api/oauth/usage - an endpoint that:

  • Requires authentication (keeps the token active)
  • Does NOT consume any tokens or context
  • Is already used elsewhere in the codebase

Happy to address any feedback!

@ruijdacd

Copy link
Copy Markdown

Why was this PR closed?

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.

OAuth token expires after inactivity causing 'Token refresh failed: 400'

2 participants