#!/usr/bin/env python3 """Fail-closed local immutable publication of the already-tested standing candidate.""" import argparse import ctypes import json import os import shutil import tarfile import tempfile import traceback from pathlib import Path from runtime_common import write_json from standing_acceptance import audit, require from verify_frozen_standing import digest, verify NAME = "go2_fl_loaded_stand-1.0.0" def rename_no_replace(source, target): # Linux atomic publication, including protection against non-cooperating target races. libc = ctypes.CDLL(None, use_errno=True) function = libc.renameat2 function.argtypes = [ctypes.c_int, ctypes.c_char_p, ctypes.c_int, ctypes.c_char_p, ctypes.c_uint] function.restype = ctypes.c_int if function(-100, os.fsencode(source), -100, os.fsencode(target), 1) != 0: code = ctypes.get_errno() raise OSError(code, os.strerror(code), str(target)) def cleanup_stage(path): if path and path.exists(): for p in path.rglob("*"): p.chmod(0o700 if p.is_dir() else 0o600) path.chmod(0o700) shutil.rmtree(path) def inode(path): stat = path.stat() return stat.st_dev, stat.st_ino def quarantine_owned(releases, owned): """Preserve rejected bytes, but remove only this invocation's official artifacts.""" quarantine = None for path, identity in owned.items(): if path.exists() and inode(path) == identity: if quarantine is None: quarantine = Path(tempfile.mkdtemp(prefix=f".{NAME}.rejected-", dir=releases)) write_json(quarantine / "REJECTED.json", {"accepted_skill": False, "status": "rejected"}) # Moving a directory between parents updates its '..' link on Linux. # Temporarily grant owner-write on this owned root only, not its contents. mode = path.stat().st_mode & 0o777 if path.is_dir(): path.chmod(mode | 0o200) try: rename_no_replace(path, quarantine / path.name) finally: remaining = quarantine / path.name if (quarantine / path.name).exists() else path remaining.chmod(mode) return str(quarantine) if quarantine else None def copy_pinned_waiver(source, target, expected_sha256): shutil.copyfile(source, target) require(digest(target) == expected_sha256, "Staged waiver differs from approved bytes") def publish(run, releases, python, *, waiver_path=None, waiver_sha256=None, engineering_path=None): package = run / "candidate" target = releases / NAME archive = releases / f"{NAME}.tar.gz" output = releases / f"{NAME}.acceptance.json" releases.mkdir(parents=True, exist_ok=True) require(not any(p.exists() for p in (target, archive, output)), "Immutable version already exists") lock = releases / f".{NAME}.publish.lock" descriptor = os.open(lock, os.O_CREAT | os.O_EXCL | os.O_WRONLY, 0o600) os.close(descriptor) # Recheck while holding the lock, outside any handler that writes acceptance.json. try: require(not any(p.exists() for p in (target, archive, output)), "Immutable version already exists") except BaseException: lock.unlink(missing_ok=True) raise stage, temporary_archive = None, None owned = {} try: write_json(output, {"passed": False, "status": "running", "accepted_skill": False}) # This re-opens and reduces all raw physics evidence; no stale passed-only shortcut. gate_path = run / ("publication_gate_with_deviation.json" if waiver_path else "publication_gate.json") options = {} if waiver_path is not None or waiver_sha256 is not None: options.update(waiver_path=waiver_path, waiver_sha256=waiver_sha256) if engineering_path is not None: options["engineering_path"] = engineering_path evidence = audit(run, gate_path, **options) require( evidence["passed"] and evidence["stage"] == "eligible_for_frozen_publication", "Acceptance not eligible" ) stage = Path(tempfile.mkdtemp(prefix=f".{NAME}.stage-", dir=releases)) shutil.copytree(package, stage, dirs_exist_ok=True, ignore=shutil.ignore_patterns("__pycache__")) old = json.loads((stage / "manifest.json").read_text()) require(digest(stage / "manifest.json") == evidence["candidate_manifest_sha256"], "Untested candidate identity") for path, value in evidence["core_files"].items(): require(digest(stage / path) == value, "Changed tested core") (stage / "evidence").mkdir() shutil.copyfile(package / "manifest.json", stage / "evidence/tested_candidate_manifest.json") shutil.copyfile(gate_path, stage / "evidence/acceptance_gate.json") engineering_file = Path(evidence.get("engineering_path", run / "engineering.json")) shutil.copyfile(engineering_file, stage / "evidence/engineering.json") for name in ("independent_candidate.json", "candidate_lock.json"): shutil.copyfile(run / name, stage / "evidence" / name) if evidence.get("waiver") is not None: copy_pinned_waiver(waiver_path, stage / "evidence/operator_waiver.json", evidence["waiver_sha256"]) (stage / "DEVIATIONS.md").write_text( "# Accepted with an operator-approved deviation\n\n" "The original120s stability foot-drift gate FAILED: env6=30.620924mm, env19=30.782583mm " "versus30mm. Raw results and the original threshold are unchanged. " "The operator accepted this exact candidate and recorded trial only; " "see evidence/operator_waiver.json and manifest waiver_sha256. " "This is NOT a general31mm threshold, a new physics pass, or a hardware safety approval. " "No other failed gate is waived; no new weights or trial may reuse this waiver. " "The archive and final directory are independently loaded after eligibility checks.\n" ) for name in ("full", "holdout", "hold", "stability", "exploratory"): shutil.copyfile(run / name / "result.json", stage / "evidence" / f"{name}.json") shutil.copyfile(run / f"{name}_process.json", stage / "evidence" / f"{name}_process.json") for key in ("pytest_log", "ruff_log", "review_report"): path = Path(json.loads(engineering_file.read_text())[key]) shutil.copyfile(path, stage / "evidence" / path.name) old.update( stage="accepted_with_deviation" if evidence.get("waiver") is not None else "accepted", accepted_skill=True, acceptance_basis=evidence.get("acceptance_basis", "all_required_physics_gates"), unwaived_required_physics_passed=evidence.get("unwaived_required_physics_passed", True), waiver_sha256=evidence.get("waiver_sha256"), waiver_receipts=evidence.get("waiver_receipts", []), tested_candidate_manifest_sha256=evidence["candidate_manifest_sha256"], tested_core_files=evidence["core_files"], files={ str(p.relative_to(stage)): digest(p) for p in sorted(stage.rglob("*")) if p.is_file() and p.name != "manifest.json" }, ) write_json(stage / "manifest.json", old) staged = verify(stage, python, releases / f"{NAME}.staging-load.json") require(staged["passed"], "Staged package independent load failed") # Catch evidence/core changes during the independent subprocess before committing. for path, value in evidence["artifacts"].items(): require(digest(path) == value, "Evidence changed during publication") require(digest(package / "manifest.json") == evidence["candidate_manifest_sha256"], "Candidate changed") for path, value in old["files"].items(): require(digest(stage / path) == value, "Staged package changed") final_hash = digest(stage / "manifest.json") for p in stage.rglob("*"): p.chmod(0o555 if p.is_dir() else 0o444) stage.chmod(0o555) owned[target] = inode(stage) rename_no_replace(stage, target) stage = None independent = verify(target, python, releases / f"{NAME}.independent-load.json") require(independent["passed"] and independent["manifest_sha256"] == final_hash, "Final independent load failed") with tempfile.NamedTemporaryFile(prefix=f".{NAME}.", suffix=".tar.gz", dir=releases, delete=False) as file: temporary_archive = Path(file.name) with tarfile.open(temporary_archive, "w:gz") as bundle: bundle.add( target, arcname=NAME, filter=lambda info: None if "__pycache__" in Path(info.name).parts else info ) with tempfile.TemporaryDirectory(prefix="standing-archive-") as directory: unpacked = Path(directory) with tarfile.open(temporary_archive, "r:gz") as bundle: bundle.extractall(unpacked, filter="data") restored = verify(unpacked / NAME, python, releases / f"{NAME}.archive-load.json") require(restored["passed"] and restored["manifest_sha256"] == final_hash, "Archive independent load failed") for p in unpacked.rglob("*"): p.chmod(0o700 if p.is_dir() else 0o600) owned[archive] = inode(temporary_archive) rename_no_replace(temporary_archive, archive) temporary_archive = None archive.chmod(0o444) summary = { "passed": True, "status": "completed", "accepted_skill": True, "acceptance_basis": evidence.get("acceptance_basis", "all_required_physics_gates"), "unwaived_required_physics_passed": evidence.get("unwaived_required_physics_passed", True), "waiver_sha256": evidence.get("waiver_sha256"), "waiver_receipts": evidence.get("waiver_receipts", []), "waiver": evidence.get("waiver"), "version": "1.0.0", "scope": "guided single Go2 FL, PGS, synthetic3kg payload, single-axis validated loads; not hardware", "manifest_sha256": final_hash, "candidate_manifest_sha256": evidence["candidate_manifest_sha256"], "archive_sha256": digest(archive), "core_files": evidence["core_files"], "trials": evidence["trials"], "holdout_improvement_fraction": evidence["holdout_improvement_fraction"], "independent_candidate": True, "independent_staged": True, "independent_final": True, "independent_archive": True, "raw_trace_reduction": True, "training": evidence["training"], } write_json(output, summary) return summary except BaseException: error = traceback.format_exc() quarantine = quarantine_owned(releases, owned) write_json( output, { "passed": False, "status": "failed", "accepted_skill": False, "error": error, "quarantine": quarantine, "published_directory_exists": target.exists(), "published_archive_exists": archive.exists(), }, ) raise finally: cleanup_stage(stage) if temporary_archive: temporary_archive.unlink(missing_ok=True) lock.unlink(missing_ok=True) def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--run", type=Path, required=True) parser.add_argument("--releases", type=Path, default=Path("releases")) parser.add_argument("--python", type=Path, required=True) parser.add_argument("--waiver", type=Path) parser.add_argument("--waiver-sha256") parser.add_argument("--engineering", type=Path) args = parser.parse_args() print( json.dumps( publish( args.run, args.releases, args.python, waiver_path=args.waiver, waiver_sha256=args.waiver_sha256, engineering_path=args.engineering, ), indent=2, ) ) if __name__ == "__main__": main()