# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-refuse-a-peer-warrant-that-is-cryptographically-valid-but-whose-revocation-epoch-lags-the-local-crl-floor.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-refuse-a-peer-warrant-that-is-cryptographically-valid-but-whose-revocation-epoch-lags-the-local-crl-floor.warrant.json
# Markdown SHA-256:   9dfd675f14f2a7e0883d3a05e6721da5984216b9a06494272973a9030774599c
# Code AST SHA-256:   b8d374c3f80622f95ac3dc53575423346d93425b1b79d84f53e81b10d0367d6a
# Verified Classes:   RevocationEpochLagError, Warrant, CRLSnapshot
# Defensive Guards:   3 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Optional


class RevocationEpochLagError(RuntimeError):
    """Raised when a warrant's revocation_epoch lags the local CRL floor,
    making CRL-membership evaluation unsound. Must not be downgraded to a warning."""
    pass


@dataclass(frozen=True)
class Warrant:
    serial: str
    issuer_domain: str
    revocation_epoch: Optional[int]
    expires_at: float


@dataclass(frozen=True)
class CRLSnapshot:
    floor: int
    revoked_serials: frozenset


def admit_warrant(
    warrant: Warrant,
    crl: CRLSnapshot,
    now: float,
    introspect_ok: Optional[bool] = None,
    bounded_lag_proven: bool = False,
) -> bool:
    if warrant.expires_at <= now:
        return False
    if warrant.revocation_epoch is None:
        raise RevocationEpochLagError(
            f"warrant {warrant.serial} has no revocation_epoch; cannot place on timeline"
        )

    epoch = warrant.revocation_epoch

    if epoch > crl.floor:
        # Warrant postdates local knowledge: CRL cannot speak to it.
        if introspect_ok is True:
            return True
        raise RevocationEpochLagError(
            f"warrant {warrant.serial} epoch {epoch} > local floor {crl.floor}; "
            f"not evaluable against held CRL"
        )

    if epoch < crl.floor:
        # The lag window. CRL absence does NOT imply non-revocation.
        if introspect_ok is True:
            return True
        if bounded_lag_proven:
            return warrant.serial not in crl.revoked_serials
        raise RevocationEpochLagError(
            f"warrant {warrant.serial} epoch {epoch} lags local floor {crl.floor} "
            f"by {crl.floor - epoch}; refusal is terminal for this floor"
        )

    # epoch == floor: sound to evaluate membership.
    return warrant.serial not in crl.revoked_serials
