Skip to content

Commit 6a3e5df

Browse files
committed
fix(codex): stop reporting a 5-hour window as the weekly quota
A K12-style plan sends a 5-hour primary window plus a 7-day secondary. The parser folded the primary into `weeklyPercent` whenever it was not explicitly monthly, so the 5-hour bar was reported AS the weekly quota and the real weekly reading -- the one that actually gates the account -- was discarded. The primary window is now skipped for the weekly slot when it DECLARES a duration shorter than a day, letting the 7-day secondary land there instead. A window with no declared duration is deliberately unchanged: older WHAM payloads omit `limit_window_seconds`, and inferring a duration there would reclassify every legacy account. That is why the discriminator is "declares a sub-day duration" rather than "is not seven days". This is the narrow correctness half of #1791. The broader generic-window storage (keeping every upstream window with its own reset, rather than two named slots) touches ~158 call sites across quota, routing, capacity, CLI and API surfaces, and is planned separately in devlog/_plan/260816_wave34_closeout/050_1791_quota_windows.md. Refs #1791.
1 parent 50ad068 commit 6a3e5df

2 files changed

Lines changed: 64 additions & 2 deletions

File tree

‎src/codex/quota.ts‎

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,19 @@ type WhamUsageWindow = {
5656
};
5757

5858
const MONTHLY_WINDOW_MIN_SECONDS = 28 * 24 * 60 * 60;
59+
/**
60+
* Shortest window still plausibly the WEEKLY quota (#1791).
61+
*
62+
* K12 and similar plans send a 5-hour primary window plus a 7-day secondary. Folding the
63+
* primary into `weeklyPercent` reported the 5-hour bar as the weekly one and discarded the
64+
* real weekly reading entirely, so the dashboard showed a window that reset every few hours
65+
* and routing never saw the limit that actually gates the account.
66+
*
67+
* 24h is the discriminator: anything shorter is a burst window, not a weekly one. A window
68+
* with no declared duration is unchanged, because older payloads omit `limit_window_seconds`
69+
* and guessing there would break every legacy account.
70+
*/
71+
const WEEKLY_WINDOW_MIN_SECONDS = 24 * 60 * 60;
5972
const MONTHLY_WINDOW_MIN_MINUTES = MONTHLY_WINDOW_MIN_SECONDS / 60;
6073

6174
const accountQuota = new Map<string, StoredAccountQuota>();
@@ -160,6 +173,15 @@ function hasKnownQuotaValue(quota: Omit<StoredAccountQuota, "updatedAt">): boole
160173
.some(value => typeof value === "number" && Number.isFinite(value));
161174
}
162175

176+
/** True only for a window that DECLARES a duration shorter than a day. */
177+
function isExplicitShortWindow(window: WhamUsageWindow | null | undefined): boolean {
178+
const seconds = window?.limit_window_seconds;
179+
return typeof seconds === "number"
180+
&& Number.isFinite(seconds)
181+
&& seconds > 0
182+
&& seconds < WEEKLY_WINDOW_MIN_SECONDS;
183+
}
184+
163185
function isExplicitMonthlyWindow(window: WhamUsageWindow | null | undefined): boolean {
164186
const seconds = window?.limit_window_seconds;
165187
return typeof seconds === "number"
@@ -465,10 +487,17 @@ export function parseUsageQuota(data: WhamUsageResponse): Omit<StoredAccountQuot
465487
// - 선택한 방식: only an explicit primary duration of at least 28 days changes it to monthly.
466488
// - 다른 대안 대신 이 방식을 선택한 이유: it accepts calendar-month variance and preserves legacy payloads.
467489
// - 장점, 단점 및 영향: Team monthly quotas classify correctly; unknown durations remain weekly by design.
468-
const weeklyPercent = primaryIsMonthly ? secondaryPercent : primaryPercent ?? secondaryPercent;
490+
// #1791: a primary window that declares a sub-day duration is a burst window, not the
491+
// weekly one. Skip it so the secondary (the real 7-day window) is what lands in
492+
// `weeklyPercent`; without this the 5-hour bar was reported as weekly and the actual
493+
// weekly reading was dropped on the floor.
494+
const primaryIsShort = isExplicitShortWindow(primaryWindow);
495+
const weeklyCandidatePercent = primaryIsShort ? undefined : primaryPercent;
496+
const weeklyCandidateResetAt = primaryIsShort ? undefined : primaryResetAt;
497+
const weeklyPercent = primaryIsMonthly ? secondaryPercent : weeklyCandidatePercent ?? secondaryPercent;
469498
const weeklyResetAt = primaryIsMonthly
470499
? secondaryResetAt
471-
: primaryPercent !== undefined ? primaryResetAt : secondaryResetAt;
500+
: weeklyCandidatePercent !== undefined ? weeklyCandidateResetAt : secondaryResetAt;
472501
const monthlyPercent = primaryIsMonthly ? primaryPercent ?? tertiaryPercent : tertiaryPercent;
473502
const monthlyResetAt = primaryIsMonthly && primaryPercent !== undefined ? primaryResetAt : tertiaryResetAt;
474503
if (thirtyDayOnly) {

‎tests/codex-routing.test.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,6 +1237,39 @@ describe("codex routing", () => {
12371237
});
12381238
});
12391239

1240+
1241+
test("a sub-day primary window does not masquerade as the weekly quota (#1791)", () => {
1242+
// K12 and similar plans send a 5-hour primary plus a 7-day secondary. Folding the primary
1243+
// into weeklyPercent reported the 5-hour bar as weekly and discarded the real weekly
1244+
// reading, so the dashboard showed a window resetting every few hours and routing never
1245+
// saw the limit that actually gates the account.
1246+
expect(parseUsageQuota({
1247+
rate_limit: {
1248+
primary_window: { used_percent: 90, reset_at: 1, limit_window_seconds: 5 * 60 * 60 },
1249+
secondary_window: { used_percent: 20, reset_at: 2, limit_window_seconds: 7 * 24 * 60 * 60 },
1250+
},
1251+
})).toMatchObject({ weeklyPercent: 20, weeklyResetAt: 2 });
1252+
});
1253+
1254+
test("a primary window with no declared duration is still treated as weekly (#1791)", () => {
1255+
// Older payloads omit limit_window_seconds entirely. Guessing there would reclassify
1256+
// every legacy account, so an undeclared duration keeps the historical behavior.
1257+
expect(parseUsageQuota({
1258+
rate_limit: {
1259+
primary_window: { used_percent: 40, reset_at: 1 },
1260+
secondary_window: { used_percent: 20, reset_at: 2 },
1261+
},
1262+
})).toMatchObject({ weeklyPercent: 40, weeklyResetAt: 1 });
1263+
});
1264+
1265+
test("a declared 7-day primary window remains the weekly quota (#1791)", () => {
1266+
expect(parseUsageQuota({
1267+
rate_limit: {
1268+
primary_window: { used_percent: 40, reset_at: 1, limit_window_seconds: 7 * 24 * 60 * 60 },
1269+
secondary_window: { used_percent: 20, reset_at: 2 },
1270+
},
1271+
})).toMatchObject({ weeklyPercent: 40, weeklyResetAt: 1 });
1272+
});
12401273
test("WHAM primary window uses its explicit duration to distinguish weekly and monthly quotas", () => {
12411274
expect(parseUsageQuota({
12421275
plan_type: "team",

0 commit comments

Comments
 (0)