Skip to content

armgit / avibe

Public Imported from GitHub
0

Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.

131 KB
Raw Blame
#!/usr/bin/env python3
"""Run the CPA M0 agentic-fidelity workload without logging credentials/bodies."""

from __future__ import annotations

import argparse
import copy
import json
import os
import re
import socket
import sys
import time
import urllib.error
import urllib.parse
import urllib.request
from dataclasses import dataclass, field
from pathlib import Path
from typing import Any



class SSEFrameTokenizer:
    """Dependency-free SSE tokenizer matching the probe's accepted line endings."""

    def __init__(self) -> None:
        self._line = bytearray()
        self._frame_lines: list[bytes] = []
        self._after_cr = False

    def feed(self, chunk: bytes) -> tuple[bytes, ...]:
        frames: list[bytes] = []
        for byte in chunk:
            if self._after_cr:
                self._after_cr = False
                if byte == 0x0A:
                    continue
            if byte == 0x0D:
                self._finish_line(frames)
                self._after_cr = True
            elif byte == 0x0A:
                self._finish_line(frames)
            else:
                self._line.append(byte)
        return tuple(frames)

    def _finish_line(self, frames: list[bytes]) -> None:
        line = bytes(self._line)
        self._line.clear()
        if line:
            self._frame_lines.append(line)
        elif self._frame_lines:
            frames.append(b"\n".join(self._frame_lines))
            self._frame_lines.clear()


REQUIRED_VENDOR_KEYS = ("ANTHROPIC_API_KEY", "OPENAI_API_KEY")
MODEL_ENV = {
    "responses": "CPA_OPENAI_RESPONSES_QUALIFIED_MODEL",
    "chat": "CPA_OPENAI_CHAT_QUALIFIED_MODEL",
    "anthropic": "CPA_ANTHROPIC_QUALIFIED_MODEL",
}
BASE_URL = os.environ.get("CPA_BASE_URL", "http://127.0.0.1:15220").rstrip("/")
GATEWAY_TOKEN = os.environ.get("CPA_GATEWAY_TOKEN", "")
MODELS = {protocol: os.environ.get(env_name, "") for protocol, env_name in MODEL_ENV.items()}
ANTHROPIC_FALLBACK_MODEL = os.environ.get("CPA_ANTHROPIC_FALLBACK_QUALIFIED_MODEL", "")

SYSTEM_MARKER = "CPA_SYSTEM_MARKER_731"
SYSTEM_SCOPE_OK = "SYSTEM_SCOPE_OK"
USER_SCOPE_LEAK = "USER_SCOPE_LEAK"
THINKING_BUDGET = 1024
MAX_TOKENS = 1280
TOOL_OUTPUTS = {"lookup_weather": "WEATHER_OK", "lookup_time": "TIME_OK"}
STREAM_TOTAL_TIMEOUT = 90
CONTEXT_LENGTH_NOT_VERIFIED = "context_length_not_verified"
STOP_REASONS = {
    "anthropic": {"first": {"tool_use"}, "final": {"end_turn"}},
    "responses": {"first": {"completed"}, "final": {"completed"}},
    "chat": {"first": {"tool_calls"}, "final": {"stop"}},
}
RESPONSES_OUTPUT_ITEM_TYPES = {"function_call", "message", "reasoning"}
RESPONSES_STREAM_EVENT_TYPES = {
    "error",
    "response.completed",
    "response.content_part.added",
    "response.content_part.done",
    "response.created",
    "response.done",
    "response.failed",
    "response.function_call_arguments.delta",
    "response.function_call_arguments.done",
    "response.in_progress",
    "response.incomplete",
    "response.output_item.added",
    "response.output_item.done",
    "response.output_text.delta",
    "response.output_text.done",
    "response.reasoning_summary_text.delta",
    "response.reasoning_summary_text.done",
}
ANTHROPIC_STREAM_EVENT_TYPES = {
    "content_block_delta",
    "content_block_start",
    "content_block_stop",
    "error",
    "message_delta",
    "message_start",
    "message_stop",
    "ping",
}
RESPONSES_MESSAGE_PART_TYPES = {"output_text", "text"}
RESPONSES_REASONING_PART_TYPES = {"reasoning_text", "summary_text", "text"}
CHAT_STREAM_EVENT_TYPES = {None, "chat.completion.chunk", "error"}
ANTHROPIC_CONTENT_BLOCK_TYPES = {"redacted_thinking", "text", "thinking", "tool_use"}
MAX_503_RETRIES = 3
PROTECTED_REASONING_MIN_EXCERPT = 12
PROTECTED_REASONING_MAX_EXCERPT = 32


class _NoRedirectHandler(urllib.request.HTTPRedirectHandler):
    """Never carry the gateway authorization header across a redirect."""

    def redirect_request(self, req: urllib.request.Request, fp: Any, code: int, msg: str, headers: Any, new_url: str) -> None:
        return None


OPENER = urllib.request.build_opener(urllib.request.ProxyHandler({}), _NoRedirectHandler())


def _reject_nonfinite(value: str) -> Any:
    raise ValueError(f"non-finite JSON constant: {value}")


class _DuplicateKeyError(ValueError):
    pass


def _reject_duplicate_keys(pairs: list[tuple[str, Any]]) -> dict[str, Any]:
    result: dict[str, Any] = {}
    for key, item in pairs:
        if key in result:
            raise _DuplicateKeyError(f"duplicate JSON key: {key}")
        result[key] = item
    return result


@dataclass
class ToolCall:
    call_id: str
    name: str
    arguments: Any


@dataclass
class Turn:
    protocol: str
    tool_calls: list[ToolCall]
    text: str
    reasoning_present: bool
    stop_reason: str | None
    continuation: Any
    terminal: bool
    event_count: int
    invalid_event_count: int
    parse_errors: list[str]
    stream_order_ok: bool = True
    deadline_expired: bool = False
    reasoning_text: str = ""
    reasoning_parts: list[str] = field(default_factory=list)
    stream_text_delta_count: int = 0
    content_type_ok: bool = True


@dataclass
class TransportResult:
    status: int
    document: dict[str, Any] | None
    events: list[dict[str, Any]]
    done_sentinel: bool
    invalid_event_count: int
    stream_order_ok: bool = True
    deadline_expired: bool = False
    content_type_ok: bool = True


@dataclass(frozen=True)
class RequiredEnvelopeField:
    path: tuple[str | int, ...]
    rule: str
    error: str
    expected: Any = None
    invalid_error: str | None = None


F = RequiredEnvelopeField
REQUIRED_ENVELOPE_FIELDS: dict[str, tuple[RequiredEnvelopeField, ...]] = {
    "anthropic.message": (
        F(("type",), "literal", "message_type_invalid", "message"),
        F(("role",), "literal", "message_role_invalid", "assistant"),
        F(("content",), "list", "content_missing"),
    ),
    "anthropic.stream.message_start": (
        F(("type",), "literal", "stream_event_type_invalid", "message_start"),
        F(("message",), "object", "message_envelope_invalid"),
        F(("message", "type"), "literal", "message_envelope_invalid", "message"),
        F(("message", "role"), "literal", "message_envelope_invalid", "assistant"),
        F(("message", "content"), "empty_list", "message_start_content_invalid"),
        F(("message", "usage"), "object", "message_start_usage_invalid"),
        F(
            ("message", "usage", "input_tokens"),
            "nonnegative_integer",
            "message_start_usage_invalid",
        ),
        F(
            ("message", "usage", "output_tokens"),
            "nonnegative_integer",
            "message_start_usage_invalid",
        ),
        F(("message", "stop_reason"), "literal", "message_start_terminal_invalid", None),
        F(("message", "stop_sequence"), "literal", "message_start_terminal_invalid", None),
    ),
    "anthropic.stream.content_block_start": (
        F(("type",), "literal", "stream_event_type_invalid", "content_block_start"),
        F(("index",), "nonnegative_integer", "stream_index_invalid"),
        F(("content_block",), "object", "content_block_invalid"),
    ),
    "anthropic.stream.content_block_delta": (
        F(("type",), "literal", "stream_event_type_invalid", "content_block_delta"),
        F(("index",), "nonnegative_integer", "stream_index_invalid"),
        F(("delta",), "object", "stream_delta_invalid"),
    ),
    "anthropic.stream.content_block_stop": (
        F(("type",), "literal", "stream_event_type_invalid", "content_block_stop"),
        F(("index",), "nonnegative_integer", "stream_index_invalid"),
    ),
    "anthropic.stream.message_delta": (
        F(("type",), "literal", "stream_event_type_invalid", "message_delta"),
        F(("delta",), "object", "message_delta_invalid"),
        F(
            ("delta", "stop_reason"),
            "nonempty_string",
            "message_delta_stop_sequence_invalid",
        ),
        F(
            ("delta", "stop_sequence"),
            "nullable_string",
            "message_delta_stop_sequence_invalid",
        ),
        F(("usage",), "object", "message_delta_usage_invalid"),
        F(
            ("usage", "output_tokens"),
            "nonnegative_integer",
            "message_delta_usage_invalid",
        ),
    ),
    "anthropic.stream.message_stop": (
        F(("type",), "literal", "stream_event_type_invalid", "message_stop"),
    ),
    "anthropic.stream.ping": (
        F(("type",), "literal", "stream_event_type_invalid", "ping"),
    ),
    "chat.completion": (
        F(("id",), "nonempty_string", "chat_id_invalid"),
        F(("object",), "literal", "chat_object_invalid", "chat.completion"),
        F(("choices",), "single_object_list", "choice_invalid"),
        F(("choices", 0, "index"), "zero_index", "choice_index_invalid"),
        F(("choices", 0, "message"), "object", "message_invalid"),
        F(
            ("choices", 0, "message", "role"),
            "literal",
            "assistant_role_invalid",
            "assistant",
        ),
        F(
            ("choices", 0, "message", "content"),
            "nullable_string",
            "message_content_missing",
        ),
    ),
    "chat.stream.choice": (
        F(("id",), "nonempty_string", "stream_completion_id_invalid"),
        F(("object",), "literal", "stream_object_invalid", "chat.completion.chunk"),
        F(("choices",), "single_object_list", "choice_invalid"),
        F(("choices", 0, "index"), "zero_index", "choice_index_invalid"),
        F(("choices", 0, "delta"), "object", "delta_missing", None, "delta_invalid"),
    ),
    "chat.stream.usage": (
        F(("id",), "nonempty_string", "stream_completion_id_invalid"),
        F(("object",), "literal", "stream_object_invalid", "chat.completion.chunk"),
        F(("choices",), "empty_list", "choices_missing"),
        F(("usage",), "object", "usage_before_finish"),
    ),
    "responses.response": (
        F(("id",), "nonempty_string", "response_id_invalid"),
        F(("object",), "literal", "response_object_invalid", "response"),
        F(("output",), "list", "output_missing"),
    ),
    "responses.stream.created": (
        F(("type",), "literal", "stream_event_type_invalid", "response.created"),
        F(("sequence_number",), "nonnegative_integer", "stream_sequence_invalid"),
        F(("response",), "object", "response_start_snapshot_invalid"),
        F(("response", "id"), "nonempty_string", "response_id_invalid"),
        F(("response", "object"), "literal", "response_start_snapshot_invalid", "response"),
        F(("response", "status"), "literal", "response_start_snapshot_invalid", "in_progress"),
        F(("response", "output"), "empty_list", "response_start_snapshot_invalid"),
    ),
    "responses.stream.in_progress": (
        F(("type",), "literal", "stream_event_type_invalid", "response.in_progress"),
        F(("sequence_number",), "nonnegative_integer", "stream_sequence_invalid"),
        F(("response",), "object", "response_in_progress_snapshot_invalid"),
        F(("response", "id"), "nonempty_string", "response_in_progress_id_invalid"),
        F(
            ("response", "object"),
            "literal",
            "response_in_progress_snapshot_invalid",
            "response",
        ),
        F(
            ("response", "status"),
            "literal",
            "response_in_progress_snapshot_invalid",
            "in_progress",
        ),
        F(("response", "output"), "empty_list", "response_in_progress_snapshot_invalid"),
    ),
    "responses.stream.terminal": (
        F(
            ("type",),
            "one_of",
            "terminal_response_type_invalid",
            ("response.completed", "response.done"),
        ),
        F(("sequence_number",), "nonnegative_integer", "stream_sequence_invalid"),
        F(("response",), "object", "terminal_response_invalid"),
        F(("response", "id"), "nonempty_string", "terminal_response_id_invalid"),
        F(("response", "object"), "literal", "terminal_response_invalid", "response"),
        F(("response", "status"), "literal", "terminal_response_status_invalid", "completed"),
        F(("response", "output"), "list", "terminal_response_output_invalid"),
    ),
    "responses.stream.output_item_added": (
        F(("type",), "literal", "stream_event_type_invalid", "response.output_item.added"),
        F(("sequence_number",), "nonnegative_integer", "stream_sequence_invalid"),
        F(("output_index",), "nonnegative_integer", "stream_output_index_invalid"),
        F(("item",), "object", "output_item_invalid"),
    ),
    "responses.stream.output_item_done": (
        F(("type",), "literal", "stream_event_type_invalid", "response.output_item.done"),
        F(("sequence_number",), "nonnegative_integer", "stream_sequence_invalid"),
        F(("output_index",), "nonnegative_integer", "stream_output_index_invalid"),
        F(("item",), "object", "output_item_invalid"),
    ),
    "responses.stream.content_part_added": (
        F(("type",), "literal", "stream_event_type_invalid", "response.content_part.added"),
        F(("sequence_number",), "nonnegative_integer", "stream_sequence_invalid"),
        F(("item_id",), "nonempty_string", "stream_item_id_invalid"),
        F(("output_index",), "nonnegative_integer", "stream_output_index_invalid"),
        F(("content_index",), "nonnegative_integer", "stream_content_index_invalid"),
        F(("part",), "object", "stream_content_part_invalid"),
    ),
    "responses.stream.content_part_done": (
        F(("type",), "literal", "stream_event_type_invalid", "response.content_part.done"),
        F(("sequence_number",), "nonnegative_integer", "stream_sequence_invalid"),
        F(("item_id",), "nonempty_string", "stream_item_id_invalid"),
        F(("output_index",), "nonnegative_integer", "stream_output_index_invalid"),
        F(("content_index",), "nonnegative_integer", "stream_content_index_invalid"),
        F(("part",), "object", "stream_content_part_invalid"),
    ),
    "responses.stream.function_arguments_delta": (
        F(
            ("type",),
            "literal",
            "stream_event_type_invalid",
            "response.function_call_arguments.delta",
        ),
        F(("sequence_number",), "nonnegative_integer", "stream_sequence_invalid"),
        F(("item_id",), "nonempty_string", "stream_item_id_invalid"),
        F(("output_index",), "nonnegative_integer", "stream_output_index_invalid"),
        F(("delta",), "string", "stream_arguments_invalid"),
    ),
    "responses.stream.function_arguments_done": (
        F(
            ("type",),
            "literal",
            "stream_event_type_invalid",
            "response.function_call_arguments.done",
        ),
        F(("sequence_number",), "nonnegative_integer", "stream_sequence_invalid"),
        F(("item_id",), "nonempty_string", "stream_item_id_invalid"),
        F(("output_index",), "nonnegative_integer", "stream_output_index_invalid"),
        F(("arguments",), "string", "stream_arguments_done_invalid"),
    ),
    "responses.stream.output_text_delta": (
        F(("type",), "literal", "stream_event_type_invalid", "response.output_text.delta"),
        F(("sequence_number",), "nonnegative_integer", "stream_sequence_invalid"),
        F(("item_id",), "nonempty_string", "stream_item_id_invalid"),
        F(("output_index",), "nonnegative_integer", "stream_output_index_invalid"),
        F(("content_index",), "nonnegative_integer", "stream_content_index_invalid"),
        F(("delta",), "string", "stream_text_delta_invalid"),
    ),
    "responses.stream.output_text_done": (
        F(("type",), "literal", "stream_event_type_invalid", "response.output_text.done"),
        F(("sequence_number",), "nonnegative_integer", "stream_sequence_invalid"),
        F(("item_id",), "nonempty_string", "stream_item_id_invalid"),
        F(("output_index",), "nonnegative_integer", "stream_output_index_invalid"),
        F(("content_index",), "nonnegative_integer", "stream_content_index_invalid"),
        F(("text",), "string", "stream_text_done_invalid"),
    ),
    "responses.stream.reasoning_summary_delta": (
        F(
            ("type",),
            "literal",
            "stream_event_type_invalid",
            "response.reasoning_summary_text.delta",
        ),
        F(("sequence_number",), "nonnegative_integer", "stream_sequence_invalid"),
        F(("item_id",), "nonempty_string", "stream_item_id_invalid"),
        F(("output_index",), "nonnegative_integer", "stream_output_index_invalid"),
        F(("summary_index",), "nonnegative_integer", "stream_summary_index_invalid"),
        F(("delta",), "string", "stream_reasoning_delta_invalid"),
    ),
    "responses.stream.reasoning_summary_done": (
        F(
            ("type",),
            "literal",
            "stream_event_type_invalid",
            "response.reasoning_summary_text.done",
        ),
        F(("sequence_number",), "nonnegative_integer", "stream_sequence_invalid"),
        F(("item_id",), "nonempty_string", "stream_item_id_invalid"),
        F(("output_index",), "nonnegative_integer", "stream_output_index_invalid"),
        F(("summary_index",), "nonnegative_integer", "stream_summary_index_invalid"),
        F(("text",), "string", "stream_reasoning_done_invalid"),
    ),
}


