# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-re-attests-a-federated-capability-mid-handoff-and-the-peer-s-trust-anchor-has-rotated-within-the-attestation-s-validity.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-re-attests-a-federated-capability-mid-handoff-and-the-peer-s-trust-anchor-has-rotated-within-the-attestation-s-validity.warrant.json
# Markdown SHA-256:   20d800f16a2c821ef74895dd9d85454abc82bc99120f8d1090af8ba433266a3a
# Code AST SHA-256:   2a61dfc869fea93ae93297b91cec189093976ec90d2a973e5f53b9780f5bd1ba
# Verified Classes:   AnchorContinuityError, AnchorEpoch, Attestation
# Defensive Guards:   4 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Optional


class AnchorContinuityError(RuntimeError):
    """Raised when an attestation cannot be chained to a currently-authoritative anchor
    without violating the co-bounding invariant."""
    pass


@dataclass
class AnchorEpoch:
    anchor_id: str
    valid_until: float          # absolute time this anchor stopped being authoritative
    retired: bool


@dataclass
class Attestation:
    signer_anchor_id: str
    not_before: float
    not_after: float


def admit_handoff(
    att: Attestation,
    presented_anchor_chain: list[str],
    trust_store: dict[str, AnchorEpoch],
    peer_poll_interval: float,
    now: float,
) -> str:
    """
    Returns one of: "admit", "admit_re_attest_required".
    Raises AnchorContinuityError when the invariant cannot be satisfied.
    Never admits on a presented chain that is absent from the current trust store
    without flagging re-attestation.
    """
    epoch = trust_store.get(att.signer_anchor_id)

    # Case 1: signer anchor unknown to this peer at all -> hard reject.
    if epoch is None:
        raise AnchorContinuityError(
            f"attestation signed by {att.signer_anchor_id!r} which is not in trust store"
        )

    # Co-bounding invariant: attestation must not outlive anchor authority + poll lag.
    if att.not_after > epoch.valid_until + peer_poll_interval:
        raise AnchorContinuityError(
            f"attestation not_after={att.not_after} exceeds anchor authority "
            f"{epoch.valid_until} + poll_interval {peer_poll_interval}; "
            f"silent-over-permission window unclosed"
        )

    # Boundary: anchor retired mid-window, attestation still temporally valid.
    if epoch.retired and now >= epoch.valid_until:
        if now < att.not_after:
            # Admit to preserve continuity, but force re-attestation before reuse.
            return "admit_re_attest_required"
        raise AnchorContinuityError(
            "attestation expired under a retired anchor; re-attestation required"
        )

    # Presented chain is advisory only; trust store is authoritative.
    if att.signer_anchor_id not in presented_anchor_chain:
        raise AnchorContinuityError(
            "presented anchor chain does not include the signer; "
            "cannot chain attestation to trusted root"
        )

    return "admit"
