Skip to content

Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors

fix(quota): await getQuotaStore() in enforce — quota never enforced/recorded (fail-open)

diegosouzapwbe77a03aa00b
diff --git a/src/lib/quota/enforce.ts b/src/lib/quota/enforce.ts
index d951d1516d..249c62c2cc 100644
--- a/src/lib/quota/enforce.ts
+++ b/src/lib/quota/enforce.ts
@@ -103,7 +103,7 @@ export async function enforceQuotaShare(input: EnforceInput): Promise<EnforceDec
: 1;
// 4. For each active dimension, peek consumption and saturation.
- const store = getQuotaStore();
+ const store = await getQuotaStore();
const dimensionsInfo: Array<{
key: { poolId: string; unit: QuotaUnit; window: import("./dimensions").QuotaWindow };
limit: number;
@@ -218,7 +218,7 @@ export async function recordConsumption(input: RecordConsumptionInput): Promise<
const plan = resolvePlan(input.connectionId, input.provider);
if (!plan.dimensions.length) return;
- const store = getQuotaStore();
+ const store = await getQuotaStore();
for (const dim of plan.dimensions) {
const dimKey = { poolId, unit: dim.unit, window: dim.window };
const cost = costForUnit(input.cost, dim.unit);
diff --git a/tests/unit/quota-enforce-await-store.test.ts b/tests/unit/quota-enforce-await-store.test.ts
new file mode 100644
index 0000000000..d1ce7318e5
--- /dev/null
+++ b/tests/unit/quota-enforce-await-store.test.ts
@@ -0,0 +1,32 @@
+/**
+ * Regression: getQuotaStore() is async (returns Promise<QuotaStore>). enforce.ts
+ * called it WITHOUT await, so `store` was a Promise and `store.peek` / `store.consume`
+ * threw "peek is not a function" → enforceQuotaShare failed open on EVERY request and
+ * recordConsumption never recorded. The existing enforce unit tests inject a sync mock
+ * store, so they passed while production was a no-op. This guard asserts every
+ * getQuotaStore() call in enforce.ts is awaited.
+ */
+import test from "node:test";
+import assert from "node:assert/strict";
+import { readFileSync } from "node:fs";
+import { join } from "node:path";
+import { fileURLToPath } from "node:url";
+
+test("enforce.ts awaits every getQuotaStore() call (it is async)", () => {
+ const p = join(
+ fileURLToPath(import.meta.url),
+ "..",
+ "..",
+ "..",
+ "src/lib/quota/enforce.ts"
+ );
+ const src = readFileSync(p, "utf8");
+ const totalCalls = (src.match(/getQuotaStore\(\)/g) || []).length;
+ const awaitedCalls = (src.match(/await\s+getQuotaStore\(\)/g) || []).length;
+ assert.ok(totalCalls > 0, "expected enforce.ts to call getQuotaStore()");
+ assert.equal(
+ awaitedCalls,
+ totalCalls,
+ `every getQuotaStore() must be awaited — found ${totalCalls} call(s), ${awaitedCalls} awaited`
+ );
+});