🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org
Python agentai-societiesartificial-intelligencecommunicative-aicooperative-aideep-learninglarge-language-modelsmulti-agent-systemsnatural-language-processing
fix: duplicate records for share_memory (#3271)
Wendong-Fan793e67091ba1
diff --git a/camel/societies/workforce/workforce.py b/camel/societies/workforce/workforce.pyindex 73a5e23ed..e5c468643 100644--- a/camel/societies/workforce/workforce.py+++ b/camel/societies/workforce/workforce.py@@ -295,6 +295,8 @@ class Workforce(BaseNode):self._last_snapshot_time: float = 0.0# Minimum seconds between automatic snapshotsself.snapshot_interval: float = 30.0+ # Shared memory UUID tracking to prevent re-sharing duplicates+ self._shared_memory_uuids: Set[str] = set()if self.metrics_logger:for child in self._children:worker_type = type(child).__name__@@ -651,14 +653,29 @@ class Workforce(BaseNode):)return- # Share with coordinator agent+ # Filter out already-shared records to prevent re-sharing+ # This prevents exponential growth of duplicate records+ new_records = []for record in memory_records:+ record_uuid = str(record.uuid)+ if record_uuid not in self._shared_memory_uuids:+ new_records.append(record)+ self._shared_memory_uuids.add(record_uuid)++ if not new_records:+ logger.debug(+ "No new records to share (all were already shared)"+ )+ return++ # Share with coordinator agent+ for record in new_records:# Only add records from other agents to avoid duplicationif record.agent_id != self.coordinator_agent.agent_id:self.coordinator_agent.memory.write_record(record)# Share with task agent- for record in memory_records:+ for record in new_records:if record.agent_id != self.task_agent.agent_id:self.task_agent.memory.write_record(record)@@ -670,12 +687,12 @@ class Workforce(BaseNode):]for worker in single_agent_workers:- for record in memory_records:+ for record in new_records:if record.agent_id != worker.worker.agent_id:worker.worker.memory.write_record(record)logger.info(- f"Shared {len(memory_records)} memory records across "+ f"Shared {len(new_records)} new memory records across "f"{len(single_agent_workers) + 2} agents in workforce "f"{self.node_id}")diff --git a/test/workforce/test_workflow_memory.py b/test/workforce/test_workflow_memory.pyindex bf5cb6086..b00bf409e 100644--- a/test/workforce/test_workflow_memory.py+++ b/test/workforce/test_workflow_memory.py@@ -859,3 +859,203 @@ class TestSharedContextUtility:# Should be the same as calling get_workforce_sharedshared_context = ContextUtility.get_workforce_shared()assert context_util is shared_context+++class TestWorkforceMemorySharing:+ """Test Workforce memory sharing and deduplication."""++ def test_share_memory_deduplication(self):+ """Test that Workforce prevents duplicate records from being re-shared.++ This test verifies that the Workforce layer deduplicates records based+ on UUID to prevent exponential growth of duplicate records across+ agents.+ """+ import uuid++ from camel.memories.records import MemoryRecord+ from camel.messages import BaseMessage++ # Create a workforce with share_memory enabled+ workforce = Workforce("Test Workforce", share_memory=True)++ # Create mock workers+ worker1 = MockSingleAgentWorker("worker_1")+ worker2 = MockSingleAgentWorker("worker_2")+ workforce._children = [worker1, worker2]++ # Initialize workforce agents (needed for memory operations)+ from camel.agents import ChatAgent++ sys_msg = BaseMessage.make_assistant_message(+ role_name="Coordinator", content="Test coordinator"+ )+ workforce.coordinator_agent = ChatAgent(sys_msg)+ workforce.task_agent = ChatAgent(sys_msg)++ # Create test records with UUIDs+ uuid1 = str(uuid.uuid4())+ uuid2 = str(uuid.uuid4())+ uuid3 = str(uuid.uuid4())++ msg1 = BaseMessage.make_user_message(+ role_name="user", content="Message 1"+ )+ msg2 = BaseMessage.make_user_message(+ role_name="user", content="Message 2"+ )+ msg3 = BaseMessage.make_user_message(+ role_name="user", content="Message 3"+ )++ record1 = MemoryRecord(+ message=msg1, role_at_backend="user", uuid=uuid1+ )+ record1.agent_id = "worker_1"++ record2 = MemoryRecord(+ message=msg2, role_at_backend="user", uuid=uuid2+ )+ record2.agent_id = "worker_1"++ record3 = MemoryRecord(+ message=msg3, role_at_backend="user", uuid=uuid3+ )+ record3.agent_id = "worker_2"++ # Simulate first sync cycle - 3 new records+ shared_memory_cycle1 = {+ 'coordinator': [],+ 'task_agent': [],+ 'workers': [+ record1.to_dict(),+ record2.to_dict(),+ record3.to_dict(),+ ],+ }++ workforce._share_memory_with_agents(shared_memory_cycle1)++ # Verify tracking: all 3 UUIDs should be tracked+ assert len(workforce._shared_memory_uuids) == 3+ assert uuid1 in workforce._shared_memory_uuids+ assert uuid2 in workforce._shared_memory_uuids+ assert uuid3 in workforce._shared_memory_uuids++ # Simulate second sync cycle - same records collected again (circular+ # sharing)+ shared_memory_cycle2 = {+ 'coordinator': [+ record1.to_dict(),+ record2.to_dict(),+ record3.to_dict(),+ ],+ 'task_agent': [+ record1.to_dict(),+ record2.to_dict(),+ record3.to_dict(),+ ],+ 'workers': [+ record1.to_dict(),+ record2.to_dict(),+ record3.to_dict(),+ ],+ }++ # Count coordinator memory before second sync+ coord_memory_before = len(+ workforce.coordinator_agent.memory.retrieve()+ )++ workforce._share_memory_with_agents(shared_memory_cycle2)++ # Verify no duplicates were shared (all filtered out)+ coord_memory_after = len(workforce.coordinator_agent.memory.retrieve())+ assert (+ coord_memory_after == coord_memory_before+ ) # No new records added++ # Verify UUID tracking didn't grow (still only 3 unique UUIDs)+ assert len(workforce._shared_memory_uuids) == 3++ def test_share_memory_mixed_new_and_duplicate(self):+ """Test memory sharing with a mix of new and already-shared records."""+ import uuid++ from camel.memories.records import MemoryRecord+ from camel.messages import BaseMessage++ # Create workforce with share_memory enabled+ workforce = Workforce("Test Workforce", share_memory=True)+ worker = MockSingleAgentWorker("worker_1")+ workforce._children = [worker]++ # Initialize workforce agents+ from camel.agents import ChatAgent++ sys_msg = BaseMessage.make_assistant_message(+ role_name="Coordinator", content="Test coordinator"+ )+ workforce.coordinator_agent = ChatAgent(sys_msg)+ workforce.task_agent = ChatAgent(sys_msg)++ # First sync: 2 records+ uuid1 = str(uuid.uuid4())+ uuid2 = str(uuid.uuid4())+ msg1 = BaseMessage.make_user_message(+ role_name="user", content="Message 1"+ )+ msg2 = BaseMessage.make_user_message(+ role_name="user", content="Message 2"+ )++ record1 = MemoryRecord(+ message=msg1, role_at_backend="user", uuid=uuid1+ )+ record1.agent_id = "worker_1"+ record2 = MemoryRecord(+ message=msg2, role_at_backend="user", uuid=uuid2+ )+ record2.agent_id = "worker_1"++ shared_memory1 = {+ 'coordinator': [],+ 'task_agent': [],+ 'workers': [record1.to_dict(), record2.to_dict()],+ }+ workforce._share_memory_with_agents(shared_memory1)++ assert len(workforce._shared_memory_uuids) == 2++ # Second sync: Mix of old (uuid1, uuid2) and new (uuid3)+ uuid3 = str(uuid.uuid4())+ msg3 = BaseMessage.make_user_message(+ role_name="user", content="Message 3"+ )+ record3 = MemoryRecord(+ message=msg3, role_at_backend="user", uuid=uuid3+ )+ record3.agent_id = "worker_1"++ coord_memory_before = len(+ workforce.coordinator_agent.memory.retrieve()+ )++ shared_memory2 = {+ 'coordinator': [],+ 'task_agent': [],+ 'workers': [+ record1.to_dict(), # Duplicate+ record2.to_dict(), # Duplicate+ record3.to_dict(), # NEW!+ ],+ }+ workforce._share_memory_with_agents(shared_memory2)++ # Verify only the new record was shared (uuid3)+ assert len(workforce._shared_memory_uuids) == 3+ assert uuid3 in workforce._shared_memory_uuids++ # Coordinator should only receive 1 new record (not all 3)+ coord_memory_after = len(workforce.coordinator_agent.memory.retrieve())+ assert coord_memory_after == coord_memory_before + 1