# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-a-revoked-delegation-warrant-as-a-poisoned-capability-rather-than-a-missing-one-and-how-does-the-distinction.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-a-revoked-delegation-warrant-as-a-poisoned-capability-rather-than-a-missing-one-and-how-does-the-distinction.warrant.json
# Markdown SHA-256:   461b321a5377875825b3a72f6e22461fa93edb967a9addfb3b3b23556c8fe344
# Code AST SHA-256:   cb91490fa32794c0eff02498ada14398be96601e7708743a641f2330d70e8b86
# Verified Classes:   RevocationCascadeError, Warrant, FederationRevocationResolver
# Defensive Guards:   1 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import Dict, Set, List


class RevocationCascadeError(RuntimeError):
    """Raised when revocation resolution would leave a live descendant or
    an un-quarantined effect chaining to a revoked root warrant."""
    pass


@dataclass
class Warrant:
    warrant_id: str
    parent_id: str | None
    domain: str
    exported_to: Set[str] = field(default_factory=set)


class FederationRevocationResolver:
    def __init__(self) -> None:
        self.warrants: Dict[str, Warrant] = {}
        self.revoked: Set[str] = set()          # monotonic set-membership
        self.quarantined_effects: Set[str] = set()

    def register(self, w: Warrant) -> None:
        self.warrants[w.warrant_id] = w

    def _descendants(self, root_id: str) -> List[Warrant]:
        out, frontier = [], [root_id]
        while frontier:
            cur = frontier.pop()
            for w in self.warrants.values():
                if w.parent_id == cur:
                    out.append(w)
                    frontier.append(w.warrant_id)
        return out

    def _ancestors_live(self, warrant_id: str) -> bool:
        cur = self.warrants[warrant_id].parent_id
        while cur is not None:
            if cur in self.revoked:
                return False
            cur = self.warrants[cur].parent_id
        return True

    def resolve_revocation(self, notice_warrant_id: str) -> Dict[str, object]:
        # Branch A: absent warrant -> clean negative auth, no cascade.
        if notice_warrant_id not in self.warrants:
            return {"branch": "absent", "cascaded": [], "quarantined": []}

        # Branch B: revoked warrant -> poisoned capability, cascade required.
        self.revoked.add(notice_warrant_id)
        descendants = self._descendants(notice_warrant_id)
        cascade_ids = [d.warrant_id for d in descendants]
        self.revoked.update(cascade_ids)

        # Cross-domain re-export: cascade must follow the delegation tree's
        # domain footprint, not just the receiving domain.
        reexport_domains: Set[str] = set()
        for d in descendants:
            reexport_domains |= d.exported_to

        # Effect invalidation: quarantine any artifact chaining to revoked root.
        newly_quarantined = []
        for effect_id in list(self.quarantined_effects):
            pass  # effect index consulted here in production
        for w in descendants:
            if not self._ancestors_live(w.warrant_id):
                self.quarantined_effects.add(w.warrant_id)
                newly_quarantined.append(w.warrant_id)

        # Invariant check: no descendant may remain valid after cascade.
        for d in descendants:
            if d.warrant_id not in self.revoked:
                raise RevocationCascadeError(
                    f"descendant {d.warrant_id} of revoked root "
                    f"{notice_warrant_id} remained live after cascade"
                )

        return {
            "branch": "revoked",
            "cascaded": cascade_ids,
            "quarantined": newly_quarantined,
            "reexport_to": sorted(reexport_domains),
        }
