@@ -9,13 +9,16 @@ import { isCodexAccountUsable } from "./account-usability";
99import { reconcileMainCodexAccountRuntimeState } from "./account-lifecycle" ;
1010import { MAIN_CODEX_ACCOUNT_ID , getMainAccountToken } from "./main-account" ;
1111import {
12- getCodexAccountHealthSnapshot ,
12+ codexQuotaScopeForModel ,
13+ getCodexQuotaHealthSnapshot ,
1314 releaseCodexQuotaProbeLease ,
15+ releaseCodexQuotaScopeProbeLease ,
1416 tryAcquireCodexQuotaProbeLease ,
17+ tryAcquireCodexQuotaScopeProbeLease ,
1518 pickLowestUsageCodexAccount ,
1619 resolveCodexAccountForThreadDetailed ,
1720} from "./routing" ;
18- import type { CodexCooldownSource } from "./routing" ;
21+ import type { CodexCooldownSource , CodexQuotaScope } from "./routing" ;
1922import { maskAccountId } from "../lib/privacy" ;
2023import { formatErrorResponse } from "../bridge" ;
2124import { getAccountQuota } from "./quota" ;
@@ -34,8 +37,12 @@ export type CodexAuthContext =
3437 * Set when this request was admitted through an active quota cooldown as
3538 * the account's single probe. Must be echoed into the upstream outcome so
3639 * only this request can clear the cooldown (#433).
37- */
40+ */
3841 probeLeaseId ?: string ;
42+ /** Native model quota group selected for this request, when known. */
43+ quotaScope ?: CodexQuotaScope ;
44+ /** Scope that owns `probeLeaseId`, when it is a scoped recovery probe. */
45+ probeQuotaScope ?: CodexQuotaScope ;
3946 }
4047 | {
4148 // Main Codex account participating in rotation: token injected from ~/.codex/auth.json
@@ -46,20 +53,29 @@ export type CodexAuthContext =
4653 chatgptAccountId : string ;
4754 /** See `pool.probeLeaseId`. */
4855 probeLeaseId ?: string ;
56+ quotaScope ?: CodexQuotaScope ;
57+ probeQuotaScope ?: CodexQuotaScope ;
4958 } ;
5059
5160/** Probe lease carried by this context, when it holds one. */
5261export function codexProbeLeaseId ( ctx : CodexAuthContext | undefined ) : string | undefined {
5362 return ctx ?. kind === "pool" || ctx ?. kind === "main-pool" ? ctx . probeLeaseId : undefined ;
5463}
5564
65+ /** Scope of a lease carried by this context, when it probes a model-specific quota. */
66+ export function codexProbeQuotaScope ( ctx : CodexAuthContext | undefined ) : CodexQuotaScope | undefined {
67+ return ctx ?. kind === "pool" || ctx ?. kind === "main-pool" ? ctx . probeQuotaScope : undefined ;
68+ }
69+
5670/**
5771 * Hand back a probe lease for a request that will not reach upstream. Safe to
5872 * call with a context that holds no lease.
5973 */
6074export function releaseCodexAuthContextProbeLease ( ctx : CodexAuthContext | undefined ) : void {
6175 const leaseId = codexProbeLeaseId ( ctx ) ;
62- if ( ctx && leaseId ) releaseCodexQuotaProbeLease ( ctx . accountId ! , leaseId ) ;
76+ if ( ! ctx || ctx . kind === "main" || ! leaseId ) return ;
77+ if ( ctx . probeQuotaScope ) releaseCodexQuotaScopeProbeLease ( ctx . accountId ! , ctx . probeQuotaScope , leaseId ) ;
78+ else releaseCodexQuotaProbeLease ( ctx . accountId ! , leaseId ) ;
6379}
6480
6581export type OcxRuntimeProviderConfig = OcxProviderConfig & {
@@ -157,6 +173,8 @@ export function shouldMarkAccountNeedsReauthForCodexAuthFailure(cause: unknown):
157173
158174export interface ResolveCodexAuthContextOptions {
159175 excludeAccountId ?: string ;
176+ /** Final native model selected for this request, used to select its quota group. */
177+ modelId ?: string ;
160178}
161179
162180export async function resolveCodexAuthContext (
@@ -194,14 +212,19 @@ export async function resolveCodexAuthContext(
194212 }
195213 // Snapshot (not just the deadline) so a refused request can report WHY it is cooled:
196214 // a literal Retry-After reads very differently to a user than a reset-derived guess.
197- const cooldown = getCodexAccountHealthSnapshot ( accountId ) ;
215+ const quotaScope = codexQuotaScopeForModel ( options . modelId ) ;
216+ const cooldown = getCodexQuotaHealthSnapshot ( accountId , quotaScope ) ;
198217 const cooldownUntil = cooldown ?. cooldownUntil ;
199218 // A cooled-down account never sends traffic, so upstream recovery can never be
200219 // observed and the cooldown outlives the real limit. Admit one probe per
201220 // interval; its outcome decides whether the cooldown ends (#433).
202221 let probeLeaseId : string | undefined ;
222+ let probeQuotaScope : CodexQuotaScope | undefined ;
203223 if ( cooldownUntil ) {
204- probeLeaseId = tryAcquireCodexQuotaProbeLease ( accountId ) ?? undefined ;
224+ probeQuotaScope = cooldown ?. quotaScope ;
225+ probeLeaseId = probeQuotaScope
226+ ? tryAcquireCodexQuotaScopeProbeLease ( accountId , probeQuotaScope ) ?? undefined
227+ : tryAcquireCodexQuotaProbeLease ( accountId ) ?? undefined ;
205228 if ( ! probeLeaseId ) throw new CodexAccountCooldownError ( accountId , cooldownUntil , cooldown ?. cooldownSource ) ;
206229 }
207230
@@ -210,15 +233,18 @@ export async function resolveCodexAuthContext(
210233 const token = getMainAccountToken ( ) ;
211234 if ( ! token ) {
212235 // Nothing will reach upstream, so give the probe back instead of burning it.
213- if ( probeLeaseId ) releaseCodexQuotaProbeLease ( accountId , probeLeaseId ) ;
236+ if ( probeLeaseId && probeQuotaScope ) releaseCodexQuotaScopeProbeLease ( accountId , probeQuotaScope , probeLeaseId ) ;
237+ else if ( probeLeaseId ) releaseCodexQuotaProbeLease ( accountId , probeLeaseId ) ;
214238 throw new CodexPoolAuthenticationError ( ) ;
215239 }
216240 return {
217241 kind : "main-pool" ,
218242 accountId,
219243 accessToken : token . accessToken ,
220244 chatgptAccountId : token . chatgptAccountId ,
245+ ...( quotaScope ? { quotaScope } : { } ) ,
221246 ...( probeLeaseId ? { probeLeaseId } : { } ) ,
247+ ...( probeQuotaScope ? { probeQuotaScope } : { } ) ,
222248 } ;
223249 }
224250
@@ -230,10 +256,13 @@ export async function resolveCodexAuthContext(
230256 generation : token . generation ,
231257 accessToken : token . accessToken ,
232258 chatgptAccountId : token . chatgptAccountId ,
259+ ...( quotaScope ? { quotaScope } : { } ) ,
233260 ...( probeLeaseId ? { probeLeaseId } : { } ) ,
261+ ...( probeQuotaScope ? { probeQuotaScope } : { } ) ,
234262 } ;
235263 } catch ( cause ) {
236- if ( probeLeaseId ) releaseCodexQuotaProbeLease ( accountId , probeLeaseId ) ;
264+ if ( probeLeaseId && probeQuotaScope ) releaseCodexQuotaScopeProbeLease ( accountId , probeQuotaScope , probeLeaseId ) ;
265+ else if ( probeLeaseId ) releaseCodexQuotaProbeLease ( accountId , probeLeaseId ) ;
237266 if ( shouldMarkAccountNeedsReauthForCodexAuthFailure ( cause ) ) {
238267 markAccountNeedsReauth ( accountId ) ;
239268 }
@@ -245,7 +274,7 @@ export function assertCodexAuthContextNotCooled(ctx: CodexAuthContext | undefine
245274 if ( ctx ?. kind !== "pool" && ctx ?. kind !== "main-pool" ) return ;
246275 // A context holding the probe lease was deliberately admitted through the cooldown.
247276 if ( ctx . probeLeaseId ) return ;
248- const cooldown = getCodexAccountHealthSnapshot ( ctx . accountId ) ;
277+ const cooldown = getCodexQuotaHealthSnapshot ( ctx . accountId , ctx . quotaScope ) ;
249278 if ( cooldown ?. cooldownUntil ) {
250279 throw new CodexAccountCooldownError ( ctx . accountId , cooldown . cooldownUntil , cooldown . cooldownSource ) ;
251280 }
0 commit comments