Skip to content

Commit e06ffba

Browse files
committed
fix(codex): fence retry entitlement refresh
1 parent 20f2da8 commit e06ffba

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
@@ -919,6 +919,29 @@ type CodexPoolAccountRetryResult =
919919
authCtx: Extract<CodexAuthContext, { kind: "pool" | "main-pool" }>;
920920
};
921921

922+
/** Keep retry-stage entitlement snapshots inside the native-main selection fence. */
923+
async function resolveCodexRetryModelEntitlements(
924+
config: OcxConfig,
925+
resolver: typeof resolveCodexModelEntitlements,
926+
turnAdmissionLease?: AdmissionLease,
927+
): Promise<Awaited<ReturnType<typeof resolveCodexModelEntitlements>>> {
928+
// The initial auth selection has already released its admission before the first
929+
// response arrives. Re-enter for every refresh so profile switching cannot overlap
930+
// credential discovery, and omit main entirely when a drain or recovery owns it.
931+
const selectionAdmission = codexAccountSelectionForTurn(turnAdmissionLease)?.();
932+
const nativeMainReadsForbidden = isNativeMainTrafficBlocked()
933+
|| selectionAdmission?.mainProfileDraining === true;
934+
try {
935+
return await resolver(config, {
936+
excludeAccountIds: nativeMainReadsForbidden
937+
? new Set([MAIN_CODEX_ACCOUNT_ID])
938+
: undefined,
939+
});
940+
} finally {
941+
selectionAdmission?.release();
942+
}
943+
}
944+
922945
const CODEX_ACCOUNT_GATED_CANONICAL_WIRE_MODELS: ReadonlyMap<string, string> = new Map([
923946
// The authenticated catalog currently advertises Daybreak Blue, while successful responses
924947
// identify the serving model as gpt-5.6-sol. Sending the selector itself is shard-dependent:
@@ -1013,7 +1036,11 @@ async function retryCodexPoolOnAlternateAccount(
10131036
invalidateCodexModelEntitlementsForAccount(firstAuthCtx.accountId);
10141037
let refreshed;
10151038
try {
1016-
refreshed = await entitlementResolver(config);
1039+
refreshed = await resolveCodexRetryModelEntitlements(
1040+
config,
1041+
entitlementResolver,
1042+
options.turnAdmissionLease,
1043+
);
10171044
} catch (error) {
10181045
await firstResponse.body?.cancel().catch(() => undefined);
10191046
releaseCodexAuthContextProbeLease(firstAuthCtx);
@@ -1175,7 +1202,11 @@ async function retryCodexPoolOnAlternateAccount(
11751202
invalidateCodexModelEntitlementsForAccount(retryAuthCtx.accountId);
11761203
let refreshed: Awaited<ReturnType<typeof resolveCodexModelEntitlements>>;
11771204
try {
1178-
refreshed = await entitlementResolver(config);
1205+
refreshed = await resolveCodexRetryModelEntitlements(
1206+
config,
1207+
entitlementResolver,
1208+
options.turnAdmissionLease,
1209+
);
11791210
} catch (error) {
11801211
await upstreamResponse.body?.cancel().catch(() => undefined);
11811212
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)