diff --git a/src/glider/core/flow_engine.py b/src/glider/core/flow_engine.py index 404cca6e..7b5d4d4e 100644 --- a/src/glider/core/flow_engine.py +++ b/src/glider/core/flow_engine.py @@ -510,8 +510,10 @@ def on_exec_output( logger.debug(f"Skipping callback - output '{output_name}' != expected '{expected}'") return None logger.debug(f"Exec callback fired: {fn}:{fo} -> {tn}, flow state: {self._state}") - # Execution has left the source node. - self._notify_node_active(fn, False) + # Execution has left the source node -- unless it keeps running + # after this output (a Loop's body); then it clears on return. + if output_name not in getattr(from_node_obj, "STAYS_ACTIVE_ON", ()): + self._notify_node_active(fn, False) if self._state == FlowState.RUNNING: logger.info(f"Propagating execution: {fn} -> {tn}") task = asyncio.create_task(self._propagate_execution(fn, fo, tn)) diff --git a/src/glider/nodes/control_nodes.py b/src/glider/nodes/control_nodes.py index b9a3d215..1f5b7575 100644 --- a/src/glider/nodes/control_nodes.py +++ b/src/glider/nodes/control_nodes.py @@ -44,6 +44,9 @@ class LoopNode(GliderNode): ], ) + # Still running after firing body: stays lit on the canvas (FlowEngine). + STAYS_ACTIVE_ON = ("body",) + PROPERTIES_SCHEMA = [ { "key": "count", diff --git a/tests/unit/core/test_flow_node_active.py b/tests/unit/core/test_flow_node_active.py index 211f3aff..586e67cb 100644 --- a/tests/unit/core/test_flow_node_active.py +++ b/tests/unit/core/test_flow_node_active.py @@ -37,3 +37,32 @@ async def test_a_node_is_active_from_entry_until_it_fires_on(): # Nothing is left lit once the flow stops. last = dict(events) assert not any(last.values()) + + +async def test_a_loop_stays_active_through_its_body_and_waits(): + register_experiment_nodes(FlowEngine) + register_control_nodes(FlowEngine) + register_logic_nodes(FlowEngine) + engine = FlowEngine() + engine.initialize() + engine.create_node("start", "StartExperiment", state={}) + engine.create_node("loop", "Loop", state={"count": 2, "delay": 0.1}) + engine.create_node("delay", "Delay", state={"duration": 0.02}) + engine.create_node("end", "EndExperiment", state={}) + engine.create_connection("c0", "start", 0, "loop", 0, "exec") + engine.create_connection("c1", "loop", 0, "delay", 0, "exec") + engine.create_connection("c2", "loop", 1, "end", 0, "exec") + + events: list[tuple[str, bool]] = [] + engine.on_node_active(lambda node_id, active: events.append((node_id, active))) + done = asyncio.Event() + engine.on_flow_complete(done.set) + + await engine.start() + await asyncio.wait_for(done.wait(), 5) + await engine.stop() + + # Lit once on entry, dark once when done fires: firing body doesn't clear it. + assert [e for e in events if e[0] == "loop"][0] == ("loop", True) + last_body = max(i for i, e in enumerate(events) if e == ("delay", False)) + assert events.index(("loop", False)) > last_body