diff --git a/pm-stack/aggregator/src/derive.ts b/pm-stack/aggregator/src/derive.ts index ebce1ff3ac..888a67a4e3 100644 --- a/pm-stack/aggregator/src/derive.ts +++ b/pm-stack/aggregator/src/derive.ts @@ -904,13 +904,19 @@ export function freshness(measuredAt: string | null, cadenceDays: number, now = if (!measuredAt) return { stale: true, ageDays: null }; // Date-only stamps age by calendar day (a read dated yesterday is // 1 day old all day today); datetimes age by elapsed whole days. + // The calendar is Chicago's, the timezone every other calendar + // helper here uses (#1629): a stamp is a Chicago date, so ageing it + // against the UTC day called a read dated today "1d ago" -- and + // stale -- from 19:00 Chicago onward, when UTC had rolled over and + // Chicago had not. if (measuredAt.length <= 10) { 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. + const startOfToday = Date.parse(`${chicagoYmd(now)}T00:00:00Z`); + // A date-only stamp more than the tolerance past today's Chicago + // 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 }; diff --git a/pm-stack/aggregator/test/usage.test.ts b/pm-stack/aggregator/test/usage.test.ts index 1388d8bb61..5584a3c041 100644 --- a/pm-stack/aggregator/test/usage.test.ts +++ b/pm-stack/aggregator/test/usage.test.ts @@ -320,6 +320,29 @@ 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: date-only stamps age by the Chicago calendar day (#1629)", () => { + // 19:30 CDT on 2026-10-25 is already 2026-10-26 in UTC. A read + // dated today (Chicago) is 0 days old for the whole Chicago day; + // the UTC-day age called it 1d old -- and stale at cadence 0 -- + // from 19:00 Chicago onward. + const cdtEvening = new Date("2026-10-26T00:30:00Z"); + expect(freshness("2026-10-25", 7, cdtEvening)).toEqual({ stale: false, ageDays: 0 }); + expect(freshness("2026-10-25", 0, cdtEvening)).toEqual({ stale: false, ageDays: 0 }); + expect(freshness("2026-10-24", 7, cdtEvening)).toEqual({ stale: false, ageDays: 1 }); + expect(freshness("2026-10-24", 0, cdtEvening)).toEqual({ stale: true, ageDays: 1 }); + // Still today at 23:59 Chicago. + expect(freshness("2026-10-25", 7, new Date("2026-10-26T04:59:00Z"))).toEqual({ + stale: false, + ageDays: 0, + }); + // The CST evening of the same shape: 19:00 CST on 2026-01-14 is + // 2026-01-15 in UTC. + const cstEvening = new Date("2026-01-15T01:00:00Z"); + expect(freshness("2026-01-14", 7, cstEvening)).toEqual({ stale: false, ageDays: 0 }); + expect(freshness("2026-01-13", 7, cstEvening)).toEqual({ stale: false, ageDays: 1 }); + // Tomorrow's Chicago date is still within the future tolerance. + expect(freshness("2026-10-26", 7, cdtEvening)).toEqual({ stale: false, ageDays: 0 }); + }); 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.