import datetime,pathlib,subprocess,sys,time
session,root=sys.argv[1:]
out=pathlib.Path(root)/datetime.datetime.now(datetime.timezone.utc).strftime('%Y%m%dT%H%M%SZ')
out.mkdir(parents=True,exist_ok=False)
for n in range(3):
 a,b=out/f'{n}-a.png',out/f'{n}-b.png'
 subprocess.run(['agent-device','screenshot',str(a),'--session',session],check=True)
 time.sleep(.5)
 subprocess.run(['agent-device','screenshot',str(b),'--session',session],check=True)
 if a.read_bytes()==b.read_bytes():
  print('STABLE',b);break
else:raise RuntimeError('Screenshots did not stabilize')
