lellm-graph 0.3.0

Graph/Node/Edge orchestration layer for LeLLM — with State, Delta, Checkpoint
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
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
//! Graph 执行引擎。
//!
//! 提供阻塞执行(`execute`)与流式执行(`execute_stream`)两种模式。
//! 运行时全局步数限制(`max_steps`)防止无限循环。
//!
//! 流式执行返回 `GraphExecution`(stream + handle)。
//! **Stream is primary, Blocking is derived.**

use std::collections::HashMap;
use std::time::Instant;

use tokio::sync::mpsc;

use crate::barrier_node::BarrierDefaultAction;
use crate::checkpoint::{
    Checkpoint, CheckpointPolicy, CheckpointScore, CheckpointStore, CheckpointTrigger,
    ExecutionMetadata, IncrementalSnapshotState,
};
use crate::delta::{Reducer, ReducerRegistry, StateDelta};
use crate::error::{GraphError, ObservedError, TerminalError};
use crate::event::{
    BarrierDecision, BarrierDecisionMessage, BarrierId, FlowEvent, GraphEvent, GraphExecution,
    GraphHandle,
};
use crate::graph::Graph;
use crate::ids::{SpanId, TraceId};
use crate::node::{FlowNode, NextStep, NodeKind, ParallelErrorStrategy, StreamNodeResult};
use crate::state::{ExecutionEntry, GraphResult, State};

// ─── DecisionRegistry ─────────────────────────────────────────

/// Barrier 决策注册表 — Executor 私有状态。
///
/// Level-triggered:在 Barrier 进入等待状态之前提交的决策 MUST 被保留。
struct DecisionRegistry {
    pending: HashMap<BarrierId, BarrierDecision>,
    wildcards: HashMap<String, BarrierDecision>,
    occurrence_counter: HashMap<String, u32>,
}

impl DecisionRegistry {
    fn new() -> Self {
        Self {
            pending: HashMap::new(),
            wildcards: HashMap::new(),
            occurrence_counter: HashMap::new(),
        }
    }

    fn next_id(&mut self, node_id: &str) -> BarrierId {
        let occ = self
            .occurrence_counter
            .entry(node_id.to_string())
            .or_insert(0);
        *occ += 1;
        BarrierId::new(node_id, *occ)
    }

    fn take(&mut self, target_id: &BarrierId) -> Option<BarrierDecision> {
        if let Some(decision) = self.pending.remove(target_id) {
            return Some(decision);
        }
        self.wildcards.get(&target_id.node_id).cloned()
    }

    fn process_message(
        &mut self,
        msg: BarrierDecisionMessage,
        target_id: &BarrierId,
    ) -> Option<BarrierDecision> {
        match msg {
            BarrierDecisionMessage::Exact {
                barrier_id,
                decision,
            } => {
                if barrier_id == *target_id {
                    Some(decision)
                } else {
                    self.pending.insert(barrier_id, decision);
                    None
                }
            }
            BarrierDecisionMessage::Wildcard { node_id, decision } => {
                // 始终存储通配决策,以便后续 occurrence 使用
                self.wildcards.insert(node_id.clone(), decision.clone());
                if node_id == target_id.node_id {
                    Some(decision)
                } else {
                    None
                }
            }
        }
    }
}

// ─── StepOutcome ──────────────────────────────────────────────

/// 节点执行后的下一步操作。
#[derive(Debug)]
enum StepOutcome {
    /// 继续执行,跳转到指定节点
    Continue(String),
    /// 正常结束(到达 end 节点),由外层发送 GraphComplete
    Break,
    /// 错误已发送(GraphError),直接返回
    ErrorSent,
}

// ─── GraphExecutor ────────────────────────────────────────────

/// Graph 执行器 — 可配置运行时参数。
///
/// 支持可选的 Checkpoint 集成,实现持久化执行。
pub struct GraphExecutor {
    /// 全局运行时步数限制。
    /// 1 Step = 1 Node Entry。
    pub max_steps: usize,
    /// 可选的 Checkpoint 存储后端。
    store: Option<std::sync::Arc<dyn CheckpointStore>>,
    /// Checkpoint 保存频率策略。
    policy: CheckpointPolicy,
    /// 图结构指纹(用于恢复时校验)。
    graph_hash: String,
    /// 待注册的 Reducer(在 run_loop 中应用到 ReducerRegistry)。
    pending_reducers: Vec<(String, Reducer)>,
    /// Adaptive Checkpoint 评分器。
    checkpoint_score: CheckpointScore,
    /// 增量快照:上次 checkpoint 的完整 State(base)。
    last_checkpoint_state: Option<State>,
    /// 增量快照:上次 checkpoint 以来的 deltas。
    pending_deltas: Vec<StateDelta>,
    /// 增量快照:delta 累积阈值(超过此值重新生成 base)。
    delta_compact_threshold: usize,
}

impl Clone for GraphExecutor {
    fn clone(&self) -> Self {
        Self {
            max_steps: self.max_steps,
            store: self.store.clone(),
            policy: self.policy.clone(),
            graph_hash: self.graph_hash.clone(),
            pending_reducers: self.pending_reducers.clone(),
            checkpoint_score: self.checkpoint_score.clone(),
            last_checkpoint_state: self.last_checkpoint_state.clone(),
            pending_deltas: self.pending_deltas.clone(),
            delta_compact_threshold: self.delta_compact_threshold,
        }
    }
}

impl std::fmt::Debug for GraphExecutor {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("GraphExecutor")
            .field("max_steps", &self.max_steps)
            .field("has_store", &self.store.is_some())
            .field("policy", &self.policy)
            .field("graph_hash", &self.graph_hash)
            .finish()
    }
}

impl Default for GraphExecutor {
    fn default() -> Self {
        Self {
            max_steps: 50,
            store: None,
            policy: CheckpointPolicy::default(),
            graph_hash: String::new(),
            pending_reducers: Vec::new(),
            checkpoint_score: CheckpointScore::default(),
            last_checkpoint_state: None,
            pending_deltas: Vec::new(),
            delta_compact_threshold: 20,
        }
    }
}

