"""Record the identical real-client interaction on each already seeded server.
Usage: python3 record.py SESSION SIDE OUTPUT_ROOT
Navigate to the paired parent thread and configure 1440x900 / dark before running.
Only the app's built-in Lineage detail is enabled; workspace details are hidden.
"""
import subprocess, sys, time, pathlib, datetime, json
session, side, root = sys.argv[1:]
out = pathlib.Path(root) / (datetime.datetime.now(datetime.timezone.utc).strftime('%Y%m%dT%H%M%SZ') + '-' + side)
out.mkdir(parents=True)

def ab(*args):
    r = subprocess.run(['npx','--yes','agent-browser','--session',session,*args],capture_output=True,text=True)
    if r.returncode:
        raise RuntimeError(r.stderr or r.stdout)
    return r.stdout

def click(name):
    return ab('find','role','button','click','--name',name,'--exact')

def snapshot(name):
    (out / name).write_text(ab('snapshot'))

ab('wait','--text','Inspect synthetic sample')
snapshot('initial.txt')
ab('record','start',str(out / (side + '.webm')),'--cursor')
time.sleep(2)
click('Open Inspect synthetic sample')
ab('wait','--text','Inspection complete. The synthetic sample is valid. No further work is needed.')
snapshot('child-final-answer.txt')
time.sleep(3)
click('Open parent thread: Delegation Stop evidence')
ab('wait','--text','Inspect synthetic sample')
time.sleep(2)
ab('hover','button[aria-label="Stop subagent Finished delegated child"]')
time.sleep(1)
click('Stop subagent Finished delegated child')
ab('hover','[aria-label="New thread in Synthetic sample"]')
time.sleep(6)
snapshot('after-stop.txt')
ab('record','stop')
(out / 'environment.json').write_text(json.dumps({'surface':'web development client','os':'Linux','browser':'Chrome','viewport':{'width':1440,'height':900},'theme':'dark','data':'synthetic completed delegated child and stale native subtree','side':side},indent=2))
print(out)
