File size: 2,798 Bytes
13354e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"""Export recent real briefs as the eval set. Read-only: it only selects.

    python scripts/export_eval_briefs.py            # 60 briefs -> evals/briefs.jsonl
    python scripts/export_eval_briefs.py --n 90 --out evals/briefs.jsonl

Pulls the newest `brief_intake` rows from agent_outputs (the request exactly as it
was submitted), then takes one per niche in turn so a busy niche can't crowd the
set. Each line is one GenerationRequest payload. Export more than you need and
prune by hand to ~30; see evals/README.md.
"""
from __future__ import annotations

import argparse
import json
import sys
from collections import OrderedDict
from pathlib import Path

ROOT = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(ROOT))


def round_robin(payloads: list[dict], n: int) -> list[dict]:
    """Newest-first payloads -> up to n, one niche at a time, newest of each first."""
    by_niche: OrderedDict[str, list[dict]] = OrderedDict()
    for p in payloads:
        by_niche.setdefault(p.get("niche", ""), []).append(p)
    picked: list[dict] = []
    while len(picked) < n and any(by_niche.values()):
        for queue in by_niche.values():
            if queue and len(picked) < n:
                picked.append(queue.pop(0))
    return picked


def main(argv: list[str] | None = None) -> int:
    parser = argparse.ArgumentParser(description=__doc__.split("\n")[0])
    parser.add_argument("--n", type=int, default=60)
    parser.add_argument("--out", type=Path, default=ROOT / "evals" / "briefs.jsonl")
    args = parser.parse_args(argv)

    from app.schemas import GenerationRequest
    from app.services.supabase_store import SupabaseStore

    # agent_outputs.agent holds the DB enum ("orchestrator"); the internal name
    # rides in output.internal_agent. stage "intake" is only ever brief_intake.
    rows = SupabaseStore().select(
        "agent_outputs",
        filters={"agent": "orchestrator", "stage": "intake"},
        order_by="created_at",
        descending=True,
        limit=max(args.n * 10, 500),
        columns="output",
    )
    payloads = []
    for row in rows:
        output = dict(row.get("output") or {})
        if output.pop("internal_agent", "brief_intake") != "brief_intake":
            continue
        try:
            payloads.append(GenerationRequest.model_validate(output).model_dump(mode="json"))
        except Exception:  # noqa: BLE001 - an old row from a since-changed schema
            continue

    picked = round_robin(payloads, args.n)
    args.out.parent.mkdir(parents=True, exist_ok=True)
    args.out.write_text("".join(json.dumps(p) + "\n" for p in picked))
    print(f"wrote {len(picked)} briefs across {len({p['niche'] for p in picked})} niches to {args.out}")
    return 0


if __name__ == "__main__":
    sys.exit(main())