"""Check frozen existing cross-source screens cover R106 vs all L90 fields."""
import json
from pathlib import Path
from l86_oracle import sha
R=Path(__file__).resolve().parents[1];O=R/'results/r106';p=json.loads((O/'protocol.json').read_text())
for f,h in p['inputs'].items():assert sha(R/f)==h
rows=json.loads((R/'results/r97/results.json').read_text())['records'];screen=json.loads((R/'results/r98/source-screen.json').read_text())['records'];prior=json.loads((R/'results/r105/results.json').read_text())['records'];assert len(rows)==23 and len(prior)==13
selected={x['id'] for x in rows};valid={x['index']:x for x in screen if not x['quarantined']};assert {x['index'] for x in prior}==set(valid)
checks=[]
for x in prior:
 s=valid[x['index']];assert s['source_image_sha256']==x['source_sha256'];covered={str(y['reference_name']) for y in s['comparisons'] if y['reference_group']=='r80'};assert selected<=covered;flags=[y for y in s['comparisons'] if y['reference_group']=='r80' and str(y['reference_name']) in selected and y['flag']];assert not flags;checks.append({'r105_index':x['index'],'r80_sources_compared':len(covered),'selected_r97_sources_compared':len(selected),'flagged_pairs':len(flags)})
out={'protocol_sha256':sha(O/'protocol.json'),'records':checks,'warning':'Screen covers known annotated crop sources, not all unlabeled pixels; coarse hashes do not establish biological independence.'};(O/'screen-check.json').write_text(json.dumps(out,indent=2)+'\n');print('Verified',len(checks),'retained prior fields against all',len(selected),'selected source IDs; no flagged known component pairs.')
