Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 66 additions & 29 deletions ARCHITECTURE.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!-- knowledge
last_checked: "2026-11-26T01:12:45Z"
last_checked: "2026-11-27T06:38:54Z"
-->
# Architecture

Expand All @@ -9,31 +9,50 @@ Contributor commands and contribution rules live in [CONTRIBUTING.md](CONTRIBUTI

## Public entry points

Graphon is an embeddable Python workflow engine. The host application supplies
workflow configuration, inputs, integration adapters, and any persistence.
The top-level `graphon` package does not re-export its primary classes; import
them from the packages below.
Graphon is an embeddable Python engine for executing Graphs. The host supplies
the Graph definition, inputs, integration adapters, and any persistence. Import
the primary classes from the packages below.

| Surface | Entry point | Evidence |
| --- | --- | --- |
| YAML import and inspection | `graphon.dsl.inspect`, `graphon.dsl.loads` | [Importer](src/graphon/dsl/importer.py), [DSL tests](tests/dsl/) |
| Graph construction | `graphon.graph.Graph`, `GraphBuilder`, `NodeFactory` | [Graph implementation](src/graphon/graph/graph.py), [graph tests](tests/graph/test_graph.py) |
| Execution | `graphon.engine.Engine` | [Engine](src/graphon/engine/engine.py), [workflow tests](tests/workflows/test_full_engine_events.py) |
| Python graph building | `graphon.graph_builder.GraphBuilder` | [Builder](src/graphon/graph_builder/builder.py), [builder tests](tests/graph_builder/test_graph_builder.py) |
| Shared graph definition | `graphon.graph.Graph`, `NodeDefinition`, `EdgeDefinition`, `GraphReference` | [Graph](src/graphon/graph/graph.py), [definition and execution tests](tests/graph/test_graph_definition.py) |
| Execution | `graphon.dsl.load_graph`, `graphon.engine.Engine` | [Importer](src/graphon/dsl/importer.py), [engine](src/graphon/engine/engine.py) |
| Host-supplied nodes | `graphon.engine.FrameGraph`, `graphon.protocols.NodeFactory` | [Frame graph](src/graphon/engine/frame_graph.py), [frame construction tests](tests/engine/test_frame_graph_validation.py) |
| Execution setup and state | `graphon.runtime.RuntimeState`, `VariablePool`, `InitParams` | [Runtime exports](src/graphon/runtime/__init__.py), [runtime tests](tests/runtime/) |
| Integration contracts | `graphon.protocols` | [Protocol exports](src/graphon/protocols/__init__.py), [export tests](tests/test_protocols_exports.py) |

The [Python example](examples/slim_llm/code.py) constructs nodes and an engine
directly. The [DSL example](examples/slim_llm/dsl.py) uses `loads()`, which builds
the variable pool, runtime state, node factory, graph, and engine. Built-in node
availability and default DSL support are separate concerns: inspect the
[DSL factory](src/graphon/dsl/node_factory.py) for its supported node types.
For an executable DSL document, `inspect(text).document.graph` exposes its
reusable definition. The reader groups nodes from flat Dify workflows into a
root Graph and its embedded bodies. `loads(text, ...)` creates an engine with
the document's variables and plugin dependencies. See the [DSL example](examples/slim_llm/dsl.py).

In Python, create a `GraphBuilder` and register each node with
`add_node(node_id, node_type)`. Pass the returned IDs to `connect(source, target)`
for each directed edge. `build()` returns an immutable `Graph`.
Pass a definition from either source to `load_graph(graph, ...)` to create a
fresh engine with the built-in node factory.
See the [Python example](examples/slim_llm/code.py) and [supported node types](src/graphon/dsl/node_factory.py).

With a host factory, `Engine(graph, runtime_state, node_factory=...)` creates
the root frame's executable nodes. A host that already owns those nodes can
pass their node, edge, and adjacency maps and the selected root to `FrameGraph`,
then call `Engine(frame_graph, runtime_state)`. For raw node and edge
configuration, use `FrameGraph.init(graph_config=..., node_factory=..., root_node_id=...)`.

## Import boundaries

The [Import Linter configuration](pyproject.toml) is the source of truth for
allowed package dependencies. [Layer regression tests](tests/test_import_layers.py)
exercise enforcement in CI.

The DSL reader and Python builder are independent packages at the same layer.
Both use `graphon.graph`. Hosts can construct a `Graph` with `GraphBuilder`
before loading the engine or executable node classes. `graphon.engine` exports
`FrameGraph` and its mutable `Edge` type. See the
[frame graph import tests](tests/engine/test_frame_graph_imports.py).

The public runtime package can restore snapshots without importing the engine.
Importing `graphon.protocols` does not register built-in nodes; importing a concrete
node implementation registers its type and version. These guarantees are enforced by
Expand All @@ -45,8 +64,10 @@ runtime paths for new integrations; engine queue aliases exist for compatibility

```mermaid
flowchart LR
Config[Graph config and NodeFactory] --> Graph[Scoped Graph]
Graph --> Engine[Engine and root frame]
DSL["YAML · graphon.dsl"] --> Graph["graphon.graph.Graph"]
Python["Python · graphon.graph_builder"] --> Graph
Graph --> Engine[Engine and root FrameGraph]
Host[NodeFactory and runtime state] --> Engine
Engine --> Ready[Shared ready queue]
Ready --> Workers[Worker pool]
Workers --> Dispatch[Dispatch queue]
Expand All @@ -58,9 +79,12 @@ flowchart LR
Stream --> Consumer[Engine.run consumer]
```

1. [Graph.init](src/graphon/graph/graph.py) turns input configuration into a
scoped executable graph. `Graph.new()` supplies the builder for directly
instantiated nodes; see the [Python example](examples/slim_llm/code.py).
1. For a shared definition, the engine selects the sole entry automatically.
Supply `root_node_id` when there are several. The engine binds fresh node
instances and edges to the root frame. Each container call gets a child frame
for its body. For referenced bodies, the host supplies a `graph_resolver`
that maps a reference to a Graph. See [frame graph factory](src/graphon/engine/frame_graph_factory.py) and
[child frames](src/graphon/engine/frame.py).
2. [Engine](src/graphon/engine/engine.py) binds a root frame, command processor,
event stream, fixed worker pool, and dispatcher. Calling `run()` starts or
resumes execution and yields graph and frame lifecycle events, committed
Expand Down Expand Up @@ -139,10 +163,13 @@ Keep frame events enabled so the filter receives its initial root baseline. The
filter consumes baselines and committed replacements without forwarding them.
Its detached view stays usable after the engine removes the frame.

Root templates come from the graph's response nodes. Child Answer and End
templates come from [retained graph definitions](src/graphon/engine/filter/builtin/response_stream/frame_graph.py)
without reconstructing nodes. Frames without sufficient definitions have no
response session. Response eligibility follows [reachability](src/graphon/engine/filter/builtin/response_stream/reachability.py)
Root templates come from the root graph's response nodes. For child Answer
and End templates, the filter reads the called Graph in its parent's frame or
the retained configuration for a directly built frame. Bodies can use the same
node ID for different replies. See [child templates](src/graphon/engine/filter/builtin/response_stream/frame_graph.py)
and [response tests for called graphs](tests/graph/test_graph_definition.py).
Frames without sufficient definitions have no response session.
Response eligibility follows [reachability](src/graphon/engine/filter/builtin/response_stream/reachability.py)
through nonblocking edges and consumed traversal events. A blocking edge opens
only if its first consumed decision is a take. The engine may be ahead of the
consumer, so activation uses the events already consumed in that frame. See
Expand Down Expand Up @@ -195,14 +222,24 @@ for that extension.

## State and execution invariants

Execution state is owned by frames; shared queues and workflow execution connect
those frames without requiring nested worker pools. File adapters are host
resources owned by the engine, while snapshots contain portable state. These
boundaries let the host rebuild an execution with its own integrations.
All frames in one run share a [GraphExecution](src/graphon/runtime/execution.py)
and the engine's queues and worker pool. Each frame owns its executable nodes
and edges. `Engine.graph` exposes the root frame's `FrameGraph`.
`Engine.definition` exposes the shared `Graph` when the host supplied one.
The engine keeps host file adapters while snapshots carry portable state.
See the [domain model](docs/domain-model.md#definition-and-execution) for the
boundary between a graph and a run.

[Variable pools](src/graphon/runtime/variable_pool.py) preserve node ownership through copies and serialization for both configured graphs and `Graph.new()` graphs.
[Variable pools](src/graphon/runtime/variable_pool.py) preserve node ownership through copies and serialization for both configured graphs and frames built from host-supplied nodes.
Iteration writes host variables already present in the parent pool back to that parent, while keeping inherited node variables private to each round.
Independent workflows with fresh pools have separate ownership even when ancestor or sibling graphs reuse the same node IDs.
Each Graph execution with a fresh pool tracks its own variables, even when ancestor or sibling Graphs reuse node IDs.

A stored variable has a two-string address `(scope, name)`. `VariablePool.add()`
and `remove()` require this pair. Reads can follow a longer path to an object
field or a file attribute, as in `pool.get(("start", "profile", "name"))`.
`pool.clear_scope("start")` removes every variable in that scope and preserves
its node ownership. See the [address definition](src/graphon/variables/address.py)
and [variable pool tests](tests/runtime/test_variable_pool.py).

The executable contracts below are authoritative for validation, lifecycle, and
compatibility details:
Expand All @@ -211,7 +248,7 @@ compatibility details:
| --- | --- | --- |
| Frame state and queue ownership | [Frame](src/graphon/engine/frame.py), [runtime state](src/graphon/runtime/runtime_state/state.py) | [Dispatch](tests/engine/test_dispatch_patterns.py) |
| File adapter binding and isolation | [File runtime](src/graphon/file/runtime.py), [engine](src/graphon/engine/engine.py) | [Execution file isolation](tests/engine/test_file_runtime.py), [file scopes](tests/file/test_runtime.py) |
| Container ownership and factory preflight | [Graph and NodeFactory](src/graphon/graph/graph.py), [scoping](src/graphon/graph/scoping.py), [validation](src/graphon/graph/validation.py) | [Graph scoping and validation](tests/graph/test_graph_scoping.py) |
| Container ownership and factory preflight | [Frame graph factory](src/graphon/engine/frame_graph_factory.py), [node factory contract](src/graphon/nodes/factory.py), [scoping](src/graphon/graph/scoping.py), [validation](src/graphon/graph/validation.py) | [Frame scoping and preflight](tests/engine/test_frame_graph_scoping.py) |
| Branch joins and readiness | [Scheduler](src/graphon/engine/scheduler.py) | [Scheduling](tests/engine/test_scheduler.py) |
| Container suspension and variable propagation | [Container effects](src/graphon/nodes/container_effects.py), [handlers](src/graphon/engine/container_handler/builtin/) | [Container values](tests/nodes/test_container_effects.py), [cooperative execution](tests/engine/test_cooperative_container_execution.py), [container variable scoping](tests/workflows/test_full_engine_events.py), [Iteration variable ownership](tests/engine/test_iteration_variable_scopes.py) |
| Pause, resume, and snapshot quiescence | [Dispatcher](src/graphon/engine/dispatcher.py), [execution guard](src/graphon/runtime/execution.py) | [Serialization](tests/engine/test_runtime_state_serialization.py) |
Expand Down Expand Up @@ -266,7 +303,7 @@ snapshot guard tracks engine activity, so it cannot protect arbitrary host write

| Change | Start here | Existing check |
| --- | --- | --- |
| Add a node | [Node base class](src/graphon/nodes/base/node.py), [NodeFactory](src/graphon/graph/graph.py); import the subclass to register its type/version | [Node execution binding](tests/nodes/base/test_node_execution_binding.py), [factory tests](tests/dsl/test_node_factory.py) |
| Add a node | Import the subclass to register its type/version. See the [node base class](src/graphon/nodes/base/node.py) and [NodeFactory](src/graphon/nodes/factory.py) | [Node execution binding](tests/nodes/base/test_node_execution_binding.py), [factory tests](tests/dsl/test_node_factory.py) |
| Add lifecycle observation or limits | [Layer API and usage](src/graphon/engine/layer/README.md); runtime access is read-only, controls use commands | [Layer context tests](tests/engine/test_layer_node_run_context.py) |
| Pause, abort, or update variables externally | [Command API and channels](src/graphon/engine/command/README.md) | [Dispatch tests](tests/engine/test_dispatch_patterns.py) |
| Adapt the consumer event stream | [Filter protocol](src/graphon/engine/filter/protocol.py) | [Filter tests](tests/engine/test_event_filters.py) |
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!-- knowledge
last_checked: "2026-11-26T01:12:45Z"
last_checked: "2026-11-26T03:21:10Z"
-->
# Graphon

Expand All @@ -12,7 +12,7 @@ protocols, and a runnable end-to-end example.
## Highlights

- Queue-based `Engine` orchestration with event-driven execution
- Graph parsing, validation, and fluent graph building
- Graph definitions from YAML or Python, with node and edge validation
- Frame-owned runtime state and variables with shared workflow execution state
- Built-in node implementations for common workflow patterns
- DSL import support with Slim-backed LLM nodes
Expand Down
9 changes: 5 additions & 4 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
<!-- knowledge
last_checked: "2026-11-26T01:12:45Z"
last_checked: "2026-11-27T06:43:00Z"
-->
# Repository knowledge

Start with the question you need to answer, then follow the links to code and
tests. These documents describe this checkout; they do not substitute for the
implementation or establish new public contracts.
Start with the question you need to answer. The domain model describes the
intended design. Follow the links to code and tests for current behavior and
public contracts.

## Map

| Question | Start here |
| --- | --- |
| What is Graphon and how do I run it? | [Project README](../README.md), [Slim example](../examples/slim_llm/README.md) |
| What do the five subdomains own, and where do the DSL and engine share a model? | [Graph domain model](domain-model.md) |
| Where does execution happen, and who owns state? | [Architecture](../ARCHITECTURE.md) |
| How should runtime dependencies and mutable state be owned? | [Explicit state ownership](../CONTRIBUTING.md#explicit-state-ownership) |
| Which files and tests should I change? | [Development guide](development.md) |
Expand Down
Loading
Loading