import subprocess,pathlib,re,time,sys,os
root=pathlib.Path(os.environ['EVIDENCE_ROOT'])
side=sys.argv[1];session='pr34924base' if side=='base' else 'pr34924'
def cli(*args):
 p=subprocess.run(['npx','--yes','agent-browser','--session',session,*args],text=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT,timeout=60)
 if p.returncode: raise RuntimeError(p.stdout)
 return p.stdout
snap=cli('snapshot','-i');(root/f'{side}18-final-snapshot.txt').write_text(snap)
line=next(l for l in snap.splitlines() if 'button "Worked for' in l)
ref=re.search(r'ref=(e\d+)',line)[1]
if 'expanded=false' in line:cli('click','@'+ref)
cli('screenshot',str(root/f'{side}18-a.png'))
time.sleep(.5)
cli('screenshot',str(root/f'{side}18-b.png'))
print('captured',side,flush=True)
# A short recording of inspecting the actual persisted turn history.
print(cli('record','start',str(root/f'{side}18-short.webm'),'--fps','2'),flush=True)
cli('click','@'+ref)
time.sleep(2)
cli('click','@'+ref)
time.sleep(2)
print(cli('record','stop'),flush=True)
