Skip to content

Framework for orchestrating role-playing, autonomous AI agents. By fostering collaborative intelligence, CrewAI empowers agents to work together seamlessly, tackling complex tasks.

fix: reject replay when stored tasks differ (#7155)

Shivangia3287100077e
diff --git a/lib/crewai/src/crewai/crew.py b/lib/crewai/src/crewai/crew.py
index 0f77b2d22..6f20044b5 100644
--- a/lib/crewai/src/crewai/crew.py
+++ b/lib/crewai/src/crewai/crew.py
@@ -2031,6 +2031,63 @@ class Crew(FlowTrackable, BaseModel):
None,
)
+ def _validate_replay_tasks(
+ self, stored_outputs: list[Any], start_index: int
+ ) -> None:
+ """Ensure stored outputs still correspond to the tasks that will receive them."""
+ if len(self.tasks) <= start_index:
+ raise ValueError(
+ "Cannot replay because the current crew does not match the stored task outputs."
+ )
+
+ stored_prefix = stored_outputs[: start_index + 1]
+ stored_task_keys = [
+ stored_output.get("task_key") for stored_output in stored_prefix
+ ]
+ current_task_keys = [task.key for task in self.tasks[: start_index + 1]]
+ if all(stored_task_keys):
+ if len(set(stored_task_keys)) != len(stored_task_keys) or len(
+ set(current_task_keys)
+ ) != len(current_task_keys):
+ raise ValueError(
+ "Cannot replay because the stored task identities are ambiguous."
+ )
+ if stored_task_keys != current_task_keys:
+ raise ValueError(
+ "Cannot replay because the current crew does not match the stored task outputs."
+ )
+ return
+
+ stored_identities = [
+ (
+ stored_output["output"].get("description"),
+ stored_output.get("expected_output"),
+ )
+ for stored_output in stored_prefix
+ ]
+ current_identities = [
+ (task.description, task.expected_output)
+ for task in self.tasks[: start_index + 1]
+ ]
+ if len(set(stored_identities)) != len(stored_identities) or len(
+ set(current_identities)
+ ) != len(current_identities):
+ raise ValueError(
+ "Cannot replay because the stored task identities are ambiguous."
+ )
+
+ for index, stored_output in enumerate(stored_prefix):
+ task = self.tasks[index]
+ output = stored_output["output"]
+ stored_expected_output = stored_output.get("expected_output")
+ if task.description != output.get("description") or (
+ stored_expected_output is not None
+ and task.expected_output != stored_expected_output
+ ):
+ raise ValueError(
+ "Cannot replay because the current crew does not match the stored task outputs."
+ )
+
def replay(self, task_id: str, inputs: dict[str, Any] | None = None) -> CrewOutput:
"""Replay the crew execution from a specific task."""
stored_outputs = self._task_output_handler.load()
@@ -2042,6 +2099,8 @@ class Crew(FlowTrackable, BaseModel):
if start_index is None:
raise ValueError(f"Task with id {task_id} not found in the crew's tasks.")
+ self._validate_replay_tasks(stored_outputs, start_index)
+
replay_inputs = (
inputs if inputs is not None else stored_outputs[start_index]["inputs"]
)
diff --git a/lib/crewai/src/crewai/memory/storage/kickoff_task_outputs_storage.py b/lib/crewai/src/crewai/memory/storage/kickoff_task_outputs_storage.py
index 0ff58cc9c..800c4b2df 100644
--- a/lib/crewai/src/crewai/memory/storage/kickoff_task_outputs_storage.py
+++ b/lib/crewai/src/crewai/memory/storage/kickoff_task_outputs_storage.py
@@ -32,8 +32,8 @@ class KickoffTaskOutputsSQLiteStorage:
"""Initialize the SQLite database and create the latest_kickoff_task_outputs table.
This method sets up the database schema for storing task outputs. It creates
- a table with columns for task_id, expected_output, output (as JSON),
- task_index, inputs (as JSON), was_replayed flag, and timestamp.
+ a table with columns for task_id, task_key, expected_output, output (as
+ JSON), task_index, inputs (as JSON), was_replayed flag, and timestamp.
Raises:
DatabaseOperationError: If database initialization fails due to SQLite errors.
@@ -47,6 +47,7 @@ class KickoffTaskOutputsSQLiteStorage:
"""
CREATE TABLE IF NOT EXISTS latest_kickoff_task_outputs (
task_id TEXT PRIMARY KEY,
+ task_key TEXT,
expected_output TEXT,
output JSON,
task_index INTEGER,
@@ -56,6 +57,16 @@ class KickoffTaskOutputsSQLiteStorage:
)
"""
)
+ columns = {
+ row[1]
+ for row in cursor.execute(
+ "PRAGMA table_info(latest_kickoff_task_outputs)"
+ )
+ }
+ if "task_key" not in columns:
+ cursor.execute(
+ "ALTER TABLE latest_kickoff_task_outputs ADD COLUMN task_key TEXT"
+ )
conn.commit()
except sqlite3.Error as e:
@@ -92,11 +103,12 @@ class KickoffTaskOutputsSQLiteStorage:
cursor.execute(
"""
INSERT OR REPLACE INTO latest_kickoff_task_outputs
- (task_id, expected_output, output, task_index, inputs, was_replayed)
- VALUES (?, ?, ?, ?, ?, ?)
+ (task_id, task_key, expected_output, output, task_index, inputs, was_replayed)
+ VALUES (?, ?, ?, ?, ?, ?, ?)
""",
(
str(task.id),
+ task.key,
task.expected_output,
json.dumps(output, cls=CrewJSONEncoder),
task_index,
@@ -174,7 +186,7 @@ class KickoffTaskOutputsSQLiteStorage:
with sqlite3.connect(self.db_path, timeout=30) as conn:
cursor = conn.cursor()
cursor.execute("""
- SELECT *
+ SELECT task_id, task_key, expected_output, output, task_index, inputs, was_replayed, timestamp
FROM latest_kickoff_task_outputs
ORDER BY task_index
""")
@@ -184,12 +196,13 @@ class KickoffTaskOutputsSQLiteStorage:
for row in rows:
result = {
"task_id": row[0],
- "expected_output": row[1],
- "output": json.loads(row[2]),
- "task_index": row[3],
- "inputs": json.loads(row[4]),
- "was_replayed": row[5],
- "timestamp": row[6],
+ "task_key": row[1],
+ "expected_output": row[2],
+ "output": json.loads(row[3]),
+ "task_index": row[4],
+ "inputs": json.loads(row[5]),
+ "was_replayed": row[6],
+ "timestamp": row[7],
}
results.append(result)
diff --git a/lib/crewai/src/crewai/utilities/task_output_storage_handler.py b/lib/crewai/src/crewai/utilities/task_output_storage_handler.py
index 2259bb833..cfff66815 100644
--- a/lib/crewai/src/crewai/utilities/task_output_storage_handler.py
+++ b/lib/crewai/src/crewai/utilities/task_output_storage_handler.py
@@ -43,6 +43,7 @@ class TaskOutputStorageHandler:
if log.get("was_replayed", False):
replayed = {
"task_id": str(log["task"].id),
+ "task_key": log["task"].key,
"expected_output": log["task"].expected_output,
"output": log["output"],
"was_replayed": log["was_replayed"],
diff --git a/lib/crewai/tests/test_crew.py b/lib/crewai/tests/test_crew.py
index 0195112cb..a960a6b09 100644
--- a/lib/crewai/tests/test_crew.py
+++ b/lib/crewai/tests/test_crew.py
@@ -3045,16 +3045,38 @@ def test_replay_feature(researcher, writer):
)
with patch.object(Task, "execute_sync") as mock_execute_task:
- mock_execute_task.return_value = TaskOutput(
- description="Mock description",
- raw="Mocked output for list of ideas",
- agent="Researcher",
- json_dict=None,
- output_format=OutputFormat.RAW,
- pydantic=None,
- summary="Mocked output for list of ideas",
- messages=[],
- )
+ mock_execute_task.side_effect = [
+ TaskOutput(
+ description=list_ideas.description,
+ raw="Mocked output for list of ideas",
+ agent="Researcher",
+ json_dict=None,
+ output_format=OutputFormat.RAW,
+ pydantic=None,
+ summary="Mocked output for list of ideas",
+ messages=[],
+ ),
+ TaskOutput(
+ description=write.description,
+ raw="Mocked output for list of ideas",
+ agent="Researcher",
+ json_dict=None,
+ output_format=OutputFormat.RAW,
+ pydantic=None,
+ summary="Mocked output for list of ideas",
+ messages=[],
+ ),
+ TaskOutput(
+ description=write.description,
+ raw="Mocked output for list of ideas",
+ agent="Researcher",
+ json_dict=None,
+ output_format=OutputFormat.RAW,
+ pydantic=None,
+ summary="Mocked output for list of ideas",
+ messages=[],
+ ),
+ ]
crew.kickoff()
crew.replay(str(write.id))
@@ -3062,6 +3084,154 @@ def test_replay_feature(researcher, writer):
assert mock_execute_task.call_count == 3
+def test_replay_rejects_changed_task_order(researcher):
+ """Replay must not restore a saved output onto a different current task."""
+ research = Task(
+ description="Research the topic",
+ expected_output="Research notes",
+ agent=researcher,
+ )
+ write = Task(
+ description="Write the article",
+ expected_output="An article",
+ agent=researcher,
+ )
+ plan = Task(
+ description="Plan the article",
+ expected_output="An outline",
+ agent=researcher,
+ )
+ crew = Crew(agents=[researcher], tasks=[plan, research, write])
+
+ stored_outputs = [
+ {
+ "task_id": str(research.id),
+ "expected_output": research.expected_output,
+ "output": {"description": research.description},
+ "inputs": {},
+ },
+ {
+ "task_id": str(write.id),
+ "expected_output": write.expected_output,
+ "output": {"description": write.description},
+ "inputs": {},
+ },
+ ]
+ with patch(
+ "crewai.utilities.task_output_storage_handler.TaskOutputStorageHandler.load",
+ return_value=stored_outputs,
+ ):
+ with pytest.raises(ValueError, match="current crew does not match"):
+ crew.replay(str(write.id))
+
+
+def test_replay_rejects_reordered_tasks_with_matching_expected_output(researcher):
+ """Task descriptions keep replay from confusing tasks with the same expected output."""
+ research = Task(
+ description="Research the topic",
+ expected_output="A report",
+ agent=researcher,
+ )
+ write = Task(
+ description="Write the article",
+ expected_output="A report",
+ agent=researcher,
+ )
+ crew = Crew(agents=[researcher], tasks=[write, research])
+
+ stored_outputs = [
+ {
+ "task_id": str(research.id),
+ "expected_output": research.expected_output,
+ "output": {"description": research.description},
+ "inputs": {},
+ },
+ {
+ "task_id": str(write.id),
+ "expected_output": write.expected_output,
+ "output": {"description": write.description},
+ "inputs": {},
+ },
+ ]
+ with patch(
+ "crewai.utilities.task_output_storage_handler.TaskOutputStorageHandler.load",
+ return_value=stored_outputs,
+ ):
+ with pytest.raises(ValueError, match="current crew does not match"):
+ crew.replay(str(write.id))
+
+
+def test_replay_rejects_ambiguous_task_identities(researcher):
+ """Replay must fail loud when persisted task details cannot identify a task."""
+ first_task = Task(
+ description="Write a report",
+ expected_output="A report",
+ agent=researcher,
+ )
+ second_task = Task(
+ description="Write a report",
+ expected_output="A report",
+ agent=researcher,
+ )
+ crew = Crew(agents=[researcher], tasks=[second_task, first_task])
+
+ stored_outputs = [
+ {
+ "task_id": str(first_task.id),
+ "expected_output": first_task.expected_output,
+ "output": {"description": first_task.description},
+ "inputs": {},
+ },
+ {
+ "task_id": str(second_task.id),
+ "expected_output": second_task.expected_output,
+ "output": {"description": second_task.description},
+ "inputs": {},
+ },
+ ]
+ with patch(
+ "crewai.utilities.task_output_storage_handler.TaskOutputStorageHandler.load",
+ return_value=stored_outputs,
+ ):
+ with pytest.raises(ValueError, match="task identities are ambiguous"):
+ crew.replay(str(second_task.id))
+
+
+def test_replay_uses_task_key_before_interpolating_new_inputs(researcher):
+ """A replayed template remains identifiable when the new inputs differ."""
+ task = Task(
+ description="Say hello to {name}",
+ expected_output="A greeting for {name}",
+ agent=researcher,
+ )
+ task.interpolate_inputs_and_add_conversation_history({"name": "John"})
+ stored_output = {
+ "task_id": str(task.id),
+ "task_key": task.key,
+ "expected_output": task.expected_output,
+ "output": {"description": task.description},
+ "inputs": {"name": "John"},
+ }
+ replay_task = Task(
+ description="Say hello to {name}",
+ expected_output="A greeting for {name}",
+ agent=researcher,
+ )
+ crew = Crew(agents=[researcher], tasks=[replay_task])
+
+ with (
+ patch(
+ "crewai.utilities.task_output_storage_handler.TaskOutputStorageHandler.load",
+ return_value=[stored_output],
+ ),
+ patch.object(crew, "_execute_tasks"),
+ ):
+ crew.replay(str(task.id), inputs={"name": "Maria"})
+
+ assert replay_task.description == "Say hello to Maria"
+ assert replay_task.expected_output == "A greeting for Maria"
+
+
@pytest.mark.vcr()
def test_crew_replay_error(researcher, writer):
task = Task(
@@ -3292,7 +3462,7 @@ def test_replay_with_context():
)
context_output = TaskOutput(
- description="Context Task Output",
+ description=task1.description,
agent="test_agent",
raw="context raw output",
pydantic=None,
@@ -3309,6 +3479,7 @@ def test_replay_with_context():
return_value=[
{
"task_id": str(task1.id),
+ "expected_output": task1.expected_output,
"output": {
"description": context_output.description,
"summary": context_output.summary,
@@ -3322,8 +3493,9 @@ def test_replay_with_context():
},
{
"task_id": str(task2.id),
+ "expected_output": task2.expected_output,
"output": {
- "description": "Test Task Output",
+ "description": task2.description,
"summary": None,
"raw": "test raw output",
"pydantic": None,
@@ -3394,6 +3566,7 @@ def test_replay_with_invalid_task_id():
return_value=[
{
"task_id": str(task1.id),
+ "task_key": task1.key,
"output": {
"description": context_output.description,
"summary": context_output.summary,
@@ -3407,6 +3580,7 @@ def test_replay_with_invalid_task_id():
},
{
"task_id": str(task2.id),
+ "task_key": task2.key,
"output": {
"description": "Test Task Output",
"summary": None,
@@ -3460,6 +3634,7 @@ def test_replay_interpolates_inputs_properly(mock_interpolate_inputs):
return_value=[
{
"task_id": str(task1.id),
+ "task_key": task1.key,
"output": {
"description": context_output.description,
"summary": context_output.summary,
@@ -3473,6 +3648,7 @@ def test_replay_interpolates_inputs_properly(mock_interpolate_inputs):
},
{
"task_id": str(task2.id),
+ "task_key": task2.key,
"output": {
"description": "Test Task Output",
"summary": None,
@@ -3501,7 +3677,7 @@ def test_replay_setup_context():
agent=agent,
)
context_output = TaskOutput(
- description="Context Task Output",
+ description=task1.description,
agent="test_agent",
raw="context raw output",
pydantic=None,
@@ -3525,12 +3701,13 @@ def test_replay_setup_context():
"output_format": context_output.output_format,
"agent": context_output.agent,
},
+ "expected_output": task1.expected_output,
"inputs": {"name": "John"},
},
{
"task_id": str(task2.id),
"output": {
- "description": "Test Task Output",
+ "description": task2.description,
"summary": None,
"raw": "test raw output",
"pydantic": None,
@@ -3538,6 +3715,7 @@ def test_replay_setup_context():
"output_format": "json",
"agent": "test_agent",
},
+ "expected_output": task2.expected_output,
"inputs": {"name": "John"},
},
],
@@ -3546,7 +3724,7 @@ def test_replay_setup_context():
assert crew.tasks[0].output is not None
assert isinstance(crew.tasks[0].output, TaskOutput)
- assert crew.tasks[0].output.description == "Context Task Output"
+ assert crew.tasks[0].output.description == task1.description
assert crew.tasks[0].output.agent == "test_agent"
assert crew.tasks[0].output.raw == "context raw output"
assert crew.tasks[0].output.output_format == OutputFormat.RAW