Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 86 additions & 1 deletion src/plugin/accounts.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";

import { AccountManager, type ModelFamily, type HeaderStyle, parseRateLimitReason, calculateBackoffMs, type RateLimitReason, resolveQuotaGroup } from "./accounts";
import type { AccountStorageV4 } from "./storage";
Expand Down Expand Up @@ -2063,6 +2063,91 @@ describe("AccountManager", () => {
vi.useRealTimers();
});
});

describe("cached quota exhaustion gate", () => {
const nowValue = new Date("2026-11-16T10:00:00Z").getTime();
const weeklyReset = new Date("2026-11-23T07:24:00Z").toISOString();

function managerWithArgonQuota(
quota: { remainingFraction?: number; resetTime?: string },
extra: { coolingDownUntil?: number; cooldownReason?: "network-error" } = {},
): AccountManager {
const stored: AccountStorageV4 = {
version: 4,
accounts: [
{
email: "joshuaadair.hr@gmail.com",
refreshToken: "r1",
projectId: "p1",
addedAt: 1,
lastUsed: 0,
...extra,
},
],
activeIndex: 0,
};
const manager = new AccountManager(undefined, stored);
manager.updateQuotaCache(0, { argon: quota });
return manager;
}

beforeEach(() => {
vi.useFakeTimers();
vi.setSystemTime(nowValue);
});

afterEach(() => {
vi.useRealTimers();
});

it("keeps a weekly-only bucket with quota left available despite a future resetTime", () => {
// Gemini 4 Argon only exposes a weekly window, so its resetTime is always
// in the future. That alone used to mark the model blocked at 100% left.
const manager = managerWithArgonQuota({ remainingFraction: 1, resetTime: weeklyReset });

expect(manager.getCurrentOrNextForFamily("gemini", "gemini-4-argon-high")).not.toBeNull();
expect(manager.getMinWaitTimeForFamily("gemini", "gemini-4-argon-high")).toBe(0);

const [blocked] = manager.getAllBlockedReasons("gemini", "gemini-4-argon-high");
expect(blocked?.reason).not.toContain("quota exhausted");
expect(blocked?.waitMs).toBeNull();
});

it("reports quota exhausted when the bucket is actually empty", () => {
const manager = managerWithArgonQuota({ remainingFraction: 0, resetTime: weeklyReset });

const [blocked] = manager.getAllBlockedReasons("gemini", "gemini-4-argon-high");
expect(blocked?.reason).toContain("quota exhausted (0% remaining");
expect(blocked?.waitMs).toBe(new Date(weeklyReset).getTime() - nowValue);
});

it("fails open when the cached fraction is missing", () => {
const manager = managerWithArgonQuota({ resetTime: weeklyReset });

expect(manager.getCurrentOrNextForFamily("gemini", "gemini-4-argon-high")).not.toBeNull();

const [blocked] = manager.getAllBlockedReasons("gemini", "gemini-4-argon-high");
expect(blocked?.reason).not.toContain("quota exhausted");
expect(blocked?.waitMs).toBeNull();
});

it("only counts the cached reset as wait time when the group is drained", () => {
const cooldownMs = 60 * 60 * 1000;
const quotaReset = new Date(nowValue + 30 * 60 * 1000).toISOString();

const withQuotaLeft = managerWithArgonQuota(
{ remainingFraction: 1, resetTime: quotaReset },
{ coolingDownUntil: nowValue + cooldownMs, cooldownReason: "network-error" },
);
expect(withQuotaLeft.getMinWaitTimeForFamily("gemini", "gemini-4-argon-high")).toBe(cooldownMs);

const drained = managerWithArgonQuota(
{ remainingFraction: 0, resetTime: quotaReset },
{ coolingDownUntil: nowValue + cooldownMs, cooldownReason: "network-error" },
);
expect(drained.getMinWaitTimeForFamily("gemini", "gemini-4-argon-high")).toBe(30 * 60 * 1000);
});
});
});

describe("resolveQuotaGroup", () => {
Expand Down
41 changes: 36 additions & 5 deletions src/plugin/accounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,33 @@ const MIN_BACKOFF_MS = 2_000;
export const MAX_RPM_RETRY_AFTER_MS = 60_000;
const DEFAULT_QUOTA_CACHE_TTL_MS = 10 * 60 * 1000;

/**
* A cached quota group only counts as drained when its remaining fraction is at
* or below this margin.
*
* The backend reports float noise (e.g. 0.9999999) and weekly-only buckets
* (Gemini 4 Argon) always carry a resetTime in the future, so gating exhaustion
* on resetTime alone declared the model blocked for the whole window even at
* 100% remaining. The margin still flags a truly empty bucket as dry.
*/
const QUOTA_EXHAUSTED_EPSILON = 0.01;

/**
* Remaining fraction of a cached quota group when it is actually exhausted.
*
* Returns `undefined` when the group still has quota OR when the data is
* missing: an unmeasured window is unknown, not empty, so callers must fail
* open instead of turning a whole model off.
*/
function exhaustedRemainingFraction(groupData: QuotaGroupSummary | undefined): number | undefined {
const fraction = groupData?.remainingFraction;
if (typeof fraction !== "number" || !Number.isFinite(fraction)) {
return undefined;
}
const clamped = Math.max(0, Math.min(1, fraction));
return clamped <= QUOTA_EXHAUSTED_EPSILON ? clamped : undefined;
}

/**
* Generate a random jitter value for backoff timing.
* Helps prevent thundering herd problem when multiple clients retry simultaneously.
Expand Down Expand Up @@ -1115,7 +1142,9 @@ export class AccountManager {

const quotaGroup = resolveQuotaGroup(family, model);
const groupData = a.cachedQuota?.[quotaGroup];
if (groupData?.resetTime) {
// Same criterion as getAllBlockedReasons: a future resetTime only counts
// as wait time when the group is actually drained.
if (groupData?.resetTime && exhaustedRemainingFraction(groupData) !== undefined) {
const resetMs = new Date(groupData.resetTime).getTime();
if (resetMs > now) waitTimes.push(resetMs - now);
}
Expand Down Expand Up @@ -1177,10 +1206,13 @@ export class AccountManager {
};
}

// Check soft quota / exhausted cached quota
// Check soft quota / exhausted cached quota. A future resetTime alone is
// not exhaustion (weekly-only buckets always have one), so the group must
// be drained for this reason to apply.
const quotaGroup = resolveQuotaGroup(family, model);
const groupData = a.cachedQuota?.[quotaGroup];
if (groupData?.resetTime) {
const exhaustedFraction = exhaustedRemainingFraction(groupData);
if (groupData?.resetTime && exhaustedFraction !== undefined) {
const resetMs = new Date(groupData.resetTime).getTime();
if (resetMs > now) {
const remainingMs = resetMs - now;
Expand All @@ -1191,10 +1223,9 @@ export class AccountManager {
? `${Math.ceil(remSec / 60)}m`
: `${remSec}s`;
const resetDate = new Date(resetMs).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
const remainingFrac = groupData.remainingFraction ?? 0;
return {
email: label,
reason: `quota exhausted (${(remainingFrac * 100).toFixed(0)}% remaining, resets at ~${resetDate}, in ${durationStr})`,
reason: `quota exhausted (${(exhaustedFraction * 100).toFixed(0)}% remaining, resets at ~${resetDate}, in ${durationStr})`,
waitMs: remainingMs,
};
}
Expand Down
Loading