import importlib.util,json,os,shutil
from pathlib import Path
os.umask(0o077)
R=Path(__file__).parent
P=Path('/tmp/codex/ltb1021/2026-09-14_contextra-fire-runtime_r01')
s=importlib.util.spec_from_file_location('gate','/tmp/codex/ltb1021/2026-09-11_service-stop_r01/gate.py')
g=importlib.util.module_from_spec(s);s.loader.exec_module(g)
HEAD='7a51cb97269ae535e412e88fc2383ea5cbc5ef55'
if __name__=='__main__':
    assert not (R/'plan.json').exists()
    assert g.sh(['git','-C',str(g.REPO),'rev-parse','HEAD']).strip()==HEAD
    assert not g.sh(['git','-C',str(g.REPO),'status','--porcelain']).strip()
    c=g.inspect();assert c['State']['Status']=='exited' and c['HostConfig']['RestartPolicy']['Name']=='no'
    old=json.loads((P/'plan.json').read_text());env=g.load_env_file(str(g.ENV))
    overlay=Path(env['CONTEXTRA_RUNTIME_STATE_ROOT'])/'streams_runtime.yaml';active=g.ENV.parent/'metadata-active/compose.yaml'
    assert g.sha(overlay)==old['overlay_hash']
    model=json.loads((P/'model-prepared.json').read_text());directory=Path(model['directory'])
    assert g.sha(directory/'2/model.plan')==old['fire_plan_hash'] and g.sha(directory/'config.pbtxt')==model['config_hash']
    assert all(g.sha(Path(p))==h for p,h in old['pose_hashes'].items())
    assert g.sha(g.CFG)==old['config_hash'] and g.sha(g.CFG.parent/'2/model.plan')==old['plan_hash']
    inp=json.loads((P/'approved-input.json').read_text());assert g.sha(Path(inp['path']))==inp['sha256']
    root=g.sh(['docker','info','--format','{{.DockerRootDir}}']).strip();free=shutil.disk_usage(root).free;assert free>=15*1024**3
    for name,path in [('env.before',g.ENV),('overlay.before',overlay),('active-compose.before',active)]:shutil.copyfile(path,R/name)
    for name in ('model-prepared.json','approved-input.json'):shutil.copyfile(P/name,R/name)
    for name in ('samples','diagnostics'):(R/name).mkdir(mode=0o700)
    source_hashes={p:g.sha(g.REPO/p) for p in ('app/api/streams.py','app/stream_settings.py','app/stream_manager.py','tests/test_fire_path.py')}
    # Production Native sources have not changed since the actual SDK PASS image.
    native_files=g.sh(['git','-C',str(g.REPO),'ls-files','native']).splitlines()
    assert all(g.sha(g.REPO/p)==g.sha(P/'snapshot'/p) for p in native_files)
    plan=dict(started_utc='2026-09-14T02:21:41Z',head=HEAD,analytics=c,controls=str(active.parent),other_containers=g.others(),gpu_mib=g.gpu(),disk_free=free,env_hash=g.sha(g.ENV),overlay=str(overlay),overlay_hash=g.sha(overlay),active_hash=g.sha(active),config_hash=g.sha(g.CFG),plan_hash=g.sha(g.CFG.parent/'2/model.plan'),pose_hashes=old['pose_hashes'],fire_plan_hash=old['fire_plan_hash'],normal_restart=old['analytics']['HostConfig']['RestartPolicy'],source_hashes=source_hashes,native_source_unchanged=True)
    (R/'plan.json').write_text(json.dumps(plan,indent=2));(R/'budget.json').write_text(json.dumps(dict(started_at='2026-09-14T02:21:41Z',failed_groups=0,groups=[]),indent=2))
    print(json.dumps(dict(stopped=True,source_clean=True,native_unchanged=True,disk_gib=round(free/1024**3,2),gpu_mib=plan['gpu_mib'])))
