@@ -133,6 +133,18 @@ export type CodexCooldownSource = "retry-after" | "reset-derived" | "default";
133133 */
134134export type CodexQuotaScope = "shared" | "spark" ;
135135
136+ export type CodexQuotaRecoveryProbeClaim = {
137+ accountId : string ;
138+ scope ?: CodexQuotaScope ;
139+ leaseId : string ;
140+ cooldownGeneration : number ;
141+ credentialGeneration : number ;
142+ } ;
143+
144+ export type CodexQuotaRecoveryProbeProof = {
145+ credentialGeneration ?: number ;
146+ } ;
147+
136148/**
137149 * Requests without a resolved native model retain the historic one-account-per-
138150 * thread behavior. Requests with a known quota scope get an independent
@@ -417,6 +429,119 @@ function canAcquireQuotaProbeLease(health: CodexUpstreamHealth | undefined, now:
417429 return now - origin >= CODEX_QUOTA_PROBE_INTERVAL_MS ;
418430}
419431
432+ /**
433+ * Claim due reset-derived cooldown probes without consulting account selection.
434+ * Pool credentials only: the main account has no quota-refresh single-flight.
435+ */
436+ export function claimDueCodexQuotaRecoveryProbes (
437+ config : OcxConfig ,
438+ limit : number ,
439+ now = Date . now ( ) ,
440+ ) : CodexQuotaRecoveryProbeClaim [ ] {
441+ const boundedLimit = Math . max ( 0 , Math . floor ( limit ) ) ;
442+ if ( boundedLimit === 0 ) return [ ] ;
443+ const candidates : Array < {
444+ accountId : string ;
445+ scope ?: CodexQuotaScope ;
446+ health : CodexUpstreamHealth ;
447+ credentialGeneration : number ;
448+ order : number ;
449+ } > = [ ] ;
450+ for ( const [ order , account ] of ( config . codexAccounts ?? [ ] ) . entries ( ) ) {
451+ if ( ! isSelectableCodexPoolAccount ( account )
452+ || isCodexAccountPaused ( config , account . id )
453+ || isAccountNeedsReauth ( account . id ) ) continue ;
454+ const record = readCodexAccountRecord ( account . id ) ;
455+ if ( ! record ?. credential || record . deletedAt != null ) continue ;
456+ const due = [
457+ { scope : undefined , health : upstreamHealth . get ( account . id ) } ,
458+ ...[ ...( quotaScopedHealth . get ( account . id ) ?? [ ] ) ] . map ( ( [ scope , health ] ) => ( { scope, health } ) ) ,
459+ ] . filter ( ( entry ) : entry is { scope ?: CodexQuotaScope ; health : CodexUpstreamHealth } =>
460+ // `spark` is deliberately never claimed. `GET /backend-api/wham/usage` takes no scope
461+ // parameter and returns generic weekly/monthly windows, so its result can never prove a
462+ // spark recovery — a claim here would spend an upstream call to settle `false` every
463+ // time, and (with one claim per account per pass) delay the shared scope that CAN recover.
464+ entry . scope !== "spark"
465+ && entry . health ?. cooldownSource === "reset-derived"
466+ && canAcquireQuotaProbeLease ( entry . health , now ) )
467+ . sort ( ( a , b ) =>
468+ ( a . health . lastProbeAt ?? a . health . cooldownSince ?? 0 )
469+ - ( b . health . lastProbeAt ?? b . health . cooldownSince ?? 0 ) ) ;
470+ const candidate = due [ 0 ] ;
471+ if ( candidate ) candidates . push ( {
472+ accountId : account . id ,
473+ ...( candidate . scope ? { scope : candidate . scope } : { } ) ,
474+ health : candidate . health ,
475+ credentialGeneration : record . generation ,
476+ order,
477+ } ) ;
478+ }
479+ candidates . sort ( ( a , b ) => {
480+ const age = ( a . health . lastProbeAt ?? a . health . cooldownSince ?? 0 )
481+ - ( b . health . lastProbeAt ?? b . health . cooldownSince ?? 0 ) ;
482+ return age || a . order - b . order ;
483+ } ) ;
484+ return candidates . slice ( 0 , boundedLimit ) . map ( candidate => {
485+ const leaseId = randomUUID ( ) ;
486+ const next = {
487+ ...candidate . health ,
488+ probeLeaseId : leaseId ,
489+ probeLeaseGeneration : candidate . health . cooldownGeneration ?? 0 ,
490+ lastProbeAt : now ,
491+ } ;
492+ if ( candidate . scope ) setScopedHealth ( candidate . accountId , candidate . scope , next ) ;
493+ else upstreamHealth . set ( candidate . accountId , next ) ;
494+ return {
495+ accountId : candidate . accountId ,
496+ ...( candidate . scope ? { scope : candidate . scope } : { } ) ,
497+ leaseId,
498+ cooldownGeneration : candidate . health . cooldownGeneration ?? 0 ,
499+ credentialGeneration : candidate . credentialGeneration ,
500+ } ;
501+ } ) ;
502+ }
503+
504+ /** Settle one background recovery claim without mutating account-wide outcome state. */
505+ export function settleCodexQuotaRecoveryProbe (
506+ claim : CodexQuotaRecoveryProbeClaim ,
507+ recovered : boolean ,
508+ proof : CodexQuotaRecoveryProbeProof ,
509+ now = Date . now ( ) ,
510+ ) : boolean {
511+ const health = claim . scope
512+ ? scopedHealthFor ( claim . accountId , claim . scope )
513+ : upstreamHealth . get ( claim . accountId ) ;
514+ if ( ! health || health . probeLeaseId !== claim . leaseId ) return false ;
515+ const fenced = ( health . cooldownGeneration ?? 0 ) === claim . cooldownGeneration
516+ && ( health . probeLeaseGeneration ?? 0 ) === claim . cooldownGeneration
517+ && claim . credentialGeneration === proof . credentialGeneration
518+ && isCodexAccountGenerationLive ( claim . accountId , claim . credentialGeneration ) ;
519+ if ( ! recovered || ! fenced ) {
520+ const released = withProbeLeaseReleased ( health , now ) ;
521+ if ( claim . scope ) setScopedHealth ( claim . accountId , claim . scope , released ) ;
522+ else upstreamHealth . set ( claim . accountId , released ) ;
523+ return false ;
524+ }
525+ if ( claim . scope ) {
526+ deleteScopedHealth ( claim . accountId , claim . scope ) ;
527+ } else {
528+ const {
529+ cooldownUntil : _until ,
530+ cooldownSince : _since ,
531+ cooldownSource : _source ,
532+ probeLeaseId : _leaseId ,
533+ probeLeaseGeneration : _leaseGeneration ,
534+ ...rest
535+ } = health ;
536+ upstreamHealth . set ( claim . accountId , {
537+ ...rest ,
538+ cooldownGeneration : claim . cooldownGeneration + 1 ,
539+ lastProbeAt : now ,
540+ } ) ;
541+ }
542+ return true ;
543+ }
544+
420545/** Acquire the recovery probe for one confirmed model-specific quota group. */
421546export function tryAcquireCodexQuotaScopeProbeLease (
422547 accountId : string ,
0 commit comments