#!/usr/bin/env python3 """ Generate briefings for a fixed set of papers and write a page to rate them. python scripts/eval_briefings.py --papers scripts/eval_papers.txt \\ --db /tmp/eval.db --out reports/briefings/2026-10-19 Reads the real arXiv HTML pages (one request each, paced) and calls the configured model (LLM_PROVIDER, default the Groq chain) once per paper, into a scratch database, never the app's. Writes: index.html each paper: title, the briefing as readers would see it, every point the checks dropped and why, time taken ratings.csv one row per paper for the owner to mark good or bad, with a note summary.json counts by status, median seconds, model calls and spend The gate for switching BRIEFING on (docs/redesign/06-pr-6-7-paper-page-briefing.md): at least 45 of 50 rated good, no displayed number missing from the paper (true by construction; the dropped list proves the checks ran), median under 8 s. """ from __future__ import annotations import argparse import asyncio import csv import html import json import os import statistics import sys import time from pathlib import Path ROOT = Path(__file__).resolve().parents[1] def read_ids(path: Path) -> list[str]: return [line.strip() for line in path.read_text().splitlines() if line.strip() and not line.startswith("#")] def render(rows: list[dict], summary: dict) -> str: esc = html.escape parts = ["Briefing evaluation", "", f"

Briefing evaluation

{esc(json.dumps(summary))}

"] for r in rows: b = r.get("briefing") or {} parts.append(f"

{esc(r['id'])} " f"{esc(r.get('title', ''))}

" f"

{esc(r['status'])} ยท " f"{r['seconds']:.1f} s

") if b.get("things"): parts.append("") if b.get("how"): parts.append("
    " + "".join(f"
  1. {esc(s['name'])}: {esc(s['text'])}
  2. " for s in b["how"]) + "
") if b.get("use"): parts.append(f"

{esc(b['use'])}

") for d in b.get("dropped", []): parts.append(f"

Dropped: {esc(json.dumps(d))}

") if b.get("error"): parts.append(f"

{esc(b['error'])}

") return "\n".join(parts) async def run(ids: list[str], out: Path) -> dict: from app import briefing_svc, db, llm, paper_source_svc, turso_svc await db.init_db() rows = [] for i, aid in enumerate(ids, 1): t0 = time.perf_counter() meta = (await turso_svc.fetch_metadata_batch([aid])).get(aid) or {} for attempt in range(4): try: status, b = await briefing_svc.get_or_generate(aid, meta.get("title") or "", meta.get("abstract") or "") except Exception as e: # keep going; the row records it status, b = "failed", {"error": f"{type(e).__name__}: {e}"} if b.get("error") != "model unavailable": break print(f" every model is rate-limited; waiting 30 s (attempt {attempt + 1})", flush=True) await asyncio.sleep(30) secs = time.perf_counter() - t0 rows.append({"id": aid, "title": meta.get("title", ""), "status": status, "briefing": b, "seconds": secs}) print(f"[{i}/{len(ids)}] {aid} {status} {secs:.1f}s", flush=True) await paper_source_svc.stop() seconds = [r["seconds"] for r in rows if r["status"] == "ok"] summary = { "papers": len(rows), "by_status": {s: sum(r["status"] == s for r in rows) for s in ("ok", "insufficient", "failed")}, "dropped_points": sum(len((r["briefing"] or {}).get("dropped", [])) for r in rows), "median_seconds_ok": round(statistics.median(seconds), 2) if seconds else None, "model": llm.configured_model(), "today": await llm.today(), } out.mkdir(parents=True, exist_ok=True) (out / "index.html").write_text(render(rows, summary)) with open(out / "ratings.csv", "w", newline="") as f: w = csv.writer(f) w.writerow(["arxiv_id", "status", "rating (good/bad)", "note"]) for r in rows: w.writerow([r["id"], r["status"], "", ""]) (out / "summary.json").write_text(json.dumps(summary, indent=1)) return summary def main(argv=None) -> int: ap = argparse.ArgumentParser() ap.add_argument("--papers", type=Path, default=ROOT / "scripts" / "eval_papers.txt") ap.add_argument("--db", required=True, help="a scratch database; never the app's") ap.add_argument("--out", type=Path, required=True) ap.add_argument("--limit", type=int, default=0) args = ap.parse_args(argv) os.environ["DB_PATH"] = args.db os.environ.setdefault("TURSO_SYNC_DISABLED", "1") sys.path.insert(0, str(ROOT)) ids = read_ids(args.papers)[: args.limit or None] summary = asyncio.run(run(ids, args.out)) print(json.dumps(summary, indent=1)) return 0 if __name__ == "__main__": raise SystemExit(main())