fix(session): keep placeholder for tool results filtered to empty

This commit is contained in:
tangtaizhong666 2026-06-12 09:09:24 +08:00 committed by Xubin Ren
parent ac5e84d453
commit 2ebf7e2eef
2 changed files with 34 additions and 1 deletions

View File

@ -1589,7 +1589,11 @@ class AgentLoop:
elif isinstance(content, list): elif isinstance(content, list):
filtered = self._sanitize_persisted_blocks(content, should_truncate_text=True) filtered = self._sanitize_persisted_blocks(content, should_truncate_text=True)
if not filtered: if not filtered:
continue # Dropping the message would leave its assistant
# tool_call without a result; keep a placeholder.
filtered = [
{"type": "text", "text": "[tool result omitted during persistence]"}
]
entry["content"] = filtered entry["content"] = filtered
elif role == "user": elif role == "user":
if isinstance(content, str) and ContextBuilder._RUNTIME_CONTEXT_TAG in content: if isinstance(content, str) and ContextBuilder._RUNTIME_CONTEXT_TAG in content:

View File

@ -1345,3 +1345,32 @@ async def test_turn_after_unanswered_user_keeps_tool_call_pairing(tmp_path: Path
assert [m["role"] for m in persisted.messages] == [ assert [m["role"] for m in persisted.messages] == [
"user", "user", "assistant", "tool", "assistant", "user", "user", "assistant", "tool", "assistant",
] ]
def test_save_turn_keeps_placeholder_for_empty_tool_result_blocks() -> None:
# Dropping the whole tool message would leave the assistant tool_call
# without a result, which strict APIs reject as firmly as orphans.
loop = _mk_loop()
session = Session(key="test:empty-tool-blocks")
loop._save_turn(
session,
[
{
"role": "assistant",
"content": "",
"tool_calls": [{
"id": "call_empty",
"type": "function",
"function": {"name": "exec", "arguments": "{}"},
}],
},
{"role": "tool", "tool_call_id": "call_empty", "name": "exec", "content": []},
],
skip=0,
)
assert [m["role"] for m in session.messages] == ["assistant", "tool"]
assert session.messages[1]["content"] == [
{"type": "text", "text": "[tool result omitted during persistence]"}
]