# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-does-an-instance-detect-stale-capability-warrants-during-federated-registry-synchronization-across-trust-domains.md
# Remnant Warrant:    https://corpusfetch.com/c/how-does-an-instance-detect-stale-capability-warrants-during-federated-registry-synchronization-across-trust-domains.warrant.json
# Markdown SHA-256:   7096d62f3526154301a71ba5576511949308c033fc537669f819aea2e9a698c7
# Code AST SHA-256:   ea8d3379e56aa05386cc010d3fc2a7c9e194130a9c8d61123074f0ba985ee07a
# Verified Classes:   WarrantStatusIndeterminate, AuthorizationState, Warrant, RevocationView, FederatedAuthz
# Defensive Guards:   2 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass
from enum import Enum


class WarrantStatusIndeterminate(RuntimeError):
    """Raised when revocation-cursor freshness cannot be established within T_probe."""
    pass


class AuthorizationState(Enum):
    AUTHORIZED = "authorized"
    REVOKED = "revoked"
    UNKNOWN = "unknown"


@dataclass(frozen=True)
class Warrant:
    warrant_id: str
    not_after: float
    required_epoch: int          # issuer-declared min revocation epoch consumer must hold


@dataclass(frozen=True)
class RevocationView:
    cursor_epoch: int
    feed_age_s: float
    revoked_ids: frozenset


class FederatedAuthz:
    def __init__(self, max_rev_lag_s: float, probe_timeout_s: float):
        self.max_rev_lag_s = max_rev_lag_s
        self.probe_timeout_s = probe_timeout_s

    def classify(self, warrant: Warrant, view: RevocationView, now: float) -> AuthorizationState:
        if warrant.warrant_id in view.revoked_ids:
            return AuthorizationState.REVOKED
        if now >= warrant.not_after:
            return AuthorizationState.REVOKED
        # The boundary: content-fresh but authority-stale must NOT grant.
        if view.cursor_epoch < warrant.required_epoch:
            return AuthorizationState.UNKNOWN
        if view.feed_age_s > self.max_rev_lag_s:
            return AuthorizationState.UNKNOWN
        return AuthorizationState.AUTHORIZED

    def authorize_or_probe(self, warrant, view, now, probe_fn):
        state = self.classify(warrant, view, now)
        if state is AuthorizationState.AUTHORIZED:
            return state
        if state is AuthorizationState.REVOKED:
            return state
        # UNKNOWN: bounded synchronous probe, never an unbounded retry loop.
        deadline = now + self.probe_timeout_s
        probed = probe_fn(warrant.warrant_id)  # returns fresh RevocationView or None
        if probed is None or time.monotonic() > deadline:
            raise WarrantStatusIndeterminate(
                f"warrant {warrant.warrant_id} status indeterminate: "
                f"cursor {view.cursor_epoch} < required {warrant.required_epoch}, "
                f"probe timed out after {self.probe_timeout_s}s"
            )
        resolved = self.classify(warrant, probed, now)
        if resolved is AuthorizationState.UNKNOWN:
            raise WarrantStatusIndeterminate(
                f"probe returned but cursor {probed.cursor_epoch} still "
                f"< required {warrant.required_epoch}"
            )
        return resolved
