# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-a-non-convergent-agent-card-negotiation-across-a2a-v1-0-vendors-and-why-task-state-silently-forks.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-a-non-convergent-agent-card-negotiation-across-a2a-v1-0-vendors-and-why-task-state-silently-forks.warrant.json
# Markdown SHA-256:   48b9cf36b53071cbe3f654e31453389f522eba226e9d3a040cf5d506da8333ba
# Code AST SHA-256:   3602adcecfad86f018c6891e6ced65ad53f0e376e6d538b3f5c3e36f990b7c45
# Verified Classes:   NegotiationForkError, AgentCard
# Defensive Guards:   3 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import Set


class NegotiationForkError(RuntimeError):
    """Raised when a remote Agent Card is structurally non-negotiable with the
    client's required mode surface and the remote is non-streaming, so no
    recovery point exists for a terminal artifact mode mismatch."""
    pass


@dataclass
class AgentCard:
    default_input_modes: Set[str] = field(default_factory=set)
    default_output_modes: Set[str] = field(default_factory=set)
    streaming: bool = False
    push_notifications: bool = False


def assert_negotiable(client: AgentCard, remote: AgentCard,
                      client_required_output_modes: Set[str]) -> None:
    """Pre-`tasks/send` gate. Raises NegotiationForkError when the remote
    cannot emit a mode the client requires AND has no streaming recovery point."""
    missing = client_required_output_modes - remote.default_output_modes
    if missing and not remote.streaming:
        raise NegotiationForkError(
            f"remote card declares defaultOutputModes={sorted(remote.default_output_modes)} "
            f"but client requires {sorted(client_required_output_modes)}; "
            f"missing={sorted(missing)} and capabilities.streaming=false, so a terminal "
            f"artifact mode mismatch has no recovery point — refusing tasks/send"
        )
    # input direction: client must be able to emit a mode the remote accepts
    if not (client.default_output_modes & remote.default_input_modes):
        raise NegotiationForkError(
            f"no input-mode intersection between client outputs "
            f"{sorted(client.default_output_modes)} and remote inputs "
            f"{sorted(remote.default_input_modes)}"
        )


def assert_terminal_artifact_mode(artifact_parts: list, required_modes: Set[str]) -> None:
    """Post-`tasks/get` gate for non-streaming remotes: the terminal artifact's
    parts[].type must be in the required set, else the task is forked, not parsed."""
    emitted = {p.get("type") for p in artifact_parts}
    if not (emitted & required_modes):
        raise NegotiationForkError(
            f"terminal artifact emitted modes {sorted(emitted)} but client required "
            f"{sorted(required_modes)}; remote state may be 'completed' while client "
            f"must record a negotiation fork, not a parse failure"
        )
