# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-refuse-a-peer-whose-revocation-is-only-locally-visible-the-cross-domain-revocation-visibility-gap-in-federated-agent-trust.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-refuse-a-peer-whose-revocation-is-only-locally-visible-the-cross-domain-revocation-visibility-gap-in-federated-agent-trust.warrant.json
# Markdown SHA-256:   abd4f69dc60c65cb20a31014154c24fb5892b4464bdac2723019417c1fb64704
# Code AST SHA-256:   0f879c648f6f0a973d23c192dd78d59eaefc81a769741affaccb73fb3fcca1a7
# Verified Classes:   RevocationVisibilityError, PeerState, PeerWarrant
# Defensive Guards:   2 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass
from enum import Enum


class RevocationVisibilityError(RuntimeError):
    """Raised when a peer's revocation state cannot be established within the trust window."""
    pass


class PeerState(Enum):
    VALID = "valid"
    STALE = "stale"
    REVOKED = "revoked"


@dataclass
class PeerWarrant:
    peer_id: str
    issued_at: float
    last_revocation_poll: float
    cache_ttl: float
    propagation_sla: float
    clock_skew_margin: float = 5.0


def classify_peer(w: PeerWarrant, now: float, action_irreversible: bool) -> PeerState:
    age = now - w.last_revocation_poll
    if age > w.cache_ttl:
        # Cache expired without a successful re-poll: positive is no longer trustworthy.
        return PeerState.STALE
    gap_window = w.propagation_sla - w.clock_skew_margin
    if action_irreversible and age > gap_window:
        # We are inside the window where revocation could exist but not yet be visible.
        return PeerState.STALE
    return PeerState.VALID


def admit_submission(w: PeerWarrant, now: float, irreversible: bool) -> None:
    state = classify_peer(w, now, irreversible)
    if state is PeerState.REVOKED:
        raise RevocationVisibilityError(
            f"peer {w.peer_id} is revoked; submission refused"
        )
    if state is PeerState.STALE and irreversible:
        raise RevocationVisibilityError(
            f"peer {w.peer_id} revocation state unverified within trust window "
            f"(age={now - w.last_revocation_poll:.1f}s, sla={w.propagation_sla}s); "
            f"irreversible action quarantined, not admitted"
        )
    # VALID, or STALE + reversible: proceed (reversible actions tolerate the gap).
