fix(codex): treat OpenAI 401/token_expired as permanent refresh failure (re-auth, not retry)#1821
Open
sacwooky wants to merge 1 commit into
Open
Conversation
OpenAI's token endpoint returns HTTP 401 {code:"token_expired",
type:"invalid_request_error"} for a dead/invalid (expired or rotated-away)
refresh token. The existing permanent markers (refresh_token_expired/reused/
invalidated, invalid_grant) don't match it, so refreshCodexToken classified the
failure as transient and the caller retried instead of surfacing re-auth — a
retry loop that can never succeed against a dead refresh token.
Add the token_expired marker and treat any 401 from the OAuth token endpoint as
permanent (429/5xx remain transient).
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
When a Codex (OpenAI) OAuth refresh token is dead/invalid (expired or rotated away), OpenAI's token endpoint responds:
classifyOAuthRefreshError()only treats these as permanent:refresh_token_expired,refresh_token_reused,refresh_token_invalidated,invalid_grant. None match OpenAI's actualtoken_expiredcode, so the failure is classified transient.refreshCodexToken()then returnsnullinstead of{ error: "unrecoverable_refresh_error" }, so the caller retries the refresh (with backoff) instead of surfacing re-authentication — a retry loop that cannot succeed against a dead refresh token.Reproduction
POST an invalid/expired refresh token to
https://auth.openai.com/oauth/tokenand observe the real401 token_expiredpayload above; running it throughclassifyOAuthRefreshErrorreturnspermanent: false.Fix
token_expired(and the "could not validate your token" message) to the permanent markers.429/5xxremain transient.Single-file change to
classifyOAuthRefreshError; no behavior change for the existing markers. Verified the real OpenAI401 token_expiredpayload now classifies as permanent →unrecoverable_refresh_error(re-auth).