# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-a-delegated-warrant-whose-attenuation-chain-contains-a-revoked-ancestor-the-dangling-attenuation-orphan.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-a-delegated-warrant-whose-attenuation-chain-contains-a-revoked-ancestor-the-dangling-attenuation-orphan.warrant.json
# Markdown SHA-256:   1499d4d85303afe449eff2fc39894c4cff653db2d1a4e8e96b60681b4a098171
# Code AST SHA-256:   f93c24c7327c68317b13e93984c4e4d7576a4c117f67f1a733780b757b8e9c8d
# Verified Classes:   DanglingAttenuationError, Warrant, RevocationView
# Defensive Guards:   4 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass, field
from typing import Optional


class DanglingAttenuationError(RuntimeError):
    """Raised when a delegated warrant's ancestor liveness cannot be confirmed
    within the freshness bound required by the warrant's scope."""
    pass


@dataclass
class Warrant:
    warrant_id: str
    parent_id: Optional[str]
    scope: str
    # time-to-exploit for this scope; high-consequence scopes are small
    time_to_exploit_s: float
    revoked: bool = False


@dataclass
class RevocationView:
    """A verifier's possibly-stale view of revocation status."""
    last_refresh: float
    known_revoked: set = field(default_factory=set)
    # worst-case propagation latency of the revocation topology
    propagation_latency_s: float = 60.0

    def staleness_bound(self) -> float:
        return (time.time() - self.last_refresh) + self.propagation_latency_s

    def is_known_revoked(self, warrant_id: str) -> bool:
        return warrant_id in self.known_revoked


def verify_delegated(warrant: Warrant, chain: dict, view: RevocationView) -> None:
    """Verify a leaf warrant including ancestor liveness. Fails closed."""
    # Structural walk: collect ancestors leaf -> root.
    ancestors = []
    cursor = warrant
    seen = set()
    while cursor.parent_id is not None:
        if cursor.warrant_id in seen:
            raise DanglingAttenuationError(
                f"cycle in attenuation chain at {cursor.warrant_id}")
        seen.add(cursor.warrant_id)
        parent = chain.get(cursor.parent_id)
        if parent is None:
            raise DanglingAttenuationError(
                f"missing ancestor {cursor.parent_id} for {cursor.warrant_id}")
        ancestors.append(parent)
        cursor = parent

    # Leaf-only revocation check is the bug; we check the whole chain.
    for anc in ancestors:
        if view.is_known_revoked(anc.warrant_id):
            raise DanglingAttenuationError(
                f"ancestor {anc.warrant_id} revoked; leaf {warrant.warrant_id} "
                f"is a dangling attenuation")

    # Freshness invariant: staleness must be under time-to-exploit.
    staleness = view.staleness_bound()
    if staleness >= warrant.time_to_exploit_s:
        raise DanglingAttenuationError(
            f"ancestor liveness unconfirmed: staleness {staleness:.1f}s "
            f">= time-to-exploit {warrant.time_to_exploit_s:.1f}s for "
            f"scope {warrant.scope!r}; refusing to proceed")


def execute_delegated(warrant: Warrant, chain: dict, view: RevocationView) -> str:
    verify_delegated(warrant, chain, view)
    return f"executed {warrant.warrant_id} under {warrant.scope}"
