Skip to content

Commit

Permalink
Reset task parent tracking when nested flows are created
Browse files Browse the repository at this point in the history
  • Loading branch information
jlowin committed Sep 6, 2024
1 parent a811d6d commit e7d7f47
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/controlflow/flows/flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ def add_events(self, events: list[Event]):

@contextmanager
def create_context(self):
with ctx(flow=self):
# creating a new flow will reset any parent task tracking
with ctx(flow=self, tasks=None):
yield self


Expand Down
11 changes: 11 additions & 0 deletions tests/flows/test_flows.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,17 @@ def test_get_flow_nested_contexts(self):
assert get_flow() == flow1
assert get_flow() is None

def test_flow_context_resets_task_tracking(self):
parent_task = Task("Parent task")
with parent_task:
assert ctx.get("tasks") == [parent_task]
with Flow():
assert ctx.get("tasks") is None
nested_task = Task("Nested task")
assert nested_task.parent is None
assert ctx.get("tasks") == [parent_task]
assert ctx.get("tasks") == []


class TestFlowHistory:
def test_get_events_empty(self):
Expand Down

0 comments on commit e7d7f47

Please sign in to comment.