t3code icon indicating copy to clipboard operation
t3code copied to clipboard

Idle Claude profile is signed out at every token refresh (capabilities probe may abort Claude Code mid-refresh)

Open mildlyuseful opened this issue 1 week ago • 0 comments

Summary

A Claude provider instance with no open threads gets signed out at its first token refresh, about 8 hours after each sign-in. Claude Code clears claudeAiOauth in that profile's .credentials.json (empty accessToken and refreshToken, expiresAt: 0), and T3 then shows the instance as unauthenticated. This has happened several times a day on one profile. A second Claude instance, checked by the same status loop, has kept refreshing successfully.

Environment

  • T3 Code Nightly 0.1.12-nightly.20261121 on Windows 11
  • Claude Code 2.1.330, Claude Agent SDK 0.3.321
  • Two claudeAgent instances: one on the default ~/.claude, one with CLAUDE_CONFIG_DIR set to another folder. Both are Pro subscriptions signed in through claude.ai.

Pattern

Sign-in and sign-out times for the affected profile over the last day (local time):

Signed in Signed out Gap
03:07 11:04 7h57m
14:04 22:03 7h59m
23:38 07:35 7h57m

So the first refresh after each sign-in fails. It isn't the refresh token expiring: refreshTokenExpiresAt was weeks away each time.

Trace of the latest occurrence

From server.trace.ndjson, times in UTC. The stored access token was due to expire at about 15:36.

Time (UTC) Span What happened
15:30:11 checkClaudeProviderStatus (836 ms, includes a capabilities probe) /api/oauth/usage returned 429
15:31:47 checkClaudeProviderStatus (81 ms) too short to include a capabilities probe, so no SDK session appears to have started
15:32:42 checkClaudeProviderStatus /api/oauth/usage, using the token just read from .credentials.json, returned 401, about 3.5 minutes before that token's expiresAt
15:35:12 checkClaudeProviderStatus (922 ms, includes a capabilities probe) .credentials.json was rewritten at 15:35:13.03 with empty tokens; the instance has been unauthenticated since

The 401 before expiresAt suggests the token pair had already been rotated by a refresh whose result never reached .credentials.json. The next Claude Code process to start then tried to refresh with the dead refresh token, failed, and cleared the sign-in.

Suspected cause

probeClaudeCapabilities starts a full Agent SDK session and awaits initializationResult(). With includeStatus it also awaits usage and settings. It then calls abort() straight away, or after the gateway grace period.

When the stored token is inside Claude Code's pre-expiry refresh window, these short-lived probe sessions may be what runs the OAuth refresh. If a probe is aborted after the token endpoint has rotated the refresh token, but before Claude Code persists the new pair, that profile's only valid refresh token is gone.

A profile with no open threads is refreshed only by these probes, so it would hit this race far more often than a profile with long-running sessions.

Caveat: I couldn't confirm which process did the early refresh around 15:31. The trace spans don't record the PID or CLAUDE_CONFIG_DIR of the probe sessions. Other local tools that read the same credentials only call /api/oauth/usage with the existing access token, and none of them refreshed in that window.

Possible fixes

  • Don't abort a probe session while Claude Code may be refreshing credentials. For example, close the prompt stream and let the process exit on its own, or give it a short grace period before aborting.
  • Or skip the capabilities probe while the stored token is within a few minutes of expiresAt, so a normal session does the refresh.
  • Record the probe's PID and config dir in the trace, and log a 401 from /api/oauth/usage that arrives before expiresAt as a revoked token. Either would make this much easier to confirm.

Side note: pgrep on Windows

Every ~2.5 minutes snapshotProbe.spawnAndCollect fails with PlatformError: NotFound: ChildProcess.spawn (pgrep -g <pid>). pgrep doesn't exist on Windows. If that call is part of tearing down probe process trees, the teardown may not behave as intended on Windows either.

Workaround

Sign the profile in again. A long-lived claude setup-token would avoid refreshes entirely, but it would likely lose the usage-limit reporting, so it isn't a good option.

mildlyuseful avatar Nov 21 '26 18:11 mildlyuseful