# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-a-peer-s-revocation-as-authoritative-before-its-own-cached-warrant-expires.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-a-peer-s-revocation-as-authoritative-before-its-own-cached-warrant-expires.warrant.json
# Markdown SHA-256:   dffa44a3c05f39516fa6a705c6744db4b2efffa24e2eb723d8a160d8b1259dff
# Code AST SHA-256:   b82db5a3126f2c854a419f2fe340e0eb06b80985fd9d3fccec931569bc1cf9e4
# Verified Classes:   RevocationEpochFault, WarrantTTLExceedsPropagationBound, Warrant, VerifierCache
# Defensive Guards:   3 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Optional


class RevocationEpochFault(RuntimeError):
    """Raised when a cached warrant's epoch is provably dead per an observed epoch."""


class WarrantTTLExceedsPropagationBound(ValueError):
    """Raised at issuance when a warrant would outlive its revocation propagation bound."""


@dataclass(frozen=True)
class Warrant:
    subject: str
    capability: str
    exp: float
    epoch: int


class VerifierCache:
    def __init__(self, propagation_bound: float, safety_margin: float = 5.0):
        self.propagation_bound = propagation_bound
        self.safety_margin = safety_margin
        self.max_observed_epoch = 0
        self._cache: dict[str, Warrant] = {}

    def issue(self, subject: str, capability: str, now: float, ttl: float) -> Warrant:
        # Invariant: a warrant must not outlive the issuer's revocation propagation bound.
        if ttl > self.propagation_bound + self.safety_margin:
            raise WarrantTTLExceedsPropagationBound(
                f"ttl={ttl}s exceeds propagation bound "
                f"{self.propagation_bound}s + margin {self.safety_margin}s; "
                f"stale-cache accept window would be non-zero"
            )
        w = Warrant(subject=subject, capability=capability, exp=now + ttl,
                    epoch=self.max_observed_epoch)
        self._cache[subject] = w
        return w

    def observe_epoch(self, epoch: int) -> None:
        # Monotonic: epochs never decrease.
        if epoch > self.max_observed_epoch:
            self.max_observed_epoch = epoch

    def validate_for_handoff(self, warrant: Warrant, now: float) -> None:
        # Terminal epoch fault takes precedence over expiry: it is repairable, expiry is not.
        if warrant.epoch < self.max_observed_epoch:
            raise RevocationEpochFault(
                f"warrant epoch {warrant.epoch} < observed epoch "
                f"{self.max_observed_epoch}; peer cache is stale, "
                f"re-request issuance from authority (do NOT retry same warrant)"
            )
        if now >= warrant.exp:
            raise RevocationEpochFault(
                f"warrant expired at {warrant.exp} (now={now}); lease lapsed, "
                f"not a revocation — re-issue, do not treat as epoch fault"
            )
