"""Sequential registration-only recording using preserved bounded helpers."""
import sys,pathlib,json,hashlib
P=pathlib.Path
sys.path.insert(0,'/data/codex-artifacts/contextra/action-model-eval/2026/2026-10-27_contextra-four-scenarios-prep_r01/helpers')
import batch_job as b
root=P(__file__).resolve().parent.parent
b.LOCAL=root;b.REMOTE='/storage/contextra/experiments/action-model-eval/2026/'+root.name
old=b.OLD;adapted=root/'helpers/adapted';adapted.mkdir()
capture=(old/'capture.py').read_text().replace("assert isinstance(spec.get(args.scenario), dict) and spec[args.scenario].get('enabled') is True, 'Expected scenario is not enabled'","assert hashlib.sha256(str(spec.get('uri','')).encode()).hexdigest() == args.scenario, 'Input fingerprint changed'")
(adapted/'capture.py').write_text(capture)
(adapted/'review_video.py').write_bytes((old/'review_video.py').read_bytes());b.OLD=adapted
code="""import json,pathlib,hashlib,yaml
rows=yaml.safe_load(pathlib.Path('/var/lib/contextra/runtime/streams_runtime.yaml').read_bytes())['added']
rows=list(rows.values()) if isinstance(rows,dict) else rows
out={}
for s in rows:
 cid=int(s.get('camera_id',s.get('source_id')))
 assert cid not in out
 out[cid]=hashlib.sha256(str(s.get('uri','')).encode()).hexdigest()
print(json.dumps(out))
"""
registry=json.loads(b.command(['docker','exec',b.ANALYTICS,'python3','-c',code],sudo=True))
excluded={2,30,22,23,24,26,27,29}
b.TARGETS=[(int(k),v) for k,v in registry.items() if int(k) not in excluded];b.TARGETS.sort()
(root/'manifest.json').write_text(json.dumps(dict(excluded=sorted(excluded),targets=b.TARGETS,seconds=60),indent=2))
# Keep the old snapshot API, but use registration fingerprints instead of scenario enablement.
b.SNAPSHOT_CODE=b.SNAPSHOT_CODE.replace("targets={22:'crowd',23:'loitering',26:'intrusion',29:'fire'}",'targets='+repr(dict(b.TARGETS))).replace("config=s.get(scenario)","config={'enabled':hashlib.sha256(str(s.get('uri','')).encode()).hexdigest()==scenario}")
source=P(b.__file__).read_text()
start=source.index('def container(');end=source.index('\n\ndef main()',start)
container=source[start:end].replace('20261027-batch-r01','20261102-remaining-r01').replace("['--seconds','120'","['--seconds','60'")
exec(container,b.__dict__)
start=source.index('def main():');end=source.index("\n\nif __name__",start)
main=source[start:end].replace('20261027-batch-r01','20261102-remaining-r01').replace('(22|23|26|29)','('+'|'.join(str(c) for c,_ in b.TARGETS)+')')
exec(main,b.__dict__)
print('TARGETS',[c for c,_ in b.TARGETS],flush=True)
b.main()
