fix(codex): refresh + retry once on a 401 (biggest reliability gap)#108
Merged
Conversation
…gap) The loader's proactive refresh only catches token EXPIRY. A Codex access token can be revoked/invalidated server-side BEFORE its local `expires` (password change, admin revoke, clock skew, or a lost cross-process refresh race). That surfaced as a 401 returned verbatim — the request just failed, and the only recovery was a manual `keys signin`. Now a 401 on a Codex route forces one refresh and retries once. The refresh re-reads the latest persisted auth first (a sibling process may have already rotated the pair) and adopts a newer token if present, otherwise spends the refresh token and persists the rotated pair. A dead refresh token surfaces the reconnect message; a transient failure keeps the original 401 rather than making things worse. Retries at most once, so it can never loop. Extracted sendWithCodex401Retry() as a pure, dependency-injected helper so the send/refresh/retry control flow is unit-tested.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
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.
The single most important fix for making Codex (ChatGPT-subscription) sign-in reliable.
Problem
The loader's token refresh is time-based only — it refreshes shortly before the local
expires. But a Codex access token can be revoked/invalidated server-side before that (a password change, an admin revoke, clock skew, or a lost cross-process refresh race between the CLI and the workspace server). That surfaced as a 401 returned verbatim: the request failed and the only recovery was a manualopenscience keys signin. The response branch only handled 429/403 quota — never 401.Fix
On a 401 for a Codex route, force one refresh and retry once:
Reconnect it with keys signinmessage; a transient failure returns the original 401 unchanged rather than making it worse.The send/refresh/retry control flow is extracted into a pure, dependency-injected
sendWithCodex401Retry()helper.Tests
test/plugin/codex-401-retry.test.ts: 2xx passes through without refresh; a 401 refreshes once and retries with the new token; refresh-gives-up returns the original 401; a persistent 401 stops after one retry (no loop); a fatal refresh error propagates. Existingcodex-refresh/codex-device-polltests still pass.