# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-a-received-revocation-warrant-as-advisory-until-transitive-delegation-closure-is-proven.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-a-received-revocation-warrant-as-advisory-until-transitive-delegation-closure-is-proven.warrant.json
# Markdown SHA-256:   4f13e582bb8256e23f0b5db8f5a84123ccfc1cc1869a395b1cd7d9e4b9024d02
# Code AST SHA-256:   dd6e8e6c55e7298284cd62f27995c8ebe3e814bf6da7c71e837933def544c68d
# Verified Classes:   RevocationScope, AncestryVerdict, RevocationPropagationError, Warrant, Revocation
# Defensive Guards:   3 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from enum import Enum
from typing import Optional


class RevocationScope(Enum):
    IDENTITY = "identity"   # key compromised: kill edge and all transitive children
    EDGE = "edge"           # specific grant withdrawn: kill only this edge
    CAPABILITY = "capability"  # narrow: kill only this tool/action


class AncestryVerdict(Enum):
    PROVED_ANCESTOR = "proved_ancestor"
    PROVED_NON_ANCESTOR = "proved_non_ancestor"
    UNPROVABLE = "unprovable"


class RevocationPropagationError(RuntimeError):
    """Raised when a node cannot safely resolve a received revocation."""


@dataclass
class Warrant:
    edge_id: str
    parent_id: Optional[str]
    issuer: str
    ttl_seconds_remaining: int


@dataclass
class Revocation:
    revoked_edge_id: str
    scope: RevocationScope
    status_age_seconds: float


def resolve_revocation(
    held: Warrant,
    rev: Revocation,
    propagation_sla_seconds: float,
    ancestry: AncestryVerdict,
) -> str:
    """Decide whether a received revocation binds this node.

    Returns one of: 'halt', 'freeze_mint', 'continue'.
    Raises RevocationPropagationError when the node is in the
    unprovable-ancestry state and cannot bound its own exposure.
    """
    if ancestry is AncestryVerdict.PROVED_NON_ANCESTOR:
        return "continue"

    # From here the revocation may apply to us. Status freshness gates the action.
    status_fresh = rev.status_age_seconds <= propagation_sla_seconds

    if ancestry is AncestryVerdict.PROVED_ANCESTOR:
        if rev.scope is RevocationScope.IDENTITY and not status_fresh:
            # Compromised key, stale status: cannot trust remaining TTL window.
            raise RevocationPropagationError(
                f"identity revocation of {rev.revoked_edge_id} has stale status "
                f"({rev.status_age_seconds:.0f}s > SLA {propagation_sla_seconds:.0f}s); "
                f"cannot bound exposure for held edge {held.edge_id}"
            )
        return "halt"

    # UNPROVABLE ancestry: fail closed on minting, bounded execution only.
    if held.parent_id is None:
        # No back-pointer at all: we cannot even attempt proof. Freeze minting,
        # but refuse to continue if the remaining TTL exceeds our exposure budget.
        if held.ttl_seconds_remaining > propagation_sla_seconds:
            raise RevocationPropagationError(
                f"no ancestry back-pointer on {held.edge_id}; remaining TTL "
                f"{held.ttl_seconds_remaining}s exceeds propagation SLA "
                f"{propagation_sla_seconds:.0f}s — exposure unbounded"
            )
        return "freeze_mint"

    return "freeze_mint"


def enforce(held: Warrant, rev: Revocation, sla: float, ancestry: AncestryVerdict) -> None:
    action = resolve_revocation(held, rev, sla, ancestry)
    if action == "halt":
        raise RevocationPropagationError(
            f"halting execution on {held.edge_id}: ancestor {rev.revoked_edge_id} revoked"
        )
