import { mkdir, readFile, writeFile } from "node:fs/promises"; import path from "node:path"; import { registrySnapshotDigest } from "./lib/registry-compatibility.ts"; const inputPath = process.argv[2] ?? "artifacts/quality/registry-current-snapshot.json"; const outputPath = process.argv[3] ?? "config/contracts/registry-baseline.json"; const evidencePath = "config/contracts/registry-change-evidence.json"; const owner = process.env.REGISTRY_BASELINE_OWNER; const reason = process.env.REGISTRY_BASELINE_REASON; if (!owner || !reason) { process.stderr.write( "REGISTRY_BASELINE_OWNER and REGISTRY_BASELINE_REASON are required.\n", ); process.exit(1); } const input = JSON.parse(await readFile(inputPath, "utf8")); // Read before anything is written, so a missing file changes nothing. const evidence = JSON.parse(await readFile(evidencePath, "utf8")); const snapshot = input.registries ? { schemaVersion: 2, registries: input.registries } : input; const digest = registrySnapshotDigest(snapshot); await mkdir(path.dirname(outputPath), { recursive: true }); await writeFile(outputPath, `${JSON.stringify(snapshot, null, 2)}\n`); await writeFile( "config/contracts/registry-baseline.approval.json", `${JSON.stringify( { schemaVersion: 1, snapshotDigest: digest, owner, reason, approvedAt: new Date().toISOString(), }, null, 2, )}\n`, ); // 0929/07-01 (3). The new baseline is the current snapshot, so it explains // every change an evidence row described. A row left behind would match no // change and fail check:registries. Git history keeps the deleted rows. const deleted = (evidence.changes ?? []).map( (entry: { changeId?: unknown }) => String(entry.changeId), ); await writeFile( evidencePath, `${JSON.stringify({ ...evidence, changes: [] }, null, 2)}\n`, ); process.stdout.write(`Registry baseline updated: ${digest}\n`); if (deleted.length > 0) { process.stdout.write( `Deleted evidence the new baseline explains:\n${deleted.join("\n")}\n`, ); }