From 7f889911299db346ab260c6081ffe4867402f268 Mon Sep 17 00:00:00 2001 From: Anthony Volk <14987227+anth-volk@users.noreply.github.com> Date: Thu, 26 Nov 2026 23:48:29 +0400 Subject: [PATCH 1/6] Test a US version 2 staging run's list entry, detail, calibration and reform validation A US fiscal refresh build's staging run as the version 2 writer stages it (PolicyEngine/microcosm#1135): version 3 documents and result files under artifacts/, each declared in the run manifest with its SHA-256. The list entry, detail and calibration tests pass already; the two reform validation tests fail: the dashboard reads reform_validation.json only at the run folder's top level, so a version 2 run shows none from artifacts/reform_validation.json and shows a top-level copy it never wrote. Co-Authored-By: Claude Opus 5.5 --- .../us-version-2-staging-run.test.ts | 285 ++++++++++++++++++ .../test-support/fake-collector-store.ts | 13 +- .../test-support/us-version-2-staging-run.ts | 184 +++++++++++ 3 files changed, 479 insertions(+), 3 deletions(-) create mode 100644 frontend/app/api/microcosm/us-version-2-staging-run.test.ts create mode 100644 frontend/lib/microcosm/test-support/us-version-2-staging-run.ts 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..141353f3 --- /dev/null +++ b/frontend/app/api/microcosm/us-version-2-staging-run.test.ts @@ -0,0 +1,285 @@ +// 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 } 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 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/test-support/fake-collector-store.ts b/frontend/lib/microcosm/test-support/fake-collector-store.ts index 3870264d..a607582e 100644 --- a/frontend/lib/microcosm/test-support/fake-collector-store.ts +++ b/frontend/lib/microcosm/test-support/fake-collector-store.ts @@ -96,10 +96,17 @@ 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; } @@ -188,14 +195,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, 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..6bd3dc2e --- /dev/null +++ b/frontend/lib/microcosm/test-support/us-version-2-staging-run.ts @@ -0,0 +1,184 @@ +// Test support: a US fiscal refresh build's staging run as 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. 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), + ], + }; +} From 1740603be78ab932c20e075d0eb9b8641461007d Mon Sep 17 00:00:00 2001 From: Anthony Volk <14987227+anth-volk@users.noreply.github.com> Date: Thu, 26 Nov 2026 23:50:01 +0400 Subject: [PATCH 2/6] Show a version 2 staging run's declared reform validation A version 2 or 3 staging run writes its reform validation as artifacts/reform_validation.json and declares it, with its SHA-256, in its run manifest (PolicyEngine/microcosm#1135). The Candidate tab now reads that copy for such a run, and shows it only when the run manifest declares a reform_validation artifact with the copy's digest. A version 1 run's reform validation is still its top-level reform_validation.json, which a version 2 or 3 run never reads. Co-Authored-By: Claude Opus 5.5 --- docs/calibration-tree-artifacts.md | 5 ++- frontend/lib/microcosm/stored-staging.ts | 51 ++++++++++++++++++++++-- 2 files changed, 51 insertions(+), 5 deletions(-) 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/lib/microcosm/stored-staging.ts b/frontend/lib/microcosm/stored-staging.ts index e5c7287a..79065143 100644 --- a/frontend/lib/microcosm/stored-staging.ts +++ b/frontend/lib/microcosm/stored-staging.ts @@ -52,6 +52,7 @@ import { readStagingRunFilesIndex, stagingJsonObjectOrNull, type StagingRunFileName, + type StagingRunFileRecord, type StagingRunFilesIndex, type StagingRunRecord, } from "@policyengine/calibration-tree/staging-run-files"; @@ -131,8 +132,19 @@ 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 its path in +// the run folder, as its run manifest declares it. Version 1 runs write +// `reform_validation.json` at the folder's top level. +const DECLARED_REFORM_VALIDATION = "artifacts/reform_validation.json"; + +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 +171,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); @@ -209,11 +231,32 @@ export async function copiedRunDocuments( runManifest, calibrationProgress, events, - reformValidation: stagingJsonObjectOrNull(reformBytes), + reformValidation: + !structured || declaresCopy(runManifest, "reform_validation", copiedFile(record, reformValidationName)) + ? 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) === `artifacts/${logicalName}.json` && + stringValue(artifact?.sha256) === copy.sha256 + ); +} + async function readRunDocuments( country: MicrocosmCountry, runId: string, From f433b9adf764d56785d19b559b917dd85cd862fa Mon Sep 17 00:00:00 2001 From: Anthony Volk <14987227+anth-volk@users.noreply.github.com> Date: Fri, 27 Nov 2026 00:08:39 +0400 Subject: [PATCH 3/6] Test a version 2 run without a run manifest copy, and the shorter reads From review round 1: a version 2 run whose copies hold progress but no run manifest shows no reform validation, and the calibration and Build progress readers, which read shorter document lists, never read the run's reform validation copy. Both pass on the change; they add coverage. Co-Authored-By: Claude Opus 5.5 --- .../us-version-2-staging-run.test.ts | 30 ++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) 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 index 141353f3..534db4ec 100644 --- a/frontend/app/api/microcosm/us-version-2-staging-run.test.ts +++ b/frontend/app/api/microcosm/us-version-2-staging-run.test.ts @@ -6,7 +6,7 @@ import { afterEach, beforeEach, expect, test } from "bun:test"; import diagnosticsFixture from "@/lib/microcosm/fixtures/zz-release/calibration_diagnostics.json"; -import { clearBuildRunCaches } from "@/lib/microcosm/build-runs"; +import { clearBuildRunCaches, loadBuildRuns } from "@/lib/microcosm/build-runs"; import { FakeCollectorStore, type FakeStagingRun, @@ -252,6 +252,34 @@ test("a version 2 run shows no reform validation other than the one its run mani } }); +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("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"; From f261b36daff43d5a245b7656669a1ede8c04a347 Mon Sep 17 00:00:00 2001 From: Anthony Volk <14987227+anth-volk@users.noreply.github.com> Date: Fri, 27 Nov 2026 00:08:39 +0400 Subject: [PATCH 4/6] Name the declared reform validation path once From review round 1: the artifacts/ path of the run's reform validation is built from its logical name in one place, and the reader's comment says a version 2 or 3 run's declared copy is what the package's 0.1.5 Hugging Face reader does not read. Co-Authored-By: Claude Opus 5.5 --- frontend/lib/microcosm/stored-staging.ts | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/frontend/lib/microcosm/stored-staging.ts b/frontend/lib/microcosm/stored-staging.ts index 79065143..f5c5317f 100644 --- a/frontend/lib/microcosm/stored-staging.ts +++ b/frontend/lib/microcosm/stored-staging.ts @@ -132,10 +132,16 @@ export interface CopiedRunDocuments { stagedDatasetReceipt: JsonObject | null; } -// Where a version 2 or 3 run's reform validation is copied: under its path in -// the run folder, as its run manifest declares it. Version 1 runs write -// `reform_validation.json` at the folder's top level. -const DECLARED_REFORM_VALIDATION = "artifacts/reform_validation.json"; +// 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; @@ -193,6 +199,9 @@ const CALIBRATION_DOCUMENTS: readonly StagingRunFileName[] = [ /** * One run's copied documents, parsed and checked as the Hugging Face reader * parses and checks them. `names` limits what is read; the rest is absent. + * 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. */ export async function copiedRunDocuments( record: StagingRunRecord, @@ -232,7 +241,8 @@ export async function copiedRunDocuments( calibrationProgress, events, reformValidation: - !structured || declaresCopy(runManifest, "reform_validation", copiedFile(record, reformValidationName)) + !structured || + declaresCopy(runManifest, REFORM_VALIDATION_ARTIFACT, copiedFile(record, reformValidationName)) ? stagingJsonObjectOrNull(reformBytes) : null, stagedDatasetReceipt: stagingJsonObjectOrNull(receiptBytes), @@ -252,7 +262,7 @@ function declaresCopy( if (!copy || !isStructuredStagingDocument(runManifest)) return false; const artifact = objectOrNull(objectOrNull(runManifest?.artifacts)?.[logicalName]); return ( - stringValue(artifact?.path) === `artifacts/${logicalName}.json` && + stringValue(artifact?.path) === declaredArtifactPath(logicalName) && stringValue(artifact?.sha256) === copy.sha256 ); } From b511888967ab806b4897991c407a7bdc837a2606 Mon Sep 17 00:00:00 2001 From: Anthony Volk <14987227+anth-volk@users.noreply.github.com> Date: Fri, 27 Nov 2026 00:25:29 +0400 Subject: [PATCH 5/6] Test a version 3 run whose index summary records no version From review round 2: the collector records a run whose run manifest copy isn't a JSON object as a problem with no version in its summary, while its progress and events are still version 3. The reader takes the version from the summary, so it shows such a run's top-level reform_validation.json, which no version 2 or 3 run writes. The test fails on that assertion. The fake store can now set summary fields apart from the run's documents. Co-Authored-By: Claude Opus 5.5 --- .../us-version-2-staging-run.test.ts | 22 +++++++++++++++++++ .../test-support/fake-collector-store.ts | 5 ++++- 2 files changed, 26 insertions(+), 1 deletion(-) 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 index 534db4ec..1fae5245 100644 --- a/frontend/app/api/microcosm/us-version-2-staging-run.test.ts +++ b/frontend/app/api/microcosm/us-version-2-staging-run.test.ts @@ -263,6 +263,28 @@ test("a version 2 run without a copied run manifest shows no reform validation", 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< diff --git a/frontend/lib/microcosm/test-support/fake-collector-store.ts b/frontend/lib/microcosm/test-support/fake-collector-store.ts index a607582e..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"; @@ -109,6 +110,8 @@ export interface FakeStagingRun { 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 { @@ -229,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, From 2ccc14fedcc81ab7b877e526114c8cb12f4c765c Mon Sep 17 00:00:00 2001 From: Anthony Volk <14987227+anth-volk@users.noreply.github.com> Date: Fri, 27 Nov 2026 00:25:47 +0400 Subject: [PATCH 6/6] Take the run's version from its documents as well as its summary From review round 2: a run whose index summary records no version but whose documents are version 2 or 3 shows no top-level reform_validation.json, which no version 2 or 3 run writes. The module header now says where it differs from the package's 0.1.5 Hugging Face reader, and the US version 2 fixture's header says what it leaves out of what the writer stages. Co-Authored-By: Claude Opus 5.5 --- frontend/lib/microcosm/stored-staging.ts | 21 +++++++++++++------ .../test-support/us-version-2-staging-run.ts | 14 +++++++------ 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/frontend/lib/microcosm/stored-staging.ts b/frontend/lib/microcosm/stored-staging.ts index f5c5317f..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"; @@ -228,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, @@ -240,11 +247,13 @@ export async function copiedRunDocuments( runManifest, calibrationProgress, events, - reformValidation: - !structured || - declaresCopy(runManifest, REFORM_VALIDATION_ARTIFACT, copiedFile(record, reformValidationName)) - ? stagingJsonObjectOrNull(reformBytes) - : null, + reformValidation: ( + structured + ? declaresCopy(runManifest, REFORM_VALIDATION_ARTIFACT, copiedFile(record, reformValidationName)) + : !structuredDocuments + ) + ? stagingJsonObjectOrNull(reformBytes) + : null, stagedDatasetReceipt: stagingJsonObjectOrNull(receiptBytes), }; } 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 index 6bd3dc2e..711a682b 100644 --- a/frontend/lib/microcosm/test-support/us-version-2-staging-run.ts +++ b/frontend/lib/microcosm/test-support/us-version-2-staging-run.ts @@ -1,9 +1,11 @@ -// Test support: a US fiscal refresh build's staging run as 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. The digests are of each document's JSON text, as the fake -// collector store records them. +// 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;