Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/codex/routing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1074,12 +1074,20 @@ export function isCodexAccountInCooldown(accountId: string, now = Date.now()): b
* bumps it in {@link recordCodexUpstreamOutcome}, so the bump here is not load-bearing
* today and is kept so the invariant survives a future change that retains the lease.
*
* Returns false when the account carried no live cooldown (already expired or never set).
* Returns false when the account carried neither a live cooldown nor a live avoidance window.
* The window outlives the cooldown by design — the cooldown caps at fifteen minutes and the
* 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. Refusing to look at the window then would leave the hatch shut in the one
* case it exists for.
*/
export function clearCodexAccountCooldown(accountId: string, now = Date.now()): boolean {
const clear = (health: CodexUpstreamHealth): CodexUpstreamHealth | null => {
const cooldownUntil = health.cooldownUntil;
if (typeof cooldownUntil !== "number" || !Number.isFinite(cooldownUntil) || cooldownUntil <= now) return null;
const liveCooldown = typeof cooldownUntil === "number" && Number.isFinite(cooldownUntil) && cooldownUntil > now;
const avoidUntil = health.quotaAvoidUntil;
const liveAvoidance = typeof avoidUntil === "number" && Number.isFinite(avoidUntil) && avoidUntil > now;
if (!liveCooldown && !liveAvoidance) return null;
Comment on lines +1088 to +1090

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Update the owned routing docs with this behavior

This changes shared Codex routing and the operator-facing clear-cooldown contract: a live avoidance window now makes the operation succeed after the hard cooldown expires. However, none of the structure documents mapped to src/codex/ were updated, so documents such as structure/catalog.md and structure/gui-and-management-api.md still omit the new selection and management semantics; update the applicable owned documentation in this change.

AGENTS.md reference: src/AGENTS.md:L10-L11

Useful? React with 👍 / 👎.

const {
cooldownUntil: _until,
cooldownSince: _since,
Expand Down
22 changes: 22 additions & 0 deletions tests/codex-integration/codex-routing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1046,6 +1046,28 @@ describe("codex routing", () => {
expect(resolveCodexAccountForThread("cleared-after", config, now + 61_000, "spark")).toBe("a");
});

test("clearing a lapsed cooldown still lifts the avoidance it left behind", () => {
const config = makeConfig();
const now = 1_800_000_000_000;
updateAccountQuota("a", 10);
updateAccountQuota("b", 20);
recordCodexUpstreamOutcome(config, "a", 429, {
now,
modelId: "gpt-5.3-codex-spark",
resetAt: Math.floor((now + 4 * 60 * 60_000) / 1_000),
});

// The capped cooldown is already gone and only the announced window is still holding the
// account out, which is exactly when an operator reaches for this button. Reading the
// cooldown alone made the call a no-op for the next several hours.
expect(isCodexAccountInCooldown("a", now + 16 * 60_000)).toBe(false);
expect(resolveCodexAccountForThread("lapsed-before", config, now + 16 * 60_000, "spark")).toBe("b");

expect(clearCodexAccountCooldown("a", now + 16 * 60_000)).toBe(true);

expect(resolveCodexAccountForThread("lapsed-after", config, now + 17 * 60_000, "spark")).toBe("a");
});

test("a request the account serves releases the threads its quota refusal moved", () => {
const config = makeConfig();
const now = 1_800_000_000_000;
Expand Down
Loading