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.

docs(execution): describe closed-stream Collect hydration

JPPhoto70ecbcec693f
diff --git a/docs/src/content/docs/contributing/loop-nodes.md b/docs/src/content/docs/contributing/loop-nodes.md
index 20b173f259..ed63add22d 100644
--- a/docs/src/content/docs/contributing/loop-nodes.md
+++ b/docs/src/content/docs/contributing/loop-nodes.md
@@ -86,8 +86,10 @@ The execution-engine seam is additive to this loop contract. The session runner
`GraphExecutionState.apply()` using a stable execution reference and records frame-aware output tokens and accepted
effects. Legacy `For`/`ForReturn` continuation scheduling is mirrored by a typed, frame-scoped
`ContinuationRecord`. `Iterate` records ordered item tokens in a closed `StreamBuffer`; an empty `Iterate` records an
-explicit empty close. `Collect` consumes that buffer when available and retains the materializer/indegree path as its
-compatibility adapter. `loop_linkage` remains association metadata and never becomes a data token.
+explicit empty close. A direct `Iterate.item` consumer waits for the canonical stream to close, then `Collect` consumes
+its ordered values; a missing stream falls back to materialized results for legacy snapshots. The materializer still
+owns iterator expansion, iteration paths, collector grouping, collection-input hydration, and empty-source closure.
+`loop_linkage` remains association metadata and never becomes a data token.
`IfInvocation` now declares the same seam for branch activation: it emits one frame-scoped activation token for the
selected branch, and the graph state validates and persists it after invocation. The generic opaque scheduler receives
diff --git a/invokeai/app/services/shared/README.md b/invokeai/app/services/shared/README.md
index 5db0567d68..ea9781af6c 100644
--- a/invokeai/app/services/shared/README.md
+++ b/invokeai/app/services/shared/README.md
@@ -214,9 +214,11 @@ control-flow invocation on this seam: each non-empty prepared copy emits one ord
index, and the final copy emits one `close_stream` effect. Graph state maps these effects to the existing
frame-scoped iteration-stream identity, so legacy output mirroring is idempotent. The materializer still creates
prepared copies, derives iteration paths, groups collector inputs, and records the explicit close for an empty source.
-This is not yet token-authoritative downstream topology or full `Collect` migration: no author-time activation ports or
-literal successor IDs are introduced, and the remaining control-flow invocations stay on compatibility paths until
-differential coverage proves each replacement.
+For a direct `Iterate.item` edge, the scheduler defers `CollectInvocation` while its canonical stream is open, and
+runtime hydration consumes the closed stream in sequence order. If no stream exists, hydration retains the legacy
+materialized-result fallback needed by older snapshots. This is not yet token-authoritative downstream topology or full
+`Collect` migration: the materializer still owns copy expansion, iteration paths, collector grouping, collection-input
+hydration, and empty-source closure; no author-time activation ports or literal successor IDs are introduced.
`ExecutionFrame` identifies the owning state, loop iteration path, and workflow-call depth. `ExecutionReference`
identifies one prepared execution node and its frame. `ExecutionToken` records an output port, value, frame, token
@@ -329,7 +331,8 @@ Workflow-call note:
branch.
- `ExecutionEngineRuntime` Owns the typed gate, stream, and continuation records used by compatibility adapters. The
canonical stream for a prepared `IterateInvocation` is keyed by the source iterator and its parent iteration path;
- its item/close effects and legacy output mirroring update the same idempotent buffer.
+ its item/close effects and legacy output mirroring update the same idempotent buffer. Direct `CollectInvocation.item`
+ edges consume a closed canonical stream when available and otherwise use the legacy snapshot fallback.
`GraphExecutionState.model_post_init()` rehydrates private runtime helpers and caches after normal construction or a
JSON/model round trip. Rehydration reconstructs prepared exec metadata, cached iteration paths, private resolved `If`
@@ -403,10 +406,10 @@ Run `C` -> `D:0` -> enqueue `D`. Run `D` -> done.
### 4.6 Input hydration (`_prepare_inputs()`)
-- For **CollectInvocation**: gather the materialized incoming `item` values into `collection`, sorting inputs by
- iteration path so collected results are stable across expanded iterations. Incoming `collection` values are merged
- first, then incoming `item` values are appended. By the time hydration runs, the materializer has already selected the
- iteration group for this collector exec node, so hydration only sees inputs that belong to that group.
+- For **CollectInvocation**: merge incoming `collection` values first, then gather `item` inputs. A direct
+ `Iterate.item` edge uses its closed canonical stream ledger, preserving stream sequence order; an open stream is not
+ hydrated, and a missing stream falls back to the materialized source result for legacy snapshots. Materialized inputs
+ are still grouped by iteration path, so hydration only sees inputs belonging to that collector exec node.
- For **IfInvocation**: hydrate only `condition` and the selected branch input. As a defensive guard against
inconsistent runtime or deserialized session state, the runtime raises if the selected input edge points at an exec
node with no stored runtime output. In normal scheduling this path should be unreachable.
@@ -467,7 +470,10 @@ In normal execution, all runtime expansion occurs in `execution_graph` with trac
source/parent-path stream on the final copy. Exact output mirroring is
idempotent; empty-source closure remains a materializer compatibility
operation.
-- Collectors aggregate `item` inputs and may also merge incoming `collection` inputs during runtime hydration.
+- Collectors wait for available direct Iterate streams to close, then aggregate
+ their ledger values in stream order and may also merge incoming `collection`
+ inputs during runtime hydration. A missing ledger remains a legacy snapshot
+ fallback; stale legacy Iterate result mirrors do not override durable effects.
Collectors nested under iterators preserve enclosing iteration paths, so downstream consumers materialize per enclosing
iteration instead of receiving a mixed collection from unrelated outer iterations.
- Branch-exclusive nodes behind an unselected `If` branch are skipped, not failed.