# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-does-an-instance-detect-that-a-peer-s-cached-capability-warrant-outlived-its-revocation-across-a-federation-boundary.md
# Remnant Warrant:    https://corpusfetch.com/c/how-does-an-instance-detect-that-a-peer-s-cached-capability-warrant-outlived-its-revocation-across-a-federation-boundary.warrant.json
# Markdown SHA-256:   387789efd45706264792d80d52702b87737591d852d5bbecd21f33808dbe206b
# Code AST SHA-256:   0986e78d3d4bcb75174fa25f4b5e318d68b741280d752791e11cfd335b6acebe
# Verified Classes:   WarrantStalenessError, RevocationSubscription, CachedWarrant
# Defensive Guards:   4 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass


class WarrantStalenessError(RuntimeError):
    """Raised when a cached cross-domain warrant cannot be shown fresh."""
    pass


@dataclass
class RevocationSubscription:
    issuer_domain: str
    last_heartbeat_ack: float  # monotonic seconds
    epoch: int


@dataclass
class CachedWarrant:
    warrant_id: str
    issuer_domain: str
    consumer_domain: str
    cached_at: float
    remaining_lifetime: float
    validated_epoch: int


DELTA_PROP = 40.0  # seconds; issuer's stated max propagation bound
HEARTBEAT_MAX_AGE = DELTA_PROP


def serve_from_cache(
    warrant: CachedWarrant,
    sub: RevocationSubscription,
    now: float,
) -> bool:
    # Local-domain warrants are locally verifiable.
    if warrant.issuer_domain == warrant.consumer_domain:
        return warrant.remaining_lifetime > 0.0

    # Cross-domain: require a fresh subscription AND bounded cache age.
    heartbeat_age = now - sub.last_heartbeat_ack
    if heartbeat_age > HEARTBEAT_MAX_AGE:
        raise WarrantStalenessError(
            f"revocation subscription to {sub.issuer_domain} stale by "
            f"{heartbeat_age:.1f}s (max {HEARTBEAT_MAX_AGE}s); cannot "
            f"establish validity of warrant {warrant.warrant_id}"
        )

    if warrant.validated_epoch != sub.epoch:
        raise WarrantStalenessError(
            f"warrant {warrant.warrant_id} validated under epoch "
            f"{warrant.validated_epoch}, current epoch {sub.epoch}; "
            f"a heartbeat was missed since validation"
        )

    cache_age = now - warrant.cached_at
    if cache_age + DELTA_PROP > warrant.remaining_lifetime:
        # Too close to expiry to trust the cache across the propagation gap.
        raise WarrantStalenessError(
            f"warrant {warrant.warrant_id} cache_age {cache_age:.1f}s + "
            f"Delta_prop {DELTA_PROP}s exceeds remaining_lifetime "
            f"{warrant.remaining_lifetime:.1f}s; revalidate synchronously"
        )

    return True


def dispatch_task(warrant: CachedWarrant, sub: RevocationSubscription) -> None:
    now = time.monotonic()
    if not serve_from_cache(warrant, sub, now):
        raise WarrantStalenessError(
            f"warrant {warrant.warrant_id} rejected: not servable from cache"
        )
    # ... dispatch with the verified warrant ...
