"""Check reusable possible-link implementation against all frozen L77 variants."""
import json
from pathlib import Path
import numpy as np
from diatom_pilot.crossplane_links import build_possible_links
R=Path(__file__).resolve().parents[1];O=R/'results/l77';rows=json.loads((O/'results.json').read_text())['scenes'];checks=[]
for r in rows:
 z=np.load(R/r['source']/r['archive']);m=np.unpackbits(z['mask_bits'])[:int(np.prod(z['mask_shape']))].reshape(z['mask_shape']).astype(bool);lookup={str(k):v for k,v in zip(z['ids'],m)};nodes=[{'id':v['id'],'z':v['z'],'mask':lookup[v['id'][5:] if v['id'].startswith('copy:') else v['id']]} for v in r['nodes']];graph=build_possible_links(nodes);edges=[{k:v for k,v in edge.items() if k!='semantics'} for edge in graph['possible_links']];assert edges==r['possible_edges'] and [v['id'] for v in graph['candidates']]==[v['id'] for v in r['nodes']];checks.append({'source':r['source'],'pair':r['pair'],'condition':r['condition'],'variant':r['variant'],'candidate_count':len(nodes),'links':len(edges)})
(O/'production-parity.json').write_text(json.dumps({'format':'l77-link-parity-v1','scenes':checks,'warning':'Parity with archived synthetic overlaps only; biological identity and real-world association accuracy are not verified.'},indent=2)+'\n');print('Exact possible-link parity on',len(checks),'saved synthetic scene variants.')
