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.

87.6 KB
Raw Blame

InvokeAI Graph - Design Overview

High-level design for the graph module. Focuses on responsibilities, data flow, and how traversal works.

1) Purpose

Provide a typed, acyclic workflow model (Graph) plus a runtime scheduler (GraphExecutionState) that expands iterator patterns, tracks readiness via indegree (the number of incoming edges to a node in the directed graph), and executes nodes from class-grouped ready queues. In normal execution, runtime expansion happens in a separate execution graph instead of mutating the source graph. Fresh If graphs are eager by default: both branch paths are admitted and evaluated after the condition gate resolves, while the pending If attaches and hydrates only the selected branch edge/value. Lazy execution is limited to two shapes, which run only the selected branch: one root ordinary If with no other control-flow nodes or iterator axes, where both branch dependency closures are private; and the exact connected single-axis Iterate -> If -> Collect topology of six nodes and seven edges. In that mixed shape, one inputless collection producer feeds Iterate; Iterate.item feeds If.condition and one direct producer for each branch; each branch producer consumes that same item and outputs only to its corresponding If branch; and If.value is the sole input to a terminal Collect.item. No other nodes or edges are admitted. Nested, sibling, fan-out, loop, saved-workflow, and other valid shapes keep eager branch behavior; frame/control-flow combinations rejected by validation still fail before invocation. Legacy snapshots retain their generic compatibility projection and skipped-state metadata. Both scheduler adapters consume frame-local activation dependencies without pruning execution edges or calling a type-specific branch scheduler. executed_history records source-level completion and retirement, not an invocation trace. The persisted GraphExecutionState adds optional prepared_iteration_axes, which is exposed as an additive field in the queue-session schema. This changes no invocation or image contract. The token-authoritative topology is used for fresh ordinary-node If graphs. The compatibility controller remains for legacy snapshots and mixed control-flow graphs outside the bounded Iterate/If/Collect case. The execution ledgers (execution_refs, execution_tokens, execution_effects, and execution_child_dependencies) are internal persistence data. They are not part of public API schemas or client responses. Version-3 dump_execution_state() omits derived execution references and ordinary output tokens; loading rebuilds execution references and runtime indexes/state from the prepared graph, mappings, and results, while ordinary output tokens remain omitted from the persisted projection. Frame-scoped activation tokens and effects needed for stream, continuation, or active workflow-call recovery persist; completed workflow-call lifecycle effects and completed child-dependency records are omitted. Attached child state is retained only while its parent call is active. The versioned loader continues to accept version-2 snapshots; when the additive prepared_iteration_axes field is absent, it reconstructs the axes from prepared execution and source-edge context. Direct Iterate/Collect graphs that do not contain If, For, ForReturn, or saved-workflow control flow also use the generic adapter. Its adapter-level readiness predicate waits for canonical Iterate streams to close, and generic completion mirrors each Iterate result into that ledger before releasing Collect. The exact fresh four-node shape literal collection source -> Iterate -> one ordinary body -> Collect, plus any number of ordinary downstream consumers from Collect.collection, is planned by a private graph-state planner, which owns its prepared-copy expansion, iteration paths, downstream admission, and explicit empty-stream barrier without calling the legacy materializer. The exact fresh two-source/two-Iterate/shared-Collect.item and exact fresh three-source/three-Iterate/ shared-Collect.item fan-in shapes also use the planner for independent stream expansions, closure gating, and deterministic source-ordered hydration. The exact fresh two-branch body-mediated shape source_a -> Iterate_a -> body_a -> Collect.item plus source_b -> Iterate_b -> body_b -> Collect.item uses the same planner: source IDs are ordered lexically, each branch preserves its item order, and Collect runs only after both streams close, including explicit empty closes. This is a private bounded planner case: exactly seven nodes and six ordinary edges—two inputless ordinary sources, two Iterate nodes, two ordinary bodies, and one Collect; it has no Collect.collection input or downstream/extra topology. It is not a change to the public workflow contract. The planner also admits one bounded input-driven extension: one inputless ordinary collection producer feeds one ordinary collection-preparation node, which feeds the same Iterate -> ordinary body -> Collect path, with ordinary downstream consumers allowed. It owns the preparation input edge, ordered stream effects, empty closure, checkpoint rehydration, failure, rollback, and fresh retry behavior for that exact shape. It does not admit If, For, ForReturn, saved-workflow, nested-Iterate chain, fan-in, or mixed control-flow nodes outside the bounded per-item topology above; those remain compatibility-owned. The exact fresh seven-node nested shape outer For -> ordinary collection preparation -> Iterate -> ordinary body -> Collect -> linked outer ForReturn, with one ordinary outer For.output_collection consumer, is also generic-routed. The nested planner owns per-outer-frame stream expansion, parent-frame stream identity, empty closure, Collect readiness/hydration, checkpoint rehydration, and failure parity for this shape without calling the materializer's nested-copy helper. Five-level-or-deeper or sibling nested For outside the exact two-sibling CollectionConcat fan-in shape, deeper or multiple nested iterators, other input-driven outer collections, other mixed control flow, and legacy snapshots retain compatibility materializer ownership. The exact eight-node producer-driven extension—one inputless ordinary collection producer feeding outer For.collection, with the same nested body and one ordinary outer-output consumer—is also generic-routed. It owns outer input hydration, per-outer-frame stream expansion, checkpoint rehydration, and failure parity; input-driven inner preparation, deeper or multiple nesting, other mixed control flow, and legacy snapshots remain compatibility-owned. The exact nine-node producer-driven extension adds the same inputless, statically non-empty CollectionConcatInvocation outer producer to the three-level serial nested-For chain. It is generic-routed with the same trace, output, checkpoint, and failure guarantees; other producer classes and deeper, multiple, sibling, mixed, and legacy shapes remain compatibility-owned. The exact serial two-level extension—For.item -> preparation1 -> Iterate1 -> preparation2 -> Iterate2 -> ordinary body -> Collect -> linked ForReturn, plus one ordinary outer-output consumer—is also generic-routed. Its planner and compatibility adapter preserve ordered nested frame paths, close empty streams, resume from checkpoints, and stop downstream completion on failure/retry. Fan-in, sibling iterators, input-driven inner preparation, other mixed control flow, deeper nesting, and legacy snapshots remain compatibility-owned. The exact five-node/four-edge nested-Iterate chain—an inputless ordinary source feeding outer Iterate, whose item feeds one ordinary preparation node, which feeds inner Iterate, whose item feeds one ordinary body—is also generic-routed. Its private planner owns ordered outer/inner frame expansion, empty outer and inner closure, source completion, checkpoint rehydration, and failure parity without calling the compatibility materializer. For, Collect, fan-in, sibling iterators, other mixed control flow, and legacy snapshots remain compatibility-owned; the bounded deeper serial extensions are described next. The exact seven-node/six-edge serial nested-Iterate chain extends this bounded planner by adding one ordinary preparation/Iterate pair between the outer iterator and body. It owns three-component frame paths, ordered expansion, empty outer and intermediate streams, source completion, checkpoint rehydration, and failure parity. The exact nine-node/eight-edge serial chain extends the same planner to four Iterate nodes and four-component frame paths, with the same empty-stream, checkpoint, source-completion, and failure parity guarantees. The exact eleven-node/ten-edge serial chain extends the same planner to five Iterate nodes and five-component frame paths, with the same guarantees. The exact thirteen-node/twelve-edge serial chain extends the same planner to six Iterate nodes and six-component frame paths, with the same empty-stream, checkpoint, source-completion, and failure-parity guarantees. The exact fifteen-node/fourteen-edge serial chain extends the same planner to seven Iterate nodes and seven-component frame paths, with the same empty-stream, checkpoint, source-completion, and failure-parity guarantees. Three or more body-mediated branches, fan-in with four or more direct branches, broader nested iterators, and mixed control flow outside the bounded per-item topology retain _ExecutionMaterializer ownership for copy expansion, grouping, and empty-source handling. _GenericGraphSchedulerAdapter may still own readiness for these unsupported fresh Iterate/Collect graphs; the compatibility materializer owns expansion and materialization. Scheduler/materializer ownership is topology-dependent: pure legacy Iterate/Collect uses generic scheduler readiness with compatibility materializer expansion; forced compatibility and legacy control-flow paths retain _ExecutionScheduler where applicable.

