import datetime, pathlib, subprocess, sys, time
side=sys.argv[1]
session=sys.argv[2]
out=pathlib.Path(__file__).parent/(side+'-'+datetime.datetime.now(datetime.timezone.utc).strftime('%Y%m%dT%H%M%SZ'))
out.mkdir()
def shot(name):
 p=out/name
 subprocess.run(['agent-device','screenshot',str(p),'--normalize-status-bar','--session',session],check=True)
 return p
for attempt in range(10):
 a=shot('stable-a.png')
 time.sleep(.5)
 b=shot('stable-b.png')
 if subprocess.run(['compare','-metric','AE',str(a),str(b),'null:'],capture_output=True).returncode == 0:
  final=out/(side+'.png')
  subprocess.run(['convert',str(b),'-crop','1080x2194+0+230','+repage',str(final)],check=True)
  print('Stable capture:',final)
  break
else: raise RuntimeError('Screen did not become pixel-identical')
