# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-accepts-a-delegated-task-under-an-a2a-v1-0-agent-card-whose-declared-defaultinputmodes-defaultoutputmodes-are-satisfied.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-accepts-a-delegated-task-under-an-a2a-v1-0-agent-card-whose-declared-defaultinputmodes-defaultoutputmodes-are-satisfied.warrant.json
# Markdown SHA-256:   f186e2d3d3e1eb0c32eae2ccb7cd7f7d48e4c2e20fc8fb7ae7999cec84df14a8
# Code AST SHA-256:   b21c363e787cb20aedfb19f45b545c12e3c65014be0b426014fd9f86f1098e53
# Verified Classes:   SkillModeAdmissionError, Skill, AgentCard
# Defensive Guards:   2 fail-closed invariants (zero external deps)

from __future__ import annotations

from dataclasses import dataclass, field
from typing import Any


class SkillModeAdmissionError(RuntimeError):
    """Raised when a Message's part kinds fall outside the selected skill's
    declared inputModes, even though they satisfy the card-level defaults."""


@dataclass
class Skill:
    id: str
    input_modes: list[str] = field(default_factory=list)   # [] means "inherit card default"
    output_modes: list[str] = field(default_factory=list)


@dataclass
class AgentCard:
    default_input_modes: list[str]
    default_output_modes: list[str]
    skills: list[Skill]


def resolve_effective_input_modes(card: AgentCard, skill_id: str) -> list[str]:
    for skill in card.skills:
        if skill.id == skill_id:
            # Skill-scoped declaration is authoritative when present.
            return skill.input_modes or card.default_input_modes
    raise SkillModeAdmissionError(
        f"skillId {skill_id!r} not present in card; delegation cannot be admitted"
    )


def admit_task(card: AgentCard, skill_id: str, message_parts: list[dict[str, Any]]) -> None:
    effective = resolve_effective_input_modes(card, skill_id)
    for part in message_parts:
        kind = part.get("kind")
        mime = part.get("mimeType")
        # A part is admissible iff its kind (and mime, when kind == "file")
        # is in the *skill-scoped* effective set, never the card default alone.
        token = mime if kind == "file" and mime else kind
        if token not in effective:
            raise SkillModeAdmissionError(
                f"skill {skill_id!r} accepts inputModes={effective!r} "
                f"but received part kind={kind!r} mimeType={mime!r}; "
                f"card-level defaultInputModes={card.default_input_modes!r} "
                f"is a discovery hint, not an admission predicate"
            )


# --- the observed failing case, now bounded at admission ---
card = AgentCard(
    default_input_modes=["application/json", "text/plain", "application/pdf"],
    default_output_modes=["application/json"],
    skills=[
        Skill(id="extract-tables", input_modes=["application/pdf"]),
        Skill(id="validate-schema", input_modes=["application/json"]),
        Skill(id="summarize-text", input_modes=["text/plain"]),
    ],
)

bad_parts = [{"kind": "data", "data": {"table": "widgets"}}]
try:
    admit_task(card, "extract-tables", bad_parts)
except SkillModeAdmissionError as exc:
    # Caller now sees a synchronous, structured rejection and can re-serialize
    # to application/pdf instead of receiving a silent {tables: []} completion.
    print(f"rejected at admission: {exc}")
