# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-reject-a-federated-peer-s-capability-attestation-after-trust-broker-key-rotation-and-how-does-transitive-warran.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-reject-a-federated-peer-s-capability-attestation-after-trust-broker-key-rotation-and-how-does-transitive-warran.warrant.json
# Markdown SHA-256:   fddf88925aff69b446af9c830536e1cae900c77826a57ad5ebb4fdf208adc123
# Code AST SHA-256:   44e0594979b1bf1ebb433a534163a0d5a2bd59470fee842601775a0cc202c3d8
# Verified Classes:   StaleAnchorError, RevocationStalenessError, Warrant, TrustState
# Defensive Guards:   7 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass


class StaleAnchorError(RuntimeError):
    """Raised when a warrant's anchor path validates only against a superseded broker root."""


class RevocationStalenessError(RuntimeError):
    """Raised when the revocation feed is too old to authorize execution."""


@dataclass
class Warrant:
    leaf_signature_valid: bool
    anchor_path: list[str]          # [intermediate_kid, ..., root_kid]
    not_before: float
    not_after: float
    delegation_depth_remaining: int
    rotation_epoch: int
    rotation_survivable: bool


@dataclass
class TrustState:
    current_root_kid: str
    cached_root_kids: set[str]      # superseded roots, audit-only
    current_epoch: int
    revocation_feed_age_s: float
    max_revocation_staleness_s: float = 60.0


def authorize(w: Warrant, ts: TrustState, now: float | None = None) -> None:
    now = now if now is not None else time.time()

    if not w.leaf_signature_valid:
        raise StaleAnchorError("leaf signature invalid")

    if not (w.not_before <= now <= w.not_after):
        raise StaleAnchorError("warrant outside validity window")

    if w.delegation_depth_remaining < 0:
        raise StaleAnchorError("delegation depth exhausted at point of use")

    anchor_root = w.anchor_path[-1]
    if anchor_root != ts.current_root_kid:
        if anchor_root in ts.cached_root_kids:
            raise StaleAnchorError(
                f"anchor root {anchor_root} is superseded; "
                f"current root is {ts.current_root_kid}"
            )
        raise StaleAnchorError(f"anchor root {anchor_root} unknown to verifier")

    if w.rotation_epoch != ts.current_epoch and not w.rotation_survivable:
        raise StaleAnchorError(
            f"rotation epoch mismatch: warrant={w.rotation_epoch} "
            f"verifier={ts.current_epoch} and warrant not rotation_survivable"
        )

    if ts.revocation_feed_age_s > ts.max_revocation_staleness_s:
        raise RevocationStalenessError(
            f"revocation feed age {ts.revocation_feed_age_s}s exceeds "
            f"bound {ts.max_revocation_staleness_s}s"
        )
    # scope-subset and revocation-list checks elided; they run after anchor validation.
