#!/usr/bin/env python3
"""WS1 blind adjudication packet. One line per in-scope claim unit, shuffled with random.Random(20260921).
Carries NO model outputs, NO ground.py or step 03 results. The packet-id -> claim key is written separately."""
import json
import random
from pathlib import Path

WS1 = Path("out/exp/ws1")
SEED = 20260921
rows = [json.loads(l) for l in open(WS1 / "shadow_claims.jsonl", encoding="utf-8")]
ins = [r for r in rows if r["set"] == "primary" and r["scope"] == "in_scope"]
names = {}
for r in ins:
    if r["slug"] not in names:
        meta = json.load(open(WS1 / "onboarding-io" / "runs" / r["slug"] / "bundle" / "meta.json", encoding="utf-8"))
        names[r["slug"]] = meta.get("name") or r["slug"]
rng = random.Random(SEED)
order = list(range(len(ins)))
rng.shuffle(order)
ids, packet, key = set(), [], {}
for i in order:
    r = ins[i]
    pid = "%08x" % rng.getrandbits(32)
    while pid in ids:
        pid = "%08x" % rng.getrandbits(32)
    ids.add(pid)
    packet.append({"packet_id": pid, "vendor": names[r["slug"]], "section": r["section"], "claim": r["claim"],
                   "source_url": r["source_url"], "evidence": r["arm_b_evidence"], "saved_page_path": r["saved_page_path"],
                   "as_of": r["date"]})
    key[pid] = {"run_id": r["run_id"], "slug": r["slug"], "claim_id": r["claim_id"], "unit_index": r["unit_index"],
                "claim_sha256": r["claim_sha256"], "arm_b_sha256": r["arm_b_sha256"]}
with open(WS1 / "adjudication_packet.jsonl", "w", encoding="utf-8") as f:
    for p in packet:
        f.write(json.dumps(p, ensure_ascii=False) + "\n")
json.dump({"seed": SEED, "n": len(key), "note": "packet_id -> claim; keep separate from the adjudicator", "key": key},
          open(WS1 / "adjudication_key.json", "w", encoding="utf-8"), indent=1)
print(len(packet), "packet lines;", sorted(names.values()))
