#!/usr/bin/env python3
from __future__ import annotations
import json
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
PROOF = ROOT / "data" / "rsi-skill-compounding-moat-proof.json"
def fail(msg: str) -> None:
    print(f"ERROR: {msg}"); raise SystemExit(1)
def main() -> None:
    if not PROOF.exists(): fail(f"Missing proof receipt: {PROOF}")
    proof=json.loads(PROOF.read_text(encoding="utf-8"))
    for key in ["proved","agent_system","benchmark_public","pre_registered_gates","baselines_and_controls","final","comparisons","bootstrap_confidence_intervals","rsi_releases","protocol_fingerprint_sha256"]:
        if key not in proof: fail(f"Missing key: {key}")
    if not proof["proved"]: fail("Proof receipt says proved=false")
    failed=[k for k,v in proof["pre_registered_gates"].items() if not v]
    if failed: fail(f"Pre-registered gates failed: {failed}")
    f=proof["final"]
    checks=[(proof["agent_system"]["virtual_specialist_agents"]>=250_000_000,"virtual specialist agents must be at least 250M"),(proof["agent_system"]["specialist_roles"]>=8_000_000,"specialist roles must be at least 8M"),(proof["benchmark_public"]["locked_holdout_count"]>=2048,"locked holdout must be at least 2048"),(proof["rsi_release_count"]>=12,"must have at least 12 released RSI updates"),(f["value_capture_rate_percent"]>=96,"value capture must be at least 96%"),(f["minimum_domain_value_capture_percent"]>=94,"minimum domain capture must be at least 94%"),(f["weak_domain_rate_percent"]==0,"weak-domain rate must be zero"),(f["risk_breach_rate_percent"]==0,"risk breach must be zero"),(f["unauthorized_action_rate_percent"]==0,"unauthorized action must be zero"),(proof["bootstrap_confidence_intervals"]["vs_strongest_control"]["p05_gain_points"]>0,"bootstrap p05 gain over strongest control must be positive")]
    failures=[msg for ok,msg in checks if not ok]
    if failures:
        for msg in failures: print(f"- {msg}")
        raise SystemExit(1)
    print(json.dumps({"status":"PASSED","proof":str(PROOF.relative_to(ROOT)),"value_capture_percent":f["value_capture_rate_percent"],"minimum_domain_capture_percent":f["minimum_domain_value_capture_percent"],"rsi_releases":proof["rsi_release_count"]}, indent=2, sort_keys=True))
if __name__=="__main__": main()
