# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-does-an-instance-detect-a-silent-agent-card-capability-downgrade-during-a2a-v1-0-task-resumption-after-push-notification-reattachment.md
# Remnant Warrant:    https://corpusfetch.com/c/how-does-an-instance-detect-a-silent-agent-card-capability-downgrade-during-a2a-v1-0-task-resumption-after-push-notification-reattachment.warrant.json
# Markdown SHA-256:   c971c3c73f6eb83a918e0363372a4ce22e2322b24284551e32a640d2d38dc9d2
# Code AST SHA-256:   5acac1b40de6c8a08eeb6d88151ef5f00c180c5c4eeac70625e53d18c0f77a69
# Verified Classes:   CapabilityDowngrade, DeliveryPlan, AgentCard, Task
# Defensive Guards:   1 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import FrozenSet


class CapabilityDowngrade(RuntimeError):
    """Raised when a resumed task's governing Agent Card no longer admits
    the delivery plan the client committed to at tasks/send."""


@dataclass(frozen=True)
class DeliveryPlan:
    push_notifications: bool
    streaming: bool
    output_modes: FrozenSet[str]


@dataclass(frozen=True)
class AgentCard:
    version: str
    push_notifications: bool
    streaming: bool
    output_modes: FrozenSet[str]

    def admits(self, plan: DeliveryPlan) -> bool:
        if plan.push_notifications and not self.push_notifications:
            return False
        if plan.streaming and not self.streaming:
            return False
        if not plan.output_modes.issubset(self.output_modes):
            return False
        return True


@dataclass
class Task:
    task_id: str
    context_id: str
    original_card: AgentCard
    delivery_plan: DeliveryPlan
    state: str = "input-required"
    provenance: list = field(default_factory=list)


def resume_task(task: Task, fresh_card: AgentCard, client_parseable: FrozenSet[str]) -> DeliveryPlan:
    """Subset-check the delivery plan against the freshly fetched card.

    Returns the (possibly degraded) plan to resume with, or raises
    CapabilityDowngrade if no valid plan can be synthesized.
    """
    if fresh_card.admits(task.delivery_plan):
        task.provenance.append(f"resume: card {fresh_card.version} admits original plan")
        return task.delivery_plan

    # Attempt degrade-and-continue: rebuild the plan from the new card's
    # capabilities, intersected with what the client can actually parse.
    usable_modes = fresh_card.output_modes & client_parseable
    if not usable_modes:
        raise CapabilityDowngrade(
            f"task {task.task_id}: card downgraded from {task.original_card.version} "
            f"to {fresh_card.version}; no output mode survives intersection with "
            f"client parseable set {sorted(client_parseable)}"
        )

    degraded = DeliveryPlan(
        push_notifications=fresh_card.push_notifications,
        streaming=fresh_card.streaming,
        output_modes=usable_modes,
    )
    task.provenance.append(
        f"resume: card {task.original_card.version} -> {fresh_card.version} degraded plan "
        f"push={degraded.push_notifications} stream={degraded.streaming} "
        f"modes={sorted(degraded.output_modes)}"
    )
    return degraded
