From 90fed14d71d8ac236f2ceeeefa30cafa0e442e12 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Fri, 30 Oct 2026 22:58:34 -0500 Subject: [PATCH] =?UTF-8?q?aggregator:=20freshness()=20future-stamp=20boun?= =?UTF-8?q?d=20=E2=80=94=20stale=20beyond=201-day=20tolerance=20(#1568)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pm-stack/aggregator/src/derive.ts | 13 +++++++++++ pm-stack/aggregator/test/usage.test.ts | 30 +++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/pm-stack/aggregator/src/derive.ts b/pm-stack/aggregator/src/derive.ts index a1b9aecf34..ebce1ff3ac 100644 --- a/pm-stack/aggregator/src/derive.ts +++ b/pm-stack/aggregator/src/derive.ts @@ -891,6 +891,14 @@ export interface Freshness { ageDays: number | null; } +/** + * How far a measuredAt may sit in the future before it stops being + * believable (#1568). Date-only granularity and timezone noise can put a + * stamp a few hours — at most a day — ahead; beyond that the stamp is + * skewed or wrong, and its age is honestly unknown. + */ +export const FRESHNESS_FUTURE_TOLERANCE_MS = 86_400_000; // 1 day + /** A card is stale when its freshest read is older than its cadence (§1.1). */ export function freshness(measuredAt: string | null, cadenceDays: number, now = new Date()): Freshness { if (!measuredAt) return { stale: true, ageDays: null }; @@ -900,11 +908,16 @@ export function freshness(measuredAt: string | null, cadenceDays: number, now = const t = Date.parse(`${measuredAt}T00:00:00Z`); if (Number.isNaN(t)) return { stale: true, ageDays: null }; const startOfToday = Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate()); + // A date-only stamp more than the tolerance past today's UTC midnight + // is a future stamp, not a fresh one (#1568): without this bound the + // clamp below would pin it "today" until its date arrives. + if (t - startOfToday > FRESHNESS_FUTURE_TOLERANCE_MS) return { stale: true, ageDays: null }; const ageDays = Math.max(0, Math.round((startOfToday - t) / 86_400_000)); return { stale: ageDays > cadenceDays, ageDays }; } const t = Date.parse(measuredAt); if (Number.isNaN(t)) return { stale: true, ageDays: null }; + if (t - now.getTime() > FRESHNESS_FUTURE_TOLERANCE_MS) return { stale: true, ageDays: null }; const ageDays = Math.max(0, Math.floor((now.getTime() - t) / 86_400_000)); return { stale: ageDays > cadenceDays, ageDays }; } diff --git a/pm-stack/aggregator/test/usage.test.ts b/pm-stack/aggregator/test/usage.test.ts index 4a0bb4425e..efcf3472d9 100644 --- a/pm-stack/aggregator/test/usage.test.ts +++ b/pm-stack/aggregator/test/usage.test.ts @@ -8,7 +8,7 @@ import { tmpdir } from "node:os"; import { join } from "node:path"; import { buildAttentionFlags, freshness, projectPace, resetMarkers } from "../src/derive.ts"; import { countedEstimate, type QuotaView, type SnapshotRecord } from "../src/model.ts"; -import { renderQuota, renderServiceDetail, fmtMetricValue } from "../src/render.ts"; +import { renderQuota, renderServiceDetail, fmtMetricValue, freshnessStamp } from "../src/render.ts"; import { extractCharges, extractPaygLines, extractSaasServices, extractSubscriptions } from "../src/saas-extract.ts"; import { SnapshotStore, snapshotFileFor } from "../src/snapshots.ts"; import { fixtureSource } from "../src/sources/fixtures.ts"; @@ -269,6 +269,34 @@ describe("pace + freshness (§4, §1.1)", () => { expect(freshness("2026-10-01", 7, now).stale).toBe(true); expect(freshness(null, 7, now)).toEqual({ stale: true, ageDays: null }); }); + test("freshness: a far-future stamp is stale with unknown age (#1568)", () => { + const now = new Date("2026-10-26T12:00:00Z"); + // Date-only form: +30d and +365d are beyond the 1-day tolerance. + expect(freshness("2026-11-25", 7, now)).toEqual({ stale: true, ageDays: null }); + expect(freshness("2027-10-26", 7, now)).toEqual({ stale: true, ageDays: null }); + // Datetime form: +30d, +365d, and even +2d are beyond tolerance. + expect(freshness("2026-11-25T12:00:00Z", 7, now)).toEqual({ stale: true, ageDays: null }); + expect(freshness("2027-10-26T12:00:00Z", 7, now)).toEqual({ stale: true, ageDays: null }); + expect(freshness("2026-10-28T12:00:00Z", 7, now)).toEqual({ stale: true, ageDays: null }); + // Garbage is the same honest unknown-age shape. + expect(freshness("not-a-date-at-all", 7, now)).toEqual({ stale: true, ageDays: null }); + }); + test("freshness: a near-future stamp within tolerance is today, not stale (#1568)", () => { + const now = new Date("2026-10-26T12:00:00Z"); + // Date-only granularity: tomorrow's date is exactly at the tolerance. + expect(freshness("2026-10-27", 7, now)).toEqual({ stale: false, ageDays: 0 }); + // Datetime: +12h of timezone/clock noise clamps to age 0. + expect(freshness("2026-10-27T00:00:00Z", 7, now)).toEqual({ stale: false, ageDays: 0 }); + // Past controls unchanged. + expect(freshness("2026-10-25", 7, now)).toEqual({ stale: false, ageDays: 1 }); + expect(freshness("2026-10-18", 7, now)).toEqual({ stale: true, ageDays: 8 }); + }); + test("freshnessStamp: a far-future stamp renders the stale badge, never 'today' (#1568)", () => { + const future = new Date(Date.now() + 30 * 86_400_000).toISOString().slice(0, 10); + const html = freshnessStamp(future, "console", 7); + expect(html).toContain("stale — as of"); + expect(html).not.toContain("· today"); + }); }); describe("hub renders", () => {