fix(agent): keep the thinking-prefill marker so the drop pass can strip trailing stubs

This commit is contained in:
Jefftree
2026-07-30 22:35:41 -04:00
committed by kshitij
parent 1439a65829
commit 97ced4bce2
3 changed files with 41 additions and 3 deletions
+7 -2
View File
@@ -1907,8 +1907,13 @@ def run_conversation(
# Remove finish_reason - not accepted by strict APIs (e.g. Mistral)
if "finish_reason" in api_msg:
api_msg.pop("finish_reason")
# Strip internal thinking-prefill marker
api_msg.pop("_thinking_prefill", None)
# ``_thinking_prefill`` deliberately survives here. The reasoning
# fields it accompanies were just stripped above, which leaves
# ``_is_thinking_only_assistant`` no way to recognize the stub, so
# ``_drop_thinking_only_and_merge_users`` below could never fire on
# it. The transport strips every leading-underscore key before the
# wire (agent/transports/chat_completions.py), so keeping it costs
# nothing.
# Strip length-continuation marks; not every transport drops underscore keys.
api_msg.pop("_length_continuation_fragment", None)
api_msg.pop("_length_continuation_nudge", None)
+7 -1
View File
@@ -4649,7 +4649,13 @@ class AIAgent:
return False
elif content is not None and content != "":
return False
# Content is empty-ish. Is there reasoning to make it thinking-only?
# 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
@@ -45,6 +45,17 @@ class TestIsThinkingOnlyAssistant:
assert not AIAgent._is_thinking_only_assistant(None)
assert not AIAgent._is_thinking_only_assistant("hello")
def test_prefill_stub_detected_after_reasoning_stripped(self):
# The per-call copy for a provider that doesn't echo reasoning back has
# had reasoning_content/reasoning removed, leaving _thinking_prefill as
# the only evidence the turn was ever a thinking-only stub.
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}
assert not AIAgent._is_thinking_only_assistant(msg)
# ---------------------------------------------------------------------------
# _drop_thinking_only_and_merge_users — the full pass
@@ -122,6 +133,22 @@ class TestDropThinkingOnlyAndMergeUsers:
]
def test_trailing_prefill_stubs_dropped_so_request_ends_on_user(self):
# Regression for the Gemini 400 "Requests ending with a model turn are
# not supported". Two thinking-only responses in a row queue two prefill
# stubs, and on a provider that doesn't echo reasoning back both arrive
# here with their reasoning fields already stripped. Before the fix the
# pass couldn't see them and the request went out ending on assistant.
msgs = [
{"role": "system", "content": "sys"},
{"role": "user", "content": "do the thing"},
{"role": "assistant", "content": "", "_thinking_prefill": True},
{"role": "assistant", "content": "", "_thinking_prefill": True},
]
out = AIAgent._drop_thinking_only_and_merge_users(msgs)
assert [m["role"] for m in out] == ["system", "user"]
assert out[-1]["role"] != "assistant"
def test_system_messages_ignored_by_pass(self):
msgs = [
{"role": "system", "content": "sys prompt"},