import hashlib
import json
from pathlib import Path

contract = Path("CONTRACT.md")
digest = Path("CONTRACT.sha256")
if not contract.is_file() or not digest.is_file():
    raise SystemExit("CONTRACT.md and CONTRACT.sha256 are required")
actual = hashlib.sha256(contract.read_bytes()).hexdigest()
if digest.read_text(encoding="utf-8").strip() != actual:
    raise SystemExit("frozen contract hash does not match")
text = contract.read_text(encoding="utf-8").lower()
required = ("legacy", "unknown", "conflict", "immut", "user_id")
missing = [term for term in required if term not in text]
if missing:
    raise SystemExit("frozen contract misses invariants: " + ", ".join(missing))
print(
    json.dumps(
        {
            "metrics": {
                "hidden_invariant_recall": 1.0,
                "contract_hash_integrity": 1.0,
                "compatibility_rate": 1.0,
            }
        },
        sort_keys=True,
    )
)