The exact seventeen-node/sixteen-edge serial chain extends the same planner to eight Iterate nodes and eight-component frame paths, with the same empty-stream, checkpoint, source-completion, and failure-parity guarantees. Nine-level or deeper chains and all other expanded, fan-in, sibling shapes outside the exact two-sibling For/ForReturn CollectionConcat contract, mixed, five-level-or-deeper For, Collect, and legacy shapes remain compatibility-owned. A fresh graph with exactly one flat For/ForReturn pair and ordinary body nodes also uses the generic adapter for a literal collection, a supported non-empty input-driven collection, or the exact four-node/four-edge empty input-driven shape with one inputless producer and no downstream node. It projects readiness and invokes the graph-state continuation boundary, which selects the next iteration or finalizes the aggregate without exposing a successor node ID to the generic scheduler. The admitted For planner owns For-specific preparation, continuation, input hydration, iteration paths, and body expansion; it shares only low-level execution-node construction mechanics with the compatibility materializer. Other empty/input-driven shapes, five-level-or-deeper or sibling loops outside the exact two-sibling nested-For CollectionConcat fan-in shape, mixed control flow, and saved-workflow calls remain on the compatibility scheduler. One narrow fresh two-level nested-For shape (one non-empty literal outer collection, one inner For sourced from outer.item, and no continuation nodes) now uses the generic adapter; deeper-than-four-level, multiple-child shapes outside the exact two-sibling CollectionConcat fan-in contract, other empty/input-driven, mixed, and legacy-loaded shapes remain compatibility-owned. The exact fresh three-level serial nested-For shape (one literal outer collection or one statically non-empty CollectionConcatInvocation producer, one direct child at each level, no continuation nodes) also uses the generic adapter. The exact fresh four-level serial nested-For shape (one literal outer collection, one direct child at each level, no continuation nodes) is also generic-routed. Its deepest-empty behavior matches compatibility at the trace, output, and completion boundaries; the generic projection may retain synthetic empty-frame records. Five-level-or-deeper and sibling nested-For shapes outside the exact two-sibling CollectionConcat fan-in contract remain compatibility-owned. The runtime exposes an execution-engine seam: frame-scoped gates, ordered streams, continuations, and authorized child-dependency records are stored in invokeai.app.services.shared.execution_engine; legacy graph and queue behavior is retained behind adapters while those records provide the generic runtime projection for admitted shapes.

The graph runtime adds no author-time graph or invocation fields and does not change web UI interactions. Queue-session serialization exposes the additive optional prepared_iteration_axes runtime metadata field described above.

2) Major Data Types

EdgeConnection

  • Fields: node_id: str, field: str.
  • Hashable; printed as node.field for readable diagnostics.

Edge

  • Fields: source: EdgeConnection, destination: EdgeConnection.
  • One directed connection from a specific output port to a specific input port.

AnyInvocation / AnyInvocationOutput

  • Pydantic wrappers that carry concrete invocation models and outputs.
  • No registry logic in this file; they are permissive containers for heterogeneous nodes.

IterateInvocation / CollectInvocation

  • Control nodes used by validation and execution:

    • IterateInvocation: input collection, outputs include item (and index/total).
    • CollectInvocation: many item inputs aggregated to one collection output.

Internal execution-engine records

  • ExecutionFrame, ActivationGate, StreamBuffer, and ContinuationRecord carry typed runtime identity and lifecycle state without changing author-time graph models. Their private registries are rebuilt from persisted graph results, tokens, and workflow-call state after rehydration.
  • ChildExecutionCapability, ChildExecutionRecord, and ChildDependencyRecord validate authorized parent/child relationships, resource limits, ordered all-of aggregation, failure, cancellation, and idempotent completion.
  • ExecutionPlan stores opaque execution-node IDs, class names, prerequisite IDs, frame values, activation-dependency records, and stable insertion order. ExecutionScheduler owns generic readiness, opaque readiness predicates, intentional skips, deterministic ordering, completion, and durable plan rehydration. It does not import invocation classes and cannot alter author-time graph or frontend contracts.
  • ExecutionEngineRuntime owns these records for one graph state. It is private runtime machinery; it is not a new frontend node, input handle, or public workflow contract.

3) Graph (author-time model)

A container for declared nodes and edges. Does not perform iteration expansion.

3.1 Data

  • nodes: dict[str, AnyInvocation] - key must equal node.id.
  • edges: list[Edge] - zero or more.
  • Utility: _get_input_edges(node_id, field?), _get_output_edges(node_id, field?) These use cached per-node adjacency indexes rebuilt when the edge list changes.

3.2 Validation (validate_self)

Runs a sequence of checks:

  1. Node ID uniqueness No duplicate IDs; map key equals node.id.

  2. Endpoint existence Source and destination node IDs must exist.

  3. Port existence Input ports must exist on the node class; output ports on the node's output model.

  4. DAG constraint Build a flat DiGraph (no runtime expansion) and assert acyclicity.

  5. Type compatibility get_output_field_type vs get_input_field_type and are_connection_types_compatible.

    If output types:

    • For IfInvocation.value, resolve both branch inputs to their source ports, following nested If outputs and deduplicating shared sources. Check every resolved source against the destination using the existing compatibility hooks; the current condition value does not narrow the possible types.
    • Collector item/collection checks and iterator collection checks also use the resolved branch sources. When a branch source is a collector, use its inferred item root type instead of its declared list[Any] output. This preserves type checks through If branches and chained collectors, while matching string branches can feed a string collector without an intermediate string node.
    • When an If.value feeds a typed collection input, each resolved collector branch is checked against the target using its inferred item root. When an If.value feeds Collect.collection, the possible item roots from all resolved branches must have one compatible root; mutually exclusive branches do not permit a heterogeneous collector input. For example, If(Collect[int], StringCollection) -> Collect.collection is rejected.
    • Iterators over collectors validate their item consumers against the same inferred collector root type. Mixed int and float items therefore require consumers compatible with float.
    • If a branch is unresolved, retain the declared output compatibility behavior (Any for collector type inference), and stop downstream incremental type revalidation at that unresolved If. The graph's separate DAG check rejects cycles before projection.
    • This widens compatibility: a direct collector with both int and float items, previously rejected, now resolves to float. Its downstream consumers must accept floats; unrelated mixed item types remain invalid.
    • Ordinary source ports use the direct compatibility path with already resolved nodes, without If traversal or projection allocations.

    Saved-workflow special case:

    • call_saved_workflow currently accepts dynamic destination handles of the form saved_workflow_input::{childNodeId}::{childFieldName} as part of its dynamic call-boundary contract.
    • Those handles are allowed through graph validation even though they are not static Python model fields on the invocation class.
    • Runtime validates them against the selected child workflow's exposed callable interface before applying values to the child graph.
    • The editor preserves dynamic caller values only while the exposed field type remains compatible; type drift at the same child node/field path resets to the selected workflow's current initial value.
    • Saved-workflow picker search is server-backed so large workflow libraries do not require scrolling every page before selecting a workflow by name.
  6. Iterator / collector structure Enforce special rules:

    • Iterator's input must be collection; its outgoing edges use item.
    • Collector accepts many item inputs; outputs a single collection.
    • Edge fan-in to a non-collector input is rejected.

3.3 Edge admission (_validate_edge)

Checks a single prospective edge before insertion:

  • Endpoints/ports exist.
  • Destination port is not already occupied unless it's a collector item.
  • Adding the edge to the flat DAG must keep it acyclic.
  • Iterator/collector constraints re-checked when the edge creates relevant patterns.

After inserting an edge into If.true_input or If.false_input, or into a collector's item or collection, add_edge revalidates affected connections in both directions. Candidate collector checks validate local roots before insertion; after insertion, the walk checks upstream collectors against their updated downstream roots, then follows resolved If outputs and collector chains to check affected collectors and iterators once. One operation-local cache shares resolved collector item types across this walk and strict graph validation. Upstream collectors without a resolvable item type, ordinary nodes, unresolved If outputs, and unrelated unfinished nodes end or leave the dependency walk untouched. If a check fails, the new edge is removed and adjacency indexes are restored before the validation error is raised.

3.4 Topology utilities

  • nx_graph() - DiGraph of declared nodes and edges.
  • nx_graph_flat() - "flattened" DAG (still author-time; no runtime copies). Used in validation and in _prepare() during execution planning.

3.5 Mutation helpers

  • add_node, update_node (preserve edges, rewrite endpoints if id changes; failed rewrites restore the original node and edges, then invalidate lazy adjacency indexes so later lookups rebuild them), delete_node.
  • add_edge, delete_edge (with validation).

3.6 Compatibility

