import json, statistics, re
from pathlib import Path
from PIL import Image, ImageDraw, ImageFont
root=Path(__file__).parent
rows=json.loads((root/'results.json').read_text())
env='Windows Server 2025 | 4 vCPU | 15.9 GiB RAM\nNode 26.13.1 | Vite+ 1.4.0 | effect-tsgo 0.67.1'
for side,sha in [('base','35b88636d431dafdc84820621f4073aaa178ca0b'),('head','49718059fed03a9b5a4c9d2f72cd3368215f715e')]:
 data=[r for r in rows if r['side']==side]
 full=env+'\nCommit: '+sha+'\n\n'
 for r in data:
  full+=f"=== {r['phase']} {r['iteration']}: {r['seconds']:.3f} s; exit {r['exit']} ===\n"+r['output']+'\n'
 (root/f'{side}-output.txt').write_text(full)
 body=[r['seconds'] for r in data if r['phase']=='body-edit' and r['iteration']>=3]
 unchanged=[r['seconds'] for r in data if r['phase']=='unchanged']
 text=[f"{'BEFORE' if side=='base' else 'AFTER'} | {sha[:10]}",*env.splitlines(),'', 'apps/server > vp run typecheck','', 'Captured timings (seconds)                 exit']
 for r in data:
  text.append(f"{r['phase']+' '+str(r['iteration']):<33} {r['seconds']:7.3f}    {r['exit']}")
 text+=['',f'Warm body edits 3-8: median {statistics.median(body):.3f} s',f'Range {min(body):.3f}-{max(body):.3f} s; spread {max(body)-min(body):.3f} s', f'Unchanged: median {statistics.median(unchanged):.3f} s','', 'Output excerpts (full output supplied separately):']
 first=next(r for r in data if r['phase']=='unchanged')['output'].splitlines()[0]
 text+=['effect-tsgo tsrs --noEmit'+(' --incremental' if side=='head' else ''),'cache hit, replaying' if 'cache hit' in first else 'cache disabled', '', 'Injected error: normal task and clean compiler:', "doctor.ts(29,3): error TS2322", "Type 'number' is not assignable to type 'string'.",'Both exit 2. Restored source exits 0.', '', 'Warmup and edits 1-2 excluded from warm median.', 'Nonfatal Effect suggestions retained in full logs.']
 font=ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf',18)
 im=Image.new('RGB',(1000,len(text)*28+48),'#111827'); d=ImageDraw.Draw(im)
 for i,line in enumerate(text): d.text((24,24+i*28),line,font=font,fill='#e5e7eb')
 im.save(root/f'{side}-capture.png')
print('Warm improvement',1-3.26/15.638)
for p in root.iterdir():
 if p.suffix in ['.txt','.json']:
  s=p.read_text()
  bad=re.findall(r'(?:C:\\\\Users|C:\\Users|100\.95\.254\.10|t3admin|/home/user)',s)
  print(p.name,'private-path matches',len(bad))
