Repository navigation
Description
Description
TodoList.replace_pending_todos keeps the completed, failed and running steps and appends the replanned ones.
The planner schema asks for 1-based step_number, so a replan usually restarts at 1.
The list then holds duplicate step numbers, for example [1, 2, 1, 2, 3].
Every lookup uses get_by_step_number, which returns the first match.
New steps therefore alias the preserved ones:
get_ready_todossees a new step's dependency as the old, finished step. New steps that depend on each other are all "ready" at once.mark_runningandmark_completedchange the old step. The new step stays pending and is picked again. The old result is overwritten.state.observationsandexecution_logare keyed by step number and mix old and new steps.
Steps to Reproduce
- Run an agent with
planning=True(Plan-and-Execute path inAgentExecutor). - Let step 1 complete and step 2 fail, so the observer asks for a replan.
- The planner returns the revised plan with steps numbered 1, 2, 3 (step 2 depends on 1, step 3 on 2).
- The executor calls
replace_pending_todoswith these steps.
The same state can be built without an LLM:
from crewai.utilities.planning_types import TodoItem, TodoList
todos = TodoList(items=[
TodoItem(step_number=1, description="old 1", status="completed", result="A"),
TodoItem(step_number=2, description="old 2", status="failed", result="Error"),
TodoItem(step_number=3, description="old 3"),
])
todos.replace_pending_todos([
TodoItem(step_number=1, description="new 1"),
TodoItem(step_number=2, description="new 2", depends_on=[1]),
TodoItem(step_number=3, description="new 3", depends_on=[2]),
])
print([t.step_number for t in todos.items])
print([t.description for t in todos.get_ready_todos()])
todos.mark_running(1)
print([(t.description, t.status) for t in todos.items])Expected behavior
- Step numbers stay unique after a replan.
- Only
new 1is ready.new 2waits fornew 1. mark_running(<new step>)changes the new step and leavesold 1completed.
Screenshots/Code snippets
Output on main:
[1, 2, 1, 2, 3]
['new 1', 'new 2', 'new 3']
[('old 1', 'running'), ('old 2', 'failed'), ('new 1', 'pending'), ('new 2', 'pending'), ('new 3', 'pending')]
Through the executor (_trigger_replan with a mocked AgentReasoning that returns steps 1, 2, 3), get_ready_todos_method returns multiple_todos_ready instead of single_todo_ready.
execute_todos_parallel then runs all three new steps at once. It marks and completes the old items, so the new items stay pending.
Operating System
Windows 11
Python Version
3.13 (the form lists 3.10 to 3.12, nothing in the code depends on it)
crewAI Version
1.15.35 (main at 6bd60b2).
crewAI Tools Version
1.15.35
Virtual Environment
Venv (uv)
Evidence
Tests on main (new tests from the fix branch run against unfixed code):
FAILED test_planning_types.py::TestReplacePendingTodos::test_restarted_numbering_does_not_collide_with_preserved_steps
FAILED test_planning_types.py::TestReplacePendingTodos::test_only_first_new_step_is_ready_after_restarted_numbering
FAILED test_planning_types.py::TestReplacePendingTodos::test_marking_a_new_step_running_leaves_preserved_steps_alone
FAILED test_planning_types.py::TestReplacePendingTodos::test_completing_a_new_step_does_not_touch_preserved_results
FAILED test_planning_types.py::TestReplacePendingTodos::test_running_step_keeps_its_number_when_plan_is_replaced
FAILED test_agent_executor.py::TestReplanKeepsStepNumbersUnique (3 tests)
AssertionError: assert 'multiple_todos_ready' == 'single_todo_ready'
assert 5 == 3 where 5 = len([1, 2, 1, 2, 3])
Possible Solution
In replace_pending_todos, when a new step number collides with a preserved one, renumber the new steps after the highest preserved number.
Remap depends_on entries that point at other new steps.
Leave a replan alone when it already continues the numbering (3, 4, 5 after steps 1 and 2).
Additional context
No existing replan test covers replace_pending_todos or _trigger_replan.
Searches run (all without a match for this bug):
gh issue list --state all --search "replan step_number": only [BUG] Parallel planning swallows tool_failure_policy="raise" and runs downstream agents #8088 (unrelated, parallel planning and tool_failure_policy).gh pr list --state all --search "replace_pending_todos": only Lorenze/feat/plan execute pattern #4817 and Lorenze/feat/planning pt 3 todo list execution #4450 (the original feature PRs).gh issue list --state all --search "completed step replan duplicate": nothing relevant.- Local grep of all 2821 issue and 4000 PR titles for
replan,todo,step_number,depends_on,renumber: no match. git log --since=45.days -- lib/crewai/src/crewai/utilities/planning_types.py: no commits.
Activity