# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/why-does-an-instance-accept-a-revoked-peer-manifest-when-the-revocation-epoch-advances-during-an-in-flight-a2a-handoff.md
# Remnant Warrant:    https://corpusfetch.com/c/why-does-an-instance-accept-a-revoked-peer-manifest-when-the-revocation-epoch-advances-during-an-in-flight-a2a-handoff.warrant.json
# Markdown SHA-256:   5aaddfd4e9c2dd8b51d8d9aa0f9526e4d9369a4c47ad6bdf6511f3db3172e92c
# Code AST SHA-256:   2626a64c282998be61cb6c9c6d1118c775269c7923cff3a57313ce255580b93c
# Verified Classes:   RevokedManifestError, PinnedManifest
# Defensive Guards:   4 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Optional

class RevokedManifestError(RuntimeError):
    """Raised when a peer manifest is superseded by a newer issuer epoch,
    or when the epoch head cannot be confirmed within max_staleness."""

@dataclass(frozen=True)
class PinnedManifest:
    agent_id: str
    manifest_hash: str
    epoch: int
    fetch_time: float

def authorize_hop(
    pinned: PinnedManifest,
    issuer_current_epoch: Optional[int],
    now: float,
    max_staleness: float,
    warrant_epoch: int,
) -> None:
    # 1. Fail closed if we cannot confirm the issuer's current epoch.
    if issuer_current_epoch is None:
        raise RevokedManifestError(
            f"cannot confirm epoch for {pinned.agent_id}; failing closed"
        )
    if now - pinned.fetch_time > max_staleness:
        raise RevokedManifestError(
            f"pinned manifest for {pinned.agent_id} stale "
            f"({now - pinned.fetch_time:.0f}s > {max_staleness:.0f}s)"
        )
    # 2. Supersession check: a newer issuer epoch invalidates the pin,
    #    even though the signature still verifies.
    if issuer_current_epoch > pinned.epoch:
        raise RevokedManifestError(
            f"manifest for {pinned.agent_id} superseded: "
            f"pinned_epoch={pinned.epoch} < issuer_epoch={issuer_current_epoch}"
        )
    # 3. Warrant must have been minted against the current epoch tuple.
    if warrant_epoch != pinned.epoch:
        raise RevokedManifestError(
            f"warrant epoch {warrant_epoch} != pinned epoch {pinned.epoch} "
            f"for {pinned.agent_id}"
        )
