Repository navigation
Description
What is wrong
When a session's Claude access token expires, the session stops working. Every turn fails within two seconds with:
Failed to authenticate. API Error: 401 OAuth access token has expired. Re-authenticate to continue.
Messages don't help. "Try again" and "Continue" fail the same way, because the same Claude Code process keeps the expired token. Only a new run gets a working one.
In session https://app.fctl.sh/s/ses_01M585DBQGCCKMY571BGHGZ3C3, which runs on a full Claude login, the run started at 18:47 UTC and its token stopped working at 19:10. Four messages between 19:13 and 19:21 all failed this way. At 19:22 that run ended and a new one started; it got a fresh token and works. Nothing in the transcript says why. There is no credential_expired notice, because fctl-agent was never asked for a token.
How it works now
-
The control plane puts the stored access token into a session's spec when it has at least 10 minutes left, and refreshes it first otherwise (
REFRESH_MARGIN_MSincrates/fctl-control/src/spec.rs). So a run can start with a token that has minutes left: this one had about 23. -
fctl-agent passes the token to Claude Code as
CLAUDE_CODE_OAUTH_TOKEN. It answers Claude Code'soauth_token_refreshcontrol request with a fresh token from the control plane (on_claude_requestincrates/fctl-agent/src/supervisor.rs). -
Claude Code 2.1.302 sends
oauth_token_refreshonly whenCLAUDE_CODE_SDK_HAS_OAUTH_REFRESHis set and its entrypoint is Claude Desktop, Cowork or VS Code:N7s=new Set(["claude-desktop","local-agent","claude-vscode"]); function hUt(){return Boolean(a.CLAUDE_CODE_SDK_HAS_OAUTH_REFRESH)&&N7s.has(a.CLAUDE_CODE_ENTRYPOINT??"")}
In fctl's sandboxes the entrypoint is
sdk-cliand the variable is unset, so Claude Code never asks. On the 401 it retries twice with the same token (api_retry,authentication_failed) and ends the turn with the error. -
A running Claude Code can't take a new token any other way: it reads
CLAUDE_CODE_OAUTH_TOKENonce, when it starts.CLAUDE_CODE_OAUTH_TOKEN_FILE_DESCRIPTORserves Anthropic's cloud sessions, and is read at startup too. -
Claiming another entrypoint is no fix.
local-agentis Claude Desktop's Cowork, and it changes how Claude Code behaves: among other things it scrubs subprocesses' environments, which would drop project secrets from shell commands. It would also misreport the client.
docs/architecture.md (Claude credentials), the comment on MIN_VERSION in crates/fctl-agent/src/claude.rs and ADR 0017 ("the first release that asks its host for fresh OAuth tokens") still say that Claude asks its host when the token expires. ADRs aren't rewritten: a new record supersedes that line.
What should happen
- fctl-agent keeps Claude on a valid token by itself. Before the token in use expires (the
expires_atof the spec's credential, and of each one the host sends), it gets a fresh token from the control plane. Then it restarts Claude Code with it at a moment no turn runs, resuming the conversation (--resume). A message that comes when the token is about to expire waits for the restart. - A turn that still fails with
authentication_failedgets a fresh token and a restarted Claude Code, and its message is sent again, once. A session never needs a person to resume it for a token. - When the control plane can't refresh (the login was revoked), the session says so and how to fix it (connect Claude again in Settings → Claude), instead of failing each message with a 401.
- The docs and the
MIN_VERSIONcomment say how it works.
Compatibility
This changes fctl-agent and needs no protocol change: CredentialRequest { kind: Claude } and the credential's expires_at already exist. Agents of older versions keep failing when their token expires.
Restarting Claude Code stops its background tasks (#87). fctl-agent can restart while none run, and otherwise name the ones it stopped, as a stop does.
Decisions for a person
- How long before expiry fctl-agent restarts Claude Code, and whether it waits for background tasks (Sessions stop while their agents wait for background tasks #87) until the token actually expires.
- Response: Set a reasonable limit, and await long running background tasks, so that they complete.
- Whether specs should carry tokens with more life left, so that restarts are rarer (refresh when less than an hour is left, say). That's only safe if refreshing doesn't end the access tokens that other runs of the same login hold, which nobody has checked yet.
- Response: Come up with the best solution and implement it.
Notes for the fix
What a fix has to work with, found while tracing this:
- Getting a fresh token. fctl-agent asks with
AgentToHost::CredentialRequest { kind: CredentialKind::Claude }. The host passes it on (crates/fctl-host/src/session.rs, thendaemon.rs), and FleetDO answers withspec::claude_credential(credential()incrates/fctl-control/src/do_fleet.rs, one refresh per credential at a time). fctl-agent getsHostToAgent::Credentialsinon_credentials, which keeps the new credential inself.claude_credential.start_claudelaunches Claude Code with that one, so a restart picks it up. - The control plane refreshes only within 10 minutes of expiry (
REFRESH_MARGIN_MS). A request made earlier gets the same token back. So either fctl-agent asks within those 10 minutes, or the request needs a new optional field (how long the token must stay valid, say), which is a protocol change:crates/fctl-protofirst, thencargo xtask gen. - A deliberate restart must not look like a crash.
ClaudeOut::Exiteddoesn't say which process exited (crates/fctl-agent/src/claude.rs), andon_claude_exittakes whicheverself.claudeis current. If fctl-agent kills Claude Code and starts a new one, the old process's exit arrives after the new one is in place. It would be taken for a crash of the new one, which is then restarted again and counted against the three restarts in ten minutes that fail the session (self.restarts). The exit needs to name its process (a launch number, say). - Spotting the failure. The stream shows
system/api_retrywith"error": "authentication_failed", "error_status": 401, then anassistantmessage with"error": "authentication_failed"and the text above, then aresultwithis_error: true. This session's transcript has four of them (fctl logs --jsonon it). - Background tasks. A restart stops Claude's background tasks. The pull request for Sessions stop while their agents wait for background tasks #87 adds
crates/fctl-agent/src/tasks.rs: fctl-agent follows the tasks that run (Tasks), and when a stop cuts them short,finish()names them in the transcript and the next message tells Claude (persisted.unfinished). A restart for a token can do the same, or wait while tasks run and the token still holds. - fake-claude doesn't behave like Claude Code here. Its
refreshword sendsoauth_token_refresh, which Claude Code never sends in fctl's sandboxes, socargo xtask devshows a refresh that real sessions never get. It needs a way to play an expired token (the 401 sequence above until it is restarted with a new token), so that the fix can be checked locally. Then Check OAuth refresh, the usage-limit pause and long-lived tokens with real Claude #79's check can run with real Claude again.
Found while checking #79.
Activity
fctl-sh commentedon Oct 18, 2026
Claude Code 2.1.101, the oldest fctl runs (
MIN_VERSION), gates it the same way:if(F6(process.env.CLAUDE_CODE_SDK_HAS_OAUTH_REFRESH)&&$R1.has(process.env.CLAUDE_CODE_ENTRYPOINT??"")), with$R1=new Set(["claude-desktop","local-agent","claude-vscode"]). So no Claude Code that fctl supports has ever asked fctl-agent for a token: this isn't a regression, and every session on a full login stops working when its token expires.fix(agent): keep sessions up while agents wait for background tasks (#91
fctl-sh commentedon Oct 28, 2026
Sessions now keep working past their access token's expiry: #79's real-Claude check of the OAuth refresh passed, and that issue is closed.