Skip to content

The agent that grows with you

fix(agent): discard bare tool-call marker before fallback/persistence (#78148)

joaomarcosba9068c8b6e4
diff --git a/agent/conversation_loop.py b/agent/conversation_loop.py
index dd8529800ae..3858c6799c5 100644
--- a/agent/conversation_loop.py
+++ b/agent/conversation_loop.py
@@ -6117,9 +6117,25 @@ def run_conversation(
]
assistant_msg = agent._build_assistant_message(assistant_message, finish_reason)
-
+
turn_content = assistant_message.content or ""
+ # Some local tool-call templates emit a bare bracketed token
+ # (for example ``[memory]``) as assistant content alongside a
+ # function call. It is protocol scaffolding, not an answer.
+ # Persisting or caching it as visible content lets the empty
+ # post-tool fallback replay that token forever after compaction (#78148).
+ if (
+ assistant_message.tool_calls
+ and re.fullmatch(r"\[[A-Za-z_][A-Za-z0-9_.-]*\]", turn_content.strip())
+ ):
+ logger.warning(
+ "Discarding bare tool-call marker from assistant content: %s",
+ turn_content,
+ )
+ turn_content = ""
+ assistant_msg["content"] = ""
+
# Classify tools in this turn to determine if they are all housekeeping.
# This classification is needed regardless of whether the turn has visible content,
# because a substantive tool-only turn must invalidate any older housekeeping fallback.
diff --git a/tests/run_agent/test_conversation_fallback_state.py b/tests/run_agent/test_conversation_fallback_state.py
index be46edcb93a..a0d07c18abe 100644
--- a/tests/run_agent/test_conversation_fallback_state.py
+++ b/tests/run_agent/test_conversation_fallback_state.py
@@ -125,3 +125,83 @@ def test_substantive_tool_only_turn_invalidates_older_housekeeping_fallback():
)
+def test_bare_tool_marker_is_not_reused_as_final_response():
+ """
+ Regression test for #78148.
+
+ A provider/local template can emit a bare bracketed token (e.g. "[memory]")
+ as assistant content alongside a tool call. That token is protocol
+ scaffolding, not an answer. If it gets cached as `_last_content_with_tools`
+ and the following turn is empty, the post-tool fallback replays it as the
+ final response — and because it then enters the persisted transcript,
+ later context compaction preserves it, letting the model repeat the
+ marker in subsequent turns.
+
+ Test sequence:
+ 1. Content "[memory]" + skill_manage (housekeeping) tool call → the bare
+ marker must be discarded, not cached as a fallback.
+ 2. Empty content, no tool calls → enters the post-tool nudge path since
+ no fallback is available.
+ 3. Content "Recovered after nudge." → returned as the final response.
+
+ Before the fix:
+ - Step 1 cached "[memory]" as `_last_content_with_tools`.
+ - Step 2 reused it via the empty-response fallback, so the conversation
+ never reached step 3 and "[memory]" leaked into the persisted history.
+
+ After the fix:
+ - Step 1 strips the bare marker before it is cached or persisted.
+ - Step 2 has no fallback available and enters the nudge path instead.
+ - Step 3 returns the nudge response as the final answer.
+ """
+ with (
+ patch("run_agent.get_tool_definitions", return_value=_tool_defs("skill_manage")),
+ patch("run_agent.check_toolset_requirements", return_value={}),
+ patch("run_agent.OpenAI"),
+ ):
+ agent = AIAgent(
+ api_key="test-key",
+ base_url="https://openrouter.ai/api/v1/",
+ quiet_mode=True,
+ skip_context_files=True,
+ skip_memory=True,
+ )
+
+ agent._cached_system_prompt = "You are helpful."
+ agent._use_prompt_caching = False
+ agent.compression_enabled = False
+ agent.save_trajectories = False
+ agent.valid_tool_names = {"skill_manage"}
+ agent.client = MagicMock()
+ agent.client.chat.completions.create.side_effect = [
+ # Turn 1: Bare "[memory]" marker + housekeeping tool call.
+ _response(
+ content="[memory]",
+ finish_reason="tool_calls",
+ tool_calls=[_tool_call("skill_manage", "skill1")],
+ ),
+ # Turn 2: Empty response (should enter nudge path, not reuse "[memory]").
+ _response(content="", finish_reason="stop"),
+ # Turn 3: Nudge response
+ _response(content="Recovered after nudge.", finish_reason="stop"),
+ ]
+
+ with (
+ patch("run_agent.handle_function_call", return_value="ok"),
+ patch.object(agent, "_persist_session"),
+ patch.object(agent, "_save_trajectory"),
+ patch.object(agent, "_cleanup_task_resources"),
+ ):
+ result = agent.run_conversation("do the full task")
+
+ assert result["final_response"] != "[memory]", (
+ "The bare tool-call marker leaked through as the final response — "
+ "it should have been discarded before caching/persistence."
+ )
+ assert result["final_response"] == "Recovered after nudge.", (
+ f"Expected nudge recovery response, got: {result['final_response']}."
+ )
+ assert result["api_calls"] == 3, (
+ f"Expected 3 API calls (including nudge), got: {result['api_calls']}."
+ )
+