import subprocess,time,sys
from pathlib import Path
s,side,out=sys.argv[1:];out=Path(out)
def call(*args):
 p=subprocess.run(['agent-device',*map(str,args),'--session',s],capture_output=True,text=True);print(' '.join(map(str,args)),p.stdout,p.stderr,flush=True)
 if p.returncode:raise RuntimeError(p.stderr)
 return p.stdout
def shot(label):
 for n in range(4):
  a=out/f'{side}-{label}-a.png';b=out/f'{side}-{label}-b.png'
  call('screenshot',a);time.sleep(.5);call('screenshot',b)
  if a.read_bytes()==b.read_bytes():return
 raise RuntimeError('unstable screenshots')
call('record','start',out/f'{side}-dismiss-flow.mp4','--fps','10','--hide-touches')
call('swipe',200,650,200,200,'--count',3)
shot('before-dismiss')
call('swipe',300,758,30,758)
call('wait','absent','text="Memory is slowing agents on Scenario"','5000')
call('swipe',200,650,200,200,'--count',3)
shot('dismissed-bottom')
call('press','label="4 settled threads"','--settle')
call('swipe',200,650,200,200,'--count',3)
shot('dismissed-open')
call('record','stop')
