Skip to content

Conversation

devin-ai-integration[bot]
Copy link
Contributor

@devin-ai-integration devin-ai-integration bot commented Sep 29, 2025

feat: Add dynamic task ordering capability to Sequential and Hierarchical processes

Summary

Implements dynamic task ordering for CrewAI's Sequential and Hierarchical processes by adding a task_ordering_callback field to the Crew class. This allows users to provide a callback function that dynamically determines the next task to execute based on runtime conditions, completed task outputs, and current execution context.

Key Changes:

  • Added task_ordering_callback field to Crew class with proper type annotations and validation
  • Modified _execute_tasks method to support dynamic task selection via callback
  • Implemented comprehensive error handling and fallback to default ordering
  • Added validation to ensure callback has correct signature (3 parameters)
  • Maintains full backward compatibility - crews without callbacks work unchanged
  • Supports returning task index (int), Task object, or None from callback

Review & Testing Checklist for Human

⚠️ HIGH PRIORITY - 5 critical items to verify:

  • Test basic functionality end-to-end - Create crews with and without task_ordering_callback and verify they execute correctly in both Sequential and Hierarchical processes
  • Verify backward compatibility - Ensure existing crews without callbacks work exactly as before (no regressions)
  • Test callback error handling - Verify that invalid callback returns (bad indices, exceptions, wrong types) gracefully fall back to default ordering with appropriate warnings
  • Review the while loop execution logic - The core _execute_tasks method was changed from a for loop to a while loop with complex state tracking - ensure no edge cases or infinite loops
  • Test infinite loop prevention - Verify that callbacks returning the same task repeatedly don't cause infinite loops (should be prevented by executed_task_indices tracking)

Notes


Note

Adds task_ordering_callback to Crew and updates task execution to dynamically select the next task with validation, fallbacks, and tests.

  • Core Execution:
    • Add task_ordering_callback field to Crew to dynamically choose the next task (accepts index, Task, or None).
    • Refactor _execute_tasks to a loop tracking executed indices, invoking callback, handling async/sync, and preventing repeats.
    • Default ordering preserved when no callback or invalid returns (with warnings).
  • Validation:
    • Add model_validator to verify callback signature (exactly 3 params) and callability.
  • Examples:
    • New examples/dynamic_task_ordering_example.py showing priority- and condition-based ordering.
  • Tests:
    • Add tests/test_dynamic_task_ordering.py covering reverse/priority ordering, returning Task vs index, invalid indices/types, exceptions, default behavior, and infinite-loop prevention.

Written by Cursor Bugbot for commit 1de7dcd. This will update automatically on new commits. Configure here.

…ical processes

- Add task_ordering_callback field to Crew class with proper validation
- Implement dynamic task selection in _execute_tasks method
- Add comprehensive error handling and validation for callback
- Include tests for various ordering scenarios and edge cases
- Maintain backward compatibility with existing code
- Support both task index and Task object returns from callback
- Add example demonstrating priority-based and conditional ordering

Fixes #3620

Co-Authored-By: João <[email protected]>
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

cursor[bot]

This comment was marked as outdated.

- Fix W293 lint errors by removing trailing whitespace from blank lines
- Ensures compliance with Ruff formatting standards

Co-Authored-By: João <[email protected]>
cursor[bot]

This comment was marked as outdated.

- Fix task completion tracking to use task.output instead of non-existent task_id
- Update callback validation to raise ValueError instead of PydanticCustomError
- Refactor _execute_tasks to prevent task skipping and ensure all tasks execute exactly once
- Maintain replay functionality compatibility with dynamic ordering
- Remove undefined current_index variable reference

Addresses all 3 bugs reported by automated analysis:
1. Task Skipping and Replay Breakage
2. Callback Validation Error Handling Mismatch
3. TaskOutput Missing task_id Causes Errors

Co-Authored-By: João <[email protected]>
cursor[bot]

This comment was marked as outdated.

- Move signature parameter count validation outside try-except block
- Only catch exceptions from inspect.signature(), not validation errors
- Ensures invalid callbacks properly raise ValueError instead of passing silently
- Addresses final Cursor Bugbot issue with callback validation bypass

This resolves the remaining CI issue where the try-except block was too broad
and caught the ValueError that should propagate for invalid callback signatures.

Co-Authored-By: João <[email protected]>
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.

[FEATURE]
0 participants