import subprocess,time,sys,pathlib
out=pathlib.Path(sys.argv[1]); session=sys.argv[2]
def shot(p): subprocess.run(['agent-device','screenshot',str(p),'--session',session],check=True,stdout=subprocess.DEVNULL)
for i in range(5):
 shot(out.with_suffix('.candidate.png')); time.sleep(.5); shot(out)
 if out.read_bytes()==out.with_suffix('.candidate.png').read_bytes():
  print('Stable:',out.name); break
else: raise SystemExit('Screenshots did not stabilize')
