"""Drive real running T3 servers. Usage: python3 run-proof.py BASE_WORKTREE HEAD_WORKTREE OUTPUT_DIR"""
from pathlib import Path
import sys,subprocess,json,datetime
base,head,out=map(Path,sys.argv[1:]);out=out/datetime.datetime.now(datetime.timezone.utc).strftime('%Y%m%dT%H%M%SZ');out.mkdir(parents=True)
project=out/'project';project.mkdir();(project/'README.md').write_text('Synthetic Pi alias proof workspace.\n')
probe='ALIAS_PROTOCOL_PROBE: Call mcp__t3_code__t3_thread_list with {"limit":1,"titleContains":"__no_match_proof__"}. Do not use any other tool or delegate. After the tool succeeds, reply ALIAS_OK.'
models=json.loads((Path(__file__).parent/'models-template.json').read_text())
manifest={}
for side,wt in [('before',base),('after',head)]:
 agent=out/side/'agent';agent.mkdir(parents=True);(agent/'models.json').write_text(json.dumps(models));(agent/'settings.json').write_text(json.dumps({'packages':[],'checkForUpdates':False}))
 subprocess.run(['node',str(Path(__file__).parent/'configure.cjs'),str(wt),str(agent)],check=True)
 def cli(*args):
  result=subprocess.run(['node','apps/server/src/bin.ts',*args,'--base-dir',str(wt/'.t3')],cwd=wt,text=True,capture_output=True,check=True)
  return result.stdout
 cli('project','add',str(project),'--title','Pi alias proof')
 for kind,model in [('context','localcompat/gpt-6-luna'),('deferred','localmessages/claude-haiku-4-5-20251001')]:
  initial=json.loads(cli('thread','start',str(project),'Reply with just the word ok.','--title','Pi alias proof '+kind,'--provider','pi','--model',model,'--wait','--json'))
  (out/(side+'-'+kind+'-initial.json')).write_text(json.dumps(initial,indent=2))
  # To record live: open initial['threadId'] in the paired web app before sending this turn.
  result=json.loads(cli('thread','send',initial['threadId'],probe,'--wait','--json'))
  (out/(side+'-'+kind+'-result.json')).write_text(json.dumps(result,indent=2));manifest[side+'-'+kind]=initial['threadId']
(out/'threads.json').write_text(json.dumps(manifest,indent=2));print(json.dumps(manifest,indent=2))
