# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-refuse-to-honor-a-negotiated-capability-when-the-policy-warrant-outlives-the-capability-s-schema.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-refuse-to-honor-a-negotiated-capability-when-the-policy-warrant-outlives-the-capability-s-schema.warrant.json
# Markdown SHA-256:   fb358de1993d9172c2529e82780529aba86ddf260d7102634fbaa71059f4ff23
# Code AST SHA-256:   aeb054ea5f53261fd51fb9a262b9f107be4e4d6fd6c7344db95c7d9826446ca8
# Verified Classes:   FederationRefusal, Warrant, LiveCapability
# Defensive Guards:   6 fail-closed invariants (zero external deps)

import hashlib
from dataclasses import dataclass


class FederationRefusal(RuntimeError):
    """Raised when a negotiated capability warrant no longer matches the live schema."""
    pass


@dataclass(frozen=True)
class Warrant:
    capability: str
    pinned_schema_hash: str | None  # None => unpinned, risk owned by caller
    scope: str
    expires_at: float


@dataclass(frozen=True)
class LiveCapability:
    name: str
    schema_hash: str


def authorize_invocation(
    warrant: Warrant,
    live: LiveCapability,
    now: float,
    caller_accepts_unpinned: bool = False,
) -> None:
    if now > warrant.expires_at:
        raise FederationRefusal(
            f"warrant for {warrant.capability} expired at {warrant.expires_at}"
        )
    if warrant.capability != live.name:
        raise FederationRefusal(
            f"capability mismatch: warrant={warrant.capability} live={live.name}"
        )
    if warrant.pinned_schema_hash is None:
        if not caller_accepts_unpinned:
            raise FederationRefusal(
                f"warrant for {warrant.capability} is unpinned; explicit "
                f"acceptance of live schema {live.schema_hash} required"
            )
        return
    if warrant.pinned_schema_hash != live.schema_hash:
        raise FederationRefusal(
            f"schema drift on {warrant.capability}: warrant pinned "
            f"{warrant.pinned_schema_hash}, live is {live.schema_hash}; "
            f"refuse and renegotiate — do not coerce"
        )


def parse_reroute_response(payload: dict) -> dict:
    # Fail closed: only the negotiated envelope is accepted.
    if "status" in payload or "ok" not in payload:
        raise FederationRefusal(
            f"response envelope not in negotiated contract: keys={sorted(payload)}"
        )
    if not isinstance(payload["ok"], bool):
        raise FederationRefusal("ok field is not a boolean")
    return payload


if __name__ == "__main__":
    # Day-9 drift scenario from the record.
    warrant = Warrant(
        capability="shipment.reroute",
        pinned_schema_hash="sha256:9f3a",
        scope="warehouse:EU-*",
        expires_at=1_000_000.0,
    )
    drifted = LiveCapability(name="shipment.reroute", schema_hash="sha256:be71")
    authorize_invocation(warrant, drifted, now=500.0)  # raises FederationRefusal
