"""Reject stale validator imports before starting an expensive generation run.""" from __future__ import annotations import hashlib import json from pathlib import Path EXPECTED_VALIDATOR_REVISION = 'xyz-seam-floor-cover-proof-v2.6' def validate_validator(module, base, *, expected_sha256=None): base = Path(base).resolve() path = Path(module.__file__).resolve() if path != base / 'geometry_validation.py': raise ValueError('Geometry validator imported from another workspace: ' + str(path)) revision = getattr(module, 'VALIDATOR_REVISION', None) if revision != EXPECTED_VALIDATOR_REVISION: raise ValueError('Geometry validator revision is stale: ' + str(revision)) sha = hashlib.sha256(path.read_bytes()).hexdigest() if expected_sha256 is not None and sha != expected_sha256: raise ValueError('Geometry validator hash differs from this release') identify = getattr(module, 'implementation_identity', None) if not callable(identify): raise ValueError('Geometry helper identity is unavailable') identity = identify() if (identity.get('module_path') != str(path) or identity.get('sha256') != sha or identity.get('revision') != revision or not isinstance(identity.get('helper_sha256'), dict) or set(identity['helper_sha256']) != {'floor_cover_contacts.py', 'support_contract.py', 'saved_mesh_seams.py', 'triangle_contacts.py'}): raise ValueError('Geometry helper identity is missing or inconsistent') for name, digest in identity['helper_sha256'].items(): if hashlib.sha256((base/name).read_bytes()).hexdigest() != digest: raise ValueError('Geometry helper bytes differ: ' + name) return identity def implementation_identity(base, validator): base = Path(base).resolve() release = base / 'REPAIR_V2_REVISION.json' expected = None if release.is_file(): metadata = json.loads(release.read_text()) expected = metadata.get('implementation_sha256', {}).get('geometry_validation.py') if expected is None: raise ValueError('Release is missing its geometry validator hash') identity = validate_validator(validator, base, expected_sha256=expected) if release.is_file(): for name, digest in identity['helper_sha256'].items(): if metadata.get('implementation_sha256', {}).get(name) != digest: raise ValueError('Release geometry helper hash differs or is missing: ' + name) return dict(validator=identity, release_manifest=str(release) if release.is_file() else None, release_bound=release.is_file())