GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(codex): apply a quota refusal's avoidance window to the main login · agentHits/opencodex@f92e8ee

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • src/codex
    • routing.ts
  • tests/codex-integration
    • codex-routing.test.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/codex/routing.ts‎

Copy file name to clipboardExpand all lines: src/codex/routing.ts
+30-6Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -985,14 +985,27 @@ export function resetCodexRoutingForManualSelection(accountId: string): void {
985985
seedPoolRotationAccount(codexPoolKeyForScope(scope), accountId);
986986
}
987987
}
988-
const current = upstreamHealth.get(accountId);
989-
if (!current) return;
990-
const preserved = preservedCooldownFields(current);
991988
// Quota avoidance is a preference, like the soft avoid dropped above, and an operator naming
992989
// this account has overruled it. The hard cooldown is the part that survives.
993-
const { quotaAvoidUntil: _avoid, ...retained } = preserved;
994-
if (Object.keys(retained).length === 0) upstreamHealth.delete(accountId);
995-
else upstreamHealth.set(accountId, { consecutiveFailures: 0, ...retained });
990+
const overrule = (health: CodexUpstreamHealth) => {
991+
const { quotaAvoidUntil: _avoid, ...retained } = preservedCooldownFields(health);
992+
return retained;
993+
};
994+
const current = upstreamHealth.get(accountId);
995+
if (current) {
996+
const retained = overrule(current);
997+
if (Object.keys(retained).length === 0) upstreamHealth.delete(accountId);
998+
else upstreamHealth.set(accountId, { consecutiveFailures: 0, ...retained });
999+
}
1000+
// A reset-derived refusal records its avoidance on the SCOPED map and returns before the
1001+
// account-wide entry is written, so naming the account has to reach that map too. Stopping
1002+
// at `upstreamHealth` — and returning early when it holds nothing — overruled nothing in
1003+
// the case that produces the avoidance this function exists to overrule.
1004+
for (const [scope, health] of [...(quotaScopedHealth.get(accountId) ?? [])]) {
1005+
const retained = overrule(health);
1006+
if (Object.keys(retained).length === 0) deleteScopedHealth(accountId, scope);
1007+
else setScopedHealth(accountId, scope, { consecutiveFailures: 0, ...retained });
1008+
}
9961009
}
9971010

