From fc84861fdab92d54dfd503a184de53163a14b58d Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Mon, 2 Nov 2026 03:49:46 -0600 Subject: [PATCH 1/2] fix(aggregator): require token boundaries in charge matching --- pm-stack/aggregator/src/charges-match.ts | 11 ++----- .../aggregator/test/charges-match.test.ts | 33 +++++++++++++++++++ 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/pm-stack/aggregator/src/charges-match.ts b/pm-stack/aggregator/src/charges-match.ts index fa791b8f5..f302f36ae 100644 --- a/pm-stack/aggregator/src/charges-match.ts +++ b/pm-stack/aggregator/src/charges-match.ts @@ -99,10 +99,6 @@ export function normalizeMerchantName(raw: string): string { return tokens.join(" "); } -function squash(normalized: string): string { - return normalized.replace(/\s+/g, ""); -} - function daysBetween(a: string, b: string): number { const ms = Math.abs(Date.parse(a) - Date.parse(b)); return Math.floor(ms / 86_400_000); @@ -156,20 +152,17 @@ const SAAS_MARKERS = [ /** Heuristic: does this normalized merchant look like a SaaS/AI charge? */ export function isSaasLooking(normalized: string): boolean { - const flat = squash(normalized); - return SAAS_MARKERS.some((marker) => flat.includes(marker)); + const tokens = new Set(normalized.split(" ").filter(Boolean)); + return SAAS_MARKERS.some((marker) => tokens.has(marker)); } /** The hub service a normalized merchant belongs to, if any. */ export function matchService(normalized: string, services: ChargeServiceRef[]): ChargeServiceRef | undefined { - const flat = squash(normalized); const tokenSet = new Set(normalized.split(" ").filter(Boolean)); for (const service of services) { for (const candidate of [service.name, ...(service.aliases ?? [])]) { const norm = normalizeMerchantName(candidate); if (!norm) continue; - const flatNorm = squash(norm); - if (flatNorm.length >= 4 && flat.includes(flatNorm)) return service; const tokens = norm.split(" ").filter(Boolean); if (tokens.length > 0 && tokens.every((t) => tokenSet.has(t))) return service; } diff --git a/pm-stack/aggregator/test/charges-match.test.ts b/pm-stack/aggregator/test/charges-match.test.ts index d0136e300..c67bef322 100644 --- a/pm-stack/aggregator/test/charges-match.test.ts +++ b/pm-stack/aggregator/test/charges-match.test.ts @@ -88,6 +88,39 @@ describe("matchCharges", () => { expect(rows[0]?.source).toBe("Gmail alert"); }); + test("service and SaaS-marker matches require token boundaries", () => { + const candidates: ChargeCandidate[] = [ + { source: "plaid", at: "2026-10-15", merchant: "METAMUCIL", amountUsd: 12 }, + { source: "plaid", at: "2026-10-16", merchant: "METAL ROOFING LLC", amountUsd: 34 }, + { source: "plaid", at: "2026-10-17", merchant: "META PAY", amountUsd: 8 }, + ]; + const { rows, unmatchedSaas } = matchCharges(candidates, [{ name: "Meta" }]); + + expect(rows.map((row) => row.label)).toEqual(["METAMUCIL", "METAL ROOFING LLC", "Meta"]); + expect(unmatchedSaas.map((charge) => charge.merchant)).toEqual([]); + expect(isSaasLooking(normalizeMerchantName("METAMUCIL"))).toBe(false); + expect(isSaasLooking(normalizeMerchantName("METAL ROOFING LLC"))).toBe(false); + expect(isSaasLooking(normalizeMerchantName("META PAY"))).toBe(true); + }); + + test("Cursor.com still matches the Cursor service", () => { + const candidates: ChargeCandidate[] = [ + { source: "plaid", at: "2026-10-18", merchant: "CURSOR.COM", amountUsd: 20 }, + ]; + expect(matchCharges(candidates, [{ name: "Cursor" }]).rows[0]?.label).toBe("Cursor"); + }); + + test("multi-word service names retain token-set matching", () => { + const candidates: ChargeCandidate[] = [ + { source: "plaid", at: "2026-10-19", merchant: "NORTH STAR PLAN", amountUsd: 9 }, + { source: "plaid", at: "2026-10-20", merchant: "PLAN NORTH MONTHLY STAR", amountUsd: 10 }, + ]; + expect(matchCharges(candidates, [{ name: "North Star" }]).rows.map((row) => row.label)).toEqual([ + "North Star", + "North Star", + ]); + }); + test("unmatched SaaS-looking charges surface once, deduped", () => { const candidates: ChargeCandidate[] = [ { source: "plaid", at: "2026-10-12", merchant: "CURSOR.COM", amountUsd: 20 }, From 4651cbda34515ee5c4f03028f0a47a900877ac04 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Mon, 2 Nov 2026 05:08:16 -0600 Subject: [PATCH 2/2] fix(aggregator): keep METAPAY descriptor SaaS-visible under token matching Codex P2 on PR #4306: token-exact matching dropped the known METAPAY* descriptor, breaking charges-collect period-replay tests that expect METAPAY in unmatchedSaas. Add METAPAY as an explicit SAAS_MARKERS entry and a regression expectation. bun test charges-match + charges-collect: 27 pass, 0 fail. --- pm-stack/aggregator/src/charges-match.ts | 4 ++++ pm-stack/aggregator/test/charges-match.test.ts | 1 + 2 files changed, 5 insertions(+) diff --git a/pm-stack/aggregator/src/charges-match.ts b/pm-stack/aggregator/src/charges-match.ts index f302f36ae..dc4f678bc 100644 --- a/pm-stack/aggregator/src/charges-match.ts +++ b/pm-stack/aggregator/src/charges-match.ts @@ -142,6 +142,10 @@ const SAAS_MARKERS = [ // charges to these are exactly the unmatched SaaS/AI charges the // hub exists to surface. "META", + // Known Meta billing descriptor (METAPAY*...): previously matched via + // the META substring; listed explicitly so token-boundary matching + // keeps surfacing it as unmatched SaaS when Meta is not in the catalog. + "METAPAY", "CLINE", "OPENCODE", "OPENDESIGN", diff --git a/pm-stack/aggregator/test/charges-match.test.ts b/pm-stack/aggregator/test/charges-match.test.ts index c67bef322..1cfa8db61 100644 --- a/pm-stack/aggregator/test/charges-match.test.ts +++ b/pm-stack/aggregator/test/charges-match.test.ts @@ -101,6 +101,7 @@ describe("matchCharges", () => { expect(isSaasLooking(normalizeMerchantName("METAMUCIL"))).toBe(false); expect(isSaasLooking(normalizeMerchantName("METAL ROOFING LLC"))).toBe(false); expect(isSaasLooking(normalizeMerchantName("META PAY"))).toBe(true); + expect(isSaasLooking(normalizeMerchantName("METAPAY*UC5DPA22Y4 US"))).toBe(true); }); test("Cursor.com still matches the Cursor service", () => {