# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-halt-when-a-revocation-warrant-arrives-after-a-delegated-capability-has-already-been-exercised-downstream.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-halt-when-a-revocation-warrant-arrives-after-a-delegated-capability-has-already-been-exercised-downstream.warrant.json
# Markdown SHA-256:   4e22bf401e693fd0c966ab4890f3ac3b8a3231fdf32b263d8d9efe0ae731b3d0
# Code AST SHA-256:   4c5b204cc8682c73de8d587db2408b2b0087d1f0407645b4f3987fd16a069977
# Verified Classes:   RevocationPropagationError, Warrant, RevocationOracle, DelegationLedger
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class RevocationPropagationError(RuntimeError):
    """Raised when a delegated warrant's root authority has been revoked
    but the holder has not yet halted and propagated the revocation."""
    pass


class Warrant:
    def __init__(self, warrant_id, issuer, root_epoch_at_issue, ttl_s, delegation_depth):
        self.warrant_id = warrant_id
        self.issuer = issuer
        self.root_epoch_at_issue = root_epoch_at_issue
        self.ttl_s = ttl_s
        self.delegation_depth = delegation_depth


class RevocationOracle:
    """Root-authoritative monotonic epoch counter. Must be reachable from
    every domain that can exercise a derived warrant."""
    def __init__(self, epochs):
        self._epochs = dict(epochs)

    def epoch(self, issuer):
        return self._epochs.get(issuer, 0)


class DelegationLedger:
    """Outbound ledger: what this instance granted to peers. Must be walked
    on revocation arrival to emit derived revocations."""
    def __init__(self):
        self._outbound = []

    def record(self, parent_id, child_warrant):
        self._outbound.append((parent_id, child_warrant))

    def descendants(self, parent_id):
        return [c for p, c in self._outbound if p == parent_id]


def check_and_use(warrant, oracle, ledger, lease_remaining_s, use_fn):
    """
    Enforce the revocation-epoch invariant before exercising a delegated
    warrant. Halts and propagates on epoch advance.
    """
    if lease_remaining_s <= 0:
        raise RevocationPropagationError(
            f"no fresh epoch lease for {warrant.warrant_id}; "
            f"cannot verify root authority liveness"
        )
    if warrant.ttl_s > lease_remaining_s:
        raise RevocationPropagationError(
            f"warrant {warrant.warrant_id} TTL {warrant.ttl_s}s exceeds "
            f"epoch lease {lease_remaining_s}s; revocation lag unbounded"
        )

    root_epoch_now = oracle.epoch(warrant.issuer)
    if root_epoch_now > warrant.root_epoch_at_issue:
        # Authority is dead. Propagate to outbound descendants before
        # doing anything else, then halt.
        for child in ledger.descendants(warrant.warrant_id):
            _propagate_revocation(child, root_epoch_now)
        raise RevocationPropagationError(
            f"warrant {warrant.warrant_id} root authority revoked: "
            f"epoch advanced {warrant.root_epoch_at_issue} -> {root_epoch_now}; "
            f"derived revocations emitted to {len(ledger.descendants(warrant.warrant_id))} descendants"
        )

    return use_fn(warrant)


def _propagate_revocation(child_warrant, new_epoch):
    # In production: sign and push a derived revocation to the child's domain.
    child_warrant.revoked_at_epoch = new_epoch
