Skip to content

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 reasoning and finish metadata before answers (#2013)

Ali Khokhare394c633f85d
diff --git a/src/free_claude_code/core/anthropic/conversion.py b/src/free_claude_code/core/anthropic/conversion.py
index 80e24922..17fa5ec1 100644
--- a/src/free_claude_code/core/anthropic/conversion.py
+++ b/src/free_claude_code/core/anthropic/conversion.py
@@ -474,6 +474,7 @@ class AnthropicToOpenAIConverter:
messages: list[Any],
*,
reasoning_replay: ReasoningReplayMode = ReasoningReplayMode.THINK_TAGS,
+ structured_reasoning_details: bool = False,
) -> list[dict[str, Any]]:
ledger = _OpenAIChatHistoryLedger()
@@ -493,6 +494,7 @@ class AnthropicToOpenAIConverter:
content,
reasoning_content=reasoning_content,
reasoning_replay=reasoning_replay,
+ structured_reasoning_details=structured_reasoning_details,
)
for segment in segments:
if isinstance(segment, _PlainSegment):
@@ -511,6 +513,7 @@ class AnthropicToOpenAIConverter:
*,
reasoning_content: str | None,
reasoning_replay: ReasoningReplayMode,
+ structured_reasoning_details: bool = False,
) -> list[_TranscriptSegment]:
if role == "system":
system_text = _openai_system_text(
@@ -546,6 +549,7 @@ class AnthropicToOpenAIConverter:
first_tool_index=first_i,
reasoning_content=reasoning_content,
reasoning_replay=reasoning_replay,
+ structured_reasoning_details=structured_reasoning_details,
)
]
for block in content:
@@ -556,6 +560,7 @@ class AnthropicToOpenAIConverter:
content,
reasoning_content=reasoning_content,
reasoning_replay=reasoning_replay,
+ structured_reasoning_details=structured_reasoning_details,
)
)
]
@@ -595,6 +600,7 @@ class AnthropicToOpenAIConverter:
first_tool_index: int,
reasoning_content: str | None,
reasoning_replay: ReasoningReplayMode,
+ structured_reasoning_details: bool = False,
) -> _ToolTurnSegment:
pre = [
block
@@ -609,6 +615,7 @@ class AnthropicToOpenAIConverter:
content,
reasoning_content=reasoning_content,
reasoning_replay=reasoning_replay,
+ structured_reasoning_details=structured_reasoning_details,
)[0],
required_tool_ids=[],
)
@@ -634,6 +641,7 @@ class AnthropicToOpenAIConverter:
pre,
reasoning_content=reasoning_content,
reasoning_replay=reasoning_replay,
+ structured_reasoning_details=structured_reasoning_details,
)[0]
pre_msg["tool_calls"] = tool_calls
if reasoning_replay is ReasoningReplayMode.REASONING_CONTENT:
@@ -653,6 +661,7 @@ class AnthropicToOpenAIConverter:
*,
reasoning_content: str | None = None,
reasoning_replay: ReasoningReplayMode = ReasoningReplayMode.THINK_TAGS,
+ structured_reasoning_details: bool = False,
) -> list[dict[str, Any]]:
content_parts: list[str] = []
thinking_parts: list[str] = []
@@ -665,11 +674,22 @@ class AnthropicToOpenAIConverter:
content_parts.append(get_block_attr(block, "text", ""))
elif block_type == "thinking":
signature = get_block_attr(block, "signature", None)
+ thinking = get_block_attr(block, "thinking", "")
if is_replay(signature):
details.extend(reasoning_detail(signature))
if has_readable_replay(signature):
continue
- thinking = get_block_attr(block, "thinking", "")
+ elif isinstance(signature, str) and signature:
+ if structured_reasoning_details:
+ details.append(
+ {
+ "type": "reasoning.text",
+ "text": thinking,
+ "signature": signature,
+ }
+ )
+ continue
+ details.append({"type": "reasoning.encrypted", "data": signature})
if reasoning_replay == ReasoningReplayMode.DISABLED:
if thinking:
content_parts.append(reasoning_context(thinking))
@@ -836,12 +856,14 @@ def build_base_request_body(
*,
default_max_tokens: int | None = None,
reasoning_replay: ReasoningReplayMode = ReasoningReplayMode.THINK_TAGS,
+ structured_reasoning_details: bool = False,
) -> dict[str, Any]:
"""Build the common parts of an OpenAI-format request body."""
_openai_reject_native_only_top_level_fields(request_data)
messages = AnthropicToOpenAIConverter.convert_messages(
request_data.messages,
reasoning_replay=reasoning_replay,
+ structured_reasoning_details=structured_reasoning_details,
)
system = request_data.system
diff --git a/src/free_claude_code/core/anthropic/native_stream.py b/src/free_claude_code/core/anthropic/native_stream.py
index 11a42726..7c5d7544 100644
--- a/src/free_claude_code/core/anthropic/native_stream.py
+++ b/src/free_claude_code/core/anthropic/native_stream.py
@@ -2,13 +2,8 @@
import json
from collections.abc import Mapping
-from dataclasses import dataclass, field, replace
+from dataclasses import dataclass, field
-from free_claude_code.core.history_replay import (
- ReplayOrigin,
- ReplayRecord,
- encode_replay,
-)
from free_claude_code.core.json_types import JsonObject, JsonValue
from .native import NativeMessagesError
@@ -239,11 +234,8 @@ class NativeMessagesStreamState:
class NativeMessagesRelay:
"""Preserve upstream IDs, indexes, fields and event order for Messages clients."""
- def __init__(
- self, *, public_model: str, replay_origin: ReplayOrigin | None = None
- ) -> None:
+ def __init__(self, *, public_model: str) -> None:
self._public_model = public_model
- self._replay_origin = replay_origin
self._state = NativeMessagesStreamState()
@property
@@ -255,56 +247,14 @@ class NativeMessagesRelay:
return self._state.stop_reason
def feed(self, event_type: str, payload: Mapping[str, JsonValue]) -> str:
- completed = self._state.accept(event_type, payload)
+ self._state.accept(event_type, payload)
body = dict(payload)
if event_type == "message_start":
message = body.get("message")
if not isinstance(message, dict):
raise AssertionError("Validated message_start must contain a message.")
body["message"] = {**message, "model": self._public_model}
- if (
- self._replay_origin is not None
- and isinstance(message.get("model"), str)
- and message["model"]
- ):
- self._replay_origin = replace(
- self._replay_origin, model=message["model"]
- )
- prefix = ""
- if self._replay_origin is not None:
- block = body.get("content_block")
- if event_type == "content_block_start" and isinstance(block, dict):
- if block.get("type") == "thinking":
- body["content_block"] = {**block, "signature": ""}
- elif block.get("type") == "redacted_thinking":
- body["content_block"] = {
- **block,
- "data": encode_replay(ReplayRecord(self._replay_origin, block)),
- }
- delta = body.get("delta")
- if (
- event_type == "content_block_delta"
- and isinstance(delta, dict)
- and delta.get("type") == "signature_delta"
- ):
- return ""
- if (
- completed is not None
- and completed.body.get("type") == "thinking"
- and completed.body.get("signature")
- ):
- signature = encode_replay(
- ReplayRecord(self._replay_origin, completed.body)
- )
- prefix = _event(
- "content_block_delta",
- {
- "type": "content_block_delta",
- "index": body["index"],
- "delta": {"type": "signature_delta", "signature": signature},
- },
- )
- return prefix + _event(event_type, body)
+ return _event(event_type, body)
def _event(kind: str, payload: JsonObject) -> str:
diff --git a/src/free_claude_code/core/delivered_response.py b/src/free_claude_code/core/delivered_response.py
index 0a7f777a..3a03d322 100644
--- a/src/free_claude_code/core/delivered_response.py
+++ b/src/free_claude_code/core/delivered_response.py
@@ -6,8 +6,6 @@ from typing import Any, Literal
import simplejson
from .history_replay import (
- ReplayRecord,
- encode_replay,
readable_reasoning,
unencrypted_responses_replay,
)
@@ -495,18 +493,6 @@ class DeliveredResponse:
}
)
closed_item = {**deepcopy(item), "status": "completed"}
- if (
- record := unencrypted_responses_replay(
- item.get("encrypted_content")
- )
- ) is not None:
- native = {**record.native, "status": "completed"}
- for field in ("content", "summary"):
- if field in closed_item:
- native[field] = deepcopy(closed_item[field])
- closed_item["encrypted_content"] = encode_replay(
- ReplayRecord(record.origin, native)
- )
events.append(
{
"type": "response.output_item.done",
diff --git a/src/free_claude_code/core/history_replay.py b/src/free_claude_code/core/history_replay.py
index 9c05ed1a..08fc6bef 100644
--- a/src/free_claude_code/core/history_replay.py
+++ b/src/free_claude_code/core/history_replay.py
@@ -265,6 +265,7 @@ def readable_reasoning(item: Mapping[str, JsonValue]) -> list[tuple[str, bool]]:
if item.get("type") == "thinking":
text = str(item.get("thinking") or "")
return [(text, False)] if text else []
+ fields: list[tuple[str, bool]] = []
for key in ("content", "summary"):
parts = item.get(key)
if isinstance(parts, list):
@@ -273,8 +274,10 @@ def readable_reasoning(item: Mapping[str, JsonValue]) -> list[tuple[str, bool]]:
for part in parts
if isinstance(part, dict) and isinstance(part.get("text"), str)
)
- if text:
- return [(text, key == "summary")]
+ if text and text not in {value for value, _ in fields}:
+ fields.append((text, key == "summary"))
+ if fields:
+ return fields
details = item.get("reasoning_details")
parts: list[tuple[str, bool]] = []
if isinstance(details, list):
@@ -362,33 +365,6 @@ def validate_hosted_tool_history(blocks: list[JsonObject]) -> None:
)
-def responses_replay_item(item: JsonObject, origin: ReplayOrigin) -> JsonObject:
- result = deepcopy(item)
- result["encrypted_content"] = encode_replay(ReplayRecord(origin, item))
- return result
-
-
-def preserve_responses_reasoning(
- payload: JsonObject, origin: ReplayOrigin
-) -> JsonObject:
- """Wrap complete native reasoning before a presenter rewrites public metadata."""
- result = deepcopy(payload)
- item = result.get("item")
- if isinstance(item, dict) and item.get("type") == "reasoning":
- result["item"] = responses_replay_item(item, origin)
- response = result.get("response")
- if isinstance(response, dict):
- output = response.get("output")
- if isinstance(output, list):
- response["output"] = [
- responses_replay_item(item, origin)
- if isinstance(item, dict) and item.get("type") == "reasoning"
- else item
- for item in output
- ]
- return result
-
-
def reasoning_detail(value: str) -> list[JsonValue]:
"""Decode older Chat detail strings while retaining unknown opaque state."""
try:
@@ -420,6 +396,16 @@ def responses_reasoning_blocks(item: Mapping[str, JsonValue]) -> list[JsonValue]
carrier = item.get("encrypted_content")
if isinstance(carrier, str) and carrier:
record = _record(item, "encrypted_content")
+ if record is None:
+ readable = readable_reasoning(item)
+ if readable or item.get("content"):
+ return [
+ {
+ "type": "thinking",
+ "thinking": "\n\n".join(text for text, _ in readable),
+ "signature": carrier,
+ }
+ ]
if (
record is not None
and record.origin.protocol == "messages"
diff --git a/src/free_claude_code/core/openai_responses/chat_request.py b/src/free_claude_code/core/openai_responses/chat_request.py
index 40bfc71a..0f02ea48 100644
--- a/src/free_claude_code/core/openai_responses/chat_request.py
+++ b/src/free_claude_code/core/openai_responses/chat_request.py
@@ -63,33 +63,18 @@ class ResponsesChatRequest:
@dataclass(slots=True)
class _PendingReasoning:
- text: str | None = None
- encrypted: list[str] = field(default_factory=list)
- contexts: list[str] = field(default_factory=list)
+ items: list[Mapping[str, JsonValue]] = field(default_factory=list)
def add(self, item: Mapping[str, JsonValue]) -> None:
- if encrypted := encrypted_reasoning_from_item(item):
- self.encrypted.append(encrypted)
- if has_readable_replay(encrypted):
- return
- for text, summary in readable_reasoning(item):
- if summary:
- self.contexts.append(reasoning_context(text, summary=True))
- else:
- self.text = combine_reasoning(self.text, text)
+ self.items.append(item)
@property
def empty(self) -> bool:
- return self.text is None and not self.encrypted and not self.contexts
+ return not self.items
- def take(self) -> tuple[str | None, list[str], list[str]]:
- text = self.text
- encrypted = list(self.encrypted)
- self.text = None
- self.encrypted.clear()
- contexts = list(self.contexts)
- self.contexts.clear()
- return text, encrypted, contexts
+ def take(self) -> list[Mapping[str, JsonValue]]:
+ items, self.items = self.items, []
+ return items
class _ResponsesChatInputBuilder:
@@ -290,6 +275,9 @@ class _ResponsesChatInputBuilder:
def _flush_reasoning(self) -> None:
if self._pending_reasoning.empty:
return
+ if self.messages and self.messages[-1].get("role") == "assistant":
+ self._apply_pending_reasoning(self.messages[-1])
+ return
message: dict[str, object] = {"role": "assistant", "content": ""}
self._apply_pending_reasoning(message)
if len(message) > 2 or message.get("content"):
@@ -312,21 +300,61 @@ class _ResponsesChatInputBuilder:
self._pending_rich_output_parts.clear()
def _apply_pending_reasoning(self, message: dict[str, object]) -> None:
- text, encrypted, contexts = self._pending_reasoning.take()
+ contexts: list[str] = []
+ details: list[JsonValue] = []
+ for item in self._pending_reasoning.take():
+ encrypted = encrypted_reasoning_from_item(item)
+ if encrypted and has_readable_replay(encrypted):
+ details.extend(reasoning_detail(encrypted))
+ continue
+ readable = readable_reasoning(item)
+ full_text = [text for text, summary in readable if not summary]
+ content = item.get("content")
+ signed = (
+ self._structured_reasoning_details
+ and encrypted
+ and not is_replay(encrypted)
+ and (
+ full_text
+ or (
+ isinstance(content, list)
+ and any(
+ isinstance(part, dict)
+ and part.get("type") == "reasoning_text"
+ and part.get("text") == ""
+ for part in content
+ )
+ )
+ )
+ )
+ if signed:
+ details.append(
+ {
+ "type": "reasoning.text",
+ "text": "".join(full_text),
+ "signature": encrypted,
+ }
+ )
+ else:
+ for text in full_text:
+ _apply_reasoning_text(message, text, self._reasoning_replay)
+ for text, summary in readable:
+ if not summary:
+ continue
+ if self._structured_reasoning_details:
+ details.append({"type": "reasoning.summary", "summary": text})
+ else:
+ contexts.append(reasoning_context(text, summary=True))
+ if encrypted and not signed:
+ details.extend(reasoning_detail(encrypted))
if contexts:
message["content"] = "\n\n".join(
[*contexts, str(message.get("content") or "")]
).rstrip()
- if text is not None:
- _apply_reasoning_text(message, text, self._reasoning_replay)
- if encrypted:
- details = message.setdefault("reasoning_details", [])
- if isinstance(details, list):
- for value in encrypted:
- if self._structured_reasoning_details or is_replay(value):
- details.extend(reasoning_detail(value))
- if not details:
- message.pop("reasoning_details", None)
+ if details:
+ existing = message.setdefault("reasoning_details", [])
+ if isinstance(existing, list):
+ existing.extend(details)
def build_responses_chat_request(
diff --git a/src/free_claude_code/core/openai_responses/messages_stream.py b/src/free_claude_code/core/openai_responses/messages_stream.py
index 293eb20e..4d2bb58b 100644
--- a/src/free_claude_code/core/openai_responses/messages_stream.py
+++ b/src/free_claude_code/core/openai_responses/messages_stream.py
@@ -4,7 +4,6 @@ import json
import time
import uuid
from collections.abc import Mapping
-from dataclasses import replace
from typing import cast
from free_claude_code.core.anthropic.native import NativeMessagesError
@@ -13,11 +12,6 @@ from free_claude_code.core.anthropic.native_stream import (
NativeMessagesStreamState,
)
from free_claude_code.core.failures import ExecutionFailure
-from free_claude_code.core.history_replay import (
- ReplayOrigin,
- ReplayRecord,
- encode_replay,
-)
from free_claude_code.core.json_types import JsonObject, JsonValue
from .errors import openai_error_from_failure
@@ -116,12 +110,10 @@ class AnthropicToResponsesStream:
*,
public_model: str,
tool_identities: Mapping[str, ResponsesToolIdentity],
- replay_origin: ReplayOrigin,
) -> None:
self._request = request
self._public_model = public_model
self._identities = tool_identities
- self._replay_origin = replay_origin
self._response_id = new_response_id()
self._created_at = int(time.time())
self._native = NativeMessagesStreamState()
@@ -190,10 +182,6 @@ class AnthropicToResponsesStream:
if not isinstance(message, Mapping):
raise AssertionError("Validated message_start must contain a message.")
self._usage.update(message.get("usage"))
- if isinstance(message.get("model"), str) and message["model"]:
- self._replay_origin = replace(
- self._replay_origin, model=message["model"]
- )
self._started = True
return [self._events.response_created(self._payload("in_progress"))]
if event_type == "message_delta":
@@ -352,9 +340,12 @@ class AnthropicToResponsesStream:
"Native block has no corresponding Responses item."
)
if isinstance(state, ReasoningBlockState):
- state.encrypted_content = encode_replay(
- ReplayRecord(self._replay_origin, completed.body)
- )
+ field = "signature" if completed.body.get("type") == "thinking" else "data"
+ value = completed.body.get(field)
+ if isinstance(value, str) and value:
+ state.encrypted_content = value
+ if completed.body.get("type") == "thinking" and not state.text_parts:
+ state.text_parts.append("")
elif isinstance(state, ToolBlockState):
arguments = completed.tool_arguments
if arguments is None:
diff --git a/src/free_claude_code/core/openai_responses/provider_input.py b/src/free_claude_code/core/openai_responses/provider_input.py
index d6d11396..0249525c 100644
--- a/src/free_claude_code/core/openai_responses/provider_input.py
+++ b/src/free_claude_code/core/openai_responses/provider_input.py
@@ -1,7 +1,7 @@
"""Convert Anthropic Messages into an upstream OpenAI Responses request."""
import json
-from typing import Any, cast
+from typing import Any
from free_claude_code.core.anthropic.content import get_block_attr, get_block_type
from free_claude_code.core.anthropic.conversion import resolve_anthropic_tool_choice
@@ -20,9 +20,6 @@ from free_claude_code.core.anthropic.tool_results import (
)
from free_claude_code.core.history_replay import (
HistoryReplayError,
- ReplayOrigin,
- ReplayRecord,
- encode_replay,
has_readable_replay,
is_replay,
tool_history_context,
@@ -34,6 +31,7 @@ from free_claude_code.core.reasoning import ReasoningPolicy
from free_claude_code.core.tool_schema_patterns import translate_tool_schema_patterns
from .errors import ResponsesConversionError
+from .ids import new_reasoning_item_id
from .reasoning import responses_reasoning_config
@@ -214,6 +212,7 @@ def _assistant_items(
):
items.append(
{
+ "id": new_reasoning_item_id(),
"type": "reasoning",
"content": [{"type": "reasoning_text", "text": reasoning_content}],
"summary": [],
@@ -229,27 +228,15 @@ def _assistant_items(
flush_text()
if kind == "thinking":
signature = get_block_attr(block, "signature", None)
- item: dict[str, Any] = {"type": "reasoning", "summary": []}
+ item: dict[str, Any] = {
+ "id": new_reasoning_item_id(),
+ "type": "reasoning",
+ "summary": [],
+ }
if isinstance(signature, str) and signature:
- native = (
- block
- if isinstance(block, dict)
- else block.model_dump(mode="json", exclude_none=True)
- )
- item["encrypted_content"] = (
- signature
- if is_replay(signature)
- else encode_replay(
- ReplayRecord(
- ReplayOrigin(
- "unattributed/messages", "messages", "", "", "unknown"
- ),
- cast(JsonObject, native),
- )
- )
- )
+ item["encrypted_content"] = signature
if not signature or (
- is_replay(signature) and not has_readable_replay(signature)
+ not is_replay(signature) or not has_readable_replay(signature)
):
item["content"] = [
{
@@ -261,6 +248,7 @@ def _assistant_items(
elif kind == "redacted_thinking":
items.append(
{
+ "id": new_reasoning_item_id(),
"type": "reasoning",
"summary": [],
"encrypted_content": str(get_block_attr(block, "data", "")),
diff --git a/src/free_claude_code/core/openai_responses/provider_stream.py b/src/free_claude_code/core/openai_responses/provider_stream.py
index 2bb5fa17..4911aa4f 100644
--- a/src/free_claude_code/core/openai_responses/provider_stream.py
+++ b/src/free_claude_code/core/openai_responses/provider_stream.py
@@ -1,11 +1,10 @@
"""Translate upstream OpenAI Responses events into Anthropic Messages SSE."""
-from dataclasses import dataclass
+from dataclasses import dataclass, field
from typing import Any
from free_claude_code.core.anthropic.streaming import AnthropicStreamLedger
from free_claude_code.core.anthropic.usage import anthropic_input_usage_fields
-from free_claude_code.core.history_replay import is_replay
from free_claude_code.core.openai_tool_names import OpenAIToolNameCodec
@@ -39,6 +38,18 @@ class _ToolState:
stopped: bool = False
+@dataclass(slots=True)
+class _ReasoningState:
+ block_index: int | None = None
+ parts: dict[tuple[str, int], str] = field(default_factory=dict)
+ stopped: bool = False
+
+ def text(self, field_name: str) -> str:
+ return "".join(
+ text for (name, _), text in sorted(self.parts.items()) if name == field_name
+ )
+
+
class ResponsesProviderStream:
"""Stateful one-way adapter for one upstream response."""
@@ -63,7 +74,7 @@ class ResponsesProviderStream:
self.generated_output = False
self._tool_names = tool_names or OpenAIToolNameCodec.from_names(())
self._tools: dict[str, _ToolState] = {}
- self._encrypted_reasoning: dict[str, str] = {}
+ self._reasoning: dict[str, _ReasoningState] = {}
def start(self) -> list[str]:
"""Return the Anthropic message_start event."""
@@ -81,7 +92,9 @@ class ResponsesProviderStream:
"response.reasoning_text.delta",
"response.reasoning_summary_text.delta",
}:
- return self._reasoning_delta(data)
+ return self._reasoning_delta(
+ data, summary=event_type == "response.reasoning_summary_text.delta"
+ )
if event_type == "response.output_text.delta":
return self._text_delta(data)
if event_type == "response.function_call_arguments.delta":
@@ -105,26 +118,90 @@ class ResponsesProviderStream:
call_id=_string(item.get("call_id")) or item_id,
name=self._tool_names.decode(_string(item.get("name"))),
)
- if item.get("type") == "reasoning" and item_id:
- encrypted = item.get("encrypted_content")
- if isinstance(encrypted, str) and encrypted:
- self._encrypted_reasoning[item_id] = encrypted
+ if item.get("type") == "message" and self.ledger.blocks.text_started:
+ return [self.ledger.stop_text_block()]
+ if item.get("type") == "reasoning":
+ return self._item_readable(item, completed=False)
return []
- def _reasoning_delta(self, data: dict[str, Any]) -> list[str]:
+ def _reasoning_delta(
+ self, data: dict[str, Any], *, summary: bool = False
+ ) -> list[str]:
delta = data.get("delta")
if not isinstance(delta, str) or not delta:
return []
- events = list(self.ledger.ensure_thinking_block())
- events.append(self.ledger.emit_thinking_delta(delta))
+ state = self._reasoning.setdefault(
+ _string(data.get("item_id")), _ReasoningState()
+ )
+ field_name = "summary" if summary else "content"
+ key = (field_name, _integer(data.get(field_name + "_index")) or 0)
+ events = self._ensure_reasoning_started(state, completed=False)
+ state.parts[key] = state.parts.get(key, "") + delta
+ assert state.block_index is not None
+ events.append(
+ self.ledger.content_block_delta(state.block_index, "thinking_delta", delta)
+ )
self.generated_output = True
return events
+ def _ensure_reasoning_started(
+ self, state: _ReasoningState, *, completed: bool
+ ) -> list[str]:
+ events = [] if completed else self._close_text()
+ if state.block_index is None:
+ state.block_index = self.ledger.blocks.allocate_index()
+ events.append(
+ self.ledger.content_block_start(state.block_index, "thinking")
+ )
+ self.generated_output = True
+ return events
+
+ def _item_readable(self, item: dict[str, Any], *, completed: bool) -> list[str]:
+ item_id = _string(item.get("id"))
+ state = self._reasoning.setdefault(item_id, _ReasoningState())
+ events: list[str] = []
+ for field_name in ("content", "summary"):
+ parts = item.get(field_name)
+ if not isinstance(parts, list):
+ continue
+ text_parts = [
+ (index, part["text"])
+ for index, part in enumerate(parts)
+ if isinstance(part, dict) and isinstance(part.get("text"), str)
+ ]
+ full_text = "".join(text for _, text in text_parts)
+ alternate = state.text("summary" if field_name == "content" else "content")
+ for index, text in text_parts:
+ key = (field_name, index)
+ previous = state.parts.get(key, "")
+ state.parts[key] = text
+ if not text or full_text == alternate:
+ continue
+ remaining = text.removeprefix(previous)
+ if remaining:
+ events.extend(
+ self._ensure_reasoning_started(state, completed=completed)
+ )
+ assert state.block_index is not None
+ events.append(
+ self.ledger.content_block_delta(
+ state.block_index, "thinking_delta", remaining
+ )
+ )
+ return events
+
+ def _close_text(self) -> list[str]:
+ return (
+ [self.ledger.stop_text_block()] if self.ledger.blocks.text_started else []
+ )
+
def _text_delta(self, data: dict[str, Any]) -> list[str]:
delta = data.get("delta")
if not isinstance(delta, str) or not delta:
return []
- events = list(self.ledger.ensure_text_block())
+ events = (
+ [] if self.ledger.blocks.text_started else [self.ledger.start_text_block()]
+ )
events.append(self.ledger.emit_text_delta(delta))
self.generated_output = True
return events
@@ -142,7 +219,7 @@ class ResponsesProviderStream:
name="",
)
self._tools[item_id] = state
- events = list(self.ledger.close_content_blocks())
+ events = [] if state.started else self._close_text()
events.extend(self._ensure_tool_started(state))
if delta:
events.append(self.ledger.emit_tool_delta(state.tool_index, delta))
@@ -167,7 +244,7 @@ class ResponsesProviderStream:
self._tools[item_id] = state
if not state.name:
state.name = self._tool_names.decode(_string(item.get("name")))
- events = list(self.ledger.close_content_blocks())
+ events = [] if state.started else self._close_text()
events.extend(self._ensure_tool_started(state))
arguments = item.get("arguments")
if not state.received_delta and isinstance(arguments, str) and arguments:
@@ -180,19 +257,33 @@ class ResponsesProviderStream:
return events
if item_type == "reasoning":
encrypted = item.get("encrypted_content")
- if not isinstance(encrypted, str) or not encrypted:
- encrypted = self._encrypted_reasoning.get(item_id)
- if isinstance(encrypted, str) and encrypted:
- if is_replay(encrypted) and self.ledger.blocks.thinking_started:
- return [
+ events = self._item_readable(item, completed=True)
+ state = self._reasoning[item_id]
+ if (
+ state.block_index is None
+ and isinstance(encrypted, str)
+ and encrypted
+ and any(
+ isinstance(part, dict)
+ and part.get("type") == "reasoning_text"
+ and part.get("text") == ""
+ for part in item.get("content") or []
+ )
+ ):
+ events.extend(self._ensure_reasoning_started(state, completed=True))
+ if state.block_index is not None:
+ if isinstance(encrypted, str) and encrypted:
+ events.append(
self.ledger.content_block_delta(
- self.ledger.blocks.thinking_index,
+ state.block_index,
"signature_delta",
encrypted,
- ),
- self.ledger.stop_thinking_block(),
- ]
- events = list(self.ledger.close_content_blocks())
+ )
+ )
+ events.append(self.ledger.content_block_stop(state.block_index))
+ state.stopped = True
+ return events
+ if isinstance(encrypted, str) and encrypted:
index = self.ledger.blocks.allocate_index()
events.append(
self.ledger.content_block_start(
@@ -201,7 +292,10 @@ class ResponsesProviderStream:
)
events.append(self.ledger.content_block_stop(index))
self.generated_output = True
+ state.stopped = True
return events
+ state.stopped = True
+ return events
return []
def _ensure_tool_started(self, state: _ToolState) -> list[str]:
@@ -220,7 +314,17 @@ class ResponsesProviderStream:
def _finish(self, data: dict[str, Any], *, incomplete: bool) -> list[str]:
response = data.get("response")
response = response if isinstance(response, dict) else {}
- events = list(self.ledger.close_all_blocks())
+ events = []
+ for state in self._reasoning.values():
+ if state.block_index is not None and not state.stopped:
+ events.append(self.ledger.content_block_stop(state.block_index))
+ state.stopped = True
+ for state in self._tools.values():
+ if state.started and not state.stopped:
+ events.append(self.ledger.stop_tool_block(state.tool_index))
+ state.stopped = True
+ if self.ledger.blocks.text_started:
+ events.append(self.ledger.stop_text_block())
if self._pad_empty and not self.ledger.has_content_block():
events.extend(self.ledger.ensure_text_block())
events.append(self.ledger.emit_text_delta(" "))
diff --git a/src/free_claude_code/core/openai_responses/streaming/blocks.py b/src/free_claude_code/core/openai_responses/streaming/blocks.py
index 581f7b3a..90ffdd24 100644
--- a/src/free_claude_code/core/openai_responses/streaming/blocks.py
+++ b/src/free_claude_code/core/openai_responses/streaming/blocks.py
@@ -19,6 +19,7 @@ class ReasoningBlockState:
item_id: str
text_parts: list[str] = field(default_factory=list)
encrypted_content: str | None = None
+ summary_parts: list[str] = field(default_factory=list)
@dataclass(slots=True)
diff --git a/src/free_claude_code/core/openai_responses/streaming/completion.py b/src/free_claude_code/core/openai_responses/streaming/completion.py
index dcb35176..2409a6a9 100644
--- a/src/free_claude_code/core/openai_responses/streaming/completion.py
+++ b/src/free_claude_code/core/openai_responses/streaming/completion.py
@@ -63,6 +63,19 @@ class ResponseBlockCompleter:
state.item_id, state.output_index, text
)
)
+ if state.summary_parts:
+ summary = "".join(state.summary_parts)
+ self._ledger.add_reasoning_text(summary)
+ chunks.extend(
+ [
+ self._events.reasoning_summary_text(
+ state.item_id, state.output_index, summary, done=True
+ ),
+ self._events.reasoning_summary_part(
+ state.item_id, state.output_index, summary, done=True
+ ),
+ ]
+ )
chunks.append(self._events.output_item_done(state.output_index, item))
return chunks
@@ -152,8 +165,13 @@ def reasoning_output_item(
item["content"] = [
{"type": "reasoning_text", "text": "".join(state.text_parts)}
]
- return item
- return reasoning_item(state.item_id, "".join(state.text_parts), status)
+ else:
+ item = reasoning_item(state.item_id, "".join(state.text_parts), status)
+ if state.summary_parts:
+ item["summary"] = [
+ {"type": "summary_text", "text": "".join(state.summary_parts)}
+ ]
+ return item
def _reasoning_output_item(
diff --git a/src/free_claude_code/core/openai_responses/streaming/event_builders.py b/src/free_claude_code/core/openai_responses/streaming/event_builders.py
index 4dc691a7..1d3ca8ad 100644
--- a/src/free_claude_code/core/openai_responses/streaming/event_builders.py
+++ b/src/free_claude_code/core/openai_responses/streaming/event_builders.py
@@ -141,6 +141,44 @@ class ResponseEventBuilder:
},
)
+ def reasoning_summary_text(
+ self, item_id: str, output_index: int, text: str, *, done: bool
+ ) -> str:
+ kind = (
+ "response.reasoning_summary_text.done"
+ if done
+ else "response.reasoning_summary_text.delta"
+ )
+ return self._format(
+ kind,
+ {
+ "type": kind,
+ "item_id": item_id,
+ "output_index": output_index,
+ "summary_index": 0,
+ "text" if done else "delta": text,
+ },
+ )
+
+ def reasoning_summary_part(
+ self, item_id: str, output_index: int, text: str, *, done: bool
+ ) -> str:
+ kind = (
+ "response.reasoning_summary_part.done"
+ if done
+ else "response.reasoning_summary_part.added"
+ )
+ return self._format(
+ kind,
+ {
+ "type": kind,
+ "item_id": item_id,
+ "output_index": output_index,
+ "summary_index": 0,
+ "part": {"type": "summary_text", "text": text},
+ },
+ )
+
def function_call_arguments_delta(
self, item_id: str, output_index: int, arguments: str
) -> str:
diff --git a/src/free_claude_code/providers/anthropic_messages/transport.py b/src/free_claude_code/providers/anthropic_messages/transport.py
index 765fdb6c..e5c2ce6e 100644
--- a/src/free_claude_code/providers/anthropic_messages/transport.py
+++ b/src/free_claude_code/providers/anthropic_messages/transport.py
@@ -205,7 +205,7 @@ class AnthropicMessagesTransport:
endpoint_context=endpoint_context,
request_id=request_id,
presenter_factory=lambda origin: NativeMessagesRelay(
- public_model=response_model or request.model, replay_origin=origin
+ public_model=response_model or request.model
),
continuation=continuation,
)
@@ -233,7 +233,6 @@ class AnthropicMessagesTransport:
request,
public_model=response_model or request.model,
tool_identities=prepared.tool_identities,
- replay_origin=origin,
),
continuation=continuation,
)
diff --git a/src/free_claude_code/providers/google_openai/provider.py b/src/free_claude_code/providers/google_openai/provider.py
index 560a38cf..60e84944 100644
--- a/src/free_claude_code/providers/google_openai/provider.py
+++ b/src/free_claude_code/providers/google_openai/provider.py
@@ -1,6 +1,6 @@
"""Shared Google behavior for OpenAI-compatible Gemini endpoints."""
-from collections.abc import Mapping
+from collections.abc import Iterator, Mapping
from copy import deepcopy
from typing import Any
@@ -8,6 +8,7 @@ from free_claude_code.core.reasoning import ReasoningPolicy
from free_claude_code.providers.admission import ProviderAdmissionController
from free_claude_code.providers.base import ProviderConfig
from free_claude_code.providers.openai_chat import (
+ ChatStreamOutput,
OpenAIAsyncCredentialProvider,
OpenAIChatBehavior,
OpenAIChatProfile,
@@ -51,6 +52,18 @@ class GoogleChatBehavior(OpenAIChatBehavior):
)
return body
+ def extra_reasoning_events(
+ self, delta: Any, output: ChatStreamOutput, *, output_reasoning: bool
+ ) -> Iterator[str]:
+ extra = getattr(delta, "extra_content", None)
+ google = extra.get("google") if isinstance(extra, Mapping) else None
+ signature = (
+ google.get("thought_signature") if isinstance(google, Mapping) else None
+ )
+ if isinstance(signature, str) and signature:
+ output.defer_opaque_reasoning(signature)
+ return iter(())
+
class GoogleOpenAIProvider(OpenAIChatProvider):
"""Shared thought-signature and request behavior for Google Gemini APIs."""
diff --git a/src/free_claude_code/providers/google_openai/thought_signatures.py b/src/free_claude_code/providers/google_openai/thought_signatures.py
index 9eba08ad..202feedb 100644
--- a/src/free_claude_code/providers/google_openai/thought_signatures.py
+++ b/src/free_claude_code/providers/google_openai/thought_signatures.py
@@ -3,6 +3,8 @@
from copy import deepcopy
from typing import Any
+from free_claude_code.core.history_replay import is_replay
+
GOOGLE_SKIP_THOUGHT_SIGNATURE_VALIDATOR = "skip_thought_signature_validator"
@@ -16,6 +18,7 @@ def apply_google_thought_signatures(
messages = body.get("messages")
if not isinstance(messages, list):
return
+ _apply_message_thought_signatures(messages)
_apply_cached_tool_call_signatures(messages, tool_call_extra_content_by_id or {})
_apply_missing_current_turn_signatures(messages)
@@ -34,7 +37,7 @@ def _tool_call_thought_signature(tool_call: dict[str, Any]) -> str | None:
return _thought_signature_from_extra_content(tool_call.get("extra_content"))
-def _set_tool_call_thought_signature(tool_call: dict[str, Any], signature: str) -> None:
+def _set_thought_signature(tool_call: dict[str, Any], signature: str) -> None:
extra_content = tool_call.get("extra_content")
if not isinstance(extra_content, dict):
extra_content = {}
@@ -46,6 +49,35 @@ def _set_tool_call_thought_signature(tool_call: dict[str, Any], signature: str)
google["thought_signature"] = signature
+def _apply_message_thought_signatures(messages: list[Any]) -> None:
+ for message in messages:
+ if not isinstance(message, dict) or message.get("role") != "assistant":
+ continue
+ details = message.get("reasoning_details")
+ if not isinstance(details, list):
+ continue
+ opaque = [
+ detail
+ for detail in details
+ if isinstance(detail, dict)
+ and detail.get("type") == "reasoning.encrypted"
+ and isinstance(detail.get("data"), str)
+ and detail["data"]
+ and not is_replay(detail["data"])
+ ]
+ if not opaque:
+ continue
+ if len(opaque) == 1 and not _thought_signature_from_extra_content(
+ message.get("extra_content")
+ ):
+ _set_thought_signature(message, opaque[0]["data"])
+ retained = [detail for detail in details if detail not in opaque]
+ if retained:
+ message["reasoning_details"] = retained
+ else:
+ message.pop("reasoning_details", None)
+
+
def _message_has_standard_user_content(message: dict[str, Any]) -> bool:
if message.get("role") != "user":
return False
@@ -112,6 +144,4 @@ def _apply_missing_current_turn_signatures(messages: list[Any]) -> None:
continue
if _tool_call_thought_signature(first_tool_call):
continue
- _set_tool_call_thought_signature(
- first_tool_call, GOOGLE_SKIP_THOUGHT_SIGNATURE_VALIDATOR
- )
+ _set_thought_signature(first_tool_call, GOOGLE_SKIP_THOUGHT_SIGNATURE_VALIDATOR)
diff --git a/src/free_claude_code/providers/openai_chat/behavior.py b/src/free_claude_code/providers/openai_chat/behavior.py
index df2343ea..81ccbb19 100644
--- a/src/free_claude_code/providers/openai_chat/behavior.py
+++ b/src/free_claude_code/providers/openai_chat/behavior.py
@@ -43,6 +43,7 @@ class OpenAIChatBehavior:
reasoning=reasoning,
policy=self.profile.request_policy,
postprocessors=self.profile.request_postprocessors,
+ structured_reasoning_details=self.profile.structured_reasoning_details,
)
return self.finalize_chat_body(body, reasoning=reasoning)
diff --git a/src/free_claude_code/providers/openai_chat/reasoning_details.py b/src/free_claude_code/providers/openai_chat/reasoning_details.py
index 84d183fa..96c3d1fb 100644
--- a/src/free_claude_code/providers/openai_chat/reasoning_details.py
+++ b/src/free_claude_code/providers/openai_chat/reasoning_details.py
@@ -3,42 +3,30 @@
from collections.abc import Iterator, Mapping, Sequence
from copy import deepcopy
from dataclasses import dataclass, field
-from typing import Any, Literal, cast
-from uuid import uuid4
+from typing import Any
-from free_claude_code.core.history_replay import (
- ReplayOrigin,
- ReplayRecord,
- readable_reasoning,
-)
-from free_claude_code.core.json_types import JsonObject, JsonValue
+from free_claude_code.core.history_replay import readable_reasoning
from .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)
+ visible_parts: dict[tuple[str, int], str] = field(default_factory=dict)
+ output_sources: dict[tuple[str, int], int] = field(default_factory=dict)
+ output_reasoning: bool = True
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)
+ # Retain native phase transitions without using that field for publication.
+ native_primary: bool | None = None
class StructuredReasoningStream:
"""Collect replay groups independently of their visible wire blocks."""
def __init__(self) -> None:
- self._text_source: Literal["native", "details"] | None = None
self._group: _ReasoningGroup | None = None
@property
@@ -52,9 +40,7 @@ class StructuredReasoningStream:
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()
- )
+ return iter(())
def events(
self,
@@ -62,43 +48,52 @@ class StructuredReasoningStream:
output: ChatStreamOutput,
*,
native_reasoning: str | None,
+ output_reasoning: bool = True,
) -> Iterator[str]:
details = _reasoning_details(delta)
- 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"
- 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:
+ if (
+ native_reasoning
+ and self._group is not None
+ and self._group.native_primary
+ and self._group.after_content
+ ):
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()
+ yield from output.close_content_blocks()
+ if self._group is None and (
+ (output_reasoning and native_reasoning)
+ or any(
+ (output_reasoning and _reasoning_detail_text(detail))
+ or _reasoning_detail_opaque(detail)
+ for detail in details
+ )
+ ):
+ self._group = _ReasoningGroup(output_reasoning=output_reasoning)
group = self._group
if group is None:
return
+ if group.native_primary is None:
+ if any(
+ _field(detail, "type") == "reasoning.text"
+ and _reasoning_detail_text(detail)
+ for detail in details
+ ):
+ group.native_primary = False
+ elif native_reasoning:
+ group.native_primary = True
+ elif any(_reasoning_detail_text(detail) for detail in details):
+ group.native_primary = False
if native_reasoning is not None:
+ # This may be an aggregate alias of signed details arriving later.
group.native_parts.append(native_reasoning)
+ visible_parts: list[tuple[tuple[str, int], str, bool]] = []
for detail in details:
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 group.slots:
- current = group.details[group.slots[key]]
+ slot = group.slots[key]
+ current = group.details[slot]
for name, part in value.items():
if (
name
@@ -117,17 +112,113 @@ class StructuredReasoningStream:
else:
current[name] = part
else:
+ slot = len(group.details)
if isinstance(identity, str | int):
- group.slots[key] = len(group.details)
+ group.slots[key] = slot
group.details.append(value)
- for text in visible:
- yield from output.ensure_reasoning_block()
- yield output.emit_reasoning_delta(text)
+ if (
+ output_reasoning
+ and not group.after_content
+ and (text := _reasoning_detail_text(detail))
+ ):
+ visible_parts.append(
+ (
+ ("detail", slot),
+ text,
+ _field(detail, "type") == "reasoning.summary",
+ )
+ )
+ for key, text, summary in visible_parts:
+ source = _output_source(group, key)
+ group.output_sources[key] = source
+ yield from output.ensure_reasoning_block(source=source)
+ yield output.emit_reasoning_delta(text, summary=summary, source=source)
+ group.visible_parts[key] = group.visible_parts.get(key, "") + text
+
+ def _remaining_readable(
+ self, output: ChatStreamOutput, group: _ReasoningGroup
+ ) -> Iterator[str]:
+ if not group.output_reasoning:
+ return
+ typed = [
+ (("detail", slot), text, detail.get("type") == "reasoning.summary")
+ for slot, detail in enumerate(group.details)
+ if (text := _reasoning_detail_text(detail))
+ ]
+ for key, text, summary in typed:
+ yield from self._remaining_part(output, group, key, text, summary=summary)
+ group.visible_parts[key] = text
+ represented = set(group.visible_parts.values())
+ for summary in (None, False, True):
+ emitted = [
+ group.visible_parts.get(key, "")
+ for key, _, is_summary in typed
+ if summary is None or summary == is_summary
+ ]
+ represented.update(("".join(emitted), "\n\n".join(emitted)))
+ native = "".join(group.native_parts)
+ if native:
+ if native not in represented:
+ yield from self._remaining_part(
+ output, group, ("native", 0), native, summary=False
+ )
+ group.visible_parts[("native", 0)] = native
- def finish(self, output: ChatStreamOutput) -> Iterator[str]:
+ def _remaining_part(
+ self,
+ output: ChatStreamOutput,
+ group: _ReasoningGroup,
+ key: tuple[str, int],
+ text: str,
+ *,
+ summary: bool,
+ ) -> Iterator[str]:
+ remaining = text.removeprefix(group.visible_parts.get(key, ""))
+ if remaining:
+ source = _output_source(group, key)
+ group.output_sources[key] = source
+ yield from output.begin_reasoning_record(source=source)
+ yield output.emit_reasoning_delta(remaining, summary=summary, source=source)
+
+ def finish(
+ self, output: ChatStreamOutput, *, completed: bool = True
+ ) -> Iterator[str]:
group, self._group = self._group, None
if group is not None:
- yield from output.complete_reasoning_record(group.id, group.snapshot())
+ yield from self._remaining_readable(output, group)
+ for slot, detail in enumerate(group.details):
+ if detail.get("type") != "reasoning.text":
+ continue
+ key = ("detail", slot)
+ source = group.output_sources.get(key, slot + 1)
+ opaque = _reasoning_detail_opaque(detail) if completed else []
+ if (
+ group.output_reasoning
+ and detail.get("text") == ""
+ and opaque
+ and key not in group.output_sources
+ ):
+ yield from output.begin_reasoning_record(source=source)
+ yield output.emit_reasoning_delta("", source=source)
+ yield from output.complete_reasoning_record(opaque, source=source)
+ yield from output.complete_reasoning_record(
+ [
+ value
+ for detail in group.details
+ if detail.get("type") != "reasoning.text"
+ for value in _reasoning_detail_opaque(detail)
+ ]
+ if completed
+ else []
+ )
+
+
+def _output_source(group: _ReasoningGroup, key: tuple[str, int]) -> int:
+ if key in group.output_sources:
+ return group.output_sources[key]
+ if key[0] == "detail" and group.details[key[1]].get("type") == "reasoning.text":
+ return key[1] + 1
+ return 0
def _reasoning_details(delta: Any) -> Sequence[Any]:
@@ -158,6 +249,20 @@ def _reasoning_detail_text(detail: Any) -> str | None:
return None
+def _reasoning_detail_opaque(detail: Any) -> list[str]:
+ kind = str(_field(detail, "type") or "").lower()
+ keys = (
+ ("data", "signature")
+ if "encrypted" in kind or "redacted" in kind
+ else ("signature",)
+ if kind.startswith("reasoning.")
+ else ()
+ )
+ return [
+ value for key in keys if isinstance(value := _field(detail, key), str) and value
+ ]
+
+
def _field(item: Any, name: str) -> Any:
if isinstance(item, Mapping):
return item.get(name)
diff --git a/src/free_claude_code/providers/openai_chat/request_policy.py b/src/free_claude_code/providers/openai_chat/request_policy.py
index 8cd71080..56e937c4 100644
--- a/src/free_claude_code/providers/openai_chat/request_policy.py
+++ b/src/free_claude_code/providers/openai_chat/request_policy.py
@@ -52,6 +52,7 @@ def build_openai_chat_request_body(
reasoning: ReasoningPolicy,
policy: OpenAIChatRequestPolicy,
postprocessors: Iterable[OpenAIChatPostprocessor] = (),
+ structured_reasoning_details: bool = False,
) -> dict[str, Any]:
"""Build an OpenAI-compatible chat request body from an Anthropic request."""
request_data = normalize_messages_history(request_data)
@@ -66,6 +67,7 @@ def build_openai_chat_request_body(
request_data,
default_max_tokens=policy.default_max_tokens,
reasoning_replay=policy.reasoning_replay,
+ structured_reasoning_details=structured_reasoning_details,
)
except OpenAIConversionError as exc:
raise InvalidRequestError(str(exc)) from exc
diff --git a/src/free_claude_code/providers/openai_chat/stream_output.py b/src/free_claude_code/providers/openai_chat/stream_output.py
index 0cf6525b..4239910a 100644
--- a/src/free_claude_code/providers/openai_chat/stream_output.py
+++ b/src/free_claude_code/providers/openai_chat/stream_output.py
@@ -13,12 +13,6 @@ from free_claude_code.core.anthropic.streaming import (
parse_complete_tool_input,
)
from free_claude_code.core.failures import ExecutionFailure, FailureKind
-from free_claude_code.core.history_replay import (
- AssociatedReplayRecord,
- ReplayOrigin,
- ReplayRecord,
- encode_replay,
-)
from free_claude_code.core.json_types import JsonObject
from free_claude_code.core.openai_responses import (
ReasoningBlockState,
@@ -48,7 +42,9 @@ class ReasoningReplayLifecycle(Protocol):
def before_content(self, output: ChatStreamOutput) -> Iterator[str]: ...
- def finish(self, output: ChatStreamOutput) -> Iterator[str]: ...
+ def finish(
+ self, output: ChatStreamOutput, *, completed: bool = True
+ ) -> Iterator[str]: ...
@dataclass(frozen=True, slots=True)
@@ -87,13 +83,13 @@ class ChatStreamOutput(ABC):
def __init__(self, *, input_tokens: int) -> None:
self.input_tokens = input_tokens
- self.replay_origin: ReplayOrigin | None = None
self.reasoning_replay: ReasoningReplayLifecycle | None = None
self.tool_states: dict[int, ChatToolState] = {}
self._text_parts: list[str] = []
self._reasoning_parts: list[str] = []
+ self._pending_opaque: list[str] = []
self._text_started = False
- self._reasoning_started = False
+ self._open_reasoning_sources: set[int] = set()
self._content_started = False
self._terminal = False
@@ -112,54 +108,73 @@ class ChatStreamOutput(ABC):
def start_events(self) -> list[str]:
return self._start_events()
- def ensure_reasoning_block(self) -> list[str]:
+ def ensure_reasoning_block(self, *, source: int = 0) -> 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
- if not self._reasoning_started:
- events.extend(self._start_reasoning_block())
- self._reasoning_started = True
- self._content_started = True
+ events.extend(self.begin_reasoning_record(source=source))
return events
- def emit_reasoning_delta(self, content: str) -> str:
+ def emit_reasoning_delta(
+ self, content: str, *, summary: bool = False, source: int = 0
+ ) -> str:
self._reasoning_parts.append(content)
- return self._emit_reasoning_delta(content)
-
- def begin_reasoning_record(self) -> list[str]:
- return []
+ return self._emit_reasoning_delta(content, summary=summary, source=source)
- def pause_reasoning_record(self, group_id: str, record: ReplayRecord) -> list[str]:
- return []
+ def begin_reasoning_record(self, *, source: int = 0) -> list[str]:
+ if source in self._open_reasoning_sources:
+ return []
+ self._open_reasoning_sources.add(source)
+ self._content_started = True
+ return self._start_reasoning_block(source)
def complete_reasoning_record(
- self, group_id: str, record: ReplayRecord
+ self, opaque: list[str], *, source: int = 0
) -> 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)
+ events: list[str] = []
+ if source in self._open_reasoning_sources:
+ if opaque:
+ events.extend(self._attach_reasoning_replay(opaque.pop(0), source))
+ events.extend(self._stop_reasoning_block(source))
+ self._open_reasoning_sources.remove(source)
+ for data in opaque:
+ events.extend(self.emit_opaque_reasoning(data))
+ return events
- def flush_reasoning_replay(self) -> list[str]:
- if self.reasoning_replay is None:
+ def emit_opaque_reasoning(self, data: str) -> list[str]:
+ if not data:
return []
- return list(self.reasoning_replay.finish(self))
+ self._content_started = True
+ return self._emit_opaque_reasoning(data)
+
+ def defer_opaque_reasoning(self, data: str) -> None:
+ if data:
+ self._pending_opaque.append(data)
+
+ def flush_reasoning_replay(self, *, completed: bool = True) -> list[str]:
+ events = (
+ list(self.reasoning_replay.finish(self, completed=completed))
+ if self.reasoning_replay is not None
+ else []
+ )
+ opaque, self._pending_opaque = self._pending_opaque, []
+ if completed:
+ for data in opaque:
+ events.extend(self.emit_opaque_reasoning(data))
+ return events
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 (
+ if self._open_reasoning_sources and not (
self.reasoning_replay and self.reasoning_replay.active
):
- events.extend(self._stop_reasoning_block())
- self._reasoning_started = False
+ for source in sorted(self._open_reasoning_sources):
+ events.extend(self.complete_reasoning_record([], source=source))
return events
def ensure_text_block(self) -> list[str]:
@@ -186,14 +201,10 @@ class ChatStreamOutput(ABC):
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:
- events.extend(self._stop_reasoning_block())
- self._reasoning_started = False
+ for source in sorted(self._open_reasoning_sources):
+ events.extend(self.complete_reasoning_record([], source=source))
if self._text_started:
events.extend(self._stop_text_block())
self._text_started = False
@@ -247,18 +258,21 @@ class ChatStreamOutput(ABC):
return self._stop_tool_block(tool_index, state)
def close_all_blocks(self) -> list[str]:
- events = self.finish_reasoning_group()
+ events = self.flush_reasoning_replay()
+ events.extend(self._pause_reasoning())
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())
+ if self._text_started:
+ events.extend(self._stop_text_block())
+ self._text_started = False
return events
def close_unclosed_blocks(self) -> list[str]:
- events = self.finish_reasoning_group()
+ events = self.flush_reasoning_replay(completed=False)
+ events.extend(self._close_content_blocks())
for state in self.tool_states.values():
state.open = False
- events.extend(self.finish_replay_carriers())
return events
def has_emitted_tool_block(self) -> bool:
@@ -334,19 +348,21 @@ class ChatStreamOutput(ABC):
def _start_events(self) -> list[str]: ...
@abstractmethod
- def _start_reasoning_block(self) -> list[str]: ...
+ def _start_reasoning_block(self, source: int) -> list[str]: ...
@abstractmethod
- def _emit_reasoning_delta(self, content: str) -> str: ...
+ def _emit_reasoning_delta(
+ self, content: str, *, summary: bool = False, source: int = 0
+ ) -> str: ...
@abstractmethod
def _emit_opaque_reasoning(self, data: str) -> list[str]: ...
@abstractmethod
- def _attach_reasoning_replay(self, data: str) -> list[str]: ...
+ def _attach_reasoning_replay(self, data: str, source: int) -> list[str]: ...
@abstractmethod
- def _stop_reasoning_block(self) -> list[str]: ...
+ def _stop_reasoning_block(self, source: int) -> list[str]: ...
@abstractmethod
def _start_text_block(self) -> list[str]: ...
@@ -389,67 +405,20 @@ 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,
input_tokens,
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)
- ]
+ self._reasoning_indices: dict[int, int] = {}
def close_unclosed_blocks(self) -> list[str]:
- events = self.flush_reasoning_replay()
+ events = self.flush_reasoning_replay(completed=False)
events.extend(self._ledger.close_unclosed_blocks())
- events.extend(self.finish_replay_carriers())
self._text_started = False
- self._reasoning_started = False
+ self._open_reasoning_sources.clear()
+ self._reasoning_indices.clear()
for state in self.tool_states.values():
state.open = False
return events
@@ -457,11 +426,17 @@ class AnthropicChatStreamOutput(ChatStreamOutput):
def _start_events(self) -> list[str]:
return [self._ledger.message_start()]
- def _start_reasoning_block(self) -> list[str]:
- return [self._ledger.start_thinking_block()]
+ def _start_reasoning_block(self, source: int) -> list[str]:
+ index = self._ledger.blocks.allocate_index()
+ self._reasoning_indices[source] = index
+ return [self._ledger.content_block_start(index, "thinking")]
- def _emit_reasoning_delta(self, content: str) -> str:
- return self._ledger.emit_thinking_delta(content)
+ def _emit_reasoning_delta(
+ self, content: str, *, summary: bool = False, source: int = 0
+ ) -> str:
+ return self._ledger.content_block_delta(
+ self._reasoning_indices[source], "thinking_delta", content
+ )
def _emit_opaque_reasoning(self, data: str) -> list[str]:
index = self._ledger.blocks.allocate_index()
@@ -470,15 +445,15 @@ class AnthropicChatStreamOutput(ChatStreamOutput):
self._ledger.content_block_stop(index),
]
- def _attach_reasoning_replay(self, data: str) -> list[str]:
+ def _attach_reasoning_replay(self, data: str, source: int) -> list[str]:
return [
self._ledger.content_block_delta(
- self._ledger.blocks.thinking_index, "signature_delta", data
+ self._reasoning_indices[source], "signature_delta", data
)
]
- def _stop_reasoning_block(self) -> list[str]:
- return [self._ledger.stop_thinking_block()]
+ def _stop_reasoning_block(self, source: int) -> list[str]:
+ return [self._ledger.content_block_stop(self._reasoning_indices.pop(source))]
def _start_text_block(self) -> list[str]:
return [self._ledger.start_text_block()]
@@ -545,19 +520,12 @@ class ResponsesChatStreamOutput(ChatStreamOutput):
events=self._events,
)
self._text_state: TextBlockState | None = None
- self._reasoning_state: ReasoningBlockState | None = None
+ self._reasoning_states: dict[int, ReasoningBlockState] = {}
self._tool_output_states: dict[int, ToolBlockState] = {}
self._usage: dict[str, object] | None = None
self._conversion_failure: ExecutionFailure | None = None
self._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,
*,
@@ -600,14 +568,14 @@ class ResponsesChatStreamOutput(ChatStreamOutput):
self._events.response_created(self._response_payload(status="in_progress"))
]
- def _start_reasoning_block(self) -> list[str]:
+ def _start_reasoning_block(self, source: int) -> list[str]:
output_index = self._ledger.reserve_output_slot()
state = ReasoningBlockState(
index=output_index,
output_index=output_index,
item_id=new_reasoning_item_id(),
)
- self._reasoning_state = state
+ self._reasoning_states[source] = state
self._ledger.set_active_block(state)
return [
self._events.output_item_added(
@@ -616,11 +584,27 @@ class ResponsesChatStreamOutput(ChatStreamOutput):
)
]
- def _emit_reasoning_delta(self, content: str) -> str:
- state = self._reasoning_state
- if state is None or not content:
+ def _emit_reasoning_delta(
+ self, content: str, *, summary: bool = False, source: int = 0
+ ) -> str:
+ state = self._reasoning_states.get(source)
+ if state is None:
return ""
+ if summary:
+ added = (
+ ""
+ if state.summary_parts
+ else self._events.reasoning_summary_part(
+ state.item_id, state.output_index, "", done=False
+ )
+ )
+ state.summary_parts.append(content)
+ return added + self._events.reasoning_summary_text(
+ state.item_id, state.output_index, content, done=False
+ )
state.text_parts.append(content)
+ if not content:
+ return ""
return self._events.reasoning_text_delta(
state.item_id, state.output_index, content
)
@@ -644,14 +628,13 @@ class ResponsesChatStreamOutput(ChatStreamOutput):
events.extend(self._completer.complete_block(state))
return events
- def _attach_reasoning_replay(self, data: str) -> list[str]:
- if self._reasoning_state is not None:
- self._reasoning_state.encrypted_content = data
+ def _attach_reasoning_replay(self, data: str, source: int) -> list[str]:
+ if state := self._reasoning_states.get(source):
+ state.encrypted_content = data
return []
- def _stop_reasoning_block(self) -> list[str]:
- state = self._reasoning_state
- self._reasoning_state = None
+ def _stop_reasoning_block(self, source: int) -> list[str]:
+ state = self._reasoning_states.pop(source, None)
if state is None:
return []
self._ledger.pop_active_block(state.index)
diff --git a/src/free_claude_code/providers/openai_chat/transport.py b/src/free_claude_code/providers/openai_chat/transport.py
index 4a8c25d4..9f2f75fe 100644
--- a/src/free_claude_code/providers/openai_chat/transport.py
+++ b/src/free_claude_code/providers/openai_chat/transport.py
@@ -4,7 +4,7 @@ import asyncio
import sys
import uuid
from collections.abc import AsyncIterator, Awaitable, Callable, Iterator, Mapping
-from dataclasses import dataclass, replace
+from dataclasses import dataclass
from enum import StrEnum
from functools import partial
from typing import Any, cast
@@ -220,7 +220,6 @@ class _OpenAIChatStreamAssembler:
self._finish_reason: Any = None
self._raw_stop_seen = False
self._usage_info: Any = None
- self._native_reasoning_seen = False
self._tool_argument_aliases: dict[str, dict[str, str]] = {}
self._tool_argument_alias_buffers: dict[int, str] = {}
self._tool_name_buffers: dict[int, str] = {}
@@ -301,14 +300,6 @@ class _OpenAIChatStreamAssembler:
if not chunk.choices:
return
- if (
- self._output.replay_origin is not None
- and isinstance(getattr(chunk, "model", None), str)
- and chunk.model
- ):
- self._output.replay_origin = replace(
- self._output.replay_origin, model=chunk.model
- )
choice = chunk.choices[0]
delta = choice.delta
if choice.finish_reason is not None:
@@ -325,22 +316,16 @@ class _OpenAIChatStreamAssembler:
)
reasoning = self._profile.reasoning_delta(delta)
- if self._output_reasoning:
- if self._structured_reasoning is not None:
- yield from self._structured_reasoning.events(
- delta,
- self._output,
- native_reasoning=reasoning,
- )
- elif reasoning is not None and (
- reasoning or not self._native_reasoning_seen
- ):
- # Preserve initial empty reasoning for replay; later empty fields
- # are placeholders and must not interrupt text or tool output.
- self._native_reasoning_seen = True
- yield from self._output.ensure_reasoning_block()
- if reasoning:
- yield self._output.emit_reasoning_delta(reasoning)
+ if self._structured_reasoning is not None:
+ yield from self._structured_reasoning.events(
+ delta,
+ self._output,
+ native_reasoning=reasoning if self._output_reasoning else None,
+ output_reasoning=self._output_reasoning,
+ )
+ elif self._output_reasoning and reasoning:
+ yield from self._output.ensure_reasoning_block()
+ yield self._output.emit_reasoning_delta(reasoning)
yield from self._extra_reasoning_events(delta, self._output)
@@ -1050,15 +1035,6 @@ class _OpenAIChatStreamRunner:
request_id=self._request_id,
)
stream = scope.retain(stream)
- assembler.output.replay_origin = replay_origin(
- tag,
- "chat",
- str(body["model"]),
- client=self._transport._client,
- endpoint=self._endpoint.snapshot
- if self._endpoint is not None
- else None,
- )
assembler.bind_tool_argument_aliases(self._tool_argument_aliases)
async with recovery.read_stream(stream) as chunks:
async for chunk in chunks:
@@ -1604,7 +1580,7 @@ class _OpenAIChatStreamRunner:
thinking_suffix = continuation_suffix(partial_thinking, recovered.thinking)
if not (thinking_suffix or text_suffix or recovered.tool_calls):
return None
- events = output.flush_reasoning_replay()
+ events = output.flush_reasoning_replay(completed=False)
if thinking_suffix:
events.extend(output.ensure_reasoning_block())
events.append(output.emit_reasoning_delta(thinking_suffix))
diff --git a/src/free_claude_code/providers/openai_responses/transport.py b/src/free_claude_code/providers/openai_responses/transport.py
index dff407e7..5f30ce93 100644
--- a/src/free_claude_code/providers/openai_responses/transport.py
+++ b/src/free_claude_code/providers/openai_responses/transport.py
@@ -4,7 +4,6 @@ import asyncio
import sys
import uuid
from collections.abc import AsyncIterator, Callable, Mapping
-from dataclasses import replace
from functools import partial
from typing import cast
@@ -20,7 +19,6 @@ from free_claude_code.core.diagnostics import extract_upstream_error_detail
from free_claude_code.core.failures import ExecutionFailure, FailureKind
from free_claude_code.core.history_replay import (
prepare_history,
- preserve_responses_reasoning,
)
from free_claude_code.core.json_types import JsonObject
from free_claude_code.core.openai_responses import (
@@ -457,14 +455,6 @@ class OpenAIResponsesTransport:
raise context_window_exceeded_provider_failure()
if adapt_event is not None:
payload = adapt_event(upstream_event.type, payload)
- response = payload.get("response")
- if (
- isinstance(response, dict)
- and isinstance(response.get("model"), str)
- and response["model"]
- ):
- origin = replace(origin, model=response["model"])
- payload = preserve_responses_reasoning(payload, origin)
for event in presenter.feed(upstream_event.type, payload):
for held in recovery.push(event):
yield held
diff --git a/tests/api/test_chat_tool_identity_api.py b/tests/api/test_chat_tool_identity_api.py
index 1d9dd5d3..9c1ba2c8 100644
--- a/tests/api/test_chat_tool_identity_api.py
+++ b/tests/api/test_chat_tool_identity_api.py
@@ -283,7 +283,9 @@ def ambiguous_events(stop, *, incomplete=False, fragment_index=0):
@pytest.mark.asyncio
@pytest.mark.parametrize("wire", ["messages", "responses"])
[email protected]("prefix", [None, "content", "reasoning_content"])
+ "prefix", [None, "content", "reasoning_content", "reasoning_details"]
+)
@pytest.mark.parametrize("stop", [None, "same", "before"])
@pytest.mark.parametrize("max_attempts", [1, 2])
@pytest.mark.parametrize("fragment_index", [0, None])
@@ -296,7 +298,22 @@ async def test_public_identity_failure_respects_delivery_stop_and_budget(
)
first = ambiguous_events(stop, fragment_index=fragment_index)
if prefix:
- first.insert(0, chat_chunk({prefix: "already visible"}))
+ first.insert(
+ 0,
+ chat_chunk(
+ {
+ "reasoning_details": [
+ {
+ "type": "reasoning.text",
+ "text": "already visible",
+ "index": 0,
+ }
+ ]
+ }
+ if prefix == "reasoning_details"
+ else {prefix: "already visible"}
+ ),
+ )
async with _harness(
"chat",
lambda bodies: (
@@ -309,22 +326,20 @@ async def test_public_identity_failure_respects_delivery_stop_and_budget(
send(wire, [{"role": "user", "content": "read"}], tools=_tools(wire)), wire
)
- # OpenRouter's Messages thinking includes a Chat replay signature, which
- # the existing public continuation policy treats as opaque.
- opaque = wire == "messages" and prefix == "reasoning_content"
- retry = stop is None and max_attempts > 1 and not opaque
+ # Structured records retain the existing conservative recovery policy.
+ retry = stop is None and max_attempts > 1 and prefix != "reasoning_details"
assert len(bodies) == (2 if retry else 1)
assert "call_abandoned" not in result
events = parse_sse_text(result)
- if prefix:
- assert "already visible" in result
+ visible_prefix = prefix in {"content", "reasoning_details"}
+ assert ("already visible" in result) == visible_prefix
if retry:
assert completed_arguments(events, wire) == '{"path":"winning"}'
assert events[-1].event == (
"message_stop" if wire == "messages" else "response.completed"
)
assert bodies[1]["tools"] == bodies[0]["tools"]
- if prefix:
+ if visible_prefix:
assert "already visible" in str(bodies[1])
else:
assert bodies[0] == bodies[1]
diff --git a/tests/api/test_delivered_stream_recovery.py b/tests/api/test_delivered_stream_recovery.py
index 2885b8ba..943e5667 100644
--- a/tests/api/test_delivered_stream_recovery.py
+++ b/tests/api/test_delivered_stream_recovery.py
@@ -12,6 +12,7 @@ from fastapi.testclient import TestClient
from free_claude_code.config.settings import Settings
from free_claude_code.core.anthropic.passthrough import NativeMessagesRequest
from free_claude_code.core.anthropic.stream_contracts import parse_sse_text
+from free_claude_code.providers.deepseek import DeepSeekProvider
from free_claude_code.providers.failure_policy import RetryableProviderProtocolError
from free_claude_code.providers.request_recovery import RequestCorrections
from free_claude_code.providers.stream_recovery import RecoveryHoldbackBuffer
@@ -19,6 +20,7 @@ from tests.api.support import create_test_app, provider_manager_for_app
from tests.api.test_hidden_stream_retries import delivered, partial_tool
from tests.api.test_tool_call_buffer import _response
from tests.api.test_tool_call_buffer_transports import _tools
+from tests.providers.support import immediate_admission, make_provider_config
from tests.providers.test_anthropic_messages_transport import Wire, _sse
from tests.providers.test_anthropic_provider import native_body, provider
from tests.providers.test_history_transports import _events_for, _harness
@@ -719,11 +721,11 @@ def test_http_recovery_then_real_harness_tool_result_has_no_private_history():
@pytest.mark.asyncio
@pytest.mark.parametrize("wire", ["messages", "responses"])
-async def test_unsigned_thinking_can_continue_into_text(wire):
[email protected]("structured", [False, True])
+async def test_readable_thinking_retries_or_continues_into_text(wire, structured):
first = text_events("chat", "", complete=False)
- first[0]["choices"][0]["delta"] = {
- "reasoning_content": "Look at the request first."
- }
+ thought = "Look at the request first."
+ first[0]["choices"][0]["delta"] = {"reasoning_content": thought}
async with _harness(
"chat",
lambda bodies: (
@@ -731,6 +733,12 @@ async def test_unsigned_thinking_can_continue_into_text(wire):
first if len(bodies) == 1 else text_events("chat", "Answer."),
),
max_attempts=2,
+ chat_provider_factory=None
+ if structured
+ else lambda: DeepSeekProvider(
+ make_provider_config(api_key="a", base_url="https://provider.invalid/v1"),
+ admission=immediate_admission(max_attempts=2),
+ ),
) as (send, bodies, _):
result = await delivered(
send(wire, [{"role": "user", "content": "answer"}]), wire
@@ -738,7 +746,11 @@ async def test_unsigned_thinking_can_continue_into_text(wire):
events = parse_sse_text(result)
assert_completed(events, wire)
assert public_text(events, wire) == "Answer."
- assert "Look at the request first." in str(bodies[-1])
+ assert len(bodies) == 2
+ assert (thought in result) == (not structured)
+ assert (thought in str(bodies[-1])) == (not structured)
+ if structured:
+ assert bodies[0] == bodies[1]
@pytest.mark.asyncio
diff --git a/tests/api/test_recovery_reasoning.py b/tests/api/test_recovery_reasoning.py
index f22cb2a8..987427f2 100644
--- a/tests/api/test_recovery_reasoning.py
+++ b/tests/api/test_recovery_reasoning.py
@@ -6,7 +6,6 @@ import pytest
from free_claude_code.core.anthropic import aggregate_anthropic_sse_to_message
from free_claude_code.core.anthropic.stream_contracts import parse_sse_text
-from free_claude_code.core.history_replay import decode_replay
from free_claude_code.providers.stream_recovery import RecoveryHoldbackBuffer
from tests.api.test_delivered_stream_recovery import (
assert_completed,
@@ -81,10 +80,7 @@ async def test_nullable_reasoning_content_preserves_healthy_response(wire):
if wire == "responses":
item = parse_sse_text(raw)[-1].data["response"]["output"][0]
assert item["content"] is None
- assert (
- decode_replay(item["encrypted_content"]).native
- == events[-1]["response"]["output"][0]
- )
+ assert item == events[-1]["response"]["output"][0]
@pytest.mark.asyncio
@@ -218,12 +214,13 @@ async def test_synthetic_reasoning_closure_preserves_followup_history(field, see
history = [*deepcopy(output), {"role": "user", "content": "Next question."}]
await delivered(send("responses", history), "responses")
assert len(bodies) == 3
- native = bodies[2]["input"][0]
+ native = output[0]
assert native["type"] == "reasoning"
assert native["id"] == first[1]["item"]["id"]
assert native["status"] == "completed"
assert native[field][0]["text"] == seeded + "Find 17."
assert native["extension"] == first[1]["item"]["extension"]
+ assert str(bodies[2]).count(seeded + "Find 17.") == 1
assert "previous provider stream" not in str(bodies[2])
assert history[:-1] == output
@@ -260,10 +257,7 @@ async def test_readable_reasoning_survives_repeated_continuation_and_followup(wi
send(wire, [*output, {"role": "user", "content": "Next question."}]), wire
)
assert len(bodies) == 4
- native = next(
- item for item in bodies[3]["input"] if item.get("type") == "reasoning"
- )
- assert native == second[3]["item"]
+ assert str(bodies[3]).count("Find 17.") == 1
assert "previous provider stream" not in str(bodies[3])
diff --git a/tests/api/test_tool_call_buffer_transports.py b/tests/api/test_tool_call_buffer_transports.py
index a93bca99..523835d6 100644
--- a/tests/api/test_tool_call_buffer_transports.py
+++ b/tests/api/test_tool_call_buffer_transports.py
@@ -3,7 +3,6 @@
import pytest
from free_claude_code.core.anthropic.stream_contracts import parse_sse_text
-from free_claude_code.core.history_replay import decode_replay
from free_claude_code.providers.open_router import OpenRouterProvider
from tests.api.test_tool_call_buffer import _response
from tests.providers.support import immediate_admission, make_provider_config
@@ -234,9 +233,4 @@ async def test_native_signed_thinking_interleaves_with_parallel_tool_calls(wire)
calls = [item["call_id"] for item in output if item["type"] == "function_call"]
signature = output[0]["encrypted_content"]
assert calls == ["call_1", "call_2"]
- assert decode_replay(signature).native == {
- "type": "thinking",
- "thinking": "before tools during tools",
- "signature": "signed-native",
- "extension": {"keep": 17},
- }
+ assert signature == "signed-native"
diff --git a/tests/core/openai_responses/test_chat_request.py b/tests/core/openai_responses/test_chat_request.py
index 8b803175..86fafa98 100644
--- a/tests/core/openai_responses/test_chat_request.py
+++ b/tests/core/openai_responses/test_chat_request.py
@@ -138,10 +138,11 @@ def test_build_responses_chat_request_preserves_rich_supported_semantics() -> No
},
{
"role": "assistant",
- "content": "[Earlier reasoning summary]\nUse the tool.",
+ "content": "",
"reasoning_content": "",
"reasoning_details": [
- {"type": "reasoning.encrypted", "data": "opaque-replay"}
+ {"type": "reasoning.summary", "summary": "Use the tool."},
+ {"type": "reasoning.encrypted", "data": "opaque-replay"},
],
"tool_calls": [
{
diff --git a/tests/core/openai_responses/test_provider_input.py b/tests/core/openai_responses/test_provider_input.py
index 3d4e0b4c..6218fc6b 100644
--- a/tests/core/openai_responses/test_provider_input.py
+++ b/tests/core/openai_responses/test_provider_input.py
@@ -83,12 +83,17 @@ def test_build_responses_provider_request_preserves_multiturn_protocol() -> None
assert body["include"] == ["reasoning.encrypted_content"]
assert body["reasoning"] == {"effort": "xhigh", "summary": "auto"}
assert body["tool_choice"] == {"type": "function", "name": "lookup"}
+ assert body["input"][0]["id"].startswith("rs_")
+ assert body["input"][1]["id"].startswith("rs_")
+ assert body["input"][0]["id"] != body["input"][1]["id"]
assert body["input"][0] == {
+ "id": body["input"][0]["id"],
"type": "reasoning",
"summary": [],
"content": [{"type": "reasoning_text", "text": "summary"}],
}
assert body["input"][1] == {
+ "id": body["input"][1]["id"],
"type": "reasoning",
"summary": [],
"encrypted_content": "opaque",
diff --git a/tests/core/openai_responses/test_provider_stream.py b/tests/core/openai_responses/test_provider_stream.py
index b4222f19..3817a2c4 100644
--- a/tests/core/openai_responses/test_provider_stream.py
+++ b/tests/core/openai_responses/test_provider_stream.py
@@ -133,7 +133,11 @@ def test_responses_provider_stream_preserves_reasoning_tools_usage_and_ids() ->
for event in events
if event.event == "content_block_start"
]
- assert {"type": "redacted_thinking", "data": "opaque"} in starts
+ assert [
+ event.data["delta"]["signature"]
+ for event in events
+ if event.data.get("delta", {}).get("type") == "signature_delta"
+ ] == ["opaque"]
assert {
"type": "tool_use",
"id": "call_1",
diff --git a/tests/core/openai_responses/test_responses_from_messages_stream.py b/tests/core/openai_responses/test_responses_from_messages_stream.py
index 8dda1910..702952d0 100644
--- a/tests/core/openai_responses/test_responses_from_messages_stream.py
+++ b/tests/core/openai_responses/test_responses_from_messages_stream.py
@@ -11,10 +11,6 @@ from free_claude_code.core.anthropic.native import (
)
from free_claude_code.core.anthropic.stream_contracts import parse_sse_lines
from free_claude_code.core.failures import ExecutionFailure, FailureKind
-from free_claude_code.core.history_replay import (
- ReplayOrigin,
- decode_replay,
-)
from free_claude_code.core.json_types import JsonObject, JsonValue
from free_claude_code.core.openai_responses import (
AnthropicToResponsesStream,
@@ -22,8 +18,6 @@ from free_claude_code.core.openai_responses import (
build_responses_messages_request,
)
-_SCOPE = "github_copilot/anthropic_messages"
-
def _stream(*, tools: list[JsonObject] | None = None) -> AnthropicToResponsesStream:
request = OpenAIResponsesRequest(model="public", input="hi", tools=tools)
@@ -34,7 +28,6 @@ def _stream(*, tools: list[JsonObject] | None = None) -> AnthropicToResponsesStr
request,
public_model="public",
tool_identities=prepared.tool_identities,
- replay_origin=ReplayOrigin(_SCOPE, "messages", "", "", "upstream"),
)
@@ -155,15 +148,8 @@ def test_text_thinking_and_redacted_blocks_keep_order_replay_and_exact_usage() -
first = output[0]
assert isinstance(first, Mapping)
assert first["content"] == [{"type": "reasoning_text", "text": "think"}]
- assert decode_replay(cast(str, first["encrypted_content"])).native == {
- "type": "thinking",
- "thinking": "think",
- "signature": "sig-end",
- "extension": {"x": 1},
- }
- assert decode_replay(
- cast(str, cast(Mapping[str, JsonValue], output[1])["encrypted_content"])
- ).native == {"type": "redacted_thinking", "data": "opaque"}
+ assert first["encrypted_content"] == "sig-end"
+ assert cast(Mapping[str, JsonValue], output[1])["encrypted_content"] == "opaque"
assert cast(Mapping[str, JsonValue], output[2])["content"] == [
{"type": "output_text", "text": "Hello world", "annotations": []}
]
@@ -293,8 +279,8 @@ def test_signature_only_thinking_has_replay_without_invented_visible_text() -> N
events = _end(stream)
response = cast(Mapping[str, JsonValue], events[-1]["response"])
item = cast(list[JsonObject], response["output"])[0]
- assert "content" not in item
- assert decode_replay(cast(str, item["encrypted_content"])).native["thinking"] == ""
+ assert item["content"] == [{"type": "reasoning_text", "text": ""}]
+ assert item["encrypted_content"] == "opaque"
assert "output_tokens_details" not in cast(
Mapping[str, JsonValue], response["usage"]
)
diff --git a/tests/core/test_history_replay.py b/tests/core/test_history_replay.py
index 0d52f2ba..aab84ea0 100644
--- a/tests/core/test_history_replay.py
+++ b/tests/core/test_history_replay.py
@@ -16,7 +16,6 @@ from free_claude_code.core.history_replay import (
encode_replay,
prepare_history,
resolve_messages_replay,
- responses_replay_item,
)
@@ -38,7 +37,10 @@ def _native():
def test_response_history_roundtrip_across_providers_and_restart():
native = _native()
- item = responses_replay_item(native, _origin())
+ item = {
+ **native,
+ "encrypted_content": encode_replay(ReplayRecord(_origin(), native)),
+ }
saved = {
"input": [
item,
@@ -140,7 +142,10 @@ def test_messages_native_blocks_are_exact_after_carrier_replay(block):
def test_native_replay_connection_is_scoped_but_model_name_is_not_a_rule():
- item = responses_replay_item(_native(), _origin())
+ item = {
+ **_native(),
+ "encrypted_content": encode_replay(ReplayRecord(_origin(), _native())),
+ }
assert (
prepare_history({"input": [item]}, _origin(connection="other"))["input"] == []
)
diff --git a/tests/core/test_history_switching.py b/tests/core/test_history_switching.py
index ae93dddf..5726dfdd 100644
--- a/tests/core/test_history_switching.py
+++ b/tests/core/test_history_switching.py
@@ -63,10 +63,9 @@ def test_grouped_tool_calls_keep_both_reasoning_records():
]
)
assistant = messages[1]
- assert assistant["reasoning_content"] == "first\nsecond"
- assert [detail["data"] for detail in assistant["reasoning_details"]] == [
- "opaque1",
- "opaque2",
+ assert assistant["reasoning_details"] == [
+ {"type": "reasoning.text", "text": "first", "signature": "opaque1"},
+ {"type": "reasoning.text", "text": "second", "signature": "opaque2"},
]
@@ -86,9 +85,12 @@ def test_malformed_tool_call_keeps_its_reasoning_and_error_result():
]
)
assistant = next(message for message in messages if message.get("tool_calls"))
- assert assistant["reasoning_content"] == "broken call only"
- assert [detail["data"] for detail in assistant["reasoning_details"]] == [
- "opaque-broken"
+ assert assistant["reasoning_details"] == [
+ {
+ "type": "reasoning.text",
+ "text": "broken call only",
+ "signature": "opaque-broken",
+ }
]
assert [
(call["id"], call["function"]["arguments"]) for call in assistant["tool_calls"]
@@ -271,7 +273,7 @@ def test_deepseek_off_to_on_preserves_effort_and_all_available_history(wire):
assert request.model_dump() == original
-def test_summary_is_context_even_when_destination_replays_full_reasoning():
+def test_summary_keeps_its_classification_when_destination_supports_details():
messages = _chat(
[
{"role": "user", "content": "first"},
@@ -286,7 +288,9 @@ def test_summary_is_context_even_when_destination_replays_full_reasoning():
]
)
assert "reasoning_content" not in messages[1]
- assert "[Earlier reasoning summary]" in messages[1]["content"]
+ assert messages[1]["reasoning_details"] == [
+ {"type": "reasoning.summary", "summary": "A summary, not the hidden chain."}
+ ]
def test_image_tool_result_does_not_start_a_new_user_turn():
diff --git a/tests/providers/test_cline_pass.py b/tests/providers/test_cline_pass.py
index 4cad67e1..77b3ca74 100644
--- a/tests/providers/test_cline_pass.py
+++ b/tests/providers/test_cline_pass.py
@@ -17,7 +17,6 @@ from free_claude_code.core.anthropic.stream_contracts import (
text_content,
thinking_content,
)
-from free_claude_code.core.history_replay import decode_replay
from free_claude_code.core.json_types import JsonObject, JsonValue
from free_claude_code.core.reasoning import ReasoningPolicy
from free_claude_code.providers.model_listing import ModelListResponseError
@@ -330,12 +329,11 @@ async def test_stream_uses_upstream_sse_and_preserves_reasoning_details(
assert thinking_content(events) == "plan "
assert text_content(events) == "done"
records = [
- decode_replay(event.data["delta"]["signature"]).native
+ event.data["delta"]["signature"]
for event in events
if event.data.get("delta", {}).get("type") == "signature_delta"
]
- assert len(records) == 1
- assert records[0]["reasoning_details"] == [detail]
+ assert records == [detail["signature"]]
assert stream.closed
diff --git a/tests/providers/test_github_copilot_responses_identity.py b/tests/providers/test_github_copilot_responses_identity.py
index b3f651b9..82662012 100644
--- a/tests/providers/test_github_copilot_responses_identity.py
+++ b/tests/providers/test_github_copilot_responses_identity.py
@@ -19,7 +19,6 @@ from free_claude_code.core.anthropic.stream_contracts import (
parse_sse_text,
)
from free_claude_code.core.failures import ExecutionFailure
-from free_claude_code.core.history_replay import decode_replay
from free_claude_code.core.json_types import JsonObject
from free_claude_code.core.openai_responses import OpenAIResponsesRequest
from free_claude_code.providers.github_copilot.types import CopilotEgress
@@ -382,12 +381,6 @@ async def test_copilot_interleaved_tools_use_output_positions_and_preserve_final
try:
received = await _run(harness, _input(True, harness.runtime.name))
output = _assert_native_identity(received)
- output = [
- decode_replay(str(item["encrypted_content"])).native
- if item.get("type") == "reasoning"
- else item
- for item in output
- ]
assert output == [
{**final_output[0], "id": first["id"]},
{**final_output[1], "id": second["id"]},
diff --git a/tests/providers/test_history_transports.py b/tests/providers/test_history_transports.py
index 0d2b2e41..ad35145e 100644
--- a/tests/providers/test_history_transports.py
+++ b/tests/providers/test_history_transports.py
@@ -16,12 +16,14 @@ 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.failures import ExecutionFailure, FailureKind
from free_claude_code.core.history_replay import (
+ ReplayRecord,
decode_replay,
encode_replay,
resolve_messages_replay,
)
from free_claude_code.core.openai_responses import OpenAIResponsesRequest
from free_claude_code.core.reasoning import ReasoningPolicy
+from free_claude_code.providers.history_replay import replay_origin
from free_claude_code.providers.open_router import OpenRouterProvider
from tests.application.test_execution import (
ControlledProvider,
@@ -254,10 +256,42 @@ def _carrier(history, wire):
@pytest.mark.asyncio
@pytest.mark.parametrize("protocol", ["responses", "messages", "chat"])
@pytest.mark.parametrize("wire", ["responses", "messages"])
-async def test_provider_a_b_a_preserves_exact_native_history(protocol, wire):
- async with _harness(protocol) as (send, bodies, _):
- saved = await _saved_reply(
- send(wire, [{"role": "user", "content": "hello"}]), wire
+async def test_legacy_provider_a_b_a_preserves_exact_native_history(protocol, wire):
+ async with _harness(protocol) as (send, bodies, provider):
+ if protocol == "messages":
+ endpoint = Endpoint()
+ endpoint.token = "a"
+ origin = replay_origin(
+ "test/messages",
+ "messages",
+ "native",
+ endpoint=await endpoint.endpoint(),
+ )
+ else:
+ origin = replay_origin(
+ "TEST_RESPONSES" if protocol == "responses" else "OPENROUTER",
+ protocol,
+ "actual-returned",
+ client=provider._client,
+ )
+ carrier = encode_replay(ReplayRecord(origin, _native(protocol)))
+ saved = (
+ [{**_native(protocol), "encrypted_content": carrier}]
+ if wire == "responses" and protocol == "responses"
+ else [{"type": "reasoning", "summary": [], "encrypted_content": carrier}]
+ if wire == "responses"
+ else [
+ {
+ "role": "assistant",
+ "content": [
+ {
+ "type": "thinking",
+ "thinking": "Find 17.",
+ "signature": carrier,
+ }
+ ],
+ }
+ ]
)
record = decode_replay(_carrier(saved, wire))
assert record.native == _native(protocol)
@@ -431,7 +465,7 @@ async def test_stream_history_rejection_respects_the_public_commit_boundary(
@pytest.mark.asyncio
-async def test_successful_fallback_stamps_its_own_origin_and_gets_unmodified_input():
+async def test_successful_fallback_preserves_raw_state_and_gets_unmodified_input():
class MutatingPrimary(ControlledProvider):
async def stream_messages(self, request, input_tokens=0, **kwargs):
request.messages[0].content = "primary mutation"
@@ -476,9 +510,7 @@ async def test_successful_fallback_stamps_its_own_origin_and_gets_unmodified_inp
),
"messages",
)
- record = decode_replay(_carrier(saved, "messages"))
- assert record.origin.provider == "OPENROUTER"
- assert record.origin.model == "actual-returned"
+ assert _carrier(saved, "messages") == "opaque-original"
assert bodies[0]["messages"][0]["content"] == "hello"
assert routed.request.model_dump() == original
finally:
@@ -618,10 +650,8 @@ async def test_chat_buffered_content_preserves_reasoning_through_next_request(
async with _harness("chat", lambda bodies: (200, events)) as (_, bodies, provider):
saved = await _saved_reply(send([{"role": "user", "content": "hello"}]), wire)
- assert decode_replay(_carrier(saved, wire)).native == {
- "reasoning_content": "Plan.",
- "reasoning_details": expected,
- }
+ assert "Plan." in json.dumps(saved)
+ assert "fcc:history" not in json.dumps(saved)
history = deepcopy(saved)
blocks = saved[0]["content"] if wire == "messages" else saved
calls = [
@@ -658,10 +688,11 @@ async def test_chat_buffered_content_preserves_reasoning_through_next_request(
await _saved_reply(send(history), wire)
assert len(bodies) == 2
assert [
- detail
+ detail.get("data", detail.get("signature"))
for message in bodies[-1]["messages"]
for detail in message.get("reasoning_details", [])
- ] == expected
+ ] == [detail["data"] for detail in expected]
+ assert json.dumps(bodies[-1]).count("Plan.") == 1
assert history == original
@@ -675,7 +706,7 @@ async def test_chat_encrypted_only_completion_does_not_add_blank_text(wire):
saved = await _saved_reply(
send(wire, [{"role": "user", "content": "hello"}]), wire
)
- assert decode_replay(_carrier(saved, wire)).native == {"reasoning_details": details}
+ assert _carrier(saved, wire) == "opaque-only"
blocks = saved[0]["content"] if wire == "messages" else saved
assert [block["type"] for block in blocks] == [
"redacted_thinking" if wire == "messages" else "reasoning"
@@ -685,15 +716,16 @@ async def test_chat_encrypted_only_completion_does_not_add_blank_text(wire):
@pytest.mark.asyncio
@pytest.mark.parametrize("wire", ["messages", "responses"])
@pytest.mark.parametrize("committed", [False, True])
-async def test_chat_pending_reasoning_is_finalized_on_failure_or_discarded_on_retry(
- wire, committed
-):
+async def test_chat_pending_reasoning_is_discarded_on_failure_or_retry(wire, committed):
text = "Plan." * (30000 if committed else 1)
first = {"type": "reasoning.encrypted", "data": "first", "index": 0}
second = {"type": "reasoning.encrypted", "data": "second", "index": 1}
+ readable = (
+ [{"type": "reasoning.text", "text": text, "index": 2}] if committed else []
+ )
events = _chat_reasoning_events(
[
- {"reasoning_content": text, "reasoning_details": [first]},
+ {"reasoning_content": text, "reasoning_details": [first, *readable]},
{"content": "<"},
{"reasoning_details": [second]},
]
@@ -712,7 +744,7 @@ async def test_chat_pending_reasoning_is_finalized_on_failure_or_discarded_on_re
if not committed:
saved = await _saved_reply(stream, wire)
assert len(bodies) == 2
- assert decode_replay(_carrier(saved, wire)).native == _native("chat")
+ assert _carrier(saved, wire) == "opaque-original"
return
if wire == "messages":
output = ""
@@ -734,10 +766,9 @@ async def test_chat_pending_reasoning_is_finalized_on_failure_or_discarded_on_re
assert response["status"] == "failed"
saved = response["output"]
assert len(bodies) == 1
- assert decode_replay(_carrier(saved, wire)).native == {
- "reasoning_content": text,
- "reasoning_details": [first, second],
- }
+ assert text in json.dumps(saved)
+ assert "first" not in json.dumps(saved)
+ assert "second" not in json.dumps(saved)
@pytest.mark.asyncio
@@ -752,31 +783,49 @@ async def test_chat_plaintext_beside_encrypted_details_survives_switching(
events = _chat_reasoning_events(
[{"reasoning_content": text, "reasoning_details": details}]
)
- async with _harness("chat", lambda bodies: (200, events)) as (source, sent, _):
+ async with _harness("chat", lambda bodies: (200, events)) as (
+ source,
+ sent,
+ provider,
+ ):
saved = await _saved_reply(
source(wire, [{"role": "user", "content": "hello"}]), wire
)
- record = decode_replay(_carrier(saved, wire))
if incomplete_saved_record:
- record.native.pop("reasoning_content", None)
- carrier = encode_replay(record)
+ origin = replay_origin(
+ "OPENROUTER", "chat", "actual-returned", client=provider._client
+ )
+ carrier = encode_replay(
+ ReplayRecord(origin, {"reasoning_details": details})
+ )
if wire == "responses":
- saved[0]["encrypted_content"] = carrier
+ next(item for item in saved if item["type"] == "reasoning")[
+ "encrypted_content"
+ ] = carrier
else:
# 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"] = carrier
+ next(
+ block
+ for block in saved[0]["content"]
+ if block["type"] == "thinking"
+ )["signature"] = carrier
else:
- assert record.native["reasoning_content"] == text
+ assert _carrier(saved, wire) == "opaque-only"
history = json.loads(json.dumps([*saved, {"role": "user", "content": "next"}]))
original = deepcopy(history)
async with _harness(destination, key="b") as (foreign, bodies, _):
await _saved_reply(foreign(wire, history), wire)
outgoing = json.dumps(bodies[-1])
assert outgoing.count(text) == 1
- assert "opaque-only" not in outgoing and "fcc:history" not in outgoing
+ assert "fcc:history" not in outgoing
+ assert outgoing.count("opaque-only") == int(not incomplete_saved_record)
await _saved_reply(source(wire, history), wire)
- assert sent[-1]["messages"][0]["reasoning_details"] == details
+ assert sent[-1]["messages"][0]["reasoning_details"] == (
+ details
+ if incomplete_saved_record
+ else [{"type": "reasoning.text", "text": text, "signature": "opaque-only"}]
+ )
assert json.dumps(sent[-1]).count(text) == 1
assert history == original
@@ -784,13 +833,10 @@ async def test_chat_plaintext_beside_encrypted_details_survives_switching(
@pytest.mark.asyncio
@pytest.mark.parametrize("wire", ["messages", "responses"])
@pytest.mark.parametrize("encrypted", [False, True])
-async def test_chat_summary_fragments_remain_readable_and_restore_exactly(
- wire, encrypted
-):
+async def test_chat_summary_fragments_remain_readable_in_native_fields(wire, encrypted):
first = {"type": "reasoning.summary", "summary": "Short ", "index": 0}
second = {"type": "reasoning.summary", "summary": "summary.", "index": 0}
opaque = {"type": "reasoning.encrypted", "data": "opaque-only", "index": 1}
- details = [{**first, "summary": "Short summary."}, *([opaque] if encrypted else [])]
events = _chat_reasoning_events(
[
{"reasoning_details": [first]},
@@ -802,17 +848,21 @@ async def test_chat_summary_fragments_remain_readable_and_restore_exactly(
source(wire, [{"role": "user", "content": "hello"}]), wire
)
assert json.dumps(saved).count("Short summary.") == 1
- record = decode_replay(_carrier(saved, wire))
- assert record.native["reasoning_details"] == details
+ assert "fcc:history" not in json.dumps(saved)
+ if wire == "responses":
+ assert saved[0]["summary"] == [
+ {"type": "summary_text", "text": "Short summary."}
+ ]
+ if encrypted:
+ assert _carrier(saved, wire) == "opaque-only"
history = [*saved, {"role": "user", "content": "next"}]
async with _harness("responses", key="b") as (foreign, bodies, _):
await _saved_reply(foreign(wire, history), wire)
- assert bodies[-1]["input"][0] == {
- "role": "assistant",
- "content": "[Earlier reasoning summary]\nShort summary.",
- }
+ assert json.dumps(bodies[-1]).count("Short summary.") == 1
+ assert json.dumps(bodies[-1]).count("opaque-only") == int(encrypted)
await _saved_reply(source(wire, history), wire)
- assert sent[-1]["messages"][0]["reasoning_details"] == details
+ assert json.dumps(sent[-1]).count("Short summary.") == 1
+ assert json.dumps(sent[-1]).count("opaque-only") == int(encrypted)
@pytest.mark.asyncio
@@ -873,23 +923,13 @@ async def test_chat_reasoning_groups_do_not_inherit_previous_plaintext():
source("messages", [{"role": "user", "content": "hi"}]), "messages"
)
records = [
- decode_replay(block["signature"]).native
+ (block["thinking"], block["signature"])
for block in saved[0]["content"]
if block["type"] == "thinking"
]
assert records == [
- {
- "reasoning_content": "First thought.",
- "reasoning_details": [
- {"type": "reasoning.encrypted", "data": "first-secret", "index": 0}
- ],
- },
- {
- "reasoning_content": "Second thought.",
- "reasoning_details": [
- {"type": "reasoning.encrypted", "data": "second-secret", "index": 0}
- ],
- },
+ ("First thought.", "first-secret"),
+ ("Second thought.", "second-secret"),
]
diff --git a/tests/providers/test_kilo.py b/tests/providers/test_kilo.py
index b023ac48..1bf47b65 100644
--- a/tests/providers/test_kilo.py
+++ b/tests/providers/test_kilo.py
@@ -15,7 +15,6 @@ from free_claude_code.core.anthropic.stream_contracts import (
text_content,
thinking_content,
)
-from free_claude_code.core.history_replay import decode_replay
from free_claude_code.core.model_capabilities import ModelInputModality
from free_claude_code.core.reasoning import ReasoningPolicy
from free_claude_code.providers.kilo import KiloProvider
@@ -279,15 +278,12 @@ async def test_stream_uses_reasoning_field_without_duplicating_plain_details(
assert thinking_content(events) == "plan "
assert text_content(events) == "done"
records = [
- decode_replay(event.data["delta"]["signature"]).native
+ event.data["content_block"]["data"]
for event in events
- if event.data.get("delta", {}).get("type") == "signature_delta"
- ]
- assert len(records) == 1
- assert records[0]["reasoning_details"] == [
- {"type": "reasoning.text", "text": "plan "},
- encrypted,
+ if event.event == "content_block_start"
+ and event.data["content_block"]["type"] == "redacted_thinking"
]
+ assert records == [encrypted["data"]]
assert stream.closed
@@ -327,7 +323,7 @@ async def test_stream_restarts_reasoning_reconciliation_after_early_retry(
@pytest.mark.asyncio
-async def test_stream_omits_all_reasoning_representations_when_disabled(
+async def test_stream_hides_readable_reasoning_and_preserves_opaque_when_disabled(
kilo_provider,
):
stream = AsyncStream(
@@ -367,11 +363,12 @@ async def test_stream_omits_all_reasoning_representations_when_disabled(
events = parse_sse_text(event_text)
assert thinking_content(events) == ""
assert text_content(events) == "done"
- assert all(
- event.data.get("content_block", {}).get("type") != "redacted_thinking"
+ assert [
+ event.data["content_block"]["data"]
for event in events
if event.event == "content_block_start"
- )
+ and event.data["content_block"]["type"] == "redacted_thinking"
+ ] == ["opaque"]
assert stream.closed
diff --git a/tests/providers/test_native_reasoning_completion.py b/tests/providers/test_native_reasoning_completion.py
new file mode 100644
index 00000000..45362462
--- /dev/null
+++ b/tests/providers/test_native_reasoning_completion.py
@@ -0,0 +1,746 @@
+"""Native reasoning survives conversion without trailing FCC replay records."""
+
+import asyncio
+import json
+from copy import deepcopy
+
+import httpx2
+import pytest
+
+from free_claude_code.core.anthropic import ReasoningReplayMode
+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.failures import ExecutionFailure, FailureKind
+from free_claude_code.core.openai_responses.chat_request import (
+ build_responses_chat_request,
+)
+from free_claude_code.core.openai_responses.models import OpenAIResponsesRequest
+from free_claude_code.core.openai_responses.provider_input import (
+ build_responses_provider_request,
+)
+from free_claude_code.core.openai_responses.provider_stream import (
+ ResponsesProviderStream,
+)
+from free_claude_code.core.reasoning import ReasoningPolicy
+from free_claude_code.providers.gemini import GeminiProvider
+from free_claude_code.providers.openai_chat.reasoning_details import (
+ StructuredReasoningStream,
+)
+from free_claude_code.providers.openai_chat.stream_output import (
+ AnthropicChatStreamOutput,
+)
+from tests.api.test_hidden_stream_retries import delivered
+from tests.providers.support import immediate_admission, make_provider_config
+from tests.providers.test_history_transports import (
+ _events_for,
+ _harness,
+ _native,
+ _saved_reply,
+)
+
+
+def _chunks(deltas):
+ return [
+ {
+ "id": "chat_native_reasoning",
+ "object": "chat.completion.chunk",
+ "created": 0,
+ "model": "actual-returned",
+ "choices": [{"index": 0, "delta": delta, "finish_reason": finish}],
+ }
+ for delta, finish in deltas
+ ]
+
+
[email protected]("wire", ["messages", "responses"])
[email protected]("thinking", ["", "Plan."])
+async def test_plain_reasoning_emits_no_artificial_replay_state(wire, thinking):
+ upstream = _chunks(
+ [
+ ({"reasoning_content": thinking}, None),
+ ({"content": "Answer."}, "stop"),
+ ]
+ )
+ async with _harness("chat", lambda _: (200, upstream)) as (send, _, _):
+ stream = "".join(
+ [
+ chunk
+ async for chunk in send(wire, [{"role": "user", "content": "hello"}])
+ ]
+ )
+ assert "fcc:history:" not in stream
+ events = parse_sse_text(stream)
+ if wire == "messages":
+ starts = [
+ event.data for event in events if event.event == "content_block_start"
+ ]
+ assert [event["content_block"]["type"] for event in starts] == ["text"] + (
+ ["thinking"] if thinking else []
+ )
+ stops = [
+ event.data["index"]
+ for event in events
+ if event.event == "content_block_stop"
+ ]
+ assert stops[-1] == next(
+ event["index"]
+ for event in starts
+ if event["content_block"]["type"] == "text"
+ )
+ else:
+ output = events[-1].data["response"]["output"]
+ assert [item["type"] for item in output] == ["message"] + (
+ ["reasoning"] if thinking else []
+ )
+ assert all(not item.get("encrypted_content") for item in output)
+
+
[email protected]("wire", ["messages", "responses"])
+async def test_late_opaque_metadata_finishes_before_answer(wire):
+ upstream = _chunks(
+ [
+ (
+ {
+ "reasoning_content": "Plan.",
+ "reasoning_details": [
+ {"type": "reasoning.encrypted", "data": "first", "index": 0}
+ ],
+ },
+ None,
+ ),
+ ({"content": "Answer."}, None),
+ (
+ {
+ "reasoning_details": [
+ {"type": "reasoning.encrypted", "data": "second", "index": 0}
+ ]
+ },
+ "stop",
+ ),
+ ]
+ )
+ async with _harness("chat", lambda _: (200, upstream)) as (send, _, _):
+ stream = "".join(
+ [
+ chunk
+ async for chunk in send(wire, [{"role": "user", "content": "hello"}])
+ ]
+ )
+ assert "fcc:history:" not in stream
+ events = parse_sse_text(stream)
+ if wire == "messages":
+ starts = [
+ event.data for event in events if event.event == "content_block_start"
+ ]
+ text_index = next(
+ event["index"]
+ for event in starts
+ if event["content_block"]["type"] == "text"
+ )
+ assert [
+ event.data["index"]
+ for event in events
+ if event.event == "content_block_stop"
+ ][-1] == text_index
+ assert [
+ event.data["delta"]["signature"]
+ for event in events
+ if event.data.get("delta", {}).get("type") == "signature_delta"
+ ] == ["firstsecond"]
+ else:
+ done = [
+ event.data["item"]
+ for event in events
+ if event.event == "response.output_item.done"
+ ]
+ assert [
+ item.get("encrypted_content")
+ for item in done
+ if item["type"] == "reasoning"
+ ] == ["firstsecond"]
+ assert done[-1]["type"] == "message"
+
+
[email protected]("protocol", ["messages", "responses"])
+async def test_native_reasoning_is_forwarded_without_replacement(protocol):
+ async with _harness(protocol) as (send, _, _):
+ saved = await _saved_reply(
+ send(protocol, [{"role": "user", "content": "hello"}]), protocol
+ )
+ item = saved[0]["content"][0] if protocol == "messages" else saved[0]
+ assert item == _native(protocol)
+
+
[email protected]("wire", ["messages", "responses"])
[email protected]("signature_first", [False, True])
+async def test_google_message_signature_round_trips_in_its_native_field(
+ wire, signature_first
+):
+ upstream = _chunks(
+ [
+ ({"content": "Pong"}, None),
+ (
+ {
+ "extra_content": {
+ "google": {"thought_signature": "google-signature"}
+ }
+ },
+ "stop",
+ ),
+ ]
+ )
+ if signature_first:
+ signature = upstream[1]["choices"][0]
+ signature["finish_reason"] = None
+ upstream[0]["choices"][0]["finish_reason"] = "stop"
+ upstream.reverse()
+
+ def factory():
+ return GeminiProvider(
+ make_provider_config(
+ api_key="test-key", base_url="https://provider.invalid/v1"
+ ),
+ admission=immediate_admission(),
+ )
+
+ async with _harness(
+ "chat", lambda _: (200, upstream), chat_provider_factory=factory
+ ) 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
+ )
+ assistant = [
+ message for message in bodies[-1]["messages"] if message["role"] == "assistant"
+ ]
+ assert len(assistant) == 1
+ assert assistant[0]["content"] == "Pong"
+ assert (
+ assistant[0]["extra_content"]["google"]["thought_signature"]
+ == "google-signature"
+ )
+ assert "reasoning_details" not in assistant[0]
+
+
[email protected]("wire", ["messages", "responses"])
[email protected]("provider_kind", ["google", "structured"])
+async def test_interrupted_opaque_only_state_keeps_hidden_retry_eligible(
+ wire, provider_kind
+):
+ delta = (
+ {"extra_content": {"google": {"thought_signature": "abandoned-signature"}}}
+ if provider_kind == "google"
+ else {
+ "reasoning_details": [
+ {
+ "type": "reasoning.encrypted",
+ "data": "abandoned-signature",
+ "index": 0,
+ }
+ ]
+ }
+ )
+ first = _chunks([(delta, None)])
+
+ class Cutoff(httpx2.AsyncByteStream):
+ async def __aiter__(self):
+ yield ("data: " + json.dumps(first[0]) + "\n\n").encode()
+ await asyncio.sleep(0.9)
+
+ def factory():
+ return GeminiProvider(
+ make_provider_config(
+ api_key="test-key", base_url="https://provider.invalid/v1"
+ ),
+ admission=immediate_admission(max_attempts=2),
+ )
+
+ second = _chunks([({"content": "Recovered answer."}, "stop")])
+ async with _harness(
+ "chat",
+ lambda bodies: (200, Cutoff() if len(bodies) == 1 else second),
+ chat_provider_factory=factory if provider_kind == "google" else None,
+ max_attempts=2,
+ ) as (_, bodies, provider):
+ if wire == "messages":
+ stream = provider.stream_messages(
+ MessagesRequest(
+ model="requested", messages=[{"role": "user", "content": "hello"}]
+ ),
+ reasoning=ReasoningPolicy.off(),
+ )
+ else:
+ stream = provider.stream_responses(
+ OpenAIResponsesRequest(model="requested", input="hello"),
+ reasoning=ReasoningPolicy.off(),
+ )
+ raw = await delivered(stream, wire)
+ assert len(bodies) == 2
+ assert "Recovered answer." in raw
+ assert "abandoned-signature" not in raw
+ assert "response.failed" not in raw
+
+
+def test_failure_does_not_publish_unfinished_opaque_state():
+ output = AnthropicChatStreamOutput(
+ message_id="msg_test", model="public", input_tokens=1
+ )
+ reasoning = StructuredReasoningStream()
+ output.reasoning_replay = reasoning
+ frames = output.start_events()
+ frames += list(
+ reasoning.events(
+ {
+ "reasoning_details": [
+ {
+ "type": "reasoning.encrypted",
+ "data": "partial-cipher",
+ "index": 0,
+ }
+ ]
+ },
+ output,
+ native_reasoning="Plan.",
+ )
+ )
+ frames += output.ensure_text_block()
+ frames.append(output.emit_text_delta("Partial answer."))
+ output.defer_opaque_reasoning("pending-message-signature")
+ frames += output.finish_failure(
+ ExecutionFailure(FailureKind.UPSTREAM, 502, "Disconnected", True)
+ )
+ stream = "".join(frames)
+ assert "partial-cipher" not in stream
+ assert "pending-message-signature" not in stream
+ assert "message_stop" not in stream
+
+
+async def test_responses_late_cipher_preserves_thinking_and_final_answer():
+ original = _events_for("responses")
+ message = {
+ "id": "msg_answer",
+ "type": "message",
+ "role": "assistant",
+ "status": "completed",
+ "content": [{"type": "output_text", "text": "Pong", "annotations": []}],
+ }
+ terminal = original[-1]
+ terminal["response"]["output"].append(message)
+ upstream = [
+ *original[:3],
+ {
+ "type": "response.output_item.added",
+ "sequence_number": 3,
+ "output_index": 1,
+ "item": {**message, "status": "in_progress", "content": []},
+ },
+ {
+ "type": "response.output_text.delta",
+ "sequence_number": 4,
+ "output_index": 1,
+ "item_id": "msg_answer",
+ "content_index": 0,
+ "delta": "Pong",
+ },
+ {
+ "type": "response.output_item.done",
+ "sequence_number": 5,
+ "output_index": 1,
+ "item": message,
+ },
+ {**original[3], "sequence_number": 6},
+ {**terminal, "sequence_number": 7},
+ ]
+ async with _harness("responses", lambda _: (200, upstream)) as (send, _, _):
+ stream = "".join(
+ [
+ chunk
+ async for chunk in send(
+ "messages", [{"role": "user", "content": "hello"}]
+ )
+ ]
+ )
+ events = parse_sse_text(stream)
+ starts = [event.data for event in events if event.event == "content_block_start"]
+ text_index = next(
+ event["index"] for event in starts if event["content_block"]["type"] == "text"
+ )
+ assert [
+ event.data["index"] for event in events if event.event == "content_block_stop"
+ ][-1] == text_index
+ assert [
+ event.data["delta"]["signature"]
+ for event in events
+ if event.data.get("delta", {}).get("type") == "signature_delta"
+ ] == ["opaque-original"]
+
+
+async def test_responses_retains_distinct_summary_and_full_reasoning():
+ upstream = _chunks(
+ [
+ (
+ {
+ "reasoning_details": [
+ {
+ "type": "reasoning.summary",
+ "summary": "Short summary.",
+ "index": 0,
+ }
+ ]
+ },
+ None,
+ ),
+ ({"reasoning_content": "Distinct full thought."}, None),
+ ({"content": "Answer."}, "stop"),
+ ]
+ )
+ async with _harness("chat", lambda _: (200, upstream)) as (send, _, _):
+ saved = await _saved_reply(
+ send("responses", [{"role": "user", "content": "hello"}]), "responses"
+ )
+ reasoning = saved[0]
+ assert reasoning["summary"] == [{"type": "summary_text", "text": "Short summary."}]
+ assert reasoning["content"] == [
+ {"type": "reasoning_text", "text": "Distinct full thought."}
+ ]
+
+
[email protected]("wire", ["messages", "responses"])
+async def test_hiding_readable_thinking_preserves_opaque_replay_state(wire):
+ upstream = _chunks(
+ [
+ (
+ {
+ "reasoning_content": "Hidden thought.",
+ "reasoning_details": [
+ {
+ "type": "reasoning.encrypted",
+ "data": "opaque-kept",
+ "index": 0,
+ }
+ ],
+ },
+ None,
+ ),
+ ({"content": "Answer."}, "stop"),
+ ]
+ )
+ async with _harness("chat", lambda _: (200, upstream)) as (_, _, provider):
+ if wire == "messages":
+ stream = provider.stream_messages(
+ MessagesRequest(
+ model="requested", messages=[{"role": "user", "content": "hello"}]
+ ),
+ input_tokens=0,
+ reasoning=ReasoningPolicy.off(),
+ )
+ else:
+ stream = provider.stream_responses(
+ OpenAIResponsesRequest(model="requested", input="hello"),
+ input_tokens=0,
+ reasoning=ReasoningPolicy.off(),
+ )
+ output = "".join([chunk async for chunk in stream])
+ assert "Hidden thought." not in output
+ assert "opaque-kept" in output
+
+
+def test_converted_top_level_thought_has_valid_responses_identity():
+ body = build_responses_provider_request(
+ MessagesRequest(
+ model="native",
+ messages=[
+ {
+ "role": "assistant",
+ "content": "Answer.",
+ "reasoning_content": "Plan.",
+ }
+ ],
+ ),
+ reasoning=ReasoningPolicy.on(),
+ )
+ assert body["input"][0]["id"].startswith("rs_")
+
+
+def test_responses_summary_maps_to_structured_chat_summary():
+ request = OpenAIResponsesRequest(
+ model="native",
+ input=[
+ {
+ "type": "reasoning",
+ "summary": [{"type": "summary_text", "text": "Short summary."}],
+ "content": [{"type": "reasoning_text", "text": "Full thought."}],
+ "encrypted_content": "cipher",
+ },
+ {"type": "message", "role": "assistant", "content": "Answer."},
+ ],
+ )
+ body = build_responses_chat_request(
+ request,
+ reasoning_replay=ReasoningReplayMode.REASONING_CONTENT,
+ structured_reasoning_details=True,
+ ).body
+ messages = body["messages"]
+ assert isinstance(messages, list)
+ assistant = messages[0]
+ assert assistant["content"] == "Answer."
+ assert assistant["reasoning_details"] == [
+ {"type": "reasoning.text", "text": "Full thought.", "signature": "cipher"},
+ {"type": "reasoning.summary", "summary": "Short summary."},
+ ]
+ assert json.dumps(body).count("Full thought.") == 1
+
+
+def test_existing_responses_tool_delta_does_not_close_final_text():
+ stream = ResponsesProviderStream(
+ message_id="msg_test", model="native", input_tokens=1
+ )
+ frames = stream.start()
+ frames += stream.feed(
+ "response.output_item.added",
+ {
+ "item": {
+ "type": "function_call",
+ "id": "fc_1",
+ "call_id": "call_1",
+ "name": "lookup",
+ }
+ },
+ )
+ frames += stream.feed(
+ "response.function_call_arguments.delta", {"item_id": "fc_1", "delta": '{"q":'}
+ )
+ frames += stream.feed(
+ "response.output_text.delta", {"item_id": "msg_answer", "delta": "Answer."}
+ )
+ frames += stream.feed(
+ "response.function_call_arguments.delta", {"item_id": "fc_1", "delta": '"x"}'}
+ )
+ frames += stream.feed("response.completed", {"response": {}})
+ events = parse_sse_text("".join(frames))
+ starts = [event.data for event in events if event.event == "content_block_start"]
+ text_index = next(
+ event["index"] for event in starts if event["content_block"]["type"] == "text"
+ )
+ assert [
+ event.data["index"] for event in events if event.event == "content_block_stop"
+ ][-1] == text_index
+
+
[email protected]("wire", ["messages", "responses"])
+async def test_unrecognized_detail_data_is_not_a_reasoning_signature(wire):
+ upstream = _chunks(
+ [
+ (
+ {
+ "reasoning_details": [
+ {"type": "provider.usage", "data": "ordinary-extra-data"}
+ ],
+ "content": "Answer.",
+ },
+ "stop",
+ )
+ ]
+ )
+ async with _harness("chat", lambda _: (200, upstream)) as (send, _, _):
+ saved = await _saved_reply(
+ send(wire, [{"role": "user", "content": "hello"}]), wire
+ )
+ assert "ordinary-extra-data" not in str(saved)
+ blocks = saved[0]["content"] if wire == "messages" else saved
+ assert [block["type"] for block in blocks] == [
+ "text" if wire == "messages" else "message"
+ ]
+
+
[email protected]("wire", ["messages", "responses"])
+async def test_fragmented_summary_and_distinct_full_thought_replay_once(wire):
+ upstream = _chunks(
+ [
+ (
+ {
+ "reasoning_details": [
+ {"type": "reasoning.summary", "summary": "Short ", "index": 0}
+ ]
+ },
+ None,
+ ),
+ (
+ {
+ "reasoning_details": [
+ {"type": "reasoning.summary", "summary": "summary.", "index": 0}
+ ]
+ },
+ None,
+ ),
+ ({"reasoning_content": "Full thought."}, None),
+ ({"content": "Answer."}, "stop"),
+ ]
+ )
+ async with _harness("chat", lambda _: (200, upstream)) 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
+ )
+ assert json.dumps(saved).count("Short summary.") == 1
+ assert json.dumps(saved).count("Full thought.") == 1
+ assert json.dumps(bodies[-1]).count("Short summary.") == 1
+ assert json.dumps(bodies[-1]).count("Full thought.") == 1
+
+
+async def test_hidden_readable_only_cutoff_retries_without_delivered_content():
+ first = _chunks(
+ [
+ (
+ {
+ "reasoning_details": [
+ {
+ "type": "reasoning.text",
+ "text": "Hidden thought.",
+ "index": 0,
+ }
+ ]
+ },
+ None,
+ )
+ ]
+ )
+
+ class Cutoff(httpx2.AsyncByteStream):
+ async def __aiter__(self):
+ yield ("data: " + json.dumps(first[0]) + "\n\n").encode()
+ await asyncio.sleep(0.9)
+
+ second = _chunks([({"content": "Recovered answer."}, "stop")])
+ async with _harness(
+ "chat",
+ lambda bodies: (200, Cutoff() if len(bodies) == 1 else second),
+ max_attempts=2,
+ ) as (_, bodies, provider):
+ stream = provider.stream_responses(
+ OpenAIResponsesRequest(model="requested", input="hello"),
+ reasoning=ReasoningPolicy.off(),
+ )
+ raw = await delivered(stream, "responses")
+ assert len(bodies) == 2
+ assert "Recovered answer." in raw
+ assert "Hidden thought." not in raw
+ assert "response.failed" not in raw
+ assert '"type": "reasoning"' not in raw
+
+
[email protected]("answer_before_done", [False, True])
+async def test_completed_full_reasoning_survives_streamed_summary(answer_before_done):
+ upstream = deepcopy(_events_for("responses"))
+ upstream[3]["item"]["content"] = [
+ {"type": "reasoning_text", "text": "Distinct full thought."}
+ ]
+ if answer_before_done:
+ message = {
+ "id": "msg_answer",
+ "type": "message",
+ "role": "assistant",
+ "status": "completed",
+ "content": [{"type": "output_text", "text": "Answer.", "annotations": []}],
+ }
+ upstream[3:3] = [
+ {
+ "type": "response.output_item.added",
+ "output_index": 1,
+ "item": {**message, "status": "in_progress", "content": []},
+ },
+ {
+ "type": "response.output_text.delta",
+ "item_id": "msg_answer",
+ "output_index": 1,
+ "content_index": 0,
+ "delta": "Answer.",
+ },
+ {"type": "response.output_item.done", "output_index": 1, "item": message},
+ ]
+ upstream[-1]["response"]["output"].append(message)
+ for sequence, event in enumerate(upstream):
+ event["sequence_number"] = sequence
+ async with _harness("responses", lambda _: (200, upstream)) as (send, bodies, _):
+ frames = [
+ frame
+ async for frame in send("messages", [{"role": "user", "content": "hello"}])
+ ]
+
+ async def saved_frames():
+ for frame in frames:
+ yield frame
+
+ saved = await _saved_reply(saved_frames(), "messages")
+ await _saved_reply(
+ send("messages", [*saved, {"role": "user", "content": "next"}]), "messages"
+ )
+ assert json.dumps(saved).count("Find 17.") == 1
+ assert json.dumps(saved).count("Distinct full thought.") == 1
+ assert json.dumps(bodies[-1]).count("Find 17.") == 1
+ assert json.dumps(bodies[-1]).count("Distinct full thought.") == 1
+ if answer_before_done:
+ events = parse_sse_text("".join(frames))
+ text_index = next(
+ event.data["index"]
+ for event in events
+ if event.event == "content_block_start"
+ and event.data["content_block"]["type"] == "text"
+ )
+ assert [
+ event.data["index"]
+ for event in events
+ if event.event == "content_block_stop"
+ ][-1] == text_index
+
+
[email protected]("summary", [False, True])
+def test_responses_seeded_reasoning_and_fragments_are_completed_once(summary):
+ stream = ResponsesProviderStream(
+ message_id="msg_test", model="native", input_tokens=1
+ )
+ field = "summary" if summary else "content"
+ part = "summary_text" if summary else "reasoning_text"
+ item = {
+ "type": "reasoning",
+ "id": "rs_seeded",
+ field: [{"type": part, "text": "First, "}],
+ }
+ frames = stream.start()
+ frames += stream.feed("response.output_item.added", {"item": item})
+ frames += stream.feed(
+ "response.reasoning_summary_text.delta"
+ if summary
+ else "response.reasoning_text.delta",
+ {"item_id": "rs_seeded", "delta": "think."},
+ )
+ frames += stream.feed(
+ "response.output_item.done",
+ {"item": {**item, field: [{"type": part, "text": "First, think."}]}},
+ )
+ frames += stream.feed("response.completed", {"response": {}})
+ events = parse_sse_text("".join(frames))
+ assert (
+ "".join(
+ event.data["delta"]["thinking"]
+ for event in events
+ if event.data.get("delta", {}).get("type") == "thinking_delta"
+ )
+ == "First, think."
+ )
diff --git a/tests/providers/test_open_router.py b/tests/providers/test_open_router.py
index fcbfde7f..bce260d7 100644
--- a/tests/providers/test_open_router.py
+++ b/tests/providers/test_open_router.py
@@ -18,7 +18,6 @@ from free_claude_code.core.anthropic.stream_contracts import (
thinking_content,
)
from free_claude_code.core.failures import FailureKind
-from free_claude_code.core.history_replay import decode_replay
from free_claude_code.core.model_capabilities import ModelInputModality
from free_claude_code.providers.failure_policy import classify_provider_failure
from free_claude_code.providers.open_router import OpenRouterProvider
@@ -429,15 +428,12 @@ async def test_stream_maps_reasoning_content_and_details(open_router_provider):
parsed = parse_sse_text(event_text)
assert thinking_content(parsed) == "plan "
carriers = [
- event.data["delta"]["signature"]
+ event.data["content_block"]["data"]
for event in parsed
- if event.data.get("delta", {}).get("type") == "signature_delta"
- ]
- assert len(carriers) == 1
- assert decode_replay(carriers[0]).native["reasoning_details"] == [
- {"type": "reasoning.text", "text": "plan "},
- redacted,
+ if event.event == "content_block_start"
+ and event.data["content_block"]["type"] == "redacted_thinking"
]
+ assert carriers == [redacted["data"]]
assert text_content(parsed) == "done"
assert stream.closed
diff --git a/tests/providers/test_reasoning_representation_lifecycle.py b/tests/providers/test_reasoning_representation_lifecycle.py
new file mode 100644
index 00000000..f70e925c
--- /dev/null
+++ b/tests/providers/test_reasoning_representation_lifecycle.py
@@ -0,0 +1,533 @@
+"""Native reasoning item and part ownership survives completion and replay."""
+
+import json
+from copy import deepcopy
+
+import pytest
+
+from free_claude_code.core.anthropic.stream_contracts import parse_sse_text
+from free_claude_code.core.json_types import JsonObject
+from free_claude_code.providers.kilo import KiloProvider
+from free_claude_code.providers.openai_chat.reasoning_details import (
+ StructuredReasoningStream,
+)
+from free_claude_code.providers.openai_chat.stream_output import (
+ AnthropicChatStreamOutput,
+ ChatStreamUsage,
+)
+from tests.providers.support import immediate_admission, make_provider_config
+from tests.providers.test_history_transports import _events_for, _harness, _saved_reply
+from tests.providers.test_native_reasoning_completion import _chunks
+
+
+def _responses_events(
+ texts, *, field="summary", streamed=True, late=False, signature=None
+):
+ source = deepcopy(_events_for("responses"))
+ reasoning = {**source[3]["item"], "summary": [], "content": []}
+ reasoning.pop("encrypted_content", None)
+ part_type = "summary_text" if field == "summary" else "reasoning_text"
+ reasoning[field] = [{"type": part_type, "text": text} for text in texts]
+ if signature is not None:
+ reasoning["encrypted_content"] = signature
+ message = {
+ "id": "msg_answer",
+ "type": "message",
+ "role": "assistant",
+ "status": "completed",
+ "content": [{"type": "output_text", "text": "Answer.", "annotations": []}],
+ }
+ events = [
+ source[0],
+ {
+ "type": "response.output_item.added",
+ "output_index": 0,
+ "item": {
+ **reasoning,
+ "status": "in_progress",
+ "summary": [],
+ "content": [],
+ "encrypted_content": None,
+ },
+ },
+ ]
+ if streamed:
+ for index, text in enumerate(texts):
+ events.append(
+ {
+ "type": "response.reasoning_summary_text.delta"
+ if field == "summary"
+ else "response.reasoning_text.delta",
+ "item_id": reasoning["id"],
+ "output_index": 0,
+ "summary_index" if field == "summary" else "content_index": index,
+ "delta": text,
+ }
+ )
+ done = {"type": "response.output_item.done", "output_index": 0, "item": reasoning}
+ if not late:
+ events.append(done)
+ events += [
+ {
+ "type": "response.output_item.added",
+ "output_index": 1,
+ "item": {**message, "status": "in_progress", "content": []},
+ },
+ {
+ "type": "response.output_text.delta",
+ "item_id": message["id"],
+ "output_index": 1,
+ "content_index": 0,
+ "delta": "Answer.",
+ },
+ {"type": "response.output_item.done", "output_index": 1, "item": message},
+ ]
+ if late:
+ events.append(done)
+ events.append(
+ {
+ "type": "response.completed",
+ "response": {**source[-1]["response"], "output": [reasoning, message]},
+ }
+ )
+ for sequence, event in enumerate(events):
+ event["sequence_number"] = sequence
+ return events
+
+
[email protected]("field", ["summary", "content"])
+async def test_native_reasoning_parts_are_saved_and_replayed_once(field):
+ upstream = _responses_events(["First part.", "Second part."], field=field)
+ async with _harness("responses", lambda _: (200, upstream)) as (send, bodies, _):
+ saved = await _saved_reply(
+ send("messages", [{"role": "user", "content": "hello"}]), "messages"
+ )
+ await _saved_reply(
+ send("messages", [*saved, {"role": "user", "content": "next"}]), "messages"
+ )
+ for text in ("First part.", "Second part."):
+ assert json.dumps(saved).count(text) == 1
+ assert json.dumps(bodies[-1]).count(text) == 1
+
+
[email protected]("field", ["summary", "content"])
+async def test_completed_only_reasoning_keeps_final_answer_open(field):
+ upstream = _responses_events(
+ ["Completed thought."],
+ field=field,
+ streamed=False,
+ late=True,
+ signature="cipher",
+ )
+ async with _harness("responses", lambda _: (200, upstream)) as (send, _, _):
+ frames = [
+ frame
+ async for frame in send("messages", [{"role": "user", "content": "hello"}])
+ ]
+ events = parse_sse_text("".join(frames))
+ starts = {
+ event.data["index"]: event.data["content_block"]["type"]
+ for event in events
+ if event.event == "content_block_start"
+ }
+ stops = [
+ event.data["index"] for event in events if event.event == "content_block_stop"
+ ]
+ assert starts[stops[-1]] == "text"
+ assert "Answer." in "".join(frames)
+ assert "Completed thought." in "".join(frames)
+
+
[email protected]("wire", ["messages", "responses"])
+async def test_chat_secondary_reasoning_prefix_is_not_repeated(wire):
+ upstream = _chunks(
+ [
+ (
+ {
+ "reasoning_details": [
+ {
+ "type": "reasoning.summary",
+ "summary": "Short summary.",
+ "index": 0,
+ }
+ ]
+ },
+ None,
+ ),
+ ({"reasoning_content": "Full "}, None),
+ ({"content": "Answer."}, None),
+ ({"reasoning_content": "thought."}, "stop"),
+ ]
+ )
+ async with _harness("chat", lambda _: (200, upstream)) 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
+ )
+ assert json.dumps(saved).count("Full ") == 1
+ assert "Full thought." in json.dumps(saved)
+ assert json.dumps(bodies[-1]).count("Full ") == 1
+
+
+async def test_independent_native_items_keep_their_own_text_and_signature():
+ upstream = _responses_events(
+ ["Alpha."], field="content", signature="cipher-a", late=True
+ )
+ second = {
+ "type": "reasoning",
+ "id": "rs_second",
+ "status": "completed",
+ "summary": [],
+ "content": [{"type": "reasoning_text", "text": "Beta."}],
+ "encrypted_content": "cipher-b",
+ }
+ upstream[3:3] = [
+ {
+ "type": "response.output_item.added",
+ "output_index": 2,
+ "item": {
+ **second,
+ "status": "in_progress",
+ "content": [],
+ "encrypted_content": None,
+ },
+ },
+ {
+ "type": "response.reasoning_text.delta",
+ "item_id": second["id"],
+ "output_index": 2,
+ "content_index": 0,
+ "delta": "Beta.",
+ },
+ ]
+ upstream.insert(
+ -1, {"type": "response.output_item.done", "output_index": 2, "item": second}
+ )
+ upstream[-1]["response"]["output"].append(second)
+ for sequence, event in enumerate(upstream):
+ event["sequence_number"] = sequence
+ async with _harness("responses", lambda _: (200, upstream)) as (send, _, _):
+ saved = await _saved_reply(
+ send("messages", [{"role": "user", "content": "hello"}]), "messages"
+ )
+ thinking = [
+ (block["thinking"], block.get("signature"))
+ for block in saved[0]["content"]
+ if block["type"] == "thinking"
+ ]
+ assert thinking == [("Alpha.", "cipher-a"), ("Beta.", "cipher-b")]
+
+
+async def test_native_signed_empty_thinking_keeps_its_block_kind():
+ upstream = _responses_events(
+ [""], field="content", streamed=False, signature="empty-signed"
+ )
+ async with _harness("responses", lambda _: (200, upstream)) as (send, _, _):
+ saved = await _saved_reply(
+ send("messages", [{"role": "user", "content": "hello"}]), "messages"
+ )
+ assert saved[0]["content"][0] == {
+ "type": "thinking",
+ "thinking": "",
+ "signature": "empty-signed",
+ }
+
+
[email protected]("wire", ["messages", "responses"])
[email protected]("fragmented", [False, True])
[email protected]("native_alias", [False, True])
+async def test_chat_signed_details_keep_their_own_text_on_replay(
+ wire, fragmented, native_alias
+):
+ deltas: list[tuple[JsonObject, str | None]]
+ if fragmented:
+ deltas = [
+ (
+ {
+ "reasoning_details": [
+ {"type": "reasoning.text", "text": "Al", "index": 0}
+ ]
+ },
+ None,
+ ),
+ (
+ {
+ "reasoning_details": [
+ {"type": "reasoning.text", "text": "pha.", "index": 0},
+ {"type": "reasoning.text", "text": "Beta.", "index": 1},
+ ]
+ },
+ None,
+ ),
+ ({"content": "Answer."}, None),
+ (
+ {
+ "reasoning_details": [
+ {"type": "reasoning.text", "signature": "sig-", "index": 0}
+ ]
+ },
+ None,
+ ),
+ (
+ {
+ "reasoning_details": [
+ {"type": "reasoning.text", "signature": "alpha", "index": 0},
+ {"type": "reasoning.text", "signature": "sig-beta", "index": 1},
+ ]
+ },
+ "stop",
+ ),
+ ]
+ aliases = ["Al", "pha.Beta."]
+ else:
+ deltas = [
+ (
+ {
+ "reasoning_details": [
+ {
+ "type": "reasoning.text",
+ "text": "Alpha.",
+ "signature": "sig-alpha",
+ "index": 0,
+ },
+ {
+ "type": "reasoning.text",
+ "text": "Beta.",
+ "signature": "sig-beta",
+ "index": 1,
+ },
+ ]
+ },
+ None,
+ ),
+ ({"content": "Answer."}, "stop"),
+ ]
+ aliases = ["Alpha.Beta."]
+ if native_alias:
+ for (delta, _), text in zip(deltas, aliases, strict=False):
+ delta["reasoning_content"] = text
+ async with _harness("chat", lambda _: (200, _chunks(deltas))) 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
+ )
+ if wire == "messages":
+ pairs = [
+ (block["thinking"], block.get("signature"))
+ for block in saved[0]["content"]
+ if block["type"] == "thinking"
+ ]
+ else:
+ pairs = [
+ (
+ "".join(part["text"] for part in item.get("content", [])),
+ item.get("encrypted_content"),
+ )
+ for item in saved
+ if item["type"] == "reasoning"
+ ]
+ assert pairs == [("Alpha.", "sig-alpha"), ("Beta.", "sig-beta")]
+ details = bodies[-1]["messages"][0]["reasoning_details"]
+ assert [(detail["text"], detail.get("signature")) for detail in details] == pairs
+ assert json.dumps(bodies[-1]).count("Alpha.") == 1
+ assert json.dumps(bodies[-1]).count("Beta.") == 1
+
+
[email protected]("wire", ["messages", "responses"])
+async def test_chat_explicit_signed_empty_text_retains_its_kind(wire):
+ upstream = _chunks(
+ [
+ (
+ {
+ "reasoning_details": [
+ {
+ "type": "reasoning.text",
+ "text": "",
+ "signature": "empty-signature",
+ "index": 0,
+ }
+ ]
+ },
+ None,
+ ),
+ ({"content": "Answer."}, "stop"),
+ ]
+ )
+ async with _harness("chat", lambda _: (200, upstream)) 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
+ )
+ if wire == "messages":
+ assert {
+ "type": "thinking",
+ "thinking": "",
+ "signature": "empty-signature",
+ } in saved[0]["content"]
+ else:
+ reasoning = next(item for item in saved if item["type"] == "reasoning")
+ assert reasoning["content"] == [{"type": "reasoning_text", "text": ""}]
+ assert reasoning["encrypted_content"] == "empty-signature"
+ assert bodies[-1]["messages"][0]["reasoning_details"] == [
+ {"type": "reasoning.text", "text": "", "signature": "empty-signature"}
+ ]
+
+
[email protected]("wire", ["messages", "responses"])
[email protected]("after_answer", [False, True])
[email protected]("fragmented", [False, True])
+async def test_native_first_alias_preserves_later_signed_details(
+ wire, after_answer, fragmented
+):
+ details = [
+ {
+ "type": "reasoning.text",
+ "text": "Alpha.",
+ "signature": "sig-alpha",
+ "index": 0,
+ },
+ {
+ "type": "reasoning.text",
+ "text": "Beta.",
+ "signature": "sig-beta",
+ "index": 1,
+ },
+ ]
+ typed = (
+ [
+ ([{"type": "reasoning.text", "text": "Al", "index": 0}], None),
+ (
+ [{**details[0], "text": "pha."}, details[1]],
+ "stop" if after_answer else None,
+ ),
+ ]
+ if fragmented
+ else [(details, "stop" if after_answer else None)]
+ )
+ deltas = [({"reasoning": "Alpha.Beta."}, None)]
+ if after_answer:
+ deltas.append(({"content": "Answer."}, None))
+ deltas.extend(({"reasoning_details": parts}, finish) for parts, finish in typed)
+ if not after_answer:
+ deltas.append(({"content": "Answer."}, "stop"))
+ async with _harness(
+ "chat",
+ lambda _: (200, _chunks(deltas)),
+ chat_provider_factory=lambda: KiloProvider(
+ make_provider_config(api_key="a", base_url="https://provider.invalid/v1"),
+ admission=immediate_admission(),
+ ),
+ ) 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
+ )
+ replayed = bodies[-1]["messages"][0]["reasoning_details"]
+ assert [(part.get("text"), part.get("signature")) for part in replayed] == [
+ ("Alpha.", "sig-alpha"),
+ ("Beta.", "sig-beta"),
+ ]
+ assert json.dumps(saved).count("Alpha.") == 1
+ assert json.dumps(saved).count("Beta.") == 1
+ assert "Alpha.Beta." not in json.dumps(saved)
+ assert json.dumps(bodies[-1]).count("Alpha.") == 1
+ assert json.dumps(bodies[-1]).count("Beta.") == 1
+
+
[email protected]("wire", ["messages", "responses"])
+async def test_identical_signed_details_keep_independent_ownership(wire):
+ upstream = _chunks(
+ [
+ ({"reasoning_content": "Same.Same."}, None),
+ ({"content": "Answer."}, None),
+ (
+ {
+ "reasoning_details": [
+ {
+ "type": "reasoning.text",
+ "text": "Same.",
+ "signature": "a",
+ "index": 0,
+ },
+ {
+ "type": "reasoning.text",
+ "text": "Same.",
+ "signature": "b",
+ "index": 1,
+ },
+ ]
+ },
+ "stop",
+ ),
+ ]
+ )
+ async with _harness("chat", lambda _: (200, upstream)) 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
+ )
+ assert bodies[-1]["messages"][0]["reasoning_details"] == [
+ {"type": "reasoning.text", "text": "Same.", "signature": "a"},
+ {"type": "reasoning.text", "text": "Same.", "signature": "b"},
+ ]
+
+
[email protected]("typed", [False, True])
+def test_typed_reasoning_streams_while_native_fallback_waits(typed):
+ output = AnthropicChatStreamOutput(
+ message_id="msg_test", model="test", input_tokens=1
+ )
+ reasoning = StructuredReasoningStream()
+ output.reasoning_replay = reasoning
+ early = list(
+ reasoning.events(
+ {
+ "reasoning_details": [
+ {"type": "reasoning.text", "text": "Plan.", "index": 0}
+ ]
+ }
+ if typed
+ else {},
+ output,
+ native_reasoning="Plan.",
+ )
+ )
+ assert ("Plan." in "".join(early)) == typed
+ answer = [*output.ensure_text_block(), output.emit_text_delta("Answer.")]
+ assert "Answer." in "".join(answer)
+ complete = output.finish_success(
+ stop_reason="end_turn", usage=ChatStreamUsage(input_tokens=1, output_tokens=3)
+ )
+ events = parse_sse_text("".join(early + answer + complete))
+ assert (
+ sum(event.data.get("delta", {}).get("thinking") == "Plan." for event in events)
+ == 1
+ )
+ starts = {
+ event.data["index"]: event.data["content_block"]["type"]
+ for event in events
+ if event.event == "content_block_start"
+ }
+ stops = [
+ event.data["index"] for event in events if event.event == "content_block_stop"
+ ]
+ assert starts[stops[-1]] == "text"
diff --git a/tests/providers/test_streaming_errors.py b/tests/providers/test_streaming_errors.py
index e5c3280a..d95d2a29 100644
--- a/tests/providers/test_streaming_errors.py
+++ b/tests/providers/test_streaming_errors.py
@@ -619,7 +619,7 @@ class TestStreamingExceptionHandling:
assert "The answer" in event_text
@pytest.mark.asyncio
- async def test_stream_with_empty_reasoning_content_starts_thinking_block_only(self):
+ async def test_stream_with_empty_reasoning_content_creates_no_thinking_block(self):
"""Empty reasoning_content is stateful but must not emit visible thinking text."""
provider = _make_provider()
request = _make_request()
@@ -651,7 +651,7 @@ class TestStreamingExceptionHandling:
if event.event == "content_block_delta"
and event.data["delta"]["type"] == "thinking_delta"
]
- assert len(thinking_starts) == 1
+ assert thinking_starts == []
assert thinking_deltas == []
assert parsed[-1].event == "message_stop"
@@ -667,8 +667,8 @@ class TestStreamingExceptionHandling:
),
pytest.param(
[("", "The quick"), ("", " brown fox"), ("", "")],
- [("reasoning", ""), ("text", "The quick brown fox")],
- id="first-empty-reasoning-is-preserved",
+ [("text", "The quick brown fox")],
+ id="first-empty-reasoning-is-ignored",
),
pytest.param(
[(None, "The quick"), (None, " brown fox")],
@@ -678,7 +678,6 @@ class TestStreamingExceptionHandling:
pytest.param(
[("", None), ("", "first"), ("more", None), ("", "second")],
[
- ("reasoning", ""),
("text", "first"),
("reasoning", "more"),
("text", "second"),
@@ -705,7 +704,7 @@ class TestStreamingExceptionHandling:
new_callable=AsyncMock,
side_effect=lambda **kwargs: AsyncStreamMock(chunks),
):
- # Each request must preserve its own first explicit reasoning value.
+ # Empty placeholders do not create output items on either request.
for _ in range(2):
if wire == "messages":
stream = provider.stream_messages(_make_request())
diff --git a/tests/providers/test_structured_reasoning_lifecycle.py b/tests/providers/test_structured_reasoning_lifecycle.py
index 2fb281df..660746de 100644
--- a/tests/providers/test_structured_reasoning_lifecycle.py
+++ b/tests/providers/test_structured_reasoning_lifecycle.py
@@ -10,9 +10,13 @@ from free_claude_code.core.anthropic.stream_contracts import parse_sse_text
from free_claude_code.core.history_replay import (
AssociatedReplayRecord,
ReplayOrigin,
- decode_replay,
+ encode_replay,
+)
+from free_claude_code.core.json_types import JsonObject
+from free_claude_code.providers.history_replay import (
+ normalize_messages_history,
+ replay_origin,
)
-from free_claude_code.providers.history_replay import normalize_messages_history
from free_claude_code.providers.openai_chat.reasoning_details import (
StructuredReasoningStream,
)
@@ -67,7 +71,8 @@ async def test_encrypted_only_reasoning_stays_with_its_answer():
saved = await _saved_reply(
send("responses", [{"role": "user", "content": "hello"}]), "responses"
)
- assert [item["type"] for item in saved] == ["reasoning", "message"]
+ assert [item["type"] for item in saved] == ["message", "reasoning"]
+ assert saved[1]["encrypted_content"] == "opaque"
await _saved_reply(
send("responses", [*saved, {"role": "user", "content": "next"}]),
"responses",
@@ -75,11 +80,13 @@ async def test_encrypted_only_reasoning_stays_with_its_answer():
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
+ assert assistants[0]["reasoning_details"] == [
+ {"type": "reasoning.encrypted", "data": "opaque"}
+ ]
@pytest.mark.asyncio
-async def test_late_metadata_does_not_overlap_messages_content_blocks():
+async def test_late_metadata_uses_open_indices_and_closes_final_text_last():
events = _chat_reasoning_events(
[
{
@@ -101,17 +108,19 @@ async def test_late_metadata_does_not_overlap_messages_content_blocks():
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
+ parsed = parse_sse_text("".join(stream))
+ _assert_indexed(parsed)
+ text_index = max(
+ event.data["index"]
+ for event in parsed
+ if event.event == "content_block_start"
+ and event.data["content_block"]["type"] == "text"
+ )
+ assert [
+ event.data["index"]
+ for event in parsed
+ if event.event == "content_block_stop"
+ ][-1] == text_index
saved = await _saved_reply(
send("messages", [{"role": "user", "content": "hello"}]), "messages"
)
@@ -119,36 +128,49 @@ async def test_late_metadata_does_not_overlap_messages_content_blocks():
send("messages", [*saved, {"role": "user", "content": "next"}]), "messages"
)
assert bodies[-1]["messages"][0]["reasoning_details"] == [
- {"type": "reasoning.encrypted", "data": "firstsecond", "index": 0}
+ {"type": "reasoning.text", "text": "Plan.", "signature": "firstsecond"}
]
@pytest.mark.asyncio
@pytest.mark.parametrize("destination", ["chat", "messages", "responses"])
@pytest.mark.parametrize("readable", [False, True])
-async def test_associated_messages_replay_into_every_transport(destination, readable):
- first = {
+async def test_legacy_associated_messages_replay_into_every_transport(
+ destination, readable
+):
+ first: JsonObject = {
"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"
+ async with _harness("chat") as (_, _, provider):
+ origin = replay_origin(
+ "OPENROUTER", "chat", "actual-returned", client=provider._client
)
+ final: JsonObject = {
+ **first,
+ "reasoning_details": [
+ {"type": "reasoning.encrypted", "data": "firstsecond", "index": 0}
+ ],
+ }
+ anchor = encode_replay(AssociatedReplayRecord(origin, first, "group", "anchor"))
+ completed = encode_replay(AssociatedReplayRecord(origin, final, "group", "final"))
+ saved = [
+ {
+ "role": "assistant",
+ "content": [
+ {
+ "type": "thinking",
+ "thinking": "Plan." if readable else "",
+ "signature": anchor,
+ },
+ {"type": "text", "text": "Answer."},
+ {"type": "redacted_thinking", "data": completed},
+ ],
+ }
+ ]
history = [*saved, {"role": "user", "content": "next"}]
before = json.dumps(history)
async with _harness(destination) as (send, bodies, _):
@@ -169,7 +191,6 @@ 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
@@ -189,17 +210,19 @@ def _readable(reasoning, output, text="Plan.", data="first"):
)
-def _assert_serial(events):
- active = None
+def _assert_indexed(events):
+ active = set()
+ seen = set()
for event in events:
if event.event == "content_block_start":
- assert active is None
- active = event.data["index"]
+ assert event.data["index"] not in seen
+ seen.add(event.data["index"])
+ active.add(event.data["index"])
elif event.event in {"content_block_delta", "content_block_stop"}:
- assert event.data["index"] == active
+ assert event.data["index"] in active
if event.event == "content_block_stop":
- active = None
- assert active is None
+ active.remove(event.data["index"])
+ assert not active
@pytest.mark.parametrize(
@@ -251,44 +274,26 @@ def test_immediate_text_and_late_replay_survive_every_exit(terminal):
== []
)
events = parse_sse_text("".join(frames))
- _assert_serial(events)
- finals = [
- decode_replay(e.data["content_block"]["data"])
+ _assert_indexed(events)
+ signatures = [
+ e.data["delta"]["signature"]
for e in events
- if e.event == "content_block_start"
- and e.data["content_block"]["type"] == "redacted_thinking"
+ if e.data.get("delta", {}).get("type") == "signature_delta"
]
- assert len(finals) == 1
- assert isinstance(finals[0], AssociatedReplayRecord)
- assert finals[0].part == "final"
- assert finals[0].native == {
+ assert signatures == ([] if terminal == "failure" else ["firstsecond"])
+ assert "fcc:history" not in "".join(frames)
+
+
+def test_request_copy_preserves_routing_and_does_not_join_across_messages():
+ origin = ReplayOrigin("test", "chat", "endpoint", "account", "model")
+ native: JsonObject = {
"reasoning_content": "Plan.",
"reasoning_details": [
- {"type": "reasoning.encrypted", "data": "firstsecond", "index": 0}
+ {"type": "reasoning.encrypted", "data": "first", "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"
- )
+ anchor = encode_replay(AssociatedReplayRecord(origin, native, "group", "anchor"))
+ final = encode_replay(AssociatedReplayRecord(origin, native, "group", "final"))
request = MessagesRequest(
model="route",
original_model="original",
@@ -353,17 +358,12 @@ async def test_structured_replay_respects_retry_and_continuation_boundary(
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,
- }
- ]
+ if committed:
+ assert "Original thought." in json.dumps(saved)
+ assert 'original"' not in json.dumps(saved)
+ else:
+ assert "Recovery thought." in json.dumps(saved)
+ assert _carrier(saved, wire) == "replacement"
assert "continued" in json.dumps(saved)
if not committed:
assert "Original thought." not in json.dumps(saved)
@@ -388,7 +388,7 @@ async def test_first_metadata_after_text_does_not_invent_earlier_reasoning(wire)
part
async for part in send(wire, [{"role": "user", "content": "hello"}])
]
- _assert_serial(parse_sse_text("".join(frames)))
+ _assert_indexed(parse_sse_text("".join(frames)))
saved = await _saved_reply(
send(wire, [{"role": "user", "content": "hello"}]), wire
)
diff --git a/tests/providers/test_zenmux.py b/tests/providers/test_zenmux.py
index fcf7b2ea..a4ca71dc 100644
--- a/tests/providers/test_zenmux.py
+++ b/tests/providers/test_zenmux.py
@@ -18,7 +18,6 @@ from free_claude_code.core.anthropic.stream_contracts import (
text_content,
thinking_content,
)
-from free_claude_code.core.history_replay import decode_replay
from free_claude_code.core.model_capabilities import ModelInputModality
from free_claude_code.core.reasoning import (
ReasoningCapability,
@@ -343,12 +342,11 @@ async def test_stream_preserves_signed_details_without_duplicating_reasoning(
assert thinking_content(events) == "plan "
assert text_content(events) == "done"
records = [
- decode_replay(event.data["delta"]["signature"]).native
+ event.data["delta"]["signature"]
for event in events
if event.data.get("delta", {}).get("type") == "signature_delta"
]
- assert len(records) == 1
- assert records[0]["reasoning_details"] == [detail]
+ assert records == [detail["signature"]]
assert stream.closed