oxidizedgraph 0.2.0

A humble attempt at LangGraph in Rust - R-LangGraph framework for building AI agent workflows
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
//! Parallel subgraph execution

use async_trait::async_trait;
use std::collections::HashMap;
use std::sync::Arc;
use tracing::{debug, info, warn};

use crate::error::NodeError;
use crate::graph::{CompiledGraph, NodeExecutor, NodeOutput};
use crate::runner::{GraphRunner, RunnerConfig};
use crate::state::{AgentState, SharedState};

use super::handle::SubgraphResult;
use super::{clone_state, ResultMerger, StateMapper};

/// Strategy for joining parallel subgraph results
#[derive(Clone, Debug, Default)]
pub enum JoinStrategy {
    /// Wait for all subgraphs to complete (default)
    #[default]
    WaitAll,
    /// Return as soon as the first subgraph completes
    WaitFirst,
    /// Return as soon as any subgraph fails (fail-fast)
    FailFast,
    /// Wait for a specific number of subgraphs to complete
    WaitN(usize),
}

/// Configuration for a single subgraph in parallel execution
struct SubgraphConfig {
    /// Subgraph identifier
    id: String,
    /// The compiled graph
    graph: Arc<CompiledGraph>,
    /// State mapper for this subgraph
    state_mapper: StateMapper,
    /// Result merger for this subgraph
    result_merger: ResultMerger,
}

/// A node that executes multiple subgraphs in parallel
///
/// This is useful for fan-out patterns where you need to run multiple
/// independent workflows concurrently and aggregate their results.
///
/// # Example
///
/// ```rust,ignore
/// let parallel = ParallelSubgraphs::new("multi_search")
///     .add_subgraph("web", web_search_graph)
///     .add_subgraph("docs", doc_search_graph)
///     .add_subgraph("code", code_search_graph)
///     .with_join_strategy(JoinStrategy::WaitAll)
///     .with_default_merger(|parent, child| {
///         // Each child's results are stored under its subgraph ID
///         parent.set_context(&format!("{}_results", child_id), child.context);
///     });
/// ```
pub struct ParallelSubgraphs {
    /// Node identifier
    id: String,
    /// Subgraph configurations
    subgraphs: Vec<SubgraphConfig>,
    /// Runner configuration for subgraphs
    config: RunnerConfig,
    /// Join strategy
    join_strategy: JoinStrategy,
    /// Default state mapper (if not specified per-subgraph)
    #[allow(dead_code)]
    default_state_mapper: StateMapper,
    /// Default result merger (if not specified per-subgraph)
    #[allow(dead_code)]
    default_result_merger: ResultMerger,
    /// Next node to continue to
    next_node: Option<String>,
}

impl ParallelSubgraphs {
    /// Create a new parallel subgraphs node
    pub fn new(id: impl Into<String>) -> Self {
        Self {
            id: id.into(),
            subgraphs: Vec::new(),
            config: RunnerConfig::default(),
            join_strategy: JoinStrategy::default(),
            default_state_mapper: clone_state(),
            default_result_merger: Box::new(|parent, child| {
                // Default: merge all context from child
                for (key, value) in child.context {
                    parent.context.insert(key, value);
                }
            }),
            next_node: None,
        }
    }

    /// Add a subgraph to execute in parallel
    pub fn add_subgraph(mut self, id: impl Into<String>, graph: CompiledGraph) -> Self {
        let id = id.into();
        let default_merger = self.create_namespace_merger(&id);

        self.subgraphs.push(SubgraphConfig {
            id,
            graph: Arc::new(graph),
            state_mapper: clone_state(),
            result_merger: default_merger,
        });
        self
    }

    /// Add a subgraph with custom state mapper and result merger
    pub fn add_subgraph_with_handlers<SM, RM>(
        mut self,
        id: impl Into<String>,
        graph: CompiledGraph,
        state_mapper: SM,
        result_merger: RM,
    ) -> Self
    where
        SM: Fn(&AgentState) -> AgentState + Send + Sync + 'static,
        RM: Fn(&mut AgentState, AgentState) + Send + Sync + 'static,
    {
        self.subgraphs.push(SubgraphConfig {
            id: id.into(),
            graph: Arc::new(graph),
            state_mapper: Box::new(state_mapper),
            result_merger: Box::new(result_merger),
        });
        self
    }

