Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

refactor(execution): route empty flat for generically

JPPhoto890f1e7f4bbb
diff --git a/docs/src/content/docs/contributing/loop-nodes.md b/docs/src/content/docs/contributing/loop-nodes.md
index a3775e5cb3..048faad34e 100644
--- a/docs/src/content/docs/contributing/loop-nodes.md
+++ b/docs/src/content/docs/contributing/loop-nodes.md
@@ -97,13 +97,15 @@ The execution-engine seam is additive to this loop contract. The session runner
iteration and state; the `ForReturn` effect completes it with output, state, and the continue decision. These effects
are persisted under the invocation reference. Session-built effect references carry graph-state, durable-frame,
iteration-path, and workflow-call-depth identity, and stale or cross-scope effects are rejected before mutation. They
-never encode `loop_linkage` as a data token. A fresh graph with exactly one static, non-empty flat `For`/`ForReturn`
-pair now uses the generic scheduler adapter for readiness and continuation transitions. Graph state owns the
+never encode `loop_linkage` as a data token. A fresh graph with exactly one static flat `For`/`ForReturn` pair,
+including an empty literal collection, now uses the generic scheduler adapter for readiness and continuation transitions.
+Graph state owns the
generic continuation boundary: it carries returned state, honors `continue_condition`, materializes the next body
-iteration, and finalizes the aggregate. The generic scheduler remains opaque and never receives a literal successor
-node ID. The compatibility continuation bridge is retained only for unsupported loop shapes and explicitly
-legacy-loaded snapshots. Empty or input-driven outer collections, deeper or multiple nested loops, and unsupported mixed
-control flow remain on the compatibility scheduler; the narrow canonical two-level nested-`For` shape and the exact
+iteration, and finalizes the aggregate. An empty literal collection completes through the existing synthetic terminal
+`For` result without running the body or `ForReturn`. The generic scheduler remains opaque and never receives a literal
+successor node ID. The compatibility continuation bridge is retained only for unsupported loop shapes and explicitly
+legacy-loaded snapshots. Input-driven outer collections, deeper or multiple nested loops, and unsupported mixed control
+flow remain on the compatibility scheduler; the narrow canonical two-level nested-`For` shape and the exact
bounded outer-`For`/`Iterate`/`Collect` shape are now generic-routed. This additive effect seam does not claim generic
scheduling for the remaining shapes. `Iterate` records ordered item tokens in
a closed `StreamBuffer`; an empty `Iterate` records an
diff --git a/invokeai/app/services/shared/README.md b/invokeai/app/services/shared/README.md
index 4e6244aa19..afa681f26a 100644
--- a/invokeai/app/services/shared/README.md
+++ b/invokeai/app/services/shared/README.md
@@ -406,13 +406,14 @@ four-node source/Iterate/body/Collect shape and any number of ordinary downstrea
private planner instead.
No author-time activation ports or literal successor IDs are introduced.
-For a fresh graph with one static, non-empty flat `For`/`ForReturn` pair, the same adapter now projects ordinary
-readiness and calls the graph-state generic continuation boundary after each `ForReturn`. That boundary carries returned
-state, honors `continue_condition`, creates the next prepared iteration when needed, and finalizes `output_collection`
-and `final_state`. The generic scheduler receives only opaque node IDs and dependencies; it never receives a literal
-next node ID. The compatibility continuation bridge remains available only for unsupported loop shapes and explicitly
-legacy-loaded snapshots. Unsupported empty/input-driven outer collections and deeper, multiple, or unsupported mixed
-loop shapes remain compatibility-owned; the narrow canonical two-level nested-`For` shape, the canonical bounded
+For a fresh graph with one static flat `For`/`ForReturn` pair, including the exact empty literal collection case, the
+same adapter now projects ordinary readiness and calls the graph-state generic continuation boundary after each
+`ForReturn` when iterations exist. That boundary carries returned state, honors `continue_condition`, creates the next
+prepared iteration when needed, and finalizes `output_collection` and `final_state`. For an empty literal collection,
+the generic path completes through the existing synthetic terminal `For` result without running the body or `ForReturn`.
+The generic scheduler receives only opaque node IDs and dependencies; it never receives a literal next node ID. The
+compatibility continuation bridge remains available only for unsupported loop shapes and explicitly legacy-loaded snapshots.
+Input-driven outer collections, deeper, multiple, or unsupported mixed loop shapes remain compatibility-owned; the narrow canonical two-level nested-`For` shape, the canonical bounded
internal `Iterate`/`Collect` shape, the exact producer-driven outer nested extension, and the bounded two- through
six-level serial nested-`Iterate` chains are generic-routed. On
rehydration, the generic adapter restores the active class-drain
diff --git a/invokeai/app/services/shared/graph.py b/invokeai/app/services/shared/graph.py
index 2edaf86da5..e6948b656c 100644
--- a/invokeai/app/services/shared/graph.py
+++ b/invokeai/app/services/shared/graph.py
@@ -802,7 +802,7 @@ class GraphExecutionState(BaseModel):
return True
if len(for_nodes) != 1 or len(return_nodes) != 1:
return False
- if self.graph._get_input_edges(for_nodes[0].id, COLLECTION_FIELD) or not for_nodes[0].collection:
+ if self.graph._get_input_edges(for_nodes[0].id, COLLECTION_FIELD):
return False
body_path = self.graph._get_for_body_path_to_return(for_nodes[0].id, self._get_source_graph_flat())
if body_path is None:
diff --git a/tests/app/services/shared/test_execution_engine_differential.py b/tests/app/services/shared/test_execution_engine_differential.py
index 39df73c6df..89a5763c8f 100644
--- a/tests/app/services/shared/test_execution_engine_differential.py
+++ b/tests/app/services/shared/test_execution_engine_differential.py
@@ -4892,10 +4892,33 @@ def test_flat_for_frame_and_continuation_identity_matches_compatibility_schedule
assert other_frames.isdisjoint(generic_frames)
+def test_empty_literal_flat_for_uses_generic_scheduler() -> None:
+ state = GraphExecutionState(graph=_flat_for_graph(collection=[]))
+
+ assert state._can_use_generic_scheduler()
+ assert isinstance(state._scheduler(), _GenericGraphSchedulerAdapter)
+
+
+def test_empty_literal_flat_for_matches_compatibility_scheduler() -> None:
+ generic_trace, generic_state = _run_graph_with_effects(GraphExecutionState(graph=_flat_for_graph(collection=[])))
+ compatibility_trace, compatibility_state = _run_graph_with_effects(
+ GraphExecutionState(graph=_flat_for_graph(collection=[])),
+ force_compatibility_scheduler=True,
+ )
+
+ assert generic_trace == compatibility_trace == []
+ assert _final_for_output(generic_state).output_collection == []
+ assert _final_for_output(compatibility_state).output_collection == []
+ assert generic_state.is_complete()
+ assert compatibility_state.is_complete()
+ assert isinstance(generic_state._execution_scheduler, _GenericGraphSchedulerAdapter)
+ assert isinstance(compatibility_state._execution_scheduler, _ExecutionScheduler)
+ assert _state_projection(generic_state) == _state_projection(compatibility_state)
+
+
@pytest.mark.parametrize(
("graph_kwargs", "expected_trace", "expected_collection"),
[
- ({"collection": []}, [], []),
({"input_collection": [1, 2]}, ["collection", "for", "body", "return", "for", "body", "return"], [11, 12]),
],
)
@@ -4927,6 +4950,20 @@ def test_flat_for_ineligible_collections_use_compatibility_scheduler(
_assert_execution_identity_consistent(restored)
+def test_input_driven_empty_flat_for_uses_compatibility_scheduler() -> None:
+ state = GraphExecutionState(graph=_flat_for_graph(input_collection=[]))
+
+ assert state._can_use_generic_scheduler() is False
+ trace, state = _run_graph_with_effects(state)
+
+ assert trace == ["collection"]
+ assert isinstance(state._execution_scheduler, _ExecutionScheduler)
+ assert _final_for_output(state).output_collection == []
+ assert state.is_complete()
+ assert list(state._generic_runtime().continuations.values()) == []
+ _assert_execution_identity_consistent(state)
+
+
@pytest.mark.parametrize(
("continue_condition", "expected_trace", "expected_collection", "expected_state"),
[