proof-or-claim / scripts /verify_labels.py
issaquahd's picture
Initial release (part 2)
611d454 verified
Raw History Blame Contribute Delete
3.15 kB
#!/usr/bin/env python3
"""Re-run every sandbox receipt against its saved final state and re-derive every label.
Usage: verify_labels.py
For each of the 260 sandbox items, the item's sandbox directory is copied to a temporary
directory and its receipt is run there, so the repo is never written to. The receipt result must
equal the stored "truth" bit, and label must equal PROVEN iff truth and evidence. Failure Lab
items are checked for internal consistency (label from truth, reproduction and evidence); their
runs are not repeated here. Clinic labels come from case design and are not re-run.
Receipt forms used in this data (a subset of the prove-it-done receipt syntax):
cmd:ARGV [| contains=TEXT] run ARGV without a shell; pass on exit 0 (and TEXT in stdout+stderr)
file:PATH | sha256=HEX PATH is a regular file with that SHA-256
"python3" in a receipt runs the current interpreter. Exit 1 on any mismatch.
"""
import hashlib
import json
import os
import re
import shlex
import shutil
import subprocess
import sys
import tempfile
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent))
from pocb import ROOT, load_jsonl # noqa: E402
SPLIT = re.compile(r"\s+\|\s+(?=(?:contains|sha256)=)")
def run_receipt(spec, cwd):
head, *opts = SPLIT.split(spec)
opts = dict(o.split("=", 1) for o in opts)
kind, target = head.split(":", 1)
if kind == "file":
p = Path(cwd) / target
if not p.is_file():
return False
return "sha256" not in opts or hashlib.sha256(p.read_bytes()).hexdigest() == opts["sha256"].lower()
if kind == "cmd":
argv = shlex.split(target)
if argv[0] == "python3":
argv[0] = sys.executable
env = dict(os.environ, PYTHONDONTWRITEBYTECODE="1")
try:
proc = subprocess.run(argv, cwd=cwd, stdin=subprocess.DEVNULL, capture_output=True, timeout=60, env=env)
except (OSError, subprocess.TimeoutExpired):
return False
if proc.returncode != 0:
return False
return "contains" not in opts or opts["contains"].encode() in proc.stdout + b"\n" + proc.stderr
raise ValueError(f"receipt kind not used in this data: {kind}")
def main():
bad, n_sb, n_fl = [], 0, 0
for it in load_jsonl(ROOT / "data/items.jsonl"):
if it["source"] == "sandbox":
n_sb += 1
with tempfile.TemporaryDirectory() as td:
work = Path(td) / "sb"
shutil.copytree(ROOT / "data" / it["sandbox"], work)
truth = run_receipt(it["receipt"], work)
if truth != it["truth"] or ("PROVEN" if it["truth"] and it["evidence"] else "UNPROVEN") != it["label"]:
bad.append(it["id"])
elif it["source"] == "faultlab":
n_fl += 1
if ("PROVEN" if it["truth"] and it["fl_reproduced"] and it["evidence"] else "UNPROVEN") != it["label"]:
bad.append(it["id"])
print(json.dumps({"sandbox_receipts_rerun": n_sb, "faultlab_labels_checked": n_fl, "mismatches": bad}))
sys.exit(1 if bad else 0)
if __name__ == "__main__":
main()