diff --git a/pm-stack/aggregator/src/charges-match.ts b/pm-stack/aggregator/src/charges-match.ts index 6740a5944f..fa791b8f57 100644 --- a/pm-stack/aggregator/src/charges-match.ts +++ b/pm-stack/aggregator/src/charges-match.ts @@ -202,10 +202,15 @@ export function matchCharges(candidates: ChargeCandidate[], services: ChargeServ ); for (const candidate of ordered) { const normalized = normalizeMerchantName(candidate.merchant); + // Only pair observations from different sources. When normalization + // removes every token, also require the raw descriptor to agree so + // unrelated numeric merchant IDs cannot collapse into one charge. const existing = clusters.find( (cl) => cl.amountUsd === candidate.amountUsd && cl.normalized === normalized && + (candidate.source === "plaid" ? !cl.plaid : !cl.gmail) && + (normalized !== "" || cl.first.merchant.trim().toUpperCase() === candidate.merchant.trim().toUpperCase()) && daysBetween(cl.first.at, candidate.at) <= DEDUPE_WINDOW_DAYS, ); if (existing) { diff --git a/pm-stack/aggregator/test/charges-match.test.ts b/pm-stack/aggregator/test/charges-match.test.ts index 5590bbe641..d0136e3004 100644 --- a/pm-stack/aggregator/test/charges-match.test.ts +++ b/pm-stack/aggregator/test/charges-match.test.ts @@ -46,6 +46,30 @@ describe("matchCharges", () => { expect(matchCharges(candidates, SERVICES).rows).toHaveLength(2); }); + test("same-source Plaid charges are not deduped", () => { + const candidates: ChargeCandidate[] = [ + { source: "plaid", at: "2026-10-28", merchant: "OPENAI", amountUsd: 20 }, + { source: "plaid", at: "2026-10-29", merchant: "OPENAI", amountUsd: 20 }, + ]; + expect(matchCharges(candidates, SERVICES).rows).toHaveLength(2); + }); + + test("same-source Gmail charges are not deduped", () => { + const candidates: ChargeCandidate[] = [ + { source: "gmail", at: "2026-10-28", merchant: "OPENAI", amountUsd: 20 }, + { source: "gmail", at: "2026-10-29", merchant: "OPENAI", amountUsd: 20 }, + ]; + expect(matchCharges(candidates, SERVICES).rows).toHaveLength(2); + }); + + test("distinct empty-normalized merchants are not deduped", () => { + const candidates: ChargeCandidate[] = [ + { source: "plaid", at: "2026-10-28", merchant: "1234567", amountUsd: 20 }, + { source: "gmail", at: "2026-10-29", merchant: "9876543", amountUsd: 20 }, + ]; + expect(matchCharges(candidates, SERVICES).rows).toHaveLength(2); + }); + test("matched charges take the service name as their label", () => { const candidates: ChargeCandidate[] = [ { source: "plaid", at: "2026-10-11", merchant: "ANTHROPIC PBC", amountUsd: 100 },