# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-reject-a-transitive-a2a-v1-0-capability-attestation-when-the-attesting-org-s-reputation-score-crosses-a-trust-a.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-reject-a-transitive-a2a-v1-0-capability-attestation-when-the-attesting-org-s-reputation-score-crosses-a-trust-a.warrant.json
# Markdown SHA-256:   565e507ee0590c4e25b96b85bb9ecfde01021f814964d16b7ff2e0a93c7ca8b4
# Code AST SHA-256:   58a79a9ca7478bbe370ec5d2aee823f8afb244bbe6548d5d5e80f6fa8d7a0506
# Verified Classes:   ReputationAnchorError, StaleReputationAnchor, AttestorBelowFloorPostSlash, Attestation, ReputationRead
# Defensive Guards:   5 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Optional


class ReputationAnchorError(RuntimeError):
    """Base for reputation-epoch boundary violations during A2A attestation validation."""


class StaleReputationAnchor(ReputationAnchorError):
    """Verifier's reputation read cannot be proven >= attestation mint epoch and post-slash."""


class AttestorBelowFloorPostSlash(ReputationAnchorError):
    """Attestor's authoritative post-slash score is below the verifier's trust-anchor floor."""


@dataclass(frozen=True)
class Attestation:
    attestor_org: str
    capability: str
    minted_epoch: Optional[int]  # None => unboundable staleness


@dataclass(frozen=True)
class ReputationRead:
    org: str
    score: float
    feed_epoch: int
    last_slash_epoch: int


def validate_transitive_attestation(
    attestation: Attestation,
    cached: ReputationRead,
    synchronous_reader,  # callable(org) -> ReputationRead
    trust_anchor_floor: float,
) -> None:
    # 1. Cannot bound staleness without a mint epoch.
    if attestation.minted_epoch is None:
        raise StaleReputationAnchor(
            f"attestation from {attestation.attestor_org} lacks minted_epoch; "
            f"cannot bound reputation staleness for capability {attestation.capability}"
        )

    mint = attestation.minted_epoch

    # 2. Cache is usable only if it is at-or-after mint AND has seen any slash at-or-before mint.
    cache_usable = (
        cached.feed_epoch >= mint and cached.last_slash_epoch <= mint
    )

    if cache_usable:
        if cached.score < trust_anchor_floor:
            raise AttestorBelowFloorPostSlash(
                f"cached score {cached.score} for {cached.org} below floor {trust_anchor_floor} "
                f"at feed_epoch {cached.feed_epoch}"
            )
        return

    # 3. Cache is permissively stale (feed lags mint, or a slash post-dates mint).
    #    Force a synchronous read; do NOT treat feed lag as evidence of innocence.
    fresh = synchronous_reader(attestation.attestor_org)

    if fresh.feed_epoch < mint:
        raise StaleReputationAnchor(
            f"authoritative feed_epoch {fresh.feed_epoch} still lags minted_epoch {mint} "
            f"for {fresh.org}; rejecting rather than assuming innocence under feed lag"
        )

    if fresh.last_slash_epoch > mint and fresh.score < trust_anchor_floor:
        raise AttestorBelowFloorPostSlash(
            f"post-slash score {fresh.score} for {fresh.org} below floor {trust_anchor_floor} "
            f"(slash at epoch {fresh.last_slash_epoch} > mint {mint})"
        )

    if fresh.score < trust_anchor_floor:
        raise AttestorBelowFloorPostSlash(
            f"fresh score {fresh.score} for {fresh.org} below floor {trust_anchor_floor}"
        )
