Skip to content

Commit 7432203

Browse files
committed
fix(codex): preserve unknown main identity state
1 parent 94e40ad commit 7432203

2 files changed

Lines changed: 34 additions & 1 deletion

File tree

‎src/codex/auth-api.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,8 @@ async function retryMainAccountInfoIfIdentityChanged(
261261
requestAccountId: string | null,
262262
retriesRemaining: number,
263263
): Promise<MainAccountInfo | null> {
264-
if (getMainChatgptAccountId() === requestAccountId) return null;
264+
const currentAccountId = getMainChatgptAccountId();
265+
if (currentAccountId === null || currentAccountId === requestAccountId) return null;
265266
reconcileMainCodexAccountRuntimeState();
266267
return retriesRemaining > 0
267268
? fetchMainAccountInfoAttempt(true, retriesRemaining - 1)

‎tests/codex-main-rotation.test.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,38 @@ describe("main account rotation (Option A)", () => {
309309
}
310310
});
311311

312+
test("does not retry an in-flight usage response when the current identity is temporarily unknown", async () => {
313+
expect(reconcileMainCodexAccountRuntimeState()).toBe(false);
314+
const originalFetch = globalThis.fetch;
315+
let resolveUsage!: (response: Response) => void;
316+
let usageCalls = 0;
317+
globalThis.fetch = (async (input, init) => {
318+
if (!String(input).includes("/backend-api/wham/usage")) return originalFetch(input, init);
319+
usageCalls++;
320+
expect(new Headers(init?.headers).get("ChatGPT-Account-Id")).toBe("main_acct");
321+
return new Promise<Response>(resolve => { resolveUsage = resolve; });
322+
}) as typeof fetch;
323+
try {
324+
const infoPromise = fetchMainAccountInfo(true);
325+
rmSync(join(CODEX_DIR, "auth.json"));
326+
resolveUsage(new Response(JSON.stringify({
327+
328+
plan_type: "plus",
329+
rate_limit: { primary_window: { used_percent: 23, reset_at: 1_788_000_000 } },
330+
}), { status: 200 }));
331+
332+
await expect(infoPromise).resolves.toMatchObject({
333+
334+
plan: "plus",
335+
quota: { weeklyPercent: 23 },
336+
});
337+
expect(usageCalls).toBe(1);
338+
expect(isAccountNeedsReauth(MAIN_CODEX_ACCOUNT_ID)).toBe(false);
339+
} finally {
340+
globalThis.fetch = originalFetch;
341+
}
342+
});
343+
312344
test("no active id selects from main plus added accounts and binds main affinity", async () => {
313345
const config = makeConfig({ activeCodexAccountId: undefined, autoSwitchThreshold: 0 });
314346
updateAccountQuota(MAIN_CODEX_ACCOUNT_ID, 5, 0);

0 commit comments

Comments
 (0)