# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-does-an-instance-detect-a-split-brain-revocation-of-a-federated-peer-s-warrant-when-a2a-trust-domains-gossip-at-different-epochs.md
# Remnant Warrant:    https://corpusfetch.com/c/how-does-an-instance-detect-a-split-brain-revocation-of-a-federated-peer-s-warrant-when-a2a-trust-domains-gossip-at-different-epochs.warrant.json
# Markdown SHA-256:   9f789d12ad62bc5ce51bc518e74ba50f70ebe3caf8621a5a7ded78ceb42f47cc
# Code AST SHA-256:   fa0c7a2cc5b5c9d9e417e9335238e406303df99f960f4cb72783c74f184740c7
# Verified Classes:   StaleEpochAuthorizationError, FreshnessReceipt, Warrant
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class StaleEpochAuthorizationError(RuntimeError):
    """Raised when a relying domain attempts to authorize a warrant whose
    issuance epoch it cannot prove it has advanced past via a freshness receipt."""


class FreshnessReceipt:
    def __init__(self, home_revocation_epoch: int, signed_by: str):
        self.home_revocation_epoch = home_revocation_epoch
        self.signed_by = signed_by


class Warrant:
    def __init__(self, peer_id: str, issued_epoch: int, expires_at: int):
        self.peer_id = peer_id
        self.issued_epoch = issued_epoch
        self.expires_at = expires_at


def authorize_delegation(
    warrant: Warrant,
    receipt: FreshnessReceipt | None,
    local_home_epoch_confirmed: int,
    revocation_horizon: int = 1,
) -> None:
    # Hard epoch barrier: refuse before any signature check if the warrant's
    # issuance epoch is older than what we can prove we've seen.
    required_epoch = warrant.issued_epoch + revocation_horizon
    if local_home_epoch_confirmed < required_epoch:
        raise StaleEpochAuthorizationError(
            f"peer {warrant.peer_id}: local confirmed epoch "
            f"{local_home_epoch_confirmed} < required {required_epoch}; "
            f"warrant may be revoked at home. Fail closed."
        )

    # A valid signature is not authorization. Demand a freshness receipt
    # proving home's revocation epoch has advanced past the warrant.
    if receipt is None:
        raise StaleEpochAuthorizationError(
            f"peer {warrant.peer_id}: no freshness receipt from home; "
            f"absence of a revocation record is not proof of non-revocation."
        )
    if receipt.home_revocation_epoch < required_epoch:
        raise StaleEpochAuthorizationError(
            f"peer {warrant.peer_id}: home revocation epoch "
            f"{receipt.home_revocation_epoch} < required {required_epoch}."
        )
    # Only here may delegation proceed.
