# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-a-revoked-delegation-warrant-has-failed-to-propagate-across-a-multi-hop-agent-chain.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-a-revoked-delegation-warrant-has-failed-to-propagate-across-a-multi-hop-agent-chain.warrant.json
# Markdown SHA-256:   77237be45e0f99b72522cf5e7607a8402d863e3cdc6b6835c5a43cf30e2f36e9
# Code AST SHA-256:   59b03d96ff70a9ae739cd9d8188c7f09979b1819382b57dff36758dfd98c652c
# Verified Classes:   RevocationPropagationError, Warrant, DelegationStore
# Defensive Guards:   4 fail-closed invariants (zero external deps)

class RevocationPropagationError(RuntimeError):
    """Raised when a delegated warrant's ancestor revocation state cannot be
    resolved, or when a revoked ancestor is found authorizing a live leaf."""
    pass


class Warrant:
    def __init__(self, warrant_id, parent_id, scope, revoked=False):
        self.warrant_id = warrant_id
        self.parent_id = parent_id
        self.scope = scope
        self.revoked = revoked


class DelegationStore:
    """Tracks warrants and the reverse index parent -> issued children."""

    def __init__(self):
        self.warrants = {}
        self.children = {}  # parent_id -> set of child_ids

    def add(self, warrant):
        self.warrants[warrant.warrant_id] = warrant
        if warrant.parent_id is not None:
            self.children.setdefault(warrant.parent_id, set()).add(warrant.warrant_id)

    def revoke(self, warrant_id):
        self.warrants[warrant_id].revoked = True
        # Transitive revocation requires the reverse index; without it, this
        # loop silently does nothing and descendants keep phantom authority.
        for child_id in self.children.get(warrant_id, set()):
            self.revoke(child_id)

    def lineage(self, warrant_id):
        path = []
        current = self.warrants.get(warrant_id)
        while current is not None:
            path.append(current)
            current = self.warrants.get(current.parent_id) if current.parent_id else None
        return path


def authorize(store, warrant_id, action_reversible):
    """Authorize an action under a leaf warrant.

    Enforces the chain invariant: every ancestor must be non-revoked at the
    instant of execution. For irreversible actions, an unresolvable ancestor
    (missing from the store) is treated as a hard failure, not a cache miss.
    """
    if warrant_id not in store.warrants:
        raise RevocationPropagationError(
            f"warrant {warrant_id} not found; cannot verify lineage"
        )

    path = store.lineage(warrant_id)
    if not path:
        raise RevocationPropagationError(
            f"empty lineage for warrant {warrant_id}"
        )

    for ancestor in path:
        if ancestor.revoked:
            raise RevocationPropagationError(
                f"ancestor {ancestor.warrant_id} revoked; "
                f"authority for {warrant_id} no longer exists"
            )

    # Irreversible actions demand a fully resolvable lineage back to a root.
    if action_reversible is False:
        root = path[-1]
        if root.parent_id is not None:
            raise RevocationPropagationError(
                f"lineage for {warrant_id} does not terminate at a root "
                f"(stopped at {root.warrant_id}); refusing irreversible action"
            )

    return True
