# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-a-delegated-warrant-whose-trust-anchor-rotated-mid-chain-a2a-cross-org-federation-and-the-anchor-epoch-d.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-a-delegated-warrant-whose-trust-anchor-rotated-mid-chain-a2a-cross-org-federation-and-the-anchor-epoch-d.warrant.json
# Markdown SHA-256:   f225eccc4a6e59964f7a3e76eace80f09fca6d1fca78b1b61b4ef9997f7162b5
# Code AST SHA-256:   93eaa38be86b7baea23d9271d93969f5bef93cad366ece5aa2f2e75e9ae734c0
# Verified Classes:   EpochStraddleError, AuthorityAmplificationError, Link, Epoch
# Defensive Guards:   5 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Mapping


class EpochStraddleError(RuntimeError):
    """Raised when a delegated warrant's chain spans anchor epochs without re-attestation."""


class AuthorityAmplificationError(RuntimeError):
    """Raised when a link's effective scope exceeds the epoch under which it was signed."""


@dataclass(frozen=True)
class Link:
    issuer: str
    subject: str
    scope: frozenset[str]
    signed_under_epoch: str


@dataclass(frozen=True)
class Epoch:
    id: str
    trusted_for_verify: bool
    # Authority ceiling granted by this epoch's policy table.
    scope_ceiling: frozenset[str]


def validate_chain(
    chain: list[Link],
    epochs: Mapping[str, Epoch],
    current_epoch_id: str,
    reattestation_epoch: str | None,
) -> None:
    if not chain:
        raise EpochStraddleError("empty chain has no anchor epoch")

    epochs_in_chain = {link.signed_under_epoch for link in chain}

    # Invariant 1: no straddling without explicit re-attestation.
    if len(epochs_in_chain) > 1:
        if reattestation_epoch not in epochs_in_chain:
            raise EpochStraddleError(
                f"chain straddles epochs {sorted(epochs_in_chain)} "
                f"without re-attestation under {reattestation_epoch!r}"
            )

    for link in chain:
        epoch = epochs.get(link.signed_under_epoch)
        if epoch is None:
            raise EpochStraddleError(
                f"link {link.issuer}->{link.subject} references unknown epoch "
                f"{link.signed_under_epoch!r}"
            )
        if not epoch.trusted_for_verify:
            raise EpochStraddleError(
                f"epoch {epoch.id!r} is no longer trusted for verification"
            )

        # Invariant 2: authority is bounded by the signing epoch's ceiling,
        # never by the current epoch's (possibly widened) policy.
        if not link.scope.issubset(epoch.scope_ceiling):
            raise AuthorityAmplificationError(
                f"link {link.issuer}->{link.subject} scope {sorted(link.scope)} "
                f"exceeds epoch {epoch.id!r} ceiling {sorted(epoch.scope_ceiling)}; "
                f"current epoch {current_epoch_id!r} does not retroactively widen it"
            )


def admit_warrant(chain: list[Link], epochs: Mapping[str, Epoch],
                  current_epoch_id: str, reattestation_epoch: str | None) -> bool:
    validate_chain(chain, epochs, current_epoch_id, reattestation_epoch)
    return True
