#!/usr/bin/env python3
"""Run the identical final driver twice against each exact installed checkout."""
import datetime, json, os, pathlib, subprocess
root=pathlib.Path(__file__).resolve().parent
manifest=[]
for number in [1,2]:
    for side, checkout in [('before','/home/ubuntu/wt/pr-28078-base'),('after','/home/ubuntu/wt/pr-28078')]:
        stamp=datetime.datetime.now(datetime.timezone.utc).strftime('%Y%m%dT%H%M%S%fZ')
        name=f'{side}-run{number}-{stamp}'
        directory=root/name
        transcript=root/(name+'.jsonl')
        command=['python3',str(root/'subreaper.py'),str(directory),'node',str(root/'driver.mjs')]
        cwd=checkout+'/packages/provider-acp'
        print(json.dumps(dict(event='run_start',side=side,run=number,command=command,cwd=cwd,transcript=str(transcript))),flush=True)
        with transcript.open('w') as output:
            result=subprocess.run(command,cwd=cwd,stdout=output,stderr=subprocess.STDOUT,env={k:v for k,v in os.environ.items() if k!='PROOF_ROWS'})
        record=dict(side=side,run=number,directory=str(directory),transcript=str(transcript),exitCode=result.returncode,command=command,cwd=cwd)
        manifest.append(record)
        (root/'runs-manifest.json').write_text(json.dumps(manifest,indent=2)+'\n')
        print(json.dumps(dict(event='run_complete',**record)),flush=True)
        if result.returncode:raise SystemExit(result.returncode)
