From 53635b562e776c2b4ff9789021d48437403f08b2 Mon Sep 17 00:00:00 2001 From: "developing-today[bot]" <5454422+developing-today[bot]@users.noreply.github.com> Date: Tue, 3 Nov 2026 15:58:09 -0600 Subject: [PATCH] aggregator: gate item-history cutoffAt and coverage.fetchedAt with isIsoDate (#5172) isItemHistoryCutoff admitted impossible dates (Date.parse rolls 2026-02-30 to Mar 2) and non-ISO stamps for cutoffAt, and any string at all for coverage.fetchedAt, so one corrupt item-history.jsonl line silently shifted blocker-age and weekly-flow numbers. Gate both fields with isIsoDate from validate.ts, consistent with the sibling gates (#2264/#2483). Tests: item-history.test.ts 13/13 (3 new: refused stamps for both fields plus valid control; round-trip stays green). Full aggregator suite: path-segment server tests flaked under the full run and pass in isolation in both this tree and clean main. Typecheck: exactly the 2 pre-existing baseline errors (GhKind, overview HealthView), none new. Refs #5172. Task 8855. --- pm-stack/aggregator/src/item-history.ts | 9 ++++-- pm-stack/aggregator/test/item-history.test.ts | 28 +++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) 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 = [