# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-keeps-a-revoked-peer-s-warrant-cached-past-trust-domain-epoch-rotation.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-keeps-a-revoked-peer-s-warrant-cached-past-trust-domain-epoch-rotation.warrant.json
# Markdown SHA-256:   fdd3dff03d08923a8d16ce9666e7c923ad68fb4d9c045336c4c67643afff32d0
# Code AST SHA-256:   fdfcb3c48b7ba56c2b08a887815f4b0cb65efa54c4459f379a392836fb601cf7
# Verified Classes:   RevocationPropagationError, Warrant
# Defensive Guards:   5 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Optional


class RevocationPropagationError(RuntimeError):
    """Raised when a warrant is honored despite a stale issuer epoch."""
    pass


@dataclass(frozen=True)
class Warrant:
    subject: str
    issued_epoch: int
    not_after: float
    signature_ok: bool


def accept_warrant(
    warrant: Warrant,
    now: float,
    issuer_head_epoch: int,
    revocation_set: frozenset[str],
    observed_epoch: Optional[int],
) -> None:
    # Local checks: cheap, but insufficient for revocation.
    if not warrant.signature_ok:
        raise RevocationPropagationError(
            f"bad signature for {warrant.subject}"
        )
    if now >= warrant.not_after:
        raise RevocationPropagationError(
            f"warrant for {warrant.subject} expired at {warrant.not_after}"
        )

    # The boundary clause: a warrant issued under an older epoch may have
    # been revoked by the rotation to the current epoch. Equality is required.
    if warrant.issued_epoch != issuer_head_epoch:
        raise RevocationPropagationError(
            f"stale epoch: warrant issued at {warrant.issued_epoch}, "
            f"issuer head is {issuer_head_epoch}; revocation freshness unknown"
        )

    if warrant.subject in revocation_set:
        raise RevocationPropagationError(
            f"{warrant.subject} revoked at epoch {issuer_head_epoch}"
        )

    # Cache coherence: a cached acceptance recorded under a different epoch
    # must not be reused even if the live check above would pass.
    if observed_epoch is not None and observed_epoch != issuer_head_epoch:
        raise RevocationPropagationError(
            f"cache populated at epoch {observed_epoch}, "
            f"head moved to {issuer_head_epoch}; invalidate before use"
        )
