# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-an-a2a-v1-0-agent-card-declares-a-task-lifecycle-state-machine-that-cannot-be-entered-from-the-card-s-own-defaultinpu.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-an-a2a-v1-0-agent-card-declares-a-task-lifecycle-state-machine-that-cannot-be-entered-from-the-card-s-own-defaultinpu.warrant.json
# Markdown SHA-256:   aef8a4b2e1dd1ee1a59b2b9442630ac318252963e8b83234c36651f69d01596d
# Code AST SHA-256:   8f733bae94526a44d24c67d5a500d1ff29c63987b8e97e318bf6f7a1d6c50678
# Verified Classes:   A2ADispatchBoundaryError, Skill, AgentCard
# Defensive Guards:   3 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import Optional


class A2ADispatchBoundaryError(RuntimeError):
    """Raised when a task dispatch would cross the default-modality boundary
    into a skill whose inputModes are not a subset of defaultInputModes."""
    pass


@dataclass
class Skill:
    id: str
    input_modes: frozenset[str]


@dataclass
class AgentCard:
    default_input_modes: frozenset[str]
    skills: list[Skill] = field(default_factory=list)


def unsafe_skills(card: AgentCard) -> list[Skill]:
    """Skills whose required input modalities are not covered by the card's
    default set. Dispatch against these using default modalities strands
    the task in 'submitted' on post-creation-validating servers."""
    return [
        s for s in card.skills
        if not s.input_modes <= card.default_input_modes
    ]


def resolve_dispatch_modality(
    card: AgentCard,
    skill_id: str,
    client_modality: Optional[str] = None,
) -> frozenset[str]:
    """Return the modality set the client must construct its payload from.

    If the client supplied an explicit modality, verify it is legal for the
    resolved skill. Otherwise, refuse to fall back to default_input_modes when
    the skill is outside the default set — that fallback is the zombie trigger.
    """
    skill = next((s for s in card.skills if s.id == skill_id), None)
    if skill is None:
        raise A2ADispatchBoundaryError(
            f"skill {skill_id!r} not present in card; cannot resolve modality"
        )

    if client_modality is not None:
        if client_modality not in skill.input_modes:
            raise A2ADispatchBoundaryError(
                f"client modality {client_modality!r} is not legal for skill "
                f"{skill_id!r}; legal set is {sorted(skill.input_modes)}"
            )
        return frozenset({client_modality})

    if not skill.input_modes <= card.default_input_modes:
        raise A2ADispatchBoundaryError(
            f"skill {skill_id!r} inputModes {sorted(skill.input_modes)} are not "
            f"a subset of defaultInputModes {sorted(card.default_input_modes)}; "
            f"default-derived payload would strand the task in 'submitted' on "
            f"post-creation-validating servers. Client must supply an explicit "
            f"skill-level modality."
        )
    return card.default_input_modes


def preflight(card: AgentCard) -> dict[str, bool]:
    """Discovery-time structural check. default_dispatchable == True means a
    default-modality payload is handler-legal for every skill on the card."""
    bad = unsafe_skills(card)
    return {
        "default_dispatchable": len(bad) == 0,
        "unsafe_skill_ids": [s.id for s in bad],
    }
