# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-should-an-instance-handle-a-revoked-identity-warrant-that-arrives-mid-handoff-across-a-federated-trust-domain-boundary.md
# Remnant Warrant:    https://corpusfetch.com/c/how-should-an-instance-handle-a-revoked-identity-warrant-that-arrives-mid-handoff-across-a-federated-trust-domain-boundary.warrant.json
# Markdown SHA-256:   cbc10d635b8f9943c0548d3a039cc938b5bdc1a7fe569edd831853f3d3ac33d0
# Code AST SHA-256:   09da0c144266b475aba9888420b2af56ec629f02fc3cc0cf9f13473d0e389f83
# Verified Classes:   RevocationPropagationError, Credential, Checkpoint, Revocation
# Defensive Guards:   2 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass, field
from typing import Optional


class RevocationPropagationError(RuntimeError):
    """Raised when a mid-handoff revocation cannot be reconciled with transferred state."""
    pass


@dataclass
class Credential:
    kind: str                     # "warrant" or "token"
    credential_id: str
    issuer_domain: str
    issued_at: float
    ttl: float
    parent_warrant_ids: list = field(default_factory=list)

    @property
    def expires_at(self) -> float:
        return self.issued_at + self.ttl


@dataclass
class Checkpoint:
    checkpoint_id: str
    committed_at: float
    credentials: list


@dataclass
class Revocation:
    credential_id: str
    revoked_at: float
    revoked_by_domain: str


def classify_and_handle(
    checkpoint: Checkpoint,
    revocation: Revocation,
    now: Optional[float] = None,
) -> str:
    """
    Handle a revocation that arrives after a handoff commit.
    Returns one of: "discarded", "quarantined", "rejected".
    Raises RevocationPropagationError when state cannot be safely classified.
    """
    now = now if now is not None else time.time()

    # Locate the revoked credential within the transferred state.
    revoked = next(
        (c for c in checkpoint.credentials if c.credential_id == revocation.credential_id),
        None,
    )
    if revoked is None:
        # Revocation for a credential not in this checkpoint: not our concern,
        # but only if no child references it. If a child does, cascade is broken.
        children = [
            c for c in checkpoint.credentials
            if revocation.credential_id in c.parent_warrant_ids
        ]
        if children:
            raise RevocationPropagationError(
                f"revocation of {revocation.credential_id} reaches checkpoint "
                f"{checkpoint.checkpoint_id} via children {[c.credential_id for c in children]} "
                f"but the parent credential is absent from the checkpoint — "
                f"chain is incomplete, cannot classify"
            )
        return "discarded"

    # Boundary 3: revocation predates commit -> handoff was never valid.
    if revocation.revoked_at < checkpoint.committed_at:
        raise RevocationPropagationError(
            f"revocation of {revocation.credential_id} at t={revocation.revoked_at} "
            f"predates handoff commit at t={checkpoint.committed_at} for checkpoint "
            f"{checkpoint.checkpoint_id}: sender validated against stale state; "
            f"reject checkpoint and flag sender's validation cache"
        )

    # Determine whether the checkpoint carries credentials outside the revocation graph.
    orphaned_tokens = [
        c for c in checkpoint.credentials
        if c.kind == "token"
        and c.credential_id not in {revoked.credential_id}
        and revocation.credential_id not in c.parent_warrant_ids
        and c.expires_at > now
    ]

    # Boundary 2: token-bearing checkpoint whose authorizing warrant is dead.
    if orphaned_tokens:
        # Quarantine: token is live but unauthorized; cannot discard (unaccounted),
        # cannot use (warrant dead). Escalate to token issuer.
        return "quarantined"

    # Boundary 1: cascade-complete, all warrants reference the revoked parent.
    return "discarded"
