From d4e7a33373b65040163fe1d2dda9063d065d6363 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Sun, 1 Nov 2026 20:50:26 -0600 Subject: [PATCH] fix(aggregator): reject impossible charge dates --- .../scripts/produce-charge-candidates.ts | 6 +++--- pm-stack/aggregator/src/charges-collect.ts | 7 +++++-- pm-stack/aggregator/src/charges-match.ts | 15 +++++++++++++++ .../aggregator/test/charges-collect.test.ts | 19 +++++++++++++++++++ 4 files changed, 42 insertions(+), 5 deletions(-) diff --git a/pm-stack/aggregator/scripts/produce-charge-candidates.ts b/pm-stack/aggregator/scripts/produce-charge-candidates.ts index 58704085db..c73e743ae8 100644 --- a/pm-stack/aggregator/scripts/produce-charge-candidates.ts +++ b/pm-stack/aggregator/scripts/produce-charge-candidates.ts @@ -39,7 +39,7 @@ import { writeFile } from "node:fs/promises"; import { homedir } from "node:os"; import { join } from "node:path"; import { promisify } from "node:util"; -import type { ChargeCandidate } from "../src/charges-match.ts"; +import { isRealCalendarDate, type ChargeCandidate } from "../src/charges-match.ts"; const run = promisify(execFile); const PLAID_CLI = process.env.PLAID_CLI ?? "/opt/hatch/bin/plaid"; @@ -100,7 +100,7 @@ async function plaidCandidates(start: string, end: string): Promise 0)) continue; if (EXCLUDED_CATEGORIES.has(t.category?.[0] ?? "")) continue; const merchant = (t.merchant_name ?? t.name ?? "").trim(); - if (!merchant || !/^\d{4}-\d{2}-\d{2}$/.test(t.date)) continue; + if (!merchant || !isRealCalendarDate(t.date)) continue; const method = methodLabel(accounts.get(t.account_id)); out.push({ source: "plaid", at: t.date, merchant, amountUsd: t.amount, ...(method ? { method } : {}) }); } @@ -234,7 +234,7 @@ async function gmailCandidates( continue; } const at = gmailMessageDay(m); - if (!/^\d{4}-\d{2}-\d{2}$/.test(at)) { + if (!isRealCalendarDate(at)) { skipped++; continue; } diff --git a/pm-stack/aggregator/src/charges-collect.ts b/pm-stack/aggregator/src/charges-collect.ts index c8d0dd35cb..22886001d7 100644 --- a/pm-stack/aggregator/src/charges-collect.ts +++ b/pm-stack/aggregator/src/charges-collect.ts @@ -14,6 +14,7 @@ import { DEDUPE_WINDOW_DAYS, + isRealCalendarDate, normalizeMerchantName, type ChargeCandidate, type UnmatchedCharge, @@ -41,12 +42,14 @@ export function parseChargeCandidates(raw: unknown): ChargeCandidate[] { if (typeof entry !== "object" || entry === null) continue; const c = entry as Partial; if (c.source !== "plaid" && c.source !== "gmail") continue; - if (typeof c.at !== "string" || !/^\d{4}-\d{2}-\d{2}/.test(c.at)) continue; + if (typeof c.at !== "string") continue; + const at = c.at.slice(0, 10); + if (!isRealCalendarDate(at)) continue; if (typeof c.merchant !== "string" || !c.merchant.trim()) continue; if (typeof c.amountUsd !== "number" || !Number.isFinite(c.amountUsd)) continue; out.push({ source: c.source, - at: c.at.slice(0, 10), + at, merchant: c.merchant, amountUsd: c.amountUsd, ...(typeof c.method === "string" && c.method ? { method: c.method } : {}), diff --git a/pm-stack/aggregator/src/charges-match.ts b/pm-stack/aggregator/src/charges-match.ts index fa791b8f57..8d5cd49dab 100644 --- a/pm-stack/aggregator/src/charges-match.ts +++ b/pm-stack/aggregator/src/charges-match.ts @@ -59,6 +59,21 @@ export interface ChargeMatchResult { /** Charges from two sources for the same purchase post within days. */ export const DEDUPE_WINDOW_DAYS = 3; +/** Whether a string is a real YYYY-MM-DD date in the proleptic Gregorian calendar. */ +export function isRealCalendarDate(value: string): boolean { + const match = /^(\d{4})-(\d{2})-(\d{2})$/.exec(value); + if (!match) return false; + + const year = Number(match[1]); + const month = Number(match[2]); + const day = Number(match[3]); + if (year < 1 || month < 1 || month > 12 || day < 1) return false; + + const leapYear = year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0); + const daysInMonth = [31, leapYear ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; + return day <= daysInMonth[month - 1]!; +} + const DROP_TOKENS = new Set([ "US", "USA", diff --git a/pm-stack/aggregator/test/charges-collect.test.ts b/pm-stack/aggregator/test/charges-collect.test.ts index 277c80c05b..2043bb50ba 100644 --- a/pm-stack/aggregator/test/charges-collect.test.ts +++ b/pm-stack/aggregator/test/charges-collect.test.ts @@ -40,6 +40,25 @@ describe("parseChargeCandidates", () => { expect(parseChargeCandidates("nope")).toEqual([]); expect(parseChargeCandidates({ charges: [] })).toEqual([]); }); + + test("rejects impossible calendar dates but keeps leap days and timestamp slicing", () => { + const dates = [ + "2026-13-45", + "2026-02-30", + "2026-00-00", + "2026-10-00", + "2026-02-29", + "2026-04-31", + "2024-02-29", + "2026-02-28T14:03:00Z", + ]; + + expect( + parseChargeCandidates(dates.map((at) => ({ source: "plaid", at, merchant: "Example", amountUsd: 1 }))).map( + (candidate) => candidate.at, + ), + ).toEqual(["2024-02-29", "2026-02-28"]); + }); }); describe("mergeChargeRows canonical-label dedupe", () => {