# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-reject-a-peer-whose-revocation-has-not-yet-reached-it-propagation-lag-versus-token-rotation-in-federated-agent-meshes.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-reject-a-peer-whose-revocation-has-not-yet-reached-it-propagation-lag-versus-token-rotation-in-federated-agent-meshes.warrant.json
# Markdown SHA-256:   1d896cbb98cb977cf39f96d9afda2a5f0dac29e812ef6ee54785c1a49cb1cdbd
# Code AST SHA-256:   56e2f167d8b5c12b6f25f7e53daaadbda8d84e6263ff0dec151365caf84f3dd8
# Verified Classes:   RevocationBoundaryError, Warrant, Revocation, FreshnessProof
# Defensive Guards:   6 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Optional


class RevocationBoundaryError(RuntimeError):
    """Raised when a warrant's validity cannot be soundly established
    relative to the verifier's local revocation state."""


@dataclass(frozen=True)
class Warrant:
    issuer_id: str
    subject_id: str
    issuance_seq: int      # strictly increasing per (issuer, subject)
    iat: float             # wall clock; NOT authoritative for revocation
    exp: float


@dataclass(frozen=True)
class Revocation:
    issuer_id: str
    subject_id: str
    revoked_through_seq: int   # highest seq the issuer will ever mint
    effective_at: float


@dataclass(frozen=True)
class FreshnessProof:
    issuer_id: str
    subject_id: str
    observed_epoch: int        # issuer's revocation epoch the verifier has seen


def verify_warrant(
    w: Warrant,
    revocations: list[Revocation],
    freshness: Optional[FreshnessProof],
    issuer_epoch_at_issuance: int,
    now: float,
) -> None:
    if not (w.iat <= now < w.exp):
        raise RevocationBoundaryError(
            f"warrant {w.issuer_id}/{w.subject_id} seq={w.issuance_seq} "
            f"outside validity window iat={w.iat} exp={w.exp} now={now}"
        )

    # Hard rejection: sequence-based, never timestamp-based.
    for r in revocations:
        if r.issuer_id == w.issuer_id and r.subject_id == w.subject_id:
            if w.issuance_seq <= r.revoked_through_seq:
                raise RevocationBoundaryError(
                    f"warrant {w.issuer_id}/{w.subject_id} seq={w.issuance_seq} "
                    f"<= revoked_through_seq={r.revoked_through_seq}; "
                    f"revocation effective_at={r.effective_at} "
                    f"(iat={w.iat} is irrelevant across a rotation boundary)"
                )

    # Freshness: we must know the issuer's revocation state as of issuance.
    if freshness is None:
        raise RevocationBoundaryError(
            f"no freshness proof for {w.issuer_id}/{w.subject_id}; "
            f"cannot distinguish 'never revoked' from 'revocation not yet gossiped'"
        )
    if freshness.issuer_id != w.issuer_id or freshness.subject_id != w.subject_id:
        raise RevocationBoundaryError(
            f"freshness proof scope mismatch: proof={freshness.issuer_id}/"
            f"{freshness.subject_id} warrant={w.issuer_id}/{w.subject_id}"
        )
    if freshness.observed_epoch < issuer_epoch_at_issuance:
        raise RevocationBoundaryError(
            f"stale freshness: observed_epoch={freshness.observed_epoch} < "
            f"issuer_epoch_at_issuance={issuer_epoch_at_issuance}; defer and retry"
        )


def mint_warrant(
    issuer_id: str,
    subject_id: str,
    next_seq: int,
    revoked_through_seq: int,
    now: float,
    ttl: float,
) -> Warrant:
    # Re-check inside the signing transaction, not at admission.
    if next_seq <= revoked_through_seq:
        raise RevocationBoundaryError(
            f"refusing to mint {issuer_id}/{subject_id} seq={next_seq}: "
            f"revoked_through_seq={revoked_through_seq} (mint/revoke race)"
        )
    return Warrant(
        issuer_id=issuer_id,
        subject_id=subject_id,
        issuance_seq=next_seq,
        iat=now,
        exp=now + ttl,
    )
