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
3 changes: 2 additions & 1 deletion pm-stack/aggregator/c4-methodology-log.jsonl
Original file line number Diff line number Diff line change
@@ -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"}
{"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"}
31 changes: 24 additions & 7 deletions pm-stack/aggregator/src/quota-arrival.test.ts
Original file line number Diff line number Diff line change
@@ -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_
Expand Down Expand Up @@ -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", () => {
Expand Down Expand Up @@ -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",
);
});
});

Expand Down
41 changes: 14 additions & 27 deletions pm-stack/aggregator/src/quota-arrival.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,26 @@
// 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
// wiring (gatherer tick -> quota-predictions.jsonl + the Turso
// 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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down