import json,html,textwrap,subprocess,sys
from pathlib import Path
root=Path(sys.argv[1])
for side,label in [('base','Before'),('head','After')]:
 d=json.loads((root/f'{side}.json').read_text())
 lines=[f'{label} | MCP documentation and live server transcript',f'Commit: {d["sha"]}', 'Environment: Linux / HTTP MCP / fresh isolated data / synthetic requests', '', 'REFERENCE DOCUMENT: t3_thread_update', *d['document'].split('\n'),'', 'LIVE tools/list:', 't3_thread_update action enum: '+json.dumps(d['registeredTool']['inputSchema']['properties']['action']['enum']), 'Separate registered tools: '+', '.join(x['name'] for x in d['separateTools']),'','LIVE tools/call:']
 for c in d['calls']:
  lines+=['> t3_thread_update '+json.dumps(c['arguments']),f'< JSON-RPC error {c["response"]["error"]["code"]}:',*c['response']['error']['message'].split('\n'),'']
 lines=[z for l in lines for z in (textwrap.wrap(l,width=110,replace_whitespace=False) or [''])]
 (root/f'{side}.txt').write_text('\n'.join(lines))
 height=60+len(lines)*23
 svg=f'<svg xmlns="http://www.w3.org/2000/svg" width="1500" height="{height}"><rect width="100%" height="100%" fill="#111827"/>'
 for i,l in enumerate(lines):
  color='#93c5fd' if i<3 else '#e5e7eb'
  svg+=f'<text x="30" y="{40+i*23}" fill="{color}" font-family="DejaVu Sans Mono" font-size="19">{html.escape(l)}</text>'
 svg+='</svg>'
 (root/f'{side}.svg').write_text(svg)
 subprocess.run(['convert',str(root/f'{side}.svg'),str(root/f'{side}.png')],check=True)
