import pathlib, subprocess, sys, time
from PIL import Image, ImageChops
out = pathlib.Path(sys.argv[1])
a = out.with_name(out.stem + '-check.png')
def shot(p):
    subprocess.run(['agent-device','screenshot',str(p),'--session','proof33078'],check=True,capture_output=True)
for attempt in range(4):
    shot(a)
    time.sleep(0.5)
    shot(out)
    if ImageChops.difference(Image.open(a).convert('RGB'),Image.open(out).convert('RGB')).getbbox() is None:
        print('Identical screenshots at least 500 ms apart:', out.name)
        break
else:
    raise SystemExit('Screenshots did not stabilize')
