diff --git a/pm-stack/aggregator/src/item-history.ts b/pm-stack/aggregator/src/item-history.ts index 1bda7e710e..c30d48fe45 100644 --- a/pm-stack/aggregator/src/item-history.ts +++ b/pm-stack/aggregator/src/item-history.ts @@ -41,6 +41,7 @@ import { appendFile, mkdir, readFile } from "node:fs/promises"; import { dirname, join } from "node:path"; import type { GhState, ProjectPoolItem, RefKey } from "./model.ts"; +import { isIsoDate } from "./validate.ts"; export const ITEM_HISTORY_SCHEMA_VERSION = 1; @@ -141,10 +142,14 @@ export function isItemHistoryCutoff(v: unknown): v is ItemHistoryCutoff { if (!v || typeof v !== "object") return false; const c = v as Record; if (c.schemaVersion !== ITEM_HISTORY_SCHEMA_VERSION) return false; - if (typeof c.cutoffAt !== "string" || Number.isNaN(Date.parse(c.cutoffAt))) return false; + // Real calendar dates only (#5172): Date.parse rolls impossible dates + // over (2026-02-30 -> Mar 2) and parses non-ISO strings in a + // timezone-dependent way, so gate with isIsoDate like the sibling + // gates (snapshots.ts, #2264/#2483). + if (!isIsoDate(c.cutoffAt)) return false; const coverage = c.coverage as Record | undefined; if (!coverage || typeof coverage !== "object") return false; - if (typeof coverage.source !== "string" || typeof coverage.fetchedAt !== "string") return false; + if (typeof coverage.source !== "string" || !isIsoDate(coverage.fetchedAt)) return false; if (typeof coverage.complete !== "boolean" || typeof coverage.unavailable !== "boolean") { return false; } diff --git a/pm-stack/aggregator/test/item-history.test.ts b/pm-stack/aggregator/test/item-history.test.ts index 9792891af3..cf34e72860 100644 --- a/pm-stack/aggregator/test/item-history.test.ts +++ b/pm-stack/aggregator/test/item-history.test.ts @@ -96,6 +96,34 @@ describe("captureItemHistory", () => { }); }); +describe("isItemHistoryCutoff date gates (#5172)", () => { + const valid = () => cutoff(W1, [poolItem(7)]); + + test("valid ISO cutoff and coverage stamps pass (control)", () => { + expect(isItemHistoryCutoff(valid())).toBe(true); + }); + + test("impossible and non-ISO cutoffAt stamps are refused", () => { + for (const stamp of [ + "2026-02-30T00:00:00Z", + "2026-04-31T00:00:00Z", + "March 2, 2026", + "03/02/2026", + ]) { + expect(isItemHistoryCutoff({ ...valid(), cutoffAt: stamp })).toBe(false); + } + }); + + test("impossible and non-ISO coverage.fetchedAt stamps are refused", () => { + for (const stamp of ["not-a-date", "2026-02-30T00:00:00Z"]) { + const c = valid(); + expect( + isItemHistoryCutoff({ ...c, coverage: { ...c.coverage, fetchedAt: stamp } }), + ).toBe(false); + } + }); +}); + describe("weeklyTransitions / weeklyCompletions", () => { test("close counts once; a reopen leaves the completed count until re-closed", () => { const cutoffs = [