GeezerVison is showing github.com — fix(codex): apply a quota refusal's avoidance window to the main login · agentHits/opencodex@f92e8ee
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: src/codex/routing.ts +30-6Lines changed: 30 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -985,14 +985,27 @@ export function resetCodexRoutingForManualSelection(accountId: string): void { | 985 | 985 | seedPoolRotationAccount(codexPoolKeyForScope(scope), accountId); | 986 | 986 | } | 987 | 987 | } | 988 | | - const current = upstreamHealth.get(accountId); | 989 | | - if (!current) return; | 990 | | - const preserved = preservedCooldownFields(current); | 991 | 988 | // Quota avoidance is a preference, like the soft avoid dropped above, and an operator naming | 992 | 989 | // 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 | + } | 996 | 1009 | } | 997 | 1010 |
| 998 | 1011 | export function getCodexAccountCooldownUntil(accountId: string, now = Date.now()): number | null { | @@ -1073,6 +1086,11 @@ export function clearCodexAccountCooldown(accountId: string, now = Date.now()): | 1073 | 1086 | cooldownSource: _source, | 1074 | 1087 | probeLeaseId: _leaseId, | 1075 | 1088 | 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, | 1076 | 1094 | ...rest | 1077 | 1095 | } = health; | 1078 | 1096 | return { | @@ -1417,6 +1435,12 @@ function getEligiblePoolAccounts( | 1417 | 1435 | && (!isAccountNeedsReauth(MAIN_CODEX_ACCOUNT_ID) || hasMainAccountRefreshGrant()) | 1418 | 1436 | && getCodexQuotaHealthSnapshot(MAIN_CODEX_ACCOUNT_ID, quotaScope, now) === null | 1419 | 1437 | && !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) | 1420 | 1444 | && (!skipFailoverReadyCandidates || !shouldFailover(config, MAIN_CODEX_ACCOUNT_ID, now)) | 1421 | 1445 | && isCodexAccountUsable(config, MAIN_CODEX_ACCOUNT_ID, selectionOptions) | 1422 | 1446 | ) { | |
Collapse file Copy file name to clipboardExpand all lines: tests/codex-integration/codex-routing.test.ts +75Lines changed: 75 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -971,6 +971,81 @@ describe("codex routing", () => { | 971 | 971 | expect(resolveCodexAccountForThread("spark-refused", config, now + 16 * 60_000, "shared")).toBe("a"); | 972 | 972 | }); | 973 | 973 |
| | 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 | + | 974 | 1049 | test("a request the account serves releases the threads its quota refusal moved", () => { | 975 | 1050 | const config = makeConfig(); | 976 | 1051 | const now = 1_800_000_000_000; | |
|