# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-accepts-a-peer-s-signed-agent-passport-after-the-issuer-s-revocation-list-has-diverged-across-federated-trust-anchors.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-accepts-a-peer-s-signed-agent-passport-after-the-issuer-s-revocation-list-has-diverged-across-federated-trust-anchors.warrant.json
# Markdown SHA-256:   623f87f51f3bd54c2d5f572de3493a9f5bda0995e83a3f45528a8355576d9fc1
# Code AST SHA-256:   b2af1aeebc45ffe320585cd8cfef6aa42a191dad284eadfd299c19557ee88e96
# Verified Classes:   RevocationFreshnessError, Passport, RevocationView
# Defensive Guards:   4 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass


class RevocationFreshnessError(RuntimeError):
    """Raised when a mutating action is attempted on a passport whose
    revocation status cannot be proven current enough for the action."""


@dataclass
class Passport:
    serial: str
    not_after: float          # epoch seconds
    max_check_age: float      # issuer-signed bound on staleness for mutating actions


@dataclass
class RevocationView:
    version: int
    checked_at: float         # epoch seconds when this view was resolved
    revoked_serials: set


def authorize_action(
    passport: Passport,
    view: RevocationView,
    now: float,
    mutating: bool,
    live_check_available: bool,
) -> None:
    if now >= passport.not_after:
        raise RevocationFreshnessError(
            f"passport {passport.serial} expired at {passport.not_after}"
        )

    if passport.serial in view.revoked_serials:
        raise RevocationFreshnessError(
            f"passport {passport.serial} is revoked (view v{view.version})"
        )

    if not mutating:
        # Read-only actions may proceed on a cached, even stale, view.
        return

    age = now - view.checked_at
    remaining = passport.not_after - now

    # Cache is only admissible if the credential expires before the cache
    # can become meaningfully stale relative to the issuer's bound.
    if remaining <= passport.max_check_age:
        return

    if age > passport.max_check_age:
        if not live_check_available:
            # Fail closed: cannot prove freshness, action mutates state.
            raise RevocationFreshnessError(
                f"revocation view stale ({age:.0f}s > "
                f"{passport.max_check_age:.0f}s) and live check unavailable; "
                f"refusing mutating action for {passport.serial}"
            )
        raise RevocationFreshnessError(
            f"revocation view stale ({age:.0f}s > "
            f"{passport.max_check_age:.0f}s); live re-check required "
            f"before mutating action on {passport.serial}"
        )
