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.

test(execution): prove mixed If ownership boundary

JPPhoto715846865b9c
diff --git a/docs/src/content/docs/contributing/loop-nodes.md b/docs/src/content/docs/contributing/loop-nodes.md
index 3cbafba677..98b6e86272 100644
--- a/docs/src/content/docs/contributing/loop-nodes.md
+++ b/docs/src/content/docs/contributing/loop-nodes.md
@@ -177,8 +177,9 @@ consumer, the exact bounded three-`If` chain with direct inner-to-middle-to-oute
chain, or exactly two, exactly three, or exactly four independent ordinary-node sibling `If`s compile opaque, frame-local
activation dependencies in graph state. The three- and four-`If` admissions require `default` edges, all branch inputs
(`condition`, `true_input`, `false_input`) on each `If`, and no output from inner or middle `If` except its direct nested
-branch edge; the four-`If` chain permits no extra output fan-out. Five-or-more nested `If`s, other fan-out, mixed,
-loop-containing, saved-workflow, legacy, and five-or-more sibling shapes use the compatibility scheduler with the dedicated
+branch edge; the four-`If` chain permits no extra output fan-out. Five-or-more nested `If`s, other fan-out, mixed
+shapes outside the bounded per-item `Iterate`/`If`/`Collect` topology, loop-containing, saved-workflow, legacy, and
+five-or-more sibling shapes use the compatibility scheduler with the dedicated
`_IfActivationController` fallback. Legacy snapshots retain their generic compatibility
projection. Legacy
skipped-state projection remains only for old snapshots. Fresh materialization prepares the condition boundary,
diff --git a/invokeai/app/services/shared/README.md b/invokeai/app/services/shared/README.md
index ceae4dd386..bd041e6241 100644
--- a/invokeai/app/services/shared/README.md
+++ b/invokeai/app/services/shared/README.md
@@ -638,10 +638,11 @@ legacy unmarked snapshots are treated as version 0, while unreadable snapshots a
iterator ancestors because the source path crosses a collector, prepared parent iteration paths are still used to
materialize one downstream exec node for each preserved collector path.
-- For a fresh **IfInvocation**: prepare only its condition inputs first. After the condition resolves, the activation
- controller admits selected branch contexts and attaches only the selected branch input to the pending `If` execution.
- Rejected branch sources are not materialized. Pending `If` state is reconstructed from its durable execution edges,
- prepared mappings, and activation token after rehydration.
+- For a fresh **IfInvocation**: prepare only its condition inputs first. After the condition resolves, graph-state
+ activation dependencies admit selected branch contexts and attach only the selected branch input to the pending `If`
+ execution. Unsupported fresh shapes and legacy snapshots use the compatibility controller. Rejected branch sources
+ are not materialized. Pending `If` state is reconstructed from its durable execution edges, prepared mappings, and
+ activation token after rehydration.
- For each new exec node:
@@ -699,10 +700,11 @@ Run `C` -> `D:0` -> enqueue `D`. Run `D` -> done.
- The `condition` input must resolve first.
- Nodes that are exclusive to the true or false branch remain unmaterialized until their branch is admitted, even when
their indegree would otherwise be zero.
-- Once the condition resolves, the controller records the selected activation token and attaches only the selected
- branch input to the pending `If` execution. Branch-exclusive ancestors of the unselected branch are never prepared,
- executed, or added to fresh execution history. The activation controller and both scheduler adapters use append-only
- execution edges; no type-specific branch scheduler performs pruning or skip propagation.
+- Once the condition resolves, graph-state activation logic records the selected activation token and attaches only the
+ selected branch input to the pending `If` execution. Branch-exclusive ancestors of the unselected branch are never
+ prepared, executed, or added to fresh execution history. Unsupported fresh shapes and legacy snapshots use the
+ compatibility controller. Both scheduler adapters use append-only execution edges; no type-specific branch
+ scheduler performs pruning or skip propagation.
- Legacy snapshots that already contain skipped prepared nodes retain their compatibility metadata and scheduler
projection. Fresh execution does not create that projection.
- The SQLite queue/processor path has evidence for cancellation before and after `If` resolution and retry from each
@@ -754,7 +756,8 @@ In normal execution, all runtime expansion occurs in `execution_graph` with trac
fallback; stale legacy Iterate result mirrors do not override durable effects.
Collectors nested under iterators preserve enclosing iteration paths, so downstream consumers materialize per enclosing
iteration instead of receiving a mixed collection from unrelated outer iterations.
-- Branch-exclusive nodes behind an unselected `If` branch are skipped, not failed.
+- Branch-exclusive nodes behind an unselected fresh `If` branch remain unmaterialized. Legacy snapshots may retain
+ skipped nodes for compatibility; they are not failed.
## 7) Extensibility
diff --git a/tests/app/services/shared/test_execution_engine_differential.py b/tests/app/services/shared/test_execution_engine_differential.py
index b5d753e82e..a1335e0ef0 100644
--- a/tests/app/services/shared/test_execution_engine_differential.py
+++ b/tests/app/services/shared/test_execution_engine_differential.py
@@ -5723,6 +5723,76 @@ def test_supported_fresh_if_shapes_do_not_use_controller_or_skip_projection(
)
+def test_fresh_mixed_iterate_if_collect_uses_selected_branches_in_both_scheduler_routes(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ graph = Graph()
+ graph.add_node(CollectionConcatInvocation(id="source", first=[True, False, True, False]))
+ graph.add_node(IterateInvocation(id="iterate"))
+ graph.add_node(IfInvocation(id="if"))
+ graph.add_node(AnyTypeTestInvocation(id="true_branch"))
+ graph.add_node(AnyTypeTestInvocation(id="false_branch"))
+ graph.add_node(CollectInvocation(id="collect"))
+ graph.add_edge(create_edge("source", "collection", "iterate", "collection"))
+ graph.add_edge(create_edge("iterate", "item", "if", "condition"))
+ graph.add_edge(create_edge("iterate", "item", "true_branch", "value"))
+ graph.add_edge(create_edge("iterate", "item", "false_branch", "value"))
+ graph.add_edge(create_edge("true_branch", "value", "if", "true_input"))
+ graph.add_edge(create_edge("false_branch", "value", "if", "false_input"))
+ graph.add_edge(create_edge("if", "value", "collect", "item"))
+
+ def fail_controller(*_: object, **__: object) -> Any:
+ raise AssertionError("fresh mixed If used compatibility controller")
+
+ def fail_generic_retirement(*_: object, **__: object) -> None:
+ raise AssertionError("fresh mixed If used generic skip projection")
+
+ monkeypatch.setattr(GraphExecutionState, "_if_activation_controller", fail_controller)
+ monkeypatch.setattr(_GenericGraphSchedulerAdapter, "_retire_unselected_node", fail_generic_retirement)
+
+ runs: list[tuple[list[str], GraphExecutionState]] = []
+ for force_compatibility_scheduler in (False, True):
+ state = GraphExecutionState(graph=graph.model_copy(deep=True))
+ assert state._can_use_fresh_mixed_if_iterate_collect()
+ trace, state = _run_graph(state, force_compatibility_scheduler=force_compatibility_scheduler)
+ runs.append((trace, state))
+
+ assert trace[0] == "source"
+ assert trace[-1] == "collect"
+ assert trace.count("iterate") == 4
+ assert trace.count("true_branch") == 2
+ assert trace.count("false_branch") == 2
+ assert trace.count("if") == 4
+ assert state.executed_history[-1] == "collect"
+ assert set(state.executed_history) == {"source", "iterate", "true_branch", "false_branch", "if", "collect"}
+ assert list(state.prepared_source_mapping.values()).count("true_branch") == 2
+ assert list(state.prepared_source_mapping.values()).count("false_branch") == 2
+ assert state.results[next(iter(state.source_prepared_mapping["collect"]))].collection == [
+ True,
+ False,
+ True,
+ False,
+ ]
+ activation_tokens = sorted(
+ (token for token in state.execution_tokens.values() if token.token_kind == "activation"),
+ key=lambda token: token.frame.iteration_path,
+ )
+ assert [token.port for token in activation_tokens] == [
+ "true_input",
+ "false_input",
+ "true_input",
+ "false_input",
+ ]
+ assert all(
+ state._get_prepared_exec_metadata(exec_node_id).state != "skipped"
+ for exec_node_id in state.prepared_source_mapping
+ )
+ assert state.is_complete()
+
+ assert runs[0][0] == runs[1][0]
+ assert _state_projection(runs[0][1]) == _state_projection(runs[1][1])
+
+
@pytest.mark.parametrize("force_compatibility_scheduler", [False, True])
@pytest.mark.parametrize(
("first_condition", "second_condition", "third_condition"),