# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-halt-when-a-crl-style-revocation-epoch-advances-mid-handoff-before-the-peer-has-observed-the-new-epoch.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-halt-when-a-crl-style-revocation-epoch-advances-mid-handoff-before-the-peer-has-observed-the-new-epoch.warrant.json
# Markdown SHA-256:   ecd1eeaa60d85b4e2d68809ec221ac85338b116bf36492886ca8ef726b030ed0
# Code AST SHA-256:   bc5b7dbdd7ff6a6a658b107db3111b4e9c5283f0914a8b44231e5ea39585a7eb
# Verified Classes:   RevocationBoundaryError, Status, Validation, Warrant
# Defensive Guards:   3 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from enum import Enum


class RevocationBoundaryError(RuntimeError):
    """Raised when a warrant crosses a revocation epoch boundary unsafely."""


class Status(Enum):
    VALID = "valid"
    REVOKED = "revoked"
    UNKNOWN = "unknown"


@dataclass(frozen=True)
class Validation:
    credential_id: str
    epoch: int
    verdict: Status


@dataclass(frozen=True)
class Warrant:
    credential_id: str
    basis: Validation  # the (cred, epoch, verdict) the minter used
    capability: str


def admit_warrant(
    warrant: Warrant,
    receiver_epoch: int,
    receiver_status: dict[str, Status],
) -> None:
    """
    Enforce epoch-pinned validation at handoff.

    The receiver must reject any warrant whose pinned epoch differs from the
    receiver's observed epoch, and must reject any warrant whose credential is
    revoked at the receiver's epoch -- regardless of reputation.
    """
    pinned = warrant.basis.epoch

    if pinned != receiver_epoch:
        # Stale or ahead: the warrant's basis is not binding here.
        raise RevocationBoundaryError(
            f"epoch mismatch: warrant pinned at {pinned}, "
            f"receiver at {receiver_epoch}; refusing handoff"
        )

    current = receiver_status.get(warrant.credential_id, Status.UNKNOWN)
    if current is not Status.VALID:
        # Catches both REVOKED and UNKNOWN (fail-closed).
        raise RevocationBoundaryError(
            f"credential {warrant.credential_id} is {current.value} "
            f"at epoch {receiver_epoch}; refusing warrant"
        )


def admit_with_reputation(
    warrant: Warrant,
    receiver_epoch: int,
    receiver_status: dict[str, Status],
    reputation: float,
) -> None:
    """
    Reputation is a tiebreaker among epoch-current candidates only.
    It is a product with status currency, never a weighted sum.
    """
    try:
        admit_warrant(warrant, receiver_epoch, receiver_status)
    except RevocationBoundaryError:
        # Status currency is zero; reputation cannot launder it.
        raise RevocationBoundaryError(
            f"high reputation {reputation:.2f} does not override "
            f"stale or revoked status for {warrant.credential_id}"
        )


# Example: the split-epoch handoff from the record.
planner_basis = Validation("exec-7", epoch=4470, verdict=Status.VALID)
w = Warrant("exec-7", basis=planner_basis, capability="tool:write")

try:
    admit_warrant(w, receiver_epoch=4471, receiver_status={"exec-7": Status.REVOKED})
except RevocationBoundaryError as e:
    print(f"blocked before side effect: {e}")
