ri-agent-graph 0.2.2

Graph-based agent orchestration for Rust — LangGraph-inspired execution engine with checkpointing, parallel fan-out/fan-in, interrupt/resume, and event streaming
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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
#![allow(deprecated)] // Constructs GraphEvent with legacy trace_id/attempt fields during migration

use crate::checkpoint::Checkpoint;
use crate::checkpoint_store::{CheckpointStore, RunStatus, RunSummary};
use crate::command::{Navigation, NodeOutput};
use crate::config::GraphConfig;
use crate::edge::EdgeType;
use crate::error::{AgentGraphError, CheckpointStoreOperation, Result};
use crate::event_sink::{EventSink, GraphEvent, NodeOutcomeKind};
use crate::graph::{AgentGraph, END, START};
use crate::interrupt::{ExecutionResult, InterruptCheckpoint};
use crate::retry::RetryPolicy;
use crate::router::RouterOutput;
use crate::state::AgentState;
use crate::stream::StreamEvent;
use serde_json::Value;
use std::collections::{HashMap, HashSet};
use std::future::Future;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use tokio::sync::mpsc;

fn checkpoint_store_error(
    operation: CheckpointStoreOperation,
    error: AgentGraphError,
) -> AgentGraphError {
    AgentGraphError::CheckpointStore {
        operation,
        message: error.to_string(),
    }
}

/// Execution-related methods on AgentGraph.
impl AgentGraph {
    fn unstarted_run_summary(&self, trace_ctx: stack_ids::TraceCtx) -> RunSummary {
        let now = chrono::Utc::now();
        RunSummary {
            // An empty ID is reserved for a run that was never persisted.
            run_id: String::new(),
            graph_name: self
                .graph_name
                .clone()
                .unwrap_or_else(|| "unnamed".to_string()),
            status: RunStatus::Failed,
            total_nodes_executed: 0,
            total_attempts: 0,
            failed_attempts: 0,
            trace_id: Some(trace_ctx.to_legacy_trace_id().to_string()),
            trace_ctx: Some(trace_ctx),
            started_at: now,
            finished_at: Some(now),
        }
    }

    /// Execute the graph starting from a specific node.
    pub async fn execute(&self, start_node: &str, state: AgentState) -> Result<AgentState> {
        self.execute_with_config(start_node, state, GraphConfig::default())
            .await
    }

    /// Execute the graph and return a structured run summary alongside the result.
    pub async fn execute_with_summary(
        &self,
        start_node: &str,
        state: AgentState,
        config: GraphConfig,
    ) -> (Result<AgentState>, RunSummary) {
        self.register_reducers_on_state(&state).await;
        let trace_ctx = config.resolve_trace_ctx();
        let run_id = match self.create_run_id().await {
            Ok(run_id) => run_id,
            Err(error) => return (Err(error), self.unstarted_run_summary(trace_ctx)),
        };
        let event_sink = self.resolve_event_sink(None);
        let cancel = Arc::new(AtomicBool::new(false));

        let executor = GraphExecutor {
            graph: self,
            state,
            config,
            iteration: 0,
            event_sink,
            run_id,
            trace_ctx,
            cancel_flag: cancel,
            started_at: chrono::Utc::now(),
            total_attempts: 0,
            failed_attempts: 0,
            executed_nodes: HashSet::new(),
        };

        executor.execute(start_node).await
    }

    /// Execute the graph with a runtime config.
    pub async fn execute_with_config(
        &self,
        start_node: &str,
        state: AgentState,
        config: GraphConfig,
    ) -> Result<AgentState> {
        let (result, _summary) = self.execute_with_summary(start_node, state, config).await;
        result
    }

    /// Execute the graph with interrupt support.
    pub async fn execute_with_interrupt(
        &self,
        start_node: &str,
        state: AgentState,
        config: GraphConfig,
    ) -> ExecutionResult {
        self.register_reducers_on_state(&state).await;
        let state_clone = state.clone();
        let trace_ctx = config.resolve_trace_ctx();
        let run_id = match self.create_run_id().await {
            Ok(run_id) => run_id,
            Err(error) => {
                return ExecutionResult::Failed {
                    error,
                    state: state_clone,
                }
            }
        };
        let event_sink = self.resolve_event_sink(None);
        let cancel = Arc::new(AtomicBool::new(false));

        let executor = GraphExecutor {
            graph: self,
            state,
            config,
            iteration: 0,
            event_sink,
            run_id,
            trace_ctx,
            cancel_flag: cancel,
            started_at: chrono::Utc::now(),
            total_attempts: 0,
            failed_attempts: 0,
            executed_nodes: HashSet::new(),
        };

        let (result, _summary) = executor.execute(start_node).await;
        match result {
            Ok(final_state) => ExecutionResult::Complete(final_state),
            Err(AgentGraphError::InterruptError {
                ref node,
                ref value,
            }) => ExecutionResult::Interrupted {
                state: state_clone,
                node: node.clone(),
                interrupt_value: value.clone(),
                checkpoint_data: Some(InterruptCheckpoint {
                    resume_node: node.clone(),
                    resume_before: false,
                    iteration: 0,
                    active_nodes: Vec::new(),
                    graph_hash: Some(self.compute_graph_hash()),
                }),
            },
            // AG-001: Preserve ordinary errors as Failed, not Complete.
            // Only InterruptError maps to Interrupted; everything else
            // is a real failure that must not be silently erased.
            Err(e) => ExecutionResult::Failed {
                error: e,
                state: state_clone,
            },
        }
    }

