Skip to content

JiuwenSwarm is an intelligent AI Agent built on openJiuwen. It extends the powerful capabilities of large language models directly to your fingertips through various communication apps you use daily.

Python

fix(team): 团队工具结果嵌套字符串按 512 字符截断

openJiuwen-botd50a242f8621
diff --git a/jiuwenswarm/server/runtime/agent_adapter/team_helpers.py b/jiuwenswarm/server/runtime/agent_adapter/team_helpers.py
index 7e64911a05..0978d6d09e 100644
--- a/jiuwenswarm/server/runtime/agent_adapter/team_helpers.py
+++ b/jiuwenswarm/server/runtime/agent_adapter/team_helpers.py
@@ -1310,6 +1310,51 @@ def _enrich_leader_event(parsed: dict[str, Any], chunk: Any) -> dict[str, Any]:
_TEAM_TOOL_RESULT_TEXT_LIMIT = 512
+def _clip_tool_result_value(value: Any) -> tuple[Any, int, bool]:
+ """Clip long strings inside a tool-result field.
+
+ Returns the clipped value, the total length of strings visited, and whether
+ any string was clipped. Dict and list values are walked so a nested
+ ``raw_output`` string cannot bypass the limit. Containers are copied only
+ when a child changes.
+ """
+ if isinstance(value, str):
+ if len(value) <= _TEAM_TOOL_RESULT_TEXT_LIMIT:
+ return value, len(value), False
+ return value[:_TEAM_TOOL_RESULT_TEXT_LIMIT], len(value), True
+ if isinstance(value, dict):
+ original_size = 0
+ truncated = False
+ rebuilt: dict[Any, Any] | None = None
+ for key, item in value.items():
+ new_item, item_size, item_truncated = _clip_tool_result_value(item)
+ original_size += item_size
+ truncated = truncated or item_truncated
+ if new_item is not item:
+ if rebuilt is None:
+ rebuilt = dict(value)
+ rebuilt[key] = new_item
+ return (value if rebuilt is None else rebuilt), original_size, truncated
+ if isinstance(value, (list, tuple)):
+ original_size = 0
+ truncated = False
+ rebuilt_items: list[Any] | None = None
+ for index, item in enumerate(value):
+ new_item, item_size, item_truncated = _clip_tool_result_value(item)
+ original_size += item_size
+ truncated = truncated or item_truncated
+ if new_item is not item:
+ if rebuilt_items is None:
+ rebuilt_items = list(value)
+ rebuilt_items[index] = new_item
+ if rebuilt_items is None:
+ return value, original_size, truncated
+ if isinstance(value, tuple):
+ return tuple(rebuilt_items), original_size, truncated
+ return rebuilt_items, original_size, truncated
+ return value, 0, False
+
+
def _truncate_team_tool_result_event(parsed: dict[str, Any]) -> dict[str, Any]:
"""Trim large team tool result fields before forwarding them to clients."""
if parsed.get("event_type") != "chat.tool_result":
@@ -1319,14 +1364,12 @@ def _truncate_team_tool_result_event(parsed: dict[str, Any]) -> dict[str, Any]:
truncated = False
original_size = 0
for key in ("result", "raw_output"):
- value = next_event.get(key)
- if not isinstance(value, str):
- continue
- original_size += len(value)
- if len(value) <= _TEAM_TOOL_RESULT_TEXT_LIMIT:
+ if key not in next_event:
continue
- next_event[key] = value[:_TEAM_TOOL_RESULT_TEXT_LIMIT]
- truncated = True
+ new_value, value_size, value_truncated = _clip_tool_result_value(next_event[key])
+ original_size += value_size
+ truncated = truncated or value_truncated
+ next_event[key] = new_value
if truncated:
next_event["truncated"] = True
diff --git a/tests/unit_tests/agentserver/test_team_helpers.py b/tests/unit_tests/agentserver/test_team_helpers.py
index ce111ce1d2..5d796a964a 100644
--- a/tests/unit_tests/agentserver/test_team_helpers.py
+++ b/tests/unit_tests/agentserver/test_team_helpers.py
@@ -5586,3 +5586,77 @@ async def test_await_runner_pool_release_stops_entry_that_stays_running(monkeypa
await team_helpers._await_runner_pool_release("oc_team", "sess")
assert stopped == [{"team_name": "oc_team", "session_id": "sess"}]
+
+
+def test_truncate_team_tool_result_clips_nested_raw_output_strings() -> None:
+ limit = team_helpers._TEAM_TOOL_RESULT_TEXT_LIMIT
+ full_markdown = "x" * (limit + 100)
+ short_note = "ok"
+ raw_output = {"result": full_markdown, "note": short_note}
+ parsed = {
+ "event_type": "chat.tool_result",
+ "result": "y" * (limit + 10),
+ "raw_output": raw_output,
+ "tool_name": "read_file_content",
+ }
+
+ clipped = team_helpers._truncate_team_tool_result_event(parsed)
+
+ assert clipped["truncated"] is True
+ assert clipped["original_size"] == (limit + 10) + (limit + 100) + len(short_note)
+ assert clipped["result"] == "y" * limit
+ assert clipped["raw_output"]["result"] == "x" * limit
+ assert clipped["raw_output"]["note"] == short_note
+ assert clipped["tool_name"] == "read_file_content"
+ assert raw_output["result"] == full_markdown
+ assert parsed["raw_output"] is raw_output
+
+
+def test_truncate_team_tool_result_leaves_short_nested_text_untouched() -> None:
+ raw_output = {"result": "short", "rows": ["a", "b"]}
+ parsed = {
+ "event_type": "chat.tool_result",
+ "result": "preview",
+ "raw_output": raw_output,
+ }
+
+ clipped = team_helpers._truncate_team_tool_result_event(parsed)
+
+ assert "truncated" not in clipped
+ assert "original_size" not in clipped
+ assert clipped["raw_output"] is raw_output
+
+
+def test_truncate_team_tool_result_ignores_other_events() -> None:
+ parsed = {"event_type": "chat.delta", "result": "z" * 1000}
+
+ assert team_helpers._truncate_team_tool_result_event(parsed) is parsed
+
+
+def test_truncate_team_tool_result_clips_top_level_list_and_tuple() -> None:
+ limit = team_helpers._TEAM_TOOL_RESULT_TEXT_LIMIT
+ long_text = "x" * (limit + 20)
+ nested_text = "y" * (limit + 5)
+ raw_list = [long_text, "ok", {"note": nested_text}]
+ parsed = {"event_type": "chat.tool_result", "raw_output": raw_list}
+
+ clipped = team_helpers._truncate_team_tool_result_event(parsed)
+
+ assert isinstance(clipped["raw_output"], list)
+ assert clipped["truncated"] is True
+ assert clipped["original_size"] == (limit + 20) + len("ok") + (limit + 5)
+ assert clipped["raw_output"][0] == "x" * limit
+ assert clipped["raw_output"][1] == "ok"
+ assert clipped["raw_output"][2]["note"] == "y" * limit
+ assert raw_list[0] == long_text
+ assert parsed["raw_output"] is raw_list
+
+ raw_tuple = ("z" * (limit + 3), "ok")
+ parsed_tuple = {"event_type": "chat.tool_result", "raw_output": raw_tuple}
+ clipped_tuple = team_helpers._truncate_team_tool_result_event(parsed_tuple)
+
+ assert isinstance(clipped_tuple["raw_output"], tuple)
+ assert clipped_tuple["raw_output"][0] == "z" * limit
+ assert clipped_tuple["raw_output"][1] == "ok"
+ assert clipped_tuple["original_size"] == (limit + 3) + len("ok")
+ assert raw_tuple[0] == "z" * (limit + 3)