# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-refuse-a-peer-s-cached-warrant-revocation-propagation-lag-across-federated-a2a-meshes-with-short-lived-credential-rotation.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-refuse-a-peer-s-cached-warrant-revocation-propagation-lag-across-federated-a2a-meshes-with-short-lived-credential-rotation.warrant.json
# Markdown SHA-256:   1f4df3ff5ef510ff95b36124ccab0ee82819cb7fda88e1fb8f82323a58b375a9
# Code AST SHA-256:   b26c65fb07a285e6e66189773ab1d6e84cb076424080904f6feb86ba320e8069
# Verified Classes:   RevocationFreshnessError, Warrant
# Defensive Guards:   4 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass


class RevocationFreshnessError(RuntimeError):
    """Raised when a warrant's residual life exceeds the revocation horizon
    of the verifier's slowest delegation link, or when an epoch regression
    indicates replay of a pre-revocation warrant."""


@dataclass(frozen=True)
class Warrant:
    issuer: str
    subject: str
    epoch: int          # issuer revocation epoch at mint time
    issued_at: float    # monotonic seconds
    ttl: float          # seconds

    def residual(self, now: float) -> float:
        return (self.issued_at + self.ttl) - now


def verify_warrant(
    warrant: Warrant,
    now: float,
    local_max_epoch_seen: int,
    l_max: float,                 # measured worst-case propagation latency to this verifier
    issuer_epoch_confirmed_at: float | None,
) -> bool:
    # Regime 3: epoch regression -> replay or compromised instance re-presenting.
    if warrant.epoch < local_max_epoch_seen:
        raise RevocationFreshnessError(
            f"epoch regression: warrant epoch {warrant.epoch} < seen {local_max_epoch_seen} "
            f"from issuer {warrant.issuer}; refusing as replay"
        )

    residual = warrant.residual(now)
    if residual <= 0:
        return False  # ordinary expiry, not a freshness violation

    # Regime 1: residual life shorter than revocation horizon -> safe on cache.
    if residual <= l_max:
        return True

    # Regime 2: warrant outlives revocation horizon. Require fresh epoch proof.
    fresh = (
        issuer_epoch_confirmed_at is not None
        and (now - issuer_epoch_confirmed_at) <= l_max
        and local_max_epoch_seen >= warrant.epoch
    )
    if not fresh:
        raise RevocationFreshnessError(
            f"warrant from {warrant.issuer} has residual {residual:.1f}s > L_max {l_max:.1f}s "
            f"and issuer epoch not confirmed within horizon; refusing on cached trust"
        )
    return True


def admit_delegation(parent: Warrant, child: Warrant, parent_l_max: float) -> None:
    # A sub-delegation may not extend usable life beyond the tightest link's horizon.
    if child.ttl > parent_l_max:
        raise RevocationFreshnessError(
            f"sub-delegation TTL {child.ttl:.1f}s exceeds upstream L_max {parent_l_max:.1f}s; "
            f"revocation lag would compound across hops"
        )
    if child.epoch < parent.epoch:
        raise RevocationFreshnessError(
            f"child epoch {child.epoch} older than parent epoch {parent.epoch}; "
            f"cannot narrow authority by rolling back revocation state"
        )