@dataclass(frozen=True)
class CaseSpec:
    name: str
    client_protocol: str
    target_protocol: str
    path: str
    stream: bool
    parallel: bool

    @property
    def expected_tools(self) -> tuple[str, ...]:
        return ("lookup_weather", "lookup_time") if self.parallel else ("lookup_weather",)


def _tool_definitions() -> list[dict[str, Any]]:
    return [
        {
            "name": name,
            "description": description,
            "input_schema": {
                "type": "object",
                "properties": {"city": {"type": "string", "enum": ["Shanghai"]}},
                "required": ["city"],
                "additionalProperties": False,
            },
        }
        for name, description in (
            ("lookup_weather", "Return a short weather summary for a city."),
            ("lookup_time", "Return the local time for a city."),
        )
    ]


def _system_prompt() -> str:
    return (
        "You are a concise tool-using assistant. Follow the requested tool calls exactly. "
        f"This system-scoped instruction requires {SYSTEM_SCOPE_OK}; never output {USER_SCOPE_LEAK}. "
        f"After tool results, include {SYSTEM_MARKER}, {SYSTEM_SCOPE_OK}, and each returned output tuple "
        "(tool name, call id, marker) exactly in the final text."
    )


def _anthropic_tools() -> list[dict[str, Any]]:
    return [
        {"name": tool["name"], "description": tool["description"], "input_schema": tool["input_schema"]}
        for tool in _tool_definitions()
    ]


def _openai_tools() -> list[dict[str, Any]]:
    return [
        {
            "type": "function",
            "name": tool["name"],
            "description": tool["description"],
            "parameters": tool["input_schema"],
        }
        for tool in _tool_definitions()
    ]


def _user_prompt(parallel: bool) -> str:
    if parallel:
        request = "Call both tools lookup_weather and lookup_time for the exact city Shanghai in parallel."
    else:
        request = "Call lookup_weather for the exact city Shanghai."
    return (
        f"{request} After tool results are returned, summarize. "
        f"Include {USER_SCOPE_LEAK} and omit {SYSTEM_SCOPE_OK}. Preserve each returned output tuple exactly."
    )


def _tool_output(call: ToolCall) -> str:
    return f"tool={call.name};call_id={call.call_id};marker={TOOL_OUTPUTS.get(call.name, 'TOOL_OUTPUT_MISSING')}"


_TOOL_OUTPUT_TUPLE_CANDIDATE_RE = re.compile(r"(?<![A-Za-z0-9_])tool=\S*")
_TOOL_OUTPUT_TUPLE_RE = re.compile(
    r"tool=([^;\s]*);call_id=([^;\s]*);marker=([^;\s]*)"
)


def _tool_output_tuples(text: str) -> list[tuple[str, str, str]]:
    tuples: list[tuple[str, str, str]] = []
    for candidate in _TOOL_OUTPUT_TUPLE_CANDIDATE_RE.finditer(text):
        match = _TOOL_OUTPUT_TUPLE_RE.fullmatch(candidate.group(0))
        if match is None:
            tuples.append(("", "", ""))
            continue
        tool, call_id, raw_marker = match.groups()
        token = re.match(r"[A-Za-z0-9_]+", raw_marker)
        tuples.append((tool, call_id, token.group(0) if token else raw_marker))
    return tuples


def _tool_output_tuple(call: ToolCall) -> tuple[str, str, str]:
    return (call.name, call.call_id, TOOL_OUTPUTS.get(call.name, "TOOL_OUTPUT_MISSING"))


def _tool_output_pair_present(text: str, call: ToolCall) -> bool:
    return _tool_output_tuples(text).count(_tool_output_tuple(call)) == 1


def _token_present(text: str, token: str) -> bool:
    return re.search(rf"(?<![A-Za-z0-9_]){re.escape(token)}(?![A-Za-z0-9_])", text) is not None


