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):
 task=c.execute("select child_thread_id from orchestration_v2_projection_subagents where thread_id=? and origin='app_owned' and json_extract(payload_json,'$.title')='Background checksum'",(a.parent,)).fetchone()
 if task:
  turns=c.execute('select status from orchestration_v2_projection_provider_turns where thread_id=?',(task[0],)).fetchall()
  native=c.execute('select status from orchestration_v2_projection_subagents where thread_id=?',(task[0],)).fetchall()
  command=c.execute("select count(*) from orchestration_v2_projection_turn_items where thread_id=? and type='command_execution' and status='running' and json_extract(payload_json,'$.input')='python3 -u checksum.py'",(a.parent,)).fetchone()[0]
  if turns==[('completed',)] and ('running',) in native and command:
   print(json.dumps({'childProviderTurn':'completed','nativeBackgroundAgent':'running','parentCommand':'running'}));break
 time.sleep(.1)
else:raise SystemExit('required pre-Stop state was not reached')
