#!/usr/bin/env python3
"""Drive two real T3/Pi turns. Run after adding Pi and setting --extension <capture-final.ts> in the app.
Usage: python3 drive.py <base|head> <checkout>
The checkout must run `T3CODE_DEV_SEED=0 vp run dev` with fresh state; add a synthetic git project through `t3 project add` first.
"""
import datetime,json,pathlib,subprocess,sys
side, checkout=sys.argv[1:]
root=pathlib.Path(__file__).resolve().parent
project=root/'project'
out=root/('run-'+datetime.datetime.now(datetime.timezone.utc).strftime('%Y%m%dT%H%M%SZ')+'-'+side)
out.mkdir()
def run(args):
 return subprocess.run(args,cwd=checkout,text=True,capture_output=True)
sha=run(['git','rev-parse','HEAD']).stdout.strip()
metadata={'side':side,'sha':sha,'provider':'Pi','version':run(['pi','--version']).stdout.strip(),'model':'anthropic/claude-haiku-5-5','state':'Fresh checkout-local T3 home, synthetic project, GitHub origin; no production seed','surface':'Real T3 dev server and CLI through live MCP; paired web client','extension':'capture-final.ts loaded before the T3-injected extension'}
(out/'environment.json').write_text(json.dumps(metadata,indent=2))
for mode in ['sections','fallback']:
 message=f'[{side}-{mode}] Reply only ok. Do not use tools.'
 command=['node','apps/server/src/bin.ts','thread','start',str(project),message,'--provider','pi','--model','anthropic/claude-haiku-5-5','--wait','--json','--base-dir','.t3']
 result=run(command)
 data={'side':side,'path':mode,'message':message,'exitCode':result.returncode,'stdout':result.stdout,'stderr':result.stderr.replace(str(root),'<EVIDENCE>').replace(str(pathlib.Path.home()),'<HOME>')}
 (out/(mode+'-turn.json')).write_text(json.dumps(data,indent=2))
 print(json.dumps(data),flush=True)
 if result.returncode:sys.exit(result.returncode)
print('OUTPUT '+out.name,flush=True)
