Skip to content

Commit a2b2b06

Browse files
committed
fix: scope Spark cooldowns to its native quota
1 parent e545512 commit a2b2b06

10 files changed

Lines changed: 544 additions & 37 deletions

File tree

‎docs-site/src/content/docs/guides/providers.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,10 @@ Terminal refresh failures mark the account as needing reauthentication instead o
120120
**Cooldowns (Codex pool).** Upstream `429` / quota responses set a hard cooldown from
121121
`Retry-After`, quota `reset` headers (capped), or a short default backoff. Accounts on an explicit
122122
`Retry-After` cooldown are not probed early; reset-derived cooldowns may receive a paced probe lease
123-
so recovery can be detected without flooding the provider.
123+
so recovery can be detected without flooding the provider. Reset-derived native-model cooldowns
124+
also preserve known independent quota groups: `gpt-5.3-codex-spark` does not prevent the same account
125+
from trying the shared GPT-5.6 Terra/Luna quota, while models in that shared group still protect one
126+
another. Explicit `Retry-After` and default cooldowns always remain account-wide.
124127

125128
**Session affinity.** Codex thread→account affinity is process-local (in-memory only; not persisted
126129
across proxy restarts). On credential failures (`401` / `403`) the account is quarantined for

‎src/codex/auth-context.ts‎

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,16 @@ import { isCodexAccountUsable } from "./account-usability";
99
import { reconcileMainCodexAccountRuntimeState } from "./account-lifecycle";
1010
import { MAIN_CODEX_ACCOUNT_ID, getMainAccountToken } from "./main-account";
1111
import {
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";
1922
import { maskAccountId } from "../lib/privacy";
2023
import { formatErrorResponse } from "../bridge";
2124
import { 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. */
5261
export 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
*/
6074
export 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

6581
export type OcxRuntimeProviderConfig = OcxProviderConfig & {
@@ -157,6 +173,8 @@ export function shouldMarkAccountNeedsReauthForCodexAuthFailure(cause: unknown):
157173

158174
export interface ResolveCodexAuthContextOptions {
159175
excludeAccountId?: string;
176+
/** Final native model selected for this request, used to select its quota group. */
177+
modelId?: string;
160178
}
161179

162180
export 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

Comments
 (0)