weavegraph 0.3.0

Graph-driven, concurrent agent workflow framework with versioned state, deterministic barrier merges, and rich diagnostics.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
//! GraphBuilder implementation for constructing workflow graphs.
//!
//! This module contains the main GraphBuilder type and its fluent API
//! for constructing workflow graphs with nodes, edges, and configuration.

use rustc_hash::FxHashMap;
use std::sync::Arc;

use super::edges::{ConditionalEdge, EdgePredicate};
use crate::node::Node;
use crate::reducers::{Reducer, ReducerRegistry};
use crate::runtimes::{EventBusConfig, RuntimeConfig};
use crate::types::{ChannelType, NodeKind};

/// Type alias for the internal parts of a GraphBuilder.
/// Used to reduce type complexity in the `into_parts()` method.
type GraphParts = (
    FxHashMap<NodeKind, Arc<dyn Node>>,
    FxHashMap<NodeKind, Vec<NodeKind>>,
    Vec<ConditionalEdge>,
    RuntimeConfig,
    ReducerRegistry,
);

/// Builder for constructing workflow graphs with fluent API.
///
/// `GraphBuilder` provides a builder pattern for constructing workflow graphs
/// by adding nodes, edges, and configuration before compiling to an executable
/// [`App`](crate::app::App). The builder ensures type safety and provides clear error messages
/// for common configuration mistakes.
///
/// # Required Configuration
///
/// Every graph must have:
/// - At least one executable node added via [`GraphBuilder::add_node`](Self::add_node)
/// - Edges connecting from `NodeKind::Start` to define entry points
/// - Edges connecting to `NodeKind::End` to define exit points
///
/// Note: `NodeKind::Start` and `NodeKind::End` are virtual endpoints and should
/// never be registered with `add_node`. They exist only for structural definition.
///
/// # Examples
///
/// ## Basic Usage
/// ```
/// use weavegraph::graphs::GraphBuilder;
/// use weavegraph::types::NodeKind;
///
/// # struct MyNode;
/// # #[async_trait::async_trait]
/// # impl weavegraph::node::Node for MyNode {
/// #     async fn run(&self, _: weavegraph::state::StateSnapshot, _: weavegraph::node::NodeContext) -> Result<weavegraph::node::NodePartial, weavegraph::node::NodeError> {
/// #         Ok(weavegraph::node::NodePartial::default())
/// #     }
/// # }
///
/// // Linear workflow: Start -> worker -> End
/// let app = GraphBuilder::new()
///     .add_node(NodeKind::Custom("worker".into()), MyNode)
///     .add_edge(NodeKind::Start, NodeKind::Custom("worker".into()))
///     .add_edge(NodeKind::Custom("worker".into()), NodeKind::End)
///     .compile();
/// ```
///
/// ## Conditional Routing
/// ```
/// use weavegraph::graphs::{GraphBuilder, EdgePredicate};
/// use weavegraph::types::NodeKind;
/// use std::sync::Arc;
///
/// # struct MyNode;
/// # #[async_trait::async_trait]
/// # impl weavegraph::node::Node for MyNode {
/// #     async fn run(&self, _: weavegraph::state::StateSnapshot, _: weavegraph::node::NodeContext) -> Result<weavegraph::node::NodePartial, weavegraph::node::NodeError> {
/// #         Ok(weavegraph::node::NodePartial::default())
/// #     }
/// # }
///
/// let route_by_count: EdgePredicate = Arc::new(|snapshot| {
///     if snapshot.messages.len() > 5 {
///         vec!["heavy_processing".to_string()]
///     } else {
///         vec!["light_processing".to_string()]
///     }
/// });
///
/// let app = GraphBuilder::new()
///     .add_node(NodeKind::Custom("heavy_processing".into()), MyNode)
///     .add_node(NodeKind::Custom("light_processing".into()), MyNode)
///     .add_conditional_edge(NodeKind::Start, route_by_count)
///     .add_edge(NodeKind::Custom("heavy_processing".into()), NodeKind::End)
///     .add_edge(NodeKind::Custom("light_processing".into()), NodeKind::End)
///     .compile();
/// ```
pub struct GraphBuilder {
    /// Registry of all nodes in the graph, keyed by their identifier.
    nodes: FxHashMap<NodeKind, Arc<dyn Node>>,
    /// Unconditional edges defining static graph topology.
    edges: FxHashMap<NodeKind, Vec<NodeKind>>,
    /// Conditional edges for dynamic routing based on state.
    conditional_edges: Vec<ConditionalEdge>,
    /// Runtime configuration for the compiled application.
    runtime_config: RuntimeConfig,
    /// Reducer registry for channel update operations.
    reducer_registry: ReducerRegistry,
}

