import subprocess,sys,pathlib,time,json
# Run after choosing Codex and the requested permission mode in a fresh app thread.
# Browser executable/session and output directory are supplied by the operator.
browser,session,out,label=sys.argv[1:]
out=pathlib.Path(out);out.mkdir(parents=True,exist_ok=True)
def ab(*args):
 r=subprocess.run([browser,'--session',session,*args],capture_output=True,text=True)
 if r.returncode: raise RuntimeError(r.stdout+r.stderr)
 return r.stdout
prompt='Run exactly gh api user --jq .login inside the sandbox, without escalation. Report the command output and whether it succeeded. Do not retry with broader permissions.'
# Hide private operational metadata; crop the sidebar and workspace from exports.
ab('eval',"(()=>{const s=document.createElement('style');s.textContent='button:has([data-chat-provider-model-picker-label]){visibility:hidden!important}';document.head.append(s)})()")
ab('record','start',str(out/(label+'-raw.webm')))
ab('find','role','textbox','fill','--name','Message New thread',prompt)
ab('find','role','button','click','--name','Submit message','--exact')
ab('wait','--text','Worked for')
ab('wait','--fn',"!!document.querySelector('button[aria-label=\"Submit message\"]') && !document.querySelector('button[aria-label=\"Stop response\"]')")
time.sleep(1)
(out/(label+'-private-dom.txt')).write_text(ab('read'))
ab('screenshot',str(out/(label+'-stable-1.png')));time.sleep(.5)
ab('screenshot',str(out/(label+'-stable-2.png')))
ab('record','stop')
subprocess.run(['ffmpeg','-y','-i',str(out/(label+'-raw.webm')),'-vf','crop=724:632:256:0','-c:v','libx264','-pix_fmt','yuv420p',str(out/(label+'.mp4'))],check=True,capture_output=True)
subprocess.run(['convert',str(out/(label+'-stable-2.png')),'-crop','724x632+256+0','+repage',str(out/(label+'.png'))],check=True)
print((out/(label+'-private-dom.txt')).read_text())
