Skip to content

feat(graph)!: separate graph definitions from execution frames - #437

Draft
laipz8200 wants to merge 12 commits into
mainfrom
laipz8200/graph-definition-and-frame-graph
Draft

laipz8200 wants to merge 12 commits into
mainfrom
laipz8200/graph-definition-and-frame-graph

Conversation

@laipz8200

@laipz8200 laipz8200 commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

Important

  1. Make sure you have read our contribution guidelines
  2. Search existing issues and pull requests to confirm this change is not a duplicate
  3. Open or identify the issue this pull request resolves or advances
  4. Use a Conventional Commits title for this pull request, and mark breaking changes with !
  5. Remember that the pull request title will become the squash merge commit message
  6. If CLA Assistant prompts you, sign CLA.md in the pull request conversation

Related Issue

Refs #405

Summary

The DSL reader and the Python builder both produce an immutable Graph
containing node definitions, edges, and container call targets. The engine
selects an entry for each run and builds a fresh FrameGraph for the root and for each call
to a container body. Each frame owns its executable nodes and edge states.

flowchart LR
    DSL["DSL document"] --> Definition["Root Graph"]
    Python["graphon.graph_builder.GraphBuilder"] --> Definition
    Definition -->|run A| RootA["Root FrameGraph A"]
    Definition -->|run B| RootB["Root FrameGraph B"]
    RootA -->|container call| Child["Child FrameGraph"]
    Body["Embedded or referenced Graph"] --> Child
Loading

Host migration

Existing host code After upgrading
Graph(root_node=node) Import FrameGraph from graphon.engine and call FrameGraph(root_node=node). The nodes, edges, in_edges, out_edges, graph_config, and node_factory keywords still work.
Graph.init(...) Call FrameGraph.init(...) with the same graph_config, node_factory, root_node_id, and any container_id or skip_validation arguments.
Graph.new() or GraphBuilder imported from graphon.graph Import GraphBuilder from graphon.graph_builder and call GraphBuilder(). Drop the old graph_cls argument.
add_root(node) or add_node(node, from_node_id=..., source_handle=...) Call builder.add_node(node_id, node_type, version=..., config=..., call_target=...) for each node, including entries. It returns the local ID. Use builder.connect(previous_id, node_id, source_handle=...) for each edge the old call created.
connect(tail=..., head=...) in a builder chain Call builder.connect(source, target) for each edge, then builder.build() to get the immutable Graph. Edge IDs start at edge_0 in connection order.
An annotation or isinstance check that expects an executable graph Use FrameGraph. Read the selected root, live nodes, and edge states from engine.graph. Use engine.definition for the shared definition when one was supplied.

Import mutable Edge from graphon.engine and the host's NodeFactory from
graphon.protocols. Hosts that already own runnable nodes can pass the root
and the nodes, edges, in_edges, and out_edges maps to FrameGraph.
Run that frame with Engine(frame_graph, runtime_state).

When upgrading Dify's GraphOn dependency, replace both Graph.init calls in
workflow_app_runner.py and the calls in pipeline_runner.py and
workflow_tool_container_handler.py. Update the protocol import in
api/core/workflow/node_factory.py and migrate executable-graph annotations
and builder calls in the same change.

The same Python definition can be run with different inputs.

from graphon.dsl import load_graph
from graphon.graph_builder import GraphBuilder

builder = GraphBuilder()
start_id = builder.add_node("start", "start")
reply_id = builder.add_node("reply", "answer", config={"answer": "{{#start.name#}}"})
builder.connect(start_id, reply_id)
graph = builder.build()
engine = load_graph(graph, start_inputs={"name": "Ada"})

To specify edge IDs yourself, use Graph(nodes=..., edges=...) with
EdgeDefinition values. Use node(id), incoming(id), and outgoing(id)
to inspect the graph. Pass root_node_id when executing a graph with
multiple entries.

dsl.loads(text, ...) still loads the document's variables and plugin
dependencies. To reuse an executable document, set plan = dsl.inspect(text)
and call dsl.load_graph(plan.document.graph) for each run. Pass
runtime_variables=plan.document.runtime_variables and
dependencies=plan.dependencies when needed. With a host node factory,
use Engine(definition, runtime_state, node_factory=factory).

Flat Iteration and Loop bodies import as embedded Graphs. When building a
NodeDefinition, pass call_target=body to embed a Graph or
call_target=GraphReference("body", version="v1") to reference it.
For a reference, pass a graph_resolver that takes a GraphReference and
returns its Graph to dsl.loads, dsl.load_graph, or Engine.