impl Default for GraphBuilder {
    fn default() -> Self {
        Self::new()
    }
}

impl GraphBuilder {
    /// Creates a new, empty graph builder.
    ///
    /// The builder starts with no nodes, edges, or configuration.
    /// Use the fluent API methods to add components before calling
    /// [`compile`](Self::compile).
    ///
    /// # Examples
    ///
    /// ```
    /// use weavegraph::graphs::GraphBuilder;
    ///
    /// let builder = GraphBuilder::new();
    /// // Add nodes, edges, and configuration...
    /// ```
    #[must_use]
    pub fn new() -> Self {
        Self {
            nodes: FxHashMap::default(),
            edges: FxHashMap::default(),
            conditional_edges: Vec::new(),
            runtime_config: RuntimeConfig::default(),
            reducer_registry: ReducerRegistry::default(),
        }
    }

    /// Adds a conditional edge to the graph.
    ///
    /// Conditional edges enable dynamic routing based on the current state.
    /// When execution reaches the `from` node, the `predicate` function is
    /// evaluated with the current [`StateSnapshot`](crate::state::StateSnapshot) and returns the target
    /// node names for routing.
    ///
    /// # Parameters
    ///
    /// - `from`: The source node for the conditional edge
    /// - `predicate`: Function that determines target nodes based on state
    #[must_use]
    pub fn add_conditional_edge(mut self, from: NodeKind, predicate: EdgePredicate) -> Self {
        self.conditional_edges
            .push(ConditionalEdge::new(from, predicate));
        self
    }

    /// Adds a node to the graph.
    ///
    /// NOTE: `NodeKind::Start` and `NodeKind::End` are virtual structural endpoints.
    /// If either is passed to `add_node`, the registration is ignored and a warning
    /// is emitted. They are not stored in the node registry and are never executed;
    /// the scheduler skips them automatically while still allowing edges from
    /// `Start` and to `End` for topology.
    ///
    /// Registers a node implementation with the given identifier. Each node
    /// must have a unique [`NodeKind`] identifier within the graph. The node
    /// implementation must implement the [`Node`] trait.
    ///
    /// # Parameters
    ///
    /// - `id`: Unique identifier for this node in the graph
    /// - `node`: Implementation of the [`Node`] trait
    #[must_use]
    pub fn add_node(mut self, id: NodeKind, node: impl Node + 'static) -> Self {
        // Ignore attempts to register virtual Start/End node kinds; emit a warning.
        match id {
            NodeKind::Start | NodeKind::End => {
                tracing::warn!(
                    ?id,
                    "Ignoring registration of virtual node kind (Start/End are virtual)"
                );
                // Do not insert into registry.
            }
            _ => {
                self.nodes.insert(id, Arc::new(node));
            }
        }
        self
    }

    /// Adds an unconditional edge between two nodes.
    ///
    /// Creates a direct connection from one node to another. When the `from`
    /// node completes execution, the scheduler will consider the `to` node
    /// for execution in the next step. Multiple edges from the same node
    /// create fan-out patterns, while multiple edges to the same node
    /// create fan-in patterns.
    ///
    /// # Parameters
    ///
    /// - `from`: Source node identifier
    /// - `to`: Target node identifier
    #[must_use]
    pub fn add_edge(mut self, from: NodeKind, to: NodeKind) -> Self {
        self.edges.entry(from).or_default().push(to);
        self
    }

    /// Configures runtime settings for the compiled application.
    ///
    /// Runtime configuration controls execution behavior such as concurrency
    /// limits, checkpointing, and session management. If not specified,
    /// default configuration is used.
    ///
    /// # Parameters
    ///
    /// - `runtime_config`: Configuration for the compiled application
    #[must_use]
    pub fn with_runtime_config(mut self, runtime_config: RuntimeConfig) -> Self {
        self.runtime_config = runtime_config;
        self
    }

    /// Overrides only the event bus configuration while keeping other runtime settings.
    #[must_use]
    pub fn with_event_bus_config(mut self, config: EventBusConfig) -> Self {
        let mut runtime_config = self.runtime_config.clone();
        runtime_config.event_bus = config;
        self.runtime_config = runtime_config;
        self
    }

