#!/usr/bin/env python3 """Materialize exact registered configs and propagate the immutable science-spec hash.""" from __future__ import annotations import json import re import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src")) from repro_control.configs import config_sha256, registered_config_for from repro_control.hashing import atomic_write_json, sha256_file SUDOKU_REVISION = "4d5aa527a9fb9aacca0b0d5b8b77d569fa9afcaa" TRACKIO_WHEEL_SHA256 = "277340507ac46c02c06900c1d680129bdb528223c8110b0b6bc9326bb9f0891d" BASE_IMAGE = ( "python:3.12-slim@" "sha256:cab2dbf575e971934a81e4622f5aba17aa7929719bd7e31033a3a83b97fd0464" ) IMAGE_SPACE_ID = "Mindcraft/sheaf-admm-icml2026-executor" def _replace_spec_hash(path: Path, old: str, new: str) -> None: text = path.read_text() if old not in text: raise RuntimeError(f"old science-spec hash not present in {path}") path.write_text(text.replace(old, new)) def main() -> int: root = Path(__file__).resolve().parents[1] spec_path = root / "SCIENCE-SPEC.yaml" old_hash = sha256_file(spec_path) spec = json.loads(spec_path.read_text()) if spec.get("outcomes") != {} or len(spec.get("run_identities", [])) != 15: raise RuntimeError("science spec is not in the pre-outcome fifteen-run state") spec["authorities"].update( { "sudoku_dataset_revision": SUDOKU_REVISION, "dependency_lock_sha256": sha256_file(root / "uv.lock"), "trackio_wheel_sha256": TRACKIO_WHEEL_SHA256, "base_image": BASE_IMAGE, "execution_image_space": IMAGE_SPACE_ID, } ) spec["data_rules"]["sudoku"]["revision"] = SUDOKU_REVISION registered = {} config_root = root / "control" / "registered-configs" for logical_id in spec["run_identities"]: config = registered_config_for(root, logical_id) digest = config_sha256(config) atomic_write_json(config_root / f"{logical_id}.json", config) if sha256_file(config_root / f"{logical_id}.json") == digest: raise RuntimeError("file-byte hash must remain distinct from canonical config hash") registered[logical_id] = { "canonical_sha256": digest, "file": f"control/registered-configs/{logical_id}.json", "config": config, } spec["registered_configs"] = registered atomic_write_json(spec_path, spec) new_hash = sha256_file(spec_path) if new_hash == old_hash: raise RuntimeError("science spec did not change") constants_path = root / "src" / "repro_control" / "constants.py" constants = constants_path.read_text() constants = re.sub( r'SCIENCE_SPEC_SHA256 = "[0-9a-f]{64}"', f'SCIENCE_SPEC_SHA256 = "{new_hash}"', constants, count=1, ) constants_path.write_text(constants) _replace_spec_hash(root / "Dockerfile", old_hash, new_hash) image_contract_path = root / "IMAGE-SOURCE-CONTRACT.json" image_contract = json.loads(image_contract_path.read_text()) image_contract.update( base_image_digest=BASE_IMAGE.rsplit("@", 1)[1], science_spec_sha256=new_hash, ) atomic_write_json(image_contract_path, image_contract) freeze_path = root / "control" / "SCIENCE-FREEZE.template.yaml" freeze = json.loads(freeze_path.read_text()) freeze["science_spec_sha256"] = new_hash atomic_write_json(freeze_path, freeze) inventory_path = root / "PUBLICATION-INVENTORY.json" inventory = json.loads(inventory_path.read_text()) image_rows = [ row for row in inventory["entries"] if row["asset_or_surface_type"] == "execution_image" ] if len(image_rows) != 1: raise RuntimeError("publication inventory must have exactly one execution image row") image_rows[0]["full_url_or_expected_repo"] = ( f"https://huggingface.co/spaces/{IMAGE_SPACE_ID}" ) atomic_write_json(inventory_path, inventory) status_path = root / "LOCAL-IMPLEMENTATION-STATUS.md" if status_path.exists() and old_hash in status_path.read_text(): _replace_spec_hash(status_path, old_hash, new_hash) print( json.dumps( { "old_science_spec_sha256": old_hash, "science_spec_sha256": new_hash, "registered_config_count": len(registered), "sudoku_revision": SUDOKU_REVISION, }, sort_keys=True, ) ) return 0 if __name__ == "__main__": raise SystemExit(main())