# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-revokes-a-federated-warrant-but-its-downstream-verifier-cache-still-honors-the-old-capability-for-800ms.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-revokes-a-federated-warrant-but-its-downstream-verifier-cache-still-honors-the-old-capability-for-800ms.warrant.json
# Markdown SHA-256:   9cd99dbae2775b7b8a46fbf5491d101d37c8b5da5d7d36964adaa59d8eb8f077
# Code AST SHA-256:   40e8fbb37de48e0c57f6e78f9f011d667351fe71ce3aedac68f06371ff2c292e
# Verified Classes:   RevocationShadowError, Warrant, Verifier
# Defensive Guards:   2 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import Dict, Tuple


class RevocationShadowError(RuntimeError):
    """Raised when a tool call is admitted under a warrant version that a
    verifier has already seen revoked, or when a supervisor attempts a
    revoke-then-grant sequence before the revocation is acknowledged."""
    pass


@dataclass
class Warrant:
    warrant_id: str
    version: int = 0          # monotonic; bumped on every revoke
    revoked: bool = False


@dataclass
class Verifier:
    name: str
    # grow-only: warrant_id -> highest revocation version seen
    seen_revocations: Dict[str, int] = field(default_factory=dict)

    def observe_revocation(self, warrant_id: str, version: int) -> None:
        # monotonic merge: never lower a seen version
        prev = self.seen_revocations.get(warrant_id, -1)
        if version > prev:
            self.seen_revocations[warrant_id] = version

    def admit_call(self, warrant: Warrant, call_version: int) -> None:
        """Call-scoped enforcement: reject if the call's warrant version is
        older than the last revocation this verifier has observed."""
        floor = self.seen_revocations.get(warrant.warrant_id, -1)
        if call_version <= floor:
            raise RevocationShadowError(
                f"verifier {self.name} rejects call under warrant "
                f"{warrant.warrant_id} v{call_version}: revocation v{floor} "
                f"already observed (revocation shadow)"
            )


def supervisor_revoke_then_grant(
    warrant: Warrant,
    verifiers: Tuple[Verifier, ...],
    staleness_bound_ms: int,
    ack_latency_ms: int,
) -> None:
    """Supervisor must not grant a replacement until every verifier on the
    new grant's path has acknowledged the revocation version. Otherwise a
    dual-liveness window opens."""
    warrant.revoked = True
    warrant.version += 1
    for v in verifiers:
        v.observe_revocation(warrant.warrant_id, warrant.version)

    # The sequencing lock: if acks cannot complete inside the bound, refuse
    # to issue the replacement rather than open a shadow window.
    if ack_latency_ms > staleness_bound_ms:
        raise RevocationShadowError(
            f"cannot grant replacement for {warrant.warrant_id}: ack latency "
            f"{ack_latency_ms}ms exceeds staleness bound {staleness_bound_ms}ms; "
            f"dual-liveness window would open"
        )
    # safe to grant replacement here
