@@ -115,13 +115,20 @@ export class CodexAccountCooldownError extends Error {
115115 accountId : string ;
116116 cooldownUntil : number ;
117117 cooldownSource ?: CodexCooldownSource ;
118+ quotaScope ?: CodexQuotaScope ;
118119
119- constructor ( accountId : string , cooldownUntil : number , cooldownSource ?: CodexCooldownSource ) {
120+ constructor (
121+ accountId : string ,
122+ cooldownUntil : number ,
123+ cooldownSource ?: CodexCooldownSource ,
124+ quotaScope ?: CodexQuotaScope ,
125+ ) {
120126 super ( "Selected Codex account is cooling down" ) ;
121127 this . name = "CodexAccountCooldownError" ;
122128 this . accountId = accountId ;
123129 this . cooldownUntil = cooldownUntil ;
124130 this . cooldownSource = cooldownSource ;
131+ this . quotaScope = quotaScope ;
125132 }
126133}
127134
@@ -143,7 +150,12 @@ export function cooldownAccountLabel(accountId: string): string {
143150 */
144151export function cooldownErrorMessage ( err : CodexAccountCooldownError ) : string {
145152 const until = new Date ( err . cooldownUntil ) . toISOString ( ) ;
146- return `Selected Codex account (${ cooldownAccountLabel ( err . accountId ) } ) is cooling down until ${ until } `
153+ const scope = err . quotaScope === "spark"
154+ ? "Spark quota"
155+ : err . quotaScope === "shared"
156+ ? "shared native quota"
157+ : null ;
158+ return `Selected Codex account (${ cooldownAccountLabel ( err . accountId ) } )${ scope ? ` ${ scope } is` : " is" } cooling down until ${ until } `
147159 + ` (source: ${ err . cooldownSource ?? "default" } ).`
148160 + ` Run 'ocx account list openai' to find the id, then`
149161 + ` 'ocx account clear-cooldown openai <id>' to lift it, or switch accounts with 'ocx account use openai <id>'.` ;
@@ -189,16 +201,17 @@ export async function resolveCodexAuthContext(
189201 }
190202 reconcileMainCodexAccountRuntimeState ( ) ;
191203 const threadId = headers . get ( "x-codex-parent-thread-id" ) ;
204+ const quotaScope = codexQuotaScopeForModel ( options . modelId ) ;
192205 const resolution = options . excludeAccountId
193206 ? ( ( ) => {
194- const accountId = pickLowestUsageCodexAccount ( config , options . excludeAccountId ) ;
207+ const accountId = pickLowestUsageCodexAccount ( config , options . excludeAccountId , Date . now ( ) , quotaScope ) ;
195208 return accountId
196209 ? { status : "selected" as const , accountId }
197210 : { status : "none" as const } ;
198211 } ) ( )
199212 : resolveCodexAccountForThreadDetailed ( threadId , config ) ;
200213 if ( resolution . status === "expired" ) throw new CodexThreadAffinityExpiredError ( resolution . accountId ) ;
201- const accountId = resolution . status === "selected" ? resolution . accountId : null ;
214+ let accountId = resolution . status === "selected" ? resolution . accountId : null ;
202215 if ( ! accountId ) throw new CodexPoolAuthenticationError ( ) ;
203216 // Lazy prime: if the selected account has no quota yet, the pool is likely
204217 // unprimed (dashboard never opened, or startup prime was blocked). Kick a
@@ -212,8 +225,17 @@ export async function resolveCodexAuthContext(
212225 }
213226 // Snapshot (not just the deadline) so a refused request can report WHY it is cooled:
214227 // a literal Retry-After reads very differently to a user than a reset-derived guess.
215- const quotaScope = codexQuotaScopeForModel ( options . modelId ) ;
216- const cooldown = getCodexQuotaHealthSnapshot ( accountId , quotaScope ) ;
228+ let cooldown = getCodexQuotaHealthSnapshot ( accountId , quotaScope ) ;
229+ // Keep session affinity and the active account intact for independent quota
230+ // groups, but use an eligible peer for this model when one exists. This lets
231+ // Spark fail over to another account without moving Terra/Luna off accountId.
232+ if ( cooldown ?. quotaScope ) {
233+ const alternate = pickLowestUsageCodexAccount ( config , accountId , Date . now ( ) , quotaScope ) ;
234+ if ( alternate ) {
235+ accountId = alternate ;
236+ cooldown = getCodexQuotaHealthSnapshot ( accountId , quotaScope ) ;
237+ }
238+ }
217239 const cooldownUntil = cooldown ?. cooldownUntil ;
218240 // A cooled-down account never sends traffic, so upstream recovery can never be
219241 // observed and the cooldown outlives the real limit. Admit one probe per
@@ -225,7 +247,9 @@ export async function resolveCodexAuthContext(
225247 probeLeaseId = probeQuotaScope
226248 ? tryAcquireCodexQuotaScopeProbeLease ( accountId , probeQuotaScope ) ?? undefined
227249 : tryAcquireCodexQuotaProbeLease ( accountId ) ?? undefined ;
228- if ( ! probeLeaseId ) throw new CodexAccountCooldownError ( accountId , cooldownUntil , cooldown ?. cooldownSource ) ;
250+ if ( ! probeLeaseId ) {
251+ throw new CodexAccountCooldownError ( accountId , cooldownUntil , cooldown ?. cooldownSource , cooldown ?. quotaScope ) ;
252+ }
229253 }
230254
231255 if ( accountId === MAIN_CODEX_ACCOUNT_ID ) {
@@ -276,7 +300,7 @@ export function assertCodexAuthContextNotCooled(ctx: CodexAuthContext | undefine
276300 if ( ctx . probeLeaseId ) return ;
277301 const cooldown = getCodexQuotaHealthSnapshot ( ctx . accountId , ctx . quotaScope ) ;
278302 if ( cooldown ?. cooldownUntil ) {
279- throw new CodexAccountCooldownError ( ctx . accountId , cooldown . cooldownUntil , cooldown . cooldownSource ) ;
303+ throw new CodexAccountCooldownError ( ctx . accountId , cooldown . cooldownUntil , cooldown . cooldownSource , cooldown . quotaScope ) ;
280304 }
281305}
282306
0 commit comments