# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-refuse-a-delegated-capability-after-an-upstream-revocation-has-already-been-acknowledged.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-refuse-a-delegated-capability-after-an-upstream-revocation-has-already-been-acknowledged.warrant.json
# Markdown SHA-256:   e12e5aa4b695f3fb9cc91796b610470ee897dafca70b034677f2e4a86fd83766
# Code AST SHA-256:   0e41eeae4341d65a57adfed1797fda8e32b1466043dee31077db80e1cb90bc24
# Verified Classes:   ZombieGrantError, WarrantGraph
# Defensive Guards:   4 fail-closed invariants (zero external deps)

class ZombieGrantError(RuntimeError):
    """Raised when a capability token verifies locally but has no unrevoked path to a trusted root."""
    pass


class WarrantGraph:
    def __init__(self):
        self.revoked = set()          # warrant_ids known revoked
        self.parent_of = {}           # warrant_id -> parent_warrant_id (None for root)
        self.trusted_roots = set()
        self.freshness_window_s = 30

    def revoke(self, warrant_id):
        self.revoked.add(warrant_id)

    def _walk_to_root(self, warrant_id):
        path = []
        cur = warrant_id
        seen = set()
        while cur is not None:
            if cur in seen:
                raise ZombieGrantError(f"cycle in warrant graph at {cur}")
            seen.add(cur)
            path.append(cur)
            cur = self.parent_of.get(cur)
        return path

    def verify_authorized(self, token, now, last_checked):
        # Local cryptographic checks pass (signature, scope subset, expiry) -- assumed true here.
        # The failure is that these checks do not establish current authority.
        if now - last_checked <= self.freshness_window_s:
            # Stale cache: local verification result is reused without graph re-walk.
            # This is the exact window in which zombie grants survive.
            return True

        path = self._walk_to_root(token.warrant_id)
        root = path[-1]
        if root not in self.trusted_roots:
            raise ZombieGrantError(f"token {token.warrant_id}: root {root} is not trusted")

        for wid in path:
            if wid in self.revoked:
                raise ZombieGrantError(
                    f"token {token.warrant_id} verifies but warrant {wid} on its path is revoked; "
                    f"authority withdrawn at hop {wid}"
                )
        return True


def propagate_revocation(graph, from_warrant, downstream_tokens, acknowledged):
    # Acknowledgment to upstream does NOT discharge propagation obligation.
    graph.revoke(from_warrant)
    open_obligations = []
    for tok in downstream_tokens:
        path = graph._walk_to_root(tok.warrant_id)
        if from_warrant in path:
            # delegable=False does not exempt this token: it depends on the revoked parent.
            delivered = tok.deliver_revocation()
            if not delivered:
                open_obligations.append(tok.warrant_id)
    if open_obligations:
        raise ZombieGrantError(
            f"revocation of {from_warrant} acknowledged but propagation incomplete to "
            f"{open_obligations}; these tokens remain live zombie grants until expiry"
        )
    return True
