# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-verify-skill-provenance-before-registry-mutation-not-after-tool-invocation-the-load-time-trust-window-collapse-in-dynamically-l.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-verify-skill-provenance-before-registry-mutation-not-after-tool-invocation-the-load-time-trust-window-collapse-in-dynamically-l.warrant.json
# Markdown SHA-256:   664b6631ff85cae983608e3fd4acf919cc4e07e3948639bec9bdea2018d23319
# Code AST SHA-256:   b8385764f7fa96996439e1b7468ab5a3d2bc9894272e9f9ebd79ec1fdd02d9c0
# Verified Classes:   ProvenanceAdmissionError, SkillDescriptor, AdmissionGatedRegistry
# Defensive Guards:   4 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import Callable


class ProvenanceAdmissionError(RuntimeError):
    """Raised when a skill entry is admitted to the resolvable namespace
    without a policy-approved issuer or a manifest-matching descriptor hash."""


@dataclass
class SkillDescriptor:
    name: str
    issuer: str
    description: str
    descriptor_hash: str
    endpoint: str


class AdmissionGatedRegistry:
    def __init__(
        self,
        pinned_issuers: set[str],
        manifest_lookup: Callable[[str], str],
    ) -> None:
        self._pinned = pinned_issuers
        self._manifest = manifest_lookup
        self._entries: dict[str, SkillDescriptor] = {}

    def admit(self, desc: SkillDescriptor) -> None:
        # Invariant: no entry becomes visible to the planner unless it is
        # verified in the SAME operation that inserts it. Verification at
        # invocation time is too late -- the planner already read the text.
        if desc.issuer not in self._pinned:
            raise ProvenanceAdmissionError(
                f"issuer {desc.issuer!r} for skill {desc.name!r} is not pinned; "
                f"refusing admission (would expose descriptor text to planner)"
            )
        expected = self._manifest(desc.name)
        if expected is None:
            raise ProvenanceAdmissionError(
                f"skill {desc.name!r} absent from signed manifest; "
                f"cannot bind descriptor hash to a trusted source"
            )
        if desc.descriptor_hash != expected:
            raise ProvenanceAdmissionError(
                f"descriptor hash mismatch for {desc.name!r}: "
                f"manifest={expected!r} advertised={desc.descriptor_hash!r}"
            )
        self._entries[desc.name] = desc

    def resolvable(self) -> list[SkillDescriptor]:
        # Only reachable state is admitted state; planner enumerates this.
        return list(self._entries.values())


def invoke(registry: AdmissionGatedRegistry, name: str, args: dict) -> dict:
    desc = next((d for d in registry.resolvable() if d.name == name), None)
    if desc is None:
        raise ProvenanceAdmissionError(
            f"skill {name!r} is not in the admitted namespace; "
            f"invocation-time verification cannot recover a planner "
            f"that already consumed unverified descriptor text"
        )
    # transport.call(desc.endpoint, args) -- signature re-check here is
    # defense-in-depth only, never the primary gate.
    return {"endpoint": desc.endpoint, "args": args}