    /// Execute with cancellation support using an Arc-wrapped graph.
    pub fn execute_cancellable(
        self: Arc<Self>,
        start_node: &str,
        state: AgentState,
        config: GraphConfig,
    ) -> (tokio::task::JoinHandle<Result<AgentState>>, Arc<AtomicBool>) {
        let cancel = Arc::new(AtomicBool::new(false));
        let cancel_clone = cancel.clone();
        let start = start_node.to_string();
        let graph = self;

        let handle = tokio::spawn(async move {
            graph.register_reducers_on_state(&state).await;
            let trace_ctx = config.resolve_trace_ctx();
            let run_id = graph.create_run_id().await?;
            let event_sink = graph.resolve_event_sink(None);

            let executor = GraphExecutor {
                graph: &graph,
                state,
                config,
                iteration: 0,
                event_sink,
                run_id,
                trace_ctx,
                cancel_flag: cancel_clone,
                started_at: chrono::Utc::now(),
                total_attempts: 0,
                failed_attempts: 0,
                executed_nodes: HashSet::new(),
            };

            let (result, _summary) = executor.execute(&start).await;
            result
        });

        (handle, cancel)
    }

    /// Execute with streaming using Arc-wrapped graph.
    /// Returns a join handle for the result and a receiver for events.
    pub fn stream(
        self: Arc<Self>,
        start_node: &str,
        state: AgentState,
        config: GraphConfig,
    ) -> (
        tokio::task::JoinHandle<Result<AgentState>>,
        mpsc::Receiver<StreamEvent>,
    ) {
        let (tx, rx) = mpsc::channel(256);
        let start = start_node.to_string();
        let graph = self;

        let handle = tokio::spawn(async move {
            graph.register_reducers_on_state(&state).await;
            let trace_ctx = config.resolve_trace_ctx();
            let run_id = graph.create_run_id().await?;
            let event_sink = graph.resolve_event_sink(Some(tx));
            let cancel = Arc::new(AtomicBool::new(false));

            let executor = GraphExecutor {
                graph: &graph,
                state,
                config,
                iteration: 0,
                event_sink,
                run_id,
                trace_ctx,
                cancel_flag: cancel,
                started_at: chrono::Utc::now(),
                total_attempts: 0,
                failed_attempts: 0,
                executed_nodes: HashSet::new(),
            };

            let (result, _summary) = executor.execute(&start).await;
            result
        });

        (handle, rx)
    }
}

/// Internal executor that runs the graph using superstep-based execution.
struct GraphExecutor<'a> {
    graph: &'a AgentGraph,
    state: AgentState,
    config: GraphConfig,
    iteration: usize,
    event_sink: Arc<dyn EventSink>,
    run_id: String,
    /// Canonical trace context resolved from config at execution start.
    /// The legacy `trace_id: String` is derived on demand via
    /// `self.trace_ctx.to_legacy_trace_id().to_string()` at event emission sites.
    trace_ctx: stack_ids::TraceCtx,
    cancel_flag: Arc<AtomicBool>,
    started_at: chrono::DateTime<chrono::Utc>,
    total_attempts: usize,
    failed_attempts: usize,
    executed_nodes: HashSet<String>,
}

impl<'a> GraphExecutor<'a> {
    /// Derive the legacy trace_id string from the canonical TraceCtx.
    fn legacy_trace_id(&self) -> String {
        self.trace_ctx.to_legacy_trace_id().to_string()
    }

    async fn execute(mut self, start_node: &str) -> (Result<AgentState>, RunSummary) {
        // Derive legacy trace_id once at the run-level compatibility boundary.
        let run_legacy_tid = self.legacy_trace_id();

        // Emit run start
        self.event_sink.emit(GraphEvent::RunStart {
            run_id: self.run_id.clone(),
            trace_id: run_legacy_tid.clone(),
            trace_ctx: Some(self.trace_ctx.clone()),
            graph_name: self.graph.graph_name.clone(),
        });

        let mut result = self.execute_inner(start_node).await;

        // A configured checkpoint store is part of the execution contract.
        // Do not report success (or a durable failure) if its terminal state
        // could not be persisted.
        if let Some(ref store) = self.graph.checkpoint_store {
            let terminal_result = match &result {
                Ok(_) => store.complete_run(&self.run_id).await.map_err(|error| {
                    checkpoint_store_error(CheckpointStoreOperation::CompleteRun, error)
                }),
                Err(AgentGraphError::Cancelled) => store
                    .fail_run(&self.run_id, "cancelled")
                    .await
                    .map_err(|error| {
                        checkpoint_store_error(CheckpointStoreOperation::FailRun, error)
                    }),
                Err(error) => store
                    .fail_run(&self.run_id, &error.to_string())
                    .await
                    .map_err(|error| {
                        checkpoint_store_error(CheckpointStoreOperation::FailRun, error)
                    }),
            };
            if let Err(error) = terminal_result {
                result = Err(error);
            }
        }

        let status = match &result {
            Ok(_) => RunStatus::Completed,
            Err(AgentGraphError::Cancelled) => RunStatus::Cancelled,
            Err(AgentGraphError::InterruptError { .. }) => RunStatus::Interrupted,
            Err(_) => RunStatus::Failed,
        };

        let summary = self.build_run_summary(status, run_legacy_tid.clone());

        // Emit run end
        self.event_sink.emit(GraphEvent::RunEnd {
            run_id: self.run_id.clone(),
            trace_id: run_legacy_tid,
            trace_ctx: Some(self.trace_ctx.clone()),
        });

        (result, summary)
    }

