diff --git a/docs/calibration-tree-artifacts.md b/docs/calibration-tree-artifacts.md index 472179e8..5d781c48 100644 --- a/docs/calibration-tree-artifacts.md +++ b/docs/calibration-tree-artifacts.md @@ -367,7 +367,10 @@ files of the run's stored tree, whether its diagnostics came from the run folder or its staged bundle. A run with diagnostics shows them once the collector has built its tree and, when the run's files declare the diagnostics' digest (in a structured run manifest or a staged bundle's receipt), only while the tree -holds those diagnostics. While a run's copy records no end and the page's +holds those diagnostics. A version 1 run's reform validation is the copy of +its top-level `reform_validation.json`; a version 2 or 3 run's is the copy of +`artifacts/reform_validation.json`, shown only when its run manifest declares +a `reform_validation` artifact with that copy's digest. While a run's copy records no end and the page's state for it comes from the collector's record, its Stage timeline and calibration progress come from that record too. The Stage timeline lists the record's stage changes, including the stage ends the emitter adds, and its diff --git a/frontend/app/api/microcosm/us-version-2-staging-run.test.ts b/frontend/app/api/microcosm/us-version-2-staging-run.test.ts new file mode 100644 index 00000000..1fae5245 --- /dev/null +++ b/frontend/app/api/microcosm/us-version-2-staging-run.test.ts @@ -0,0 +1,335 @@ +// A US fiscal refresh build's staging run as the version 2 writer stages it +// (PolicyEngine/microcosm#1135), read through the Staging routes' own handlers +// against a fake collector store: its list entry, its detail, its calibration +// and its reform validation, which version 2 writes under `artifacts/`. A +// version 1 run's top-level reform validation is still read. +import { afterEach, beforeEach, expect, test } from "bun:test"; + +import diagnosticsFixture from "@/lib/microcosm/fixtures/zz-release/calibration_diagnostics.json"; +import { clearBuildRunCaches, loadBuildRuns } from "@/lib/microcosm/build-runs"; +import { + FakeCollectorStore, + type FakeStagingRun, +} from "@/lib/microcosm/test-support/fake-collector-store"; +import { + US_V2_BUILD_MANIFEST, + US_V2_CANDIDATE, + US_V2_RELEASE, + US_V2_RELEASE_MANIFEST, + US_V2_RUN, + usV2CalibrationProgress, + usV2Events, + usV2Progress, + usV2ReformValidation, + usV2RunManifest, +} from "@/lib/microcosm/test-support/us-version-2-staging-run"; + +import { GET as stagingRun } from "./staging/run/route"; +import { GET as stagingRuns } from "./staging/runs/route"; +import { GET as stagingTargetTree } from "./staging/target-tree/route"; + +const COLLECTOR_SETTINGS = [ + "MICROCOSM_TELEMETRY_COLLECTOR_URL", + "MICROCOSM_BUILD_MONITOR_COLLECTOR_ONLY", +] as const; +const originalCollectorSettings = Object.fromEntries( + COLLECTOR_SETTINGS.map((name) => [name, process.env[name]]), +); + +type RunFiles = FakeStagingRun["files"]; + +/** The run's copied files: its documents, declared result files and reform validation. */ +function usV2RunFiles(overrides: Partial = {}): RunFiles { + return { + "progress.json": usV2Progress(), + "run_manifest.json": usV2RunManifest({ diagnostics: diagnosticsFixture }), + "events.ndjson": usV2Events(), + "calibration_progress.json": usV2CalibrationProgress(), + "artifacts/reform_validation.json": usV2ReformValidation(), + ...overrides, + }; +} + +/** A store holding the US version 2 run's copies and its tree. */ +function usV2Store(files: RunFiles = usV2RunFiles()): FakeCollectorStore { + const store = new FakeCollectorStore(); + store.addBuild({ + country: "us", + kind: "staging", + stagingRunId: US_V2_RUN, + label: US_V2_CANDIDATE, + hfRepo: "policyengine/populace-us-staging", + files: { + calibrationDiagnostics: diagnosticsFixture, + buildManifest: US_V2_BUILD_MANIFEST, + releaseManifest: US_V2_RELEASE_MANIFEST, + }, + }); + store.addStagingRun({ country: "us", runId: US_V2_RUN, files }); + return store; +} + +let restore: (() => void) | null = null; + +function serve(store: FakeCollectorStore): FakeCollectorStore { + restore = store.install(); + return store; +} + +beforeEach(() => { + clearBuildRunCaches(); + // No collector: the Candidate tab shows the states the copied files record. + for (const name of COLLECTOR_SETTINGS) delete process.env[name]; +}); + +afterEach(() => { + restore?.(); + restore = null; + for (const name of COLLECTOR_SETTINGS) { + const original = originalCollectorSettings[name]; + if (original == null) delete process.env[name]; + else process.env[name] = original; + } + clearBuildRunCaches(); +}); + +function request(path: string): Request { + return new Request(`https://dashboard.example/calibration/dashboard/api/microcosm${path}`); +} + +async function runDetail(runId = US_V2_RUN) { + const response = await stagingRun(request(`/staging/run?country=us&id=${runId}`)); + expect(response.status).toBe(200); + return response.json(); +} + +test("the Candidate tab lists a US version 2 run with its version 2 identity", async () => { + const store = serve(usV2Store()); + + const response = await stagingRuns(request("/staging/runs?country=us")); + + expect(response.status).toBe(200); + const body = await response.json(); + expect(body).toMatchObject({ available: true, incompatible_runs: [] }); + expect(body.runs).toEqual([ + expect.objectContaining({ + run_id: US_V2_RUN, + candidate_release_id: US_V2_CANDIDATE, + release_id: US_V2_RELEASE, + country_code: "US", + run_kind: "release", + non_release: false, + schema_version: 3, + // The tab's state for a completed run, with the copy's own status. + status: "passed", + hugging_face_status: "completed", + hugging_face_stage: "complete", + started_at: "2026-11-27T01:00:00+00:00", + state_source: "hugging_face", + }), + ]); + expect(store.huggingFaceRequests).toEqual([]); +}); + +test("a US version 2 run's detail reads its copied version 3 documents", async () => { + const store = serve(usV2Store()); + + const body = await runDetail(); + + expect(body).toMatchObject({ + available: true, + source_repo: "policyengine/populace-us-staging", + run_id: US_V2_RUN, + candidate_release_id: US_V2_CANDIDATE, + release_id: US_V2_RELEASE, + country_code: "US", + run_kind: "release", + non_release: false, + schema_version: 3, + delivery: { mode: "local_and_remote", upload_successes: 3, read_back: "passed" }, + progress: { status: "completed", operation_id: "us_fiscal_refresh_build" }, + run_manifest: { + pipeline: { id: "us_fiscal_refresh" }, + artifacts: { + reform_validation: { + path: "artifacts/reform_validation.json", + staging_path: `runs/${US_V2_RUN}/artifacts/reform_validation.json`, + }, + }, + }, + calibration_progress: { candidate_release_id: US_V2_CANDIDATE }, + state: { state_source: "hugging_face", status: "passed" }, + }); + expect(body.calibration_progress.events.map((event: { epoch: number }) => event.epoch)).toEqual([ + 10, 100, + ]); + expect(body.events.map((event: { stage: string }) => event.stage)).toEqual([ + "created", + "calibration", + "calibration", + "reform_validation", + "complete", + ]); + expect(store.huggingFaceRequests).toEqual([]); +}); + +test("a US version 2 run's calibration comes from its declared diagnostics and manifests", async () => { + const store = serve(usV2Store()); + + const body = await runDetail(); + const tree = await stagingTargetTree(request(`/staging/target-tree?country=us&run=${US_V2_RUN}`)); + + expect(body).toMatchObject({ + has_calibration: true, + calibration: { + source: "huggingface_live", + release_id: US_V2_CANDIDATE, + final_loss: 0.0125, + }, + build_manifest: US_V2_BUILD_MANIFEST, + release_manifest: US_V2_RELEASE_MANIFEST, + }); + expect(tree.status).toBe(200); + expect(await tree.json()).toMatchObject({ releaseId: US_V2_CANDIDATE }); + expect(store.huggingFaceRequests).toEqual([]); +}); + +test("a US version 2 run shows the reform validation its run manifest declares under artifacts/", async () => { + const store = serve(usV2Store()); + + const body = await runDetail(); + + expect(body.reform_validation).not.toBeNull(); + expect(body.reform_validation).toMatchObject({ + available: true, + release_id: US_V2_CANDIDATE, + updated_at: "2026-11-27T05:00:00+00:00", + scoring_window: "FY2025-2034", + rows: [expect.objectContaining({ id: "obbba_salt_cap", within_10pct: true })], + summary: { + n_reforms: 1, + n_out_of_sample: 1, + n_out_of_sample_scored: 1, + out_of_sample_within_10pct: 1, + }, + }); + expect(body.reform_validation.summary.out_of_sample_mean_abs_relative_error).toBeCloseTo(0.05); + expect(store.huggingFaceRequests).toEqual([]); +}); + +test("a version 2 run shows no reform validation other than the one its run manifest declares", async () => { + const cases: [string, RunFiles][] = [ + // The copy holds other bytes than the run manifest declares. + ["a copy that differs", usV2RunFiles({ "artifacts/reform_validation.json": usV2ReformValidation(-50) })], + // The run manifest declares no reform validation. + [ + "an undeclared copy", + usV2RunFiles({ + "run_manifest.json": usV2RunManifest({ diagnostics: diagnosticsFixture, reformValidation: null }), + }), + ], + // Version 2 writes result files only under artifacts/. + [ + "a top-level copy", + (() => { + const { "artifacts/reform_validation.json": declared, ...files } = usV2RunFiles(); + return { ...files, "reform_validation.json": declared }; + })(), + ], + ]; + for (const [label, files] of cases) { + const store = serve(usV2Store(files)); + const body = await runDetail(); + restore?.(); + restore = null; + clearBuildRunCaches(); + + expect({ label, reform_validation: body.reform_validation }).toEqual({ + label, + reform_validation: null, + }); + expect(store.huggingFaceRequests).toEqual([]); + } +}); + +test("a version 2 run without a copied run manifest shows no reform validation", async () => { + const { "run_manifest.json": _manifest, ...files } = usV2RunFiles(); + const store = serve(usV2Store(files)); + + const body = await runDetail(); + + expect(body.progress).toMatchObject({ schema_version: 3, run_id: US_V2_RUN }); + expect(body.reform_validation).toBeNull(); + expect(store.huggingFaceRequests).toEqual([]); +}); + +test("a version 3 run whose index summary records no version shows no top-level reform validation", async () => { + // The collector records a run whose run manifest copy isn't a JSON object + // as a problem, with no version in its summary; its progress and events are + // still version 3. + const { "artifacts/reform_validation.json": declared, ...files } = usV2RunFiles(); + const store = new FakeCollectorStore(); + store.addStagingRun({ + country: "us", + runId: US_V2_RUN, + files: { ...files, "run_manifest.json": "not a JSON object", "reform_validation.json": declared }, + problem: "The run manifest is not a JSON object.", + summary: { schema_version: null }, + }); + serve(store); + + const body = await runDetail(); + + expect(body.progress).toMatchObject({ schema_version: 3, run_id: US_V2_RUN }); + expect(body.reform_validation).toBeNull(); + expect(store.huggingFaceRequests).toEqual([]); +}); + +test("the calibration and Build progress readers don't read a version 2 run's reform validation", async () => { + const store = serve(usV2Store()); + const copiedFiles = store.stagingIndex("us").runs[US_V2_RUN].files as Record< + string, + { path: string } | undefined + >; + const reformValidationCopy = copiedFiles["artifacts/reform_validation.json"]?.path; + + const tree = await stagingTargetTree(request(`/staging/target-tree?country=us&run=${US_V2_RUN}`)); + const builds = await loadBuildRuns("staging", "us"); + + expect(reformValidationCopy).toBeString(); + expect(tree.status).toBe(200); + expect(builds.runs.map((run) => run.run_id)).toEqual([US_V2_RUN]); + expect(store.requests).not.toContain(reformValidationCopy); +}); + +test("a US version 1 run still shows its top-level reform validation", async () => { + const runId = "populace-us-2026-v1-run-20261020T010000Z"; + const candidate = "populace-us-2026-v1-candidate"; + const store = new FakeCollectorStore(); + store.addStagingRun({ + country: "us", + runId, + files: { + "progress.json": { + schema_version: 1, + run_id: runId, + candidate_release_id: candidate, + status: "passed", + stage: "complete", + started_at: "2026-10-20T01:00:00+00:00", + updated_at: "2026-10-20T02:00:00+00:00", + }, + "reform_validation.json": { ...usV2ReformValidation(), release_id: candidate }, + }, + }); + serve(store); + + const body = await runDetail(runId); + + expect(body.reform_validation).toMatchObject({ + available: true, + release_id: candidate, + summary: { n_reforms: 1, out_of_sample_within_10pct: 1 }, + }); + expect(store.huggingFaceRequests).toEqual([]); +}); diff --git a/frontend/lib/microcosm/stored-staging.ts b/frontend/lib/microcosm/stored-staging.ts index e5c7287a..1c15eda0 100644 --- a/frontend/lib/microcosm/stored-staging.ts +++ b/frontend/lib/microcosm/stored-staging.ts @@ -4,7 +4,9 @@ // Each gives the answer the package's Hugging Face reader of the same name // gives for the same files (`loadStagingRuns`, `loadStagingRun`, // `loadStagingCalibration` and the rest), and none reads Hugging Face. The -// copies hold what a run's files said when the collector last copied them; +// one difference: a version 2 or 3 run's reform validation is the copy its +// run manifest declares under `artifacts/`, which the package's 0.1.5 Hugging +// Face reader does not read. The copies hold what a run's files said when the collector last copied them; // an unfinished run's current state comes from the collector's telemetry // when a page is requested (`staging-run-state.ts`). import { getCalibrationTreeBlob } from "@policyengine/calibration-tree/calibration-tree-blob"; @@ -52,6 +54,7 @@ import { readStagingRunFilesIndex, stagingJsonObjectOrNull, type StagingRunFileName, + type StagingRunFileRecord, type StagingRunFilesIndex, type StagingRunRecord, } from "@policyengine/calibration-tree/staging-run-files"; @@ -131,8 +134,25 @@ export interface CopiedRunDocuments { stagedDatasetReceipt: JsonObject | null; } -async function readCopiedJson(record: StagingRunRecord, name: StagingRunFileName) { - const file = record.files[name]; +// Where a version 2 or 3 run's reform validation is copied: under the path in +// the run folder its run manifest declares for the artifact. Version 1 runs +// write `reform_validation.json` at the folder's top level. +const REFORM_VALIDATION_ARTIFACT = "reform_validation"; + +function declaredArtifactPath(logicalName: Name) { + return `artifacts/${logicalName}.json` as const; +} + +const DECLARED_REFORM_VALIDATION = declaredArtifactPath(REFORM_VALIDATION_ARTIFACT); + +type CopiedFileName = StagingRunFileName | typeof DECLARED_REFORM_VALIDATION; + +function copiedFile(record: StagingRunRecord, name: CopiedFileName) { + return (record.files as Partial>)[name]; +} + +async function readCopiedJson(record: StagingRunRecord, name: CopiedFileName) { + const file = copiedFile(record, name); return file ? readStagingRunFile({ record: file, client: collectorStoreBlob }) : null; @@ -159,6 +179,8 @@ async function copiedRunRecords(country: MicrocosmCountry): Promise { const wanted = new Set(names); + // A version 2 or 3 run's copied documents say so in the index's summary, + // so its reform validation is read from where version 2 writes it. + const structured = (record.summary.schema_version ?? 1) >= 2; + const reformValidationName: CopiedFileName = structured + ? DECLARED_REFORM_VALIDATION + : "reform_validation.json"; const read = (name: StagingRunFileName) => - wanted.has(name) ? readCopiedJson(record, name) : Promise.resolve(null); + wanted.has(name) + ? readCopiedJson(record, name === "reform_validation.json" ? reformValidationName : name) + : Promise.resolve(null); const [progressBytes, manifestBytes, calibrationBytes, eventBytes, reformBytes, receiptBytes] = await Promise.all(ALL_DOCUMENTS.map(read)); const progressDocument = stagingJsonObjectOrNull(progressBytes); @@ -197,6 +230,11 @@ export async function copiedRunDocuments( const calibrationProgress = calibrationDocument && parseStagingCalibrationProgress(calibrationDocument); const events = parseStagingEvents(eventBytes && new TextDecoder().decode(eventBytes)); + // The documents' own version: a run whose summary records none may still + // hold version 2 or 3 documents, which never hold a top-level copy. + const structuredDocuments = [progress, runManifest, calibrationProgress, ...events].some( + (document) => isStructuredStagingDocument(document), + ); validateStagingRunConsistency(record.runId, { progress, runManifest, @@ -209,11 +247,35 @@ export async function copiedRunDocuments( runManifest, calibrationProgress, events, - reformValidation: stagingJsonObjectOrNull(reformBytes), + reformValidation: ( + structured + ? declaresCopy(runManifest, REFORM_VALIDATION_ARTIFACT, copiedFile(record, reformValidationName)) + : !structuredDocuments + ) + ? stagingJsonObjectOrNull(reformBytes) + : null, stagedDatasetReceipt: stagingJsonObjectOrNull(receiptBytes), }; } +/** + * Whether a structured run manifest declares the artifact `logicalName` with + * the digest of the copy: a version 2 run shows only the result files its + * run manifest declares, as they were when it declared them. + */ +function declaresCopy( + runManifest: JsonObject | null, + logicalName: string, + copy: StagingRunFileRecord | undefined, +): boolean { + if (!copy || !isStructuredStagingDocument(runManifest)) return false; + const artifact = objectOrNull(objectOrNull(runManifest?.artifacts)?.[logicalName]); + return ( + stringValue(artifact?.path) === declaredArtifactPath(logicalName) && + stringValue(artifact?.sha256) === copy.sha256 + ); +} + async function readRunDocuments( country: MicrocosmCountry, runId: string, diff --git a/frontend/lib/microcosm/test-support/fake-collector-store.ts b/frontend/lib/microcosm/test-support/fake-collector-store.ts index 3870264d..6e3bdcc7 100644 --- a/frontend/lib/microcosm/test-support/fake-collector-store.ts +++ b/frontend/lib/microcosm/test-support/fake-collector-store.ts @@ -42,6 +42,7 @@ import { import { summaryFromManifest, summaryFromProgress, + type StagingRunSummary, } from "@policyengine/calibration-tree/staging-artifact"; const MANIFEST_PATH = "calibration-trees/manifest.json"; @@ -96,12 +97,21 @@ export interface FakeBuild { files: Partial>; } +/** + * A file a run's index record may hold a copy of: the package's names, and a + * version 2 run's reform validation, which the collector copies under its + * path in the run folder. + */ +export type FakeStagingRunFileName = StagingRunFileName | "artifacts/reform_validation.json"; + export interface FakeStagingRun { country: MicrocosmCountry; runId: string; - files: Partial>; + files: Partial>; removed?: boolean; problem?: string | null; + /** Fields of the index's summary of the run that differ from its documents'. */ + summary?: Partial; } export class FakeCollectorStore { @@ -188,14 +198,14 @@ export class FakeCollectorStore { /** Copies a staging run's files into its country's index. */ addStagingRun(run: FakeStagingRun): void { const index = this.stagingIndex(run.country); - const files: Partial> = {}; + const files: Partial> = {}; for (const [name, document] of Object.entries(run.files)) { const bytes = bytesOf(document!); const digest = sha256(bytes); const compressed = gzipSync(bytes); const path = stagingRunFilePath(run.country, run.runId, digest); this.objects.set(path, compressed); - files[name as StagingRunFileName] = { + files[name as FakeStagingRunFileName] = { path, sha256: digest, rawBytes: bytes.byteLength, @@ -222,7 +232,7 @@ export class FakeCollectorStore { hfCommitSha: fakeCommit(run.runId), copiedAt: "2026-10-20T00:00:00.000Z", folderFingerprint: sha256(`folder:${run.runId}`), - summary, + summary: { ...summary, ...run.summary }, problem: run.problem ?? null, files, treeCheck: null, diff --git a/frontend/lib/microcosm/test-support/us-version-2-staging-run.ts b/frontend/lib/microcosm/test-support/us-version-2-staging-run.ts new file mode 100644 index 00000000..711a682b --- /dev/null +++ b/frontend/lib/microcosm/test-support/us-version-2-staging-run.ts @@ -0,0 +1,186 @@ +// Test support: a US fiscal refresh build's staging run in the shape the +// version 2 writer stages it (PolicyEngine/microcosm#1135): version 3 +// documents that name the run's country, operation, pipeline, candidate, +// release and run kind, and result files under `artifacts/`, each declared in +// the run manifest with its SHA-256. It is reduced to what the Candidate tab +// reads: four of the result files the writer declares, stage events only, and +// a candidate ID apart from the release ID. The digests are of each +// document's JSON text, as the fake collector store records them. +import { fakeDocumentSha256 } from "@/lib/microcosm/test-support/fake-collector-store"; + +type JsonObject = Record; + +export const US_V2_RUN = "populace-us-2026-v2-run-20261127T010000Z"; +export const US_V2_CANDIDATE = "populace-us-2026-v2-candidate-20261127T010000Z"; +export const US_V2_RELEASE = "populace-us-2026"; +const STARTED_AT = "2026-11-27T01:00:00+00:00"; +const UPDATED_AT = "2026-11-27T05:00:00+00:00"; + +export const US_V2_BUILD_MANIFEST: JsonObject = { + schema_version: 1, + build_id: "populace-us-2026-v2-build-20261127T010000Z", + gates: { target_compilation: { status: "passed", dropped_target_names: [] } }, +}; + +export const US_V2_RELEASE_MANIFEST: JsonObject = { + schema_version: 1, + build: { + schema_version: 1, + build_id: "populace-us-2026-v2-build-20261127T010000Z", + built_at: "2026-11-27T05:00:00Z", + population_year: 2026, + }, + default_datasets: { national: "populace_us_2026" }, +}; + +/** A reform validation with one out-of-sample reform 5% under its JCT score. */ +export function usV2ReformValidation(budgetEffect = -95_000_000_000): JsonObject { + return { + schema_version: 1, + release_id: US_V2_CANDIDATE, + baseline_period: 2026, + scoring_window: "FY2025-2034", + reforms: [ + { + id: "obbba_salt_cap", + name: "SALT cap change", + category: "OBBBA", + jct: { score: -100_000_000_000, score_type: "conventional", source: "JCX-29-25" }, + microcosm: { budget_effect: budgetEffect }, + }, + ], + }; +} + +const IDENTITY = { + run_id: US_V2_RUN, + country_code: "US", + operation_id: "us_fiscal_refresh_build", + pipeline: { id: "us_fiscal_refresh", version: "2026.11" }, + candidate_id: US_V2_CANDIDATE, + release_id: US_V2_RELEASE, + run_kind: "release", + non_release: false, + started_at: STARTED_AT, + updated_at: UPDATED_AT, + status: "completed", + current_stage: "complete", +} as const; + +const DELIVERY = { + contract_version: 2, + enabled: true, + mode: "local_and_remote", + run_id: US_V2_RUN, + configured_repository: "policyengine/populace-us-staging", + upload_attempts: 3, + upload_successes: 3, + read_back: "passed", + last_error_code: null, + opt_out_reason: null, +} as const; + +function declared(logicalName: string, artifactKind: string, document: JsonObject, classification = "aggregate") { + return { + logical_name: logicalName, + artifact_kind: artifactKind, + contract_relative_path: `artifacts/${logicalName}.json`, + media_type: "application/json", + sha256: fakeDocumentSha256(document), + classification, + }; +} + +export function usV2Progress(): JsonObject { + return { + schema_name: "microcosm.staging.progress", + schema_version: 3, + ...IDENTITY, + sample: { mode: "full" }, + delivery: DELIVERY, + message: "Staging run completed.", + details: {}, + failure: null, + block: null, + }; +} + +/** + * The run manifest, declaring the run's diagnostics, its build and release + * manifests and, unless `reformValidation` is null, its reform validation. + */ +export function usV2RunManifest(options: { + diagnostics: JsonObject; + reformValidation?: JsonObject | null; +}): JsonObject { + const reformValidation = + options.reformValidation === undefined ? usV2ReformValidation() : options.reformValidation; + return { + schema_name: "microcosm.staging.run-manifest", + schema_version: 3, + ...IDENTITY, + sample: { mode: "full" }, + delivery: DELIVERY, + artifacts: [ + declared("build_manifest", "build_metadata", US_V2_BUILD_MANIFEST, "non_row_level"), + declared("calibration_diagnostics", "aggregate_diagnostics", options.diagnostics), + ...(reformValidation ? [declared("reform_validation", "validation_summary", reformValidation)] : []), + declared("release_manifest", "build_metadata", US_V2_RELEASE_MANIFEST, "non_row_level"), + ], + failure: null, + block: null, + paths: { + progress: `runs/${US_V2_RUN}/progress.json`, + events: `runs/${US_V2_RUN}/events.ndjson`, + calibration_progress: `runs/${US_V2_RUN}/calibration_progress.json`, + }, + }; +} + +export function usV2Events(): string { + return [ + { stage_id: "created", status: "started", timestamp: STARTED_AT, message: "Staging run created." }, + { stage_id: "calibration", status: "started", timestamp: "2026-11-27T02:00:00+00:00", message: null }, + { stage_id: "calibration", status: "completed", timestamp: "2026-11-27T04:00:00+00:00", message: null }, + { stage_id: "reform_validation", status: "completed", timestamp: "2026-11-27T04:30:00+00:00", message: null }, + { stage_id: "complete", status: "completed", timestamp: UPDATED_AT, message: "Staging run completed." }, + ] + .map((event, index) => + JSON.stringify({ + schema_name: "microcosm.staging.event", + schema_version: 3, + sequence: index + 1, + event_type: "stage", + run_id: US_V2_RUN, + details: {}, + ...event, + }), + ) + .join("\n"); +} + +export function usV2CalibrationProgress(): JsonObject { + const event = (timestamp: string, epoch: number, loss: number) => ({ + timestamp, + epoch, + epochs: 100, + phase: "calibration", + loss, + iteration: null, + budget_search: null, + budget_iteration: null, + budget_iters: null, + l0_lambda: null, + }); + return { + schema_name: "microcosm.staging.calibration-progress", + schema_version: 3, + run_id: US_V2_RUN, + candidate_id: US_V2_CANDIDATE, + updated_at: "2026-11-27T04:00:00+00:00", + events: [ + event("2026-11-27T02:30:00+00:00", 10, 0.5), + event("2026-11-27T03:30:00+00:00", 100, 0.0125), + ], + }; +}