Skip to content

Commit 6b5360d

Browse files
committed
fix(core): harden summary content rendering edge cases
- Stringify non-JSON-serializable MCP result mappings with the same json.dumps(default=str) fallback used by Content.from_function_result, preventing compaction crashes inside _select_summary_input_groups. - Preserve time order of text and structured contents in _format_summary_message by emitting consecutive text blocks in place. - Use tool_name as the fallback identity for MCP tool calls nested in approval contents. Addresses review feedback on PR #8087; adds three regression tests.
1 parent 7cced63 commit 6b5360d

2 files changed

Lines changed: 71 additions & 6 deletions

File tree

‎python/packages/core/agent_framework/_compaction.py‎

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,7 +1198,10 @@ def _tool_result_text(value: Any) -> str:
11981198
if text_parts:
11991199
return "\n".join(text_parts)
12001200
if isinstance(value, Mapping):
1201-
return json.dumps(cast(Mapping[str, object], value), ensure_ascii=False)
1201+
try:
1202+
return json.dumps(cast(Mapping[str, object], value), ensure_ascii=False, default=str)
1203+
except (TypeError, ValueError):
1204+
return str(cast(object, value))
12021205
return str(cast(object, value))
12031206

12041207

@@ -1237,7 +1240,7 @@ def _format_summary_content(content: Content) -> str:
12371240
return f"mcp_tool_result: {result_text}{call_id_suffix}"
12381241
if content.type in ("function_approval_request", "function_approval_response"):
12391242
nested_call = content.function_call
1240-
name = nested_call.name if nested_call is not None else ""
1243+
name = "" if nested_call is None else nested_call.name or nested_call.tool_name or ""
12411244
label = "approval_request" if content.type == "function_approval_request" else "approval_response"
12421245
rendered = f"{label}: {name} [id={content.id}]"
12431246
if content.type == "function_approval_response":
@@ -1247,10 +1250,20 @@ def _format_summary_content(content: Content) -> str:
12471250

12481251

12491252
def _format_summary_message(index: int, message: Message) -> str:
1250-
parts = [_format_summary_content(content) for content in message.contents]
1251-
if message.text:
1252-
parts.append(message.text)
1253-
content_text = "; ".join(part for part in parts if part)
1253+
parts: list[str] = []
1254+
pending_text: list[str] = []
1255+
for content in message.contents:
1256+
rendered = _format_summary_content(content)
1257+
if rendered:
1258+
if pending_text:
1259+
parts.append(" ".join(pending_text))
1260+
pending_text = []
1261+
parts.append(rendered)
1262+
elif content.type == "text" and content.text:
1263+
pending_text.append(content.text)
1264+
if pending_text:
1265+
parts.append(" ".join(pending_text))
1266+
content_text = "; ".join(parts)
12541267
if not content_text:
12551268
content_text = ", ".join(content.type for content in message.contents)
12561269
return f"{index}. [{message.role}] {content_text}"

‎python/packages/core/tests/core/test_compaction.py‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from __future__ import annotations
44

55
import logging
6+
from datetime import date
67
from typing import Any
78

89
import pytest
@@ -1100,6 +1101,57 @@ def test_format_summary_message_includes_approval_response() -> None:
11001101
assert "approved=True" in rendered
11011102

11021103

1104+
def test_format_summary_message_stringifies_non_json_mcp_result_without_crash() -> None:
1105+
message = Message(
1106+
role="tool",
1107+
contents=[Content("mcp_server_tool_result", call_id="mcp_1", output={"when": date(2026, 1, 1)})],
1108+
)
1109+
1110+
rendered = _format_summary_message(9, message)
1111+
1112+
assert "2026" in rendered
1113+
assert "[call_id=mcp_1]" in rendered
1114+
1115+
1116+
def test_format_summary_message_preserves_time_order_for_mixed_contents() -> None:
1117+
message = Message(
1118+
role="assistant",
1119+
contents=[
1120+
"I'll check the weather.",
1121+
Content.from_function_call(call_id="call_1", name="get_weather", arguments='{"city":"Seattle"}'),
1122+
"Please wait.",
1123+
],
1124+
)
1125+
1126+
rendered = _format_summary_message(10, message)
1127+
1128+
assert rendered.index("I'll check the weather.") < rendered.index("function_call")
1129+
assert rendered.index("function_call") < rendered.index("Please wait.")
1130+
1131+
1132+
def test_format_summary_message_uses_tool_name_for_mcp_approval() -> None:
1133+
message = Message(
1134+
role="assistant",
1135+
contents=[
1136+
Content.from_function_approval_request(
1137+
id="approval_mcp_1",
1138+
function_call=Content.from_mcp_server_tool_call(
1139+
call_id="mcp_1",
1140+
tool_name="search",
1141+
server_name="test_server",
1142+
arguments='{"query":"x"}',
1143+
),
1144+
)
1145+
],
1146+
)
1147+
1148+
rendered = _format_summary_message(11, message)
1149+
1150+
assert "approval_request" in rendered
1151+
assert "search" in rendered
1152+
assert "[id=approval_mcp_1]" in rendered
1153+
1154+
11031155
async def test_summarization_strategy_returns_false_when_summary_generation_fails(
11041156
caplog: Any,
11051157
) -> None:

0 commit comments

Comments
 (0)