Graph validation runs when a workflow is queued and when a persisted runtime session is hydrated. New inferred collector root checks can therefore reject an older saved workflow at enqueue time, or make a pending queue snapshot unreadable when the queue worker restores it. Unreadable pending snapshots are marked failed and retained for recovery by the queue's quarantine path; they are not silently executed. Completed history entries with unreadable snapshots are shown as failed placeholders with the session and workflow data dropped. The workflow can be edited to make collector inputs and consumers agree with the inferred root before it is queued again.

4) GraphExecutionState (runtime)

Holds the state for a single run. Keeps the source graph intact and materializes a separate execution graph. GraphExecutionState is still the public runtime entry point, but most execution behavior is now delegated to a small set of internal helper classes. For ordinary static DAGs and legacy-shaped If graphs, readiness and completion are projected through the generic ExecutionPlan/ExecutionScheduler adapter. Direct Iterate/Collect graphs without other control-flow nodes also use that adapter: its readiness predicate waits for canonical streams to close and its completion mirrors Iterate outputs into the stream ledger. A fresh graph with one static flat For/ForReturn pair, including a supported non-empty input-driven collection producer or the exact bounded empty input-driven producer shape, also uses the adapter for readiness and continuation transitions; graph state owns the invocation-specific continuation boundary while the generic scheduler remains opaque. Other empty or unsupported input-driven shapes, five-level-or-deeper or sibling loops outside the exact two-sibling nested-For CollectionConcat fan-in contract, saved-workflow calls, and unsupported mixed control-flow shapes outside the bounded per-item topology continue to use the legacy compatibility scheduler until their differential coverage is complete. The narrow canonical two-level nested-For shape, the canonical outer-For/bounded-Iterate/Collect shape, the exact five-node nested-Iterate chain, and the bounded three-, four-, five-, and six-, seven-, and eight-level serial nested-Iterate chains are generic-routed. The nested For shape has one ordinary preparation node from For.item into Iterate.collection, one ordinary body node into Collect.item, the Collect.collection output into the linked ForReturn, and one final outer-output consumer. Each outer iteration, including an empty inner collection, remains isolated by its frame path. Iterate also records non-empty item streams through the generic effect ledger; for this exact seven-node shape, the private nested planner owns per-outer-frame copy expansion, stream identity, empty closure, Collect readiness/hydration, checkpoint rehydration, and failure parity. The exact producer-driven outer extension described above is also generic-routed. The exact fresh three-level serial nested-For shape, including its statically non-empty producer-driven outer variant, and the exact fresh four-level serial nested-For shape are also generic-routed; five-level-or-deeper or sibling nested For outside the exact two-sibling CollectionConcat fan-in contract, nine-level or deeper nested iterators, other input-driven outer collections, mixed control flow, and legacy snapshots retain compatibility materializer ownership. The exact nested-Iterate chains above do not admit For, Collect, fan-in, sibling, or deeper-than-eight-level iterator topology. The materializer remains authoritative for fallback expansion, iteration paths, collector grouping, and empty-source compatibility handling. The exact fresh four-node body shape, exact two- or three-stream fan-in shapes, and exact two-branch body-mediated fan-in use the private planner for those responsibilities; three or more body-mediated branches and four or more direct streams remain compatibility-owned. Direct Collect.item consumers now use the closed stream ledger when available; the full Iterate/Collect compatibility matrix covers empty, nested, fan-in, partial rehydration, failure, cancellation, and retry behavior. The materializer and queue adapters remain the compatibility owners for other shapes. The SQLite/session boundary also preserves this exact nested shape across cancellation, simulated interrupted-process startup, and retry: startup cancels stale in-progress rows without changing the persisted partial snapshot, while retry creates fresh execution/frame/stream identities and completes the same outer collection. Broader nested shapes remain compatibility-owned.

The source graph is treated as stable during normal execution, but the runtime object still exposes guarded graph mutation helpers. Those helpers reject changes once the affected nodes have already been prepared or executed.

4.1 Data

  • graph: Graph - source graph for the run; treated as stable during normal execution.
  • execution_graph: Graph - materialized runtime nodes/edges. This is mutable runtime state, not an immutable audit log. Fresh eager Ifs admit both branch paths after the condition gate resolves, but attach and hydrate only the selected branch edge/value; the two private lazy candidates run only that selected path. Legacy loaded execution graphs may still contain their recorded selected-only projection and skipped nodes. Retry paths rebuild from graph, not from a previously persisted execution_graph.
  • executed: set[str], executed_history: list[str].
  • results: dict[str, AnyInvocationOutput], errors: dict[str, str].
  • prepared_source_mapping: dict[str, str] - exec id -> source id.
  • source_prepared_mapping: dict[str, set[str]] - source id -> exec ids.
  • indegree: dict[str, int] - unmet inputs per exec node.
  • Workflow-call runtime state:
    • workflow_call_stack - active parent call frames.
    • workflow_call_history - completed or failed workflow-call relationships observed by this execution state.
    • workflow_call_parent - parent workflow-call relationship metadata when this execution state is a child session.
    • waiting_workflow_call - the call frame currently suspending this execution state, if any.
    • waiting_workflow_call_execution - the active parent/child workflow-call relationship record for the waiting call.
    • waiting_workflow_call_child_session - attached child execution state for the waiting workflow call, if any.
    • max_workflow_call_depth - runtime guardrail for nested or recursive workflow calls.
  • Prepared exec metadata caches:
    • source node id
    • iteration path
    • runtime state such as pending, ready, executed, or skipped
  • _engine_frame() stores canonical immutable frames in the owning state's private runtime, keyed by state ID, workflow-call depth, and iteration path. The cache is process-local, is not serialized, and retains only the distinct frames requested while that state is alive. State copies get their own cache dictionary; a copy with a new ID starts with no frames for the old ID.
  • If activation dependency lookup has a reverse index from If owner, selected branch, and frame to prepared exec IDs. A restored state rebuilds the index once; newly prepared nodes extend it. The index is derived runtime state, not snapshot data.
  • execution_refs: dict[str, ExecutionReference] - in-memory stable references for prepared execution nodes, including their source node and execution frame; versioned snapshots rebuild these references instead of storing them.
  • execution_tokens: dict[str, ExecutionToken] - in-memory output tokens produced by applied execution results; version-3 snapshots retain only frame-scoped activation tokens.
  • execution_effects: dict[str, list[Any]] - JSON-safe effects accepted for each execution reference.
  • Ready queues grouped by class (private projection): _ready_queues: dict[class_name, deque[str]] and _active_class: Optional[str]. Ordinary static DAGs and legacy-shaped If graphs derive readiness from the generic scheduler; the If adapter stores frame-local activation dependencies whose private gate state plus persisted token checks control generic branch readiness. Supported static flat For graphs, the narrow canonical two-level nested For shape, the exact fresh three-level and four-level serial nested-For shapes (including the exact three-level producer-driven outer variant), the exact producer-driven nested extension, the exact bounded empty input-driven flat For, the bounded two- through eight-level serial nested-Iterate chains, and the bounded per-item Iterate/If/Collect topology use the generic adapter for readiness and continuation projection; other unsupported input-driven, nine-level or deeper, mixed graphs outside that topology retain the legacy scheduler, while saved-workflow calls use the generic lifecycle effect/dependency projection with the dedicated durable queue adapter. Optional ready_order: list[str] prioritizes classes. Queues are rebuilt from persisted execution state when a session is deserialized.

4.2 Core methods

  • next() Returns the next ready exec node. If none are ready, it asks the admitted planner or compatibility materializer to expand more source nodes and then retries. If the execution state is paused on a workflow call boundary, it returns None without scheduling more work. Before returning a node, the runtime helper deep-copies inbound values into the node fields.
  • complete(node_id, output) is the compatibility completion boundary. For a first JSON-safe completion it delegates validation and the atomic scheduler/ledger transition to apply(), including output tokens and synthetic continuation effects for direct For/ForReturn callers. An already-applied node retains the historical idempotent result-replacement behavior; in-memory values that cannot be represented in the JSON effect ledger use the scheduler-only compatibility path and are not persistable.
  • apply(execution_ref, output, effects) validates a result against its prepared node and frame, then applies the result through the existing scheduler while recording references, output/effect tokens, and JSON-safe effects. The transition and ledger update are atomic. Direct complete() callers therefore receive the same durable state for supported values without changing the invocation output contract.

Current execution-effects seam

InvocationContext exposes a restricted execution facade and the underlying execution_effects recorder. The existing invocation output contract remains unchanged. The session runner calls invoke_internal_with_effects() and passes its InvocationRunResult to GraphExecutionState.apply().

