#!/usr/bin/env python3
import json,re,pathlib
root=pathlib.Path('/tmp/lead-25991-r1/evidence-capture')
ui=root/'run-20261027T160500Z';cli=root/'run-20261027T160706Z';retry=root/'run-20261027T161012Z'
out=root/'delivery';out.mkdir(exist_ok=True)
def redact(s):
 s='\n'.join(l for l in s.splitlines() if not any(0x2580<=ord(c)<=0x259f for c in l))
 s=re.sub(r'token=[^\s"\']+','token=[REDACTED]',s)
 s=re.sub(r'(Token:|Pairing code:)\s*\S+',r'\1 [REDACTED]',s)
 s=re.sub(r'eyJ[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+','[REDACTED_SESSION]',s)
 return s+'\n'
def bundle(name,files):
 parts=['Synthetic Linux network fixture. No native Windows/macOS claim. QR encodings omitted; credentials redacted.\n']
 for p in files:
  if not p.exists():raise FileNotFoundError(p)
  parts+=['\n===== '+str(p.relative_to(root))+' =====\n',redact(p.read_text())]
 (out/name).write_text(''.join(parts))
for row in ['R05','R06','R07','R08']:
 bundle(row+'-transcript.txt',[cli/f'{row}-{rev}.{kind}.raw.log' for rev in ['base','head'] for kind in ['serve','rpc']])
bundle('R04-transcript.txt',[p/f'{row}-{rev}.{kind}.raw.log' for row,p in [('R04-vpn18',retry),('R04-vpn19',retry),('R04-lan',cli)] for rev in ['base','head'] for kind in ['serve','rpc']])
bundle('R01-transcript.txt',[root/'r01-base.raw.log',root/'r01-base-rpc.raw.log',ui/'R01-head.raw.log',ui/'R01-head-rpc.raw.log'])
u2=root/'run-20261027T161900Z';u3=root/'run-20261027T161700Z'
bundle('R02-transcript.txt',[u2/'R02-base.raw.log',u2/'R02-base-rpc.raw.log',u2/'R02-control-base.raw.log',u2/'R02-upgrade-head.raw.log',u2/'R02-head-rpc.raw.log',u2/'R02-catalog.json'])
bundle('R03-transcript.txt',[root/'run-20261027T161400Z/R03-base-recapture.raw.log',ui/'R03-base-rpc.raw.log',u3/'R03-control-base.raw.log',u3/'R03-upgrade-head.raw.log',ui/'R03-head-rpc.raw.log',u3/'R03-catalog.json'])
(out/'unit-tests.log').write_text((root/'unit-tests.log').read_text())
for p in out.glob('*.txt'):
 assert not re.search(r'token=(?!\[REDACTED\])\w+',p.read_text()),p
 assert not re.search(r'Token:\s+(?!\[REDACTED\])\w+',p.read_text()),p
print('wrote redacted transcripts to',out)