    /// Registers a custom reducer for a specific channel.
    ///
    /// This method enables registration of custom reducers to extend or replace
    /// the default reducer behavior for a channel. Multiple reducers can be
    /// registered for the same channel and will be applied in registration order.
    ///
    /// # Parameters
    ///
    /// - `channel`: The channel type to register the reducer for
    /// - `reducer`: The reducer implementation wrapped in Arc
    ///
    /// # Examples
    ///
    /// ```
    /// use std::sync::Arc;
    /// use weavegraph::graphs::GraphBuilder;
    /// use weavegraph::reducers::{Reducer, AddMessages};
    /// use weavegraph::types::{ChannelType, NodeKind};
    ///
    /// # struct MyNode;
    /// # #[async_trait::async_trait]
    /// # impl weavegraph::node::Node for MyNode {
    /// #     async fn run(&self, _: weavegraph::state::StateSnapshot, _: weavegraph::node::NodeContext) -> Result<weavegraph::node::NodePartial, weavegraph::node::NodeError> {
    /// #         Ok(weavegraph::node::NodePartial::default())
    /// #     }
    /// # }
    ///
    /// let app = GraphBuilder::new()
    ///     .add_node(NodeKind::Custom("worker".into()), MyNode)
    ///     .with_reducer(ChannelType::Message, Arc::new(AddMessages))
    ///     .add_edge(NodeKind::Start, NodeKind::Custom("worker".into()))
    ///     .add_edge(NodeKind::Custom("worker".into()), NodeKind::End)
    ///     .compile();
    /// ```
    #[must_use]
    pub fn with_reducer(mut self, channel: ChannelType, reducer: Arc<dyn Reducer>) -> Self {
        self.reducer_registry.register(channel, reducer);
        self
    }

    /// Replaces the entire reducer registry with a custom one.
    ///
    /// This method allows complete control over reducer configuration by
    /// replacing the default registry. Useful when you need fine-grained
    /// control over reducer ordering or want to start with an empty registry.
    ///
    /// # Parameters
    ///
    /// - `registry`: The reducer registry to use
    ///
    /// # Examples
    ///
    /// ```
    /// use std::sync::Arc;
    /// use weavegraph::graphs::GraphBuilder;
    /// use weavegraph::reducers::{ReducerRegistry, AddMessages};
    /// use weavegraph::types::{ChannelType, NodeKind};
    ///
    /// # struct MyNode;
    /// # #[async_trait::async_trait]
    /// # impl weavegraph::node::Node for MyNode {
    /// #     async fn run(&self, _: weavegraph::state::StateSnapshot, _: weavegraph::node::NodeContext) -> Result<weavegraph::node::NodePartial, weavegraph::node::NodeError> {
    /// #         Ok(weavegraph::node::NodePartial::default())
    /// #     }
    /// # }
    ///
    /// let custom_registry = ReducerRegistry::new()
    ///     .with_reducer(ChannelType::Message, Arc::new(AddMessages));
    ///
    /// let app = GraphBuilder::new()
    ///     .add_node(NodeKind::Custom("worker".into()), MyNode)
    ///     .with_reducer_registry(custom_registry)
    ///     .add_edge(NodeKind::Start, NodeKind::Custom("worker".into()))
    ///     .add_edge(NodeKind::Custom("worker".into()), NodeKind::End)
    ///     .compile();
    /// ```
    #[must_use]
    pub fn with_reducer_registry(mut self, registry: ReducerRegistry) -> Self {
        self.reducer_registry = registry;
        self
    }

    // =========================================================================
    // Iterators (petgraph-style API)
    // =========================================================================

