# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-a-federated-handoff-when-the-peer-s-trust-anchor-is-valid-but-its-policy-epoch-is-stale.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-a-federated-handoff-when-the-peer-s-trust-anchor-is-valid-but-its-policy-epoch-is-stale.warrant.json
# Markdown SHA-256:   979626fd5f6ce1381bd2d0dd06d48056f0440a622e54021596a0f1d2fb6e1e05
# Code AST SHA-256:   502af90cadaed0ce97c3e5ed4185a4a064806d8733bb6e3b73b88f80389248be
# Verified Classes:   FederationHandoffError, EpochVerdict, EpochManifest, Handoff
# Defensive Guards:   2 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from enum import Enum


class FederationHandoffError(RuntimeError):
    """Raised when a federated handoff violates a boundary invariant."""
    pass


class EpochVerdict(Enum):
    ACCEPT = "accept"
    QUARANTINE = "quarantine"
    REJECT = "reject"


@dataclass(frozen=True)
class EpochManifest:
    issuer_domain: str
    ratified_set: frozenset
    retired_floor: int
    manifest_hash: str
    freshness_ok: bool  # False if served from stale-on-error fallback


@dataclass(frozen=True)
class Handoff:
    peer_domain: str
    anchor_valid: bool
    asserted_epoch: int
    anchor_issuer: str
    epoch_issuer: str


def classify_handoff(handoff: Handoff, manifest: EpochManifest) -> EpochVerdict:
    # Plane 1: identity. Independent of policy.
    if not handoff.anchor_valid:
        return EpochVerdict.REJECT

    # Same-authority binding: anchor and epoch must resolve to one domain identity.
    if handoff.anchor_issuer != handoff.epoch_issuer:
        raise FederationHandoffError(
            f"split-authority: anchor issuer {handoff.anchor_issuer!r} != "
            f"epoch issuer {handoff.epoch_issuer!r} for peer {handoff.peer_domain!r}"
        )

    # Plane 2: policy currency. Never inferred from anchor validity.
    if not manifest.freshness_ok:
        # Unknown currency: cannot prove membership against a fresh manifest.
        return EpochVerdict.QUARANTINE

    if handoff.asserted_epoch < manifest.retired_floor:
        # Known-stale: demonstrably behind a retired epoch.
        return EpochVerdict.REJECT

    if handoff.asserted_epoch not in manifest.ratified_set:
        # Fresh manifest, epoch not ratified: unknown currency, not stale.
        return EpochVerdict.QUARANTINE

    return EpochVerdict.ACCEPT


def route_federated_handoff(handoff: Handoff, manifest: EpochManifest) -> str:
    verdict = classify_handoff(handoff, manifest)
    if verdict is EpochVerdict.REJECT:
        raise FederationHandoffError(
            f"handoff from {handoff.peer_domain!r} rejected: "
            f"epoch {handoff.asserted_epoch} below retired floor "
            f"{manifest.retired_floor} (manifest {manifest.manifest_hash})"
        )
    if verdict is EpochVerdict.QUARANTINE:
        return f"quarantined:{manifest.manifest_hash}"
    return f"executed:{manifest.manifest_hash}"
