"""Real server driver. Usage: python3 run-comparison.py CHECKOUT OUTPUT SIDE.
Requires evidence-hook.ts and the instrumented Orchestrator/EffectWorker in CHECKOUT.
Runs only free replay agents. Fresh synthetic data; never reads installed T3 state.
"""
import sys,os,json,pathlib,subprocess,time,urllib.request,urllib.error,uuid,statistics,signal
root=pathlib.Path(sys.argv[1]); output=pathlib.Path(sys.argv[2]); side=sys.argv[3]
run_dir=output/(side+'-'+time.strftime('%Y%m%d-%H%M%S'));run_dir.mkdir(parents=True)
home=run_dir/'home';(home/'userdata').mkdir(parents=True)
settings={'defaultModelSelection':{'instanceId':'devReplay','model':'multi-turn'},'providerInstances':{x:{'driver':x,'enabled':False} for x in ['codex','claudeAgent','cursor','grok','muse','pi','opencode','antigravity']}}
(home/'userdata/settings.json').write_text(json.dumps(settings))
workspace=run_dir/'workspace';workspace.mkdir();
subprocess.run(['git','init','--quiet','--initial-branch=main',str(workspace)],check=True)
subprocess.run(['git','-C',str(workspace),'-c','user.name=Evidence','-c','user.email=evidence@example.invalid','commit','--allow-empty','--quiet','-m','Synthetic workspace'],check=True)
port=16880; hook=16881; raw=run_dir/'measurements.jsonl'
env={**os.environ,'T3CODE_DEV_SEED':'0','EVIDENCE_PORT':str(hook),'EVIDENCE_LOG':str(raw)}
log=open(run_dir/'private-server.log','w')
recording=open(run_dir/'terminal.cast','w');recording.write(json.dumps({'version':2,'width':120,'height':32,'timestamp':int(time.time()),'env':{'TERM':'xterm-256color'}})+'\n');record_start=time.monotonic()
def record(text):
 recording.write(json.dumps([time.monotonic()-record_start,'o',text+'\r\n'])+'\n');recording.flush()
record('Real T3 server checkpoint capture | '+side+' | Linux x86_64 | free ACP replay')
record('Fresh file-backed SQLite; synthetic workspace; parent waiting for approval.')
record('SQL observer '+('OFF (zero means not counted)' if os.environ.get('EVIDENCE_COUNT_SQL')=='0' else 'ON')+'; wake=False means an existing delivery is already queued.')
server=subprocess.Popen(['node','apps/server/src/bin.ts','serve','--base-dir',str(home),'--port',str(port),'--host','127.0.0.1','--dev-url','http://127.0.0.1:16882'],cwd=root,env=env,stdout=log,stderr=log)
def rpc(**body):
 req=urllib.request.Request(f'http://127.0.0.1:{hook}',data=json.dumps(body).encode(),headers={'Content-Type':'application/json'})
 try:return json.load(urllib.request.urlopen(req,timeout=120))
 except urllib.error.HTTPError as e:raise RuntimeError(e.read().decode())
def dispatch(**c):return rpc(op='dispatch',command={'commandId':str(uuid.uuid4()),**c})
def wait_until(fn,description,timeout=90):
 end=time.monotonic()+timeout
 while time.monotonic()<end:
  if server.poll() is not None: raise RuntimeError('Server exited')
  try:
   result=fn()
   if result:return result
  except (ConnectionError,urllib.error.URLError):pass
  time.sleep(.1)
 raise RuntimeError('Timed out: '+description)
def records():return [json.loads(l) for l in raw.read_text().splitlines()] if raw.exists() else []
def state(thread):return rpc(op='read',threadId=thread)
def summary(p):
 return {'runs':len(p['runs']),'completedCheckpoints':sum(r['status']=='completed' and bool(r['checkpointId']) for r in p['runs']),'openDeliveries':sum((r.get('delegatedCompletion') or {}).get('disposition')=='open' for r in p['runs']),'tasks':p['subagents'],'pendingApprovals':sum(r['status']=='pending' for r in p['requests'])}
