# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-a-rotated-short-lived-credential-as-a-revocation-signal-rather-than-a-benign-refresh-the-cross-domain-warrant.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-a-rotated-short-lived-credential-as-a-revocation-signal-rather-than-a-benign-refresh-the-cross-domain-warrant.warrant.json
# Markdown SHA-256:   4f3c38e323117dc0fbfc30d505f403b691a21c3b48ed9a02e35718cd95664d9d
# Code AST SHA-256:   2d4fce015ca7499c8f2b1ee510c7eec61b2d7620d1b6dc389cc616471a379913
# Verified Classes:   RevocationPropagationGap, Warrant, VerifierState
# Defensive Guards:   5 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass


class RevocationPropagationGap(RuntimeError):
    """Raised when a rotated credential cannot be distinguished from a revoked one."""
    pass


@dataclass
class Warrant:
    subject: str
    epoch: int
    iat: float
    exp: float
    lineage_status: str | None  # None means issuer did not attest


@dataclass
class VerifierState:
    last_observed_revocation_epoch: int
    revocation_feed_last_seen: float
    max_staleness_seconds: float
    rotation_sweep_window_seconds: float


def verify_rotated_warrant(w: Warrant, v: VerifierState, now: float | None = None) -> bool:
    now = now if now is not None else time.time()

    if now >= w.exp:
        raise RevocationPropagationGap(f"warrant for {w.subject} expired at {w.exp}")

    # Staleness of the revocation feed invalidates any freshness claim.
    feed_age = now - v.revocation_feed_last_seen
    if feed_age > v.max_staleness_seconds:
        raise RevocationPropagationGap(
            f"revocation feed stale by {feed_age:.1f}s > {v.max_staleness_seconds}s; "
            f"cannot distinguish rotation from revocation for {w.subject}"
        )

    # Epoch must strictly outrun the last observed revocation epoch.
    if w.epoch <= v.last_observed_revocation_epoch:
        raise RevocationPropagationGap(
            f"credential epoch {w.epoch} <= observed revocation epoch "
            f"{v.last_observed_revocation_epoch} for {w.subject}"
        )

    # If the credential was minted inside a rotation sweep, lineage status is mandatory.
    within_sweep = (now - w.iat) <= v.rotation_sweep_window_seconds
    if within_sweep and w.lineage_status is None:
        raise RevocationPropagationGap(
            f"credential for {w.subject} minted within rotation window "
            f"({now - w.iat:.1f}s) without lineage_status attestation; "
            f"treating as revoked-pending-confirmation"
        )

    if w.lineage_status is not None and w.lineage_status != "active":
        raise RevocationPropagationGap(
            f"lineage_status={w.lineage_status!r} for {w.subject}; refusing to proceed"
        )

    return True
