# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-halt-on-a-revoked-warrant-it-cannot-yet-see-the-revocation-propagation-horizon-in-federated-aaif-trust-domains.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-halt-on-a-revoked-warrant-it-cannot-yet-see-the-revocation-propagation-horizon-in-federated-aaif-trust-domains.warrant.json
# Markdown SHA-256:   cc67f20c69d7bbe2940383599b9779c349f1fa2be769e3593ffb5df05bdc98c8
# Code AST SHA-256:   0366f4eb18e6306ab2673c68636afd087dc84206ab4ed542fb1088f70860fcb6
# Verified Classes:   RevocationHorizonExceeded, IndeterminateAuthority, WarrantState
# Defensive Guards:   4 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass, field


class RevocationHorizonExceeded(RuntimeError):
    """Raised when an agent attempts a side-effecting call while its
    authority belief is older than the warrant's max_staleness bound."""
    pass


class IndeterminateAuthority(RuntimeError):
    """Raised when the revocation endpoint is unreachable and the call
    is side-effecting; the agent must not guess between valid and revoked."""
    pass


@dataclass
class WarrantState:
    warrant_id: str
    exp: float                      # issuer's stated ceiling
    max_staleness: float            # federation-bounded freshness, <= min CRL sync SLA
    last_revocation_check: float = 0.0
    revoked: bool = False
    idempotency_keys: dict = field(default_factory=dict)


def assert_authority_live(state: WarrantState, revocation_probe, *, side_effecting: bool, now: float | None = None) -> None:
    now = time.time() if now is None else now

    if state.revoked:
        raise RevocationHorizonExceeded(f"warrant {state.warrant_id} already known revoked")

    if now >= state.exp:
        raise RevocationHorizonExceeded(f"warrant {state.warrant_id} past exp ceiling")

    age = now - state.last_revocation_check
    if age > state.max_staleness:
        if not side_effecting:
            return  # read-only calls may proceed on stale-but-signed authority
        # attempt an in-line refresh before halting
        try:
            live = revocation_probe(state.warrant_id)
        except Exception as e:
            raise IndeterminateAuthority(
                f"revocation endpoint unreachable for {state.warrant_id}; "
                f"side-effecting call refused (age={age:.1f}s > bound={state.max_staleness:.1f}s)"
            ) from e
        if live is False:
            state.revoked = True
            raise RevocationHorizonExceeded(f"warrant {state.warrant_id} revoked on refresh")
        state.last_revocation_check = now
        return

    # within bound: authority belief is fresh enough to act on


def execute_side_effect(state: WarrantState, revocation_probe, key: str, action) -> None:
    if key in state.idempotency_keys:
        return  # already committed; do not replay across a revocation restart
    assert_authority_live(state, revocation_probe, side_effecting=True)
    state.idempotency_keys[key] = "committed"
    action()
