# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-accepts-a-peer-s-signed-manifest-but-misses-the-revocation-epoch-that-invalidated-its-signing-key-mid-handoff.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-accepts-a-peer-s-signed-manifest-but-misses-the-revocation-epoch-that-invalidated-its-signing-key-mid-handoff.warrant.json
# Markdown SHA-256:   765cc2ad446b2935f953e000464f4cf2f6a83a68b2e21bc319846dbc1eabbd2c
# Code AST SHA-256:   24d4fdb9dd7a687aa1e404ad3621856663fb4a9a69ecdc266fad0c1d07173460
# Verified Classes:   RevocationEpochStale, IrreversibleCapabilityUnderStaleEpoch, Manifest, RevocationView
# Defensive Guards:   4 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Optional


class RevocationEpochStale(RuntimeError):
    """Raised when a manifest would be admitted against a revocation view older than its issuance."""
    pass


class IrreversibleCapabilityUnderStaleEpoch(RuntimeError):
    """Raised when a session attempts an irreversible action under an epoch that has since advanced."""
    pass


@dataclass(frozen=True)
class Manifest:
    peer_org: str
    signing_key_id: str
    issued_at: int          # authority-signed timestamp
    valid_until: int
    capabilities: frozenset[str]


@dataclass(frozen=True)
class RevocationView:
    epoch: int
    epoch_timestamp: int    # authority-signed timestamp of this epoch
    revoked_keys: frozenset[str]
    max_staleness: int      # seconds; poll_interval + propagation_delay bound


IRREVERSIBLE = frozenset({"payments.initiate", "data.delete", "key.rotate"})


def admit_manifest(manifest: Manifest, view: RevocationView, now: int) -> None:
    # Guard 1: epoch must be at least as new as the manifest's issuance.
    if view.epoch_timestamp < manifest.issued_at:
        raise RevocationEpochStale(
            f"revocation epoch {view.epoch} (t={view.epoch_timestamp}) predates "
            f"manifest issued_at={manifest.issued_at} for org {manifest.peer_org}"
        )
    # Guard 2: staleness bound must fit inside remaining validity for irreversible caps.
    if manifest.capabilities & IRREVERSIBLE:
        remaining = manifest.valid_until - now
        if view.max_staleness >= remaining:
            raise RevocationEpochStale(
                f"staleness bound {view.max_staleness}s not strictly below remaining "
                f"validity {remaining}s for irreversible capabilities"
            )
    # Guard 3: key must not be revoked in this view.
    if manifest.signing_key_id in view.revoked_keys:
        raise RevocationEpochStale(
            f"signing key {manifest.signing_key_id} revoked at epoch {view.epoch}"
        )


def execute_capability(
    capability: str,
    session_epoch: int,
    current_view: RevocationView,
    session_key_id: str,
) -> None:
    if capability in IRREVERSIBLE and current_view.epoch > session_epoch:
        if session_key_id in current_view.revoked_keys:
            raise IrreversibleCapabilityUnderStaleEpoch(
                f"session admitted at epoch {session_epoch} attempts {capability} under "
                f"epoch {current_view.epoch}; key {session_key_id} revoked — terminate session"
            )
