# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-refuse-to-trust-a-peer-s-still-valid-capability-attestation-after-revocation-propagation-lag.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-refuse-to-trust-a-peer-s-still-valid-capability-attestation-after-revocation-propagation-lag.warrant.json
# Markdown SHA-256:   105c6d6c173a3d9a3c0585c87e95934dd2455e264a7f2c1daea8a825bc2a3b9f
# Code AST SHA-256:   b31c1fd08ecbc62dc39538b2c51704418544f3da73b34b056bfc0eefb877cf8d
# Verified Classes:   RevocationHorizonExceeded, Warrant, RevocationView
# Defensive Guards:   1 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass


class RevocationHorizonExceeded(RuntimeError):
    """Raised when a warrant's remaining TTL exceeds the detection horizon,
    meaning B cannot certify non-revocation and must not accept on provenance alone."""
    pass


@dataclass(frozen=True)
class Warrant:
    subject: str
    capability: str
    not_after: float          # absolute epoch seconds
    issuer: str


@dataclass(frozen=True)
class RevocationView:
    last_successful_poll: float   # epoch seconds
    staleness_tolerance: float    # seconds B permits its view to lag
    issuer_bound: float           # issuer's *claimed* worst-case propagation bound


def detection_horizon(view: RevocationView, now: float) -> float:
    """Worst-case seconds from 'revocation issued now' to 'B would observe it'.
    Grows as the last poll recedes."""
    view_age = now - view.last_successful_poll
    return view.issuer_bound + view.staleness_tolerance + view_age


def authorize(w: Warrant, view: RevocationView, revoked: set[tuple[str, str]],
              now: float) -> bool:
    # Provenance checks (cheap, local, deterministic).
    if now >= w.not_after:
        return False
    if (w.subject, w.capability) in revoked:
        return False

    # Authority-currency check: can we certify non-revocation for the whole
    # remaining life of this warrant?
    horizon = detection_horizon(view, now)
    remaining = w.not_after - now
    if remaining > horizon:
        # A revocation issued right now might not reach us before this warrant
        # is used again. Provenance-valid is insufficient.
        raise RevocationHorizonExceeded(
            f"warrant {w.subject}:{w.capability} remaining={remaining:.0f}s "
            f"exceeds detection horizon={horizon:.0f}s "
            f"(issuer_bound={view.issuer_bound:.0f}s, "
            f"staleness={view.staleness_tolerance:.0f}s, "
            f"view_age={now - view.last_successful_poll:.0f}s); "
            f"refuse or force synchronous revocation check"
        )
    return True


def poll_and_authorize(w: Warrant, view: RevocationView,
                       revoked: set[tuple[str, str]], now: float) -> bool:
    """Wrap authorize with a poll-freshness gate: if the view is too stale
    relative to the warrant's remaining life, fail closed before deciding."""
    if now - view.last_successful_poll > view.staleness_tolerance:
        # The view is stale; recompute horizon and let authorize decide.
        # If it cannot certify, authorize raises; we do not silently accept.
        pass
    return authorize(w, view, revoked, now)


if __name__ == "__main__":
    now = 1_000_000.0
    view = RevocationView(
        last_successful_poll=now - 300,   # polled 300s ago
        staleness_tolerance=300.0,
        issuer_bound=120.0,
    )
    w = Warrant(subject="W", capability="write:/data/staging",
                not_after=now + 3600, issuer="I")
    try:
        poll_and_authorize(w, view, revoked=set(), now=now)
    except RevocationHorizonExceeded as e:
        print(f"REFUSED: {e}")
