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.

feat(execution): version runtime state snapshots

JPPhotoe843c1cc1674
diff --git a/invokeai/app/services/shared/execution_state_migration.py b/invokeai/app/services/shared/execution_state_migration.py
new file mode 100644
index 0000000000..077bf34e41
--- /dev/null
+++ b/invokeai/app/services/shared/execution_state_migration.py
@@ -0,0 +1,48 @@
+"""Compatibility helpers for persisted internal execution-state snapshots."""
+
+from collections.abc import Mapping
+from typing import Any, Final
+
+from invokeai.app.services.shared.graph import GraphExecutionState
+
+CURRENT_EXECUTION_STATE_VERSION: Final[int] = 1
+
+
+class UnsupportedExecutionStateVersionError(ValueError):
+ """Raised when an execution-state snapshot cannot be read by this runtime."""
+
+
+def dump_execution_state(state: GraphExecutionState) -> dict[str, Any]:
+ """Dump an internal execution state in its versioned snapshot envelope."""
+ return {
+ "version": CURRENT_EXECUTION_STATE_VERSION,
+ "state": state.model_dump(mode="json", warnings=False, exclude_none=True),
+ }
+
+
+def load_execution_state(snapshot: Mapping[str, Any]) -> GraphExecutionState:
+ """Load a versioned snapshot or a legacy unwrapped execution state."""
+ if not isinstance(snapshot, Mapping):
+ raise TypeError("Execution state snapshot must be a mapping")
+
+ if "version" not in snapshot:
+ return GraphExecutionState.model_validate(snapshot, strict=False)
+
+ version = snapshot["version"]
+ if isinstance(version, bool) or not isinstance(version, int):
+ raise ValueError("Execution state snapshot version must be an integer")
+ if version > CURRENT_EXECUTION_STATE_VERSION:
+ raise UnsupportedExecutionStateVersionError(
+ f"Execution state snapshot version {version} is newer than supported version "
+ f"{CURRENT_EXECUTION_STATE_VERSION}"
+ )
+ if version != CURRENT_EXECUTION_STATE_VERSION:
+ raise UnsupportedExecutionStateVersionError(
+ f"Execution state snapshot version {version} is unsupported; current version is "
+ f"{CURRENT_EXECUTION_STATE_VERSION}"
+ )
+
+ state = snapshot.get("state")
+ if not isinstance(state, Mapping):
+ raise ValueError("Versioned execution state snapshot must contain a mapping in 'state'")
+ return GraphExecutionState.model_validate(state, strict=False)
diff --git a/tests/app/services/shared/test_execution_state_migration.py b/tests/app/services/shared/test_execution_state_migration.py
new file mode 100644
index 0000000000..c5ac1b3bb4
--- /dev/null
+++ b/tests/app/services/shared/test_execution_state_migration.py
@@ -0,0 +1,53 @@
+import pytest
+
+from invokeai.app.services.shared.execution_state_migration import (
+ CURRENT_EXECUTION_STATE_VERSION,
+ UnsupportedExecutionStateVersionError,
+ dump_execution_state,
+ load_execution_state,
+)
+from invokeai.app.services.shared.graph import Graph, GraphExecutionState
+
+
+def _make_state() -> GraphExecutionState:
+ return GraphExecutionState(
+ id="state-id",
+ graph=Graph(),
+ executed={"node-id"},
+ executed_history=["node-id"],
+ errors={"node-id": "failure"},
+ prepared_source_mapping={"exec-node": "node-id"},
+ source_prepared_mapping={"node-id": {"exec-node"}},
+ finalized_loop_contexts={("loop", (0, 1))},
+ prepared_iteration_paths={"exec-node": (0, 1)},
+ ready_order=["exec-node"],
+ indegree={"exec-node": 0},
+ )
+
+
+def test_loads_legacy_unwrapped_graph_execution_state() -> None:
+ state = _make_state()
+ legacy_payload = state.model_dump(mode="json", warnings=False, exclude_none=True)
+
+ restored = load_execution_state(legacy_payload)
+
+ assert restored.model_dump(mode="json", warnings=False, exclude_none=True) == legacy_payload
+
+
+def test_dumps_and_loads_versioned_execution_state_envelope() -> None:
+ state = _make_state()
+
+ snapshot = dump_execution_state(state)
+ restored = load_execution_state(snapshot)
+
+ assert snapshot["version"] == CURRENT_EXECUTION_STATE_VERSION
+ assert set(snapshot) == {"version", "state"}
+ assert restored.model_dump(mode="json", warnings=False, exclude_none=True) == snapshot["state"]
+
+
+def test_rejects_future_execution_state_versions() -> None:
+ snapshot = dump_execution_state(_make_state())
+ snapshot["version"] = CURRENT_EXECUTION_STATE_VERSION + 1
+
+ with pytest.raises(UnsupportedExecutionStateVersionError, match="newer than supported"):
+ load_execution_state(snapshot)