Skip to content

Commit c8556f3

Browse files
committed
fix(codex): skip failing quota candidates
1 parent 230e024 commit c8556f3

2 files changed

Lines changed: 116 additions & 2 deletions

File tree

‎src/codex/routing.ts‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,12 +1006,14 @@ function getEligiblePoolAccounts(
10061006
now = Date.now(),
10071007
quotaScope?: CodexQuotaScope,
10081008
selectionOptions?: CodexAccountUsabilityOptions,
1009+
skipFailoverReadyCandidates = false,
10091010
): readonly string[] {
10101011
const ids = (config.codexAccounts ?? [])
10111012
.filter(account => isSelectableCodexPoolAccount(account)
10121013
&& account.id !== excludeId
10131014
&& !isCodexAccountPaused(config, account.id)
1014-
&& !isAccountNeedsReauth(account.id))
1015+
&& !isAccountNeedsReauth(account.id)
1016+
&& (!skipFailoverReadyCandidates || !shouldFailover(config, account.id, now)))
10151017
.filter(account => getCodexQuotaHealthSnapshot(account.id, quotaScope, now) === null)
10161018
.filter(account => !isCodexAccountSoftAvoided(account.id, now))
10171019
.filter(account => isCodexAccountUsable(config, account.id, selectionOptions))
@@ -1024,6 +1026,7 @@ function getEligiblePoolAccounts(
10241026
&& !isAccountNeedsReauth(MAIN_CODEX_ACCOUNT_ID)
10251027
&& getCodexQuotaHealthSnapshot(MAIN_CODEX_ACCOUNT_ID, quotaScope, now) === null
10261028
&& !isCodexAccountSoftAvoided(MAIN_CODEX_ACCOUNT_ID, now)
1029+
&& (!skipFailoverReadyCandidates || !shouldFailover(config, MAIN_CODEX_ACCOUNT_ID, now))
10271030
&& isCodexAccountUsable(config, MAIN_CODEX_ACCOUNT_ID, selectionOptions)
10281031
) {
10291032
ids.unshift(MAIN_CODEX_ACCOUNT_ID);
@@ -1244,10 +1247,18 @@ function pickLowerUsageAccount(
12441247
now: number,
12451248
quotaScope?: CodexQuotaScope,
12461249
selectionOptions?: CodexAccountUsabilityOptions,
1250+
skipFailoverReadyCandidates = false,
12471251
): string {
12481252
let best = active;
12491253
let bestUsage = activeUsage;
1250-
for (const id of getEligiblePoolAccounts(config, active, now, quotaScope, selectionOptions)) {
1254+
for (const id of getEligiblePoolAccounts(
1255+
config,
1256+
active,
1257+
now,
1258+
quotaScope,
1259+
selectionOptions,
1260+
skipFailoverReadyCandidates,
1261+
)) {
12511262
const usage = computeCodexUsageScore(
12521263
getAccountQuota(id),
12531264
getPoolAccountPlanForSelection(config, id, selectionOptions),
@@ -1623,6 +1634,7 @@ function previewReusableAffinityAccount(
16231634
now,
16241635
quotaScope,
16251636
selectionOptions,
1637+
true,
16261638
);
16271639
if (best !== entry.accountId) return best;
16281640
}
@@ -1666,6 +1678,7 @@ function reevaluateAffinityQuota(
16661678
now,
16671679
quotaScope,
16681680
selectionOptions,
1681+
true,
16691682
);
16701683
return best === entry.accountId ? null : best;
16711684
}

‎tests/codex-routing.test.ts‎

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1906,6 +1906,107 @@ describe("codex account selection order", () => {
19061906
expect(resolveCodexAccountForThread(threadId, config, now + 6, "shared")).toBe("b");
19071907
});
19081908

1909+
test("quota detour re-evaluation skips failover-ready cooler candidates", () => {
1910+
const now = 1_800_000_000_000;
1911+
const threadId = "quota-detour-failover-candidate";
1912+
const modelId = "gpt-daybreak-blue-latest";
1913+
const config = makeConfig({
1914+
accountPoolStrategy: "quota",
1915+
activeCodexAccountId: "c",
1916+
codexAccounts: [
1917+
{ id: "a", email: "a@test", isMain: false },
1918+
{ id: "b", email: "b@test", isMain: false },
1919+
{ id: "c", email: "c@test", isMain: false },
1920+
],
1921+
});
1922+
saveTestCredential("c");
1923+
updateAccountQuota("a", 10);
1924+
updateAccountQuota("b", 5);
1925+
updateAccountQuota("c", 10);
1926+
resetCodexRoutingForManualSelection("c");
1927+
expect(resolveCodexAccountForThread(threadId, config, now, "shared")).toBe("c");
1928+
expect(resolveCodexAccountForThreadDetailed(
1929+
threadId,
1930+
config,
1931+
now + 1,
1932+
"shared",
1933+
{ modelEligibleAccountIds: new Set(["a"]) },
1934+
modelId,
1935+
)).toEqual({ status: "selected", accountId: "a" });
1936+
// B is the highest tier after the detour exists. Filtering only after tier
1937+
// selection would drop B without ever exposing healthy C to the picker.
1938+
config.codexAccountPriorities = { b: 2, c: 1 };
1939+
1940+
for (let attempt = 0; attempt < 3; attempt += 1) {
1941+
recordCodexUpstreamOutcome(config, "b", 503, {
1942+
fixedAccount: true,
1943+
now: now + attempt + 2,
1944+
});
1945+
}
1946+
updateAccountQuota("a", 90);
1947+
const resolveAt = now + CODEX_TRANSIENT_SOFT_AVOID_MS + 5;
1948+
const eligible = { modelEligibleAccountIds: new Set(["a", "b", "c"]) };
1949+
1950+
expect(previewCodexAccountForRequest(
1951+
threadId,
1952+
config,
1953+
resolveAt,
1954+
"shared",
1955+
eligible,
1956+
modelId,
1957+
)).toBe("c");
1958+
expect(resolveCodexAccountForThreadDetailed(
1959+
threadId,
1960+
config,
1961+
resolveAt,
1962+
"shared",
1963+
eligible,
1964+
modelId,
1965+
)).toEqual({ status: "selected", accountId: "c" });
1966+
expect(config.activeCodexAccountId).toBe("c");
1967+
expect(config.activeCodexAccountPinned).toBeUndefined();
1968+
expect(getEffectiveActiveCodexAccountId(config)).toBe("c");
1969+
});
1970+
1971+
test("ordinary quota affinity re-evaluation skips a failover-ready higher tier", () => {
1972+
const now = 1_800_000_000_000;
1973+
const threadId = "ordinary-quota-failover-candidate";
1974+
const config = makeConfig({
1975+
accountPoolStrategy: "quota",
1976+
activeCodexAccountId: "a",
1977+
codexAccounts: [
1978+
{ id: "a", email: "a@test", isMain: false },
1979+
{ id: "b", email: "b@test", isMain: false },
1980+
{ id: "c", email: "c@test", isMain: false },
1981+
],
1982+
});
1983+
saveTestCredential("c");
1984+
updateAccountQuota("a", 10);
1985+
updateAccountQuota("b", 5);
1986+
updateAccountQuota("c", 10);
1987+
expect(resolveCodexAccountForThread(threadId, config, now, "shared")).toBe("a");
1988+
config.codexAccountPriorities = { b: 2, c: 1 };
1989+
1990+
for (let attempt = 0; attempt < 3; attempt += 1) {
1991+
recordCodexUpstreamOutcome(config, "b", 503, {
1992+
fixedAccount: true,
1993+
now: now + attempt + 1,
1994+
});
1995+
}
1996+
updateAccountQuota("a", 90);
1997+
const resolveAt = now + CODEX_TRANSIENT_SOFT_AVOID_MS + 4;
1998+
1999+
expect(previewCodexAccountForRequest(threadId, config, resolveAt, "shared")).toBe("c");
2000+
expect(resolveCodexAccountForThreadDetailed(
2001+
threadId,
2002+
config,
2003+
resolveAt,
2004+
"shared",
2005+
)).toEqual({ status: "selected", accountId: "c" });
2006+
expect(config.activeCodexAccountId).toBe("c");
2007+
expect(getEffectiveActiveCodexAccountId(config)).toBe("c");
2008+
});
2009+
19092010
test("model preview and final keep a live detour after ordinary affinity cleanup", () => {
19102011
const now = 1_800_000_000_000;
19112012
const threadId = "detour-after-ordinary-cleanup";

0 commit comments

Comments
 (0)