"""Usage: python3 reproduce.py BASE_CHECKOUT HEAD_CHECKOUT OUTPUT_DIRECTORY
Install each checkout with vp i first. Copies driver.ts beside this script into
apps/server/evidence-driver.ts. Runs real server service code, not a test runner.
"""
import subprocess,os,time,pathlib,sys,shutil
base,head,out=map(lambda s:pathlib.Path(s).resolve(),sys.argv[1:])
run=out/('run-'+time.strftime('%Y%m%dT%H%M%S'));run.mkdir(parents=True)
env=dict(os.environ,EVIDENCE_DIR=str(run/'data'),T3CODE_DEV_SEED='0')
for checkout in [base,head]:
 shutil.copyfile(pathlib.Path(__file__).with_name('driver.ts'),checkout/'apps/server/evidence-driver.ts')
for round in range(1,7):
 for side in (['base','head'] if round%2 else ['head','base']):
  cwd=({'base':base,'head':head}[side])/'apps/server'
  with open(run/f'{side}-{round}.txt','w') as log:
   subprocess.run(['node','evidence-driver.ts',str(run/f'{side}-{round}.json'),str(round)],cwd=cwd,env=env,stdout=log,stderr=subprocess.STDOUT,check=True)
  print(f'{side} round {round} complete',flush=True)
print('Run summarize.py with the resulting run directory to verify hashes and compare medians.')