    async fn execute_inner(&mut self, start_node: &str) -> Result<AgentState> {
        let mut current_superstep = if start_node == START {
            self.get_edge_targets(START).await?
        } else {
            vec![start_node.to_string()]
        };

        let mut step_number: usize = 0;
        let max_iter = self.config.recursion_limit.min(self.graph.max_iterations);

        // Derive legacy trace_id once for all superstep-level events in this execution.
        let loop_legacy_tid = self.legacy_trace_id();

        loop {
            current_superstep.retain(|n| n != END);

            if current_superstep.is_empty() {
                break;
            }

            // Check cancellation
            if self.cancel_flag.load(Ordering::Relaxed) {
                return Err(AgentGraphError::Cancelled);
            }

            // Check max iterations
            if self.iteration >= max_iter {
                return Err(AgentGraphError::MaxIterationsExceeded {
                    current: self.iteration,
                    max: max_iter,
                });
            }

            // Cycle detection
            if self.graph.enable_cycle_detection && step_number > max_iter * 2 {
                return Err(AgentGraphError::CycleDetected {
                    path: current_superstep.clone(),
                });
            }

            // Emit superstep start
            self.event_sink.emit(GraphEvent::SuperstepStart {
                run_id: self.run_id.clone(),
                trace_id: loop_legacy_tid.clone(),
                trace_ctx: Some(self.trace_ctx.clone()),
                step: step_number,
                nodes: current_superstep.clone(),
            });

            // Check interrupt_before
            if let Some(ref interrupt_cfg) = self.graph.interrupt_config {
                for node_name in &current_superstep {
                    if interrupt_cfg.should_interrupt_before(node_name) {
                        self.event_sink.emit(GraphEvent::InterruptRaised {
                            run_id: self.run_id.clone(),
                            trace_id: loop_legacy_tid.clone(),
                            trace_ctx: Some(self.trace_ctx.clone()),
                            node_id: node_name.clone(),
                            kind: "before".to_string(),
                            payload: Value::Null,
                        });

                        // Save legacy checkpoint
                        if let Some(ref checkpointer) = self.graph.checkpointer {
                            if let Some(ref thread_id) = self.config.thread_id {
                                let cp = Checkpoint {
                                    execution_id: thread_id.clone(),
                                    timestamp: chrono::Utc::now(),
                                    current_node: node_name.clone(),
                                    iteration: self.iteration,
                                    state: self.state.snapshot().await,
                                    step_number,
                                    active_nodes: current_superstep.clone(),
                                };
                                let _ = checkpointer.save(&cp).await;
                            }
                        }

                        // Save state to checkpoint store
                        if let Some(ref store) = self.graph.checkpoint_store {
                            let state_data = self.state.export().await;
                            store
                                .save_state_snapshot(&self.run_id, &state_data)
                                .await
                                .map_err(|error| {
                                    checkpoint_store_error(
                                        CheckpointStoreOperation::SaveStateSnapshot,
                                        error,
                                    )
                                })?;
                        }

                        return Err(AgentGraphError::InterruptError {
                            node: node_name.clone(),
                            value: None,
                        });
                    }
                }
            }

            // Execute the superstep
            let mut next_nodes = Vec::new();

            if current_superstep.len() == 1 {
                let node_name = &current_superstep[0];
                let output = self.execute_single_node(node_name).await?;
                let targets = self.resolve_output(node_name, output).await?;
                next_nodes.extend(targets);
            } else {
                // Parallel execution
                let snapshot_data = self.state.export().await;
                let mut join_set = tokio::task::JoinSet::new();
                let max_parallelism = self.config.max_parallelism.max(1);
                let mut pending = current_superstep.iter();

                for _ in 0..max_parallelism {
                    let Some(node_name) = pending.next() else {
                        break;
                    };
                    self.spawn_parallel_branch(&mut join_set, node_name).await?;
                }

                let mut branch_results = Vec::new();
                let mut active_branches: std::collections::HashSet<String> = current_superstep
                    .iter()
                    .take(max_parallelism)
                    .cloned()
                    .collect();
                while let Some(result) = join_set.join_next().await {
                    let inner = result.map_err(|e| AgentGraphError::ExecutionError(e.to_string()));
                    let branch = match inner {
                        Ok(Ok(branch)) => {
                            active_branches.remove(&branch.0);
                            branch
                        }
                        Ok(Err(error)) => {
                            self.failed_attempts += 1;
                            // A branch failure is terminal for this superstep. Abort every
                            // still-running sibling and drain the JoinSet before returning so
                            // no task can continue producing effects after the parent fails.
                            join_set.abort_all();
                            while join_set.join_next().await.is_some() {}
                            for node_id in active_branches {
                                self.event_sink.emit(GraphEvent::NodeEnd {
                                    run_id: self.run_id.clone(),
                                    trace_id: self.legacy_trace_id(),
                                    trace_ctx: Some(self.trace_ctx.clone()),
                                    node_id,
                                    outcome: NodeOutcomeKind::Interrupted,
                                    attempt_id: None,
                                    trial_id: None,
                                });
                            }
                            self.event_sink.emit(GraphEvent::ParallelCancellation {
                                run_id: self.run_id.clone(),
                                trace_id: self.legacy_trace_id(),
                                trace_ctx: Some(self.trace_ctx.clone()),
                                external_effects_may_have_escaped: true,
                            });
                            return Err(error);
                        }
                        Err(error) => {
                            self.failed_attempts += 1;
                            join_set.abort_all();
                            while join_set.join_next().await.is_some() {}
                            for node_id in active_branches {
                                self.event_sink.emit(GraphEvent::NodeEnd {
                                    run_id: self.run_id.clone(),
                                    trace_id: self.legacy_trace_id(),
                                    trace_ctx: Some(self.trace_ctx.clone()),
                                    node_id,
                                    outcome: NodeOutcomeKind::Interrupted,
                                    attempt_id: None,
                                    trial_id: None,
                                });
                            }
                            self.event_sink.emit(GraphEvent::ParallelCancellation {
                                run_id: self.run_id.clone(),
                                trace_id: self.legacy_trace_id(),
                                trace_ctx: Some(self.trace_ctx.clone()),
                                external_effects_may_have_escaped: true,
                            });
                            return Err(error);
                        }
                    };
                    branch_results.push(branch);

                    if let Some(node_name) = pending.next() {
                        active_branches.insert(node_name.to_string());
                        self.spawn_parallel_branch(&mut join_set, node_name).await?;
                    }
                }

                let order: HashMap<&str, usize> = current_superstep
                    .iter()
                    .enumerate()
                    .map(|(index, node)| (node.as_str(), index))
                    .collect();
                branch_results.sort_by_key(|(name, _, _)| {
                    order.get(name.as_str()).copied().unwrap_or(usize::MAX)
                });

                self.merge_parallel_states(&snapshot_data, &branch_results)
                    .await?;

                for (name, _, output) in branch_results {
                    let targets = self.resolve_output(&name, output).await?;
                    next_nodes.extend(targets);
                }
            }

            // Check interrupt_after
            if let Some(ref interrupt_cfg) = self.graph.interrupt_config {
                for node_name in &current_superstep {
                    if interrupt_cfg.should_interrupt_after(node_name) {
                        if let Some(ref checkpointer) = self.graph.checkpointer {
                            if let Some(ref thread_id) = self.config.thread_id {
                                let cp = Checkpoint {
                                    execution_id: thread_id.clone(),
                                    timestamp: chrono::Utc::now(),
                                    current_node: node_name.clone(),
                                    iteration: self.iteration,
                                    state: self.state.snapshot().await,
                                    step_number,
                                    active_nodes: next_nodes.clone(),
                                };
                                let _ = checkpointer.save(&cp).await;
                            }
                        }

                        if let Some(ref store) = self.graph.checkpoint_store {
                            let state_data = self.state.export().await;
                            store
                                .save_state_snapshot(&self.run_id, &state_data)
                                .await
                                .map_err(|error| {
                                    checkpoint_store_error(
                                        CheckpointStoreOperation::SaveStateSnapshot,
                                        error,
                                    )
                                })?;
                        }

                        return Err(AgentGraphError::InterruptError {
                            node: node_name.clone(),
                            value: None,
                        });
                    }
                }
            }

            // Save checkpoint after superstep
            if let Some(ref checkpointer) = self.graph.checkpointer {
                if let Some(ref thread_id) = self.config.thread_id {
                    let current = current_superstep.first().cloned().unwrap_or_default();
                    let cp = Checkpoint {
                        execution_id: thread_id.clone(),
                        timestamp: chrono::Utc::now(),
                        current_node: current,
                        iteration: self.iteration,
                        state: self.state.snapshot().await,
                        step_number,
                        active_nodes: next_nodes.clone(),
                    };
                    let _ = checkpointer.save(&cp).await;
                }
            }

            // Save state snapshot to new checkpoint store
            if let Some(ref store) = self.graph.checkpoint_store {
                let state_data = self.state.export().await;
                store
                    .save_state_snapshot(&self.run_id, &state_data)
                    .await
                    .map_err(|error| {
                        checkpoint_store_error(CheckpointStoreOperation::SaveStateSnapshot, error)
                    })?;
            }

            // Emit superstep end
            self.event_sink.emit(GraphEvent::SuperstepEnd {
                run_id: self.run_id.clone(),
                trace_id: loop_legacy_tid.clone(),
                trace_ctx: Some(self.trace_ctx.clone()),
                step: step_number,
            });

            // Deduplicate next nodes
            let mut seen = std::collections::HashSet::new();
            next_nodes.retain(|n| seen.insert(n.clone()));

            self.iteration += 1;
            step_number += 1;
            current_superstep = next_nodes;
        }

        Ok(self.state.clone())
    }

