mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-11 00:58:30 +00:00
Revert "fix: Rely on validator for memory copy, update test assertions"
This reverts commit 8702bf1e34.
This commit is contained in:
@@ -1176,14 +1176,10 @@ class Crew(BaseModel):
|
|||||||
"_execution_span",
|
"_execution_span",
|
||||||
"_file_handler",
|
"_file_handler",
|
||||||
"_cache_handler",
|
"_cache_handler",
|
||||||
"_short_term_memory", # Exclude private attributes if they exist
|
"_short_term_memory",
|
||||||
"_long_term_memory",
|
"_long_term_memory",
|
||||||
"_entity_memory",
|
"_entity_memory",
|
||||||
"_external_memory",
|
"_external_memory",
|
||||||
"short_term_memory", # Exclude public memory attributes from model_dump
|
|
||||||
"long_term_memory",
|
|
||||||
"entity_memory",
|
|
||||||
"external_memory",
|
|
||||||
"_telemetry",
|
"_telemetry",
|
||||||
"agents",
|
"agents",
|
||||||
"tasks",
|
"tasks",
|
||||||
@@ -1218,7 +1214,12 @@ class Crew(BaseModel):
|
|||||||
|
|
||||||
copied_data = self.model_dump(exclude=exclude)
|
copied_data = self.model_dump(exclude=exclude)
|
||||||
copied_data = {k: v for k, v in copied_data.items() if v is not None}
|
copied_data = {k: v for k, v in copied_data.items() if v is not None}
|
||||||
|
if self.short_term_memory:
|
||||||
|
copied_data["short_term_memory"] = self.short_term_memory.model_copy(deep=True)
|
||||||
|
if self.long_term_memory:
|
||||||
|
copied_data["long_term_memory"] = self.long_term_memory.model_copy(deep=True)
|
||||||
|
if self.entity_memory:
|
||||||
|
copied_data["entity_memory"] = self.entity_memory.model_copy(deep=True)
|
||||||
|
|
||||||
|
|
||||||
copied_data.pop("agents", None)
|
copied_data.pop("agents", None)
|
||||||
|
|||||||
@@ -4119,29 +4119,20 @@ def test_crew_kickoff_for_each_works_with_manager_agent_copy():
|
|||||||
assert crew_copy.manager_agent.role == crew.manager_agent.role
|
assert crew_copy.manager_agent.role == crew.manager_agent.role
|
||||||
assert crew_copy.manager_agent.goal == crew.manager_agent.goal
|
assert crew_copy.manager_agent.goal == crew.manager_agent.goal
|
||||||
|
|
||||||
def test_crew_copy_with_memory():
|
def test_crew_train_with_memory():
|
||||||
"""Test that copying a crew with memory enabled does not raise validation errors and copies memory correctly."""
|
"""Test that training a crew with memory enabled does not raise validation errors."""
|
||||||
agent = Agent(role="Test Agent", goal="Test Goal", backstory="Test Backstory")
|
agent = Agent(role="Test Agent", goal="Test Goal", backstory="Test Backstory")
|
||||||
task = Task(description="Test Task", expected_output="Test Output", agent=agent)
|
task = Task(description="Test Task", expected_output="Test Output", agent=agent)
|
||||||
crew = Crew(agents=[agent], tasks=[task], memory=True)
|
crew = Crew(agents=[agent], tasks=[task], memory=True)
|
||||||
|
|
||||||
assert crew._short_term_memory is not None, "Private short term memory should be initialized"
|
with tempfile.TemporaryDirectory() as tmpdir:
|
||||||
original_short_term_memory_id = id(crew._short_term_memory)
|
filename = os.path.join(tmpdir, "training_data.pkl")
|
||||||
|
try:
|
||||||
try:
|
crew.train(n_iterations=1, filename=filename)
|
||||||
crew_copy = crew.copy()
|
except pydantic_core.ValidationError as e:
|
||||||
|
if "Input should be an instance of" in str(e) and ("Memory" in str(e)):
|
||||||
assert hasattr(crew_copy, "_short_term_memory"), "Copied crew should have _short_term_memory attribute"
|
pytest.fail(f"Training with memory raised Pydantic ValidationError, likely due to incorrect memory copy: {e}")
|
||||||
assert crew_copy._short_term_memory is not None, "Private short term memory should be initialized in copy"
|
else:
|
||||||
assert id(crew_copy._short_term_memory) != original_short_term_memory_id, "Copied short term memory should be a new object instance"
|
raise e
|
||||||
assert hasattr(crew_copy, "_long_term_memory") and crew_copy._long_term_memory is not None
|
except Exception as e:
|
||||||
assert id(crew_copy._long_term_memory) != id(crew._long_term_memory)
|
print(f"Warning: Training raised an unexpected exception: {e}")
|
||||||
assert hasattr(crew_copy, "_entity_memory") and crew_copy._entity_memory is not None
|
|
||||||
assert id(crew_copy._entity_memory) != id(crew._entity_memory)
|
|
||||||
except pydantic_core.ValidationError as e:
|
|
||||||
if "Input should be an instance of" in str(e) and ("Memory" in str(e)):
|
|
||||||
pytest.fail(f"Copying with memory raised Pydantic ValidationError, likely due to incorrect memory copy: {e}")
|
|
||||||
else:
|
|
||||||
raise e
|
|
||||||
except Exception as e:
|
|
||||||
pytest.fail(f"Copying crew raised an unexpected exception: {e}")
|
|
||||||
|
|||||||
Reference in New Issue
Block a user