From 8100b53fd71c533ebf7a03d8e26f1d6574d9e2f4 Mon Sep 17 00:00:00 2001 From: diegosouzapw Date: Tue, 28 Apr 2026 00:18:30 -0300 Subject: [PATCH] fix(codex): raise default quota threshold from 90% to 99% to avoid premature account blocking (#1697) The previous 90% default treated Codex accounts as unavailable while they still had ~10% quota remaining. A 99% threshold reserves only a minimal safety margin near true exhaustion while preserving usable quota. - Export DEFAULT_QUOTA_THRESHOLD_PERCENT=99 from quotaCache.ts - Replace CODEX_QUOTA_THRESHOLD_PERCENT in auth.ts with shared constant - Update quota policy tests to match new default Co-authored-by: dhaern --- src/domain/quotaCache.ts | 3 ++- src/sse/services/auth.ts | 13 ++++++++++--- tests/unit/quota-policy-generalization.test.ts | 2 +- tests/unit/sse-auth.test.ts | 2 +- 4 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/domain/quotaCache.ts b/src/domain/quotaCache.ts index 759335228c..61017e5123 100644 --- a/src/domain/quotaCache.ts +++ b/src/domain/quotaCache.ts @@ -49,6 +49,7 @@ const ACTIVE_TTL_MS = 5 * 60 * 1000; // 5 minutes for active accounts 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 +export const DEFAULT_QUOTA_THRESHOLD_PERCENT = 99; // ─── State ────────────────────────────────────────────────────────────────── @@ -260,7 +261,7 @@ export function isAccountQuotaExhausted(connectionId: string): boolean { export function getQuotaWindowStatus( connectionId: string, windowName: string, - thresholdPercent = 90 + thresholdPercent = DEFAULT_QUOTA_THRESHOLD_PERCENT ): QuotaWindowStatus | null { const entry = cache.get(connectionId); if (!entry) return null; diff --git a/src/sse/services/auth.ts b/src/sse/services/auth.ts index 6e84b0d975..f2fdbf28f1 100644 --- a/src/sse/services/auth.ts +++ b/src/sse/services/auth.ts @@ -6,7 +6,12 @@ import { getSettings, getCachedSettings, } from "@/lib/localDb"; -import { getQuotaCache, getQuotaWindowStatus, isAccountQuotaExhausted } from "@/domain/quotaCache"; +import { + DEFAULT_QUOTA_THRESHOLD_PERCENT, + getQuotaCache, + getQuotaWindowStatus, + isAccountQuotaExhausted, +} from "@/domain/quotaCache"; import { isAccountUnavailable, getUnavailableUntil, @@ -82,7 +87,6 @@ interface CooldownInspectionState { retryableModelCooldownMs: number | null; } -const CODEX_QUOTA_THRESHOLD_PERCENT = 90; const MIN_QUOTA_THRESHOLD_PERCENT = 1; const MAX_QUOTA_THRESHOLD_PERCENT = 100; const NON_RETRYABLE_MODEL_LOCKOUT_REASONS = new Set(["not_found", "not_found_local"]); @@ -168,7 +172,10 @@ interface QuotaCacheView { >; } -function normalizeQuotaThreshold(value: unknown, fallback = CODEX_QUOTA_THRESHOLD_PERCENT): number { +function normalizeQuotaThreshold( + value: unknown, + fallback = DEFAULT_QUOTA_THRESHOLD_PERCENT +): number { const parsed = toNumber(value, fallback); return Math.min(MAX_QUOTA_THRESHOLD_PERCENT, Math.max(MIN_QUOTA_THRESHOLD_PERCENT, parsed)); } diff --git a/tests/unit/quota-policy-generalization.test.ts b/tests/unit/quota-policy-generalization.test.ts index a99c27a172..7a0eb3480c 100644 --- a/tests/unit/quota-policy-generalization.test.ts +++ b/tests/unit/quota-policy-generalization.test.ts @@ -18,7 +18,7 @@ test("resolveQuotaLimitPolicy keeps codex legacy defaults when generic policy is assert.equal(policy.enabled, true); assert.deepEqual(policy.windows, ["session"]); - assert.equal(policy.thresholdPercent, 90); + assert.equal(policy.thresholdPercent, 99); }); test("resolveQuotaLimitPolicy enforces codex weekly window when weekly toggle is enabled", () => { diff --git a/tests/unit/sse-auth.test.ts b/tests/unit/sse-auth.test.ts index 8c99607eae..2add1820ef 100644 --- a/tests/unit/sse-auth.test.ts +++ b/tests/unit/sse-auth.test.ts @@ -264,7 +264,7 @@ test("resolveQuotaLimitPolicy normalizes Codex windows, thresholds, and defaults }); assert.deepEqual(defaults, { enabled: true, - thresholdPercent: 90, + thresholdPercent: 99, windows: ["session", "weekly"], }); assert.deepEqual(generic, {