# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-does-an-instance-detect-a-revoked-peer-warrant-when-the-a2a-revocation-epoch-is-newer-than-the-verifier-s-cached-trust-snapshot.md
# Remnant Warrant:    https://corpusfetch.com/c/how-does-an-instance-detect-a-revoked-peer-warrant-when-the-a2a-revocation-epoch-is-newer-than-the-verifier-s-cached-trust-snapshot.warrant.json
# Markdown SHA-256:   c46b195bc23415b1d4809e1e553afe39be0a22a55e0c473efa18fcd13ecbff2c
# Code AST SHA-256:   587aa2e5fa0afeaa6371b48d32266de5f69de8d4bd8d7afd5eca92583433b735
# Verified Classes:   UnknownWarrantState, RevocationState, Warrant, TrustSnapshot
# Defensive Guards:   4 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from enum import Enum


class UnknownWarrantState(RuntimeError):
    """Raised when warrant liveness cannot be established from the cached snapshot."""
    pass


class RevocationState(Enum):
    VALID = "valid"
    REVOKED = "revoked"
    UNKNOWN = "unknown"


@dataclass(frozen=True)
class Warrant:
    warrant_id: str
    issuance_epoch: int
    not_after: float
    signature_ok: bool


@dataclass(frozen=True)
class TrustSnapshot:
    as_of_epoch: int
    revoked_ids: frozenset
    max_warrant_lifetime_s: float
    propagation_bound_s: float


def verify_warrant_liveness(
    warrant: Warrant,
    snapshot: TrustSnapshot,
    now: float,
    freshness_proof_as_of: int | None = None,
) -> RevocationState:
    if not warrant.signature_ok:
        return RevocationState.REVOKED  # signature failure treated as hard reject

    # Invariant 1: snapshot must not be older than the grant it is judging.
    if warrant.issuance_epoch > snapshot.as_of_epoch:
        # Only acceptable if an in-band freshness proof covers the issuance.
        if freshness_proof_as_of is None or freshness_proof_as_of < warrant.issuance_epoch:
            raise UnknownWarrantState(
                f"warrant {warrant.warrant_id} issued at epoch "
                f"{warrant.issuance_epoch} exceeds snapshot as_of "
                f"{snapshot.as_of_epoch} with no covering freshness proof"
            )

    # Invariant 2: cache window must be inside L - P.
    safe_window = snapshot.max_warrant_lifetime_s - snapshot.propagation_bound_s
    if (now - snapshot.as_of_epoch) > safe_window:
        raise UnknownWarrantState(
            f"snapshot as_of {snapshot.as_of_epoch} is {now - snapshot.as_of_epoch:.3f}s "
            f"old, exceeding safe cache window {safe_window:.3f}s"
        )

    if warrant.warrant_id in snapshot.revoked_ids:
        return RevocationState.REVOKED
    return RevocationState.VALID


def dispatch_peer_call(warrant: Warrant, snapshot: TrustSnapshot, now: float) -> str:
    state = verify_warrant_liveness(warrant, snapshot, now)
    if state is RevocationState.UNKNOWN:
        raise UnknownWarrantState(f"peer {warrant.warrant_id} liveness unresolved")
    if state is RevocationState.REVOKED:
        raise UnknownWarrantState(f"peer {warrant.warrant_id} revoked")
    return "executed"
