"""Drive an already paired Android client at Usage > Limits.
Usage: python3 reproduce.py SESSION OUT
The server uses hub.py and fresh state; source config: kind=cliproxy,
url=http://127.0.0.1:18317, label=Demo usage hub, managementKey=synthetic-proof-key.
"""
import subprocess,sys,time,pathlib,json,datetime,re
session,out=sys.argv[1:]; root=pathlib.Path(out)/datetime.datetime.now(datetime.timezone.utc).strftime('%Y%m%dT%H%M%SZ');root.mkdir(parents=True)
def run(*args):
 p=subprocess.run(['agent-device',*args,'--session',session],capture_output=True,text=True,check=True)
 with (root/'transcript.txt').open('a') as f: f.write(datetime.datetime.now(datetime.timezone.utc).isoformat()+' '+ ' '.join(args)+'\n'+p.stdout+'\n')
 return p.stdout
run('record','start',str(root/'timed.mp4'),'--hide-touches')
snapshot=run("snapshot","-i")
ref=re.search(r'(@e\S+) \[button\] "Segment 1,.*of the window left"',snapshot).group(1)
run("press",ref,"--settle")
run('screenshot',str(root/'start.png'))
for _ in range(4):
 time.sleep(53)
 run('snapshot','-i')
run('screenshot',str(root/'end-a.png'));time.sleep(.5);run('screenshot',str(root/'end-b.png'))
run('back','--settle');run('snapshot','-i');run('screenshot',str(root/'list.png'))
run('record','stop')
