fix(quota): a stale exhausted sample with no reset no longer pauses an account

An exhausted quota window that carries no future reset time and was last seen
more than 2 hours ago now reads as unknown, not as reached. A paused account
receives no fresh quota headers, so one old sample kept it paused forever.
A window with a future reset still pauses until that reset, and a window seen
within 2 hours still pauses.

Finding: sub2api-43.
(cherry picked from commit e0b51b8052e42e475e8554d904b3f96223f181a1)
This commit is contained in:
serenity
2026-11-13 17:38:48 +03:30
parent 7c546a3c71
commit 9a2da1815d
4 changed files with 52 additions and 1 deletions

View File

@@ -0,0 +1 @@
- **fix(quota):** an exhausted quota window with no future reset time no longer pauses an account once its sample is older than 2 hours. A paused account stops getting fresh quota headers, so one stale sample used to keep it paused forever.

View File

@@ -105,6 +105,9 @@ export interface QuotaWindowObservation {
const EXHAUSTED_TTL_MS = 5 * 60 * 1000; // 5 minutes for 429-sourced entries (no resetAt)
const EXHAUSTED_REFRESH_MS = 5 * 60 * 1000; // 5 minutes: recheck exhausted accounts (aligned with TTL)
const REFRESH_INTERVAL_MS = 60 * 1000; // Background tick every 1 minute
// A window with no future reset, last seen this long ago, must not keep an account paused: a paused
// account gets no fresh headers, so the sample would otherwise freeze the pause forever.
const STALE_NO_RESET_RELEASE_MS = 2 * 60 * 60 * 1000;
export const DEFAULT_QUOTA_THRESHOLD_PERCENT = 99;
// ─── State ──────────────────────────────────────────────────────────────────
@@ -284,6 +287,10 @@ export function normalizeConnectionQuotas(rawQuotas: Record<string, unknown>, pr
// ─── Public API ─────────────────────────────────────────────────────────────
function isStaleWithoutReset(entry: QuotaCacheEntry, resetAt: string | null, now: number): boolean {
return resetAt === null && now - entry.fetchedAt >= STALE_NO_RESET_RELEASE_MS;
}
/**
* #17000 — opt-in (QUOTA_WINDOW_BOUNDED_PARK, default off): the per-window policy honours the
* bounded park (#14359) the account-level check already applies — a passed park deadline, or an
@@ -1179,6 +1186,7 @@ export function getQuotaWindowStatus(
reachedThreshold:
windowExpired ||
window.fractionReported === false ||
isStaleWithoutReset(entry, resetAt, now) ||
(entry.provider !== "codex" && isWindowParkReleased(entry, now))
? false
: remainingPercentage <= 0

View File

@@ -819,7 +819,8 @@
"tests/unit/muse-policy-account-marking.test.ts",
"tests/unit/muse-refresh-review-regressions.test.ts",
"tests/unit/stream-lifecycle-timer-cleanup-18669.test.ts",
"tests/unit/auth-selection-skips-full-account.test.ts"
"tests/unit/auth-selection-skips-full-account.test.ts",
"tests/unit/quota-window-stale-no-reset.test.ts"
],
"nodeArgs": [
"--import",

View File

@@ -0,0 +1,41 @@
import test from "node:test";
import assert from "node:assert/strict";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "omni-quota-stale-no-reset-"));
process.env.DATA_DIR = dir;
process.env.DISABLE_SQLITE_AUTO_BACKUP = "true";
delete process.env.QUOTA_WINDOW_BOUNDED_PARK;
const core = await import("../../src/lib/db/core.ts");
const quota = await import("../../src/domain/quotaCache.ts");
const HOUR = 3600000;
const futureReset = new Date(Date.now() + 7 * 86400000).toISOString();
/** An exhausted window last observed `ageMs` ago. */
function seed(id: string, ageMs: number, resetAt?: string) {
quota.setQuotaCache(id, "openai", {
session: { remainingPercentage: 0, ...(resetAt ? { resetAt } : {}) },
});
quota.getQuotaCache(id)!.fetchedAt = Date.now() - ageMs;
}
test.afterEach(() => quota.__clearForTests());
test.after(() => {
quota.stopBackgroundRefresh();
core.resetDbInstance();
fs.rmSync(dir, { recursive: true, force: true });
});
test("an exhausted window with no reset, seen over 2 h ago, no longer pauses the account", () => {
seed("stale", 2 * HOUR + 1);
assert.equal(quota.getQuotaWindowStatus("stale", "session")?.reachedThreshold, false);
});
test("the same window seen 1 h ago still pauses the account", () => {
seed("recent", HOUR);
assert.equal(quota.getQuotaWindowStatus("recent", "session")?.reachedThreshold, true);
});
test("an old window with a future reset keeps pausing the account until that reset", () => {
seed("future", 3 * HOUR, futureReset);
assert.equal(quota.getQuotaWindowStatus("future", "session")?.reachedThreshold, true);
});