impl GraphExecutor {
    /// 创建基础执行器(无 Checkpoint)。
    pub fn new(max_steps: usize) -> Self {
        Self {
            max_steps,
            store: None,
            policy: CheckpointPolicy::default(),
            graph_hash: String::new(),
            pending_reducers: Vec::new(),
            checkpoint_score: CheckpointScore::default(),
            last_checkpoint_state: None,
            pending_deltas: Vec::new(),
            delta_compact_threshold: 20,
        }
    }

    /// 创建带 Checkpoint 的执行器。
    pub fn with_checkpoint(
        max_steps: usize,
        store: std::sync::Arc<dyn CheckpointStore>,
        policy: CheckpointPolicy,
        graph: &Graph,
    ) -> Self {
        Self {
            max_steps,
            store: Some(store),
            policy,
            graph_hash: graph.hash(),
            pending_reducers: Vec::new(),
            checkpoint_score: CheckpointScore::default(),
            last_checkpoint_state: None,
            pending_deltas: Vec::new(),
            delta_compact_threshold: 20,
        }
    }

    /// 设置 Checkpoint 存储后端。
    pub fn set_store(&mut self, store: std::sync::Arc<dyn CheckpointStore>) {
        self.store = Some(store);
    }

    /// 设置 Checkpoint 频率策略。
    pub fn set_policy(&mut self, policy: CheckpointPolicy) {
        self.policy = policy;
    }

    /// 注册 key 的 Reducer(用于 ParallelNode 合并策略)。
    pub fn register_reducer(&mut self, key: &str, reducer: Reducer) {
        // ReducerRegistry 在 run_loop 中创建,这里存储待注册的 reducers
        // 通过一个新字段传递
        self.pending_reducers.push((key.to_string(), reducer));
    }

    /// 设置 Adaptive Checkpoint 评分器。
    pub fn set_checkpoint_score(mut self, score: CheckpointScore) -> Self {
        self.checkpoint_score = score;
        self
    }

    /// 设置图结构指纹。
    pub fn set_graph(&mut self, graph: &Graph) {
        self.graph_hash = graph.hash();
    }

    // ─── 阻塞执行 ──────────────────────────────────────────────

    /// 执行 Graph(阻塞模式)。
    ///
    /// **Blocking is derived from stream.** 内部消费 stream 直到结束。
    ///
    /// ⚠️ **BarrierNode 不支持阻塞模式。** 如果图中包含 BarrierNode,
    /// 会提前返回错误,引导用户使用 `execute_stream()`。
    pub async fn execute(
        &self,
        graph: std::sync::Arc<Graph>,
        initial_state: State,
    ) -> Result<GraphResult, GraphError> {
        for (name, node) in &graph.nodes {
            if matches!(node, NodeKind::Barrier(_)) {
                return Err(GraphError::Terminal(TerminalError::InvalidGraph(format!(
                    "BarrierNode '{}' requires stream mode. Use GraphExecutor::execute_stream() for human-in-the-loop.",
                    name
                ))));
            }
        }

        let GraphExecution { mut stream, handle } = self.execute_stream(graph, initial_state);

        drop(handle);

        let mut result = None;

        while let Some(event) = stream.recv().await {
            match event {
                GraphEvent::GraphComplete { result: r } => {
                    result = Some(Ok(r));
                }
                GraphEvent::GraphError { error, .. } => {
                    result = Some(Err(error));
                }
                _ => {}
            }
        }

        result.unwrap_or_else(|| {
            Err(GraphError::Terminal(TerminalError::InvalidGraph(
                "stream ended without completion".into(),
            )))
        })
    }

    // ─── 流式执行 ──────────────────────────────────────────────

    /// 流式执行 Graph,返回 `GraphExecution`(stream + handle)。
    ///
    /// **Stream is primary, Blocking is derived.**
    pub fn execute_stream(
        &self,
        graph: std::sync::Arc<Graph>,
        initial_state: State,
    ) -> GraphExecution {
        let executor = self.clone();
        let (event_tx, event_rx) = mpsc::channel(32);
        let (decision_tx, decision_rx) = mpsc::channel(16);
        let (cancel_tx, cancel_rx) = mpsc::channel(1);
        let (checkpoint_tx, checkpoint_rx) = mpsc::channel(8);

        let handle = GraphHandle::new(decision_tx, cancel_tx, checkpoint_tx);

        tokio::spawn(async move {
            executor
                .run_loop(
                    graph,
                    initial_state,
                    event_tx,
                    decision_rx,
                    cancel_rx,
                    checkpoint_rx,
                )
                .await;
        });

        GraphExecution {
            stream: event_rx,
            handle,
        }
    }

