From 2ad02ec9f47984d297e25bc0dfede85509a609c7 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Mon, 23 Nov 2026 11:46:34 -0600 Subject: [PATCH] fix(pm-stack): render arrival predictions with the shared C4 formatter (#13408) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit quota-arrival.ts carried its own renderPredictionRange (ms-number signature, duration and day forms only — its header admitted the calendar form was "not yet"), diverging from the one §4 formatter the design names and the other C4 kinds already use. - buildArrivalRecords now calls renderPredictionRange from quota-downtime.ts (ISO-string signature) at both bases; the local copy and its private helpers are deleted. - ARRIVAL_METHODOLOGY_VERSION bumps c4-meth:1 -> c4-meth:2 with the §6 entry appended to c4-methodology-log.jsonl, per the issue's prescription (methodology-visible change, never a silent edit). earliest/latest/likely instants, bases, and scoring are unchanged. - Arrival tests pin the shared forms: duration form for a same-day future range, calendar form for ranges crossing Chicago day boundaries (including the detector-history record whose earliest bound precedes issuance — previously rendered as a relative-day string). Verification (laptop worktree): bun test on the C4 files (quota-arrival, quota-downtime, quota-reset-duration, quota-forecast-alerts, quota-forecast, quota-forecast-accuracy, quota-weekly-review) — 98 pass / 0 fail; bun x tsc --noEmit clean. Closes #13408 --- pm-stack/aggregator/c4-methodology-log.jsonl | 3 +- pm-stack/aggregator/src/quota-arrival.test.ts | 31 ++++++++++---- pm-stack/aggregator/src/quota-arrival.ts | 41 +++++++------------ 3 files changed, 40 insertions(+), 35 deletions(-) diff --git a/pm-stack/aggregator/c4-methodology-log.jsonl b/pm-stack/aggregator/c4-methodology-log.jsonl index 3fdff105cb..cea5bda8e4 100644 --- a/pm-stack/aggregator/c4-methodology-log.jsonl +++ b/pm-stack/aggregator/c4-methodology-log.jsonl @@ -1 +1,2 @@ -{"change":"First C4 methodology. downtime_window records (C4.1, #13257) derive the complete-downtime interval by intersecting the Stage-3 S1 per-pool wall intervals [wall, reset) across every routing pool (keyed by canonical provider id), basis since_last_reset; the prediction range is [latest member wall, earliest member reset), rendered by the §4 formatter at issuance. Providers the forecast cannot project publish insufficient_history and veto a no-window verdict; a defensible no-window tick publishes no record. No day-type or spike-day adjustments (inputs.adjustments is []).","evidence":["doc/2026-11-13T15-12-00Z_design_reporting-architecture-event-driven/c4-forecasting-design.md","#12695 forecast product (quota-forecast.json)"],"expected_effect":"Downtime-window predictions exist on every gatherer tick and are scorable per design §3 (start/end error, interval overlap); §3's accuracy bar is coverage inside the calibrated band with median window error under one gatherer tick span at one-tick lead.","reason":"The C4 design (doc/2026-11-13T15-12-00Z_design_reporting-architecture-event-driven/c4-forecasting-design.md, PR #13268) specifies the record, scoring, and methodology-log formats, and S1's same-period pace projection is the only measured per-pool wall source; §5 adjustments are data-gated below their sufficiency thresholds.","review_after":"2026-12-21","ts":"2026-11-23T13:10:00.000Z","version":"c4-meth:1"} \ No newline at end of file +{"change":"First C4 methodology. downtime_window records (C4.1, #13257) derive the complete-downtime interval by intersecting the Stage-3 S1 per-pool wall intervals [wall, reset) across every routing pool (keyed by canonical provider id), basis since_last_reset; the prediction range is [latest member wall, earliest member reset), rendered by the §4 formatter at issuance. Providers the forecast cannot project publish insufficient_history and veto a no-window verdict; a defensible no-window tick publishes no record. No day-type or spike-day adjustments (inputs.adjustments is []).","evidence":["doc/2026-11-13T15-12-00Z_design_reporting-architecture-event-driven/c4-forecasting-design.md","#12695 forecast product (quota-forecast.json)"],"expected_effect":"Downtime-window predictions exist on every gatherer tick and are scorable per design §3 (start/end error, interval overlap); §3's accuracy bar is coverage inside the calibrated band with median window error under one gatherer tick span at one-tick lead.","reason":"The C4 design (doc/2026-11-13T15-12-00Z_design_reporting-architecture-event-driven/c4-forecasting-design.md, PR #13268) specifies the record, scoring, and methodology-log formats, and S1's same-period pace projection is the only measured per-pool wall source; §5 adjustments are data-gated below their sufficiency thresholds.","review_after":"2026-12-21","ts":"2026-11-23T13:10:00.000Z","version":"c4-meth:1"} +{"change": "Arrival-kind rendering unified on the one shared §4 formatter (renderPredictionRange in quota-downtime.ts): model_plan_arrival records (C4.4, #13260) now store the shared formatter's renderings at issuance, replacing the arrival module's own duration/day-only copy, which had no calendar form. Stored rendered strings for arrival ranges change accordingly — a range crossing Chicago day boundaries now renders in calendar form (e.g. \"between Saturday morning and Tuesday morning\" in place of \"between 2.5 and 5.7 days from now\"), and a range whose earliest bound precedes issuance renders in calendar form instead of a negative relative duration. earliest/latest/likely instants, bases, and scoring are unchanged.", "evidence": ["doc/2026-11-13T15-12-00Z_design_reporting-architecture-event-driven/c4-forecasting-design.md §4 (one formatter)", "#13408"], "expected_effect": "Arrival records render identically to downtime_window and reset_duration records for the same ranges — one §4 surface for readers and for the S7 weekly review; arrival scores remain attributable per methodology_version.", "reason": "Design §4 specifies one formatter producing the stored rendered string; the arrival module's duplicate had already diverged (no compound or calendar forms), so the kinds rendered the same ranges differently.", "review_after": "2026-12-21", "ts": "2026-11-23T17:45:40.000Z", "version": "c4-meth:2"} diff --git a/pm-stack/aggregator/src/quota-arrival.test.ts b/pm-stack/aggregator/src/quota-arrival.test.ts index 9b21e965b3..f1ec1a200c 100644 --- a/pm-stack/aggregator/src/quota-arrival.test.ts +++ b/pm-stack/aggregator/src/quota-arrival.test.ts @@ -1,11 +1,11 @@ import { describe, expect, test } from "bun:test"; import { buildArrivalRecords, - renderPredictionRange, scoreArrivalPrediction, validateArrivalRecord, type ArrivalPredictionRecord, } from "./quota-arrival.ts"; +import { renderPredictionRange } from "./quota-downtime.ts"; import type { AnomalyFinding } from "./quota-anomaly.ts"; // Oracle: the C4 design record (doc/2026-11-13T15-12-00Z_design_ @@ -93,7 +93,10 @@ describe("detector_history basis", () => { expect(rec.prediction?.likely).toBe(iso(T0 + 37.5 * DAY)); expect(rec.inputs.samples).toBe(3); expect(rec.inputs.span).toEqual({ from: iso(T0), to: iso(T0 + 25 * DAY) }); - expect(rec.prediction?.rendered).toContain("days from now"); + // Shared §4 formatter: the range crosses Chicago day boundaries + // (and its earliest bound precedes issuance), so it renders in + // calendar form — never a negative or relative-day string. + expect(rec.prediction?.rendered).toBe("between Wednesday evening and this evening"); }); test("several findings at one instant are one arrival", () => { @@ -235,13 +238,27 @@ describe("validateArrivalRecord — the no-speculative-dates guard", () => { }); }); -describe("renderPredictionRange (§4 forms)", () => { - test("spans under 48h render in half-hour-rounded hours", () => { - expect(renderPredictionRange(T0 + 4 * HOUR, T0 + 6.4 * HOUR, T0)).toBe("between 4 and 6.5 hours from now"); +describe("renderPredictionRange — the shared §4 formatter (#13408)", () => { + // The arrival kind stores the one shared formatter's renderings + // (quota-downtime.ts); these pin the forms for arrival-shaped + // ranges so the kinds cannot drift apart silently. Oracle: + // design §4 and quota-downtime.test.ts. + test("a same-day future range renders in duration form, half-hour rounded", () => { + expect(renderPredictionRange("2026-10-01T16:15:00.000Z", "2026-10-01T19:45:00.000Z", "2026-10-01T14:00:00.000Z")).toBe( + "between 2.5 and 6 hours from now", + ); }); - test("spans of 48h or more render in days", () => { - expect(renderPredictionRange(T0 + 2.5 * DAY, T0 + 5.7 * DAY, T0)).toBe("between 2.5 and 5.7 days from now"); + test("a multi-day range crossing Chicago day boundaries renders in calendar form", () => { + expect(renderPredictionRange(iso(T0 + 2.5 * DAY), iso(T0 + 5.7 * DAY), iso(T0))).toBe( + "between Saturday morning and Tuesday morning", + ); + }); + + test("a sub-48h range crossing a Chicago day boundary renders in calendar form", () => { + expect(renderPredictionRange(iso(T0 + 4 * HOUR), iso(T0 + 6.4 * HOUR), iso(T0))).toBe( + "between today 11 pm and tomorrow 1 am", + ); }); }); diff --git a/pm-stack/aggregator/src/quota-arrival.ts b/pm-stack/aggregator/src/quota-arrival.ts index 8a189f8181..fa3f860ab5 100644 --- a/pm-stack/aggregator/src/quota-arrival.ts +++ b/pm-stack/aggregator/src/quota-arrival.ts @@ -38,9 +38,10 @@ // no_basis / insufficient_history records are never scored — they // are the honest answer, and scoring them would pay for guessing. // -// rendered (§4): the duration and day forms are implemented; the -// calendar form (day names / dayparts) is not yet, and the machine -// range never leaves the record regardless. +// rendered (§4): the stored string comes from the one shared +// formatter (renderPredictionRange in quota-downtime.ts) at +// issuance — what was shown is exactly what the scorer grades; +// the machine range never leaves the record regardless. // // Pure functions over plain values — no I/O, no clock reads except // through arguments (the quota-anomaly.ts discipline). The live @@ -48,11 +49,15 @@ // mirror under source "quota-prediction", design §1) is a follow-up // slice; this module is the record contract that wiring writes. import type { AnomalyFinding } from "./quota-anomaly.ts"; +import { renderPredictionRange } from "./quota-downtime.ts"; -/** First methodology-log entry for the arrival kind (design §6): - * every record this module emits names it, so a score is always - * attributable to the method that produced it. */ -export const ARRIVAL_METHODOLOGY_VERSION = "c4-meth:1"; +/** Methodology version for the arrival kind (design §6; the + * c4-methodology-log.jsonl entry of the same name): every record + * this module emits names it, so a score is always attributable + * to the method that produced it. c4-meth:2 (#13408) unified the + * stored rendering on the one shared §4 formatter; c4-meth:1 + * records rendered with this module's own duration/day-only copy. */ +export const ARRIVAL_METHODOLOGY_VERSION = "c4-meth:2"; /** Ledger source the live wiring mirrors arrival records under * (design §1) — distinct from S1's "quota-forecast"; C4 records @@ -154,24 +159,6 @@ function dur(ms: number): string { return `${Number.isInteger(d) ? String(d) : d.toFixed(1)} days`; } -const roundHalf = (n: number): number => Math.round(n * 2) / 2; -const round1 = (n: number): number => Math.round(n * 10) / 10; -const fmtNum = (n: number): string => (Number.isInteger(n) ? String(n) : n.toFixed(1)); - -/** The §4 range rendering, duration and day forms: duration form - * (span < 48h) "between X and Y hours from now" with X/Y rounded to - * the half hour; day form (span >= 48h) in days to one decimal. */ -export function renderPredictionRange(earliestMs: number, latestMs: number, issuedMs: number): string { - if (latestMs - earliestMs < 48 * HOUR_MS) { - const x = roundHalf((earliestMs - issuedMs) / HOUR_MS); - const y = roundHalf((latestMs - issuedMs) / HOUR_MS); - return `between ${fmtNum(x)} and ${fmtNum(y)} hours from now`; - } - const x = round1((earliestMs - issuedMs) / DAY_MS); - const y = round1((latestMs - issuedMs) / DAY_MS); - return `between ${fmtNum(x)} and ${fmtNum(y)} days from now`; -} - /** The distinct arrival instants a pool's plan-change findings * record, ascending. Findings are per-window; several at one * instant are one arrival (the plan changed once). Findings with no @@ -260,7 +247,7 @@ export function buildArrivalRecords(input: ArrivalBuildInput): ArrivalPrediction earliest: iso(earliest), latest: iso(latest), likely: iso(last + median(sorted)), - rendered: renderPredictionRange(earliest, latest, issuedMs), + rendered: renderPredictionRange(iso(earliest), iso(latest), iso(issuedMs)), }, state: "predicted", state_reason: undefined, @@ -318,7 +305,7 @@ export function buildArrivalRecords(input: ArrivalBuildInput): ArrivalPrediction earliest: iso(earliest), latest: iso(latest), likely: iso(anchorMs + k * cad.periodMs), - rendered: renderPredictionRange(earliest, latest, issuedMs), + rendered: renderPredictionRange(iso(earliest), iso(latest), iso(issuedMs)), }, state: "predicted", state_reason: undefined,