# Usage: python3 analyze.py <rollout.jsonl>...  -> one summary line per rollout
import json,sys
for f in sys.argv[1:]:
    meta=ctx=None; usage={}; calls=0; empty=0; devmsgs=0; first_user=None
    for l in open(f):
        o=json.loads(l); t=o.get("type"); p=o.get("payload",{})
        if t=="session_meta": meta=(p.get("source"),p.get("originator"),p.get("cli_version"))
        elif t=="turn_context" and ctx is None: ctx=(p.get("effort"),p.get("summary"),(p.get("sandbox_policy") or {}).get("type"))
        elif t=="event_msg" and p.get("type")=="token_count" and p.get("info"): usage=p["info"]["total_token_usage"]
        elif t=="response_item":
            pt=p.get("type")
            if pt in ("function_call","custom_tool_call","local_shell_call"):
                calls+=1; a=str(p.get("arguments") or p.get("input") or "")
                if 'cmd":""' in a.replace(" ","") or "cmd:\"\"" in a.replace(" ",""): empty+=1
            if pt=="message" and p.get("role")=="developer": devmsgs+=1
            if pt=="message" and p.get("role")=="user" and first_user is None:
                txt="".join(c.get("text","") for c in p.get("content",[]))
                if not txt.startswith("<"): first_user=txt[:60].replace("\n","\\n")
    print(f.split("rollout-")[-1][20:56], meta, ctx, "reason=%s out=%s calls=%d empty=%d dev=%d" % (usage.get("reasoning_output_tokens"),usage.get("output_tokens"),calls,empty,devmsgs), repr(first_user))
