Skip to content

Commit d426eef

Browse files
committed
fix(codex): stop the background main refresh from retracting a reauth quarantine
`fetchMainAccountInfoWhileOwned` cleared `needsReauth` for `__main__` on every successful `/wham/usage` response, including the periodic background refresh. A 200 from `/wham/usage` only proves the token authenticates to the usage endpoint. It does not prove the account can serve Responses traffic, which is a different backend path and still answers 403 when the token's workspace can no longer be selected — the exact condition reported in #327. So when real traffic 401/403'd and `recordCodexUpstreamOutcome` quarantined the account (routing.ts, `outcomeClass === "credential"`), the next background refresh silently retracted that quarantine. The account went straight back into rotation, the following request failed identically and re-marked it, and `needsReauth` never settled — which is why the dashboard showed nothing, the symptom #327 was filed about. Gate the clear on `forceRefresh`. An explicit refresh is an operator asking to re-evaluate, normally right after signing in again, so it stays authoritative and #327's recovery path is unchanged. Sign-in and account-add still clear unconditionally through `account-lifecycle`. Verified on win32: - bun run typecheck exit 0 - bun test tests/server-auth.test.ts 79 pass / 0 fail (was 78/1) - bun test tests/codex-auth-api.test.ts 191 pass / 0 fail - 10 adjacent auth/routing/oauth suites 325 pass / 0 fail Mutation-checked: restoring the unconditional clear fails the new regression test and re-fails `server-auth`'s OpenAI option auth matrix.
1 parent 31ee7a6 commit d426eef

2 files changed

Lines changed: 45 additions & 2 deletions

File tree

‎src/codex/auth-api.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -745,7 +745,16 @@ async function fetchMainAccountInfoWhileOwned(
745745
ts: Date.now(),
746746
};
747747
setMainAccountInfoCache(result);
748-
clearAccountNeedsReauth(MAIN_CODEX_ACCOUNT_ID);
748+
// Only an explicit refresh may retract a reauth quarantine. A 200 from
749+
// /wham/usage proves the token authenticates to the usage endpoint; it does not
750+
// prove the account can serve Responses traffic, which is a different backend path
751+
// and still answers 403 for a workspace the token may no longer select (#327).
752+
// Letting the background poll clear the flag put such an account straight back into
753+
// rotation: the next request failed the same way and re-marked it, so needsReauth
754+
// never settled and the dashboard kept showing nothing — the symptom #327 reported.
755+
// An explicit refresh is an operator asking to re-evaluate, normally right after
756+
// signing in again, so it stays authoritative.
757+
if (forceRefresh) clearAccountNeedsReauth(MAIN_CODEX_ACCOUNT_ID);
749758
// Mirror main quota + plan into the shared stores so the rotation engine can
750759
// score and auto-switch the main account exactly like a pool account (Option A).
751760
setMainAccountPlan(result.plan);

‎tests/codex-auth-api.test.ts‎

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
handleCodexAuthAPI, updateAccountQuota, getAccountQuota,
1414
checkAccountIdCollision, getMainChatgptAccountId,
1515
markAccountNeedsReauth, isAccountNeedsReauth, clearAccountNeedsReauth, clearAccountQuota,
16-
clearMainAccountInfoCache, maskEmail,
16+
clearMainAccountInfoCache, maskEmail, fetchMainAccountInfo,
1717
clearCodexQuotaPrimeState, primeCodexPoolQuotas, seedCodexAuthAdmissionForTests,
1818
type CodexAuthAccountDto,
1919
listCodexAuthAccounts,
@@ -858,6 +858,40 @@ describe("codex-auth API", () => {
858858
expect(isAccountNeedsReauth(MAIN_CODEX_ACCOUNT_ID)).toBe(false);
859859
});
860860

861+
test("a background main account refresh does not retract a reauth quarantine (#327)", async () => {
862+
// #327's own repro: the token is valid, but its workspace can no longer be
863+
// selected, so Responses traffic answers 403 and quarantines the account.
864+
// /wham/usage is a different backend path and keeps answering 200 for that same
865+
// token, so the periodic refresh must not read its own 200 as proof the account
866+
// can serve traffic again — doing so returned the account to rotation, the next
867+
// request failed identically and re-marked it, and needsReauth never settled.
868+
writeFileSync(join(TEST_CODEX_HOME, "auth.json"), JSON.stringify({
869+
tokens: { access_token: "live-main", account_id: "acct-main" },
870+
}));
871+
markAccountNeedsReauth(MAIN_CODEX_ACCOUNT_ID);
872+
clearMainAccountInfoCache();
873+
let usageCalls = 0;
874+
globalThis.fetch = (async () => {
875+
usageCalls += 1;
876+
return Response.json({
877+
878+
plan_type: "pro",
879+
rate_limit: { primary_window: { used_percent: 10, reset_at: 1783000000 } },
880+
});
881+
}) as typeof fetch;
882+
883+
expect((await fetchMainAccountInfo(false)).email).toBe("[email protected]");
884+
// The probe really ran and really succeeded — the quarantine survives it anyway.
885+
expect(usageCalls).toBe(1);
886+
expect(isAccountNeedsReauth(MAIN_CODEX_ACCOUNT_ID)).toBe(true);
887+
888+
// An explicit refresh is an operator asking to re-evaluate, so it stays
889+
// authoritative and still clears the flag (the test above pins that direction).
890+
clearMainAccountInfoCache();
891+
expect((await fetchMainAccountInfo(true)).email).toBe("[email protected]");
892+
expect(isAccountNeedsReauth(MAIN_CODEX_ACCOUNT_ID)).toBe(false);
893+
});
894+
861895
test("BUG-R327: main account exposes and updates needsReauth from WHAM auth responses", async () => {
862896
writeFileSync(join(TEST_CODEX_HOME, "auth.json"), JSON.stringify({
863897
tokens: {

0 commit comments

Comments
 (0)