    /// 主执行循环。
    async fn run_loop(
        &self,
        graph: std::sync::Arc<Graph>,
        initial_state: State,
        event_tx: mpsc::Sender<GraphEvent>,
        mut decision_rx: mpsc::Receiver<BarrierDecisionMessage>,
        mut cancel_rx: mpsc::Receiver<()>,
        mut checkpoint_rx: mpsc::Receiver<()>,
    ) {
        let start_time = Instant::now();
        let mut state = initial_state;
        let mut execution_log = Vec::new();
        let mut decision_registry = DecisionRegistry::new();
        let mut reducer_registry = ReducerRegistry::new();
        let mut snapshot_state = IncrementalSnapshotState::new(self.delta_compact_threshold);

        // 应用待注册的 Reducer
        for (key, reducer) in &self.pending_reducers {
            reducer_registry.register(key, *reducer);
        }

        let mut current = graph.start_node().to_string();
        let mut step: usize = 0;
        let trace_id = TraceId::default();

        // 发射 GraphStart 事件
        if self
            .send(&event_tx, GraphEvent::GraphStart { trace_id })
            .await
        {
            return;
        }

        loop {
            // ⚡ 取消信号检测
            if cancel_rx.try_recv().is_ok() {
                self.send_graph_error(
                    &event_tx,
                    GraphError::Terminal(TerminalError::BarrierCancelled {
                        node: "execution cancelled by handle".into(),
                    }),
                    &state,
                    &execution_log,
                    start_time,
                    trace_id,
                )
                .await;
                return;
            }

            // ⚡ Manual checkpoint 信号检测 — 立即保存
            if checkpoint_rx.try_recv().is_ok() {
                self.save_checkpoint_if_needed(
                    &event_tx,
                    &trace_id,
                    &current,
                    &state,
                    step,
                    CheckpointTrigger::Explicit,
                    &mut snapshot_state,
                )
                .await;
            }

            step += 1;

            // ⚡ 运行时熔断
            if step > self.max_steps {
                self.send_graph_error(
                    &event_tx,
                    GraphError::Terminal(TerminalError::StepsExceeded {
                        limit: self.max_steps,
                    }),
                    &state,
                    &execution_log,
                    start_time,
                    trace_id,
                )
                .await;
                return;
            }

            // 查找节点
            let node = match graph.nodes.get(&current) {
                Some(n) => n,
                None => {
                    self.send_graph_error(
                        &event_tx,
                        GraphError::Terminal(TerminalError::NodeNotFound(current.clone())),
                        &state,
                        &execution_log,
                        start_time,
                        trace_id,
                    )
                    .await;
                    return;
                }
            };

            let node_name = current.clone();
            let span_id = SpanId::new();

            if self
                .send(
                    &event_tx,
                    GraphEvent::NodeStart {
                        node_name: node_name.clone(),
                        trace_id,
                        span_id,
                        step,
                    },
                )
                .await
            {
                return;
            }

            let node_start = Instant::now();
            let result = if matches!(node, NodeKind::Parallel(_)) {
                self.handle_parallel(node, &state, &event_tx, span_id, &node_name)
                    .await
            } else {
                node.execute_stream(&state, &event_tx, span_id).await
            };
            let node_end = Instant::now();
            let duration = node_end.duration_since(node_start);

            match result {
                Ok(StreamNodeResult::Continue {
                    deltas,
                    next,
                    span_id,
                    observed,
                    metadata,
                }) => {
                    // Adaptive Checkpoint: 使用节点提供的元数据
                    if self.policy.has_adaptive_trigger() {
                        let exec_metadata = ExecutionMetadata {
                            duration_ms: duration.as_millis() as u64,
                            token_cost: metadata.as_ref().map_or(0.0, |m| m.token_cost),
                            has_side_effects: metadata
                                .as_ref()
                                .is_some_and(|m| m.has_side_effects),
                        };
                        self.save_checkpoint_if_needed(
                            &event_tx,
                            &trace_id,
                            &current,
                            &state,
                            step,
                            CheckpointTrigger::Adaptive(exec_metadata),
                            &mut snapshot_state,
                        )
                        .await;
                    }

                    // 记录 deltas 到增量快照状态(用于 StateSnapshot)
                    snapshot_state.record_deltas(deltas.clone());

                    // Apply deltas to state
                    if matches!(node, NodeKind::Parallel(_)) {
                        // Parallel 节点 — 使用 merge_deltas 处理多 writer 冲突
                        if let Err(e) = reducer_registry.merge_deltas(&mut state, &deltas) {
                            // 冲突即错误 — 终止执行
                            self.handle_error(
                                &event_tx,
                                &mut execution_log,
                                &node_name,
                                node_start,
                                node_end,
                                span_id,
                                step,
                                trace_id,
                                GraphError::Terminal(TerminalError::StateError(format!(
                                    "parallel merge conflict: {}",
                                    e
                                ))),
                                &state,
                            )
                            .await;
                            return;
                        }
                        // 发射 StateChanged 事件
                        for delta in &deltas {
                            let d: StateDelta = delta.clone();
                            let _ = self
                                .send(
                                    &event_tx,
                                    GraphEvent::Node {
                                        span_id: SpanId::new(),
                                        node_name: node_name.to_string(),
                                        event: FlowEvent::StateChanged {
                                            node_id: node_name.to_string(),
                                            delta: d,
                                        },
                                    },
                                )
                                .await;
                        }
                    } else {
                        self.apply_deltas(
                            &event_tx,
                            &mut reducer_registry,
                            &mut state,
                            &node_name,
                            &deltas,
                        )
                        .await;
                    }

                    let outcome = self
                        .handle_continue(
                            &event_tx,
                            &graph,
                            &current,
                            &mut state,
                            &mut execution_log,
                            next,
                            span_id,
                            observed,
                            step,
                            &node_name,
                            node_start,
                            node_end,
                            duration,
                            trace_id,
                        )
                        .await;

                    match outcome {
                        StepOutcome::Continue(target) => {
                            // 💾 Checkpoint: Explicit 模式下保存(节点标注了 .checkpoint())
                            self.save_checkpoint_if_needed(
                                &event_tx,
                                &trace_id,
                                &target,
                                &state,
                                step,
                                CheckpointTrigger::Explicit,
                                &mut snapshot_state,
                            )
                            .await;
                            current = target;
                        }
                        StepOutcome::Break => {
                            // 正常结束(到达 end 节点)
                            self.send_graph_complete(
                                &event_tx,
                                &state,
                                &execution_log,
                                start_time,
                                trace_id,
                                &mut snapshot_state,
                            )
                            .await;
                            return;
                        }
                        StepOutcome::ErrorSent => {
                            return;
                        }
                    }
                }

                Ok(StreamNodeResult::Pause {
                    deltas: barrier_deltas,
                    node_name: barrier_name,
                    span_id,
                    timeout,
                    default_action,
                    ..
                }) => {
                    // Apply pre-pause deltas
                    self.apply_deltas(
                        &event_tx,
                        &mut reducer_registry,
                        &mut state,
                        &barrier_name,
                        &barrier_deltas,
                    )
                    .await;

                    let outcome = self
                        .handle_barrier(
                            &event_tx,
                            &graph,
                            &mut decision_rx,
                            &mut decision_registry,
                            &mut cancel_rx,
                            &mut reducer_registry,
                            node,
                            &current,
                            &mut state,
                            &mut execution_log,
                            &barrier_name,
                            span_id,
                            timeout,
                            default_action,
                            step,
                            node_start,
                            trace_id,
                        )
                        .await;

                    match outcome {
                        StepOutcome::Continue(target) => {
                            // 💾 Checkpoint: BarrierResolved 模式下保存
                            self.save_checkpoint_if_needed(
                                &event_tx,
                                &trace_id,
                                &target,
                                &state,
                                step,
                                CheckpointTrigger::BarrierResolved,
                                &mut snapshot_state,
                            )
                            .await;
                            current = target;
                        }
                        StepOutcome::Break => {
                            // 正常结束(到达 end 节点)
                            self.send_graph_complete(
                                &event_tx,
                                &state,
                                &execution_log,
                                start_time,
                                trace_id,
                                &mut snapshot_state,
                            )
                            .await;
                            return;
                        }
                        StepOutcome::ErrorSent => {
                            return;
                        }
                    }
                }

                Ok(StreamNodeResult::Fallback {
                    deltas: fallback_deltas,
                    reason,
                    node_name: fallback_node,
                }) => {
                    // Apply pre-fallback deltas
                    self.apply_deltas(
                        &event_tx,
                        &mut reducer_registry,
                        &mut state,
                        &fallback_node,
                        &fallback_deltas,
                    )
                    .await;

                    let outcome = self
                        .handle_fallback(
                            &event_tx,
                            &graph,
                            &current,
                            &mut state,
                            &mut execution_log,
                            &fallback_node,
                            &reason,
                            step,
                            node_start,
                            node_end,
                            trace_id,
                        )
                        .await;

                    match outcome {
                        StepOutcome::Continue(target) => {
                            current = target;
                        }
                        StepOutcome::ErrorSent => {
                            return;
                        }
                        StepOutcome::Break => {
                            // handle_fallback 不会返回 Break
                            unreachable!("handle_fallback only returns Continue or ErrorSent");
                        }
                    }
                }

                Err(e) => {
                    self.handle_error(
                        &event_tx,
                        &mut execution_log,
                        &node_name,
                        node_start,
                        node_end,
                        span_id,
                        step,
                        trace_id,
                        e,
                        &state,
                    )
                    .await;
                    return;
                }
            }
        }
    }

