"""Run from each checkout after opening its seeded showcase thread on the paired iOS app.
Arguments: T3 home, project directory, session, output directory.
Creates a showcase history turn, then records a second identical turn.
Open the created thread before pressing Enter to begin the second turn.
"""
import json, pathlib, subprocess, sys, time
home, project, session, output = sys.argv[1:]
out = pathlib.Path(output); out.mkdir(parents=True, exist_ok=False)
def cli(*args):
    return subprocess.check_output(['node', 'apps/server/src/bin.ts', *args, '--base-dir', home], text=True)
cli('project', 'add', project, '--title', 'math-demo')
thread = json.loads(cli('thread', 'start', project, 'Give me a short tour', '--title', 'Streaming reply', '--provider', 'devReplay', '--model', 'showcase', '--wait', '--json'))['threadId']
print('Open Streaming reply on the paired client.'); input('Press Enter when ready: ')
subprocess.run(['agent-device', 'record', 'start', str(out/'stream.mp4'), '--hide-touches', '--session', session], check=True)
try:
    cli('thread', 'send', thread, 'Give me a short tour', '--wait', '--json')
    time.sleep(4)  # Keep recording until the remote client has rendered the final streamed chunks.
finally:
    subprocess.run(['agent-device', 'record', 'stop', '--session', session], check=True)
