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
32 changes: 32 additions & 0 deletions pm-stack/aggregator/scripts/backtest-report.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env bun
// Run the rolling-origin forecast backtest and print the calibration
// report (#3986):
//
// bun run scripts/backtest-report.ts \
// --snapshots data/forecast-snapshots.jsonl \
// --outcomes outcomes.json [--as-of <iso>]
//
// --outcomes is a JSON file holding ForecastOutcome[]:
// { subjectRef, completedAt (iso|null), recordedAt (iso) }
// One report is printed per model version in the store; aggregates
// never mix versions. The report's calibration gate is enforced in
// src/backtest.ts: below 30 resolved forecasts across 8 cutoffs the
// state is not_yet_calibrated, whatever the point metrics say.
import { readFile } from "node:fs/promises";
import { evaluateBacktest, loadSnapshots, type ForecastOutcome } from "../src/backtest.ts";

function arg(name: string): string | undefined {
const i = process.argv.indexOf(`--${name}`);
return i >= 0 ? process.argv[i + 1] : undefined;
}

const snapshotsPath = arg("snapshots");
const outcomesPath = arg("outcomes");
if (!snapshotsPath || !outcomesPath) {
console.error("usage: backtest-report.ts --snapshots <jsonl> --outcomes <json> [--as-of <iso>]");
process.exit(2);
}
const snapshots = await loadSnapshots(snapshotsPath);
const outcomes = JSON.parse(await readFile(outcomesPath, "utf8")) as ForecastOutcome[];
const reports = evaluateBacktest({ snapshots, outcomes, asOf: arg("as-of") ?? new Date().toISOString() });
console.log(JSON.stringify(reports, null, 2));
66 changes: 66 additions & 0 deletions pm-stack/aggregator/scripts/forecast-snapshot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#!/usr/bin/env bun
// Capture one weekly forecast snapshot into the backtest store
// (#3986). The weekly job is this command on a timer:
//
// bun run scripts/forecast-snapshot.ts \
// --forecasts forecasts.json --out data/forecast-snapshots.jsonl \
// --watermark item-history=<cursor> [--cutoff <iso>]
//
// --forecasts is a JSON file as served by GET /api/forecasts.json:
// an object whose `contracts` array holds every forecast contract
// (epic-completion, quota-exhaustion, and stall-risk). The CLI selects
// the epic-completion contracts at this boundary; the snapshot store
// itself keeps refusing anything else. A bare ForecastContract[] (or
// an object with a `forecasts` array) is also accepted and passed
// through unfiltered, so a mixed file in those shapes still fails the
// store's strict validation instead of being silently trimmed. The
// snapshot freezes the epic-completion forecasts with their model
// version, the cutoff, and the source watermarks that name exactly
// what source state they were derived from.
import { readFile } from "node:fs/promises";
import { appendSnapshot, createSnapshot } from "../src/backtest.ts";
import type { ForecastContract } from "../src/forecast-contract.ts";

function arg(name: string): string | undefined {
const i = process.argv.indexOf(`--${name}`);
return i >= 0 ? process.argv[i + 1] : undefined;
}

const forecastsPath = arg("forecasts");
const out = arg("out");
if (!forecastsPath || !out) {
console.error("usage: forecast-snapshot.ts --forecasts <json> --out <jsonl> [--cutoff <iso>] [--watermark k=v ...]");
process.exit(2);
}
const parsed = JSON.parse(await readFile(forecastsPath, "utf8")) as
| ForecastContract[]
| { forecasts?: ForecastContract[]; contracts?: ForecastContract[] };
let forecasts: ForecastContract[];
if (Array.isArray(parsed)) {
forecasts = parsed;
} else if (Array.isArray(parsed.contracts)) {
// The actual /api/forecasts.json shape: select the epic-completion
// contracts here; createSnapshot keeps its strict validation for
// direct callers (Codex P2, PR #8436).
forecasts = parsed.contracts.filter((f) => f.type === "epic_completion");
} else if (Array.isArray(parsed.forecasts)) {
forecasts = parsed.forecasts;
} else {
console.error("forecast-snapshot: --forecasts file has no contracts/forecasts array (expected the /api/forecasts.json shape)");
process.exit(2);
}
const sourceWatermarks: Record<string, string> = {};
for (let i = 0; i < process.argv.length; i++) {
if (process.argv[i] === "--watermark") {
const [k, ...v] = (process.argv[i + 1] ?? "").split("=");
if (k) sourceWatermarks[k] = v.join("=");
}
}
const snapshot = createSnapshot({
forecasts,
capturedAt: new Date().toISOString(),
sourceWatermarks,
cutoff: arg("cutoff"),
});
await appendSnapshot(out, snapshot);
console.log(`snapshot: model=${snapshot.modelVersion} cutoff=${snapshot.cutoff} forecasts=${snapshot.forecasts.length} -> ${out}`);
Loading