The recorder dispatches emit and close_stream by default. A runner may opt an invocation into lifecycle recording with an engine-issued ChildExecutionCapability; this records validated spawn_execution, await, and fail effects and returns a capability-bound child handle. Calls without that capability remain rejected. Mutation effects and queue row creation remain owned by graph/queue adapters, so an invocation cannot mutate either directly. A cache hit never suppresses effects: effect-enabled invocations bypass the ordinary output cache for that dispatch.

IfInvocation is the first control-flow invocation to declare an activation-effect contract. It emits one frame-scoped activation token for the selected true_input or false_input port; GraphExecutionState validates that port against the producing invocation's declared activation fields and persists it without creating a data stream. On rehydration, every activation token is bound to a currently prepared owner and its derived execution reference; its declared port, value, canonical token id, mapping key, and known frame fields must match. Unknown extra frame metadata remains forward-compatible. GraphExecutionState compiles opaque, frame-local activation dependencies for fresh acyclic ordinary-node If graphs without a nested or sibling count limit. The compiled dependencies keep only the gate frontier: a nested If's own admission prerequisites imply its outer gates, while incomparable owners and branch labels remain explicit. With V source nodes, E edges, I If owners and machine word width w, the packed masks use O(VI/w) words and analysis takes O((V+E)I/w) word operations; a nested chain with I = Θ(V) is therefore quadratic in graph size. The materializer's prepared-context path/axis prefix index has worst-case O(P D_path D_axes) work and entries for P prepared contexts, path depth D_path and axis depth D_axes; its near-linear frame-count scaling holds at fixed depths. Fresh If execution is eager except for the two private-branch candidates described above. Loop and saved-workflow graphs outside the exact connected per-item Iterate/If/Collect candidate use the compatibility scheduler and _IfActivationController; legacy snapshots retain their generic compatibility projection and skipped-state metadata. The controller remains the fallback runtime dependency owner, and the schedulers do not call a legacy compiler. _GenericGraphSchedulerAdapter consumes these records through the opaque plan: its readiness callback requires a resolved private ActivationGate and matching persisted activation token for the frame. Both scheduler adapters consume the same dependency decisions without pruning execution edges or calling a type-specific branch scheduler. The compatibility path still projects discarded prepared nodes into legacy skipped metadata when loading old execution graphs; fresh runs do not create that projection. apply() validates and persists the invocation-emitted effect afterward, replacing the compatibility token by stable identity. Activation effects are excluded from data-stream handling. IterateInvocation is the first stream-producing control-flow invocation on this seam: each non-empty prepared copy emits one ordered item effect with its iteration index, and the final copy emits one close_stream effect. Direct Iterate/Collect-only graphs now run through the generic scheduler adapter; its completion maps these results/effects to the existing frame-scoped iteration-stream identity, so legacy output mirroring is idempotent. The exact fresh four-node source/Iterate/body/Collect shape, its ordinary downstream consumers, the exact two- or three-stream fan-in shapes, and the exact two-branch body-mediated fan-in use the private planner described above; broader body-mediated fan-in, four or more streams, and other fallback shapes still let the materializer create prepared copies, derive iteration paths, group collector inputs, and record the explicit close for an empty source. 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 beyond the supported direct-planner shape or full Collect support: fallback shapes still let the materializer own copy expansion, iteration paths, collector grouping, collection-input hydration, and empty-source closure; the exact fresh four-node source/Iterate/body/Collect shape and any number of ordinary downstream-consumer extensions use the private planner instead. No author-time activation ports or literal successor IDs are introduced.

For a fresh graph with one static flat For/ForReturn pair, including the exact empty literal collection case, the same adapter now projects ordinary readiness and calls the graph-state generic continuation boundary after each ForReturn when iterations exist. That boundary carries returned state, honors continue_condition, creates the next prepared iteration when needed, and finalizes output_collection and final_state. For an empty literal collection, the generic path completes through the existing synthetic terminal For result without running the body or ForReturn. The generic scheduler receives only opaque node IDs and dependencies; it never receives a literal next node ID. Compatibility nested-For expansion also checks whether any prepared node is the synthetic empty terminal before finalizing empty contexts; non-empty sibling contexts still run their bodies. This handles mixed outer collections such as [[], [1]] as well as all-empty collections. The compatibility continuation bridge remains available only for unsupported loop shapes and explicitly legacy-loaded snapshots. The exact four-node/four-edge empty input-driven shape with one inputless static-list producer is also generic-routed. Other input-driven outer collections, five-level-or-deeper or sibling nested loops outside the exact two-sibling CollectionConcat fan-in contract, or unsupported mixed loop shapes remain compatibility-owned; the exact fresh two-sibling For/ForReturn CollectionConcat fan-in shape, the narrow canonical two-level, and exact fresh three-level and four-level serial nested-For shapes (including the exact three-level producer-driven outer variant), the canonical bounded internal Iterate/Collect shape, the exact producer-driven outer nested extension, and the bounded two- through six-, seven-, and eight-level serial nested-Iterate chains are generic-routed. On rehydration, the generic adapter restores the active class-drain boundary so an in-flight nested stream resumes in the same frame/sequence order.

For scheduling is linear in the collection size. The continuation transfers ownership of the remaining collection to the next prepared For node instead of deep-copying it at every iteration. Source completion uses a derived, lazy per-source count of pending prepared executions, and continuation validation uses the existing prepared-For index rather than rescanning every prior iteration. The count is runtime-only and is rebuilt after snapshot rehydration; it does not change the persisted execution-state or invocation contracts. The focused For performance, collection-ownership, count-invariant, and generic/compatibility differential tests cover this boundary. The direct Iterate planner has separate readiness projection costs and is not part of this For-specific optimization.

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 kind, and optional sequence. loop_linkage remains association metadata and never becomes a data token. This ledger is currently additive. Ordinary static-DAG and legacy-shaped If readiness comes from the generic scheduler through a compatibility projection; the activation token is authoritative for generic If branch readiness. Supported static flat For readiness and continuation transitions also use the generic adapter, while materialization and type-specific control paths remain authoritative for unsupported loop shapes and workflow-call graphs.

The generic scheduler is an in-memory graph-state component only. It does not create, update, retry, cancel, delete, or recover SessionQueueItem rows and does not own queue statuses or events. A graph containing a saved-workflow call therefore stays on the legacy scheduler and the dedicated workflow-call queue adapters.

Control-flow nodes remain concrete invocation subclasses that declare frame-scoped data, activation, stream-closure, child, and terminal effects. The generic scheduler selects nodes from required effects for the current frame; it never receives a literal successor-node ID. Current implementation is narrower: IfInvocation and non-empty IterateInvocation use the effect recorder for activation and stream effects; the supported static flat For/ForReturn shape, including the non-empty input-driven producer case, uses it for frame-scoped continuation effects and generic readiness/continuation projection. Direct Iterate/Collect-only graphs use the generic scheduler adapter for readiness and completion. The exact fresh four-node source/Iterate/body/Collect shape, its ordinary downstream-consumer extensions, the exact two- or three-stream fan-in shapes, and the exact two-branch body-mediated fan-in use the private planner for expansion and empty-stream closure; broader body-mediated fan-in, four or more streams, workflow-call invocations, unsupported loop shapes, and mixed control-flow graphs remain on compatibility paths. ForInvocation and ForReturnInvocation now declare one validated, frame-scoped continuation effect per prepared invocation: For starts the for continuation with its iteration/state payload, and ForReturn completes it with output/state/ continue-decision data. Session-built recorders bind the effect reference to the graph-state ID, durable frame ID, iteration path, and workflow-call depth; graph-state validation rejects a stale or cross-frame continuation before mutation. Graph state persists these effects under the invocation reference and excludes loop_linkage from token data. Continuation payloads are normalized to JSON values before runtime or durable comparison. Exact duplicate continuation effects in one batch are idempotently retained once; conflicting terminal payloads, including type-distinct JSON values, are rejected transactionally. Before complete() mutates scheduler state, ForInvocationOutput and ForReturnInvocationOutput continuation fields must match their prepared nodes; the For item must match its prepared collection item, and continuation effects must independently match those prepared values. Current versioned snapshots require an execution-effect ledger: each bucket must belong to an executed prepared node, while unknown references, pending-node buckets, missing executed markers, missing required continuation effects, and malformed ownership are rejected. Persisted For outputs are checked against prepared iteration data and, after finalization, their authoritative returned collection and final state. Unversioned legacy snapshots retain the compatibility loader. For start payloads must match the prepared index, collection total, and state before mutation. This is the invocation/effect ownership seam: the generic adapter calls the graph-state generic continuation boundary, which creates the next prepared iteration, aggregates outputs, and finalizes the supported flat loop. The compatibility bridge remains for other unsupported input-driven, five-level-or-deeper/sibling nested outside the exact two-sibling CollectionConcat fan-in contract, mixed-loop, and legacy-snapshot execution; those paths retain their existing materialization and linkage ownership. The supported fresh static flat For/ForReturn shape, the exact bounded empty input-driven flat shape, the narrow canonical two-level and exact fresh three-level and four-level serial nested-For shapes (including the exact three-level producer-driven outer variant), and the canonical bounded internal Iterate/Collect shape use the generic adapter. For If, generic readiness consumes opaque frame-local plan dependencies and requires a resolved private ActivationGate plus a persisted activation token. Fresh ordinary-node graphs admit and evaluate both branch paths by default, then attach only the selected branch edge/value to the pending If; only the two bounded candidates run just the selected path. Legacy snapshots retain their recorded selected-only projection and skipped metadata. This is not token-built successor topology. Loop and workflow-call adapters still own materialization for unsupported shapes and durable queue lifecycle. Web UI interactions remain unchanged; the generated queue-session API contracts add the optional prepared_iteration_axes runtime metadata field described above.

