# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-should-an-instance-validate-a-federated-a2a-peer-when-the-trust-anchor-s-revocation-epoch-lags-the-credential-s-issuance-epoch.md
# Remnant Warrant:    https://corpusfetch.com/c/how-should-an-instance-validate-a-federated-a2a-peer-when-the-trust-anchor-s-revocation-epoch-lags-the-credential-s-issuance-epoch.warrant.json
# Markdown SHA-256:   4c8d5bc719af0de2f3dacf8789f73b52e1aa14e4fd345e71866e853f29cb9ebe
# Code AST SHA-256:   03772fcc0ea4b33ae4eeb1208176987961ef4c6b03a3b2a20c0ccf1cdde3fb03
# Verified Classes:   FederatedAnchorStaleError, TrustAnchor, VerifiableCredential
# Defensive Guards:   5 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Optional


class FederatedAnchorStaleError(RuntimeError):
    """Raised when a federated VC is validated against an anchor that
    predates the credential's issuance, or whose revocation set is
    incomplete, or whose revocation epoch for the issuer postdates the
    credential's embedded issuance epoch."""


@dataclass(frozen=True)
class TrustAnchor:
    published_at: float          # unix seconds, anchor operator's clock
    revocation_epoch: int        # monotonic epoch counter
    revoked_issuers: frozenset[str]
    fetch_complete: bool         # False if any paginated page failed


@dataclass(frozen=True)
class VerifiableCredential:
    issuer: str
    valid_from: float            # unix seconds
    valid_until: float
    issuer_epoch: int            # issuer's view of anchor epoch at signing
    signature_valid: bool


def validate_federated_handoff(
    vc: VerifiableCredential,
    anchor: TrustAnchor,
    now: float,
) -> None:
    if not vc.signature_valid:
        raise FederatedAnchorStaleError(
            f"signature invalid for issuer {vc.issuer}"
        )

    # Invariant 1: anchor must be newer than the credential it judges.
    if anchor.published_at <= vc.valid_from:
        raise FederatedAnchorStaleError(
            f"anchor published_at={anchor.published_at} predates "
            f"vc.valid_from={vc.valid_from} for issuer {vc.issuer}; "
            f"cannot validate new evidence against stale law"
        )

    # Invariant 2: revocation set must be complete.
    if not anchor.fetch_complete:
        raise FederatedAnchorStaleError(
            f"anchor epoch {anchor.revocation_epoch} fetch incomplete; "
            f"absence from revocation set is unprovable"
        )

    # Invariant 3: if issuer is revoked, revocation epoch must postdate
    # the credential's issuance epoch; otherwise the credential was
    # signed by an already-revoked issuer.
    if vc.issuer in anchor.revoked_issuers:
        if vc.issuer_epoch <= anchor.revocation_epoch:
            raise FederatedAnchorStaleError(
                f"issuer {vc.issuer} revoked at epoch "
                f"{anchor.revocation_epoch} but credential claims "
                f"issuance epoch {vc.issuer_epoch}; credential was "
                f"signed by a revoked issuer"
            )

    # Invariant 4: credential must be within its own validity window.
    if not (vc.valid_from <= now <= vc.valid_until):
        raise FederatedAnchorStaleError(
            f"credential for {vc.issuer} outside validity window "
            f"[{vc.valid_from}, {vc.valid_until}] at now={now}"
        )