    /// 处理节点正常完成(`StreamNodeResult::Continue`)。
    ///
    /// 发送 NodeEnd 事件,记录执行日志,解析下一步路由。
    #[allow(clippy::too_many_arguments)]
    async fn handle_continue(
        &self,
        event_tx: &mpsc::Sender<GraphEvent>,
        graph: &Graph,
        current: &str,
        state: &mut State,
        execution_log: &mut Vec<ExecutionEntry>,
        next: NextStep,
        span_id: SpanId,
        observed: Option<ObservedError>,
        step: usize,
        node_name: &str,
        node_start: Instant,
        node_end: Instant,
        duration: std::time::Duration,
        trace_id: TraceId,
    ) -> StepOutcome {
        // 记录执行日志
        execution_log.push(ExecutionEntry {
            step,
            node_name: node_name.to_string(),
            start_time: node_start,
            end_time: node_end,
            success: true,
        });

        // 发送 NodeEnd 事件
        if self
            .send(
                event_tx,
                GraphEvent::NodeEnd {
                    node_name: node_name.to_string(),
                    trace_id,
                    span_id,
                    success: true,
                    duration,
                },
            )
            .await
        {
            return StepOutcome::Break;
        }

        // 如果有观测错误,发送 ObservedError 事件
        if let Some(error) = observed
            && self
                .send(
                    event_tx,
                    GraphEvent::ObservedError {
                        error,
                        node_name: node_name.to_string(),
                    },
                )
                .await
        {
            return StepOutcome::Break;
        }

        // 🛑 end 节点检查
        if current == graph.end_node() {
            return StepOutcome::Break;
        }

        // 解析下一步路由
        match self.resolve_next(graph, current, state, next) {
            Ok(target) => StepOutcome::Continue(target),
            Err(e) => {
                self.send_graph_error(event_tx, e, state, execution_log, Instant::now(), trace_id)
                    .await;
                StepOutcome::ErrorSent
            }
        }
    }

    /// 处理 Barrier 暂停(`StreamNodeResult::Pause`)。
    ///
    /// 发射 BarrierWaiting 事件,等待外部决策,应用决策结果。
    #[allow(clippy::too_many_arguments)]
    async fn handle_barrier(
        &self,
        event_tx: &mpsc::Sender<GraphEvent>,
        graph: &Graph,
        decision_rx: &mut mpsc::Receiver<BarrierDecisionMessage>,
        decision_registry: &mut DecisionRegistry,
        cancel_rx: &mut mpsc::Receiver<()>,
        reducer_registry: &mut ReducerRegistry,
        node: &NodeKind,
        current: &str,
        state: &mut State,
        execution_log: &mut Vec<ExecutionEntry>,
        barrier_name: &str,
        span_id: SpanId,
        timeout: Option<std::time::Duration>,
        default_action: BarrierDefaultAction,
        step: usize,
        node_start: Instant,
        trace_id: TraceId,
    ) -> StepOutcome {
        let barrier_id = decision_registry.next_id(barrier_name);

        // 发射 BarrierWaiting 事件
        if self
            .send(
                event_tx,
                GraphEvent::BarrierWaiting {
                    barrier_id: barrier_id.clone(),
                    node_name: barrier_name.to_string(),
                    span_id,
                },
            )
            .await
        {
            return StepOutcome::Break;
        }

        // 等待决策
        let decision = self
            .wait_barrier_decision(
                decision_rx,
                decision_registry,
                &barrier_id,
                timeout,
                &default_action,
                cancel_rx,
            )
            .await;

        // 检查取消信号
        if cancel_rx.try_recv().is_ok() {
            self.send_graph_error(
                event_tx,
                GraphError::Terminal(TerminalError::BarrierCancelled {
                    node: barrier_name.to_string(),
                }),
                state,
                execution_log,
                node_start,
                trace_id,
            )
            .await;
            return StepOutcome::ErrorSent;
        }

        // 发射 BarrierResolved 事件
        if self
            .send(
                event_tx,
                GraphEvent::BarrierResolved {
                    barrier_id: barrier_id.clone(),
                    decision: decision.clone(),
                },
            )
            .await
        {
            return StepOutcome::Break;
        }

        // 应用决策 — apply_decision 返回 (NextStep, Vec<StateDelta>)
        let (next, barrier_deltas) = match node {
            NodeKind::Barrier(b) => b.apply_decision(decision),
            _ => {
                self.send_graph_error(
                    event_tx,
                    GraphError::Terminal(TerminalError::InvalidGraph(
                        "expected BarrierNode but got unexpected node type for BarrierPaused"
                            .to_string(),
                    )),
                    state,
                    execution_log,
                    node_start,
                    trace_id,
                )
                .await;
                return StepOutcome::ErrorSent;
            }
        };

        // Apply decision deltas
        self.apply_deltas(
            event_tx,
            reducer_registry,
            state,
            barrier_name,
            &barrier_deltas,
        )
        .await;

        // 记录执行日志
        let end_time = Instant::now();
        execution_log.push(ExecutionEntry {
            step,
            node_name: barrier_name.to_string(),
            start_time: node_start,
            end_time,
            success: true,
        });

        // 发送 NodeEnd 事件
        if self
            .send(
                event_tx,
                GraphEvent::NodeEnd {
                    node_name: barrier_name.to_string(),
                    trace_id,
                    span_id,
                    success: true,
                    duration: end_time.duration_since(node_start),
                },
            )
            .await
        {
            return StepOutcome::Break;
        }

        // 🛑 end 节点检查
        if current == graph.end_node() {
            return StepOutcome::Break;
        }

        // 解析下一步路由
        match self.resolve_next(graph, current, state, next) {
            Ok(target) => StepOutcome::Continue(target),
            Err(e) => {
                self.send_graph_error(event_tx, e, state, execution_log, end_time, trace_id)
                    .await;
                StepOutcome::ErrorSent
            }
        }
    }