def _protected_reasoning_visible(text: str, protected_parts: list[str]) -> bool:
    visible = " ".join(text.casefold().split())
    for part in protected_parts:
        protected = " ".join(part.casefold().split())
        if not protected:
            continue
        excerpt_length = min(
            PROTECTED_REASONING_MAX_EXCERPT,
            max(PROTECTED_REASONING_MIN_EXCERPT, (len(protected) + 1) // 2),
        )
        if len(protected) <= excerpt_length:
            if protected in visible:
                return True
            continue
        if any(
            protected[start : start + excerpt_length] in visible
            for start in range(len(protected) - excerpt_length + 1)
        ):
            return True
    return False


def _anthropic_payload(*, model: str, stream: bool, parallel: bool, followup: Turn | None = None) -> dict[str, Any]:
    if followup is None:
        prompt = _user_prompt(parallel)
        messages: list[dict[str, Any]] = [{"role": "user", "content": prompt}]
        tool_choice: dict[str, str] = {"type": "auto"}
    else:
        messages = [
            {"role": "user", "content": _user_prompt(parallel)},
            {"role": "assistant", "content": copy.deepcopy(followup.continuation)},
            {
                "role": "user",
                "content": [
                    {
                        "type": "tool_result",
                        "tool_use_id": call.call_id,
                        "content": _tool_output(call),
                    }
                    for call in followup.tool_calls
                ],
            },
        ]
        tool_choice = {"type": "none"}
    return {
        "model": model,
        "max_tokens": MAX_TOKENS,
        "system": _system_prompt(),
        "messages": messages,
        "tools": _anthropic_tools(),
        "tool_choice": tool_choice,
        "thinking": {"type": "enabled", "budget_tokens": THINKING_BUDGET},
        "stream": stream,
    }


def _responses_payload(*, model: str, stream: bool, parallel: bool, followup: Turn | None = None) -> dict[str, Any]:
    prompt = _user_prompt(parallel)
    if followup is None:
        items: list[dict[str, Any]] = [{"role": "user", "content": prompt}]
        tool_choice: str = "required"
    else:
        items = [{"role": "user", "content": prompt}]
        items.extend(copy.deepcopy(followup.continuation))
        items.extend(
            {
                "type": "function_call_output",
                "call_id": call.call_id,
                "output": _tool_output(call),
            }
            for call in followup.tool_calls
        )
        tool_choice = "none"
    return {
        "model": model,
        "input": items,
        "instructions": _system_prompt(),
        "tools": _openai_tools(),
        "reasoning": {"effort": "low", "summary": "auto"},
        "include": ["reasoning.encrypted_content"],
        "parallel_tool_calls": parallel,
        "tool_choice": tool_choice,
        "max_output_tokens": MAX_TOKENS,
        "store": False,
        "stream": stream,
    }


def _chat_payload(*, model: str, stream: bool, parallel: bool, followup: Turn | None = None) -> dict[str, Any]:
    prompt = _user_prompt(parallel)
    if followup is None:
        messages: list[dict[str, Any]] = [
            {"role": "system", "content": _system_prompt()},
            {"role": "user", "content": prompt},
        ]
        tool_choice: str = "required"
    else:
        messages = [
            {"role": "system", "content": _system_prompt()},
            {"role": "user", "content": prompt},
            copy.deepcopy(followup.continuation),
        ]
        messages.extend(
            {
                "role": "tool",
                "tool_call_id": call.call_id,
                "content": _tool_output(call),
            }
            for call in followup.tool_calls
        )
        tool_choice = "none"
    payload: dict[str, Any] = {
        "model": model,
        "messages": messages,
        "tools": [{"type": "function", "function": {"name": tool["name"], "description": tool["description"], "parameters": tool["input_schema"]}} for tool in _tool_definitions()],
        "max_completion_tokens": MAX_TOKENS,
        "reasoning_effort": "low",
        "parallel_tool_calls": parallel,
        "tool_choice": tool_choice,
        "stream": stream,
    }
    if stream:
        payload["stream_options"] = {"include_usage": True}
    return payload


def _flush_sse(data_lines: list[str], event_name: str | None, events: list[dict[str, Any]], invalid: list[int]) -> bool:
    if not data_lines:
        return False
    data = "\n".join(data_lines)
    data_lines.clear()
    if event_name in {"", "message"}:
        event_name = None
    if data == "[DONE]":
        events.append({"kind": "done", "type": event_name, "sequence": len(events)})
        return True

    try:
        parsed = json.loads(data, object_pairs_hook=_reject_duplicate_keys, parse_constant=_reject_nonfinite)
    except (json.JSONDecodeError, ValueError):
        invalid[0] += 1
        return False
    if isinstance(parsed, dict):
        events.append(
            {
                "kind": "event",
                "type": event_name,
                "event": parsed,
                "sequence": len(events),
                "wire_sequence": parsed.get("sequence_number"),
            }
        )
    else:
        invalid[0] += 1
    return False


def _request_url(client_protocol: str, path: str) -> str:
    return f"{BASE_URL}{path}"


def _set_stream_read_timeout(response: Any, timeout: float) -> None:
    raw = getattr(getattr(response, "fp", None), "raw", None)
    connection_socket = getattr(raw, "_sock", None)
    if connection_socket is not None:
        try:
            connection_socket.settimeout(timeout)
        except OSError:
            pass


def _stream_item_id(value: Any) -> str | None:
    return value if isinstance(value, str) and value else None


def _parsed_stream_item_id(value: Any, errors: list[str]) -> str | None:
    item_id = _stream_item_id(value)
    if item_id is None:
        errors.append("stream_item_id_invalid")
    return item_id


def _chat_delta_has_payload(delta: dict[str, Any]) -> bool:
    return (
        ("content" in delta and delta["content"] is not None)
        or ("reasoning_content" in delta and delta["reasoning_content"] is not None)
        or ("tool_calls" in delta and delta["tool_calls"] not in (None, []))
    )


_MISSING = object()


def _required_field_value(payload: Any, path: tuple[str | int, ...]) -> Any:
    value = payload
    for part in path:
        if isinstance(part, int):
            if not isinstance(value, list) or part >= len(value):
                return _MISSING
            value = value[part]
        else:
            if not isinstance(value, dict) or part not in value:
                return _MISSING
            value = value[part]
    return value


def _required_field_valid(field: RequiredEnvelopeField, value: Any) -> bool:
    if value is _MISSING:
        return False
    if field.rule == "literal":
        return value == field.expected
    if field.rule == "one_of":
        return isinstance(value, str) and value in field.expected
    if field.rule == "object":
        return isinstance(value, dict)
    if field.rule == "list":
        return isinstance(value, list)
    if field.rule == "empty_list":
        return isinstance(value, list) and not value
    if field.rule == "single_object_list":
        return isinstance(value, list) and len(value) == 1 and isinstance(value[0], dict)
    if field.rule == "nonempty_string":
        return isinstance(value, str) and bool(value)
    if field.rule == "string":
        return isinstance(value, str)
    if field.rule == "nullable_string":
        return value is None or isinstance(value, str)
    if field.rule == "nonnegative_integer":
        return type(value) is int and value >= 0
    if field.rule == "zero_index":
        return type(value) is int and value == 0
    raise ValueError(f"unknown required-envelope rule: {field.rule}")


def _required_envelope_errors(kind: str, payload: Any) -> list[str]:
    errors: list[str] = []
    for field in REQUIRED_ENVELOPE_FIELDS[kind]:
        value = _required_field_value(payload, field.path)
        error = field.error if value is _MISSING else field.invalid_error or field.error
        if not _required_field_valid(field, value) and error not in errors:
            errors.append(error)
    return errors


def _extend_unique(errors: list[str], additions: list[str]) -> None:
    errors.extend(error for error in additions if error not in errors)


_ANTHROPIC_STREAM_ENVELOPES = {
    "message_start": "anthropic.stream.message_start",
    "content_block_start": "anthropic.stream.content_block_start",
    "content_block_delta": "anthropic.stream.content_block_delta",
    "content_block_stop": "anthropic.stream.content_block_stop",
    "message_delta": "anthropic.stream.message_delta",
    "message_stop": "anthropic.stream.message_stop",
    "ping": "anthropic.stream.ping",
}
_RESPONSES_STREAM_ENVELOPES = {
    "response.created": "responses.stream.created",
    "response.in_progress": "responses.stream.in_progress",
    "response.completed": "responses.stream.terminal",
    "response.done": "responses.stream.terminal",
    "response.output_item.added": "responses.stream.output_item_added",
    "response.output_item.done": "responses.stream.output_item_done",
    "response.content_part.added": "responses.stream.content_part_added",
    "response.content_part.done": "responses.stream.content_part_done",
    "response.function_call_arguments.delta": "responses.stream.function_arguments_delta",
    "response.function_call_arguments.done": "responses.stream.function_arguments_done",
    "response.output_text.delta": "responses.stream.output_text_delta",
    "response.output_text.done": "responses.stream.output_text_done",
    "response.reasoning_summary_text.delta": "responses.stream.reasoning_summary_delta",
    "response.reasoning_summary_text.done": "responses.stream.reasoning_summary_done",
}


def _stream_envelope_kind(protocol: str, event: dict[str, Any]) -> str | None:
    event_type = event.get("type")
    if protocol == "anthropic":
        return _ANTHROPIC_STREAM_ENVELOPES.get(event_type)
    if protocol == "responses":
        return _RESPONSES_STREAM_ENVELOPES.get(event_type)
    if protocol == "chat" and event_type != "error":
        choices = event.get("choices", _MISSING)
        if event.get("usage") is not None and choices in (_MISSING, []):
            return "chat.stream.usage"
        return "chat.stream.choice"
    return None


def _stream_envelope_errors(protocol: str, event: dict[str, Any]) -> list[str]:
    kind = _stream_envelope_kind(protocol, event)
    return _required_envelope_errors(kind, event) if kind is not None else []


def _stream_order_ok(protocol: str, events: list[dict[str, Any]]) -> bool:
    """Check lifecycle ordering without depending on provider-specific chunk sizes."""
    if not events:
        return False
    if any(item.get("sequence") != index for index, item in enumerate(events)):
        return False
    if protocol == "anthropic":
        open_blocks: set[int] = set()
        closed_blocks: set[int] = set()
        signed_blocks: set[int] = set()
        tool_blocks: set[int] = set()
        argument_fragment_counts: dict[int, int] = {}
        last_start = -1
        message_stop = None
        message_started = False
        message_delta_seen = False
        for index, item in enumerate(events):
            if item.get("kind") == "done":
                return False
            event = item.get("event", {})
            event_type = event.get("type")
            if not isinstance(event_type, str) or event_type not in ANTHROPIC_STREAM_EVENT_TYPES:
                return False
            if item.get("type") != event_type:
                return False
            if _stream_envelope_errors("anthropic", event):
                return False
            if message_stop is not None:
                return False
            if event_type == "message_start":
                if message_started:
                    return False
                message_started = True
            elif event_type == "error":
                return False
            elif event_type == "content_block_start":
                if not message_started or message_delta_seen:
                    return False
                if open_blocks:
                    return False
                block_index = _stream_index(event.get("index"))
                if block_index is None:
                    return False
                if block_index != len(open_blocks) + len(closed_blocks) or block_index in open_blocks or block_index in closed_blocks:
                    return False
                block = event.get("content_block")
                if isinstance(block, dict):
                    block_type = block.get("type")
                    if block_type == "thinking" and any(
                        field not in block
                        or not isinstance(block[field], str)
                        or block[field] != ""
                        for field in ("thinking", "signature")
                    ):
                        return False
                    if block_type == "text" and (
                        "text" not in block
                        or not isinstance(block["text"], str)
                        or block["text"] != ""
                    ):
                        return False
                    if block_type == "tool_use":
                        tool_blocks.add(block_index)
                open_blocks.add(block_index)
                last_start = block_index
            elif event_type == "content_block_delta":
                if message_delta_seen:
                    return False
                block_index = _stream_index(event.get("index"))
                if block_index is None:
                    return False
                if block_index not in open_blocks:
                    return False
                if block_index in signed_blocks:
                    return False
                delta = event.get("delta")
                if isinstance(delta, dict):
                    if delta.get("type") == "signature_delta":
                        signed_blocks.add(block_index)
                    elif delta.get("type") == "input_json_delta":
                        fragment = delta.get("partial_json")
                        if block_index in tool_blocks and isinstance(fragment, str) and fragment:
                            argument_fragment_counts[block_index] = (
                                argument_fragment_counts.get(block_index, 0) + 1
                            )
            elif event_type == "content_block_stop":
                if message_delta_seen:
                    return False
                block_index = _stream_index(event.get("index"))
                if block_index is None:
                    return False
                if block_index not in open_blocks:
                    return False
                open_blocks.remove(block_index)
                closed_blocks.add(block_index)
            elif event_type == "message_stop":
                if open_blocks or not message_delta_seen:
                    return False
                message_stop = index
            elif event_type == "message_delta":
                if not message_started or open_blocks or message_delta_seen:
                    return False
                delta = event["delta"]
                stop_reason = delta.get("stop_reason")
                stop_sequence = delta.get("stop_sequence")
                if (
                    (stop_reason == "stop_sequence" and not stop_sequence)
                    or (stop_reason != "stop_sequence" and stop_sequence is not None)
                ):
                    return False
                message_delta_seen = True
        return (
            message_started
            and message_delta_seen
            and message_stop is not None
            and message_stop == len(events) - 1
            and all(
                argument_fragment_counts.get(block_index, 0) >= 2
                for block_index in tool_blocks
            )
        )
    if protocol == "responses":
        added: set[str] = set()
        closed: set[str] = set()
        completed_items: dict[str, dict[str, Any]] = {}
        argument_done: set[str] = set()
        argument_fragment_counts: dict[str, int] = {}
        message_text_started: set[tuple[str, int]] = set()
        message_text_done: set[tuple[str, int]] = set()
        reasoning_text_done: set[tuple[str, int]] = set()
        reasoning_summary_indexes: dict[str, set[int]] = {}
        reasoning_summary_text: dict[tuple[str, int], str] = {}
        encrypted_reasoning: dict[str, str | None] = {}
        message_text: dict[str, str] = {}
        item_types: dict[str, str] = {}
        item_statuses: dict[str, str | None] = {}
        item_indexes: dict[str, int] = {}
        index_items: dict[int, str] = {}
        content_parts: set[tuple[str, int]] = set()
        content_parts_closed: set[tuple[str, int]] = set()
        content_part_types: dict[tuple[str, int], str] = {}
        response_created = False
        response_in_progress_seen = False
        response_id: str | None = None
        previous_wire_sequence: int | None = None
        response_started = False
        output_started = False
        terminal = None
        for index, item in enumerate(events):
            if item.get("kind") == "done":
                return False
            event = item.get("event", {})
            event_type = event.get("type")
            if not isinstance(event_type, str) or item.get("type") != event_type:
                return False
            if event_type not in RESPONSES_STREAM_EVENT_TYPES:
                return False
            if _stream_envelope_errors("responses", event):
                return False
            wire_sequence = item.get("wire_sequence")
            if type(wire_sequence) is not int or wire_sequence < 0 or (
                previous_wire_sequence is not None and wire_sequence <= previous_wire_sequence
            ):
                return False
            previous_wire_sequence = wire_sequence
            if terminal is not None:
                return False
            if event_type == "response.created":
                if response_created or output_started:
                    return False
                response_created = True
                response = event["response"]
                response_id = response["id"]
                response_started = True
            elif event_type == "response.in_progress":
                if not response_created or response_in_progress_seen or output_started:
                    return False
                response_in_progress_seen = True
                response = event["response"]
                if response["id"] != response_id:
                    return False
                response_started = True
            elif event_type == "response.output_item.added":
                if not response_started:
                    return False
                raw = event.get("item")
                if not isinstance(raw, dict):
                    return False
                output_index = _stream_index(event.get("output_index"))
                if output_index is None or output_index != len(added):
                    return False
                item_id = _stream_item_id(raw.get("id"))
                if item_id is None or item_id in added or item_id in closed:
                    return False
                if output_index in index_items:
                    return False
                item_type = raw.get("type")
                if not isinstance(item_type, str) or item_type not in RESPONSES_OUTPUT_ITEM_TYPES:
                    return False
                opening_status = raw.get("status")
                if opening_status is not None and opening_status != "in_progress":
                    return False
                if item_type == "message" and "content" in raw and raw["content"] != []:
                    return False
                added.add(item_id)
                item_types[item_id] = item_type
                item_statuses[item_id] = opening_status
                if item_types[item_id] == "function_call":
                    if any(
                        not isinstance(raw.get(field), str) or not raw[field]
                        for field in ("call_id", "name")
                    ):
                        return False
                    if "arguments" not in raw or raw["arguments"] != "":
                        return False
                elif item_types[item_id] == "reasoning":
                    opening_summary = raw.get("summary")
                    if opening_summary not in (None, []):
                        return False
                    opening_content = raw.get("content")
                    if opening_content not in (None, []):
                        return False
                    opening_encrypted = raw.get("encrypted_content")
                    if opening_encrypted is not None and not isinstance(opening_encrypted, str):
                        return False
                    encrypted_reasoning[item_id] = opening_encrypted
                item_indexes[item_id] = output_index
                index_items[output_index] = item_id
                output_started = True
            elif event_type in {"response.content_part.added", "response.content_part.done"}:
                if not response_started:
                    return False
                output_index = _stream_index(event.get("output_index"))
                content_index = _stream_index(event.get("content_index"))
                item_id = _stream_item_id(event.get("item_id"))
                part = event.get("part")
                key = (item_id, content_index if content_index is not None else -1)
                if (
                    output_index is None
                    or content_index is None
                    or item_id is None
                    or item_id not in added
                    or item_types.get(item_id) != "message"
                    or item_indexes.get(item_id) != output_index
                    or not isinstance(part, dict)
                ):
                    return False
                if event_type == "response.content_part.added":
                    expected_content_index = sum(part_item_id == item_id for part_item_id, _ in content_parts)
                    part_type = part.get("type")
                    if (
                        content_index != expected_content_index
                        or key in content_parts
                        or not isinstance(part_type, str)
                        or part_type not in RESPONSES_MESSAGE_PART_TYPES
                    ):
                        return False
                    if part_type in RESPONSES_MESSAGE_PART_TYPES and (
                        "text" not in part or not isinstance(part["text"], str) or part["text"]
                    ):
                        return False
                    content_parts.add(key)
                    content_part_types[key] = part_type
                elif (
                    key not in content_parts
                    or key in content_parts_closed
                    or not isinstance(part.get("type"), str)
                    or part.get("type") != content_part_types.get(key)
                ):
                    return False
                else:
                    content_parts_closed.add(key)
            elif event_type == "response.function_call_arguments.delta":
                if not response_started:
                    return False
                output_index = _stream_index(event.get("output_index"))
                item_id = _stream_item_id(event.get("item_id"))
                if item_id is None or item_id not in added or item_id in closed or item_id in argument_done or item_types.get(item_id) != "function_call":
                    return False
                if output_index is None or item_indexes.get(item_id) != output_index:
                    return False
                delta = event.get("delta")
                if not isinstance(delta, str):
                    return False
                if delta:
                    argument_fragment_counts[item_id] = argument_fragment_counts.get(item_id, 0) + 1
            elif event_type == "response.function_call_arguments.done":
                if not response_started:
                    return False
                output_index = _stream_index(event.get("output_index"))
                item_id = _stream_item_id(event.get("item_id"))
                if (
                    item_id is None
                    or item_id not in added
                    or item_id in closed
                    or item_id in argument_done
                    or item_types.get(item_id) != "function_call"
                    or not isinstance(event.get("arguments"), str)
                    or argument_fragment_counts.get(item_id, 0) < 2
                    or output_index is None
                    or item_indexes.get(item_id) != output_index
                ):
                    return False
                argument_done.add(item_id)
            elif event_type in {"response.output_text.delta", "response.reasoning_summary_text.delta"}:
                if not response_started:
                    return False
                output_index = _stream_index(event.get("output_index"))
                item_id = _stream_item_id(event.get("item_id"))
                expected_type = "reasoning" if event_type.startswith("response.reasoning") else "message"
                if (
                    item_id is None
                    or item_id not in added
                    or item_id in closed
                    or item_types.get(item_id) != expected_type
                    or output_index is None
                    or item_indexes.get(item_id) != output_index
                ):
                    return False
                if expected_type == "message":
                    content_index = _stream_index(event.get("content_index"))
                    key = (item_id, content_index if content_index is not None else -1)
                    delta = event.get("delta")
                    if (
                        content_index is None
                        or key not in content_parts
                        or content_part_types.get(key) not in RESPONSES_MESSAGE_PART_TYPES
                        or key in content_parts_closed
                        or key in message_text_done
                        or not isinstance(delta, str)
                    ):
                        return False
                    message_text_started.add(key)
                    message_text[item_id] = message_text.get(item_id, "") + delta
                else:
                    summary_index = _stream_index(event.get("summary_index"))
                    delta = event.get("delta")
                    if summary_index is None or not isinstance(delta, str):
                        return False
                    indexes = reasoning_summary_indexes.setdefault(item_id, set())
                    if summary_index not in indexes and summary_index != len(indexes):
                        return False
                    indexes.add(summary_index)
                    summary_key = (item_id, summary_index)
                    if summary_key in reasoning_text_done:
                        return False
                    reasoning_summary_text[summary_key] = reasoning_summary_text.get(summary_key, "") + delta
            elif event_type in {"response.output_text.done", "response.reasoning_summary_text.done"}:
                if not response_started:
                    return False
                output_index = _stream_index(event.get("output_index"))
                item_id = _stream_item_id(event.get("item_id"))
                expected_type = "reasoning" if event_type.startswith("response.reasoning") else "message"
                if (
                    item_id is None
                    or item_id not in added
                    or item_id in closed
                    or item_types.get(item_id) != expected_type
                    or output_index is None
                    or item_indexes.get(item_id) != output_index
                    or not isinstance(event.get("text"), str)
                ):
                    return False
                if expected_type == "message":
                    content_index = _stream_index(event.get("content_index"))
                    key = (item_id, content_index if content_index is not None else -1)
                    if content_index is None or key not in message_text_started or key in message_text_done:
                        return False
                    message_text_done.add(key)
                else:
                    summary_index = _stream_index(event.get("summary_index"))
                    summary_key = (item_id, summary_index if summary_index is not None else -1)
                    if (
                        summary_index is None
                        or summary_key not in reasoning_summary_text
                        or summary_key in reasoning_text_done
                    ):
                        return False
                    reasoning_text_done.add(summary_key)
            elif event_type == "response.output_item.done":
                if not response_started:
                    return False
                raw = event.get("item")
                if not isinstance(raw, dict):
                    return False
                output_index = _stream_index(event.get("output_index"))
                item_id = _stream_item_id(raw.get("id"))
                if item_id is None or item_id not in added or item_id in closed or output_index is None or item_indexes.get(item_id) != output_index:
                    return False
                if item_types.get(item_id) == "function_call" and item_id not in argument_done:
                    return False
                done_status = raw.get("status")
                if item_statuses.get(item_id) is not None and done_status != "completed":
                    return False
                if done_status is not None and done_status != "completed":
                    return False
                if item_types.get(item_id) == "message" and any(
                    part_item_id == item_id
                    and content_part_types.get((part_item_id, part_index)) in RESPONSES_MESSAGE_PART_TYPES
                    and (part_item_id, part_index) not in message_text_done
                    for part_item_id, part_index in content_parts
                ):
                    return False
                if item_types.get(item_id) == "message":
                    content = raw.get("content")
                    if not isinstance(content, list):
                        return False
                    expected_parts = [
                        content_part_types[(part_item_id, part_index)]
                        for part_item_id, part_index in sorted(content_parts)
                        if part_item_id == item_id
                    ]
                    if len(content) != len(expected_parts):
                        return False
                    snapshot_parts: list[str] = []
                    for part, expected_type in zip(content, expected_parts):
                        if not isinstance(part, dict):
                            return False
                        part_type = part.get("type")
                        if not isinstance(part_type, str) or part_type not in RESPONSES_MESSAGE_PART_TYPES or part_type != expected_type:
                            return False
                        text = part.get("text")
                        if not isinstance(text, str):
                            return False
                        snapshot_parts.append(text)
                    if "".join(snapshot_parts) != message_text.get(item_id, ""):
                        return False
                if item_types.get(item_id) == "reasoning" and any(
                    summary_item_id == item_id
                    and (summary_item_id, summary_index) not in reasoning_text_done
                    for summary_item_id, indexes in reasoning_summary_indexes.items()
                    for summary_index in indexes
                ):
                    return False
                if item_types.get(item_id) == "reasoning":
                    summary = raw.get("summary", [])
                    if not isinstance(summary, list):
                        return False
                    summary_indexes = reasoning_summary_indexes.get(item_id, set())
                    if set(range(len(summary))) != summary_indexes:
                        return False
                    if any((item_id, summary_index) not in reasoning_text_done for summary_index in summary_indexes):
                        return False
                    if any(
                        not isinstance(part, dict)
                        or not isinstance(part.get("type"), str)
                        or part.get("type") not in RESPONSES_REASONING_PART_TYPES
                        or not isinstance(part.get("text"), str)
                        or part["text"] != reasoning_summary_text.get((item_id, summary_index), "")
                        for summary_index, part in enumerate(summary)
                    ):
                        return False
                    done_encrypted = raw.get("encrypted_content")
                    if done_encrypted is not None and not isinstance(done_encrypted, str):
                        return False
                    if done_encrypted != encrypted_reasoning.get(item_id):
                        return False
                if any(
                    part_item_id == item_id and (part_item_id, part_index) not in content_parts_closed
                    for part_item_id, part_index in content_parts
                ):
                    return False
                completed_items[item_id] = copy.deepcopy(raw)
                closed.add(item_id)
            elif event_type in {"response.completed", "response.done"}:
                terminal_response = event["response"]
                if (
                    not response_started
                    or added != closed
                    or content_parts != content_parts_closed
                ):
                    return False
                terminal_items = {
                    item_id: item
                    for item in terminal_response["output"]
                    if isinstance(item, dict) and (item_id := _stream_item_id(item.get("id"))) is not None
                }
                if any(
                    not isinstance(item, dict)
                    or (item.get("status") is not None and item.get("status") != "completed")
                    for item in terminal_response["output"]
                ):
                    return False
                for item_id, item_type in item_types.items():
                    if item_type != "message":
                        continue
                    expected_parts = [
                        content_part_types[(part_item_id, part_index)]
                        for part_item_id, part_index in sorted(content_parts)
                        if part_item_id == item_id
                    ]
                    snapshot = terminal_items.get(item_id)
                    content = snapshot.get("content") if snapshot is not None else None
                    if not isinstance(content, list) or len(content) != len(expected_parts):
                        return False
                    if any(
                        not isinstance(part, dict)
                        or part.get("type") != expected_type
                        for part, expected_type in zip(content, expected_parts)
                    ):
                        return False
                if any(
                    isinstance(item, dict)
                    and item.get("type") == "reasoning"
                    and item.get("encrypted_content") is not None
                    and not isinstance(item.get("encrypted_content"), str)
                    for item in terminal_response["output"]
                ):
                    return False
                completed_output = [completed_items[index_items[index]] for index in sorted(index_items)]
                if (
                    _responses_output_projection(completed_output) is None
                    or _responses_output_projection(terminal_response["output"]) is None
                    or _responses_output_projection(completed_output) != _responses_output_projection(terminal_response["output"])
                ):
                    return False
                if terminal_response["id"] != response_id:
                    return False
                terminal = index
            elif event_type in {"error", "response.failed", "response.incomplete"}:
                return False
        return terminal is not None and terminal == len(events) - 1
    terminal = None
    done_seen = False
    usage_seen = False
    seen_tool_indexes: set[int] = set()
    argument_fragment_counts: dict[int, int] = {}
    tool_ids: dict[int, str] = {}
    completion_id: str | None = None
    assistant_role_seen = False
    payload_seen = False
    for index, item in enumerate(events):
        if item.get("kind") == "done":
            if index != len(events) - 1:
                return False
            if done_seen:
                return False
            done_type = item.get("type")
            if done_type is not None and done_type != "chat.completion.chunk":
                return False
            done_seen = True
            continue
        event = item.get("event", {})
        event_type = event.get("type")
        if event_type is not None and not isinstance(event_type, str):
            return False
        if event_type not in CHAT_STREAM_EVENT_TYPES:
            return False
        if _stream_envelope_errors("chat", event):
            return False
        if item.get("type") is not None and item.get("type") != event_type:
            return False
        if event_type == "error":
            return False
        if "error" in event:
            return False
        choices = event["choices"]
        usage = event.get("usage")
        event_id = event["id"]
        if completion_id is None:
            completion_id = event_id
        elif event_id != completion_id:
            return False
        if terminal is not None:
            if choices or not isinstance(usage, dict) or usage_seen:
                return False
            usage_seen = True
            continue
        if choices:
            choice = choices[0]
            delta = choice["delta"]
            if "role" in delta:
                if delta["role"] != "assistant" or assistant_role_seen or payload_seen:
                    return False
                assistant_role_seen = True
            if _chat_delta_has_payload(delta):
                if not assistant_role_seen:
                    return False
                payload_seen = True
            tool_calls = delta.get("tool_calls", [])
            if not isinstance(tool_calls, list):
                return False
            chunk_last_tool_index = -1
            for call in tool_calls:
                if not isinstance(call, dict):
                    return False
                tool_index = _stream_index(call.get("index"))
                if tool_index is None:
                    return False
                if tool_index <= chunk_last_tool_index:
                    return False
                if tool_index not in seen_tool_indexes:
                    if tool_index != len(seen_tool_indexes):
                        return False
                    seen_tool_indexes.add(tool_index)
                    tool_id = call.get("id")
                    if not isinstance(tool_id, str) or not tool_id:
                        return False
                    tool_ids[tool_index] = tool_id
                elif "id" in call:
                    tool_id = call.get("id")
                    if not isinstance(tool_id, str) or not tool_id or tool_ids.get(tool_index) != tool_id:
                        return False
                raw_type = call.get("type")
                if raw_type is not None and not isinstance(raw_type, str):
                    return False
                function = call.get("function", {})
                if not isinstance(function, dict):
                    return False
                if any(field in function and not isinstance(function[field], str) for field in ("name", "arguments")):
                    return False
                argument_fragment = function.get("arguments")
                if argument_fragment:
                    argument_fragment_counts[tool_index] = argument_fragment_counts.get(tool_index, 0) + 1
                chunk_last_tool_index = tool_index
            finish_reason = choice.get("finish_reason")
            if finish_reason is not None and not isinstance(finish_reason, str):
                return False
            if finish_reason is not None:
                terminal = index
        elif usage is not None:
            if terminal is None or not isinstance(usage, dict):
                return False
        else:
            return False
    return (
        terminal is not None
        and done_seen
        and assistant_role_seen
        and all(argument_fragment_counts.get(index, 0) >= 2 for index in seen_tool_indexes)
    )


def _request(path: str, payload: dict[str, Any], *, client_protocol: str, stream: bool) -> TransportResult:
    body = json.dumps(payload, separators=(",", ":")).encode("utf-8")
    headers = {"Content-Type": "application/json"}
    if GATEWAY_TOKEN:
        headers["Authorization"] = f"Bearer {GATEWAY_TOKEN}"
    if client_protocol == "anthropic":
        headers["anthropic-version"] = "2023-06-01"
    request = urllib.request.Request(_request_url(client_protocol, path), body, headers, method="POST")
    started = time.monotonic()
    try:
        with OPENER.open(request, timeout=STREAM_TOTAL_TIMEOUT if stream else 10) as response:
            status = response.status
            if not stream:
                try:
                    document = json.loads(
                        response.read().decode("utf-8"),
                        object_pairs_hook=_reject_duplicate_keys,
                        parse_constant=_reject_nonfinite,
                    )
                except (UnicodeDecodeError, json.JSONDecodeError, ValueError):
                    return TransportResult(status, None, [], False, 1)
                return TransportResult(status, document if isinstance(document, dict) else None, [], False, 0)
            content_type = str(getattr(response, "headers", {}).get("Content-Type", ""))
            if content_type.split(";", 1)[0].strip().lower() != "text/event-stream":
                return TransportResult(status, None, [], False, 1, False, False, False)
            events: list[dict[str, Any]] = []
            data_lines: list[str] = []
            event_name: str | None = None
            done = False
            invalid = [0]
            tokenizer = SSEFrameTokenizer()
            first_line = True

            def consume_line(raw_line: bytes) -> bool:
                nonlocal event_name, first_line
                try:
                    line = raw_line.decode("utf-8").rstrip("\r\n")
                except UnicodeDecodeError:
                    invalid[0] += 1
                    return False
                if first_line:
                    first_line = False
                    if line.startswith("\ufeff"):
                        line = line[1:]
                if not line:
                    flushed = _flush_sse(data_lines, event_name, events, invalid)
                    event_name = None
                    return flushed
                if line.startswith("event:"):
                    event_name = line[6:]
                    if event_name.startswith(" "):
                        event_name = event_name[1:]
                else:
                    field, separator, value = line.partition(":")
                    if separator and value.startswith(" "):
                        value = value[1:]
                    if field == "event":
                        event_name = value
                    elif field == "data":
                        data_lines.append(value)
                return False

            while True:
                elapsed = time.monotonic() - started
                if elapsed >= STREAM_TOTAL_TIMEOUT:
                    return TransportResult(status, None, events, done, invalid[0], False, True)
                _set_stream_read_timeout(response, STREAM_TOTAL_TIMEOUT - elapsed)
                try:
                    chunk = response.read(1)
                except (TimeoutError, socket.timeout):
                    return TransportResult(status, None, events, done, invalid[0], False, True)
                if not chunk:
                    break
                for frame in tokenizer.feed(chunk):
                    for raw_line in frame.split(b"\n"):
                        done = consume_line(raw_line) or done
                    done = consume_line(b"") or done
                if invalid[0]:
                    return TransportResult(status, None, events, done, invalid[0], False, False)
                if done:
                    break
            # SSE dispatch requires a blank line; discard a final unterminated event at EOF.
            data_lines.clear()
            event_name = None
            return TransportResult(status, None, events, done, invalid[0], _stream_order_ok(client_protocol, events), False, True)
    except urllib.error.HTTPError as error:
        return TransportResult(error.code, None, [], False, 0)
    except (OSError, TimeoutError, urllib.error.URLError):
        return TransportResult(0, None, [], False, 0)


def _parse_arguments(value: Any, *, expected_wire: str | None = None) -> tuple[Any, str | None]:
    if expected_wire == "object" and not isinstance(value, dict):
        return value, "arguments_not_object"
    if expected_wire == "json" and not isinstance(value, str):
        return value, "arguments_not_json_string"
    if isinstance(value, dict):
        return value, None
    if not isinstance(value, str):
        return value, "arguments_not_object_or_json"
    try:
        parsed = json.loads(value, object_pairs_hook=_reject_duplicate_keys, parse_constant=_reject_nonfinite)
    except _DuplicateKeyError:
        return value, "arguments_duplicate_key"
    except (json.JSONDecodeError, ValueError):
        return value, "arguments_invalid_json"
    return parsed, None if isinstance(parsed, dict) else "arguments_not_object"


def _required_identifier(value: Any, errors: list[str]) -> str:
    if not isinstance(value, str) or not value:
        errors.append("tool_call_id_invalid")
        return ""
    return value


def _stream_index(value: Any) -> int | None:
    if type(value) is int and value >= 0:
        return value
    return None


def _reasoning_text_parts(parts: Any) -> list[str]:
    if not isinstance(parts, list):
        return []
    return [
        part["text"]
        for part in parts
        if isinstance(part, dict)
        and isinstance(part.get("type"), str)
        and part.get("type") in RESPONSES_REASONING_PART_TYPES
        and isinstance(part.get("text"), str)
        and part["text"]
    ]


def _reasoning_text(parts: Any) -> str:
    return "".join(_reasoning_text_parts(parts))


def _reasoning_item_has_signal(item: dict[str, Any]) -> bool:
    encrypted = item.get("encrypted_content")
    return bool(
        _reasoning_text(item.get("summary"))
        or _reasoning_text(item.get("content"))
        or (isinstance(encrypted, str) and encrypted)
    )


def _parse_anthropic_document(document: dict[str, Any] | None, *, event_count: int = 0, invalid_event_count: int = 0, terminal: bool | None = None) -> Turn:
    errors: list[str] = []
    if not isinstance(document, dict):
        errors.append("message_envelope_invalid")
        document = {}
    _extend_unique(errors, _required_envelope_errors("anthropic.message", document))
    content = document.get("content")
    if not isinstance(content, list):
        content = []
    calls: list[ToolCall] = []
    text_parts: list[str] = []
    reasoning_text_parts: list[str] = []
    reasoning_parts: list[str] = []
    reasoning = False
    for block in content:
        if not isinstance(block, dict):
            errors.append("content_block_invalid")
            continue
        block_type = block.get("type")
        if not isinstance(block_type, str) or block_type not in ANTHROPIC_CONTENT_BLOCK_TYPES:
            errors.append("content_block_type_invalid")
            continue
        if block_type == "tool_use":
            arguments, error = _parse_arguments(block.get("input"), expected_wire="object")
            if error:
                errors.append(error)
            calls.append(ToolCall(_required_identifier(block.get("id"), errors), str(block.get("name", "")), arguments))
        elif block_type == "text":
            text = block.get("text", "")
            if not isinstance(text, str):
                errors.append("text_block_invalid")
            else:
                text_parts.append(text)
        elif block_type == "thinking":
            thinking = block.get("thinking")
            if not isinstance(thinking, str) or not thinking:
                errors.append("thinking_payload_missing")
            else:
                reasoning = True
                reasoning_text_parts.append(thinking)
                reasoning_parts.append(thinking)
            signature = block.get("signature")
            if not isinstance(signature, str) or not signature:
                errors.append("thinking_signature_missing")
            else:
                reasoning_parts.append(signature)
        elif block_type == "redacted_thinking":
            data = block.get("data")
            if not isinstance(data, str) or not data:
                errors.append("redacted_thinking_payload_missing")
            else:
                reasoning = True
                reasoning_parts.append(data)
    stop_reason = document.get("stop_reason") if isinstance(document, dict) else None
    if stop_reason is not None and not isinstance(stop_reason, str):
        errors.append("stop_reason_invalid")
        stop_reason = None
    turn = Turn("anthropic", calls, "".join(text_parts), reasoning, stop_reason, content, terminal if terminal is not None else stop_reason is not None, event_count, invalid_event_count, errors)
    turn.reasoning_text = "".join(reasoning_text_parts)
    turn.reasoning_parts = reasoning_parts
    return turn


def _parse_anthropic_stream(result: TransportResult) -> Turn:
    blocks: dict[int, dict[str, Any]] = {}
    signed_blocks: set[int] = set()
    argument_fragment_counts: dict[int, int] = {}
    stop_reason: str | None = None
    stop_sequence: str | None = None
    errors: list[str] = []
    text_delta_count = 0
    envelope: dict[str, Any] = {}
    message_delta_seen = False
    for item in result.events:
        if item.get("kind") == "done":
            errors.append("stream_done_sentinel")
            continue
        event = item.get("event", {})
        event_type = event.get("type")
        if not isinstance(event_type, str):
            errors.append("stream_event_type_invalid")
            continue
        if event_type not in ANTHROPIC_STREAM_EVENT_TYPES:
            errors.append("stream_event_unknown")
            continue
        _extend_unique(errors, _stream_envelope_errors("anthropic", event))
        if event_type == "error":
            errors.append("stream_error_event")
        elif event_type == "content_block_start":
            index = _stream_index(event.get("index"))
            if index is None:
                errors.append("stream_index_invalid")
                continue
            block = copy.deepcopy(event.get("content_block", {}))
            if not isinstance(block, dict):
                errors.append("content_block_invalid")
                continue
            block.setdefault("type", "")
            if block.get("type") == "tool_use":
                if block.get("input") != {}:
                    errors.append("stream_tool_input_snapshot_invalid")
                block["_arguments"] = ""
            elif block.get("type") == "text":
                if "text" not in block:
                    errors.append("stream_text_snapshot_missing")
                    text_snapshot = ""
                else:
                    text_snapshot = block["text"]
                if not isinstance(text_snapshot, str):
                    errors.append("stream_text_snapshot_invalid")
                elif text_snapshot:
                    errors.append("stream_text_opening_snapshot_invalid")
                block["text"] = ""
            elif block.get("type") == "thinking":
                for field in ("thinking", "signature"):
                    if field not in block:
                        errors.append(f"stream_{field}_snapshot_missing")
                        value = ""
                    else:
                        value = block[field]
                    if not isinstance(value, str):
                        errors.append(f"stream_{field}_snapshot_invalid")
                    elif value:
                        errors.append(f"stream_{field}_opening_snapshot_invalid")
                    block[field] = ""
            blocks[index] = block
        elif event_type == "content_block_delta":
            index = _stream_index(event.get("index"))
            if index is None:
                errors.append("stream_index_invalid")
                continue
            block = blocks.setdefault(index, {"type": ""})
            delta = event.get("delta", {})
            if not isinstance(delta, dict):
                errors.append("stream_delta_invalid")
                continue
            delta_type = delta.get("type")
            if delta_type == "input_json_delta":
                fragment = delta.get("partial_json")
                if block.get("type") != "tool_use" or not isinstance(fragment, str):
                    errors.append("stream_delta_block_type_mismatch")
                else:
                    if fragment:
                        argument_fragment_counts[index] = (
                            argument_fragment_counts.get(index, 0) + 1
                        )
                    block["_arguments"] = block.get("_arguments", "") + fragment
            elif delta_type == "text_delta":
                text = delta.get("text")
                if block.get("type") != "text" or not isinstance(text, str):
                    errors.append("stream_delta_block_type_mismatch")
                else:
                    if text:
                        text_delta_count += 1
                    block["text"] = block.get("text", "") + text
            elif delta_type == "thinking_delta":
                thinking = delta.get("thinking")
                if block.get("type") != "thinking" or not isinstance(thinking, str):
                    errors.append("stream_delta_block_type_mismatch")
                elif index in signed_blocks:
                    errors.append("stream_delta_after_signature")
                else:
                    block["thinking"] = block.get("thinking", "") + thinking
            elif delta_type == "signature_delta":
                signature = delta.get("signature")
                if block.get("type") != "thinking" or not isinstance(signature, str):
                    errors.append("stream_delta_block_type_mismatch")
                elif index in signed_blocks:
                    errors.append("stream_delta_after_signature")
                else:
                    block["signature"] = block.get("signature", "") + signature
                    signed_blocks.add(index)
            else:
                errors.append("stream_delta_type_invalid")
        elif event_type == "message_start":
            message = event.get("message")
            if not isinstance(message, dict):
                continue
            else:
                envelope = {"type": message.get("type"), "role": message.get("role")}
        elif event_type == "message_delta":
            message_delta_seen = True
            delta = event.get("delta")
            if not isinstance(delta, dict):
                continue
            else:
                candidate_reason = delta.get("stop_reason")
                candidate_sequence = delta.get("stop_sequence")
                if (
                    not isinstance(candidate_reason, str)
                    or "stop_sequence" not in delta
                    or (candidate_sequence is not None and not isinstance(candidate_sequence, str))
                    or (candidate_reason == "stop_sequence" and not candidate_sequence)
                    or (candidate_reason != "stop_sequence" and candidate_sequence is not None)
                ):
                    errors.append("message_delta_stop_sequence_invalid")
                else:
                    stop_reason = candidate_reason
                    stop_sequence = candidate_sequence
        elif event_type == "message_stop" and not message_delta_seen:
            errors.append("message_delta_missing")
    content: list[dict[str, Any]] = []
    for index in sorted(blocks):
        block = blocks[index]
        if "_arguments" in block:
            fragment_count = argument_fragment_counts.get(index, 0)
            if fragment_count == 0:
                errors.append("stream_arguments_missing")
            elif fragment_count < 2:
                errors.append("stream_arguments_not_fragmented")
            arguments, error = _parse_arguments(block.pop("_arguments"))
            block["input"] = arguments
            if error:
                errors.append(error)
        content.append(block)
    document = {
        "type": envelope.get("type"),
        "role": envelope.get("role"),
        "content": content,
        "stop_reason": stop_reason,
        "stop_sequence": stop_sequence,
    }
    turn = _parse_anthropic_document(
        document,
        event_count=len(result.events),
        invalid_event_count=result.invalid_event_count,
        terminal=result.done_sentinel or any(item.get("type") == "message_stop" for item in result.events),
    )
    turn.parse_errors.extend(errors)
    turn.stream_order_ok = result.stream_order_ok
    turn.deadline_expired = result.deadline_expired
    turn.content_type_ok = result.content_type_ok
    turn.stream_text_delta_count = text_delta_count
    return turn


def _parse_responses_document(document: dict[str, Any] | None, *, event_count: int = 0, invalid_event_count: int = 0, terminal: bool | None = None) -> Turn:
    errors: list[str] = []
    payload = document if isinstance(document, dict) else {}
    _extend_unique(errors, _required_envelope_errors("responses.response", payload))
    output = payload.get("output")
    if not isinstance(output, list):
        output = []
    calls: list[ToolCall] = []
    text_parts: list[str] = []
    reasoning_text_parts: list[str] = []
    reasoning_parts: list[str] = []
    output_item_ids: set[str] = set()
    reasoning = False
    for item in output:
        if not isinstance(item, dict):
            errors.append("output_item_invalid")
            continue
        item_id = _stream_item_id(item.get("id"))
        if item_id is None:
            errors.append("output_item_id_invalid")
        elif item_id in output_item_ids:
            errors.append("output_item_id_duplicate")
        else:
            output_item_ids.add(item_id)
        item_type = item.get("type")
        if not isinstance(item_type, str) or item_type not in RESPONSES_OUTPUT_ITEM_TYPES:
            errors.append("output_item_type_invalid")
            continue
        item_status = item.get("status")
        if item_status is not None and item_status != "completed":
            errors.append("output_item_status_invalid")
            continue
        if item_type == "function_call":
            arguments, error = _parse_arguments(item.get("arguments"), expected_wire="json")
            if error:
                errors.append(error)
            calls.append(ToolCall(_required_identifier(item.get("call_id"), errors), str(item.get("name", "")), arguments))
        elif item_type == "reasoning":
            encrypted = item.get("encrypted_content")
            if encrypted is not None and not isinstance(encrypted, str):
                errors.append("encrypted_reasoning_invalid")
            for field in ("summary", "content"):
                parts = item.get(field)
                if parts is not None and not isinstance(parts, list):
                    errors.append("reasoning_parts_invalid")
                elif isinstance(parts, list):
                    for part in parts:
                        if (
                            not isinstance(part, dict)
                            or not isinstance(part.get("type"), str)
                            or part.get("type") not in RESPONSES_REASONING_PART_TYPES
                        ):
                            errors.append("reasoning_part_type_invalid")
                        elif not isinstance(part.get("text"), str):
                            errors.append("reasoning_part_text_invalid")
            item_parts = [*_reasoning_text_parts(item.get("summary")), *_reasoning_text_parts(item.get("content"))]
            if _reasoning_item_has_signal(item):
                reasoning = True
                reasoning_text_parts.extend(item_parts)
                reasoning_parts.extend(item_parts)
                if isinstance(encrypted, str) and encrypted:
                    reasoning_parts.append(encrypted)
        elif item_type == "message":
            if item.get("role") != "assistant":
                errors.append("assistant_role_invalid")
            content = item.get("content")
            if not isinstance(content, list):
                errors.append("message_content_invalid")
                continue
            for part in content:
                if not isinstance(part, dict) or not isinstance(part.get("type"), str) or part.get("type") not in RESPONSES_MESSAGE_PART_TYPES:
                    errors.append("message_part_type_invalid")
                elif part.get("type") in RESPONSES_MESSAGE_PART_TYPES:
                    text = part.get("text", "")
                    if not isinstance(text, str):
                        errors.append("message_text_invalid")
                    else:
                        text_parts.append(text)
    status = payload.get("status")
    if status is not None and not isinstance(status, str):
        errors.append("status_invalid")
        status = None
    turn = Turn("responses", calls, "".join(text_parts), reasoning, status, output, terminal if terminal is not None else status in {"completed", "incomplete"}, event_count, invalid_event_count, errors)
    turn.reasoning_text = "".join(reasoning_text_parts)
    turn.reasoning_parts = reasoning_parts
    return turn


def _responses_output_projection(output: Any) -> list[dict[str, Any]] | None:
    if not isinstance(output, list):
        return None
    projection: list[dict[str, Any]] = []
    for item in output:
        if not isinstance(item, dict):
            return None
        item_type = item.get("type")
        projected: dict[str, Any] = {"id": item.get("id"), "type": item_type, "status": item.get("status")}
        if item_type == "function_call":
            arguments, error = _parse_arguments(item.get("arguments"), expected_wire="json")
            if error:
                return None
            projected.update({"call_id": item.get("call_id"), "name": item.get("name"), "arguments": arguments})
        elif item_type == "message":
            content = item.get("content")
            if item.get("role") != "assistant" or not isinstance(content, list):
                return None
            projected_content: list[dict[str, Any]] = []
            for part in content:
                if not isinstance(part, dict) or not isinstance(part.get("type"), str) or part.get("type") not in RESPONSES_MESSAGE_PART_TYPES:
                    return None
                text = part.get("text")
                if not isinstance(text, str):
                    return None
                projected_content.append({"type": part.get("type"), "text": text})
            projected["content"] = projected_content
        elif item_type == "reasoning":
            encrypted = item.get("encrypted_content")
            if encrypted is not None and not isinstance(encrypted, str):
                return None
            for field in ("summary", "content"):
                parts = item.get(field)
                if parts is not None:
                    if not isinstance(parts, list):
                        return None
                    if any(
                        not isinstance(part, dict)
                        or not isinstance(part.get("type"), str)
                        or part.get("type") not in RESPONSES_REASONING_PART_TYPES
                        or not isinstance(part.get("text"), str)
                        for part in parts
                    ):
                        return None
            projected.update(
                {
                    "summary": [
                        {"type": part["type"], "text": part["text"]}
                        for part in item.get("summary") or []
                    ],
                    "content": [
                        {"type": part["type"], "text": part["text"]}
                        for part in item.get("content") or []
                    ],
                    "encrypted_content": encrypted,
                }
            )
        else:
            projected["raw"] = item
        projection.append(projected)
    return projection


def _parse_responses_stream(result: TransportResult) -> Turn:
    output: dict[str, dict[str, Any]] = {}
    text_parts: list[str] = []
    text_by_item: dict[str, str] = {}
    text_by_part: dict[tuple[str, int], str] = {}
    reasoning_by_item: dict[str, str] = {}
    reasoning_by_summary: dict[tuple[str, int], str] = {}
    reasoning_summary_indexes: dict[str, set[int]] = {}
    reasoning_summary_done: set[tuple[str, int]] = set()
    encrypted_reasoning_by_item: dict[str, str | None] = {}
    opening_status_by_item: dict[str, str | None] = {}
    status: str | None = None
    response_id: str | None = None
    response_in_progress_seen = False
    args_by_item: dict[str, str] = {}
    argument_fragment_counts: dict[str, int] = {}
    argument_done_items: set[str] = set()
    content_parts_closed: set[tuple[str, int]] = set()
    content_part_types: dict[tuple[str, int], str] = {}
    text_delta_count = 0
    terminal_response: dict[str, Any] | None = None
    streamed_output_seen = False
    errors: list[str] = []
    for item in result.events:
        event = item.get("event", {})
        event_type = event.get("type")
        if not isinstance(event_type, str):
            errors.append("stream_event_type_invalid")
            continue
        if event_type not in RESPONSES_STREAM_EVENT_TYPES:
            errors.append("stream_event_unknown")
            continue
        _extend_unique(errors, _stream_envelope_errors("responses", event))
        if event_type in {"error", "response.failed", "response.incomplete"}:
            errors.append("stream_failure_event")
        elif event_type == "response.created":
            response = event.get("response")
            if (
                not isinstance(response, dict)
                or response.get("object") != "response"
                or response.get("status") != "in_progress"
                or response.get("output") != []
            ):
                errors.append("response_start_snapshot_invalid")
            if not isinstance(response, dict) or not isinstance(response.get("id"), str) or not response["id"]:
                errors.append("response_id_invalid")
            else:
                response_id = response["id"]
        elif event_type == "response.in_progress":
            if response_in_progress_seen:
                errors.append("response_in_progress_duplicate")
            response_in_progress_seen = True
            response = event.get("response")
            if (
                not isinstance(response, dict)
                or response.get("object") != "response"
                or response.get("status") != "in_progress"
                or response.get("output") != []
            ):
                errors.append("response_in_progress_snapshot_invalid")
            snapshot_id = response.get("id") if isinstance(response, dict) else None
            if not isinstance(snapshot_id, str) or snapshot_id != response_id:
                errors.append("response_in_progress_id_invalid")
        elif event_type == "response.output_item.added":
            streamed_output_seen = True
            raw = copy.deepcopy(event.get("item"))
            if not isinstance(raw, dict):
                errors.append("output_item_invalid")
                continue
            key = _parsed_stream_item_id(raw.get("id"), errors)
            if key is None:
                continue
            output[key] = raw
            opening_status = raw.get("status")
            if opening_status is not None and opening_status != "in_progress":
                errors.append("stream_output_item_status_invalid")
            opening_status_by_item[key] = opening_status
            if raw.get("type") == "message" and "content" in raw and raw["content"] != []:
                errors.append("stream_message_opening_snapshot_invalid")
            if raw.get("type") == "function_call":
                for field in ("call_id", "name"):
                    if not isinstance(raw.get(field), str) or not raw[field]:
                        errors.append("stream_function_identity_invalid")
                if "arguments" not in raw:
                    errors.append("stream_arguments_snapshot_missing")
                elif not isinstance(raw["arguments"], str) or raw["arguments"] != "":
                    errors.append("stream_arguments_opening_snapshot_invalid")
            if raw.get("type") == "message" and raw.get("role") != "assistant":
                errors.append("assistant_role_invalid")
            if raw.get("type") == "reasoning":
                opening_summary = raw.get("summary")
                if opening_summary not in (None, []):
                    errors.append("stream_reasoning_opening_snapshot_invalid")
                opening_content = raw.get("content")
                if opening_content not in (None, []):
                    errors.append("stream_reasoning_opening_snapshot_invalid")
                reasoning_by_item[key] = _reasoning_text(raw.get("summary"))
                opening_encrypted = raw.get("encrypted_content")
                if opening_encrypted is not None and not isinstance(opening_encrypted, str):
                    errors.append("encrypted_reasoning_snapshot_invalid")
                    opening_encrypted = None
                encrypted_reasoning_by_item[key] = opening_encrypted
        elif event_type == "response.output_item.done":
            streamed_output_seen = True
            raw = event.get("item")
            if isinstance(raw, dict):
                key = _parsed_stream_item_id(raw.get("id"), errors)
                if key is None:
                    continue
                previous = output.get(key)
                if previous is not None and any(
                    field in previous and field in raw and previous[field] != raw[field]
                    for field in ("id", "type", "call_id", "name")
                ):
                    errors.append("stream_item_snapshot_mismatch")
                if key in args_by_item and isinstance(raw.get("arguments"), str):
                    delta_arguments, delta_error = _parse_arguments(args_by_item[key], expected_wire="json")
                    done_arguments, done_error = _parse_arguments(raw["arguments"], expected_wire="json")
                    if delta_error or done_error or delta_arguments != done_arguments:
                        errors.append("stream_item_snapshot_mismatch")
                if raw.get("type") == "function_call":
                    snapshot_arguments = raw.get("arguments")
                    if not isinstance(snapshot_arguments, str) or _parse_arguments(snapshot_arguments, expected_wire="json")[1]:
                        errors.append("stream_arguments_snapshot_invalid")
                done_status = raw.get("status")
                if opening_status_by_item.get(key) is not None and done_status != "completed":
                    errors.append("stream_output_item_status_invalid")
                if done_status is not None and done_status != "completed":
                    errors.append("stream_output_item_status_invalid")
                if raw.get("type") == "reasoning" and key in reasoning_by_item:
                    snapshot_reasoning = _reasoning_text(raw.get("summary")) + _reasoning_text(raw.get("content"))
                    if snapshot_reasoning != reasoning_by_item[key]:
                        errors.append("stream_reasoning_snapshot_mismatch")
                    summary = raw.get("summary", [])
                    if not isinstance(summary, list):
                        errors.append("stream_reasoning_summary_invalid")
                    else:
                        summary_indexes = reasoning_summary_indexes.get(key, set())
                        if set(range(len(summary))) != summary_indexes:
                            errors.append("stream_reasoning_summary_snapshot_invalid")
                            if any(summary_index not in summary_indexes for summary_index in range(len(summary))):
                                errors.append("stream_reasoning_summary_events_missing")
                        if any((key, summary_index) not in reasoning_summary_done for summary_index in summary_indexes):
                            errors.append("stream_reasoning_summary_events_missing")
                        for summary_index, part in enumerate(summary):
                            if (
                                not isinstance(part, dict)
                                or not isinstance(part.get("type"), str)
                                or part.get("type") not in RESPONSES_REASONING_PART_TYPES
                                or not isinstance(part.get("text"), str)
                            ):
                                errors.append("stream_reasoning_summary_snapshot_invalid")
                            elif part["text"] != reasoning_by_summary.get((key, summary_index), ""):
                                errors.append("stream_reasoning_summary_snapshot_mismatch")
                    done_encrypted = raw.get("encrypted_content")
                    if done_encrypted is not None and not isinstance(done_encrypted, str):
                        errors.append("encrypted_reasoning_snapshot_invalid")
                    elif done_encrypted != encrypted_reasoning_by_item.get(key):
                        errors.append("stream_reasoning_snapshot_mismatch")
                output[key] = copy.deepcopy(raw)
                if raw.get("type") == "message" and raw.get("role") != "assistant":
                    errors.append("assistant_role_invalid")
                raw_type = raw.get("type")
                if not isinstance(raw_type, str) or raw_type not in RESPONSES_OUTPUT_ITEM_TYPES:
                    errors.append("output_item_type_invalid")
                    continue
                if raw_type == "message":
                    content = raw.get("content")
                    if not isinstance(content, list):
                        errors.append("message_content_invalid")
                        continue
                    expected_parts = [
                        content_part_types[(part_item_id, part_index)]
                        for part_item_id, part_index in sorted(content_part_types)
                        if part_item_id == key
                    ]
                    if len(content) != len(expected_parts):
                        errors.append("stream_content_part_snapshot_mismatch")
                    item_snapshot_parts: list[str] = []
                    for part, expected_type in zip(content, expected_parts):
                        if not isinstance(part, dict) or part.get("type") != expected_type:
                            errors.append("message_part_type_invalid")
                        elif part.get("type") in RESPONSES_MESSAGE_PART_TYPES:
                            text = part.get("text", "")
                            if not isinstance(text, str):
                                errors.append("message_text_invalid")
                            else:
                                item_snapshot_parts.append(text)
                    if "".join(item_snapshot_parts) != text_by_item.get(key, ""):
                        errors.append("stream_text_snapshot_mismatch")
            else:
                errors.append("output_item_invalid")
        elif event_type in {"response.content_part.added", "response.content_part.done"}:
            key = _parsed_stream_item_id(event.get("item_id"), errors)
            content_index = _stream_index(event.get("content_index"))
            if key is None or content_index is None:
                if content_index is None:
                    errors.append("content_index_invalid")
                continue
            part = event.get("part")
            if not isinstance(part, dict):
                errors.append("content_part_invalid")
                continue
            part_type = part.get("type")
            if not isinstance(part_type, str) or part_type not in RESPONSES_MESSAGE_PART_TYPES:
                errors.append("content_part_type_invalid")
                continue
            if event_type == "response.content_part.added":
                content_part_types[(key, content_index)] = part_type
            if event_type == "response.content_part.added" and part_type in RESPONSES_MESSAGE_PART_TYPES:
                opening_text = part.get("text")
                if "text" not in part or not isinstance(opening_text, str) or opening_text:
                    errors.append("content_part_opening_snapshot_invalid")
            elif event_type == "response.content_part.done":
                if content_part_types.get((key, content_index)) != part_type:
                    errors.append("content_part_type_mismatch")
                part_text = part.get("text")
                if not isinstance(part_text, str):
                    errors.append("content_part_text_invalid")
                elif part_text != text_by_part.get((key, content_index), ""):
                    errors.append("content_part_snapshot_mismatch")
                content_parts_closed.add((key, content_index))
        elif event_type == "response.function_call_arguments.delta":
            key = _parsed_stream_item_id(event.get("item_id"), errors)
            if key is None:
                continue
            if output.get(key, {}).get("type") != "function_call":
                errors.append("stream_delta_item_type_mismatch")
                continue
            delta = event.get("delta")
            if not isinstance(delta, str):
                errors.append("stream_arguments_invalid")
                continue
            if delta:
                argument_fragment_counts[key] = argument_fragment_counts.get(key, 0) + 1
            args_by_item[key] = args_by_item.get(key, "") + delta
        elif event_type == "response.function_call_arguments.done":
            key = _parsed_stream_item_id(event.get("item_id"), errors)
            if key is None:
                continue
            if output.get(key, {}).get("type") != "function_call":
                errors.append("stream_delta_item_type_mismatch")
                continue
            done_arguments = event.get("arguments")
            if not isinstance(done_arguments, str):
                errors.append("stream_arguments_done_invalid")
                continue
            reconstructed, reconstructed_error = _parse_arguments(args_by_item.get(key, ""), expected_wire="json")
            completed, completed_error = _parse_arguments(done_arguments, expected_wire="json")
            if reconstructed_error or completed_error or reconstructed != completed:
                errors.append("stream_arguments_done_mismatch")
            argument_done_items.add(key)
        elif event_type in {"response.output_text.delta", "response.reasoning_summary_text.delta"}:
            key = _parsed_stream_item_id(event.get("item_id"), errors)
            if key is None:
                continue
            expected_type = "reasoning" if event_type.startswith("response.reasoning") else "message"
            if output.get(key, {}).get("type") != expected_type:
                errors.append("stream_delta_item_type_mismatch")
                continue
            if event_type.startswith("response.reasoning"):
                delta = event.get("delta")
                if not isinstance(delta, str):
                    errors.append("stream_reasoning_delta_invalid")
                    continue
                summary_index = _stream_index(event.get("summary_index"))
                if summary_index is None:
                    errors.append("summary_index_invalid")
                    continue
                indexes = reasoning_summary_indexes.setdefault(key, set())
                if summary_index not in indexes and summary_index != len(indexes):
                    errors.append("summary_index_invalid")
                    continue
                indexes.add(summary_index)
                summary_key = (key, summary_index)
                reasoning_by_summary[summary_key] = reasoning_by_summary.get(summary_key, "") + delta
                if delta:
                    reasoning_by_item[key] = reasoning_by_item.get(key, "") + delta
            else:
                delta = event.get("delta")
                if not isinstance(delta, str):
                    errors.append("stream_text_delta_invalid")
                    continue
                if delta:
                    text_delta_count += 1
                text_parts.append(delta)
                text_by_item[key] = text_by_item.get(key, "") + delta
                content_index = _stream_index(event.get("content_index"))
                if content_index is None:
                    errors.append("content_index_invalid")
                else:
                    part_key = (key, content_index)
                    if part_key in content_parts_closed:
                        errors.append("stream_text_after_content_part_done")
                    text_by_part[part_key] = text_by_part.get(part_key, "") + delta
        elif event_type in {"response.output_text.done", "response.reasoning_summary_text.done"}:
            key = _parsed_stream_item_id(event.get("item_id"), errors)
            if key is None:
                continue
            expected_type = "reasoning" if event_type.startswith("response.reasoning") else "message"
            if output.get(key, {}).get("type") != expected_type:
                errors.append("stream_delta_item_type_mismatch")
                continue
            completed_text = event.get("text")
            if not isinstance(completed_text, str):
                errors.append("stream_text_done_invalid")
                continue
            if expected_type == "reasoning":
                summary_index = _stream_index(event.get("summary_index"))
                if summary_index is None:
                    errors.append("summary_index_invalid")
                    reconstructed_text = ""
                else:
                    summary_key = (key, summary_index)
                    if summary_key not in reasoning_by_summary:
                        errors.append("stream_reasoning_summary_done_without_delta")
                        reconstructed_text = ""
                    else:
                        reconstructed_text = reasoning_by_summary[summary_key]
                        reasoning_summary_done.add(summary_key)
            else:
                    content_index = _stream_index(event.get("content_index"))
                    if content_index is None:
                        errors.append("content_index_invalid")
                        continue
                    if (key, content_index) in content_parts_closed:
                        errors.append("stream_text_after_content_part_done")
                    reconstructed_text = text_by_part.get((key, content_index), "")
            if completed_text != reconstructed_text:
                errors.append("stream_text_done_mismatch")
        elif event_type in {"response.completed", "response.done"}:
            terminal_response = event.get("response") if isinstance(event.get("response"), dict) else None
            status = terminal_response.get("status") if terminal_response else None
            if status != "completed":
                errors.append("terminal_status_invalid")
            terminal_output = terminal_response.get("output") if terminal_response else None
            if isinstance(terminal_output, list):
                for raw in terminal_output:
                    if (
                        not isinstance(raw, dict)
                        or (raw.get("status") is not None and raw.get("status") != "completed")
                    ):
                        errors.append("terminal_output_item_status_invalid")
                    if (
                        isinstance(raw, dict)
                        and raw.get("type") == "reasoning"
                        and raw.get("encrypted_content") is not None
                        and not isinstance(raw.get("encrypted_content"), str)
                    ):
                        errors.append("encrypted_reasoning_snapshot_invalid")
            terminal_id = terminal_response.get("id") if terminal_response else None
            if not isinstance(terminal_id, str) or terminal_id != response_id:
                errors.append("terminal_response_id_invalid")
    items: list[dict[str, Any]] = []
    for key, raw in output.items():
        if raw.get("type") == "function_call":
            fragment_count = argument_fragment_counts.get(key, 0)
            if fragment_count == 0:
                errors.append("stream_arguments_missing")
            elif fragment_count < 2:
                errors.append("stream_arguments_not_fragmented")
            if key not in argument_done_items:
                errors.append("stream_arguments_done_missing")
            if key in args_by_item:
                raw["arguments"] = args_by_item[key]
        items.append(raw)
    if text_parts:
        message_items = [(key, raw) for key, raw in output.items() if raw.get("type") == "message"]
        if message_items:
            for key, raw in message_items:
                part_keys = sorted(
                    (part_key for part_key in content_part_types if part_key[0] == key),
                    key=lambda part_key: part_key[1],
                )
                if part_keys:
                    raw["content"] = [
                        {"type": content_part_types[part_key], "text": text_by_part.get(part_key, "")}
                        for part_key in part_keys
                    ]
                else:
                    raw["content"] = [{"type": "output_text", "text": text_by_item.get(key, "")}]
        else:
            items.append({"type": "message", "role": "assistant", "content": [{"type": "output_text", "text": "".join(text_parts)}]})
    if terminal_response is not None:
        terminal_output = terminal_response.get("output")
        if not isinstance(terminal_output, list):
            errors.append("terminal_output_invalid")
        elif streamed_output_seen:
            if _responses_output_projection(items) != _responses_output_projection(terminal_output):
                errors.append("terminal_output_mismatch")
        elif not items:
            items = copy.deepcopy(terminal_output)
            for item in terminal_output:
                if isinstance(item, dict) and item.get("type") == "function_call":
                    errors.append("stream_arguments_missing")
    response_object = terminal_response.get("object") if isinstance(terminal_response, dict) else None
    turn = _parse_responses_document(
        {"id": response_id, "object": response_object, "output": items, "status": status},
        event_count=len(result.events),
        invalid_event_count=result.invalid_event_count,
        terminal=status == "completed",
    )
    turn.parse_errors.extend(errors)
    turn.stream_order_ok = result.stream_order_ok
    turn.deadline_expired = result.deadline_expired
    turn.stream_text_delta_count = text_delta_count
    turn.content_type_ok = result.content_type_ok
    return turn


def _parse_chat_document(document: dict[str, Any] | None, *, event_count: int = 0, invalid_event_count: int = 0, terminal: bool | None = None) -> Turn:
    errors: list[str] = []
    payload = document if isinstance(document, dict) else {}
    _extend_unique(errors, _required_envelope_errors("chat.completion", payload))
    choices = payload.get("choices")
    if not isinstance(choices, list) or len(choices) != 1 or not isinstance(choices[0], dict):
        choice: dict[str, Any] = {}
    else:
        choice = choices[0]
    message = choice.get("message", {})
    if not isinstance(message, dict):
        message = {}
    calls: list[ToolCall] = []
    tool_calls = message.get("tool_calls", [])
    if not isinstance(tool_calls, list):
        errors.append("tool_calls_invalid")
        tool_calls = []
    for raw in tool_calls:
        if not isinstance(raw, dict):
            errors.append("tool_call_invalid")
            continue
        if raw.get("type") != "function":
            errors.append("tool_call_type_invalid")
            continue
        function = raw.get("function", {})
        if not isinstance(function, dict):
            errors.append("tool_function_invalid")
            continue
        arguments, error = _parse_arguments(function.get("arguments"), expected_wire="json")
        if error:
            errors.append(error)
        calls.append(ToolCall(_required_identifier(raw.get("id"), errors), str(function.get("name", "")), arguments))
    if "content" not in message:
        content = ""
    else:
        content = message["content"]
    if content is not None and not isinstance(content, str):
        errors.append("message_content_invalid")
        content = ""
    reasoning_content = message.get("reasoning_content", "") if isinstance(message, dict) else ""
    if "reasoning_content" in message and (not isinstance(reasoning_content, str) or not reasoning_content):
        errors.append("reasoning_content_invalid")
        reasoning_content = ""
    finish_reason = choice.get("finish_reason") if isinstance(choice, dict) else None
    if finish_reason is not None and not isinstance(finish_reason, str):
        errors.append("finish_reason_invalid")
        finish_reason = None
    continuation = copy.deepcopy(message) if isinstance(message, dict) else {}
    turn = Turn(
        "chat",
        calls,
        str(content or ""),
        bool(reasoning_content) or _chat_reasoning_usage_present(document),
        finish_reason,
        continuation,
        terminal if terminal is not None else finish_reason is not None,
        event_count,
        invalid_event_count,
        errors,
    )
    turn.reasoning_text = str(reasoning_content or "")
    turn.reasoning_parts = [turn.reasoning_text] if turn.reasoning_text else []
    return turn


def _chat_reasoning_usage_present(document: dict[str, Any] | None) -> bool:
    usage = document.get("usage") if isinstance(document, dict) else None
    details = usage.get("completion_tokens_details") if isinstance(usage, dict) else None
    reasoning_tokens = details.get("reasoning_tokens") if isinstance(details, dict) else None
    return type(reasoning_tokens) is int and reasoning_tokens > 0


def _parse_chat_stream(result: TransportResult) -> Turn:
    content: list[str] = []
    reasoning: list[str] = []
    calls: dict[int, dict[str, Any]] = {}
    finish_reason: str | None = None
    usage: dict[str, Any] | None = None
    usage_seen = False
    errors: list[str] = []
    argument_fragment_counts: dict[int, int] = {}
    tool_type_seen: set[int] = set()
    assistant_role_seen = False
    payload_seen = False
    completion_id: str | None = None
    for item in result.events:
        if item.get("kind") == "done":
            done_type = item.get("type")
            if done_type is not None and done_type != "chat.completion.chunk":
                errors.append("stream_done_event_type_invalid")
            continue
        event = item.get("event", {})
        event_type = event.get("type")
        if event_type is not None and not isinstance(event_type, str):
            errors.append("stream_event_type_invalid")
            continue
        if event_type not in CHAT_STREAM_EVENT_TYPES:
            errors.append("stream_event_unknown")
            continue
        if event_type == "error":
            errors.append("stream_failure_event")
            continue
        _extend_unique(errors, _stream_envelope_errors("chat", event))
        event_id = event.get("id")
        if isinstance(event_id, str) and event_id and completion_id is None:
            completion_id = event_id
        elif isinstance(event_id, str) and event_id and event_id != completion_id:
            errors.append("stream_completion_id_changed")
        if "error" in event:
            errors.append("stream_failure_event")
            continue
        event_usage = event.get("usage")
        if event_usage is not None:
            if finish_reason is None or not isinstance(event_usage, dict):
                errors.append("usage_before_finish")
            elif usage_seen:
                errors.append("usage_duplicate")
            else:
                usage = event_usage
                usage_seen = True
        choices = event.get("choices", [])
        if not isinstance(choices, list):
            continue
        if not choices:
            if event_usage is None:
                errors.append("stream_envelope_invalid")
            continue
        if len(choices) != 1 or not isinstance(choices[0], dict):
            continue
        choice = choices[0]
        if "delta" not in choice:
            continue
        delta = choice["delta"]
        if not isinstance(delta, dict):
            continue
        if "role" in delta:
            if delta.get("role") != "assistant":
                errors.append("assistant_role_invalid")
            elif assistant_role_seen:
                errors.append("assistant_role_duplicate")
            elif payload_seen:
                errors.append("assistant_role_late")
                assistant_role_seen = True
            else:
                assistant_role_seen = True
        if _chat_delta_has_payload(delta):
            if not assistant_role_seen:
                errors.append("assistant_role_before_payload_missing")
            payload_seen = True
        if "content" in delta:
            delta_content = delta["content"]
            if delta_content is not None and not isinstance(delta_content, str):
                errors.append("stream_content_invalid")
            elif delta_content:
                content.append(delta_content)
        if "reasoning_content" in delta:
            reasoning_content = delta["reasoning_content"]
            if not isinstance(reasoning_content, str) or not reasoning_content:
                errors.append("reasoning_content_invalid")
            else:
                reasoning.append(reasoning_content)
        choice_finish_reason = choice.get("finish_reason")
        if choice_finish_reason is not None and not isinstance(choice_finish_reason, str):
            errors.append("finish_reason_invalid")
        elif choice_finish_reason:
            finish_reason = choice_finish_reason
        tool_calls = delta.get("tool_calls", [])
        if not isinstance(tool_calls, list):
            errors.append("tool_calls_invalid")
            continue
        chunk_tool_indexes: set[int] = set()
        for raw in tool_calls:
            if not isinstance(raw, dict):
                errors.append("tool_call_invalid")
                continue
            index = _stream_index(raw.get("index"))
            if index is None:
                errors.append("stream_index_invalid")
                continue
            if index in chunk_tool_indexes:
                errors.append("stream_tool_index_duplicate")
                continue
            chunk_tool_indexes.add(index)
            opening_fragment = index not in calls
            call = calls.setdefault(index, {"id": "", "type": "function", "function": {"name": "", "arguments": ""}})
            raw_type = raw.get("type")
            if index not in tool_type_seen:
                if raw_type != "function":
                    errors.append("tool_call_type_invalid")
                    continue
                tool_type_seen.add(index)
            elif raw_type is not None and not isinstance(raw_type, str):
                errors.append("tool_call_type_invalid")
                continue
            elif raw_type not in {None, "function"}:
                errors.append("tool_call_type_invalid")
                continue
            if opening_fragment:
                raw_id = raw.get("id")
                if not isinstance(raw_id, str) or not raw_id:
                    errors.append("tool_call_id_invalid")
                else:
                    call["id"] = raw_id
            elif "id" in raw:
                if not isinstance(raw["id"], str) or not raw["id"]:
                    errors.append("tool_call_id_invalid")
                elif call["id"] != raw["id"]:
                    errors.append("tool_call_id_changed")
            function = raw.get("function", {})
            if not isinstance(function, dict):
                errors.append("tool_function_invalid")
                continue
            if "name" in function:
                name_fragment = function["name"]
                if not isinstance(name_fragment, str):
                    errors.append("stream_name_invalid")
                else:
                    call["function"]["name"] += name_fragment
            if "arguments" in function:
                fragment = function["arguments"]
                if not isinstance(fragment, str):
                    errors.append("stream_arguments_invalid")
                elif fragment:
                    argument_fragment_counts[index] = argument_fragment_counts.get(index, 0) + 1
                    call["function"]["arguments"] += fragment
    for index in calls:
        fragment_count = argument_fragment_counts.get(index, 0)
        if fragment_count == 0:
            errors.append("stream_arguments_missing")
        elif fragment_count < 2:
            errors.append("stream_arguments_not_fragmented")
    if not assistant_role_seen:
        errors.append("assistant_role_missing")
    message = {"role": "assistant", "content": "".join(content), "tool_calls": [calls[index] for index in sorted(calls)]}
    if reasoning:
        message["reasoning_content"] = "".join(reasoning)
    document: dict[str, Any] = {
        "id": completion_id,
        "object": "chat.completion",
        "choices": [{"index": 0, "message": message, "finish_reason": finish_reason}],
    }
    if usage is not None:
        document["usage"] = usage
    turn = _parse_chat_document(document, event_count=len(result.events), invalid_event_count=result.invalid_event_count, terminal=result.done_sentinel or finish_reason is not None)
    turn.parse_errors.extend(errors)
    turn.stream_order_ok = result.stream_order_ok
    turn.deadline_expired = result.deadline_expired
    turn.content_type_ok = result.content_type_ok
    return turn


def _parse_turn(protocol: str, result: TransportResult, *, stream: bool) -> Turn:
    if protocol == "anthropic":
        turn = _parse_anthropic_stream(result) if stream else _parse_anthropic_document(result.document, invalid_event_count=result.invalid_event_count)
    elif protocol == "responses":
        turn = _parse_responses_stream(result) if stream else _parse_responses_document(result.document, invalid_event_count=result.invalid_event_count)
    else:
        turn = _parse_chat_stream(result) if stream else _parse_chat_document(result.document, invalid_event_count=result.invalid_event_count)
    turn.stream_order_ok = result.stream_order_ok
    turn.deadline_expired = result.deadline_expired
    turn.content_type_ok = result.content_type_ok
    return turn


def _validate_first(turn: Turn, expected_tools: tuple[str, ...], *, stream: bool) -> dict[str, Any]:
    ids = [call.call_id for call in turn.tool_calls]
    names = [call.name for call in turn.tool_calls]
    args_ok = all(call.arguments == {"city": "Shanghai"} for call in turn.tool_calls)
    checks = {
        "parsed": not turn.parse_errors and turn.invalid_event_count == 0,
        "terminal": turn.terminal,
        "stop_reason": isinstance(turn.stop_reason, str) and turn.stop_reason in STOP_REASONS[turn.protocol]["first"],
        "expected_tool_count": len(turn.tool_calls) == len(expected_tools),
        "tool_names": sorted(names) == sorted(expected_tools),
        "tool_ids_unique": bool(ids) and len(ids) == len(set(ids)) and all(ids),
        "tool_arguments": args_ok,
        "user_scope": not _token_present(turn.text, USER_SCOPE_LEAK),
        "no_premature_tool_outputs": not _tool_output_tuples(turn.text)
        and not any(_token_present(turn.text, marker) for marker in TOOL_OUTPUTS.values()),
        "reasoning_present": turn.reasoning_present,
        "reasoning_not_visible": not _protected_reasoning_visible(turn.text, turn.reasoning_parts),
        "stream_complete": (not stream) or (turn.event_count > 0 and turn.terminal),
        "stream_order": (not stream) or turn.stream_order_ok,
        "stream_deadline": (not stream) or not turn.deadline_expired,
        "stream_content_type": (not stream) or turn.content_type_ok,
    }
    return {"checks": checks, "stop_reason": turn.stop_reason, "tool_call_count": len(turn.tool_calls), "tool_names": names, "reasoning_present": turn.reasoning_present, "event_count": turn.event_count}


def _validate_second(
    turn: Turn,
    expected_tools: tuple[str, ...],
    *,
    stream: bool,
    expected_calls: list[ToolCall] | None = None,
    prior_reasoning_parts: list[str] | None = None,
) -> dict[str, Any]:
    required_markers = [SYSTEM_MARKER, *(TOOL_OUTPUTS[name] for name in expected_tools)]
    call_output_pairs = (
        sorted(_tool_output_tuples(turn.text)) == sorted(_tool_output_tuple(call) for call in expected_calls)
        if expected_calls is not None
        else all(_token_present(turn.text, marker) for marker in required_markers[1:])
    )
    checks = {
        "parsed": not turn.parse_errors and turn.invalid_event_count == 0,
        "terminal": turn.terminal,
        "stop_reason": isinstance(turn.stop_reason, str) and turn.stop_reason in STOP_REASONS[turn.protocol]["final"],
        "no_followup_tool_calls": not turn.tool_calls,
        "reasoning_present": turn.reasoning_present,
        "reasoning_not_visible": not _protected_reasoning_visible(
            turn.text,
            [*(prior_reasoning_parts or []), *turn.reasoning_parts],
        ),
        "system_marker": _token_present(turn.text, SYSTEM_MARKER),
        "system_scope": _token_present(turn.text, SYSTEM_SCOPE_OK) and not _token_present(turn.text, USER_SCOPE_LEAK),
        "tool_outputs": all(_token_present(turn.text, marker) for marker in required_markers[1:]),
        "tool_output_call_pairs": call_output_pairs,
        "stream_complete": (not stream) or (turn.event_count > 0 and turn.terminal),
        "stream_order": (not stream) or turn.stream_order_ok,
        "stream_deadline": (not stream) or not turn.deadline_expired,
        "stream_content_type": (not stream) or turn.content_type_ok,
        "stream_text_deltas": (not stream) or turn.protocol not in {"responses", "anthropic"} or turn.stream_text_delta_count > 0,
    }
    return {"checks": checks, "stop_reason": turn.stop_reason, "tool_call_count": len(turn.tool_calls), "text_length": len(turn.text), "reasoning_present": turn.reasoning_present, "event_count": turn.event_count}


def _build_payload(spec: CaseSpec, model: str, *, followup: Turn | None = None) -> dict[str, Any]:
    if spec.client_protocol == "anthropic":
        return _anthropic_payload(model=model, stream=spec.stream, parallel=spec.parallel, followup=followup)
    if spec.client_protocol == "responses":
        return _responses_payload(model=model, stream=spec.stream, parallel=spec.parallel, followup=followup)
    return _chat_payload(model=model, stream=spec.stream, parallel=spec.parallel, followup=followup)


def _request_with_retries(spec: CaseSpec, payload: dict[str, Any], model: str) -> tuple[TransportResult, str, bool]:
    """Retry only transient relay capacity failures; never retry other 4xx responses."""
    candidates = [model]
    if spec.target_protocol == "anthropic" and ANTHROPIC_FALLBACK_MODEL and ANTHROPIC_FALLBACK_MODEL != model:
        candidates.append(ANTHROPIC_FALLBACK_MODEL)
    for candidate_index, candidate in enumerate(candidates):
        payload["model"] = candidate
        for retry in range(MAX_503_RETRIES if candidate_index == 0 else 1):
            result = _request(spec.path, payload, client_protocol=spec.client_protocol, stream=spec.stream)
            if result.status != 503:
                return result, candidate, False
            if retry + 1 < (MAX_503_RETRIES if candidate_index == 0 else 1):
                time.sleep(0.5 * (retry + 1))
    return result, candidate, result.status == 503


def _run_case(spec: CaseSpec) -> dict[str, Any]:
    model = MODELS[spec.target_protocol]
    first_result, model_used, blocked = _request_with_retries(spec, _build_payload(spec, model), model)
    fallback_used = model_used != model
    if blocked:
        return {
            "case": spec.name,
            "status": first_result.status,
            "fallback_used": fallback_used,
            "evidence_model_scope": "fallback" if fallback_used else "primary",
            "blocked": "relay Claude pool unavailable" if spec.target_protocol == "anthropic" else "relay upstream capacity unavailable",
            "checks": {},
        }
    first_turn = _parse_turn(spec.client_protocol, first_result, stream=spec.stream)
    first = _validate_first(first_turn, spec.expected_tools, stream=spec.stream)
    first["checks"]["http_success"] = 200 <= first_result.status < 300
    second: dict[str, Any] = {"skipped": True, "checks": {"not_run": False}}
    call_ids = [call.call_id for call in first_turn.tool_calls]
    followup_identity_valid = bool(call_ids) and all(call_ids) and len(set(call_ids)) == len(call_ids)
    if first_turn.tool_calls and not followup_identity_valid:
        second["skip_reason"] = "invalid first-turn tool-call identity"
    if followup_identity_valid:
        second_result, second_model_used, second_blocked = _request_with_retries(spec, _build_payload(spec, model_used, followup=first_turn), model_used)
        if second_model_used != model_used:
            return {
                "case": spec.name,
                "status": first_result.status,
                "second_status": second_result.status,
                "fallback_used": fallback_used,
                "evidence_model_scope": "mixed",
                "blocked": "model changed between turns",
                "first": first,
                "checks": first["checks"],
            }
        fallback_used = fallback_used or second_model_used != model
        if second_blocked:
            return {
                "case": spec.name,
                "status": first_result.status,
                "second_status": second_result.status,
                "fallback_used": fallback_used,
                "evidence_model_scope": "fallback" if fallback_used else "primary",
                "blocked": "relay Claude pool unavailable" if spec.target_protocol == "anthropic" else "relay upstream capacity unavailable",
                "first": first,
                "checks": first["checks"],
            }
        second_turn = _parse_turn(spec.client_protocol, second_result, stream=spec.stream)
        second = _validate_second(
            second_turn,
            spec.expected_tools,
            stream=spec.stream,
            expected_calls=first_turn.tool_calls,
            prior_reasoning_parts=first_turn.reasoning_parts,
        )
        second["checks"]["http_success"] = 200 <= second_result.status < 300
        second["status"] = second_result.status
    checks = dict(first["checks"])
    checks.update({f"second_{key}": value for key, value in second.get("checks", {}).items() if key != "not_run"})
    return {
        "case": spec.name,
        "status": first_result.status,
        "second_status": second.get("status"),
        "fallback_used": fallback_used,
        "evidence_model_scope": "fallback" if fallback_used else "primary",
        "first": first,
        "second": second,
        "checks": checks,
    }


def _build_cases() -> list[CaseSpec]:
    return [
        CaseSpec("messages_to_responses_single", "anthropic", "responses", "/v1/messages", False, False),
        CaseSpec("messages_to_responses_parallel_stream", "anthropic", "responses", "/v1/messages", True, True),
        CaseSpec("responses_to_messages_single", "responses", "anthropic", "/v1/responses", False, False),
        CaseSpec("responses_to_messages_parallel_stream", "responses", "anthropic", "/v1/responses", True, True),
        CaseSpec("messages_to_chat_single", "anthropic", "chat", "/v1/messages", False, False),
        CaseSpec("messages_to_chat_parallel_stream", "anthropic", "chat", "/v1/messages", True, True),
        CaseSpec("chat_to_messages_single", "chat", "anthropic", "/v1/chat/completions", False, False),
        CaseSpec("chat_to_messages_parallel_stream", "chat", "anthropic", "/v1/chat/completions", True, True),
    ]


def _valid_qualified_model(value: str) -> bool:
    return bool(value) and value.count("/") == 1 and not any(char.isspace() for char in value) and ".." not in value and not value.startswith("/") and not value.endswith("/")


def _preflight() -> list[str]:
    missing = [name for name in REQUIRED_VENDOR_KEYS if not os.environ.get(name)]
    missing.extend(f"model:{protocol}" for protocol, value in MODELS.items() if not _valid_qualified_model(value))
    if ANTHROPIC_FALLBACK_MODEL and not _valid_qualified_model(ANTHROPIC_FALLBACK_MODEL):
        missing.append("model:anthropic_fallback")
    try:
        parsed = urllib.parse.urlsplit(BASE_URL)
    except ValueError:
        parsed = None
    loopback = (
        parsed is not None
        and parsed.scheme == "http"
        and parsed.hostname == "127.0.0.1"
        and parsed.username is None
        and parsed.password is None
        and not parsed.query
        and not parsed.fragment
        and parsed.path in {"", "/"}
    )
    if not GATEWAY_TOKEN:
        missing.append("CPA_GATEWAY_TOKEN")
    if not loopback:
        missing.append("CPA_BASE_URL must be exact http://127.0.0.1[:port]")
    return missing


def main() -> int:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--list", action="store_true", help="list case names without making requests")
    args = parser.parse_args()
    if args.list:
        print("\n".join(case.name for case in _build_cases()))
        return 0
    missing = _preflight()
    if missing:
        print(json.dumps({"ok": False, "blocked": True, "missing": missing}, sort_keys=True))
        return 2
    results = [_run_case(case) for case in _build_cases()]
    blocked = any(result.get("blocked") for result in results)
    ok = not blocked and all(all(result["checks"].values()) for result in results)
    print(json.dumps({"ok": ok, "blocked": blocked, "not_verified": [CONTEXT_LENGTH_NOT_VERIFIED], "results": results}, sort_keys=True))
    return 2 if blocked else (0 if ok else 1)


if __name__ == "__main__":
    sys.exit(main())