@@ -109,6 +109,7 @@ import {
109109 formatAccountLabel ,
110110 formatCooldown ,
111111 formatWaitTime ,
112+ resolveRuntimeRequestIdentity ,
112113 sanitizeEmail ,
113114 selectBestAccountCandidate ,
114115 shouldUpdateAccountIdFromToken ,
@@ -1495,13 +1496,19 @@ while (attempted.size < Math.max(1, accountCount)) {
14951496 continue ;
14961497 }
14971498
1498- const hadAccountId = ! ! account . accountId ;
1499- const tokenAccountId = extractAccountId ( accountAuth . access ) ;
1500- const accountId = resolveRequestAccountId (
1501- account . accountId ,
1502- account . accountIdSource ,
1503- tokenAccountId ,
1504- ) ;
1499+ const storedAccountId = account . accountId ;
1500+ const storedAccountIdSource = account . accountIdSource ;
1501+ const storedEmail = account . email ;
1502+ const hadAccountId = ! ! storedAccountId ;
1503+ const runtimeIdentity = resolveRuntimeRequestIdentity ( {
1504+ storedAccountId,
1505+ source : storedAccountIdSource ,
1506+ storedEmail,
1507+ accessToken : accountAuth . access ,
1508+ idToken : accountAuth . idToken ,
1509+ } ) ;
1510+ const tokenAccountId = runtimeIdentity . tokenAccountId ;
1511+ const accountId = runtimeIdentity . accountId ;
15051512 if ( ! accountId ) {
15061513 accountManager . markAccountCoolingDown (
15071514 account ,
@@ -1511,19 +1518,13 @@ while (attempted.size < Math.max(1, accountCount)) {
15111518 accountManager . saveToDiskDebounced ( ) ;
15121519 continue ;
15131520 }
1514- const resolvedEmail =
1515- extractAccountEmail ( accountAuth . access ) ?? account . email ;
1521+ const resolvedEmail = runtimeIdentity . email ;
15161522 const entitlementAccountKey = resolveEntitlementAccountKey ( {
1517- accountId : account . accountId ?? accountId ,
1523+ accountId : storedAccountId ?? accountId ,
15181524 email : resolvedEmail ,
15191525 refreshToken : account . refreshToken ,
15201526 index : account . index ,
15211527 } ) ;
1522- account . accountId = accountId ;
1523- if ( ! hadAccountId && tokenAccountId && accountId === tokenAccountId ) {
1524- account . accountIdSource = account . accountIdSource ?? "token" ;
1525- }
1526- account . email = resolvedEmail ;
15271528 const entitlementBlock = entitlementCache . isBlocked (
15281529 entitlementAccountKey ,
15291530 model ?? modelFamily ,
@@ -1536,6 +1537,13 @@ while (attempted.size < Math.max(1, accountCount)) {
15361537 ) ;
15371538 continue ;
15381539 }
1540+ account . accountId = accountId ;
1541+ if ( ! hadAccountId && tokenAccountId && accountId === tokenAccountId ) {
1542+ account . accountIdSource = storedAccountIdSource ?? "token" ;
1543+ }
1544+ if ( resolvedEmail ) {
1545+ account . email = resolvedEmail ;
1546+ }
15391547
15401548 if (
15411549 accountCount > 1 &&
@@ -1594,6 +1602,7 @@ while (attempted.size < Math.max(1, accountCount)) {
15941602
15951603 let sameAccountRetryCount = 0 ;
15961604 let successAccountForResponse = account ;
1605+ let successEntitlementAccountKey = entitlementAccountKey ;
15971606 while ( true ) {
15981607 let response : Response ;
15991608 const fetchStart = performance . now ( ) ;
@@ -2101,19 +2110,58 @@ while (attempted.size < Math.max(1, accountCount)) {
21012110 continue ;
21022111 }
21032112
2104- const fallbackTokenAccountId = extractAccountId ( fallbackAuth . access ) ;
2105- const fallbackAccountId = resolveRequestAccountId (
2106- fallbackAccount . accountId ,
2107- fallbackAccount . accountIdSource ,
2108- fallbackTokenAccountId ,
2109- ) ;
2113+ const fallbackStoredAccountId = fallbackAccount . accountId ;
2114+ const fallbackStoredAccountIdSource = fallbackAccount . accountIdSource ;
2115+ const fallbackStoredEmail = fallbackAccount . email ;
2116+ const hadFallbackAccountId = ! ! fallbackStoredAccountId ;
2117+ const fallbackRuntimeIdentity = resolveRuntimeRequestIdentity ( {
2118+ storedAccountId : fallbackStoredAccountId ,
2119+ source : fallbackStoredAccountIdSource ,
2120+ storedEmail : fallbackStoredEmail ,
2121+ accessToken : fallbackAuth . access ,
2122+ idToken : fallbackAuth . idToken ,
2123+ } ) ;
2124+ const fallbackTokenAccountId = fallbackRuntimeIdentity . tokenAccountId ;
2125+ const fallbackAccountId = fallbackRuntimeIdentity . accountId ;
21102126 if ( ! fallbackAccountId ) {
21112127 continue ;
21122128 }
2129+ const fallbackResolvedEmail = fallbackRuntimeIdentity . email ;
2130+ const fallbackEntitlementAccountKey = resolveEntitlementAccountKey ( {
2131+ accountId : fallbackStoredAccountId ?? fallbackAccountId ,
2132+ email : fallbackResolvedEmail ,
2133+ refreshToken : fallbackAccount . refreshToken ,
2134+ index : fallbackAccount . index ,
2135+ } ) ;
2136+ const fallbackEntitlementBlock = entitlementCache . isBlocked (
2137+ fallbackEntitlementAccountKey ,
2138+ model ?? modelFamily ,
2139+ ) ;
2140+ if ( fallbackEntitlementBlock . blocked ) {
2141+ runtimeMetrics . accountRotations ++ ;
2142+ runtimeMetrics . lastError =
2143+ `Entitlement cached block for account ${ fallbackAccount . index + 1 } ` ;
2144+ logWarn (
2145+ `Skipping account ${ fallbackAccount . index + 1 } due to cached entitlement block (${ formatWaitTime ( fallbackEntitlementBlock . waitMs ) } remaining).` ,
2146+ ) ;
2147+ continue ;
2148+ }
21132149
21142150 if ( ! accountManager . consumeToken ( fallbackAccount , modelFamily , model ) ) {
21152151 continue ;
21162152 }
2153+ fallbackAccount . accountId = fallbackAccountId ;
2154+ if (
2155+ ! hadFallbackAccountId &&
2156+ fallbackTokenAccountId &&
2157+ fallbackAccountId === fallbackTokenAccountId
2158+ ) {
2159+ fallbackAccount . accountIdSource =
2160+ fallbackStoredAccountIdSource ?? "token" ;
2161+ }
2162+ if ( fallbackResolvedEmail ) {
2163+ fallbackAccount . email = fallbackResolvedEmail ;
2164+ }
21172165
21182166 const fallbackHeaders = createCodexHeaders (
21192167 requestInit ,
@@ -2155,7 +2203,7 @@ while (attempted.size < Math.max(1, accountCount)) {
21552203 ) ;
21562204 if ( fallbackSnapshot ) {
21572205 preemptiveQuotaScheduler . update (
2158- `${ resolveEntitlementAccountKey ( fallbackAccount ) } :${ model ?? modelFamily } ` ,
2206+ `${ fallbackEntitlementAccountKey } :${ model ?? modelFamily } ` ,
21592207 fallbackSnapshot ,
21602208 ) ;
21612209 }
@@ -2180,13 +2228,14 @@ while (attempted.size < Math.max(1, accountCount)) {
21802228 accountManager . recordFailure ( fallbackAccount , modelFamily , model ) ;
21812229 }
21822230 capabilityPolicyStore . recordFailure (
2183- resolveEntitlementAccountKey ( fallbackAccount ) ,
2231+ fallbackEntitlementAccountKey ,
21842232 capabilityModelKey ,
21852233 ) ;
21862234 continue ;
21872235 }
21882236
21892237 successAccountForResponse = fallbackAccount ;
2238+ successEntitlementAccountKey = fallbackEntitlementAccountKey ;
21902239 runtimeMetrics . streamFailoverRecoveries += 1 ;
21912240 if ( fallbackAccount . index !== account . index ) {
21922241 runtimeMetrics . streamFailoverCrossAccountRecoveries += 1 ;
@@ -2206,7 +2255,7 @@ while (attempted.size < Math.max(1, accountCount)) {
22062255 accountManager . refundToken ( fallbackAccount , modelFamily , model ) ;
22072256 accountManager . recordFailure ( fallbackAccount , modelFamily , model ) ;
22082257 capabilityPolicyStore . recordFailure (
2209- resolveEntitlementAccountKey ( fallbackAccount ) ,
2258+ fallbackEntitlementAccountKey ,
22102259 capabilityModelKey ,
22112260 ) ;
22122261 logWarn (
@@ -2296,10 +2345,7 @@ while (attempted.size < Math.max(1, accountCount)) {
22962345 if ( successAccountForResponse . index !== account . index ) {
22972346 accountManager . markSwitched ( successAccountForResponse , "rotation" , modelFamily ) ;
22982347 }
2299- const successAccountKey =
2300- successAccountForResponse . index === account . index
2301- ? entitlementAccountKey
2302- : resolveEntitlementAccountKey ( successAccountForResponse ) ;
2348+ const successAccountKey = successEntitlementAccountKey ;
23032349 accountManager . recordSuccess ( successAccountForResponse , modelFamily , model ) ;
23042350 capabilityPolicyStore . recordSuccess (
23052351 successAccountKey ,
0 commit comments