🐫 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
enhancement: Smart Workflow Retrieval (#3291)
Hesam Sheikh03e5d7d08cc7
diff --git a/camel/societies/workforce/__init__.py b/camel/societies/workforce/__init__.pyindex 8b2f3fe99..a7f67476d 100644--- a/camel/societies/workforce/__init__.py+++ b/camel/societies/workforce/__init__.py@@ -14,10 +14,12 @@from .role_playing_worker import RolePlayingWorkerfrom .single_agent_worker import SingleAgentWorker+from .workflow_memory_manager import WorkflowSelectionMethodfrom .workforce import Workforce__all__ = ["Workforce","SingleAgentWorker","RolePlayingWorker",+ "WorkflowSelectionMethod",]diff --git a/camel/societies/workforce/single_agent_worker.py b/camel/societies/workforce/single_agent_worker.pyindex d1181346f..8ed3a2711 100644--- a/camel/societies/workforce/single_agent_worker.py+++ b/camel/societies/workforce/single_agent_worker.py@@ -15,12 +15,8 @@ from __future__ import annotationsimport asyncioimport datetime-import glob-import os-import reimport timefrom collections import deque-from pathlib import Pathfrom typing import Any, Dict, List, Optionalfrom colorama import Fore@@ -34,8 +30,11 @@ from camel.societies.workforce.structured_output_handler import ()from camel.societies.workforce.utils import TaskResultfrom camel.societies.workforce.worker import Worker+from camel.societies.workforce.workflow_memory_manager import (+ WorkflowMemoryManager,+)from camel.tasks.task import Task, TaskState, is_task_result_insufficient-from camel.utils.context_utils import ContextUtility, WorkflowSummary+from camel.utils.context_utils import ContextUtilitylogger = get_logger(__name__)@@ -254,6 +253,9 @@ class SingleAgentWorker(Worker):# from all task processingself._conversation_accumulator: Optional[ChatAgent] = None+ # workflow memory manager for handling workflow operations+ self._workflow_manager: Optional[WorkflowMemoryManager] = None+# note: context utility is set on the worker agent during save/load# operations to avoid creating session folders during initialization@@ -316,6 +318,21 @@ class SingleAgentWorker(Worker):)return self._conversation_accumulator+ def _get_workflow_manager(self) -> WorkflowMemoryManager:+ r"""Get or create the workflow memory manager."""+ if self._workflow_manager is None:+ context_util = (+ self._shared_context_utility+ if self._shared_context_utility is not None+ else None+ )+ self._workflow_manager = WorkflowMemoryManager(+ worker=self.worker,+ description=self.description,+ context_utility=context_util,+ )+ return self._workflow_manager+async def _process_task(self, task: Task, dependencies: List[Task]) -> TaskState:@@ -590,6 +607,8 @@ class SingleAgentWorker(Worker):is based on either the worker's explicit role_name or the generatedtask_title from the summary.+ Delegates to WorkflowMemoryManager for all workflow operations.+Returns:Dict[str, Any]: Result dictionary with keys:- status (str): "success" or "error"@@ -609,67 +628,24 @@ class SingleAgentWorker(Worker):DeprecationWarning,stacklevel=2,)- try:- # validate requirements- validation_error = self._validate_workflow_save_requirements()- if validation_error:- return validation_error-- # setup context utility and agent- context_util = self._get_context_utility()- self.worker.set_context_utility(context_util)-- # prepare workflow summarization components- structured_prompt = self._prepare_workflow_prompt()- agent_to_summarize = self._select_agent_for_summarization(- context_util- )- # check if we should use role_name or let summarize extract- # task_title- role_name = getattr(self.worker, 'role_name', 'assistant')- use_role_name_for_filename = role_name.lower() not in {- 'assistant',- 'agent',- 'user',- 'system',- }-- # generate and save workflow summary- # if role_name is explicit, use it for filename- # if role_name is generic, pass none to let summarize use- # task_title- filename = (- self._generate_workflow_filename()- if use_role_name_for_filename- else None- )+ manager = self._get_workflow_manager()+ result = manager.save_workflow(+ conversation_accumulator=self._conversation_accumulator+ )- result = agent_to_summarize.summarize(- filename=filename,- summary_prompt=structured_prompt,- response_format=WorkflowSummary,+ # clean up accumulator after successful save+ if (+ result.get("status") == "success"+ and self._conversation_accumulator is not None+ ):+ logger.info(+ "Cleaning up conversation accumulator after workflow "+ "summarization")+ self._conversation_accumulator = None- # add worker metadata and cleanup- result["worker_description"] = self.description- if self._conversation_accumulator is not None:- logger.info(- "Cleaning up conversation accumulator after workflow "- "summarization"- )- self._conversation_accumulator = None-- return result-- except Exception as e:- return {- "status": "error",- "summary": "",- "file_path": None,- "worker_description": self.description,- "message": f"Failed to save workflow memories: {e!s}",- }+ return resultasync def save_workflow_memories_async(self) -> Dict[str, Any]:r"""Asynchronously save the worker's current workflow memories using@@ -679,6 +655,8 @@ class SingleAgentWorker(Worker):asummarize() for non-blocking LLM calls, enabling parallelsummarization of multiple workers.+ Delegates to WorkflowMemoryManager for all workflow operations.+Returns:Dict[str, Any]: Result dictionary with keys:- status (str): "success" or "error"@@ -686,322 +664,62 @@ class SingleAgentWorker(Worker):- file_path (str): Path to saved file- worker_description (str): Worker description used"""- try:- # validate requirements- validation_error = self._validate_workflow_save_requirements()- if validation_error:- return validation_error-- # setup context utility and agent- context_util = self._get_context_utility()- self.worker.set_context_utility(context_util)-- # prepare workflow summarization components- structured_prompt = self._prepare_workflow_prompt()- agent_to_summarize = self._select_agent_for_summarization(- context_util- )-- # check if we should use role_name or let summarize extract- # task_title- role_name = getattr(self.worker, 'role_name', 'assistant')- use_role_name_for_filename = role_name.lower() not in {- 'assistant',- 'agent',- 'user',- 'system',- }-- # generate and save workflow summary- # if role_name is explicit, use it for filename- # if role_name is generic, pass none to let summarize use- # task_title- filename = (- self._generate_workflow_filename()- if use_role_name_for_filename- else None- )+ manager = self._get_workflow_manager()+ result = await manager.save_workflow_async(+ conversation_accumulator=self._conversation_accumulator+ )- # **KEY CHANGE**: Using asummarize() instead of summarize()- result = await agent_to_summarize.asummarize(- filename=filename,- summary_prompt=structured_prompt,- response_format=WorkflowSummary,+ # clean up accumulator after successful save+ if (+ result.get("status") == "success"+ and self._conversation_accumulator is not None+ ):+ logger.info(+ "Cleaning up conversation accumulator after workflow "+ "summarization")+ self._conversation_accumulator = None- # add worker metadata and cleanup- result["worker_description"] = self.description- if self._conversation_accumulator is not None:- logger.info(- "Cleaning up conversation accumulator after workflow "- "summarization"- )- self._conversation_accumulator = None-- return result-- except Exception as e:- return {- "status": "error",- "summary": "",- "file_path": None,- "worker_description": self.description,- "message": f"Failed to save workflow memories: {e!s}",- }+ return resultdef load_workflow_memories(self,pattern: Optional[str] = None,- max_files_to_load: int = 3,+ max_workflows: int = 3,session_id: Optional[str] = None,+ use_smart_selection: bool = True,) -> bool:- r"""Load workflow memories matching worker description- from saved files.+ r"""Load workflow memories using intelligent agent-based selection.++ This method uses the worker agent to intelligently select the most+ relevant workflows based on metadata (title, description, tags)+ rather than simple filename pattern matching.- This method searches for workflow memory files that match the worker's- description and loads them into the agent's memory using- ContextUtility.+ Delegates to WorkflowMemoryManager for all workflow operations.Args:- pattern (Optional[str]): Custom search pattern for workflow- memory files.- If None, uses worker description to generate pattern.- max_files_to_load (int): Maximum number of workflow files to load.+ pattern (Optional[str]): Legacy parameter for backward+ compatibility. When use_smart_selection=False, uses this+ pattern for file matching. Ignored when smart selection+ is enabled.+ max_workflows (int): Maximum number of workflow files to load.(default: :obj:`3`)session_id (Optional[str]): Specific workforce session ID to loadfrom. If None, searches across all sessions.(default: :obj:`None`)+ use_smart_selection (bool): Whether to use agent-based intelligent+ workflow selection. When True, uses metadata and LLM to select+ most relevant workflows. When False, falls back to pattern+ matching. (default: :obj:`True`)Returns:bool: True if workflow memories were successfully loaded, Falseotherwise."""- try:- # reset system message to original state before loading- # this prevents duplicate workflow context on multiple calls- if isinstance(self.worker, ChatAgent):- self.worker.reset_to_original_system_message()-- # Find workflow memory files matching the pattern- workflow_files = self._find_workflow_files(pattern, session_id)- if not workflow_files:- return False-- # Load the workflow memory files- loaded_count = self._load_workflow_files(- workflow_files, max_files_to_load- )-- # Report results- logger.info(- f"Successfully loaded {loaded_count} workflow file(s) for "- f"{self.description}"- )- return loaded_count > 0-- except Exception as e:- logger.warning(- f"Error loading workflow memories for {self.description}: "- f"{e!s}"- )- return False-- def _find_workflow_files(- self, pattern: Optional[str], session_id: Optional[str] = None- ) -> List[str]:- r"""Find and return sorted workflow files matching the pattern.-- Args:- pattern (Optional[str]): Custom search pattern for workflow files.- If None, uses worker description to generate pattern.- session_id (Optional[str]): Specific session ID to search in.- If None, searches across all sessions.-- Returns:- List[str]: Sorted list of workflow file paths (empty if- validation fails).- """- # Ensure we have a ChatAgent worker- if not isinstance(self.worker, ChatAgent):- logger.warning(- f"Cannot load workflow: {self.description} worker is not "- "a ChatAgent"- )- return []-- # generate filename-safe search pattern from worker role name- if pattern is None:- from camel.utils.context_utils import ContextUtility-- # get role_name (always available, defaults to "assistant")- role_name = getattr(self.worker, 'role_name', 'assistant')- clean_name = ContextUtility.sanitize_workflow_filename(role_name)-- # check if role_name is generic- generic_names = {'assistant', 'agent', 'user', 'system'}- if clean_name in generic_names:- # for generic role names, search for all workflow files- # since filename is based on task_title- pattern = "*_workflow*.md"- else:- # for explicit role names, search for role-specific files- pattern = f"{clean_name}_workflow*.md"-- # Get the base workforce_workflows directory- camel_workdir = os.environ.get("CAMEL_WORKDIR")- if camel_workdir:- base_dir = os.path.join(camel_workdir, "workforce_workflows")- else:- base_dir = "workforce_workflows"-- # search for workflow files in specified or all session directories- if session_id:- search_path = str(Path(base_dir) / session_id / pattern)- else:- # search across all session directories using wildcard pattern- search_path = str(Path(base_dir) / "*" / pattern)- workflow_files = glob.glob(search_path)-- if not workflow_files:- logger.info(f"No workflow files found for pattern: {pattern}")- return []-- # prioritize most recent sessions by session timestamp in- # directory name- def extract_session_timestamp(filepath: str) -> str:- match = re.search(r'session_(\d{8}_\d{6}_\d{6})', filepath)- return match.group(1) if match else ""-- workflow_files.sort(key=extract_session_timestamp, reverse=True)- return workflow_files-- def _load_workflow_files(- self, workflow_files: List[str], max_files_to_load: int- ) -> int:- r"""Load workflow files and return count of successful loads.-- Args:- workflow_files (List[str]): List of workflow file paths to load.-- Returns:- int: Number of successfully loaded workflow files.- """- loaded_count = 0- # limit loading to prevent context overflow- for file_path in workflow_files[:max_files_to_load]:- try:- # extract file and session info from full path- filename = os.path.basename(file_path).replace('.md', '')- session_dir = os.path.dirname(file_path)- session_id = os.path.basename(session_dir)-- # create context utility for the specific session- # where file exists- temp_utility = ContextUtility.get_workforce_shared(session_id)-- status = temp_utility.load_markdown_context_to_memory(- self.worker, filename- )-- if "Context appended" in status:- loaded_count += 1- logger.info(f"Loaded workflow: {filename}")- else:- logger.warning(- f"Failed to load workflow {filename}: {status}"- )-- except Exception as e:- logger.warning(- f"Failed to load workflow file {file_path}: {e!s}"- )- continue-- return loaded_count-- def _validate_workflow_save_requirements(self) -> Optional[Dict[str, Any]]:- r"""Validate requirements for workflow saving.-- Returns:- Optional[Dict[str, Any]]: Error result dict if validation fails,- None if validation passes.- """- if not isinstance(self.worker, ChatAgent):- return {- "status": "error",- "summary": "",- "file_path": None,- "worker_description": self.description,- "message": (- "Worker must be a ChatAgent instance to save workflow "- "memories"- ),- }- return None-- def _generate_workflow_filename(self) -> str:- r"""Generate a filename for the workflow based on worker role name.-- Uses the worker's explicit role_name when available.-- Returns:- str: Sanitized filename without timestamp and without .md- extension. Format: {role_name}_workflow- """- from camel.utils.context_utils import ContextUtility-- # get role_name (always available, defaults to "assistant"/"Assistant")- role_name = getattr(self.worker, 'role_name', 'assistant')- clean_name = ContextUtility.sanitize_workflow_filename(role_name)-- return f"{clean_name}_workflow"-- def _prepare_workflow_prompt(self) -> str:- r"""Prepare the structured prompt for workflow summarization.-- Returns:- str: Structured prompt for workflow summary.- """- workflow_prompt = WorkflowSummary.get_instruction_prompt()- return StructuredOutputHandler.generate_structured_prompt(- base_prompt=workflow_prompt, schema=WorkflowSummary+ manager = self._get_workflow_manager()+ return manager.load_workflows(+ pattern=pattern,+ max_files_to_load=max_workflows,+ session_id=session_id,+ use_smart_selection=use_smart_selection,)-- def _select_agent_for_summarization(- self, context_util: ContextUtility- ) -> ChatAgent:- r"""Select the best agent for workflow summarization.-- Args:- context_util: Context utility to set on selected agent.-- Returns:- ChatAgent: Agent to use for summarization.- """- agent_to_summarize = self.worker-- if self._conversation_accumulator is not None:- accumulator_messages, _ = (- self._conversation_accumulator.memory.get_context()- )- if accumulator_messages:- self._conversation_accumulator.set_context_utility(- context_util- )- agent_to_summarize = self._conversation_accumulator- logger.info(- f"Using conversation accumulator with "- f"{len(accumulator_messages)} messages for workflow "- f"summary"- )- else:- logger.info(- "Using original worker for workflow summary (no "- "accumulated conversations)"- )- else:- logger.info(- "Using original worker for workflow summary (no accumulator)"- )-- return agent_to_summarizediff --git a/camel/societies/workforce/workflow_memory_manager.py b/camel/societies/workforce/workflow_memory_manager.pynew file mode 100644index 000000000..0212ae136--- /dev/null+++ b/camel/societies/workforce/workflow_memory_manager.py@@ -0,0 +1,772 @@+# ========= Copyright 2023-2024 @ 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-2024 @ CAMEL-AI.org. All Rights Reserved. =========++import glob+import os+import re+from enum import Enum+from pathlib import Path+from typing import Any, Dict, List, Optional++from camel.agents import ChatAgent+from camel.logger import get_logger+from camel.societies.workforce.structured_output_handler import (+ StructuredOutputHandler,+)+from camel.types import OpenAIBackendRole+from camel.utils.context_utils import ContextUtility, WorkflowSummary++logger = get_logger(__name__)+++class WorkflowSelectionMethod(Enum):+ r"""Enum representing the method used to select workflows.++ Attributes:+ AGENT_SELECTED: Agent-based intelligent selection using metadata.+ ROLE_NAME_MATCH: Pattern matching by role_name.+ MOST_RECENT: Fallback to most recent workflows.+ ALL_AVAILABLE: Returned all workflows (fewer than max requested).+ NONE: No workflows available.+ """++ AGENT_SELECTED = "agent_selected"+ ROLE_NAME_MATCH = "role_name_match"+ MOST_RECENT = "most_recent"+ ALL_AVAILABLE = "all_available"+ NONE = "none"+++class WorkflowMemoryManager:+ r"""Manages workflow memory operations for workforce workers.++ This class encapsulates all workflow memory functionality including+ intelligent loading, saving, and selection of workflows. It separates+ workflow management concerns from the core worker task processing logic.++ Args:+ worker (ChatAgent): The worker agent that will use workflows.+ description (str): Description of the worker's role.+ context_utility (Optional[ContextUtility]): Shared context utility+ for workflow operations. If None, creates a new instance.+ """++ def __init__(+ self,+ worker: ChatAgent,+ description: str,+ context_utility: Optional[ContextUtility] = None,+ ):+ # validate worker type at initialization+ if not isinstance(worker, ChatAgent):+ raise TypeError(+ f"Worker must be a ChatAgent instance, "+ f"got {type(worker).__name__}"+ )++ self.worker = worker+ self.description = description+ self._context_utility = context_utility++ def _get_context_utility(self) -> ContextUtility:+ r"""Get context utility with lazy initialization."""+ if self._context_utility is None:+ self._context_utility = ContextUtility.get_workforce_shared()+ return self._context_utility++ def load_workflows(+ self,+ pattern: Optional[str] = None,+ max_files_to_load: int = 3,+ session_id: Optional[str] = None,+ use_smart_selection: bool = True,+ ) -> bool:+ r"""Load workflow memories using intelligent agent-based selection.++ This method uses the worker agent to intelligently select the most+ relevant workflows based on workflow information (title, description,+ tags) rather than simple filename pattern matching.++ Args:+ pattern (Optional[str]): Legacy parameter for backward+ compatibility. When use_smart_selection=False, uses this+ pattern for file matching. Ignored when smart selection+ is enabled.+ max_files_to_load (int): Maximum number of workflow files to load.+ (default: :obj:`3`)+ session_id (Optional[str]): Specific workforce session ID to load+ from. If None, searches across all sessions.+ (default: :obj:`None`)+ use_smart_selection (bool): Whether to use agent-based+ intelligent workflow selection. When True, uses workflow+ information and LLM to select most relevant workflows. When+ False, falls back to pattern matching. (default: :obj:`True`)++ Returns:+ bool: True if workflow memories were successfully loaded, False+ otherwise.+ """+ try:+ # reset system message to original state before loading+ # this prevents duplicate workflow context on multiple calls+ self.worker.reset_to_original_system_message()++ # determine which selection method to use+ if use_smart_selection:+ # smart selection: use workflow information and agent+ # intelligence+ context_util = self._get_context_utility()+ workflows_metadata = context_util.get_all_workflows_info(+ session_id+ )++ if not workflows_metadata:+ logger.info("No workflow files found")+ return False++ # use agent to select most relevant workflows+ selected_files, selection_method = (+ self._select_relevant_workflows(+ workflows_metadata, max_files_to_load, session_id+ )+ )++ if not selected_files:+ logger.info(+ f"No workflows selected "+ f"(method: {selection_method.value})"+ )+ return False++ # log selection method used+ logger.info(+ f"Workflow selection method: {selection_method.value}"+ )++ # load selected workflows+ loaded_count = self._load_workflow_files(+ selected_files, max_files_to_load+ )++ else:+ # legacy pattern matching approach+ workflow_files = self._find_workflow_files(pattern, session_id)+ if not workflow_files:+ return False++ loaded_count = self._load_workflow_files(+ workflow_files, max_files_to_load+ )++ # report results+ if loaded_count > 0:+ logger.info(+ f"Successfully loaded {loaded_count} workflow file(s) for "+ f"{self.description}"+ )+ return loaded_count > 0++ except Exception as e:+ logger.warning(+ f"Error loading workflow memories for {self.description}: "+ f"{e!s}"+ )+ return False++ def save_workflow(+ self, conversation_accumulator: Optional[ChatAgent] = None+ ) -> Dict[str, Any]:+ r"""Save the worker's current workflow memories using agent+ summarization.++ This method generates a workflow summary from the worker agent's+ conversation history and saves it to a markdown file.++ Args:+ conversation_accumulator (Optional[ChatAgent]): Optional+ accumulator agent with collected conversations. If provided,+ uses this instead of the main worker agent.++ Returns:+ Dict[str, Any]: Result dictionary with keys:+ - status (str): "success" or "error"+ - summary (str): Generated workflow summary+ - file_path (str): Path to saved file+ - worker_description (str): Worker description used+ """+ try:+ # setup context utility and agent+ context_util = self._get_context_utility()+ self.worker.set_context_utility(context_util)++ # prepare workflow summarization components+ structured_prompt = self._prepare_workflow_prompt()++ # check if we should use role_name or let summarize extract+ # task_title+ clean_name = self._get_sanitized_role_name()+ use_role_name_for_filename = clean_name not in {+ 'assistant',+ 'agent',+ 'user',+ 'system',+ }++ # if role_name is explicit, use it for filename+ # if role_name is generic, pass none to let summarize use+ # task_title+ filename = (+ self._generate_workflow_filename()+ if use_role_name_for_filename+ else None+ )++ # select agent for summarization+ agent_to_summarize = self.worker+ if conversation_accumulator is not None:+ accumulator_messages, _ = (+ conversation_accumulator.memory.get_context()+ )+ if accumulator_messages:+ conversation_accumulator.set_context_utility(context_util)+ agent_to_summarize = conversation_accumulator+ logger.info(+ f"Using conversation accumulator with "+ f"{len(accumulator_messages)} messages for workflow "+ f"summary"+ )++ # generate and save workflow summary+ result = agent_to_summarize.summarize(+ filename=filename,+ summary_prompt=structured_prompt,+ response_format=WorkflowSummary,+ )++ # add worker metadata+ result["worker_description"] = self.description+ return result++ except Exception as e:+ return {+ "status": "error",+ "summary": "",+ "file_path": None,+ "worker_description": self.description,+ "message": f"Failed to save workflow memories: {e!s}",+ }++ async def save_workflow_async(+ self, conversation_accumulator: Optional[ChatAgent] = None+ ) -> Dict[str, Any]:+ r"""Asynchronously save the worker's current workflow memories using+ agent summarization.++ This is the async version of save_workflow() that uses asummarize() for+ non-blocking LLM calls, enabling parallel summarization of multiple+ workers.++ Args:+ conversation_accumulator (Optional[ChatAgent]): Optional+ accumulator agent with collected conversations. If provided,+ uses this instead of the main worker agent.++ Returns:+ Dict[str, Any]: Result dictionary with keys:+ - status (str): "success" or "error"+ - summary (str): Generated workflow summary+ - file_path (str): Path to saved file+ - worker_description (str): Worker description used+ """+ try:+ # setup context utility and agent+ context_util = self._get_context_utility()+ self.worker.set_context_utility(context_util)++ # prepare workflow summarization components+ structured_prompt = self._prepare_workflow_prompt()++ # select agent for summarization+ agent_to_summarize = self.worker+ if conversation_accumulator is not None:+ accumulator_messages, _ = (+ conversation_accumulator.memory.get_context()+ )+ if accumulator_messages:+ conversation_accumulator.set_context_utility(context_util)+ agent_to_summarize = conversation_accumulator+ logger.info(+ f"Using conversation accumulator with "+ f"{len(accumulator_messages)} messages for workflow "+ f"summary"+ )++ # check if we should use role_name or let asummarize extract+ # task_title+ clean_name = self._get_sanitized_role_name()+ use_role_name_for_filename = clean_name not in {+ 'assistant',+ 'agent',+ 'user',+ 'system',+ }++ # generate and save workflow summary+ # if role_name is explicit, use it for filename+ # if role_name is generic, pass none to let asummarize use+ # task_title+ filename = (+ self._generate_workflow_filename()+ if use_role_name_for_filename+ else None+ )++ # **KEY CHANGE**: Using asummarize() instead of summarize()+ result = await agent_to_summarize.asummarize(+ filename=filename,+ summary_prompt=structured_prompt,+ response_format=WorkflowSummary,+ )++ # add worker metadata+ result["worker_description"] = self.description+ return result++ except Exception as e:+ return {+ "status": "error",+ "summary": "",+ "file_path": None,+ "worker_description": self.description,+ "message": f"Failed to save workflow memories: {e!s}",+ }++ def _select_relevant_workflows(+ self,+ workflows_metadata: List[Dict[str, Any]],+ max_files: int,+ session_id: Optional[str] = None,+ ) -> tuple[List[str], WorkflowSelectionMethod]:+ r"""Use worker agent to select most relevant workflows.++ This method creates a prompt with all available workflow information+ and uses the worker agent to intelligently select the most relevant+ workflows based on the worker's role and description.++ Args:+ workflows_metadata (List[Dict[str, Any]]): List of workflow+ information dicts (contains title, description, tags,+ file_path).+ max_files (int): Maximum number of workflows to select.+ session_id (Optional[str]): Specific workforce session ID to+ search in for fallback pattern matching. If None, searches+ across all sessions. (default: :obj:`None`)++ Returns:+ tuple[List[str], WorkflowSelectionMethod]: Tuple of (selected+ workflow file paths, selection method used).+ """+ if not workflows_metadata:+ return [], WorkflowSelectionMethod.NONE++ # format workflows for selection+ workflows_str = self._format_workflows_for_selection(+ workflows_metadata+ )++ # create selection prompt+ selection_prompt = (+ f"You are a {self.description}. "+ f"Review the following {len(workflows_metadata)} available "+ f"workflow memories and select the {max_files} most relevant "+ f"ones for your current role. Consider:\n"+ f"1. Task similarity to your role\n"+ f"2. Domain relevance\n"+ f"3. Tool and capability overlap\n\n"+ f"Available workflows:\n{workflows_str}\n\n"+ f"Respond with ONLY the workflow numbers you selected "+ f"(e.g., '1, 3, 5'), separated by commas. "+ f"Select exactly {max_files} workflows."+ )++ try:+ # use worker agent for selection+ from camel.messages import BaseMessage++ selection_msg = BaseMessage.make_user_message(+ role_name="user", content=selection_prompt+ )++ response = self.worker.step(selection_msg)++ # parse response to extract workflow numbers+ numbers_str = response.msgs[0].content+ numbers = re.findall(r'\d+', numbers_str)+ selected_indices = [int(n) - 1 for n in numbers[:max_files]]++ # validate indices and get file paths+ selected_paths = []+ for idx in selected_indices:+ if 0 <= idx < len(workflows_metadata):+ selected_paths.append(workflows_metadata[idx]['file_path'])+ else:+ logger.warning(+ f"Agent selected invalid workflow index {idx + 1}, "+ f"only {len(workflows_metadata)} workflows available"+ )++ if selected_paths:+ logger.info(+ f"Agent selected {len(selected_paths)} workflow(s) for "+ f"{self.description}"+ )+ return selected_paths, WorkflowSelectionMethod.AGENT_SELECTED++ # agent returned empty results+ logger.warning(+ "Agent selection returned no valid workflows, "+ "falling back to role-based pattern matching"+ )++ except Exception as e:+ logger.warning(+ f"Error during agent selection: {e!s}. "+ f"Falling back to role-based pattern matching"+ )++ finally:+ # clean up selection conversation from memory to prevent+ # pollution. this runs whether selection succeeded, failed,+ # or raised exception+ self.worker.memory.clear()+ if self.worker._system_message is not None:+ self.worker.update_memory(+ self.worker._system_message, OpenAIBackendRole.SYSTEM+ )++ # fallback: try pattern matching by role_name+ pattern_matched_files = self._find_workflow_files(+ pattern=None, session_id=session_id+ )+ if pattern_matched_files:+ return (+ pattern_matched_files[:max_files],+ WorkflowSelectionMethod.ROLE_NAME_MATCH,+ )++ # last resort: return most recent workflows+ logger.info(+ "No role-matched workflows found, using most recent workflows"+ )+ return (+ [wf['file_path'] for wf in workflows_metadata[:max_files]],+ WorkflowSelectionMethod.MOST_RECENT,+ )++ def _format_workflows_for_selection(+ self, workflows_metadata: List[Dict[str, Any]]+ ) -> str:+ r"""Format workflow information into a readable prompt for selection.++ Args:+ workflows_metadata (List[Dict[str, Any]]): List of workflow+ information dicts (contains title, description, tags,+ file_path).++ Returns:+ str: Formatted string presenting workflows for LLM selection.+ """+ if not workflows_metadata:+ return "No workflows available."++ formatted_lines = []+ for i, workflow in enumerate(workflows_metadata, 1):+ formatted_lines.append(f"\nWorkflow {i}:")+ formatted_lines.append(f"- Title: {workflow.get('title', 'N/A')}")+ formatted_lines.append(+ f"- Description: {workflow.get('description', 'N/A')}"+ )+ tags = workflow.get('tags', [])+ tags_str = ', '.join(tags) if tags else 'No tags'+ formatted_lines.append(f"- Tags: {tags_str}")+ formatted_lines.append(+ f"- File: {workflow.get('file_path', 'N/A')}"+ )++ return '\n'.join(formatted_lines)++ def _find_workflow_files(+ self, pattern: Optional[str], session_id: Optional[str] = None+ ) -> List[str]:+ r"""Find and return sorted workflow files matching the pattern.++ Args:+ pattern (Optional[str]): Custom search pattern for workflow files.+ If None, uses worker role_name to generate pattern.+ session_id (Optional[str]): Specific session ID to search in.+ If None, searches across all sessions.++ Returns:+ List[str]: Sorted list of workflow file paths (empty if+ validation fails).+ """+ # generate filename-safe search pattern from worker role name+ if pattern is None:+ # get sanitized role name+ clean_name = self._get_sanitized_role_name()++ # check if role_name is generic+ generic_names = {'assistant', 'agent', 'user', 'system'}+ if clean_name in generic_names:+ # for generic role names, search for all workflow files+ # since filename is based on task_title+ pattern = "*_workflow*.md"+ else:+ # for explicit role names, search for role-specific files+ pattern = f"{clean_name}_workflow*.md"++ # get the base workforce_workflows directory+ camel_workdir = os.environ.get("CAMEL_WORKDIR")+ if camel_workdir:+ base_dir = os.path.join(camel_workdir, "workforce_workflows")+ else:+ base_dir = "workforce_workflows"++ # search for workflow files in specified or all session directories+ if session_id:+ search_path = str(Path(base_dir) / session_id / pattern)+ else:+ # search across all session directories using wildcard pattern+ search_path = str(Path(base_dir) / "*" / pattern)+ workflow_files = glob.glob(search_path)++ if not workflow_files:+ logger.info(f"No workflow files found for pattern: {pattern}")+ return []++ # prioritize most recent sessions by session timestamp in+ # directory name+ def extract_session_timestamp(filepath: str) -> str:+ match = re.search(r'session_(\d{8}_\d{6}_\d{6})', filepath)+ return match.group(1) if match else ""++ workflow_files.sort(key=extract_session_timestamp, reverse=True)+ return workflow_files++ def _collect_workflow_contents(+ self, workflow_files: List[str]+ ) -> List[Dict[str, str]]:+ r"""Collect and load workflow file contents.++ Args:+ workflow_files (List[str]): List of workflow file paths to load.++ Returns:+ List[Dict[str, str]]: List of dicts with 'filename' and+ 'content' keys.+ """+ workflows_to_load = []+ for file_path in workflow_files:+ try:+ # extract file and session info from full path+ filename = os.path.basename(file_path).replace('.md', '')+ session_dir = os.path.dirname(file_path)+ session_id = os.path.basename(session_dir)++ # create context utility for the specific session+ temp_utility = ContextUtility.get_workforce_shared(session_id)++ # load the workflow content+ content = temp_utility.load_markdown_file(filename)++ if content and content.strip():+ # filter out metadata section+ content = temp_utility._filter_metadata_from_content(+ content+ )+ workflows_to_load.append(+ {'filename': filename, 'content': content}+ )+ logger.info(f"Loaded workflow content: {filename}")+ else:+ logger.warning(+ f"Workflow file empty or not found: {filename}"+ )++ except Exception as e:+ logger.warning(+ f"Failed to load workflow file {file_path}: {e!s}"+ )+ continue++ return workflows_to_load++ def _format_workflows_for_context(+ self, workflows_to_load: List[Dict[str, str]]+ ) -> str:+ r"""Format workflows into a context string for the agent.++ Args:+ workflows_to_load (List[Dict[str, str]]): List of workflow+ dicts with 'filename' and 'content' keys.++ Returns:+ str: Formatted workflow context string with header and all+ workflows.+ """+ # create single header for all workflows+ if len(workflows_to_load) == 1:+ prefix_prompt = (+ "The following is the context from a previous "+ "session or workflow which might be useful for "+ "the current task. This information might help you "+ "understand the background, choose which tools to use, "+ "and plan your next steps."+ )+ else:+ prefix_prompt = (+ f"The following are {len(workflows_to_load)} previous "+ "workflows which might be useful for "+ "the current task. These workflows provide context about "+ "similar tasks, tools used, and approaches taken. "+ "Review them to understand patterns and make informed "+ "decisions for your current task."+ )++ # combine all workflows into single content block+ combined_content = f"\n\n--- Previous Workflows ---\n{prefix_prompt}\n"++ for i, workflow_data in enumerate(workflows_to_load, 1):+ combined_content += (+ f"\n\n{'=' * 60}\n"+ f"Workflow {i}: {workflow_data['filename']}\n"+ f"{'=' * 60}\n\n"+ f"{workflow_data['content']}"+ )++ return combined_content++ def _add_workflows_to_system_message(self, workflow_context: str) -> bool:+ r"""Add workflow context to agent's system message.++ Args:+ workflow_context (str): The formatted workflow context to add.++ Returns:+ bool: True if successful, False otherwise.+ """+ # check if agent has a system message+ if self.worker._original_system_message is None:+ logger.error(+ f"Agent {self.worker.agent_id} has no system message. "+ "Cannot append workflow memories."+ )+ return False++ # update the current system message+ current_system_message = self.worker._system_message+ if current_system_message is not None:+ new_sys_content = current_system_message.content + workflow_context+ self.worker._system_message = (+ current_system_message.create_new_instance(new_sys_content)+ )++ # replace the system message in memory+ self.worker.memory.clear()+ self.worker.update_memory(+ self.worker._system_message, OpenAIBackendRole.SYSTEM+ )++ return True++ def _load_workflow_files(+ self, workflow_files: List[str], max_workflows: int+ ) -> int:+ r"""Load workflow files and return count of successful loads.++ Loads all workflows together with a single header to avoid repetition.++ Args:+ workflow_files (List[str]): List of workflow file paths to load.+ max_workflows (int): Maximum number of workflows to load.++ Returns:+ int: Number of successfully loaded workflow files.+ """+ if not workflow_files:+ return 0++ # collect workflow contents from files+ workflows_to_load = self._collect_workflow_contents(+ workflow_files[:max_workflows]+ )++ if not workflows_to_load:+ return 0++ # format workflows into context string+ try:+ workflow_context = self._format_workflows_for_context(+ workflows_to_load+ )++ # add workflow context to agent's system message+ if not self._add_workflows_to_system_message(workflow_context):+ return 0++ char_count = len(workflow_context)+ logger.info(+ f"Appended {len(workflows_to_load)} workflow(s) to agent "+ f"{self.worker.agent_id} ({char_count} characters)"+ )++ return len(workflows_to_load)++ except Exception as e:+ logger.error(+ f"Failed to append workflows to system message: {e!s}"+ )+ return 0++ def _get_sanitized_role_name(self) -> str:+ r"""Get the sanitized role name for the worker.++ Returns:+ str: Sanitized role name suitable for use in filenames.+ """+ role_name = getattr(self.worker, 'role_name', 'assistant')+ return ContextUtility.sanitize_workflow_filename(role_name)++ def _generate_workflow_filename(self) -> str:+ r"""Generate a filename for the workflow based on worker role name.++ Uses the worker's explicit role_name when available.++ Returns:+ str: Sanitized filename without timestamp and without .md+ extension. Format: {role_name}_workflow+ """+ clean_name = self._get_sanitized_role_name()+ return f"{clean_name}_workflow"++ def _prepare_workflow_prompt(self) -> str:+ r"""Prepare the structured prompt for workflow summarization.++ Returns:+ str: Structured prompt for workflow summary.+ """+ workflow_prompt = WorkflowSummary.get_instruction_prompt()+ return StructuredOutputHandler.generate_structured_prompt(+ base_prompt=workflow_prompt, schema=WorkflowSummary+ )diff --git a/camel/societies/workforce/workforce.py b/camel/societies/workforce/workforce.pyindex e9787b7d8..93d16ca28 100644--- a/camel/societies/workforce/workforce.py+++ b/camel/societies/workforce/workforce.py@@ -2439,8 +2439,10 @@ class Workforce(BaseNode):def load_workflow_memories(self,- max_files_to_load: int = 3,session_id: Optional[str] = None,+ worker_max_workflows: int = 3,+ coordinator_max_workflows: int = 5,+ task_agent_max_workflows: int = 3,) -> Dict[str, bool]:r"""Load workflow memories for all SingleAgentWorker instances in theworkforce.@@ -2451,11 +2453,15 @@ class Workforce(BaseNode):method. Workers match files based on their description names.Args:- max_files_to_load (int): Maximum number of workflow files to load- per worker. (default: :obj:`3`)session_id (Optional[str]): Specific workforce session ID to loadfrom. If None, searches across all sessions.(default: :obj:`None`)+ worker_max_workflows (int): Maximum number of workflow files to+ load per worker agent. (default: :obj:`3`)+ coordinator_max_workflows (int): Maximum number of workflow files+ to load for the coordinator agent. (default: :obj:`5`)+ task_agent_max_workflows (int): Maximum number of workflow files+ to load for the task planning agent. (default: :obj:`3`)Returns:Dict[str, bool]: Dictionary mapping worker node IDs to load@@ -2467,7 +2473,11 @@ class Workforce(BaseNode):>>> workforce.add_single_agent_worker(... "data_analyst", analyst_agent... )- >>> success_status = workforce.load_workflows()+ >>> success_status = workforce.load_workflow_memories(+ ... worker_max_workflows=5,+ ... coordinator_max_workflows=10,+ ... task_agent_max_workflows=5+ ... )>>> print(success_status){'worker_123': True} # Successfully loaded workflows for# data_analyst@@ -2485,7 +2495,7 @@ class Workforce(BaseNode):# For loading, don't set shared context utility# Let each worker search across existing sessionssuccess = child.load_workflow_memories(- max_files_to_load=max_files_to_load,+ max_workflows=worker_max_workflows,session_id=session_id,)results[child.node_id] = success@@ -2500,13 +2510,18 @@ class Workforce(BaseNode):results[child.node_id] = False# Load aggregated workflow summaries for coordinator and task agents- self._load_management_agent_workflows(max_files_to_load, session_id)+ self._load_management_agent_workflows(+ coordinator_max_workflows, task_agent_max_workflows, session_id+ )logger.info(f"Workflow load completed for {len(results)} workers")return resultsdef _load_management_agent_workflows(- self, max_files_to_load: int, session_id: Optional[str] = None+ self,+ coordinator_max_workflows: int,+ task_agent_max_workflows: int,+ session_id: Optional[str] = None,) -> None:r"""Load workflow summaries for coordinator and task planning agents.@@ -2517,7 +2532,10 @@ class Workforce(BaseNode):successful strategiesArgs:- max_files_to_load (int): Maximum number of workflow files to load.+ coordinator_max_workflows (int): Maximum number of workflow files+ to load for the coordinator agent.+ task_agent_max_workflows (int): Maximum number of workflow files+ to load for the task planning agent.session_id (Optional[str]): Specific session ID to load from.If None, searches across all sessions."""@@ -2555,9 +2573,9 @@ class Workforce(BaseNode):key=lambda x: os.path.getmtime(x), reverse=True)- # Load workflows for coordinator agent (up to 5 most recent)+ # Load workflows for coordinator agentcoordinator_loaded = 0- for file_path in workflow_files[:max_files_to_load]:+ for file_path in workflow_files[:coordinator_max_workflows]:try:filename = os.path.basename(file_path).replace('.md', '')session_dir = os.path.dirname(file_path)@@ -2578,9 +2596,9 @@ class Workforce(BaseNode):f"Failed to load coordinator workflow {file_path}: {e}")- # Load workflows for task agent (up to 3 most recent)+ # Load workflows for task agenttask_agent_loaded = 0- for file_path in workflow_files[:max_files_to_load]:+ for file_path in workflow_files[:task_agent_max_workflows]:try:filename = os.path.basename(file_path).replace('.md', '')session_dir = os.path.dirname(file_path)diff --git a/camel/utils/context_utils.py b/camel/utils/context_utils.pyindex 3c1bb5ca5..801298abc 100644--- a/camel/utils/context_utils.py+++ b/camel/utils/context_utils.py@@ -91,6 +91,17 @@ class WorkflowSummary(BaseModel):"mid-task by using the HumanToolkit.",default="",)+ tags: List[str] = Field(+ description="3-10 categorization tags that describe the workflow "+ "type, domain, and key capabilities. Use lowercase with hyphens. "+ "Tags should be broad, reusable categories to help with semantic "+ "matching to similar tasks. "+ "Examples: 'data-analysis', 'web-scraping', 'api-integration', "+ "'code-generation', 'file-processing', 'database-query', "+ "'text-processing', 'image-manipulation', 'email-automation', "+ "'report-generation'.",+ default_factory=list,+ )@classmethoddef get_instruction_prompt(cls) -> str:@@ -112,7 +123,12 @@ class WorkflowSummary(BaseModel):'about a simple math problem, the workflow must be short, ''e.g. <60 words. By contrast, if the task is complex and ''multi-step, such as finding particular job applications based '- 'on user CV, the workflow must be longer, e.g. about 120 words.'+ 'on user CV, the workflow must be longer, e.g. about 120 words. '+ 'For tags, provide 3-5 broad categorization tags using lowercase '+ 'with hyphens (e.g., "data-analysis", "web-scraping") that '+ 'describe the workflow domain, type, and key capabilities to '+ 'help future agents discover this workflow when working on '+ 'similar tasks.')@@ -533,7 +549,6 @@ class ContextUtility:'session_id': self.session_id,'working_directory': str(self.working_directory),'created_at': datetime.now().isoformat(),- 'base_directory': str(self.working_directory.parent),}def list_sessions(self, base_dir: Optional[str] = None) -> List[str]:@@ -794,6 +809,137 @@ class ContextUtility:result = '\n'.join(filtered_lines).strip()return result+ # ========= WORKFLOW INFO METHODS =========++ def extract_workflow_info(self, file_path: str) -> Dict[str, Any]:+ r"""Extract info from a workflow markdown file.++ This method reads only the essential info from a workflow file+ (title, description, tags) for use in workflow selection without+ loading the entire workflow content.++ Args:+ file_path (str): Full path to the workflow markdown file.++ Returns:+ Dict[str, Any]: Workflow info including title, description,+ tags, and file_path. Returns empty dict on error.+ """+ import re++ try:+ with open(file_path, 'r', encoding='utf-8') as f:+ content = f.read()++ metadata: Dict[str, Any] = {'file_path': file_path}++ # extract task title+ title_match = re.search(+ r'### Task Title\s*\n(.+?)(?:\n###|\n\n|$)', content, re.DOTALL+ )+ if title_match:+ metadata['title'] = title_match.group(1).strip()+ else:+ metadata['title'] = ""++ # extract task description+ desc_match = re.search(+ r'### Task Description\s*\n(.+?)(?:\n###|\n\n|$)',+ content,+ re.DOTALL,+ )+ if desc_match:+ metadata['description'] = desc_match.group(1).strip()+ else:+ metadata['description'] = ""++ # extract tags+ tags_match = re.search(+ r'### Tags\s*\n(.+?)(?:\n###|\n\n|$)', content, re.DOTALL+ )+ if tags_match:+ tags_section = tags_match.group(1).strip()+ # Parse bullet list of tags+ tags = [+ line.strip().lstrip('- ')+ for line in tags_section.split('\n')+ if line.strip().startswith('-')+ ]+ metadata['tags'] = tags+ else:+ metadata['tags'] = []++ return metadata++ except Exception as e:+ logger.warning(+ f"Error extracting workflow info from {file_path}: {e}"+ )+ return {}++ def get_all_workflows_info(+ self, session_id: Optional[str] = None+ ) -> List[Dict[str, Any]]:+ r"""Get info from all workflow files in workforce_workflows.++ This method scans the workforce_workflows directory for workflow+ markdown files and extracts their info for use in workflow+ selection.++ Args:+ session_id (Optional[str]): If provided, only return workflows+ from this specific session. If None, returns workflows from+ all sessions.++ Returns:+ List[Dict[str, Any]]: List of workflow info dicts, sorted+ by session timestamp (newest first).+ """+ import glob+ import re++ workflows_metadata = []++ # Determine base directory for workforce workflows+ camel_workdir = os.environ.get("CAMEL_WORKDIR")+ if camel_workdir:+ base_dir = os.path.join(camel_workdir, "workforce_workflows")+ else:+ base_dir = "workforce_workflows"++ # Build search pattern+ if session_id:+ search_pattern = os.path.join(+ base_dir, session_id, "*_workflow.md"+ )+ else:+ search_pattern = os.path.join(base_dir, "*", "*_workflow.md")++ # Find all workflow files+ workflow_files = glob.glob(search_pattern)++ if not workflow_files:+ logger.info(f"No workflow files found in {base_dir}")+ return []++ # Sort by session timestamp (newest first)+ def extract_session_timestamp(filepath: str) -> str:+ match = re.search(r'session_(\d{8}_\d{6}_\d{6})', filepath)+ return match.group(1) if match else ""++ workflow_files.sort(key=extract_session_timestamp, reverse=True)++ # Extract info from each file+ for file_path in workflow_files:+ metadata = self.extract_workflow_info(file_path)+ if metadata: # Only add if extraction succeeded+ workflows_metadata.append(metadata)++ logger.info(+ f"Found {len(workflows_metadata)} workflow file(s) with info"+ )+ return workflows_metadata+# ========= SHARED SESSION MANAGEMENT METHODS =========@classmethoddiff --git a/test/workforce/test_workflow_memory.py b/test/workforce/test_workflow_memory.pyindex 04d53aaaf..d2e59b7c6 100644--- a/test/workforce/test_workflow_memory.py+++ b/test/workforce/test_workflow_memory.py@@ -119,11 +119,10 @@ class TestSingleAgentWorkerWorkflow:# Replace the worker with a non-ChatAgent objectworker.worker = "not_a_chat_agent"- result = await worker.save_workflow_memories_async()-- assert result["status"] == "error"- assert "Worker must be a ChatAgent" in result["message"]- assert result["worker_description"] == "test"+ # With the refactored code, TypeError is raised during+ # WorkflowMemoryManager initialization+ with pytest.raises(TypeError, match="Worker must be a ChatAgent"):+ await worker.save_workflow_memories_async()@pytest.mark.asyncioasync def test_save_workflow_exception(self, temp_context_dir):@@ -147,7 +146,7 @@ class TestSingleAgentWorkerWorkflow:def test_load_workflow_success(self, mock_getmtime, mock_glob, temp_context_dir):- """Test successful workflow loading."""+ """Test successful workflow loading (legacy pattern matching mode)."""worker = MockSingleAgentWorker("data_analyst")# Mock file discovery@@ -157,20 +156,24 @@ class TestSingleAgentWorkerWorkflow:mock_glob.return_value = mock_filesmock_getmtime.return_value = 1234567890- # Mock the shared context utility method+ # Mock the shared context utility methodsmock_context_utility = MagicMock()- mock_context_utility.load_markdown_context_to_memory.return_value = (- "Context appended successfully"+ mock_context_utility.load_markdown_file.return_value = (+ "# Workflow content\nThis is workflow data"+ )+ mock_context_utility._filter_metadata_from_content.return_value = (+ "This is workflow data")with patch('camel.utils.context_utils.ContextUtility.get_workforce_shared',return_value=mock_context_utility,):- result = worker.load_workflow_memories()+ # Use legacy pattern matching mode+ result = worker.load_workflow_memories(use_smart_selection=False)assert result is True- mock_context_utility.load_markdown_context_to_memory.assert_called()+ mock_context_utility.load_markdown_file.assert_called()def test_load_workflow_no_files(self, temp_context_dir):"""Test workflow loading when no files found."""@@ -192,15 +195,17 @@ class TestSingleAgentWorkerWorkflow:# Replace the worker with a non-ChatAgent objectworker.worker = "not_a_chat_agent"- result = worker.load_workflow_memories()-- assert result is False+ # With the refactored code, TypeError is raised during+ # WorkflowMemoryManager initialization+ with pytest.raises(TypeError, match="Worker must be a ChatAgent"):+ worker.load_workflow_memories()@patch('glob.glob')def test_load_workflow_prioritizes_newest_session(self, mock_glob, temp_context_dir):- """Test that workflow loading prioritizes files from newest sessions.+ """Test that workflow loading prioritizes files from newest sessions+ (legacy mode).This test verifies that when multiple workflow files exist acrossdifferent sessions, files from the newest session (by session@@ -222,31 +227,32 @@ class TestSingleAgentWorkerWorkflow:]mock_glob.return_value = mock_files- # Mock the shared context utility method+ # Mock the shared context utility methodsmock_context_utility = MagicMock()- mock_context_utility.load_markdown_context_to_memory.return_value = (- "Context appended successfully"+ mock_context_utility.load_markdown_file.return_value = (+ "# Workflow content\nThis is workflow data"+ )+ mock_context_utility._filter_metadata_from_content.return_value = (+ "This is workflow data")with patch('camel.utils.context_utils.ContextUtility.get_workforce_shared',return_value=mock_context_utility,):- result = worker.load_workflow_memories(max_files_to_load=1)+ # Use legacy pattern matching mode+ result = worker.load_workflow_memories(+ max_workflows=1, use_smart_selection=False+ )assert result is True# Verify load was called once- assert (- mock_context_utility.load_markdown_context_to_memory.call_count- == 1- )+ assert mock_context_utility.load_markdown_file.call_count == 1- # Get the filename that was loaded (second argument)+ # Get the filename that was loaded (first argument)loaded_filename = (- mock_context_utility.load_markdown_context_to_memory.call_args[- 0- ][1]+ mock_context_utility.load_markdown_file.call_args[0][0])# The loaded file should be 'data_analyst_workflow' (without .md)@@ -258,7 +264,7 @@ class TestSingleAgentWorkerWorkflow:self, mock_glob, temp_context_dir):"""Test that multiple calls to load_workflow_memories reset system- message.+ message (legacy mode).This test verifies that calling load_workflow_memories multiple timesdoesn't accumulate workflow context but instead resets to original@@ -275,24 +281,31 @@ class TestSingleAgentWorkerWorkflow:]mock_glob.return_value = mock_files- # mock the shared context utility method+ # mock the shared context utility methodsmock_context_utility = MagicMock()- mock_context_utility.load_markdown_context_to_memory.return_value = (- "Context appended successfully"+ mock_context_utility.load_markdown_file.return_value = (+ "# Workflow content\nThis is workflow data"+ )+ mock_context_utility._filter_metadata_from_content.return_value = (+ "This is workflow data")with patch('camel.utils.context_utils.ContextUtility.get_workforce_shared',return_value=mock_context_utility,):- # first call to load_workflow_memories- worker.load_workflow_memories(max_files_to_load=1)+ # first call to load_workflow_memories (legacy mode)+ worker.load_workflow_memories(+ max_workflows=1, use_smart_selection=False+ )# verify reset was called (system message matches original)first_call_system_content = worker.worker._system_message.content- # second call to load_workflow_memories- worker.load_workflow_memories(max_files_to_load=1)+ # second call to load_workflow_memories (legacy mode)+ worker.load_workflow_memories(+ max_workflows=1, use_smart_selection=False+ )# after second call, system message should still be based on# original (not accumulating from first call)@@ -306,10 +319,7 @@ class TestSingleAgentWorkerWorkflow:assert original_content in second_call_system_content# verify load was called twice (once per load_workflow_memories)- assert (- mock_context_utility.load_markdown_context_to_memory.call_count- == 2- )+ assert mock_context_utility.load_markdown_file.call_count == 2class TestWorkforceWorkflowMemoryMethods:@@ -879,6 +889,555 @@ class TestSharedContextUtility:assert context_util is shared_context+class TestSmartWorkflowSelection:+ """Test smart workflow selection feature."""++ def test_smart_selection_with_metadata(self, temp_context_dir):+ """Test smart workflow selection using metadata."""+ worker = MockSingleAgentWorker("data_analyst")++ # Mock workflow metadata+ mock_metadata = [+ {+ 'title': 'Data Analysis Workflow',+ 'description': 'Analyze sales data and generate reports',+ 'tags': ['data-analysis', 'statistics', 'reporting'],+ 'file_path': (+ f"{temp_context_dir}/session_1/data_analyst_workflow.md"+ ),+ },+ {+ 'title': 'Web Scraping Workflow',+ 'description': 'Scrape website data',+ 'tags': ['web-scraping', 'data-collection'],+ 'file_path': (+ f"{temp_context_dir}/session_1/web_scraper_workflow.md"+ ),+ },+ {+ 'title': 'Database Query Workflow',+ 'description': 'Query database for analysis',+ 'tags': ['database', 'sql', 'data-analysis'],+ 'file_path': (+ f"{temp_context_dir}/session_1/db_analyst_workflow.md"+ ),+ },+ ]++ # Mock context utility to return info+ mock_context_utility = MagicMock()+ mock_context_utility.get_all_workflows_info.return_value = (+ mock_metadata+ )+ mock_context_utility.load_markdown_file.return_value = (+ "# Workflow content\nThis is workflow data"+ )+ mock_context_utility._filter_metadata_from_content.return_value = (+ "This is workflow data"+ )++ # Mock agent response to select first workflow+ mock_agent_response = MagicMock()+ mock_agent_response.msgs = [MagicMock(content="1, 3")]++ # Mock get_workforce_shared to return our mock context utility+ with (+ patch(+ 'camel.societies.workforce.workflow_memory_manager.ContextUtility.get_workforce_shared',+ return_value=mock_context_utility,+ ),+ patch.object(+ worker.worker, 'step', return_value=mock_agent_response+ ),+ ):+ result = worker.load_workflow_memories(+ max_workflows=2, use_smart_selection=True+ )++ # Verify smart selection was used+ assert result is True+ mock_context_utility.get_all_workflows_info.assert_called_once()+ worker.worker.step.assert_called_once()++ # Verify agent was asked to select workflows+ selection_call = worker.worker.step.call_args[0][0]+ assert "data_analyst" in selection_call.content+ assert "select the 2 most relevant" in selection_call.content+ assert "data-analysis" in selection_call.content++ def test_smart_selection_no_metadata(self, temp_context_dir):+ """Test smart selection when no workflow info found."""+ worker = MockSingleAgentWorker("data_analyst")++ # Mock context utility to return empty info+ mock_context_utility = MagicMock()+ mock_context_utility.get_all_workflows_info.return_value = []++ with patch(+ 'camel.societies.workforce.workflow_memory_manager.ContextUtility.get_workforce_shared',+ return_value=mock_context_utility,+ ):+ result = worker.load_workflow_memories(use_smart_selection=True)++ assert result is False+ mock_context_utility.get_all_workflows_info.assert_called_once()++ def test_smart_selection_fewer_workflows_than_max(self, temp_context_dir):+ """Test smart selection when fewer workflows exist than max_files."""+ worker = MockSingleAgentWorker("data_analyst")++ # Mock only 2 workflows but ask for 5+ mock_metadata = [+ {+ 'title': 'Workflow 1',+ 'description': 'Test workflow 1',+ 'tags': ['test'],+ 'file_path': f"{temp_context_dir}/session_1/workflow1.md",+ },+ {+ 'title': 'Workflow 2',+ 'description': 'Test workflow 2',+ 'tags': ['test'],+ 'file_path': f"{temp_context_dir}/session_1/workflow2.md",+ },+ ]++ mock_context_utility = MagicMock()+ mock_context_utility.get_all_workflows_info.return_value = (+ mock_metadata+ )+ mock_context_utility.load_markdown_file.return_value = (+ "# Workflow content\nThis is workflow data"+ )+ mock_context_utility._filter_metadata_from_content.return_value = (+ "This is workflow data"+ )++ with patch(+ 'camel.societies.workforce.workflow_memory_manager.ContextUtility.get_workforce_shared',+ return_value=mock_context_utility,+ ):+ result = worker.load_workflow_memories(+ max_workflows=5, use_smart_selection=True+ )++ # Should load all 2 workflows without agent selection+ assert result is True+ assert mock_context_utility.load_markdown_file.call_count == 2++ def test_smart_selection_agent_selection_failure_fallback(+ self, temp_context_dir+ ):+ """Test fallback to most recent when agent selection fails."""+ worker = MockSingleAgentWorker("data_analyst")++ mock_metadata = [+ {+ 'title': 'Workflow 1',+ 'description': 'Test',+ 'tags': ['test'],+ 'file_path': f"{temp_context_dir}/session_1/workflow1.md",+ },+ {+ 'title': 'Workflow 2',+ 'description': 'Test',+ 'tags': ['test'],+ 'file_path': f"{temp_context_dir}/session_1/workflow2.md",+ },+ {+ 'title': 'Workflow 3',+ 'description': 'Test',+ 'tags': ['test'],+ 'file_path': f"{temp_context_dir}/session_1/workflow3.md",+ },+ ]++ mock_context_utility = MagicMock()+ mock_context_utility.get_all_workflows_info.return_value = (+ mock_metadata+ )+ mock_context_utility.load_markdown_file.return_value = (+ "# Workflow content\nThis is workflow data"+ )+ mock_context_utility._filter_metadata_from_content.return_value = (+ "This is workflow data"+ )++ # Mock agent to return invalid response+ mock_agent_response = MagicMock()+ mock_agent_response.msgs = [MagicMock(content="invalid response")]++ with (+ patch(+ 'camel.societies.workforce.workflow_memory_manager.ContextUtility.get_workforce_shared',+ return_value=mock_context_utility,+ ),+ patch.object(+ worker.worker, 'step', return_value=mock_agent_response+ ),+ ):+ result = worker.load_workflow_memories(+ max_workflows=2, use_smart_selection=True+ )++ # Should fallback to first 2 workflows (most recent)+ assert result is True+ assert mock_context_utility.load_markdown_file.call_count == 2++ def test_smart_selection_memory_cleanup(self, temp_context_dir):+ """Test that agent memory is cleaned after smart selection."""+ worker = MockSingleAgentWorker("data_analyst")++ mock_metadata = [+ {+ 'title': 'Test Workflow',+ 'description': 'Test',+ 'tags': ['test'],+ 'file_path': f"{temp_context_dir}/session_1/workflow.md",+ }+ ]++ mock_context_utility = MagicMock()+ mock_context_utility.get_all_workflows_info.return_value = (+ mock_metadata+ )+ mock_context_utility.load_markdown_file.return_value = (+ "# Workflow content\nThis is workflow data"+ )+ mock_context_utility._filter_metadata_from_content.return_value = (+ "This is workflow data"+ )++ with patch(+ 'camel.societies.workforce.workflow_memory_manager.ContextUtility.get_workforce_shared',+ return_value=mock_context_utility,+ ):+ result = worker.load_workflow_memories(use_smart_selection=True)++ assert result is True++ # Memory should be cleaned (only system message remains)+ final_memory = worker.worker.memory.get_context()[0]+ # Should have system message only+ assert len(final_memory) == 1++ def test_extract_workflow_info(self, temp_context_dir):+ """Test info extraction from workflow markdown file."""+ from camel.utils.context_utils import ContextUtility++ # Create a test workflow file+ workflow_content = """### Task Title+Data Analysis Workflow++### Task Description+Analyze sales data and generate comprehensive reports with visualizations.++### Tags+- data-analysis+- statistics+- reporting+- visualization++### Steps Taken+1. Load data+2. Clean data+3. Analyze trends+"""+ workflow_path = f"{temp_context_dir}/test_workflow.md"+ with open(workflow_path, 'w') as f:+ f.write(workflow_content)++ # Extract info+ context_util = ContextUtility.get_workforce_shared()+ metadata = context_util.extract_workflow_info(workflow_path)++ assert metadata['title'] == "Data Analysis Workflow"+ assert "sales data" in metadata['description']+ assert 'data-analysis' in metadata['tags']+ assert 'statistics' in metadata['tags']+ assert 'reporting' in metadata['tags']+ assert 'visualization' in metadata['tags']+ assert metadata['file_path'] == workflow_path++ def test_get_all_workflows_info(self, temp_context_dir):+ """Test getting info from all workflow files."""+ # Create test workflow files+ import os++ from camel.utils.context_utils import ContextUtility++ os.makedirs(+ f"{temp_context_dir}/workforce_workflows/session_1", exist_ok=True+ )+ os.makedirs(+ f"{temp_context_dir}/workforce_workflows/session_2", exist_ok=True+ )++ workflow1 = (+ f"{temp_context_dir}/workforce_workflows/session_1/"+ "analyst_workflow.md"+ )+ workflow2 = (+ f"{temp_context_dir}/workforce_workflows/session_2/"+ "developer_workflow.md"+ )++ with open(workflow1, 'w') as f:+ f.write("""### Task Title+Analysis Workflow++### Task Description+Data analysis++### Tags+- data-analysis+""")++ with open(workflow2, 'w') as f:+ f.write("""### Task Title+Development Workflow++### Task Description+Code development++### Tags+- development+- coding+""")++ # Get all info+ context_util = ContextUtility.get_workforce_shared()+ all_metadata = context_util.get_all_workflows_info()++ assert len(all_metadata) == 2+ titles = [m['title'] for m in all_metadata]+ assert "Analysis Workflow" in titles+ assert "Development Workflow" in titles++ def test_smart_selection_with_session_filter(self, temp_context_dir):+ """Test smart selection with specific session ID."""+ worker = MockSingleAgentWorker("data_analyst")++ mock_metadata = [+ {+ 'title': 'Test Workflow',+ 'description': 'Test',+ 'tags': ['test'],+ 'file_path': f"{temp_context_dir}/session_123/workflow.md",+ }+ ]++ mock_context_utility = MagicMock()+ mock_context_utility.get_all_workflows_info.return_value = (+ mock_metadata+ )+ mock_context_utility.load_markdown_file.return_value = (+ "# Workflow content\nThis is workflow data"+ )+ mock_context_utility._filter_metadata_from_content.return_value = (+ "This is workflow data"+ )++ with patch(+ 'camel.societies.workforce.workflow_memory_manager.ContextUtility.get_workforce_shared',+ return_value=mock_context_utility,+ ):+ result = worker.load_workflow_memories(+ session_id="session_123", use_smart_selection=True+ )++ assert result is True+ # Verify session_id was passed to get_all_workflows_info+ mock_context_utility.get_all_workflows_info.assert_called_once_with(+ "session_123"+ )++ def test_practical_smart_selection(self, temp_context_dir):+ """Test smart selection with 10 real workflow files in temp+ directory.+ """+ import os++ # Create session directory in workforce_workflows+ session_dir = os.path.join(+ temp_context_dir, "workforce_workflows", "session_test"+ )+ os.makedirs(session_dir, exist_ok=True)++ # Create 10 different workflow files with varied content+ workflows = [+ {+ "name": "data_analysis_workflow.md",+ "title": "Data Analysis Pipeline",+ "description": (+ "Analyze CSV data and generate statistical reports"+ ),+ "tags": ["data-analysis", "statistics", "csv-processing"],+ },+ {+ "name": "web_scraping_workflow.md",+ "title": "Web Scraper",+ "description": "Scrape product data from e-commerce websites",+ "tags": ["web-scraping", "data-collection", "api-integration"],+ },+ {+ "name": "email_automation_workflow.md",+ "title": "Email Campaign Manager",+ "description": "Send automated marketing emails to customers",+ "tags": ["email-automation", "marketing", "communication"],+ },+ {+ "name": "image_processing_workflow.md",+ "title": "Image Optimizer",+ "description": "Resize and compress images for web use",+ "tags": [+ "image-processing",+ "file-processing",+ "optimization",+ ],+ },+ {+ "name": "database_query_workflow.md",+ "title": "Database Reporter",+ "description": "Query database and generate business reports",+ "tags": ["database-query", "sql", "report-generation"],+ },+ {+ "name": "api_integration_workflow.md",+ "title": "API Connector",+ "description": "Connect to external APIs and sync data",+ "tags": ["api-integration", "data-sync", "web-services"],+ },+ {+ "name": "text_processing_workflow.md",+ "title": "Text Analyzer",+ "description": "Process and analyze large text documents",+ "tags": ["text-processing", "nlp", "data-analysis"],+ },+ {+ "name": "code_generation_workflow.md",+ "title": "Code Generator",+ "description": "Generate boilerplate code from templates",+ "tags": ["code-generation", "automation", "development"],+ },+ {+ "name": "file_organizer_workflow.md",+ "title": "File Organizer",+ "description": "Organize files by type and date",+ "tags": ["file-processing", "automation", "organization"],+ },+ {+ "name": "report_builder_workflow.md",+ "title": "Report Builder",+ "description": "Build PDF reports from data sources",+ "tags": ["report-generation", "pdf", "data-visualization"],+ },+ ]++ # Write workflow files+ for workflow in workflows:+ file_path = os.path.join(session_dir, workflow["name"])+ content = f"""## Metadata++- session_id: session_test+- working_directory: {session_dir}+- created_at: 2025-01-15T10:00:00.000000+- agent_id: test-agent-id+- message_count: 5++## WorkflowSummary++### Task Title+{workflow["title"]}++### Task Description+{workflow["description"]}++### Tags+{", ".join(workflow["tags"])}++### Tools+(No tools recorded)++### Steps+1. Initial step+2. Processing step+3. Final step++### Failure And Recovery Strategies+(No failure and recovery strategies recorded)++### Notes And Observations+(No notes and observations provided)+"""+ with open(file_path, 'w') as f:+ f.write(content)++ # Create worker that needs data analysis+ worker = MockSingleAgentWorker("data_analyst")++ # Mock the agent to select data-related workflows+ mock_agent_response = MagicMock()+ mock_agent_response.msgs = [+ MagicMock(+ content="Based on the role, I select: 1, 5, 7"+ ) # data_analysis, database_query, text_processing+ ]++ # Create real ContextUtility to test actual metadata extraction+ from pathlib import Path++ from camel.utils.context_utils import ContextUtility++ # Save the original function before patching+ original_get_workforce_shared = ContextUtility.get_workforce_shared++ # Create a side_effect function that returns properly+ # configured utilities+ def get_utility_with_base_dir(session_id=None):+ utility = original_get_workforce_shared(session_id)+ utility._base_directory = Path(temp_context_dir)+ return utility++ # Store original system message before loading+ original_system_message = worker.worker._system_message.content++ with (+ patch(+ 'camel.societies.workforce.workflow_memory_manager.ContextUtility.get_workforce_shared',+ side_effect=get_utility_with_base_dir,+ ),+ patch.object(+ worker.worker, 'step', return_value=mock_agent_response+ ),+ ):+ # Load with smart selection (max 3 files)+ result = worker.load_workflow_memories(+ max_workflows=3, use_smart_selection=True+ )++ # Verify success+ assert result is True++ # Verify agent was asked to select+ assert worker.worker.step.called++ # Verify that workflows were actually added to the system message+ updated_system_message = worker.worker._system_message.content++ # System message should be different from original+ # (workflows added)+ assert updated_system_message != original_system_message++ # System message should contain workflow titles from the+ # loaded files. At least some workflow-related content+ # should be present+ assert len(updated_system_message) > len(original_system_message)++ # Verify system message contains workflow metadata+ assert "Previous Workflows" in updated_system_message++class TestWorkforceMemorySharing:"""Test Workforce memory sharing and deduplication."""@@ -1077,3 +1636,802 @@ class TestWorkforceMemorySharing:# 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+++class TestConversationAccumulator:+ """Test conversation accumulator behavior - critical for workflow+ memory.+ """++ @pytest.mark.asyncio+ async def test_accumulator_collects_from_multiple_tasks(+ self, temp_context_dir+ ):+ """Test that accumulator properly collects conversations from+ multiple task executions.++ This validates the core accumulation mechanism across multiple+ tasks, ensuring all conversations are captured before+ summarization.+ """+ # create worker with workflow memory enabled+ sys_msg = BaseMessage.make_assistant_message(+ role_name="Test Worker", content="You are a test worker."+ )+ agent = ChatAgent(sys_msg)+ worker = SingleAgentWorker(+ description="test_worker",+ worker=agent,+ enable_workflow_memory=True,+ use_agent_pool=False, # disable pooling for simpler test+ )++ # process multiple tasks+ task1 = Task(content="Calculate 2+2", id="t1")+ task2 = Task(content="Analyze data trends", id="t2")+ task3 = Task(content="Generate summary report", id="t3")++ # mock the step method to simulate task processing+ with patch.object(ChatAgent, 'step') as mock_step:+ mock_step.return_value = BaseMessage.make_assistant_message(+ role_name="Assistant", content="Task completed"+ )++ await worker._process_task(task1, [])+ await worker._process_task(task2, [])+ await worker._process_task(task3, [])++ # verify accumulator was created+ assert (+ worker._conversation_accumulator is not None+ ), "Accumulator should be created when workflow memory is enabled"++ # verify accumulator has messages from ALL tasks+ accumulator = worker._conversation_accumulator+ records = accumulator.memory.retrieve()++ # should contain messages from all 3 tasks+ # each task creates user message + assistant response+ assert len(records) >= 6, (+ f"Expected at least 6 messages (3 tasks * 2 messages each), "+ f"got {len(records)}"+ )++ # verify messages are about the tasks we created+ messages_content = [+ record.memory_record.message.content for record in records+ ]+ assert any(+ "2+2" in content for content in messages_content+ ), "Should contain content from task1"+ assert any(+ "data trends" in content for content in messages_content+ ), "Should contain content from task2"+ assert any(+ "summary report" in content for content in messages_content+ ), "Should contain content from task3"++ @pytest.mark.asyncio+ async def test_workflow_memory_disabled_skips_accumulation(+ self, temp_context_dir+ ):+ """Test that enable_workflow_memory=False prevents accumulation.++ Verifies the feature can be disabled and doesn't impact performance+ with unnecessary memory operations.+ """+ sys_msg = BaseMessage.make_assistant_message(+ role_name="Test Worker", content="You are a test worker."+ )+ agent = ChatAgent(sys_msg)+ worker = SingleAgentWorker(+ description="test_worker",+ worker=agent,+ enable_workflow_memory=False, # explicitly disabled+ use_agent_pool=False,+ )++ # process a task+ task = Task(content="Process this task", id="t1")++ with patch.object(ChatAgent, 'step') as mock_step:+ mock_step.return_value = BaseMessage.make_assistant_message(+ role_name="Assistant", content="Task completed"+ )+ await worker._process_task(task, [])++ # accumulator should NOT be created when disabled+ assert worker._conversation_accumulator is None, (+ "Accumulator should not be created when "+ "enable_workflow_memory=False"+ )++ @pytest.mark.asyncio+ async def test_accumulator_cleanup_after_successful_save(+ self, temp_context_dir+ ):+ """Test that accumulator is cleared after successful workflow save.++ Validates cleanup to prevent memory leaks and duplicate content+ in future saves.+ """+ sys_msg = BaseMessage.make_assistant_message(+ role_name="Test Worker", content="You are a test worker."+ )+ agent = ChatAgent(sys_msg)+ worker = SingleAgentWorker(+ description="test_worker",+ worker=agent,+ enable_workflow_memory=True,+ use_agent_pool=False,+ )++ # process tasks to create accumulator+ task1 = Task(content="Task 1", id="t1")+ task2 = Task(content="Task 2", id="t2")++ with patch.object(ChatAgent, 'step') as mock_step:+ mock_step.return_value = BaseMessage.make_assistant_message(+ role_name="Assistant", content="Done"+ )+ await worker._process_task(task1, [])+ await worker._process_task(task2, [])++ # verify accumulator exists+ assert (+ worker._conversation_accumulator is not None+ ), "Accumulator should exist after processing tasks"++ # save workflow with mocked summarization+ mock_summary_result = {+ "status": "success",+ "summary": "Test summary",+ "file_path": f"{temp_context_dir}/test_workflow.md",+ }++ with patch.object(+ ChatAgent, 'asummarize', return_value=mock_summary_result+ ):+ result = await worker.save_workflow_memories_async()++ # verify save was successful+ assert (+ result["status"] == "success"+ ), f"Save should succeed, got: {result}"++ # accumulator should be None after successful save+ assert worker._conversation_accumulator is None, (+ "Accumulator should be cleared after successful save to "+ "prevent memory leaks and duplicate content in future saves"+ )++ @pytest.mark.asyncio+ async def test_accumulator_receives_memory_not_main_worker(+ self, temp_context_dir+ ):+ """Test that conversations go to accumulator, not main worker.++ Validates the design separation - accumulator collects task+ conversations while main worker memory stays clean.+ """+ sys_msg = BaseMessage.make_assistant_message(+ role_name="Test Worker", content="You are a test worker."+ )+ agent = ChatAgent(sys_msg)+ worker = SingleAgentWorker(+ description="test_worker",+ worker=agent,+ enable_workflow_memory=True,+ use_agent_pool=False, # use cloning instead of pool+ )++ # get initial main worker memory count+ main_worker_records_before = len(worker.worker.memory.retrieve())++ # process task+ task = Task(content="Process this task", id="t1")++ with patch.object(ChatAgent, 'step') as mock_step:+ mock_step.return_value = BaseMessage.make_assistant_message(+ role_name="Assistant", content="Task completed"+ )+ await worker._process_task(task, [])++ # main worker should still have same memory (cloned agents are used)+ main_worker_records_after = len(worker.worker.memory.retrieve())+ assert main_worker_records_after == main_worker_records_before, (+ f"Main worker memory should not change "+ f"(before: {main_worker_records_before}, "+ f"after: {main_worker_records_after})"+ )++ # accumulator should have the task conversations+ accumulator = worker._conversation_accumulator+ assert accumulator is not None, "Accumulator should exist"++ accumulator_records = accumulator.memory.retrieve()+ assert len(accumulator_records) > main_worker_records_before, (+ f"Accumulator should have more records than main worker "+ f"(accumulator: {len(accumulator_records)}, "+ f"main: {main_worker_records_before})"+ )++ @pytest.mark.asyncio+ async def test_save_workflow_with_no_conversation_history(+ self, temp_context_dir+ ):+ """Test saving workflow when agent has no conversations.++ Should handle gracefully (return error or empty state) rather+ than crashing.+ """+ sys_msg = BaseMessage.make_assistant_message(+ role_name="Test Worker", content="You are a test worker."+ )+ agent = ChatAgent(sys_msg)+ worker = SingleAgentWorker(+ description="test_worker",+ worker=agent,+ enable_workflow_memory=True,+ )++ # don't process any tasks - no conversation history+ # accumulator should be None+ assert worker._conversation_accumulator is None++ # mock asummarize to handle the case+ mock_summary_result = {+ "status": "error",+ "message": "No conversation history to summarize",+ }++ with patch.object(+ ChatAgent, 'asummarize', return_value=mock_summary_result+ ):+ result = await worker.save_workflow_memories_async()++ # should return result with status (either success or error)+ assert "status" in result, "Result should have 'status' key"++ # if error, should have descriptive message+ if result["status"] == "error":+ assert "message" in result, "Error result should have 'message'"+++class TestAgentPoolMemoryTransfer:+ """Test agent pool integration with workflow memory system."""++ @pytest.mark.asyncio+ async def test_pooled_agent_memory_transfers_to_accumulator(+ self, temp_context_dir+ ):+ """Test that conversations from pooled agents are accumulated.++ Validates the memory transfer logic from pooled agents to the+ accumulator, which is critical for the agent pooling feature.+ """+ sys_msg = BaseMessage.make_assistant_message(+ role_name="Test Worker", content="You are a test worker."+ )+ base_agent = ChatAgent(sys_msg)+ worker = SingleAgentWorker(+ description="test_worker",+ worker=base_agent,+ use_agent_pool=True, # enable agent pooling+ enable_workflow_memory=True,+ )++ # process task (will use pooled agent)+ task = Task(content="Calculate 2+2", id="t1")++ with patch.object(ChatAgent, 'step') as mock_step:+ mock_step.return_value = BaseMessage.make_assistant_message(+ role_name="Assistant", content="The answer is 4"+ )+ await worker._process_task(task, [])++ # verify accumulator received the conversation from pooled agent+ accumulator = worker._conversation_accumulator+ assert (+ accumulator is not None+ ), "Accumulator should exist when workflow memory is enabled"++ records = accumulator.memory.retrieve()+ assert len(records) >= 2, (+ f"Expected at least 2 messages (user + assistant), "+ f"got {len(records)}"+ )++ # verify the messages are about the task+ messages_content = [r.memory_record.message.content for r in records]+ assert any(+ "2+2" in msg for msg in messages_content+ ), "Should contain task content from pooled agent"++ @pytest.mark.asyncio+ async def test_memory_accumulation_with_and_without_pool(+ self, temp_context_dir+ ):+ """Test that memory accumulation works consistently with/without pool.++ Validates that the feature works identically regardless of+ pooling configuration.+ """++ def create_test_agent():+ sys_msg = BaseMessage.make_assistant_message(+ role_name="Test Worker", content="You are a test worker."+ )+ return ChatAgent(sys_msg)++ # test with pool enabled+ worker_pooled = SingleAgentWorker(+ description="test_worker",+ worker=create_test_agent(),+ use_agent_pool=True,+ enable_workflow_memory=True,+ )++ task = Task(content="Process this data", id="t1")++ with patch.object(ChatAgent, 'step') as mock_step:+ mock_step.return_value = BaseMessage.make_assistant_message(+ role_name="Assistant", content="Data processed"+ )+ await worker_pooled._process_task(task, [])++ pooled_memory = (+ worker_pooled._conversation_accumulator.memory.retrieve()+ )++ # test with pool disabled+ worker_no_pool = SingleAgentWorker(+ description="test_worker",+ worker=create_test_agent(),+ use_agent_pool=False,+ enable_workflow_memory=True,+ )++ with patch.object(ChatAgent, 'step') as mock_step:+ mock_step.return_value = BaseMessage.make_assistant_message(+ role_name="Assistant", content="Data processed"+ )+ await worker_no_pool._process_task(task, [])++ no_pool_memory = (+ worker_no_pool._conversation_accumulator.memory.retrieve()+ )++ # should accumulate same number of messages+ assert len(pooled_memory) == len(no_pool_memory), (+ f"Pooled and non-pooled should accumulate same number of "+ f"messages (pooled: {len(pooled_memory)}, "+ f"non-pooled: {len(no_pool_memory)})"+ )++ @pytest.mark.asyncio+ async def test_pooled_agent_reset_after_return(self, temp_context_dir):+ """Test that pooled agents are properly reset after being returned.++ Validates memory isolation between different task executions+ using the same pooled agent.+ """+ sys_msg = BaseMessage.make_assistant_message(+ role_name="Test Worker", content="You are a test worker."+ )+ base_agent = ChatAgent(sys_msg)+ worker = SingleAgentWorker(+ description="test_worker",+ worker=base_agent,+ use_agent_pool=True,+ enable_workflow_memory=True,+ pool_initial_size=1, # single agent in pool+ )++ # process first task+ task1 = Task(content="First task", id="t1")++ with patch.object(ChatAgent, 'step') as mock_step:+ mock_step.return_value = BaseMessage.make_assistant_message(+ role_name="Assistant", content="First task done"+ )+ await worker._process_task(task1, [])++ # get accumulator state after first task+ accumulator_after_task1 = len(+ worker._conversation_accumulator.memory.retrieve()+ )++ # process second task (should reuse same pooled agent)+ task2 = Task(content="Second task", id="t2")++ with patch.object(ChatAgent, 'step') as mock_step:+ mock_step.return_value = BaseMessage.make_assistant_message(+ role_name="Assistant", content="Second task done"+ )+ await worker._process_task(task2, [])++ # accumulator should have more messages now+ accumulator_after_task2 = len(+ worker._conversation_accumulator.memory.retrieve()+ )++ assert accumulator_after_task2 > accumulator_after_task1, (+ "Accumulator should grow after processing second task "+ f"(after task1: {accumulator_after_task1}, "+ f"after task2: {accumulator_after_task2})"+ )++ # verify both tasks are represented in accumulator+ records = worker._conversation_accumulator.memory.retrieve()+ messages_content = [r.memory_record.message.content for r in records]++ assert any(+ "First task" in msg for msg in messages_content+ ), "Should contain first task content"+ assert any(+ "Second task" in msg for msg in messages_content+ ), "Should contain second task content"+++class TestWorkflowContentValidation:+ """Test workflow content loading and formatting."""++ def test_workflow_content_actually_in_system_message(+ self, temp_context_dir+ ):+ """Test that loaded workflow content appears in system message.++ Validates end-to-end file I/O and content parsing to ensure+ workflows are properly loaded and added to agent context.+ """+ # create real workflow file with specific content+ workflow_content = """### Task Title+Data Analysis Pipeline++### Task Description+This workflow analyzes sales data using pandas and matplotlib.++### Tags+- data-analysis+- python+- visualization++### Steps+1. Load CSV file with pandas+2. Clean missing values+3. Generate summary statistics+4. Create bar chart visualization+"""++ # save to file in specific session+ session_dir = os.path.join(+ temp_context_dir, "workforce_workflows", "session_test_123"+ )+ os.makedirs(session_dir, exist_ok=True)++ workflow_file = os.path.join(session_dir, "analyst_workflow.md")+ with open(workflow_file, 'w') as f:+ f.write(workflow_content)++ # create worker and load workflow+ worker = MockSingleAgentWorker("data_analyst")+ original_system = worker.worker._system_message.content++ # load workflow from the specific session+ result = worker.load_workflow_memories(session_id="session_test_123")+ assert result is True, "Workflow load should succeed"++ # verify system message contains workflow content+ updated_system = worker.worker._system_message.content+ assert (+ updated_system != original_system+ ), "System message should be modified"+ assert (+ "Data Analysis Pipeline" in updated_system+ ), "Should contain task title"+ assert (+ "pandas and matplotlib" in updated_system+ ), "Should contain task description content"+ assert (+ "Load CSV file" in updated_system+ ), "Should contain workflow steps"++ # verify proper formatting with header+ assert (+ "Previous Workflows" in updated_system+ ), "Should have workflows header"+ assert "=" * 60 in updated_system, "Should have separator lines"++ def test_multiple_workflows_formatted_correctly(self, temp_context_dir):+ """Test that multiple workflows are formatted with correct headers.++ Validates plural formatting and proper separation of multiple+ workflow entries by directly testing the formatting method.+ """+ # test the formatting method directly with mock workflow data++ worker = MockSingleAgentWorker("test_worker")+ manager = worker._get_workflow_manager()++ # create mock workflow data+ workflows_to_load = [+ {+ 'filename': 'workflow_1',+ 'content': """### Task Title+First Workflow++### Description+This is the first workflow.""",+ },+ {+ 'filename': 'workflow_2',+ 'content': """### Task Title+Second Workflow++### Description+This is the second workflow.""",+ },+ {+ 'filename': 'workflow_3',+ 'content': """### Task Title+Third Workflow++### Description+This is the third workflow.""",+ },+ ]++ # test the formatting method directly+ formatted_content = manager._format_workflows_for_context(+ workflows_to_load+ )++ # verify formatting+ assert (+ "3 previous" in formatted_content.lower()+ or "3" in formatted_content+ ), "Should indicate 3 workflows"+ assert "Previous Workflows" in formatted_content, "Should have header"++ # each workflow should be numbered+ assert "Workflow 1:" in formatted_content+ assert "Workflow 2:" in formatted_content+ assert "Workflow 3:" in formatted_content++ # all content should be present+ assert "First Workflow" in formatted_content+ assert "Second Workflow" in formatted_content+ assert "Third Workflow" in formatted_content++ # should have separators+ separator_count = formatted_content.count("=" * 60)+ assert (+ separator_count >= 3+ ), f"Should have at least 3 separators, got {separator_count}"++ def test_metadata_filtered_from_loaded_workflow(self, temp_context_dir):+ """Test that metadata section is not included in system message.++ Validates that metadata is properly filtered to avoid polluting+ agent context with irrelevant session info.+ """+ # create workflow with metadata section+ workflow_with_metadata = """## Metadata++- session_id: test_session_456+- created_at: 2025-01-01+- agent_id: test-agent-789++## WorkflowSummary++### Task Title+Actual Workflow Content++### Description+This is the actual workflow description that should be loaded.++### Steps+1. Do important work+2. Complete the task+"""++ # save to file+ session_dir = os.path.join(+ temp_context_dir, "workforce_workflows", "session_metadata_test"+ )+ os.makedirs(session_dir, exist_ok=True)++ workflow_file = os.path.join(session_dir, "test_workflow.md")+ with open(workflow_file, 'w') as f:+ f.write(workflow_with_metadata)++ # load workflow+ worker = MockSingleAgentWorker("test_worker")+ result = worker.load_workflow_memories(+ session_id="session_metadata_test"+ )+ assert result is True, "Workflow with metadata should load"++ # verify system message+ system_content = worker.worker._system_message.content++ # should contain actual workflow content+ assert (+ "Actual Workflow Content" in system_content+ ), "Should contain workflow title"+ assert (+ "actual workflow description" in system_content+ ), "Should contain workflow description"+ assert (+ "Do important work" in system_content+ ), "Should contain workflow steps"++ # should NOT contain metadata+ assert (+ "session_id: test_session_456" not in system_content+ ), "Should filter out session_id metadata"+ assert (+ "agent_id: test-agent-789" not in system_content+ ), "Should filter out agent_id metadata"+ assert (+ "created_at: 2025-01-01" not in system_content+ ), "Should filter out created_at metadata"+++class TestErrorHandlingAndEdgeCases:+ """Test error handling and edge cases in workflow memory system."""++ def test_load_workflow_with_corrupted_markdown(self, temp_context_dir):+ """Test loading workflow from corrupted/malformed file.++ Should handle gracefully without crashing.+ """+ # create file with malformed content (not valid markdown structure)+ corrupted_content = """This is just random text+ with no proper structure+ ### Missing proper headers+ No Task Title section+ Random characters: !@#$%^&*()+ """++ # save corrupted file+ session_dir = os.path.join(+ temp_context_dir, "workforce_workflows", "session_corrupted"+ )+ os.makedirs(session_dir, exist_ok=True)++ corrupted_file = os.path.join(session_dir, "corrupted_workflow.md")+ with open(corrupted_file, 'w') as f:+ f.write(corrupted_content)++ # attempt to load - should not crash+ worker = MockSingleAgentWorker("test_worker")++ try:+ # loading may succeed or fail gracefully, but should not crash+ result = worker.load_workflow_memories(+ session_id="session_corrupted"+ )++ # if it succeeds, verify it handled the content+ if result is True:+ # system message should be updated even with malformed content+ system_content = worker.worker._system_message.content+ # the content might be loaded as-is, which is acceptable+ assert isinstance(+ system_content, str+ ), "System message should still be a string"++ except Exception as e:+ # if it raises an exception, it should be a handled one+ # (not a crash like AttributeError or KeyError)+ pytest.fail(+ f"Loading corrupted workflow should handle errors gracefully, "+ f"but got unhandled exception: {type(e).__name__}: {e}"+ )++ def test_workflow_operations_without_camel_workdir(self):+ """Test that workflows work when CAMEL_WORKDIR is not set.++ Should use default "workforce_workflows" directory.+ """+ # save current env var if it exists+ original_workdir = os.environ.get("CAMEL_WORKDIR")++ try:+ # remove CAMEL_WORKDIR env var+ if "CAMEL_WORKDIR" in os.environ:+ del os.environ["CAMEL_WORKDIR"]++ # create worker and test operations+ worker = MockSingleAgentWorker("test_worker")++ # should be able to create workflow manager without error+ manager = worker._get_workflow_manager()+ assert (+ manager is not None+ ), "Workflow manager should be created without CAMEL_WORKDIR"++ # context utility should use default directory+ context_util = manager._get_context_utility()+ assert (+ context_util is not None+ ), "Context utility should be created with default directory"++ # the context directory should use default "workforce_workflows"+ # (we don't test actual file operations to avoid creating files+ # in unexpected locations)++ finally:+ # restore original env var+ if original_workdir is not None:+ os.environ["CAMEL_WORKDIR"] = original_workdir+ elif "CAMEL_WORKDIR" in os.environ:+ del os.environ["CAMEL_WORKDIR"]++ @pytest.mark.asyncio+ async def test_save_workflow_with_very_long_conversation(+ self, temp_context_dir+ ):+ """Test workflow saving with very long conversation.++ Should handle large conversations gracefully (may truncate or+ summarize effectively).+ """+ sys_msg = BaseMessage.make_assistant_message(+ role_name="Test Worker", content="You are a test worker."+ )+ agent = ChatAgent(sys_msg)+ worker = SingleAgentWorker(+ description="test_worker",+ worker=agent,+ enable_workflow_memory=True,+ use_agent_pool=False,+ )++ # create many tasks to generate long conversation+ tasks = [+ Task(content=f"Task {i}: " + "x" * 100, id=f"t{i}")+ for i in range(50) # 50 tasks with long content+ ]++ with patch.object(ChatAgent, 'step') as mock_step:+ mock_step.return_value = BaseMessage.make_assistant_message(+ role_name="Assistant",+ content="Task completed with long response " + "y" * 100,+ )++ # process all tasks+ for task in tasks:+ await worker._process_task(task, [])++ # verify accumulator has many messages+ accumulator = worker._conversation_accumulator+ assert accumulator is not None+ records = accumulator.memory.retrieve()+ assert (+ len(records) > 50+ ), f"Should have many records (>50), got {len(records)}"++ # attempt to save - should handle gracefully+ mock_summary_result = {+ "status": "success",+ "summary": "Summary of very long conversation",+ "file_path": f"{temp_context_dir}/long_conversation_workflow.md",+ }++ with patch.object(+ ChatAgent, 'asummarize', return_value=mock_summary_result+ ):+ result = await worker.save_workflow_memories_async()++ # should succeed or return informative error+ assert "status" in result, "Result should have status"++ if result["status"] == "success":+ # successful handling+ assert "summary" in result+ elif result["status"] == "error":+ # graceful error handling+ assert "message" in result