The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(agent): persist promoted clean-stop reasoning; pin the length negative
teknium128931c5c04b2
diff --git a/agent/turn_final_response.py b/agent/turn_final_response.pyindex 93bfcab32a8..71dcd185ddf 100644--- a/agent/turn_final_response.py+++ b/agent/turn_final_response.py@@ -72,21 +72,27 @@ def finish_text_response(result=result,)- final_response = assistant_message.content or ""- ordinary_content_empty = assistant_message.content is None or (- isinstance(assistant_message.content, str)- and not assistant_message.content.strip()- )+ # Reasoning-only clean stop: some reasoning parsers (vLLM nemotron_v3 past ~500K+ # prompt tokens) file the whole answer as reasoning when the model omits the closing+ # delimiter. ``finish_reason == "stop"`` means the provider considers generation+ # complete, so the empty-response ladder would only re-bill the same input to arrive+ # at a truncated preview of this text; promote the reasoning to the visible answer+ # BEFORE the ladder. ``length`` (cut off mid-thought) stays on the continuation path,+ # and the promoted text is persisted as ordinary content so the next turn replays it.+ _content = assistant_message.contentif (finish_reason == "stop"and not assistant_message.tool_calls- and ordinary_content_empty+ and (_content is None or (isinstance(_content, str) and not _content.strip()))):- from agent.auxiliary_client import extract_content_or_reasoning-- structured_response = extract_content_or_reasoning(assistant_message)- if structured_response:- final_response = structured_response+ _promoted = agent._extract_reasoning(assistant_message)+ if _promoted:+ logger.info(+ "Reasoning-only clean stop (%d chars) — using reasoning as the final response",+ len(_promoted),+ )+ assistant_message.content = _promoted+ final_response = assistant_message.content or ""# Unmute: _mute_post_response from a housekeeping tool turn must not silence# empty-response warnings on the final response path.agent._mute_post_response = Falsediff --git a/tests/agent/test_empty_terminal_reasoning_surface.py b/tests/agent/test_empty_terminal_reasoning_surface.pyindex 7fbf21859f0..21c13ad3044 100644--- a/tests/agent/test_empty_terminal_reasoning_surface.py+++ b/tests/agent/test_empty_terminal_reasoning_surface.py@@ -1,14 +1,15 @@-"""Tests for the empty-terminal reasoning surface.+"""Tests for reasoning-only final responses.-When a clean-stop response has no ordinary content but does have structured-reasoning, that reasoning is the final answer without entering the recovery-ladder. Idea credit: PR #48795 (@ligl0325).+A clean-stop response (``finish_reason == "stop"``) with no ordinary content but+structured reasoning IS the answer: the reasoning is promoted to the visible reply and+persisted as assistant content without entering the empty-response recovery ladder+(every rung re-bills the full prompt). Idea credit: PR #48795 (@ligl0325).Invariants pinned here:-- The persisted assistant message keeps the "(empty)" sentinel and the- ``_empty_terminal_sentinel`` marker (replay semantics unchanged).-- Inline think-only content still goes through the recovery ladder.-- A truly empty response (no reasoning either) still uses the recovery ladder.+- Clean-stop reasoning-only → returned and persisted after ONE API call.+- ``finish_reason == "length"`` reasoning is unfinished: never promoted, the+ continuation path still owns it.+- A truly empty response (no reasoning either) still reaches the ladder terminal."""from __future__ import annotations@@ -44,17 +45,17 @@ def _build_agent(tmp_path, monkeypatch):return agent-def _reasoning_only_response():+def _reasoning_only_response(finish_reason="stop"):return SimpleNamespace(choices=[SimpleNamespace(message=SimpleNamespace(- content="",+ content=None,reasoning="The answer is 42 because of the calculation above.",reasoning_content=None,reasoning_details=None,tool_calls=None,),- finish_reason="stop",+ finish_reason=finish_reason,)],usage=None,model="test-model",@@ -90,6 +91,9 @@ def test_clean_stop_reasoning_only_returns_on_first_call(tmp_path, monkeypatch):assert result["final_response"] == "The answer is 42 because of the calculation above."assert result["api_calls"] == 1+ # The promoted text is durable content, so the next turn replays a real answer.+ assert result["messages"][-1]["role"] == "assistant"+ assert result["messages"][-1]["content"] == "The answer is 42 because of the calculation above."def test_exhausted_truly_empty_keeps_existing_behavior(tmp_path, monkeypatch):@@ -111,24 +115,12 @@ def test_exhausted_truly_empty_keeps_existing_behavior(tmp_path, monkeypatch):assert "only internal reasoning" not in final-def test_inline_thinking_still_uses_recovery_ladder(tmp_path, monkeypatch):- """Inline think blocks are not ordinary empty content and remain recoverable."""+def test_length_cut_reasoning_is_not_promoted(tmp_path, monkeypatch):+ """``finish_reason == "length"`` means the model was cut off mid-thought: the reasoning+ is not an answer, so the continuation path runs and the model's real text wins."""agent = _build_agent(tmp_path, monkeypatch)responses = [- SimpleNamespace(- choices=[SimpleNamespace(- message=SimpleNamespace(- content="<think>working it out</think>",- reasoning=None,- reasoning_content=None,- reasoning_details=None,- tool_calls=None,- ),- finish_reason="stop",- )],- usage=None,- model="test-model",- ),+ _reasoning_only_response(finish_reason="length"),SimpleNamespace(choices=[SimpleNamespace(message=SimpleNamespace(diff --git a/tests/agent/test_thinking_prefill_trailing_turn.py b/tests/agent/test_thinking_prefill_trailing_turn.pyindex f782134e3f6..4e41b055521 100644--- a/tests/agent/test_thinking_prefill_trailing_turn.py+++ b/tests/agent/test_thinking_prefill_trailing_turn.py@@ -1,6 +1,7 @@"""Regression test for the thinking-only prefill reaching the wire.-A thinking-only response (reasoning tokens, no visible text) makes the loop+A thinking-only response (reasoning tokens, no visible text) that is NOT a clean+``stop`` (a clean-stop reasoning-only reply is promoted to the answer up front) makes the loopappend an empty assistant turn and re-send so the model continues its ownreasoning. On providers that don't echo reasoning back, the API copy has itsreasoning fields stripped before ``_drop_thinking_only_and_merge_users`` runs,@@ -52,11 +53,11 @@ def loop_agent():def _thinking_only_response():- """Reasoning tokens, no visible text — what triggers the prefill retry."""+ """Reasoning tokens, no visible text, no clean stop — what triggers the prefill retry."""from tests.agent.test_run_agent import _mock_responsereturn _mock_response(content="",- finish_reason="stop",+ finish_reason="tool_calls",reasoning="Let me work through the request step by step.",)