"""Run after pairing and opening Scheduled tasks; pass session and a new output directory.
The slow-connection proxy must be active with its delay flag present.
Uses only synthetic data: Daily review / Review the project and summarize the next steps.
"""
import pathlib, re, subprocess, sys, time
session, output = sys.argv[1:]
output = pathlib.Path(output)
output.mkdir(parents=True, exist_ok=False)
def run(*args):
    result = subprocess.run(['agent-device', *args, '--session', session], capture_output=True, text=True, check=True)
    return result.stdout
def ref(label):
    snapshot = run('snapshot','-i')
    match = re.search(r'(@e\d+) \[button\] "'+re.escape(label)+'"',snapshot)
    if not match: raise RuntimeError('Required control absent: '+label)
    return match[1]
run('click',ref('Edit Daily review'))
run('snapshot','-i')
run('record','start',str(output/'interaction.mp4'),'--scope','device','--hide-touches')
run('scroll','bottom','--settle')
run('click',ref('Save changes'))
run('open','com.t3tools.t3code.dev','t3code-dev://settings/scheduled-tasks/new')
run('click',ref('OK'))
run('scroll','top','--settle')
run('screenshot',str(output/'a.png'))
time.sleep(.5)
run('screenshot',str(output/'b.png'))
run('scroll','bottom','--settle')
run('record','stop')
print('Finished scripted interaction')
