# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-reject-a-federated-agent-card-whose-verifiable-credential-is-valid-but-whose-issuer-s-delegation-chain-has-been-re-rooted-mid-ha.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-reject-a-federated-agent-card-whose-verifiable-credential-is-valid-but-whose-issuer-s-delegation-chain-has-been-re-rooted-mid-ha.warrant.json
# Markdown SHA-256:   ff0f75d5957e6ba803edd1a1661f74b94da4ec254506497e4c08a7699e49b55f
# Code AST SHA-256:   400d5013729a838dc1e6fb79e88b86535a8b722d3585829801c7fdaa120d3955
# Verified Classes:   FederatedTrustEpochError, AgentCard, Credential
# Defensive Guards:   5 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Optional


class FederatedTrustEpochError(RuntimeError):
    """Raised when a delegated credential cannot be bound to a single chain epoch."""


@dataclass(frozen=True)
class AgentCard:
    agent_id: str
    root_did: str
    chain_hash: str
    chain_epoch: int
    cached_capabilities: frozenset[str]


@dataclass(frozen=True)
class Credential:
    signer_did: str
    root_did: str
    chain_hash: str
    chain_epoch: int
    asserted_capabilities: frozenset[str]
    issued_at: float


def verify_handoff(
    card: AgentCard,
    cred: Credential,
    live_authorized_capabilities: Optional[frozenset[str]],
    now: float,
    max_epoch_skew: int = 0,
) -> None:
    """Accept a cross-org handoff only if card, credential, and root share one epoch.

    live_authorized_capabilities is the signer link's current authorization
    record, fetched live. None means the issuer exposes no status endpoint.
    """
    if card.root_did != cred.root_did:
        raise FederatedTrustEpochError(
            f"root mismatch: card={card.root_did} cred={cred.root_did}"
        )

    # Epoch binding: a credential minted under a later epoch than the cached
    # card cannot be authorized by that card's capability envelope.
    if cred.chain_epoch - card.chain_epoch > max_epoch_skew:
        raise FederatedTrustEpochError(
            f"epoch splice: card epoch {card.chain_epoch} "
            f"cannot authorize credential epoch {cred.chain_epoch}; refetch required"
        )

    # Capability scoping must be checked against the signer link's LIVE
    # authorization, never the card's cached envelope.
    if live_authorized_capabilities is None:
        raise FederatedTrustEpochError(
            f"no live capability attestation for {cred.signer_did}; "
            f"fail-closed pending refetch (cached envelope is stale)"
        )

    unauthorized = cred.asserted_capabilities - live_authorized_capabilities
    if unauthorized:
        raise FederatedTrustEpochError(
            f"delegation-invalid: signer {cred.signer_did} not authorized for "
            f"{sorted(unauthorized)} under epoch {cred.chain_epoch}"
        )

    if cred.issued_at > now:
        raise FederatedTrustEpochError(
            f"credential issued_at {cred.issued_at} is in the future of verifier clock {now}"
        )
