"""Run from each checkout after T3CODE_DEV_SEED=0 vp run dev.
Usage: python run.py CHECKOUT SIDE
The same scripted ACP agent and seed are used on both sides; no checkpoint data is injected.
"""
import datetime, json, pathlib, subprocess, sys, time, urllib.request
checkout=pathlib.Path(sys.argv[1]).resolve(); side=sys.argv[2]
root=pathlib.Path(__file__).resolve().parent
run=root/(side+'-'+datetime.datetime.now(datetime.timezone.utc).strftime('%Y%m%dT%H%M%S%fZ'));run.mkdir()
fixture=run/'fixture'
subprocess.run([sys.executable,str(root/'seed.py'),str(fixture)],check=True)
settings_path=checkout/'.t3/userdata/settings.json'
settings=json.loads(settings_path.read_text())
settings['providerInstances']['checkpointEvidence']={'driver':'acpRegistry','displayName':'Checkpoint demo','config':{'source':'local','commandPath':'node','commandArgs':[str(root/'scripted-agent.mjs')]}}
settings_path.write_text(json.dumps(settings,indent=2))
command=['node','apps/server/src/bin.ts']
subprocess.run(command+['project','add','--base-dir','.t3','--title','Checkpoint evidence',str(fixture)],cwd=checkout,check=True,stdout=subprocess.DEVNULL)
prompt='Create a three-line created.txt, delete doomed.txt, rename old-name.txt to new-name.txt using git mv, and rewrite image.bin in one turn.'
turn=subprocess.run(command+['thread','start','--base-dir','.t3','--provider','checkpointEvidence','--model','scripted','--title','File checkpoint proof',str(fixture),prompt,'--json'],cwd=checkout,check=True,capture_output=True,text=True)
result=json.loads(turn.stdout);(run/'turn.json').write_text(json.dumps(result,indent=2))
runtime=json.loads((checkout/'.t3/userdata/server-runtime.json').read_text())
env=json.load(urllib.request.urlopen(runtime['origin']+'/.well-known/t3/environment'))
url=runtime['devUrl'].rstrip('/')+'/'+env['environmentId']+'/'+urllib.parse.quote(result['threadId'],safe='')
print(json.dumps({'run':str(run),'threadId':result['threadId'],'url':url}),flush=True)
# After opening the printed URL and starting the recording, release the script with:
# python -c "from pathlib import Path; Path('RUN/fixture/.git/evidence-release').touch()"
# Wait for the real app's Changed Files card, then capture the stable UI.
