Skip to content

The agent that grows with you

test(agent): cover the API-copy build so restoring the marker pop fails

Jefftree126a6ffa4d62
diff --git a/run_agent.py b/run_agent.py
index 081a8ae6fc1..275cca799c2 100644
--- a/run_agent.py
+++ b/run_agent.py
@@ -4626,6 +4626,16 @@ class AIAgent:
return False
if msg.get("tool_calls"):
return False
+ # A prefill stub is thinking-only by construction: the loop only sets
+ # ``_thinking_prefill`` on a turn that had reasoning and no visible
+ # text. It has to be checked ahead of the content inspection below,
+ # because both things this function would otherwise key on are gone by
+ # the time the drop pass runs. The reasoning fields are stripped from
+ # the API copy for providers that don't echo them back, and
+ # ``repair_empty_non_final_messages`` rewrites a non-final stub's empty
+ # content to a placeholder, which would read here as real output.
+ if msg.get("_thinking_prefill"):
+ return True
# Does it have any actual output?
content = msg.get("content")
if isinstance(content, str):
@@ -4649,13 +4659,6 @@ class AIAgent:
return False
elif content is not None and content != "":
return False
- # Content is empty-ish. A prefill stub is thinking-only by construction:
- # the loop only sets ``_thinking_prefill`` on a turn that had reasoning
- # and no visible text. Check it first, because the per-call copy has its
- # reasoning fields stripped for providers that don't echo them back,
- # which would otherwise leave nothing here to key on.
- if msg.get("_thinking_prefill"):
- return True
reasoning = msg.get("reasoning_content") or msg.get("reasoning")
if isinstance(reasoning, str) and reasoning.strip():
return True
diff --git a/tests/run_agent/test_thinking_only_sanitizer.py b/tests/run_agent/test_thinking_only_sanitizer.py
index 4a4e21a42e5..0ca0eab4368 100644
--- a/tests/run_agent/test_thinking_only_sanitizer.py
+++ b/tests/run_agent/test_thinking_only_sanitizer.py
@@ -52,8 +52,25 @@ class TestIsThinkingOnlyAssistant:
on_wire = {"role": "assistant", "content": "", "_thinking_prefill": True}
assert AIAgent._is_thinking_only_assistant(on_wire)
- def test_prefill_marker_does_not_override_visible_content(self):
- msg = {"role": "assistant", "content": "real text", "_thinking_prefill": True}
+ def test_healed_prefill_stub_is_still_detected(self):
+ # repair_empty_non_final_messages runs before the drop pass and rewrites
+ # a non-final stub's empty content to a placeholder. The marker has to
+ # outrank that, or the healed stub survives and the request still ends
+ # on a model turn.
+ healed = {
+ "role": "assistant",
+ "content": "[response interrupted]",
+ "_thinking_prefill": True,
+ }
+ assert AIAgent._is_thinking_only_assistant(healed)
+
+ def test_prefill_marker_does_not_override_tool_calls(self):
+ msg = {
+ "role": "assistant",
+ "content": "",
+ "tool_calls": [{"id": "c1", "function": {"name": "t", "arguments": "{}"}}],
+ "_thinking_prefill": True,
+ }
assert not AIAgent._is_thinking_only_assistant(msg)
diff --git a/tests/run_agent/test_thinking_prefill_trailing_turn.py b/tests/run_agent/test_thinking_prefill_trailing_turn.py
new file mode 100644
index 00000000000..acba82a5d2f
--- /dev/null
+++ b/tests/run_agent/test_thinking_prefill_trailing_turn.py
@@ -0,0 +1,146 @@
+"""Regression test for the thinking-only prefill reaching the wire.
+
+A thinking-only response (reasoning tokens, no visible text) makes the loop
+append an empty assistant turn and re-send so the model continues its own
+reasoning. On providers that don't echo reasoning back, the API copy has its
+reasoning fields stripped before ``_drop_thinking_only_and_merge_users`` runs,
+so the drop pass used to see a bare ``{"role": "assistant", "content": ""}``
+and let it through. Gemini rejects that with
+
+ 400 INVALID_ARGUMENT: Requests ending with a model turn are not supported.
+
+classified as non-retryable, so the turn aborts outright.
+
+Unlike the unit tests in ``test_thinking_only_sanitizer.py``, this drives
+``run_conversation`` and asserts on the payload actually handed to the client,
+so it exercises the API-copy build that decides whether ``_thinking_prefill``
+survives as far as the drop pass.
+"""
+
+from __future__ import annotations
+
+from types import SimpleNamespace
+from unittest.mock import MagicMock, patch
+
+import pytest
+
+
+def loop_agent():
+ """AIAgent with a mocked OpenAI client, mirroring the fixture in
+ ``test_dropped_tool_call_recovery.py``."""
+ from run_agent import AIAgent
+ with (
+ patch("run_agent.get_tool_definitions", return_value=[]),
+ patch("run_agent.check_toolset_requirements", return_value={}),
+ patch("run_agent.OpenAI"),
+ ):
+ agent = AIAgent(
+ api_key="test-key-1234567890",
+ base_url="https://openrouter.ai/api/v1",
+ quiet_mode=True,
+ skip_context_files=True,
+ skip_memory=True,
+ )
+ agent.client = MagicMock()
+ agent._cached_system_prompt = "You are helpful."
+ agent._use_prompt_caching = False
+ agent.tool_delay = 0
+ agent.compression_enabled = False
+ agent.save_trajectories = False
+ return agent
+
+
+def _thinking_only_response():
+ """Reasoning tokens, no visible text — what triggers the prefill retry."""
+ from tests.run_agent.test_run_agent import _mock_response
+ return _mock_response(
+ content="",
+ finish_reason="stop",
+ reasoning="Let me work through the request step by step.",
+ )
+
+
+def _final_response(text="Here is the answer."):
+ """An ordinary text turn that ends the loop."""
+ from tests.run_agent.test_run_agent import _mock_response
+ return _mock_response(content=text, finish_reason="stop")
+
+
+def _sent_messages(create_mock, call_index):
+ call = create_mock.call_args_list[call_index]
+ return call.kwargs.get("messages") or call.args[0].get("messages")
+
+
+class TestThinkingPrefillTrailingTurn:
+
+ def test_request_after_prefills_does_not_end_on_assistant(self, loop_agent):
+ # Two thinking-only responses queue two prefill stubs, then the model
+ # finally produces text. The third request is the one that used to go
+ # out ending on a model turn.
+ loop_agent.client.chat.completions.create.side_effect = [
+ _thinking_only_response(),
+ _thinking_only_response(),
+ _final_response(),
+ ]
+
+ with (
+ patch.object(loop_agent, "_persist_session"),
+ patch.object(loop_agent, "_save_trajectory"),
+ patch.object(loop_agent, "_cleanup_task_resources"),
+ ):
+ loop_agent.run_conversation("do the thing")
+
+ create = loop_agent.client.chat.completions.create
+ assert create.call_count >= 3, (
+ "Two thinking-only responses should each trigger a prefill retry."
+ )
+
+ final_request = _sent_messages(create, 2)
+ assert final_request[-1]["role"] != "assistant", (
+ "Request ends on a model turn, which Gemini rejects with a "
+ "non-retryable 400. The thinking-only prefill stubs must be "
+ f"dropped before send. Got roles: {[m['role'] for m in final_request]}"
+ )
+
+ def test_prefill_stubs_are_absent_from_the_wire_payload(self, loop_agent):
+ """The stubs should be gone entirely, not merely trailed by a nudge."""
+ loop_agent.client.chat.completions.create.side_effect = [
+ _thinking_only_response(),
+ _final_response(),
+ ]
+
+ with (
+ patch.object(loop_agent, "_persist_session"),
+ patch.object(loop_agent, "_save_trajectory"),
+ patch.object(loop_agent, "_cleanup_task_resources"),
+ ):
+ loop_agent.run_conversation("do the thing")
+
+ sent = _sent_messages(loop_agent.client.chat.completions.create, 1)
+ empty_assistants = [
+ m for m in sent
+ if m.get("role") == "assistant" and not (m.get("content") or "").strip()
+ ]
+ assert not empty_assistants, (
+ f"Empty assistant stub(s) reached the wire: {empty_assistants}"
+ )
+
+ def test_internal_marker_never_reaches_the_wire(self, loop_agent):
+ """``_thinking_prefill`` survives the API-copy build on purpose, but the
+ transport must still keep it off the wire."""
+ loop_agent.client.chat.completions.create.side_effect = [
+ _thinking_only_response(),
+ _final_response(),
+ ]
+
+ with (
+ patch.object(loop_agent, "_persist_session"),
+ patch.object(loop_agent, "_save_trajectory"),
+ patch.object(loop_agent, "_cleanup_task_resources"),
+ ):
+ loop_agent.run_conversation("do the thing")
+
+ sent = _sent_messages(loop_agent.client.chat.completions.create, 1)
+ leaked = [m for m in sent if any(str(k).startswith("_") for k in m)]
+ assert not leaked, f"Internal scaffolding keys reached the wire: {leaked}"