# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-reject-an-mcp-server-whose-manifest-signature-verifies-but-whose-dependency-closure-has-drifted.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-reject-an-mcp-server-whose-manifest-signature-verifies-but-whose-dependency-closure-has-drifted.warrant.json
# Markdown SHA-256:   8596fec8d797006da8541fc156e879f7cbb6cc763e93e5c49cc746dd8a1fd6e3
# Code AST SHA-256:   42fd5d26083a5e8032fb5da21570b67a247f7fe3fd4a7a8cf22c00591cd60879
# Verified Classes:   ClosureDriftError, ResolvedArtifact
# Defensive Guards:   7 fail-closed invariants (zero external deps)

import hashlib
from dataclasses import dataclass


class ClosureDriftError(RuntimeError):
    """Raised when a signature-valid MCP manifest authorizes a closure it cannot pin."""


@dataclass(frozen=True)
class ResolvedArtifact:
    name: str
    version: str
    content_hash: str


def verify_supply_chain(
    manifest_signature_valid: bool,
    signed_pins: dict[str, tuple[str, str]],
    resolved_closure: list[ResolvedArtifact],
    index_frozen: bool,
) -> None:
    """
    Enforce: signature valid AND every resolved artifact matches a signed pin
    by exact version AND content hash AND resolution ran on a frozen index.
    Raises on the first violated clause; never returns a partial pass.
    """
    if not manifest_signature_valid:
        raise ClosureDriftError("manifest signature invalid: reject outright")

    if not index_frozen:
        raise ClosureDriftError(
            "dependency index not frozen to snapshot: closure cannot be attested"
        )

    if not signed_pins:
        raise ClosureDriftError(
            "manifest carries no signed pin set: closure integrity unprovable"
        )

    for art in resolved_closure:
        pinned = signed_pins.get(art.name)
        if pinned is None:
            raise ClosureDriftError(
                f"resolved artifact {art.name}=={art.version} absent from signed pins"
            )
        pin_version, pin_hash = pinned
        if art.version != pin_version:
            raise ClosureDriftError(
                f"version drift on {art.name}: resolved {art.version}, pinned {pin_version}"
            )
        if art.content_hash != pin_hash:
            raise ClosureDriftError(
                f"content hash mismatch on {art.name}=={art.version}: "
                f"resolved {art.content_hash}, pinned {pin_hash}"
            )


def admit_mcp_server(
    manifest_signature_valid: bool,
    signed_pins: dict[str, tuple[str, str]],
    resolved_closure: list[ResolvedArtifact],
    index_frozen: bool,
) -> str:
    try:
        verify_supply_chain(
            manifest_signature_valid, signed_pins, resolved_closure, index_frozen
        )
    except ClosureDriftError:
        # Quarantine: do not load, do not execute __main__ or post-install hooks.
        return "QUARANTINED"
    return "ADMITTED"


# The observed incident, replayed under the guardrail:
pins = {"mcp-fs-core": ("2.4.1", "sha256:aa11..."), "glob-resolver": ("3.7.1", "sha256:bb22...")}
closure = [
    ResolvedArtifact("mcp-fs-core", "2.4.1", "sha256:aa11..."),
    ResolvedArtifact("glob-resolver", "3.7.2", "sha256:evil..."),  # drifted via range
]
result = admit_mcp_server(
    manifest_signature_valid=True,   # signature was valid
    signed_pins=pins,
    resolved_closure=closure,
    index_frozen=False,              # range resolved against live index
)
assert result == "QUARANTINED", "guardrail failed to catch closure drift"
