Skip to content

Sessions stop working when their Claude access token expires #90

Closed

Description

@fctl-sh

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_MS in crates/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's oauth_token_refresh control request with a fresh token from the control plane (on_claude_request in crates/fctl-agent/src/supervisor.rs).

  • Claude Code 2.1.302 sends oauth_token_refresh only when CLAUDE_CODE_SDK_HAS_OAUTH_REFRESH is 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-cli and 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_TOKEN once, when it starts. CLAUDE_CODE_OAUTH_TOKEN_FILE_DESCRIPTOR serves Anthropic's cloud sessions, and is read at startup too.

  • Claiming another entrypoint is no fix. local-agent is 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_at of 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_failed gets 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_VERSION comment 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, then daemon.rs), and FleetDO answers with spec::claude_credential (credential() in crates/fctl-control/src/do_fleet.rs, one refresh per credential at a time). fctl-agent gets HostToAgent::Credentials in on_credentials, which keeps the new credential in self.claude_credential. start_claude launches 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-proto first, then cargo xtask gen.
  • A deliberate restart must not look like a crash. ClaudeOut::Exited doesn't say which process exited (crates/fctl-agent/src/claude.rs), and on_claude_exit takes whichever self.claude is 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_retry with "error": "authentication_failed", "error_status": 401, then an assistant message with "error": "authentication_failed" and the text above, then a result with is_error: true. This session's transcript has four of them (fctl logs --json on 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 refresh word sends oauth_token_refresh, which Claude Code never sends in fctl's sandboxes, so cargo xtask dev shows 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.

Session: https://app.fctl.sh/s/ses_01M585DBQGCCKMY571BGHGZ3C3

Activity

  1. added
    bug
    agents
    on Oct 18, 2026
  2. fctl-sh commented on Oct 18, 2026

    @fctl-sh
    ContributorAuthor

    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.

  3. fctl-sh commented on Oct 28, 2026

    @fctl-sh
    ContributorAuthor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    agentsbug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions