@@ -22,6 +22,8 @@ import {
2222 hasHeadroomEvidence ,
2323 isAccountQuotaExhausted ,
2424 rankAccountsByHeadroom ,
25+ classifyModelFamilyForQuota ,
26+ type QuotaModelFamily ,
2527} from "./account-quota-rank" ;
2628import {
2729 genericPoolKey ,
@@ -81,13 +83,14 @@ const health = new Map<string, AccountHealth>();
8183/** Provider -> recent eligible-account count. TTL-bounded; never holds credential material. */
8284const presence = new Map < string , PresenceEntry > ( ) ;
8385
84- const healthKey = ( provider : string , accountId : string ) => `${ provider } \u0000${ accountId } ` ;
86+ const healthKey = ( provider : string , accountId : string , family ?: QuotaModelFamily ) =>
87+ family ? `${ provider } \u0000${ accountId } \u0000${ family } ` : `${ provider } \u0000${ accountId } ` ;
8588
86- function isCooled ( provider : string , accountId : string , now : number ) : boolean {
87- const entry = health . get ( healthKey ( provider , accountId ) ) ;
89+ function isCooled ( provider : string , accountId : string , now : number , family ?: QuotaModelFamily ) : boolean {
90+ const entry = health . get ( healthKey ( provider , accountId , family ) ) ;
8891 if ( ! entry ) return false ;
8992 if ( entry . cooldownUntil <= now ) {
90- health . delete ( healthKey ( provider , accountId ) ) ;
93+ health . delete ( healthKey ( provider , accountId , family ) ) ;
9194 return false ;
9295 }
9396 return true ;
@@ -175,11 +178,11 @@ function isProactivePreferenceEnabled(config: OcxConfig, providerName: string, n
175178}
176179
177180/** Accounts that may serve traffic right now: not cooled, not flagged for reauth. */
178- export function eligibleFailoverAccounts ( providerName : string , now = Date . now ( ) ) : string [ ] {
181+ export function eligibleFailoverAccounts ( providerName : string , now = Date . now ( ) , family ?: QuotaModelFamily ) : string [ ] {
179182 const set = getAccountSet ( providerName ) ;
180183 if ( ! set ) return [ ] ;
181184 return set . accounts
182- . filter ( account => account . needsReauth !== true && ! isCooled ( providerName , account . id , now ) )
185+ . filter ( account => account . needsReauth !== true && ! isCooled ( providerName , account . id , now , family ) )
183186 . map ( account => account . id ) ;
184187}
185188
@@ -230,8 +233,8 @@ function stableGenericRoster(providerName: string): string[] {
230233 * statement about observed usage, and treating "no observation" as "spent" would evacuate every
231234 * quota-less provider off its active account on the very first request.
232235 */
233- function isOverAutoSwitchThreshold ( providerName : string , accountId : string , threshold : number ) : boolean {
234- const headroom = accountHeadroomPercent ( providerName , accountId ) ;
236+ function isOverAutoSwitchThreshold ( providerName : string , accountId : string , threshold : number , requestedModelId ?: string | null ) : boolean {
237+ const headroom = accountHeadroomPercent ( providerName , accountId , requestedModelId ) ;
235238 if ( headroom === null ) return false ;
236239 return 100 - headroom >= threshold ;
237240}
@@ -245,15 +248,17 @@ function pickFillFirstGenericAccount(
245248 providerName : string ,
246249 activeId : string | undefined ,
247250 now : number ,
251+ requestedModelId ?: string | null ,
248252) : string | null {
249253 const stableAll = stableGenericRoster ( providerName ) ;
250254 if ( stableAll . length < 2 ) return null ;
251- const eligible = new Set ( eligibleFailoverAccounts ( providerName , now ) ) ;
255+ const family = classifyModelFamilyForQuota ( providerName , requestedModelId ) ;
256+ const eligible = new Set ( eligibleFailoverAccounts ( providerName , now , family ) ) ;
252257 const stored = config . providers ?. [ providerName ] ?. oauthAccountFailover ?. autoSwitchThreshold ;
253258 const threshold = typeof stored === "number" && Number . isInteger ( stored ) && stored >= 0 && stored <= 100
254259 ? stored
255260 : DEFAULT_GENERIC_AUTO_SWITCH_THRESHOLD ;
256- if ( activeId && eligible . has ( activeId ) && ! isOverAutoSwitchThreshold ( providerName , activeId , threshold ) ) {
261+ if ( activeId && eligible . has ( activeId ) && ! isOverAutoSwitchThreshold ( providerName , activeId , threshold , requestedModelId ) ) {
257262 return null ;
258263 }
259264 const start = activeId ? stableAll . indexOf ( activeId ) : - 1 ;
@@ -302,6 +307,7 @@ export function rotateGenericOAuthAccountOn429(
302307 failedAccountId : string ,
303308 retryAfterHeader : string | null | undefined ,
304309 now = Date . now ( ) ,
310+ requestedModelId ?: string | null ,
305311) : string | null {
306312 if ( ! isGenericOAuthFailoverEnabled ( config , providerName ) ) return null ;
307313 const set = getAccountSet ( providerName ) ;
@@ -314,13 +320,14 @@ export function rotateGenericOAuthAccountOn429(
314320 // A Retry-After from upstream still wins — it is the server's own instruction.
315321 const exhausted = parsed === undefined ? exhaustedCooldownMs ( providerName , failedAccountId , now ) : null ;
316322 const cooldownMs = exhausted ?? Math . min ( parsed ?? DEFAULT_COOLDOWN_MS , MAX_COOLDOWN_MS ) ;
317- health . set ( healthKey ( providerName , failedAccountId ) , {
323+ const family = classifyModelFamilyForQuota ( providerName , requestedModelId ) ;
324+ health . set ( healthKey ( providerName , failedAccountId , family ) , {
318325 cooldownUntil : now + cooldownMs ,
319326 cooldownSource : parsed ? "retry-after" : "default" ,
320327 } ) ;
321328 sweepExpiredOnWrite ( now ) ;
322329
323- const eligible = eligibleFailoverAccounts ( providerName , now ) . filter ( id => id !== failedAccountId ) ;
330+ const eligible = eligibleFailoverAccounts ( providerName , now , family ) . filter ( id => id !== failedAccountId ) ;
324331 if ( eligible . length === 0 ) return null ;
325332 // A rotation means the roster in use just changed; do not answer the next activation question
326333 // from a count read before the failure.
@@ -359,7 +366,7 @@ export function rotateGenericOAuthAccountOn429(
359366 }
360367 // With no quota evidence this returns the ring untouched, so providers without
361368 // per-account quota keep exactly the traversal they have today.
362- return rankAccountsByHeadroom ( providerName , candidates ) [ 0 ] ?? null ;
369+ return rankAccountsByHeadroom ( providerName , candidates , requestedModelId ) [ 0 ] ?? null ;
363370}
364371
365372/**
@@ -392,6 +399,7 @@ export function preferredInitialAccount(
392399 config : OcxConfig ,
393400 providerName : string ,
394401 now = Date . now ( ) ,
402+ requestedModelId ?: string | null ,
395403) : string | null {
396404 // The PROACTIVE predicate, not the reactive one: this steers a request upstream has not
397405 // refused, so `oauthAccountFailover.enabled: false` must still be able to refuse it.
@@ -411,7 +419,8 @@ export function preferredInitialAccount(
411419 // would never reach its own test. Cooldowns and reauth are still honoured inside each pick.
412420 const strategy = activeGenericStrategy ( config , providerName ) ;
413421 if ( strategy === "round-robin" ) {
414- const eligibleNow = eligibleFailoverAccounts ( providerName , now ) ;
422+ const family = classifyModelFamilyForQuota ( providerName , requestedModelId ) ;
423+ const eligibleNow = eligibleFailoverAccounts ( providerName , now , family ) ;
415424 if ( eligibleNow . length === 0 ) return null ;
416425 // PEEK, not pick: this proposal is discardable, and advancing the ring for an account the
417426 // resolver then rejects would skip a turn for nothing. noteGenericPoolSelection commits.
@@ -423,26 +432,26 @@ export function preferredInitialAccount(
423432 return picked && picked !== active ? picked : null ;
424433 }
425434 if ( strategy === "fill-first" ) {
426- const picked = pickFillFirstGenericAccount ( config , providerName , active , now ) ;
435+ const picked = pickFillFirstGenericAccount ( config , providerName , active , now , requestedModelId ) ;
427436 return picked && picked !== active ? picked : null ;
428437 }
429438
430439 const activeRow = selected . accounts . find ( account => account . id === active ) ;
431440 if ( activeRow && activeRow . needsReauth !== true
432- && ! isCooled ( providerName , activeRow . id , now )
433- && ! isAccountQuotaExhausted ( providerName , activeRow . id ) ) return null ;
441+ && ! isCooled ( providerName , activeRow . id , now , classifyModelFamilyForQuota ( providerName , requestedModelId ) )
442+ && ! isAccountQuotaExhausted ( providerName , activeRow . id , requestedModelId ) ) return null ;
434443
435444 // Evidence is required BEFORE eligibility narrows the field. Without this, a provider
436445 // with no quota data at all could still be redirected: cool the active account with a
437446 // 429 and the eligible list collapses to one candidate, which any ranking returns
438447 // unchanged — an answer that looks ranked but was never measured. The no-op guarantee
439448 // for quota-less providers has to be checked on the full roster.
440- if ( ! hasHeadroomEvidence ( providerName , order ) ) return null ;
449+ if ( ! hasHeadroomEvidence ( providerName , order , requestedModelId ) ) return null ;
441450
442451 // Cooldowns are respected here, unlike in the presence count: this picks the account to
443452 // send to right now, and one inside its 429 window is the single candidate we hold
444453 // positive evidence against.
445- const eligible = order . filter ( id => ! isCooled ( providerName , id , now ) ) ;
454+ const eligible = order . filter ( id => ! isCooled ( providerName , id , now , classifyModelFamilyForQuota ( providerName , requestedModelId ) ) ) ;
446455 if ( eligible . length === 0 ) return null ;
447456
448457 // Start the ring at the active account so an unranked outcome reproduces today's choice.
@@ -451,7 +460,7 @@ export function preferredInitialAccount(
451460 const candidates = ring . filter ( id => eligible . includes ( id ) ) ;
452461 if ( candidates . length === 0 ) return null ;
453462
454- const best = rankAccountsByHeadroom ( providerName , candidates ) [ 0 ] ?? null ;
463+ const best = rankAccountsByHeadroom ( providerName , candidates , requestedModelId ) [ 0 ] ?? null ;
455464 // Nothing to do when the ranking agrees with the account we would have used anyway.
456465 //
457466 // A proposal still needs guarded selection commit after credential resolution: a
@@ -461,12 +470,10 @@ export function preferredInitialAccount(
461470
462471/** Earliest remaining cooldown, for a client-facing Retry-After when every account is cooled. */
463472export function genericFailoverRetryAfterSeconds ( providerName : string , now = Date . now ( ) ) : number | null {
464- const set = getAccountSet ( providerName ) ;
465- if ( ! set ) return null ;
473+ const prefix = `${ providerName } \u0000` ;
466474 let earliest : number | null = null ;
467- for ( const account of set . accounts ) {
468- const entry = health . get ( healthKey ( providerName , account . id ) ) ;
469- if ( ! entry || entry . cooldownUntil <= now ) continue ;
475+ for ( const [ key , entry ] of health ) {
476+ if ( ! key . startsWith ( prefix ) || entry . cooldownUntil <= now ) continue ;
470477 if ( earliest === null || entry . cooldownUntil < earliest ) earliest = entry . cooldownUntil ;
471478 }
472479 return earliest === null ? null : Math . max ( 1 , Math . ceil ( ( earliest - now ) / 1000 ) ) ;
0 commit comments