    /// Returns an iterator over all registered nodes in the graph.
    ///
    /// This iterates over custom nodes only; virtual `Start` and `End` nodes
    /// are not included as they are not stored in the registry.
    ///
    /// # Examples
    ///
    /// ```
    /// use weavegraph::graphs::GraphBuilder;
    /// use weavegraph::types::NodeKind;
    ///
    /// # struct MyNode;
    /// # #[async_trait::async_trait]
    /// # impl weavegraph::node::Node for MyNode {
    /// #     async fn run(&self, _: weavegraph::state::StateSnapshot, _: weavegraph::node::NodeContext) -> Result<weavegraph::node::NodePartial, weavegraph::node::NodeError> {
    /// #         Ok(weavegraph::node::NodePartial::default())
    /// #     }
    /// # }
    ///
    /// let builder = GraphBuilder::new()
    ///     .add_node(NodeKind::Custom("A".into()), MyNode)
    ///     .add_node(NodeKind::Custom("B".into()), MyNode)
    ///     .add_edge(NodeKind::Start, NodeKind::Custom("A".into()))
    ///     .add_edge(NodeKind::Custom("A".into()), NodeKind::Custom("B".into()))
    ///     .add_edge(NodeKind::Custom("B".into()), NodeKind::End);
    ///
    /// let node_count = builder.nodes().count();
    /// assert_eq!(node_count, 2);
    /// ```
    pub fn nodes(&self) -> super::iteration::NodesIter<'_> {
        super::iteration::NodesIter::new(self.nodes.keys())
    }

    /// Returns an iterator over all edges in the graph as (source, target) pairs.
    ///
    /// Includes edges from/to virtual `Start` and `End` nodes.
    /// The iteration order is not deterministic due to hash map iteration;
    /// use [`topological_sort`](Self::topological_sort) for ordered traversal.
    ///
    /// # Examples
    ///
    /// ```
    /// use weavegraph::graphs::GraphBuilder;
    /// use weavegraph::types::NodeKind;
    ///
    /// # struct MyNode;
    /// # #[async_trait::async_trait]
    /// # impl weavegraph::node::Node for MyNode {
    /// #     async fn run(&self, _: weavegraph::state::StateSnapshot, _: weavegraph::node::NodeContext) -> Result<weavegraph::node::NodePartial, weavegraph::node::NodeError> {
    /// #         Ok(weavegraph::node::NodePartial::default())
    /// #     }
    /// # }
    ///
    /// let builder = GraphBuilder::new()
    ///     .add_node(NodeKind::Custom("A".into()), MyNode)
    ///     .add_edge(NodeKind::Start, NodeKind::Custom("A".into()))
    ///     .add_edge(NodeKind::Custom("A".into()), NodeKind::End);
    ///
    /// let edge_count = builder.edges().count();
    /// assert_eq!(edge_count, 2);
    /// ```
    pub fn edges(&self) -> super::iteration::EdgesIter<'_> {
        super::iteration::EdgesIter::new(&self.edges)
    }

    /// Returns the number of registered nodes in the graph.
    ///
    /// Does not include virtual `Start` and `End` nodes.
    #[must_use]
    pub fn node_count(&self) -> usize {
        self.nodes.len()
    }

    /// Returns the number of edges in the graph.
    ///
    /// Counts all edges including those from/to virtual nodes.
    #[must_use]
    pub fn edge_count(&self) -> usize {
        self.edges.values().map(|v| v.len()).sum()
    }

    // =========================================================================
    // Graph Algorithms
    // =========================================================================

    /// Returns a topologically sorted list of all nodes in the graph.
    ///
    /// The result includes virtual `Start` (always first) and `End` (always last)
    /// nodes along with all custom nodes. Nodes at the same topological level
    /// are sorted lexicographically for deterministic ordering.
    ///
    /// This is useful for:
    /// - Deterministic iteration over nodes
    /// - Dependency analysis
    /// - Visualization and debugging
    ///
    /// # Note
    ///
    /// This method assumes the graph is acyclic. If the graph contains cycles,
    /// the result will exclude nodes involved in cycles. Use [`compile`](Self::compile)
    /// to validate the graph before relying on topological sort.
    ///
    /// # Examples
    ///
    /// ```
    /// use weavegraph::graphs::GraphBuilder;
    /// use weavegraph::types::NodeKind;
    ///
    /// # struct MyNode;
    /// # #[async_trait::async_trait]
    /// # impl weavegraph::node::Node for MyNode {
    /// #     async fn run(&self, _: weavegraph::state::StateSnapshot, _: weavegraph::node::NodeContext) -> Result<weavegraph::node::NodePartial, weavegraph::node::NodeError> {
    /// #         Ok(weavegraph::node::NodePartial::default())
    /// #     }
    /// # }
    ///
    /// let builder = GraphBuilder::new()
    ///     .add_node(NodeKind::Custom("A".into()), MyNode)
    ///     .add_node(NodeKind::Custom("B".into()), MyNode)
    ///     .add_edge(NodeKind::Start, NodeKind::Custom("A".into()))
    ///     .add_edge(NodeKind::Custom("A".into()), NodeKind::Custom("B".into()))
    ///     .add_edge(NodeKind::Custom("B".into()), NodeKind::End);
    ///
    /// let sorted = builder.topological_sort();
    /// assert_eq!(sorted[0], NodeKind::Start);
    /// assert_eq!(sorted[sorted.len() - 1], NodeKind::End);
    ///
    /// // A comes before B due to edge A -> B
    /// let a_pos = sorted.iter().position(|n| n == &NodeKind::Custom("A".into())).unwrap();
    /// let b_pos = sorted.iter().position(|n| n == &NodeKind::Custom("B".into())).unwrap();
    /// assert!(a_pos < b_pos);
    /// ```
    #[must_use]
    pub fn topological_sort(&self) -> Vec<crate::types::NodeKind> {
        super::iteration::topological_sort(&self.edges)
    }

    // =========================================================================
    // petgraph Compatibility (feature-gated)
    // =========================================================================

    /// Converts the graph to a petgraph `DiGraph` for advanced algorithms.
    ///
    /// This is useful for:
    /// - Advanced graph algorithms (shortest path, max flow, etc.)
    /// - Graph analysis and metrics
    /// - Integration with petgraph ecosystem tools
    ///
    /// # Feature Gate
    ///
    /// This method requires the `petgraph-compat` feature:
    /// ```toml
    /// weavegraph = { features = ["petgraph-compat"] }
    /// ```
    ///
    /// # Examples
    ///
    /// ```ignore
    /// use weavegraph::graphs::GraphBuilder;
    /// use petgraph::algo::is_cyclic_directed;
    ///
    /// let builder = GraphBuilder::new()
    ///     .add_node(NodeKind::Custom("A".into()), MyNode)
    ///     .add_edge(NodeKind::Start, NodeKind::Custom("A".into()))
    ///     .add_edge(NodeKind::Custom("A".into()), NodeKind::End);
    ///
    /// let pg = builder.to_petgraph();
    /// assert!(!is_cyclic_directed(&pg.graph));
    /// ```
    #[cfg(feature = "petgraph-compat")]
    #[cfg_attr(docsrs, doc(cfg(feature = "petgraph-compat")))]
    #[must_use]
    pub fn to_petgraph(&self) -> super::petgraph_compat::PetgraphConversion {
        super::petgraph_compat::to_petgraph(&self.edges)
    }

    /// Exports the graph to DOT format for visualization.
    ///
    /// The output can be rendered using Graphviz (`dot -Tpng graph.dot -o graph.png`)
    /// or online tools like <https://dreampuf.github.io/GraphvizOnline/>.
    ///
    /// # Feature Gate
    ///
    /// This method requires the `petgraph-compat` feature:
    /// ```toml
    /// weavegraph = { features = ["petgraph-compat"] }
    /// ```
    ///
    /// # Examples
    ///
    /// ```ignore
    /// use weavegraph::graphs::GraphBuilder;
    /// use std::fs;
    ///
    /// let builder = GraphBuilder::new()
    ///     .add_node(NodeKind::Custom("A".into()), MyNode)
    ///     .add_edge(NodeKind::Start, NodeKind::Custom("A".into()))
    ///     .add_edge(NodeKind::Custom("A".into()), NodeKind::End);
    ///
    /// let dot = builder.to_dot();
    /// fs::write("workflow.dot", &dot)?;
    /// // Then run: dot -Tpng workflow.dot -o workflow.png
    /// ```
    #[cfg(feature = "petgraph-compat")]
    #[cfg_attr(docsrs, doc(cfg(feature = "petgraph-compat")))]
    #[must_use]
    pub fn to_dot(&self) -> String {
        super::petgraph_compat::to_dot(&self.edges)
    }

    /// Checks if the graph contains cycles using petgraph's algorithm.
    ///
    /// This provides an alternative to the built-in cycle detection for
    /// cross-verification or when you need petgraph's specific behavior.
    ///
    /// # Feature Gate
    ///
    /// This method requires the `petgraph-compat` feature.
    #[cfg(feature = "petgraph-compat")]
    #[cfg_attr(docsrs, doc(cfg(feature = "petgraph-compat")))]
    #[must_use]
    pub fn is_cyclic_petgraph(&self) -> bool {
        super::petgraph_compat::is_cyclic(&self.edges)
    }

    // =========================================================================
    // Internal Helpers
    // =========================================================================

    /// Extracts the components for compilation (internal use only).
    pub(super) fn into_parts(self) -> GraphParts {
        (
            self.nodes,
            self.edges,
            self.conditional_edges,
            self.runtime_config,
            self.reducer_registry,
        )
    }

    // Internal read-only accessors for validation in sibling modules
    pub(super) fn nodes_ref(&self) -> &FxHashMap<NodeKind, Arc<dyn Node>> {
        &self.nodes
    }
    pub(super) fn edges_ref(&self) -> &FxHashMap<NodeKind, Vec<NodeKind>> {
        &self.edges
    }
    pub(super) fn conditional_edges_ref(&self) -> &Vec<ConditionalEdge> {
        &self.conditional_edges
    }
}