#!/usr/bin/env python3
"""Run with: python3 recorder-driver.py CHECKOUT OUTPUT_DIR REAL_CODEX
Place trees-server.cjs beside this driver. Requires checkout dependencies and Node 26.
Run once per checkout on fresh output directories; inspect item/completed in raw and replay.
"""
import os, pathlib, subprocess, sys
checkout, output, codex = map(pathlib.Path, sys.argv[1:])
checkout=checkout.resolve(); output=output.resolve(); output.mkdir(parents=True, exist_ok=False)
workspace=output/'workspace'; workspace.mkdir()
(workspace/'AGENTS.md').write_text('Before replying to any request, you must call the MCP tool lookup on the trees server exactly once.\n')
server=pathlib.Path(__file__).with_name('trees-server.cjs').resolve()
wrapper=output/'codex-wrapper'
wrapper.write_text("#!/usr/bin/env python3\nimport subprocess,sys,os,json\np=subprocess.Popen(["+repr(str(codex))+",'-c','mcp_servers.trees.command=\"node\"','-c','mcp_servers.trees.args='+json.dumps(["+repr(str(server))+"]),*sys.argv[1:]],stdout=subprocess.PIPE)\nwith open(os.environ['RAW_CAPTURE'],'wb') as f:\n for line in p.stdout:\n  f.write(line);f.flush();sys.stdout.buffer.write(line);sys.stdout.buffer.flush()\nsys.exit(p.wait())\n")
wrapper.chmod(0o755)
env={**os.environ,'T3_CODEX_BIN':str(wrapper),'RAW_CAPTURE':str(output/'raw.ndjson')}
with (output/'recorder.log').open('wb') as log:
 result=subprocess.run(['node',str(checkout/'apps/server/scripts/record-codex-app-server-replay-fixture.ts'),'--scenario','simple','--out',str(output/'replay.ndjson')],cwd=workspace,env=env,stdout=log,stderr=subprocess.STDOUT)
print('Recorder exit:',result.returncode)
sys.exit(result.returncode)
