# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-a-delegated-capability-whose-revocation-epoch-is-older-than-its-own-grant-epoch.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-a-delegated-capability-whose-revocation-epoch-is-older-than-its-own-grant-epoch.warrant.json
# Markdown SHA-256:   2b80584861305fe6bf2011181ca0b336794a9f23b2a30fc307d0915a78fabfda
# Code AST SHA-256:   1d5fb36e8b0f557b7694f51adcb208edb20edc4113f0b0906d0a98d1cadf7a2c
# Verified Classes:   StaleEpochError
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class StaleEpochError(RuntimeError):
    """Raised when a receiver's revocation epoch is behind the warrant's grant epoch.

    The receiver cannot bound its own uncertainty about revocations that
    predate the grantor's decision to delegate, so it must refuse rather
    than execute against an unverifiable authorization basis.
    """

    def __init__(self, grant_epoch: int, observed_epoch: int, warrant_id: str):
        self.grant_epoch = grant_epoch
        self.observed_epoch = observed_epoch
        self.warrant_id = warrant_id
        super().__init__(
            f"warrant {warrant_id}: observed_epoch={observed_epoch} < "
            f"grant_epoch={grant_epoch}; causality inversion, refuse"
        )


def admit_warrant(warrant, observed_epoch: int, epoch_heartbeat_max_age_s: float,
                  heartbeat_age_s: float) -> None:
    """Gate execution on the epoch-dominance invariant.

    observed_epoch must dominate grant_epoch, OR the receiver must hold a
    fresh signed heartbeat from the root proving it is not partitioned.
    A stale heartbeat does not license the tolerance relaxation.
    """
    delta = warrant.grant_epoch - observed_epoch
    if delta <= 0:
        return  # safe: receiver at or ahead of grant point

    # Inversion detected. Tolerance is only sound with a fresh root heartbeat.
    heartbeat_fresh = heartbeat_age_s <= epoch_heartbeat_max_age_s
    if heartbeat_fresh and delta <= 1:
        # Heartbeat bounds the gap to a single in-flight revocation we can
        # reconcile before next use; still log, but permit.
        return

    raise StaleEpochError(
        grant_epoch=warrant.grant_epoch,
        observed_epoch=observed_epoch,
        warrant_id=warrant.warrant_id,
    )


def attenuate(parent_warrant, narrowed_scope, attenuator_observed_epoch: int):
    """Attenuation must not launder a stale epoch into a fresh-looking warrant."""
    if attenuator_observed_epoch < parent_warrant.grant_epoch:
        raise StaleEpochError(
            grant_epoch=parent_warrant.grant_epoch,
            observed_epoch=attenuator_observed_epoch,
            warrant_id=parent_warrant.warrant_id,
        )
    return parent_warrant.derive(
        scope=narrowed_scope,
        grant_epoch=max(parent_warrant.grant_epoch, attenuator_observed_epoch),
    )
