# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-a-federated-credential-revocation-as-effective-before-its-local-trust-cache-converges.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-a-federated-credential-revocation-as-effective-before-its-local-trust-cache-converges.warrant.json
# Markdown SHA-256:   689e9dfa1ed1c229a1c57a4d7a31dc311de421a734e283356d5c642365ef43fd
# Code AST SHA-256:   c6aafd538e498c4c7ec8a21cdac6181eb1fb75f019062fdf3ca8f94c26ba2970
# Verified Classes:   StaleTrustCacheError, Warrant, TrustView
# Defensive Guards:   4 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass, field


class StaleTrustCacheError(RuntimeError):
    """Raised when a warrant would be forwarded on the strength of a cached
    trust view that cannot bound the revocation staleness of the destination."""
    pass


@dataclass
class Warrant:
    warrant_id: str
    issuer_epoch: int          # monotonic revocation epoch at issuance-time view
    remaining_lifetime_s: float


@dataclass
class TrustView:
    last_sync_issuer_s: float          # seconds since last anti-entropy sync
    gossip_heartbeat_gap_s: float      # worst peer heartbeat gap
    known_destination_epoch: int | None


def assert_forward_admissible(
    warrant: Warrant,
    view: TrustView,
    delta_sync_s: float,
) -> None:
    """Enforce the forward-admissibility invariant before delegating a warrant.

    A converged local cache is NOT sufficient grounds to forward: the
    destination's view may be staler than ours, and the chain can outrun
    the tombstone. We require an explicit freshness bound the destination
    can re-check.
    """
    if view.last_sync_issuer_s >= warrant.remaining_lifetime_s:
        raise StaleTrustCacheError(
            f"warrant {warrant.warrant_id}: sync age "
            f"{view.last_sync_issuer_s:.1f}s >= remaining lifetime "
            f"{warrant.remaining_lifetime_s:.1f}s; revocation could propagate "
            f"entirely within warrant life unseen"
        )

    if view.gossip_heartbeat_gap_s >= delta_sync_s:
        raise StaleTrustCacheError(
            f"warrant {warrant.warrant_id}: peer heartbeat gap "
            f"{view.gossip_heartbeat_gap_s:.1f}s exceeds convergence bound "
            f"{delta_sync_s:.1f}s; a partitioned peer is outside the guarantee"
        )

    if view.known_destination_epoch is None:
        raise StaleTrustCacheError(
            f"warrant {warrant.warrant_id}: destination revocation epoch "
            f"unknown; cannot bind freshness obligation to destination"
        )

    if view.known_destination_epoch < warrant.issuer_epoch:
        raise StaleTrustCacheError(
            f"warrant {warrant.warrant_id}: destination epoch "
            f"{view.known_destination_epoch} behind issuer epoch "
            f"{warrant.issuer_epoch}; destination cache is stale by construction"
        )


def forward_warrant(warrant: Warrant, view: TrustView, delta_sync_s: float) -> str:
    assert_forward_admissible(warrant, view, delta_sync_s)
    return f"forwarded:{warrant.warrant_id}@epoch={warrant.issuer_epoch}"
