From b1e756a1194e5f0f7e43c8364063b0764f1a171c Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Sat, 31 Oct 2026 12:47:23 -0500 Subject: [PATCH 1/2] fix(aggregator): match charges to quota providers --- .../aggregator/scripts/collect-fleet-feed.ts | 3 ++- pm-stack/aggregator/src/charges-catalog.ts | 26 ++++++++++++++++++ .../aggregator/test/charges-catalog.test.ts | 27 +++++++++++++++++++ 3 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 pm-stack/aggregator/src/charges-catalog.ts create mode 100644 pm-stack/aggregator/test/charges-catalog.test.ts diff --git a/pm-stack/aggregator/scripts/collect-fleet-feed.ts b/pm-stack/aggregator/scripts/collect-fleet-feed.ts index f8fc7adf0..5df316147 100644 --- a/pm-stack/aggregator/scripts/collect-fleet-feed.ts +++ b/pm-stack/aggregator/scripts/collect-fleet-feed.ts @@ -84,6 +84,7 @@ import type { SessionRecord, } from "../src/model.ts"; import { matchCharges, type UnmatchedCharge } from "../src/charges-match.ts"; +import { chargeServiceCatalog } from "../src/charges-catalog.ts"; import { filterNewUnmatched, mergeChargeRows, parseChargeCandidates } from "../src/charges-collect.ts"; import { extractQuotaPools } from "../src/quota-extract.ts"; import { computeChoices } from "../src/quota-guidance.ts"; @@ -434,7 +435,7 @@ async function main(): Promise { if (candidates.length) { const auto = matchCharges( candidates, - (saas ?? []).map((s) => ({ name: s.name })), + chargeServiceCatalog(saas ?? [], pools), ); charges = mergeChargeRows(auto.rows, charges ?? []); let surfaced = new Set(); diff --git a/pm-stack/aggregator/src/charges-catalog.ts b/pm-stack/aggregator/src/charges-catalog.ts new file mode 100644 index 000000000..7f550bcaf --- /dev/null +++ b/pm-stack/aggregator/src/charges-catalog.ts @@ -0,0 +1,26 @@ +import type { ChargeServiceRef } from "./charges-match.ts"; +import type { QuotaPool, SaasService } from "./model.ts"; + +// Merchant names, not plan labels: generic plan words are unsafe aliases. +const PROVIDER_ALIASES: Record = { + Anthropic: ["Claude"], + OpenAI: ["ChatGPT", "Codex"], + "Meta Model API": ["Meta", "MetaPay"], + ClinePass: ["Cline Pass"], + "Open Design": ["OpenDesign"], + "Command Code": ["CommandCode"], +}; + +/** Build the match catalog from the same service and pool identities the hub exposes. */ +export function chargeServiceCatalog( + services: readonly Pick[], + pools: readonly Pick[], +): ChargeServiceRef[] { + const catalog = new Map(); + for (const { name } of services) catalog.set(name.toLowerCase(), { name }); + for (const { provider } of pools) { + const key = provider.toLowerCase(); + if (!catalog.has(key)) catalog.set(key, { name: provider, aliases: PROVIDER_ALIASES[provider] }); + } + return [...catalog.values()]; +} diff --git a/pm-stack/aggregator/test/charges-catalog.test.ts b/pm-stack/aggregator/test/charges-catalog.test.ts new file mode 100644 index 000000000..48272bf47 --- /dev/null +++ b/pm-stack/aggregator/test/charges-catalog.test.ts @@ -0,0 +1,27 @@ +import { describe, expect, test } from "bun:test"; +import { chargeServiceCatalog } from "../src/charges-catalog.ts"; +import { matchCharges } from "../src/charges-match.ts"; +import { extractQuotaPools } from "../src/quota-extract.ts"; +import { extractSaasServices } from "../src/saas-extract.ts"; + +describe("charge service catalog", () => { + test("matches quota providers and merchant aliases alongside SaaS services", () => { + const catalog = chargeServiceCatalog(extractSaasServices(""), extractQuotaPools("")); + const merchants = ["ANTHROPIC", "OPENAI", "CLAUDE", "CHATGPT", "VERCEL", "PADDLE MYSTERY"]; + const result = matchCharges(merchants.map((merchant, i) => ({ + source: "plaid" as const, at: "2026-10-01", merchant, amountUsd: i + 1, + })), catalog); + expect(result.rows.map((row) => row.label)).toEqual(expect.arrayContaining(["Anthropic", "OpenAI", "Vercel"])); + expect(result.rows.filter((row) => row.label === "Anthropic")).toHaveLength(2); + expect(result.rows.filter((row) => row.label === "OpenAI")).toHaveLength(2); + expect(result.unmatchedSaas.map((row) => row.merchant)).not.toContain("ANTHROPIC"); + expect(result.unmatchedSaas.map((row) => row.merchant)).not.toContain("OPENAI"); + expect(result.unmatchedSaas.map((row) => row.merchant)).toContain("PADDLE MYSTERY"); + }); + + test("deduplicates providers and respects filtered pools", () => { + const catalog = chargeServiceCatalog([], extractQuotaPools("", new Date(), new Set(["claude-code"]))); + expect(catalog.filter((entry) => entry.name === "OpenCode Go")).toHaveLength(1); + expect(catalog.some((entry) => entry.name === "Anthropic")).toBe(false); + }); +}); From a002a3e1ae5f4438d69eb871147b278f847ca421 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Sat, 31 Oct 2026 13:12:55 -0500 Subject: [PATCH 2/2] fix(aggregator): preserve charge dedupe across canonical provider labels (#3119) sameCharge now expands each label through its provider alias group (PROVIDER_ALIASES exported from charges-catalog), so a canonicalized auto row (Meta Model API) still dedupes the merchant-spelled hand-fed row (Meta PAYG spill, around the METAPAY alias). Codex review finding on PR #3168. --- pm-stack/aggregator/src/charges-catalog.ts | 2 +- pm-stack/aggregator/src/charges-collect.ts | 32 ++++++++++++++++--- .../aggregator/test/charges-collect.test.ts | 18 +++++++++++ 3 files changed, 47 insertions(+), 5 deletions(-) diff --git a/pm-stack/aggregator/src/charges-catalog.ts b/pm-stack/aggregator/src/charges-catalog.ts index 7f550bcaf..d31ab2170 100644 --- a/pm-stack/aggregator/src/charges-catalog.ts +++ b/pm-stack/aggregator/src/charges-catalog.ts @@ -2,7 +2,7 @@ import type { ChargeServiceRef } from "./charges-match.ts"; import type { QuotaPool, SaasService } from "./model.ts"; // Merchant names, not plan labels: generic plan words are unsafe aliases. -const PROVIDER_ALIASES: Record = { +export const PROVIDER_ALIASES: Record = { Anthropic: ["Claude"], OpenAI: ["ChatGPT", "Codex"], "Meta Model API": ["Meta", "MetaPay"], diff --git a/pm-stack/aggregator/src/charges-collect.ts b/pm-stack/aggregator/src/charges-collect.ts index 7a75fa7fc..c8d0dd35c 100644 --- a/pm-stack/aggregator/src/charges-collect.ts +++ b/pm-stack/aggregator/src/charges-collect.ts @@ -19,6 +19,7 @@ import { type UnmatchedCharge, } from "./charges-match.ts"; import type { ChargeRow } from "./model.ts"; +import { PROVIDER_ALIASES } from "./charges-catalog.ts"; /** * Structural validation of the producer's candidates file. Accepts @@ -71,13 +72,36 @@ function daysBetween(a: string, b: string): number { * METAPAY). Conservative: a hand-fed row is only dropped when an * auto row plausibly IS that charge. */ +/** + * Every squashed spelling a label answers to: its own, plus the + * canonical provider name and merchant aliases of its alias group + * when the label names one of them. Canonicalizing a matched label + * (the charge catalog's "Meta Model API" for a METAPAY charge) must + * not break deduplication against hand-fed rows written under the + * merchant spelling ("Meta PAYG spill" squashes around METAPAY, an + * alias of the canonical name) -- without the group expansion the + * same charge lands in the feed twice (Codex review, PR #3168). + */ +function labelSquashes(label: string): string[] { + const base = squash(normalizeMerchantName(label)); + if (!base) return []; + const out = new Set([base]); + for (const [canonical, aliases] of Object.entries(PROVIDER_ALIASES)) { + const group = [canonical, ...aliases].map((n) => squash(normalizeMerchantName(n))); + if (group.includes(base)) { + for (const g of group) out.add(g); + } + } + return [...out]; +} + function sameCharge(handFed: ChargeRow, auto: ChargeRow): boolean { if (handFed.amountUsd !== auto.amountUsd) return false; if (daysBetween(handFed.at, auto.at) > DEDUPE_WINDOW_DAYS) return false; - const a = squash(normalizeMerchantName(handFed.label)); - const b = squash(normalizeMerchantName(auto.label)); - if (!a || !b) return false; - return a.includes(b) || b.includes(a); + const aSet = labelSquashes(handFed.label); + const bSet = labelSquashes(auto.label); + if (!aSet.length || !bSet.length) return false; + return aSet.some((a) => bSet.some((b) => a.includes(b) || b.includes(a))); } /** diff --git a/pm-stack/aggregator/test/charges-collect.test.ts b/pm-stack/aggregator/test/charges-collect.test.ts index 0e42028af..277c80c05 100644 --- a/pm-stack/aggregator/test/charges-collect.test.ts +++ b/pm-stack/aggregator/test/charges-collect.test.ts @@ -42,6 +42,24 @@ describe("parseChargeCandidates", () => { }); }); +describe("mergeChargeRows canonical-label dedupe", () => { + // Codex finding on PR #3168: the charge catalog canonicalizes a + // METAPAY auto-match to "Meta Model API", which no longer + // containment-matches the hand-fed "Meta PAYG spill" row for the + // same charge, so the charge would appear twice in the feed. + test("a canonicalized auto row still dedupes the merchant-spelled hand-fed row", () => { + const auto = [{ at: "2026-10-01", label: "Meta Model API", amountUsd: 20 }]; + const handFed = [{ at: "2026-10-01", label: "Meta PAYG spill", amountUsd: 20 }]; + expect(mergeChargeRows(auto, handFed)).toEqual(auto); + }); + + test("a different amount under the same provider still keeps both rows", () => { + const auto = [{ at: "2026-10-01", label: "Meta Model API", amountUsd: 20 }]; + const handFed = [{ at: "2026-10-01", label: "Meta PAYG spill", amountUsd: 25 }]; + expect(mergeChargeRows(auto, handFed)).toHaveLength(2); + }); +}); + describe("mergeChargeRows", () => { const auto: ChargeRow[] = [ { at: "2026-10-28", label: "METAPAY*UC5DPA22Y4 US", amountUsd: 20, method: "Visa •7535", source: "Plaid" },