From 86fcd5fb79f4c3e8b8ed1cdaa36a5ffd192c85d6 Mon Sep 17 00:00:00 2001 From: "developing-today[bot]" <5454422+developing-today[bot]@users.noreply.github.com> Date: Tue, 3 Nov 2026 14:46:07 -0600 Subject: [PATCH 1/2] docs(aggregator): write the #3161 charges contract rules into the module headers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Phase 3 closing truth-up for the charges pipeline contract (design #3161): state the three rules where the pipeline states its philosophy — admission (payment evidence, amount paid) in the produce-charge-candidates header, identity and the token-boundary classification rule in the charges-match header, the shared identity rule on charges-collect sameCharge plus the delivery rule (surfaced means delivered) in the charges-collect header. Comment-only; no behavior change. Charges suites: 68 pass. --- .../scripts/produce-charge-candidates.ts | 8 +++++++ pm-stack/aggregator/src/charges-collect.ts | 18 +++++++++++++++ pm-stack/aggregator/src/charges-match.ts | 22 +++++++++++++++++++ 3 files changed, 48 insertions(+) diff --git a/pm-stack/aggregator/scripts/produce-charge-candidates.ts b/pm-stack/aggregator/scripts/produce-charge-candidates.ts index bea956dda7..f248ba4298 100644 --- a/pm-stack/aggregator/scripts/produce-charge-candidates.ts +++ b/pm-stack/aggregator/scripts/produce-charge-candidates.ts @@ -44,6 +44,14 @@ // label (card brand + mask). No balances, no debt, no account // numbers, no account-owner names ever enter the file. // +// Admission (#3161, the charges contract's first rule): a +// candidate exists only where money demonstrably moved. +// Declined/failed alerts and amounts merely due are excluded by +// isNotChargeSubject below, and a candidate's amount is the +// amount PAID: explicit paid/charged labels outrank a bare total, +// and a bare total counts only as a standalone label, never as a +// substring of "Subtotal". +// // Usage: bun run scripts/produce-charge-candidates.ts // [--start YYYY-MM-DD] [--end YYYY-MM-DD] [--out ] [--skip-gmail] // Defaults: start = first of the current month, end = today, diff --git a/pm-stack/aggregator/src/charges-collect.ts b/pm-stack/aggregator/src/charges-collect.ts index 47717e97ea..c376a65825 100644 --- a/pm-stack/aggregator/src/charges-collect.ts +++ b/pm-stack/aggregator/src/charges-collect.ts @@ -11,6 +11,13 @@ // Everything here is pure except the state the collector itself // passes in/out; file IO lives in collect-fleet-feed.ts, following // the same split as charges-match.ts (decisions here, state there). +// +// The charges contract (#3161) as it applies here: identity is the +// shared rule stated in charges-match.ts — sameCharge below is its +// hand-fed-vs-auto form — and delivery means surfaced keys are +// persisted only for charges in a feed that was successfully +// published, so an unmatched charge counts as shown only once it +// was actually delivered. import { DEDUPE_WINDOW_DAYS, @@ -119,6 +126,17 @@ function containsTokenSequence(tokens: string[], sequence: string[]): boolean { return false; } +/** + * The shared identity rule (#3161), hand-fed-vs-auto form: the two + * rows are the same charge only when amounts are equal, dates fall + * inside the dedupe window, and the merchant identity holds at + * token boundaries — token-sequence containment in either + * direction over the alias-expanded label token sequences (which + * preserves the documented METAPAY / "Meta PAYG spill" merge while + * excluding substring lookalikes). An empty normalized label never + * matches, and conflicting known card masks veto the merge; + * agreement or absence of a payment method does not block it. + */ 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; diff --git a/pm-stack/aggregator/src/charges-match.ts b/pm-stack/aggregator/src/charges-match.ts index 9eefa8bbb3..de1a302f7b 100644 --- a/pm-stack/aggregator/src/charges-match.ts +++ b/pm-stack/aggregator/src/charges-match.ts @@ -16,6 +16,28 @@ // // No balances or debt data pass through here: candidates are charge // evidence only (date, merchant, amount, payment-method label). +// +// The charges contract (#3161 — a charge is evidence), stated once: +// (1) Admission requires payment evidence: a candidate exists only +// where money demonstrably moved — declined/failed alerts and +// amounts merely due are not charges, and the candidate's amount is +// the amount paid (the producer's rule, produce-charge-candidates). +// (2) Identity is one rule everywhere: two observations are the +// same charge only when their provenance differs (cross-source +// here; hand-fed vs auto in charges-collect's sameCharge), amounts +// are equal, dates fall inside the 3-day window +// (DEDUPE_WINDOW_DAYS), and the merchant identity holds at token +// boundaries; an empty normalized merchant never matches anything, +// and conflicting known payment methods veto the merge (agreement +// or absence does not block it). Classification follows the same +// boundary rule: service names, aliases and SaaS markers match at +// token boundaries only, against the hub's whole known catalog — +// SaaS services plus the quota pools and their aliases. +// (3) Delivery: surfaced means delivered — surfaced keys are +// persisted only for charges in a feed that was successfully +// published, and consumers read the charges sections on their own +// validity, never gated on an unrelated section (the collector's +// and the live source's side of the contract). import type { ChargeRow } from "./model.ts"; From 2b82ab4e4b38d0b24c2580e71f0bc39dece563c2 Mon Sep 17 00:00:00 2001 From: "developing-today[bot]" <5454422+developing-today[bot]@users.noreply.github.com> Date: Tue, 3 Nov 2026 14:51:06 -0600 Subject: [PATCH 2/2] docs(aggregator): describe the two identity forms as implemented (#3161) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Codex review on PR #5131: the charges-match header overstated the cross-source dedupe — matchCharges clustering does not compare payment methods (the card-mask veto is sameCharge-only) and empty-normalized merchants merge when their raw descriptors agree exactly. Distinguish the two forms of the identity rule. --- pm-stack/aggregator/src/charges-match.ts | 25 +++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/pm-stack/aggregator/src/charges-match.ts b/pm-stack/aggregator/src/charges-match.ts index de1a302f7b..c7f48841bd 100644 --- a/pm-stack/aggregator/src/charges-match.ts +++ b/pm-stack/aggregator/src/charges-match.ts @@ -22,17 +22,20 @@ // where money demonstrably moved — declined/failed alerts and // amounts merely due are not charges, and the candidate's amount is // the amount paid (the producer's rule, produce-charge-candidates). -// (2) Identity is one rule everywhere: two observations are the -// same charge only when their provenance differs (cross-source -// here; hand-fed vs auto in charges-collect's sameCharge), amounts -// are equal, dates fall inside the 3-day window -// (DEDUPE_WINDOW_DAYS), and the merchant identity holds at token -// boundaries; an empty normalized merchant never matches anything, -// and conflicting known payment methods veto the merge (agreement -// or absence does not block it). Classification follows the same -// boundary rule: service names, aliases and SaaS markers match at -// token boundaries only, against the hub's whole known catalog — -// SaaS services plus the quota pools and their aliases. +// (2) Identity is one rule, applied in two forms. Cross-source +// dedupe (matchCharges below) pairs observations from different +// sources only, with equal amounts, dates inside the 3-day window +// (DEDUPE_WINDOW_DAYS), and equal normalized merchants; when +// normalization removes every token, the raw descriptors must +// also agree exactly, so unrelated numeric merchant IDs cannot +// collapse into one charge. The hand-fed merge (charges-collect's +// sameCharge) compares merchant identity at token boundaries +// instead, and adds the payment-method veto: conflicting known +// card masks never merge there (agreement or absence does not +// block it). Classification follows the boundary rule: service +// names, aliases and SaaS markers match at token boundaries +// only, against the hub's whole known catalog — SaaS services +// plus the quota pools and their aliases. // (3) Delivery: surfaced means delivered — surfaced keys are // persisted only for charges in a feed that was successfully // published, and consumers read the charges sections on their own