# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/why-does-an-instance-in-a-federated-a2a-mesh-silently-accept-a-peer-whose-trust-anchor-was-rotated-mid-handshake-and-why-does-the-resulting-warrant-ch.md
# Remnant Warrant:    https://corpusfetch.com/c/why-does-an-instance-in-a-federated-a2a-mesh-silently-accept-a-peer-whose-trust-anchor-was-rotated-mid-handshake-and-why-does-the-resulting-warrant-ch.warrant.json
# Markdown SHA-256:   16e6b218fd88f6c6de3e5352486860733a68e043d13a7f1d0df0627a4cb0bf6b
# Code AST SHA-256:   29be51198f702a6f38a039e3a887998cdb2d7b4587a8567a2c4259b3ef7595d9
# Verified Classes:   AnchorState, AnchorEpoch, PeerCard, Warrant, EpochSkewError
# Defensive Guards:   4 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from enum import Enum


class AnchorState(Enum):
    CURRENT = "current"
    RETIRED_BUT_HONORED = "retired_but_honored"
    REVOKED = "revoked"


@dataclass(frozen=True)
class AnchorEpoch:
    fingerprint: str
    state: AnchorState


@dataclass(frozen=True)
class PeerCard:
    peer_id: str
    bound_epoch: AnchorEpoch
    card_hash: str


@dataclass(frozen=True)
class Warrant:
    issuer: str
    subject: str
    parent_anchor: AnchorEpoch


class EpochSkewError(RuntimeError):
    """Raised when a peer's identity epoch differs from its warrant's anchor epoch."""


def verify_federated_handoff(
    card: PeerCard,
    warrant: Warrant,
    anchor_registry: dict[str, AnchorEpoch],
    *,
    allow_rebind: bool = True,
) -> str:
    # Resolve the warrant's parent anchor to a concrete epoch.
    resolved = anchor_registry.get(warrant.parent_anchor.fingerprint)
    if resolved is None:
        raise EpochSkewError(
            f"warrant parent_anchor {warrant.parent_anchor.fingerprint} unresolvable"
        )
    if resolved.state is AnchorState.REVOKED:
        raise EpochSkewError(
            f"warrant anchored at revoked epoch {resolved.fingerprint}"
        )

    # The invariant: identity epoch must equal authorization epoch.
    if card.bound_epoch.fingerprint != resolved.fingerprint:
        if not allow_rebind:
            raise EpochSkewError(
                f"epoch skew: card bound to {card.bound_epoch.fingerprint}, "
                f"warrant anchored at {resolved.fingerprint}; rebind disabled"
            )
        # Forced re-binding: re-fetch and re-verify the card under the new epoch.
        rebound = rebind_card(card, resolved)
        if rebound.bound_epoch.fingerprint != resolved.fingerprint:
            raise EpochSkewError(
                f"rebind failed: card still bound to {rebound.bound_epoch.fingerprint}"
            )
        return "VALID_AFTER_REBIND"

    # Epochs match; a retired-but-honored anchor is acceptable only here.
    return "VALID"


def rebind_card(card: PeerCard, epoch: AnchorEpoch) -> PeerCard:
    # In production this re-fetches the card and verifies its signature
    # against `epoch`. The stub preserves the hash to model an unchanged card.
    return PeerCard(
        peer_id=card.peer_id,
        bound_epoch=epoch,
        card_hash=card.card_hash,
    )
