import importlib.util
import json
import os
from pathlib import Path
import subprocess
import time

os.umask(0o077)
R = Path(__file__).parent
R.chmod(0o700)
s = importlib.util.spec_from_file_location('prior_observer', '/tmp/codex/ltb1021/2026-09-15_contextra-three-channel_r01/observe.py')
w = importlib.util.module_from_spec(s)
s.loader.exec_module(w)
w.R = R
p = w.p
p.R = R
channels = dict(zip(['rtsp-test-source-01', 'rtsp-test-source-02', 'rtsp-test-source-03'],
                    ['rtsp-test-01', 'web-cam08-test', 'web-cam01-test']))
states = p.api('/api/v1/streams')['streams']
settings = {src:p.api('/api/v1/streams/'+sid+'/settings') for src,sid in channels.items()}
assert all(not q['configured']['tracking']['enabled'] and not q['configured']['intrusion']['enabled'] for q in settings.values())
assert all(q['configured']['analysisFpsTarget']==10 for q in settings.values())
p.save('plan.json', dict(at=time.time(), states=states, settings=settings,
    head=subprocess.check_output(['git','rev-parse','HEAD'],cwd=p.ops.REPO,text=True).strip(),
    analytics=p.ops.base.inspect('contextra-analytics-1'), resources=p.resource(),
    registry_hash=p.ops.base.g.sha(p.REG), sender=p.ops.prior.prev.sender(),
    limits=dict(window_seconds=24, helper_cpu_seconds=60, helper_ram_mib=512,
                analytics_ram_gib=12, min_host_available_gib=4, gpu_mib=16000,
                max_samples=26, log_mib=30, start_calls=0, stop_calls=0),
    restore='Metrics Off in finally; retain all run/config/image/sender/Redis; no Start/Stop.'))
w.window('three-current',channels,24)
p.save('final.json',dict(states=p.api('/api/v1/streams'), publisher=p.api('/api/v1/metadata/status'),
    registry_hash=p.ops.base.g.sha(p.REG), resources=p.resource()))