Checklist

  • This pull request links the issue it resolves or advances
  • This pull request title follows Conventional Commits, and any breaking change is marked with !
  • If CLA Assistant prompted me, I signed CLA.md in the pull request conversation

laipz8200 and others added 7 commits November 26, 2026 17:48
Share one immutable Graph between the DSL and the engine. Each root
or container call gets its own FrameGraph so that choosing an entry
or traversing an edge in one execution leaves other executions alone.

Migrating host code

- Replace Graph(root_node=node) with FrameGraph(root_node=node) and
  import FrameGraph from graphon.engine. Pass existing nodes, edges,
  in_edges, out_edges, graph_config and node_factory with the same
  keyword arguments.
- Graph.init(...) and Graph.new().add_root(node).build() still work.
  Annotate their results as FrameGraph and update isinstance checks
  for executable graphs. New code can call FrameGraph.init(...) or
  FrameGraph.new(). For GraphBuilder(graph_cls=Graph), pass FrameGraph
  as graph_cls or use FrameGraph.new().
- Read the selected root, node instances and edge states from
  engine.graph. Read the reusable definition from engine.definition,
  which can be None for a host-built frame. Edge and GraphBuilder are
  available from graphon.engine, and NodeFactory from graphon.protocols.
  Their existing imports under graphon.graph still resolve.

Building and running a reusable definition

- Import Graph, NodeDefinition and EdgeDefinition from graphon.graph.
  Construct Graph(nodes=..., edges=...) with definition values. Inspect
  topology with node(id), incoming(id) and outgoing(id). To edit the
  definition, build a new Graph from the updated values. When
  entry_node_ids contains multiple entries, pass root_node_id to choose
  one for the execution.
- dsl.loads(text, ...) still builds an Engine with the document's
  variables and plugin dependencies. To reuse an executable document,
  set plan = dsl.inspect(text) and call dsl.load_graph(plan.document.graph)
  for each run. Pass runtime_variables=plan.document.runtime_variables
  and dependencies=plan.dependencies when needed. With a host factory, use
  Engine(definition, runtime_state, node_factory=factory).
- Existing flat Iteration and Loop bodies are read as embedded Graphs.
  When constructing a NodeDefinition, pass call_target=body for an
  embedded Graph or call_target=GraphReference("body", version="v1")
  for a reference. Supply graph_resolver to dsl.loads, dsl.load_graph
  or Engine for referenced bodies. It takes a GraphReference and
  returns its Graph.

BREAKING CHANGE: graphon.graph.Graph now takes definition values.
The mutable graph constructor and execution state belong to
graphon.engine.FrameGraph.
Keep the shared Graph focused on its immutable definition. Remove the
execution constructors, type forwarding modules and the import rule
that allowed graphon.graph to reach into graphon.engine.

Migrating hosts that construct executable graphs

- Import FrameGraph from graphon.engine. Replace Graph.init(...) with
  FrameGraph.init(...). Keep graph_config, node_factory, root_node_id
  and any container_id or skip_validation arguments.
- Replace Graph.new().add_root(node).build() with
  FrameGraph.new().add_root(node).build(). The builder also accepts
  the existing add_node() and connect() calls. When constructing it
  directly, use GraphBuilder(graph_cls=FrameGraph).
- Replace Graph(root_node=node) with FrameGraph(root_node=node).
  Pass any existing nodes, edges, in_edges and out_edges maps with
  the same keywords. Use FrameGraph for executable graph type
  annotations and isinstance checks.
- Use "from graphon.engine import Edge, GraphBuilder" for the former
  aliases under graphon.graph or graphon.graph.graph. Importing Edge
  from graphon.engine also replaces "from graphon.graph.edge import Edge".
- Replace NodeFactory imports from graphon.graph or graphon.graph.graph
  with "from graphon.protocols import NodeFactory".

Construct an immutable graphon.graph.Graph from NodeDefinition and
EdgeDefinition values. For DSL text, dsl.loads(text)
builds an engine with the document's variables and plugin dependencies.
For an existing definition, use dsl.load_graph(definition) or
Engine(definition, runtime_state, node_factory=factory). Supply
root_node_id when there is more than one entry. Read the selected root,
live nodes and edge states from engine.graph. Read the shared Graph
from engine.definition, which can be None for a host-built frame.

When upgrading Dify's pinned GraphOn dependency, update both Graph.init
calls in workflow_app_runner.py and the calls in pipeline_runner.py and
workflow_tool_container_handler.py. Update the protocol import in
api/core/workflow/node_factory.py and switch host builders to
FrameGraph.new().

BREAKING CHANGE: Graph.init(), Graph.new(), graphon.graph.edge and the
execution type re-exports under graphon.graph are removed.
Keep Python graph authoring alongside the definition shared by the DSL
and engine. Each build captures the node definitions, ordered edges, and
eligible entry IDs. A caller can extend the builder and reuse graphs it
has already built. The Slim example now binds the model at execution.

