Skip to content

The agent that grows with you

fix(agent): stop _relay_thinking re-emitting plain reply text as reasoning.available (132477)

webtecnica4c08c0d4075d
diff --git a/agent/turn_response_intake.py b/agent/turn_response_intake.py
index 7523a885c15..51897f9e664 100644
--- a/agent/turn_response_intake.py
+++ b/agent/turn_response_intake.py
@@ -13,6 +13,7 @@ import re
from typing import Any, Dict, Optional
from agent.provider_projection import splice_provider_projection
+from agent.think_scrubber import THINK_TAG_NAMES
from agent.trajectory import has_incomplete_scratchpad
from agent.turn_truncation import (
CODEX_FALLBACK_ACTIVATED, continue_codex_incomplete, normalize_response_for_agent, partial_result,
@@ -20,7 +21,14 @@ from agent.turn_truncation import (
logger = logging.getLogger("agent.conversation_loop")
-_REASONING_TAG_RE = re.compile(r'</?(?:REASONING_SCRATCHPAD|think|reasoning)>')
+# One alternation over the shared tag list so this path tracks every tag the scrubber hides
+# (the old 3-of-9 list leaked <thinking>/<思考>/... previews), case-insensitive like the scrubber.
+_THINK_TAG_ALT = "|".join(re.escape(name) for name in THINK_TAG_NAMES)
+_REASONING_TAG_RE = re.compile(rf'</?(?:{_THINK_TAG_ALT})>', re.IGNORECASE)
+_INLINE_REASONING_RE = re.compile(
+ rf'<(?:{_THINK_TAG_ALT})>(.*?)</(?:{_THINK_TAG_ALT})>',
+ re.DOTALL | re.IGNORECASE,
+)
@dataclass
@@ -100,21 +108,61 @@ def _fire_post_api_request_hook(
pass
-def _relay_thinking(agent: Any, content: str) -> None:
- """Relay the model's text to the progress callback: subagents send the first line to
- the parent display; any agent with a structured callback gets ``reasoning.available``."""
- _think_text = _REASONING_TAG_RE.sub('', content.strip()).strip()
- first_line = _think_text.split('\n')[0][:80] if _think_text else ""
- if first_line and getattr(agent, '_delegate_depth', 0) > 0:
- try:
- agent.tool_progress_callback("_thinking", first_line)
- except Exception:
- pass
- elif _think_text:
+def _reasoning_preview(agent: Any, content: str, assistant_message: Any = None) -> str:
+ """Reasoning text for the ``reasoning.available`` progress event, or ``""`` when the turn
+ carries no reasoning at all.
+
+ The structured extractor (``agent._extract_reasoning``) is authoritative: native
+ ``reasoning`` / ``reasoning_content`` / ``reasoning_details`` first, else inline think blocks
+ over the shared ``THINK_TAG_NAMES`` set. Only when no extractor is available do we match the
+ inline blocks ourselves. Plain reply text is never reasoning — clients render this event as
+ the turn's thinking block (Desktop ``message-stream.ts`` → ``appendReasoningDelta``), so
+ forwarding the answer here prints it twice and dresses visible output up as internal
+ reasoning (#132477, #118934, #13007)."""
+ extractor = getattr(agent, "_extract_reasoning", None)
+ if callable(extractor) and assistant_message is not None:
try:
- agent.tool_progress_callback("reasoning.available", "_thinking", _think_text[:500], None)
+ extracted = extractor(assistant_message)
except Exception:
- pass
+ extracted = None
+ if isinstance(extracted, str) and extracted.strip():
+ return extracted.strip()
+ if not content:
+ return ""
+ return "\n\n".join(
+ block.strip() for block in _INLINE_REASONING_RE.findall(content) if block and block.strip()
+ )
+
+
+def _relay_thinking(agent: Any, content: str, assistant_message: Any = None) -> None:
+ """Relay the model's output to the progress callback: a subagent sends its first line to the
+ parent display; a top-level agent with a structured callback gets ``reasoning.available``
+ carrying extracted reasoning only — never the visible reply (#132477)."""
+ _think_text = _REASONING_TAG_RE.sub('', (content or "").strip()).strip()
+ first_line = _think_text.split('\n')[0][:80] if _think_text else ""
+
+ if getattr(agent, '_delegate_depth', 0) > 0:
+ # Subagent lane: an activity preview for the parent, never a reasoning block.
+ if first_line:
+ try:
+ agent.tool_progress_callback("_thinking", first_line)
+ except Exception:
+ pass
+ return
+
+ # Native reasoning already went out as ``reasoning.delta`` for this model response (the flag
+ # resets per response in _reset_stream_delivery_tracking); re-emitting it as
+ # ``reasoning.available`` renders the same thinking twice (#132477).
+ if getattr(agent, "_native_reasoning_streamed", False):
+ return
+
+ reasoning_text = _reasoning_preview(agent, content, assistant_message)
+ if not reasoning_text:
+ return
+ try:
+ agent.tool_progress_callback("reasoning.available", "_thinking", reasoning_text[:500], None)
+ except Exception:
+ pass
def normalize_model_response(
@@ -154,8 +202,10 @@ def normalize_model_response(
agent._vprint(f"{agent.log_prefix}🤖 Assistant: {content}")
else:
agent._vprint(f"{agent.log_prefix}🤖 Assistant: {content[:100]}{'...' if len(content) > 100 else ''}")
- if content and agent.tool_progress_callback:
- _relay_thinking(agent, content)
+ # Gate on the callback alone, not on non-empty content: a tool-call-only turn (empty content)
+ # still carries reasoning the preview can extract (#132477 / nekwo's note on #124031).
+ if agent.tool_progress_callback:
+ _relay_thinking(agent, content, assistant_message)
# Incomplete <REASONING_SCRATCHPAD> (opened, never closed): the model ran out of
# output tokens mid-reasoning — retry up to 2 times, then save as partial.
diff --git a/tests/agent/test_turn_response_intake_relay.py b/tests/agent/test_turn_response_intake_relay.py
new file mode 100644
index 00000000000..90c84a6e0b3
--- /dev/null
+++ b/tests/agent/test_turn_response_intake_relay.py
@@ -0,0 +1,148 @@
+"""``reasoning.available`` carries extracted reasoning, never the reply text (#132477).
+
+``normalize_model_response`` relays every assistant response through ``_relay_thinking``. The old
+relay stripped reasoning *delimiters* out of the whole reply and forwarded whatever was left, so a
+structured-reasoning model answering in plain text (no ``<think>``/``<REASONING_SCRATCHPAD>``
+block) had its entire answer re-emitted as ``reasoning.available``. Desktop's ``message-stream.ts``
+handles that event with ``appendReasoningDelta(text, replace=true)`` and renders it as the turn's
+thinking block — on top of the native reasoning already streamed via ``reasoning.delta`` — so
+reply fragments rendered twice (#132477).
+
+The relay must fire only with genuinely extracted reasoning (structured ``reasoning`` /
+``reasoning_content`` / ``reasoning_details`` fields or inline think blocks), stay silent when the
+response already streamed native reasoning deltas, and keep the subagent ``_thinking`` first-line
+preview intact.
+"""
+from types import SimpleNamespace
+from typing import Any
+
+from agent.agent_runtime_helpers import extract_reasoning
+from agent.turn_response_intake import _relay_thinking, normalize_model_response
+
+
+class _Transport:
+ def normalize_response(self, response, strip_tool_prefix=False):
+ return response
+
+
+class _Agent:
+ """Minimal stand-in exposing exactly what the intake path touches."""
+
+ api_mode = "openai_chat"
+ quiet_mode = True
+ verbose_logging = False
+ log_prefix = ""
+ _delegate_depth = 0
+ _incomplete_scratchpad_retries = 0
+ _native_reasoning_streamed = False
+
+ def __init__(self, delegate_depth: int = 0):
+ self._delegate_depth = delegate_depth
+ self.calls = []
+ # Typed loosely so a test can swap in ``None`` (no callback registered).
+ self.tool_progress_callback: Any = lambda *args: self.calls.append(args)
+ self._extract_reasoning = lambda msg: extract_reasoning(self, msg)
+
+ def _get_transport(self):
+ return _Transport()
+
+ def _vprint(self, *args, **kwargs):
+ pass
+
+ @property
+ def reasoning_calls(self):
+ return [c for c in self.calls if c[0] == "reasoning.available"]
+
+
+def _run(agent, *, content, tool_calls=None, reasoning=None, reasoning_content=None):
+ message = SimpleNamespace(
+ content=content, finish_reason="stop", tool_calls=tool_calls, reasoning=reasoning,
+ reasoning_content=reasoning_content, reasoning_details=None,
+ )
+ normalize_model_response(
+ agent, response=message, messages=[], api_messages=[], conversation_history={},
+ api_call_count=1, api_duration=0.0, api_start_time=0.0, api_request_id="r1",
+ effective_task_id="t1", turn_id="t1",
+ )
+ return agent.calls
+
+
+def test_plain_reply_is_not_relayed_as_reasoning():
+ """#132477: a reply with no reasoning blocks must emit no ``reasoning.available`` at all."""
+ agent = _Agent()
+ calls = _run(agent, content="The answer is 42.")
+ assert calls == []
+
+
+def test_inline_thinking_block_is_relayed_without_the_reply():
+ """Only the text inside the think block is reasoning; the visible reply is not."""
+ agent = _Agent()
+ calls = _run(
+ agent,
+ content="<REASONING_SCRATCHPAD>plan step one</REASONING_SCRATCHPAD>\nThe answer is 42.",
+ )
+ assert calls == [("reasoning.available", "_thinking", "plan step one", None)]
+
+
+def test_structured_reasoning_field_is_relayed_instead_of_the_reply():
+ """Non-streaming providers hand reasoning over as a field, not as content tags."""
+ agent = _Agent()
+ calls = _run(agent, content="Plain reply.", reasoning_content="secret plan")
+ assert len(agent.reasoning_calls) == 1
+ preview = agent.reasoning_calls[0][2]
+ assert preview == "secret plan"
+ assert "Plain reply" not in preview
+
+
+def test_native_reasoning_streamed_suppresses_the_relay():
+ """#132477: the reasoning already went out as ``reasoning.delta`` for this response, so
+ re-emitting it as ``reasoning.available`` would render the thinking twice."""
+ agent = _Agent()
+ agent._native_reasoning_streamed = True
+ calls = _run(agent, content="The answer is 42.", reasoning_content="secret plan")
+ assert calls == []
+
+
+def test_tool_call_only_turn_relays_extracted_reasoning():
+ """The gate is the callback alone: empty content must not hide a tool turn's reasoning."""
+ agent = _Agent()
+ calls = _run(
+ agent, content=None, tool_calls=[{"id": "c1", "type": "function", "function": {"name": "ls"}}],
+ reasoning="planning the tool call",
+ )
+ assert calls == [("reasoning.available", "_thinking", "planning the tool call", None)]
+
+
+def test_tool_call_only_turn_without_reasoning_stays_silent():
+ agent = _Agent()
+ calls = _run(
+ agent, content="", tool_calls=[{"id": "c1", "type": "function", "function": {"name": "ls"}}],
+ )
+ assert calls == []
+
+
+def test_subagent_first_line_preview_still_reaches_the_parent():
+ """Delegated agents keep their first-line ``_thinking`` preview and never a reasoning block."""
+ agent = _Agent(delegate_depth=1)
+ calls = _run(agent, content="First line of subagent work\nsecond line")
+ assert calls == [("_thinking", "First line of subagent work")]
+
+
+def test_subagent_preview_strips_the_full_think_tag_set():
+ """The preview binds to the shared THINK_TAG_NAMES list, not the old 3-of-9 subset
+ (``REASONING_SCRATCHPAD|think|reasoning``), so a ``<thinking>`` tag never leaks upstream."""
+ agent = _Agent(delegate_depth=1)
+ calls = _run(agent, content="<thinking>hidden plan</thinking>\nVisible work")
+ assert calls == [("_thinking", "hidden plan")]
+
+
+def test_relay_is_silent_without_a_callback():
+ """No structured callback registered → nothing relayed, nothing raised, even with reasoning."""
+ agent = _Agent()
+ agent.tool_progress_callback = None
+ _relay_thinking(
+ agent,
+ "<REASONING_SCRATCHPAD>hush</REASONING_SCRATCHPAD>ok",
+ SimpleNamespace(content="<REASONING_SCRATCHPAD>hush</REASONING_SCRATCHPAD>ok"),
+ )
+ assert agent.calls == []