diff --git a/src/controlflow/flows/flow.py b/src/controlflow/flows/flow.py index 35a78952..8bcf9369 100644 --- a/src/controlflow/flows/flow.py +++ b/src/controlflow/flows/flow.py @@ -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 diff --git a/tests/flows/test_flows.py b/tests/flows/test_flows.py index 9da9851f..f5a85ac3 100644 --- a/tests/flows/test_flows.py +++ b/tests/flows/test_flows.py @@ -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):