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.

Guard effect recorder lifecycle and aliases

JPPhoto9cc4c5962f9d
diff --git a/invokeai/app/services/shared/execution_effects.py b/invokeai/app/services/shared/execution_effects.py
index 6a60a0eb40..957c9604ee 100644
--- a/invokeai/app/services/shared/execution_effects.py
+++ b/invokeai/app/services/shared/execution_effects.py
@@ -141,6 +141,37 @@ class ExecutionRef(_ExecutionModel):
if not isinstance(value, dict):
return value
data = dict(value)
+ if "token" in data and data["token"] is not None:
+ token = data["token"]
+ if isinstance(token, BaseModel):
+ token_data = token.model_dump(mode="python", warnings=False)
+ else:
+ token_data = token
+
+ def _token_value(names: tuple[str, ...]) -> tuple[bool, Any]:
+ if isinstance(token_data, dict):
+ for name in names:
+ if name in token_data:
+ return True, token_data[name]
+ return False, None
+
+ def _values_match(left: Any, right: Any) -> bool:
+ if isinstance(left, (tuple, list)) and isinstance(right, (tuple, list)):
+ return tuple(left) == tuple(right)
+ return left == right
+
+ for aliases, token_names, label in (
+ (("node_id", "invocation_id"), ("node_id", "invocation_id"), "node_id"),
+ (("field", "port", "output", "output_name"), ("field", "port", "output", "output_name"), "field"),
+ (("frame", "iteration_path"), ("frame", "iteration_path", "frame_path"), "frame"),
+ ):
+ token_present, token_value = _token_value(token_names)
+ if not token_present:
+ continue
+ for alias in aliases:
+ if alias in data and not _values_match(data[alias], token_value):
+ raise ValueError(f"Token {label} conflicts with legacy alias {alias}")
+
if "token" not in data and any(
name in data for name in ("node_id", "invocation_id", "field", "port", "output", "output_name")
):
@@ -168,7 +199,9 @@ class ExecutionRef(_ExecutionModel):
data["execution_node_id"] = token_data.get("node_id", token_data.get("invocation_id"))
if "frame_path" not in data and isinstance(data.get("token"), dict):
token_data = data["token"]
- data["frame_path"] = token_data.get("frame", token_data.get("iteration_path", ()))
+ data["frame_path"] = token_data.get(
+ "frame", token_data.get("iteration_path", token_data.get("frame_path", ()))
+ )
for alias in ("node_id", "invocation_id", "field", "port", "output", "output_name", "frame", "iteration_path"):
data.pop(alias, None)
return data
@@ -459,6 +492,8 @@ class ExecutionEffectsRecorder:
def record(self, effect: ExecutionEffect) -> None:
if not isinstance(effect, ExecutionEffect):
raise TypeError(f"Expected ExecutionEffect, got {type(effect).__name__}")
+ if effect.kind in {"spawn_execution", "await", "fail"}:
+ raise UnsupportedExecutionEffectError(f"Execution effect kind '{effect.kind}' is not supported")
try:
effect.model_dump(mode="json", warnings="error")
except (PydanticSerializationError, TypeError, ValueError) as exc:
diff --git a/tests/app/services/shared/test_execution_effects.py b/tests/app/services/shared/test_execution_effects.py
index da7e9ad234..1debea707f 100644
--- a/tests/app/services/shared/test_execution_effects.py
+++ b/tests/app/services/shared/test_execution_effects.py
@@ -112,6 +112,26 @@ def test_execution_ref_identity_aliases_are_safe_without_token() -> None:
assert ref.output_name == ""
+ "alias, value",
+ [
+ ("node_id", "other"),
+ ("invocation_id", "other"),
+ ("field", "other"),
+ ("port", "other"),
+ ("output", "other"),
+ ("output_name", "other"),
+ ("frame", (9,)),
+ ("iteration_path", (9,)),
+ ],
+)
+def test_execution_ref_rejects_conflicting_legacy_alias_with_token(alias: str, value: object) -> None:
+ token = ExecutionToken(node_id="node", field="value", frame=(2,))
+
+ with pytest.raises(ValidationError, match="conflicts"):
+ ExecutionRef(token=token, **{alias: value})
+
+
def test_execution_interface_rejects_unsupported_lifecycle_effects() -> None:
recorder = ExecutionEffectsRecorder(source_node_id="parent")
execution = ExecutionInterface(recorder)
@@ -239,17 +259,37 @@ def test_set_value_effect_requires_a_value() -> None:
def test_lifecycle_effect_models_validate_owner_refs() -> None:
- recorder = ExecutionEffectsRecorder(source_node_id="parent")
await_effect = AwaitEffect(
execution_ref=ExecutionRef(execution_node_id="parent"),
dependency=ExecutionRef(execution_node_id="child"),
)
fail_effect = FailEffect(execution_ref=ExecutionRef(execution_node_id="parent"), message="failed")
- recorder.record(await_effect)
- recorder.record(fail_effect)
assert isinstance(await_effect, AwaitEffect)
assert isinstance(fail_effect, FailEffect)
- assert recorder.snapshot() == (await_effect, fail_effect)
+
+
+ "effect",
+ [
+ SpawnExecutionEffect(
+ parent=ExecutionRef(execution_node_id="parent"),
+ graph={},
+ child_execution_id="child",
+ ),
+ AwaitEffect(
+ execution_ref=ExecutionRef(execution_node_id="parent"),
+ dependency=ExecutionRef(execution_node_id="child"),
+ ),
+ FailEffect(execution_ref=ExecutionRef(execution_node_id="parent"), message="failed"),
+ ],
+)
+def test_recorder_rejects_unsupported_lifecycle_effects(effect: object) -> None:
+ recorder = ExecutionEffectsRecorder(source_node_id="parent")
+
+ with pytest.raises(UnsupportedExecutionEffectError, match="not supported"):
+ recorder.record(effect) # type: ignore[arg-type]
+
+ assert recorder.snapshot() == ()
@pytest.mark.parametrize(