Repository navigation
llm: resolve subscription OAuth credentials per request - #13997
Merged
Merged
Conversation
This was referenced Aug 29, 2026
vito
force-pushed
the
extract/llm-credentials
branch
from
August 31, 2026 17:47
2462472 to
fa8513b
Compare
vito
marked this pull request as ready for review
August 31, 2026 17:53
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
force-pushed
the
extract/llm-credentials
branch
from
September 1, 2026 18:56
fa8513b to
6048d00
Compare
shykes
approved these changes
Sep 1, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
A long
dagger agent/dagger shellsession using Claude Code (Anthropic) or Codex (ChatGPT) subscription OAuth starts failing with401 authentication_errorpartway through and stays broken until the CLI restarts. The client-side refresh hook exists and is wired, but it never fires:(*LLM).Endpointmemoizes the router andClone()copies it, so after the firstEndpoint()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 aCredentialSourcewithInvalidate; resolution yieldsCredential{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 atdagger llm setup. Expiry travels with the token (*_AUTH_TOKEN_EXPIRES_AT, derived by suffix; absent/unparseable means unknown, never expired) and the cache lifetime ismin(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;envProviderlogs 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_atis persisted alongside legacytoken_expiryso 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-envstack; independent of the other extractions.🤖 Generated with Claude Code
https://claude.ai/code/session_01EgJumuDQNviWmF35dMxsEm