Skip to content

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

fix(agent): copy the context config before applying the deprecated buffer override (#3681)

Kiko030162fb009d221a
diff --git a/src/agentscope/agent/_agent.py b/src/agentscope/agent/_agent.py
index eabdaa455..589267b1f 100644
--- a/src/agentscope/agent/_agent.py
+++ b/src/agentscope/agent/_agent.py
@@ -186,6 +186,8 @@ class Agent:
DeprecationWarning,
stacklevel=2,
)
+ # Other agents may share the caller's config.
+ self.context_config = self.context_config.model_copy(deep=True)
self.context_config.context_buffer_ratio = (
self.injection_config.context_buffer_ratio
)
diff --git a/tests/agent_basic_test.py b/tests/agent_basic_test.py
index 61298f53f..801b49ccc 100644
--- a/tests/agent_basic_test.py
+++ b/tests/agent_basic_test.py
@@ -104,7 +104,9 @@ class MockConcurrentTool(ToolBase):
)
-class AgentBasicTest(IsolatedAsyncioTestCase):
+class AgentBasicTest( # pylint: disable=too-many-public-methods
+ IsolatedAsyncioTestCase,
+):
"""The basic test of the agent class."""
async def asyncSetUp(self) -> None:
@@ -226,6 +228,31 @@ class AgentBasicTest(IsolatedAsyncioTestCase):
self.assertEqual(agent.context_config.context_buffer_ratio, 0.3)
+ async def test_deprecated_override_does_not_mutate_the_caller_config(
+ self,
+ ) -> None:
+ """Migrating the deprecated ratio must not write through a config
+ object that another agent shares."""
+ context_config = ContextConfig()
+ with self.assertWarns(DeprecationWarning):
+ overriding = Agent(
+ name="overriding",
+ system_prompt="You are an agent.",
+ model=MockModel(),
+ context_config=context_config,
+ injection_config=InjectionConfig(context_buffer_ratio=0.5),
+ )
+ sharing = Agent(
+ name="sharing",
+ system_prompt="You are an agent.",
+ model=MockModel(),
+ context_config=context_config,
+ )
+
+ self.assertEqual(overriding.context_config.context_buffer_ratio, 0.5)
+ self.assertEqual(context_config.context_buffer_ratio, 0.2)
+ self.assertEqual(sharing.context_config.context_buffer_ratio, 0.2)
+
async def test_streaming_reasoning(self) -> None:
"""Test the streaming model inference without tool calls generated,
only text in model response.