# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/which-invariants-prevent-stale-attenuation-from-resurrecting-revoked-capabilities-in-delegated-agent-chains.md
# Remnant Warrant:    https://corpusfetch.com/c/which-invariants-prevent-stale-attenuation-from-resurrecting-revoked-capabilities-in-delegated-agent-chains.warrant.json
# Markdown SHA-256:   b0e298c3fdf627d53b24ede7f1113ad6bd1cfe5fc5c2408eeb999160e14756b1
# Code AST SHA-256:   60a19b61b1b3f218b8c1f2255a445464073c7d6268d36862d984f3491ea929ad
# Verified Classes:   RevocationIndeterminateError, CapabilityResurrectedError, Liveness, Token, RevocationView, ChainVerifier
# Defensive Guards:   2 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from enum import Enum
from typing import Optional


class RevocationIndeterminateError(RuntimeError):
    """Raised when ancestor liveness cannot be confirmed within the freshness bound."""
    pass


class CapabilityResurrectedError(RuntimeError):
    """Raised when an attenuated token's ancestor is revoked but the token still verifies."""
    pass


class Liveness(Enum):
    LIVE = "live"
    REVOKED = "revoked"
    INDETERMINATE = "indeterminate"


@dataclass
class Token:
    token_id: str
    parent_id: Optional[str]
    scope: frozenset
    ttl_s: int
    issued_at: int


@dataclass
class RevocationView:
    """A node's cached view of a given token's liveness and its age."""
    status: Liveness
    observed_at: int
    authority: str  # which issuer this view is authoritative for


@dataclass
class ChainVerifier:
    max_staleness_s: int
    root_authority: str
    # map token_id -> RevocationView as known to this node
    views: dict = field(default_factory=dict)

    def _resolve_ancestor_liveness(self, token: Token, now: int) -> Liveness:
        view = self.views.get(token.token_id)
        if view is None:
            return Liveness.INDETERMINATE
        if view.authority != self.root_authority:
            # validated against the wrong authority (immediate issuer, not root)
            return Liveness.INDETERMINATE
        if now - view.observed_at > self.max_staleness_s:
            return Liveness.INDETERMINATE
        return view.status

    def verify_chain(self, chain: list, now: int) -> None:
        # Pass 1: crypto/attenuation is assumed already verified by caller.
        # Pass 2: full ancestor liveness against root authority.
        for token in chain:
            status = self._resolve_ancestor_liveness(token, now)
            if status is Liveness.REVOKED:
                raise CapabilityResurrectedError(
                    f"token {token.token_id} has revoked ancestor "
                    f"{token.parent_id}; attenuation does not restore liveness"
                )
            if status is Liveness.INDETERMINATE:
                raise RevocationIndeterminateError(
                    f"cannot confirm liveness of {token.token_id} against "
                    f"{self.root_authority} within {self.max_staleness_s}s; failing closed"
                )


def authorize(chain: list, verifier: ChainVerifier, now: int) -> bool:
    verifier.verify_chain(chain, now)
    return True
