# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-revokes-an-agent-attestation-while-a-downstream-federation-peer-is-mid-handshake.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-revokes-an-agent-attestation-while-a-downstream-federation-peer-is-mid-handshake.warrant.json
# Markdown SHA-256:   c3ffe5866cf5c408d2adf1b187db31acb523af10323a4b8ad17e8a82648cee48
# Code AST SHA-256:   58f9a4fa8d889bdeb7bd87b5be4459070abc68eb72149a4131a4773f8ce0a135
# Verified Classes:   RevocationStateError, RevocationSnapshot, FederationHandshake
# Defensive Guards:   3 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass


class RevocationStateError(RuntimeError):
    """Raised when a relying party cannot confirm fresh, gap-free revocation state."""
    pass


@dataclass
class RevocationSnapshot:
    issuer: str
    epoch: int
    fetched_at: float
    revoked_subjects: frozenset


class FederationHandshake:
    def __init__(self, max_cache_age_s: float, expected_epoch: int):
        self.max_cache_age_s = max_cache_age_s
        self.expected_epoch = expected_epoch

    def commit(self, subject: str, snapshot: RevocationSnapshot) -> None:
        age = time.monotonic() - snapshot.fetched_at
        if age > self.max_cache_age_s:
            raise RevocationStateError(
                f"revocation snapshot for {snapshot.issuer} is {age:.1f}s old, "
                f"exceeds budget {self.max_cache_age_s}s; refusing to commit handshake"
            )
        if snapshot.epoch != self.expected_epoch:
            raise RevocationStateError(
                f"revocation epoch gap for {snapshot.issuer}: expected "
                f"{self.expected_epoch}, got {snapshot.epoch}; a revocation event "
                f"was likely lost in propagation"
            )
        if subject in snapshot.revoked_subjects:
            raise RevocationStateError(
                f"subject {subject} revoked by {snapshot.issuer} at epoch "
                f"{snapshot.epoch}; handshake must not commit"
            )
