From 7805af72f2e460a4161ec17834e80026c6d8d76d Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Mon, 2 Nov 2026 09:32:54 -0600 Subject: [PATCH] aggregator(feed): reject impossible charge rows (#3458) isChargeRow and isUnmatchedCharge now require amountUsd > 0 and isIsoDate(at), preserving existing shape checks. Invalid rows drop via the existing loadFeed sanitization path with chargesDropped / chargesAttentionDropped. Adds #3458 gate regressions. --- pm-stack/aggregator/src/feed.ts | 19 +++++-- pm-stack/aggregator/test/feed.test.ts | 81 +++++++++++++++++++++++++++ 2 files changed, 96 insertions(+), 4 deletions(-) diff --git a/pm-stack/aggregator/src/feed.ts b/pm-stack/aggregator/src/feed.ts index d437008dd8..2df82ae568 100644 --- a/pm-stack/aggregator/src/feed.ts +++ b/pm-stack/aggregator/src/feed.ts @@ -498,22 +498,33 @@ export function isBurnSegment(v: unknown): v is BurnSegment { * with toFixed and used as a severity, so those three must be a * string, a finite number, and a string. normalized/source are not * gate-required: the consumer tolerates any source value and never - * dereferences normalized. */ + * dereferences normalized. (#3458): amountUsd must be positive and at + * must be a real ISO calendar date — impossible rows alter charge + * totals and attention severity. */ export function isUnmatchedCharge(v: unknown): v is UnmatchedCharge { if (!isRecord(v)) return false; - return typeof v.merchant === "string" && isFiniteNumber(v.amountUsd) && typeof v.at === "string"; + return ( + typeof v.merchant === "string" && + isFiniteNumber(v.amountUsd) && + v.amountUsd > 0 && + isIsoDate(v.at) + ); } /** One charges-evidence row as the /quota charges strip renders it * (#2445): a record with string at/label/method/source and a * finite-number amountUsd (ChargeRow, model.ts). Built on the shared - * validate.ts primitives, like the quota element validators above. */ + * validate.ts primitives, like the quota element validators above. + * (#3458): amountUsd must be positive and at must be a real ISO + * calendar date — impossible rows alter charge totals and attention + * severity. */ export function isChargeRow(v: unknown): v is ChargeRow { if (!isRecord(v)) return false; return ( - typeof v.at === "string" && + isIsoDate(v.at) && typeof v.label === "string" && isFiniteNumber(v.amountUsd) && + v.amountUsd > 0 && typeof v.method === "string" && typeof v.source === "string" ); diff --git a/pm-stack/aggregator/test/feed.test.ts b/pm-stack/aggregator/test/feed.test.ts index ac629a8d9d..3fcb13fc1e 100644 --- a/pm-stack/aggregator/test/feed.test.ts +++ b/pm-stack/aggregator/test/feed.test.ts @@ -729,6 +729,87 @@ describe("feed chargesAttention gate + sanitization (#2439)", () => { }); }); +describe("charge gates reject impossible rows (#3458)", () => { + const CHOICES = { hasHeadroom: [], burnBeforeReset: [], doNotStart: [] }; + const ROW = { + at: "2026-10-30", + label: "Mystery AI Inc", + amountUsd: 42.5, + method: "Visa …7535", + source: "plaid", + }; + const UNMATCHED = { + merchant: "Mystery AI Inc", + normalized: "mystery ai", + amountUsd: 42.5, + at: "2026-10-30", + source: "plaid", + }; + const baseFeed = (extra: unknown) => ({ + version: 1, + generatedAt: "2026-10-01T00:00:00Z", + quota: { pools: [], burn: [], choices: CHOICES }, + attention: [], + ...(extra as Record), + }); + const load = async (feed: unknown) => { + const dir = mkdtempSync(join(tmpdir(), "agg-feed-3458-")); + const file = join(dir, "feed.json"); + writeFileSync(file, JSON.stringify(feed)); + return loadFeed(file); + }; + + test("isChargeRow rejects non-positive amounts and non-calendar dates, accepts a valid row", () => { + expect(isChargeRow(ROW)).toBe(true); + expect(isChargeRow({ ...ROW, amountUsd: -1 })).toBe(false); + expect(isChargeRow({ ...ROW, amountUsd: 0 })).toBe(false); + expect(isChargeRow({ ...ROW, at: "banana" })).toBe(false); + expect(isChargeRow({ ...ROW, at: "2026-02-30" })).toBe(false); + }); + + test("isUnmatchedCharge rejects non-positive amounts and non-calendar dates, accepts a valid row", () => { + expect(isUnmatchedCharge(UNMATCHED)).toBe(true); + expect(isUnmatchedCharge({ ...UNMATCHED, amountUsd: -5 })).toBe(false); + expect(isUnmatchedCharge({ ...UNMATCHED, amountUsd: 0 })).toBe(false); + expect(isUnmatchedCharge({ ...UNMATCHED, at: "banana" })).toBe(false); + expect(isUnmatchedCharge({ ...UNMATCHED, at: "2026-02-30" })).toBe(false); + }); + + test("loadFeed drops impossible charges individually and surfaces chargesDropped", async () => { + const loaded = await load( + baseFeed({ + charges: [ + ROW, + { ...ROW, amountUsd: -1 }, + { ...ROW, amountUsd: 0 }, + { ...ROW, at: "banana" }, + { ...ROW, at: "2026-02-30" }, + ], + }), + ); + expect(loaded).not.toBeNull(); + expect(loaded?.charges).toEqual([ROW]); + expect(loaded?.chargesDropped).toBe(4); + }); + + test("loadFeed drops impossible chargesAttention individually and surfaces chargesAttentionDropped", async () => { + const loaded = await load( + baseFeed({ + chargesAttention: [ + UNMATCHED, + { ...UNMATCHED, amountUsd: -1 }, + { ...UNMATCHED, amountUsd: 0 }, + { ...UNMATCHED, at: "banana" }, + { ...UNMATCHED, at: "2026-02-30" }, + ], + }), + ); + expect(loaded).not.toBeNull(); + expect(loaded?.chargesAttention).toEqual([UNMATCHED]); + expect(loaded?.chargesAttentionDropped).toBe(4); + }); +}); + describe("collector section metadata (#1178)", () => { const baseFeed = () => ({ version: 1,