Skip to content

Commit 36946b9

Browse files
committed
fix(codex): classify bulk pause with fresh plan
1 parent 84247cd commit 36946b9

2 files changed

Lines changed: 14 additions & 7 deletions

File tree

‎src/codex/auth-api.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,8 @@ interface PoolQuotaResult {
369369
needsReauth: boolean;
370370
/** Present only when this call freshly parsed a WHAM usage response. */
371371
freshQuota?: Omit<StoredAccountQuota, "updatedAt">;
372+
/** Present only when this call's WHAM response included a non-empty `plan_type`. */
373+
freshPlan?: string;
372374
/** Present only when this call's WHAM response included `rate_limit_reset_credits.available_count`. */
373375
freshResetCredits?: number;
374376
}
@@ -403,14 +405,18 @@ async function fetchPoolAccountQuota(accountId: string, forceRefresh = false, co
403405
});
404406
if (!resp.ok) return { quota: existing ?? null, needsReauth: resp.status === 401 };
405407
const data = (await resp.json()) as WhamUsageResponse;
406-
const quota = parseUsageQuota({ ...data, plan_type: data.plan_type ?? configuredPlan });
408+
const freshPlan = typeof data.plan_type === "string" && data.plan_type.trim() !== ""
409+
? data.plan_type
410+
: undefined;
411+
const quota = parseUsageQuota({ ...data, plan_type: freshPlan ?? configuredPlan });
407412
const freshResetCredits = quota?.resetCredits;
408413
if (!quota) return { quota: existing ?? null, needsReauth: false };
409414
setAccountQuotaFromParsed(accountId, quota);
410415
return {
411416
quota: getAccountQuota(accountId),
412417
needsReauth: false,
413418
freshQuota: quota,
419+
...(freshPlan !== undefined ? { freshPlan } : {}),
414420
...(freshResetCredits !== undefined ? { freshResetCredits } : {}),
415421
};
416422
} catch (e) {
@@ -536,7 +542,7 @@ async function pauseExhaustedCodexAccounts(config: OcxConfig): Promise<string[]>
536542
for (const { account, quotaResult } of poolResults) {
537543
if (
538544
!isCodexAccountPaused(config, account.id)
539-
&& isCodexQuotaExhausted(quotaResult?.freshQuota ?? null, account.plan)
545+
&& isCodexQuotaExhausted(quotaResult?.freshQuota ?? null, quotaResult?.freshPlan ?? account.plan)
540546
) {
541547
exhaustedIds.push(account.id);
542548
}

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,6 +1149,7 @@ describe("codex-auth API", () => {
11491149
{ id: "exhausted", email: "[email protected]", plan: "plus", isMain: false },
11501150
{ id: "available", email: "[email protected]", plan: "plus", isMain: false },
11511151
{ id: "free-weekly-only", email: "[email protected]", plan: "free", isMain: false },
1152+
{ id: "upgraded-plus", email: "[email protected]", plan: "free", isMain: false },
11521153
{ id: "stale-unknown", email: "[email protected]", plan: "plus", isMain: false },
11531154
{ id: "already-paused", email: "[email protected]", plan: "plus", isMain: false },
11541155
],
@@ -1169,10 +1170,10 @@ describe("codex-auth API", () => {
11691170
globalThis.fetch = (async (_input: RequestInfo | URL, init?: RequestInit) => {
11701171
const accountId = new Headers(init?.headers).get("ChatGPT-Account-Id");
11711172
if (accountId === "stale-unknown") return new Response(null, { status: 502 });
1172-
const weekly = accountId === "exhausted" || accountId === "already-paused" || accountId === "free-weekly-only"
1173+
const weekly = accountId === "exhausted" || accountId === "already-paused" || accountId === "free-weekly-only" || accountId === "upgraded-plus"
11731174
? 100
11741175
: 72;
1175-
const monthly = accountId === "free-weekly-only" ? 20 : 40;
1176+
const monthly = accountId === "free-weekly-only" || accountId === "upgraded-plus" ? 20 : 40;
11761177
return Response.json({
11771178
plan_type: accountId === "free-weekly-only" ? "free" : "plus",
11781179
rate_limit: {
@@ -1187,12 +1188,12 @@ describe("codex-auth API", () => {
11871188

11881189
expect(resp!.status).toBe(200);
11891190
expect(await resp!.json()).toMatchObject({
1190-
pausedAccountIds: ["exhausted"],
1191-
pausedCount: 1,
1191+
pausedAccountIds: ["exhausted", "upgraded-plus"],
1192+
pausedCount: 2,
11921193
activeCodexAccountId: null,
11931194
appliesImmediately: true,
11941195
});
1195-
expect(config.pausedCodexAccountIds).toEqual(["already-paused", "exhausted"]);
1196+
expect(config.pausedCodexAccountIds).toEqual(["already-paused", "exhausted", "upgraded-plus"]);
11961197
expect(resolveCodexAccountForThread("bulk-pause-thread", config)).toBe("free-weekly-only");
11971198
});
11981199

0 commit comments

Comments
 (0)