[PR #9112] feat: add OAuth token keep-alive to prevent expiration #12984

Closed
opened 2026-02-16 18:17:52 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/anomalyco/opencode/pull/9112

State: closed
Merged: No


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
**Original Pull Request:** https://github.com/anomalyco/opencode/pull/9112 **State:** closed **Merged:** No --- ## 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: - Fixes #9111 - Related: #6559, #4992 ## 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
yindo added the pull-request label 2026-02-16 18:17:52 -05:00
yindo closed this issue 2026-02-16 18:17:52 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#12984