"""Build the window-only accept re-read set from primary records.

Accept = accept_B_conservative in the run's shadow_claims.jsonl, joined on claim_sha256.
Window-only = no reviewer's label (`checked`) lists saved_page or live_url; a second reviewer who opened the page counts as a read.
Excludes the nine packets already re-read in adj/recheck-spliced/.
Writes adj/recheck-window-only/inputs/{slice_r1..r4}.jsonl with absolute page/rubric/bundle paths.
"""
import io, json, os, glob, random, sys, collections

EXP = r"out/exp/"
OUT = os.path.join(EXP, "adj", "recheck-window-only")
N_AGENTS = 4


def jl(p):
    return [json.loads(x) for x in io.open(p, encoding="utf-8") if x.strip()]


runs = {
    "ws1": dict(key=os.path.join(EXP, "ws1", "adjudication_key.json"),
                shadow=os.path.join(EXP, "ws1", "shadow_claims.jsonl"),
                adj=os.path.join(EXP, "adj"), agents="a*"),
    "f4": dict(key=os.path.join(EXP, "f4", "adjudication_key.json"),
               shadow=os.path.join(EXP, "f4", "shadow_claims.jsonl"),
               adj=os.path.join(EXP, "f4", "adj"), agents="b*"),
}

already = {r["packet_id"] for r in jl(os.path.join(EXP, "adj", "recheck-spliced", "labels.jsonl"))}
print("already re-read (spliced set):", len(already))

rows, stats = [], collections.Counter()
for run, cfg in runs.items():
    key = json.load(io.open(cfg["key"], encoding="utf-8"))["key"]
    acc = {c["claim_sha256"] for c in jl(cfg["shadow"]) if c.get("accept_B_conservative")}
    # packets per slice, keyed by packet id, remembering which slice file it came from
    packets = {}
    for sp in sorted(glob.glob(os.path.join(cfg["adj"], "inputs", "slice_*.jsonl"))):
        for p in jl(sp):
            packets[p["packet_id"]] = p
    labels = collections.defaultdict(list)  # every non-superseded label for the packet, any reviewer
    for lp in sorted(glob.glob(os.path.join(cfg["adj"], cfg["agents"], "labels.jsonl"))):
        for l in jl(lp):
            if not l.get("superseded"):
                labels[l["packet_id"]].append(l)
    for pid, k in key.items():
        if pid not in labels:
            continue
        stats[run + " labelled"] += 1
        is_acc = k["claim_sha256"] in acc
        if not is_acc:
            continue
        stats[run + " accepts labelled"] += 1
        chs = [set(l.get("checked") or []) for l in labels[pid]]
        if any(c & {"saved_page", "live_url"} for c in chs):
            stats[run + " accept: some reviewer read the page"] += 1
            continue
        stats[run + " accept: no reviewer read the page"] += 1
        ch = set().union(*chs)
        if len(chs) > 1:
            stats[run + "   of which double-reviewed"] += 1
        if pid in already:
            stats[run + " accept no page read, already re-read"] += 1
            continue
        p = dict(packets[pid])
        base = os.path.join(cfg["adj"], "inputs")
        for f in ("page_file", "rubric_file", "bundle_evidence_file"):
            if p.get(f):
                p[f] = os.path.normpath(os.path.join(base, p[f])).replace("\\", "/")
                assert os.path.isfile(p[f]), p[f]
        p["run"] = run
        p["prior_checked"] = sorted(ch)  # union of what the prior reviewer(s) recorded; verdicts withheld
        rows.append(p)

for k in sorted(stats):
    print(f"{stats[k]:4d}  {k}")
print("to re-read:", len(rows))

random.Random(20260922).shuffle(rows)
os.makedirs(os.path.join(OUT, "inputs"), exist_ok=True)
for i in range(N_AGENTS):
    part = rows[i::N_AGENTS]
    with io.open(os.path.join(OUT, "inputs", f"slice_r{i+1}.jsonl"), "w", encoding="utf-8", newline="\n") as f:
        for p in part:
            f.write(json.dumps(p, ensure_ascii=False) + "\n")
    print(f"slice_r{i+1}: {len(part)} packets, vendors {len({p['vendor'] for p in part})}")
json.dump({"built": "2026-09-22", "n": len(rows), "rule": "accept_B_conservative and no reviewer label checked lists saved_page or live_url, minus the 7 accepts already re-read in recheck-spliced",
           "stats": dict(stats)}, io.open(os.path.join(OUT, "inputs", "MANIFEST.json"), "w", encoding="utf-8"), indent=1)
