Skip to content

llm: resolve subscription OAuth credentials per request - #13997

Merged
vito merged 2 commits into
dagger:mainfrom
vito:extract/llm-credentials
Sep 1, 2026
Merged

vito merged 2 commits into
dagger:mainfrom
vito:extract/llm-credentials

Conversation

@vito

@vito vito commented Aug 29, 2026

Copy link
Copy Markdown
Contributor

Problem

A long dagger agent / dagger shell session using Claude Code (Anthropic) or Codex (ChatGPT) subscription OAuth starts failing with 401 authentication_error partway through and stays broken until the CLI restarts. The client-side refresh hook exists and is wired, but it never fires: (*LLM).Endpoint memoizes the router and Clone() copies it, so after the first Endpoint() call the token is frozen for the session, and the token is captured inside the provider SDK client at construction so mutating it in place wouldn't help either.

What this does

Engine (core/llm.go, core/llm_credential.go) — the model credential is resolved per request instead of once per LLM object. The TTL closure becomes a CredentialSource with Invalidate; resolution yields Credential{Token, ExpiresAt}; the router re-bases resolvers onto a session-scoped context with a per-resolution timeout, so a detached agent loop no longer depends on whichever request first routed the endpoint. A 401 is recoverable exactly once: the send path invalidates the cached credential and retries; a genuinely revoked login fails fast with a message pointing at dagger llm setup. Expiry travels with the token (*_AUTH_TOKEN_EXPIRES_AT, derived by suffix; absent/unparseable means unknown, never expired) and the cache lifetime is min(expiresAt − 1m, now + 5m), with a 30s refresh floor.

Client (internal/cmd/dagger/llmconfig, secretprovider/env.go) — load→refresh→persist runs as one flock critical section that re-reads the config after taking the lock, so a process that lost a cross-process refresh race adopts the winner's rotated token instead of spending a dead one. The refresh token is kept when a response omits it; already-refreshed providers are persisted even if a later one fails; OAuth requests are bound to a 10s context; envProvider logs and records a span event instead of swallowing errors; an explicitly exported token is never clobbered. A background goroutine refreshes each enabled OAuth provider ~5 minutes before expiry (no goroutine at all unless a subscription provider is configured). token_expires_at is persisted alongside legacy token_expiry so an older CLI sharing the file keeps working.

Second commit — an invalid or missing refresh grant is classified as a reauthentication requirement rather than retried forever; untrusted response bodies are kept out of the error; the background scheduler warns once and retires a terminal failure while still retrying transient ones.

The design and defect inventory are in hack/designs/llm-oauth-token-lifecycle.md.

Not in this PR

LLM.withSmallModel (small-model routing), which sat next to these commits, has its only consumer in the agent session CLI and will land with that.

No public GraphQL API changes.

Extracted from the llm-workspace-dev-env stack; independent of the other extractions.

🤖 Generated with Claude Code

https://claude.ai/code/session_01EgJumuDQNviWmF35dMxsEm

vito added 2 commits September 1, 2026 14:56
Resolve the model credential for each request. This lets the CLI refresh
an OAuth token without a new LLM object, and it keeps a credential from one
request out of the next.

Signed-off-by: Alex Suraci <[email protected]>
(cherry picked from commit 2bd4cd2)
Classify an invalid or missing refresh grant as a reauthentication
requirement, and keep untrusted response bodies out of the error. Let the
background scheduler warn once and retire a terminal failure. Continue to
retry transient failures.

Signed-off-by: Alex Suraci <[email protected]>
(cherry picked from commit d74c729)
@vito
vito force-pushed the extract/llm-credentials branch from fa8513b to 6048d00 Compare September 1, 2026 18:56
@vito
vito merged commit 59afc48 into dagger:main Sep 1, 2026
89 checks passed
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.

2 participants