Skip to content

The agent that grows with you

fix(agent): return reasoning-only clean stops

KoNit-K88a9d9eac5ae
diff --git a/agent/turn_final_response.py b/agent/turn_final_response.py
index 4907d6b09b9..93bfcab32a8 100644
--- a/agent/turn_final_response.py
+++ b/agent/turn_final_response.py
@@ -73,6 +73,20 @@ def finish_text_response(
)
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()
+ )
+ if (
+ finish_reason == "stop"
+ and not assistant_message.tool_calls
+ and ordinary_content_empty
+ ):
+ from agent.auxiliary_client import extract_content_or_reasoning
+
+ structured_response = extract_content_or_reasoning(assistant_message)
+ if structured_response:
+ final_response = structured_response
# Unmute: _mute_post_response from a housekeeping tool turn must not silence
# empty-response warnings on the final response path.
agent._mute_post_response = False
diff --git a/tests/agent/test_empty_terminal_reasoning_surface.py b/tests/agent/test_empty_terminal_reasoning_surface.py
index 7cfd1aea02c..7fbf21859f0 100644
--- a/tests/agent/test_empty_terminal_reasoning_surface.py
+++ b/tests/agent/test_empty_terminal_reasoning_surface.py
@@ -1,17 +1,14 @@
"""Tests for the empty-terminal reasoning surface.
-When the empty-response ladder is fully exhausted (prefill continuation,
-empty-content retries, provider fallback) and the model produced structured
-reasoning but no visible text, the DELIVERED final_response is a clearly
-labeled reasoning excerpt instead of a bare "(empty)" — the reasoning often
-contains the actual answer. Idea credit: PR #48795 (@ligl0325).
+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).
Invariants pinned here:
- The persisted assistant message keeps the "(empty)" sentinel and the
``_empty_terminal_sentinel`` marker (replay semantics unchanged).
-- Raw reasoning is NEVER promoted earlier in the ladder — a reasoning-only
- response still goes through prefill continuation first.
-- A truly empty exhaustion (no reasoning either) still returns "(empty)".
+- Inline think-only content still goes through the recovery ladder.
+- A truly empty response (no reasoning either) still uses the recovery ladder.
"""
from __future__ import annotations
@@ -81,10 +78,8 @@ def _truly_empty_response():
)
-def test_exhausted_reasoning_only_delivers_labeled_excerpt(tmp_path, monkeypatch):
- """After the full ladder is exhausted on reasoning-only responses, the
- delivered text is the labeled excerpt — not a bare '(empty)' — while the
- transcript keeps its existing sentinel-scaffolding semantics."""
+def test_clean_stop_reasoning_only_returns_on_first_call(tmp_path, monkeypatch):
+ """A clean stop promotes structured reasoning without a recovery call."""
agent = _build_agent(tmp_path, monkeypatch)
monkeypatch.setattr(
agent, "_interruptible_api_call",
@@ -93,20 +88,8 @@ def test_exhausted_reasoning_only_delivers_labeled_excerpt(tmp_path, monkeypatch
result = agent.run_conversation("what is the answer?")
- final = result["final_response"]
- assert "(empty)" != final
- assert "only internal reasoning" in final
- assert "The answer is 42" in final
-
- # Persistence semantics unchanged: the delivered excerpt is
- # delivery-only. The turn finalizer strips the "(empty)" terminal
- # sentinel from the transcript tail (replay safety, existing design),
- # and the labeled excerpt must never be persisted as assistant content.
- assert not any(
- m.get("role") == "assistant"
- and "only internal reasoning" in (m.get("content") or "")
- for m in result["messages"]
- )
+ assert result["final_response"] == "The answer is 42 because of the calculation above."
+ assert result["api_calls"] == 1
def test_exhausted_truly_empty_keeps_existing_behavior(tmp_path, monkeypatch):
@@ -128,13 +111,24 @@ def test_exhausted_truly_empty_keeps_existing_behavior(tmp_path, monkeypatch):
assert "only internal reasoning" not in final
-def test_reasoning_never_promoted_before_ladder_exhaustion(tmp_path, monkeypatch):
- """A reasoning-only response must first go through prefill continuation —
- if the model then produces real text, THAT is the answer, and no labeled
- reasoning excerpt appears."""
+def test_inline_thinking_still_uses_recovery_ladder(tmp_path, monkeypatch):
+ """Inline think blocks are not ordinary empty content and remain recoverable."""
agent = _build_agent(tmp_path, monkeypatch)
responses = [
- _reasoning_only_response(),
+ 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",
+ ),
SimpleNamespace(
choices=[SimpleNamespace(
message=SimpleNamespace(
@@ -158,4 +152,4 @@ def test_reasoning_never_promoted_before_ladder_exhaustion(tmp_path, monkeypatch
result = agent.run_conversation("what is the answer?")
assert result["final_response"] == "42."
- assert "only internal reasoning" not in result["final_response"]
+ assert result["api_calls"] == 2
diff --git a/tests/agent/test_run_agent.py b/tests/agent/test_run_agent.py
index 559634cf94d..9418b396b90 100644
--- a/tests/agent/test_run_agent.py
+++ b/tests/agent/test_run_agent.py
@@ -3490,8 +3490,8 @@ class TestRunConversation:
assert result["completed"] is True
assert result["api_calls"] == 2
- def test_reasoning_only_local_resumed_no_compression_triggered(self, agent):
- """Reasoning-only responses no longer trigger compression — prefill then accepted."""
+ def test_reasoning_only_local_clean_stop_returns_immediately(self, agent):
+ """A clean-stop reasoning answer returns without compression or recovery."""
self._setup_agent(agent)
agent.base_url = "http://127.0.0.1:1234/v1"
agent.compression_enabled = True
@@ -3505,7 +3505,6 @@ class TestRunConversation:
{"role": "assistant", "content": "old answer"},
]
- # 6 responses: original + 2 prefill + 3 retries after prefill exhaustion
with (
patch.object(agent, "_interruptible_api_call", side_effect=[empty_resp] * 6),
patch.object(agent, "_compress_context") as mock_compress,
@@ -3517,26 +3516,18 @@ class TestRunConversation:
mock_compress.assert_not_called() # no compression triggered
assert result["completed"] is True
- # The bare "(empty)" sentinel is never delivered for reasoning-only
- # exhaustion: the labeled reasoning excerpt (which may contain the
- # answer) replaces it at the terminal. See
- # test_empty_terminal_reasoning_surface.py; #34452's explainer still
- # covers the truly-empty case.
- assert result["final_response"] != "(empty)"
- assert "only internal reasoning" in result["final_response"]
- assert "reasoning only" in result["final_response"]
- assert result["turn_exit_reason"] == "empty_response_exhausted"
- assert result["api_calls"] == 6 # 1 original + 2 prefill + 3 retries
+ assert result["final_response"] == "reasoning only"
+ assert result["turn_exit_reason"] == "text_response(finish_reason=stop)"
+ assert result["api_calls"] == 1
- def test_reasoning_only_response_prefill_then_empty(self, agent):
- """Structured reasoning-only triggers prefill (2), then retries (3), then (empty)."""
+ def test_reasoning_only_response_returns_on_first_call(self, agent):
+ """Structured reasoning-only clean stops bypass the empty-response ladder."""
self._setup_agent(agent)
empty_resp = _mock_response(
content=None,
finish_reason="stop",
reasoning_content="structured reasoning answer",
)
- # 6 responses: 1 original + 2 prefill + 3 retries after prefill exhaustion
agent.client.chat.completions.create.side_effect = [empty_resp] * 6
with (
patch.object(agent, "_persist_session"),
@@ -3545,13 +3536,8 @@ class TestRunConversation:
):
result = agent.run_conversation("answer me")
assert result["completed"] is True
- # Reasoning-only exhaustion delivers the labeled reasoning excerpt
- # instead of the bare "(empty)" sentinel (see
- # test_empty_terminal_reasoning_surface.py).
- assert result["final_response"] != "(empty)"
- assert "only internal reasoning" in result["final_response"]
- assert "structured reasoning answer" in result["final_response"]
- assert result["api_calls"] == 6 # 1 original + 2 prefill + 3 retries
+ assert result["final_response"] == "structured reasoning answer"
+ assert result["api_calls"] == 1
def test_truly_empty_response_stops_after_repeated_empty(self, agent):