# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/why-does-an-instance-trust-a-revoked-peer-revocation-lag-and-warrant-resurrection-across-federated-agent-trust-domains.md
# Remnant Warrant:    https://corpusfetch.com/c/why-does-an-instance-trust-a-revoked-peer-revocation-lag-and-warrant-resurrection-across-federated-agent-trust-domains.warrant.json
# Markdown SHA-256:   fff79e0e2607616f6321c9879d1f11e14a80bc0ed2b12436fc5ec23addae1c16
# Code AST SHA-256:   475863de3ce0415f65d10888723331a42e32dbc9a449b512583b2e22cc2dab92
# Verified Classes:   RevocationResurrectionError, Warrant, RevocationView
# Defensive Guards:   5 fail-closed invariants (zero external deps)

"""
Federated warrant verification with chain-complete, causally-fresh revocation checking.
Demonstrates the guardrail that prevents warrant resurrection.
"""
from dataclasses import dataclass, field
from typing import Optional


class RevocationResurrectionError(RuntimeError):
    """Raised when a warrant chain roots in a revoked or unverifiable ancestor."""
    pass


@dataclass
class Warrant:
    warrant_id: str
    issuer_domain: str
    principal: str
    parent_id: Optional[str]
    scope: frozenset


@dataclass
class RevocationView:
    """A relying domain's local view of a remote domain's revocation list."""
    domain: str
    seen_version: int
    revoked_ids: frozenset = field(default_factory=frozenset)

    def status_of(self, warrant_id: str) -> str:
        # Returns 'revoked', 'unrevoked', or 'unknown' (stale/unseen).
        if self.seen_version < 0:
            return "unknown"
        return "revoked" if warrant_id in self.revoked_ids else "unrevoked"


def verify_chain(
    leaf: Warrant,
    ancestry: dict[str, Warrant],
    views: dict[str, RevocationView],
    required_min_version: dict[str, int],
) -> None:
    """
    Verify the full delegation chain of `leaf`, checking revocation status of
    EVERY ancestor (including root), with causal freshness. Fails closed.
    """
    chain: list[Warrant] = [leaf]
    cursor = leaf
    while cursor.parent_id is not None:
        parent = ancestry.get(cursor.parent_id)
        if parent is None:
            raise RevocationResurrectionError(
                f"truncated chain: parent {cursor.parent_id} of {cursor.warrant_id} missing"
            )
        chain.append(parent)
        cursor = parent

    for w in chain:
        view = views.get(w.issuer_domain)
        if view is None:
            raise RevocationResurrectionError(
                f"no revocation view for domain {w.issuer_domain}; cannot verify {w.warrant_id}"
            )
        # Causal freshness: must have seen at least the required version.
        if view.seen_version < required_min_version.get(w.issuer_domain, 0):
            raise RevocationResurrectionError(
                f"stale revocation view for {w.issuer_domain}: "
                f"seen {view.seen_version} < required {required_min_version[w.issuer_domain]}"
            )
        status = view.status_of(w.warrant_id)
        if status == "revoked":
            raise RevocationResurrectionError(
                f"warrant {w.warrant_id} (domain {w.issuer_domain}) is revoked; "
                f"chain rooted at {chain[-1].warrant_id} is dead"
            )
        if status == "unknown":
            raise RevocationResurrectionError(
                f"status of {w.warrant_id} unknown; failing closed"
            )
    # All ancestors unrevoked with fresh views: chain is honorable.
    return None
