Skip to content

Commit b4310bd

Browse files
committed
fix(codex): fence retry entitlement refresh
1 parent 8011ff8 commit b4310bd

2 files changed

Lines changed: 81 additions & 2 deletions

File tree

‎src/server/responses/core.ts‎

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -924,6 +924,29 @@ type CodexPoolAccountRetryResult =
924924
authCtx: Extract<CodexAuthContext, { kind: "pool" | "main-pool" }>;
925925
};
926926

927+
/** Keep retry-stage entitlement snapshots inside the native-main selection fence. */
928+
async function resolveCodexRetryModelEntitlements(
929+
config: OcxConfig,
930+
resolver: typeof resolveCodexModelEntitlements,
931+
turnAdmissionLease?: AdmissionLease,
932+
): Promise<Awaited<ReturnType<typeof resolveCodexModelEntitlements>>> {
933+
// The initial auth selection has already released its admission before the first
934+
// response arrives. Re-enter for every refresh so profile switching cannot overlap
935+
// credential discovery, and omit main entirely when a drain or recovery owns it.
936+
const selectionAdmission = codexAccountSelectionForTurn(turnAdmissionLease)?.();
937+
const nativeMainReadsForbidden = isNativeMainTrafficBlocked()
938+
|| selectionAdmission?.mainProfileDraining === true;
939+
try {
940+
return await resolver(config, {
941+
excludeAccountIds: nativeMainReadsForbidden
942+
? new Set([MAIN_CODEX_ACCOUNT_ID])
943+
: undefined,
944+
});
945+
} finally {
946+
selectionAdmission?.release();
947+
}
948+
}
949+
927950
const CODEX_ACCOUNT_GATED_CANONICAL_WIRE_MODELS: ReadonlyMap<string, string> = new Map([
928951
// The authenticated catalog currently advertises Daybreak Blue, while successful responses
929952
// identify the serving model as gpt-5.6-sol. Sending the selector itself is shard-dependent:
@@ -1018,7 +1041,11 @@ async function retryCodexPoolOnAlternateAccount(
10181041
invalidateCodexModelEntitlementsForAccount(firstAuthCtx.accountId);
10191042
let refreshed;
10201043
try {
1021-
refreshed = await entitlementResolver(config);
1044+
refreshed = await resolveCodexRetryModelEntitlements(
1045+
config,
1046+
entitlementResolver,
1047+
options.turnAdmissionLease,
1048+
);
10221049
} catch (error) {
10231050
await firstResponse.body?.cancel().catch(() => undefined);
10241051
releaseCodexAuthContextProbeLease(firstAuthCtx);
@@ -1180,7 +1207,11 @@ async function retryCodexPoolOnAlternateAccount(
11801207
invalidateCodexModelEntitlementsForAccount(retryAuthCtx.accountId);
11811208
let refreshed: Awaited<ReturnType<typeof resolveCodexModelEntitlements>>;
11821209
try {
1183-
refreshed = await entitlementResolver(config);
1210+
refreshed = await resolveCodexRetryModelEntitlements(
1211+
config,
1212+
entitlementResolver,
1213+
options.turnAdmissionLease,
1214+
);
11841215
} catch (error) {
11851216
await upstreamResponse.body?.cancel().catch(() => undefined);
11861217
await firstResponse.body?.cancel().catch(() => undefined);

‎tests/subagent-fallback-handle-responses.test.ts‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1726,6 +1726,54 @@ describe("account-gated retry entitlement boundary", () => {
17261726
});
17271727
}
17281728

1729+
test("temporary main drain fences every retry-stage entitlement refresh", async () => {
1730+
const now = 1_800_000_000_000;
1731+
Date.now = () => now;
1732+
installPoolCredential("pool-a", "pool_acc_a", now);
1733+
const cfg = retryConfig();
1734+
let selectionReleases = 0;
1735+
const turnAdmissionLease = {
1736+
release() {},
1737+
beginCodexAccountSelection() {
1738+
return {
1739+
mainProfileDraining: true,
1740+
claimMainProfile: () => false,
1741+
release: () => { selectionReleases += 1; },
1742+
};
1743+
},
1744+
} satisfies Pick<ActiveTurnLease, "release" | "beginCodexAccountSelection">;
1745+
const mainExclusions: boolean[] = [];
1746+
let fetchCalls = 0;
1747+
globalThis.fetch = (async () => {
1748+
fetchCalls += 1;
1749+
if (fetchCalls <= 2) return unsupportedCodexModelResponse(model);
1750+
return Response.json({
1751+
id: "resp_retry_fenced",
1752+
object: "response",
1753+
status: "completed",
1754+
model,
1755+
output: [],
1756+
});
1757+
}) as typeof fetch;
1758+
1759+
const response = await postDirectCodex(
1760+
cfg,
1761+
{ model, input: "hello", stream: false },
1762+
{
1763+
turnAdmissionLease,
1764+
resolveCodexModelEntitlements: async (_config, resolveOptions) => {
1765+
mainExclusions.push(resolveOptions?.excludeAccountIds?.has("__main__") === true);
1766+
return entitlementSnapshot({ "pool-a": [model] });
1767+
},
1768+
},
1769+
);
1770+
1771+
expect(response.status).toBe(200);
1772+
expect(fetchCalls).toBe(3);
1773+
expect(mainExclusions).toEqual([true, true, true]);
1774+
expect(selectionReleases).toBe(3);
1775+
});
1776+
17291777
test("a first-refresh programmer error cancels the 400 and releases its quota probe", async () => {
17301778
const cooldownAt = 1_800_000_000_000;
17311779
const probeAt = cooldownAt + CODEX_QUOTA_PROBE_INTERVAL_MS;

0 commit comments

Comments
 (0)