"""Capture the split-title reproduction after opening a terminal and revealing its Clear key.
Usage: python3 mobile-capture.py native|fallback SESSION OUTPUT_DIR [HOME ORIGIN SEED_JSON]
Run from a checkout containing terminal-proof-control.ts under scripts/scenarios for fallback.
"""
import hashlib,pathlib,subprocess,sys,time
mode,session,out=sys.argv[1:4]
out=pathlib.Path(out);out.mkdir(parents=True,exist_ok=False)
def device(*args):
 return subprocess.run(['agent-device',*args,'--session',session],check=True,text=True,capture_output=True).stdout
command=r"printf '\033]0;Build '; sleep 8; printf 'progress\a'; printf 'ready\n'"
print(device('record','start',str(out/'interaction.mp4')),flush=True)
if mode=='native':
 print(device('type',command),flush=True)
 print(device('keyboard','enter'),flush=True)
else:
 home,origin,seed=sys.argv[4:7]
 subprocess.run(['node','scripts/scenarios/terminal-proof-control.ts',home,origin,seed,'write',command+'\r'],check=True)
time.sleep(1.5)
print(device('press','label="clear"','--settle'),flush=True)
time.sleep(9)
# Retain all raw captures, including any pair rejected for cursor or status-bar movement.
previous=None
for i in range(8):
 path=out/f'result-{i}.png'
 print(device('screenshot',str(path)),flush=True)
 current=hashlib.sha256(path.read_bytes()).hexdigest()
 if current==previous:
  (out/'stable.png').write_bytes(path.read_bytes());print('Two screenshots at least 500ms apart are identical.',flush=True);break
 previous=current
 time.sleep(.5)
else:
 print('No identical pair; use the interaction recording.',flush=True)
print(device('record','stop'),flush=True)
