# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-halt-when-aaif-revocation-propagation-lags-short-lived-token-rotation-on-a-long-running-agent.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-halt-when-aaif-revocation-propagation-lags-short-lived-token-rotation-on-a-long-running-agent.warrant.json
# Markdown SHA-256:   1d4fa009fe66170fefe1e3e21b088d1ab666a193386674a54eec5bf5ba422b2c
# Code AST SHA-256:   74017507b7216077c171e85a29d437298c4c01192cea41377963b2ea0956bdec
# Verified Classes:   RevocationPropagationViolation, AuthState
# Defensive Guards:   4 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import Optional


class RevocationPropagationViolation(RuntimeError):
    """Raised when an agent would act under a warrant whose revocation state
    is not locally verifiable, or when a mint would resurrect a tombstoned warrant."""


@dataclass
class AuthState:
    auth_high_water: int = 0
    revoked_warrants: set = field(default_factory=set)

    def observe_epoch(self, epoch: int) -> None:
        # Monotonic high-water mark; never decreases.
        self.auth_high_water = max(self.auth_high_water, epoch)

    def tombstone(self, warrant_id: str) -> None:
        self.revoked_warrants.add(warrant_id)


def guarded_mint(state: AuthState, warrant_id: str, mint_epoch: int,
                 verifier_epoch: int, mint_response: dict) -> dict:
    """Mint a rotating token only if it cannot resurrect a revoked warrant
    and the mint node's epoch is not behind our high-water mark."""
    if warrant_id in state.revoked_warrants:
        raise RevocationPropagationViolation(
            f"refusing to mint for tombstoned warrant {warrant_id}; "
            f"revocation is terminal and must not be resurrected by rotation"
        )
    if mint_epoch < state.auth_high_water:
        raise RevocationPropagationViolation(
            f"mint node epoch {mint_epoch} is behind agent high-water "
            f"{state.auth_high_water}; token would be minted against stale revocation state"
        )
    if verifier_epoch < state.auth_high_water:
        raise RevocationPropagationViolation(
            f"verifier epoch {verifier_epoch} < high-water {state.auth_high_water}; "
            f"acting here would open a split-brain authorization window"
        )
    state.observe_epoch(mint_epoch)
    return mint_response


def handle_auth_response(state: AuthState, warrant_id: str,
                         status: str, epoch: int) -> str:
    """Route auth outcomes; only TOKEN_EXPIRED is retryable."""
    if status == "TOKEN_EXPIRED":
        return "retry_mint"
    if status == "WARRANT_REVOKED":
        state.tombstone(warrant_id)
        return "halt_escalate"
    if status == "EPOCH_STALE":
        state.observe_epoch(epoch)
        return "halt_reread_authoritative"
    raise RevocationPropagationViolation(
        f"unclassified auth status {status!r} for {warrant_id}; "
        f"refusing to fall through to generic retry"
    )
