#!/usr/bin/env bash
# CHECK-arxiv-tt20 -- arxiv-tt20 extract (Extract lane, 2026-10-29).
# Re-fetches the arXiv export API and replays the parse; diffs the
# (arxiv_id, title) pairs against the committed arxiv-tt20.json.
# Exit code IS the verdict: 0 = PASS, nonzero = FAIL.
set -u
cd /root/troupe-standup/wizard-work/ledger || exit 1

WORK="$(mktemp -d)" || exit 1
trap 'rm -rf "$WORK"' EXIT

python3 - "$WORK" <<'PY'
import json, re, sys, urllib.request

api = ("https://export.arxiv.org/api/query"
       "?search_query=all:%22test-time%22+OR+all:%22test-time+scaling%22"
       "+OR+all:%22test_time+compute%22&start=0&max_results=20")
req = urllib.request.Request(api, headers={"User-Agent": "Mozilla/5.0"})
xml = urllib.request.urlopen(req, timeout=30).read().decode("utf-8", errors="replace")

got = []
for entry in re.finditer(r"<entry>(.*?)</entry>", xml, re.S):
    b = entry.group(1)
    iid = re.search(r"<id>(.*?)</id>", b, re.S)
    tt = re.search(r"<title>(.*?)</title>", b, re.S)
    if not iid or not tt:
        print("FAIL: malformed entry in replay")
        sys.exit(1)
    got.append((iid.group(1).split("/abs/", 1)[-1].strip(),
                tt.group(1).strip()))

committed = json.load(open("arxiv-tt20.json", encoding="utf-8"))
exp = [(p["arxiv_id"], p["title"]) for p in committed["papers"]]

if len(got) != len(exp):
    print(f"FAIL: count replay={len(got)} committed={len(exp)}")
    sys.exit(1)
for i, ((g1, g2), (e1, e2)) in enumerate(zip(got, exp), 1):
    if g1 != e1 or g2 != e2:
        print(f"FAIL: entry {i} replay=({g1!r},{g2!r}) committed=({e1!r},{e2!r})")
        sys.exit(1)

print(f"PASS: {len(exp)} papers replay id+title match committed arxiv-tt20.json")
PY
exit $?
