# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-a-peer-s-capability-attestation-as-revoked-mid-handoff-when-federation-trust-roots-disagree-on-policy-epoch.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-a-peer-s-capability-attestation-as-revoked-mid-handoff-when-federation-trust-roots-disagree-on-policy-epoch.warrant.json
# Markdown SHA-256:   f8d46f36fd3aef834088de620708e2430107fe91cd1c90f3855b48952a6b8705
# Code AST SHA-256:   cfe73b1512c614aa2657f72ff787c640ce71250c7375b7bf2da7621622381aac
# Verified Classes:   FederatedAttestationError, CapabilityAttestation, EpochFloor
# Defensive Guards:   4 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass


class FederatedAttestationError(RuntimeError):
    """Raised when a cross-org attestation cannot be safely executed."""
    pass


@dataclass
class CapabilityAttestation:
    capability: str
    amount: int
    policy_epoch: int
    sender_root: str
    signature_valid: bool


@dataclass
class EpochFloor:
    peer: str
    floor: int
    resolved_at: float
    validity_window_s: float


def resolve_epoch_floor(peer: str, feed_floor: int, resolved_at: float,
                        validity_window_s: float) -> EpochFloor:
    return EpochFloor(peer, feed_floor, resolved_at, validity_window_s)


def authorize_federated_invocation(
    attest: CapabilityAttestation,
    sender_floor: EpochFloor,
    receiver_floor: EpochFloor,
    now: float,
) -> None:
    if not attest.signature_valid:
        raise FederatedAttestationError(
            f"signature invalid for {attest.capability}; cannot authorize"
        )

    # An unresolved (stale) floor is a hard stop, never a permissive default.
    for floor in (sender_floor, receiver_floor):
        if now - floor.resolved_at > floor.validity_window_s:
            raise FederatedAttestationError(
                f"epoch floor for {floor.peer} is stale "
                f"({now - floor.resolved_at:.1f}s > {floor.validity_window_s}s); "
                f"refusing to execute under unconfirmed policy epoch"
            )

    required_floor = max(sender_floor.floor, receiver_floor.floor)
    if attest.policy_epoch < required_floor:
        # Valid signature, but the attestation predates a floor bump:
        # this is the revocation-bypass condition.
        raise FederatedAttestationError(
            f"attestation epoch {attest.policy_epoch} < required floor "
            f"{required_floor} for {attest.capability}; "
            f"provisionally revoked, escalate to reconciliation"
        )

    # Floor satisfied at use-time; side effect may proceed atomically.
    return None


# --- Reproduction of the observed incident ---
now = time.time()
attest = CapabilityAttestation(
    capability="settle_invoice",
    amount=47_000,
    policy_epoch=41,
    sender_root="B-root-v7",
    signature_valid=True,  # signature genuinely valid at receipt
)
sender_floor = resolve_epoch_floor("OrgB", feed_floor=42, resolved_at=now, validity_window_s=60)
receiver_floor = resolve_epoch_floor("OrgA", feed_floor=41, resolved_at=now, validity_window_s=60)

try:
    authorize_federated_invocation(attest, sender_floor, receiver_floor, now)
    print("executed")  # would print under the naive verify-on-receipt path
except FederatedAttestationError as e:
    # Correct behavior: abort instead of moving $47k on withdrawn authority.
    raise FederatedAttestationError(f"handoff aborted: {e}") from e