The test-only differential harness at tests/app/services/shared/test_execution_engine_differential.py compares generic and forced-compatibility scheduling for static DAGs, a constructed mixed/nested If graph, the supported static flat For/ForReturn shape, the narrow canonical nested-For shape, and the canonical nested For/Iterate/Collect shape. The latter covers empty inner collections and dump/load resume ordering. Its fixture corpus covers fresh completion, true/false branch selection, nested branch isolation, partial checkpoints, versioned rehydration, in-flight claim replay, activation-token persistence, injected failure, loop continuation, carried state, early break, output None, cancellation/retry isolation, and continuation/effect integrity. The If comparison includes strict source-level results, executed history, errors, terminal state, and normalized indegrees; compatibility skip propagation must not leave stale downstream indegrees. Both scheduler adapters expose the same skip transition; the legacy path releases downstream indegrees without trying to hydrate inputs from the skipped node. It does not claim durable persistence of the generic scheduler's private claim set, Other unsupported input-driven, deeper/multiple, mixed, or workflow-call loop shapes remain on their compatibility paths. Real queue/processor coverage in tests/app/services/session_processor/test_if_processor_sqlite.py also exercises true and false If selection, cancellation before and after resolution, retry from a canceled SQLite item, fresh execution identities, and selected-only completion. Stale identity and legacy loop-frame rehydration are covered separately by the differential and graph-state tests.

Workflow-call note:

  • GraphExecutionState can represent a paused parent execution plus an attached child execution state, but it does not itself orchestrate child execution.
  • In the current implementation, DefaultSessionRunner.run_node() invokes CallSavedWorkflowInvocation through the effect-aware path. The invocation records capability-bound spawn_execution plus await (or fail) intent; GraphExecutionState.apply() persists the pending batch, WorkflowCallCoordinator handles durable child setup, and WorkflowCallQueueLifecycle later resumes or fails the parent based on the generic child dependency and child queue outcome.
  • Child SessionQueueItem rows created by the coordinator now carry explicit relationship metadata such as workflow_call_id, parent_item_id, parent_session_id, root_item_id, and workflow_call_depth, even though the higher-level scheduler semantics are still evolving.
  • The session_queue schema now has matching columns for those relationship fields, and parent queue items can enter a waiting status while suspended on a child workflow execution.
  • Queue lifecycle semantics for workflow-call chains are:
    • child success resumes the waiting parent
    • multiple child queue rows may complete under one waiting parent when the called workflow contains direct batch nodes; the parent resumes only after all expected child rows complete
    • child failure fails the waiting parent and can cascade upward through ancestors
    • failing child rows cancel their remaining workflow-call siblings before the parent is failed
    • cancelation is chain-aware across parents and children, including nested descendants of batched siblings
    • "all except current" queue actions preserve the active current item plus its workflow-call chain, while still canceling or deleting unrelated waiting chains
    • startup recovery cancels interrupted in_progress or waiting workflow-call chains, including pending descendants
    • deleting a workflow-call queue row currently deletes the whole parent/child chain rather than leaving orphaned rows behind
    • retry is root-oriented and should not be exposed directly on child queue rows in the UI
    • child queue-row creation is cleaned up on boundary-setup failure and child fan-out is bounded by remaining queue capacity
    • child workflows that mix supported batch nodes with unrelated generator nodes are rejected
  • The generic ChildDependencyRecord now owns the internal child identity, ordered all-of aggregation, capability identity, resource limits, idempotent terminal transition, and parent terminal decision alongside this workflow-call lifecycle. Existing queue fields, statuses, cancellation, retry, and event behavior remain authoritative; queue-row creation and recovery are not silently delegated to an in-memory record.

4.3 Runtime helper classes

GraphExecutionState now delegates most runtime behavior to internal helpers:

  • _PreparedExecRegistry Owns the relationship between source graph nodes and prepared execution graph nodes, plus cached metadata such as iteration path and runtime state.
  • _ExecutionMaterializer is the compatibility planner facade. It expands source graph nodes into concrete execution graph nodes when an unsupported or legacy shape runs out of ready work. On those compatibility paths it owns iterator expansion, collector grouping, prepared-parent selection, and creation of execution-graph edges. When matching prepared parents for a downstream exec node, skipped prepared exec nodes are ignored and cannot be selected as live inputs. The class lives in graph_materializer.py and is re-exported by graph.py.
  • _GenericForPlanner owns admitted fresh For/ForReturn preparation, continuation transitions, carried state, frame paths, and nested For completion. It uses _ExecutionNodeBuilder only for low-level copy, edge, input, and scheduler-registration mechanics; it does not construct _ExecutionMaterializer. Unsupported and legacy shapes retain the compatibility facade.
  • Private graph_iterate_planner.py expands the supported direct and body-mediated Iterate/Collect shapes. Graph-state method wrappers preserve the admission, copy creation, edge attachment, and atomic preparation entry points. The planner continues to use the graph state's journal, mappings, caches, and scheduler.
  • Private graph_loop_topology.py classifies every For once per graph: its body path to the linked ForReturn, and its supported nested body shape (NestedForBody, NestedIterateBody, or NestedIterateChain). It also recognizes the whole-graph serial Iterate chain (NestedIterateSequence). Graph validation rejects nested For and Iterate-in-For bodies without a shape. Graph state, both schedulers, the nested planner, and the materializer read GraphExecutionState._loop_topology() instead of re-recognizing shapes per completed node. Classification reads only node types and edges, so it is dropped with the cached flat source graph (graph mutation, transaction rollback, or a runtime-cache reset) and rebuilt from the next one.
  • _IfActivationController Owns fallback If dependency analysis for legacy snapshots and control-flow graphs that use compatibility materialization. Fresh acyclic ordinary-node If graphs compile opaque, frame-local dependency records in graph state, regardless of nesting or sibling count. Fresh branches are eager unless the exact lazy candidate predicate admits one private root If or the connected per-item Iterate/If/Collect topology. External consumers of branch producers force eager execution; legacy skipped-state projections remain loadable.
  • Private graph_if_dependencies.py compiles fresh activation dependencies, and graph_if_runtime.py records dependencies and evaluates admission against gates and tokens. GraphExecutionState retains the method entry points, durable token/reference/effect storage, derived caches, transaction journal, and fallback controller selection.
  • Private graph_scheduler.py contains _ExecutionScheduler and _GenericGraphSchedulerAdapter. graph.py deliberately re-exports both classes, preserving their existing import paths and monkeypatch seams while keeping scheduler implementation details out of the graph-state façade.
  • _GenericGraphSchedulerAdapter Projects the generic ExecutionPlan/ExecutionScheduler into the existing state fields for ordinary static DAGs and legacy-shaped If graphs; the generic scheduler owns opaque readiness, intentional discards, indegree transitions, deterministic ordering, claimed work, and completion. The adapter registers activation dependencies from graph-state compilation or the fallback controller and checks private gate state plus persisted activation tokens. Fresh If branch nodes are admitted before materialization; legacy prepared nodes may still be discarded for compatibility. If scheduling does not call a type-specific branch scheduler, prune, or delete execution edges.
  • _ExecutionScheduler Owns materialized-graph indegree transitions, class-grouped ready queues, downstream release, control-flow continuation scheduling, and the shared opaque activation-dependency projection for compatibility graphs.
  • _ExecutionRuntime Owns iteration-path lookup, collect input ordering, and input hydration for prepared exec nodes. Its implementation lives in the private graph_execution_runtime.py module; graph.py re-exports the runtime class and its fan-in record types so existing imports and test seams remain stable.
  • 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. 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 gate state from condition results or persisted activation tokens, non-empty iteration streams from durable effects and legacy Iterate results, explicit empty-source closes, For continuation identity, and ready queues from execution_graph, indegree, executed, and results. Activation tokens persist; private ActivationGate runtime state does not and is reconstructed from condition results or persisted activation tokens. Pending selected If inputs remain excluded from ready-queue projection during rehydration, preserving ready-node order across checkpoints before the pending If is admitted. Before token validation, missing legacy iteration-path metadata is rebuilt from the prepared execution graph. Persisted activation identity is then validated fail-closed against prepared owners, derived references, declared activation fields, canonical ids, and known frame fields; extra frame metadata is retained. Version-3 dump_execution_state() omits derived execution references and ordinary output tokens, keeps activation tokens, retains stream/continuation effects needed to resume, and drops terminal saved-workflow lifecycle effects and completed child-dependency records. Active workflow-call child state remains attached for recovery; completed calls use their workflow-call history instead. Ordinary model serialization and public schemas omit these four internal ledgers; private helper objects are not serialized. Queue snapshots carry an additive execution-state version marker and use a version-aware loader; version-2 snapshots remain readable, including snapshots predating prepared_iteration_axes, while legacy unmarked snapshots are treated as version 0 and unreadable snapshots are quarantined by queue service.

