"""Posthoc reference-assisted inspection of matching candidate prompt types."""
import json
from pathlib import Path
import numpy as np
from l86_oracle import sha
R=Path(__file__).resolve().parents[1];O=R/'results/r107';rows=json.loads((O/'results.json').read_text())['records'];out=[]
for r in rows:
 path=O/r['archive'];assert sha(path)==r['archive_sha256'];z=np.load(path);names=z['ids'].tolist();records=[]
 for n in ('10','30','60','all'):
  for a in r['scores']['boundary'][n]['0.5']['assignments']:
   rank=a['candidate_index'];ident=r['rankings']['boundary'][rank];idx=names.index(ident);records.append({'budget':n,'target':a['annotation_id'],'rank':rank+1,'candidate_id':ident,'iou':a['iou'],'prompt_type':str(z['kind'][idx]),'point':z['points'][idx].tolist()})
 out.append({'id':r['id'],'view':r['view'],'assignments':records})
(O/'prompt-audit.json').write_text(json.dumps({'results_sha256':sha(O/'results.json'),'script_sha256':sha(__file__),'warning':'Reference-conditioned assignments after inference; source labels choose the reported match, not model points.','records':out},indent=2)+'\n')
