Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

fix(backend/blocks): orchestrator EXTENDED_THINKING emits final-answer only (#13188)

Zamil Majdyb50bf6b1ba1d
diff --git a/autogpt_platform/backend/backend/blocks/orchestrator.py b/autogpt_platform/backend/backend/blocks/orchestrator.py
index cbb89c9600..679648a9a5 100644
--- a/autogpt_platform/backend/backend/blocks/orchestrator.py
+++ b/autogpt_platform/backend/backend/blocks/orchestrator.py
@@ -364,6 +364,33 @@ def _disambiguate_tool_names(tools: list[dict[str, Any]]) -> None:
func["description"] = f"{original_desc} [Pre-configured: {summary}]"
+def _select_final_answer_parts(
+ text_parts: list[str],
+ has_tool_calls: bool,
+ current: list[str],
+) -> list[str]:
+ """Pick the text parts that should be treated as the agent's final
+ answer for the ``finished`` output pin in EXTENDED_THINKING SDK mode.
+
+ Contract: an assistant message contributes only when it carries text
+ AND no tool calls. That means the model has stopped calling tools
+ and is composing a response — which is what ``finished`` is supposed
+ to surface to ``AgentOutputBlock``. Messages with tool calls
+ (regardless of whether they also carry text) are intermediate
+ narration that belongs in ``conversations``, not the final answer.
+ Empty messages don't contribute.
+
+ The caller threads ``current`` across the SDK stream; the last
+ text-only message wins. If no message in the run qualifies, the
+ returned list is empty and ``finished`` surfaces as ``""`` — useful
+ diagnostic signal that the agent's prompt didn't compose a final
+ answer.
+ """
+ if not has_tool_calls and any(part.strip() for part in text_parts):
+ return list(text_parts)
+ return current
+
+
class OrchestratorBlock(Block):
"""A block that uses a language model to orchestrate tool calls.
@@ -1701,7 +1728,21 @@ class OrchestratorBlock(Block):
prefix=f"orchestrator-sdk-{execution_params.graph_exec_id}-"
)
- response_parts: list[str] = []
+ # ``final_response_parts`` is the agent's *final answer* — only the
+ # text from the last assistant message that has no tool calls. We
+ # intentionally do NOT accumulate every assistant TextBlock across
+ # the SDK stream: those intermediate texts are narration between
+ # tool calls, not the composed answer the user wired into
+ # ``AgentOutputBlock``. This matches BUILT_IN's behaviour where
+ # ``tool_call_loop`` only yields ``response.response_text`` once
+ # the model stops calling tools. If the agent never produces a
+ # text-only message (e.g. it keeps calling tools until max
+ # iterations), ``final_response_parts`` stays empty and the
+ # ``finished`` output surfaces as an empty string — that's useful
+ # signal for dry-run / autopilot diagnostics ("the agent didn't
+ # compose a final answer; repair the prompt") rather than a
+ # transcript dump that masks the missing composition.
+ final_response_parts: list[str] = []
conversation: list[dict[str, Any]] = list(prompt) # Start with input prompt
total_prompt_tokens = 0
total_completion_tokens = 0
@@ -1788,7 +1829,6 @@ class OrchestratorBlock(Block):
for content_block in sdk_msg.content:
if isinstance(content_block, TextBlock):
text_parts.append(content_block.text)
- response_parts.append(content_block.text)
elif isinstance(content_block, ToolUseBlock):
raw_name = getattr(content_block, "name", "unknown")
# Strip MCP prefix for readability in
@@ -1802,6 +1842,16 @@ class OrchestratorBlock(Block):
),
}
)
+ # Capture the final answer: the last assistant
+ # message that has only text (no tool calls)
+ # wins. See ``_select_final_answer_parts`` for
+ # the contract + diagnostic-signal rationale.
+ final_response_parts = _select_final_answer_parts(
+ text_parts=text_parts,
+ has_tool_calls=bool(tool_use_parts),
+ current=final_response_parts,
+ )
+
if text_parts or tool_use_parts:
msg_content = "".join(text_parts)
if tool_use_parts:
@@ -1894,7 +1944,7 @@ class OrchestratorBlock(Block):
yield "error", str(sdk_error)
return
- response_text = "".join(response_parts)
+ response_text = "".join(final_response_parts)
yield "finished", response_text
yield "conversations", conversation
diff --git a/autogpt_platform/backend/backend/blocks/test/test_orchestrator_execution_mode.py b/autogpt_platform/backend/backend/blocks/test/test_orchestrator_execution_mode.py
index 4d3ca2b326..d1ed9ad0a1 100644
--- a/autogpt_platform/backend/backend/blocks/test/test_orchestrator_execution_mode.py
+++ b/autogpt_platform/backend/backend/blocks/test/test_orchestrator_execution_mode.py
@@ -11,7 +11,11 @@ from unittest.mock import AsyncMock, MagicMock, patch
import pytest
from backend.blocks.llm import LlmModel
-from backend.blocks.orchestrator import ExecutionMode, OrchestratorBlock
+from backend.blocks.orchestrator import (
+ ExecutionMode,
+ OrchestratorBlock,
+ _select_final_answer_parts,
+)
# ---------------------------------------------------------------------------
# ExecutionMode enum integrity
@@ -200,3 +204,106 @@ class TestExtendedThinkingValidationRaisesInBlock:
execution_context=MagicMock(),
execution_processor=MagicMock(),
)
+
+
+# ---------------------------------------------------------------------------
+# _select_final_answer_parts — pins the SDK-mode "finished" contract:
+# only the last text-only assistant message contributes; messages with
+# tool calls (text or no) are intermediate narration.
+# ---------------------------------------------------------------------------
+
+
+class TestSelectFinalAnswerParts:
+ """The helper is the testable extraction of the inline branch in
+ ``_execute_tools_sdk_mode`` that decides which assistant-message
+ texts make it into the ``finished`` output pin. Covering it here
+ keeps the SDK path's contract pinned without mocking the full
+ Claude Agent SDK + MCP stream."""
+
+ def test_text_only_message_replaces_current(self) -> None:
+ # Model stopped calling tools and emitted text — that's the
+ # composed final answer. Replaces any prior selection.
+ assert _select_final_answer_parts(
+ text_parts=["final answer"],
+ has_tool_calls=False,
+ current=["prior"],
+ ) == ["final answer"]
+
+ def test_text_with_tool_calls_keeps_current(self) -> None:
+ # Mixed text+tool messages are intermediate narration — keep
+ # the last text-only selection unchanged.
+ assert _select_final_answer_parts(
+ text_parts=["narration before tool"],
+ has_tool_calls=True,
+ current=["earlier final answer"],
+ ) == ["earlier final answer"]
+
+ def test_tool_only_message_keeps_current(self) -> None:
+ # Message with no text and only tool calls — keep current.
+ assert _select_final_answer_parts(
+ text_parts=[],
+ has_tool_calls=True,
+ current=["earlier final"],
+ ) == ["earlier final"]
+
+ def test_empty_message_keeps_current(self) -> None:
+ # Defensive: empty assistant message contributes nothing.
+ assert _select_final_answer_parts(
+ text_parts=[],
+ has_tool_calls=False,
+ current=["earlier final"],
+ ) == ["earlier final"]
+
+ def test_no_qualifying_message_returns_empty(self) -> None:
+ # The agent never composes a final answer (e.g. keeps calling
+ # tools until max iterations) — ``current`` stays empty so
+ # ``finished`` surfaces as ``""``, the diagnostic signal for
+ # autopilot / dry-run.
+ current: list[str] = []
+ for text, has_tools in [
+ (["narration"], True),
+ ([], True),
+ (["more narration"], True),
+ ]:
+ current = _select_final_answer_parts(
+ text_parts=text, has_tool_calls=has_tools, current=current
+ )
+ assert current == []
+
+ def test_last_text_only_message_wins_in_sequence(self) -> None:
+ # Multiple text-only messages over the run — the **last** one
+ # is the agent's answer (most recent composition).
+ current: list[str] = []
+ current = _select_final_answer_parts(
+ text_parts=["first answer"], has_tool_calls=False, current=current
+ )
+ current = _select_final_answer_parts(
+ text_parts=["narration"], has_tool_calls=True, current=current
+ )
+ current = _select_final_answer_parts(
+ text_parts=["second answer"], has_tool_calls=False, current=current
+ )
+ assert current == ["second answer"]
+
+ def test_returns_copy_not_reference(self) -> None:
+ # Defensive: the SDK call site reuses the ``text_parts`` list
+ # per-message; the helper must return a copy so subsequent
+ # mutations don't poison the captured final answer.
+ text_parts = ["final"]
+ result = _select_final_answer_parts(
+ text_parts=text_parts, has_tool_calls=False, current=[]
+ )
+ text_parts.clear()
+ assert result == ["final"]
+
+ def test_blank_text_only_message_keeps_current(self) -> None:
+ # Whitespace-only / empty-string text blocks with no tool calls
+ # would otherwise look like a "final" answer and clobber a real
+ # one captured earlier in the stream. The docstring says empty
+ # messages don't contribute — enforce that for blank strings too.
+ for blank in [[""], [" "], ["\n"], ["", " ", "\t"]]:
+ assert _select_final_answer_parts(
+ text_parts=blank,
+ has_tool_calls=False,
+ current=["real final answer"],
+ ) == ["real final answer"], f"blank={blank!r} clobbered current"