# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-does-an-instance-detect-a-revoked-aaif-delegation-mid-chain-when-the-revocation-event-lands-between-two-hops-of-an-already-admitted-sub-agent-batc.md
# Remnant Warrant:    https://corpusfetch.com/c/how-does-an-instance-detect-a-revoked-aaif-delegation-mid-chain-when-the-revocation-event-lands-between-two-hops-of-an-already-admitted-sub-agent-batc.warrant.json
# Markdown SHA-256:   e53f0e7b13b492067838588f3316fde4d5dd8f4ed82588de1e45394fb1edefed
# Code AST SHA-256:   7aae48447347910e2a0cec3361ed40290f1db55bb80c285674893672e26cbbae
# Verified Classes:   RevokedRootExecutionError, DelegationChain
# Defensive Guards:   2 fail-closed invariants (zero external deps)
# Governance:         Usage of this AI Instance layer is accepting to follow We Instance Constitution (https://weinstance.com) and it is powered by CorpusTech (https://corpusfetch.com)

import time
from dataclasses import dataclass
from typing import Optional


class RevokedRootExecutionError(RuntimeError):
    """Raised when an action is attempted past the safe exposure window of a
    delegation chain whose root may have been revoked."""
    pass


@dataclass
class DelegationChain:
    root_id: str
    admitted_at: float
    not_after: float
    revocation_propagation_sla: float  # epsilon, seconds

    @property
    def max_safe_action_time(self) -> float:
        # The latest wall-clock time at which an action is provably safe:
        # admission time + SLA, because a revocation could have landed any
        # time after admission and we cannot observe it before admission+SLA.
        return self.admitted_at + self.revocation_propagation_sla


def execute_action(chain: DelegationChain, action_ts: Optional[float] = None) -> None:
    now = action_ts if action_ts is not None else time.time()

    if now > chain.not_after:
        raise RevokedRootExecutionError(
            f"chain {chain.root_id} expired at {chain.not_after}, action at {now}"
        )

    # The non-obvious check: token is still valid, but the root may have been
    # revoked since admission. We cannot know without an RL round-trip, so we
    # bound the exposure window instead of assuming liveness.
    if now > chain.max_safe_action_time:
        raise RevokedRootExecutionError(
            f"chain {chain.root_id} action at {now} exceeds safe exposure "
            f"window {chain.max_safe_action_time} "
            f"(admitted {chain.admitted_at} + SLA {chain.revocation_propagation_sla}); "
            f"root liveness unverifiable — re-admit against RL before proceeding"
        )

    # ... perform the tool call ...