    /// Set the join strategy
    pub fn with_join_strategy(mut self, strategy: JoinStrategy) -> Self {
        self.join_strategy = strategy;
        self
    }

    /// Set the runner configuration for subgraphs
    pub fn with_config(mut self, config: RunnerConfig) -> Self {
        self.config = config;
        self
    }

    /// Set the default state mapper for subgraphs
    pub fn with_default_state_mapper<F>(mut self, mapper: F) -> Self
    where
        F: Fn(&AgentState) -> AgentState + Send + Sync + 'static,
    {
        self.default_state_mapper = Box::new(mapper);
        self
    }

    /// Set the next node to continue to
    pub fn then(mut self, next_node: impl Into<String>) -> Self {
        self.next_node = Some(next_node.into());
        self
    }

    /// Create a merger that stores results under a namespace
    fn create_namespace_merger(&self, namespace: &str) -> ResultMerger {
        let ns = namespace.to_string();
        Box::new(move |parent, child| {
            if let Ok(child_json) = serde_json::to_value(&child.context) {
                parent.context.insert(ns.clone(), child_json);
            }
        })
    }

    /// Execute all subgraphs and return results
    async fn execute_all(&self, parent_state: &AgentState) -> Vec<(String, SubgraphResult)> {
        let mut handles = Vec::with_capacity(self.subgraphs.len());

        // Spawn all subgraphs
        for subgraph_config in &self.subgraphs {
            let child_state = (subgraph_config.state_mapper)(parent_state);
            let graph = subgraph_config.graph.clone();
            let config = self.config.clone();
            let subgraph_id = subgraph_config.id.clone();

            let handle = tokio::spawn(async move {
                let runner = GraphRunner::new((*graph).clone(), config);
                match runner.invoke(child_state).await {
                    Ok(state) => SubgraphResult::Completed {
                        subgraph_id,
                        state,
                    },
                    Err(error) => SubgraphResult::Failed {
                        subgraph_id,
                        error,
                    },
                }
            });

            handles.push((subgraph_config.id.clone(), handle));
        }

        // Collect results based on join strategy
        match &self.join_strategy {
            JoinStrategy::WaitAll => {
                let mut results = Vec::with_capacity(handles.len());
                for (id, handle) in handles {
                    let result = handle.await.unwrap_or_else(|e| SubgraphResult::Failed {
                        subgraph_id: id.clone(),
                        error: crate::error::RuntimeError::InvalidState(format!(
                            "Task panicked: {}",
                            e
                        )),
                    });
                    results.push((id, result));
                }
                results
            }
            JoinStrategy::WaitFirst => {
                if handles.is_empty() {
                    return Vec::new();
                }

                // Race all handles using boxed futures for Unpin
                let futures: Vec<_> = handles
                    .into_iter()
                    .map(|(id, h)| {
                        let id_clone = id.clone();
                        Box::pin(async move {
                            let result = h.await.unwrap_or_else(|e| SubgraphResult::Failed {
                                subgraph_id: id_clone.clone(),
                                error: crate::error::RuntimeError::InvalidState(format!(
                                    "Task panicked: {}",
                                    e
                                )),
                            });
                            (id_clone, result)
                        })
                    })
                    .collect();

                let (result, _, _) = futures::future::select_all(futures).await;
                vec![result]
            }
            JoinStrategy::FailFast => {
                let mut results = Vec::new();
                for (id, handle) in handles {
                    let result = handle.await.unwrap_or_else(|e| SubgraphResult::Failed {
                        subgraph_id: id.clone(),
                        error: crate::error::RuntimeError::InvalidState(format!(
                            "Task panicked: {}",
                            e
                        )),
                    });
                    let is_failed = result.is_failed();
                    results.push((id, result));
                    if is_failed {
                        break;
                    }
                }
                results
            }
            JoinStrategy::WaitN(n) => {
                let mut results = Vec::new();
                let mut completed = 0;
                for (id, handle) in handles {
                    if completed >= *n {
                        break;
                    }
                    let result = handle.await.unwrap_or_else(|e| SubgraphResult::Failed {
                        subgraph_id: id.clone(),
                        error: crate::error::RuntimeError::InvalidState(format!(
                            "Task panicked: {}",
                            e
                        )),
                    });
                    if result.is_completed() {
                        completed += 1;
                    }
                    results.push((id, result));
                }
                results
            }
        }
    }
}

