import subprocess, sys, time, hashlib
from pathlib import Path
session, output = sys.argv[1:]
p=Path(output)
prev=None
for i in range(12):
    subprocess.run(['agent-device','screenshot',str(p),'--session',session],check=True,stdout=subprocess.DEVNULL)
    current=hashlib.sha256(p.read_bytes()).hexdigest()
    if prev==current:
        print(f'Stable consecutive screenshots: {p.name} ({current})')
        break
    prev=current
    time.sleep(.5)
else:
    raise SystemExit('Screenshots did not stabilize')
