diff --git a/pm-stack/aggregator/src/feed.ts b/pm-stack/aggregator/src/feed.ts index e80c75e452..7691c7edad 100644 --- a/pm-stack/aggregator/src/feed.ts +++ b/pm-stack/aggregator/src/feed.ts @@ -41,6 +41,10 @@ export interface FleetFeed { /** Element drops loadFeed's quota sanitization made (#2429), set * only on feeds returned by loadFeed; absent on raw parsed feeds. */ quotaDropped?: { pools: number; windows: number; burn: number }; + /** Element drops loadFeed's chargesAttention sanitization made + * (#2439), set only when at least one entry dropped; absent on raw + * parsed feeds and on feeds whose entries all survived. */ + chargesAttentionDropped?: number; /** Subscription rows loadFeed's sanitization dropped (#2446), set * only when at least one row (or a non-array section) was dropped; * absent on raw parsed feeds and on clean loads. */ @@ -334,6 +338,17 @@ export function isBurnSegment(v: unknown): v is BurnSegment { ); } +/** One unmatched-charge entry as the live source dereferences it + * (#2439): merchant and at are interpolated, amountUsd is formatted + * 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. */ +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"; +} + /** The choices block renderQuota dereferences (hasHeadroom et al.). */ function isQuotaChoices(v: unknown): boolean { if (!isRecord(v)) return false; @@ -416,7 +431,13 @@ export function isFleetFeed(v: unknown): v is FleetFeed { // feeds, rather than crashing /quota. Array.isArray(q.burn) && isQuotaChoices(q.choices) && - Array.isArray(f.attention) + Array.isArray(f.attention) && + // chargesAttention is optional, but a present non-array value is + // a shape failure, not data (#2439): the live source maps it + // unguarded, so — like burn/choices above — the feed is rejected + // whole. Malformed ELEMENTS inside an array are loadFeed's + // drop-and-count concern, not the gate's. + (f.chargesAttention === undefined || Array.isArray(f.chargesAttention)) ); } @@ -461,11 +482,18 @@ export async function loadFeed(path: string): Promise { // sees only renderer-safe pools/windows (#2429). Drop counts are // attached to the feed so they are visible, not swallowed. const san = sanitizeQuotaView(parsed.quota); + // Same drop-and-count treatment for chargesAttention elements + // (#2439): the gate has already pinned the section to array-or- + // absent, so arrayOf cannot return null here; malformed entries + // drop individually and well-formed siblings still surface. + const charges = parsed.chargesAttention === undefined ? undefined : arrayOf(isUnmatchedCharge)(parsed.chargesAttention); const subs = sanitizeSubscriptions(parsed.subscriptions); return { ...parsed, quota: san.view, quotaDropped: { pools: san.droppedPools, windows: san.droppedWindows, burn: san.droppedBurn }, + ...(charges ? { chargesAttention: charges.values } : {}), + ...(charges && charges.dropped > 0 ? { chargesAttentionDropped: charges.dropped } : {}), subscriptions: subs.rows, ...(subs.dropped > 0 ? { subscriptionsDropped: subs.dropped } : {}), }; diff --git a/pm-stack/aggregator/test/charges-feed-gate.test.ts b/pm-stack/aggregator/test/charges-feed-gate.test.ts index 10412f8d8e..4d98af3fca 100644 --- a/pm-stack/aggregator/test/charges-feed-gate.test.ts +++ b/pm-stack/aggregator/test/charges-feed-gate.test.ts @@ -86,6 +86,17 @@ describe("live source usage(): feed charges outside the saas gate (#3124)", () = expect(flags[0]!.label).toContain("Mystery AI Inc"); }); + test("malformed chargesAttention entries drop at load; the well-formed flag still renders (#2439)", async () => { + const src = await sourceWith({ + feedOver: { chargesAttention: [null, { ...FEED_UNMATCHED, amountUsd: "42.50" }, FEED_UNMATCHED] }, + quotaView: VIEW, + }); + const usage = await src.usage(); + const flags = usage.attention.filter((a) => a.kind === "unmatched-charge"); + expect(flags).toHaveLength(1); + expect(flags[0]!.label).toContain("Mystery AI Inc"); + }); + test("fixtures fallback: feed charges are kept when the quota view is unreadable", async () => { const src = await sourceWith({ feedOver: { charges: [FEED_CHARGE], chargesAttention: [FEED_UNMATCHED] }, diff --git a/pm-stack/aggregator/test/feed.test.ts b/pm-stack/aggregator/test/feed.test.ts index 3ada49b4a3..e83ffb5cdc 100644 --- a/pm-stack/aggregator/test/feed.test.ts +++ b/pm-stack/aggregator/test/feed.test.ts @@ -1,5 +1,5 @@ import { describe, expect, test } from "bun:test"; -import { isFleetFeed, isProjectPoolSection, loadFeed, poolFromFeed, projectPoolSectionProblem, sanitizeQuotaView, sanitizeSubscriptions } from "../src/feed.ts"; +import { isFleetFeed, isProjectPoolSection, isUnmatchedCharge, loadFeed, poolFromFeed, projectPoolSectionProblem, sanitizeQuotaView, sanitizeSubscriptions } from "../src/feed.ts"; import { renderQuota } from "../src/render.ts"; import { mkdtempSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; @@ -307,3 +307,62 @@ describe("feed subscriptions sanitization (#2446)", () => { expect(clean?.subscriptionsDropped).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" }; + const feedWithCa = (chargesAttention: unknown) => ({ + version: 1, + generatedAt: "2026-10-01T00:00:00Z", + quota: { pools: [], burn: [], choices: { hasHeadroom: [], burnBeforeReset: [], doNotStart: [] } }, + attention: [], + chargesAttention, + }); + const loadFrom = async (feed: unknown) => { + const dir = mkdtempSync(join(tmpdir(), "agg-feed-ca-")); + const file = join(dir, "feed.json"); + writeFileSync(file, JSON.stringify(feed)); + return loadFeed(file); + }; + + test("the gate accepts absent and array chargesAttention, rejects a present non-array", () => { + const absent = feedWithCa(undefined) as Record; + delete absent.chargesAttention; + expect(isFleetFeed(absent)).toBe(true); + expect(isFleetFeed(feedWithCa([]))).toBe(true); + expect(isFleetFeed(feedWithCa([UNMATCHED]))).toBe(true); + expect(isFleetFeed(feedWithCa("nope"))).toBe(false); + expect(isFleetFeed(feedWithCa(42))).toBe(false); + expect(isFleetFeed(feedWithCa(null))).toBe(false); + }); + + test("isUnmatchedCharge pins exactly the fields the live source dereferences", () => { + expect(isUnmatchedCharge(UNMATCHED)).toBe(true); + expect(isUnmatchedCharge(null)).toBe(false); + expect(isUnmatchedCharge({ ...UNMATCHED, merchant: 42 })).toBe(false); + expect(isUnmatchedCharge({ ...UNMATCHED, amountUsd: "42.50" })).toBe(false); + expect(isUnmatchedCharge({ ...UNMATCHED, amountUsd: Number.NaN })).toBe(false); + const noAmount = { ...UNMATCHED } as Record; + delete noAmount.amountUsd; + expect(isUnmatchedCharge(noAmount)).toBe(false); + expect(isUnmatchedCharge({ ...UNMATCHED, at: 20261030 })).toBe(false); + // source/normalized are tolerated in any form (consumer never dereferences them unsafely) + expect(isUnmatchedCharge({ merchant: "X", amountUsd: 1, at: "2026-10-30" })).toBe(true); + }); + + test("loadFeed drops malformed entries individually and surfaces the count", async () => { + const loaded = await loadFrom(feedWithCa([null, UNMATCHED, { ...UNMATCHED, amountUsd: "42.50" }, { merchant: "No Amount Co", at: "2026-10-30" }])); + expect(loaded).not.toBeNull(); + expect(loaded?.chargesAttention).toEqual([UNMATCHED]); + expect(loaded?.chargesAttentionDropped).toBe(3); + }); + + test("loadFeed returns null for a non-array chargesAttention (fixture-fallback path)", async () => { + expect(await loadFrom(feedWithCa("nope"))).toBeNull(); + }); + + test("a well-formed chargesAttention passes loadFeed unchanged, with no dropped field", async () => { + const loaded = await loadFrom(feedWithCa([UNMATCHED])); + expect(loaded?.chargesAttention).toEqual([UNMATCHED]); + expect(loaded?.chargesAttentionDropped).toBeUndefined(); + }); +});