Skip to content

armgit / camel

Public Imported from GitHub
0

🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org

fix(agents): isolate response terminators when cloning ChatAgent (#4200)

Tao Sun2d5772aaffad
diff --git a/camel/agents/chat_agent.py b/camel/agents/chat_agent.py
index 533819f0d..6e57acc02 100644
--- a/camel/agents/chat_agent.py
+++ b/camel/agents/chat_agent.py
@@ -17,6 +17,7 @@ import asyncio
import atexit
import base64
import concurrent.futures
+import copy
import functools
import hashlib
import inspect
@@ -6229,7 +6230,7 @@ class ChatAgent(BaseAgent):
external_tools=[
schema for schema in self._external_tool_schemas.values()
],
- response_terminators=self.response_terminators,
+ response_terminators=copy.deepcopy(self.response_terminators),
scheduling_strategy=(
self.model_backend.scheduling_strategy.__name__
),
diff --git a/test/agents/test_chat_agent_clone.py b/test/agents/test_chat_agent_clone.py
new file mode 100644
index 000000000..51cf39fc1
--- /dev/null
+++ b/test/agents/test_chat_agent_clone.py
@@ -0,0 +1,80 @@
+# ========= Copyright 2023-2026 @ CAMEL-AI.org. All Rights Reserved. =========
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# ========= Copyright 2023-2026 @ CAMEL-AI.org. All Rights Reserved. =========
+from camel.agents import ChatAgent
+from camel.messages import BaseMessage
+from camel.models import StubModel
+from camel.terminators import ResponseWordsTerminator
+from camel.types import ModelType
+
+
+def _make_agent() -> ChatAgent:
+ return ChatAgent(
+ model=StubModel(ModelType.STUB),
+ response_terminators=[ResponseWordsTerminator(words_dict={"stop": 2})],
+ )
+
+
+def _stop_response():
+ return [
+ BaseMessage.make_assistant_message(
+ role_name="assistant",
+ content="stop",
+ )
+ ]
+
+
+def test_clone_response_terminators_have_independent_identity():
+ source = _make_agent()
+ first = source.clone()
+ second = source.clone()
+
+ assert first.response_terminators is not source.response_terminators
+ assert second.response_terminators is not source.response_terminators
+ assert first.response_terminators is not second.response_terminators
+ assert first.response_terminators[0] is not source.response_terminators[0]
+ assert second.response_terminators[0] is not source.response_terminators[0]
+ assert first.response_terminators[0] is not second.response_terminators[0]
+
+
+def test_clone_response_terminator_counts_are_independent():
+ source = _make_agent()
+ first = source.clone()
+ second = source.clone()
+
+ assert first.response_terminators[0].is_terminated(_stop_response()) == (
+ False,
+ None,
+ )
+ assert second.response_terminators[0].is_terminated(_stop_response()) == (
+ False,
+ None,
+ )
+
+
+def test_resetting_clone_does_not_reset_another_clone_terminator():
+ source = _make_agent()
+ first = source.clone()
+ second = source.clone()
+
+ assert first.response_terminators[0].is_terminated(_stop_response()) == (
+ False,
+ None,
+ )
+ second.reset()
+
+ terminated, reason = first.response_terminators[0].is_terminated(
+ _stop_response()
+ )
+ assert terminated
+ assert reason is not None