"""q06_freeze.py: record the sha256 of the pre-registration files at the Phase 3 stop.

Run after the review round and immediately before delivery. The Phase 3 check asserts that the files on disk
still carry these hashes, and the Phase 4 check asserts the same before it computes anything; a change after
this point is a deviation that goes to the deviations log with its reason.
"""
import datetime, hashlib, sys

sys.path.insert(0, "/home/claude/mill/code")
from vals import Values

P3 = "/home/claude/mill/phase3"
V = Values(path=f"{P3}/outputs/values3.json", script="q06_freeze.py")
FILES = {"freeze_sha256_plan": "inputs/plan.json", "freeze_sha256_q_common": "code/q_common.py",
         "freeze_sha256_q_confirm": "code/q_confirm.py", "freeze_sha256_q_rules": "code/q_rules.py"}
for name, rel in FILES.items():
    h = hashlib.sha256(open(f"{P3}/{rel}", "rb").read()).hexdigest()
    V.set(name, h, "text", {"population": "Phase 3 pre-registration files", "filter": rel, "statistic": "sha256 of the file at the freeze"}, kind="text")
V.set("freeze_time", datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ"), "text",
      {"population": "Phase 3 pre-registration files", "filter": "q06_freeze.py", "statistic": "time the hashes were taken (UTC)"}, kind="text")
V.save()
print("q06_freeze: registered", len([k for k in V.v if V.v[k]["source"] == "q06_freeze.py"]), "values")
