Skip to content

Commit bd981da

Browse files
committed
fix(codex): let clear-cooldown lift an avoidance the cooldown outlived
clearCodexAccountCooldown returned before it looked at quotaAvoidUntil whenever cooldownUntil had already passed. The two durations have different lengths on purpose — the cooldown caps at fifteen minutes, the announced window runs up to six hours — so the moment an operator actually reaches for this escape hatch is usually after the cooldown lapsed and only the window is still keeping the account out of rotation. The call was a no-op there, and the dashboard reported cleared: false while selection kept passing the account over for hours. Treat a live avoidance window as its own reason to act, and keep the existing contract otherwise: failure counters and softAvoid still survive.
1 parent 4b6849c commit bd981da

2 files changed

Lines changed: 32 additions & 2 deletions

File tree

‎src/codex/routing.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,12 +1074,20 @@ export function isCodexAccountInCooldown(accountId: string, now = Date.now()): b
10741074
* bumps it in {@link recordCodexUpstreamOutcome}, so the bump here is not load-bearing
10751075
* today and is kept so the invariant survives a future change that retains the lease.
10761076
*
1077-
* Returns false when the account carried no live cooldown (already expired or never set).
1077+
* Returns false when the account carried neither a live cooldown nor a live avoidance window.
1078+
* The window outlives the cooldown by design — the cooldown caps at fifteen minutes and the
1079+
* window runs up to six hours — so the moment an operator actually reaches for this escape
1080+
* hatch is usually after the cooldown lapsed and only the window is still keeping the account
1081+
* out of rotation. Refusing to look at the window then would leave the hatch shut in the one
1082+
* case it exists for.
10781083
*/
10791084
export function clearCodexAccountCooldown(accountId: string, now = Date.now()): boolean {
10801085
const clear = (health: CodexUpstreamHealth): CodexUpstreamHealth | null => {
10811086
const cooldownUntil = health.cooldownUntil;
1082-
if (typeof cooldownUntil !== "number" || !Number.isFinite(cooldownUntil) || cooldownUntil <= now) return null;
1087+
const liveCooldown = typeof cooldownUntil === "number" && Number.isFinite(cooldownUntil) && cooldownUntil > now;
1088+
const avoidUntil = health.quotaAvoidUntil;
1089+
const liveAvoidance = typeof avoidUntil === "number" && Number.isFinite(avoidUntil) && avoidUntil > now;
1090+
if (!liveCooldown && !liveAvoidance) return null;
10831091
const {
10841092
cooldownUntil: _until,
10851093
cooldownSince: _since,

‎tests/codex-integration/codex-routing.test.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,6 +1046,28 @@ describe("codex routing", () => {
10461046
expect(resolveCodexAccountForThread("cleared-after", config, now + 61_000, "spark")).toBe("a");
10471047
});
10481048

1049+
test("clearing a lapsed cooldown still lifts the avoidance it left behind", () => {
1050+
const config = makeConfig();
1051+
const now = 1_800_000_000_000;
1052+
updateAccountQuota("a", 10);
1053+
updateAccountQuota("b", 20);
1054+
recordCodexUpstreamOutcome(config, "a", 429, {
1055+
now,
1056+
modelId: "gpt-5.3-codex-spark",
1057+
resetAt: Math.floor((now + 4 * 60 * 60_000) / 1_000),
1058+
});
1059+
1060+
// The capped cooldown is already gone and only the announced window is still holding the
1061+
// account out, which is exactly when an operator reaches for this button. Reading the
1062+
// cooldown alone made the call a no-op for the next several hours.
1063+
expect(isCodexAccountInCooldown("a", now + 16 * 60_000)).toBe(false);
1064+
expect(resolveCodexAccountForThread("lapsed-before", config, now + 16 * 60_000, "spark")).toBe("b");
1065+
1066+
expect(clearCodexAccountCooldown("a", now + 16 * 60_000)).toBe(true);
1067+
1068+
expect(resolveCodexAccountForThread("lapsed-after", config, now + 17 * 60_000, "spark")).toBe("a");
1069+
});
1070+
10491071
test("a request the account serves releases the threads its quota refusal moved", () => {
10501072
const config = makeConfig();
10511073
const now = 1_800_000_000_000;

0 commit comments

Comments
 (0)