Skip to content

Build and run agents you can see, understand and trust.

fix(agent): preserve multi-tool pairs during context compression (#2093)

qiuC1232d5ebe15fe83
diff --git a/src/agentscope/agent/_agent.py b/src/agentscope/agent/_agent.py
index b15128a2e..553cc4603 100644
--- a/src/agentscope/agent/_agent.py
+++ b/src/agentscope/agent/_agent.py
@@ -2045,21 +2045,30 @@ class Agent:
break
block_index -= 1
- # Adjust the block_index to avoid splitting tool call and result pairs
-
- # Check if the reserved part has tool results that don't have the
- # corresponding tool calls
- remain_result_ids = {}
- for i in range(len(boundary_msg_content) - 1, block_index, -1):
- block = boundary_msg_content[i]
- if isinstance(block, ToolResultBlock):
- remain_result_ids[block.id] = i
- elif isinstance(block, ToolCallBlock):
- remain_result_ids.pop(block.id, None)
+ # Adjust the block_index to avoid splitting tool call and result pairs.
+ # Moving the boundary can bring another tool call into the compressed
+ # part while leaving its result reserved, so repeat until it is stable.
+ while True:
+ # Check if the reserved part has tool results that don't have the
+ # corresponding tool calls
+ remain_result_ids = {}
+ for i in range(
+ len(boundary_msg_content) - 1,
+ block_index,
+ -1,
+ ):
+ block = boundary_msg_content[i]
+ if isinstance(block, ToolResultBlock):
+ remain_result_ids[block.id] = i
+ elif isinstance(block, ToolCallBlock):
+ remain_result_ids.pop(block.id, None)
+
+ # All tool result blocks in the reserved part are paired.
+ if not remain_result_ids:
+ break
- # Find the largest index of the remaining tool results, which doesn't
- # have the corresponding tool calls in the reserved parts
- if remain_result_ids:
+ # Move unmatched results into the compressed part and recheck,
+ # because this move can split another tool call/result pair.
block_index = max(remain_result_ids.values())
# Split the boundary msg content
diff --git a/tests/compress_context_test.py b/tests/compress_context_test.py
index e589a2e8a..49e0abc1f 100644
--- a/tests/compress_context_test.py
+++ b/tests/compress_context_test.py
@@ -18,6 +18,8 @@ from agentscope.message import (
AssistantMsg,
TextBlock,
ToolCallBlock,
+ ToolResultBlock,
+ ToolResultState,
HintBlock,
Msg,
)
@@ -634,6 +636,183 @@ class ContextCompressionTest(IsolatedAsyncioTestCase):
],
)
+ async def test_split_multi_tool_pairs_reaches_stable_boundary(
+ self,
+ ) -> None:
+ """The boundary is rechecked after moving an unmatched result."""
+ agent = Agent(
+ name="Friday",
+ system_prompt="",
+ model=MockModel(context_size=1_000),
+ state=AgentState(
+ session_id="multi-tool-compression",
+ context=[
+ UserMsg("User", "old" * 80, id="old-user"),
+ AssistantMsg(
+ "Friday",
+ [
+ ToolCallBlock(
+ id="tc1",
+ name="first_tool",
+ input=json.dumps({"value": "a" * 80}),
+ ),
+ ToolCallBlock(
+ id="tc2",
+ name="second_tool",
+ input=json.dumps({"value": "b" * 80}),
+ ),
+ ToolResultBlock(
+ id="tc1",
+ name="first_tool",
+ output=[
+ TextBlock(text="first result " * 8),
+ ],
+ state=ToolResultState.SUCCESS,
+ ),
+ ToolResultBlock(
+ id="tc2",
+ name="second_tool",
+ output=[
+ TextBlock(text="second result " * 8),
+ ],
+ state=ToolResultState.SUCCESS,
+ ),
+ TextBlock(
+ text="Both tools completed.",
+ id="final-text",
+ ),
+ ],
+ id="multi-tool-msg",
+ ),
+ UserMsg(
+ "User",
+ "latest question",
+ id="latest-user",
+ ),
+ ],
+ ),
+ toolkit=Toolkit(),
+ )
+
+ to_compress, to_reserve = await agent._split_context_for_compression(
+ to_reserved_tokens=86,
+ tools=[],
+ )
+
+ self.assertListEqual(
+ [msg.model_dump() for msg in to_compress],
+ [
+ {
+ "id": "old-user",
+ "created_at": AnyString(),
+ "finished_at": AnyString(),
+ "name": "User",
+ "role": "user",
+ "content": [
+ {
+ "id": AnyString(),
+ "type": "text",
+ "text": "old" * 80,
+ },
+ ],
+ "metadata": {},
+ "usage": None,
+ },
+ {
+ "id": "multi-tool-msg",
+ "created_at": AnyString(),
+ "finished_at": None,
+ "name": "Friday",
+ "role": "assistant",
+ "content": [
+ {
+ "id": "tc1",
+ "type": "tool_call",
+ "name": "first_tool",
+ "input": json.dumps({"value": "a" * 80}),
+ "state": "pending",
+ "suggested_rules": [],
+ },
+ {
+ "id": "tc2",
+ "type": "tool_call",
+ "name": "second_tool",
+ "input": json.dumps({"value": "b" * 80}),
+ "state": "pending",
+ "suggested_rules": [],
+ },
+ {
+ "id": "tc1",
+ "type": "tool_result",
+ "name": "first_tool",
+ "output": [
+ {
+ "id": AnyString(),
+ "type": "text",
+ "text": "first result " * 8,
+ },
+ ],
+ "state": "success",
+ "metadata": {},
+ },
+ {
+ "id": "tc2",
+ "type": "tool_result",
+ "name": "second_tool",
+ "output": [
+ {
+ "id": AnyString(),
+ "type": "text",
+ "text": "second result " * 8,
+ },
+ ],
+ "state": "success",
+ "metadata": {},
+ },
+ ],
+ "metadata": {},
+ "usage": None,
+ },
+ ],
+ )
+ self.assertListEqual(
+ [msg.model_dump() for msg in to_reserve],
+ [
+ {
+ "id": "multi-tool-msg",
+ "created_at": AnyString(),
+ "finished_at": None,
+ "name": "Friday",
+ "role": "assistant",
+ "content": [
+ {
+ "id": "final-text",
+ "type": "text",
+ "text": "Both tools completed.",
+ },
+ ],
+ "metadata": {},
+ "usage": None,
+ },
+ {
+ "id": "latest-user",
+ "created_at": AnyString(),
+ "finished_at": AnyString(),
+ "name": "User",
+ "role": "user",
+ "content": [
+ {
+ "id": AnyString(),
+ "type": "text",
+ "text": "latest question",
+ },
+ ],
+ "metadata": {},
+ "usage": None,
+ },
+ ],
+ )
+
async def test_context_compression(self) -> None:
"""Test the context compression logic."""
model = MockModel(context_size=100)