Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow parent tasks to optionally wait for subtasks #346

Merged
merged 1 commit into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/controlflow/orchestration/orchestrator.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,10 @@ def collect_tasks(task: Task):
for dependency in task.depends_on:
collect_tasks(dependency)

# Collect parent
if task.parent and not task.parent.wait_for_subtasks:
collect_tasks(task.parent)

# Check if the task is ready
if task.is_ready():
ready_tasks.append(task)
Expand Down
10 changes: 9 additions & 1 deletion src/controlflow/tasks/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ class Task(ControlFlowModel):
"which this task is considered `assigned`.",
)
created_at: datetime.datetime = Field(default_factory=datetime.datetime.now)
wait_for_subtasks: bool = Field(
default=True,
description="If True, the task will not be considered ready until all subtasks are complete.",
)
_subtasks: set["Task"] = set()
_downstreams: set["Task"] = set()
_cm_stack: list[contextmanager] = []
Expand Down Expand Up @@ -465,7 +469,11 @@ def is_ready(self) -> bool:
Returns True if all dependencies are complete and this task is
incomplete, meaning it is ready to be worked on.
"""
return self.is_incomplete() and all(t.is_complete() for t in self.depends_on)
depends_on = self.depends_on
if not self.wait_for_subtasks:
depends_on = depends_on.difference(self._subtasks)

return self.is_incomplete() and all(t.is_complete() for t in depends_on)

def get_agents(self) -> list[Agent]:
if self.agents is not None:
Expand Down