    // ─── handle_fallback ──────────────────────────────────────

    /// 处理节点 Fallback(`StreamNodeResult::Fallback`)。
    ///
    /// Fallback 是控制流 — 节点主动声明降级策略。
    #[allow(clippy::too_many_arguments)]
    async fn handle_fallback(
        &self,
        event_tx: &mpsc::Sender<GraphEvent>,
        graph: &Graph,
        current: &str,
        state: &mut State,
        execution_log: &mut Vec<ExecutionEntry>,
        fallback_node: &str,
        reason: &str,
        step: usize,
        node_start: Instant,
        node_end: Instant,
        trace_id: TraceId,
    ) -> StepOutcome {
        // 记录执行日志
        execution_log.push(ExecutionEntry {
            step,
            node_name: fallback_node.to_string(),
            start_time: node_start,
            end_time: node_end,
            success: false,
        });

        // 查找 fallback 边
        if let Some(fallback_target) = graph.find_fallback_edge(current) {
            // 发送降级 ObservedError 事件
            if self
                .send(
                    event_tx,
                    GraphEvent::ObservedError {
                        error: ObservedError::Degraded {
                            node: fallback_node.to_string(),
                            message: format!("fallback to '{}': {}", fallback_target, reason),
                        },
                        node_name: fallback_node.to_string(),
                    },
                )
                .await
            {
                return StepOutcome::ErrorSent;
            }
            StepOutcome::Continue(fallback_target)
        } else {
            // 无 fallback 边 → 终止
            self.send_graph_error(
                event_tx,
                GraphError::Terminal(TerminalError::NodeExecutionFailed {
                    node: fallback_node.to_string(),
                    source: format!("fallback with no fallback edge: {}", reason).into(),
                }),
                state,
                execution_log,
                node_end,
                trace_id,
            )
            .await;
            StepOutcome::ErrorSent
        }
    }

    // ─── handle_error ─────────────────────────────────────────

    /// 处理节点执行错误。
    #[allow(clippy::too_many_arguments)]
    async fn handle_error(
        &self,
        event_tx: &mpsc::Sender<GraphEvent>,
        execution_log: &mut Vec<ExecutionEntry>,
        node_name: &str,
        node_start: Instant,
        node_end: Instant,
        span_id: SpanId,
        step: usize,
        trace_id: TraceId,
        error: GraphError,
        state: &State,
    ) {
        let duration = node_end.duration_since(node_start);

        // 记录执行日志
        execution_log.push(ExecutionEntry {
            step,
            node_name: node_name.to_string(),
            start_time: node_start,
            end_time: node_end,
            success: false,
        });

        // 发送 NodeEnd (failure) 事件
        if self
            .send(
                event_tx,
                GraphEvent::NodeEnd {
                    node_name: node_name.to_string(),
                    trace_id,
                    span_id,
                    success: false,
                    duration,
                },
            )
            .await
        {
            return;
        }

        // 发送 GraphError 事件
        self.send_graph_error(event_tx, error, state, execution_log, node_end, trace_id)
            .await;
    }

    // ─── handle_parallel ──────────────────────────────────────