4.4 Compatibility preparation (_prepare())

The steps below describe compatibility materialization for unsupported shapes and legacy snapshots. Supported fresh Iterate/Collect shapes use the bounded planners described above and do not perform this ancestry or collector materialization.

  • Build a flat DAG from the source graph.

  • Choose the next source node in topological order that:

    1. has not been prepared,
    2. if it is an iterator, its inputs are already executed,
    3. it has no unexecuted iterator ancestors.
  • If the node is a CollectInvocation: group prepared parent exec nodes by iteration path and create one collector exec node per group. A collector collapses the immediate iterator that feeds its item input, but preserves enclosing iterator paths. This lets a shape such as outer_iter -> inner_collection -> inner_iter -> collect -> consumer produce one collected result per outer iteration instead of mixing all inner items into one global collection. Incoming collection inputs are treated as ancestor groups and are copied into each matching descendant item group. Root item inputs are also broadcast into every deeper item group; when deeper groups exist, they do not create an extra root-only result. Empty enclosing groups remain at their original frame path, even when a deeper nested stream has no items; no deeper child frame is fabricated to hold an empty group.

  • Otherwise: compute all combinations of prepared iterator ancestors. For each combination, choose the prepared parent for each upstream by matching iterator ancestry, then create one exec node. If a node no longer has visible iterator ancestors because the source path crosses a collector, prepared parent iteration paths are still used to materialize one downstream exec node for each preserved collector path.

  • For an IfInvocation with a wired condition, prepare condition inputs first. Readiness waits for condition source nodes and their ancestors, and for the iterators in the If's own iterator ancestry, which give it its frames and are never gated by it; other iterator ancestry on a branch cannot hold the gate open. After the condition resolves, activation dependencies gate branch work. Fresh eager graphs admit both branch paths but attach only the selected input edge/value; the exact private lazy candidates run only that selected path. Legacy snapshots retain their selected-only projection. A literal condition is gated by the ancestry of its known selected branch; an iterator that only feeds the unselected branch, such as one closed by a branch Collect, cannot hold it back. Its frames still wait for the iterators open at it. Unsupported mixed control-flow shapes use the compatibility controller. Rejected lazy-branch sources are retired empty without invocation. Pending If state is reconstructed from durable execution edges, prepared mappings, and the activation token after rehydration.

  • A branch Collect whose item source combines exactly two sibling Iterate axes can close the axis beside the If condition axis. The compatibility materializer expands that sibling Iterate beneath each resolved If frame selecting the Collect branch, pairs only the matching condition frame, and closes the branch interval empty when no frame selects it. Independent iterator axes follow destination input-port order; node insertion order and node IDs do not reorder distinct ports. The condition and branch axes must be nested; divergent inner axes are rejected even when they share an outer axis. An uncollected deeper branch axis that cannot be selected from the condition frame raises UnsupportedIfIterationAxesError before invocation. One sibling Iterate can belong to only one If owner. A Collect may feed both branch ports of that same If; if it would serve different If owners, materialization raises UnsupportedSharedIfCollectBranchesError before invocation rather than dropping frames.

  • For a root-conditioned If whose selected branch input is a completed direct Collect, compatibility materialization preserves each non-root Collect iteration path through ordinary descendants. It runs one If execution per preserved group, with a matching activation frame; the already-resolved root activation gate and token are aligned to the first group. Ordinary descendants of If.value use those same frames, and a downstream Collect gathers every completed group through the standard grouping behavior. This bounded case does not imply propagation through further nested controls or loops.

  • For each new exec node:

    • Deep-copy the source node; assign a fresh ID (and index for iterators).
    • Cache the preserved iteration path when the materializer has one, such as for grouped collectors.
    • Wire edges from chosen prepared parents.
    • Set indegree = number of unmet inputs (i.e., parents not yet executed). The generic scheduler mirrors this into its opaque plan for ordinary static DAGs, legacy-shaped If graphs, and direct Iterate/Collect-only graphs.
    • Try to resolve any If-specific scheduling state.
    • If the node is ready and not deferred by an unresolved If, enqueue it into its class queue.

next() applies _GenericForPlanner.prepare() and If-bearing _ExecutionMaterializer.prepare() calls as state transactions. If one of those expansions raises, journaled execution-graph, prepared-mapping, frame, activation-token, gate, and scheduler mutations are rolled back; derived caches are reset and ready queues are rebuilt from the restored state before the exception propagates. A later next() can retry from the pre-expansion state without retaining partially materialized nodes or activation state. If-free _ExecutionMaterializer.prepare() runs without this transaction wrapper to avoid overhead on ordinary graphs; root-If completed-Collect expansion retains its own transaction.

This transaction boundary does not cover pending-If attachment (_attach_pending_if_inputs), prepare_nested_iterate_sequence(), or direct Iterate/Collect preparation. Those operations run outside the wrapper and must be considered separately when reasoning about rollback behavior.

The execution-node builder retains a filtered iterator graph for the current source-graph identity. It builds this graph when compatibility preparation needs sibling-Collect relationships or iterator ancestry. The graph replaces Collect item inputs with its collection inputs for traversal; edge-scoped ancestry retains item axes needed for grouping. Final For outputs do not carry an open iteration axis. If branch gating and axis validation instead use the ancestry's frame iterators, which follow a final For output into the For's parent frame: the For's own axis is closed there, but the axes it runs under stay open.

Iterator ancestry owns ordered ancestor and edge-axis analysis plus effective-path memoization for one graph traversal. The builder shares that object across candidate admission and materialization. Its separate per-source axis cache adds sibling-Collect relationship ordering and is cleared when that relationship graph is established; it does not repeat the ancestor-list cache. Independent parent paths merge in destination input-port order, with source node ID used only to break ties on the same input port. A standalone path lookup owns a separate context. These caches are process-local and rebuilt when graph identity changes; they are not serialized.

Each pending-If attachment sweep also builds a local index from source edge and prepared iteration path to execution node ID. It enumerates each prepared source edge once per sweep, then resolves the deepest matching path prefix for each condition-ready If frame. This preserves deterministic attachment while avoiding a repeated full prepared-source scan for every If frame. The index is discarded at the end of the call; a later sweep rebuilds it from the current prepared mapping, so it is not retained across graph edits, rollback, or state restore. This optimization bounds candidate lookup for attachment; other preparation and branch-context work has separate costs.

Inactive branch-source retirement uses an event queue owned by one materializer. A rejected branch queues only its activation-dependent source IDs when the corresponding If resolves; an empty condition Iterate queues both branch source sets. Each preparation pass checks the queued sources in topological order rather than rescanning every unprepared source. The index is tied to source-graph identity, rebuilt from durable activation state after graph changes or state restore, and is not serialized. Queue mutations participate in execution-state rollback. A process-local completed-source ID cache avoids rescanning the full executed set for every inactivity check; For/Iterate completion invalidates it because axis completion can change branch activity. Registering a new prepared execution evicts its source ID from the cached set; re-registering an existing execution does not. Graph edits or transaction rollback clear the derived cache. This bounds source-inactivity checks for serial If chains, but does not bound other preparation or branch-context costs.

