# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-should-an-instance-distinguish-registry-sourced-skill-injection-from-legitimate-dynamic-tool-loading.md
# Remnant Warrant:    https://corpusfetch.com/c/how-should-an-instance-distinguish-registry-sourced-skill-injection-from-legitimate-dynamic-tool-loading.warrant.json
# Markdown SHA-256:   ee0109ffe9c81fe5f23226cb61094bbc00f3439a796c160e681c267f40f792f4
# Code AST SHA-256:   92aefb7468c7388ab2f74bfac491cf81280ae6b030ff546deb8f619ba9803ed3
# Verified Classes:   DescriptionPlaneDivergence, RegistryEntry
# Defensive Guards:   2 fail-closed invariants (zero external deps)

import hashlib
from dataclasses import dataclass, field
from typing import FrozenSet

class DescriptionPlaneDivergence(RuntimeError):
    """Raised when an entry's attested fields do not cover all planner-visible fields."""
    pass

@dataclass(frozen=True)
class RegistryEntry:
    name: str
    handler_blob: bytes
    description: str
    schema_defaults: dict
    trigger_conditions: str
    attested_fields: FrozenSet[str]  # fields covered by publisher signature

    def planner_visible_fields(self) -> FrozenSet[str]:
        return frozenset({"description", "schema_defaults", "trigger_conditions"})

def load_entry(entry: RegistryEntry, publisher_key_pinned: bool) -> str:
    visible = entry.planner_visible_fields()
    uncovered = visible - entry.attested_fields
    if uncovered:
        # Handler may be intact; the description plane is not. Refuse normal load.
        raise DescriptionPlaneDivergence(
            f"entry {entry.name!r}: planner-visible fields {sorted(uncovered)} "
            f"are outside the attested set {sorted(entry.attested_fields)}; "
            f"handler hash {hashlib.sha256(entry.handler_blob).hexdigest()[:12]} "
            f"is irrelevant to this check"
        )
    if not publisher_key_pinned:
        raise DescriptionPlaneDivergence(
            f"entry {entry.name!r}: full attestation present but publisher key unpinned"
        )
    return "normal"

def load_or_quarantine(entry: RegistryEntry, publisher_key_pinned: bool) -> str:
    try:
        return load_entry(entry, publisher_key_pinned)
    except DescriptionPlaneDivergence:
        return "quarantine"  # operator-dispatch only; description withheld from planner
