Skip to content

Commit 60a349b

Browse files
committed
feat(oauth): rank Antigravity failover by Gemini vs Claude quota family
Keep Claude exhaustion from starving Gemini selection, and cool 429s per model family so a Claude limit still leaves the account eligible for Gemini.
1 parent 3ea88f3 commit 60a349b

6 files changed

Lines changed: 231 additions & 41 deletions

File tree

‎scripts/test-layout/layout.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -951,6 +951,7 @@
951951
"nvidia-nim-hardening.test.ts": "providers",
952952
"oauth-account-attribution.test.ts": "oauth",
953953
"oauth-account-id-collision.test.ts": "oauth",
954+
"oauth-account-quota-rank.test.ts": "oauth",
954955
"oauth-accounts-api.test.ts": "oauth",
955956
"oauth-callback-binds.test.ts": "oauth",
956957
"oauth-callback-server.test.ts": "oauth",

‎src/oauth/account-quota-rank.ts‎

Lines changed: 69 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,35 @@
1313
import { getCachedProviderAccountQuota, hasPassiveAccountQuota } from "../providers/quota";
1414
import { getKiroAccountExhaustion } from "../providers/kiro-usage";
1515

16+
/** Antigravity hosts Gemini and Claude windows on one account; ranking must not mix them. */
17+
export type QuotaModelFamily = "gem" | "cla";
18+
19+
export function classifyModelFamilyForQuota(
20+
provider: string,
21+
modelId?: string | null,
22+
): QuotaModelFamily | undefined {
23+
if (provider !== "google-antigravity" || typeof modelId !== "string" || !modelId.trim()) {
24+
return undefined;
25+
}
26+
const id = modelId.toLowerCase();
27+
// Gemma is not Gemini: a substring/prefix match would poison Gemini ranking.
28+
if (/(?:^|[^a-z])gemma(?:[^a-z]|$)/.test(id)) return undefined;
29+
if (/(?:^|[^a-z])gemini(?:[^a-z]|$)/.test(id) || /(?:^|[^a-z])gem(?:[^a-z]|$)/.test(id)) return "gem";
30+
if (
31+
/(?:^|[^a-z])claude(?:[^a-z]|$)/.test(id)
32+
|| /(?:^|[^a-z])opus(?:[^a-z]|$)/.test(id)
33+
|| /(?:^|[^a-z])sonnet(?:[^a-z]|$)/.test(id)
34+
|| /(?:^|[^a-z])haiku(?:[^a-z]|$)/.test(id)
35+
) return "cla";
36+
return undefined;
37+
}
38+
39+
function windowMatchesFamily(label: string, family: QuotaModelFamily): boolean {
40+
const token = label.trim().split(/[\s(/]+/)[0] ?? "";
41+
if (family === "gem") return /^gem(?:ini)?$/i.test(token);
42+
return /^cla(?:ude)?$/i.test(token);
43+
}
44+
1645
/** Lower sorts earlier. Unknown sits between measured-healthy and measured-empty. */
1746
const RANK_HEALTHY = 0;
1847
const RANK_UNKNOWN = 1;
@@ -48,15 +77,24 @@ const PASSIVE_HEADROOM_MAX_AGE_MS = 60 * 60_000;
4877
/**
4978
* Remaining headroom across every window the provider reports.
5079
*
51-
* The minimum wins: an account at 5% of its five-hour window is unusable right now even if
52-
* its monthly allowance is barely touched.
53-
*/
54-
function headroomOf(provider: string, accountId: string): number | null {
80+
* The minimum wins: an account at 5% of its five-hour window is unusable right now even if
81+
* its monthly allowance is barely touched.
82+
*/
83+
function headroomOf(provider: string, accountId: string, requestedModelId?: string | null): number | null {
5584
const quota = getCachedProviderAccountQuota(provider, accountId);
5685
if (!quota) return null;
5786
// Null, not a low rank: this must reproduce "no evidence" so a stale roster degrades to
5887
// the unranked ring rather than to a differently wrong answer.
5988
if (hasPassiveAccountQuota(provider) && Date.now() - quota.updatedAt > PASSIVE_HEADROOM_MAX_AGE_MS) return null;
89+
const family = classifyModelFamilyForQuota(provider, requestedModelId);
90+
if (family) {
91+
const percents = (quota.customWindows ?? [])
92+
.filter(window => windowMatchesFamily(window.label, family))
93+
.map(window => window.percent)
94+
.filter((value): value is number => typeof value === "number");
95+
if (percents.length === 0) return null;
96+
return 100 - Math.max(...percents);
97+
}
6098
const percents = [
6199
quota.fiveHourPercent,
62100
quota.weeklyPercent,
@@ -74,15 +112,23 @@ function headroomOf(provider: string, accountId: string): number | null {
74112
* than an ordering. Null stays null all the way out: a caller must decide what "unmeasured"
75113
* means for its own rule instead of being handed a fabricated 0 or 100.
76114
*/
77-
export function accountHeadroomPercent(provider: string, accountId: string): number | null {
78-
return headroomOf(provider, accountId);
115+
export function accountHeadroomPercent(
116+
provider: string,
117+
accountId: string,
118+
requestedModelId?: string | null,
119+
): number | null {
120+
return headroomOf(provider, accountId, requestedModelId);
79121
}
80122

81123
/** Unknown usage is not exhaustion; Kiro's explicit overage verdict is authoritative. */
82-
export function isAccountQuotaExhausted(provider: string, accountId: string): boolean {
124+
export function isAccountQuotaExhausted(
125+
provider: string,
126+
accountId: string,
127+
requestedModelId?: string | null,
128+
): boolean {
83129
const exhaustion = provider === "kiro" ? getKiroAccountExhaustion(`${provider}\u0000${accountId}`) : null;
84130
if (exhaustion !== null) return exhaustion.exhausted;
85-
const headroom = headroomOf(provider, accountId);
131+
const headroom = headroomOf(provider, accountId, requestedModelId);
86132
return headroom !== null && headroom <= 0;
87133
}
88134

@@ -92,24 +138,28 @@ export function isAccountQuotaExhausted(provider: string, accountId: string): bo
92138
* Returns the input untouched when no candidate has quota evidence, which keeps every
93139
* provider without per-account quota on exactly the behaviour it has today.
94140
*/
95-
export function rankAccountsByHeadroom(provider: string, ring: readonly string[]): string[] {
141+
export function rankAccountsByHeadroom(
142+
provider: string,
143+
ring: readonly string[],
144+
requestedModelId?: string | null,
145+
): string[] {
96146
if (ring.length < 2) return [...ring];
97147

98148
let sawEvidence = false;
99149
// Same rule as hasHeadroomEvidence: a passive provider's partial roster must not rank
100150
// at all. The failover path calls this directly (selectFailoverAccount), so the guard
101151
// cannot live only in the pre-dispatch predicate.
102-
if (hasPassiveAccountQuota(provider) && !ring.every(id => headroomOf(provider, id) !== null)) {
152+
if (hasPassiveAccountQuota(provider) && !ring.every(id => headroomOf(provider, id, requestedModelId) !== null)) {
103153
return [...ring];
104154
}
105155
const ranked: Ranked[] = ring.map((id, index) => {
106156
// A provider-declared exhaustion verdict outranks the percentage: an account may sit at
107157
// 100% and still be servable when overage is enabled, and the verdict knows that.
108158
const exhaustion = provider === "kiro" ? getKiroAccountExhaustion(`${provider}\u0000${id}`) : null;
109-
const headroom = headroomOf(provider, id);
159+
const headroom = headroomOf(provider, id, requestedModelId);
110160
if (exhaustion !== null || headroom !== null) sawEvidence = true;
111161

112-
if (isAccountQuotaExhausted(provider, id)) return { id, bucket: RANK_EXHAUSTED, headroom: 0, index };
162+
if (isAccountQuotaExhausted(provider, id, requestedModelId)) return { id, bucket: RANK_EXHAUSTED, headroom: 0, index };
113163
if (headroom === null) return { id, bucket: RANK_UNKNOWN, headroom: 0, index };
114164
return { id, bucket: RANK_HEALTHY, headroom, index };
115165
});
@@ -129,7 +179,11 @@ export function rankAccountsByHeadroom(provider: string, ring: readonly string[]
129179
* told "ranked" when nothing was measured. Pre-dispatch selection asks this first so it
130180
* can decline to act on a roster it knows nothing about.
131181
*/
132-
export function hasHeadroomEvidence(provider: string, ids: readonly string[]): boolean {
182+
export function hasHeadroomEvidence(
183+
provider: string,
184+
ids: readonly string[],
185+
requestedModelId?: string | null,
186+
): boolean {
133187
// A PASSIVE provider needs evidence for EVERY candidate, not any one of them.
134188
//
135189
// A probe fills the whole roster in one pass (fetchProviderAccountQuotas), so "any"
@@ -140,10 +194,10 @@ export function hasHeadroomEvidence(provider: string, ids: readonly string[]): b
140194
// AWAY from an unmeasured account and TOWARD the one account known to be spent, which
141195
// is the exact inversion of what ranking is for.
142196
if (hasPassiveAccountQuota(provider)) {
143-
return ids.length > 0 && ids.every(id => headroomOf(provider, id) !== null);
197+
return ids.length > 0 && ids.every(id => headroomOf(provider, id, requestedModelId) !== null);
144198
}
145199
return ids.some(id =>
146-
headroomOf(provider, id) !== null
200+
headroomOf(provider, id, requestedModelId) !== null
147201
|| (provider === "kiro" && getKiroAccountExhaustion(`${provider}\u0000${id}`) !== null));
148202
}
149203
/**

‎src/oauth/generic-account-failover.ts‎

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import {
2222
hasHeadroomEvidence,
2323
isAccountQuotaExhausted,
2424
rankAccountsByHeadroom,
25+
classifyModelFamilyForQuota,
26+
type QuotaModelFamily,
2527
} from "./account-quota-rank";
2628
import {
2729
genericPoolKey,
@@ -81,13 +83,14 @@ const health = new Map<string, AccountHealth>();
8183
/** Provider -> recent eligible-account count. TTL-bounded; never holds credential material. */
8284
const presence = new Map<string, PresenceEntry>();
8385

84-
const healthKey = (provider: string, accountId: string) => `${provider}\u0000${accountId}`;
86+
const healthKey = (provider: string, accountId: string, family?: QuotaModelFamily) =>
87+
family ? `${provider}\u0000${accountId}\u0000${family}` : `${provider}\u0000${accountId}`;
8588

86-
function isCooled(provider: string, accountId: string, now: number): boolean {
87-
const entry = health.get(healthKey(provider, accountId));
89+
function isCooled(provider: string, accountId: string, now: number, family?: QuotaModelFamily): boolean {
90+
const entry = health.get(healthKey(provider, accountId, family));
8891
if (!entry) return false;
8992
if (entry.cooldownUntil <= now) {
90-
health.delete(healthKey(provider, accountId));
93+
health.delete(healthKey(provider, accountId, family));
9194
return false;
9295
}
9396
return true;
@@ -175,11 +178,11 @@ function isProactivePreferenceEnabled(config: OcxConfig, providerName: string, n
175178
}
176179

177180
/** Accounts that may serve traffic right now: not cooled, not flagged for reauth. */
178-
export function eligibleFailoverAccounts(providerName: string, now = Date.now()): string[] {
181+
export function eligibleFailoverAccounts(providerName: string, now = Date.now(), family?: QuotaModelFamily): string[] {
179182
const set = getAccountSet(providerName);
180183
if (!set) return [];
181184
return set.accounts
182-
.filter(account => account.needsReauth !== true && !isCooled(providerName, account.id, now))
185+
.filter(account => account.needsReauth !== true && !isCooled(providerName, account.id, now, family))
183186
.map(account => account.id);
184187
}
185188

@@ -230,8 +233,8 @@ function stableGenericRoster(providerName: string): string[] {
230233
* statement about observed usage, and treating "no observation" as "spent" would evacuate every
231234
* quota-less provider off its active account on the very first request.
232235
*/
233-
function isOverAutoSwitchThreshold(providerName: string, accountId: string, threshold: number): boolean {
234-
const headroom = accountHeadroomPercent(providerName, accountId);
236+
function isOverAutoSwitchThreshold(providerName: string, accountId: string, threshold: number, requestedModelId?: string | null): boolean {
237+
const headroom = accountHeadroomPercent(providerName, accountId, requestedModelId);
235238
if (headroom === null) return false;
236239
return 100 - headroom >= threshold;
237240
}
@@ -245,15 +248,17 @@ function pickFillFirstGenericAccount(
245248
providerName: string,
246249
activeId: string | undefined,
247250
now: number,
251+
requestedModelId?: string | null,
248252
): string | null {
249253
const stableAll = stableGenericRoster(providerName);
250254
if (stableAll.length < 2) return null;
251-
const eligible = new Set(eligibleFailoverAccounts(providerName, now));
255+
const family = classifyModelFamilyForQuota(providerName, requestedModelId);
256+
const eligible = new Set(eligibleFailoverAccounts(providerName, now, family));
252257
const stored = config.providers?.[providerName]?.oauthAccountFailover?.autoSwitchThreshold;
253258
const threshold = typeof stored === "number" && Number.isInteger(stored) && stored >= 0 && stored <= 100
254259
? stored
255260
: DEFAULT_GENERIC_AUTO_SWITCH_THRESHOLD;
256-
if (activeId && eligible.has(activeId) && !isOverAutoSwitchThreshold(providerName, activeId, threshold)) {
261+
if (activeId && eligible.has(activeId) && !isOverAutoSwitchThreshold(providerName, activeId, threshold, requestedModelId)) {
257262
return null;
258263
}
259264
const start = activeId ? stableAll.indexOf(activeId) : -1;
@@ -302,6 +307,7 @@ export function rotateGenericOAuthAccountOn429(
302307
failedAccountId: string,
303308
retryAfterHeader: string | null | undefined,
304309
now = Date.now(),
310+
requestedModelId?: string | null,
305311
): string | null {
306312
if (!isGenericOAuthFailoverEnabled(config, providerName)) return null;
307313
const set = getAccountSet(providerName);
@@ -314,13 +320,14 @@ export function rotateGenericOAuthAccountOn429(
314320
// A Retry-After from upstream still wins — it is the server's own instruction.
315321
const exhausted = parsed === undefined ? exhaustedCooldownMs(providerName, failedAccountId, now) : null;
316322
const cooldownMs = exhausted ?? Math.min(parsed ?? DEFAULT_COOLDOWN_MS, MAX_COOLDOWN_MS);
317-
health.set(healthKey(providerName, failedAccountId), {
323+
const family = classifyModelFamilyForQuota(providerName, requestedModelId);
324+
health.set(healthKey(providerName, failedAccountId, family), {
318325
cooldownUntil: now + cooldownMs,
319326
cooldownSource: parsed ? "retry-after" : "default",
320327
});
321328
sweepExpiredOnWrite(now);
322329

323-
const eligible = eligibleFailoverAccounts(providerName, now).filter(id => id !== failedAccountId);
330+
const eligible = eligibleFailoverAccounts(providerName, now, family).filter(id => id !== failedAccountId);
324331
if (eligible.length === 0) return null;
325332
// A rotation means the roster in use just changed; do not answer the next activation question
326333
// from a count read before the failure.
@@ -359,7 +366,7 @@ export function rotateGenericOAuthAccountOn429(
359366
}
360367
// With no quota evidence this returns the ring untouched, so providers without
361368
// per-account quota keep exactly the traversal they have today.
362-
return rankAccountsByHeadroom(providerName, candidates)[0] ?? null;
369+
return rankAccountsByHeadroom(providerName, candidates, requestedModelId)[0] ?? null;
363370
}
364371

365372
/**
@@ -392,6 +399,7 @@ export function preferredInitialAccount(
392399
config: OcxConfig,
393400
providerName: string,
394401
now = Date.now(),
402+
requestedModelId?: string | null,
395403
): string | null {
396404
// The PROACTIVE predicate, not the reactive one: this steers a request upstream has not
397405
// refused, so `oauthAccountFailover.enabled: false` must still be able to refuse it.
@@ -411,7 +419,8 @@ export function preferredInitialAccount(
411419
// would never reach its own test. Cooldowns and reauth are still honoured inside each pick.
412420
const strategy = activeGenericStrategy(config, providerName);
413421
if (strategy === "round-robin") {
414-
const eligibleNow = eligibleFailoverAccounts(providerName, now);
422+
const family = classifyModelFamilyForQuota(providerName, requestedModelId);
423+
const eligibleNow = eligibleFailoverAccounts(providerName, now, family);
415424
if (eligibleNow.length === 0) return null;
416425
// PEEK, not pick: this proposal is discardable, and advancing the ring for an account the
417426
// resolver then rejects would skip a turn for nothing. noteGenericPoolSelection commits.
@@ -423,26 +432,26 @@ export function preferredInitialAccount(
423432
return picked && picked !== active ? picked : null;
424433
}
425434
if (strategy === "fill-first") {
426-
const picked = pickFillFirstGenericAccount(config, providerName, active, now);
435+
const picked = pickFillFirstGenericAccount(config, providerName, active, now, requestedModelId);
427436
return picked && picked !== active ? picked : null;
428437
}
429438

430439
const activeRow = selected.accounts.find(account => account.id === active);
431440
if (activeRow && activeRow.needsReauth !== true
432-
&& !isCooled(providerName, activeRow.id, now)
433-
&& !isAccountQuotaExhausted(providerName, activeRow.id)) return null;
441+
&& !isCooled(providerName, activeRow.id, now, classifyModelFamilyForQuota(providerName, requestedModelId))
442+
&& !isAccountQuotaExhausted(providerName, activeRow.id, requestedModelId)) return null;
434443

435444
// Evidence is required BEFORE eligibility narrows the field. Without this, a provider
436445
// with no quota data at all could still be redirected: cool the active account with a
437446
// 429 and the eligible list collapses to one candidate, which any ranking returns
438447
// unchanged — an answer that looks ranked but was never measured. The no-op guarantee
439448
// for quota-less providers has to be checked on the full roster.
440-
if (!hasHeadroomEvidence(providerName, order)) return null;
449+
if (!hasHeadroomEvidence(providerName, order, requestedModelId)) return null;
441450

442451
// Cooldowns are respected here, unlike in the presence count: this picks the account to
443452
// send to right now, and one inside its 429 window is the single candidate we hold
444453
// positive evidence against.
445-
const eligible = order.filter(id => !isCooled(providerName, id, now));
454+
const eligible = order.filter(id => !isCooled(providerName, id, now, classifyModelFamilyForQuota(providerName, requestedModelId)));
446455
if (eligible.length === 0) return null;
447456

448457
// Start the ring at the active account so an unranked outcome reproduces today's choice.
@@ -451,7 +460,7 @@ export function preferredInitialAccount(
451460
const candidates = ring.filter(id => eligible.includes(id));
452461
if (candidates.length === 0) return null;
453462

454-
const best = rankAccountsByHeadroom(providerName, candidates)[0] ?? null;
463+
const best = rankAccountsByHeadroom(providerName, candidates, requestedModelId)[0] ?? null;
455464
// Nothing to do when the ranking agrees with the account we would have used anyway.
456465
//
457466
// A proposal still needs guarded selection commit after credential resolution: a
@@ -461,12 +470,10 @@ export function preferredInitialAccount(
461470

462471
/** Earliest remaining cooldown, for a client-facing Retry-After when every account is cooled. */
463472
export function genericFailoverRetryAfterSeconds(providerName: string, now = Date.now()): number | null {
464-
const set = getAccountSet(providerName);
465-
if (!set) return null;
473+
const prefix = `${providerName}\u0000`;
466474
let earliest: number | null = null;
467-
for (const account of set.accounts) {
468-
const entry = health.get(healthKey(providerName, account.id));
469-
if (!entry || entry.cooldownUntil <= now) continue;
475+
for (const [key, entry] of health) {
476+
if (!key.startsWith(prefix) || entry.cooldownUntil <= now) continue;
470477
if (earliest === null || entry.cooldownUntil < earliest) earliest = entry.cooldownUntil;
471478
}
472479
return earliest === null ? null : Math.max(1, Math.ceil((earliest - now) / 1000));

‎src/server/responses/core.ts‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4945,7 +4945,7 @@ async function handleResponsesInner(
49454945
// measured as spent. A null answer means "use the active account", so every provider
49464946
// without quota evidence keeps the resolution it has today.
49474947
const preferredAccountId = isGenericFailoverProvider(route.providerName, route.provider)
4948-
? preferredInitialAccount(config, route.providerName)
4948+
? preferredInitialAccount(config, route.providerName, Date.now(), route.modelId)
49494949
: null;
49504950
// Resolved account-scoped, NOT through failoverAccountSnapshot: that helper marks a
49514951
// rotation site, and rotation sites must apply their credential through
@@ -6364,6 +6364,8 @@ async function handleResponsesInner(
63646364
const nextAccountId = rotateGenericOAuthAccountOn429(
63656365
config, route.providerName, genericFailoverAccountId,
63666366
upstreamResponse.headers.get("retry-after"),
6367+
Date.now(),
6368+
route.modelId,
63676369
);
63686370
let snapshot: OAuthAccessSnapshot | undefined;
63696371
if (nextAccountId) {
@@ -7434,6 +7436,8 @@ async function handleResponsesInner(
74347436
route.providerName,
74357437
genericFailoverAccountId,
74367438
retryAfter,
7439+
Date.now(),
7440+
route.modelId,
74377441
);
74387442
if (!nextAccountId) {
74397443
hop.permit?.release();
@@ -7840,6 +7844,8 @@ async function handleResponsesInner(
78407844
route.providerName,
78417845
genericFailoverAccountId,
78427846
null,
7847+
Date.now(),
7848+
route.modelId,
78437849
);
78447850
if (!nextAccountId) {
78457851
hop.permit?.release();
@@ -8628,6 +8634,8 @@ async function handleResponsesInner(
86288634
route.providerName,
86298635
genericFailoverAccountId,
86308636
upstreamResponse.headers.get("retry-after"),
8637+
Date.now(),
8638+
route.modelId,
86318639
);
86328640
if (!nextAccountId) {
86338641
hop.permit?.release();
@@ -9112,6 +9120,8 @@ async function handleResponsesInner(
91129120
route.providerName,
91139121
genericFailoverAccountId,
91149122
response.headers.get("retry-after"),
9123+
Date.now(),
9124+
route.modelId,
91159125
)
91169126
: null;
91179127
if (!nextAccountId) hop.permit?.release();

0 commit comments

Comments
 (0)