    /// 处理并行节点(`NodeKind::Parallel`)。
    ///
    /// Fork State 快照给每个分支,并发执行,收集所有 Delta 后合并。
    async fn handle_parallel(
        &self,
        node: &NodeKind,
        state: &State,
        event_tx: &mpsc::Sender<GraphEvent>,
        parent_span_id: SpanId,
        node_name: &str,
    ) -> Result<StreamNodeResult, GraphError> {
        let parallel = match node {
            NodeKind::Parallel(p) => p,
            _ => unreachable!("handle_parallel called on non-Parallel node"),
        };

        let branch_count = parallel.branch_count();
        let error_strategy = parallel.error_strategy();
        let display_name = parallel.label().unwrap_or(node_name).to_string();

        // 发射 ParallelStarted 事件
        if self
            .send(
                event_tx,
                GraphEvent::Node {
                    span_id: parent_span_id,
                    node_name: node_name.to_string(),
                    event: FlowEvent::ParallelStarted {
                        node_id: display_name.clone(),
                        branch_count,
                        span_id: parent_span_id,
                    },
                },
            )
            .await
        {
            return Err(GraphError::Terminal(TerminalError::InvalidGraph(
                "consumer dropped during parallel execution".into(),
            )));
        }

        let parallel_start = Instant::now();

        // Fork State 快照,spawn 所有分支
        let mut handles = Vec::with_capacity(branch_count);
        for (branch_name, branch_node) in parallel.branches_iter() {
            let state_copy = state.clone();
            let branch_node = branch_node.clone();
            let name = branch_name.to_string();

            let handle = tokio::spawn(async move {
                let branch_start = Instant::now();
                // 分支直接调用 execute(阻塞模式),不经过 stream
                // 因为 stream 会发射重复的 NodeStart/NodeEnd 事件
                let result = branch_node.execute(&state_copy).await;
                let branch_end = Instant::now();
                (name, result, branch_end.duration_since(branch_start))
            });

            handles.push(handle);
        }

        // 收集所有结果
        let mut all_deltas: Vec<StateDelta> = Vec::new();
        let mut first_error: Option<GraphError> = None;
        let mut any_failure = false;

        for handle in handles {
            let (branch_name, result, branch_duration) = match handle.await {
                Ok(res) => res,
                Err(join_err) => {
                    let err = GraphError::Terminal(TerminalError::NodeExecutionFailed {
                        node: format!("{}/{}", display_name, "<unknown>"),
                        source: join_err.into(),
                    });
                    // 发射 BranchCompleted (failure)
                    let _ = self
                        .send(
                            event_tx,
                            GraphEvent::Node {
                                span_id: parent_span_id,
                                node_name: node_name.to_string(),
                                event: FlowEvent::BranchCompleted {
                                    branch_name: "<unknown>".to_string(),
                                    node_id: display_name.clone(),
                                    span_id: SpanId::new(),
                                    success: false,
                                    duration: std::time::Duration::ZERO,
                                },
                            },
                        )
                        .await;

                    if matches!(error_strategy, ParallelErrorStrategy::FailFast) {
                        return Err(err);
                    }
                    first_error.get_or_insert(err);
                    any_failure = true;
                    continue;
                }
            };

            match result {
                Ok(output) => {
                    all_deltas.extend(output.deltas);

                    // 发射 BranchCompleted (success)
                    let _ = self
                        .send(
                            event_tx,
                            GraphEvent::Node {
                                span_id: parent_span_id,
                                node_name: node_name.to_string(),
                                event: FlowEvent::BranchCompleted {
                                    branch_name: branch_name.clone(),
                                    node_id: display_name.clone(),
                                    span_id: SpanId::new(),
                                    success: true,
                                    duration: branch_duration,
                                },
                            },
                        )
                        .await;
                }
                Err(e) => {
                    // 发射 BranchCompleted (failure)
                    let _ = self
                        .send(
                            event_tx,
                            GraphEvent::Node {
                                span_id: parent_span_id,
                                node_name: node_name.to_string(),
                                event: FlowEvent::BranchCompleted {
                                    branch_name: branch_name.clone(),
                                    node_id: display_name.clone(),
                                    span_id: SpanId::new(),
                                    success: false,
                                    duration: branch_duration,
                                },
                            },
                        )
                        .await;

                    if matches!(error_strategy, ParallelErrorStrategy::FailFast) {
                        return Err(e);
                    }
                    first_error.get_or_insert(e);
                    any_failure = true;
                }
            }
        }

        // 如果有失败且为 CollectAll 模式,返回错误
        if any_failure {
            return Err(first_error.unwrap());
        }

        // 合并所有 Delta — 使用 merge_deltas 处理多 writer 冲突
        // 注意:此处不直接 apply,返回给外层统一 apply
        // merge_deltas 用于验证冲突,实际 apply 由外层 handle_continue 完成

        // 发射 ParallelCompleted 事件
        let parallel_duration = parallel_start.elapsed();
        let _ = self
            .send(
                event_tx,
                GraphEvent::Node {
                    span_id: parent_span_id,
                    node_name: node_name.to_string(),
                    event: FlowEvent::ParallelCompleted {
                        node_id: display_name,
                        span_id: parent_span_id,
                        duration: parallel_duration,
                    },
                },
            )
            .await;

        Ok(StreamNodeResult::Continue {
            deltas: all_deltas,
            next: NextStep::GoToNext,
            span_id: parent_span_id,
            observed: None,
            metadata: None,
        })
    }

    // ─── 辅助方法 ──────────────────────────────────────────────

    /// 应用节点返回的 StateDelta 到 State,并发射 StateChanged 事件。
    async fn apply_deltas(
        &self,
        event_tx: &mpsc::Sender<GraphEvent>,
        reducer_registry: &mut ReducerRegistry,
        state: &mut State,
        node_name: &str,
        deltas: &[StateDelta],
    ) {
        for delta in deltas {
            // Apply delta to state
            if let Err(e) = reducer_registry.apply_delta(state, delta) {
                tracing::warn!(
                    node = %node_name,
                    key = %delta.key,
                    error = %e,
                    "failed to apply state delta"
                );
            }

            // 发射 StateChanged 事件
            let _ = self
                .send(
                    event_tx,
                    GraphEvent::Node {
                        span_id: SpanId::new(), // TODO: 使用节点的 span_id
                        node_name: node_name.to_string(),
                        event: FlowEvent::StateChanged {
                            node_id: node_name.to_string(),
                            delta: delta.clone(),
                        },
                    },
                )
                .await;
        }
    }

    /// 发送事件,返回 `true` 表示 consumer 已断开(应终止执行)。
    async fn send(&self, event_tx: &mpsc::Sender<GraphEvent>, event: GraphEvent) -> bool {
        event_tx.send(event).await.is_err()
    }

    /// 发送 GraphError 事件(携带 state 快照)。
    async fn send_graph_error(
        &self,
        event_tx: &mpsc::Sender<GraphEvent>,
        error: GraphError,
        state: &State,
        _execution_log: &Vec<ExecutionEntry>,
        _start_time: Instant,
        _trace_id: TraceId,
    ) {
        let _ = self
            .send(
                event_tx,
                GraphEvent::GraphError {
                    error,
                    state: state.clone(),
                },
            )
            .await;
    }

