# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-halt-when-a-revoked-warrant-is-only-one-hop-deep-in-a-chained-delegation-graph.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-halt-when-a-revoked-warrant-is-only-one-hop-deep-in-a-chained-delegation-graph.warrant.json
# Markdown SHA-256:   eca1c3f48298664698d52f95bf5eb4ba6ec7b7fb8e3589a0ee1dd4797c49f1dc
# Code AST SHA-256:   cfa38a7f825a65f7907bd59f5cc085fffd6ca7e2217313c4b6367cc2ccc99a11
# Verified Classes:   RevocationChainError, Warrant
# Defensive Guards:   5 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Optional


class RevocationChainError(RuntimeError):
    """Raised when an agent cannot establish the unrevoked status of its full warrant ancestry."""


@dataclass
class Warrant:
    warrant_hash: str
    issuer: str
    expires_at: float
    ancestor_hashes: tuple[str, ...]  # full chain to root, root-last
    max_depth: int


def verify_warrant_transitively(
    warrant: Warrant,
    now: float,
    revocation_feed: dict[str, float],  # warrant_hash -> revocation timestamp
    feed_reachable: bool,
) -> None:
    """
    Fail-closed transitive revocation check.

    Halts unless EVERY ancestor in the chain is known-unrevoked at `now`.
    A missing feed entry is NOT treated as unrevoked; it is treated as unknown,
    and unknown halts the action.
    """
    if not feed_reachable:
        raise RevocationChainError(
            f"revocation feed unreachable; cannot establish unrevoked ancestry "
            f"for warrant {warrant.warrant_hash}; halting (fail-closed)"
        )

    if warrant.expires_at <= now:
        raise RevocationChainError(
            f"warrant {warrant.warrant_hash} expired at {warrant.expires_at}, now {now}"
        )

    if len(warrant.ancestor_hashes) > warrant.max_depth:
        raise RevocationChainError(
            f"warrant {warrant.warrant_hash} chain depth {len(warrant.ancestor_hashes)} "
            f"exceeds authority cap {warrant.max_depth}; ancestry unresolvable"
        )

    for ancestor in warrant.ancestor_hashes:
        if ancestor not in revocation_feed:
            # Feed is reachable but has no entry for this ancestor. Under a
            # complete/monotonic feed this means "not yet revoked," but we
            # cannot distinguish that from "entry dropped." Treat as unknown.
            raise RevocationChainError(
                f"revocation status of ancestor {ancestor} is unknown for "
                f"warrant {warrant.warrant_hash}; halting (fail-closed)"
            )
        revoked_at = revocation_feed[ancestor]
        if revoked_at <= now:
            raise RevocationChainError(
                f"ancestor {ancestor} revoked at {revoked_at}; warrant "
                f"{warrant.warrant_hash} transitively revoked, action refused"
            )


def act(agent_id: str, warrant: Warrant, now: float,
        feed: dict[str, float], reachable: bool) -> str:
    verify_warrant_transitively(warrant, now, feed, reachable)
    return f"{agent_id} executed under {warrant.warrant_hash}"
