"""Usage: python3 setup.py WORKTREE base|head; start the dev server with T3CODE_DEV_SEED=0 first.
The replay agent only supplies a normal live provider MCP credential. Production snapshot code is unchanged.
Then run python3 driver.py base|head within 120 seconds.
"""
import pathlib,sys,subprocess,json
root=pathlib.Path(__file__).parent.resolve()
worktree=pathlib.Path(sys.argv[1]).resolve();side=sys.argv[2]
p=worktree/'apps/server/scripts/dev-replay-agent.ts'
s=p.read_text()
s=s.replace('const params = message.params ?? {};','const params = message.params ?? {};\n    if (message.method === "session/new") NodeFS.writeFileSync('+json.dumps(str(root/f'{side}-credential.json'))+', JSON.stringify(params), { mode: 0o600 });')
s=s.replace('case "session/prompt": {','case "session/prompt": {\n        await new Promise(resolve => setTimeout(resolve, 120000));')
p.write_text(s)
sample=root/'sample';sample.mkdir(exist_ok=True)
subprocess.run(['git','init','-q',str(sample)],check=True)
def cli(*args):
 subprocess.run(['node','apps/server/src/bin.ts',*args,'--base-dir','.t3'],cwd=worktree,check=True)
cli('project','add',str(sample),'--title','Preview evidence')
cli('thread','start',str(sample),'Hold while preview is captured','--provider','devReplay','--model','multi-turn','--title','Preview proof','--json')
