# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-reject-a-federated-peer-s-verifiable-credential-when-revocation-status-is-unverifiable-at-handshake-time.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-reject-a-federated-peer-s-verifiable-credential-when-revocation-status-is-unverifiable-at-handshake-time.warrant.json
# Markdown SHA-256:   15f545297ec5fc8ec0e33242f6c6a8694bcaae798788423172ca38eb6e3df329
# Code AST SHA-256:   9e1fc40ee6bcd538c3c85b827775b942dfcb6bcee23129f2a405ccbd06e63320
# Verified Classes:   RevocationUnverifiableError, StatusSnapshot, IssuerStatusMeta
# Defensive Guards:   5 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from datetime import datetime, timedelta


class RevocationUnverifiableError(RuntimeError):
    """Raised when non-revocation cannot be proven within the issuer's rotation window."""
    pass


@dataclass
class StatusSnapshot:
    issued_at: datetime
    revoked: bool
    handshake_nonce: str          # must match the live handshake, else replayable
    signature_valid: bool


@dataclass
class IssuerStatusMeta:
    rotation_period: timedelta    # issuer's own status-list republish interval
    max_propagation_lag: timedelta


def admit_federated_peer(
    snapshot: StatusSnapshot | None,
    meta: IssuerStatusMeta,
    live_nonce: str,
    now: datetime,
    authority_reachable: bool,
) -> bool:
    # Predicate B: status-verifiable within the issuer's own rotation window.
    if snapshot is None or not authority_reachable:
        # A-unknown: cannot prove non-revocation. Fail closed.
        raise RevocationUnverifiableError(
            "no reachable status authority and no in-bound snapshot; "
            "absence of a revocation signal is not evidence of non-revocation"
        )

    if not snapshot.signature_valid:
        raise RevocationUnverifiableError("status snapshot signature invalid")

    if snapshot.handshake_nonce != live_nonce:
        # A pre-revocation snapshot replayed into a post-revocation session.
        raise RevocationUnverifiableError(
            "status snapshot not bound to this handshake; replay suspected"
        )

    age = now - snapshot.issued_at
    if age >= meta.rotation_period:
        # Freshness subordination violated: snapshot may predate a published revocation.
        raise RevocationUnverifiableError(
            f"status snapshot age {age} >= issuer rotation {meta.rotation_period}; "
            "cannot exclude a revocation published since snapshot"
        )

    # Predicate A: non-revoked.
    if snapshot.revoked:
        raise RevocationUnverifiableError(
            "credential is revoked; emit propagation event to federation"
        )

    return True  # admit, with re-check scheduled at issued_at + rotation_period
