Skip to content

Conversation

@devin-ai-integration
Copy link
Contributor

Summary

Fixes issue #3953 where the task planner was attaching plans to the wrong tasks when the LLM returned them out of order.

The Problem: The original implementation used zip() to blindly pair tasks with plans, assuming they were in the same order. However, LLMs sometimes return plans in unexpected order (e.g., starting with "Task Number 21" instead of "Task Number 1"), causing plan mismatches.

The Solution:

  • Extract task numbers from each plan using regex pattern matching on the task field
  • Build a mapping of task number → plan
  • Apply plans to tasks based on correct task number matches
  • Add warning logs when task numbers can't be extracted or plans are missing

Changes:

  • Modified Crew._handle_crew_planning() to use task number extraction instead of positional matching
  • Added comprehensive test that reproduces the bug with plans in wrong order and verifies the fix

Review & Testing Checklist for Human

  • Test with real LLM planning: Run a crew with planning=True and 5-10 tasks to verify the regex pattern r"Task Number (\d+)" correctly matches actual LLM outputs from the planning agent
  • Verify warning logs: Check that warning messages are visible and actionable when plans don't match (test by manually creating a malformed plan response)
  • Edge case: Different task number formats: Consider if LLMs might format task numbers differently (e.g., "task number" lowercase, "Task Fix tiny typo #1", "Task 1:", etc.) and whether the regex should be more flexible
  • Large crew test: Test with a crew that has 20+ tasks (matching the original issue report mentioning "Task Number 21") to ensure the fix works at scale

Test Plan

  1. Create a crew with 5-10 tasks and planning=True
  2. Run crew.kickoff() and verify each task receives the correct plan in its description
  3. Check logs for any warnings about missing or mismatched plans
  4. Optionally: Add a print statement in _handle_crew_planning() to log the plan_map and verify task numbers are extracted correctly

Notes

  • The fix assumes task numbers in plans follow the format "Task Number X" (case-insensitive) as generated by CrewPlanner._create_tasks_summary()
  • If a plan's task number can't be extracted, it's logged as a warning but execution continues
  • The test uses mocked LLM responses, so real-world testing is important to validate the regex pattern

Link to Devin run: https://app.devin.ai/sessions/c96705b2147b411682a3b3d6339b4a13
Requested by: João (joao@crewai.com)

The task planner was blindly zipping tasks with plans returned by the LLM,
assuming they were in the same order. However, the LLM sometimes returns
plans in the wrong order (e.g., starting with Task 21 instead of Task 1),
causing plans to be attached to the wrong tasks.

This fix:
- Extracts the task number from each plan's task field using regex
- Creates a mapping of task number to plan
- Applies plans to tasks based on the correct task number match
- Adds warning logs when task numbers can't be extracted or plans are missing

Added comprehensive test that reproduces the bug by mocking an LLM response
with plans in the wrong order and verifies the fix correctly matches plans
to their corresponding tasks.

Co-Authored-By: João <joao@crewai.com>
@devin-ai-integration
Copy link
Contributor Author

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant