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): unwrap pool usage response so quota-share page renders pools with allocations

diegosouzapwcf9a78ad32b3
diff --git a/src/app/(dashboard)/dashboard/costs/quota-share/components/StackedAllocationBar.tsx b/src/app/(dashboard)/dashboard/costs/quota-share/components/StackedAllocationBar.tsx
index d9cf7f1720..ef46ff6e78 100644
--- a/src/app/(dashboard)/dashboard/costs/quota-share/components/StackedAllocationBar.tsx
+++ b/src/app/(dashboard)/dashboard/costs/quota-share/components/StackedAllocationBar.tsx
@@ -39,9 +39,9 @@ export default function StackedAllocationBar({
// Build a map of apiKeyId → { consumed, fairShare } from the relevant dimension
const perKeyMap: Record<string, { consumed: number; fairShare: number }> = {};
if (usage) {
- const dim = usage.dimensions[dimensionIndex];
+ const dim = usage.dimensions?.[dimensionIndex];
if (dim) {
- for (const entry of dim.perKey) {
+ for (const entry of dim.perKey ?? []) {
perKeyMap[entry.apiKeyId] = { consumed: entry.consumed, fairShare: entry.fairShare };
}
}
diff --git a/src/app/(dashboard)/dashboard/costs/quota-share/hooks/usePoolUsage.ts b/src/app/(dashboard)/dashboard/costs/quota-share/hooks/usePoolUsage.ts
index 4a2af4181a..2cbfc02e33 100644
--- a/src/app/(dashboard)/dashboard/costs/quota-share/hooks/usePoolUsage.ts
+++ b/src/app/(dashboard)/dashboard/costs/quota-share/hooks/usePoolUsage.ts
@@ -20,9 +20,13 @@ export function usePoolUsage(poolId: string, pollIntervalMs = 15_000): UsePoolUs
try {
const res = await fetch(`/api/quota/pools/${poolId}/usage`);
if (!res.ok) throw new Error(`HTTP ${res.status}`);
- const data = (await res.json()) as PoolUsageSnapshot;
+ // The endpoint wraps the snapshot as `{ usage: snapshot }` — unwrap it.
+ // Storing the wrapper directly left `usage.dimensions` undefined, which
+ // crashed StackedAllocationBar (usage.dimensions[i]) for any pool that has
+ // allocations — taking down the whole quota-share page.
+ const data = (await res.json()) as { usage?: PoolUsageSnapshot | null };
if (!mountedRef.current) return;
- setUsage(data);
+ setUsage(data?.usage ?? null);
setError(null);
} catch (err) {
if (!mountedRef.current) return;
diff --git a/tests/unit/quota-pool-usage-shape.test.ts b/tests/unit/quota-pool-usage-shape.test.ts
new file mode 100644
index 0000000000..4823104964
--- /dev/null
+++ b/tests/unit/quota-pool-usage-shape.test.ts
@@ -0,0 +1,47 @@
+/**
+ * Regression: the pool-usage page crashed ("tela de erro") for any pool that has
+ * allocations. Root cause — the GET /api/quota/pools/[id]/usage endpoint wraps the
+ * snapshot as `{ usage: snapshot }`, but usePoolUsage stored the WHOLE response,
+ * leaving `usage.dimensions` undefined. StackedAllocationBar then dereferenced
+ * `usage.dimensions[dimensionIndex]` → `undefined[0]` → crash.
+ *
+ * These structural assertions pin the contract (endpoint wraps) + the fix
+ * (hook unwraps, bar guards) so the crash cannot silently return.
+ */
+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";
+
+const ROOT = join(fileURLToPath(import.meta.url), "..", "..", "..");
+const read = (rel: string) => readFileSync(join(ROOT, rel), "utf8");
+
+const HOOK = "src/app/(dashboard)/dashboard/costs/quota-share/hooks/usePoolUsage.ts";
+const BAR = "src/app/(dashboard)/dashboard/costs/quota-share/components/StackedAllocationBar.tsx";
+
+test("usage endpoint wraps the snapshot as { usage: snapshot }", () => {
+ const src = read("src/app/api/quota/pools/[id]/usage/route.ts");
+ assert.ok(
+ /NextResponse\.json\(\s*\{\s*usage:/.test(src),
+ "endpoint must return { usage: snapshot }"
+ );
+});
+
+test("usePoolUsage unwraps data.usage (not the whole response)", () => {
+ const src = read(HOOK);
+ assert.ok(src.includes("data?.usage"), "hook must unwrap data.usage");
+ assert.ok(
+ !/setUsage\(\s*data\s*\)/.test(src),
+ "hook must NOT store the raw wrapper as usage"
+ );
+});
+
+test("StackedAllocationBar guards usage.dimensions + dim.perKey", () => {
+ const src = read(BAR);
+ assert.ok(
+ src.includes("usage.dimensions?.[") || src.includes("usage.dimensions ?? []"),
+ "must optional-chain usage.dimensions access"
+ );
+ assert.ok(src.includes("dim.perKey ?? []"), "must guard dim.perKey iteration");
+});