# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/why-does-an-instance-keep-trusting-a-revoked-delegate-after-the-warrant-s-expiry-propagates-across-a-federated-boundary.md
# Remnant Warrant:    https://corpusfetch.com/c/why-does-an-instance-keep-trusting-a-revoked-delegate-after-the-warrant-s-expiry-propagates-across-a-federated-boundary.warrant.json
# Markdown SHA-256:   abcc5edfdabe463fc268696d710a78b8927147acf8dff692e6041563feb8c644
# Code AST SHA-256:   907481187317018940464fade82b007581e0d4713debbde960f8c3e3be419ae7
# Verified Classes:   StaleRevocationViewError, Warrant, RevocationView
# Defensive Guards:   2 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass


class StaleRevocationViewError(RuntimeError):
    """Raised when a verifier cannot prove its revocation view is fresh enough
    to admit a foreign-rooted delegation warrant."""
    pass


@dataclass
class Warrant:
    root_domain: str
    root_revocation_epoch: int   # epoch attested by root at mint time
    max_staleness: float         # seconds; issuer-guaranteed propagation bound
    exp: float


@dataclass
class RevocationView:
    epoch: int
    last_sync: float


def admit_foreign_warrant(
    warrant: Warrant,
    local_view: RevocationView,
    now: float,
    fetch_fn,  # callable(domain) -> RevocationView, may raise on timeout
) -> bool:
    if now > warrant.exp:
        return False  # expired; not our concern here

    def fresh_enough(view: RevocationView) -> bool:
        # Must have seen a view at least as new as mint time AND be within the
        # issuer's staleness bound. Epoch alone is insufficient: a revocation
        # issued after mint advances the root epoch past the attested value.
        return (
            view.epoch >= warrant.root_revocation_epoch
            and (now - view.last_sync) <= warrant.max_staleness
        )

    if fresh_enough(local_view):
        return True

    # Local view is stale relative to the warrant's freshness contract.
    try:
        synced = fetch_fn(warrant.root_domain)
    except TimeoutError as exc:
        raise StaleRevocationViewError(
            f"revocation fetch for {warrant.root_domain} timed out; "
            f"cannot prove freshness, failing closed"
        ) from exc

    if not fresh_enough(synced):
        raise StaleRevocationViewError(
            f"post-fetch view for {warrant.root_domain} still stale: "
            f"epoch={synced.epoch} need>={warrant.root_revocation_epoch}, "
            f"age={now - synced.last_sync:.2f}s bound={warrant.max_staleness}s"
        )

    # Fresh; caller re-checks the (now-current) revocation set for the root key.
    return True
