Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions pm-stack/aggregator/src/derive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand All @@ -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 };
}
Expand Down
30 changes: 29 additions & 1 deletion pm-stack/aggregator/test/usage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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", () => {
Expand Down