#!/usr/bin/env python3
"""Real T3 replay-turn performance proof. Requires node, vp, git, ffmpeg, Pillow.
Usage: python3 prove.py --base-checkout PATH --head-checkout PATH --output PATH
Each checkout must already have `vp i`. Output must be a new directory.
No provider credentials or existing T3 data are used.
"""
import argparse, concurrent.futures, hashlib, json, os, pathlib, platform, shutil, signal, socket, statistics, subprocess, time, urllib.request
P=pathlib.Path
NAMES=['GitVcsDriver.checkpoints.captureCheckpoint','orchestrationV2.checkpointCapture.execute','RunFinalizationService.finalize']
def command(args,cwd=None,env=None):
    p=subprocess.run(args,cwd=cwd,env=env,text=True,capture_output=True)
    if p.returncode: raise RuntimeError(f'{args[:3]} exited {p.returncode}: {p.stderr[-3000:]} {p.stdout[-1000:]}')
    return p.stdout.strip()
def git(repo,*args): return command(['git',*args],cwd=repo)
def seed(repo):
    repo.mkdir(); (repo/'tracked').mkdir(); (repo/'untracked').mkdir()
    for i in range(5000): (repo/'tracked'/f'{i:04}.txt').write_text(f'synthetic tracked file {i}\n')
    git(repo,'init','-q','-b','main'); git(repo,'add','tracked')
    git(repo,'-c','user.name=Evidence','-c','user.email=evidence@example.invalid','commit','-qm','Synthetic baseline')
    # Unique deterministic pseudo-random bytes: no compression/dedup shortcut.
    for i in range(2000): (repo/'untracked'/f'{i:04}.bin').write_bytes(hashlib.shake_256(f'checkpoint-proof-{i}'.encode()).digest(1024*1024))
def summary(values):
    return dict(n=len(values),median=statistics.median(values),min=min(values),max=max(values),spread=max(values)-min(values))
