diff --git a/pm-stack/aggregator/src/feed.ts b/pm-stack/aggregator/src/feed.ts index d437008dd8..d97d18d19b 100644 --- a/pm-stack/aggregator/src/feed.ts +++ b/pm-stack/aggregator/src/feed.ts @@ -17,6 +17,7 @@ import { isFiniteNumber, isIsoDate, isNonNegative, + isPercentOf, isRecord, isStringArray, isTimestampNotFuture, @@ -438,18 +439,24 @@ export function coverageFromFeed(feed: Partial | null | undefined): R return rows.length ? rows : null; } +const isQuotaPercent = isPercentOf(100); + /** One quota window as the renderers dereference it (#2429): `limit` * is read arithmetically by windowPercent, `used` is rendered, and a - * non-object element crashes both renderQuota and buildAttentionFlags. */ + * non-object element crashes both renderQuota and buildAttentionFlags. + * Value rules (#3427): a non-null resetsAt must be a real ISO calendar + * date (impossible dates roll forward in guidance), non-null limit and + * used must be non-negative finite, and percent when present must be + * within 0..100 (negative or >100 percents misread as healthy/walled). */ export function isQuotaWindow(v: unknown): v is QuotaWindow { if (!isRecord(v)) return false; return ( typeof v.name === "string" && (v.unit === "requests" || v.unit === "tokens" || v.unit === "usd" || v.unit === "messages" || v.unit === "other") && - (v.limit === null || isFiniteNumber(v.limit)) && - isFiniteNumber(v.used) && - (v.resetsAt === null || typeof v.resetsAt === "string") && - (v.percent === undefined || isFiniteNumber(v.percent)) + (v.limit === null || isNonNegative(v.limit)) && + isNonNegative(v.used) && + (v.resetsAt === null || isIsoDate(v.resetsAt)) && + (v.percent === undefined || isQuotaPercent(v.percent)) ); } diff --git a/pm-stack/aggregator/test/feed.test.ts b/pm-stack/aggregator/test/feed.test.ts index ac629a8d9d..e0fecc7350 100644 --- a/pm-stack/aggregator/test/feed.test.ts +++ b/pm-stack/aggregator/test/feed.test.ts @@ -252,6 +252,63 @@ describe("feed quota section gate + sanitization (#2429)", () => { expect(out.view.pools[0]?.windows).toEqual([WINDOW]); }); + // #3427: value-malformed windows (impossible resetsAt, negative + // limit/used, out-of-range percent) used to pass the type-only guard + // and mislead guidance/health. They now drop individually and count, + // exactly like shape failures; valid windows are untouched. + test("Feb-30 resetsAt window drops individually", () => { + const bad = { ...WINDOW, resetsAt: "2026-02-30T00:00:00Z" }; + const pool = { ...POOL, windows: [WINDOW, bad] }; + const out = sanitizeQuotaView(quotaWith([pool]) as never); + expect(out.droppedWindows).toBe(1); + expect(out.view.pools[0]?.windows).toEqual([WINDOW]); + }); + + test("negative used window drops individually", () => { + const bad = { ...WINDOW, used: -5 }; + const pool = { ...POOL, windows: [WINDOW, bad] }; + const out = sanitizeQuotaView(quotaWith([pool]) as never); + expect(out.droppedWindows).toBe(1); + expect(out.view.pools[0]?.windows).toEqual([WINDOW]); + }); + + test("negative limit window drops individually", () => { + const bad = { ...WINDOW, limit: -100 }; + const pool = { ...POOL, windows: [WINDOW, bad] }; + const out = sanitizeQuotaView(quotaWith([pool]) as never); + expect(out.droppedWindows).toBe(1); + expect(out.view.pools[0]?.windows).toEqual([WINDOW]); + }); + + test("percent -50 window drops individually", () => { + const bad = { ...WINDOW, percent: -50 }; + const pool = { ...POOL, windows: [WINDOW, bad] }; + const out = sanitizeQuotaView(quotaWith([pool]) as never); + expect(out.droppedWindows).toBe(1); + expect(out.view.pools[0]?.windows).toEqual([WINDOW]); + }); + + test("percent 15000 window drops individually", () => { + const bad = { ...WINDOW, percent: 15000 }; + const pool = { ...POOL, windows: [WINDOW, bad] }; + const out = sanitizeQuotaView(quotaWith([pool]) as never); + expect(out.droppedWindows).toBe(1); + expect(out.view.pools[0]?.windows).toEqual([WINDOW]); + }); + + test("valid windows are untouched and droppedWindows counts every drop", () => { + const feb30 = { ...WINDOW, name: "feb30", resetsAt: "2026-02-30T00:00:00Z" }; + const negUsed = { ...WINDOW, name: "neg-used", used: -1 }; + const negLimit = { ...WINDOW, name: "neg-limit", limit: -1 }; + const negPct = { ...WINDOW, name: "neg-pct", percent: -50 }; + const hugePct = { ...WINDOW, name: "huge-pct", percent: 15000 }; + const pool = { ...POOL, windows: [WINDOW, feb30, negUsed, negLimit, negPct, hugePct] }; + const out = sanitizeQuotaView(quotaWith([pool]) as never); + expect(out.droppedWindows).toBe(5); + expect(out.view.pools[0]?.windows).toEqual([WINDOW]); + expect(out.view.dropped).toEqual({ pools: 0, windows: 5, burn: 0 }); + }); + test("malformed burn segments drop individually", () => { const seg = { provider: "opencode",