diff --git a/pm-stack/aggregator/src/feed.ts b/pm-stack/aggregator/src/feed.ts index 7691c7edad..08447b30d5 100644 --- a/pm-stack/aggregator/src/feed.ts +++ b/pm-stack/aggregator/src/feed.ts @@ -49,6 +49,9 @@ export interface FleetFeed { * only when at least one row (or a non-array section) was dropped; * absent on raw parsed feeds and on clean loads. */ subscriptionsDropped?: number; + /** Rows loadFeed's charges sanitization dropped (#2445), set only + * when drops occurred; absent on raw parsed feeds and clean loads. */ + chargesDropped?: number; /** Sandbox-server + Jules sessions, pre-classification (render classifies). */ attention: SessionFeedEntry[]; /** Jules keeper totals (slice 2); absent when the keeper state is unreadable. */ @@ -347,6 +350,20 @@ export function isBurnSegment(v: unknown): v is BurnSegment { 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"; + +/** 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. */ +export function isChargeRow(v: unknown): v is ChargeRow { + if (!isRecord(v)) return false; + return ( + typeof v.at === "string" && + typeof v.label === "string" && + isFiniteNumber(v.amountUsd) && + typeof v.method === "string" && + typeof v.source === "string" + ); } /** The choices block renderQuota dereferences (hasHeadroom et al.). */ @@ -488,6 +505,21 @@ export async function loadFeed(path: string): Promise { // drop individually and well-formed siblings still surface. const charges = parsed.chargesAttention === undefined ? undefined : arrayOf(isUnmatchedCharge)(parsed.chargesAttention); const subs = sanitizeSubscriptions(parsed.subscriptions); + // Charges rows get the same drop-and-count treatment at this same + // shared gate (#2445): the section is optional, so absence stays + // absent; a non-array section drops whole (counted as one dropped + // section) instead of silently emptying the charges strip or + // crashing a consumer; malformed rows drop individually. The + // count rides out on the feed when drops occur, never swallowed. + let chargesPart: { charges?: ChargeRow[]; chargesDropped?: number } = {}; + if (parsed.charges !== undefined) { + const rows = arrayOf(isChargeRow)(parsed.charges); + if (rows === null) { + chargesPart = { charges: [], chargesDropped: 1 }; + } else { + chargesPart = { charges: rows.values, ...(rows.dropped > 0 ? { chargesDropped: rows.dropped } : {}) }; + } + } return { ...parsed, quota: san.view, @@ -496,6 +528,7 @@ export async function loadFeed(path: string): Promise { ...(charges && charges.dropped > 0 ? { chargesAttentionDropped: charges.dropped } : {}), subscriptions: subs.rows, ...(subs.dropped > 0 ? { subscriptionsDropped: subs.dropped } : {}), + ...chargesPart, }; } catch { return null; diff --git a/pm-stack/aggregator/src/sources/live.ts b/pm-stack/aggregator/src/sources/live.ts index a2a11f4fa8..a9c2811b43 100644 --- a/pm-stack/aggregator/src/sources/live.ts +++ b/pm-stack/aggregator/src/sources/live.ts @@ -890,6 +890,12 @@ export function liveSource(cfg: Config, opts?: { standingAuto?: () => Promise ({ kind: "unmatched-charge", label: `${u.merchant} — unrecognized SaaS/AI charge`, @@ -898,7 +904,7 @@ export function liveSource(cfg: Config, opts?: { standingAuto?: () => Promise Promise Promise { expect(clean?.subscriptionsDropped).toBeUndefined(); }); }); +describe("isChargeRow + loadFeed charges sanitization (#2445)", () => { + const CHOICES = { hasHeadroom: [], burnBeforeReset: [], doNotStart: [] }; + const ROW = { at: "2026-10-30", label: "Mystery AI Inc", amountUsd: 42.5, method: "Visa \u20227535", source: "plaid" }; + const feedWithCharges = (charges: unknown) => ({ + version: 1, + generatedAt: "2026-10-01T00:00:00Z", + quota: { pools: [], burn: [], choices: CHOICES }, + attention: [], + ...(charges === undefined ? {} : { charges }), + }); + const load = async (feed: unknown) => { + const dir = mkdtempSync(join(tmpdir(), "agg-feed-charges-")); + const file = join(dir, "feed.json"); + writeFileSync(file, JSON.stringify(feed)); + return loadFeed(file); + }; + + test("isChargeRow accepts a well-formed row and rejects malformed ones", () => { + expect(isChargeRow(ROW)).toBe(true); + expect(isChargeRow(null)).toBe(false); + expect(isChargeRow({ ...ROW, amountUsd: undefined })).toBe(false); + expect(isChargeRow({ ...ROW, amountUsd: "42.50" })).toBe(false); + expect(isChargeRow({ at: ROW.at, label: ROW.label, amountUsd: 1, source: "plaid" })).toBe(false); + }); + + test("charges [null] drops the row, keeps the feed, surfaces the count", async () => { + const loaded = await load(feedWithCharges([null])); + expect(loaded).not.toBeNull(); + expect(loaded?.charges).toEqual([]); + expect(loaded?.chargesDropped).toBe(1); + }); + + test("[valid, null] keeps the valid row", async () => { + const loaded = await load(feedWithCharges([ROW, null])); + expect(loaded?.charges).toEqual([ROW]); + expect(loaded?.chargesDropped).toBe(1); + }); + + test("entries missing amountUsd or with a string amountUsd drop individually", async () => { + const noAmount = { at: ROW.at, label: ROW.label, method: ROW.method, source: ROW.source }; + const loaded = await load(feedWithCharges([noAmount, { ...ROW, amountUsd: "42.50" }, ROW])); + expect(loaded?.charges).toEqual([ROW]); + expect(loaded?.chargesDropped).toBe(2); + }); + + test("a non-array charges section yields [] with the drop surfaced", async () => { + for (const bad of ["oops", { at: ROW.at }]) { + const loaded = await load(feedWithCharges(bad)); + expect(loaded).not.toBeNull(); + expect(loaded?.charges).toEqual([]); + expect(loaded?.chargesDropped).toBe(1); + } + }); + + test("an absent charges section stays absent", async () => { + const loaded = await load(feedWithCharges(undefined)); + expect(loaded).not.toBeNull(); + expect(loaded?.charges).toBeUndefined(); + expect(loaded?.chargesDropped).toBeUndefined(); + }); + + test("a well-formed feed is unchanged through the gate + loadFeed (collector self-acceptance)", async () => { + const feed = feedWithCharges([ROW]); + expect(isFleetFeed(feed)).toBe(true); + const loaded = await load(feed); + expect(loaded?.charges).toEqual([ROW]); + expect(loaded?.chargesDropped).toBeUndefined(); + }); +}); describe("feed chargesAttention gate + sanitization (#2439)", () => { const UNMATCHED = { merchant: "Mystery AI Inc", normalized: "mystery ai", amountUsd: 42.5, at: "2026-10-30", source: "plaid" };