# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-refuse-a-delegated-a2a-task-because-the-mcp-capability-manifest-it-was-bridged-through-under-reports-its-own-side-effects.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-refuse-a-delegated-a2a-task-because-the-mcp-capability-manifest-it-was-bridged-through-under-reports-its-own-side-effects.warrant.json
# Markdown SHA-256:   42a63040d81f03b9b666db045aa1398df61483b8b57dc8c2097718fdf18fe08e
# Code AST SHA-256:   63d126698b02d160c1bb19b11022846ebf089abd815718bbc7bba80482587586
# Verified Classes:   EffectClass, ManifestFidelityError, CapabilityCard
# Defensive Guards:   1 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from enum import Enum


class EffectClass(Enum):
    READ_ONLY = "read_only"
    MUTATING = "mutating"
    UNKNOWN = "unknown"


class ManifestFidelityError(RuntimeError):
    """Raised when a delegated task's effect class cannot be proven from an
    authoritative source and the plan depends on its purity."""


@dataclass(frozen=True)
class CapabilityCard:
    tool_name: str
    effect_class: EffectClass
    source: str  # "live_mcp_descriptor" | "bridged_cache" | "operator_override"
    explicit_read_only_hint: bool


def translate_mcp_descriptor(descriptor: dict) -> CapabilityCard:
    # Correct conservative default: absent hint => UNKNOWN, never READ_ONLY.
    if "readOnlyHint" not in descriptor and "destructiveHint" not in descriptor:
        effect = EffectClass.UNKNOWN
        explicit = False
    elif descriptor.get("readOnlyHint") is True and descriptor.get("destructiveHint") is not True:
        effect = EffectClass.READ_ONLY
        explicit = True
    else:
        effect = EffectClass.MUTATING
        explicit = False
    return CapabilityCard(
        tool_name=descriptor["name"],
        effect_class=effect,
        source="bridged_cache",
        explicit_read_only_hint=explicit,
    )


def require_purity_proof(card: CapabilityCard, plan_requires_commutativity: bool) -> None:
    if not plan_requires_commutativity:
        return  # under-report is inert for serialized plans
    if card.effect_class is EffectClass.READ_ONLY and card.explicit_read_only_hint:
        return  # positive proof from an authoritative descriptor
    if card.source == "operator_override":
        return  # signed human/operator assertion
    raise ManifestFidelityError(
        f"cannot dispatch {card.tool_name!r} concurrently: effect class is "
        f"{card.effect_class.value} sourced from {card.source!r} with no explicit "
        f"read-only hint; refusing rather than inferring purity from a bridged default"
    )


def dispatch_fanout(cards: list[CapabilityCard]) -> list[str]:
    dispatched = []
    for card in cards:
        require_purity_proof(card, plan_requires_commutativity=True)
        dispatched.append(card.tool_name)
    return dispatched
