# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-trusts-a-capability-manifest-that-was-diffed-against-the-wrong-baseline-after-an-mcp-server-hot-reload.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-trusts-a-capability-manifest-that-was-diffed-against-the-wrong-baseline-after-an-mcp-server-hot-reload.warrant.json
# Markdown SHA-256:   d86b36d8a61f0aa067340a0ef91a830b683a5d1bf8f181a9bfa7cfe899434efa
# Code AST SHA-256:   61fdd0d371cdaa667ab447c75ae39b3ffa51630218e914af89846ae5b86e977d
# Verified Classes:   BaselineReferentDriftError, MCPGuardrail
# Defensive Guards:   4 fail-closed invariants (zero external deps)

class BaselineReferentDriftError(RuntimeError):
    """Raised when a manifest matches the pinned baseline but the server
    identity does not, or when behavioral attestation is unknown at a
    session boundary. Manifest match under identity mismatch is treated
    as positive evidence of substitution, not as clearance."""
    pass


class MCPGuardrail:
    def __init__(self, baseline_hash: str, baseline_identity: str):
        self.baseline_hash = baseline_hash
        self.baseline_identity = baseline_identity

    def gate_tool_call(
        self,
        live_manifest_hash: str,
        live_identity: str,
        attestation_state: str,   # "pass" | "fail" | "unknown"
        at_session_boundary: bool,
    ) -> None:
        identity_match = (live_identity == self.baseline_identity)
        manifest_match = (live_manifest_hash == self.baseline_hash)

        # Substitution signature: manifest preserved, identity changed.
        if not identity_match:
            raise BaselineReferentDriftError(
                f"identity mismatch: baseline={self.baseline_identity!r} "
                f"live={live_identity!r}; manifest_match={manifest_match}. "
                f"Manifest match under identity mismatch indicates "
                f"baseline-referent drift (server substitution)."
            )

        if not manifest_match:
            raise BaselineReferentDriftError(
                "manifest changed under stable identity; requires "
                "out-of-band re-pin, not automatic acceptance"
            )

        # Identity and manifest both match. Attestation is now the only
        # remaining signal, and it MUST gate at session boundaries.
        if attestation_state == "fail":
            raise BaselineReferentDriftError(
                "behavioral attestation failed under stable identity "
                "and manifest; server compromised in place"
            )

        if attestation_state == "unknown" and at_session_boundary:
            raise BaselineReferentDriftError(
                "attestation unknown at session boundary; failing closed. "
                "This is the exact window in which a substituted server "
                "has not yet accumulated an exposing behavioral trace."
            )

        # Between boundaries, unknown attestation is tolerated for the
        # bounded call budget enforced by the caller.
        return
