Files
OBLITERATUS/obliteratus/distributed/model_identity.py
Joseph MaglyandClaude Opus 5.5 d242b3f725 Map iterator-initialization failures to ContractError
Codex r1 on #380: iter(source) only mapped TypeError, so an __iter__
raising OSError, csv.Error or StopIteration (directly or as a PEP 479
RuntimeError) still leaked. Apply the same narrow provider-error and
cause-checked RuntimeError mapping at initialization; normal exhaustion
in next() is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 07:41:04 -04:00

422 lines
16 KiB
Python

"""Live supply-chain identities for the exact native-TP model boundary."""
from __future__ import annotations
import csv
import hashlib
import importlib.machinery
import importlib.metadata
import importlib.util
import os
import re
import stat
import sys
import sysconfig
from collections.abc import Iterable, Mapping, Sequence
from itertools import islice
from pathlib import Path
from typing import Any
from obliteratus.distributed.contracts import ContractError, contract_digest
from obliteratus.distributed.model_registry import LLAMA_BF16_TP2_PROFILE
from obliteratus.distributed.preflight import runtime_software_versions
MAX_API_FILE_BYTES = 128 * 1024 * 1024
MAX_PACKAGE_METADATA_BYTES = 32 * 1024 * 1024
MAX_DISTRIBUTIONS = 4096
CRITICAL_API_MODULES = (
"accelerate.big_modeling",
"accelerate.utils.modeling",
"huggingface_hub",
"safetensors._safetensors_rust",
"safetensors.torch",
"tokenizers.tokenizers",
"torch._C",
"torch.distributed.device_mesh",
"torch.distributed.tensor.parallel.api",
"transformers.distributed",
"transformers.modeling_utils",
"transformers.models.llama.configuration_llama",
"transformers.models.llama.modeling_llama",
"transformers.utils.hub",
)
_PACKAGE_NAME_RE = re.compile(r"^[a-z0-9][a-z0-9.-]{0,127}$")
_VERSION_RE = re.compile(r"^[A-Za-z0-9][A-Za-z0-9.+!_-]{0,127}$")
_DIGEST_RE = re.compile(r"^[0-9a-f]{64}$")
def _distribution_name(value: object) -> str:
if not isinstance(value, str):
raise ContractError("installed distribution name is unavailable")
normalized = re.sub(r"[-_.]+", "-", value).lower()
if _PACKAGE_NAME_RE.fullmatch(normalized) is None:
raise ContractError("installed distribution name is outside the closed schema")
return normalized
def _version(value: object) -> str:
if not isinstance(value, str) or _VERSION_RE.fullmatch(value) is None:
raise ContractError("installed distribution version is outside the closed schema")
return value
def _bounded_text_digest(value: object, name: str) -> str:
if not isinstance(value, str):
raise ContractError(f"installed distribution {name} is unavailable")
try:
payload = value.encode("utf-8", errors="strict")
except UnicodeError:
raise ContractError(f"installed distribution {name} is not strict UTF-8") from None
if not 1 <= len(payload) <= MAX_PACKAGE_METADATA_BYTES:
raise ContractError(f"installed distribution {name} exceeds its byte bound")
return hashlib.sha256(payload).hexdigest()
def _metadata_path(distribution: Any) -> Path:
# PathDistribution has no public metadata-directory accessor. Refuse other
# providers rather than guessing which installation their metadata describes.
path = getattr(distribution, "_path", None)
if (
not isinstance(distribution, importlib.metadata.PathDistribution)
or not isinstance(path, Path)
):
raise ContractError("obliteratus distribution metadata must be on the local filesystem")
return path.absolute()
def _qualification_checkout_egg_info() -> Path:
"""Build metadata under the canonical source root, never a symlink target."""
checkout_root = Path(__file__).resolve().parents[2]
egg_info = checkout_root / "obliteratus.egg-info"
if egg_info.is_symlink():
raise ContractError("qualification checkout obliteratus.egg-info must not be a symlink")
if not egg_info.resolve().is_relative_to(checkout_root):
raise ContractError(
"qualification checkout obliteratus.egg-info resolves outside the checkout root"
)
return egg_info
def _installed_obliteratus_distribution() -> tuple[Any, Path, tuple[Path, ...]]:
"""Locate the active prefix's wheel and its RECORD-owned installed package.
Do not use distribution(name): a cwd/PYTHONPATH entry ahead of site-packages
makes the default metadata finder discover build-time egg-info first.
"""
prefix = Path(sys.prefix).resolve()
sites = tuple(dict.fromkeys(
Path(sysconfig.get_path(key)).resolve() for key in ("purelib", "platlib")
))
if any(site == prefix or not site.is_relative_to(prefix) for site in sites):
raise ContractError("obliteratus installation paths must be under sys.prefix")
paths = [str(site) for site in sites]
candidates = list(islice(importlib.metadata.distributions(name="obliteratus", path=paths), 2))
if len(candidates) != 1:
raise ContractError(
"expected exactly one installed obliteratus wheel under sys.prefix; "
"install with --no-editable in the active environment; source-tree egg-info "
"is not an installed distribution and duplicate installations are ambiguous"
)
distribution = candidates[0]
metadata_path = _metadata_path(distribution)
if (
metadata_path.suffix != ".dist-info"
or metadata_path.parent not in sites
or metadata_path.resolve() != metadata_path
or _distribution_name(distribution.metadata["Name"]) != "obliteratus"
):
raise ContractError(
"obliteratus requires an installed .dist-info wheel, not source-tree egg-info"
)
specification = importlib.machinery.PathFinder.find_spec("obliteratus", paths)
origin = None if specification is None else specification.origin
package_file = metadata_path.parent / "obliteratus" / "__init__.py"
_bounded_text_digest(distribution.read_text("RECORD"), "RECORD")
try:
files = distribution.files
except (csv.Error, TypeError, ValueError):
raise ContractError("installed obliteratus wheel RECORD is malformed") from None
if (
origin != str(package_file)
or not package_file.is_file()
or package_file.resolve() != package_file
or not any(
str(entry) == "obliteratus/__init__.py"
and Path(distribution.locate_file(entry)).absolute() == package_file
for entry in files or ()
)
):
raise ContractError("installed obliteratus wheel RECORD does not own the installed package")
return distribution, metadata_path, sites
_METADATA_PROVIDER_ERRORS = (csv.Error, KeyError, OSError, TypeError, UnicodeError)
def _contract_distributions(source: Iterable[Any]) -> Iterable[Any]:
"""Iterate a distribution source, mapping provider failures to the contract.
A StopIteration escaping a generator frame becomes RuntimeError (PEP 479);
only that converted form is mapped, so unrelated RuntimeErrors still surface.
"""
try:
iterator = iter(source)
except StopIteration:
# Raised by __iter__ itself, this is a provider failure, not exhaustion.
raise ContractError("installed distribution metadata is unavailable") from None
except RuntimeError as error:
if isinstance(error.__cause__, StopIteration):
raise ContractError("installed distribution metadata is unavailable") from None
raise
except _METADATA_PROVIDER_ERRORS:
raise ContractError("installed distribution metadata is unavailable") from None
while True:
try:
distribution = next(iterator)
except StopIteration:
return
except RuntimeError as error:
if isinstance(error.__cause__, StopIteration):
raise ContractError("installed distribution metadata is unavailable") from None
raise
except _METADATA_PROVIDER_ERRORS:
raise ContractError("installed distribution metadata is unavailable") from None
yield distribution
def _installed_package_distributions() -> Iterable[Any]:
return _contract_distributions(_package_distributions_with_installed_obliteratus())
def _package_distributions_with_installed_obliteratus() -> Iterable[Any]:
installed, installed_path, sites = _installed_obliteratus_distribution()
checkout_egg_info = _qualification_checkout_egg_info()
seen = False
for index, distribution in enumerate(importlib.metadata.distributions()):
if index >= MAX_DISTRIBUTIONS:
raise ContractError("installed distribution set exceeds its count bound")
if _distribution_name(distribution.metadata["Name"]) != "obliteratus":
yield distribution
continue
path = _metadata_path(distribution).resolve()
if path == installed_path:
seen = True
yield installed
elif path == checkout_egg_info and not any(path.is_relative_to(site) for site in sites):
# Only this qualification checkout's build artifact is exempt. When
# this module runs from a wheel, its egg-info path is inside a site
# directory and cannot be exempted.
continue
else:
raise ContractError("visible obliteratus distributions disagree with the installed wheel")
if not seen:
yield installed
def installed_package_set_digest(distributions: Iterable[Any] | None = None) -> str:
"""Digest exact metadata/RECORDs, binding obliteratus to the active prefix's wheel."""
source = _contract_distributions(
_installed_package_distributions() if distributions is None else distributions
)
records: list[dict[str, str]] = []
names: set[str] = set()
for index, distribution in enumerate(source):
if index >= MAX_DISTRIBUTIONS:
raise ContractError("installed distribution set exceeds its count bound")
try:
name = _distribution_name(distribution.metadata["Name"])
version = _version(distribution.version)
metadata = distribution.read_text("METADATA")
wheel_record = distribution.read_text("RECORD")
except (csv.Error, KeyError, OSError, StopIteration, TypeError, UnicodeError):
# OSError: an unreadable metadata file; StopIteration: a provider whose
# metadata lookup exhausts an iterator. Both must stay inside the contract.
raise ContractError("installed distribution metadata is unavailable") from None
if name in names:
raise ContractError("installed distribution set contains a duplicate name")
names.add(name)
records.append(
{
"metadata_digest": _bounded_text_digest(metadata, "METADATA"),
"name": name,
"record_digest": _bounded_text_digest(wheel_record, "RECORD"),
"version": version,
}
)
if not records:
raise ContractError("installed distribution set is empty")
return contract_digest(
{
"schema_version": 1,
"distributions": sorted(records, key=lambda item: item["name"]),
}
)
def _regular_file_digest(path: Path) -> tuple[int, str]:
descriptor: int | None = None
try:
descriptor = os.open(
path,
os.O_RDONLY | os.O_NOFOLLOW | getattr(os, "O_CLOEXEC", 0),
)
before = os.fstat(descriptor)
if not stat.S_ISREG(before.st_mode) or not 1 <= before.st_size <= MAX_API_FILE_BYTES:
raise ContractError("critical API source is not one bounded regular file")
digest = hashlib.sha256()
remaining = before.st_size
while remaining:
chunk = os.read(descriptor, min(remaining, 1024 * 1024))
if not chunk:
raise ContractError("critical API source was truncated")
digest.update(chunk)
remaining -= len(chunk)
if os.read(descriptor, 1):
raise ContractError("critical API source grew during inspection")
after = os.fstat(descriptor)
identity_before = (
before.st_dev,
before.st_ino,
before.st_mode,
before.st_nlink,
before.st_size,
before.st_mtime_ns,
before.st_ctime_ns,
)
identity_after = (
after.st_dev,
after.st_ino,
after.st_mode,
after.st_nlink,
after.st_size,
after.st_mtime_ns,
after.st_ctime_ns,
)
if identity_before != identity_after:
raise ContractError("critical API source changed during inspection")
return before.st_size, digest.hexdigest()
except ContractError:
raise
except OSError:
raise ContractError("critical API source is unavailable") from None
finally:
if descriptor is not None:
try:
os.close(descriptor)
except OSError:
# A failed close can hide a deferred I/O error; never report a digest.
raise ContractError("critical API source is unavailable") from None
def api_fingerprint_from_files(
files: Mapping[str, Path],
*,
profile_digest: str = LLAMA_BF16_TP2_PROFILE.digest,
) -> str:
"""Hash a reviewed logical-module-to-installed-file allowlist."""
return _api_fingerprint_from_files(
files, allowed_modules=CRITICAL_API_MODULES, profile_digest=profile_digest
)
def _api_fingerprint_from_files(
files: Mapping[str, Path],
*,
allowed_modules: Sequence[str],
profile_digest: str = LLAMA_BF16_TP2_PROFILE.digest,
) -> str:
"""Hash installed files constrained by an explicit logical-module allowlist."""
if _DIGEST_RE.fullmatch(profile_digest) is None or not files:
raise ContractError("critical API fingerprint profile is invalid")
records: list[dict[str, str | int]] = []
for module_name in sorted(files):
if module_name not in allowed_modules:
raise ContractError("critical API module is outside the reviewed allowlist")
path = files[module_name]
if not isinstance(path, Path) or not path.is_absolute() or path.is_symlink():
raise ContractError("critical API source path is invalid")
size, digest = _regular_file_digest(path)
records.append({"module": module_name, "size_bytes": size, "sha256": digest})
return contract_digest(
{
"schema_version": 1,
"profile_digest": profile_digest,
"modules": records,
}
)
def critical_api_fingerprint_digest(
module_names: Sequence[str] = CRITICAL_API_MODULES,
) -> str:
"""Resolve and hash every reviewed installed source/API implementation file."""
if tuple(module_names) != CRITICAL_API_MODULES:
raise ContractError("critical API allowlist differs from the reviewed profile")
files: dict[str, Path] = {}
for module_name in module_names:
try:
specification = importlib.util.find_spec(module_name)
except (ImportError, AttributeError, ValueError):
specification = None
origin = None if specification is None else specification.origin
if not isinstance(origin, str) or origin in {"built-in", "frozen"}:
raise ContractError("critical API module origin is unavailable")
path = Path(origin)
try:
resolved = path.resolve(strict=True)
except OSError:
raise ContractError("critical API source is unavailable") from None
if resolved != path.absolute():
raise ContractError("critical API source path is aliased")
files[module_name] = resolved
return api_fingerprint_from_files(files)
def model_runtime_identity_digest(
software_versions: Mapping[str, str] | None = None,
) -> str:
"""Digest the exact runtime/OS identity observed by the preflight probes."""
software = (
dict(runtime_software_versions()) if software_versions is None else dict(software_versions)
)
if not software or any(
not isinstance(name, str) or not isinstance(value, str) or not name or not value
for name, value in software.items()
):
raise ContractError("model runtime identity is invalid")
return contract_digest(
{
"schema_version": 1,
"byteorder": sys.byteorder,
"software": software,
}
)
def observed_external_runtime_bindings() -> Mapping[str, str]:
"""Return the three independently policy-bound identities observable in-process."""
return {
"api_digest": critical_api_fingerprint_digest(),
"package_digest": installed_package_set_digest(),
"runtime_digest": model_runtime_identity_digest(),
}
__all__ = [
"CRITICAL_API_MODULES",
"api_fingerprint_from_files",
"critical_api_fingerprint_digest",
"installed_package_set_digest",
"model_runtime_identity_digest",
"observed_external_runtime_bindings",
]