# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-reject-a-federated-aaif-credential-whose-pairwise-subject-alias-resolves-correctly-but-whose-linkability-proof-bind.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-reject-a-federated-aaif-credential-whose-pairwise-subject-alias-resolves-correctly-but-whose-linkability-proof-bind.warrant.json
# Markdown SHA-256:   5e528e3010485abf27d3b71003ab504ec58169640787e5e4b320dbbb16a14f2a
# Code AST SHA-256:   2030a433d0024adab82bd900114fb20f0ad13eafa4e1625fe9a8de9d0db3c6e1
# Verified Classes:   EpochBindingError, Credential, AliasRegistryEntry
# Defensive Guards:   5 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Optional


class EpochBindingError(RuntimeError):
    """Raised when a credential's linkability proof binds to an epoch
    inconsistent with the audience's current alias-registry epoch."""


@dataclass(frozen=True)
class Credential:
    sub_pairwise: str
    audience: str
    epoch_cred: int
    proof_valid: bool


@dataclass(frozen=True)
class AliasRegistryEntry:
    sub_pairwise: str
    epoch_alias: int


def admit_federated_credential(
    cred: Credential,
    registry: Optional[AliasRegistryEntry],
    current_epoch: int,
    max_skew_epochs: int = 1,
) -> str:
    if not cred.proof_valid:
        raise EpochBindingError(
            f"linkability proof failed for sub_pairwise={cred.sub_pairwise!r}"
        )
    if registry is None:
        # Absence of a registry entry is not "valid by default"; it is
        # unverifiable and must not confer authority.
        raise EpochBindingError(
            f"no alias registry entry for audience={cred.audience!r}; "
            "cannot establish epoch binding"
        )
    if registry.sub_pairwise != cred.sub_pairwise:
        raise EpochBindingError(
            f"alias mismatch: cred={cred.sub_pairwise!r} "
            f"registry={registry.sub_pairwise!r}"
        )
    if cred.epoch_cred != registry.epoch_alias:
        # The alias has been re-anchored (cred < alias) or the verifier's
        # registry view lags (cred > alias). Both are inadmissible.
        raise EpochBindingError(
            f"epoch binding severed: epoch_cred={cred.epoch_cred} "
            f"epoch_alias={registry.epoch_alias} audience={cred.audience!r}"
        )
    if abs(current_epoch - cred.epoch_cred) > max_skew_epochs:
        raise EpochBindingError(
            f"epoch outside acceptance window: epoch_cred={cred.epoch_cred} "
            f"current={current_epoch} max_skew={max_skew_epochs}"
        )
    return f"admitted:{cred.sub_pairwise}@{cred.epoch_cred}"
