# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-reject-a-quorum-attested-a2a-delegation-because-the-attestation-set-is-internally-inconsistent-under-transitive-trust.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-reject-a-quorum-attested-a2a-delegation-because-the-attestation-set-is-internally-inconsistent-under-transitive-trust.warrant.json
# Markdown SHA-256:   8c431c01fad15d3eb07633d0c9e6b4d0cbf29277e19e86fd95a799ea565c7711
# Code AST SHA-256:   305132e320b342359e4464b900586130caafd2cb71132cec0df2370d5427a41c
# Verified Classes:   QuorumEpochInconsistency, Attestation, Warrant
# Defensive Guards:   2 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Iterable


class QuorumEpochInconsistency(RuntimeError):
    """Raised when an attestation set is quorum-valid per-signature but not jointly satisfiable at any single trust-epoch."""


@dataclass(frozen=True)
class Attestation:
    attester: str
    warrant_id: str
    declared_epoch: int          # epoch the attester claims to have verified at
    resolved_key_version: int    # key version the resolver actually used
    signature_valid: bool


@dataclass(frozen=True)
class Warrant:
    warrant_id: str
    signing_epoch: int
    signing_key_version: int
    revoked_at_epoch: int | None


def admit_delegation(
    warrant: Warrant,
    attestations: Iterable[Attestation],
    quorum: int,
    propagation_bound: int,
) -> bool:
    attestations = list(attestations)

    # Step 1: discard attestations whose resolver fell back to a newer key version.
    # These are the B-class attestations: signature "valid" but computed against
    # key material that did not exist at the warrant's signing epoch.
    consistent = [
        a for a in attestations
        if a.signature_valid
        and a.warrant_id == warrant.warrant_id
        and a.resolved_key_version == warrant.signing_key_version
    ]

    # Step 2: reject if the surviving attestations span an epoch window wider
    # than the federation's propagation bound. No single epoch can make them
    # all correct.
    if consistent:
        epochs = [a.declared_epoch for a in consistent]
        if max(epochs) - min(epochs) > propagation_bound:
            raise QuorumEpochInconsistency(
                f"attestation epochs span {min(epochs)}..{max(epochs)} "
                f"exceeds propagation bound {propagation_bound} for warrant "
                f"{warrant.warrant_id}; set is not jointly satisfiable"
            )

    # Step 3: fail closed if the warrant was revoked at or before the
    # latest epoch any surviving attester claims to have observed.
    if warrant.revoked_at_epoch is not None and consistent:
        latest = max(a.declared_epoch for a in consistent)
        if warrant.revoked_at_epoch <= latest:
            raise QuorumEpochInconsistency(
                f"warrant {warrant.warrant_id} revoked at epoch "
                f"{warrant.revoked_at_epoch} but attested up to epoch {latest}; "
                f"admitting would execute under a dead warrant"
            )

    # Step 4: quorum is counted over the epoch-consistent subset only.
    return len(consistent) >= quorum
