From e10b3e1a4d62940dcd325af7dd24fe620a727a6c Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Sun, 1 Nov 2026 23:57:39 -0600 Subject: [PATCH] fix(aggregator): drop out-of-range pctOfLimit at both quota-current readers (#2582) --- pm-stack/aggregator/src/quota-current.ts | 15 ++- .../aggregator/test/quota-current.test.ts | 118 ++++++++++++++++++ 2 files changed, 131 insertions(+), 2 deletions(-) diff --git a/pm-stack/aggregator/src/quota-current.ts b/pm-stack/aggregator/src/quota-current.ts index 3e930bf1dc..faecd9890f 100644 --- a/pm-stack/aggregator/src/quota-current.ts +++ b/pm-stack/aggregator/src/quota-current.ts @@ -116,9 +116,20 @@ function windowName(name: string): string { return name === "5h" ? "5-hour" : name; } +/** + * A pctOfLimit is usable only as a finite number inside [0, 100] (#2582, + * gate pinned by triage task 3837). Out-of-range reads DROP, never clamp: + * clamping 150 -> 100 would still wall a healthy pool, and publishing -20 + * would report negative usage. Shared by both reader sites below so the + * two can never drift apart. + */ +function usablePct(v: unknown): v is number { + return typeof v === "number" && Number.isFinite(v) && v >= 0 && v <= 100; +} + function pctWindows(entry: QuotaCurrentEntry): QuotaWindow[] { return entry.windows - .filter((w) => typeof w.pctOfLimit === "number" && Number.isFinite(w.pctOfLimit)) + .filter((w) => usablePct(w.pctOfLimit)) .map((w) => ({ name: windowName(w.name), unit: "other" as const, @@ -317,7 +328,7 @@ export function overlayTursoUsage(services: SaasService[], file: QuotaCurrentFil used: w.value, limit: meta.limit, unit: meta.unit, - ...(w.pctOfLimit != null ? { percent: w.pctOfLimit } : {}), + ...(usablePct(w.pctOfLimit) ? { percent: w.pctOfLimit } : {}), highlight: prev.get(w.name)?.highlight ?? true, }); } diff --git a/pm-stack/aggregator/test/quota-current.test.ts b/pm-stack/aggregator/test/quota-current.test.ts index 4477040e9c..d5050195ad 100644 --- a/pm-stack/aggregator/test/quota-current.test.ts +++ b/pm-stack/aggregator/test/quota-current.test.ts @@ -647,3 +647,121 @@ describe("overlay: alternate ChatGPT pool (openai.alternate)", () => { expect(main.windows[0].percent).toBe(11); }); }); + +describe("out-of-range pctOfLimit drops, never clamps (#2582)", () => { + test("a fresh ok read at 150% does not wall the pool: the window drops and prior windows stand", () => { + const out = overlayQuotaCurrent( + baseView(), + file([entry({ serviceId: "anthropic:claude-pro", windows: [win("weekly", 150, iso(5 * 60_000))] })]), + NOW, + ); + const poolOut = out.pools.find((p) => p.id === "anthropic:claude-pro")!; + expect(poolOut.windows.map((w) => [w.name, w.percent])).toEqual([["weekly", 68]]); + expect(poolOut.state).toBe("healthy"); + expect(poolOut.freshness?.state).toBe("ok"); + expect(out.choices.doNotStart).not.toContain("anthropic:claude-pro"); + }); + + test("a fresh ok read at -20% drops the same way instead of publishing negative usage", () => { + const out = overlayQuotaCurrent( + baseView(), + file([entry({ serviceId: "anthropic:claude-pro", windows: [win("weekly", -20, iso(5 * 60_000))] })]), + NOW, + ); + const poolOut = out.pools.find((p) => p.id === "anthropic:claude-pro")!; + expect(poolOut.windows.map((w) => [w.name, w.percent])).toEqual([["weekly", 68]]); + expect(poolOut.state).toBe("healthy"); + }); + + test("a mixed entry applies only its in-range windows", () => { + const out = overlayQuotaCurrent( + baseView(), + file([ + entry({ + serviceId: "anthropic:claude-pro", + windows: [win("5h", 42, iso(5 * 60_000)), win("weekly", 150, iso(5 * 60_000))], + }), + ]), + NOW, + ); + const poolOut = out.pools.find((p) => p.id === "anthropic:claude-pro")!; + expect(poolOut.windows.map((w) => [w.name, w.percent])).toEqual([["5-hour", 42]]); + }); + + test("the boundary values 0 and 100 stay usable", () => { + const out = overlayQuotaCurrent( + baseView(), + file([ + entry({ + serviceId: "anthropic:claude-pro", + windows: [win("5h", 0, iso(5 * 60_000)), win("weekly", 100, iso(5 * 60_000))], + }), + ]), + NOW, + ); + const poolOut = out.pools.find((p) => p.id === "anthropic:claude-pro")!; + expect(poolOut.windows.map((w) => [w.name, w.percent])).toEqual([ + ["5-hour", 0], + ["weekly", 100], + ]); + }); + + describe("overlayTursoUsage percent gate", () => { + const tursoSvc = (): SaasService => ({ + id: "turso", + name: "Turso", + group: "database", + plan: "Starter Free $0", + status: "active", + metrics: [{ id: "databases", label: "Databases", used: 1, limit: 100, unit: "count", percent: 1, highlight: true }], + paygEstimate: null, + measuredAt: "2026-10-20T00:00:00.000Z", + sourceClass: "manual", + sourceNote: "quota-view.md Part (b)", + nextDate: "2026-10-31 cycle end", + cadenceDays: 7, + conflicts: [], + reads: [], + }); + const tursoPct = (pct: unknown) => { + const at = iso(5 * 60_000); + const out = overlayTursoUsage( + [tursoSvc()], + file([ + entry({ + serviceId: "turso", + providerId: "turso", + source: "turso-api", + measuredAt: at, + windows: [ + { + name: "databases", + value: 3, + pctOfLimit: pct as number, + valueLabel: null, + windowSeconds: 2_678_400, + resetsAt: "2026-11-01T04:00:00.000Z", + measuredAt: at, + source: "turso-api", + state: "ok" as const, + }, + ], + }), + ]), + NOW, + ); + return out[0].metrics.find((m) => m.id === "databases")!; + }; + + test("percent is omitted for out-of-range or non-numeric pctOfLimit, present in range", () => { + for (const bad of [150, -20, "42", Number.NaN]) { + const metric = tursoPct(bad); + expect(metric.used).toBe(3); + expect(metric).not.toHaveProperty("percent"); + } + expect(tursoPct(42).percent).toBe(42); + expect(tursoPct(0).percent).toBe(0); + expect(tursoPct(100).percent).toBe(100); + }); + }); +});