# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-a-federated-agent-s-delegated-revocation-has-silently-failed-to-propagate-across-an-org-boundary.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-a-federated-agent-s-delegated-revocation-has-silently-failed-to-propagate-across-an-org-boundary.warrant.json
# Markdown SHA-256:   62ac7ccc643086500f4e5dbed10d6659a69aacbbd81398aa89a3377fb90bdaf6
# Code AST SHA-256:   158943b2848ecfe4aacc6e2652e6d5a9615bcfd7b1d9026e78810a856f80bb5b
# Verified Classes:   RevocationPropagationError, AuthorityTuple
# Defensive Guards:   4 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Optional


class RevocationPropagationError(RuntimeError):
    """Raised when a delegated revocation cannot be confirmed at the relying party."""


@dataclass(frozen=True)
class AuthorityTuple:
    issuer: str
    subject: str
    capability: str
    scope: str
    key_epoch: str          # kid / rotation epoch — MUST be part of the key
    not_before: int

    def revocation_key(self) -> str:
        # Revocation is keyed on the exact authority, never on a bare warrant_id.
        return "|".join(
            (self.issuer, self.subject, self.capability,
             self.scope, self.key_epoch, str(self.not_before))
        )


def confirm_revocation(
    authority: AuthorityTuple,
    delegator_store: dict,
    relying_cache: dict,
    cache_ttl: int,
    warrant_remaining_ttl: int,
    relying_reachable: bool,
) -> bool:
    key = authority.revocation_key()

    # Invariant 1: a cached-valid decision must never outlive the warrant itself.
    if cache_ttl >= warrant_remaining_ttl:
        raise RevocationPropagationError(
            f"cache_ttl={cache_ttl}s >= remaining_ttl={warrant_remaining_ttl}s; "
            "revoked warrant can be served past expiry"
        )

    # Invariant 2: the delegator's store must be the source of truth for intent.
    if key not in delegator_store:
        raise RevocationPropagationError(
            f"authority {key} absent from delegator revocation store "
            "(possible tombstone loss during key rotation)"
        )

    if delegator_store[key] != "revoked":
        return False  # not revoked; nothing to propagate

    # Invariant 3: propagation is complete only when the relying party reflects it.
    if not relying_reachable:
        # Fail closed: a stale cached-valid decision is not acceptable.
        raise RevocationPropagationError(
            f"relying party unreachable; cannot confirm revocation of {key}; "
            "failing closed rather than serving last-known decision"
        )

    if relying_cache.get(key) != "revoked":
        raise RevocationPropagationError(
            f"revocation of {key} not yet reflected at relying party; "
            f"blind window up to {cache_ttl}s"
        )

    return True
