# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-reject-a-peer-s-agent-card-when-the-card-s-declared-defaultinputmodes-and-skills-inputmodes-disagree-with-the-mime-types-actual.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-reject-a-peer-s-agent-card-when-the-card-s-declared-defaultinputmodes-and-skills-inputmodes-disagree-with-the-mime-types-actual.warrant.json
# Markdown SHA-256:   45858794cc27def9a0ed682b2a22d76f1629a8a902c32bff2caf8ff6baad7edb
# Code AST SHA-256:   ca7e75be2741fda8b2a60712013c99792562b7ba11eaaf8d99678508b97ce80f
# Verified Classes:   PushBindingMimeMismatch, AgentCard, WebhookHandler
# Defensive Guards:   3 fail-closed invariants (zero external deps)

from __future__ import annotations

from dataclasses import dataclass, field
from typing import FrozenSet, Optional


class PushBindingMimeMismatch(RuntimeError):
    """Raised when a push-notification binding would validate future
    payloads against a MIME set that cannot accept the skill's output
    modes. Binding under these conditions guarantees spurious task
    failure on the server side and silent work loss on the client."""


@dataclass(frozen=True)
class AgentCard:
    default_input_modes: FrozenSet[str]
    skills: dict[str, FrozenSet[str]]  # skill_id -> output_modes
    push_notifications: bool


@dataclass
class WebhookHandler:
    # Singleton handler: validates every payload against one fixed set.
    # This is the common SDK shape and the source of the C3 defect.
    accepted_mime_types: FrozenSet[str] = field(default_factory=frozenset)


def bind_push_notification(
    card: AgentCard,
    skill_id: str,
    handler: WebhookHandler,
    *,
    per_task_dispatch: bool,
) -> None:
    if not card.push_notifications:
        raise PushBindingMimeMismatch(
            f"card does not advertise pushNotifications; cannot bind for skill {skill_id!r}"
        )
    if skill_id not in card.skills:
        raise PushBindingMimeMismatch(
            f"skill {skill_id!r} absent from card; refusing to bind"
        )

    skill_outputs = card.skills[skill_id]
    # Payloads carry both status updates (top-level modes) and artifacts
    # (skill-level modes). The handler must accept the union.
    required = card.default_input_modes | skill_outputs

    if per_task_dispatch:
        # Handler will be re-parameterized per task; safe to bind.
        handler.accepted_mime_types = required
        return

    # Singleton handler: it can only ever accept one set. If the skill's
    # outputs exceed what the handler already accepts, binding is unsafe.
    missing = required - handler.accepted_mime_types
    if missing:
        raise PushBindingMimeMismatch(
            f"skill {skill_id!r} requires MIME types {sorted(missing)} "
            f"not accepted by singleton webhook handler "
            f"({sorted(handler.accepted_mime_types)}); "
            f"bind would cause server retry exhaustion and mark a "
            f"completed task failed. Fall back to tasks/get polling."
        )
    # Safe: handler covers the union.