    /// Execute a single node (sequential path).
    async fn execute_single_node(&mut self, name: &str) -> Result<NodeOutput> {
        self.total_attempts += 1;
        self.executed_nodes.insert(name.to_string());
        let node = self
            .graph
            .nodes
            .get(name)
            .cloned()
            .ok_or_else(|| AgentGraphError::NodeNotFound(name.to_string()))?;

        // AttemptId is stable across the whole retry family (I031).
        let canonical_attempt_id = stack_ids::AttemptId::generate();
        let family_attempt = self.total_attempts as u32;

        // Derive legacy trace_id once at the compatibility boundary for all
        // events emitted during this node execution.
        let legacy_tid = self.legacy_trace_id();
        let trace_ctx = Some(self.trace_ctx.clone());
        let node_name = name.to_string();
        let retry = self.graph.retry_policies.get(name).cloned();

        let before = self.state.export().await;
        let outcome = if let Some(ref executor) = self.graph.executor {
            let executor = executor.clone();
            let state = self.state.clone();
            let config = self.config.clone();
            execute_node_attempt_family(
                move || {
                    let executor = executor.clone();
                    let node = node.clone();
                    let state = state.clone();
                    let config = config.clone();
                    async move { executor.execute_node(node, state, config).await }
                },
                retry,
                self.cancel_flag.clone(),
                self.state.clone(),
                self.event_sink.clone(),
                self.graph.checkpoint_store.clone(),
                self.run_id.clone(),
                node_name.clone(),
                legacy_tid.clone(),
                trace_ctx.clone(),
                family_attempt,
                canonical_attempt_id.clone(),
            )
            .await
        } else {
            let state = self.state.clone();
            let config = self.config.clone();
            execute_node_attempt_family(
                move || {
                    let node = node.clone();
                    let state = state.clone();
                    let config = config.clone();
                    async move { node.execute(&state, &config).await }
                },
                retry,
                self.cancel_flag.clone(),
                self.state.clone(),
                self.event_sink.clone(),
                self.graph.checkpoint_store.clone(),
                self.run_id.clone(),
                node_name.clone(),
                legacy_tid.clone(),
                trace_ctx.clone(),
                family_attempt,
                canonical_attempt_id.clone(),
            )
            .await
        };

        match outcome {
            Ok(outcome) => {
                // Track state updates for events
                let after = self.state.export().await;
                let mut updates = HashMap::new();
                for (key, val) in &after {
                    match before.get(key) {
                        Some(old_val) if old_val != val => {
                            updates.insert(key.clone(), val.clone());
                        }
                        None => {
                            updates.insert(key.clone(), val.clone());
                        }
                        _ => {}
                    }
                }
                if !updates.is_empty() {
                    self.event_sink.emit(GraphEvent::StateUpdate {
                        run_id: self.run_id.clone(),
                        trace_id: legacy_tid.clone(),
                        trace_ctx: trace_ctx.clone(),
                        node_id: node_name.clone(),
                        updates,
                    });
                }

                self.event_sink.emit(GraphEvent::NodeEnd {
                    run_id: self.run_id.clone(),
                    trace_id: legacy_tid.clone(),
                    trace_ctx,
                    node_id: node_name,
                    outcome: NodeOutcomeKind::Success,
                    attempt_id: Some(canonical_attempt_id),
                    trial_id: Some(outcome.trial_id),
                });
                Ok(outcome.output)
            }
            Err(failure) => {
                self.failed_attempts += 1;
                self.event_sink.emit(GraphEvent::NodeEnd {
                    run_id: self.run_id.clone(),
                    trace_id: legacy_tid,
                    trace_ctx,
                    node_id: node_name,
                    outcome: failure.outcome,
                    attempt_id: Some(canonical_attempt_id),
                    trial_id: Some(failure.trial_id),
                });
                Err(failure.error)
            }
        }
    }

