"""Export synthetic evidence data without provider configuration, model ids, or paths.
Usage: python3 export-runtime.py base-checkout head-checkout output.json
"""
import json
import pathlib
import sqlite3
import subprocess
import sys

result = {"environment": "Real web development client, Linux Chromium, light theme, 1440x900 CSS, DPR 2; isolated fresh databases; real provider turns", "sides": {}}
for side, directory in zip(["base", "head"], sys.argv[1:3]):
    db = sqlite3.connect(f"file:{directory}/.t3/userdata/statev2.sqlite?mode=ro", uri=True)
    cases = []
    for title, payload in db.execute("SELECT json_extract(t.payload_json,'$.title'),p.payload_json FROM orchestration_v2_projection_threads t JOIN orchestration_v2_projection_provider_turns p ON p.thread_id=t.thread_id ORDER BY p.started_at"):
        if title == "Claude reported window":
            case = "claude-default"
        elif title == "/root/ok":
            case = "child-agent"
        elif title and title.startswith("Context "):
            case = title.removeprefix("Context ")
        else:
            continue
        turn = json.loads(payload)
        usage = turn.get("tokenUsage", {})
        cases.append({"case": case, "status": turn["status"], "usage": {key: usage[key] for key in ["usedTokens", "maxTokens", "autoCompactThreshold"] if key in usage}})
    sha = subprocess.check_output(["git", "-C", directory, "rev-parse", "HEAD"], text=True).strip()
    result["sides"][side] = {"commit": sha, "cases": cases}
pathlib.Path(sys.argv[3]).write_text(json.dumps(result, indent=2)+"\n")
print(json.dumps(result, indent=2))
