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 graph completion cache invalidation

JPPhoto97c70a35c61a
diff --git a/invokeai/app/services/shared/graph.py b/invokeai/app/services/shared/graph.py
index 2d5d3ec0d5..bc1fa56f18 100644
--- a/invokeai/app/services/shared/graph.py
+++ b/invokeai/app/services/shared/graph.py
@@ -4009,16 +4009,14 @@ class GraphExecutionState(BaseModel):
def _mark_source_executed(self, source_node_id: str) -> None:
self.executed.add(source_node_id)
- if self._completed_source_ids_cache is None:
- self._completed_source_ids_cache = set()
- self._completed_source_ids_cache.add(source_node_id)
+ self._get_completed_source_ids_cache().add(source_node_id)
if source_node_id not in self.executed_history:
self.executed_history.append(source_node_id)
def _discard_source_executed(self, source_node_id: str) -> None:
self.executed.discard(source_node_id)
- if self._completed_source_ids_cache is not None:
- self._completed_source_ids_cache.discard(source_node_id)
+ # A source can be discarded while its already-prepared executions are still complete. New executions
+ # invalidate the derived completion cache when they are registered.
def _get_completed_source_ids_cache(self) -> set[str]:
if self._completed_source_ids_cache is None:
@@ -4066,6 +4064,9 @@ class GraphExecutionState(BaseModel):
def _register_prepared_exec_node(self, exec_node_id: str, source_node_id: str) -> None:
self._prepared_registry().register(exec_node_id, source_node_id)
+ self.executed.discard(source_node_id)
+ if self._completed_source_ids_cache is not None:
+ self._completed_source_ids_cache.discard(source_node_id)
self._invalidate_loop_caches_for_source(source_node_id)
if (
self._prepared_for_index is not None
diff --git a/tests/app/services/shared/test_for_scheduler_performance.py b/tests/app/services/shared/test_for_scheduler_performance.py
index e2b0c679b9..f84ec8c00d 100644
--- a/tests/app/services/shared/test_for_scheduler_performance.py
+++ b/tests/app/services/shared/test_for_scheduler_performance.py
@@ -70,3 +70,21 @@ def test_completion_predicate_preserves_durable_state() -> None:
restored_state = GraphExecutionState.model_validate_json(before)
assert restored_state.is_complete()
assert restored_state.model_dump_json() == before
+
+
+def test_completion_cache_preserves_completed_sources_after_restore() -> None:
+ graph = Graph()
+ graph.add_node(AnyTypeTestInvocation(id="first", value=1))
+ graph.add_node(AnyTypeTestInvocation(id="second", value=2))
+ state = GraphExecutionState(graph=graph)
+
+ first = state.next()
+ assert first is not None
+ state.complete(first.id, first.invoke(Mock()))
+
+ restored_state = GraphExecutionState.model_validate_json(state.model_dump_json())
+ second = restored_state.next()
+ assert second is not None
+ restored_state.complete(second.id, second.invoke(Mock()))
+
+ assert restored_state.is_complete()