diff --git a/pm-stack/aggregator/src/feed.ts b/pm-stack/aggregator/src/feed.ts index 6415674370..a4b1fee780 100644 --- a/pm-stack/aggregator/src/feed.ts +++ b/pm-stack/aggregator/src/feed.ts @@ -11,7 +11,15 @@ import type { UnmatchedCharge } from "./charges-match.ts"; import type { CheckoutDrift } from "./checkout-drift.ts"; import { isProjectPoolItem } from "./projects.ts"; import { isRepoCensus, type RepoCensus } from "./coverage.ts"; -import { arrayOf, isFiniteNumber, isIsoDate, isRecord, isStringArray, isTimestampNotFuture } from "./validate.ts"; +import { + arrayOf, + isFiniteNumber, + isIsoDate, + isNonNegative, + isRecord, + isStringArray, + isTimestampNotFuture, +} from "./validate.ts"; import type { AaFeedSection, BurnSegment, @@ -404,15 +412,20 @@ export function isQuotaPool(v: unknown): v is QuotaPool { return true; } -/** One historical burn segment as renderQuota maps it (#2429). */ +/** One historical burn segment as renderQuota maps it (#2429). + * Values, not just types (#3428): from/to must be real ISO calendar + * dates, tokens may not be negative, and the range may not be + * inverted — a segment failing any of these is not measured history + * and drops individually in sanitizeQuotaView (#2429 counting). */ export function isBurnSegment(v: unknown): v is BurnSegment { if (!isRecord(v)) return false; return ( typeof v.provider === "string" && typeof v.pool === "string" && - typeof v.from === "string" && - typeof v.to === "string" && - isFiniteNumber(v.tokens) + isIsoDate(v.from) && + isIsoDate(v.to) && + isNonNegative(v.tokens) && + Date.parse(v.from) <= Date.parse(v.to) ); } diff --git a/pm-stack/aggregator/test/feed.test.ts b/pm-stack/aggregator/test/feed.test.ts index baaa3dee58..abd714fe48 100644 --- a/pm-stack/aggregator/test/feed.test.ts +++ b/pm-stack/aggregator/test/feed.test.ts @@ -1,6 +1,7 @@ import { describe, expect, test } from "bun:test"; import { feedSectionStatus, + isBurnSegment, isChargeRow, isFleetFeed, isProjectPoolSection, @@ -224,6 +225,34 @@ describe("feed quota section gate + sanitization (#2429)", () => { expect(out.view.burn).toEqual([seg]); }); + // #3428: value-malformed segments (negative tokens, non-date or + // impossible from/to, inverted range) used to pass the type-only + // guard and render as measured history. They now drop individually + // and count, exactly like shape failures. + test("value-malformed burn segments drop individually", () => { + const seg = { + provider: "opencode", + pool: "go", + from: "2026-10-01T00:00:00Z", + to: "2026-10-01T01:00:00Z", + tokens: 42, + }; + const negative = { ...seg, tokens: -12345 }; + const garbageFrom = { ...seg, from: "banana" }; + const impossibleTo = { ...seg, to: "2026-02-30T01:00:00Z" }; + const inverted = { ...seg, from: "2026-10-02T00:00:00Z", to: "2026-10-01T00:00:00Z" }; + expect(isBurnSegment(seg)).toBe(true); + expect(isBurnSegment(negative)).toBe(false); + expect(isBurnSegment(garbageFrom)).toBe(false); + expect(isBurnSegment(impossibleTo)).toBe(false); + expect(isBurnSegment(inverted)).toBe(false); + const out = sanitizeQuotaView( + quotaWith([POOL], { burn: [seg, negative, garbageFrom, impossibleTo, inverted] }) as never, + ); + expect(out.droppedBurn).toBe(4); + expect(out.view.burn).toEqual([seg]); + }); + test("a fully well-formed quota view is unchanged", () => { const q = quotaWith([POOL], { burn: [] }) as unknown as QuotaView; const out = sanitizeQuotaView(q);