#[async_trait]
impl NodeExecutor for ParallelSubgraphs {
    fn id(&self) -> &str {
        &self.id
    }

    async fn execute(&self, state: SharedState) -> Result<NodeOutput, NodeError> {
        if self.subgraphs.is_empty() {
            warn!(node_id = %self.id, "No subgraphs configured");
            return match &self.next_node {
                Some(next) => Ok(NodeOutput::continue_to(next.clone())),
                None => Ok(NodeOutput::finish()),
            };
        }

        // Get parent state
        let parent_state = {
            let guard = state
                .read()
                .map_err(|e| NodeError::execution_failed(e.to_string()))?;
            guard.clone()
        };

        info!(
            node_id = %self.id,
            subgraph_count = self.subgraphs.len(),
            strategy = ?self.join_strategy,
            "Executing parallel subgraphs"
        );

        // Execute all subgraphs
        let results = self.execute_all(&parent_state).await;

        debug!(
            node_id = %self.id,
            completed = results.iter().filter(|(_, r)| r.is_completed()).count(),
            failed = results.iter().filter(|(_, r)| r.is_failed()).count(),
            "Parallel subgraphs completed"
        );

        // Create a map of subgraph configs by ID for merger lookup
        let config_map: HashMap<&str, &SubgraphConfig> = self
            .subgraphs
            .iter()
            .map(|c| (c.id.as_str(), c))
            .collect();

        // Merge results back into parent state
        {
            let mut guard = state
                .write()
                .map_err(|e| NodeError::execution_failed(e.to_string()))?;

            for (id, result) in results {
                if let SubgraphResult::Completed { state: child_state, .. } = result {
                    if let Some(config) = config_map.get(id.as_str()) {
                        (config.result_merger)(&mut guard, child_state);
                    }
                }
            }
        }

        match &self.next_node {
            Some(next) => Ok(NodeOutput::continue_to(next.clone())),
            None => Ok(NodeOutput::finish()),
        }
    }

    fn description(&self) -> Option<&str> {
        Some("Executes multiple subgraphs in parallel")
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::graph::GraphBuilder;
    use std::sync::RwLock;
    use std::time::Duration;

    struct DelayedSetNode {
        id: String,
        key: String,
        value: String,
        delay_ms: u64,
    }

    #[async_trait]
    impl NodeExecutor for DelayedSetNode {
        fn id(&self) -> &str {
            &self.id
        }

        async fn execute(&self, state: SharedState) -> Result<NodeOutput, NodeError> {
            tokio::time::sleep(Duration::from_millis(self.delay_ms)).await;
            {
                let mut guard = state
                    .write()
                    .map_err(|e| NodeError::execution_failed(e.to_string()))?;
                guard.set_context(&self.key, self.value.clone());
            }
            Ok(NodeOutput::finish())
        }
    }

    fn create_delayed_graph(id: &str, key: &str, value: &str, delay_ms: u64) -> CompiledGraph {
        GraphBuilder::new()
            .add_node(DelayedSetNode {
                id: id.to_string(),
                key: key.to_string(),
                value: value.to_string(),
                delay_ms,
            })
            .set_entry_point(id)
            .compile()
            .unwrap()
    }

    #[tokio::test]
    async fn test_parallel_subgraphs_wait_all() {
        let parallel = ParallelSubgraphs::new("parallel")
            .add_subgraph("fast", create_delayed_graph("fast", "fast_result", "fast_value", 10))
            .add_subgraph("slow", create_delayed_graph("slow", "slow_result", "slow_value", 50))
            .with_join_strategy(JoinStrategy::WaitAll);

        let state = Arc::new(RwLock::new(AgentState::new()));
        let result = parallel.execute(state.clone()).await.unwrap();

        assert!(result.is_terminal());

        // Both results should be present
        let guard = state.read().unwrap();
        assert!(guard.context.contains_key("fast"));
        assert!(guard.context.contains_key("slow"));
    }

    #[tokio::test]
    async fn test_parallel_empty() {
        let parallel = ParallelSubgraphs::new("parallel");

        let state = Arc::new(RwLock::new(AgentState::new()));
        let result = parallel.execute(state).await.unwrap();

        assert!(result.is_terminal());
    }
}