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