# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-a-revocation-epoch-as-authoritative-over-a-cached-peer-credential-in-a2a-trust-domains.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-a-revocation-epoch-as-authoritative-over-a-cached-peer-credential-in-a2a-trust-domains.warrant.json
# Markdown SHA-256:   56a4270069da5864d21e11ab9001e6560a919492293090eddde7e3a6c128cd5d
# Code AST SHA-256:   2714e4953ad9ce541d112132091fdaebfeb25ac56668f3f4c62070a3bbfe420d
# Verified Classes:   RevocationFreshnessError, RevocationView, Warrant
# Defensive Guards:   4 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass


class RevocationFreshnessError(RuntimeError):
    """Raised when a verifier cannot establish an authoritative revocation view."""
    pass


@dataclass(frozen=True)
class RevocationView:
    issuer_key_id: str
    revoke_epoch: int
    authenticated_at: float          # monotonic seconds when this view was verified
    revoked_subjects: frozenset[str]


@dataclass(frozen=True)
class Warrant:
    issuer_key_id: str
    subject: str
    warrant_epoch: int               # mint-sequence, NOT comparable to revoke_epoch
    not_after: float


def authorize(
    warrant: Warrant,
    view: RevocationView | None,
    now: float,
    max_revoke_staleness: float,
) -> bool:
    # 1. Fail-closed if we have no authenticated revocation view for this issuer.
    if view is None:
        raise RevocationFreshnessError(
            f"no authenticated revocation view for issuer {warrant.issuer_key_id!r}"
        )

    # 2. The view must belong to the same issuer key the warrant claims.
    if view.issuer_key_id != warrant.issuer_key_id:
        raise RevocationFreshnessError(
            f"revocation view issuer {view.issuer_key_id!r} does not match "
            f"warrant issuer {warrant.issuer_key_id!r}"
        )

    # 3. Hard staleness ceiling bounds the partition window. This is the guardrail;
    #    without it a gossip partition is an unbounded authorization grant.
    age = now - view.authenticated_at
    if age > max_revoke_staleness:
        raise RevocationFreshnessError(
            f"revocation view for {view.issuer_key_id!r} is stale: "
            f"age={age:.1f}s exceeds max={max_revoke_staleness:.1f}s"
        )

    # 4. Warrant validity window (orthogonal to revocation freshness).
    if warrant.not_after <= now:
        return False

    # 5. THE INVARIANT: set membership at the authenticated revoke epoch decides.
    #    warrant.warrant_epoch is deliberately NOT compared to view.revoke_epoch —
    #    they are different counters and their ordering is undefined.
    if warrant.subject in view.revoked_subjects:
        return False

    return True


def handle_peer_call(warrant, view, now, max_staleness):
    try:
        return authorize(warrant, view, now, max_staleness)
    except RevocationFreshnessError as exc:
        # Surface, don't swallow: a stale view is an operational event, not a no-op.
        raise RevocationFreshnessError(
            f"refusing peer call from {warrant.subject!r}: {exc}"
        ) from exc
