# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-honor-a-revocation-it-cannot-verify-stale-warrant-execution-in-attenuated-agent-delegation-chains.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-honor-a-revocation-it-cannot-verify-stale-warrant-execution-in-attenuated-agent-delegation-chains.warrant.json
# Markdown SHA-256:   02021c570ae69a0df0ec055ff062b99cdf0575d0e8c26e447397455cda7b8087
# Code AST SHA-256:   961d241a4be69ee4cc23edebe36f6164feb5356343123ba2ec1c0496cf1deb22
# Verified Classes:   RevocationWalkIncomplete, ControlPlaneUnreachable
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class RevocationWalkIncomplete(RuntimeError):
    """Raised when a warrant is accepted without a root-anchored liveness check."""
    pass


class ControlPlaneUnreachable(RuntimeError):
    """Raised when the registry cannot be consulted and no lease covers the step."""
    pass


def authorize_step(warrant_chain, registry, now, convergence_bound, lease=None):
    """
    warrant_chain: list [W_leaf, ..., W_root], each with .id and .revoked_at (may be None)
    registry: callable(epoch) -> {warrant_id: revoked_at}
    lease: optional signed object with .expires_at granting local authority until then
    """
    if lease is not None and now < lease.expires_at:
        # Lease path: locally enforceable, no remote walk required.
        return True

    try:
        epoch = registry.current_epoch()
        revocations = registry.snapshot(epoch)
    except Exception as exc:
        # No lease and no registry: bearer warrant cannot be proven live.
        raise ControlPlaneUnreachable(
            f"cannot verify liveness of root {warrant_chain[-1].id} at t={now}"
        ) from exc

    # Walk to root. Skipping the root is the exact failure this guards against.
    for w in warrant_chain:
        if revocations.get(w.id) is not None:
            raise RevocationWalkIncomplete(
                f"warrant {w.id} revoked at {revocations[w.id]}; "
                f"chain root {warrant_chain[-1].id} is dead at t={now}"
            )

    root = warrant_chain[-1]
    if root.id not in revocations:
        # Absence of a revocation bit is not proof of liveness unless the
        # snapshot epoch is recent enough to cover convergence.
        if now - registry.epoch_time(epoch) > convergence_bound:
            raise RevocationWalkIncomplete(
                f"root {root.id} liveness unconfirmed: snapshot epoch too old "
                f"({now - registry.epoch_time(epoch)}s > {convergence_bound}s)"
            )
    return True