    /// 发送 GraphComplete 事件。
    async fn send_graph_complete(
        &self,
        event_tx: &mpsc::Sender<GraphEvent>,
        state: &State,
        execution_log: &[ExecutionEntry],
        start_time: Instant,
        trace_id: TraceId,
        snapshot_state: &mut IncrementalSnapshotState,
    ) {
        // 💾 Checkpoint: ExecutionCompleted 模式下保存最终状态
        if self.policy.should_checkpoint_on_completion()
            && let Some(store) = &self.store
        {
            // 生成增量快照
            let (base, deltas, current) = snapshot_state.snapshot(state);
            let ck = if let Some(base_state) = base {
                Checkpoint::with_snapshot(
                    trace_id,
                    &self.graph_hash,
                    "__complete__",
                    current,
                    base_state,
                    deltas,
                )
            } else if !deltas.is_empty() {
                Checkpoint::with_snapshot(
                    trace_id,
                    &self.graph_hash,
                    "__complete__",
                    current.clone(),
                    current,
                    deltas,
                )
            } else {
                Checkpoint::new(trace_id, &self.graph_hash, "__complete__", state.clone())
            };

            match store.save(&ck).await {
                Ok(()) => {
                    let _ = self
                        .send(
                            event_tx,
                            GraphEvent::CheckpointSaved {
                                checkpoint_id: ck.checkpoint_id.clone(),
                                node_name: "__complete__".to_string(),
                                step: execution_log.len(),
                            },
                        )
                        .await;
                    tracing::debug!(
                        checkpoint = %ck.checkpoint_id,
                        "final checkpoint saved on completion"
                    );
                }
                Err(e) => {
                    tracing::warn!(error = %e, "final checkpoint save failed");
                }
            }
        }

        let _ = self
            .send(
                event_tx,
                GraphEvent::GraphComplete {
                    result: GraphResult {
                        trace_id,
                        state: state.clone(),
                        execution_log: execution_log.to_vec(),
                        duration: start_time.elapsed(),
                    },
                },
            )
            .await;
    }

    // ─── 等待 Barrier 决策 ─────────────────────────────────────

    /// 等待 Barrier 决策(支持取消信号)。
    async fn wait_barrier_decision(
        &self,
        decision_rx: &mut mpsc::Receiver<BarrierDecisionMessage>,
        registry: &mut DecisionRegistry,
        target_id: &BarrierId,
        timeout: Option<std::time::Duration>,
        default_action: &BarrierDefaultAction,
        cancel_rx: &mut mpsc::Receiver<()>,
    ) -> BarrierDecision {
        if let Some(decision) = registry.take(target_id) {
            return decision;
        }

        while let Ok(msg) = decision_rx.try_recv() {
            if let Some(decision) = registry.process_message(msg, target_id) {
                return decision;
            }
        }

        if cancel_rx.try_recv().is_ok() {
            return Self::default_decision(default_action);
        }

        if let Some(timeout) = timeout {
            let start = std::time::Instant::now();
            loop {
                match tokio::time::timeout(std::time::Duration::from_millis(50), decision_rx.recv())
                    .await
                {
                    Ok(Some(msg)) => {
                        if let Some(decision) = registry.process_message(msg, target_id) {
                            return decision;
                        }
                    }
                    Ok(None) => return Self::default_decision(default_action),
                    Err(_) => {}
                }
                if cancel_rx.try_recv().is_ok() {
                    return Self::default_decision(default_action);
                }
                if start.elapsed() >= timeout {
                    return Self::default_decision(default_action);
                }
            }
        } else {
            loop {
                if let Some(msg) = decision_rx.recv().await {
                    if let Some(decision) = registry.process_message(msg, target_id) {
                        return decision;
                    }
                } else {
                    return Self::default_decision(default_action);
                }
                if cancel_rx.try_recv().is_ok() {
                    return Self::default_decision(default_action);
                }
            }
        }
    }

    fn default_decision(action: &BarrierDefaultAction) -> BarrierDecision {
        match action {
            BarrierDefaultAction::Approve => BarrierDecision::Approve,
            BarrierDefaultAction::Reject => BarrierDecision::Reject {
                reason: "timeout — no decision received".into(),
            },
            BarrierDefaultAction::Skip => BarrierDecision::Approve,
        }
    }

    // ─── 路由解析 ──────────────────────────────────────────────

    /// 解析 NextStep 为目标节点名称。
    fn resolve_next(
        &self,
        graph: &Graph,
        current: &str,
        state: &mut State,
        next: NextStep,
    ) -> Result<String, GraphError> {
        match next {
            NextStep::Goto(target) => {
                graph.find_edge(current, &target).ok_or_else(|| {
                    GraphError::Terminal(TerminalError::MissingEdge {
                        from: current.to_string(),
                        to: target.clone(),
                    })
                })?;
                Ok(target)
            }
            NextStep::GoToNext => Self::find_next_node(graph, current, state),
            NextStep::End => Err(GraphError::Terminal(TerminalError::InvalidGraph(
                "unexpected End next step".into(),
            ))),
        }
    }

    /// 查找下一个节点(三类边 + 有序路由)。
    fn find_next_node(graph: &Graph, current: &str, state: &State) -> Result<String, GraphError> {
        let edges = graph.edges_from(current);

        if edges.is_empty() {
            return Err(GraphError::Terminal(TerminalError::InvalidGraph(format!(
                "node '{}' has no outgoing edges and is not the end node",
                current
            ))));
        }

        // 1. 条件边 — 按注册顺序求值,first match wins
        for edge in &edges {
            if edge.is_conditional() && edge.condition.as_ref().is_some_and(|c| c(state)) {
                return Ok(edge.to.clone());
            }
        }

        // 2. 普通边 — 无条件非 fallback,取第一条
        for edge in &edges {
            if edge.is_normal() {
                return Ok(edge.to.clone());
            }
        }

        // 3. Fallback 边 — 无条件 fallback,取第一条
        for edge in &edges {
            if edge.fallback {
                return Ok(edge.to.clone());
            }
        }

        // 4. 无匹配 → Unrouted
        let attempted: Vec<crate::error::ConditionEval> = edges
            .iter()
            .map(|e| crate::error::ConditionEval {
                edge: format!("{}{}", e.from, e.to),
                condition: e.condition.as_ref().map(|_| "condition".to_string()),
                matched: e.condition.as_ref().is_some_and(|c| c(state)),
            })
            .collect();

        Err(GraphError::Terminal(TerminalError::Unrouted {
            node: current.to_string(),
            attempted_conditions: attempted,
        }))
    }

    // ─── Checkpoint 方法 ──────────────────────────────────────