    /// Resolve the output of a node to a list of next node names.
    async fn resolve_output(&self, node_name: &str, output: NodeOutput) -> Result<Vec<String>> {
        match output {
            NodeOutput::Done => self.get_edge_targets(node_name).await,
            NodeOutput::Command(cmd) => match cmd.goto {
                Navigation::Default => self.get_edge_targets(node_name).await,
                Navigation::Node(n) => Ok(vec![n]),
                Navigation::Nodes(ns) => Ok(ns),
                Navigation::End => Ok(vec![END.to_string()]),
                Navigation::Send(ops) => Ok(ops.into_iter().map(|op| op.node).collect()),
            },
        }
    }

    /// Get all edge targets from a node.
    async fn get_edge_targets(&self, node_name: &str) -> Result<Vec<String>> {
        let mut targets = Vec::new();
        if let Some(edge_list) = self.graph.edges.get(node_name) {
            for edge in edge_list {
                match edge {
                    EdgeType::Normal(to) => targets.push(to.clone()),
                    EdgeType::Conditional(router) => {
                        match router.route(&self.state, &self.config).await? {
                            RouterOutput::Next(Some(n)) => targets.push(n),
                            RouterOutput::Next(None) => {}
                            RouterOutput::FanOut(ns) => targets.extend(ns),
                        }
                    }
                }
            }
        }
        Ok(targets)
    }

    /// Merge state changes from parallel branches back into the main state.
    async fn merge_parallel_states(
        &self,
        snapshot: &HashMap<String, Value>,
        branches: &[(String, AgentState, NodeOutput)],
    ) -> Result<()> {
        let mut changes: HashMap<String, Vec<Value>> = HashMap::new();

        for (_, branch_state, _) in branches {
            let branch_data = branch_state.export().await;
            for (key, new_value) in &branch_data {
                let changed = match snapshot.get(key) {
                    Some(old_val) => old_val != new_value,
                    None => true,
                };
                if changed {
                    changes
                        .entry(key.clone())
                        .or_default()
                        .push(new_value.clone());
                }
            }
        }

        for (key, values) in changes {
            let base = snapshot.get(&key).cloned().unwrap_or(Value::Null);
            let mut current = base;
            for value in values {
                current = self.state.apply_reducer(&key, &current, &value).await?;
            }
            self.state.set_raw(&key, current).await?;
        }

        Ok(())
    }

    async fn spawn_parallel_branch(
        &mut self,
        join_set: &mut tokio::task::JoinSet<Result<(String, AgentState, NodeOutput)>>,
        node_name: &str,
    ) -> Result<()> {
        self.total_attempts += 1;
        self.executed_nodes.insert(node_name.to_string());

        let forked_state = self.state.fork().await;
        let node = self
            .graph
            .nodes
            .get(node_name)
            .cloned()
            .ok_or_else(|| AgentGraphError::NodeNotFound(node_name.to_string()))?;
        let config = self.config.clone();
        let name = node_name.to_string();
        let retry_policy = self.graph.retry_policies.get(node_name).cloned();
        let event_sink = self.event_sink.clone();
        let run_id = self.run_id.clone();
        let trace_id = self.legacy_trace_id();
        let trace_ctx = Some(self.trace_ctx.clone());
        let checkpoint_store = self.graph.checkpoint_store.clone();
        let node_attempt_count = self.total_attempts as u32;
        let cancel_flag = self.cancel_flag.clone();

        if let Some(ref executor) = self.graph.executor {
            let exec = executor.clone();
            join_set.spawn(async move {
                // AttemptId is stable across the retry family (I031).
                let canonical_attempt_id = stack_ids::AttemptId::generate();

                let before = forked_state.export().await;
                let execution_state = forked_state.clone();
                let outcome = execute_node_attempt_family(
                    move || {
                        let exec = exec.clone();
                        let node = node.clone();
                        let forked_state = execution_state.clone();
                        let config = config.clone();
                        async move { exec.execute_node(node, forked_state, config).await }
                    },
                    retry_policy,
                    cancel_flag.clone(),
                    forked_state.clone(),
                    event_sink.clone(),
                    checkpoint_store.clone(),
                    run_id.clone(),
                    name.clone(),
                    trace_id.clone(),
                    trace_ctx.clone(),
                    node_attempt_count,
                    canonical_attempt_id.clone(),
                )
                .await;

                match outcome {
                    Ok(outcome) => {
                        let after = forked_state.export().await;
                        let mut updates = HashMap::new();
                        for (key, val) in &after {
                            match before.get(key) {
                                Some(old_val) if old_val != val => {
                                    updates.insert(key.clone(), val.clone());
                                }
                                None => {
                                    updates.insert(key.clone(), val.clone());
                                }
                                _ => {}
                            }
                        }
                        if !updates.is_empty() {
                            event_sink.emit(GraphEvent::StateUpdate {
                                run_id: run_id.clone(),
                                trace_id: trace_id.clone(),
                                trace_ctx: trace_ctx.clone(),
                                node_id: name.clone(),
                                updates,
                            });
                        }

                        event_sink.emit(GraphEvent::NodeEnd {
                            run_id: run_id.clone(),
                            trace_id: trace_id.clone(),
                            trace_ctx: trace_ctx.clone(),
                            node_id: name.clone(),
                            outcome: NodeOutcomeKind::Success,
                            attempt_id: Some(canonical_attempt_id),
                            trial_id: Some(outcome.trial_id),
                        });

                        Ok::<_, AgentGraphError>((name, forked_state, outcome.output))
                    }
                    Err(failure) => {
                        event_sink.emit(GraphEvent::NodeEnd {
                            run_id: run_id.clone(),
                            trace_id: trace_id.clone(),
                            trace_ctx: trace_ctx.clone(),
                            node_id: name.clone(),
                            outcome: failure.outcome,
                            attempt_id: Some(canonical_attempt_id),
                            trial_id: Some(failure.trial_id),
                        });
                        Err(failure.error)
                    }
                }
            });
        } else {
            join_set.spawn(async move {
                // AttemptId is stable across the retry family (I031).
                let canonical_attempt_id = stack_ids::AttemptId::generate();

                let before = forked_state.export().await;
                let execution_state = forked_state.clone();
                let outcome = execute_node_attempt_family(
                    move || {
                        let node = node.clone();
                        let forked_state = execution_state.clone();
                        let config = config.clone();
                        async move { node.execute(&forked_state, &config).await }
                    },
                    retry_policy,
                    cancel_flag.clone(),
                    forked_state.clone(),
                    event_sink.clone(),
                    checkpoint_store.clone(),
                    run_id.clone(),
                    name.clone(),
                    trace_id.clone(),
                    trace_ctx.clone(),
                    node_attempt_count,
                    canonical_attempt_id.clone(),
                )
                .await;

                match outcome {
                    Ok(outcome) => {
                        let after = forked_state.export().await;
                        let mut updates = HashMap::new();
                        for (key, val) in &after {
                            match before.get(key) {
                                Some(old_val) if old_val != val => {
                                    updates.insert(key.clone(), val.clone());
                                }
                                None => {
                                    updates.insert(key.clone(), val.clone());
                                }
                                _ => {}
                            }
                        }
                        if !updates.is_empty() {
                            event_sink.emit(GraphEvent::StateUpdate {
                                run_id: run_id.clone(),
                                trace_id: trace_id.clone(),
                                trace_ctx: trace_ctx.clone(),
                                node_id: name.clone(),
                                updates,
                            });
                        }

                        event_sink.emit(GraphEvent::NodeEnd {
                            run_id: run_id.clone(),
                            trace_id: trace_id.clone(),
                            trace_ctx: trace_ctx.clone(),
                            node_id: name.clone(),
                            outcome: NodeOutcomeKind::Success,
                            attempt_id: Some(canonical_attempt_id),
                            trial_id: Some(outcome.trial_id),
                        });

                        Ok::<_, AgentGraphError>((name, forked_state, outcome.output))
                    }
                    Err(failure) => {
                        event_sink.emit(GraphEvent::NodeEnd {
                            run_id: run_id.clone(),
                            trace_id: trace_id.clone(),
                            trace_ctx: trace_ctx.clone(),
                            node_id: name.clone(),
                            outcome: failure.outcome,
                            attempt_id: Some(canonical_attempt_id),
                            trial_id: Some(failure.trial_id),
                        });
                        Err(failure.error)
                    }
                }
            });
        }

        Ok(())
    }

