#!/usr/bin/env python3
"""Record the real app's first pre-compaction revert.
Usage: revert-driver.py <agent-browser> <session> <output.webm>
Open the freshly seeded thread and close the details panel before running.
Apply privacy.js first. The same actions run on both commits.
"""
import subprocess,sys,time
browser,session,output=sys.argv[1:]
def ab(*args):
    return subprocess.run([browser,'--session',session,*args],check=True,text=True,capture_output=True).stdout
ab('record','start',output,'--fps','5')
time.sleep(1)
ab('find','role','button','click','--name','Agent Third checkpoint. Say SET and keep remembering the code word. Do not use tools.','--exact')
time.sleep(1)
ab('find','role','button','click','--name','Edit from here','--exact')
time.sleep(1)
ab('find','role','button','click','--name','Revert and keep changes','--exact')
ab('wait','--fn','document.querySelector("[contenteditable=true]")?.innerText.includes("Third checkpoint")')
time.sleep(1)
ab('find','role','textbox','fill','--name','Message Compaction rollback proof','What is the retained code word? Reply with only that word. Do not use tools.')
time.sleep(1)
ab('find','role','button','click','--name','Submit message','--exact')
ab('wait','--fn','(document.body.innerText.includes("Provider error") || (document.body.innerText.match(/MARIGOLD/g)||[]).length >= 3) && !document.body.innerText.includes("Working for")')
time.sleep(2)
print(ab('eval','document.body.innerText'))
print(ab('record','stop'))
