# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-should-an-instance-reconcile-a-revoked-attenuated-capability-against-an-in-flight-delegated-sub-agent-holding-a-stale-caveat-chain.md
# Remnant Warrant:    https://corpusfetch.com/c/how-should-an-instance-reconcile-a-revoked-attenuated-capability-against-an-in-flight-delegated-sub-agent-holding-a-stale-caveat-chain.warrant.json
# Markdown SHA-256:   404fdf95849baa900e23afba6f7c0cffb7565a03a3e8f1ee90229d293bd8f9f2
# Code AST SHA-256:   57b460a1219393896298efbd8c8a8ef4c23457480e461df0597bd0edf44575d2
# Verified Classes:   StaleRootAuthorizationError, LivenessAttestation, AttenuatedCapability
# Defensive Guards:   6 fail-closed invariants (zero external deps)

class StaleRootAuthorizationError(RuntimeError):
    """Raised when a consequential action is attempted under an attenuated
    capability whose root liveness cannot be attested within the action's
    consequence horizon."""
    pass


import time
from dataclasses import dataclass


@dataclass
class LivenessAttestation:
    root_id: str
    issued_at: float          # issuer clock, seconds
    delta: float              # attestation validity window


@dataclass
class AttenuatedCapability:
    root_id: str
    caveats: list             # appended caveats, monotonic
    expiry: float             # absolute, issuer clock


def authorize_consequential_action(
    cap: AttenuatedCapability,
    attest: LivenessAttestation,
    action_consequence_horizon: float,
    issuer_now: float,
    max_uninterruptible_segment: float,
) -> None:
    # (a) caveat chain must discharge — signature check omitted for brevity,
    #     assume verified upstream.
    if any(c.get("denied") for c in cap.caveats):
        raise StaleRootAuthorizationError(
            f"caveat chain for root {cap.root_id} contains a denial"
        )

    # (b) root liveness must be attested, and the attestation must be fresh
    #     relative to the issuer's clock, not the holder's.
    if attest.root_id != cap.root_id:
        raise StaleRootAuthorizationError(
            f"attestation root {attest.root_id} != capability root {cap.root_id}"
        )
    attestation_age = issuer_now - attest.issued_at
    if attestation_age > attest.delta:
        raise StaleRootAuthorizationError(
            f"root {cap.root_id} liveness attestation stale by "
            f"{attestation_age - attest.delta:.3f}s; refusing action"
        )

    # (c) the action's consequence horizon must be bounded below the
    #     attestation window, otherwise revocation cannot gate it in time.
    if action_consequence_horizon >= attest.delta:
        raise StaleRootAuthorizationError(
            f"consequence horizon {action_consequence_horizon}s >= attestation "
            f"window {attest.delta}s for root {cap.root_id}; action cannot be "
            f"gated by revocation"
        )

    # (d) the holder's uninterruptible segment must not exceed the window,
    #     or a revocation arriving mid-segment will be observed too late.
    if max_uninterruptible_segment > attest.delta:
        raise StaleRootAuthorizationError(
            f"uninterruptible segment {max_uninterruptible_segment}s exceeds "
            f"attestation window {attest.delta}s; revocation propagation is "
            f"structurally unsound for root {cap.root_id}"
        )

    # (e) expiry caveat evaluated against the issuer clock.
    if issuer_now >= cap.expiry:
        raise StaleRootAuthorizationError(
            f"capability for root {cap.root_id} expired at {cap.expiry} "
            f"(issuer now {issuer_now})"
        )
    # action proceeds
