import json, sqlite3, sys
# Read only the synthetic proof thread; omit identifiers, paths and credentials.
db, thread, transcript, sha = sys.argv[1:]
c = sqlite3.connect("file:" + db + "?mode=ro", uri=True)
runs = [json.loads(r[0]) for r in c.execute("select payload_json from orchestration_v2_projection_runs where thread_id=? order by ordinal", (thread,))]
result = {"commit": sha, "runs": [{k:r.get(k) for k in ("ordinal", "status", "requestedAt", "startedAt", "completedAt", "modelSelection", "executedModel")} for r in runs], "assistantMessages": []}
for line in open(transcript):
    d=json.loads(line)
    if d.get("type") != "assistant": continue
    m=d["message"]
    content=[]
    for part in m.get("content", []):
        if part.get("type")=="text": content.append({"text":part["text"]})
        elif part.get("type")=="tool_use": content.append({"tool":part["name"], "input":part["input"]})
    result["assistantMessages"].append({"timestamp":d.get("timestamp"), "model":m.get("model"), "content":content})
print(json.dumps(result, indent=2))
