# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/which-invariants-prevent-delegated-aaif-agent-credentials-from-surviving-a-mid-chain-revocation-during-a-live-tool-call.md
# Remnant Warrant:    https://corpusfetch.com/c/which-invariants-prevent-delegated-aaif-agent-credentials-from-surviving-a-mid-chain-revocation-during-a-live-tool-call.warrant.json
# Markdown SHA-256:   8c1fb374c6621a1365b83794cfd8cfea37eb9d7daceb14d073c7f125e51fb3d1
# Code AST SHA-256:   870cfd0591f6cb3bcfd9c1729d83f16d0dcea3d058778a8a1dbc6220bb958115
# Verified Classes:   RevokedChainCommitError, ChainLivenessProof
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class RevokedChainCommitError(RuntimeError):
    """Raised when a delegated action attempts to commit on a chain whose
    ancestor liveness proof is stale or whose remaining TTL cannot cover
    the revocation-propagation bound. Terminal: must NOT be retried."""


class ChainLivenessProof:
    def __init__(self, issued_at: float, max_age: float, revoked_jtis: set[str]):
        self.issued_at = issued_at
        self.max_age = max_age
        self.revoked_jtis = revoked_jtis

    def is_fresh(self, now: float) -> bool:
        return (now - self.issued_at) <= self.max_age


def commit_delegated_action(
    chain_jtis: list[str],
    token_ttl_remaining: float,
    propagation_bound_L: float,
    proof_freshness_delta: float,
    proof: ChainLivenessProof,
    now: float,
) -> None:
    # Invariant 1: proof must be fresh at commit, not at entry.
    if not proof.is_fresh(now):
        raise RevokedChainCommitError(
            f"stale liveness proof: age={now - proof.issued_at:.1f}s "
            f"> max_age={proof.max_age:.1f}s; re-anchor at commit"
        )

    # Invariant 2: remaining TTL must cover proof freshness + propagation lag.
    required = proof_freshness_delta + propagation_bound_L
    if token_ttl_remaining < required:
        raise RevokedChainCommitError(
            f"TTL budget insufficient for safe commit: remaining={token_ttl_remaining:.1f}s "
            f"< delta+L={required:.1f}s; re-mint with longer TTL or shorter L"
        )

    # Invariant 3: no ancestor may be revoked in the fresh proof.
    for jti in chain_jtis:
        if jti in proof.revoked_jtis:
            raise RevokedChainCommitError(
                f"ancestor {jti} revoked; action is terminal, do not re-mint/retry"
            )

    # ... perform irreversible side effect here, inside the bounded window ...
