import sqlite3,json,sys
checkout,seed,out=sys.argv[1:]
thread=json.load(open(seed))['target']['threadId']
c=sqlite3.connect('file:'+checkout+'/.t3/evidence/userdata/statev2.sqlite?mode=ro',uri=True)
rows=[]
for typ,at,raw in c.execute("select event_type,occurred_at,payload_json from orchestration_events where stream_id=? and event_type in ('run.updated','thread.visit-recorded') order by sequence",(thread,)):
 p=json.loads(raw)
 rows.append({k:v for k,v in {'event':typ,'recordedAt':at,'status':p.get('status'),'completedAt':p.get('completedAt'),'lastVisitedAt':p.get('lastVisitedAt')}.items() if v is not None})
json.dump(rows,open(out,'w'),indent=2)
print(json.dumps(rows[-12:],indent=2))
