import argparse,sqlite3,time,subprocess
from pathlib import Path
p=argparse.ArgumentParser();p.add_argument('checkout');p.add_argument('thread');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=c.execute('select status from orchestration_v2_projection_provider_turns where thread_id=? order by started_at desc limit 1',(a.thread,)).fetchall()
 count=c.execute('select count(*) from orchestration_v2_projection_provider_turns where thread_id=?',(a.thread,)).fetchone()[0]
 if count>=2 and rows==[('completed',)]:
  subprocess.run(['python3',str(Path(__file__).with_name('driver.py')),a.checkout,'hold','--thread',a.thread],check=True);break
 time.sleep(.1)
else:raise SystemExit('parent provider turn did not complete')
