# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/which-invariants-prevent-attested-identity-handshake-collapse-when-revocation-propagation-races-mutual-verification-in-agent-to-agent-swarms.md
# Remnant Warrant:    https://corpusfetch.com/c/which-invariants-prevent-attested-identity-handshake-collapse-when-revocation-propagation-races-mutual-verification-in-agent-to-agent-swarms.warrant.json
# Markdown SHA-256:   0786990359f6dac43455fbd71397033c0871ca8450482300ce4dab6b75a0e0c8
# Code AST SHA-256:   eb1a9506ef5d55c0ce0e0e73c8000cc59b34c88d71ff4be248d9bfb0ad9499c0
# Verified Classes:   AttestationHandshakeError, Attestation, Manifest
# Defensive Guards:   5 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass


class AttestationHandshakeError(RuntimeError):
    """Raised when an attested-identity handshake violates a session invariant."""
    pass


@dataclass(frozen=True)
class Attestation:
    peer_id: str
    epoch: int
    not_after: float
    self_refreshed: bool


@dataclass(frozen=True)
class Manifest:
    epoch: int
    revoked: frozenset


def admit_session(
    att_a: Attestation,
    att_b: Attestation,
    manifest_a: Manifest,
    manifest_b: Manifest,
    now: float,
) -> bool:
    # Invariant 1: neither attestation may be self-refreshed.
    for att in (att_a, att_b):
        if att.self_refreshed:
            raise AttestationHandshakeError(
                f"peer {att.peer_id} presented a self-refreshed attestation; "
                "only RA-issued warrants are admissible"
            )

    # Invariant 2: both peers must have observed a manifest epoch that is
    # at least as new as the attestation epoch they present.
    for att, man in ((att_a, manifest_a), (att_b, manifest_b)):
        if man.epoch < att.epoch:
            raise AttestationHandshakeError(
                f"epoch skew: peer {att.peer_id} att epoch {att.epoch} "
                f"> manifest epoch {man.epoch}"
            )

    # Invariant 3: there must exist a common epoch e such that both
    # attestations are non-revoked and both peers' observed epochs are >= e.
    common_epoch = max(att_a.epoch, att_b.epoch)
    for man in (manifest_a, manifest_b):
        if man.epoch < common_epoch:
            raise AttestationHandshakeError(
                f"no common epoch: manifest {man.epoch} < required {common_epoch}"
            )
    for man in (manifest_a, manifest_b):
        if att_a.peer_id in man.revoked or att_b.peer_id in man.revoked:
            raise AttestationHandshakeError(
                f"revocation observed in manifest epoch {man.epoch}; "
                "handshake is non-retryable"
            )

    # Invariant 4: attestations must not be expired at admission time.
    for att in (att_a, att_b):
        if att.not_after <= now:
            raise AttestationHandshakeError(
                f"peer {att.peer_id} attestation expired at {att.not_after}"
            )

    return True
