diff --git a/pm-stack/aggregator/src/snapshot-records.ts b/pm-stack/aggregator/src/snapshot-records.ts index a613d686d3..a13c75de98 100644 --- a/pm-stack/aggregator/src/snapshot-records.ts +++ b/pm-stack/aggregator/src/snapshot-records.ts @@ -279,7 +279,10 @@ export function recordsFromProviderUsage(snap: unknown, observedBy: string): Sna codex_usage?: { usage?: { rate_limit?: { primary_window?: unknown; secondary_window?: unknown } } }; }; const ts = typeof s.generated_at === "string" ? s.generated_at : null; - if (!ts || Number.isNaN(Date.parse(ts))) return []; + // Strict gate, same as isSnapshotRecord's isIsoDate check on r.ts: + // Date.parse alone accepts impossible dates (2026-02-30) and non-ISO + // strings the store then refuses, silently dropping the snapshot (#5140). + if (!ts || parseIsoDate(ts) === null) return []; const out: SnapshotRecord[] = []; if (Array.isArray(s.sessions)) { out.push({ diff --git a/pm-stack/aggregator/test/snapshot-writers.test.ts b/pm-stack/aggregator/test/snapshot-writers.test.ts index 039488da23..8d5e62805f 100644 --- a/pm-stack/aggregator/test/snapshot-writers.test.ts +++ b/pm-stack/aggregator/test/snapshot-writers.test.ts @@ -213,6 +213,18 @@ describe("recordsFromProviderUsage", () => { expect(recordsFromProviderUsage({ sessions: [] }, "check-usage")).toEqual([]); }); + test("refuses impossible and non-ISO generated_at values the store would reject (#5140)", () => { + const withTs = (generated_at: unknown) => ({ generated_at, sessions: [1] }); + // Date.parse accepts the first three; isSnapshotRecord's isIsoDate gate + // does not — records built on them were silently dropped at append. + for (const bad of ["2026-02-30T10:59:33Z", "2026-04-31T10:59:33Z", "March 2, 2026", "not-a-date"]) { + expect(recordsFromProviderUsage(withTs(bad), "check-usage")).toEqual([]); + } + const ok = recordsFromProviderUsage(withTs("2026-10-26T10:59:33Z"), "check-usage"); + expect(ok).toHaveLength(1); + expect(ok.every((r) => isSnapshotRecord(r))).toBe(true); + }); + test("skips an invalid window without suppressing sessions or a valid companion", () => { const snapshot = { generated_at: "2026-10-26T10:59:33Z",