BREAKING CHANGE: Import GraphBuilder from graphon.graph and construct it
with GraphBuilder() or Graph.new(). Replace each runnable Node passed to
add_root or add_node with a NodeDefinition carrying its ID, type,
version, config, and optional call target. Remove graph_cls from direct
builder construction. Existing add_node(from_node_id=..., source_handle=...)
and connect(tail=..., head=...) calls keep their edge order and edge_N IDs
starting at edge_0. Use Graph(nodes=..., edges=...) for explicit edge IDs.

Pass the resulting Graph to dsl.load_graph(graph, start_inputs=...) or to
Engine(graph, runtime_state, node_factory=factory). Supply entry_types
for custom roots and root_node_id to select among multiple entries.
Code using the draft's FrameGraph.new() or graphon.engine.GraphBuilder
should use the graphon.graph authoring API above. Hosts that already own
runnable nodes can pass their root, nodes, edges, in_edges, and out_edges
to FrameGraph and run it with Engine(frame_graph, runtime_state).
Place graphon.graph_builder beside graphon.dsl. Both author the same
immutable graphon.graph.Graph. Keep the entry types, node settings and
container call targets in that shared definition. Connecting nodes
explicitly lets callers use ordinary branches and loops while building
a graph, regardless of the order in which nodes are registered.

BREAKING CHANGE: Remove Graph.new() and the GraphBuilder exports from
graphon.graph and graphon.graph.graph. Import GraphBuilder from
graphon.graph_builder and create a builder with GraphBuilder(). If
complete NodeDefinition and EdgeDefinition values are already available,
pass them directly to Graph(nodes=..., edges=...).

For NodeDefinition inputs, replace add_root(node) and add_node(node) with
builder.add_node(node.id, node.node_type, version=node.version,
config=node.config, call_target=node.call_target). Each call returns
the local ID. Register entry nodes this same way. Pass entry_types to
GraphBuilder for host-defined entry types and versions.

Call builder.connect(source, target, source_handle=...) for every former
implicit successor, from_node_id connection or connect(tail=..., head=...)
call. Add edges in their original order to preserve outgoing order and
generated IDs (edge_0, edge_1, ...). Finish with graph = builder.build()
and pass that reusable definition to load_graph or Engine. Hosts with
prebuilt executable nodes can pass their node, edge and adjacency maps
and selected root directly to FrameGraph for execution.
Pass the host node factory when constructing an Engine from a shared
Graph and select the entry for graphs with multiple roots. Show how to
queue an abort, read its terminal event and give each execution a
unique Redis command channel key.

The other four guides from the earlier documentation review are already
in main through #441, including the revised Python support range.
@laipz8200
laipz8200 force-pushed the laipz8200/graph-definition-and-frame-graph branch from 6c4eabd to ae110b9 Compare November 26, 2026 09:53
autofix-ci Bot and others added 5 commits November 26, 2026 09:53
Bind root and child node factories to a live read-only view. Reading a
value, variable or file returns detached data. Variable Assigner v2
stages its dependent assignments in a private pool before yielding the
updates. Copy variable writes, output values and container requests
before resuming the node so later edits cannot change a pending commit.

BREAKING CHANGE: NodeFactory.with_runtime_state receives a read-only
frame view. Pass it to node constructors. Tool adapters that receive a
pool should accept ReadOnlyVariablePool. Read abort status through
node.runtime_state.aborted. Yield VariableUpdatedEvent for a shared
assignment or return a node output. Use variable_pool.mutable_copy()
for local calculations that need several dependent writes. A host can
still seed a RuntimeState and pass it to a directly constructed node.
Use a two-string VariableAddress for pool writes and removals. Existing
two-part lists and tuples remain valid. Keep field traversal on reads
and reject malformed addresses before changing the pool. Have Loop
clear just the node scopes it owns when it starts the next round.

BREAKING CHANGE: Replace pool.remove([scope]) with
pool.clear_scope(scope). Use pool.remove((scope, name)) to delete one
variable. A field path such as (scope, name, field) still works with
get(), and now raises ValueError on removal. An empty selector and a
bare string also raise ValueError on writes and removals.
Keep VariableEntity available for saved Start configurations and its
existing public import. Mark it final and deprecated for type checkers
and generated schemas without warning on construction. Limit exceptions
to its existing compatibility uses.

Pin the form fields, their types and declared defaults, required fields,
and input kinds in a contract test. Track host ownership in TD-11 and
direct new input controls and form validation to the host.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant