"""Bounded read-only mapping of observed runtime drift; no raw URLs or settings dump."""
from collections import Counter
import json
import control_review as c

before=json.loads((c.REPORT/'production_before.json').read_text())
after=json.loads((c.REPORT/'production_after.json').read_text())
comparison=json.loads((c.REPORT/'operational_comparison.json').read_text())
changed=list(comparison['stream_changes'])
code="""import json,pathlib,yaml
p=pathlib.Path('/var/lib/contextra/runtime/streams_runtime.yaml')
rows=yaml.safe_load(p.read_bytes())['added']
rows=list(rows.values()) if isinstance(rows,dict) else rows
wanted=set(%s)
print(json.dumps({'channels':[{'camera_id':r.get('camera_id'),'stream_id':r.get('stream_id')} for r in rows if r.get('stream_id') in wanted],'runtime_config_mtime':p.stat().st_mtime}))
""" % repr(changed)
mapping=json.loads(c.old.checked(['docker','exec','contextra-ai2-analytics-1','python3','-c',code],timeout=60))
result=dict(status='OBSERVED_DRIFT_NOT_ATTRIBUTED',
    before_states=dict(Counter(s['state'] for s in before['streams'].values())),
    after_states=dict(Counter(s['state'] for s in after['streams'].values())),
    after_ready=after['health']['ready'],changed_channels=mapping['channels'],
    camera24_changed_field_names=[k for k in before['camera24'] if before['camera24'][k]!=after['camera24'].get(k)],
    runtime_config_mtime=mapping['runtime_config_mtime'],full_config_before_not_retained=True,
    model_and_pose_config_unchanged=comparison['config_model_unchanged']['pose_engine_sha256'] and comparison['config_model_unchanged']['pose_config_sha256'],
    operating_containers_unchanged=comparison['containers_unchanged'],task_control_calls=0,task_gpu_jobs=0,
    note='Configuration hash and channel run changed during the task. Source is not established; no rollback/restart or attribution was attempted. Offline caches and baseline B outputs remain verified.')
c.save('runtime_drift_note.json',result)
print(json.dumps(result,ensure_ascii=False))
