Use Claude Code, Codex, VSCode, Pi, and OpenCode (and 6 other harnesses) for free (1.3B+ free tokens) from your terminal, app, IDE, or phone, and now from the browser with native browser sessions (multi-harness + multi-model) like OpenClaw (voice supported + ToS friendly)
Python
patch: preserve native tool arguments and remove text tool parsers (#1929)
Ali Khokharcc573ba8bba7
diff --git a/e2e/test_code_sessions.py b/e2e/test_code_sessions.pyindex 550f094c..d31296ae 100644--- a/e2e/test_code_sessions.py+++ b/e2e/test_code_sessions.py@@ -2055,21 +2055,23 @@ def test_library_reconnect_removes_missed_deletion_and_searches_beyond_first_pag):control_feed(page)- async def seed():- first = await code_control.service.create_session(- str(uuid.uuid4()), str(tmp_path)- )- await code_control.service.update_settings(- first.id, first.revision, {"title": "Needle project"}- )- for _ in range(26):- await code_control.service.create_session(str(uuid.uuid4()), str(tmp_path))- return first.id-# Start the isolated service before seeding it.page.goto(f"{admin_base_url}/admin/code")expect(page.locator("#codeNew")).to_be_enabled()- session_id = code_control.run(seed())+ # Bound each operation, not the cumulative cost of filling a whole page.+ first = code_control.run(+ code_control.service.create_session(str(uuid.uuid4()), str(tmp_path))+ )+ code_control.run(+ code_control.service.update_settings(+ first.id, first.revision, {"title": "Needle project"}+ )+ )+ for _ in range(26):+ code_control.run(+ code_control.service.create_session(str(uuid.uuid4()), str(tmp_path))+ )+ session_id = first.idpage.get_by_role("searchbox", name="Search titles and folders").fill("Needle")expect(page.locator(".session-card")).to_have_count(1)expect(page.locator(".session-card")).to_contain_text("Needle project")diff --git a/smoke/capabilities.py b/smoke/capabilities.pyindex 69a1fbbc..a1db5ff7 100644--- a/smoke/capabilities.py+++ b/smoke/capabilities.py@@ -269,13 +269,16 @@ CAPABILITY_CONTRACTS: tuple[CapabilityContract, ...] = (),CapabilityContract("streaming_conversion",- "heuristic_tools",- "heuristic_tool_parser",- "free_claude_code.core.anthropic.tools",- "textual tool-call output",+ "native_tools",+ "native_tool_assembly",+ "free_claude_code.providers.openai_chat.tool_calls",+ "native structured tool-call output","structured Anthropic tool_use blocks",- "text fallback when malformed",- ("tests/providers/test_parsers.py", "tests/contracts/test_stream_contracts.py"),+ "raw arguments preserved for harness validation",+ (+ "tests/providers/test_native_tool_arguments.py",+ "tests/contracts/test_stream_contracts.py",+ ),("test_live_tool_use_when_configured_model_supports_tools","test_provider_reasoning_tool_continuation_e2e",diff --git a/smoke/features.py b/smoke/features.pyindex 1df16014..550d3c38 100644--- a/smoke/features.py+++ b/smoke/features.py@@ -325,9 +325,12 @@ FEATURE_INVENTORY: tuple[FeatureCoverage, ...] = ("configured providers must not reject adaptive thinking payloads",),FeatureCoverage(- "heuristic_tool_parser",+ "native_tool_assembly","Tool use and tool result continuation survive provider/client paths",- ("tests/providers/test_parsers.py", "tests/contracts/test_stream_contracts.py"),+ (+ "tests/providers/test_native_tool_arguments.py",+ "tests/contracts/test_stream_contracts.py",+ ),("test_live_tool_use_when_configured_model_supports_tools",),("test_provider_interleaved_thinking_tool_e2e",diff --git a/src/free_claude_code/core/anthropic/__init__.py b/src/free_claude_code/core/anthropic/__init__.pyindex 9b527926..78c7dbdd 100644--- a/src/free_claude_code/core/anthropic/__init__.py+++ b/src/free_claude_code/core/anthropic/__init__.py@@ -46,7 +46,6 @@ from .streaming import ()from .thinking import ContentChunk, ContentType, ThinkTagParserfrom .tokens import get_token_count-from .tools import FunctionTagToolParser, HeuristicToolParserfrom .utils import set_if_not_none__all__ = [@@ -64,8 +63,6 @@ __all__ = ["ContentBlockWebSearchToolResult","ContentChunk","ContentType",- "FunctionTagToolParser",- "HeuristicToolParser","Message","MessagesRequest","MessagesResponse",diff --git a/src/free_claude_code/core/anthropic/native_stream.py b/src/free_claude_code/core/anthropic/native_stream.pyindex 2ea0a166..11a42726 100644--- a/src/free_claude_code/core/anthropic/native_stream.py+++ b/src/free_claude_code/core/anthropic/native_stream.py@@ -3,7 +3,6 @@import jsonfrom collections.abc import Mappingfrom dataclasses import dataclass, field, replace-from typing import castfrom free_claude_code.core.history_replay import (ReplayOrigin,@@ -32,6 +31,14 @@ class _Block:fragments: dict[str, list[str]] = field(default_factory=dict)+@dataclass(frozen=True, slots=True)+class CompletedMessagesBlock:+ """Completed metadata and opaque streamed tool input, kept off the wire."""++ body: JsonObject+ tool_arguments: str | None = None++class NativeMessagesStreamState:"""Validate one upstream lifecycle without rewriting protocol identities."""@@ -45,7 +52,7 @@ class NativeMessagesStreamState:def accept(self, event_type: str, payload: Mapping[str, JsonValue]- ) -> JsonObject | None:+ ) -> CompletedMessagesBlock | None:if self.completed:raise NativeMessagesError("Messages event arrived after message_stop.")try:@@ -216,21 +223,17 @@ class NativeMessagesStreamState:raise NativeMessagesError("Completed native thinking requires its signature.")- if block.body["type"] == "tool_use" and block.parts:- try:- value = cast(JsonValue, json.loads("".join(block.parts)))- if not isinstance(value, Mapping):- raise NativeMessagesError(- "Native tool arguments must decode to an object."- )- json.dumps(value, ensure_ascii=False, allow_nan=False).encode("utf-8")- except (ValueError, UnicodeError, RecursionError) as exc:- raise NativeMessagesError(- "Native tool arguments are incomplete or invalid JSON."- ) from exc- block.body["input"] = dict(value)+ arguments = None+ if block.body["type"] == "tool_use":+ arguments = (+ "".join(block.parts)+ if block.parts+ else json.dumps(+ block.body["input"], ensure_ascii=False, separators=(",", ":")+ )+ )self._blocks.pop(index)- return block.body+ return CompletedMessagesBlock(block.body, arguments)class NativeMessagesRelay:@@ -287,10 +290,12 @@ class NativeMessagesRelay:return ""if (completed is not None- and completed.get("type") == "thinking"- and completed.get("signature")+ and completed.body.get("type") == "thinking"+ and completed.body.get("signature")):- signature = encode_replay(ReplayRecord(self._replay_origin, completed))+ signature = encode_replay(+ ReplayRecord(self._replay_origin, completed.body)+ )prefix = _event("content_block_delta",{diff --git a/src/free_claude_code/core/anthropic/sse_aggregation.py b/src/free_claude_code/core/anthropic/sse_aggregation.pyindex 58940a1f..18233920 100644--- a/src/free_claude_code/core/anthropic/sse_aggregation.py+++ b/src/free_claude_code/core/anthropic/sse_aggregation.py@@ -12,6 +12,8 @@ import uuidfrom collections.abc import AsyncIteratorfrom typing import Any+from free_claude_code.core.failures import ExecutionFailure, FailureKind+from .stream_contracts import SSEEventfrom .streaming.decoder import AnthropicSSEDecoder@@ -120,14 +122,22 @@ async def aggregate_anthropic_sse_to_message(elif btype == "thinking":block["thinking"] = str(block.get("thinking", "")) + accumulatedblock.setdefault("signature", "")- elif btype == "tool_use":- if accumulated.strip():- try:- block["input"] = json.loads(accumulated)- except json.JSONDecodeError:- block["input"] = block.get("input") or {}- elif not isinstance(block.get("input"), dict):- block["input"] = {}+ elif btype == "tool_use" and error is None:+ try:+ value = (+ json.loads(accumulated) if parts.get(idx) else block.get("input")+ )+ if not isinstance(value, dict):+ raise ValueError("Tool input must be an object.")+ json.dumps(value, ensure_ascii=False, allow_nan=False).encode("utf-8")+ except (ValueError, UnicodeError, RecursionError) as exc:+ raise ExecutionFailure(+ FailureKind.UPSTREAM,+ 502,+ "Provider tool arguments cannot be represented as a Messages input object.",+ False,+ ) from exc+ block["input"] = valuecontent.append(block)message["content"] = contentdiff --git a/src/free_claude_code/core/anthropic/tools.py b/src/free_claude_code/core/anthropic/tools.pydeleted file mode 100644index 3b56ace5..00000000--- a/src/free_claude_code/core/anthropic/tools.py+++ /dev/null@@ -1,493 +0,0 @@-"""Heuristic parser for text-emitted tool calls."""--import json-import re-import uuid-from collections.abc import Mapping-from dataclasses import dataclass-from enum import Enum-from typing import Any--from loguru import logger--from ..openai_tool_names import OpenAIToolNameCodec-from .models import MessagesRequest-from .tool_schema import arguments_match_schema, coerce_text_argument--_CONTROL_TOKEN_RE = re.compile(r"<\|[^|>]{1,80}\|>")-_CONTROL_TOKEN_START = "<|"-_CONTROL_TOKEN_END = "|>"-_FUNCTION_TAG_BLOCK_START = "<tool_call>"-_FUNCTION_TAG_BLOCK_END = "</tool_call>"-_FUNCTION_TAG_START = "<function="-_FUNCTION_TAG_END = "</function>"-_PARAMETER_TAG_START = "<parameter="-_PARAMETER_TAG_END = "</parameter>"-_MAX_FUNCTION_TAG_CANDIDATE_CHARS = 4 * 1024 * 1024---@dataclass(frozen=True, slots=True)-class _RawFunctionTagCall:- name: str- arguments: dict[str, str]---class _FunctionTagState(Enum):- SEARCHING = 1- CANDIDATE = 2- DISABLED = 3- FINISHED = 4---class FunctionTagToolParser:- """Parse an exact terminal function-tag envelope into tool use."""-- def __init__(self, request: MessagesRequest):- schemas: dict[str, dict[str, Any]] = {}- for tool in request.tools or ():- if tool.name:- schemas[tool.name] = (- tool.input_schema- if tool.input_schema is not None- else {"type": "object"}- )- tool_choice = request.tool_choice- tool_choice_type = (- tool_choice.get("type") if isinstance(tool_choice, dict) else None- )- self._initialize(- tool_names=OpenAIToolNameCodec.from_request(request),- schemas=schemas,- enabled=tool_choice_type != "none",- )-- @classmethod- def from_schemas(- cls,- *,- tool_names: OpenAIToolNameCodec,- schemas: Mapping[str, Mapping[str, Any]],- enabled: bool,- ) -> FunctionTagToolParser:- """Build the parser from a protocol-neutral Chat tool contract."""- parser = cls.__new__(cls)- parser._initialize(- tool_names=tool_names,- schemas={name: dict(schema) for name, schema in schemas.items()},- enabled=enabled,- )- return parser-- def _initialize(- self,- *,- tool_names: OpenAIToolNameCodec,- schemas: dict[str, dict[str, Any]],- enabled: bool,- ) -> None:- self._tool_names = tool_names- self._schemas = schemas- self._state = (- _FunctionTagState.SEARCHING- if self._schemas and enabled- else _FunctionTagState.DISABLED- )- self._parts: list[str] = []- self._length = 0- self._marker_tail = ""-- def feed(self, text: str) -> str:- """Hold a possible reserved response and return text safe to expose."""- if not text:- return ""- if self._state in {_FunctionTagState.DISABLED, _FunctionTagState.FINISHED}:- return text-- if self._state is _FunctionTagState.CANDIDATE:- self._parts.append(text)- self._length += len(text)- if self._length > _MAX_FUNCTION_TAG_CANDIDATE_CHARS:- return self.disable()- return ""-- candidate = "".join((self._marker_tail, text))- marker_index = candidate.find(_FUNCTION_TAG_BLOCK_START)- if marker_index >= 0:- visible = candidate[:marker_index]- control = candidate[marker_index:]- self._state = _FunctionTagState.CANDIDATE- self._marker_tail = ""- self._parts.append(control)- self._length = len(control)- if self._length > _MAX_FUNCTION_TAG_CANDIDATE_CHARS:- return "".join((visible, self.disable()))- return visible-- held_length = _partial_function_tag_marker_suffix_length(candidate)- if held_length:- self._marker_tail = candidate[-held_length:]- return candidate[:-held_length]- self._marker_tail = ""- return candidate-- def disable(self) -> str:- """Disable textual recovery and release any held candidate unchanged."""- if self._state in {_FunctionTagState.DISABLED, _FunctionTagState.FINISHED}:- return ""- self._state = _FunctionTagState.DISABLED- text = "".join((self._marker_tail, *self._parts))- self._marker_tail = ""- self._parts.clear()- self._length = 0- return text-- def finish(self) -> tuple[str, tuple[dict[str, Any], ...]]:- """Finalize one response atomically as visible text or validated tools."""- if self._state in {_FunctionTagState.DISABLED, _FunctionTagState.FINISHED}:- return "", ()- if self._state is _FunctionTagState.SEARCHING:- self._state = _FunctionTagState.FINISHED- text = self._marker_tail- self._marker_tail = ""- return text, ()- self._state = _FunctionTagState.FINISHED- raw = "".join(self._parts)- self._parts.clear()- self._length = 0- try:- calls = _parse_function_tag_calls(raw)- tool_uses = self._validated_tool_uses(calls)- except ValueError:- return raw, ()- return "", tool_uses-- def _validated_tool_uses(- self, calls: tuple[_RawFunctionTagCall, ...]- ) -> tuple[dict[str, Any], ...]:- tool_uses: list[dict[str, Any]] = []- for call in calls:- name = self._tool_names.decode(call.name)- schema = self._schemas.get(name)- if schema is None:- raise ValueError- properties = schema.get("properties")- property_schemas = properties if isinstance(properties, Mapping) else {}- additional = schema.get("additionalProperties")- arguments: dict[str, Any] = {}- for parameter_name, value in call.arguments.items():- parameter_schema = property_schemas.get(parameter_name)- if not isinstance(parameter_schema, Mapping):- parameter_schema = (- additional if isinstance(additional, Mapping) else {}- )- arguments[parameter_name] = coerce_text_argument(- value,- parameter_schema,- )- if not arguments_match_schema(arguments, schema):- raise ValueError- tool_uses.append(- {- "type": "tool_use",- "id": f"toolu_function_tag_{uuid.uuid4().hex[:8]}",- "name": name,- "input": arguments,- }- )- return tuple(tool_uses)---def _parse_function_tag_calls(text: str) -> tuple[_RawFunctionTagCall, ...]:- cursor = 0- calls: list[_RawFunctionTagCall] = []- while True:- cursor = _skip_function_tag_whitespace(text, cursor)- if cursor == len(text):- break- if not text.startswith(_FUNCTION_TAG_BLOCK_START, cursor):- raise ValueError-- block_start = cursor + len(_FUNCTION_TAG_BLOCK_START)- block_end = text.find(_FUNCTION_TAG_BLOCK_END, block_start)- if block_end < 0:- raise ValueError- name, arguments = _parse_function_tag_block(text[block_start:block_end])- calls.append(_RawFunctionTagCall(name=name, arguments=arguments))- cursor = block_end + len(_FUNCTION_TAG_BLOCK_END)-- if not calls:- raise ValueError- return tuple(calls)---def _parse_function_tag_block(block: str) -> tuple[str, dict[str, str]]:- cursor = _skip_function_tag_whitespace(block, 0)- if not block.startswith(_FUNCTION_TAG_START, cursor):- raise ValueError- name_end = block.find(">", cursor + len(_FUNCTION_TAG_START))- if name_end < 0:- raise ValueError- name = block[cursor + len(_FUNCTION_TAG_START) : name_end]- if not _valid_function_tag_name(name):- raise ValueError-- arguments: dict[str, str] = {}- cursor = name_end + 1- while True:- cursor = _skip_function_tag_whitespace(block, cursor)- if block.startswith(_FUNCTION_TAG_END, cursor):- cursor += len(_FUNCTION_TAG_END)- break- if cursor == len(block) or not block.startswith(_PARAMETER_TAG_START, cursor):- raise ValueError-- parameter_name_end = block.find(">", cursor + len(_PARAMETER_TAG_START))- if parameter_name_end < 0:- raise ValueError- parameter_name = block[cursor + len(_PARAMETER_TAG_START) : parameter_name_end]- if not _valid_function_tag_name(parameter_name) or parameter_name in arguments:- raise ValueError-- value_start = parameter_name_end + 1- value_end = block.find(_PARAMETER_TAG_END, value_start)- if value_end < 0:- raise ValueError- arguments[parameter_name] = _unwrap_function_tag_newlines(- block[value_start:value_end]- )- cursor = value_end + len(_PARAMETER_TAG_END)-- if block[cursor:].strip():- raise ValueError- return name, arguments---def _skip_function_tag_whitespace(text: str, cursor: int) -> int:- while cursor < len(text) and text[cursor].isspace():- cursor += 1- return cursor---def _valid_function_tag_name(value: str) -> bool:- return bool(value) and not any(- character.isspace() or character in "<>" for character in value- )---def _unwrap_function_tag_newlines(value: str) -> str:- if value.startswith("\r\n"):- value = value[2:]- elif value.startswith("\n"):- value = value[1:]- if value.endswith("\r\n"):- return value[:-2]- if value.endswith("\n"):- return value[:-1]- return value---def _partial_function_tag_marker_suffix_length(text: str) -> int:- max_length = min(len(text), len(_FUNCTION_TAG_BLOCK_START) - 1)- for length in range(max_length, 0, -1):- if _FUNCTION_TAG_BLOCK_START.startswith(text[-length:]):- return length- return 0---class ParserState(Enum):- TEXT = 1- MATCHING_FUNCTION = 2- PARSING_PARAMETERS = 3---class HeuristicToolParser:- """- Stateful parser for raw text tool calls.-- Some OpenAI-compatible models emit tool calls as text rather than structured- chunks. This parser converts the common ``● <function=...>`` form into- Anthropic-style ``tool_use`` blocks.- """-- _FUNC_START_PATTERN = re.compile(r"●\s*<function=([^>]+)>")- _PARAM_PATTERN = re.compile(- r"<parameter=([^>]+)>(.*?)(?:</parameter>|$)", re.DOTALL- )- _WEB_TOOL_JSON_PATTERN = re.compile(- r"(?is)\b(?:use\s+)?(?P<tool>WebFetch|WebSearch)\b.*?(?P<json>\{.*?\})"- )-- def __init__(self):- self._state = ParserState.TEXT- self._buffer = ""- self._current_tool_id = None- self._current_function_name = None- self._current_parameters = {}-- def _extract_web_tool_json_calls(self) -> tuple[str, list[dict[str, Any]]]:- detected_tools: list[dict[str, Any]] = []-- for match in self._WEB_TOOL_JSON_PATTERN.finditer(self._buffer):- try:- tool_input = json.loads(match.group("json"))- except json.JSONDecodeError:- continue- if not isinstance(tool_input, dict):- continue-- tool_name = match.group("tool")- if tool_name == "WebFetch" and "url" not in tool_input:- continue- if tool_name == "WebSearch" and "query" not in tool_input:- continue-- detected_tools.append(- {- "type": "tool_use",- "id": f"toolu_heuristic_{uuid.uuid4().hex[:8]}",- "name": tool_name,- "input": tool_input,- }- )- logger.debug(- "Heuristic bypass: Detected JSON-style tool call '{}'",- tool_name,- )-- if not detected_tools:- return self._buffer, []-- return "", detected_tools-- def _strip_control_tokens(self, text: str) -> str:- return _CONTROL_TOKEN_RE.sub("", text)-- def _split_incomplete_control_token_tail(self) -> str:- start = self._buffer.rfind(_CONTROL_TOKEN_START)- if start == -1:- return ""- end = self._buffer.find(_CONTROL_TOKEN_END, start)- if end != -1:- return ""-- prefix = self._buffer[:start]- self._buffer = self._buffer[start:]- return prefix-- def feed(self, text: str) -> tuple[str, list[dict[str, Any]]]:- """Feed text and return safe text plus detected tool calls."""- self._buffer += text- self._buffer = self._strip_control_tokens(self._buffer)- self._buffer, detected_tools = self._extract_web_tool_json_calls()- filtered_output_parts: list[str] = []-- while True:- if self._state == ParserState.TEXT:- if "●" in self._buffer:- idx = self._buffer.find("●")- filtered_output_parts.append(self._buffer[:idx])- self._buffer = self._buffer[idx:]- self._state = ParserState.MATCHING_FUNCTION- else:- safe_prefix = self._split_incomplete_control_token_tail()- if safe_prefix:- filtered_output_parts.append(safe_prefix)- break-- filtered_output_parts.append(self._buffer)- self._buffer = ""- break-- if self._state == ParserState.MATCHING_FUNCTION:- match = self._FUNC_START_PATTERN.search(self._buffer)- if match:- self._current_function_name = match.group(1).strip()- self._current_tool_id = f"toolu_heuristic_{uuid.uuid4().hex[:8]}"- self._current_parameters = {}- self._buffer = self._buffer[match.end() :]- self._state = ParserState.PARSING_PARAMETERS- logger.debug(- "Heuristic bypass: Detected start of tool call '{}'",- self._current_function_name,- )- elif len(self._buffer) > 100:- filtered_output_parts.append(self._buffer[0])- self._buffer = self._buffer[1:]- self._state = ParserState.TEXT- else:- break-- if self._state == ParserState.PARSING_PARAMETERS:- finished_tool_call = False-- while True:- param_match = self._PARAM_PATTERN.search(self._buffer)- if param_match and "</parameter>" in param_match.group(0):- pre_match_text = self._buffer[: param_match.start()]- if pre_match_text:- filtered_output_parts.append(pre_match_text)-- key = param_match.group(1).strip()- val = param_match.group(2).strip()- self._current_parameters[key] = val- self._buffer = self._buffer[param_match.end() :]- else:- break-- if "●" in self._buffer:- idx = self._buffer.find("●")- if idx > 0:- filtered_output_parts.append(self._buffer[:idx])- self._buffer = self._buffer[idx:]- finished_tool_call = True- elif len(self._buffer) > 0 and not self._buffer.strip().startswith("<"):- if "<parameter=" not in self._buffer:- filtered_output_parts.append(self._buffer)- self._buffer = ""- finished_tool_call = True-- if finished_tool_call:- detected_tools.append(- {- "type": "tool_use",- "id": self._current_tool_id,- "name": self._current_function_name,- "input": self._current_parameters,- }- )- logger.debug(- "Heuristic bypass: Emitting tool call '{}' with {} params",- self._current_function_name,- len(self._current_parameters),- )- self._state = ParserState.TEXT- else:- break-- return "".join(filtered_output_parts), detected_tools-- def flush(self) -> list[dict[str, Any]]:- """Flush any remaining tool call in the buffer."""- self._buffer = self._strip_control_tokens(self._buffer)- detected_tools = []- if self._state == ParserState.PARSING_PARAMETERS:- partial_matches = re.finditer(- r"<parameter=([^>]+)>(.*)$", self._buffer, re.DOTALL- )- for match in partial_matches:- key = match.group(1).strip()- val = match.group(2).strip()- self._current_parameters[key] = val-- detected_tools.append(- {- "type": "tool_use",- "id": self._current_tool_id,- "name": self._current_function_name,- "input": self._current_parameters,- }- )- self._state = ParserState.TEXT- self._buffer = ""-- return detected_toolsdiff --git a/src/free_claude_code/core/history_replay.py b/src/free_claude_code/core/history_replay.pyindex 5e8c298f..7543ff52 100644--- a/src/free_claude_code/core/history_replay.py+++ b/src/free_claude_code/core/history_replay.py@@ -2,7 +2,7 @@import base64import json-from collections.abc import Mapping+from collections.abc import Mapping, Sequencefrom copy import deepcopyfrom dataclasses import asdict, dataclassfrom enum import StrEnum@@ -13,6 +13,7 @@ from .openai_chat import ChatToolResultImagestype HistoryProtocol = Literal["responses", "messages", "chat"]_PREFIX = "fcc:history:v1:"+_ASSOCIATED_PREFIX = "fcc:history:v2:"_LEGACY_PREFIX = "fcc:anthropic-reasoning:v1:"@@ -52,6 +53,12 @@ class ReplayRecord:native: JsonObject+@dataclass(frozen=True, slots=True)+class AssociatedReplayRecord(ReplayRecord):+ group_id: str+ part: Literal["anchor", "final"]++def _json_copy(value: object) -> JsonObject:if not isinstance(value, Mapping):raise HistoryReplayError("History replay must contain a native object.")@@ -86,15 +93,22 @@ def is_replay(value: object) -> bool:def encode_replay(record: ReplayRecord) -> str:"""Preserve native state, without claiming to encrypt or authenticate it."""payload = _json_copy({"origin": asdict(record.origin), "native": record.native})- _validate_record(payload)+ associated = isinstance(record, AssociatedReplayRecord)+ if associated:+ payload.update(group_id=record.group_id, part=record.part)+ _validate_record(payload, associated=associated)raw = json.dumps(payload, ensure_ascii=False, separators=(",", ":")).encode("utf-8")- return _PREFIX + base64.urlsafe_b64encode(raw).decode("ascii").rstrip("=")+ prefix = _ASSOCIATED_PREFIX if associated else _PREFIX+ return prefix + base64.urlsafe_b64encode(raw).decode("ascii").rstrip("=")-def _validate_record(payload: JsonObject) -> ReplayRecord:+def _validate_record(payload: JsonObject, *, associated: bool = False) -> ReplayRecord:origin, native = payload.get("origin"), payload.get("native")+ fields = (+ {"origin", "native", "group_id", "part"} if associated else {"origin", "native"}+ )if (- set(payload) != {"origin", "native"}+ set(payload) != fieldsor not isinstance(origin, dict)or set(origin) != {"provider", "protocol", "endpoint", "connection", "model"}or not all(isinstance(value, str) for value in origin.values())@@ -129,7 +143,7 @@ def _validate_record(payload: JsonObject) -> ReplayRecord:)if protocol == "chat" and not isinstance(native.get("reasoning_details"), list):raise HistoryReplayError("Chat replay requires structured reasoning details.")- return ReplayRecord(+ record = ReplayRecord(ReplayOrigin(str(origin["provider"]),protocol,@@ -139,11 +153,32 @@ def _validate_record(payload: JsonObject) -> ReplayRecord:),native,)+ if associated:+ group_id, part = payload.get("group_id"), payload.get("part")+ if (+ protocol != "chat"+ or not isinstance(group_id, str)+ or not group_id+ or not isinstance(part, str)+ or part not in {"anchor", "final"}+ ):+ raise HistoryReplayError("Invalid reasoning replay association.")+ return AssociatedReplayRecord(+ record.origin,+ record.native,+ group_id,+ part,+ )+ return recorddef decode_replay(value: str) -> ReplayRecord:prefix = next(- (prefix for prefix in (_PREFIX, _LEGACY_PREFIX) if value.startswith(prefix)),+ (+ prefix+ for prefix in (_PREFIX, _ASSOCIATED_PREFIX, _LEGACY_PREFIX)+ if value.startswith(prefix)+ ),None,)if prefix is None:@@ -175,7 +210,54 @@ def decode_replay(value: str) -> ReplayRecord:},"native": payload["block"],}- return _validate_record(payload)+ return _validate_record(payload, associated=prefix == _ASSOCIATED_PREFIX)+++def resolve_messages_replay(content: Sequence[JsonValue]) -> list[JsonValue]:+ """Join late metadata within one assistant message on a fresh copy."""+ blocks = deepcopy(list(content))+ groups: dict[str, dict[str, tuple[int, str, AssociatedReplayRecord]]] = {}+ for index, block in enumerate(blocks):+ if not isinstance(block, dict):+ continue+ key = (+ "signature"+ if block.get("type") == "thinking"+ else "data"+ if block.get("type") == "redacted_thinking"+ else None+ )+ if key is None or not is_replay(value := block.get(key)):+ continue+ record = decode_replay(cast(str, value))+ if not isinstance(record, AssociatedReplayRecord):+ continue+ group = groups.setdefault(record.group_id, {})+ if record.part in group or any(+ previous.origin != record.origin for _, _, previous in group.values()+ ):+ raise HistoryReplayError("Conflicting reasoning replay association.")+ if record.part == "final" and key != "data":+ raise HistoryReplayError(+ "Final reasoning replay requires a redacted carrier."+ )+ group[record.part] = (index, key, record)+ removed: set[int] = set()+ for group in groups.values():+ anchor, final = group.get("anchor"), group.get("final")+ if anchor and final and anchor[0] >= final[0]:+ raise HistoryReplayError("Reasoning replay final precedes its anchor.")+ target = anchor or final+ assert target is not None+ index, key, record = target+ if final:+ record = final[2]+ block = blocks[index]+ assert isinstance(block, dict)+ block[key] = encode_replay(ReplayRecord(record.origin, record.native))+ if anchor and final:+ removed.add(final[0])+ return [block for index, block in enumerate(blocks) if index not in removed]def readable_reasoning(item: Mapping[str, JsonValue]) -> list[tuple[str, bool]]:diff --git a/src/free_claude_code/core/openai_responses/__init__.py b/src/free_claude_code/core/openai_responses/__init__.pyindex 32b65c15..b879fa9a 100644--- a/src/free_claude_code/core/openai_responses/__init__.py+++ b/src/free_claude_code/core/openai_responses/__init__.py@@ -32,7 +32,6 @@ from .streaming.completion import (reasoning_output_item,tool_item,)-from .streaming.error_mapping import replay_unsafe_function_call_errorfrom .streaming.event_builders import ResponseEventBuilderfrom .streaming.ledger import ResponsesOutputLedgerfrom .tokens import estimate_responses_input_tokens@@ -82,7 +81,6 @@ __all__ = ["openai_error_type_for_failure","openai_failure_payload","reasoning_output_item",- "replay_unsafe_function_call_error","responses_reasoning_config","responses_reasoning_policy","responses_stream_failure_from_event",diff --git a/src/free_claude_code/core/openai_responses/chat_request.py b/src/free_claude_code/core/openai_responses/chat_request.pyindex 26814935..40bfc71a 100644--- a/src/free_claude_code/core/openai_responses/chat_request.py+++ b/src/free_claude_code/core/openai_responses/chat_request.py@@ -23,7 +23,6 @@ from free_claude_code.core.openai_chat import (image_tool_result_label,)from free_claude_code.core.openai_tool_names import OpenAIToolNameCodec-from free_claude_code.core.trace import trace_eventfrom .errors import ResponsesConversionErrorfrom .models import OpenAIResponsesRequest@@ -35,7 +34,6 @@ from .tool_adaptation import ResponsesToolAdapter, ResponsesToolPolicyfrom .tools import (call_id_from_item,optional_str,- parse_arguments,required_str,)@@ -107,7 +105,6 @@ class _ResponsesChatInputBuilder:self._structured_reasoning_details = structured_reasoning_detailsself._pending_reasoning = _PendingReasoning()self._pending_rich_output_parts: list[dict[str, object]] = []- self._quarantined_call_ids: set[str] = set()self._discovery_outputs: list[dict[str, object]] = []def add(self, item: JsonValue, *, source_type: str | None = None) -> None:@@ -208,19 +205,6 @@ class _ResponsesChatInputBuilder:call_id = call_id_from_item(item)name = required_str(item.get("name"), "function_call.name")raw_arguments = item.get("arguments")- try:- parse_arguments(raw_arguments)- except ResponsesConversionError as exc:- self._quarantined_call_ids.add(call_id)- self._pending_reasoning.take()- trace_event(- stage="responses",- event="responses.input.function_call_quarantined",- source="openai_responses",- call_id=call_id,- error_type=type(exc).__name__,- )- returnarguments = _arguments_text(raw_arguments)message: dict[str, object] | None = self._last_tool_call_message()@@ -249,8 +233,6 @@ class _ResponsesChatInputBuilder:) -> None:function = source_type != "custom_tool_call_output"call_id = call_id_from_item(item)- if function and call_id in self._quarantined_call_ids:- returnif not self._pending_reasoning.empty:previous = self._last_tool_call_message()if previous is not None:diff --git a/src/free_claude_code/core/openai_responses/messages_stream.py b/src/free_claude_code/core/openai_responses/messages_stream.pyindex d06b86de..cdb22c74 100644--- a/src/free_claude_code/core/openai_responses/messages_stream.py+++ b/src/free_claude_code/core/openai_responses/messages_stream.py@@ -8,7 +8,10 @@ from dataclasses import replacefrom typing import castfrom free_claude_code.core.anthropic.native import NativeMessagesError-from free_claude_code.core.anthropic.native_stream import NativeMessagesStreamState+from free_claude_code.core.anthropic.native_stream import (+ CompletedMessagesBlock,+ NativeMessagesStreamState,+)from free_claude_code.core.failures import ExecutionFailurefrom free_claude_code.core.history_replay import (ReplayOrigin,@@ -17,7 +20,7 @@ from free_claude_code.core.history_replay import ()from free_claude_code.core.json_types import JsonObject, JsonValue-from .errors import ResponsesConversionError, openai_error_from_failure+from .errors import openai_error_from_failurefrom .ids import (new_message_item_id,new_reasoning_item_id,@@ -129,7 +132,6 @@ class AnthropicToResponsesStream:self._completer = ResponseBlockCompleter(self._ledger,events=self._events,- on_invalid_function_call=self._invalid_function,)@property@@ -336,13 +338,13 @@ class AnthropicToResponsesStream:if isinstance(state, ReasoningBlockState) and kind == "signature_delta":return []if isinstance(state, ToolBlockState) and kind == "input_json_delta":- # The completer emits validated arguments once, after native block stop.+ # The completer emits the accumulated arguments once, at block stop.return []raise NativeMessagesError("Responses cannot represent the native content delta.")- def _finish_block(self, index: int, block: JsonObject) -> list[str]:+ def _finish_block(self, index: int, completed: CompletedMessagesBlock) -> list[str]:state = self._ledger.active_block(index)if state is None:raise NativeMessagesError(@@ -350,32 +352,32 @@ class AnthropicToResponsesStream:)if isinstance(state, ReasoningBlockState):state.encrypted_content = encode_replay(- ReplayRecord(self._replay_origin, block)+ ReplayRecord(self._replay_origin, completed.body))elif isinstance(state, ToolBlockState):- arguments = block.get("input")- if not isinstance(arguments, Mapping):- raise NativeMessagesError("Native tool input must be an object.")- if state.kind == "custom" and (- set(arguments) != {"input"}- or not isinstance(arguments.get("input"), str)- ):- raise NativeMessagesError(- "Native custom tool input must contain exactly one text input."- )- state.argument_parts.append(- json.dumps(arguments, ensure_ascii=False, separators=(",", ":"))- )+ arguments = completed.tool_arguments+ if arguments is None:+ raise AssertionError("Completed native tool must retain its arguments.")+ if state.kind == "custom":+ try:+ wrapper = json.loads(arguments)+ except (ValueError, RecursionError) as exc:+ raise NativeMessagesError(+ "Invalid native custom tool wrapper."+ ) from exc+ if (+ not isinstance(wrapper, dict)+ or set(wrapper) != {"input"}+ or not isinstance(wrapper["input"], str)+ ):+ raise NativeMessagesError(+ "Native custom tool input must contain exactly one text input."+ )+ state.argument_parts.append(arguments)events = self._completer.complete_block(state)self._ledger.pop_active_block(index)return events- @staticmethod- def _invalid_function(- _state: ToolBlockState, error: ResponsesConversionError- ) -> list[str]:- raise NativeMessagesError("Native tool arguments are invalid.") from error-def terminal_failure(self, failure: ExecutionFailure) -> list[str]:"""Terminate once without completing partial tools or inventing arguments."""diff --git a/src/free_claude_code/core/openai_responses/streaming/completion.py b/src/free_claude_code/core/openai_responses/streaming/completion.pyindex bb19d191..658b0e3b 100644--- a/src/free_claude_code/core/openai_responses/streaming/completion.py+++ b/src/free_claude_code/core/openai_responses/streaming/completion.py@@ -1,21 +1,13 @@"""Block finalization for OpenAI Responses streams."""-from collections.abc import Callable--from ..errors import ResponsesConversionErrorfrom ..items import encrypted_reasoning_item, message_item, reasoning_itemfrom ..tools import (custom_tool_input_text_from_arguments,- normalized_function_call_arguments,)from .blocks import BlockState, ReasoningBlockState, TextBlockState, ToolBlockStatefrom .event_builders import ResponseEventBuilderfrom .ledger import ResponsesOutputLedger-InvalidFunctionCallHandler = Callable[- [ToolBlockState, ResponsesConversionError], list[str]-]-class ResponseBlockCompleter:"""Finalize active Responses output blocks."""@@ -25,13 +17,9 @@ class ResponseBlockCompleter:ledger: ResponsesOutputLedger,*,events: ResponseEventBuilder,- on_invalid_function_call: InvalidFunctionCallHandler,- prepare_tool_arguments: Callable[[str, str], str] | None = None,) -> None:self._ledger = ledgerself._events = events- self._on_invalid_function_call = on_invalid_function_call- self._prepare_tool_arguments = prepare_tool_argumentsdef complete_block(self, state: BlockState) -> list[str]:if isinstance(state, TextBlockState):@@ -68,17 +56,8 @@ class ResponseBlockCompleter:def _complete_tool_block(self, state: ToolBlockState) -> list[str]:if state.kind == "custom":return self._complete_custom_tool_block(state)- raw_arguments = "".join(state.argument_parts)- try:- arguments = (- self._prepare_tool_arguments(state.name, raw_arguments)- if self._prepare_tool_arguments is not None- else normalized_function_call_arguments(raw_arguments or "{}")- )- except ResponsesConversionError as exc:- return self._on_invalid_function_call(state, exc)+ arguments = "".join(state.argument_parts)item = tool_item(state, status="completed", arguments=arguments)- self._ledger.commit_output(state.output_index, item)chunks: list[str] = []if arguments:chunks.append(@@ -94,6 +73,7 @@ class ResponseBlockCompleter:self._events.output_item_done(state.output_index, item),])+ self._ledger.commit_output(state.output_index, item)return chunksdef _complete_custom_tool_block(self, state: ToolBlockState) -> list[str]:diff --git a/src/free_claude_code/core/openai_responses/streaming/error_mapping.py b/src/free_claude_code/core/openai_responses/streaming/error_mapping.pyindex ad5878b9..619401b8 100644--- a/src/free_claude_code/core/openai_responses/streaming/error_mapping.py+++ b/src/free_claude_code/core/openai_responses/streaming/error_mapping.py@@ -14,15 +14,3 @@ def openai_error_from_anthropic_error(data: Mapping[str, Any]) -> dict[str, Any]"param": None,"code": None,}---def replay_unsafe_function_call_error() -> dict[str, Any]:- return {- "message": (- "Upstream function_call arguments were not a valid JSON object; "- "refusing to emit replay-unsafe Responses output."- ),- "type": "api_error",- "param": None,- "code": None,- }diff --git a/src/free_claude_code/core/openai_responses/tool_adaptation.py b/src/free_claude_code/core/openai_responses/tool_adaptation.pyindex 0c5b92a8..5a7ef66a 100644--- a/src/free_claude_code/core/openai_responses/tool_adaptation.py+++ b/src/free_claude_code/core/openai_responses/tool_adaptation.py@@ -416,35 +416,6 @@ class ResponsesToolAdapter:raise ResponsesConversionError("Ambiguous tool name returned by provider.")return next(iter(candidates)) if len(candidates) == 1 else None- def prepare_arguments(- self, name: str, arguments: str, *, namespace: str | None = None- ) -> str:- """Validate provider arguments before publishing a completed tool call."""- identity = self._identity({"name": name, "namespace": namespace})- if identity is not None and identity.kind == "custom":- return json.dumps(- {"input": custom_tool_input_text_from_arguments(arguments)},- ensure_ascii=False,- separators=(",", ":"),- )- try:- parsed = json.loads(- arguments,- parse_float=_canonical_number,- parse_constant=_reject_json_constant,- )- except ValueError as exc:- raise ResponsesConversionError("Invalid tool call arguments.") from exc- if not isinstance(parsed, dict):- raise ResponsesConversionError("Tool call arguments must be a JSON object.")- return simplejson.dumps(- parsed,- ensure_ascii=False,- separators=(",", ":"),- allow_nan=False,- use_decimal=True,- )-def restore_item(self, value: JsonValue) -> JsonValue:if not isinstance(value, dict):return value@@ -456,18 +427,6 @@ class ResponsesToolAdapter:if value.get("type") != "function_call":return valueidentity = self._identity(value)- if value.get("status") == "completed" and (- self._policy.client_tool_search or self._policy.flatten_namespaces- ):- raw = value.get("arguments")- value = {- **value,- "arguments": self.prepare_arguments(- _name(value),- raw if isinstance(raw, str) else "",- namespace=_namespace(value),- ),- }if self._is_search(value):arguments = value.get("arguments")if not is_unfinished_search_call(value):@@ -662,7 +621,6 @@ class ResponsesToolEventAdapter:self._tools = toolsself._custom_items: set[str] = set()self._search_items: set[str] = set()- self._function_items: set[str] = set()self._sequence = 0def feed(@@ -676,34 +634,6 @@ class ResponsesToolEventAdapter:item = self._tools.restore_item(original_item)if isinstance(item, dict):data["item"] = item- if item.get("type") == "function_call" and (- self._tools._policy.client_tool_search- or self._tools._policy.flatten_namespaces- ):- if isinstance(item_id := item.get("id"), str):- self._function_items.add(item_id)- if event_type == "response.output_item.added":- item["arguments"] = ""- if (- event_type == "response.output_item.done"- and item.get("status") == "completed"- ):- coordinates = {- "item_id": item.get("id"),- "output_index": data.get("output_index"),- }- arguments = item.get("arguments", "")- yield self._emit(- "response.function_call_arguments.delta",- {**coordinates, "delta": arguments},- )- identity = {- key: item[key] for key in ("name", "namespace") if key in item- }- yield self._emit(- "response.function_call_arguments.done",- {**coordinates, **identity, "arguments": arguments},- )if item.get("type") == "tool_search_call":if isinstance(item_id := item.get("id"), str):self._search_items.add(item_id)@@ -742,8 +672,7 @@ class ResponsesToolEventAdapter:"response.function_call_arguments.delta","response.function_call_arguments.done",}- and data.get("item_id")- in self._custom_items | self._search_items | self._function_items+ and data.get("item_id") in self._custom_items | self._search_items):returnif (diff --git a/src/free_claude_code/core/openai_responses/tools.py b/src/free_claude_code/core/openai_responses/tools.pyindex 73752837..2f19b760 100644--- a/src/free_claude_code/core/openai_responses/tools.py+++ b/src/free_claude_code/core/openai_responses/tools.py@@ -41,30 +41,6 @@ def flatten_responses_tool_name(name: str, *, namespace: str | None = None) -> sreturn f"{combined[:prefix_len]}_{digest}"-def parse_arguments(value: Any) -> dict[str, Any]:- if value is None or value == "":- return {}- if isinstance(value, dict):- return value- if not isinstance(value, str):- raise ResponsesConversionError("Responses function_call arguments must be JSON")- try:- parsed = json.loads(value)- except json.JSONDecodeError as exc:- raise ResponsesConversionError(- f"Responses function_call arguments are invalid JSON: {exc.msg}"- ) from exc- if not isinstance(parsed, dict):- raise ResponsesConversionError(- "Responses function_call arguments must decode to an object"- )- return parsed---def normalized_function_call_arguments(value: Any) -> str:- return json.dumps(parse_arguments(value), separators=(",", ":"))--def custom_tool_input_text(value: Any) -> str:if value is None:return ""diff --git a/src/free_claude_code/providers/anthropic_messages/transport.py b/src/free_claude_code/providers/anthropic_messages/transport.pyindex 7710ac7d..b19207bc 100644--- a/src/free_claude_code/providers/anthropic_messages/transport.py+++ b/src/free_claude_code/providers/anthropic_messages/transport.py@@ -58,6 +58,7 @@ from free_claude_code.providers.failure_policy import (is_retryable_stream_error,)from free_claude_code.providers.history_replay import (+ normalize_messages_history,replay_origin,validate_history,)@@ -121,7 +122,7 @@ class AnthropicMessagesTransport:capabilities: MessagesModelCapabilities,preserve_native_controls: bool,) -> PreparedMessagesRequest:- validate_history(request.model_dump(mode="json"))+ request = normalize_messages_history(request)try:options = resolve_messages_options(model=request.model,diff --git a/src/free_claude_code/providers/history_replay.py b/src/free_claude_code/providers/history_replay.pyindex b462a36f..224c3f4a 100644--- a/src/free_claude_code/providers/history_replay.py+++ b/src/free_claude_code/providers/history_replay.py@@ -5,10 +5,11 @@ import jsonimport refrom collections.abc import Iterator, Mappingfrom copy import deepcopy-from typing import TYPE_CHECKING, Any+from typing import TYPE_CHECKING, Any, castfrom urllib.parse import urlsplit, urlunsplitfrom free_claude_code.application.errors import InvalidRequestError+from free_claude_code.core.anthropic.models import Message, MessagesRequestfrom free_claude_code.core.diagnostics import extract_upstream_error_detailfrom free_claude_code.core.history_replay import (HistoryProtocol,@@ -18,7 +19,9 @@ from free_claude_code.core.history_replay import (is_replay,readable_reasoning,reasoning_context,+ resolve_messages_replay,)+from free_claude_code.core.json_types import JsonValuefrom .endpoint_types import HttpEndpoint@@ -39,6 +42,25 @@ def validate_history(body: Mapping[str, Any]) -> None:raise InvalidRequestError(str(error)) from error+def normalize_messages_history(request: MessagesRequest) -> MessagesRequest:+ """Resolve persisted associations before any lossy protocol conversion."""+ validate_history(request.model_dump(mode="json"))+ messages = []+ try:+ for message in request.messages:+ if message.role != "assistant" or isinstance(message.content, str):+ messages.append(message)+ continue+ body = message.model_dump(mode="json")+ body["content"] = resolve_messages_replay(+ cast(list[JsonValue], body["content"])+ )+ messages.append(Message.model_validate(body))+ except HistoryReplayError as error:+ raise InvalidRequestError(str(error)) from error+ return request.model_copy(update={"messages": messages})++def replay_origin(provider: str,protocol: HistoryProtocol,diff --git a/src/free_claude_code/providers/lmstudio/client.py b/src/free_claude_code/providers/lmstudio/client.pyindex 3967efeb..ca20888a 100644--- a/src/free_claude_code/providers/lmstudio/client.py+++ b/src/free_claude_code/providers/lmstudio/client.py@@ -7,8 +7,7 @@ model's jinja chat template with strict role-alternation rules and a fragile(``[TOOL_CALLS]Read``) and dumping whole tool calls into text(``Read[ARGS]{...}``), which ends agent runs silently. The OpenAI``/v1/chat/completions`` path is LM Studio's mature parsing route, and fcc's-OpenAI provider layers its own tool-call assembly, think-tag parsing, and-heuristic recovery on top.+OpenAI provider layers its own native tool-call assembly and think-tag parsing on top."""import asynciodiff --git a/src/free_claude_code/core/anthropic/tool_schema.py b/src/free_claude_code/providers/nvidia_nim/native_tool_schema.pysimilarity index 97%rename from src/free_claude_code/core/anthropic/tool_schema.pyrename to src/free_claude_code/providers/nvidia_nim/native_tool_schema.pyindex b5b9104c..4a513452 100644--- a/src/free_claude_code/core/anthropic/tool_schema.py+++ b/src/free_claude_code/providers/nvidia_nim/native_tool_schema.py@@ -1,4 +1,4 @@-"""JSON Schema helpers for text-emitted Anthropic tool input."""+"""JSON Schema helpers for NIM model-native tool arguments."""import jsonfrom collections.abc import Mappingdiff --git a/src/free_claude_code/providers/nvidia_nim/native_tool_stream.py b/src/free_claude_code/providers/nvidia_nim/native_tool_stream.pyindex e1d239d5..8bce3069 100644--- a/src/free_claude_code/providers/nvidia_nim/native_tool_stream.py+++ b/src/free_claude_code/providers/nvidia_nim/native_tool_stream.py@@ -8,13 +8,14 @@ from enum import StrEnumfrom types import SimpleNamespacefrom typing import Any-from free_claude_code.core.anthropic.tool_schema import (+from free_claude_code.providers.failure_policy import RetryableToolProtocolError+from free_claude_code.providers.http import maybe_await_aclose++from .native_tool_schema import (arguments_match_schema,coerce_text_argument,schema_type,)-from free_claude_code.providers.failure_policy import RetryableToolProtocolError-from free_claude_code.providers.http import maybe_await_aclose_NAMESPACE = "]<]minimax[>["_TOOL_BLOCK_START = f"{_NAMESPACE}<tool_call>"diff --git a/src/free_claude_code/providers/openai_chat/reasoning_details.py b/src/free_claude_code/providers/openai_chat/reasoning_details.pyindex 27eb3db4..84d183fa 100644--- a/src/free_claude_code/providers/openai_chat/reasoning_details.py+++ b/src/free_claude_code/providers/openai_chat/reasoning_details.py@@ -2,22 +2,59 @@from collections.abc import Iterator, Mapping, Sequencefrom copy import deepcopy+from dataclasses import dataclass, fieldfrom typing import Any, Literal, cast+from uuid import uuid4-from free_claude_code.core.history_replay import readable_reasoning+from free_claude_code.core.history_replay import (+ ReplayOrigin,+ ReplayRecord,+ readable_reasoning,+)from free_claude_code.core.json_types import JsonObject, JsonValuefrom .stream_output import ChatStreamOutput+@dataclass(slots=True)+class _ReasoningGroup:+ origin: ReplayOrigin+ id: str = field(default_factory=lambda: uuid4().hex)+ details: list[dict[str, Any]] = field(default_factory=list)+ slots: dict[tuple[object, object], int] = field(default_factory=dict)+ native_parts: list[str] = field(default_factory=list)+ after_content: bool = False++ def snapshot(self) -> ReplayRecord:+ native: JsonObject = {+ "reasoning_details": cast(list[JsonValue], deepcopy(self.details))+ }+ if self.native_parts:+ native["reasoning_content"] = "".join(self.native_parts)+ return ReplayRecord(self.origin, native)++class StructuredReasoningStream:- """Reconcile alternate plaintext reasoning representations for one stream."""+ """Collect replay groups independently of their visible wire blocks."""def __init__(self) -> None:self._text_source: Literal["native", "details"] | None = None- self._details: list[dict[str, Any]] = []- self._slots: dict[tuple[object, object], int] = {}- self._native_parts: list[str] = []+ self._group: _ReasoningGroup | None = None++ @property+ def active(self) -> bool:+ return self._group is not None++ def before_reasoning(self, output: ChatStreamOutput) -> Iterator[str]:+ if self._group is not None and self._group.after_content:+ yield from self.finish(output)++ def before_content(self, output: ChatStreamOutput) -> Iterator[str]:+ if self._group is not None and not self._group.after_content:+ self._group.after_content = True+ yield from output.pause_reasoning_record(+ self._group.id, self._group.snapshot()+ )def events(self,@@ -26,36 +63,45 @@ class StructuredReasoningStream:*,native_reasoning: str | None,) -> Iterator[str]:- """Emit plaintext once while preserving every opaque reasoning detail."""details = _reasoning_details(delta)- if native_reasoning is not None:- self._native_parts.append(native_reasoning)if self._text_source is None:if native_reasoning:self._text_source = "native"elif any(_reasoning_detail_text(detail) for detail in details):self._text_source = "details"-- if self._text_source == "native" and native_reasoning:- yield from output.ensure_reasoning_block()- yield output.emit_reasoning_delta(native_reasoning)-+ visible = (+ [native_reasoning]+ if self._text_source == "native" and native_reasoning+ else [+ text for detail in details if (text := _reasoning_detail_text(detail))+ ]+ if self._text_source == "details"+ else []+ )+ if visible:+ yield from self.before_reasoning(output)+ if self._group is None and (native_reasoning is not None or details):+ if output.replay_origin is None:+ raise AssertionError(+ "Structured reasoning requires attempt provenance."+ )+ self._group = _ReasoningGroup(output.replay_origin)+ yield from output.begin_reasoning_record()+ group = self._group+ if group is None:+ return+ if native_reasoning is not None:+ group.native_parts.append(native_reasoning)for detail in details:- if self._text_source == "details":- text = _reasoning_detail_text(detail)- if text:- yield from output.ensure_reasoning_block()- yield output.emit_reasoning_delta(text)-if isinstance(detail, Mapping):value = deepcopy(dict(detail))identity = value.get("index", value.get("id"))key = (identity, value.get("type"))- if isinstance(identity, str | int) and key in self._slots:- current = self._details[self._slots[key]]- for field, part in value.items():+ if isinstance(identity, str | int) and key in group.slots:+ current = group.details[group.slots[key]]+ for name, part in value.items():if (- field+ namein {"text","summary",@@ -65,27 +111,23 @@ class StructuredReasoningStream:"signature",}and isinstance(part, str)- and isinstance(current.get(field), str)+ and isinstance(current.get(name), str)):- current[field] += part+ current[name] += partelse:- current[field] = part+ current[name] = partelse:if isinstance(identity, str | int):- self._slots[key] = len(self._details)- self._details.append(value)-- def flush(self, output: ChatStreamOutput) -> Iterator[str]:- if self._details:- native: JsonObject = {- "reasoning_details": cast(list[JsonValue], self._details)- }- if self._native_parts:- native["reasoning_content"] = "".join(self._native_parts)- yield from output.emit_reasoning_replay(native)- self._details.clear()- self._slots.clear()- self._native_parts.clear()+ group.slots[key] = len(group.details)+ group.details.append(value)+ for text in visible:+ yield from output.ensure_reasoning_block()+ yield output.emit_reasoning_delta(text)++ def finish(self, output: ChatStreamOutput) -> Iterator[str]:+ group, self._group = self._group, None+ if group is not None:+ yield from output.complete_reasoning_record(group.id, group.snapshot())def _reasoning_details(delta: Any) -> Sequence[Any]:diff --git a/src/free_claude_code/providers/openai_chat/request_policy.py b/src/free_claude_code/providers/openai_chat/request_policy.pyindex 65378cf6..8cd71080 100644--- a/src/free_claude_code/providers/openai_chat/request_policy.py+++ b/src/free_claude_code/providers/openai_chat/request_policy.py@@ -19,7 +19,9 @@ from free_claude_code.core.openai_tool_names import (encode_openai_chat_tool_names,)from free_claude_code.core.reasoning import ReasoningPolicy-from free_claude_code.providers.history_replay import validate_history+from free_claude_code.providers.history_replay import (+ normalize_messages_history,+)MaxTokensField = Literal["max_tokens", "max_completion_tokens"]OpenAIChatPostprocessor = Callable[@@ -52,7 +54,7 @@ def build_openai_chat_request_body(postprocessors: Iterable[OpenAIChatPostprocessor] = (),) -> dict[str, Any]:"""Build an OpenAI-compatible chat request body from an Anthropic request."""- validate_history(request_data.model_dump(mode="json"))+ request_data = normalize_messages_history(request_data)logger.debug("{}_REQUEST: conversion start model={} msgs={}",policy.provider_name,diff --git a/src/free_claude_code/providers/openai_chat/stream_output.py b/src/free_claude_code/providers/openai_chat/stream_output.pyindex 86ad9ed1..9a7a9074 100644--- a/src/free_claude_code/providers/openai_chat/stream_output.py+++ b/src/free_claude_code/providers/openai_chat/stream_output.py@@ -1,19 +1,20 @@"""Chat-source output writers for Anthropic Messages and OpenAI Responses."""-import jsonimport timeimport uuidfrom abc import ABC, abstractmethod-from collections.abc import Callable, Iterator, Mapping+from collections.abc import Iterator, Mappingfrom dataclasses import dataclass, field+from typing import Protocolfrom free_claude_code.core.anthropic.streaming import (AnthropicStreamLedger,ToolSchema,parse_complete_tool_input,)-from free_claude_code.core.failures import ExecutionFailure+from free_claude_code.core.failures import ExecutionFailure, FailureKindfrom free_claude_code.core.history_replay import (+ AssociatedReplayRecord,ReplayOrigin,ReplayRecord,encode_replay,@@ -34,11 +35,20 @@ from free_claude_code.core.openai_responses import (new_response_id,openai_error_from_failure,reasoning_output_item,- replay_unsafe_function_call_error,tool_item,)from free_claude_code.core.token_estimation import estimate_text_tokens-from free_claude_code.core.trace import trace_event+++class ReasoningReplayLifecycle(Protocol):+ @property+ def active(self) -> bool: ...++ def before_reasoning(self, output: ChatStreamOutput) -> Iterator[str]: ...++ def before_content(self, output: ChatStreamOutput) -> Iterator[str]: ...++ def finish(self, output: ChatStreamOutput) -> Iterator[str]: ...@dataclass(frozen=True, slots=True)@@ -78,9 +88,7 @@ class ChatStreamOutput(ABC):def __init__(self, *, input_tokens: int) -> None:self.input_tokens = input_tokensself.replay_origin: ReplayOrigin | None = None- self.reasoning_replay_events: (- Callable[[ChatStreamOutput], Iterator[str]] | None- ) = None+ self.reasoning_replay: ReasoningReplayLifecycle | None = Noneself.tool_states: dict[int, ChatToolState] = {}self._text_parts: list[str] = []self._reasoning_parts: list[str] = []@@ -106,6 +114,8 @@ class ChatStreamOutput(ABC):def ensure_reasoning_block(self) -> list[str]:events: list[str] = []+ if self.reasoning_replay is not None:+ events.extend(self.reasoning_replay.before_reasoning(self))if self._text_started:events.extend(self._stop_text_block())self._text_started = False@@ -119,29 +129,41 @@ class ChatStreamOutput(ABC):self._reasoning_parts.append(content)return self._emit_reasoning_delta(content)- def emit_reasoning_replay(self, native: JsonObject) -> list[str]:- data = json.dumps(native["reasoning_details"], separators=(",", ":"))- if self.replay_origin is not None:- data = encode_replay(ReplayRecord(self.replay_origin, native))- if self._reasoning_started:- return self._attach_reasoning_replay(data)- events = self._close_content_blocks()- events.extend(self._emit_opaque_reasoning(data))- if data:- self._content_started = True- return events+ def begin_reasoning_record(self) -> list[str]:+ return []++ def pause_reasoning_record(self, group_id: str, record: ReplayRecord) -> list[str]:+ return []++ def complete_reasoning_record(+ self, group_id: str, record: ReplayRecord+ ) -> list[str]:+ data = encode_replay(record)+ if self._reasoning_started:+ events = self._attach_reasoning_replay(data)+ events.extend(self._stop_reasoning_block())+ self._reasoning_started = False+ return events+ return self._emit_opaque_reasoning(data)def flush_reasoning_replay(self) -> list[str]:- """Save the complete record when parsed output ends a reasoning block."""- if self.reasoning_replay_events is None:+ if self.reasoning_replay is None:return []- return list(self.reasoning_replay_events(self))+ return list(self.reasoning_replay.finish(self))- def ensure_text_block(self) -> list[str]:- events = self.flush_reasoning_replay()- if self._reasoning_started:+ def _pause_reasoning(self) -> list[str]:+ events: list[str] = []+ if self.reasoning_replay is not None:+ events.extend(self.reasoning_replay.before_content(self))+ if self._reasoning_started and not (+ self.reasoning_replay and self.reasoning_replay.active+ ):events.extend(self._stop_reasoning_block())self._reasoning_started = False+ return events++ def ensure_text_block(self) -> list[str]:+ events = self._pause_reasoning()if not self._text_started:events.extend(self._start_text_block())self._text_started = True@@ -153,10 +175,20 @@ class ChatStreamOutput(ABC):return self._emit_text_delta(content)def close_content_blocks(self) -> list[str]:+ events = self._pause_reasoning()+ if self._text_started:+ events.extend(self._stop_text_block())+ self._text_started = False+ return events++ def finish_reasoning_group(self) -> list[str]:events = self.flush_reasoning_replay()events.extend(self._close_content_blocks())return events+ def finish_replay_carriers(self) -> list[str]:+ return []+def _close_content_blocks(self) -> list[str]:events: list[str] = []if self._reasoning_started:@@ -215,10 +247,11 @@ class ChatStreamOutput(ABC):return self._stop_tool_block(tool_index, state)def close_all_blocks(self) -> list[str]:- events = self.close_content_blocks()+ events = self.finish_reasoning_group()for tool_index, state in self.tool_states.items():if state.open:events.extend(self.stop_tool_block(tool_index))+ events.extend(self.finish_replay_carriers())return eventsdef close_unclosed_blocks(self) -> list[str]:@@ -231,7 +264,9 @@ class ChatStreamOutput(ABC):return self._content_starteddef final_stop_reason(self, fallback: str) -> str:- return "tool_use" if self.has_emitted_tool_block() else fallback+ if self.has_emitted_tool_block():+ return "tool_use"+ return "end_turn" if fallback == "tool_use" else fallbackdef tool_block_for_tool_index(self, tool_index: int) -> ChatToolState | None:state = self.tool_states.get(tool_index)@@ -347,6 +382,8 @@ class AnthropicChatStreamOutput(ChatStreamOutput):log_raw_events: bool = False,) -> None:super().__init__(input_tokens=input_tokens)+ self._anchored_groups: set[str] = set()+ self._pending_replay: list[str] = []self._ledger = AnthropicStreamLedger(message_id,model,@@ -354,9 +391,56 @@ class AnthropicChatStreamOutput(ChatStreamOutput):log_raw_events=log_raw_events,)+ def pause_reasoning_record(self, group_id: str, record: ReplayRecord) -> list[str]:+ if group_id in self._anchored_groups:+ return []+ if not self._reasoning_started and any(+ state.open for state in self.tool_states.values()+ ):+ return []+ self._anchored_groups.add(group_id)+ data = encode_replay(+ AssociatedReplayRecord(record.origin, record.native, group_id, "anchor")+ )+ self._content_started = True+ if self._reasoning_started:+ events = self._attach_reasoning_replay(data)+ events.extend(self._stop_reasoning_block())+ self._reasoning_started = False+ return events+ events = self._close_content_blocks()+ events.extend(self._emit_opaque_reasoning(data))+ return events++ def complete_reasoning_record(+ self, group_id: str, record: ReplayRecord+ ) -> list[str]:+ if group_id in self._anchored_groups:+ self._anchored_groups.remove(group_id)+ self._pending_replay.append(+ encode_replay(+ AssociatedReplayRecord(+ record.origin, record.native, group_id, "final"+ )+ )+ )+ return []+ if self._reasoning_started:+ return super().complete_reasoning_record(group_id, record)+ self._pending_replay.append(encode_replay(record))+ self._content_started = True+ return []++ def finish_replay_carriers(self) -> list[str]:+ pending, self._pending_replay = self._pending_replay, []+ return [+ event for data in pending for event in self._emit_opaque_reasoning(data)+ ]+def close_unclosed_blocks(self) -> list[str]:events = self.flush_reasoning_replay()events.extend(self._ledger.close_unclosed_blocks())+ events.extend(self.finish_replay_carriers())self._text_started = Falseself._reasoning_started = Falsefor state in self.tool_states.values():@@ -452,16 +536,21 @@ class ResponsesChatStreamOutput(ChatStreamOutput):self._completer = ResponseBlockCompleter(self._ledger,events=self._events,- on_invalid_function_call=self._fail_invalid_function_call,- prepare_tool_arguments=tool_adapter.prepare_arguments,)self._text_state: TextBlockState | None = Noneself._reasoning_state: ReasoningBlockState | None = Noneself._tool_output_states: dict[int, ToolBlockState] = {}self._usage: dict[str, object] | None = None- self._provisional_error: dict[str, object] | None = None+ self._conversion_failure: ExecutionFailure | None = Noneself._started = False+ def begin_reasoning_record(self) -> list[str]:+ if self._reasoning_started:+ return []+ self._reasoning_started = True+ self._content_started = True+ return self._start_reasoning_block()+def _response_payload(self,*,@@ -629,16 +718,23 @@ class ResponsesChatStreamOutput(ChatStreamOutput):if output_state is None:return []self._ledger.pop_active_block(output_state.index)- return self._completer.complete_block(output_state)+ try:+ return self._completer.complete_block(output_state)+ except ResponsesConversionError:+ # Object-valued protocol conversions (such as tool search) cannot+ # carry opaque malformed JSON. Ordinary function arguments can.+ self._conversion_failure = ExecutionFailure(+ FailureKind.UPSTREAM,+ 502,+ "Provider tool output cannot be represented in the requested protocol.",+ False,+ )+ return []def _finish_success(self, *, stop_reason: str, usage: ChatStreamUsage) -> list[str]:self._usage = _responses_usage(usage)- if self._provisional_error is not None:- response = self._response_payload(- status="failed",- error=self._provisional_error,- )- return [self._events.response_failed(response)]+ if self._conversion_failure is not None:+ return self._finish_failure(self._conversion_failure)if stop_reason in {"length", "max_tokens"}:response = self._response_payload(status="incomplete",@@ -655,21 +751,6 @@ class ResponsesChatStreamOutput(ChatStreamOutput):)return [self._events.response_failed(response)]- def _fail_invalid_function_call(- self, state: ToolBlockState, exc: ResponsesConversionError- ) -> list[str]:- trace_event(- stage="responses",- event="responses.output.function_call_invalid_arguments",- source="openai_responses",- call_id=state.call_id,- tool_name=state.name,- error_type=type(exc).__name__,- )- if self._provisional_error is None:- self._provisional_error = replay_unsafe_function_call_error()- return []-def _responses_usage(usage: ChatStreamUsage) -> dict[str, object]:cached_tokens = usage.cached_tokensdiff --git a/src/free_claude_code/providers/openai_chat/tool_calls.py b/src/free_claude_code/providers/openai_chat/tool_calls.pyindex 363f4b3f..6e2476d7 100644--- a/src/free_claude_code/providers/openai_chat/tool_calls.py+++ b/src/free_claude_code/providers/openai_chat/tool_calls.py@@ -117,31 +117,6 @@ class OpenAIToolCallCollector:return tuple(completed)-def iter_heuristic_tool_use_events(- output: ChatStreamOutput,- tool_use: dict[str, Any],- *,- tool_names: OpenAIToolNameCodec | None = None,-) -> Iterator[str]:- """Emit SSE for one heuristic tool_use block."""- name = tool_use.get("name")- if tool_names is not None and isinstance(name, str):- decoded_name = tool_names.decode(name)- if decoded_name != name:- tool_use = {**tool_use, "name": decoded_name}- yield from output.close_content_blocks()- tool_index = len(output.tool_states)- output.ensure_tool_state(tool_index)- output.register_tool_name(tool_index, str(tool_use["name"]))- yield output.start_tool_block(- tool_index,- str(tool_use["id"]),- str(tool_use["name"]),- )- yield output.emit_tool_delta(tool_index, json.dumps(tool_use["input"]))- yield from output.stop_tool_block(tool_index)--def tool_call_extra_content(tool_call: Any) -> dict[str, Any] | None:"""Return provider-specific extra tool-call metadata from OpenAI objects."""if isinstance(tool_call, dict):diff --git a/src/free_claude_code/providers/openai_chat/transport.py b/src/free_claude_code/providers/openai_chat/transport.pyindex 112fdb26..db7f2715 100644--- a/src/free_claude_code/providers/openai_chat/transport.py+++ b/src/free_claude_code/providers/openai_chat/transport.py@@ -18,8 +18,6 @@ from free_claude_code.application.model_metadata import ProviderModelInfofrom free_claude_code.core.anthropic import (ContentBlockToolUse,ContentType,- FunctionTagToolParser,- HeuristicToolParser,ThinkTagParser,)from free_claude_code.core.anthropic.models import MessagesRequest@@ -115,7 +113,6 @@ from .tool_calls import (CompletedOpenAIToolCall,OpenAIToolCallAssembler,OpenAIToolCallCollector,- iter_heuristic_tool_use_events,tool_call_extra_content,)from .usage import (@@ -147,39 +144,6 @@ def _iter_visible_text_events(yield output.emit_text_delta(text)-def _iter_text_parser_events(- output: ChatStreamOutput,- parser: HeuristicToolParser,- text: str,- *,- tool_names: OpenAIToolNameCodec,-) -> Iterator[str]:- """Route visible text through the established heuristic tool parser."""- filtered_text, detected_tools = parser.feed(text)- if filtered_text:- yield from _iter_visible_text_events(output, filtered_text)- for tool_use in detected_tools:- yield from iter_heuristic_tool_use_events(- output,- tool_use,- tool_names=tool_names,- )---def _iter_text_tool_use_events(- output: ChatStreamOutput,- tool_uses: tuple[dict[str, Any], ...] | list[dict[str, Any]],- *,- tool_names: OpenAIToolNameCodec,-) -> Iterator[str]:- for tool_use in tool_uses:- yield from iter_heuristic_tool_use_events(- output,- tool_use,- tool_names=tool_names,- )--@dataclass(frozen=True, slots=True)class _OpenAIChatCompletion:finish_reason: Any@@ -224,7 +188,6 @@ class _OpenAIChatStreamAssembler:output_reasoning: bool,tool_names: OpenAIToolNameCodec,tool_schemas: dict[str, ToolSchema],- tool_choice_enabled: bool,tool_calls: OpenAIToolCallAssembler,extra_reasoning_events: _ExtraReasoningEvents,) -> None:@@ -237,21 +200,13 @@ class _OpenAIChatStreamAssembler:self._tool_calls = tool_callsself._extra_reasoning_events = extra_reasoning_eventsself._think_parser = ThinkTagParser()- self._function_tag_parser = FunctionTagToolParser.from_schemas(- tool_names=tool_names,- schemas={- name: schema.input_schema for name, schema in tool_schemas.items()- },- enabled=tool_choice_enabled,- )- self._heuristic_parser = HeuristicToolParser()self._structured_reasoning = (StructuredReasoningStream()if profile.structured_reasoning_detailselse None)if self._structured_reasoning is not None:- self._output.reasoning_replay_events = self._structured_reasoning.flush+ self._output.reasoning_replay = self._structured_reasoningself._finish_reason: Any = Noneself._usage_info: Any = Noneself._native_reasoning_seen = False@@ -367,11 +322,6 @@ class _OpenAIChatStreamAssembler:yield from self._extra_reasoning_events(delta, self._output)native_tool_calls = delta.tool_calls- if native_tool_calls:- released_text = self._function_tag_parser.disable()- if released_text:- yield from _iter_visible_text_events(self._output, released_text)-if delta.content:for part in self._think_parser.feed(delta.content):if part.type == ContentType.THINKING:@@ -380,14 +330,7 @@ class _OpenAIChatStreamAssembler:yield from self._output.ensure_reasoning_block()yield self._output.emit_reasoning_delta(part.content)else:- safe_text = self._function_tag_parser.feed(part.content)- if safe_text:- yield from _iter_text_parser_events(- self._output,- self._heuristic_parser,- safe_text,- tool_names=self._tool_names,- )+ yield from _iter_visible_text_events(self._output, part.content)if native_tool_calls:yield from self._output.close_content_blocks()@@ -436,28 +379,8 @@ class _OpenAIChatStreamAssembler:yield from self._output.ensure_reasoning_block()yield self._output.emit_reasoning_delta(remaining.content)else:- safe_text = self._function_tag_parser.feed(remaining.content)- if safe_text:- yield from _iter_text_parser_events(- self._output,- self._heuristic_parser,- safe_text,- tool_names=self._tool_names,- )+ yield from _iter_visible_text_events(self._output, remaining.content)- fallback_text, function_tag_tools = self._function_tag_parser.finish()- if fallback_text:- yield from _iter_visible_text_events(self._output, fallback_text)- yield from _iter_text_tool_use_events(- self._output,- function_tag_tools,- tool_names=self._tool_names,- )- yield from _iter_text_tool_use_events(- self._output,- self._heuristic_parser.flush(),- tool_names=self._tool_names,- )yield from self._output.flush_reasoning_replay()self._upstream_finished = True@@ -1505,7 +1428,9 @@ class _OpenAIChatStreamRunner:)text_suffix = continuation_suffix(partial_text, recovered.text)thinking_suffix = continuation_suffix(partial_thinking, recovered.thinking)- events: list[str] = []+ if not (thinking_suffix or text_suffix or recovered.tool_calls):+ return None+ events = output.flush_reasoning_replay()if thinking_suffix:events.extend(output.ensure_reasoning_block())events.append(output.emit_reasoning_delta(thinking_suffix))@@ -1628,10 +1553,6 @@ class _OpenAIChatStreamRunner:output_reasoning=output_reasoning,tool_names=self._tool_names,tool_schemas=self._tool_schemas,- tool_choice_enabled=(- bool(self._body.get("tools"))- and self._body.get("tool_choice") != "none"- ),tool_calls=OpenAIToolCallAssembler(reserved_tool_ids=self._reserved_tool_ids,record_extra_content=self._transport._behavior.record_tool_call_extra_content,diff --git a/src/free_claude_code/providers/openai_responses/transport.py b/src/free_claude_code/providers/openai_responses/transport.pyindex 49d3457b..4503b39a 100644--- a/src/free_claude_code/providers/openai_responses/transport.py+++ b/src/free_claude_code/providers/openai_responses/transport.py@@ -54,6 +54,7 @@ from free_claude_code.providers.failure_policy import (reports_context_window_incomplete,)from free_claude_code.providers.history_replay import (+ normalize_messages_history,replay_origin,validate_history,)@@ -197,7 +198,7 @@ class OpenAIResponsesTransport:model_info: ProviderModelInfo | None = None,can_disable_reasoning: bool = True,) -> JsonObject:- validate_history(request.model_dump(mode="json"))+ request = normalize_messages_history(request)request, reasoning = prepare_messages_reasoning(request,reasoning,diff --git a/tests/contracts/test_stream_contracts.py b/tests/contracts/test_stream_contracts.pyindex 0b4a7d8e..87ba64fc 100644--- a/tests/contracts/test_stream_contracts.py+++ b/tests/contracts/test_stream_contracts.py@@ -8,7 +8,6 @@ from collections.abc import Iterablefrom free_claude_code.core.anthropic import (AnthropicStreamLedger,ContentType,- HeuristicToolParser,ThinkTagParser,)from free_claude_code.core.anthropic.stream_contracts import (@@ -116,19 +115,6 @@ def test_enable_thinking_false_suppresses_reasoning_only() -> None:assert text_content(events) == "hello world"-def test_task_tool_parser_preserves_background_argument() -> None:- parser = HeuristicToolParser()- filtered, detected = parser.feed(- "● <function=Task><parameter=description>Inspect</parameter>"- "<parameter=run_in_background>true</parameter> trailing"- )- detected.extend(parser.flush())- assert "trailing" in filtered- task = detected[0]- assert task["name"] == "Task"- assert task["input"]["run_in_background"] == "true"--def _interleaved_thinking_text_events(parts: tuple[str, str, str, str],) -> Iterable[str]:diff --git a/tests/core/anthropic/test_native_messages_stream.py b/tests/core/anthropic/test_native_messages_stream.pyindex 32971c43..18aa2573 100644--- a/tests/core/anthropic/test_native_messages_stream.py+++ b/tests/core/anthropic/test_native_messages_stream.py@@ -69,7 +69,7 @@ async def test_native_citations_survive_block_completion_and_fragmented_aggregatblock = state.accept(cast(str, event["type"]), event)if block is not None:completed = block- assert completed is not None and completed["citations"] == [citation]+ assert completed is not None and completed.body["citations"] == [citation]relay = NativeMessagesRelay(public_model="public")async def stream() -> AsyncIterator[str]:@@ -197,7 +197,7 @@ def test_reused_block_indexes_and_events_after_terminal_are_rejected() -> None:@pytest.mark.parametrize("partial", ['{"x":', "[]", '{"x":NaN}'])-def test_invalid_json_tools_never_complete(partial: str) -> None:+def test_invalid_json_tools_are_forwarded_for_harness_validation(partial: str) -> None:relay = NativeMessagesRelay(public_model="public")relay.feed("message_start", _START)relay.feed(@@ -221,6 +221,9 @@ def test_invalid_json_tools_never_complete(partial: str) -> None:"delta": {"type": "input_json_delta", "partial_json": partial},},)- with pytest.raises(NativeMessagesError, match="arguments"):- relay.feed("content_block_stop", {"type": "content_block_stop", "index": 0})- assert not relay.completed+ relay.feed("content_block_stop", {"type": "content_block_stop", "index": 0})+ relay.feed(+ "message_delta", {"type": "message_delta", "delta": {"stop_reason": "tool_use"}}+ )+ relay.feed("message_stop", {"type": "message_stop"})+ assert relay.completeddiff --git a/tests/core/anthropic/test_text_tools.py b/tests/core/anthropic/test_text_tools.pydeleted file mode 100644index a57276ac..00000000--- a/tests/core/anthropic/test_text_tools.py+++ /dev/null@@ -1,258 +0,0 @@-import pytest--from free_claude_code.core.anthropic import FunctionTagToolParser-from free_claude_code.core.anthropic import tools as tool_parsers-from free_claude_code.core.openai_tool_names import OpenAIToolNameCodec-from tests.providers.request_factory import make_messages_request---def _request(*tools: dict):- return make_messages_request(- "test-model",- messages=[],- max_tokens=256,- tools=list(tools),- )---def _tool(name: str, properties: dict, required: list[str] | None = None) -> dict:- schema = {- "type": "object",- "properties": properties,- "additionalProperties": False,- }- if required is not None:- schema["required"] = required- return {"name": name, "input_schema": schema}---def _call(name: str, **arguments: str) -> str:- parameters = "".join(- f"<parameter={key}>\n{value}\n</parameter>\n"- for key, value in arguments.items()- )- return f"<tool_call>\n<function={name}>\n{parameters}</function>\n</tool_call>"---def _parse(parser: FunctionTagToolParser, *chunks: str):- visible = "".join(parser.feed(chunk) for chunk in chunks)- tail, calls = parser.finish()- return visible + tail, calls---def test_function_tag_parser_accepts_exact_reserved_response() -> None:- parser = FunctionTagToolParser(- _request(_tool("Bash", {"command": {"type": "string"}}, ["command"]))- )-- visible, calls = _parse(parser, _call("Bash", command="printf ok"))-- assert visible == ""- assert len(calls) == 1- assert calls[0]["id"].startswith("toolu_function_tag_")- assert calls[0]["name"] == "Bash"- assert calls[0]["input"] == {"command": "printf ok"}---def test_function_tag_parser_handles_every_two_chunk_split() -> None:- raw = _call("Read", file_path="README.md")- request = _request(_tool("Read", {"file_path": {"type": "string"}}, ["file_path"]))-- for split in range(len(raw) + 1):- visible, calls = _parse(- FunctionTagToolParser(request),- raw[:split],- raw[split:],- )- assert visible == "", split- assert [call["input"] for call in calls] == [{"file_path": "README.md"}], split---def test_function_tag_parser_streams_prefix_and_parses_terminal_call() -> None:- request = _request(_tool("Bash", {"command": {"type": "string"}}, ["command"]))- prefix = "I will run the requested command.\n"- raw_call = _call("Bash", command="printf ok")- parser = FunctionTagToolParser(request)-- first_visible = parser.feed(prefix + raw_call[:5])- second_visible = parser.feed(raw_call[5:])- tail, calls = parser.finish()-- assert first_visible == prefix- assert second_visible == ""- assert tail == ""- assert [call["input"] for call in calls] == [{"command": "printf ok"}]---def test_function_tag_parser_accepts_multiple_typed_calls_atomically() -> None:- request = _request(- _tool(- "Configure",- {- "count": {"type": "integer"},- "ratio": {"type": "number"},- "enabled": {"type": "boolean"},- "nothing": {"type": "null"},- "items": {"type": "array", "items": {"type": "integer"}},- "metadata": {"type": "object"},- "mode": {"enum": ["fast", "safe"]},- "fixed": {"const": 7},- },- [- "count",- "ratio",- "enabled",- "nothing",- "items",- "metadata",- "mode",- "fixed",- ],- ),- _tool("Bash", {"command": {"type": "string"}}, ["command"]),- )- raw = "\n".join(- (- _call(- "Configure",- count="2",- ratio="1.5",- enabled="true",- nothing="null",- items="[1,2]",- metadata='{"key":"value"}',- mode="safe",- fixed="7",- ),- _call("Bash", command="printf first\nprintf second"),- )- )-- visible, calls = _parse(FunctionTagToolParser(request), raw)-- assert visible == ""- assert [call["name"] for call in calls] == ["Configure", "Bash"]- assert calls[0]["input"] == {- "count": 2,- "ratio": 1.5,- "enabled": True,- "nothing": None,- "items": [1, 2],- "metadata": {"key": "value"},- "mode": "safe",- "fixed": 7,- }- assert calls[1]["input"] == {"command": "printf first\nprintf second"}---def test_function_tag_parser_decodes_openai_tool_alias() -> None:- original = "mcp__function_tag__" + "x" * 80- request = _request(_tool(original, {}, []))- alias = OpenAIToolNameCodec.from_request(request).encode(original)-- visible, calls = _parse(FunctionTagToolParser(request), _call(alias))-- assert visible == ""- assert [call["name"] for call in calls] == [original]--- "raw",- [- "Example: {call}\nThat was only an example.",- "{call}\nDone.",- "```xml\n{call}\n```",- "<tool_call><function=Bash></function>",- (- "<tool_call><function=Bash>"- "<parameter=command>x</parameter>"- "<parameter=command>y</parameter>"- "</function></tool_call>"- ),- "<tool_call><function=Unknown></function></tool_call>",- "<tool_call><function=Bash></function></tool_call>",- "{wrong_type}",- "{extra_parameter}",- ],-)-def test_function_tag_parser_preserves_non_control_lookalikes(raw: str) -> None:- valid_call = _call("Bash", command="printf ok")- wrong_type = _call("Count", value="not-an-integer")- extra_parameter = _call("Bash", command="printf ok", extra="no")- raw = raw.format(- call=valid_call,- wrong_type=wrong_type,- extra_parameter=extra_parameter,- )- request = _request(- _tool("Bash", {"command": {"type": "string"}}, ["command"]),- _tool("Count", {"value": {"type": "integer"}}, ["value"]),- )-- visible, calls = _parse(FunctionTagToolParser(request), raw)-- assert visible == raw- assert calls == ()---def test_function_tag_parser_preserves_whole_multi_call_candidate_on_failure() -> None:- request = _request(_tool("Bash", {"command": {"type": "string"}}, ["command"]))- raw = _call("Bash", command="printf ok") + _call("Unknown")-- visible, calls = _parse(FunctionTagToolParser(request), raw)-- assert visible == raw- assert calls == ()---def test_function_tag_parser_without_tools_is_immediate_passthrough() -> None:- raw = _call("Bash", command="printf ok")- parser = FunctionTagToolParser(_request())-- assert parser.feed(raw) == raw- assert parser.finish() == ("", ())---def test_function_tag_parser_respects_explicit_none_choice() -> None:- raw = _call("Bash", command="printf ok")- request = make_messages_request(- "test-model",- messages=[],- max_tokens=256,- tools=[_tool("Bash", {"command": {"type": "string"}}, ["command"])],- tool_choice={"type": "none"},- )- parser = FunctionTagToolParser(request)-- assert parser.feed(raw) == raw- assert parser.finish() == ("", ())---def test_function_tag_parser_disable_releases_candidate_once() -> None:- raw = _call("Bash", command="printf ok")- parser = FunctionTagToolParser(- _request(_tool("Bash", {"command": {"type": "string"}}, ["command"]))- )-- assert parser.feed(raw[:20]) == ""- assert parser.disable() == raw[:20]- assert parser.disable() == ""- assert parser.feed(raw[20:]) == raw[20:]- assert parser.finish() == ("", ())---def test_function_tag_parser_releases_oversized_candidate(- monkeypatch: pytest.MonkeyPatch,-) -> None:- raw = _call("Bash", command="printf ok")- monkeypatch.setattr(- tool_parsers,- "_MAX_FUNCTION_TAG_CANDIDATE_CHARS",- len(raw) - 1,- )- parser = FunctionTagToolParser(- _request(_tool("Bash", {"command": {"type": "string"}}, ["command"]))- )-- assert parser.feed(raw) == raw- assert parser.finish() == ("", ())diff --git a/tests/core/openai_responses/test_chat_request.py b/tests/core/openai_responses/test_chat_request.pyindex 8b6d3f0c..8b803175 100644--- a/tests/core/openai_responses/test_chat_request.py+++ b/tests/core/openai_responses/test_chat_request.py@@ -260,7 +260,7 @@ def test_build_responses_chat_request_uses_provider_reasoning_replay(}-def test_build_responses_chat_request_quarantines_one_malformed_call_pair() -> None:+def test_build_responses_chat_request_keeps_malformed_call_and_error_result() -> None:translated = build_responses_chat_request(_request(input=[@@ -274,7 +274,7 @@ def test_build_responses_chat_request_quarantines_one_malformed_call_pair() -> N{"type": "function_call_output","call_id": "call_bad",- "output": "stale",+ "output": "Invalid arguments",},{"type": "function_call",@@ -294,6 +294,19 @@ def test_build_responses_chat_request_quarantines_one_malformed_call_pair() -> Nassert translated.body["messages"] == [{"role": "user", "content": "Hello"},+ {+ "role": "assistant",+ "content": "",+ "reasoning_content": "",+ "tool_calls": [+ {+ "id": "call_bad",+ "type": "function",+ "function": {"name": "echo", "arguments": "{"},+ }+ ],+ },+ {"role": "tool", "tool_call_id": "call_bad", "content": "Invalid arguments"},{"role": "assistant","content": "",diff --git a/tests/core/openai_responses/test_client_tool_discovery.py b/tests/core/openai_responses/test_client_tool_discovery.pyindex cebe5b06..a3f865df 100644--- a/tests/core/openai_responses/test_client_tool_discovery.py+++ b/tests/core/openai_responses/test_client_tool_discovery.py@@ -177,7 +177,7 @@ def test_native_namespaced_argument_done_event_matches_completed_item() -> None:},))- assert done == []+ assert len(done) == 1completed = list(events.feed("response.output_item.done",@@ -192,13 +192,16 @@ def test_native_namespaced_argument_done_event_matches_completed_item() -> None:))done_event = next(- data- for kind, data in completed- if kind == "response.function_call_arguments.done"+ data for kind, data in done if kind == "response.function_call_arguments.done")assert done_event["name"] == "spawn_agent"assert done_event["namespace"] == "agents"+ assert (+ done_event["arguments"]+ == cast(dict[str, Any], completed[-1][1]["item"])["arguments"]+ )+def test_native_rejects_ambiguous_bare_names() -> None:other = deepcopy(AGENTS)@@ -313,17 +316,16 @@ def test_native_search_buffers_partial_added_arguments() -> None:}-def test_native_does_not_publish_empty_completed_arguments_as_success() -> None:- adapter = _native_adapter([AGENTS])- with pytest.raises(ResponsesConversionError, match="arguments"):- adapter.restore_item(- {- "type": "function_call",- "name": "agents__spawn_agent",- "status": "completed",- "arguments": "",- }- )+def test_native_preserves_empty_completed_arguments_for_the_harness() -> None:+ item = _native_adapter([AGENTS]).restore_item(+ {+ "type": "function_call",+ "name": "agents__spawn_agent",+ "status": "completed",+ "arguments": "",+ }+ )+ assert isinstance(item, dict) and item["arguments"] == ""@pytest.mark.parametrize("terminal", ["failed", "incomplete"])@@ -827,7 +829,7 @@ def test_flattened_function_collision_is_rejected() -> None:@pytest.mark.parametrize("search", [True, False])-def test_integral_json_numbers_are_accepted_by_native_codex(search: bool) -> None:+def test_only_object_conversion_normalizes_integral_numbers(search: bool) -> None:adapter = _native_adapter([SEARCH, AGENTS])value = adapter.restore_item({@@ -840,11 +842,11 @@ def test_integral_json_numbers_are_accepted_by_native_codex(search: bool) -> Nonassert isinstance(value, dict)arguments = value["arguments"] if search else json.loads(str(value["arguments"]))assert isinstance(arguments, dict)- assert type(arguments["limit"]) is int+ assert type(arguments["limit"]) is (int if search else float)assert arguments["fraction"] == 0.25nested = arguments["nested"]assert isinstance(nested, list)- assert type(nested[0]) is int+ assert type(nested[0]) is (int if search else float)def test_server_search_choice_stays_native() -> None:@@ -964,7 +966,7 @@ def test_real_unnamespaced_tool_wins_over_bare_namespace_alias() -> None:@pytest.mark.parametrize("prefix", ["", '{"limit":', '{"limit":8.0}'])-def test_native_canonical_arguments_agree_across_the_stream(prefix: str) -> None:+def test_native_original_arguments_agree_across_the_stream(prefix: str) -> None:adapter = _native_adapter([{**AGENTS, "description": "Agent tools"}])stream = adapter.event_adapter()assert stream is not None@@ -1017,7 +1019,7 @@ def test_native_canonical_arguments_agree_across_the_stream(prefix: str) -> Nonedone = next(data for kind, data in events if kind == "response.function_call_arguments.done")- assert delta == done["arguments"] == completed["arguments"]+ assert prefix + delta == done["arguments"] == completed["arguments"]sdk: ResponseStreamState[Any] = ResponseStreamState(input_tools=omit, text_format=omit)@@ -1749,6 +1751,17 @@ def test_interleaved_tool_kinds_keep_arguments_and_terminal_output_consistent("arguments": "".join(fragments[i]),}completed.append(finished)+ events.extend(+ data+ for _, data in stream.feed(+ "response.function_call_arguments.done",+ {+ "item_id": item["id"],+ "output_index": i,+ "arguments": finished["arguments"],+ },+ )+ )events.extend(datafor _, data in stream.feed(@@ -1792,7 +1805,7 @@ def test_interleaved_tool_kinds_keep_arguments_and_terminal_output_consistent(assert final[0]["arguments"] == {"query": "agent"}assert final[1]["type"] == "function_call"assert (final[1]["name"], final[1]["namespace"]) == ("lookup", "group")- assert final[1]["arguments"] == '{"n":8}'+ assert final[1]["arguments"] == '{"n":8.0}'assert final[2]["type"] == "custom_tool_call"assert (final[2]["name"], final[2]["namespace"], final[2]["input"]) == ("edit",@@ -1840,7 +1853,7 @@ def test_chat_preserves_custom_result_text_serialization() -> None:@pytest.mark.parametrize("native", [False, True])@pytest.mark.parametrize("search", [False, True])@pytest.mark.parametrize("constant", ["NaN", "Infinity", "-Infinity"])-def test_non_finite_arguments_cannot_complete_or_corrupt_sse(+def test_non_finite_arguments_are_opaque_except_in_object_conversion(native: bool, search: bool, constant: str) -> None:request = OpenAIResponsesRequest(@@ -1848,16 +1861,23 @@ def test_non_finite_arguments_cannot_complete_or_corrupt_sse()name = "fcc_tool_search" if search else "agents__spawn_agent"arguments = '{"nested":[{"limit":' + constant + "}]}"- if native:+ if search and native:with pytest.raises(ResponsesConversionError, match="arguments"):- _completed_tool_events(request, native=True, name=name, arguments=arguments)- else:+ _completed_tool_events(+ request, native=native, name=name, arguments=arguments+ )+ elif search:events = _completed_tool_events(- request, native=False, name=name, arguments=arguments+ request, native=native, name=name, arguments=arguments)assert events[-1]["type"] == "response.failed"assert events[-1]["response"]["output"] == []- assert not any(event["type"] == "response.output_item.done" for event in events)+ else:+ events = _completed_tool_events(+ request, native=native, name=name, arguments=arguments+ )+ assert events[-1]["type"] == "response.completed"+ assert events[-1]["response"]["output"][0]["arguments"] == argumentsjson.dumps(events, allow_nan=False)@@ -1894,7 +1914,7 @@ def test_non_finite_spellings_in_json_strings_remain_valid(item = events[-1]["response"]["output"][0]arguments = item["arguments"] if search else json.loads(item["arguments"])assert arguments == {"text": "NaN Infinity -Infinity", "limit": 8}- assert type(arguments["limit"]) is int+ assert type(arguments["limit"]) is (int if search else float)json.dumps(events, allow_nan=False)@@ -1997,7 +2017,9 @@ def test_argument_numbers_survive_sse_serialization(else json.loads(item["arguments"], parse_float=Decimal))assert actual == json.loads(arguments, parse_float=Decimal)- assert type(actual["nested"]["values"][1]) is int+ assert type(actual["nested"]["values"][1]) is (int if search else Decimal)+ if not search:+ assert item["arguments"] == arguments@pytest.mark.parametrize("custom", [False, True])diff --git a/tests/core/test_history_replay.py b/tests/core/test_history_replay.pyindex 222fe936..0d52f2ba 100644--- a/tests/core/test_history_replay.py+++ b/tests/core/test_history_replay.py@@ -7,6 +7,7 @@ from typing import Any, castimport pytestfrom free_claude_code.core.history_replay import (+ AssociatedReplayRecord,HistoryReplayError,HistoryScope,ReplayOrigin,@@ -14,6 +15,7 @@ from free_claude_code.core.history_replay import (decode_replay,encode_replay,prepare_history,+ resolve_messages_replay,responses_replay_item,)@@ -190,3 +192,115 @@ def test_chat_replay_keeps_full_text_and_summary_meaning(native_text):else:assert result["reasoning_content"] == "Full reasoning."assert body == original+++def _associated(part, data, *, group_id="group-a", origin=None):+ return encode_replay(+ AssociatedReplayRecord(+ origin or _origin(protocol="chat"),+ {"reasoning_details": [{"type": "reasoning.encrypted", "data": data}]},+ group_id,+ part,+ )+ )++[email protected]("readable", [False, True])+def test_late_replay_replaces_snapshot_at_original_position(readable):+ anchor = (+ {+ "type": "thinking",+ "thinking": "Plan.",+ "signature": _associated("anchor", "first"),+ }+ if readable+ else {"type": "redacted_thinking", "data": _associated("anchor", "first")}+ )+ text = {"type": "text", "text": "answer"}+ blocks = [+ anchor,+ text,+ {"type": "redacted_thinking", "data": _associated("final", "firstsecond")},+ ]+ original = deepcopy(blocks)+ resolved = resolve_messages_replay(blocks)+ assert len(resolved) == 2+ assert resolved[1] == text+ first = resolved[0]+ assert isinstance(first, dict)+ key = "signature" if readable else "data"+ value = first[key]+ assert isinstance(value, str)+ record = decode_replay(value)+ assert not isinstance(record, AssociatedReplayRecord)+ assert record.native["reasoning_details"] == [+ {"type": "reasoning.encrypted", "data": "firstsecond"}+ ]+ if readable:+ assert first["thinking"] == "Plan."+ assert blocks == original+ assert resolve_messages_replay(resolved) == resolved++[email protected]("part", ["anchor", "final"])+def test_unpaired_replay_preserves_available_data(part):+ resolved = resolve_messages_replay(+ [{"type": "redacted_thinking", "data": _associated(part, "available")}]+ )+ block = resolved[0]+ assert isinstance(block, dict)+ value = block["data"]+ assert isinstance(value, str)+ assert decode_replay(value).native["reasoning_details"] == [+ {"type": "reasoning.encrypted", "data": "available"}+ ]+ assert not isinstance(decode_replay(value), AssociatedReplayRecord)+++ "case",+ [+ "duplicate_anchor",+ "duplicate_final",+ "reversed",+ "different_model",+ "different_connection",+ "final_in_thinking",+ ],+)+def test_conflicting_replay_associations_are_rejected(case):+ anchor = {+ "type": "thinking",+ "thinking": "plan",+ "signature": _associated("anchor", "first"),+ }+ final = {"type": "redacted_thinking", "data": _associated("final", "firstsecond")}+ blocks = [anchor, final]+ if case == "duplicate_anchor":+ blocks.insert(1, deepcopy(anchor))+ elif case == "duplicate_final":+ blocks.append(deepcopy(final))+ elif case == "reversed":+ blocks.reverse()+ elif case in {"different_model", "different_connection"}:+ origin = (+ _origin(protocol="chat", model="other")+ if case == "different_model"+ else _origin(protocol="chat", connection="other")+ )+ final["data"] = _associated("final", "firstsecond", origin=origin)+ else:+ blocks[1] = {"type": "thinking", "thinking": "plan", "signature": final["data"]}+ with pytest.raises(HistoryReplayError):+ resolve_messages_replay(blocks)+++def test_unrelated_native_and_older_replay_blocks_are_unchanged():+ blocks = [+ {"type": "thinking", "thinking": "native", "signature": "upstream-signature"},+ {+ "type": "redacted_thinking",+ "data": encode_replay(ReplayRecord(_origin(), _native())),+ },+ ]+ assert resolve_messages_replay(blocks) == blocksdiff --git a/tests/core/test_history_switching.py b/tests/core/test_history_switching.pyindex 37c048c8..ae93dddf 100644--- a/tests/core/test_history_switching.py+++ b/tests/core/test_history_switching.py@@ -70,19 +70,34 @@ def test_grouped_tool_calls_keep_both_reasoning_records():]-def test_quarantined_tool_call_does_not_donate_its_reasoning():+def test_malformed_tool_call_keeps_its_reasoning_and_error_result():messages = _chat([{"role": "user", "content": "continue"},_reasoning("broken call only", "opaque-broken"),_call("broken", "{"),_call("valid"),+ {+ "type": "function_call_output",+ "call_id": "broken",+ "output": "Invalid JSON",+ },{"type": "function_call_output", "call_id": "valid", "output": "17"},])assistant = next(message for message in messages if message.get("tool_calls"))- assert not assistant.get("reasoning_content")- assert "reasoning_details" not in assistant+ assert assistant["reasoning_content"] == "broken call only"+ assert [detail["data"] for detail in assistant["reasoning_details"]] == [+ "opaque-broken"+ ]+ assert [+ (call["id"], call["function"]["arguments"]) for call in assistant["tool_calls"]+ ] == [("broken", "{"), ("valid", "{}")]+ assert [+ (message["tool_call_id"], message["content"])+ for message in messages+ if message["role"] == "tool"+ ] == [("broken", "Invalid JSON"), ("valid", "17")]def test_completed_hosted_tool_history_remains_readable():diff --git a/tests/providers/test_history_transports.py b/tests/providers/test_history_transports.pyindex 1b823bcf..cb6997d0 100644--- a/tests/providers/test_history_transports.py+++ b/tests/providers/test_history_transports.py@@ -15,7 +15,11 @@ from free_claude_code.core.anthropic import aggregate_anthropic_sse_to_messagefrom free_claude_code.core.anthropic.models import MessagesRequestfrom free_claude_code.core.anthropic.stream_contracts import parse_sse_textfrom free_claude_code.core.failures import ExecutionFailure, FailureKind-from free_claude_code.core.history_replay import decode_replay, encode_replay+from free_claude_code.core.history_replay import (+ decode_replay,+ encode_replay,+ resolve_messages_replay,+)from free_claude_code.core.openai_responses import OpenAIResponsesRequestfrom free_claude_code.core.reasoning import ReasoningPolicyfrom free_claude_code.providers.open_router import OpenRouterProvider@@ -178,7 +182,7 @@ async def _harness(protocol, responder=None, *, key="a", chat_provider_factory=N)extras = {}- def stream(wire, history):+ def stream(wire, history, *, tools=None):options: dict[str, Any] = dict(input_tokens=0,request_id="history-test",@@ -192,12 +196,14 @@ async def _harness(protocol, responder=None, *, key="a", chat_provider_factory=Nif wire == "responses":return provider.stream_responses(OpenAIResponsesRequest.model_validate(- {"model": "requested", "input": history}+ {"model": "requested", "input": history, "tools": tools}),**options,)return provider.stream_messages(- MessagesRequest.model_validate({"model": "requested", "messages": history}),+ MessagesRequest.model_validate(+ {"model": "requested", "messages": history, "tools": tools}+ ),**options,)@@ -224,12 +230,14 @@ def _carrier(history, wire):return next(item["encrypted_content"]for item in history- if item.get("type") == "reasoning"+ if item.get("type") == "reasoning" and item.get("encrypted_content"))return next(block.get("signature", block.get("data"))- for block in history[0]["content"]- if block["type"] in {"thinking", "redacted_thinking"}+ for block in resolve_messages_replay(history[0]["content"])+ if isinstance(block, dict)+ and block["type"] in {"thinking", "redacted_thinking"}+ and block.get("signature", block.get("data")))@@ -516,13 +524,13 @@ def _chat_reasoning_events(deltas):"<tool_call>","<function=lookup><parameter=query>x</parameter></function></tool_call>","stop",- True,+ False,),("● <function=lookup>","<parameter=query>x</parameter></function>","stop",- True,+ False,),("<", None, "tool_calls", True),("<", "", "length", False),@@ -745,6 +753,8 @@ async def test_chat_plaintext_beside_encrypted_details_survives_switching(if wire == "responses":saved[0]["encrypted_content"] = carrierelse:+ # Model an older complete v1 transcript before removing its readable field.+ saved[0]["content"] = resolve_messages_replay(saved[0]["content"])saved[0]["content"][0]["signature"] = carrierelse:assert record.native["reasoning_content"] == textdiff --git a/tests/providers/test_native_tool_arguments.py b/tests/providers/test_native_tool_arguments.pynew file mode 100644index 00000000..b7406c9a--- /dev/null+++ b/tests/providers/test_native_tool_arguments.py@@ -0,0 +1,309 @@+"""Ordinary tool arguments and harness error feedback survive protocol translation."""++import json++import pytest++from free_claude_code.core.anthropic.sse_aggregation import (+ aggregate_anthropic_sse_to_message,+)+from free_claude_code.core.anthropic.stream_contracts import parse_sse_text+from free_claude_code.core.failures import ExecutionFailure, FailureKind+from free_claude_code.core.openai_responses.streaming.event_builders import (+ ResponseEventBuilder,+)+from tests.providers.test_history_transports import _events_for, _harness++ARGUMENTS = [+ '{"path":"ok"}',+ '{"path":',+ "{}",+ '{"path":42}',+ "[]",+ "null",+ "",+ '{"x":1e400,"unicode":"\\u263a"}',+]+++def tool_events(protocol, arguments):+ if protocol == "chat":+ template = _events_for("chat")[0]+ deltas = [+ {+ "tool_calls": [+ {+ "index": 0,+ "id": "call_probe",+ "type": "function",+ "function": {"name": "read", "arguments": ""},+ }+ ]+ }+ ]+ deltas += [+ {"tool_calls": [{"index": 0, "function": {"arguments": part}}]}+ for part in arguments+ ]+ return [+ {+ **template,+ "choices": [{"index": 0, "delta": delta, "finish_reason": None}],+ }+ for delta in deltas+ ] + [+ {+ **template,+ "choices": [{"index": 0, "delta": {}, "finish_reason": "tool_calls"}],+ }+ ]+ if protocol == "messages":+ return [+ _events_for("messages")[0],+ {+ "type": "content_block_start",+ "index": 0,+ "content_block": {+ "type": "tool_use",+ "id": "call_probe",+ "name": "read",+ "input": {},+ },+ },+ *[+ {+ "type": "content_block_delta",+ "index": 0,+ "delta": {"type": "input_json_delta", "partial_json": part},+ }+ for part in arguments or [""]+ ],+ {"type": "content_block_stop", "index": 0},+ {+ "type": "message_delta",+ "delta": {"stop_reason": "tool_use"},+ "usage": {"output_tokens": 10},+ },+ {"type": "message_stop"},+ ]+ builder = ResponseEventBuilder()+ item = {+ "type": "function_call",+ "id": "fc_probe",+ "call_id": "call_probe",+ "name": "read",+ "arguments": arguments,+ "status": "completed",+ }+ response = {+ "id": "resp_probe",+ "object": "response",+ "created_at": 0,+ "model": "upstream",+ "status": "completed",+ "output": [item],+ "usage": {"input_tokens": 1, "output_tokens": 10, "total_tokens": 11},+ }+ wire = builder.response_created({**response, "status": "in_progress", "output": []})+ wire += builder.output_item_added(+ 0, {**item, "status": "in_progress", "arguments": ""}+ )+ wire += "".join(+ builder.function_call_arguments_delta("fc_probe", 0, part) for part in arguments+ )+ wire += builder.function_call_arguments_done("fc_probe", 0, arguments)+ wire += builder.output_item_done(0, item) + builder.response_completed(response)+ return [event.data for event in parse_sse_text(wire)]++[email protected]("protocol", ["chat", "messages", "responses"])[email protected]("wire", ["messages", "responses"])[email protected]("arguments", ARGUMENTS)+async def test_completed_native_arguments_are_transparent(protocol, wire, arguments):+ async with _harness(+ protocol, lambda _: (200, tool_events(protocol, arguments))+ ) as (send, bodies, _):+ history = [{"role": "user", "content": "read"}]+ events = parse_sse_text(+ "".join(+ [+ chunk+ async for chunk in send(+ wire,+ history,+ tools=[+ {+ "type": "function",+ "name": "read",+ "parameters": {"type": "object"},+ }+ ]+ if wire == "responses"+ else [{"name": "read", "input_schema": {"type": "object"}}],+ )+ ]+ )+ )+ assert len(bodies) == 1+ if wire == "responses":+ assert events[-1].event == "response.completed"+ call = events[-1].data["response"]["output"][0]+ assert call["arguments"] == arguments+ assert call["call_id"] == "call_probe"+ assert (+ "".join(+ e.data["delta"]+ for e in events+ if e.event == "response.function_call_arguments.delta"+ )+ == arguments+ )+ else:+ assert events[-1].event == "message_stop"+ assert not any(e.event == "error" for e in events)+ assert (+ "".join(+ e.data["delta"]["partial_json"]+ for e in events+ if e.data.get("delta", {}).get("type") == "input_json_delta"+ )+ == arguments+ )++[email protected]("protocol", ["chat", "responses"])[email protected]("arguments", ARGUMENTS)+async def test_harness_tool_error_is_kept_in_next_request(protocol, arguments):+ async with _harness(+ protocol, lambda _: (200, tool_events(protocol, arguments))+ ) as (send, bodies, _):+ history = [{"role": "user", "content": "read"}]+ first = parse_sse_text(+ "".join([chunk async for chunk in send("responses", history)])+ )+ call = first[-1].data["response"]["output"][0]+ history += [+ call,+ {+ "type": "function_call_output",+ "call_id": call["call_id"],+ "output": "Invalid arguments. Supply a path string.",+ },+ ]+ _ = [chunk async for chunk in send("responses", history)]+ if protocol == "chat":+ sent = bodies[-1]["messages"]+ assert sent[-2]["tool_calls"][0]["function"]["arguments"] == arguments+ assert sent[-1]["tool_call_id"] == call["call_id"]+ assert sent[-1]["content"] == history[-1]["output"]+ else:+ assert bodies[-1]["input"][-2:] == history[-2:]+++ "arguments", ['{"path":', "[]", "null", "", '{"x":NaN}', '{"x":1e400}']+)[email protected]("upstream_error", [False, True])+async def test_nonstream_messages_requires_an_object_and_preserves_upstream_errors(+ arguments, upstream_error+):+ error = {"type": "overloaded_error", "message": "Try again"}++ async def stream():+ for event in tool_events("messages", arguments):+ yield "data: " + json.dumps(event) + "\n\n"+ if upstream_error:+ yield "data: " + json.dumps({"type": "error", "error": error}) + "\n\n"++ if upstream_error:+ _, actual, _ = await aggregate_anthropic_sse_to_message(stream())+ assert actual == error+ else:+ with pytest.raises(ExecutionFailure) as raised:+ await aggregate_anthropic_sse_to_message(stream())+ assert raised.value.kind is FailureKind.UPSTREAM+ assert raised.value.status_code == 502+ assert not raised.value.retryable++[email protected]("wire", ["messages", "responses"])+ "text",+ [+ "before <tool_call><function=read><parameter=path>file</parameter></function></tool_call> after",+ "before ● <function=read><parameter=path>file</parameter> after",+ 'WebSearch {"query":"example"}',+ 'WebFetch {"url":"https://example.test"}',+ "before <|im_end|> after",+ ],+)+async def test_tool_looking_text_stays_literal_across_chunk_boundaries(wire, text):+ template = _events_for("chat")[0]+ for split in range(len(text) + 1):+ events = [+ {+ **template,+ "choices": [+ {"index": 0, "delta": {"content": part}, "finish_reason": None}+ ],+ }+ for part in [text[:split], text[split:]]+ ]+ events.append(+ {+ **template,+ "choices": [{"index": 0, "delta": {}, "finish_reason": "tool_calls"}],+ }+ )+ async with _harness("chat", lambda _, events=events: (200, events)) as (+ send,+ _bodies,+ _,+ ):+ output = parse_sse_text(+ "".join(+ [+ chunk+ async for chunk in send(+ wire,+ [{"role": "user", "content": "read"}],+ tools=[+ {+ "type": "function",+ "name": "read",+ "parameters": {"type": "object"},+ }+ ]+ if wire == "responses"+ else [{"name": "read", "input_schema": {"type": "object"}}],+ )+ ]+ )+ )+ if wire == "responses":+ items = output[-1].data["response"]["output"]+ assert all(item["type"] == "message" for item in items)+ assert (+ "".join(part["text"] for item in items for part in item["content"])+ == text+ )+ else:+ assert not any(+ e.data.get("content_block", {}).get("type") == "tool_use"+ for e in output+ )+ assert (+ "".join(e.data.get("delta", {}).get("text", "") for e in output) == text+ )+ assert (+ next(+ e.data["delta"]["stop_reason"]+ for e in output+ if e.event == "message_delta"+ )+ == "end_turn"+ )diff --git a/tests/providers/test_openai_chat_stream_output.py b/tests/providers/test_openai_chat_stream_output.pyindex 91da63a2..2bbcd138 100644--- a/tests/providers/test_openai_chat_stream_output.py+++ b/tests/providers/test_openai_chat_stream_output.py@@ -377,7 +377,7 @@ def test_responses_chat_output_preserves_custom_tool_free_form_input() -> None:]-def test_responses_chat_output_fails_malformed_function_call_once() -> None:+def test_responses_chat_output_completes_malformed_function_call_once() -> None:output = _responses_output(OpenAIResponsesRequest.model_validate({@@ -413,8 +413,14 @@ def test_responses_chat_output_fails_malformed_function_call_once() -> None:events = _parse_frames(frames)event_types = [event_type for event_type, _ in events]- assert event_types.count("response.failed") == 1- assert "response.completed" not in event_types+ assert event_types.count("response.completed") == 1+ assert "response.failed" not in event_typesfinal = _object_dict(events[-1][1]["response"])- assert final["status"] == "failed"- assert _object_dict(final["error"])["type"] == "api_error"+ assert final["status"] == "completed"+ assert final["error"] is None+ final_output = final["output"]+ assert isinstance(final_output, list)+ assert len(final_output) == 1+ item = _object_dict(final_output[0])+ assert item["arguments"] == '{"q":'+ assert item["call_id"] == "call_bad"diff --git a/tests/providers/test_parsers.py b/tests/providers/test_parsers.pyindex c382ccff..793a05ad 100644--- a/tests/providers/test_parsers.py+++ b/tests/providers/test_parsers.py@@ -2,7 +2,6 @@ import pytestfrom free_claude_code.core.anthropic import (ContentType,- HeuristicToolParser,ThinkTagParser,)@@ -35,239 +34,9 @@ def test_think_tag_parser_streaming():assert chunks[0].content == "reasoning"-def test_heuristic_tool_parser_basic():- parser = HeuristicToolParser()- text = "Let's call a tool. ● <function=Grep><parameter=pattern>hello</parameter><parameter=path>.</parameter>"- filtered, tools_initial = parser.feed(text)- tools_final = parser.flush()- tools = tools_initial + tools_final-- assert "Let's call a tool." in filtered- assert len(tools) == 1- assert tools[0]["name"] == "Grep"- assert tools[0]["input"] == {"pattern": "hello", "path": "."}---def test_heuristic_tool_parser_streaming():- parser = HeuristicToolParser()-- # Feed part 1- _filtered1, tools1 = parser.feed("● <function=Write>")- assert tools1 == []-- # Feed part 2- _filtered2, tools2 = parser.feed("<parameter=path>test.txt</parameter>")- assert tools2 == []-- # Feed part 3 (triggering flush or completion)- filtered3, tools3 = parser.feed("\nDone.")- assert len(tools3) == 1- assert tools3[0]["name"] == "Write"- assert tools3[0]["input"] == {"path": "test.txt"}- assert "Done." in filtered3---def test_heuristic_tool_parser_flush():- parser = HeuristicToolParser()- parser.feed("● <function=Bash><parameter=command>ls -la")- tools = parser.flush()-- assert len(tools) == 1- assert tools[0]["name"] == "Bash"- assert tools[0]["input"] == {"command": "ls -la"}---def test_heuristic_tool_parser_strips_control_tokens():- p = HeuristicToolParser()- filtered, tools = p.feed("Hello <|tool_call_end|> world")- tools.extend(p.flush())-- assert "<|tool_call_end|>" not in filtered- assert filtered == "Hello world"- assert tools == []---def test_heuristic_tool_parser_strips_control_tokens_split_across_chunks():- p = HeuristicToolParser()- f1, t1 = p.feed("Hello <|tool_call_")- f2, t2 = p.feed("end|> world")- tools = t1 + t2 + p.flush()-- assert "<|tool_call_end|>" not in (f1 + f2)- assert (f1 + f2) == "Hello world"- assert tools == []---def test_heuristic_tool_parser_strips_control_tokens_inside_tool_text():- p = HeuristicToolParser()- text = (- "Before <|tool_calls_section_end|> ● <function=Grep>"- "<parameter=pattern>hi</parameter> After"- )- filtered, tools = p.feed(text)- tools.extend(p.flush())-- assert "<|tool_calls_section_end|>" not in filtered- assert "Before" in filtered- assert "After" in filtered- assert len(tools) == 1- assert tools[0]["name"] == "Grep"- assert tools[0]["input"] == {"pattern": "hi"}---def test_interleaved_thinking_and_tools():- parser_think = ThinkTagParser()- parser_tool = HeuristicToolParser()-- text = "<think>I need to search for a file.</think> ● <function=Grep><parameter=pattern>test</parameter>"-- # 1. Parse thinking- chunks = list(parser_think.feed(text))- thinking = [c for c in chunks if c.type == ContentType.THINKING]- text_remaining = "".join([c.content for c in chunks if c.type == ContentType.TEXT])-- assert len(thinking) == 1- assert thinking[0].content == "I need to search for a file."-- # 2. Parse tool from remaining text- _filtered, tools = parser_tool.feed(text_remaining)- tools += parser_tool.flush()-- assert len(tools) == 1- assert tools[0]["name"] == "Grep"- assert tools[0]["input"] == {"pattern": "test"}---def test_partial_interleaved_streaming():- parser_think = ThinkTagParser()- parser_tool = HeuristicToolParser()-- # Chunk 1: Partial thinking (it emits since it's definitely not the start of <think>)- chunks1 = list(parser_think.feed("<think>Part 1"))- assert len(chunks1) == 1- assert chunks1[0].type == ContentType.THINKING- assert chunks1[0].content == "Part 1"-- # Chunk 2: Thinking ends, tool starts- chunks2 = list(parser_think.feed(" ends</think> ● <func"))- assert len(chunks2) == 2- assert chunks2[0].type == ContentType.THINKING- assert chunks2[0].content == " ends"-- text_rem = chunks2[1].content- _filtered, tools = parser_tool.feed(text_rem)- assert tools == []-- # Chunk 3: Tool ends- chunks3 = list(parser_think.feed("tion=Read><parameter=path>test.py</parameter>"))- text_rem3 = "".join([c.content for c in chunks3])- _filtered3, tools3 = parser_tool.feed(text_rem3)- tools3 += parser_tool.flush()-- assert len(tools3) == 1- assert tools3[0]["name"] == "Read"- assert tools3[0]["input"] == {"path": "test.py"}--# --- New Robustness Tests ----def test_split_across_markers():- # Split across the trigger chaaracter- # "● <function=Test>"- # Split at various points- full_text = "● <function=Test><parameter=arg>val</parameter>"-- for i in range(len(full_text)):- p = HeuristicToolParser()- chunk1 = full_text[:i]- chunk2 = full_text[i:]-- tools = []- _filtered, t = p.feed(chunk1)- tools.extend(t)- _filtered2, t = p.feed(chunk2)- tools.extend(t)- tools.extend(p.flush())-- if len(tools) != 1:- print(f"Failed split at index {i}: '{chunk1}' | '{chunk2}'")-- assert len(tools) == 1, f"Failed split at index {i}"- assert tools[0]["name"] == "Test"- assert tools[0]["input"] == {"arg": "val"}---def test_value_with_special_chars():- parser = HeuristicToolParser()- # Value with > inside- text = "● <function=Test><parameter=arg>a > b</parameter>"- _, tools = parser.feed(text)- tools.extend(parser.flush())-- assert len(tools) == 1- assert tools[0]["input"]["arg"] == "a > b"---def test_multiple_params_split():- full_text = (- "● <function=Test><parameter=p1>v1</parameter><parameter=p2>v2</parameter>"- )-- for i in range(len(full_text)):- p = HeuristicToolParser()- tools = []- _, t = p.feed(full_text[:i])- tools.extend(t)- _, t = p.feed(full_text[i:])- tools.extend(t)- tools.extend(p.flush())-- assert len(tools) == 1, f"Failed split at {i}"- assert tools[0]["input"] == {"p1": "v1", "p2": "v2"}---def test_incomplete_tag_flush():- p = HeuristicToolParser()- p.feed("● <function=Recover><parameter=msg>hello")- tools = p.flush()-- assert len(tools) == 1- assert tools[0]["input"]["msg"] == "hello"---def test_garbage_interleaved():- p = HeuristicToolParser()- tools = []- _, t = p.feed("Some text ")- tools.extend(t)- _, t = p.feed("● <function=T1><parameter=x>1</parameter>")- tools.extend(t)- _, t = p.feed(" more text ")- tools.extend(t)- _, t = p.feed("● <function=T2><parameter=y>2</parameter>")- tools.extend(t)- tools.extend(p.flush())-- assert len(tools) == 2- assert tools[0]["name"] == "T1"- assert tools[1]["name"] == "T2"---def test_text_between_params_lost():- p = HeuristicToolParser()- # " text1 " is between function end and first param- # " text2 " is between params- text = "● <function=F> text1 <parameter=a>1</parameter> text2 <parameter=b>2</parameter>"- filtered, tools = p.feed(text)- tools.extend(p.flush())-- # Check if "text1" and "text2" are preserved in filtered output- assert "text1" in filtered- assert "text2" in filtered- assert tools[0]["input"] == {"a": "1", "b": "2"}--# --- Orphan </think> Tag Tests (Step Fun AI compatibility) ---@@ -436,79 +205,4 @@ def test_think_tag_parser_unicode():assert "日本語" in textassert "結果" in text--def test_heuristic_tool_parser_empty_input():- """Empty string input should return empty filtered text and no tools."""- parser = HeuristicToolParser()- filtered, tools = parser.feed("")- assert filtered == ""- assert tools == []---def test_heuristic_tool_parser_flush_no_tool():- """Flush when no tool is being parsed should return empty list."""- parser = HeuristicToolParser()- parser.feed("plain text")- tools = parser.flush()- assert tools == []---def test_heuristic_tool_parser_json_style_web_fetch_tool_call():- parser = HeuristicToolParser()- text = (- "Use WebFetch on the article.\n\n"- "{\n"- ' "url": "https://example.com/article",\n'- ' "prompt": "Summarize it."\n'- "}\n"- )-- filtered, tools = parser.feed(text)- tools.extend(parser.flush())-- assert filtered == ""- assert len(tools) == 1- assert tools[0]["name"] == "WebFetch"- assert tools[0]["input"] == {- "url": "https://example.com/article",- "prompt": "Summarize it.",- }---def test_heuristic_tool_parser_json_style_web_search_tool_call():- parser = HeuristicToolParser()-- filtered, tools = parser.feed('Use WebSearch {"query": "DeepSeek V4"}')- tools.extend(parser.flush())-- assert filtered == ""- assert len(tools) == 1- assert tools[0]["name"] == "WebSearch"- assert tools[0]["input"] == {"query": "DeepSeek V4"}---def test_heuristic_tool_parser_unicode_function_name():- """Unicode characters in function parameters."""- parser = HeuristicToolParser()- text = "● <function=Search><parameter=query>日本語テスト</parameter>"- _filtered, tools = parser.feed(text)- tools.extend(parser.flush())- assert len(tools) == 1- assert tools[0]["name"] == "Search"- assert tools[0]["input"]["query"] == "日本語テスト"--- "malformed_text",- [- "● <function=>",- "● <function=><parameter=x>v</parameter>",- ],- ids=["empty_name", "empty_name_with_param"],-)-def test_heuristic_tool_parser_malformed_function_tag(malformed_text):- """Malformed function tags should still be handled without crashing."""- parser = HeuristicToolParser()- _filtered, tools = parser.feed(malformed_text)- tools.extend(parser.flush())# Should not crash; may or may not detect a tool depending on regex matchdiff --git a/tests/providers/test_streaming_errors.py b/tests/providers/test_streaming_errors.pyindex 2a34affa..557b2235 100644--- a/tests/providers/test_streaming_errors.py+++ b/tests/providers/test_streaming_errors.py@@ -35,7 +35,6 @@ from free_claude_code.providers.openai_chat.stream_output import (from free_claude_code.providers.openai_chat.tool_calls import (OpenAIToolCallAssembler,OpenAIToolCallCollector,- iter_heuristic_tool_use_events,)from free_claude_code.providers.openai_chat.transport import (_OpenAIChatStreamRunner,@@ -915,10 +914,8 @@ class TestStreamingExceptionHandling:@pytest.mark.asyncio@pytest.mark.parametrize("finish_reason", ["tool_calls", "stop"])- async def test_heuristic_only_tool_stream_does_not_emit_fallback_text(- self, finish_reason- ):- """Text-parsed tool calls count as emitted tool output when finalizing."""+ async def test_legacy_tool_markup_remains_visible_text(self, finish_reason):+ """Legacy markup cannot manufacture executable tool calls."""provider = _make_provider()request = _make_request()heuristic_tool = (@@ -941,7 +938,7 @@ class TestStreamingExceptionHandling:events = await _collect_stream(provider, request)parsed = parse_sse_text("".join(events))- assert any(+ assert not any(event.event == "content_block_start"and event.data.get("content_block", {}).get("type") == "tool_use"for event in parsed@@ -954,13 +951,18 @@ class TestStreamingExceptionHandling:)assert any(event.event == "message_delta"- and event.data.get("delta", {}).get("stop_reason") == "tool_use"+ and event.data.get("delta", {}).get("stop_reason") == "end_turn"for event in parsed)+ assert (+ "".join(event.data.get("delta", {}).get("text", "") for event in parsed)+ == heuristic_tool+ )+@pytest.mark.asyncio- async def test_function_tag_tool_stream_becomes_one_anthropic_tool_use(self):- """Exact control-only function tags use the established tool lifecycle."""+ async def test_function_tag_markup_remains_text_beside_reasoning(self):+ """Function-looking text stays visible after reasoning parsing."""provider = _make_provider()request = _make_request(tools=[@@ -1017,9 +1019,9 @@ class TestStreamingExceptionHandling:and event.data.get("delta", {}).get("type") == "text_delta")- assert [block["name"] for block in tool_starts] == ["Bash"]- assert json.loads(input_json) == {"command": "printf FCC_STEP_TOOL"}- assert visible_text == "I will invoke Bash now.\n"+ assert tool_starts == []+ assert input_json == ""+ assert visible_text == raw_call.split("</think>", 1)[1]assert any(event.event == "content_block_delta"and event.data.get("delta", {}).get("type") == "thinking_delta"@@ -1027,7 +1029,7 @@ class TestStreamingExceptionHandling:)assert any(event.event == "message_delta"- and event.data.get("delta", {}).get("stop_reason") == "tool_use"+ and event.data.get("delta", {}).get("stop_reason") == "end_turn"for event in parsed)@@ -1133,8 +1135,9 @@ class TestStreamingExceptionHandling:assert tool_starts == []@pytest.mark.asyncio- async def test_function_tag_candidate_is_reset_before_early_retry(self):- """An abandoned textual candidate cannot leak or duplicate after retry."""+ @pytest.mark.parametrize("committed", [False, True])+ async def test_literal_markup_respects_connection_commit_boundary(self, committed):+ """Literal markup follows ordinary retry and continuation boundaries."""provider = _make_provider()request = _make_request(tools=[@@ -1149,6 +1152,8 @@ class TestStreamingExceptionHandling:])abandoned = "<tool_call>\n<function=Bash>"+ if committed:+ abandoned += "x" * 66000complete = ("<tool_call>\n<function=Bash>\n<parameter=command>\n""printf retry\n</parameter>\n</function>\n</tool_call>"@@ -1184,8 +1189,10 @@ class TestStreamingExceptionHandling:]assert mock_create.await_count == 2- assert visible_text == ""- assert [block["name"] for block in tool_starts] == ["Bash"]+ assert visible_text == (abandoned + complete if committed else complete)+ assert tool_starts == []++ assert ("tools" not in mock_create.call_args_list[1].kwargs) == committed@pytest.mark.asyncioasync def test_precommit_retry_discards_abandoned_tool_id_candidate(self):@@ -2513,25 +2520,6 @@ class TestStreamingExceptionHandling:class TestProcessToolCall:"""Tests for OpenAI tool-call assembly."""- def test_heuristic_tool_use_sse_marks_committed_tool_output(self):- """Heuristic tool blocks are emitted content, even without OpenAI tool state."""- output = _make_anthropic_output()- events = list(- iter_heuristic_tool_use_events(- output,- {- "id": "toolu_heuristic",- "name": "Read",- "input": {"path": "test.py"},- },- )- )-- event_text = "".join(events)- assert "tool_use" in event_text- assert output.has_emitted_tool_block()- assert output.committed_output-def test_tool_call_with_id(self):"""Tool call with id starts a tool block."""provider = _make_provider()@@ -2856,31 +2844,6 @@ class TestProcessToolCall:assert calls is not Noneassert calls[0]["function"]["name"] == original- def test_heuristic_tool_call_restores_original_name(self):- """Complete heuristic calls share the same outbound name contract."""- original = "mcp__heuristic_output__" + "x" * 70- request = _make_request(- tools=[{"name": original, "input_schema": {"type": "object"}}]- )- codec = OpenAIToolNameCodec.from_request(request)- sse = _make_anthropic_output()-- events = list(- iter_heuristic_tool_use_events(- sse,- {- "id": "call_heuristic",- "name": codec.encode(original),- "input": {},- },- tool_names=codec,- )- )-- event_text = "".join(events)- assert original in event_text- assert codec.encode(original) not in event_text-def test_tool_call_id_arrives_before_name_still_emits_id_and_name(self):"""Split-stream tool: id (no name) then name then args; id preserved on start."""provider = _make_provider()diff --git a/tests/providers/test_structured_reasoning_lifecycle.py b/tests/providers/test_structured_reasoning_lifecycle.pynew file mode 100644index 00000000..2fb281df--- /dev/null+++ b/tests/providers/test_structured_reasoning_lifecycle.py@@ -0,0 +1,430 @@+"""Reasoning stays complete and attached while answers stream."""++import json+from unittest.mock import AsyncMock, patch++import pytest++from free_claude_code.core.anthropic.models import MessagesRequest+from free_claude_code.core.anthropic.stream_contracts import parse_sse_text+from free_claude_code.core.history_replay import (+ AssociatedReplayRecord,+ ReplayOrigin,+ decode_replay,+)+from free_claude_code.providers.history_replay import normalize_messages_history+from free_claude_code.providers.openai_chat.reasoning_details import (+ StructuredReasoningStream,+)+from free_claude_code.providers.openai_chat.stream_output import (+ AnthropicChatStreamOutput,+ ChatStreamUsage,+)+from free_claude_code.providers.openai_chat.transport import _OpenAIChatStreamRunner+from tests.providers.test_history_transports import (+ _carrier,+ _chat_reasoning_events,+ _harness,+ _saved_reply,+)+from tests.providers.test_streaming_errors import _recovery_output++[email protected]("wire", ["messages", "responses"])+async def test_distinct_native_reasoning_survives_structured_summary(wire):+ events = _chat_reasoning_events(+ [+ {+ "reasoning_details": [+ {+ "type": "reasoning.summary",+ "summary": "Short summary.",+ "index": 0,+ }+ ]+ },+ {"reasoning_content": "Distinct native thought."},+ ]+ )+ async with _harness("chat", lambda _: (200, events)) as (send, bodies, _):+ saved = await _saved_reply(+ send(wire, [{"role": "user", "content": "hello"}]), wire+ )+ await _saved_reply(+ send(wire, [*saved, {"role": "user", "content": "next"}]), wire+ )+ body = json.dumps(bodies[-1])+ assert body.count("Distinct native thought.") == 1+ assert body.count("Short summary.") == 1+++async def test_encrypted_only_reasoning_stays_with_its_answer():+ details = [{"type": "reasoning.encrypted", "data": "opaque", "index": 0}]+ events = _chat_reasoning_events([{"reasoning_details": details}])+ async with _harness("chat", lambda _: (200, events)) as (send, bodies, _):+ saved = await _saved_reply(+ send("responses", [{"role": "user", "content": "hello"}]), "responses"+ )+ assert [item["type"] for item in saved] == ["reasoning", "message"]+ await _saved_reply(+ send("responses", [*saved, {"role": "user", "content": "next"}]),+ "responses",+ )+ assistants = [msg for msg in bodies[-1]["messages"] if msg["role"] == "assistant"]+ assert len(assistants) == 1+ assert assistants[0]["content"] == "17"+ assert assistants[0]["reasoning_details"] == details+++async def test_late_metadata_does_not_overlap_messages_content_blocks():+ events = _chat_reasoning_events(+ [+ {+ "reasoning_content": "Plan.",+ "reasoning_details": [+ {"type": "reasoning.encrypted", "data": "first", "index": 0}+ ],+ },+ {"content": "Answer."},+ {+ "reasoning_details": [+ {"type": "reasoning.encrypted", "data": "second", "index": 0}+ ]+ },+ ]+ )+ async with _harness("chat", lambda _: (200, events)) as (send, bodies, _):+ stream = [+ chunk+ async for chunk in send("messages", [{"role": "user", "content": "hello"}])+ ]+ active = None+ for event in parse_sse_text("".join(stream)):+ if event.event == "content_block_start":+ assert active is None+ active = event.data["index"]+ elif event.event == "content_block_delta":+ assert event.data["index"] == active+ elif event.event == "content_block_stop":+ assert event.data["index"] == active+ active = None+ assert active is None+ saved = await _saved_reply(+ send("messages", [{"role": "user", "content": "hello"}]), "messages"+ )+ await _saved_reply(+ send("messages", [*saved, {"role": "user", "content": "next"}]), "messages"+ )+ assert bodies[-1]["messages"][0]["reasoning_details"] == [+ {"type": "reasoning.encrypted", "data": "firstsecond", "index": 0}+ ]++[email protected]("destination", ["chat", "messages", "responses"])[email protected]("readable", [False, True])+async def test_associated_messages_replay_into_every_transport(destination, readable):+ first = {+ "reasoning_details": [+ {"type": "reasoning.encrypted", "data": "first", "index": 0}+ ]+ }+ if readable:+ first["reasoning_content"] = "Plan."+ events = _chat_reasoning_events(+ [+ first,+ {"content": "Answer."},+ {+ "reasoning_details": [+ {"type": "reasoning.encrypted", "data": "second", "index": 0}+ ]+ },+ ]+ )+ async with _harness("chat", lambda _: (200, events)) as (send, _, _):+ saved = await _saved_reply(+ send("messages", [{"role": "user", "content": "hello"}]), "messages"+ )+ history = [*saved, {"role": "user", "content": "next"}]+ before = json.dumps(history)+ async with _harness(destination) as (send, bodies, _):+ await _saved_reply(send("messages", history), "messages")+ assert json.dumps(history) == before+ body = json.dumps(bodies[-1])+ assert "fcc:history" not in body+ assert body.count("Plan.") == int(readable)+ if destination == "chat":+ assert bodies[-1]["messages"][0]["reasoning_details"] == [+ {"type": "reasoning.encrypted", "data": "firstsecond", "index": 0}+ ]+ else:+ assert "firstsecond" not in body+++def _messages_writer():+ output = AnthropicChatStreamOutput(+ message_id="msg_test", model="public", input_tokens=1+ )+ output.replay_origin = ReplayOrigin("test", "chat", "endpoint", "account", "model")+ reasoning = StructuredReasoningStream()+ output.reasoning_replay = reasoning+ return output, reasoning+++def _readable(reasoning, output, text="Plan.", data="first"):+ return list(+ reasoning.events(+ {+ "reasoning_details": [+ {"type": "reasoning.encrypted", "data": data, "index": 0}+ ]+ },+ output,+ native_reasoning=text,+ )+ )+++def _assert_serial(events):+ active = None+ for event in events:+ if event.event == "content_block_start":+ assert active is None+ active = event.data["index"]+ elif event.event in {"content_block_delta", "content_block_stop"}:+ assert event.data["index"] == active+ if event.event == "content_block_stop":+ active = None+ assert active is None+++ "terminal", ["normal", "length", "failure", "tool", "continuation"]+)+def test_immediate_text_and_late_replay_survive_every_exit(terminal):+ output, reasoning = _messages_writer()+ frames = output.start_events() + _readable(reasoning, output)+ frames += output.ensure_text_block()+ frames.append(output.emit_text_delta("Answer."))+ # Text has already been emitted, while upstream metadata and termination are pending.+ early = parse_sse_text("".join(frames))+ assert any(e.data.get("delta", {}).get("text") == "Answer." for e in early)+ assert not any(e.event == "message_stop" for e in early)+ if terminal == "tool":+ frames += output.close_content_blocks()+ frames.append(output.start_tool_block(0, "call_a", "lookup"))+ frames.append(output.emit_tool_delta(0, '{"query":"x"}'))+ frames += list(+ reasoning.events(+ {+ "reasoning_details": [+ {"type": "reasoning.encrypted", "data": "second", "index": 0}+ ]+ },+ output,+ native_reasoning=None,+ )+ )+ if terminal == "continuation":+ frames += output.flush_reasoning_replay()+ frames += output.ensure_reasoning_block()+ frames.append(output.emit_reasoning_delta("Recovery thought."))+ frames += output.ensure_text_block()+ frames.append(output.emit_text_delta(" Continued."))+ if terminal == "failure":+ frames += output.close_unclosed_blocks()+ assert output.close_unclosed_blocks() == []+ else:+ frames += output.finish_success(+ stop_reason="max_tokens" if terminal == "length" else "end_turn",+ usage=ChatStreamUsage(input_tokens=1, output_tokens=4),+ )+ assert (+ output.finish_success(+ stop_reason="end_turn",+ usage=ChatStreamUsage(input_tokens=1, output_tokens=4),+ )+ == []+ )+ events = parse_sse_text("".join(frames))+ _assert_serial(events)+ finals = [+ decode_replay(e.data["content_block"]["data"])+ for e in events+ if e.event == "content_block_start"+ and e.data["content_block"]["type"] == "redacted_thinking"+ ]+ assert len(finals) == 1+ assert isinstance(finals[0], AssociatedReplayRecord)+ assert finals[0].part == "final"+ assert finals[0].native == {+ "reasoning_content": "Plan.",+ "reasoning_details": [+ {"type": "reasoning.encrypted", "data": "firstsecond", "index": 0}+ ],+ }+++def test_request_copy_preserves_routing_and_does_not_join_across_messages():+ output, reasoning = _messages_writer()+ frames = output.start_events() + _readable(reasoning, output)+ frames += output.ensure_text_block()+ frames.append(output.emit_text_delta("answer"))+ frames += output.finish_success(+ stop_reason="end_turn", usage=ChatStreamUsage(input_tokens=1, output_tokens=1)+ )+ events = parse_sse_text("".join(frames))+ anchor = next(+ e.data["delta"]["signature"]+ for e in events+ if e.data.get("delta", {}).get("type") == "signature_delta"+ )+ final = next(+ e.data["content_block"]["data"]+ for e in events+ if e.event == "content_block_start"+ and e.data["content_block"]["type"] == "redacted_thinking"+ )+ request = MessagesRequest(+ model="route",+ original_model="original",+ resolved_provider_model="actual",+ messages=[+ {+ "role": "assistant",+ "content": [+ {"type": "thinking", "thinking": "Plan.", "signature": anchor}+ ],+ },+ {"role": "user", "content": "next"},+ {+ "role": "assistant",+ "content": [{"type": "redacted_thinking", "data": final}],+ },+ ],+ )+ before = request.model_dump_json()+ normalized = normalize_messages_history(request)+ assert normalized.original_model == "original"+ assert normalized.resolved_provider_model == "actual"+ assert len(normalized.messages) == 3+ assert request.model_dump_json() == before+ assert "fcc:history:v2:" not in normalized.model_dump_json()+ assert normalize_messages_history(normalized) == normalized++[email protected]("wire", ["messages", "responses"])[email protected]("committed", [False, True])+async def test_structured_replay_respects_retry_and_continuation_boundary(+ wire, committed+):+ prefix = "x" * 66000 if committed else "partial"+ first = _chat_reasoning_events(+ [+ {+ "reasoning_content": "Original thought.",+ "reasoning_details": [+ {"type": "reasoning.encrypted", "data": "original", "index": 0}+ ],+ },+ {"content": prefix},+ ]+ )[:-1]+ second = _chat_reasoning_events(+ [+ {+ "reasoning_content": "Recovery thought.",+ "reasoning_details": [+ {"type": "reasoning.encrypted", "data": "replacement", "index": 0}+ ],+ },+ {"content": "continued"},+ ]+ )+ async with _harness(+ "chat", lambda bodies: (200, first if len(bodies) == 1 else second)+ ) as (send, bodies, _):+ saved = await _saved_reply(+ send(wire, [{"role": "user", "content": "hello"}]), wire+ )+ assert len(bodies) == 2+ record = decode_replay(_carrier(saved, wire))+ assert record.native["reasoning_content"] == (+ "Original thought." if committed else "Recovery thought."+ )+ assert record.native["reasoning_details"] == [+ {+ "type": "reasoning.encrypted",+ "data": "original" if committed else "replacement",+ "index": 0,+ }+ ]+ assert "continued" in json.dumps(saved)+ if not committed:+ assert "Original thought." not in json.dumps(saved)++[email protected]("wire", ["messages", "responses"])+async def test_first_metadata_after_text_does_not_invent_earlier_reasoning(wire):+ events = _chat_reasoning_events(+ [+ {"content": "Earlier answer."},+ {+ "reasoning_details": [+ {"type": "reasoning.encrypted", "data": "late", "index": 0}+ ]+ },+ ]+ )+ async with _harness("chat", lambda _: (200, events)) as (send, _, _):+ if wire == "messages":+ frames = [+ part+ async for part in send(wire, [{"role": "user", "content": "hello"}])+ ]+ _assert_serial(parse_sse_text("".join(frames)))+ saved = await _saved_reply(+ send(wire, [{"role": "user", "content": "hello"}]), wire+ )+ assert (+ saved[0]["type"] == "message"+ if wire == "responses"+ else saved[0]["content"][0]["type"] == "text"+ )+++async def test_finishing_replay_does_not_make_empty_continuation_successful():+ events = _chat_reasoning_events(+ [+ {+ "reasoning_content": "Plan.",+ "reasoning_details": [+ {"type": "reasoning.encrypted", "data": "opaque", "index": 0}+ ],+ },+ {"content": "x" * 66000},+ ]+ )[:-1]+ async with _harness("chat", lambda _: (200, events)) as (send, _, _):+ with patch.object(+ _OpenAIChatStreamRunner,+ "_collect_recovery_output",+ new_callable=AsyncMock,+ return_value=_recovery_output(),+ ):+ frames = [+ frame+ async for frame in send(+ "responses", [{"role": "user", "content": "hello"}]+ )+ ]+ parsed = parse_sse_text("".join(frames))+ assert parsed[-1].event == "response.failed"+ assert not any(event.event == "response.completed" for event in parsed)diff --git a/tests/providers/test_subagent_interception.py b/tests/providers/test_subagent_interception.pyindex 51f40f84..f098e8a3 100644--- a/tests/providers/test_subagent_interception.py+++ b/tests/providers/test_subagent_interception.py@@ -1,4 +1,3 @@-import copyimport jsonimport pytest@@ -17,7 +16,6 @@ from free_claude_code.providers.openai_chat.stream_output import ()from free_claude_code.providers.openai_chat.tool_calls import (OpenAIToolCallAssembler,- iter_heuristic_tool_use_events,)@@ -44,28 +42,6 @@ def _argument_deltas(frames: list[str]) -> list[str]:return parts- "arguments",- [- {"run_in_background": True},- {"run_in_background": False},- {"run_in_background": None},- {"prompt": "inspect"},- ],-)-def test_heuristic_task_preserves_arguments(output, arguments):- tool_use = {- "type": "tool_use",- "id": "call_task",- "name": "Task",- "input": {**arguments, "nested": {"items": [1, 2]}},- }- original = copy.deepcopy(tool_use)- frames = list(iter_heuristic_tool_use_events(output, tool_use))- assert json.loads("".join(_argument_deltas(frames))) == original["input"]- assert tool_use == original--@pytest.mark.parametrize("name", ["Task", "ordinary_tool"])@pytest.mark.parametrize("arguments",@@ -106,23 +82,12 @@ def test_tool_arguments_stream_without_name_specific_rewrites(output, name, arguassert output.tool_states[0].content == argumentsassert output.tool_states[0].tool_id == "call_task"events = parse_sse_text("".join(frames))- if isinstance(output, ResponsesChatStreamOutput):- valid = arguments in (- '{"run_in_background":true,"prompt":"inspect"}',- "{}",- '{"run_in_background":null}',- )- assert any(e.event == "response.completed" for e in events) == valid- assert any(e.event == "response.failed" for e in events) != valid- if valid:- assert json.loads("".join(_argument_deltas(frames))) == json.loads(- arguments- )- else:- assert not _argument_deltas(frames)- else:- assert events[-1].event == "message_stop"- assert "".join(_argument_deltas(frames)) == arguments+ assert events[-1].event == (+ "response.completed"+ if isinstance(output, ResponsesChatStreamOutput)+ else "message_stop"+ )+ assert "".join(_argument_deltas(frames)) == argumentsdef test_task_argument_aliases_are_restored_recursively(output):