# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-reject-a-resumed-a2a-task-when-the-peer-s-agent-card-has-silently-downgraded-its-declared-capabilities-pushnotifications-betwee.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-reject-a-resumed-a2a-task-when-the-peer-s-agent-card-has-silently-downgraded-its-declared-capabilities-pushnotifications-betwee.warrant.json
# Markdown SHA-256:   99bdbf6a0bd1f5eccda1368ba7141bbf01eaf5b51577c9808e226686303e0257
# Code AST SHA-256:   b4e7dfc98e8600c09376181d08582d8a3e386d8be75ee8c2d2187e19f3b0384f
# Verified Classes:   WebhookCapabilityRevokedError, AgentCard, TaskView, A2AClient
# Defensive Guards:   2 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass
from typing import Optional


class WebhookCapabilityRevokedError(RuntimeError):
    """Raised when a task's registered webhook can no longer be honored
    because the peer's Agent Card dropped pushNotifications between
    registration and the current state transition."""


@dataclass
class AgentCard:
    version: str
    push_notifications: bool
    skills: tuple[str, ...]


@dataclass
class TaskView:
    task_id: str
    state: str
    webhook_registered: bool
    registered_card_version: str


class A2AClient:
    CARD_TTL_SECONDS = 60.0

    def __init__(self, fetch_card, get_task, poll_interval=5.0):
        self._fetch_card = fetch_card
        self._get_task = get_task
        self._poll_interval = poll_interval
        self._card_cache: Optional[AgentCard] = None
        self._card_fetched_at: float = 0.0

    def _card(self, force: bool = False) -> AgentCard:
        now = time.monotonic()
        if force or self._card_cache is None or (now - self._card_fetched_at) > self.CARD_TTL_SECONDS:
            self._card_cache = self._fetch_card()
            self._card_fetched_at = now
        return self._card_cache

    def resume_task(self, task: TaskView) -> TaskView:
        # Force a card re-fetch on any state-changing resume; never trust
        # the cached card for a task with an active webhook registration.
        card = self._card(force=True)
        fresh = self._get_task(task.task_id)

        if fresh.state != task.state and task.webhook_registered:
            if not card.push_notifications:
                raise WebhookCapabilityRevokedError(
                    f"task {task.task_id}: registered webhook under card "
                    f"v{task.registered_card_version} but peer now serves "
                    f"v{card.version} with pushNotifications=false; "
                    f"webhook is dead, switch to degraded-polling"
                )
            if set(card.skills) != set(task.registered_skills):
                raise WebhookCapabilityRevokedError(
                    f"task {task.task_id}: skill set changed "
                    f"{task.registered_skills} -> {card.skills}; "
                    f"re-validate task routing before resuming"
                )

        task.state = fresh.state
        return task
