From 5ed7f005a9da561f39b49c67482bfa3c5310c422 Mon Sep 17 00:00:00 2001 From: "Bingxi Zhao (Frank)" <13051345080@163.com> Date: Tue, 27 Oct 2026 21:46:35 +0800 Subject: [PATCH] fix: discard completed Anthropic thinking at durable history replay --- .../llm/provider_core/anthropic_provider.py | 56 +------- deeptutor/services/session/model_history.py | 32 ++++- .../llm/test_anthropic_provider_fixes.py | 124 ------------------ tests/services/session/test_model_history.py | 66 +++++++++- 4 files changed, 96 insertions(+), 182 deletions(-) diff --git a/deeptutor/services/llm/provider_core/anthropic_provider.py b/deeptutor/services/llm/provider_core/anthropic_provider.py index 4bb0bc638..47bd029e5 100644 --- a/deeptutor/services/llm/provider_core/anthropic_provider.py +++ b/deeptutor/services/llm/provider_core/anthropic_provider.py @@ -142,61 +142,7 @@ class AnthropicProvider(LLMProvider): ) continue - return system, self._drop_completed_turn_thinking(self._merge_consecutive(raw)) - - @staticmethod - def _opens_new_turn(msg: dict[str, Any]) -> bool: - """Whether a converted user message is a new prompt. - - A user message carrying ``tool_result`` blocks answers the preceding - assistant's tool calls, so it continues that turn. Any other user - message starts one. - """ - if msg.get("role") != "user": - return False - content = msg.get("content") - if not isinstance(content, list): - return True - return not any( - isinstance(block, dict) and block.get("type") == "tool_result" for block in content - ) - - @classmethod - def _drop_completed_turn_thinking(cls, msgs: list[dict[str, Any]]) -> list[dict[str, Any]]: - """Keep signed thinking only for the turn still in progress. - - Anthropic needs the thinking blocks of the assistant rounds after the - latest user prompt (a tool loop must replay them verbatim). Blocks - from earlier, completed turns are optional, and the API binds each - signature to the system prompt and messages it was produced under. - DeepTutor rebuilds that prefix every turn (runtime context, learning - journal, KB seed), so replaying an earlier turn's block is rejected - with ``Invalid `signature` in `thinking` block`` and every follow-up - message in a conversation fails. - """ - last_prompt = -1 - for index, msg in enumerate(msgs): - if cls._opens_new_turn(msg): - last_prompt = index - if last_prompt <= 0: - return msgs - - result: list[dict[str, Any]] = [] - for index, msg in enumerate(msgs): - content = msg.get("content") - if index < last_prompt and msg.get("role") == "assistant" and isinstance(content, list): - kept = [ - block - for block in content - if not ( - isinstance(block, dict) - and block.get("type") in {"thinking", "redacted_thinking"} - ) - ] - if len(kept) != len(content): - msg = {**msg, "content": kept or [{"type": "text", "text": "(empty)"}]} - result.append(msg) - return result + return system, self._merge_consecutive(raw) @staticmethod def _tool_result_block(msg: dict[str, Any]) -> dict[str, Any]: diff --git a/deeptutor/services/session/model_history.py b/deeptutor/services/session/model_history.py index b422d14d4..f530f85d2 100644 --- a/deeptutor/services/session/model_history.py +++ b/deeptutor/services/session/model_history.py @@ -130,6 +130,34 @@ def history_groups(rows: list[dict[str, Any]]) -> list[list[dict[str, Any]]]: return groups +def _without_completed_thinking(messages: list[dict[str, Any]]) -> list[dict[str, Any]]: + """Drop prefix-bound Anthropic thoughts only at the durable turn boundary. + + Current-turn tool rounds never pass through history replay. Keep their + signed state intact, and leave the stored transcript and other providers' + replay state untouched. + """ + result = deepcopy(messages) + for message in result: + if message.get("role") != "assistant": + continue + message.pop("thinking_blocks", None) + state = message.get("_provider_response_state") + if isinstance(state, dict): + state.pop("thinking_blocks", None) + if not state: + message.pop("_provider_response_state", None) + content = message.get("content") + if isinstance(content, list): + message["content"] = [ + block + for block in content + if not isinstance(block, dict) + or block.get("type") not in {"thinking", "redacted_thinking"} + ] + return result + + def replay_group( rows: list[dict[str, Any]], route: dict[str, str] | None = None ) -> list[dict[str, Any]]: @@ -165,7 +193,7 @@ def replay_group( message.pop(key, None) for call in message.get("tool_calls") or []: call.pop("extra_content", None) - return messages + return _without_completed_thinking(messages) messages = [] for row in rows: expanded = expand_message_context(row) @@ -177,7 +205,7 @@ def replay_group( if state: expanded[-1]["_provider_response_state"] = state messages.extend(expanded) - return messages + return _without_completed_thinking(messages) #: Snapshot name the rolling summary rides under when it is replayed. diff --git a/tests/services/llm/test_anthropic_provider_fixes.py b/tests/services/llm/test_anthropic_provider_fixes.py index 781236ca0..132728a02 100644 --- a/tests/services/llm/test_anthropic_provider_fixes.py +++ b/tests/services/llm/test_anthropic_provider_fixes.py @@ -293,127 +293,3 @@ def test_assistant_blocks_omit_unsigned_history_blocks() -> None: ) assert all(block.get("type") != "thinking" for block in blocks) - - -# --------------------------------------------------------------------------- -# Thinking from completed turns is not replayed. The API binds each signed -# block to the prefix it was produced under, and DeepTutor rebuilds that prefix -# every turn, so replaying it failed every follow-up message with -# "Invalid `signature` in `thinking` block". -# --------------------------------------------------------------------------- - - -def _thinking_signatures(message: dict[str, Any]) -> list[str]: - return [ - block["signature"] - for block in message["content"] - if isinstance(block, dict) and block.get("type") == "thinking" - ] - - -def _tool_call(call_id: str) -> dict[str, Any]: - return {"id": call_id, "type": "function", "function": {"name": "rag", "arguments": "{}"}} - - -def test_convert_messages_drops_thinking_from_a_completed_turn() -> None: - _, converted = _provider("claude-opus-5-5")._convert_messages( - [ - {"role": "system", "content": "sys"}, - {"role": "user", "content": "first question"}, - { - "role": "assistant", - "content": "first answer", - "thinking_blocks": [_signed_block("earlier-turn")], - }, - {"role": "user", "content": "follow-up"}, - ] - ) - - assert [m["role"] for m in converted] == ["user", "assistant", "user"] - assert _thinking_signatures(converted[1]) == [] - assert converted[1]["content"] == [{"type": "text", "text": "first answer"}] - - -def test_convert_messages_drops_thinking_rebuilt_from_history_state() -> None: - _, converted = _provider("claude-opus-5-5")._convert_messages( - [ - {"role": "user", "content": "first question"}, - { - "role": "assistant", - "content": "first answer", - "_provider_response_state": {"thinking_blocks": [_signed_block("persisted")]}, - }, - {"role": "user", "content": "follow-up"}, - ] - ) - - assert _thinking_signatures(converted[1]) == [] - - -def test_convert_messages_keeps_thinking_for_the_tool_loop_in_progress() -> None: - _, converted = _provider("claude-opus-5-5")._convert_messages( - [ - {"role": "user", "content": "first question"}, - { - "role": "assistant", - "content": "", - "tool_calls": [_tool_call("call_old")], - "thinking_blocks": [_signed_block("old-round")], - }, - {"role": "tool", "tool_call_id": "call_old", "content": "old result"}, - { - "role": "assistant", - "content": "first answer", - "thinking_blocks": [_signed_block("old-final")], - }, - {"role": "user", "content": "follow-up"}, - { - "role": "assistant", - "content": "", - "tool_calls": [_tool_call("call_live_1")], - "thinking_blocks": [_signed_block("live-1")], - }, - {"role": "tool", "tool_call_id": "call_live_1", "content": "live result 1"}, - { - "role": "assistant", - "content": "", - "tool_calls": [_tool_call("call_live_2")], - "thinking_blocks": [_signed_block("live-2")], - }, - {"role": "tool", "tool_call_id": "call_live_2", "content": "live result 2"}, - ] - ) - - assistant = [m for m in converted if m["role"] == "assistant"] - assert [_thinking_signatures(m) for m in assistant] == [[], [], ["live-1"], ["live-2"]] - # The tool loop of the completed turn keeps its tool_use blocks. - assert any(block.get("type") == "tool_use" for block in assistant[0]["content"]) - - -def test_convert_messages_never_leaves_a_completed_turn_empty() -> None: - _, converted = _provider("claude-opus-5-5")._convert_messages( - [ - {"role": "user", "content": "first question"}, - {"role": "assistant", "content": "", "thinking_blocks": [_signed_block()]}, - {"role": "user", "content": "follow-up"}, - ] - ) - - assert converted[1]["content"] == [{"type": "text", "text": "(empty)"}] - - -def test_convert_messages_leaves_a_single_turn_untouched() -> None: - _, converted = _provider("claude-opus-5-5")._convert_messages( - [ - {"role": "user", "content": "question"}, - { - "role": "assistant", - "content": "", - "tool_calls": [_tool_call("call_1")], - "thinking_blocks": [_signed_block("only-turn")], - }, - {"role": "tool", "tool_call_id": "call_1", "content": "result"}, - ] - ) - - assert _thinking_signatures(converted[1]) == ["only-turn"] diff --git a/tests/services/session/test_model_history.py b/tests/services/session/test_model_history.py index f1c4fddc6..b215fbd14 100644 --- a/tests/services/session/test_model_history.py +++ b/tests/services/session/test_model_history.py @@ -296,7 +296,7 @@ async def test_budget_uses_the_replayed_route_and_preserves_stored_signed_state( session_id="s", llm_config=SimpleNamespace(binding="openai", model="gpt-test", context_window=16000), ) - builder._summarize.assert_awaited_once() + builder._summarize.assert_not_awaited() assert records == before @@ -371,3 +371,67 @@ async def test_recompaction_retains_prior_summary_when_history_replay_has_no_sum assert request["messages"][-1]["content"] == "[Conversation summary]\nEarlier stable preference" assert all("_context_snapshot" not in message for message in request["messages"]) assert "already condensed" not in str(request["messages"]) + + +@pytest.mark.parametrize("representation", ["direct", "private", "content"]) +def test_completed_turn_replay_drops_thinking_before_a_new_question(representation): + from deeptutor.services.llm.provider_core.anthropic_provider import AnthropicProvider + + record = turn_record() + # A completed stored turn may end with a tool result. The following user + # question must not be confused with another tool-loop continuation. + record["messages"].pop() + thought = {"type": "thinking", "thinking": "old thought", "signature": "old-signature"} + assistant = record["messages"][1] + encrypted = {"type": "reasoning", "id": "r", "encrypted_content": "opaque"} + assistant["_provider_response_state"] = {"responses_output_items": [encrypted]} + if representation == "direct": + assistant["thinking_blocks"] = [thought] + elif representation == "private": + assistant["_provider_response_state"]["thinking_blocks"] = [thought] + else: + assistant["content"] = [thought, {"type": "redacted_thinking", "data": "opaque"}] + source = rows(record) + before = deepcopy(source) + history = replay_history(source) + assert history[1]["_provider_response_state"] == {"responses_output_items": [encrypted]} + provider = object.__new__(AnthropicProvider) + _, converted = provider._convert_messages([*history, {"role": "user", "content": "next"}]) + assert all( + block.get("type") not in {"thinking", "redacted_thinking"} + for message in converted + for block in message["content"] + if isinstance(block, dict) + ) + assert history[2]["content"] == "tool evidence" + assert source == before + + # A live tool round bypasses durable replay, including internal user nudges. + _, current = provider._convert_messages( + [ + {"role": "user", "content": "current question"}, + {"role": "assistant", "content": "working", "thinking_blocks": [thought]}, + {"role": "user", "content": "finish the current turn"}, + ] + ) + assert current[1]["content"][0] == thought + + +def test_legacy_display_history_drops_only_completed_thinking(): + thought = {"type": "thinking", "thinking": "old", "signature": "signature"} + source = [ + { + "role": "assistant", + "content": "answer", + "metadata": { + "provider_response_state": { + "thinking_blocks": [thought], + "reasoning_content": "portable", + } + }, + } + ] + before = deepcopy(source) + replayed = replay_history(source) + assert replayed[0]["_provider_response_state"] == {"reasoning_content": "portable"} + assert source == before