diff --git a/pm-stack/aggregator/src/saas-extract.ts b/pm-stack/aggregator/src/saas-extract.ts index 32b500d274..58c5dcd210 100644 --- a/pm-stack/aggregator/src/saas-extract.ts +++ b/pm-stack/aggregator/src/saas-extract.ts @@ -808,17 +808,22 @@ export function applyCloudflareExport( nowMs: number = Date.now(), ): SaasService[] { if (!doc || doc.status !== "ok" || !doc.measuredAt || !Array.isArray(doc.metrics)) return services; - const measuredMs = Date.parse(doc.measuredAt); - if (!Number.isFinite(measuredMs)) return services; + // Real-calendar ISO only (#5018): Date.parse rolls impossible dates + // over (2026-02-30 -> Mar 2) and accepts non-ISO strings, so the + // freshness window must be computed from parseIsoDate's verdict. + const measured = parseIsoDate(doc.measuredAt); + if (!measured) return services; + const measuredMs = measured.getTime(); if (nowMs - measuredMs > CLOUDFLARE_EXPORT_STALE_MS || measuredMs > nowMs + 5 * 60 * 1000) return services; // A tick that ran just before a UTC month rollover carries the // previous cycle's totals; overlaying them would compare a full month // against a fresh one's limits. Documents without cycleStart (older // producers) stay backwards compatible. if (doc.cycleStart) { - const cycleMs = Date.parse(doc.cycleStart); - if (!Number.isFinite(cycleMs)) return services; - const cycle = new Date(cycleMs); + // Same real-calendar rule (#5018): an impossible cycleStart must not + // roll into a neighbouring month and evade the same-month check. + const cycle = parseIsoDate(doc.cycleStart); + if (!cycle) return services; const current = new Date(nowMs); if (cycle.getUTCFullYear() !== current.getUTCFullYear() || cycle.getUTCMonth() !== current.getUTCMonth()) { return services; @@ -930,8 +935,10 @@ export function applySupabaseExport( ): SaasService[] { if (!doc || (doc.status !== "ok" && doc.status !== "partial")) return services; if (typeof doc.measuredAt !== "string" || !doc.measuredAt || !Array.isArray(doc.metrics)) return services; - const measuredMs = Date.parse(doc.measuredAt); - if (!Number.isFinite(measuredMs)) return services; + // Real-calendar ISO only (#5018): see the Cloudflare gate above. + const measured = parseIsoDate(doc.measuredAt); + if (!measured) return services; + const measuredMs = measured.getTime(); if (nowMs - measuredMs > SUPABASE_EXPORT_STALE_MS || measuredMs > nowMs + 5 * 60 * 1000) return services; const supabase = services.find((s) => s.id === "supabase"); if (!supabase) return services; diff --git a/pm-stack/aggregator/test/cloudflare-export.test.ts b/pm-stack/aggregator/test/cloudflare-export.test.ts index d86391d76a..5032a27c89 100644 --- a/pm-stack/aggregator/test/cloudflare-export.test.ts +++ b/pm-stack/aggregator/test/cloudflare-export.test.ts @@ -74,6 +74,48 @@ describe("applyCloudflareExport", () => { } }); + test("an impossible measuredAt date changes nothing — no Date.parse rollover (#5018)", () => { + // now is 2026-03-02T12:05Z: Date.parse("2026-02-30T12:00:00.000Z") + // rolls over to Mar 2 12:00 and would pass the freshness window. + const now = Date.parse("2026-03-02T12:05:00Z"); + for (const bad of ["2026-02-30T12:00:00.000Z", "2026-04-31T12:00:00.000Z", "March 2, 2026 12:00 UTC"]) { + const doc = freshDoc(); + doc.measuredAt = bad; + doc.cycleStart = "2026-03-01T00:00:00.000Z"; + const services = applyCloudflareExport(extractSaasServices(VIEW), doc, now); + const cf = services.find((s) => s.id === "cloudflare")!; + expect(cf.metrics.find((m) => m.id === "workers-requests")!.used).toBeNull(); + expect(cf.sourceClass).toBe("manual"); + expect(cf.measuredAt).toBe("2026-10-20"); + } + }); + + test("an impossible cycleStart changes nothing instead of rolling into the current month (#5018)", () => { + // 2026-02-30 rolls over to Mar 2 under Date.parse, which would pass + // the same-month check at a March `now`; the real-calendar gate + // refuses the document outright. + const now = Date.parse("2026-03-02T12:05:00Z"); + const doc = freshDoc(); + doc.measuredAt = "2026-03-02T12:00:00.000Z"; + doc.cycleStart = "2026-02-30T00:00:00.000Z"; + const services = applyCloudflareExport(extractSaasServices(VIEW), doc, now); + const cf = services.find((s) => s.id === "cloudflare")!; + expect(cf.metrics.find((m) => m.id === "workers-requests")!.used).toBeNull(); + expect(cf.sourceClass).toBe("manual"); + }); + + test("a leap-date measuredAt still overlays (#5018)", () => { + const now = Date.parse("2024-02-29T12:05:00Z"); + const doc = freshDoc(); + doc.measuredAt = "2024-02-29T12:00:00.000Z"; + doc.cycleStart = "2024-02-01T00:00:00.000Z"; + const services = applyCloudflareExport(extractSaasServices(VIEW), doc, now); + const cf = services.find((s) => s.id === "cloudflare")!; + expect(cf.metrics.find((m) => m.id === "workers-requests")!.used).toBe(75225); + expect(cf.sourceClass).toBe("api"); + expect(cf.measuredAt).toBe("2024-02-29"); + }); + test("previous cycle's document keeps the prose fallback at a month rollover", () => { const doc = freshDoc(); doc.cycleStart = "2026-09-01T00:00:00.000Z"; diff --git a/pm-stack/aggregator/test/supabase-export.test.ts b/pm-stack/aggregator/test/supabase-export.test.ts index f04349c44c..f5226b226e 100644 --- a/pm-stack/aggregator/test/supabase-export.test.ts +++ b/pm-stack/aggregator/test/supabase-export.test.ts @@ -171,6 +171,28 @@ describe("applySupabaseExport", () => { expect(metricOf(out, "db-size")).toEqual(recordedDb); }); + test("an impossible or non-ISO measuredAt changes nothing — no Date.parse rollover (#5018)", () => { + // now is 2026-03-02T12:05Z: Date.parse rolls 2026-02-30 over to + // Mar 2 12:00, inside the freshness window; the real-calendar gate + // refuses it, and non-ISO stamps are refused the same way. + const now = Date.parse("2026-03-02T12:05:00Z"); + for (const bad of ["2026-02-30T12:00:00.000Z", "2026-04-31T12:00:00.000Z", "March 2, 2026 12:00 UTC"]) { + const base = extractSaasServices(VIEW); + const out = applySupabaseExport(base, freshDoc({ measuredAt: bad }), now); + expect(out).toEqual(base); + expect(supabaseCard(out).sourceClass).toBe("manual"); + } + }); + + test("a leap-date measuredAt still overlays (#5018)", () => { + const now = Date.parse("2024-02-29T12:05:00Z"); + const base = extractSaasServices(VIEW); + const out = applySupabaseExport(base, freshDoc({ measuredAt: "2024-02-29T12:00:00.000Z" }), now); + expect(metricOf(out, "db-size").used).toBe(1.154); + expect(supabaseCard(out).sourceClass).toBe("api"); + expect(supabaseCard(out).measuredAt).toBe("2024-02-29"); + }); + test("a stale export falls back to exactly today's card", () => { const base = extractSaasServices(VIEW); const out = applySupabaseExport(base, freshDoc({ measuredAt: "2026-10-29T12:00:00Z" }), NOW);