import subprocess,sys,time,hashlib
from pathlib import Path
side,out=sys.argv[1:3];out=Path(out);out.mkdir(parents=True,exist_ok=True)
def a(*args):
 r=subprocess.run(['agent-device',*args,'--session','proof46736'],text=True,capture_output=True,check=True)
 print(r.stdout,flush=True)
def click(s): a('click',s,'--settle')
def menu():
 click('label="Archived thread options"');click('text="Environment"')
def shot(label):
 p=out/f'{side}-{label}.png'
 for i in range(4):
  a('screenshot',str(p));old=p.read_bytes();time.sleep(.5);a('screenshot',str(p))
  if p.read_bytes()==old: break
 else: raise RuntimeError('Screen did not stabilize')
a('record','start',str(out/f'{side}-archive.mp4'))
click('text="Alpha"');shot('archive-alpha')
menu();shot('archive-selected');click('text="Lab"');shot('archive-lab')
menu();click('text="Alpha"');shot('archive-after-lab')
menu();click('text="All environments"');shot('archive-all')
a('record','stop')
a('back','--settle');click('text="Usage"')
