import json
from pathlib import Path

def load_arm(tag):
    recs = {}
    for line in Path(f"results/parity_{tag}.jsonl").read_text().splitlines():
        if not line.strip():
            continue
        r = json.loads(line)
        pid = r["video"] if r["tag"] == "pos" else None
        key = ("pos", r["video"]) if r["tag"] == "pos" else ("neg", r["video"], r["spec_of"])
        recs[key] = r
    return recs

for tag in ("hosted", "46v", "local"):
    a = load_arm(tag)
    print(f"== {tag} ==")
    echo_cnt = 0
    total = 0
    for key, nrow in sorted(a.items(), key=lambda x: str(x[0])):
        if key[0] != "neg":
            continue
        k, vid, sid = key
        prow = a.get(("pos", vid))
        total += 1
        nf = [(f["frame"], f.get("pass"), f.get("conf")) for f in nrow["frames"]]
        pf = [(f["frame"], f.get("pass"), f.get("conf")) for f in prow["frames"]]
        same = nf == pf and nrow["verdict"] == prow["verdict"]
        echo_cnt += same
        mark = "ECHO!" if same else "differs"
        print(f"  neg {vid}->{sid}: {nrow['verdict']}({nrow['votes']}) vs pos {vid}: {prow['verdict']}({prow['votes']}) -> {mark}")
    print(f"  => {echo_cnt}/{total} 完全回声")
