Skip to content

Commit 0bb95ed

Browse files
committed
fix(codex): carry the K12 short-window quota through cache and routing
parseUsageQuota populates shortPercent/shortResetAt/shortWindowSeconds, but setAccountQuotaFromParsed dropped them: the refresh JSON reported weeklyPercent while every short-window field read null, hiding imminent burst exhaustion from the API, dashboard, and routing (#2047; the end-to-end gap left by the #1863 review). Copy the short-window fields into the cached snapshot like the longer windows (credits-only refreshes carry them forward from the existing snapshot), and count the burst window in computeCodexUsageScore on every plan -- mirroring isCodexQuotaExhausted, which already counts it everywhere because upstream enforces it independently. A 1% weekly account with the burst window saturated no longer scores as the idle routing pick.
1 parent e97fb26 commit 0bb95ed

4 files changed

Lines changed: 63 additions & 4 deletions

File tree

‎src/codex/quota.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,9 @@ export function setAccountQuotaFromParsed(
246246
if (existing?.monthlyPercent !== undefined) next.monthlyPercent = existing.monthlyPercent;
247247
if (existing?.monthlyResetAt !== undefined) next.monthlyResetAt = existing.monthlyResetAt;
248248
if (existing?.monthlyIsPrimaryWindow === true) next.monthlyIsPrimaryWindow = true;
249+
if (existing?.shortPercent !== undefined) next.shortPercent = existing.shortPercent;
250+
if (existing?.shortResetAt !== undefined) next.shortResetAt = existing.shortResetAt;
251+
if (existing?.shortWindowSeconds !== undefined) next.shortWindowSeconds = existing.shortWindowSeconds;
249252
next.resetCredits = quota.resetCredits;
250253
accountQuota.set(accountId, next);
251254
schedulePersistAccountQuotas();
@@ -276,6 +279,15 @@ export function setAccountQuotaFromParsed(
276279
if (existing.monthlyIsPrimaryWindow === true) next.monthlyIsPrimaryWindow = true;
277280
}
278281

282+
// The burst (short) window is upstream-enforced on every plan, so its
283+
// fields must survive parse → cache like the longer windows do: dropping
284+
// them here reported shortPercent=null while the raw payload carried 0%,
285+
// hiding imminent burst exhaustion from the API, dashboard, and routing
286+
// (#2047).
287+
if (quota.shortPercent !== undefined) next.shortPercent = quota.shortPercent;
288+
if (quota.shortResetAt !== undefined) next.shortResetAt = quota.shortResetAt;
289+
if (quota.shortWindowSeconds !== undefined) next.shortWindowSeconds = quota.shortWindowSeconds;
290+
279291
if (quota.resetCredits !== undefined) next.resetCredits = quota.resetCredits;
280292
else if (existing?.resetCredits !== undefined) next.resetCredits = existing.resetCredits;
281293

‎src/codex/routing.ts‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -322,14 +322,23 @@ function deleteScopedHealth(accountId: string, scope: CodexQuotaScope): void {
322322
export function computeCodexUsageScore(quota: {
323323
weeklyPercent?: number;
324324
monthlyPercent?: number;
325+
shortPercent?: number;
325326
} | null, plan?: unknown): number {
326327
if (!quota) return CODEX_UNKNOWN_USAGE_SCORE;
328+
// The burst window counts on every plan (upstream-enforced independently,
329+
// see isCodexQuotaExhausted): a 0% long window with the burst window at
330+
// 100% must not score as idle, or routing picks the account that 429s
331+
// on the very next request (#2047).
332+
const burst = typeof quota.shortPercent === "number" && Number.isFinite(quota.shortPercent)
333+
? quota.shortPercent
334+
: undefined;
327335
if (isThirtyDayOnlyCodexPlan(plan)) {
328-
return typeof quota.monthlyPercent === "number" && Number.isFinite(quota.monthlyPercent)
329-
? quota.monthlyPercent
330-
: CODEX_UNKNOWN_USAGE_SCORE;
336+
if (typeof quota.monthlyPercent !== "number" || !Number.isFinite(quota.monthlyPercent)) {
337+
return burst !== undefined ? burst : CODEX_UNKNOWN_USAGE_SCORE;
338+
}
339+
return burst !== undefined ? Math.max(quota.monthlyPercent, burst) : quota.monthlyPercent;
331340
}
332-
const values = [quota.weeklyPercent, quota.monthlyPercent]
341+
const values = [quota.weeklyPercent, quota.monthlyPercent, burst]
333342
.filter((value): value is number => typeof value === "number" && Number.isFinite(value));
334343
return values.length > 0 ? Math.max(...values) : CODEX_UNKNOWN_USAGE_SCORE;
335344
}

‎tests/codex-routing.test.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,16 @@ describe("codex routing", () => {
122122
expect(computeCodexUsageScore({ weeklyPercent: 15 })).toBe(15);
123123
});
124124

125+
test("usage score counts the burst window on every plan (#2047)", () => {
126+
// A long window at 0-1% with the burst window saturated must not score idle.
127+
expect(computeCodexUsageScore({ weeklyPercent: 1, shortPercent: 100 })).toBe(100);
128+
expect(computeCodexUsageScore({ monthlyPercent: 0, shortPercent: 87 }, "k12")).toBe(87);
129+
expect(computeCodexUsageScore({ weeklyPercent: 15, shortPercent: 9 })).toBe(15);
130+
// Missing burst data keeps the pre-existing behavior (long windows only).
131+
expect(computeCodexUsageScore({ weeklyPercent: 15 })).toBe(15);
132+
expect(computeCodexUsageScore(null)).toBe(CODEX_UNKNOWN_USAGE_SCORE);
133+
});
134+
125135
test("exact-account failures record health without rotating the active Pool account", () => {
126136
const transient = makeConfig({ upstreamFailoverThreshold: 1, activeCodexAccountId: "a" });
127137
const transientThread = "fixed-transient-thread";

‎tests/rate-limit-reset-credits.test.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,3 +445,31 @@ describe("rate-limit reset credits", () => {
445445
});
446446
});
447447
});
448+
449+
// ── short-window quota survives the account cache (#2047) ──────────────
450+
451+
describe("short-window quota cache (#2047)", () => {
452+
it("setAccountQuotaFromParsed carries the burst window into the cached snapshot", () => {
453+
setAccountQuotaFromParsed("acct-short", {
454+
weeklyPercent: 1,
455+
weeklyResetAt: 1_000,
456+
shortPercent: 0,
457+
shortResetAt: 2_000,
458+
shortWindowSeconds: 18_000,
459+
});
460+
const snap = getAccountQuota("acct-short");
461+
expect(snap).not.toBeNull();
462+
// 0% is data, not missing.
463+
expect(snap!.shortPercent).toBe(0);
464+
expect(snap!.shortResetAt).toBe(2_000);
465+
expect(snap!.shortWindowSeconds).toBe(18_000);
466+
expect(snap!.weeklyPercent).toBe(1);
467+
468+
// credits-only refresh must not drop the burst window either
469+
setAccountQuotaFromParsed("acct-short", { resetCredits: 12 });
470+
const carried = getAccountQuota("acct-short")!;
471+
expect(carried.shortPercent).toBe(0);
472+
expect(carried.shortWindowSeconds).toBe(18_000);
473+
expect(carried.resetCredits).toBe(12);
474+
});
475+
});

0 commit comments

Comments
 (0)