9981011
export function getCodexAccountCooldownUntil(accountId: string, now = Date.now()): number | null {
@@ -1073,6 +1086,11 @@ export function clearCodexAccountCooldown(accountId: string, now = Date.now()):
10731086
cooldownSource: _source,
10741087
probeLeaseId: _leaseId,
10751088
probeLeaseGeneration: _leaseGeneration,
1089+
// Same reasoning as the probe recovery above: "the quota window moved" is a statement
1090+
// about the whole refusal, so the avoidance it announced goes with the block it
1091+
// produced. Keeping it would leave this escape hatch not escaping, because selection
1092+
// would still pass over the account for as long as the announced window runs.
1093+
quotaAvoidUntil: _avoid,
10761094
...rest
10771095
} = health;
10781096
return {
@@ -1417,6 +1435,12 @@ function getEligiblePoolAccounts(
14171435
&& (!isAccountNeedsReauth(MAIN_CODEX_ACCOUNT_ID) || hasMainAccountRefreshGrant())
14181436
&& getCodexQuotaHealthSnapshot(MAIN_CODEX_ACCOUNT_ID, quotaScope, now) === null
14191437
&& !isCodexAccountSoftAvoided(MAIN_CODEX_ACCOUNT_ID, now)
1438+
// The main login is not in `config.codexAccounts`, so it never passes through the
1439+
// filters above and this is the only place an avoidance window can exclude it. Without
1440+
// this the window a refusal announced applies to the pool but not to the account that
1441+
// earned it: the cooldown caps at fifteen minutes, the window runs up to six hours, and
1442+
// in between the main account returns as a first-class candidate.
1443+
&& !isCodexQuotaAvoided(MAIN_CODEX_ACCOUNT_ID, quotaScope, now)
14201444
&& (!skipFailoverReadyCandidates || !shouldFailover(config, MAIN_CODEX_ACCOUNT_ID, now))
14211445
&& isCodexAccountUsable(config, MAIN_CODEX_ACCOUNT_ID, selectionOptions)
14221446
) {
Collapse file

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

Copy file name to clipboardExpand all lines: tests/codex-integration/codex-routing.test.ts
+75Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -971,6 +971,81 @@ describe("codex routing", () => {
971971
expect(resolveCodexAccountForThread("spark-refused", config, now + 16 * 60_000, "shared")).toBe("a");
972972
});
973973

974+
test("the main login is passed over by the window its own refusal announced", () => {
975+
// The main account is not in `config.codexAccounts`, so it reaches selection through a
976+
// separate re-insertion branch. That branch is the only place an avoidance window can
977+
// exclude it, and it is the case the pool filters above cannot cover.
978+
writeFileSync(join(TEST_DIR, "auth.json"), JSON.stringify({
979+
tokens: { access_token: "main-access", account_id: "main-chatgpt-id" },
980+
}));
981+
const config = makeConfig({
982+
codexAccounts: [{ id: "a", email: "a@test", isMain: false }],
983+
activeCodexAccountId: MAIN_CODEX_ACCOUNT_ID,
984+
});
985+
const now = 1_800_000_000_000;
986+
updateAccountQuota(MAIN_CODEX_ACCOUNT_ID, 10);
987+
updateAccountQuota("a", 20);
988+
expect(resolveCodexAccountForThread("main-spark-first", config, now, "spark")).toBe(MAIN_CODEX_ACCOUNT_ID);
989+
990+
recordCodexUpstreamOutcome(config, MAIN_CODEX_ACCOUNT_ID, 429, {
991+
now,
992+
threadId: "main-spark-first",
993+
modelId: "gpt-5.3-codex-spark",
994+
resetAt: Math.floor((now + 4 * 60 * 60_000) / 1_000),
995+
});
996+
997+
// Selection has to actually reach the re-insertion branch for this to prove anything.
998+
// Leaving the main login active would exclude it by id on the fallback instead, so move
999+
// the active account and put it over the switch threshold: now a cooler candidate is
1000+
// wanted, and the only thing that can keep the main login out is its avoidance window.
1001+
config.activeCodexAccountId = "a";
1002+
updateAccountQuota("a", 85);
1003+
1004+
// The capped cooldown has lapsed and the weekly bar still reads coolest, so without the
1005+
// window this request goes straight back to the account that just refused one.
1006+
expect(resolveCodexAccountForThread("main-spark-next", config, now + 16 * 60_000, "spark")).toBe("a");
1007+
});
1008+
1009+
test("naming the account overrules an avoidance the refusal recorded on the scoped lane", () => {
1010+
const config = makeConfig();
1011+
const now = 1_800_000_000_000;
1012+
updateAccountQuota("a", 10);
1013+
updateAccountQuota("b", 20);
1014+
recordCodexUpstreamOutcome(config, "a", 429, {
1015+
now,
1016+
threadId: "scoped-manual",
1017+
modelId: "gpt-5.3-codex-spark",
1018+
resetAt: Math.floor((now + 4 * 60 * 60_000) / 1_000),
1019+
});
1020+
expect(resolveCodexAccountForThread("scoped-avoided", config, now + 16 * 60_000, "spark")).toBe("b");
1021+
1022+
// A reset-derived refusal writes only the scoped entry, so an operator naming the account
1023+
// has to reach that map. Stopping at the account-wide entry leaves the pick refused.
1024+
config.activeCodexAccountId = "a";
1025+
resetCodexRoutingForManualSelection("a");
1026+
1027+
expect(resolveCodexAccountForThread("scoped-named", config, now + 17 * 60_000, "spark")).toBe("a");
1028+
});
1029+
1030+
test("clearing the cooldown also lifts the avoidance that refusal announced", () => {
1031+
const config = makeConfig();
1032+
const now = 1_800_000_000_000;
1033+
updateAccountQuota("a", 10);
1034+
updateAccountQuota("b", 20);
1035+
recordCodexUpstreamOutcome(config, "a", 429, {
1036+
now,
1037+
modelId: "gpt-5.3-codex-spark",
1038+
resetAt: Math.floor((now + 4 * 60 * 60_000) / 1_000),
1039+
});
1040+
expect(resolveCodexAccountForThread("cleared-before", config, now + 16 * 60_000, "spark")).toBe("b");
1041+
1042+
// The escape hatch has to escape. Automatic probe recovery already drops the window; an
1043+
// operator lifting the same cooldown by hand was leaving it in place for up to six hours.
1044+
expect(clearCodexAccountCooldown("a", now + 60_000)).toBe(true);
1045+
1046+
expect(resolveCodexAccountForThread("cleared-after", config, now + 61_000, "spark")).toBe("a");
1047+
});
1048+
9741049
test("a request the account serves releases the threads its quota refusal moved", () => {
9751050
const config = makeConfig();
9761051
const now = 1_800_000_000_000;

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.