fix(agent): avoid persisting empty-response recovery scaffolding

This commit is contained in:
wxst 2026-05-03 19:03:13 +08:00 committed by Teknium
parent 80717a157f
commit e73508979f
2 changed files with 87 additions and 5 deletions

View file

@ -3792,11 +3792,21 @@ class AIAgent:
Ensures conversations are never lost, even on errors or early returns.
"""
self._drop_trailing_empty_recovery_synthetic(messages)
self._apply_persist_user_message_override(messages)
self._session_messages = messages
self._save_session_log(messages)
self._flush_messages_to_session_db(messages, conversation_history)
def _drop_trailing_empty_recovery_synthetic(self, messages: List[Dict]) -> None:
"""Remove private empty-response retry scaffolding from transcript tails."""
while (
messages
and isinstance(messages[-1], dict)
and messages[-1].get("_empty_recovery_synthetic")
):
messages.pop()
def _flush_messages_to_session_db(self, messages: List[Dict], conversation_history: List[Dict] = None):
"""Persist any un-flushed messages to the SQLite session store.
@ -13706,6 +13716,7 @@ class AIAgent:
# APIs reject as an invalid sequence.
_nudge_msg = self._build_assistant_message(assistant_message, finish_reason)
_nudge_msg["content"] = "(empty)"
_nudge_msg["_empty_recovery_synthetic"] = True
messages.append(_nudge_msg)
messages.append({
"role": "user",
@ -13714,6 +13725,7 @@ class AIAgent:
"empty response. Please process the tool "
"results above and continue with the task."
),
"_empty_recovery_synthetic": True,
})
continue
@ -13816,6 +13828,7 @@ class AIAgent:
# "(empty)" terminal.
_turn_exit_reason = "empty_response_exhausted"
reasoning_text = self._extract_reasoning(assistant_message)
self._drop_trailing_empty_recovery_synthetic(messages)
assistant_msg = self._build_assistant_message(assistant_message, finish_reason)
assistant_msg["content"] = "(empty)"
messages.append(assistant_msg)
@ -13890,14 +13903,17 @@ class AIAgent:
final_msg = self._build_assistant_message(assistant_message, finish_reason)
# Pop thinking-only prefill message(s) before appending
# the final response. This avoids consecutive assistant
# messages which break strict-alternation providers
# (Anthropic Messages API) and keeps history clean.
# Pop thinking-only prefill and empty-response retry
# scaffolding before appending the final response. These
# internal turns are only for the next API retry and should
# not become durable transcript context.
while (
messages
and isinstance(messages[-1], dict)
and messages[-1].get("_thinking_prefill")
and (
messages[-1].get("_thinking_prefill")
or messages[-1].get("_empty_recovery_synthetic")
)
):
messages.pop()