Artifex/validate_historical_oracle_acceptance.py
2026-08-18 20:45:09 +07:00

75 lines
2.9 KiB
Python

#!/usr/bin/env python3
"""Validate a completed 711-feature historical-oracle run without modifying it."""
from __future__ import annotations
import argparse
import hashlib
import json
import os
from pathlib import Path
from typing import Any
import numpy as np
def canonical(value: object) -> bytes:
return json.dumps(value, sort_keys=True, separators=(",", ":"), allow_nan=False).encode("utf-8")
def array_digest(values: np.ndarray) -> str:
values = np.ascontiguousarray(values)
return hashlib.sha256(
canonical({"dtype": values.dtype.str, "shape": values.shape}) + b"\0" + values.tobytes()
).hexdigest()
def validate(output_dir: Path) -> dict[str, Any]:
status_path = output_dir / "final_status.json"
status = json.loads(status_path.read_text(encoding="utf-8"))
checkpoints = status.get("checkpoints")
if status.get("artifact") != "HISTORICAL_FEATURE_ORACLE_FULL_STATUS_V1":
raise ValueError("invalid full-status artifact")
if status.get("status") != "PASS" or status.get("completed_features") != 711:
raise ValueError("full-status artifact does not attest to 711 passing features")
if not isinstance(checkpoints, list) or len(checkpoints) != 711:
raise ValueError("full-status artifact must contain exactly 711 checkpoints")
request_ids = set()
for checkpoint in checkpoints:
request_id = checkpoint.get("request_id")
if not isinstance(request_id, str) or request_id in request_ids:
raise ValueError("checkpoint request IDs must be unique strings")
request_ids.add(request_id)
path = output_dir / "checkpoints" / str(checkpoint.get("path", ""))
if path.name != f"{request_id}.npy" or not path.is_file():
raise ValueError(f"missing checkpoint: {request_id}")
values = np.load(path, allow_pickle=False)
if (
values.dtype.str != checkpoint.get("dtype")
or list(values.shape) != checkpoint.get("shape")
):
raise ValueError(f"checkpoint dtype or shape mismatch: {request_id}")
if array_digest(values) != checkpoint.get("sha256"):
raise ValueError(f"checkpoint semantic digest mismatch: {request_id}")
return {
"artifact": "HISTORICAL_FEATURE_ORACLE_ACCEPTANCE_VALIDATION_V1",
"status": "PASS",
"completed_features": len(checkpoints),
"status_sha256": hashlib.sha256(status_path.read_bytes()).hexdigest(),
}
def main() -> None:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--output-dir", type=Path, required=True)
parser.add_argument("--manifest", type=Path)
args = parser.parse_args()
manifest = validate(args.output_dir)
if args.manifest:
args.manifest.write_bytes(canonical(manifest) + b"\n")
os.chmod(args.manifest, 0o444)
print(canonical(manifest).decode("utf-8"))
if __name__ == "__main__":
main()