@@ -235,14 +235,8 @@ export function computeQuotaCooldownUntil(meta: CodexUpstreamOutcomeMeta = {}):
235235 * Returns the lease id, or null when no probe may go out right now.
236236 */
237237export function tryAcquireCodexQuotaProbeLease ( accountId : string , now = Date . now ( ) ) : string | null {
238- const health = upstreamHealth . get ( accountId ) ;
239- if ( ! health ) return null ;
240- const cooldownUntil = health . cooldownUntil ;
241- if ( typeof cooldownUntil !== "number" || ! Number . isFinite ( cooldownUntil ) || cooldownUntil <= now ) return null ;
242- if ( health . cooldownSource === "retry-after" ) return null ;
243- if ( health . probeLeaseId !== undefined ) return null ;
244- const origin = health . lastProbeAt ?? health . cooldownSince ?? cooldownUntil ;
245- if ( now - origin < CODEX_QUOTA_PROBE_INTERVAL_MS ) return null ;
238+ if ( ! canAcquireCodexQuotaProbeLease ( accountId , now ) ) return null ;
239+ const health = upstreamHealth . get ( accountId ) ! ;
246240 const probeLeaseId = randomUUID ( ) ;
247241 upstreamHealth . set ( accountId , {
248242 ...health ,
@@ -253,6 +247,18 @@ export function tryAcquireCodexQuotaProbeLease(accountId: string, now = Date.now
253247 return probeLeaseId ;
254248}
255249
250+ /** Side-effect-free check mirroring {@link tryAcquireCodexQuotaProbeLease} eligibility. */
251+ export function canAcquireCodexQuotaProbeLease ( accountId : string , now = Date . now ( ) ) : boolean {
252+ const health = upstreamHealth . get ( accountId ) ;
253+ if ( ! health ) return false ;
254+ const cooldownUntil = health . cooldownUntil ;
255+ if ( typeof cooldownUntil !== "number" || ! Number . isFinite ( cooldownUntil ) || cooldownUntil <= now ) return false ;
256+ if ( health . cooldownSource === "retry-after" ) return false ;
257+ if ( health . probeLeaseId !== undefined ) return false ;
258+ const origin = health . lastProbeAt ?? health . cooldownSince ?? cooldownUntil ;
259+ return now - origin >= CODEX_QUOTA_PROBE_INTERVAL_MS ;
260+ }
261+
256262/**
257263 * Hand a probe lease back without recording an upstream outcome. Used by paths
258264 * that take a lease and then fail before any request reaches upstream.
@@ -391,7 +397,7 @@ function getEligiblePoolAccounts(config: OcxConfig, excludeId?: string, now = Da
391397 return ids ;
392398}
393399
394- function getPoolAccountPlan ( config : OcxConfig , accountId : string ) : string | undefined {
400+ export function getPoolAccountPlan ( config : OcxConfig , accountId : string ) : string | undefined {
395401 if ( accountId === MAIN_CODEX_ACCOUNT_ID ) return getMainAccountPlan ( ) ;
396402 return ( config . codexAccounts ?? [ ] ) . find ( account => ! account . isMain && account . id === accountId ) ?. plan ;
397403}
@@ -499,6 +505,74 @@ export function resolveCodexAccountForThread(
499505 return resolution . status === "selected" ? resolution . accountId : null ;
500506}
501507
508+ /**
509+ * Side-effect-free preview of the Codex pool account native routing would prefer.
510+ * Used for subagent fallback quota decisions before final auth.
511+ *
512+ * Does not mutate activeCodexAccountId, thread affinity, config on disk, or probe leases.
513+ * Mirrors {@link resolveCodexAccountForThreadDetailed} account choice, including returning a
514+ * configured cooled account so callers can evaluate probe/quota availability.
515+ */
516+ export function previewCodexAccountForRequest (
517+ threadId : string | null ,
518+ config : OcxConfig ,
519+ now = Date . now ( ) ,
520+ ) : string | null {
521+ if ( threadId && threadAccountMap . has ( threadId ) ) {
522+ const entry = threadAccountMap . get ( threadId ) ! ;
523+ if (
524+ ! isThreadAffinityExpired ( entry , now )
525+ && isThreadAffinityGenerationLive ( entry )
526+ && isCodexAccountSelectable ( config , entry . accountId , now )
527+ && ! shouldFailover ( config , entry . accountId , now )
528+ ) {
529+ const threshold = config . autoSwitchThreshold ?? 80 ;
530+ if ( threshold > 0 ) {
531+ const usage = computeCodexUsageScore (
532+ getAccountQuota ( entry . accountId ) ,
533+ getPoolAccountPlan ( config , entry . accountId ) ,
534+ ) ;
535+ if ( usage >= threshold ) {
536+ const best = pickLowerUsageAccount ( config , entry . accountId , usage , now ) ;
537+ if ( best !== entry . accountId ) return best ;
538+ }
539+ }
540+ return entry . accountId ;
541+ }
542+ // Stale/unusable affinity is ignored for preview (no map mutation).
543+ }
544+
545+ let active = config . activeCodexAccountId ?? null ;
546+ if ( ! active ) {
547+ return pickLowestUsageCodexAccount ( config , undefined , now ) ;
548+ }
549+ if ( ! isCodexAccountSelectable ( config , active , now ) ) {
550+ const fallback = pickLowestUsageCodexAccount ( config , active , now ) ;
551+ if ( fallback ) active = fallback ;
552+ else if ( hasConfiguredPoolAccount ( config , active ) ) return active ;
553+ else return null ;
554+ }
555+
556+ const threshold = config . autoSwitchThreshold ?? 80 ;
557+ if ( threshold > 0 ) {
558+ const usage = computeCodexUsageScore ( getAccountQuota ( active ) , getPoolAccountPlan ( config , active ) ) ;
559+ if ( usage >= threshold ) {
560+ active = pickLowerUsageAccount ( config , active , usage , now ) ;
561+ }
562+ }
563+ if ( shouldFailover ( config , active , now ) ) {
564+ const best = pickLowestUsageCodexAccount ( config , active , now ) ;
565+ if ( best ) active = best ;
566+ }
567+ if ( ! isCodexAccountUsable ( config , active ) ) {
568+ return hasConfiguredPoolAccount ( config , active ) ? active : null ;
569+ }
570+ if ( isCodexAccountInCooldown ( active , now ) ) {
571+ return hasConfiguredPoolAccount ( config , active ) ? active : null ;
572+ }
573+ return active ;
574+ }
575+
502576export function resolveCodexAccountForThreadDetailed (
503577 threadId : string | null ,
504578 config : OcxConfig ,
0 commit comments