def run_side(side,checkout,root,template):
    out=root/side; out.mkdir(); repo=out/'workspace'; shutil.copytree(template,repo)
    # Prime Git's object store equally on both sides; initial compression is not the reported repeated hashing.
    prime_env=dict(os.environ,GIT_INDEX_FILE=str(repo/'.git/proof-priming-index'))
    command(['git','add','-A'],cwd=repo,env=prime_env)
    home=out/'home'; (home/'userdata').mkdir(parents=True)
    nomodel=out/'no-model'; nomodel.write_text('#!/bin/sh\nexit 1\n'); nomodel.chmod(0o755)
    drivers=['codex','claudeAgent','cursor','grok','muse','pi','opencode','antigravity']
    settings={'defaultModelSelection':{'instanceId':'devReplay','model':'showcase'},'providerInstances':{d:{'driver':d,'enabled':False,**({'config':{'binaryPath':str(nomodel)}} if d=='codex' else {})} for d in drivers}}
    (home/'userdata'/'settings.json').write_text(json.dumps(settings))
    env=dict(os.environ,T3CODE_DEV_SEED='0',T3CODE_AUTO_BOOTSTRAP_PROJECT_FROM_CWD='0')
    # Do not inherit baked web origins or a shared T3 home.
    for key in ['T3CODE_HOME','VITE_HTTP_URL','VITE_WS_URL']: env.pop(key,None)
    with socket.socket() as s: s.bind(('127.0.0.1',0)); port=s.getsockname()[1]
    rawlog=(out/'private-server.log').open('w')
    server=subprocess.Popen(['node','scripts/dev-runner.ts','dev:server','--home-dir',str(home),'--no-seed','--port',str(port)],cwd=checkout,env=env,stdout=rawlog,stderr=subprocess.STDOUT,process_group=0)
    start=time.monotonic(); events=[]
    sha=git(checkout,'rev-parse','HEAD')
    def emit(message):
        event={'seconds':round(time.monotonic()-start,3),'text':message}; events.append(event)
        (out/'transcript.json').write_text(json.dumps(events,indent=2)); print(side, event,flush=True)
    def cli(*args): return command(['node','apps/server/src/bin.ts',*args,'--base-dir',str(home)],cwd=checkout,env=env)
    trace=home/'userdata'/'logs'/'server.trace.ndjson'
    try:
        for _ in range(180):
            if server.poll() is not None: raise RuntimeError('Server exited: '+(out/'private-server.log').read_text()[-5000:])
            try:
                urllib.request.urlopen(f'http://127.0.0.1:{port}/.well-known/t3/environment',timeout=1); break
            except Exception: time.sleep(1)
        else: raise RuntimeError('Server not ready')
        cli('project','add',str(repo),'--title','Checkpoint timing proof')
        emit(f'{side.upper()} {sha[:10]} | Linux, Git 2.43, 8 CPUs')
        emit('5,000 committed files + 2,000 untracked 1 MiB files (2,000 MiB)')
        index_before=hashlib.sha256((repo/'.git/index').read_bytes()).hexdigest()
        def spans():
            if not trace.exists():
                found=list(home.rglob('server.trace.ndjson'))
                if not found: return []
                target=found[0]
            else: target=trace
            # Oldest rotation first, then current, so offsets survive 10 MiB rotation.
            files=sorted(target.parent.glob(target.name+'.[0-9]*'),key=lambda p:int(p.suffix[1:]),reverse=True)+[target]
            return [json.loads(line) for file in files for line in file.read_text().splitlines() if line.strip()]
        def turn(label):
            t=time.monotonic()
            result=json.loads(cli('thread','start','--provider','devReplay','--model','showcase','--wait','--json',str(repo),f'Checkpoint proof {label}','--title',label))
            return {'label':label,'wallSeconds':time.monotonic()-t,'result':result}
        groups=[]
        emit('First capture (Git objects preseeded): start one real replay turn; wait for finalization')
        before=len(spans()); cold=turn('cold'); elapsed=cold['wallSeconds']
        groups.append({'phase':'cold','turns':[cold],'wallSeconds':elapsed,'spans':spans()[before:]})
        emit(f'Cold turn complete: {elapsed:.3f}s')
        for i in range(6):
            emit(f'Sequential {i+1}/6: t3 thread start --provider devReplay --model showcase --wait')
            before=len(spans()); row=turn(f'sequential-{i+1}')
            ss=spans()[before:]; groups.append({'phase':'sequential','turns':[row],'wallSeconds':row['wallSeconds'],'spans':ss})
            captures=[r['durationMs']/1000 for r in ss if r.get('name')==NAMES[0]]
            emit(f'Completed: wall {row["wallSeconds"]:.3f}s; captures '+', '.join(f'{x:.3f}s' for x in captures))
        for i in range(3):
            emit(f'Parallel batch {i+1}/3: start FOUR replay threads in the SAME workspace')
            before=len(spans()); t=time.monotonic()
            with concurrent.futures.ThreadPoolExecutor(max_workers=4) as pool:
                rows=list(pool.map(turn,[f'parallel-{i+1}-{j+1}' for j in range(4)]))
            elapsed=time.monotonic()-t; ss=spans()[before:]
            groups.append({'phase':'parallel','turns':rows,'wallSeconds':elapsed,'spans':ss})
            finals=[r['durationMs']/1000 for r in ss if r.get('name')==NAMES[2]]
            emit(f'All four complete: {elapsed:.3f}s; finalize '+', '.join(f'{x:.3f}s' for x in finals))
        # Inspect real checkpoint objects, independently of the server cache.
        def check_tree():
            refs=git(repo,'for-each-ref','--format=%(refname)','refs/t3').splitlines()
            trees=sorted(set(git(repo,'rev-parse',ref+'^{tree}') for ref in refs))
            entries={}
            for d in ['tracked','untracked']:
                for file in sorted((repo/d).iterdir()):
                    entries[str(file.relative_to(repo))]=git(repo,'hash-object',str(file))
            expected='\n'.join(f'{k} {v}' for k,v in sorted(entries.items()))
            expected_digest=hashlib.sha256(expected.encode()).hexdigest()
            mismatches=[]
            for tree in trees:
                actual={}
                for line in git(repo,'ls-tree','-r',tree).splitlines():
                    metadata,name=line.split('\t',1); actual[name]=metadata.split()[2]
                if actual!=entries: mismatches.append(tree)
            return {'refs':len(refs),'trees':trees,'entries':len(entries),'expectedManifestSha256':expected_digest,'mismatchingTrees':mismatches}
        if sum(r.get('name')==NAMES[2] and r.get('exit',{}).get('_tag')=='Success' for g in groups for r in g['spans'])!=19: raise RuntimeError('Not every turn finalized successfully')
        emit('Verify every checkpoint tree against independently hashed workspace files')
        before_check=check_tree()
        if before_check['mismatchingTrees'] or before_check['refs']<19: raise RuntimeError(f'Checkpoint mismatch: {before_check}')
        if index_before!=hashlib.sha256((repo/'.git/index').read_bytes()).hexdigest(): raise RuntimeError('Real index changed')
        emit(f'PASS: all {before_check["refs"]} existing refs match 7,000 blobs; real index unchanged')
        # Follow-up content invalidation, with both unchanged and changed untracked files.
        oldrefs=set(git(repo,'for-each-ref','--format=%(refname)','refs/t3').splitlines())
        changed=repo/'untracked/0000.bin'; changed.write_bytes(b'X'+changed.read_bytes()[1:])
        (repo/'untracked/0001.bin').unlink(); (repo/'untracked/added.txt').write_text('new untracked content\n')
        (repo/'tracked/0000.txt').write_text('changed tracked content\n')
        emit('Correctness: same-size untracked edit, untracked add/delete, tracked edit; run another turn')
        changed_turn=turn('changed-files')
        newrefs=set(git(repo,'for-each-ref','--format=%(refname)','refs/t3').splitlines())-oldrefs
        checks={}
        for ref in sorted(newrefs):
            checks[ref]={
                'sameSizeEdit':git(repo,'rev-parse',ref+':untracked/0000.bin')==git(repo,'hash-object',str(changed)),
                'added':git(repo,'show',ref+':untracked/added.txt')=='new untracked content',
                'deleted':'untracked/0001.bin' not in git(repo,'ls-tree','-r','--name-only',ref).splitlines(),
                'trackedEdit':git(repo,'show',ref+':tracked/0000.txt')=='changed tracked content',
                'unchanged':git(repo,'rev-parse',ref+':untracked/0002.bin')==git(repo,'hash-object',str(repo/'untracked/0002.bin'))}
        if len(checks)!=2 or not all(all(x.values()) for x in checks.values()): raise RuntimeError(f'Changed tree mismatch: {checks}')
        emit('PASS: both changed-file checkpoints contain edits/addition, omit deletion, preserve unchanged blob')
        stats={}
        for phase in ['cold','sequential','parallel']:
            selected=[x for x in groups if x['phase']==phase]
            stats[phase]={'wall':summary([x['wallSeconds'] for x in selected])}
            for name in NAMES:
                values=[r['durationMs']/1000 for g in selected for r in g['spans'] if r.get('name')==name and r.get('exit',{}).get('_tag')=='Success']
                if values: stats[phase][name]=summary(values)
        emit(f'Sequential capture median: {stats["sequential"][NAMES[0]]["median"]:.3f}s')
        emit(f'Parallel batch median: {stats["parallel"]["wall"]["median"]:.3f}s')
        # Only names, durations, public IDs: no raw attributes or server log are published.
        clean_groups=[{**g,'spans':[{k:r[k] for k in ['name','durationMs','traceId','spanId','startTimeUnixNano','endTimeUnixNano','exit'] if k in r} for r in g['spans'] if r.get('name') in NAMES],'turns':[{k:r[k] for k in ['label','wallSeconds']} for r in g['turns']]} for g in groups]
        result={'side':side,'sha':sha,'environment':{'os':platform.system(),'architecture':platform.machine(),'git':command(['git','--version']),'cpus':os.cpu_count(),'node':command(['node','--version'])},'interruptedSpans':sum(r.get('exit',{}).get('_tag')!='Success' for g in groups for r in g['spans'] if r.get('name') in NAMES),'stats':stats,'groups':clean_groups,'correctness':before_check,'changedFileChecks':list(checks.values()),'changedTurnWallSeconds':changed_turn['wallSeconds']}
        
        for group in clean_groups:
            for span in group['spans']:
                span['exit']={'_tag':span.get('exit',{}).get('_tag')}
                # Interrupted baselines are reported separately, never mixed into successful timing samples.
        (out/'measurement.json').write_text(json.dumps(result,indent=2)); return result
    finally:
        os.killpg(server.pid,signal.SIGTERM)
        try: server.wait(timeout=15)
        except subprocess.TimeoutExpired: os.killpg(server.pid,signal.SIGKILL); server.wait()
        rawlog.close()
def main():
    parser=argparse.ArgumentParser(); parser.add_argument('--base-checkout',required=True); parser.add_argument('--head-checkout',required=True); parser.add_argument('--output',required=True); args=parser.parse_args()
    root=P(args.output).resolve(); root.mkdir(); template=root/'template'; seed(template)
    results=[]
    for side,checkout in [('before',args.base_checkout),('after',args.head_checkout)]: results.append(run_side(side,str(P(checkout).resolve()),root,template))
    (root/'comparison.json').write_text(json.dumps(results,indent=2))
if __name__=='__main__': main()
