import pathlib,json,subprocess,zipfile,re,sys
R=pathlib.Path('/tmp/lead-33036-r2/evidence-capture-2/20261110-gap');O=R/'deliverables';M=json.loads((R/'selected-manifest.json').read_text())
for row,m in M.items():
 for side in ['before','after']:
  src=pathlib.Path(m[side]['source']);z=O/f'{row}-{side}-source.zip'
  with zipfile.ZipFile(z,'w',compression=zipfile.ZIP_DEFLATED,compresslevel=4) as f:
   for p in sorted(src.iterdir()):
    if p.suffix in ['.jpg','.json','.txt'] and 'private' not in p.name:f.write(p,p.name)
  assert z.stat().st_size<100_000_000
files=sorted(p for p in O.iterdir() if p.is_file())
files += sorted(p for p in R.iterdir() if p.suffix in ['.ass','.wav'] or p.name.endswith('-narration.txt'))
scripts=['capture.mjs','seed.mjs','seed-agent.mjs','rpc-driver.mjs','pair-bootstrap.mjs','freeze-clock.mjs','assemble.py','verify-seed.py','audit-captures.py','narrate.py','review-videos.py','review-stills.py','write-gap-evidence.py','upload-gap.py','extend-driver.py','annotate-driver.py','fix-observer.py']
files += [R/n for n in scripts]+[R/'REPRODUCE-gap.md',R/'capture-audit.txt',R/'selected-manifest.json']
review=sorted(R.glob('video-review-*'))[-1];files+=sorted(review.glob('*.jpg'))
# Only these explicitly selected synthetic artifacts are public; auth and server logs are never selected.
for p in files:
 assert 'private' not in p.name and not p.name.startswith('server-'),p
 assert p.stat().st_size<100_000_000,p
(R/'upload-files-gap.json').write_text(json.dumps([str(p) for p in files],indent=2))
chunks=[]
for i in range(0,len(files),25):
 out=subprocess.run(['ghdrop','upload','-m',*[str(p) for p in files[i:i+25]]],capture_output=True,text=True,check=True)
 chunks.append(out.stdout);print(f'Uploaded {min(i+25,len(files))}/{len(files)}',flush=True)
(R/'uploads-gap.md').write_text('\n'.join(chunks))
index=R/'artifact-index.md';index.write_text('# Gap capture artifacts\n\nSynthetic data only. AI-assisted; verified with the tests and tools listed above.\n\n'+(R/'uploads-gap.md').read_text())
out=subprocess.run(['ghdrop','upload','-m',str(index)],capture_output=True,text=True,check=True)
with (R/'uploads-gap.md').open('a') as f:f.write('\n'+out.stdout)
print(out.stdout,flush=True)
