# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-should-an-instance-detect-and-halt-a-revocation-shadow-when-a-delegated-capability-chain-outlives-its-audit-linked-revocation-at-an-intermediate-h.md
# Remnant Warrant:    https://corpusfetch.com/c/how-should-an-instance-detect-and-halt-a-revocation-shadow-when-a-delegated-capability-chain-outlives-its-audit-linked-revocation-at-an-intermediate-h.warrant.json
# Markdown SHA-256:   7797c154f1aee78da2cdff98ce3a06a171ba9db91512049f06772dffa83498aa
# Code AST SHA-256:   97a52a88b2075cd9fe5b50750255a21257592058dfd185ddce054e0398f8b953
# Verified Classes:   RevocationShadowError, WarrantChainUnresolved, Warrant, LiveEpoch
# Defensive Guards:   5 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Optional


class RevocationShadowError(RuntimeError):
    """Raised when a sub-warrant's ancestor was revoked but the child's
    recorded epoch still matches the issuance snapshot rather than live state."""
    pass


class WarrantChainUnresolved(RuntimeError):
    """Raised when the audit_ref chain cannot be walked to the root within
    the verification deadline. Fail-closed: treat as unverified, not valid."""
    pass


@dataclass(frozen=True)
class Warrant:
    warrant_id: str
    parent_id: Optional[str]
    scope: frozenset
    revocation_epoch: int          # epoch recorded at issuance
    audit_ref: str                 # ledger entry authorizing this attenuation


@dataclass(frozen=True)
class LiveEpoch:
    warrant_id: str
    epoch: int
    cause: str                     # "revocation" | "rotation"


def verify_warrant_chain(
    presented: Warrant,
    ledger,
    deadline_exceeded: bool,
) -> bool:
    """Walk audit_ref from presented warrant to root, checking live epochs.

    Returns True only if every ancestor is live-valid and no descendant's
    recorded epoch lags a revoked ancestor's live epoch.
    """
    if deadline_exceeded:
        raise WarrantChainUnresolved(
            f"chain walk for {presented.warrant_id} exceeded deadline; "
            "fail-closed per decision boundary"
        )

    current = presented
    while current.parent_id is not None:
        parent = ledger.get_warrant(current.parent_id)
        if parent is None:
            raise WarrantChainUnresolved(
                f"parent {current.parent_id} of {current.warrant_id} "
                "not resolvable via audit_ref"
            )

        live = ledger.get_live_epoch(parent.warrant_id)
        if live is None:
            raise WarrantChainUnresolved(
                f"no live epoch for {parent.warrant_id}; cannot verify "
                f"authority basis of {current.warrant_id}"
            )

        if live.cause == "revocation" and live.epoch > current.revocation_epoch:
            raise RevocationShadowError(
                f"revocation shadow: {current.warrant_id} records epoch "
                f"{current.revocation_epoch} but ancestor {parent.warrant_id} "
                f"was revoked at epoch {live.epoch}; child authority is void"
            )

        if live.cause == "rotation" and live.epoch != current.revocation_epoch:
            # Benign rotation: require re-issuance of subtree, do not quarantine.
            raise RevocationShadowError(
                f"stale subtree: {current.warrant_id} epoch "
                f"{current.revocation_epoch} precedes ancestor rotation "
                f"epoch {live.epoch}; re-issue required"
            )

        current = parent

    return True