Literal-condition If fallback path inference uses the same ancestry context, including nested selected branches.

Ordinary source nodes are traversed with an explicit stack, but path inference recurses when a selected input comes from another unprepared If. A direct lookup over a 400-node literal-If chain raises RecursionError with CPython 3.12's default recursion limit of 1,000; serial execution of the equivalent 1,100-node graph completes. This is a pathological depth limit in the unresolved compatibility fallback, not a general graph-size limit. Supporting arbitrary nested-If depth requires converting that path inference to an explicit stack.

4.5 Readiness and class ordering

  • _enqueue_if_ready(nid) applies generic readiness: indegree == 0, not executed or claimed, and, for an If branch-local node, a resolved matching private ActivationGate and persisted activation token are present for its frame. Lazy candidates admit only selected branch nodes; eager shapes admit both after the gate resolves. For direct Collect nodes, the adapter also requires available Iterate streams to be closed. Compatibility uses the same opaque activation dependencies; legacy skipped metadata remains for old snapshots.
  • _get_next_node() uses the generic scheduler for ordinary static DAGs and fresh acyclic ordinary-node If graphs, projecting deterministic class/frame order into the compatibility queues. The exact per-item Iterate/If/Collect candidate also uses it. Other loop and saved-workflow graphs use _active_class and the legacy class queues. No batch-size or fairness cap is currently implemented.

4.5.1 Indegree (what it is and how it's used)

Indegree is the number of incoming edges to a node in the execution graph that are still unmet. In this engine:

  • For every materialized exec node, indegree[node] equals the count of its prerequisite parents that have not finished yet.
  • A node is eligible for enqueue when indegree[node] == 0, it has not executed, and it is not deferred by an unresolved If.
  • When a node completes, the active scheduler decrements indegree[child] for each outgoing edge. Any child that reaches 0 is enqueued. The generic plan preserves repeated edges as repeated prerequisites, matching execution-graph indegree semantics.

Example: edges A->C, B->C, C->D. Start: A:0, B:0, C:2, D:1. Run A -> C:1. Run B -> C:0 -> enqueue C. Run C -> D:0 -> enqueue D. Run D -> done.

4.6 Input hydration (_prepare_inputs())

  • 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 condition and the branch inputs attached by the current execution mode. Eager shapes admit and evaluate both branch paths, but attach and hydrate only the selected input edge/value; lazy candidates run only the selected path. Legacy snapshots retain their recorded projection. As a defensive guard against inconsistent runtime or deserialized session state, the runtime raises if an attached input edge points at an exec node with no stored runtime output. In normal scheduling this path should be unreachable.
  • For all others: deep-copy each incoming edge's value into the destination field. This prevents cross-node mutation through shared references.

4.7 Lazy If semantics

IfInvocation is eager by default and acts as a lazy branch boundary only for two bounded candidates.

  • The condition input must resolve first.
  • By default, once the frame-local condition gate resolves, both branch paths are admitted and evaluated; the pending If attaches and hydrates only the selected branch edge/value. Shared ancestors run once and remain shared.
  • Lazy execution applies only to one root ordinary If with private branch dependency closures and no other control flow or iterator axes, or the exact six-node, seven-edge single-axis shape described above. A branch producer with any external consumer makes the whole If eager.
  • Legacy snapshots keep their recorded selected-only and skipped-node projection. Fresh eager execution does not create skipped nodes. Both scheduler adapters use append-only execution edges; no type-specific branch scheduler performs pruning or skip propagation.
  • An If runs once per frame of the innermost iterator in its iterator ancestry, whichever branch is selected. When a condition, literal or computed in the root frame, selects a root value while the other branch carries an open Iterate, the If keeps that per-item cardinality and returns the root value in each item frame. Eager fallback admits both branch paths in each frame; a private lazy candidate leaves the unselected branch inactive.
  • Open iterator frames run regardless of the If condition. A selected For final output closes only its own axis while keeping enclosing axes open; an unselected For-final-only axis adds no If frames unless the same axis also reaches the If directly.
  • Iterating a root For.output_collection opens a new Iterate axis; a downstream Collect.item closes it into the root group.
  • Before invocation, graph validation raises UnsupportedIfIterationAxesError for these frame sets:
    • Two unrelated iterators open at the If (neither stays open inside the other, judged by iterator ancestry, where a Collect.item input closes an axis), unless the input that decides the If's frames carries both: the condition, or the known selected branch of a literal condition. A cross product computed by one selected node therefore keeps its frames, while one Iterate per branch, or a root value selected over a cross product, is rejected. A literal condition whose selected branch carries one of the two also runs when the other comes only from the unselected branch and either the unselected one reaches the If only through a For final output, which adds no frames, or the selected axis, or an iterator enclosing it, reopens the unselected one. An Iterate reopens every axis of its Collect's item (the one the Collect closes and the ones it keeps), and transitively what those reopen, when its collection comes directly from a Collect with a single item input and nothing appended through its collection: each item becomes one frame, so the selected frames pair with those axes by index. Any other reopen (a node between the Collect and the Iterate, or extra collected items) can change the length, and selecting the upstream axis over its reopening pairs it with per-group frames; both would drop or invent frames.
    • A branch whose open axes, or the axes a branch Collect keeps, are deeper than a per-item condition's, unless both branches carry the same deeper axes. Each Collect.item closes its innermost axis and keeps the outer axes; this identity is followed through chained Collects and ordinary passthrough nodes. Separate branch Collect chains may retain the same axis identities and frames; sharing a Collect node is not required. Matching lengths alone do not establish alignment. An asymmetric or distinct-axis Collect chain is rejected before invocation. Unrelated sibling Iterate streams that reach one Collect directly or through literal If nodes close into their own sibling group, with values kept in stream order; they are not paired by numeric iteration index. An Iterate directly fed by Collect.collection consumes each prepared Collect run independently and preserves its parent frames. A downstream Collect retains empty outer groups. A For.output_collection passed through zero or more literal-True If nodes into Collect.item keeps the For's parent frame for every chain length; the For final closes only its own axis. When a wired Boolean condition resolves per parent frame to select a For.output_collection, its If output retains those parent-frame groups through Collect, including through an outer literal-True If. Root Collect.item inputs broadcast into each such selected-For parent group; they do not add a separate root result.
    • A root-frame condition choosing between branches with different deeper axes. When both branches retain the same nested axes, a root-frame condition runs the If once per available frame of the innermost axis. Selecting a root value over an axis reached only through an unselected For final output is the root-frame exception described above; literal and computed root conditions have the same result cardinality. Validation and frame inference use graph topology and statically known conditions only; they do not depend on unresolved runtime branch selections.
  • A completed Collect on a branch closes its item axis; the If does not wait for an Iterate that only that Collect consumes. Runtime groups after the If follow the selected output's frame provenance: a selected Collect keeps its preserved outer groups, while a Collect or For on the unselected branch contributes no groups. Numeric iteration indexes alone do not align unrelated sources. A selected For final output keeps its parent frame grouped when the condition establishes that frame, including through Collect; selecting the other branch does not retain those parent groups. This runtime behavior is separate from static graph validation and admissibility. A Collect of a For final output groups in the For's parent frame, so it keeps that frame open.
  • A For whose loop body belongs to an If branch (its body feeds the ForReturn, which reaches the For only through the loop linkage) is admitted when that branch's frame gate opens; a For nested in that body comes along with the branch closure. After the frame-local condition resolves, eager execution admits both closures, so an unselected branch's For, body, or nested For may run and a failure there fails the execution. A For nested in the body of an ungated For runs with that loop, while two sibling Fors remain independent. A For whose final output feeds the If directly attaches its output after finalization. With a per-item condition, selected frames wait for that final output before the If input is attached, while eager execution may run the unselected final-output branch as well; the If attaches only the selected value after all required inputs are ready. A literal condition establishes the initial gate from its selected-branch ancestry, then admits the other branch before the If resolves.
  • An If has no execution in a frame where the innermost iterator in its ancestry has no items. When an Iterate in its iterator ancestry, whether it feeds If.condition or a branch, has no items at all, no If execution is created, the branch sources are retired empty, the downstream Collect produces an empty collection, and the state completes without an error. The same holds for a literal condition whose selected branch runs under an empty Iterate reached only through a For final output. Retirement may add the branch source IDs to executed_history; neither branch invocation runs and no branch result is created. With a wired root-frame condition and one parent Iterate carried through a selected For final output, the If broadcasts over the parent frames; an empty parent Iterate gives it no executions and the downstream Collect remains empty. Multiple unrelated open frames that cannot be paired still fail validation before execution.
  • The SQLite queue/processor path has evidence for cancellation before and after If resolution and retry from each boundary for both condition polarities. A canceled attempt keeps its activation ledger and cannot resume; retry starts a fresh state and must emit a fresh matching activation token before successful completion.
  • Legacy retired or skipped branch-local exec nodes may still be treated as executed for compatibility scheduling, but they do not create entries in results.
  • Shared ancestors still execute if they are required by the selected branch or by any other live path in the graph.

This behavior is implemented in the runtime scheduler, not in the invocation body itself.

5) Traversal Summary

  1. Author builds a valid Graph.

  2. Create GraphExecutionState with that graph.

  3. Loop:

    • node = state.next() -> may trigger _prepare() expansion.
    • Execute node externally -> run_result.
    • state.apply(execution_ref, run_result) -> updates indegrees, If state, ready queues, and the execution ledger.
    • Apply each result before requesting the next node; the compatibility scheduler is sequential. The generic scheduler may return None while a previously returned invocation is still claimed and in flight.
  4. next() returns None when the graph is complete, when workflow-call/lifecycle work is suspended externally, or while the generic scheduler has claimed in-flight work. If preparation finds no ready invocation for an incomplete state with no such pending work, it raises GraphExecutionStalledError.

