# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-should-an-instance-detect-that-its-federated-identity-warrant-was-revoked-mid-execution-across-an-organizational-boundary.md
# Remnant Warrant:    https://corpusfetch.com/c/how-should-an-instance-detect-that-its-federated-identity-warrant-was-revoked-mid-execution-across-an-organizational-boundary.warrant.json
# Markdown SHA-256:   70d5ac513239a5b539dec40ade755164c77b9be3db8076ac2327646ca5d79d1b
# Code AST SHA-256:   24513dc543a62f3ac180aa43008e7af2bd3de2e4f536a0ee2c1e49560b81a203
# Verified Classes:   RevocationShadowViolation, SpendReconciliationError, FederatedWarrant
# Defensive Guards:   3 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass, field


class RevocationShadowViolation(RuntimeError):
    """Raised when a committing action would execute under an unverifiable revocation status."""
    pass


class SpendReconciliationError(RuntimeError):
    """Raised when local spend counter disagrees with issuer-authoritative state after sync."""
    pass


@dataclass
class FederatedWarrant:
    subject: str
    scope: str
    spend_cap: float
    exp: float                 # token expiry (issuer clock)
    L_prop: float              # revocation propagation latency bound (seconds)
    last_sync: float           # local monotonic time of last revocation sync
    revocation_epoch: int      # issuer's revocation epoch at last_sync
    local_spend: float = 0.0
    issuer_spend: float = 0.0

    def effective_lease_remaining(self, now: float) -> float:
        # Committing authority expires at min(token exp, last_sync + L_prop)
        return min(self.exp, self.last_sync + self.L_prop) - now


def authorize_committing_action(warrant: FederatedWarrant, amount: float, now: float) -> None:
    lease = warrant.effective_lease_remaining(now)
    if lease <= 0:
        raise RevocationShadowViolation(
            f"warrant {warrant.subject} past effective lease "
            f"(last_sync={warrant.last_sync}, L_prop={warrant.L_prop}); "
            f"revocation status unverifiable — force sync before committing"
        )
    if warrant.local_spend + amount > warrant.spend_cap:
        raise SpendReconciliationError(
            f"local spend {warrant.local_spend + amount} exceeds cap {warrant.spend_cap} "
            f"under epoch {warrant.revocation_epoch}"
        )
    warrant.local_spend += amount


def on_revocation_sync(warrant: FederatedWarrant, new_epoch: int,
                       issuer_spend: float, now: float) -> None:
    if new_epoch > warrant.revocation_epoch:
        # Authority changed; local derived state is suspect.
        if abs(warrant.local_spend - issuer_spend) > 1e-6:
            raise SpendReconciliationError(
                f"epoch advanced {warrant.revocation_epoch}->{new_epoch}; "
                f"local_spend={warrant.local_spend} != issuer_spend={issuer_spend}"
            )
    warrant.revocation_epoch = new_epoch
    warrant.issuer_spend = issuer_spend
    warrant.local_spend = issuer_spend
    warrant.last_sync = now


# Trace: admission at T=0, revocation at T=140, sync at T=300, L_prop=300
w = FederatedWarrant(subject="procurement-agent-7", scope="purchase:initiate",
                     spend_cap=10000.0, exp=3600.0, L_prop=300.0,
                     last_sync=0.0, revocation_epoch=41)

authorize_committing_action(w, 500.0, now=10.0)   # ok: within lease
try:
    authorize_committing_action(w, 500.0, now=310.0)  # lease expired at 300
except RevocationShadowViolation as e:
    print("blocked:", e)
