Skip to content

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

fix(memory): fix the memory compression bug in `ReActAgent` and the serialization error in `InMemoryMemory` class (#1231)

DavdGao5baeafd93531
diff --git a/src/agentscope/agent/_react_agent.py b/src/agentscope/agent/_react_agent.py
index 29bffbdef..f8033ac67 100644
--- a/src/agentscope/agent/_react_agent.py
+++ b/src/agentscope/agent/_react_agent.py
@@ -554,7 +554,12 @@ class ReActAgent(ReActAgentBase):
prompt = await self.formatter.format(
msgs=[
Msg("system", self.sys_prompt, "system"),
- *await self.memory.get_memory(),
+ *await self.memory.get_memory(
+ exclude_mark=_MemoryMark.COMPRESSED
+ if self.compression_config
+ and self.compression_config.enable
+ else None,
+ ),
],
)
# Clear the hint messages after use
@@ -731,7 +736,12 @@ class ReActAgent(ReActAgentBase):
prompt = await self.formatter.format(
[
Msg("system", self.sys_prompt, "system"),
- *await self.memory.get_memory(),
+ *await self.memory.get_memory(
+ exclude_mark=_MemoryMark.COMPRESSED
+ if self.compression_config
+ and self.compression_config.enable
+ else None,
+ ),
hint_msg,
],
)
@@ -926,7 +936,12 @@ class ReActAgent(ReActAgentBase):
rewrite_prompt = await self.formatter.format(
msgs=[
Msg("system", self.sys_prompt, "system"),
- *await self.memory.get_memory(),
+ *await self.memory.get_memory(
+ exclude_mark=_MemoryMark.COMPRESSED
+ if self.compression_config
+ and self.compression_config.enable
+ else None,
+ ),
Msg(
"user",
"<system-hint>Now you need to rewrite "
diff --git a/src/agentscope/memory/_working_memory/_in_memory_memory.py b/src/agentscope/memory/_working_memory/_in_memory_memory.py
index 489e76990..8631a4c07 100644
--- a/src/agentscope/memory/_working_memory/_in_memory_memory.py
+++ b/src/agentscope/memory/_working_memory/_in_memory_memory.py
@@ -273,6 +273,7 @@ class InMemoryMemory(MemoryBase):
def state_dict(self) -> dict:
"""Get the state dictionary for serialization."""
return {
+ **super().state_dict(),
"content": [[msg.to_dict(), marks] for msg, marks in self.content],
}
@@ -280,10 +281,12 @@ class InMemoryMemory(MemoryBase):
"""Load the state dictionary for deserialization."""
if strict and "content" not in state_dict:
raise KeyError(
- "The state_dict does not contain 'content' key required for "
- "InMemoryMemory.",
+ "The state_dict does not contain 'content' "
+ "keys required for InMemoryMemory.",
)
+ self._compressed_summary = state_dict.get("_compressed_summary", "")
+
self.content = []
for item in state_dict.get("content", []):
if isinstance(item, (tuple, list)) and len(item) == 2:
diff --git a/tests/memory_compression_test.py b/tests/memory_compression_test.py
index a4623d39a..828e9abf9 100644
--- a/tests/memory_compression_test.py
+++ b/tests/memory_compression_test.py
@@ -6,7 +6,7 @@ from unittest import IsolatedAsyncioTestCase
from agentscope.agent import ReActAgent
from agentscope.formatter import FormatterBase
-from agentscope.message import Msg, TextBlock, ToolUseBlock, ToolResultBlock
+from agentscope.message import Msg, TextBlock
from agentscope.model import ChatModelBase, ChatResponse
from agentscope.token import CharTokenCounter
@@ -14,27 +14,48 @@ from agentscope.token import CharTokenCounter
class MockChatModel(ChatModelBase):
"""A mock chat model for testing purposes."""
+ def __init__(
+ self,
+ model_name: str,
+ stream: bool = False,
+ ) -> None:
+ """Initialize the mock chat model.
+
+ Args:
+ model_name (`str`):
+ The name of the model.
+ stream (`bool`, optional):
+ Whether to use streaming mode.
+ """
+ super().__init__(model_name=model_name, stream=stream)
+ self.call_count = 0
+ self.received_messages: list[list[dict]] = []
+
async def __call__(
self,
messages: list[dict],
**kwargs: Any,
) -> ChatResponse:
- """Mock the model's response."""
- if [_["id"] for _ in messages[1:-1]] != [str(_) for _ in range(3)]:
- raise ValueError("Unexpected messages input.")
-
- if messages[-1]["content"] != (
- "<system-hint>You have been working on the task described above "
- "but have not yet completed it. Now write a continuation summary "
- "that will allow you to resume work efficiently in a future "
- "context window where the conversation history will be replaced "
- "with this summary. Your summary should be structured, concise, "
- "and actionable.</system-hint>"
- ):
- raise ValueError("Unexpected final message content.")
+ """Mock the model's response.
+
+ Args:
+ messages (`list[dict]`):
+ The messages to process.
+
+ Returns:
+ `ChatResponse`:
+ The mocked response.
+ """
+ self.call_count += 1
+ self.received_messages.append(messages)
return ChatResponse(
- content=[],
+ content=[
+ TextBlock(
+ type="text",
+ text="This is a test response.",
+ ),
+ ],
metadata={
"task_overview": "This is a compressed summary.",
"current_state": "In progress",
@@ -59,87 +80,119 @@ class MockFormatter(FormatterBase):
`list[dict]`:
The formatted messages.
"""
- return [_.to_dict() for _ in msgs]
+ return [{"name": _.name, "content": _.content} for _ in msgs]
class MemoryCompressionTest(IsolatedAsyncioTestCase):
"""The unittest for memory compression."""
- async def test_memory_compression(self) -> None:
- """Test the memory compression functionality."""
+ async def test_no_compression_below_threshold(self) -> None:
+ """Test that compression is NOT triggered when memory is below
+ threshold.
+
+ This test verifies that:
+ 1. When memory token count is below the trigger threshold, compression
+ is not activated
+ 2. The agent's memory does not contain a compressed summary
+ 3. The model receives the full, uncompressed conversation history
+ """
+ model = MockChatModel(model_name="mock-model", stream=False)
agent = ReActAgent(
name="Friday",
sys_prompt="You are a helpful assistant.",
- model=MockChatModel(model_name="mock-model", stream=False),
+ model=model,
formatter=MockFormatter(),
compression_config=ReActAgent.CompressionConfig(
enable=True,
- trigger_threshold=100,
+ trigger_threshold=10000, # High threshold to avoid compression
agent_token_counter=CharTokenCounter(),
keep_recent=1,
),
)
- msgs = [
- Msg("user", "This is a test message 1.", "user"),
- Msg("assistant", "This is a test message 2.", "assistant"),
- Msg("assistant", "This is a test message 3.", "assistant"),
- Msg(
- "assistant",
- [
- TextBlock(
- type="text",
- text="This is a test message 3.",
- ),
- ToolUseBlock(
- id="id1",
- type="tool_use",
- name="test_tool",
- input={"param": "value"},
- ),
- ],
- "assistant",
- ),
- Msg(
- "system",
- [
- ToolResultBlock(
- id="id1",
- type="tool_result",
- name="test_tool",
- output="This is a test tool result.",
- ),
- ],
- "system",
- ),
- ]
- for i, msg in enumerate(msgs):
- msg.id = str(i)
-
- # Add mock messages to the agent's memory
- await agent.memory.add(memories=msgs[0])
+ # Create a user message that won't trigger compression
+ user_msg = Msg("user", "Hello, this is a short message.", "user")
- # Manually trigger memory compression
- await agent._compress_memory_if_needed()
+ # Call the agent
+ await agent(user_msg)
- # The memory shouldn't be compressed yet
+ # Verify that compression was NOT triggered (no compressed summary)
self.assertEqual(
agent.memory._compressed_summary,
"",
)
- memory = await agent.memory.get_memory()
+
+ # Verify the exact messages received by the model
self.assertListEqual(
- [_.id for _ in memory],
- ["0"],
+ model.received_messages,
+ [
+ [
+ {
+ "content": "You are a helpful assistant.",
+ "name": "system",
+ },
+ {
+ "content": "Hello, this is a short message.",
+ "name": "user",
+ },
+ ],
+ ],
+ )
+
+ async def test_compression_above_threshold(self) -> None:
+ """Test that compression IS triggered when memory exceeds threshold and
+ the model receives compressed prompts.
+
+ This test verifies that:
+ 1. When memory token count exceeds the trigger threshold, compression
+ is activated
+ 2. The agent's memory contains a properly formatted compressed summary
+ 3. After compression, the model receives prompts that include the
+ compressed summary instead of the full conversation history
+ 4. The compression summary follows the expected format and contains
+ the mock summary content
+
+ This is the key test ensuring that compression not only happens, but
+ also that the compressed format is actually used in subsequent model
+ calls.
+ """
+ model = MockChatModel(model_name="mock-model", stream=False)
+ agent = ReActAgent(
+ name="Friday",
+ sys_prompt="You are a helpful assistant.",
+ model=model,
+ formatter=MockFormatter(),
+ compression_config=ReActAgent.CompressionConfig(
+ enable=True,
+ trigger_threshold=100, # Low threshold to trigger compression
+ agent_token_counter=CharTokenCounter(),
+ keep_recent=1,
+ ),
)
- # Add more messages to exceed the threshold
- await agent.memory.add(memories=msgs[1:])
- await agent._compress_memory_if_needed()
+ # Create messages that will trigger compression
+ # First message - should not trigger compression
+ msgs = [
+ Msg(
+ "user",
+ "1",
+ "user",
+ ),
+ Msg(
+ "user",
+ "This is a long message " * 100, # Make it long
+ "user",
+ ),
+ Msg(
+ "user",
+ "2",
+ "user",
+ ),
+ ]
+ await agent(msgs)
- self.assertEqual(
- agent.memory._compressed_summary,
- """<system-info>Here is a summary of your previous work
+ # Verify that compression was triggered
+ summary = """<system-info>Here is a summary of your previous work
# Task Overview
This is a compressed summary.
@@ -153,31 +206,48 @@ N/A
N/A
# Context to Preserve
-N/A</system-info>""",
- )
-
- # Get all the memory including compressed
- memory = await agent.memory.get_memory()
+N/A</system-info>"""
self.assertEqual(
- [_.id for _ in memory[1:]],
- [str(_) for _ in range(5)],
+ agent.memory._compressed_summary,
+ summary,
)
- # Test get the uncompressed memory
- memory = await agent.memory.get_memory(
- exclude_mark="compressed",
- )
- self.assertEqual(
- [_.id for _ in memory[1:]],
- ["3", "4"],
- )
+ # Verify the exact messages received by the model after clearing
+ # First call: compression call
+ # Second call: agent response with compressed summary
+ expected_received_messages = [
+ [
+ {"name": "system", "content": "You are a helpful assistant."},
+ {"name": "user", "content": "1"},
+ {
+ "name": "user",
+ "content": "This is a long message " * 100,
+ },
+ {
+ "name": "user",
+ "content": (
+ "<system-hint>You have been working on the task "
+ "described above but have not yet completed it. "
+ "Now write a continuation summary that will allow "
+ "you to resume work efficiently in a future context "
+ "window where the conversation history will be "
+ "replaced with this summary. Your summary should "
+ "be structured, concise, and actionable."
+ "</system-hint>"
+ ),
+ },
+ ],
+ [
+ {"name": "system", "content": "You are a helpful assistant."},
+ {
+ "name": "user",
+ "content": summary,
+ },
+ {"name": "user", "content": "2"},
+ ],
+ ]
- # Test the prepend_summary=False option
- memory = await agent.memory.get_memory(
- exclude_mark="compressed",
- prepend_summary=False,
- )
self.assertListEqual(
- [_.id for _ in memory],
- ["3", "4"],
+ model.received_messages,
+ expected_received_messages,
)
diff --git a/tests/memory_test.py b/tests/memory_test.py
index aa068548c..1eec5fe4f 100644
--- a/tests/memory_test.py
+++ b/tests/memory_test.py
@@ -451,6 +451,47 @@ class ShortTermMemoryTest(IsolatedAsyncioTestCase):
await self.memory.clear()
+ async def _test_serialization(self) -> None:
+ """Test the serialization and deserialization of RedisMemory."""
+
+ # Test the state dict before any updates
+ self.assertDictEqual(
+ self.memory.state_dict(),
+ {
+ "_compressed_summary": "",
+ },
+ )
+
+ # Update compressed summary and test state dict
+ await self.memory.update_compressed_summary("Hi there!")
+ state_dict = self.memory.state_dict()
+
+ # Verify the state dict content
+ self.assertDictEqual(
+ state_dict,
+ {
+ "_compressed_summary": "Hi there!",
+ },
+ )
+
+ # Clear the compressed summary and verify state dict
+ await self.memory.update_compressed_summary("")
+ self.assertDictEqual(
+ self.memory.state_dict(),
+ {
+ "_compressed_summary": "",
+ },
+ )
+
+ # Load the previous state dict and verify restoration
+ self.memory.load_state_dict(state_dict)
+ self.assertDictEqual(
+ self.memory.state_dict(),
+ {
+ "_compressed_summary": "Hi there!",
+ },
+ )
+
async def asyncTearDown(self) -> None:
"""Clean up after unittests"""
await self.memory.clear()
@@ -474,6 +515,91 @@ class InMemoryMemoryTest(ShortTermMemoryTest):
await self._test_add_duplicated_msgs()
await self._test_delete_nonexistent_msg()
+ async def test_serialization(self) -> None:
+ """Test the serialization and deserialization of InMemoryMemory."""
+
+ msg = Msg("user", "1", "user")
+ await self.memory.add(msg)
+
+ # Test the state dict before any updates
+ self.assertDictEqual(
+ self.memory.state_dict(),
+ {
+ "_compressed_summary": "",
+ "content": [
+ [
+ {
+ "id": msg.id,
+ "name": msg.name,
+ "role": msg.role,
+ "content": msg.content,
+ "metadata": msg.metadata,
+ "timestamp": msg.timestamp,
+ },
+ [],
+ ],
+ ],
+ },
+ )
+
+ # Update compressed summary and test state dict
+ await self.memory.update_compressed_summary("Hello World!")
+ state_dict = self.memory.state_dict()
+
+ # Verify the state dict content
+ self.assertDictEqual(
+ state_dict,
+ {
+ "_compressed_summary": "Hello World!",
+ "content": [
+ [
+ {
+ "id": msg.id,
+ "name": msg.name,
+ "role": msg.role,
+ "content": msg.content,
+ "metadata": msg.metadata,
+ "timestamp": msg.timestamp,
+ },
+ [],
+ ],
+ ],
+ },
+ )
+
+ # Clear the compressed summary and verify state dict
+ await self.memory.update_compressed_summary("")
+ await self.memory.clear()
+ self.assertDictEqual(
+ self.memory.state_dict(),
+ {
+ "_compressed_summary": "",
+ "content": [],
+ },
+ )
+
+ # Load the previous state dict and verify restoration
+ self.memory.load_state_dict(state_dict)
+ self.assertDictEqual(
+ self.memory.state_dict(),
+ {
+ "_compressed_summary": "Hello World!",
+ "content": [
+ [
+ {
+ "id": msg.id,
+ "name": msg.name,
+ "role": msg.role,
+ "content": msg.content,
+ "metadata": msg.metadata,
+ "timestamp": msg.timestamp,
+ },
+ [],
+ ],
+ ],
+ },
+ )
+
class AsyncSQLAlchemyMemoryTest(ShortTermMemoryTest):
"""The SQLAlchemy short-term memory tests."""
@@ -511,6 +637,7 @@ class AsyncSQLAlchemyMemoryTest(ShortTermMemoryTest):
await self._mark_tests()
await self._multi_tenant_tests()
await self._multi_session_tests()
+ await self._test_serialization()
async def asyncTearDown(self) -> None:
"""Clean up after unittests"""
@@ -569,6 +696,7 @@ class RedisMemoryTest(ShortTermMemoryTest):
await self._test_delete_nonexistent_msg()
await self._multi_tenant_tests()
await self._multi_session_tests()
+ await self._test_serialization()
async def test_ttl(self) -> None:
"""Test the TTL functionality of the Redis memory."""
diff --git a/tests/plan_test.py b/tests/plan_test.py
index f4aa06059..12c32be6c 100644
--- a/tests/plan_test.py
+++ b/tests/plan_test.py
@@ -315,7 +315,7 @@ Subtask at index 2:
self.assertDictEqual(
state,
{
- "memory": {"content": []},
+ "memory": {"_compressed_summary": "", "content": []},
"toolkit": {"active_groups": []},
"plan_notebook": {
"storage": {