#!/usr/bin/env python3
"""Zero-CAIN-import cleanroom verifier for Phase 3 Frontier AI Research evidence bundle.

Proves:
1. AgentPRM trajectory hash integrity.
2. GovernedMemoryShield injection sanitization digest.
3. TriCEGAR behavioral MDP trace verification hash.
4. A2AGovernance delegation attestation receipt.
"""

import hashlib
import json
from pathlib import Path


def main():
    root = Path(__file__).resolve().parent
    print("=" * 60)
    print("CAIN Phase 3 Frontier AI Research Evidence Clean-Room Verifier")
    print("=" * 60)

    # 1. Verify PRM evaluation sample
    prm_file = root / "PRM_EVALUATION_SAMPLE.json"
    prm_data = json.loads(prm_file.read_text())
    expected_prm_hash = hashlib.sha256(
        json.dumps({
            "trajectory_id": prm_data["trajectory_id"],
            "overall": prm_data["overall_utility"],
            "min": prm_data["min_step_utility"],
            "verdict": prm_data["verdict"],
            "anomalous": prm_data["anomalous_steps"],
        }, sort_keys=True).encode()
    ).hexdigest()
    assert prm_data["evaluation_hash"] == expected_prm_hash, "PRM hash mismatch!"
    print(f"[✓] AgentPRM Trajectory Hash: VALID ({prm_data['evaluation_hash'][:16]}...)")

    # 2. Verify TriCEGAR MDP sample
    tri_file = root / "TRICEGAR_VERIFICATION_SAMPLE.json"
    tri_data = json.loads(tri_file.read_text())
    expected_tri_hash = hashlib.sha256(
        json.dumps({
            "trace_id": tri_data["trace_id"],
            "verified": tri_data["verified"],
            "states": tri_data["states_synthesized"],
            "loop": tri_data["loop_detected"],
            "depth_exceeded": tri_data["max_depth_exceeded"],
        }, sort_keys=True).encode()
    ).hexdigest()
    assert tri_data["verification_hash"] == expected_tri_hash, "TriCEGAR hash mismatch!"
    print(f"[✓] TriCEGAR MDP Verification Hash: VALID ({tri_data['verification_hash'][:16]}...)")

    # 3. Verify Memory Shield sample
    mem_file = root / "MEMORY_SHIELD_SAMPLE.json"
    mem_data = json.loads(mem_file.read_text())
    expected_mem_hash = hashlib.sha256(
        f"{mem_data['source_session']}:{mem_data['raw_text']}:{mem_data['poisoning_risk_score']}".encode()
    ).hexdigest()
    assert mem_data["evidence_digest"] == expected_mem_hash, "Memory shield digest mismatch!"
    print(f"[✓] GovernedMemoryShield Digest: VALID ({mem_data['evidence_digest'][:16]}...)")

    # 4. Verify A2A Delegation receipt
    a2a_file = root / "A2A_DELEGATION_SAMPLE.json"
    a2a_data = json.loads(a2a_file.read_text())
    expected_a2a_receipt = hashlib.sha256(
        f"{a2a_data['delegator_id']}->{a2a_data['delegatee_id']}:{a2a_data['task']}:{a2a_data['attenuated_capabilities']}".encode()
    ).hexdigest()
    assert a2a_data["attestation_receipt"] == expected_a2a_receipt, "A2A receipt mismatch!"
    print(f"[✓] A2A Delegation Receipt: VALID ({a2a_data['attestation_receipt'][:16]}...)")

    print("\n" + "=" * 60)
    print("ALL 4/4 PHASE 3 FRONTIER EVIDENCE PROOFS VERIFIED CLEAN-ROOM (PASS)")
    print("=" * 60)


if __name__ == "__main__":
    main()
