from pathlib import Path
source=Path('/tmp/codex/ltb1021/2026-09-26_provider-scale_r01/finish.py').read_text()
source=source.replace('image-d086494ab6.json','image-334fde3791.json')
source=source.replace("assert not inspect('contextra-ai-gateway-1')['State']['Running']", """trial=json.loads((HERE/'pose'/'trial-result.json').read_text())
assert trial['cleanup']['reaped'] and not trial['errors']
assert all(v['state']=='stopped' and not v['cleanup_error'] for v in trial['cleanup']['states'].values())
assert all(not p['last_cleanup']['forced'] for v in trial['cleanup']['states'].values() for p in v['providers'].values())
assert not inspect('contextra-ai-gateway-1')['State']['Running']
cmd(['docker','start','contextra-ai-gateway-1'],timeout=20)
time.sleep(5)""")
source=source.replace("bridge=json.loads(cmd(['docker','exec','contextra-ai-control-sidecar-1','python3','-B','-c',code],timeout=6))", """deadline=time.monotonic()+30
while True:
 bridge=json.loads(cmd(['docker','exec','contextra-ai-control-sidecar-1','python3','-B','-c',code],timeout=6))
 if bridge['wss']['connected'] or time.monotonic()>deadline:break
 time.sleep(2)
rows=api('/api/v1/streams')['streams']""")
source=source.replace("stopped=len(rows),provider_pids=[]", "stopped=sum(r['state']=='stopped' for r in rows),provider_pids=[r.get('child_pid') for r in rows if r.get('child_pid')]")
source=source.replace("gateway='stopped'", "gateway='running'")
source=source.replace('high-load startup stall and forced cleanup; inbound Start held','startup Stop canary and trial cleanup completed; manual Web acceptance pending')
source=source.replace('compose_only_image_changed=True', 'compose_only_image_changed=True, freshness_enabled=True, startup_seconds=10, steady_stall_seconds=2, debug_jpeg_enabled=False')
exec(compile(source,__file__,'exec'))
