# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-should-an-instance-treat-a-revocation-that-arrives-after-local-policy-has-already-authorized-the-action.md
# Remnant Warrant:    https://corpusfetch.com/c/how-should-an-instance-treat-a-revocation-that-arrives-after-local-policy-has-already-authorized-the-action.warrant.json
# Markdown SHA-256:   bb69e62e2f6a57b516ab8099055edacb5f044bf45144c22a5f20df1511419b5f
# Code AST SHA-256:   faeb7b87c9b1a705bdbc8595c4da8ec89f6220324a64094f64610b2694fae791
# Verified Classes:   StaleAuthorityError, AuthorityState, RevocationWatermark
# Defensive Guards:   3 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass
from enum import Enum


class StaleAuthorityError(RuntimeError):
    """Raised when an RP would exercise authority the issuer has withdrawn."""


class AuthorityState(Enum):
    AUTHORIZED = "authorized"
    UNAUTHORIZED = "unauthorized"
    INDETERMINATE = "authority_indeterminate"


@dataclass(frozen=True)
class RevocationWatermark:
    effective_at: float      # issuer-signed, epoch seconds
    observed_at: float       # RP monotonic receipt time
    max_skew: float          # negotiated bound between issuer and RP


def classify_action(
    action_at: float,
    watermark: RevocationWatermark | None,
    decision_ttl: float,
    now: float,
) -> AuthorityState:
    if watermark is None:
        # No revocation state at all: cannot claim authority.
        raise StaleAuthorityError("no revocation watermark observed; refusing to authorize")

    # If the watermark is older than the tolerated propagation window, the RP
    # is operating on stale authority and must not extend new authority.
    if now - watermark.observed_at > 2.0 * decision_ttl:
        raise StaleAuthorityError(
            f"revocation watermark stale by {now - watermark.observed_at:.1f}s; "
            "bounded indeterminate window exceeded"
        )

    if action_at + watermark.max_skew < watermark.effective_at:
        return AuthorityState.AUTHORIZED

    if action_at - watermark.max_skew > watermark.effective_at:
        return AuthorityState.UNAUTHORIZED

    # Action straddles the effective_at within skew: ordering is not provable.
    return AuthorityState.INDETERMINATE


def record_action(
    action_at: float,
    watermark: RevocationWatermark | None,
    decision_ttl: float,
    now: float,
) -> AuthorityState:
    state = classify_action(action_at, watermark, decision_ttl, now)
    if state is AuthorityState.INDETERMINATE:
        # Route to reconciliation rather than silently recording as authorized.
        # The caller must not treat this as a clean success.
        return state
    if state is AuthorityState.UNAUTHORIZED:
        raise StaleAuthorityError(
            f"action at {action_at:.3f} postdates revocation effective_at "
            f"{watermark.effective_at:.3f} beyond skew {watermark.max_skew:.3f}"
        )
    return state
