@@ -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+
927950const 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 ) ;
0 commit comments