try:
 # The fixture endpoint starts during construction; project CLI also waits for command readiness.
 wait_until(lambda:rpc(op='target',threadId='not-yet-created'),'server fixture endpoint')
 added=subprocess.run(['node','apps/server/src/bin.ts','project','add',str(workspace),'--title','Evidence','--base-dir',str(home)],cwd=root,check=True,capture_output=True,text=True).stdout
 project=added.split('Added project ')[1].split(' ')[0]
 dispatch(type='thread.create',threadId='evidence-parent',projectId=project,title='Checkpoint recovery evidence',modelSelection={'instanceId':'devReplay','model':'approval'},runtimeMode='approval-required',interactionMode='default',branch=None,worktreePath=None,createdBy='user',creationSource='web')
 dispatch(type='message.dispatch',threadId='evidence-parent',messageId='evidence-initial-message',text='Wait for approval while delegated work completes.',attachments=[],dispatchMode={'type':'start_immediately'},createdBy='user',creationSource='web')
 parent=wait_until(lambda: (p if any(q['status']=='pending' for q in p['requests']) else None) if (p:=state('evidence-parent')) else None,'parent pending approval')
 owner=parent['runs'][0]
 # Create one app-owned child; real ACP replay subprocess, provider ingestion, and worker.
 dispatch(type='delegated_task.request',parentThreadId='evidence-parent',parentRunId=owner['id'],parentNodeId=owner['rootNodeId'],task='Complete the replay fixture work.',completionWake='always',modelSelection={'instanceId':'devReplay','model':'multi-turn'},runtimeMode='approval-required',interactionMode='default',createdBy='agent',creationSource='mcp')
 child=state('evidence-parent')['subagents'][0]['childThreadId']
 rpc(op='target',threadId=child,phase='quiet')
 wait_until(lambda:any(r.get('event')=='checkpoint-capture' and r.get('threadId')==child for r in records()),'initial child checkpoint')
 # Each further child turn is a real message and real git checkpoint, no direct recovery call.
 def turn(phase,i):
  rpc(op='target',threadId=child,phase=phase)
  event_start=len(records())
  before={r['runId'] for r in records() if r['event']=='checkpoint-capture' and r['threadId']==child}
  dispatch(type='message.dispatch',threadId=child,messageId=str(uuid.uuid4()),text=f'Complete fixture follow-up {phase} {i}.',attachments=[],dispatchMode={'type':'start_immediately'},createdBy='user',creationSource='web')
  capture=wait_until(lambda:next((r for r in records() if r['event']=='checkpoint-capture' and r['threadId']==child and r['runId'] not in before),None),'child checkpoint '+phase)
  recovery=next(r for r in records() if r['event']=='checkpoint-recovery' and r['completedRun']==capture['runId'])
  p=state(child);r=next(r for r in p['runs'] if r['id']==capture['runId'])
  par=state('evidence-parent');tasks=[t for t in par['subagents'] if t['childThreadId']==child]
  assert r['status']=='completed' and r['checkpointId'], 'Checkpoint not persisted'
  assert tasks[-1]['status']=='completed' and tasks[-1]['result'], 'Completion result missing'
  own_offers=[r for r in records()[event_start:] if r['event']=='wake-offered' and r['parentRunId']==owner['id']]
  sample={'ownWakeReoffered':bool(own_offers),'phase':phase,'iteration':i,'checkpointMs':capture['ms'],'recoveryMs':recovery['ms'],'statements':recovery['statements'],'parentRunReads':recovery['parentRunReads'],'checkpointPersisted':True,'taskCompleted':True,'taskResult':tasks[-1]['result'],'deliveryState':tasks[-1].get('completionDelivery')}
  print(json.dumps(sample),flush=True)
  record(f"{phase:13} {i+1:02d} | capture {capture['ms']:8.2f} ms | recovery {recovery['ms']:8.2f} ms | SQL {recovery['statements']:4d} | checkpoint+result OK | wake {bool(own_offers)}")
  return sample
 samples=[]
 for i in range(3):turn('quiet-warmup',i)
 for i in range(11):samples.append(turn('quiet',i))
 rpc(op='seed',count=1200)
 for i in range(3):turn('busy-warmup',i)
 for i in range(11):samples.append(turn('busy',i))
 final=summary(state('evidence-parent')); child_final=summary(state(child))
 result={'side':side,'sha':subprocess.check_output(['git','rev-parse','HEAD'],cwd=root,text=True).strip(),'platform':'Linux x86_64; Node '+subprocess.check_output(['node','--version'],text=True).strip(),'repetitions':11,'sqlObserver':os.environ.get('EVIDENCE_COUNT_SQL')!='0','syntheticOpenDeliveries':1200,'samples':samples,'parentFinal':final,'childFinal':child_final}
 for phase in ['quiet','busy']:
  result[phase]={}
  for metric in ['checkpointMs','recoveryMs','statements','parentRunReads']:
   values=[x[metric] for x in samples if x['phase']==phase];result[phase][metric]={'median':statistics.median(values),'min':min(values),'max':max(values),'spread':max(values)-min(values)}
 record('Finished: 11 measured turns per load after 3 warm-ups. Both checkpoint and result persisted.')
 for phase in ['quiet','busy']: record(phase+' median capture '+str(round(result[phase]['checkpointMs']['median'],2))+' ms; recovery '+str(round(result[phase]['recoveryMs']['median'],2))+' ms')
 (run_dir/'result.json').write_text(json.dumps(result,indent=2));print('RESULT',run_dir/'result.json',flush=True)
finally:
 server.terminate()
 try:server.wait(timeout=15)
 except subprocess.TimeoutExpired:server.kill();server.wait()
 log.close();recording.close()
