from ops import *
HERE.chmod(0o700);PRIVATE.mkdir(mode=0o700)
containers={}
for n in cmd(['docker','ps','-a','--format','{{.Names}}']).splitlines():
    c=inspect(n);containers[n]=dict(id=c['Id'],image=c['Image'],started=c['State']['StartedAt'],running=c['State']['Running'])
rows=api('/api/v1/streams')['streams']
save('baseline.json',dict(head=cmd(['git','-C',str(REPO),'rev-parse','HEAD']).strip(),containers=containers,ledger=ledger(),states=[{k:r.get(k) for k in ('stream_id','state','child_pid','cleanup_pending')} for r in rows]))
save('compose-before.json',json.loads(cmd(compose()+['config','--format','json'])),private=True)
for src,name in [('/workspace/contextra-runtime-config/web-start-candidate-20260918/candidate.env','candidate.env.before'),('/data/contextra/runtime-state/streams_runtime.yaml','runtime-original.yaml')]:
    shutil.copyfile(src,PRIVATE/name);(PRIVATE/name).chmod(0o600)
assert shutil.disk_usage('/').free>15*1024**3
print(json.dumps(dict(channels=len(rows),active=sum(r['state']!='stopped' for r in rows))))
