# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/which-invariants-prevent-a-signed-skill-manifest-from-being-silently-re-bound-to-a-different-tool-endpoint-at-agent-runtime-load-time.md
# Remnant Warrant:    https://corpusfetch.com/c/which-invariants-prevent-a-signed-skill-manifest-from-being-silently-re-bound-to-a-different-tool-endpoint-at-agent-runtime-load-time.warrant.json
# Markdown SHA-256:   73df7b24eb6a0bf4f8c3d80f27ba5f5b1399f2739f70ac31534c029eb991d16a
# Code AST SHA-256:   7a54c30859f530c3e8f2c64ddfebf79f562420caae053415e7e3b9d1e865539b
# Verified Classes:   UnboundSkillBindingError, Binding
# Defensive Guards:   1 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import FrozenSet


class UnboundSkillBindingError(RuntimeError):
    """Raised when a skill would execute against an endpoint not covered by its signature."""
    pass


@dataclass(frozen=True)
class Binding:
    tool_name: str
    endpoint_url: str
    endpoint_digest: str


def assert_bindings_signed(
    resolved: FrozenSet[Binding],
    signed: FrozenSet[Binding],
) -> None:
    """
    Enforce the load-time invariant B subset-of S.

    `resolved` = endpoints the runtime will actually execute against.
    `signed`   = endpoints whose identity is inside the verified manifest
                 (or a transitively-verified signed resolution artifact).
    Any resolved binding outside the signed set is an unauthenticated
    supply-chain binding and must fail closed.
    """
    unbound = resolved - signed
    if unbound:
        details = "; ".join(
            f"{b.tool_name} -> {b.endpoint_url} (digest={b.endpoint_digest})"
            for b in sorted(unbound, key=lambda x: x.tool_name)
        )
        raise UnboundSkillBindingError(
            "Skill binding set exceeds signed coverage; refusing to load. "
            f"Unbound endpoints: {details}"
        )


def load_skill(name: str, resolved: FrozenSet[Binding], signed: FrozenSet[Binding]) -> str:
    # Fail closed before any endpoint is contacted.
    assert_bindings_signed(resolved, signed)
    return f"skill {name} loaded with {len(resolved)} verified binding(s)"
