import argparse,json,sqlite3,time
from pathlib import Path
p=argparse.ArgumentParser();p.add_argument('checkout');p.add_argument('parent');a=p.parse_args()
c=sqlite3.connect('file:'+str(Path(a.checkout)/'.t3/userdata/statev2.sqlite')+'?mode=ro',uri=True)
for _ in range(600):
 rows=[json.loads(s) for (s,) in c.execute("select payload_json from orchestration_v2_projection_subagents where thread_id=? and origin='app_owned'",(a.parent,))]
 if rows and all(d['status'] in ['completed','interrupted','failed','cancelled'] for d in rows):
  print(json.dumps([{k:d[k] for k in ['title','status','result','completionDelivery'] if k in d} for d in rows]));break
 time.sleep(.1)
else:raise SystemExit('tasks did not settle')
