# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-refuse-a-delegated-warrant-whose-revocation-epoch-is-unverifiable-but-whose-signature-chain-is-intact.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-refuse-a-delegated-warrant-whose-revocation-epoch-is-unverifiable-but-whose-signature-chain-is-intact.warrant.json
# Markdown SHA-256:   c5b3811ff56dcd83628a51fe4643f64a0e51feba8eee2c35fee693170a2db36e
# Code AST SHA-256:   8808c1980926eef80ef938c77065520b5e6212d8187726a7912da71606ff1102
# Verified Classes:   EpochUnverifiableError, EpochRegressionError, Warrant, ReceiverView
# Defensive Guards:   4 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Optional


class EpochUnverifiableError(RuntimeError):
    """Raised when a delegated warrant's revocation epoch cannot be reconciled
    with the receiver's knowledge epoch within the freshness bound."""
    pass


class EpochRegressionError(RuntimeError):
    """Raised when the issuer's revocation epoch moves backwards, invalidating
    all outstanding epoch comparisons."""
    pass


@dataclass(frozen=True)
class Warrant:
    lineage_root: str
    revocation_epoch: int          # mint-time epoch, signed, immutable
    scope: frozenset
    budget: int
    reversible_only: bool


@dataclass(frozen=True)
class ReceiverView:
    known_epoch: int               # last epoch confirmed from issuer
    attested_through: int          # signed non-revocation attestation covers [_, attested_through]
    delta_max: int                 # declared freshness bound


def authorize_effect(
    warrant: Warrant,
    view: ReceiverView,
    action_is_irreversible: bool,
    issuer_epoch_now: int,
) -> bool:
    # Guard 0: epoch regression at issuer invalidates everything.
    if issuer_epoch_now < view.known_epoch:
        raise EpochRegressionError(
            f"issuer epoch regressed {view.known_epoch} -> {issuer_epoch_now}; "
            f"fail closed and re-issue all warrants for {warrant.lineage_root}"
        )

    # Guard 1: receiver must be at least as current as the warrant claims.
    if view.known_epoch < warrant.revocation_epoch:
        gap = warrant.revocation_epoch - view.known_epoch
        if action_is_irreversible:
            raise EpochUnverifiableError(
                f"receiver epoch {view.known_epoch} behind warrant epoch "
                f"{warrant.revocation_epoch} (gap {gap}); irreversible action refused"
            )
        # reversible action: allowed, but caller must taint derived state
        return True  # taint bit set by caller

    # Guard 2: gap between warrant epoch and attested coverage must be bounded.
    gap = view.known_epoch - warrant.revocation_epoch
    if view.attested_through < view.known_epoch:
        if action_is_irreversible:
            raise EpochUnverifiableError(
                f"no non-revocation attestation covering "
                f"[{warrant.revocation_epoch}, {view.known_epoch}] for "
                f"{warrant.lineage_root}; irreversible action refused"
            )
    if gap > view.delta_max and action_is_irreversible:
        raise EpochUnverifiableError(
            f"freshness gap {gap} exceeds delta_max {view.delta_max}; "
            f"irreversible action refused pending re-poll"
        )

    return True
