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.

fix(execution): reject unsupported effects transactionally

JPPhotoe3b92d807608
diff --git a/invokeai/app/services/shared/graph.py b/invokeai/app/services/shared/graph.py
index 24ee388deb..894bf632b9 100644
--- a/invokeai/app/services/shared/graph.py
+++ b/invokeai/app/services/shared/graph.py
@@ -4279,14 +4279,26 @@ class GraphExecutionState(BaseModel):
token = owner.get("token")
if token is not None:
return GraphExecutionState._same_execution_owner(token, execution_ref)
- owner_id = owner.get("reference_id") or owner.get("id") or owner.get("exec_node_id") or owner.get("node_id")
+ owner_id = (
+ owner.get("reference_id")
+ or owner.get("id")
+ or owner.get("exec_node_id")
+ or owner.get("execution_node_id")
+ or owner.get("node_id")
+ )
owner_state_id = owner.get("state_id") or owner.get("session_id")
return owner_id in {execution_ref.reference_id, execution_ref.exec_node_id} and owner_state_id in {
None,
execution_ref.state_id,
}
owner_id = GraphExecutionState._value_from_object(
- owner, "reference_id", "exec_node_id", "prepared_node_id", "node_id", "invocation_id"
+ owner,
+ "reference_id",
+ "exec_node_id",
+ "execution_node_id",
+ "prepared_node_id",
+ "node_id",
+ "invocation_id",
)
return owner_id in {execution_ref.reference_id, execution_ref.exec_node_id}
@@ -4363,6 +4375,9 @@ class GraphExecutionState(BaseModel):
"execution_ref",
"execution_reference",
"owner_ref",
+ "owner",
+ "parent",
+ "dependency",
"owner_node_id",
"source_node_id",
"node_id",
@@ -4371,6 +4386,8 @@ class GraphExecutionState(BaseModel):
owner = self._value_from_object(effect, "target", "source", "token")
if owner is None or not self._same_execution_owner(owner, execution_ref):
raise ValueError("Execution effect is not owned by execution reference")
+ if effect_kind is not None and effect_kind != "emit":
+ raise ValueError(f"Unsupported execution effect kind: {effect_kind}")
effect_state_id = self._value_from_object(effect, "state_id", "session_id")
if effect_state_id is not None and effect_state_id != execution_ref.state_id:
@@ -4506,13 +4523,14 @@ class GraphExecutionState(BaseModel):
effect_values = list(batch_values if batch_values is not None else effects)
self._validate_effects(ref, effect_values, effect_count)
tokens = self._build_execution_tokens(ref, output_value, effect_values)
+ persisted_effects = copydeep(effect_values)
# All validation above is side-effect free. Preserve complete() as the scheduler compatibility boundary.
finalized_outputs = self.complete(ref.exec_node_id, output_value)
ref.effect_count = effect_count if effect_count is not None else ref.effect_count
self.execution_refs[ref.exec_node_id] = ref
self.execution_tokens.update(tokens)
- self.execution_effects[ref.reference_id] = copydeep(effect_values)
+ self.execution_effects[ref.reference_id] = persisted_effects
return finalized_outputs
def _invalidate_loop_caches_for_source(self, source_node_id: str) -> None:
diff --git a/tests/test_graph_execution_state.py b/tests/test_graph_execution_state.py
index 5e8a25305c..e554beda3c 100644
--- a/tests/test_graph_execution_state.py
+++ b/tests/test_graph_execution_state.py
@@ -285,6 +285,132 @@ def test_graph_state_apply_rejects_invalid_input_before_mutation():
state.apply(stale_ref, output)
+ "effect_kind",
+ ["set_value", "add_edge", "remove_edge", "spawn_execution", "await", "fail", "close_stream", "unknown"],
+)
+def test_graph_state_apply_rejects_unsupported_effect_kinds(effect_kind: str):
+ graph = Graph()
+ graph.add_node(AddInvocation(id="add", a=1, b=2))
+ state = GraphExecutionState(graph=graph)
+ node = state.next()
+ assert node is not None
+ output = node.invoke(Mock(InvocationContext))
+ ref = state.get_execution_ref(node.id, effect_count=1)
+
+ with pytest.raises(ValueError, match=f"Unsupported execution effect kind: {effect_kind}"):
+ state.apply(ref, output, effects=[{"kind": effect_kind, "owner_node_id": node.id}])
+
+ assert not state.executed
+ assert not state.results
+ assert not state.execution_tokens
+ assert not state.execution_effects
+
+
+ "effect_kind, owner_field",
+ [("spawn_execution", "parent"), ("await", "dependency"), ("fail", "owner")],
+)
+def test_graph_state_apply_rejects_owned_unsupported_lifecycle_effects(effect_kind: str, owner_field: str):
+ graph = Graph()
+ graph.add_node(AddInvocation(id="add", a=1, b=2))
+ state = GraphExecutionState(graph=graph)
+ node = state.next()
+ assert node is not None
+ output = node.invoke(Mock(InvocationContext))
+ ref = state.get_execution_ref(node.id, effect_count=1)
+
+ with pytest.raises(ValueError, match=f"Unsupported execution effect kind: {effect_kind}"):
+ state.apply(
+ ref,
+ output,
+ effects=[{"kind": effect_kind, owner_field: {"execution_node_id": node.id}}],
+ )
+
+ assert not state.executed
+ assert not state.results
+ assert not state.execution_tokens
+ assert not state.execution_effects
+
+
+def test_graph_state_apply_accepts_emit_effect():
+ graph = Graph()
+ graph.add_node(AddInvocation(id="add", a=1, b=2))
+ state = GraphExecutionState(graph=graph)
+ node = state.next()
+ assert node is not None
+ output = node.invoke(Mock(InvocationContext))
+ ref = state.get_execution_ref(node.id, effect_count=1)
+ emit_effect = {
+ "kind": "emit",
+ "token": {"node_id": node.id, "field": "value", "value": 3},
+ "value": 3,
+ }
+
+ state.apply(ref, output, effects=[emit_effect])
+
+ assert state.execution_tokens[f"{ref.reference_id}:value:effect"].value == 3
+ assert state.execution_effects[ref.reference_id] == [emit_effect]
+
+
+def test_graph_state_apply_rejects_effect_from_another_state_before_mutation():
+ graph = Graph()
+ graph.add_node(AddInvocation(id="add", a=1, b=2))
+ state = GraphExecutionState(graph=graph)
+ node = state.next()
+ assert node is not None
+ output = node.invoke(Mock(InvocationContext))
+ ref = state.get_execution_ref(node.id, effect_count=1)
+
+ with pytest.raises(ValueError, match="another graph execution state"):
+ state.apply(
+ ref,
+ output,
+ effects=[
+ {
+ "kind": "emit",
+ "state_id": "other-state",
+ "token": {"node_id": node.id, "field": "value", "value": 3},
+ }
+ ],
+ )
+
+ assert not state.executed
+ assert not state.results
+ assert not state.execution_tokens
+ assert not state.execution_effects
+
+
+def test_graph_state_apply_does_not_complete_when_effect_persistence_preparation_fails():
+ class Uncopyable:
+ def __deepcopy__(self, memo):
+ raise RuntimeError("cannot copy effect")
+
+ graph = Graph()
+ graph.add_node(AddInvocation(id="add", a=1, b=2))
+ state = GraphExecutionState(graph=graph)
+ node = state.next()
+ assert node is not None
+ output = node.invoke(Mock(InvocationContext))
+ ref = state.get_execution_ref(node.id, effect_count=1)
+ refs_before_apply = {
+ node_id: stored_ref.model_copy(deep=True) for node_id, stored_ref in state.execution_refs.items()
+ }
+
+ with pytest.raises(RuntimeError, match="cannot copy effect"):
+ state.apply(
+ ref,
+ output,
+ effects=[{"owner_node_id": node.id, "source_port": "value", "metadata": Uncopyable()}],
+ )
+
+ assert not state.executed
+ assert not state.results
+ assert not state.execution_tokens
+ assert not state.execution_effects
+ assert state.execution_refs == refs_before_apply
+
+
def test_graph_state_rehydrates_execution_refs_for_legacy_state():
graph = Graph()
graph.add_node(AddInvocation(id="add", a=1, b=2))