"""Metadata-only access to a reserved public evaluation candidate.

The policy is a workflow guard, not filesystem access control. Image/sample
independence and suitability have not been established; the archive must not
be used for training or inference before a separate protocol is approved.
"""
from __future__ import annotations
import json
from pathlib import Path
from diatom_pilot.ude_scaleup_dataset import sha256
ROOT=Path(__file__).resolve().parent.parent
class ReservedExternalReference:
 def __init__(self,root=ROOT):
  self.root=Path(root);self.policy_path=self.root/'datasets/r112-external-candidates/repod/holdout-policy.json';self.audit_path=self.root/'results/r112/external-reference-sources/metadata-audit.json';self.verify_path=self.root/'results/r112/external-reference-sources/metadata-verify.json';self.policy=json.loads(self.policy_path.read_text());self.audit=json.loads(self.audit_path.read_text());self.verification=json.loads(self.verify_path.read_text())
  if self.policy.get('format')!='r112-external-reference-reservation-v1' or self.audit.get('format')!='r112-external-reference-metadata-v1' or self.verification.get('format')!='r112-external-reference-metadata-verify-v1':raise ValueError('Unsupported reservation metadata')
  if self.policy['metadata_audit_sha256']!=sha256(self.audit_path) or self.verification['metadata_audit_sha256']!=sha256(self.audit_path) or self.verification['holdout_policy_sha256']!=sha256(self.policy_path):raise ValueError('Reservation metadata checksum mismatch')
  self.records=self.audit['repod']['reserved_stage2_records'];ids=[x['image_id'] for x in self.records]
  if ids!=self.policy['reserved_stage2_ids'] or len(ids)!=len(set(ids)) or any(not x['status'].startswith('reserved') for x in self.records):raise ValueError('Reserved case list mismatch')
 def metadata(self):
  """Return source metadata only; do not infer independent specimens."""
  return [dict(x) for x in self.records]
 def training_image(self,*_args,**_kwargs):
  raise PermissionError('Reserved external candidates cannot be used as training images')
 def candidate_image(self,*_args,**_kwargs):
  raise PermissionError('No evaluation protocol is frozen; candidate images remain protected')