    /// Build the run summary. `legacy_tid` is the pre-derived legacy trace ID
    /// from the run-level compatibility boundary.
    fn build_run_summary(&self, status: RunStatus, legacy_tid: String) -> RunSummary {
        RunSummary {
            run_id: self.run_id.clone(),
            graph_name: self
                .graph
                .graph_name
                .clone()
                .unwrap_or_else(|| "unnamed".to_string()),
            status,
            total_nodes_executed: self.executed_nodes.len(),
            total_attempts: self.total_attempts,
            failed_attempts: self.failed_attempts,
            trace_id: Some(legacy_tid),
            trace_ctx: Some(self.trace_ctx.clone()),
            started_at: self.started_at,
            finished_at: Some(chrono::Utc::now()),
        }
    }
}

/// Successful execution outcome for a retry family.
struct AttemptFamilySuccess {
    output: NodeOutput,
    trial_id: stack_ids::TrialId,
}

/// Final failure for a retry family after the last concrete trial ends.
struct AttemptFamilyFailure {
    error: AgentGraphError,
    outcome: NodeOutcomeKind,
    trial_id: stack_ids::TrialId,
}

#[allow(clippy::too_many_arguments)]
async fn execute_node_attempt_family<ExecOnce, ExecFut>(
    mut exec_once: ExecOnce,
    retry: Option<RetryPolicy>,
    cancel_flag: Arc<AtomicBool>,
    state: AgentState,
    event_sink: Arc<dyn EventSink>,
    checkpoint_store: Option<Arc<dyn CheckpointStore>>,
    run_id: String,
    node_id: String,
    legacy_trace_id: String,
    trace_ctx: Option<stack_ids::TraceCtx>,
    family_attempt: u32,
    canonical_attempt_id: stack_ids::AttemptId,
) -> std::result::Result<AttemptFamilySuccess, AttemptFamilyFailure>
where
    ExecOnce: FnMut() -> ExecFut,
    ExecFut: Future<Output = Result<NodeOutput>>,
{
    let max_attempts = retry
        .as_ref()
        .map_or(1, |policy| policy.max_attempts.max(1));

    for attempt_index in 0..max_attempts {
        if cancel_flag.load(Ordering::SeqCst) {
            return Err(AttemptFamilyFailure {
                error: AgentGraphError::Cancelled,
                outcome: NodeOutcomeKind::Interrupted,
                trial_id: stack_ids::TrialId::generate(),
            });
        }
        let trial_id = stack_ids::TrialId::generate();
        event_sink.emit(GraphEvent::NodeStart {
            run_id: run_id.clone(),
            trace_id: legacy_trace_id.clone(),
            trace_ctx: trace_ctx.clone(),
            node_id: node_id.clone(),
            attempt: family_attempt,
            attempt_id: Some(canonical_attempt_id.clone()),
            trial_id: Some(trial_id.clone()),
        });

        let checkpoint_attempt_id = if let Some(ref store) = checkpoint_store {
            let state_val = serde_json::to_value(&state.export().await).unwrap_or(Value::Null);
            match store
                .record_attempt(&run_id, &node_id, attempt_index as u32, &state_val)
                .await
            {
                Ok(attempt_id) => Some(attempt_id),
                Err(error) => {
                    return Err(AttemptFamilyFailure {
                        error: checkpoint_store_error(
                            CheckpointStoreOperation::RecordAttempt,
                            error,
                        ),
                        outcome: NodeOutcomeKind::Failed,
                        trial_id,
                    });
                }
            }
        } else {
            None
        };

        match exec_once().await {
            Ok(output) => {
                if let NodeOutput::Command(ref cmd) = output {
                    if let Some(ref updates) = cmd.update {
                        for (key, value) in updates {
                            if let Err(error) = state.set(key, value.clone()).await {
                                if let Some(ref store) = checkpoint_store {
                                    if let Some(ref attempt_id) = checkpoint_attempt_id {
                                        if let Err(store_error) =
                                            store.fail_attempt(attempt_id, &error.to_string()).await
                                        {
                                            return Err(AttemptFamilyFailure {
                                                error: checkpoint_store_error(
                                                    CheckpointStoreOperation::FailAttempt,
                                                    store_error,
                                                ),
                                                outcome: NodeOutcomeKind::Failed,
                                                trial_id: trial_id.clone(),
                                            });
                                        }
                                    }
                                }
                                return Err(AttemptFamilyFailure {
                                    error,
                                    outcome: NodeOutcomeKind::Failed,
                                    trial_id: trial_id.clone(),
                                });
                            }
                        }
                    }
                }

                if let Some(ref store) = checkpoint_store {
                    if let Some(ref attempt_id) = checkpoint_attempt_id {
                        let mut meta = HashMap::new();
                        meta.insert(
                            "trace_id".to_string(),
                            Value::String(legacy_trace_id.clone()),
                        );
                        let state_val =
                            serde_json::to_value(&state.export().await).unwrap_or(Value::Null);
                        if let Err(error) =
                            store.complete_attempt(attempt_id, &state_val, &meta).await
                        {
                            return Err(AttemptFamilyFailure {
                                error: checkpoint_store_error(
                                    CheckpointStoreOperation::CompleteAttempt,
                                    error,
                                ),
                                outcome: NodeOutcomeKind::Failed,
                                trial_id: trial_id.clone(),
                            });
                        }
                    }
                }

                return Ok(AttemptFamilySuccess { output, trial_id });
            }
            Err(error) => {
                if let Some(ref store) = checkpoint_store {
                    if let Some(ref attempt_id) = checkpoint_attempt_id {
                        if let Err(store_error) =
                            store.fail_attempt(attempt_id, &error.to_string()).await
                        {
                            return Err(AttemptFamilyFailure {
                                error: checkpoint_store_error(
                                    CheckpointStoreOperation::FailAttempt,
                                    store_error,
                                ),
                                outcome: NodeOutcomeKind::Failed,
                                trial_id,
                            });
                        }
                    }
                }

                let outcome = if matches!(&error, AgentGraphError::InterruptError { .. }) {
                    NodeOutcomeKind::Interrupted
                } else {
                    NodeOutcomeKind::Failed
                };

                let should_retry = retry.as_ref().is_some_and(|policy| {
                    attempt_index + 1 < max_attempts && policy.should_retry(&error)
                });

                if should_retry {
                    event_sink.emit(GraphEvent::NodeEnd {
                        run_id: run_id.clone(),
                        trace_id: legacy_trace_id.clone(),
                        trace_ctx: trace_ctx.clone(),
                        node_id: node_id.clone(),
                        outcome: outcome.clone(),
                        attempt_id: Some(canonical_attempt_id.clone()),
                        trial_id: Some(trial_id.clone()),
                    });
                    let Some(policy) = retry.as_ref() else {
                        return Err(AttemptFamilyFailure {
                            error,
                            outcome,
                            trial_id,
                        });
                    };
                    let delay = policy.delay_for_attempt(attempt_index);
                    let mut remaining = delay;
                    loop {
                        if cancel_flag.load(Ordering::SeqCst) {
                            return Err(AttemptFamilyFailure {
                                error: AgentGraphError::Cancelled,
                                outcome: NodeOutcomeKind::Interrupted,
                                trial_id,
                            });
                        }
                        let tick = std::time::Duration::from_millis(10).min(remaining);
                        tokio::time::sleep(tick).await;
                        if remaining <= tick {
                            break;
                        }
                        remaining -= tick;
                    }
                } else {
                    return Err(AttemptFamilyFailure {
                        error,
                        outcome,
                        trial_id,
                    });
                }
            }
        }
    }

    Err(AttemptFamilyFailure {
        error: AgentGraphError::ExecutionError("Retry exhausted with no error".to_string()),
        outcome: NodeOutcomeKind::Failed,
        trial_id: stack_ids::TrialId::generate(),
    })
}