# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-reject-a-federated-agent-card-whose-signature-chain-is-valid-but-whose-capability-attestation-has-expired-mid-hando.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-reject-a-federated-agent-card-whose-signature-chain-is-valid-but-whose-capability-attestation-has-expired-mid-hando.warrant.json
# Markdown SHA-256:   8680993b1c81c8f86fbd097d9e242cb26d6fe3064977ee8bd509e9062decbf2d
# Code AST SHA-256:   a429f9d4f40d4cbf2eafa4921158865db18c2dc3f167fa907e8eb95562671289
# Verified Classes:   AttestationLapseError, FederatedCard, DelegationToken, Task
# Defensive Guards:   5 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass


class AttestationLapseError(RuntimeError):
    """Raised when a federated Agent Card's capability attestation cannot cover the task horizon."""
    pass


@dataclass
class FederatedCard:
    agent_id: str
    skill: str
    signature_valid_until: float      # JWS chain window
    attestation_expires_at: float     # capability grant window
    cache_validated_at: float         # registry re-validation timestamp
    attestation_ttl: float            # duration of the attestation grant


@dataclass
class DelegationToken:
    expires_at: float


@dataclass
class Task:
    deadline_seconds: float
    idempotent_readonly: bool = False


SKEW_TOLERANCE = 60.0  # seconds, A2A v1.0 recommended bound


def assert_dispatchable(card: FederatedCard, token: DelegationToken, task: Task, now: float | None = None) -> None:
    now = time.time() if now is None else now
    horizon = now + task.deadline_seconds

    # 1. Signature must be valid (necessary, not sufficient).
    if card.signature_valid_until <= now:
        raise AttestationLapseError(
            f"card {card.agent_id} signature expired at {card.signature_valid_until}"
        )

    # 2. Registry staleness: card must have been re-validated within its attestation window.
    if card.cache_validated_at + card.attestation_ttl < now:
        raise AttestationLapseError(
            f"card {card.agent_id} served stale: validated at {card.cache_validated_at}, "
            f"ttl {card.attestation_ttl}, now {now}"
        )

    # 3. Attestation must cover the task horizon (with skew), unless idempotent/read-only.
    if not task.idempotent_readonly:
        if card.attestation_expires_at <= now + SKEW_TOLERANCE:
            raise AttestationLapseError(
                f"attestation for {card.agent_id}/{card.skill} lapsed at "
                f"{card.attestation_expires_at} (now {now}, skew {SKEW_TOLERANCE})"
            )
        if card.attestation_expires_at < horizon:
            raise AttestationLapseError(
                f"attestation for {card.agent_id}/{card.skill} expires "
                f"{card.attestation_expires_at} before task horizon {horizon}"
            )

    # 4. Token must be subordinate to the attestation grant.
    if token.expires_at > card.attestation_expires_at:
        raise AttestationLapseError(
            f"delegation token for {card.agent_id} outlives attestation: "
            f"token {token.expires_at} > attestation {card.attestation_expires_at}"
        )


def dispatch(card: FederatedCard, token: DelegationToken, task: Task) -> str:
    assert_dispatchable(card, token, task)
    return f"dispatched {task!r} to {card.agent_id}/{card.skill}"
