Skip to content

Commit 28a2045

Browse files
committed
fix(codex): preserve main claims and model detours
1 parent b4310bd commit 28a2045

7 files changed

Lines changed: 901 additions & 105 deletions

File tree

‎src/codex/auth-context.ts‎

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -361,21 +361,50 @@ export async function resolveCodexAuthContext(
361361
// selected stored credential even while the canonical OpenAI provider is globally Direct.
362362
if (mode === "direct" && fixedAccountId === undefined) {
363363
if (!hasCallerCodexBearer(headers)) throw new CodexDirectAuthenticationError();
364-
if (options.modelId && ACCOUNT_GATED_NATIVE_OPENAI_MODELS.has(options.modelId)) {
365-
const entitled = options.substituteMainCredentialForDirect
366-
? entitledCodexAccountIdsForModel(
367-
await (options.resolveCodexModelEntitlements ?? resolveCodexModelEntitlements)(config),
368-
options.modelId,
369-
)?.has(MAIN_CODEX_ACCOUNT_ID) === true
370-
: await (options.isDirectCallerEntitledToCodexModel ?? isDirectCallerEntitledToCodexModel)(
371-
headers,
372-
options.modelId,
373-
);
374-
if (!entitled) {
375-
throw new CodexPoolAuthenticationError("The selected ChatGPT account does not support this model");
364+
const substituteStoredMain = options.substituteMainCredentialForDirect === true;
365+
if (!substituteStoredMain) {
366+
if (options.modelId && ACCOUNT_GATED_NATIVE_OPENAI_MODELS.has(options.modelId)) {
367+
const entitled = await (
368+
options.isDirectCallerEntitledToCodexModel ?? isDirectCallerEntitledToCodexModel
369+
)(headers, options.modelId);
370+
if (!entitled) {
371+
throw new CodexPoolAuthenticationError("The selected ChatGPT account does not support this model");
372+
}
376373
}
374+
return { kind: "main", accountId: null };
375+
}
376+
377+
// Admission-bearer Direct requests later replace the proxy secret with the stored
378+
// native-main credential. Reserve and claim that physical profile before entitlement
379+
// discovery or materialization can read it; caller-owned Direct credentials never enter
380+
// this branch. A missing turn admission must fail closed instead of recreating an
381+
// untracked native-main read.
382+
if (isNativeMainTrafficBlocked()) throw new CodexMainProfileDrainingError();
383+
const directSelectionAdmission = options.beginCodexAccountSelection?.();
384+
if (!directSelectionAdmission) throw new CodexMainProfileDrainingError();
385+
try {
386+
if (
387+
directSelectionAdmission.mainProfileDraining
388+
|| !directSelectionAdmission.claimMainProfile()
389+
|| isNativeMainTrafficBlocked()
390+
) {
391+
throw new CodexMainProfileDrainingError();
392+
}
393+
if (options.modelId && ACCOUNT_GATED_NATIVE_OPENAI_MODELS.has(options.modelId)) {
394+
const entitled = entitledCodexAccountIdsForModel(
395+
await (options.resolveCodexModelEntitlements ?? resolveCodexModelEntitlements)(config),
396+
options.modelId,
397+
)?.has(MAIN_CODEX_ACCOUNT_ID) === true;
398+
if (!entitled) {
399+
throw new CodexPoolAuthenticationError("The selected ChatGPT account does not support this model");
400+
}
401+
}
402+
return { kind: "main", accountId: null };
403+
} finally {
404+
// The short selector reservation ends here. A successful claim remains owned by
405+
// the enclosing turn lease until the request or transferred stream settles.
406+
directSelectionAdmission.release();
377407
}
378-
return { kind: "main", accountId: null };
379408
}
380409
const affinityKey = fixedAccountId === undefined ? codexPoolAffinityKey(headers) : undefined;
381410
// Retained startup recovery makes the physical main identity ineligible. Routing
@@ -426,7 +455,14 @@ export async function resolveCodexAuthContext(
426455
? { status: "selected" as const, accountId: selected }
427456
: { status: "none" as const };
428457
})()
429-
: resolveCodexAccountForThreadDetailed(affinityKey ?? null, config, Date.now(), quotaScope, selectionOptions);
458+
: resolveCodexAccountForThreadDetailed(
459+
affinityKey ?? null,
460+
config,
461+
Date.now(),
462+
quotaScope,
463+
selectionOptions,
464+
options.modelId,
465+
);
430466
if (resolution.status === "expired") throw new CodexThreadAffinityExpiredError(resolution.accountId);
431467
const selected = resolution.status === "selected" ? resolution.accountId : null;
432468
if (!selected) {

0 commit comments

Comments
 (0)