import subprocess,sys,time,pathlib
out=pathlib.Path(sys.argv[1]); session='flag-proof-46183'
for i in range(4):
 a=out.with_name(out.stem+'-stable-a.png')
 subprocess.run(['agent-device','screenshot',str(a),'--session',session],check=True,stdout=subprocess.DEVNULL)
 time.sleep(.5)
 subprocess.run(['agent-device','screenshot',str(out),'--session',session],check=True,stdout=subprocess.DEVNULL)
 result=subprocess.run(['compare','-metric','AE',str(a),str(out),'null:'],capture_output=True)
 print('pixel difference',result.stderr.decode(),flush=True)
 if result.returncode==0: break
else: raise SystemExit('Screenshots did not stabilize')