When an invocation reuses cached media, BaseInvocation.invoke_internal() registers a temporary hold for the consuming queue session before returning the cached output. The intermediates service checks that every referenced image/video record still exists and registers the hold under the same database lock as cleanup. If cleanup already deleted a record, the invocation recomputes its output. Holds remain effective while the session is pending, running or waiting. A cache hit reuses an old row, so its media is then treated as recent for the usual grace window, counted from when cleanup first sees the session ended. A completed child remains protected while its root workflow is active: completion precedes the transaction that records its returned media in the parent. Cleanup protects both its cached references and its produced media through that handoff, including nested calls. Queue history pruning retains these children until their root ends. Other terminal/deleted sessions' holds are released on the next cache hold or cleanup query and dropped after that grace. Holds share the cache's process lifetime; after restart, persisted queue sessions supply their own media references and the invocation cache is empty.

In normal execution, all runtime expansion occurs in execution_graph with traceability back to source nodes.

Image input records

ImagesInterface.save() preserves the complete session source graph in invokeai_graph, including all original nodes, input literals and edges. The editor uses it to restore the authoring structure when invokeai_workflow is absent. The workflow JSON, when present, is saved unchanged and takes precedence in the editor.

A separate invokeai_resolved_inputs PNG text field contains a compact Graph JSON input record. It contains the active invocation with its runtime ID and resolved inputs, plus ordinary upstream invocations and the edges connecting them. This preserves inputs such as a Noise invocation's actual seed and intermediate math values. Traversal uses the execution graph's indexed input edges and stops before Iterate, Collect, If, For, ForReturn, saved-workflow calls, collection-valued output edges, and edges whose resolved destination input is a list. Inputs received across those boundaries are already resolved on the retained downstream invocation. Future nodes, unrelated branches, sibling iterations, and upstream collections beyond those boundaries are excluded. Before runtime materialization, only the active invocation's inputs are recorded.

The resolved-input record describes the image's invocation; it is not used for editor restoration. Optional internal resolved_inputs parameters carry it through ImageService.create() and DiskImageFileStorage.save(). The existing GET /api/v1/images/i/{image_name}/workflow response exposes this PNG record as optional resolved_inputs stringified Graph JSON, with null for older images. ImageService.get_resolved_inputs() reads the file from the record's subfolder, behind the route's existing read-access and storage-maintenance guards. Inspection can prefer this record, while the response's unchanged workflow and source graph remain the editor restoration inputs. It is not merged into user-owned invokeai_metadata. Legacy calls omit the field, and old PNGs remain readable without rewriting. Video responses use the shared response model's null default. Image file copies preserve all chunks.

No expanded runtime graph is serialized for either field. Each image stores one source graph plus one compact input record. Nested-loop tests count their combined UTF-8 bytes. A source with a literal collection includes that collection in every image, so its authoring payload grows with the collection size. Resolved collection inputs retained on the active invocation can also grow; the compact record is not universally a single node or constant-size.

6) Invariants

  • Source Graph remains a DAG and type-consistent.
  • execution_graph remains a DAG.
  • Nodes are enqueued only when indegree == 0 and they are not deferred by an unresolved If; generic If branch-local readiness additionally requires matching private ActivationGate runtime state and a persisted activation token.
  • results and errors are keyed by exec node id.
  • Applied execution references are unique to one prepared node and frame; their output/effect records are JSON-safe.
  • Output and emit effects produce frame-aware tokens. close_stream produces a stream_end token. Association fields such as loop_linkage are never stored as data tokens.
  • A non-empty IterateInvocation emits one item effect per prepared copy, with a contiguous sequence beginning at zero, and closes its canonical source/parent-path stream on the final copy. Exact output mirroring is idempotent. The private planner closes empty streams for the exact fresh source/Iterate/body/Collect, exact two- or three-stream fan-in, and exact two-branch body-mediated fan-in shapes; broader body-mediated fan-in, four or more streams, and other fallback shapes retain materializer compatibility ownership.
  • Collectors wait for available direct Iterate streams to close, then aggregate their ledger values in stream order; order across sources is unspecified, and repeated values are preserved. They 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.
  • Fresh eager fallback may materialize and run both branch paths. Only the two private lazy candidates leave unselected branch-exclusive nodes unmaterialized. Legacy snapshots may retain skipped nodes for compatibility; they are not failed.

7) Extensibility

  • New node types: implement as Pydantic models with typed fields and outputs. Register per your invocation system; this file accepts them as AnyInvocation.
  • Scheduling policy: adjust ready_order to prioritize class queues. A batch-size or fairness cap is not currently implemented.
  • Dynamic behaviors: effect-enabled invocations may record frame-scoped stream, activation, and authorized child lifecycle intent. GraphExecutionState.apply() remains the transactional graph boundary: it persists a pending saved-workflow lifecycle batch and generic child dependency, while the matching queue adapter owns durable row creation and queue transitions. Mutation effects remain rejected.
  • Workflow call boundaries: GraphExecutionState can suspend a parent execution state on a workflow call, attach a child execution state, and later resume the parent without mutating the source graph.

Current limitation:

  • Child workflow executions are represented as first-class queue items. Parent resume/failure remains handled by the dedicated workflow-call queue lifecycle component; ChildDependencyRecord is the generic identity and aggregation seam, not a replacement for durable queue operations.
  • Called workflows currently require exactly one valid workflow_return node to be callable at all.
  • A single workflow_return_value.value may connect directly to workflow_return.values; multiple named return members should be collected and then connected to workflow_return.values.
  • Direct batch-special child workflows are now supported by expanding them into multiple child queue rows.
  • Batch outputs may feed a named workflow_return_value.value directly. Parent resume aggregates named return maps as values: dict[str, list[Any]], and all rows in one batch call must return the same key set.
  • Generator-backed batch child workflows are now supported when the batch node is fed directly by a supported integer, float, string, or image generator.
  • Connected batch child inputs produced by ordinary non-generator upstream nodes are still rejected before any child queue row is created.
  • Workflow library API responses now include compatibility metadata so the frontend can disable unsupported callees before execution rather than failing only at runtime.
  • Workflow library list compatibility uses structural generator-backed batch validation so list and picker rendering do not enumerate every image in board-backed generators; workflow detail and runtime execution still resolve real generator values.
  • Batch-specific compatibility failures, including multiple connected inputs to one batch field, are reported as unsupported_batch_input rather than generic unsupported-node failures.
  • The workflow library list also surfaces that metadata as an informational unsupported state; workflows remain viewable/editable even when they are not currently callable by call_saved_workflow.
  • Single-user workflow CRUD socket events emit only to the admin room because every single-user socket already joins that room, avoiding duplicate delivery through both user:system and admin.

8) Error Model (selected)

  • DuplicateNodeIdError, NodeAlreadyInGraphError
  • NodeNotFoundError, NodeFieldNotFoundError
  • InvalidEdgeError, CyclicalGraphError
  • NodeInputError (raised when preparing inputs for execution)

Messages favor short, precise diagnostics (node id, field, and failing condition).

9) Rationale

  • Two-graph approach isolates authoring from execution expansion and keeps validation simple.
  • Indegree + queues gives O(1) readiness decisions with clear class-ordering semantics.
  • Iterator/collector separation keeps fan-out/fan-in explicit and testable.
  • Deep-copy hydration avoids incidental aliasing bugs between nodes.