Skip to content

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

fix(agent): keep tool-result metadata and timestamps when truncating (#2754)

Grapette.L4acb042b3500
diff --git a/src/agentscope/agent/_agent.py b/src/agentscope/agent/_agent.py
index e4a129fb8..a9f65b675 100644
--- a/src/agentscope/agent/_agent.py
+++ b/src/agentscope/agent/_agent.py
@@ -3186,12 +3186,18 @@ class Agent:
name=tool_result.name,
output=reserved_blocks,
state=tool_result.state,
+ metadata=deepcopy(tool_result.metadata),
+ created_at=tool_result.created_at,
+ finished_at=tool_result.finished_at,
)
offload_tool_result = ToolResultBlock(
id=tool_result.id,
name=tool_result.name,
output=offload_blocks,
state=tool_result.state,
+ metadata=deepcopy(tool_result.metadata),
+ created_at=tool_result.created_at,
+ finished_at=tool_result.finished_at,
)
return reserved_tool_result, offload_tool_result
diff --git a/tests/compress_tool_result_test.py b/tests/compress_tool_result_test.py
index 31587b982..0b41da258 100644
--- a/tests/compress_tool_result_test.py
+++ b/tests/compress_tool_result_test.py
@@ -572,5 +572,87 @@ class ToolResultCompressionTest(IsolatedAsyncioTestCase):
expected_offload,
)
+ async def test_split_keeps_metadata_and_timestamps(self) -> None:
+ """Both halves of a split tool result keep metadata and timestamps."""
+ tool_result = ToolResultBlock(
+ id="test_9",
+ name="Write",
+ output=[
+ TextBlock(text="A" * 20, id="block1"),
+ TextBlock(text="B" * 400, id="block2"),
+ ],
+ state="success",
+ metadata={"diff": "--- a/x\n+++ b/x", "file_path": "/tmp/x.py"},
+ created_at="2026-09-21T10:00:00",
+ finished_at="2026-09-21T10:00:05",
+ )
+
+ async def mock_count_tokens(
+ messages: list,
+ tools: list | None = None,
+ ) -> int:
+ """Mock token counting function based on content length."""
+ content = messages[0].content
+ if isinstance(content, list):
+ return sum(len(b.text) for b in content if hasattr(b, "text"))
+ return 0
+
+ self.mock_model.count_tokens = mock_count_tokens
+ (
+ reserved,
+ offload,
+ ) = await self.agent._split_tool_result_for_compression(
+ tool_result,
+ )
+
+ self.assertDictEqual(
+ reserved.model_dump(),
+ {
+ "type": "tool_result",
+ "id": "test_9",
+ "name": "Write",
+ "output": [
+ {
+ "type": "text",
+ "text": "A" * 20 + "B" * 80,
+ "id": "block1",
+ "created_at": AnyString(),
+ "finished_at": None,
+ },
+ ],
+ "state": "success",
+ "metadata": {
+ "diff": "--- a/x\n+++ b/x",
+ "file_path": "/tmp/x.py",
+ },
+ "created_at": "2026-09-21T10:00:00",
+ "finished_at": "2026-09-21T10:00:05",
+ },
+ )
+ self.assertDictEqual(
+ offload.model_dump(),
+ {
+ "type": "tool_result",
+ "id": "test_9",
+ "name": "Write",
+ "output": [
+ {
+ "type": "text",
+ "text": "B" * 320,
+ "id": "block2",
+ "created_at": AnyString(),
+ "finished_at": None,
+ },
+ ],
+ "state": "success",
+ "metadata": {
+ "diff": "--- a/x\n+++ b/x",
+ "file_path": "/tmp/x.py",
+ },
+ "created_at": "2026-09-21T10:00:00",
+ "finished_at": "2026-09-21T10:00:05",
+ },
+ )
+
async def asyncTearDown(self) -> None:
"""The async teardown method."""