    /// 根据策略判断是否需要保存 Checkpoint,如需则保存。
    ///
    /// - `BarrierResolved` — Barrier 合并后保存
    /// - `ExecutionCompleted` — 执行完成时保存
    /// - `HumanDecision` — 人类决策后保存
    /// - `Explicit` — 显式触发时保存
    /// - `Adaptive(metadata)` — 基于 ExecutionMetadata 动态决策,编译器保证 metadata 存在
    #[allow(clippy::too_many_arguments)]
    async fn save_checkpoint_if_needed(
        &self,
        event_tx: &mpsc::Sender<GraphEvent>,
        trace_id: &TraceId,
        next_node: &str,
        state: &State,
        step: usize,
        trigger: CheckpointTrigger,
        snapshot_state: &mut IncrementalSnapshotState,
    ) {
        // 检查该 trigger 是否启用
        let should_save = match &trigger {
            CheckpointTrigger::BarrierResolved => self.policy.should_checkpoint_on_barrier(),
            CheckpointTrigger::ExecutionCompleted => self.policy.should_checkpoint_on_completion(),
            CheckpointTrigger::HumanDecision => self.policy.should_checkpoint_on_human_decision(),
            CheckpointTrigger::Explicit => self.policy.should_checkpoint_on_explicit(),
            CheckpointTrigger::Adaptive(metadata) => {
                // Adaptive 模式:metadata 由调用者保证存在,直接评分
                self.checkpoint_score.should_checkpoint(metadata)
            }
        };

        if !should_save {
            return;
        }

        let store = match &self.store {
            Some(s) => s,
            None => return,
        };

        // 生成增量快照
        let (base, deltas, current) = snapshot_state.snapshot(state);
        let ck = if let Some(base_state) = base {
            // 有 base — 创建增量快照
            Checkpoint::with_snapshot(
                *trace_id,
                &self.graph_hash,
                next_node,
                current,
                base_state,
                deltas,
            )
        } else if !deltas.is_empty() {
            // 有 deltas 但无 base — 使用当前 State 作为 base
            Checkpoint::with_snapshot(
                *trace_id,
                &self.graph_hash,
                next_node,
                current.clone(),
                current,
                deltas,
            )
        } else {
            // 无 base 无 deltas — 全量快照
            Checkpoint::new(*trace_id, &self.graph_hash, next_node, state.clone())
        };

        match store.save(&ck).await {
            Ok(()) => {
                // 更新 snapshot state 的 base
                snapshot_state.base_state = Some(state.clone());
                snapshot_state.clear_pending();

                let _ = self
                    .send(
                        event_tx,
                        GraphEvent::CheckpointSaved {
                            checkpoint_id: ck.checkpoint_id.clone(),
                            node_name: next_node.to_string(),
                            step,
                        },
                    )
                    .await;
                tracing::debug!(
                    checkpoint = %ck.checkpoint_id,
                    node = %next_node,
                    step,
                    "checkpoint saved"
                );
            }
            Err(e) => {
                tracing::warn!(error = %e, node = %next_node, step, "checkpoint save failed");
            }
        }
    }

    /// 从 Checkpoint 恢复执行。
    ///
    /// 1. 加载 trace_id 对应的最新 Checkpoint
    /// 2. 校验 `graph_hash`(Strict 模式)
    /// 3. 从 `current_node` 继续执行
    ///
    /// # 参数
    /// - `store` — Checkpoint 存储后端
    /// - `trace_id` — 要恢复的执行 Trace ID
    /// - `graph` — 当前图定义(用于 hash 校验)
    ///
    /// # 错误
    /// - Checkpoint 不存在 → `NotFound`
    /// - 图结构已变更 → `InvalidGraph`(Strict 模式)
    pub async fn resume_from(
        &self,
        store: &dyn CheckpointStore,
        trace_id: &TraceId,
        graph: &std::sync::Arc<Graph>,
    ) -> Result<GraphExecution, GraphError> {
        // 加载最新 Checkpoint
        let checkpoint = store
            .load_latest(trace_id)
            .await
            .map_err(|e| {
                GraphError::Terminal(TerminalError::InvalidGraph(format!(
                    "failed to load checkpoint: {}",
                    e
                )))
            })?
            .ok_or_else(|| {
                GraphError::Terminal(TerminalError::InvalidGraph(format!(
                    "no checkpoint found for trace {}",
                    trace_id
                )))
            })?;

        // 校验图结构指纹
        let current_hash = graph.hash();
        if checkpoint.graph_hash != current_hash {
            tracing::warn!(
                saved_hash = %checkpoint.graph_hash,
                current_hash = %current_hash,
                "graph structure has changed since checkpoint — resuming anyway (Force mode)",
            );
            // v0.4 暂不实现 Strict 拒绝,仅 warn
            // TODO(v0.4): 支持 GraphHashMode::Strict 拒绝恢复
        }

        // 构建带 Checkpoint 的执行器
        let executor = Self {
            max_steps: self.max_steps,
            store: self.store.clone(),
            policy: self.policy.clone(),
            graph_hash: current_hash,
            pending_reducers: self.pending_reducers.clone(),
            checkpoint_score: self.checkpoint_score.clone(),
            // ⚠️ resume 路径暂时使用 restore_state_simple()
            // 因为 ReducerRegistry 在 run_loop 中才创建
            // 对于单分支场景是安全的,并行场景需要后续优化
            last_checkpoint_state: Some(checkpoint.restore_state_simple()),
            pending_deltas: Vec::new(),
            delta_compact_threshold: self.delta_compact_threshold,
        };

        // 从 Checkpoint 的 current_node 继续
        let initial_state = checkpoint.state.clone();

        // 覆盖 graph start — 从 checkpoint 的节点继续
        // 注意:run_loop 会从 graph.start_node() 开始,但我们需要从 current_node 开始
        // 所以这里需要特殊处理
        let execution = executor.execute_stream(graph.clone(), initial_state);

        // ⚠️ 限制:resume_from 目前从 start_node 重新开始,
        // 但携带 checkpoint 的 state。完整 resume(从 current_node 继续)
        // 需要 run_loop 支持自定义起始节点,v0.4 后续迭代实现。
        Ok(execution)
    }
}