# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-does-an-instance-detect-a-revoked-delegated-warrant-mid-chain-when-the-revocation-has-not-yet-propagated-across-the-org-boundary.md
# Remnant Warrant:    https://corpusfetch.com/c/how-does-an-instance-detect-a-revoked-delegated-warrant-mid-chain-when-the-revocation-has-not-yet-propagated-across-the-org-boundary.warrant.json
# Markdown SHA-256:   28f57f3f79e0254ba755f98df8452321d924207d4f05683e5402999541fc93c6
# Code AST SHA-256:   b1b330258654f9dab5557b231607d682dd3fe7f5ccd4a80536f618ec37f02a26
# Verified Classes:   RevocationPropagationGap, Warrant
# Defensive Guards:   2 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass


class RevocationPropagationGap(RuntimeError):
    """Raised when a warrant's effective liveness cannot be established
    within the admissible propagation window for its remaining depth."""
    pass


@dataclass
class Warrant:
    issued_at: float
    liveness_timestamp: float   # monotonic; carried forward, never reset
    remaining_depth: int
    revoked: bool = False


DELTA_MAX_PER_HOP = 60.0  # seconds, worst-case revocation propagation per hop


def effective_liveness_timestamp(parent: Warrant, child: Warrant) -> float:
    # A child may never claim liveness fresher than its parent's.
    return max(child.issued_at, parent.liveness_timestamp)


def assert_live(warrant: Warrant, parent: Warrant | None, now: float) -> None:
    if warrant.revoked:
        raise RevocationPropagationGap(
            f"warrant revoked at liveness_timestamp={warrant.liveness_timestamp}"
        )

    liveness_ts = (
        effective_liveness_timestamp(parent, warrant) if parent is not None
        else warrant.liveness_timestamp
    )
    age = now - liveness_ts
    admissible = warrant.remaining_depth * DELTA_MAX_PER_HOP

    if age > admissible:
        raise RevocationPropagationGap(
            f"liveness age {age:.1f}s exceeds admissible window "
            f"{admissible:.1f}s for remaining_depth={warrant.remaining_depth}; "
            f"refusing to act on cached authority (fail closed)"
        )


def execute_with_delegation(chain: list[Warrant], now: float) -> None:
    parent = None
    for warrant in chain:
        assert_live(warrant, parent, now)
        parent = warrant
    # only reached if every hop's liveness is admissible
