fix: address review comments - add runtime enforcement and remove unused code

- Remove unused 'import asyncio' from test file
- Remove unused 'original_execute_sync' variable from test
- Add defensive sorted() by _execution_index in _execute_tasks and _aexecute_tasks
  so the field is actively used at runtime, not just metadata

Co-Authored-By: João <joao@crewai.com>
This commit is contained in:
Devin AI
2026-03-01 16:34:08 +00:00
parent 62bdc0f3b2
commit 9e18f4855a
2 changed files with 16 additions and 4 deletions

View File

@@ -999,7 +999,10 @@ class Crew(FlowTrackable, BaseModel):
**Ordering contract**: tasks are dispatched in the exact order they
appear in *tasks* (i.e. their insertion / list order). Each task
carries an ``_execution_index`` stamped at crew-construction time
that locks this order deterministically.
that locks this order deterministically. As a defensive measure
the task list is sorted by ``_execution_index`` before iteration
so ordering is enforced even if the list is mutated after
construction.
Args:
tasks: List of tasks to execute (preserves insertion order).
@@ -1009,6 +1012,10 @@ class Crew(FlowTrackable, BaseModel):
Returns:
CrewOutput: Final output of the crew.
"""
tasks = sorted(
tasks,
key=lambda t: t._execution_index if t._execution_index is not None else 0,
)
task_outputs: list[TaskOutput] = []
pending_tasks: list[tuple[Task, asyncio.Task[TaskOutput], int]] = []
last_sync_output: TaskOutput | None = None
@@ -1203,7 +1210,10 @@ class Crew(FlowTrackable, BaseModel):
**Ordering contract**: tasks are dispatched in the exact order they
appear in *tasks* (i.e. their insertion / list order). Each task
carries an ``_execution_index`` stamped at crew-construction time
that locks this order deterministically.
that locks this order deterministically. As a defensive measure
the task list is sorted by ``_execution_index`` before iteration
so ordering is enforced even if the list is mutated after
construction.
Args:
tasks: List of tasks to execute (preserves insertion order).
@@ -1213,6 +1223,10 @@ class Crew(FlowTrackable, BaseModel):
Returns:
CrewOutput: Final output of the crew.
"""
tasks = sorted(
tasks,
key=lambda t: t._execution_index if t._execution_index is not None else 0,
)
custom_start = self._get_execution_start_index(tasks)
if custom_start is not None:
start_index = custom_start

View File

@@ -9,7 +9,6 @@ See: https://github.com/crewAIInc/crewAI/issues/4664
from __future__ import annotations
import asyncio
from unittest.mock import patch
import pytest
@@ -129,7 +128,6 @@ class TestDeterministicSyncOrder:
for _ in range(ITERATIONS):
dispatch_order: list[str] = []
original_execute_sync = Task.execute_sync
def tracking_execute_sync(self_task, *args, **kwargs):
dispatch_order.append(self_task.description)