"""Read actual persisted error events; retain only synthetic turn diagnostics."""
import sqlite3,json,sys,pathlib
home=pathlib.Path(sys.argv[1])
c=sqlite3.connect('file:'+str(home/'userdata/statev2.sqlite')+'?mode=ro',uri=True)
rows=[]
for seq,event_type,payload in c.execute("select sequence,event_type,payload_json from orchestration_events where event_type like 'turn-item.%' and json_extract(payload_json,'$.type')='error' order by sequence"):
 item=json.loads(payload)
 rows.append(dict(sequence=seq,eventType=event_type,run=item['runId'].split(':ordinal:')[-1],status=item['status'],title=item['title'],failure=item['failure'],retry=item.get('retry')))
print(json.dumps(rows,indent=2))
