Skip to content

[BUG] Replanning restarts step numbers at 1, so new plan steps alias the preserved completed steps #9133

Open

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_todos sees a new step's dependency as the old, finished step. New steps that depend on each other are all "ready" at once.
  • mark_running and mark_completed change the old step. The new step stays pending and is picked again. The old result is overwritten.
  • state.observations and execution_log are keyed by step number and mix old and new steps.

Steps to Reproduce

  1. Run an agent with planning=True (Plan-and-Execute path in AgentExecutor).
  2. Let step 1 complete and step 2 fail, so the observer asks for a replan.
  3. The planner returns the revised plan with steps numbered 1, 2, 3 (step 2 depends on 1, step 3 on 2).
  4. The executor calls replace_pending_todos with 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 1 is ready. new 2 waits for new 1.
  • mark_running(<new step>) changes the new step and leaves old 1 completed.

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):

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions