import subprocess,time,sys
from pathlib import Path
from PIL import Image,ImageChops
out=Path(sys.argv[1])
for attempt in range(8):
    a=out.with_name(out.stem+'-stability-a.png')
    subprocess.run(['agent-device','screenshot',str(a),'--session','pr47976'],check=True,stdout=subprocess.DEVNULL)
    time.sleep(.5)
    subprocess.run(['agent-device','screenshot',str(out),'--session','pr47976'],check=True,stdout=subprocess.DEVNULL)
    if ImageChops.difference(Image.open(a).convert('RGB'),Image.open(out).convert('RGB')).getbbox() is None:
        print('Stable identical pair:',out.name);break
else: raise SystemExit('No stable pair')
