"""Freeze selected stable-teacher comparison after independent operator/readiness checks."""
from pathlib import Path
import json,hashlib,datetime
ROOT=Path(__file__).resolve().parents[1]
def sha(p):
 h=hashlib.sha256()
 with (ROOT/p).open('rb') as f:
  while b:=f.read(8<<20):h.update(b)
 return h.hexdigest()
def main():
 old=json.loads((ROOT/'notes/contextual_rl_contract.json').read_text())
 for p,h in old['input_sha256'].items():assert sha(p)==h,p
 for name in ['reference_contextual_operator_cpu_check','stable_contextual_preflight']:
  assert json.loads((ROOT/f'notes/{name}.json').read_text())['status']=='PASS'
 pre=json.loads((ROOT/'notes/stable_contextual_preflight_manifest.json').read_text())
 for p,h in pre['input_sha256'].items():assert sha(p)==h,p
 split=json.loads((ROOT/'notes/stable_contextual_split.json').read_text())
 assert len(split['train_indices'])==320 and len(split['development_indices'])==512 and len(split['confirmation_indices'])==700
 assert not set(split['train_indices'])&set(split['protected_indices'])
 c=json.loads((ROOT/'notes/stable_contextual_contract_draft.json').read_text())
 assert c['updates']=={'G':80,'F':64,'E':64} and c['chunks']=={'G':5,'F':4,'E':4}
 paths=set(old['input_sha256'])-{'code/results/baseline_s17041_u50/model.pth'}
 paths.update(['code/reference_contextual_operator.py','code/check_reference_contextual_operator.py','code/train_stable_contextual_rl.py','code/eval_stable_contextual_native.py','code/analyze_stable_contextual_rl.py','code/prepare_stable_contextual_split.py','code/freeze_stable_contextual.py','code/preflight_stable_contextual.py','run_stable_contextual_queue.py','notes/stable_contextual_split.json','notes/reference_contextual_operator_cpu_check.json','notes/stable_contextual_preflight_manifest.json','notes/stable_contextual_preflight.json','notes/fresh_warm_37041_contract.json','notes/fresh_warm_37041_state.json','code/results/baseline_s37041_u50/model.pth','code/vendor/reasoning_from_scratch/utils.py','code/vendor/reasoning_from_scratch/ch02.py'])
 c.update(status='FROZEN_STABLE_CONTEXTUAL_RL',frozen_at=datetime.datetime.now(datetime.timezone.utc).isoformat(),teacher_tensor_sha256=json.loads((ROOT/'notes/stable_contextual_preflight.json').read_text())['teacher_sha256_before'],input_sha256={p:sha(p) for p in sorted(paths)})
 c['split_seed']=20641202
 c['warm_ancestry']='Original supplied50 updates, seed37041,130726generated tokens,446.879946s loop/460.1s allocation. Fixed before evaluation; no score selection.'
 c['raw_audit_sample']='For compact future evidence, first physical rollout record of every13 train chunk and first physical native record of every8 eval chunk:21 raw samples fixed prospectively. Full author audit re-extracts/regrades all raw records; compact reviewer audit retains extracted candidates and these21 samples.'
 c['novelty']='Empirical transfer test within allowed0.6B own-model setting, not invention of context distillation or on-policy JSD. No claim stable teacher is universally better or identified as earlier failure cause.'
 path=ROOT/'notes/stable_contextual_contract.json';assert not path.exists();path.write_text(json.dumps(c,indent=2))
 print(json.dumps({'contract':str(path),'sha256':sha(str(path.relative_to(ROOT))),'files':len(paths),'name':c['name']}))
if __name__=='__main__':main()
