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): close reviewed protocol safety gaps

JPPhotoa1950080ca3c
diff --git a/invokeai/app/services/session_queue/session_queue_sqlite.py b/invokeai/app/services/session_queue/session_queue_sqlite.py
index b30d35c038..4f948df6eb 100644
--- a/invokeai/app/services/session_queue/session_queue_sqlite.py
+++ b/invokeai/app/services/session_queue/session_queue_sqlite.py
@@ -45,7 +45,6 @@ from invokeai.app.services.session_queue.session_queue_common import (
uuid_string,
)
from invokeai.app.services.shared.execution_state_migration import (
- UnsupportedExecutionStateVersionError,
dump_execution_state,
)
from invokeai.app.services.shared.graph import Graph, GraphExecutionState
@@ -590,7 +589,7 @@ class SqliteSessionQueue(SessionQueueBase):
raw_result = dict(result)
try:
queue_item = SessionQueueItem.queue_item_from_dict(raw_result)
- except UnsupportedExecutionStateVersionError as exc:
+ except (TypeError, ValueError) as exc:
self._quarantine_unreadable_queue_item(raw_result, exc)
continue
queue_item = self._apply_device_affinity(queue_item, resident_model_keys)
diff --git a/invokeai/app/services/shared/execution_effects.py b/invokeai/app/services/shared/execution_effects.py
index e3fe6147f1..0e1b2d3432 100644
--- a/invokeai/app/services/shared/execution_effects.py
+++ b/invokeai/app/services/shared/execution_effects.py
@@ -159,6 +159,14 @@ class ExecutionRef(_ExecutionModel):
def _require_execution_identity(self) -> "ExecutionRef":
if self.token is None and self.execution_node_id is None:
raise ValueError("execution reference requires a token or execution_node_id")
+ if self.token is not None:
+ if "execution_node_id" in self.model_fields_set and self.execution_node_id not in (
+ None,
+ self.token.node_id,
+ ):
+ raise ValueError("execution_node_id conflicts with token node_id")
+ if "frame_path" in self.model_fields_set and self.frame_path != self.token.frame:
+ raise ValueError("frame_path conflicts with token frame")
return self
@property
@@ -239,6 +247,8 @@ class SpawnExecutionEffect(ExecutionEffect):
parent: ExecutionRef
graph: Any
inputs: dict[str, Any] = Field(default_factory=dict)
+ child_execution_id: str = Field(min_length=1)
+ authorization_context: dict[str, Any] | None = None
@field_validator("graph")
@classmethod
@@ -349,6 +359,8 @@ class ExecutionInterface:
parent=owner,
graph=graph,
inputs=inputs,
+ child_execution_id=handle.child_execution_id,
+ authorization_context=handle.authorization_context,
)
)
return handle
diff --git a/invokeai/app/services/shared/graph.py b/invokeai/app/services/shared/graph.py
index 894bf632b9..5ad87d3015 100644
--- a/invokeai/app/services/shared/graph.py
+++ b/invokeai/app/services/shared/graph.py
@@ -4397,6 +4397,15 @@ class GraphExecutionState(BaseModel):
frame_id = self._value_from_object(effect_frame, "frame_id", "id")
if frame_id not in (None, "", execution_ref.frame.frame_id):
raise ValueError("Execution effect belongs to another execution frame")
+ frame_state_id = self._value_from_object(effect_frame, "state_id", "session_id")
+ if frame_state_id not in (None, "", execution_ref.frame.state_id):
+ raise ValueError("Execution effect belongs to another graph execution state")
+ frame_path = self._value_from_object(effect_frame, "iteration_path", "frame_path")
+ if frame_path is not None and tuple(frame_path) != execution_ref.frame.iteration_path:
+ raise ValueError("Execution effect belongs to another execution frame")
+ frame_depth = self._value_from_object(effect_frame, "workflow_call_depth", "call_depth")
+ if frame_depth not in (None, execution_ref.frame.workflow_call_depth):
+ raise ValueError("Execution effect belongs to another workflow-call depth")
source_ref = self._value_from_object(effect, "source", "target", "token")
destination_ref = self._value_from_object(effect, "destination")
@@ -4485,7 +4494,12 @@ class GraphExecutionState(BaseModel):
token_value = self._value_from_object(token, "value")
token_kind = self._value_from_object(token, "token_kind") or "data"
sequence = self._value_from_object(token, "sequence")
- token_id = f"{execution_ref.reference_id}:{port}:{sequence if sequence is not None else 'effect'}"
+ token_id_base = f"{execution_ref.reference_id}:{port}:{sequence if sequence is not None else 'effect'}"
+ token_id = token_id_base
+ duplicate_index = 1
+ while token_id in tokens:
+ token_id = f"{token_id_base}:{duplicate_index}"
+ duplicate_index += 1
tokens[token_id] = ExecutionToken(
token_id=token_id,
reference_id=execution_ref.reference_id,
diff --git a/tests/app/services/session_queue/test_session_queue_dequeue.py b/tests/app/services/session_queue/test_session_queue_dequeue.py
index c07d97f052..4c66525410 100644
--- a/tests/app/services/session_queue/test_session_queue_dequeue.py
+++ b/tests/app/services/session_queue/test_session_queue_dequeue.py
@@ -155,6 +155,32 @@ def test_fifo_quarantines_future_snapshot_and_dequeues_later_work(
assert "newer than supported" in error_message
+ "session_json",
+ [
+ "{not valid json",
+ json.dumps({"execution_state_version": "invalid", "graph": {}}),
+ json.dumps({"execution_state_version": CURRENT_EXECUTION_STATE_VERSION, "graph": "invalid"}),
+ ],
+)
+def test_fifo_quarantines_unreadable_snapshot_and_dequeues_later_work(
+ session_queue_fifo: SqliteSessionQueue,
+ session_json: str,
+) -> None:
+ bad_item_id = _insert_queue_item(session_queue_fifo, "default", "bad-user", session_json=session_json)
+ valid_item_id = _insert_queue_item(session_queue_fifo, "default", "valid-user")
+
+ dequeued = session_queue_fifo.dequeue()
+
+ assert dequeued is not None
+ assert dequeued.item_id == valid_item_id
+ with session_queue_fifo._db.transaction() as cursor:
+ cursor.execute("SELECT status, error_message FROM session_queue WHERE item_id = ?", (bad_item_id,))
+ status, error_message = cursor.fetchone()
+ assert status == "failed"
+ assert "Unable to load execution state" in error_message
+
+
# ---------------------------------------------------------------------------
# Round-robin tests
# ---------------------------------------------------------------------------
diff --git a/tests/app/services/shared/test_execution_effects.py b/tests/app/services/shared/test_execution_effects.py
index cfc72a2fff..7a53f9553b 100644
--- a/tests/app/services/shared/test_execution_effects.py
+++ b/tests/app/services/shared/test_execution_effects.py
@@ -15,8 +15,8 @@ from invokeai.app.services.shared.execution_effects import (
AddEdgeEffect,
AwaitEffect,
ChildExecutionHandle,
- ExecutionInterface,
ExecutionEffectsRecorder,
+ ExecutionInterface,
ExecutionRef,
ExecutionToken,
FailEffect,
@@ -71,6 +71,11 @@ def test_execution_token_and_ref_are_frame_aware() -> None:
assert ref.invocation_id == "node"
assert ref.iteration_path == (2, 4)
+ with pytest.raises(ValidationError, match="conflicts"):
+ ExecutionRef(token=token, execution_node_id="other")
+ with pytest.raises(ValidationError, match="conflicts"):
+ ExecutionRef(token=token, frame_path=(9,))
+
def test_execution_ref_identity_aliases_are_safe_without_token() -> None:
ref = ExecutionRef(execution_node_id="node")
@@ -97,6 +102,8 @@ def test_spawn_returns_validated_child_handle_and_records_owner() -> None:
effect = recorder.snapshot()[0]
assert isinstance(effect, SpawnExecutionEffect)
assert effect.parent == effect.execution_ref
+ assert effect.child_execution_id == handle.child_execution_id
+ assert effect.authorization_context == handle.authorization_context
@pytest.mark.parametrize(
diff --git a/tests/test_graph_execution_state.py b/tests/test_graph_execution_state.py
index e554beda3c..40d7f89a52 100644
--- a/tests/test_graph_execution_state.py
+++ b/tests/test_graph_execution_state.py
@@ -353,6 +353,52 @@ def test_graph_state_apply_accepts_emit_effect():
assert state.execution_effects[ref.reference_id] == [emit_effect]
+def test_graph_state_apply_keeps_repeated_emits_without_sequence():
+ 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=2)
+ effects = [
+ {"kind": "emit", "token": {"node_id": node.id, "field": "value", "value": value}, "value": value}
+ for value in (3, 4)
+ ]
+
+ state.apply(ref, output, effects=effects)
+
+ assert state.execution_tokens[f"{ref.reference_id}:value:effect"].value == 3
+ assert state.execution_tokens[f"{ref.reference_id}:value:effect:1"].value == 4
+
+
+def test_graph_state_apply_rejects_decomposed_frame_mismatch():
+ 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 execution frame"):
+ state.apply(
+ ref,
+ output,
+ effects=[
+ {
+ "kind": "emit",
+ "frame": {"iteration_path": [99]},
+ "token": {"node_id": node.id, "field": "value", "value": 3},
+ }
+ ],
+ )
+
+ assert not state.executed
+ assert not state.results
+ assert not state.execution_tokens
+
+
def test_graph_state_apply_rejects_effect_from_another_state_before_mutation():
graph = Graph()
graph.add_node(AddInvocation(id="add", a=1, b=2))