# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-does-an-instance-detect-a-revoked-warrant-mid-handoff-when-credential-rotation-outruns-the-revocation-gossip-window.md
# Remnant Warrant:    https://corpusfetch.com/c/how-does-an-instance-detect-a-revoked-warrant-mid-handoff-when-credential-rotation-outruns-the-revocation-gossip-window.warrant.json
# Markdown SHA-256:   f85cee0ec1bd91f339d0442a295b533c4b2a09e3f9d6bb6474cc716e2c2fa022
# Code AST SHA-256:   a5785cbdd30a2d17b2349b7560a13e4c2be61789057c46e88463b777f152ffbf
# Verified Classes:   WarrantSupersededByRevocation, Warrant, Verifier
# Defensive Guards:   5 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass


class WarrantSupersededByRevocation(RuntimeError):
    """Raised when a presented warrant's revocation watermark predates the
    verifier's last-seen revocation watermark. This is terminal: the holder
    must not retry with the same or older warrant, and the rotation loop must
    halt pending operator/orchestrator review."""
    pass


@dataclass(frozen=True)
class Warrant:
    subject: str
    not_before: float
    not_after: float
    revocation_watermark: int  # monotonic issuer-published revocation counter at mint time
    signature_valid: bool


class Verifier:
    def __init__(self, last_seen_revocation_watermark: int, t_gossip_max: float,
                 t_rotate: float):
        self.last_seen_revocation_watermark = last_seen_revocation_watermark
        self.t_gossip_max = t_gossip_max
        self.t_rotate = t_rotate

    def admit(self, w: Warrant, now: float) -> None:
        if not w.signature_valid:
            raise WarrantSupersededByRevocation(
                f"invalid signature for subject={w.subject!r}"
            )
        if not (w.not_before <= now <= w.not_after):
            raise WarrantSupersededByRevocation(
                f"warrant outside validity window subject={w.subject!r}"
            )
        # The boundary check: mint-time authority vs verify-time authority.
        if w.revocation_watermark < self.last_seen_revocation_watermark:
            raise WarrantSupersededByRevocation(
                f"subject={w.subject!r} warrant watermark "
                f"{w.revocation_watermark} predates verifier watermark "
                f"{self.last_seen_revocation_watermark}; rotation likely "
                f"outran revocation gossip (T_rotate={self.t_rotate}s, "
                f"T_gossip_max={self.t_gossip_max}s)"
            )
        # Legacy path: no watermark and no proof that rotation is slow enough.
        if w.revocation_watermark == 0 and self.t_rotate < self.t_gossip_max:
            raise WarrantSupersededByRevocation(
                f"subject={w.subject!r} presented unwatermarked warrant while "
                f"T_rotate < T_gossip_max; cannot bound revocation leak"
            )
        # Admitted: authority is fresh relative to every revocation we know of.
        return None


def rotation_loop(verifier: Verifier, holder_subject: str,
                  mint_fn, present_fn, max_rotations: int = 5) -> None:
    """Rotation loop that treats revocation-supersession as terminal."""
    for _ in range(max_rotations):
        w = mint_fn(holder_subject)
        try:
            verifier.admit(w, now=time.time())
            present_fn(w)
        except WarrantSupersededByRevocation:
            # Terminal: do NOT retry. Escalate to orchestrator.
            raise
        time.sleep(max(0.0, verifier.t_rotate - 10.0))
