Skip to content

Commit 80d6794

Browse files
committed
fix(codex): address routing review follow-ups
1 parent 28a2045 commit 80d6794

2 files changed

Lines changed: 75 additions & 69 deletions

File tree

‎src/codex/routing.ts‎

Lines changed: 55 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1631,6 +1631,45 @@ function previewReusableAffinityAccount(
16311631
return entry.accountId;
16321632
}
16331633

1634+
/**
1635+
* Re-evaluate an affined account under the quota strategy. Returns a strictly
1636+
* cooler replacement, or null when the current binding should remain.
1637+
*/
1638+
function reevaluateAffinityQuota(
1639+
entry: ThreadAffinityEntry,
1640+
config: OcxConfig,
1641+
now: number,
1642+
quotaScope?: CodexQuotaScope,
1643+
selectionOptions?: CodexAccountUsabilityOptions,
1644+
): string | null {
1645+
if (normalizeAccountPoolStrategy(config.accountPoolStrategy) !== "quota") return null;
1646+
const threshold = config.autoSwitchThreshold ?? 80;
1647+
const usage = threshold > 0
1648+
? computeCodexUsageScore(
1649+
getAccountQuota(entry.accountId),
1650+
getPoolAccountPlanForSelection(config, entry.accountId, selectionOptions),
1651+
)
1652+
: 0;
1653+
const overThreshold = threshold > 0 && !isUnknownUsage(usage) && usage >= threshold;
1654+
if (
1655+
!overThreshold
1656+
&& now - entry.lastReevalAt < CODEX_THREAD_AFFINITY_REEVAL_INTERVAL_MS
1657+
) {
1658+
return null;
1659+
}
1660+
entry.lastReevalAt = now;
1661+
if (!overThreshold) return null;
1662+
const best = pickLowerUsageAccount(
1663+
config,
1664+
entry.accountId,
1665+
usage,
1666+
now,
1667+
quotaScope,
1668+
selectionOptions,
1669+
);
1670+
return best === entry.accountId ? null : best;
1671+
}
1672+
16341673
/**
16351674
* Side-effect-free preview of the Codex pool account native routing would prefer.
16361675
* Used for subagent fallback quota decisions before final auth.
@@ -1766,35 +1805,16 @@ export function resolveCodexAccountForThreadDetailed(
17661805
// Model detours follow the same affinity policy as ordinary bindings:
17671806
// RR/fill-first stay sticky, while quota strategy may re-evaluate an
17681807
// over-threshold account without changing the ordinary lane.
1769-
if (normalizeAccountPoolStrategy(config.accountPoolStrategy) === "quota") {
1770-
const threshold = config.autoSwitchThreshold ?? 80;
1771-
const usage = threshold > 0
1772-
? computeCodexUsageScore(
1773-
getAccountQuota(detourEntry.accountId),
1774-
getPoolAccountPlanForSelection(config, detourEntry.accountId, selectionOptions),
1775-
)
1776-
: 0;
1777-
const overThreshold = threshold > 0 && !isUnknownUsage(usage) && usage >= threshold;
1778-
if (
1779-
overThreshold
1780-
|| now - detourEntry.lastReevalAt >= CODEX_THREAD_AFFINITY_REEVAL_INTERVAL_MS
1781-
) {
1782-
detourEntry.lastReevalAt = now;
1783-
if (overThreshold) {
1784-
const best = pickLowerUsageAccount(
1785-
config,
1786-
detourEntry.accountId,
1787-
usage,
1788-
now,
1789-
quotaScope,
1790-
selectionOptions,
1791-
);
1792-
if (best !== detourEntry.accountId) {
1793-
bindModelDetourAffinity(threadId, best, now, modelId, quotaScope);
1794-
return { status: "selected", accountId: best };
1795-
}
1796-
}
1797-
}
1808+
const cooler = reevaluateAffinityQuota(
1809+
detourEntry,
1810+
config,
1811+
now,
1812+
quotaScope,
1813+
selectionOptions,
1814+
);
1815+
if (cooler) {
1816+
bindModelDetourAffinity(threadId, cooler, now, modelId, quotaScope);
1817+
return { status: "selected", accountId: cooler };
17981818
}
17991819
return { status: "selected", accountId: detourEntry.accountId };
18001820
}
@@ -1834,29 +1854,13 @@ export function resolveCodexAccountForThreadDetailed(
18341854
// serving for up to 60s after a secondary with quota is available (#584).
18351855
// Non-quota strategies (RR / fill-first) keep affinity for ongoing threads —
18361856
// rotation is new-session-only (affinity policy A).
1837-
const strategy = normalizeAccountPoolStrategy(config.accountPoolStrategy);
1838-
if (strategy === "quota") {
1839-
const threshold = config.autoSwitchThreshold ?? 80;
1840-
const usage = threshold > 0
1841-
? computeCodexUsageScore(
1842-
getAccountQuota(entry.accountId),
1843-
getPoolAccountPlanForSelection(config, entry.accountId, selectionOptions),
1844-
)
1845-
: 0;
1846-
const overThreshold = threshold > 0 && !isUnknownUsage(usage) && usage >= threshold;
1847-
if (overThreshold || now - entry.lastReevalAt >= CODEX_THREAD_AFFINITY_REEVAL_INTERVAL_MS) {
1848-
entry.lastReevalAt = now;
1849-
if (overThreshold) {
1850-
const best = pickLowerUsageAccount(config, entry.accountId, usage, now, quotaScope, selectionOptions);
1851-
if (best !== entry.accountId) {
1852-
if (!isIndependentCodexQuotaScope(quotaScope)) {
1853-
setActiveCodexAccount(config, best);
1854-
}
1855-
bindThreadAffinity(threadId, best, now, quotaScope); // rebinds + resets clocks
1856-
return { status: "selected", accountId: best };
1857-
}
1858-
}
1857+
const cooler = reevaluateAffinityQuota(entry, config, now, quotaScope, selectionOptions);
1858+
if (cooler) {
1859+
if (!isIndependentCodexQuotaScope(quotaScope)) {
1860+
setActiveCodexAccount(config, cooler);
18591861
}
1862+
bindThreadAffinity(threadId, cooler, now, quotaScope); // rebinds + resets clocks
1863+
return { status: "selected", accountId: cooler };
18601864
}
18611865
return { status: "selected", accountId: entry.accountId };
18621866
}

‎tests/bearer-admission-routed-provider.test.ts‎

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -278,10 +278,11 @@ describe("an admission bearer never reaches a canonical ChatGPT transport, whate
278278
);
279279

280280
const server = startServer(0, { inspectNativeCodexOwnership });
281-
await waitForNativeMainStartupGate();
282-
const drain = acquireNativeMainProfileDrain("custom-forward-substitution");
283-
expect(drain).not.toBeNull();
281+
let drain: ReturnType<typeof acquireNativeMainProfileDrain> = null;
284282
try {
283+
await waitForNativeMainStartupGate();
284+
drain = acquireNativeMainProfileDrain("custom-forward-substitution");
285+
expect(drain).not.toBeNull();
285286
const response = await postResponses(server.url, "mirror/gpt-5.5");
286287

287288
expect(response.status).toBe(503);
@@ -318,21 +319,22 @@ describe("an admission bearer never reaches a canonical ChatGPT transport, whate
318319
}) as typeof fetch;
319320

320321
const server = startServer(0, { inspectNativeCodexOwnership });
321-
await waitForNativeMainStartupGate();
322-
const pending = postResponses(server.url, "mirror/gpt-5.5");
323-
const switchUrl = new URL("http://localhost/api/native-main-profiles/switch");
324-
const switchRequest = () => new Request(switchUrl, {
325-
method: "POST",
326-
body: JSON.stringify({ target: "target", confirmedStopped: true }),
327-
});
328-
let switches = 0;
329-
const manager = {
330-
switch: async () => {
331-
switches += 1;
332-
return { ok: true };
333-
},
334-
} as unknown as NativeProfileManager;
322+
let pending: Promise<Response> | null = null;
335323
try {
324+
await waitForNativeMainStartupGate();
325+
pending = postResponses(server.url, "mirror/gpt-5.5");
326+
const switchUrl = new URL("http://localhost/api/native-main-profiles/switch");
327+
const switchRequest = () => new Request(switchUrl, {
328+
method: "POST",
329+
body: JSON.stringify({ target: "target", confirmedStopped: true }),
330+
});
331+
let switches = 0;
332+
const manager = {
333+
switch: async () => {
334+
switches += 1;
335+
return { ok: true };
336+
},
337+
} as unknown as NativeProfileManager;
336338
await upstreamStarted;
337339
expect(getNativeMainProfileRequestCount()).toBe(1);
338340
const blocked = await handleNativeProfileAPI(
@@ -359,7 +361,7 @@ describe("an admission bearer never reaches a canonical ChatGPT transport, whate
359361
expect(switches).toBe(1);
360362
} finally {
361363
releaseUpstream();
362-
await pending.catch(() => {});
364+
await pending?.catch(() => {});
363365
await server.stop(true);
364366
}
365367
});

0 commit comments

Comments
 (0)