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.
TypeScript ai-artartificial-intelligencegenerative-artimage-generationimg2imginpaintinglatent-diffusionlinuxmacosoutpaintingstable-diffusiontxt2imgwindows
fix(execution): hide runtime ledgers from API schemas
JPPhotob3e24110629b
diff --git a/invokeai/app/services/shared/README.md b/invokeai/app/services/shared/README.mdindex 19ccd14304..58eb2659c7 100644--- a/invokeai/app/services/shared/README.md+++ b/invokeai/app/services/shared/README.md@@ -35,6 +35,15 @@ compatibility controller or project fresh skipped nodes in either schedulerroute. This proves the current ownership boundary; it does not remove thecontroller, which remains required for unsupported fresh shapes and legacysnapshots.+The bounded If evidence rerun on the current branch passes 639 tests across+the differential, dependency, runtime, SQLite processor, and snapshot+migration suites. This is focused migration evidence, not a full-suite result;+the controller fallback and legacy projection remain intentional.+The execution ledgers (`execution_refs`, `execution_tokens`, and+`execution_effects`) are internal persistence data. They are excluded from+ordinary model/API serialization and schemas; `dump_execution_state()` is the+explicit internal persistence path that retains them, including nested child+states.Direct `Iterate`/`Collect` graphs that do not contain `If`, `For`, `ForReturn`, or saved-workflow control flow also usethe generic adapter. Its adapter-level readiness predicate waits for canonical Iterate streams to close, and genericcompletion mirrors each Iterate result into that ledger before releasing `Collect`. The exact fresh four-node shapediff --git a/invokeai/app/services/shared/execution_state_migration.py b/invokeai/app/services/shared/execution_state_migration.pyindex 9d158278c4..434480cd9d 100644--- a/invokeai/app/services/shared/execution_state_migration.py+++ b/invokeai/app/services/shared/execution_state_migration.py@@ -23,6 +23,31 @@ def _retain_nullable_execution_token_values(snapshot: dict[str, Any]) -> None:_retain_nullable_execution_token_values(child_snapshot)+def _append_runtime_fields(snapshot: dict[str, Any], state: GraphExecutionState) -> None:+ """Add private runtime ledgers to an internal snapshot, including child states."""+ snapshot["execution_refs"] = {+ reference_id: reference.model_dump(mode="json") for reference_id, reference in state.execution_refs.items()+ }+ snapshot["execution_tokens"] = {+ token_id: token.model_dump(mode="json") for token_id, token in state.execution_tokens.items()+ }+ snapshot["execution_effects"] = {+ reference_id: [+ effect.model_dump(mode="json") if hasattr(effect, "model_dump") else effect for effect in effects+ ]+ for reference_id, effects in state.execution_effects.items()+ }+ for reference_id, effects in state._legacy_execution_effects_for_snapshot().items():+ snapshot["execution_effects"][reference_id] = [+ effect.model_dump(mode="json") if hasattr(effect, "model_dump") else effect for effect in effects+ ]++ child_snapshot = snapshot.get("waiting_workflow_call_child_session")+ child_state = state.waiting_workflow_call_child_session+ if isinstance(child_snapshot, dict) and child_state is not None:+ _append_runtime_fields(child_snapshot, child_state)++class UnsupportedExecutionStateVersionError(ValueError):"""Raised when an execution-state snapshot cannot be read by this runtime."""@@ -36,14 +61,11 @@ def dump_execution_state(state: GraphExecutionState) -> dict[str, Any]:migration experiments."""snapshot = state.model_dump(mode="json", warnings=False, exclude_none=True)+ _append_runtime_fields(snapshot, state)# Persist nullable output tokens explicitly. `ExecutionToken.value` remains required in the# public model/schema, but an output port may legitimately carry None and the general# exclude_none policy would otherwise make the snapshot impossible to hydrate._retain_nullable_execution_token_values(snapshot)- for reference_id, effects in state._legacy_execution_effects_for_snapshot().items():- snapshot.setdefault("execution_effects", {})[reference_id] = [- effect.model_dump(mode="json") if hasattr(effect, "model_dump") else effect for effect in effects- ]snapshot["execution_state_version"] = CURRENT_EXECUTION_STATE_VERSIONreturn snapshotdiff --git a/invokeai/app/services/shared/graph.py b/invokeai/app/services/shared/graph.pyindex 0046744bef..578d9cb640 100644--- a/invokeai/app/services/shared/graph.py+++ b/invokeai/app/services/shared/graph.py@@ -166,6 +166,31 @@ from invokeai.app.util.misc import uuid_string_JSON_SERIALIZER = TypeAdapter(Any)+_EXECUTION_STATE_RUNTIME_FIELDS = frozenset({"execution_refs", "execution_tokens", "execution_effects"})+_EXECUTION_STATE_REQUIRED_FIELDS = (+ "id",+ "graph",+ "execution_graph",+ "executed",+ "executed_history",+ "results",+ "errors",+ "workflow_call_stack",+ "workflow_call_history",+ "prepared_source_mapping",+ "source_prepared_mapping",+)+++def _hide_execution_state_runtime_fields(schema: JsonSchemaValue) -> None:+ """Keep private execution-ledger fields out of API schemas."""++ properties = schema.get("properties")+ if isinstance(properties, dict):+ for field_name in _EXECUTION_STATE_RUNTIME_FIELDS:+ properties.pop(field_name, None)+ schema["required"] = [field_name for field_name in _EXECUTION_STATE_REQUIRED_FIELDS if field_name in properties]+class GraphExecutionState(BaseModel):"""Tracks source-graph expansion, execution progress, and runtime results."""@@ -245,14 +270,17 @@ class GraphExecutionState(BaseModel):execution_refs: dict[str, ExecutionReference] = Field(default_factory=dict,description="Stable frame-aware references for prepared execution nodes",+ exclude=True,)execution_tokens: dict[str, ExecutionToken] = Field(default_factory=dict,description="Data tokens produced by prepared execution output ports",+ exclude=True,)execution_effects: dict[str, list[Any]] = Field(default_factory=dict,description="Effects accepted for each execution reference",+ exclude=True,)# Ready queues grouped by node class name (internal only)_ready_queues: dict[str, Deque[str]] = PrivateAttr(default_factory=dict)@@ -2656,23 +2684,7 @@ class GraphExecutionState(BaseModel):self._synthesize_legacy_execution_effects()self._rehydrate_runtime_state()- model_config = ConfigDict(- json_schema_extra={- "required": [- "id",- "graph",- "execution_graph",- "executed",- "executed_history",- "results",- "errors",- "workflow_call_stack",- "workflow_call_history",- "prepared_source_mapping",- "source_prepared_mapping",- ]- }- )+ model_config = ConfigDict(json_schema_extra=_hide_execution_state_runtime_fields)@field_validator("graph")def graph_is_valid(cls, v: Graph):diff --git a/tests/app/services/shared/test_execution_state_migration.py b/tests/app/services/shared/test_execution_state_migration.pyindex 1827d12e0e..abdb74e595 100644--- a/tests/app/services/shared/test_execution_state_migration.py+++ b/tests/app/services/shared/test_execution_state_migration.py@@ -7,7 +7,13 @@ from invokeai.app.services.shared.execution_state_migration import (dump_execution_state,load_execution_state,)-from invokeai.app.services.shared.graph import ExecutionFrame, ExecutionToken, Graph, GraphExecutionState+from invokeai.app.services.shared.graph import (+ ExecutionFrame,+ ExecutionReference,+ ExecutionToken,+ Graph,+ GraphExecutionState,+)def _make_state() -> GraphExecutionState:@@ -50,12 +56,15 @@ def test_dumps_and_loads_versioned_execution_state_envelope() -> None:assert "state" not in snapshotexpected = dict(snapshot)expected.pop("execution_state_version")- assert restored.model_dump(mode="json", warnings=False, exclude_none=True) == expected+ restored_snapshot = dump_execution_state(restored)+ restored_snapshot.pop("execution_state_version")+ assert restored_snapshot == expecteddef test_loads_temporary_versioned_envelope() -> None:state = _make_state()- raw = state.model_dump(mode="json", warnings=False, exclude_none=True)+ raw = dump_execution_state(state)+ raw.pop("execution_state_version")restored = load_execution_state({"version": CURRENT_EXECUTION_STATE_VERSION, "state": raw})@@ -118,3 +127,33 @@ def test_round_trips_nullable_execution_token_value_in_child_state() -> None:assert restored.waiting_workflow_call_child_session is not Noneassert restored.waiting_workflow_call_child_session.execution_tokens["token-id"].value is None+++def test_internal_execution_fields_are_persisted_but_not_publicly_serialized() -> None:+ state = _make_state()+ execution_ref = ExecutionReference(+ reference_id="state-id:exec-node",+ state_id="state-id",+ exec_node_id="exec-node",+ source_node_id="node-id",+ frame=ExecutionFrame(state_id="state-id", frame_id="frame-id"),+ )+ state.execution_refs["exec-node"] = execution_ref+ state.execution_tokens["token-id"] = ExecutionToken(+ token_id="token-id",+ reference_id=execution_ref.reference_id,+ owner_node_id="exec-node",+ port="value",+ frame=execution_ref.frame,+ value=3,+ )++ public_payload = state.model_dump(mode="json", warnings=False, exclude_none=True)+ assert "execution_refs" not in public_payload+ assert "execution_tokens" not in public_payload+ assert "execution_effects" not in public_payload++ persisted_payload = dump_execution_state(state)+ assert persisted_payload["execution_refs"]["exec-node"]["reference_id"] == execution_ref.reference_id+ assert persisted_payload["execution_tokens"]["token-id"]["value"] == 3+ assert persisted_payload["execution_effects"] == {}diff --git a/tests/app/services/shared/test_graph_models.py b/tests/app/services/shared/test_graph_models.pyindex d915d2b84b..e738487152 100644--- a/tests/app/services/shared/test_graph_models.py+++ b/tests/app/services/shared/test_graph_models.py@@ -9,6 +9,7 @@ from pydantic.json_schema import models_json_schemafrom invokeai.app.invocations.logic import IfInvocationfrom invokeai.app.invocations.loops import ForInvocation, ForReturnInvocationfrom invokeai.app.invocations.math import AddInvocation+from invokeai.app.services.session_queue.session_queue_common import SessionQueueItemfrom invokeai.app.services.shared import graph as graph_facadefrom invokeai.app.services.shared import graph_models, graph_validation@@ -169,13 +170,20 @@ def test_graph_models_preserve_graph_and_execution_state_serialization() -> None"ExecutionToken",} <= definitions.keys()assert {"id", "nodes", "edges"} <= definitions["Graph"]["properties"].keys()- assert {"id", "graph", "execution_graph", "execution_refs", "execution_tokens"} <= definitions[- "GraphExecutionState"- ]["properties"].keys()+ assert {"id", "graph", "execution_graph"} <= definitions["GraphExecutionState"]["properties"].keys()+ assert {"execution_refs", "execution_tokens", "execution_effects"}.isdisjoint(+ definitions["GraphExecutionState"]["properties"].keys()+ )assert {"source", "destination", "type"} <= definitions["Edge"]["properties"].keys()assert {"reference_id", "exec_node_id", "frame"} <= definitions["ExecutionReference"]["properties"].keys()assert {"token_id", "reference_id", "owner_node_id", "value"} <= definitions["ExecutionToken"]["properties"].keys()+ _, queue_schema = models_json_schema([(SessionQueueItem, "serialization")])+ queue_graph_schema = queue_schema["$defs"]["GraphExecutionState"]+ assert {"execution_refs", "execution_tokens", "execution_effects"}.isdisjoint(+ queue_graph_schema["properties"].keys()+ )+node_refs = {reference["$ref"] for reference in definitions["Graph"]["properties"]["nodes"]["additionalProperties"]["oneOf"]}