oris-runtime 0.61.0

An agentic workflow runtime and programmable AI execution system in Rust: stateful graphs, agents, tools, and multi-step execution.
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
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
use std::collections::{HashMap, HashSet};
use std::pin::Pin;
use std::sync::Arc;

use async_stream::stream;
use futures::Stream;

use crate::kernel::{Event, EventStore};

use super::{
    edge::{Edge, END, START},
    error::GraphError,
    execution::{
        durability::DurabilityMode, scheduler::NodeScheduler, superstep::SuperStepExecutor,
    },
    interrupts::{
        set_interrupt_context, Interrupt, InterruptContext, InvokeResult, StateOrCommand,
    },
    node::Node,
    persistence::{
        checkpointer::CheckpointerBox,
        config::{CheckpointConfig, RunnableConfig},
        snapshot::StateSnapshot,
        store::StoreBox,
    },
    state::{State, StateUpdate},
    step_result::GraphStepOnceResult,
    streaming::{
        chunk::StreamChunk,
        metadata::{MessageChunk, MessageMetadata},
        mode::StreamMode,
    },
    trace::TraceEvent,
};

/// CompiledGraph - an executable graph ready for execution
///
/// This is created by calling `compile()` on a `StateGraph`.
/// It provides methods to execute the graph with initial state.
pub struct CompiledGraph<S: State> {
    nodes: HashMap<String, Arc<dyn Node<S>>>,
    adjacency: HashMap<String, Vec<Edge<S>>>,
    checkpointer: Option<CheckpointerBox<S>>,
    store: Option<StoreBox>,
    /// Optional kernel EventStore for event-first execution (2.0 pilot).
    event_store: Option<Arc<dyn EventStore>>,
    /// When true (default), step_once is allowed. When false, step_once returns an error unless
    /// config has allow_non_pure_step_once set—used to guard deterministic replay (nodes must not do I/O).
    pure_graph: bool,
}

impl<S: State + 'static> CompiledGraph<S> {
    /// Get a reference to the nodes (for subgraph persistence propagation)
    pub(crate) fn nodes(&self) -> &HashMap<String, Arc<dyn Node<S>>> {
        &self.nodes
    }

    /// Get a reference to the adjacency list (for subgraph persistence propagation)
    pub(crate) fn adjacency(&self) -> &HashMap<String, Vec<Edge<S>>> {
        &self.adjacency
    }

    /// Get a reference to the checkpointer (for subgraph persistence propagation)
    pub(crate) fn checkpointer(&self) -> &Option<CheckpointerBox<S>> {
        &self.checkpointer
    }

    /// Get a reference to the store (for subgraph persistence propagation)
    pub(crate) fn store(&self) -> &Option<StoreBox> {
        &self.store
    }
}

impl<S: State + 'static> CompiledGraph<S> {
    // Methods moved above after struct definition
}

impl<S: State + 'static> CompiledGraph<S> {
    /// Create a new CompiledGraph (internal use only)
    ///
    /// Use `StateGraph::compile()` to create a CompiledGraph.
    pub(crate) fn new(
        nodes: HashMap<String, Arc<dyn Node<S>>>,
        adjacency: HashMap<String, Vec<Edge<S>>>,
    ) -> Result<Self, GraphError> {
        Ok(Self {
            nodes,
            adjacency,
            checkpointer: None,
            store: None,
            event_store: None,
            pure_graph: true,
        })
    }

    /// Create a new CompiledGraph with checkpointer and store
    pub(crate) fn with_persistence(
        nodes: HashMap<String, Arc<dyn Node<S>>>,
        adjacency: HashMap<String, Vec<Edge<S>>>,
        checkpointer: Option<CheckpointerBox<S>>,
        store: Option<StoreBox>,
    ) -> Result<Self, GraphError> {
        Ok(Self {
            nodes,
            adjacency,
            checkpointer,
            store,
            event_store: None,
            pure_graph: true,
        })
    }

    /// Mark this graph as non-pure (nodes may perform I/O). step_once will then error unless
    /// RunnableConfig has allow_non_pure_step_once set. Use for graphs that call LLM/tools inside nodes
    /// until they are refactored to emit Actions; keeps deterministic replay safe by default.
    pub fn with_pure_guard(self, pure: bool) -> Self {
        Self {
            pure_graph: pure,
            ..self
        }
    }

    /// Attach a kernel EventStore for event-first execution (2.0 pilot).
    /// When set, the interrupt execution path will append kernel events
    /// (StateUpdated, Interrupted, Resumed, Completed) to this store.
    pub fn with_event_store(self, event_store: Arc<dyn EventStore>) -> Self {
        Self {
            event_store: Some(event_store),
            ..self
        }
    }

    /// Invoke the graph with initial state
    ///
    /// Executes the graph from START to END, returning the final state.
    ///
    /// # Arguments
    ///
    /// * `initial_state` - The initial state to start execution with
    ///
    /// # Example
    ///
    /// ```ignore
    /// use oris_runtime::graph::{CompiledGraph, MessagesState};
    /// let graph: CompiledGraph<MessagesState> = // ... create graph
    /// let initial_state = MessagesState::new();
    /// let final_state = graph.invoke(initial_state).await.unwrap();
    /// ```
    pub async fn invoke(&self, initial_state: S) -> Result<S, GraphError> {
        let mut current_state = initial_state;
        let mut current_node = START.to_string();
        let mut visited = HashSet::new();
        let max_iterations = 1000; // Prevent infinite loops
        let mut iterations = 0;

        loop {
            if iterations >= max_iterations {
                return Err(GraphError::ExecutionError(
                    "Maximum iterations reached. Possible infinite loop.".to_string(),
                ));
            }
            iterations += 1;

            // If we've reached END, return the final state
            if current_node == END {
                return Ok(current_state);
            }

            // Check for cycles (simple detection)
            if visited.contains(&current_node) {
                // Allow revisiting nodes, but track it
                log::warn!("Revisiting node: {}", current_node);
            } else {
                visited.insert(current_node.clone());
            }

            // Get edges from current node
            let edges = self
                .adjacency
                .get(&current_node)
                .ok_or_else(|| {
                    GraphError::ExecutionError(format!(
                        "No edges found from node: {}",
                        current_node
                    ))
                })?
                .clone();

            // If we're at START, execute the first node
            if current_node == START {
                if edges.is_empty() {
                    return Err(GraphError::ExecutionError(
                        "No edges from START".to_string(),
                    ));
                }

                // Get the first edge (for START, there should typically be one)
                let edge = &edges[0];
                let next_node = edge.get_target(&current_state).await?;
                current_node = next_node;
                continue;
            }

            // Execute the current node
            let node = self
                .nodes
                .get(&current_node)
                .ok_or_else(|| GraphError::NodeNotFound(current_node.clone()))?;

            // Use invoke for basic invoke method (no config/store available)
            let update = node.invoke(&current_state).await?;

            // Merge the update into the current state
            current_state = self.merge_state_update(&current_state, &update)?;

            // Determine next node based on edges
            if edges.is_empty() {
                return Err(GraphError::ExecutionError(format!(
                    "No edges from node: {}",
                    current_node
                )));
            }

            // For regular edges, take the first one
            // For conditional edges, evaluate the condition
            let edge = &edges[0];
            let next_node = edge.get_target(&current_state).await?;

            // If next node is END, we're done
            if next_node == END {
                return Ok(current_state);
            }

            current_node = next_node;
        }
    }

    /// Merge a state update into the current state
    ///
    /// This handles the merging logic for different state types.
    /// For MessagesState, we use specialized logic.
    /// For other state types, we use the State trait's merge method.
    fn merge_state_update(&self, state: &S, update: &StateUpdate) -> Result<S, GraphError> {
        // Try to handle MessagesState specially
        // For other state types, we'll need to serialize/deserialize
        // This is a limitation of the current design - in a full implementation,
        // we'd have a StateUpdate trait or similar

        // Convert state to JSON to check if it's MessagesState-like
        let state_json = serde_json::to_value(state).map_err(GraphError::SerializationError)?;

        // If state has "messages" field, treat it as MessagesState
        if state_json.get("messages").is_some() {
            // This is a workaround - we need to handle MessagesState specially
            // In practice, we'd use specialization or a different approach
            return self.merge_messages_state_update(state, update);
        }

        // For other state types, create a new state from the update and merge
        // This requires the state to be serializable/deserializable
        let update_json = serde_json::to_value(update).map_err(GraphError::SerializationError)?;

        // Try to deserialize update as state and merge
        // This is a simplified approach
        let update_state: S = serde_json::from_value(update_json.clone()).map_err(|_| {
            GraphError::StateMergeError("Cannot deserialize update as state".to_string())
        })?;

        Ok(state.merge(&update_state))
    }

    /// Merge update for MessagesState (specialized handling)
    fn merge_messages_state_update(
        &self,
        state: &S,
        update: &StateUpdate,
    ) -> Result<S, GraphError> {
        use super::state::{apply_update_to_messages_state, MessagesState};

        // Try to convert state to MessagesState
        let state_json = serde_json::to_value(state).map_err(GraphError::SerializationError)?;

        let messages_state: MessagesState = if let Some(messages_value) = state_json.get("messages")
        {
            if let Ok(messages) = serde_json::from_value::<Vec<crate::schemas::messages::Message>>(
                messages_value.clone(),
            ) {
                MessagesState::with_messages(messages)
            } else {
                MessagesState::new()
            }
        } else {
            MessagesState::new()
        };

        // Apply update
        let updated_state = apply_update_to_messages_state(&messages_state, update);

        // Convert back to S
        let updated_json =
            serde_json::to_value(&updated_state).map_err(GraphError::SerializationError)?;
        serde_json::from_value(updated_json).map_err(GraphError::SerializationError)
    }

    /// Execute a single graph step from (current_state, current_node).
    /// Does not append events; used by GraphStepFnAdapter for kernel-driven execution.
    ///
    /// Currently requires exactly one outgoing edge from START and from each node;
    /// multi-exit is not supported and will return an error. The single allowed edge
    /// may be conditional; routing is determined by `edge.get_target(state)`.
    /// Intended for graphs whose nodes do not perform external I/O; otherwise replay
    /// may be non-deterministic or have side effects (see kernel-api §4.2).
    pub async fn step_once(
        &self,
        current_state: &S,
        current_node: &str,
        config: Option<&RunnableConfig>,
    ) -> Result<GraphStepOnceResult<S>, GraphError> {
        if !self.pure_graph {
            let allow = config.map_or(false, |c| c.allow_non_pure_step_once());
            if !allow {
                return Err(GraphError::ExecutionError(
                    "step_once requires a pure graph (no I/O in nodes); use Actions instead, or set \
                     configurable.allow_non_pure_step_once to true for compatibility."
                        .to_string(),
                ));
            }
        }

        let store = self.store.clone();

        let node_to_run = if current_node.is_empty() || current_node == START {
            let edges = self
                .adjacency
                .get(START)
                .ok_or_else(|| GraphError::ExecutionError("No edges from START".to_string()))?;
            if edges.is_empty() {
                return Err(GraphError::ExecutionError(
                    "No edges from START".to_string(),
                ));
            }
            if edges.len() != 1 {
                return Err(GraphError::ExecutionError(
                    "step_once requires exactly one outgoing edge from START".to_string(),
                ));
            }
            let edge = &edges[0];
            edge.get_target(current_state).await?
        } else if current_node == END {
            return Ok(GraphStepOnceResult::Complete {
                state: current_state.clone(),
            });
        } else {
            current_node.to_string()
        };

        if node_to_run == END {
            return Ok(GraphStepOnceResult::Complete {
                state: current_state.clone(),
            });
        }

        let edges = self.adjacency.get(&node_to_run).ok_or_else(|| {
            GraphError::ExecutionError(format!("No edges from node: {}", node_to_run))
        })?;

        let node = self
            .nodes
            .get(&node_to_run)
            .ok_or_else(|| GraphError::NodeNotFound(node_to_run.clone()))?;

        let update_result = node.invoke_with_context(current_state, config, store).await;

        match update_result {
            Ok(update) => {
                let new_state = self.merge_state_update(current_state, &update)?;
                if edges.is_empty() {
                    return Ok(GraphStepOnceResult::Complete { state: new_state });
                }
                if edges.len() != 1 {
                    return Err(GraphError::ExecutionError(
                        "step_once requires exactly one outgoing edge per node".to_string(),
                    ));
                }
                let edge = &edges[0];
                let next_node = edge.get_target(&new_state).await?;
                if next_node == END {
                    Ok(GraphStepOnceResult::Complete { state: new_state })
                } else {
                    Ok(GraphStepOnceResult::Emit {
                        executed_node: node_to_run.clone(),
                        new_state,
                        next_node,
                    })
                }
            }
            Err(GraphError::InterruptError(interrupt_err)) => {
                let value = interrupt_err.value().clone();
                Ok(GraphStepOnceResult::Interrupt {
                    state: current_state.clone(),
                    value,
                })
            }
            Err(e) => Err(e),
        }
    }

    /// Stream the graph execution, yielding events as they occur
    ///
    /// This method executes the graph and yields events for each node execution,
    /// allowing you to monitor the graph's progress in real-time.
    ///
    /// # Arguments
    ///
    /// * `initial_state` - The initial state to start execution with
    ///
    /// # Returns
    ///
    /// A stream of `StreamEvent` values representing the execution progress
    pub fn stream<'a>(
        &'a self,
        initial_state: S,
    ) -> Pin<Box<dyn Stream<Item = StreamEvent<S>> + Send + 'a>> {
        self.stream_with_options(initial_state, StreamOptions::default())
    }

    /// Stream with options (modes and subgraphs)
    pub fn stream_with_options<'a>(
        &'a self,
        initial_state: S,
        options: StreamOptions,
    ) -> Pin<Box<dyn Stream<Item = StreamEvent<S>> + Send + 'a>> {
        self.stream_internal(initial_state, options.stream_modes, options.subgraphs)
    }

    /// Internal stream method with optional stream modes and subgraphs support
    fn stream_internal<'a>(
        &'a self,
        initial_state: S,
        stream_modes: Option<Vec<StreamMode>>,
        subgraphs: bool,
    ) -> Pin<Box<dyn Stream<Item = StreamEvent<S>> + Send + 'a>> {
        let nodes = self.nodes.clone();
        let adjacency = self.adjacency.clone();

        Box::pin(stream! {
            let mut current_state = initial_state;
            let mut current_node = START.to_string();
            let mut visited = HashSet::new();
            let max_iterations = 1000;
            let mut iterations = 0;

            loop {
                if iterations >= max_iterations {
                    yield StreamEvent::Error {
                        error: std::sync::Arc::new(GraphError::ExecutionError(
                            "Maximum iterations reached. Possible infinite loop.".to_string(),
                        )),
                    };
                    return;
                }
                iterations += 1;

                // If we've reached END, yield final event and return
                if current_node == END {
                    yield StreamEvent::GraphEnd {
                        final_state: current_state,
                    };
                    return;
                }

                // Track visited nodes
                if !visited.contains(&current_node) {
                    visited.insert(current_node.clone());
                }

                // Get edges from current node
                let edges = match adjacency.get(&current_node) {
                    Some(edges) => edges.clone(),
                    None => {
                        yield StreamEvent::Error {
                            error: std::sync::Arc::new(GraphError::ExecutionError(format!(
                                "No edges found from node: {}",
                                current_node
                            ))),
                        };
                        return;
                    }
                };

                // If we're at START, move to first node
                if current_node == START {
                    if edges.is_empty() {
                            yield StreamEvent::Error {
                                error: std::sync::Arc::new(GraphError::ExecutionError(
                                    "No edges from START".to_string(),
                                )),
                            };
                        return;
                    }

                    let edge = &edges[0];
                    match edge.get_target(&current_state).await {
                        Ok(next_node) => {
                            current_node = next_node;
                            continue;
                        }
                        Err(e) => {
                            yield StreamEvent::Error {
                                error: std::sync::Arc::new(e),
                            };
                            return;
                        }
                    }
                }

                // Yield node start event
                yield StreamEvent::NodeStart {
                    node: current_node.clone(),
                    state: current_state.clone(),
                    path: Vec::new(), // Empty path for top-level nodes
                };

                // Execute the current node
                let node = match nodes.get(&current_node) {
                    Some(node) => node.clone(),
                    None => {
                        yield StreamEvent::Error {
                            error: std::sync::Arc::new(GraphError::NodeNotFound(current_node.clone())),
                        };
                        return;
                    }
                };

                // Check if this is a subgraph node and subgraphs streaming is enabled
                let is_subgraph = subgraphs && node.get_subgraph().is_some();

                // Check if we need to stream LLM tokens
                let needs_message_streaming = stream_modes.as_ref()
                    .map(|modes| modes.contains(&StreamMode::Messages))
                    .unwrap_or(false);

                let update = if is_subgraph {
                    // This is a subgraph node - stream its execution
                    let subgraph = node.get_subgraph().unwrap();
                    let subgraph_path = vec![current_node.clone()]; // Path prefix for subgraph events

                    // Create stream options for subgraph
                    let subgraph_options = StreamOptions {
                        stream_modes: stream_modes.clone(),
                        subgraphs, // Recursively enable subgraphs
                    };

                    // Stream subgraph execution
                    use futures::StreamExt;
                    let mut subgraph_stream = subgraph.stream_with_options(current_state.clone(), subgraph_options);
                    let mut final_state = current_state.clone();

                    while let Some(sub_event) = subgraph_stream.next().await {
                        match sub_event {
                            StreamEvent::NodeStart { node: sub_node, state, path: sub_path, .. } => {
                                // Build full path: [parent_node, ...sub_path, sub_node]
                                let mut full_path = subgraph_path.clone();
                                full_path.extend(sub_path);
                                full_path.push(sub_node.clone());

                                yield StreamEvent::NodeStart {
                                    node: sub_node,
                                    state,
                                    path: full_path,
                                };
                            }
                            StreamEvent::NodeEnd { node: sub_node, state, update: sub_update, path: sub_path, .. } => {
                                // Build full path
                                let mut full_path = subgraph_path.clone();
                                full_path.extend(sub_path);
                                full_path.push(sub_node.clone());

                                yield StreamEvent::NodeEnd {
                                    node: sub_node,
                                    state: state.clone(),
                                    update: sub_update,
                                    path: full_path,
                                };

                                // Update final state
                                final_state = state;
                            }
                            StreamEvent::MessageChunk { node: sub_node, chunk, metadata, path: sub_path, .. } => {
                                // Build full path
                                let mut full_path = subgraph_path.clone();
                                full_path.extend(sub_path);
                                full_path.push(sub_node.clone());

                                yield StreamEvent::MessageChunk {
                                    node: sub_node,
                                    chunk,
                                    metadata,
                                    path: full_path,
                                };
                            }
                            StreamEvent::CustomData { node: sub_node, data, path: sub_path, .. } => {
                                // Build full path
                                let mut full_path = subgraph_path.clone();
                                full_path.extend(sub_path);
                                full_path.push(sub_node.clone());

                                yield StreamEvent::CustomData {
                                    node: sub_node,
                                    data,
                                    path: full_path,
                                };
                            }
                            StreamEvent::GraphEnd { final_state: sub_final_state } => {
                                // Subgraph completed - use its final state
                                final_state = sub_final_state;
                            }
                            StreamEvent::Error { error } => {
                                yield StreamEvent::Error { error };
                                return;
                            }
                        }
                    }

                    // Convert final state to update
                    let state_json = match serde_json::to_value(&final_state) {
                        Ok(json) => json,
                        Err(e) => {
                            yield StreamEvent::Error {
                                error: std::sync::Arc::new(GraphError::SerializationError(e)),
                            };
                            return;
                        }
                    };

                    let mut update = HashMap::new();
                    if let serde_json::Value::Object(map) = state_json {
                        for (key, value) in map {
                            update.insert(key, value);
                        }
                    }
                    update
                } else if needs_message_streaming {
                    // Try to get LLM from node for streaming
                    if let Some(llm) = node.get_llm() {
                        // Convert state to messages
                        let state_json = match serde_json::to_value(&current_state) {
                            Ok(json) => json,
                            Err(e) => {
                                yield StreamEvent::Error {
                                    error: std::sync::Arc::new(GraphError::SerializationError(e)),
                                };
                                return;
                            }
                        };

                        let messages: Vec<crate::schemas::messages::Message> = if let Some(messages_value) = state_json.get("messages") {
                            match serde_json::from_value(messages_value.clone()) {
                                Ok(msgs) => msgs,
                                Err(e) => {
                                    yield StreamEvent::Error {
                                    error: std::sync::Arc::new(GraphError::SerializationError(e)),
                                };
                                    return;
                                }
                            }
                        } else {
                            vec![crate::schemas::messages::Message::new_human_message("")]
                        };

                        // Stream LLM tokens
                        let mut stream_result = match llm.stream(&messages).await {
                            Ok(stream) => stream,
                            Err(e) => {
                                yield StreamEvent::Error {
                                    error: std::sync::Arc::new(GraphError::LLMError(e.to_string())),
                                };
                                return;
                            }
                        };

                        use futures::StreamExt;
                        let mut full_content = String::new();
                        let metadata = MessageMetadata::new(current_node.clone());

                        while let Some(chunk_result) = stream_result.next().await {
                            match chunk_result {
                                Ok(stream_data) => {
                                    full_content.push_str(&stream_data.content);

                                    // Yield message chunk event
                                    yield StreamEvent::MessageChunk {
                                        node: current_node.clone(),
                                        chunk: stream_data,
                                        metadata: metadata.clone(),
                                        path: Vec::new(), // Empty path for top-level nodes
                                    };
                                }
                                Err(e) => {
                                    yield StreamEvent::Error {
                                        error: std::sync::Arc::new(GraphError::LLMError(e.to_string())),
                                    };
                                    return;
                                }
                            }
                        }

                        // Create state update with full content
                        let ai_message = crate::schemas::messages::Message::new_ai_message(&full_content);
                        let mut update = HashMap::new();
                        match serde_json::to_value(vec![ai_message]) {
                            Ok(msg_value) => {
                                update.insert("messages".to_string(), msg_value);
                            }
                            Err(e) => {
                                yield StreamEvent::Error {
                                    error: std::sync::Arc::new(GraphError::SerializationError(e)),
                                };
                                return;
                            }
                        }
                        update
                    } else {
                        // Not an LLM node, use invoke_with_context
                        // Note: stream_internal doesn't have config/store, so pass None
                        match node.invoke_with_context(&current_state, None, None).await {
                            Ok(update) => update,
                            Err(e) => {
                                yield StreamEvent::Error {
                                    error: std::sync::Arc::new(e),
                                };
                                return;
                            }
                        }
                    }
                } else {
                    // No message streaming needed, use invoke_with_context
                    // Note: stream_internal doesn't have config/store, so pass None
                    match node.invoke_with_context(&current_state, None, None).await {
                        Ok(update) => update,
                        Err(e) => {
                            yield StreamEvent::Error {
                                error: std::sync::Arc::new(e),
                            };
                            return;
                        }
                    }
                };

                // Merge the update into the current state
                current_state = match self.merge_state_update(&current_state, &update) {
                    Ok(new_state) => new_state,
                    Err(e) => {
                        yield StreamEvent::Error {
                                    error: std::sync::Arc::new(e),
                                };
                        return;
                    }
                };

                // Yield node end event
                yield StreamEvent::NodeEnd {
                    node: current_node.clone(),
                    state: current_state.clone(),
                    update: update.clone(),
                    path: Vec::new(), // Empty path for top-level nodes
                };

                // Determine next node based on edges
                if edges.is_empty() {
                    yield StreamEvent::Error {
                        error: std::sync::Arc::new(GraphError::ExecutionError(format!(
                            "No edges from node: {}",
                            current_node
                        ))),
                    };
                    return;
                }

                // Get next node
                let edge = &edges[0];
                let next_node = match edge.get_target(&current_state).await {
                    Ok(node) => node,
                    Err(e) => {
                        yield StreamEvent::Error {
                                    error: std::sync::Arc::new(e),
                                };
                        return;
                    }
                };

                // If next node is END, yield final event
                if next_node == END {
                    yield StreamEvent::GraphEnd {
                        final_state: current_state,
                    };
                    return;
                }

                current_node = next_node;
            }
        })
    }

    /// Stream the graph execution with a specific stream mode
    ///
    /// This method executes the graph and yields chunks based on the specified stream mode.
    ///
    /// # Arguments
    ///
    /// * `initial_state` - The initial state to start execution with
    /// * `mode` - The stream mode (values, updates, messages, custom, or debug)
    ///
    /// # Returns
    ///
    /// A stream of `StreamChunk` values filtered by the stream mode
    pub fn stream_with_mode<'a>(
        &'a self,
        initial_state: S,
        mode: StreamMode,
    ) -> Pin<Box<dyn Stream<Item = StreamChunk<S>> + Send + 'a>> {
        // Pass stream modes to enable LLM streaming if messages mode is requested
        let stream_modes = if mode == StreamMode::Messages {
            Some(vec![StreamMode::Messages])
        } else {
            None
        };
        let event_stream = self.stream_internal(initial_state, stream_modes, false);
        Box::pin(stream! {
            use futures::StreamExt;
            let mut event_stream = event_stream;

            while let Some(event) = event_stream.next().await {
                if let Some(chunk) = Self::convert_event_to_chunk(&event, mode) {
                    yield chunk;
                }
            }
        })
    }

    /// Stream the graph execution with multiple stream modes
    ///
    /// This method executes the graph and yields chunks for all specified stream modes.
    ///
    /// # Arguments
    ///
    /// * `initial_state` - The initial state to start execution with
    /// * `modes` - A vector of stream modes to enable
    ///
    /// # Returns
    ///
    /// A stream of `(StreamMode, StreamChunk)` tuples, one for each enabled mode
    pub fn stream_with_modes<'a>(
        &'a self,
        initial_state: S,
        modes: Vec<StreamMode>,
    ) -> Pin<Box<dyn Stream<Item = (StreamMode, StreamChunk<S>)> + Send + 'a>> {
        // Pass stream modes to enable LLM streaming if messages mode is requested
        let stream_modes = if modes.contains(&StreamMode::Messages) {
            Some(modes.clone())
        } else {
            None
        };
        let event_stream = self.stream_internal(initial_state, stream_modes, false);
        Box::pin(stream! {
            use futures::StreamExt;
            let mut event_stream = event_stream;

            while let Some(event) = event_stream.next().await {
                for mode in &modes {
                    if let Some(chunk) = Self::convert_event_to_chunk(&event, *mode) {
                        yield (*mode, chunk);
                    }
                }
            }
        })
    }

    /// Convert a StreamEvent to a StreamChunk based on the stream mode
    fn convert_event_to_chunk(event: &StreamEvent<S>, mode: StreamMode) -> Option<StreamChunk<S>> {
        use super::streaming::metadata::DebugInfo;

        match (event, mode) {
            // Values mode: extract full state from NodeEnd
            (StreamEvent::NodeEnd { state, .. }, StreamMode::Values) => Some(StreamChunk::Values {
                state: state.clone(),
            }),
            (StreamEvent::GraphEnd { final_state }, StreamMode::Values) => {
                Some(StreamChunk::Values {
                    state: final_state.clone(),
                })
            }

            // Updates mode: extract update from NodeEnd
            (StreamEvent::NodeEnd { node, update, .. }, StreamMode::Updates) => {
                Some(StreamChunk::Updates {
                    node: node.clone(),
                    update: update.clone(),
                })
            }

            // Messages mode: extract from MessageChunk event
            (
                StreamEvent::MessageChunk {
                    chunk, metadata, ..
                },
                StreamMode::Messages,
            ) => Some(StreamChunk::Messages {
                chunk: MessageChunk::new(chunk.clone(), metadata.clone()),
            }),

            // Custom mode: extract from CustomData event
            (StreamEvent::CustomData { node, data, .. }, StreamMode::Custom) => {
                Some(StreamChunk::Custom {
                    node: node.clone(),
                    data: data.clone(),
                })
            }

            // Debug mode: convert all events to debug info
            (_, StreamMode::Debug) => {
                let debug_info = match event {
                    StreamEvent::NodeStart { node, .. } => {
                        DebugInfo::with_node("NodeStart", node.clone())
                    }
                    StreamEvent::NodeEnd {
                        node,
                        state,
                        update,
                        ..
                    } => {
                        let mut info = DebugInfo::with_node("NodeEnd", node.clone());
                        info =
                            info.with_info("state".to_string(), serde_json::to_value(state).ok()?);
                        info = info
                            .with_info("update".to_string(), serde_json::to_value(update).ok()?);
                        info
                    }
                    StreamEvent::GraphEnd { final_state } => {
                        let mut info = DebugInfo::new("GraphEnd");
                        info = info.with_info(
                            "final_state".to_string(),
                            serde_json::to_value(final_state).ok()?,
                        );
                        info
                    }
                    StreamEvent::Error { error } => DebugInfo::new("Error")
                        .with_info("error".to_string(), serde_json::json!(error.to_string())),
                    StreamEvent::MessageChunk {
                        node,
                        chunk,
                        metadata,
                        ..
                    } => {
                        let mut info = DebugInfo::with_node("MessageChunk", node.clone());
                        info =
                            info.with_info("chunk".to_string(), serde_json::to_value(chunk).ok()?);
                        info = info.with_info(
                            "metadata".to_string(),
                            serde_json::to_value(metadata).ok()?,
                        );
                        info
                    }
                    StreamEvent::CustomData { node, data, .. } => {
                        let mut info = DebugInfo::with_node("CustomData", node.clone());
                        info = info.with_info("data".to_string(), data.clone());
                        info
                    }
                };
                Some(StreamChunk::Debug { info: debug_info })
            }

            // Other combinations don't match
            _ => None,
        }
    }

    /// Invoke the graph with initial state and config (with persistence support)
    ///
    /// This method supports checkpointing and can resume from a checkpoint
    /// if checkpoint_id is provided in the config.
    ///
    /// By default, this uses super-step execution for parallel node execution.
    ///
    /// This method also supports interrupts. If an interrupt occurs, it returns
    /// an `InvokeResult` with interrupt information. Use `invoke_with_config_interrupt`
    /// to get the full result including interrupt information.
    ///
    /// # Arguments
    ///
    /// * `initial_state` - The initial state, or `None` to resume from a checkpoint
    ///   (requires `checkpoint_id` in config)
    /// * `config` - The runnable configuration (thread_id, checkpoint_id, etc.)
    ///
    /// # Time-Travel Support
    ///
    /// Pass `None` as `initial_state` along with a `checkpoint_id` in config to
    /// resume execution from a historical checkpoint. This creates a new fork in
    /// the execution history.
    pub async fn invoke_with_config(
        &self,
        initial_state: Option<S>,
        config: &RunnableConfig,
    ) -> Result<S, GraphError> {
        let state_or_command = match initial_state {
            Some(state) => StateOrCommand::State(state),
            None => {
                // None input - load from checkpoint (by checkpoint_id or latest for crash recovery)
                let checkpoint_config = CheckpointConfig::from_config(config)?;
                let checkpointer = self.checkpointer.as_ref().ok_or_else(|| {
                    GraphError::ExecutionError(
                        "Checkpointer is required to resume from checkpoint".to_string(),
                    )
                })?;
                let snapshot = checkpointer
                    .get(
                        &checkpoint_config.thread_id,
                        checkpoint_config.checkpoint_id.as_deref(),
                    )
                    .await
                    .map_err(|e| {
                        GraphError::ExecutionError(format!("Failed to load checkpoint: {}", e))
                    })?;
                let snapshot = snapshot.ok_or_else(|| {
                    GraphError::ExecutionError(format!(
                        "No checkpoint found for thread_id: {}",
                        checkpoint_config.thread_id
                    ))
                })?;
                StateOrCommand::State(snapshot.values)
            }
        };
        let result = self
            .invoke_with_config_interrupt(state_or_command, config)
            .await?;
        Ok(result.state)
    }

    /// Invoke the graph with initial state and config, supporting interrupts
    ///
    /// This method supports checkpointing, resuming from checkpoints, and interrupts.
    /// It returns an `InvokeResult` that includes interrupt information if an interrupt occurred.
    ///
    /// # Arguments
    ///
    /// * `initial_state` - The initial state or Command to resume from
    /// * `config` - The runnable configuration (thread_id, checkpoint_id, etc.)
    ///
    /// # Returns
    ///
    /// An `InvokeResult` containing the final state and any interrupt information
    pub async fn invoke_with_config_interrupt(
        &self,
        initial_state: StateOrCommand<S>,
        config: &RunnableConfig,
    ) -> Result<InvokeResult<S>, GraphError> {
        let checkpoint_config = CheckpointConfig::from_config(config)?;
        let thread_id = &checkpoint_config.thread_id;

        // Check if checkpointer is available (required for interrupts)
        let checkpointer = self.checkpointer.as_ref().ok_or_else(|| {
            GraphError::ExecutionError("Checkpointer is required for interrupt support".to_string())
        })?;

        // Handle Command input or regular state
        let (current_state, resume_values, parent_config) = match initial_state {
            StateOrCommand::State(state) => {
                // Regular state input
                // Check if we should load from checkpoint (time-travel)
                let (state, parent) = if let Some(checkpoint_id) = &checkpoint_config.checkpoint_id
                {
                    let snapshot = checkpointer
                        .get(thread_id, Some(checkpoint_id))
                        .await
                        .map_err(|e| {
                            GraphError::ExecutionError(format!("Failed to load checkpoint: {}", e))
                        })?;

                    let snapshot = snapshot.ok_or_else(|| {
                        GraphError::ExecutionError(format!(
                            "Checkpoint not found: {}",
                            checkpoint_id
                        ))
                    })?;

                    // Record parent config for fork tracking
                    let parent = Some(snapshot.config.clone());
                    (snapshot.values, parent)
                } else {
                    (state, None)
                };
                (state, Vec::new(), parent)
            }
            StateOrCommand::Command(cmd) => {
                // Command input - resume from checkpoint
                // Get the latest checkpoint
                let snapshot = checkpointer.get(thread_id, None).await.map_err(|e| {
                    GraphError::ExecutionError(format!("Failed to load checkpoint: {}", e))
                })?;

                let snapshot = snapshot.ok_or_else(|| {
                    GraphError::ExecutionError(format!(
                        "No checkpoint found for thread: {}",
                        thread_id
                    ))
                })?;

                // Extract resume values from command
                let resume_values = if let Some(resume_value) = cmd.resume_value() {
                    vec![resume_value.clone()]
                } else {
                    Vec::new()
                };

                // Record parent config for fork tracking
                let parent = Some(snapshot.config.clone());
                (snapshot.values, resume_values, parent)
            }
        };

        // Build trace: push ResumeReceived when resuming with values (before moving resume_values)
        let mut trace = Vec::new();
        for v in &resume_values {
            trace.push(TraceEvent::ResumeReceived { value: v.clone() });
        }

        // Event-first (2.0): append Resumed events when resuming
        if let Some(es) = &self.event_store {
            for v in &resume_values {
                es.append(thread_id, &[Event::Resumed { value: v.clone() }])
                    .map_err(|e| GraphError::ExecutionError(e.to_string()))?;
            }
        }

        // Set up interrupt context with resume values
        let interrupt_ctx = if resume_values.is_empty() {
            InterruptContext::new()
        } else {
            InterruptContext::with_resume_values(resume_values)
        };

        // Update checkpoint_config with parent if we're resuming from a checkpoint
        let mut checkpoint_config = checkpoint_config.clone();
        if let Some(ref _parent) = parent_config {
            checkpoint_config.checkpoint_id = None; // Clear checkpoint_id to create new fork
                                                    // Store parent in the checkpoint when saving
        }

        // Create RunnableConfig from checkpoint_config for nodes
        let mut runnable_config =
            RunnableConfig::with_thread_id(checkpoint_config.thread_id.clone());
        if let Some(checkpoint_id) = &checkpoint_config.checkpoint_id {
            runnable_config.configurable.insert(
                "checkpoint_id".to_string(),
                serde_json::json!(checkpoint_id),
            );
        }

        // Execute with interrupt context
        let result = set_interrupt_context(interrupt_ctx, async {
            self.execute_with_interrupt_support(
                current_state,
                &checkpoint_config,
                parent_config.as_ref(),
                Some(&runnable_config),
                self.store.clone(),
                &mut trace,
                self.event_store.as_ref(),
                &checkpoint_config.thread_id,
            )
            .await
        })
        .await;

        result
    }

    /// Execute graph with interrupt support
    ///
    /// Internal method that executes the graph and handles interrupts.
    /// Fills `trace` with StepCompleted, InterruptReached events.
    async fn execute_with_interrupt_support(
        &self,
        initial_state: S,
        checkpoint_config: &CheckpointConfig,
        parent_config: Option<&CheckpointConfig>,
        config: Option<&RunnableConfig>,
        store: Option<StoreBox>,
        trace: &mut Vec<TraceEvent>,
        event_store: Option<&Arc<dyn EventStore>>,
        run_id: &String,
    ) -> Result<InvokeResult<S>, GraphError> {
        let mut current_state = initial_state;
        let mut current_node = START.to_string();
        let mut visited = HashSet::new();
        let max_iterations = 1000;
        let mut iterations = 0;

        loop {
            if iterations >= max_iterations {
                return Err(GraphError::ExecutionError(
                    "Maximum iterations reached. Possible infinite loop.".to_string(),
                ));
            }
            iterations += 1;

            if current_node == END {
                if let Some(es) = event_store {
                    es.append(run_id, &[Event::Completed])
                        .map_err(|e| GraphError::ExecutionError(e.to_string()))?;
                }
                return Ok(InvokeResult::new_with_trace(
                    current_state,
                    std::mem::take(trace),
                ));
            }

            if !visited.contains(&current_node) {
                visited.insert(current_node.clone());
            }

            let edges = self
                .adjacency
                .get(&current_node)
                .ok_or_else(|| {
                    GraphError::ExecutionError(format!(
                        "No edges found from node: {}",
                        current_node
                    ))
                })?
                .clone();

            if current_node == START {
                if edges.is_empty() {
                    return Err(GraphError::ExecutionError(
                        "No edges from START".to_string(),
                    ));
                }
                let edge = &edges[0];
                let next_node = edge.get_target(&current_state).await?;
                current_node = next_node;
                continue;
            }

            // Execute the current node
            let node = self
                .nodes
                .get(&current_node)
                .ok_or_else(|| GraphError::NodeNotFound(current_node.clone()))?;

            // Event-first (2.0): append ActionRequested before node execution (node step as action)
            let action_id = if event_store.is_some() {
                Some(format!("{}-{}", run_id, current_node))
            } else {
                None
            };
            if let (Some(es), Some(ref aid)) = (event_store, &action_id) {
                let payload = serde_json::to_value(&current_state)
                    .map_err(|e| GraphError::ExecutionError(e.to_string()))?;
                es.append(
                    run_id,
                    &[Event::ActionRequested {
                        action_id: aid.clone(),
                        payload,
                    }],
                )
                .map_err(|e| GraphError::ExecutionError(e.to_string()))?;
            }

            // Execute node and handle interrupts
            // Use invoke_with_context to support config and store
            let update_result = node
                .invoke_with_context(&current_state, config, store.clone())
                .await;

            match update_result {
                Ok(update) => {
                    // Event-first (2.0): append ActionSucceeded after node success
                    if let (Some(es), Some(ref aid)) = (event_store, &action_id) {
                        let output = serde_json::to_value(&update)
                            .map_err(|e| GraphError::ExecutionError(e.to_string()))?;
                        es.append(
                            run_id,
                            &[Event::ActionSucceeded {
                                action_id: aid.clone(),
                                output,
                            }],
                        )
                        .map_err(|e| GraphError::ExecutionError(e.to_string()))?;
                    }
                    // Node executed successfully, merge state
                    trace.push(TraceEvent::StepCompleted {
                        node: current_node.clone(),
                    });
                    current_state = self.merge_state_update(&current_state, &update)?;
                    // Event-first (2.0): append StateUpdated after each node
                    if let Some(es) = event_store {
                        let payload = serde_json::to_value(&current_state)
                            .map_err(|e| GraphError::ExecutionError(e.to_string()))?;
                        es.append(
                            run_id,
                            &[Event::StateUpdated {
                                step_id: Some(current_node.clone()),
                                payload,
                            }],
                        )
                        .map_err(|e| GraphError::ExecutionError(e.to_string()))?;
                    }
                }
                Err(GraphError::InterruptError(interrupt_err)) => {
                    // Interrupt occurred - save checkpoint and return
                    let interrupt_value = interrupt_err.value().clone();
                    trace.push(TraceEvent::InterruptReached {
                        value: interrupt_value.clone(),
                    });
                    // Event-first (2.0): pair ActionRequested with ActionFailed(Interrupt) then Interrupted
                    if let (Some(es), Some(ref aid)) = (event_store, &action_id) {
                        es.append(
                            run_id,
                            &[
                                Event::ActionFailed {
                                    action_id: aid.clone(),
                                    error: "Interrupt".to_string(),
                                },
                                Event::Interrupted {
                                    value: interrupt_value.clone(),
                                },
                            ],
                        )
                        .map_err(|e| GraphError::ExecutionError(e.to_string()))?;
                    } else if let Some(es) = event_store {
                        es.append(
                            run_id,
                            &[Event::Interrupted {
                                value: interrupt_value.clone(),
                            }],
                        )
                        .map_err(|e| GraphError::ExecutionError(e.to_string()))?;
                    }
                    let interrupt = Interrupt::new(interrupt_value);

                    // Save checkpoint at interrupt point
                    // Note: checkpointer should always be available when using interrupt support
                    // (checked in invoke_with_config_interrupt)
                    let mut snapshot = if let Some(parent) = parent_config {
                        // Create snapshot with parent config for fork tracking
                        StateSnapshot::with_parent(
                            current_state.clone(),
                            vec![current_node.clone()],
                            checkpoint_config.clone(),
                            parent.clone(),
                        )
                    } else {
                        StateSnapshot::new(
                            current_state.clone(),
                            vec![current_node.clone()],
                            checkpoint_config.clone(),
                        )
                    };
                    if let Some(es) = event_store {
                        let seq = es
                            .head(run_id)
                            .map_err(|e| GraphError::ExecutionError(e.to_string()))?;
                        snapshot = snapshot.with_at_seq(seq);
                    }
                    if let Some(checkpointer) = &self.checkpointer {
                        checkpointer
                            .put(checkpoint_config.thread_id.as_str(), &snapshot)
                            .await
                            .map_err(|e| {
                                GraphError::ExecutionError(format!(
                                    "Failed to save checkpoint: {}",
                                    e
                                ))
                            })?;
                    }

                    return Ok(InvokeResult::with_interrupt_and_trace(
                        current_state,
                        vec![interrupt],
                        std::mem::take(trace),
                    ));
                }
                Err(e) => {
                    // Event-first (2.0): append ActionFailed on node execution error
                    if let (Some(es), Some(ref aid)) = (event_store, &action_id) {
                        let _ = es.append(
                            run_id,
                            &[Event::ActionFailed {
                                action_id: aid.clone(),
                                error: e.to_string(),
                            }],
                        );
                    }
                    return Err(e);
                }
            }

            // Determine next node
            if edges.is_empty() {
                return Err(GraphError::ExecutionError(format!(
                    "No edges from node: {}",
                    current_node
                )));
            }

            let edge = &edges[0];
            let next_node = edge.get_target(&current_state).await?;

            if next_node == END {
                if let Some(es) = event_store {
                    es.append(run_id, &[Event::Completed])
                        .map_err(|e| GraphError::ExecutionError(e.to_string()))?;
                }
                return Ok(InvokeResult::new_with_trace(
                    current_state,
                    std::mem::take(trace),
                ));
            }

            current_node = next_node;
        }
    }

    /// Invoke the graph with initial state, config, and durability mode
    ///
    /// This method supports checkpointing, resuming from checkpoints, and
    /// configurable durability modes (exit/async/sync).
    ///
    /// Uses super-step execution model for parallel node execution.
    ///
    /// # Arguments
    ///
    /// * `initial_state` - The initial state, or `None` to resume from a checkpoint
    ///   (requires `checkpoint_id` in config)
    /// * `config` - The runnable configuration (thread_id, checkpoint_id, etc.)
    /// * `durability_mode` - The durability mode for checkpoint saving
    pub async fn invoke_with_config_and_mode(
        &self,
        initial_state: Option<S>,
        config: &RunnableConfig,
        durability_mode: DurabilityMode,
    ) -> Result<S, GraphError> {
        let checkpoint_config = CheckpointConfig::from_config(config)?;
        let thread_id = &checkpoint_config.thread_id;

        // Determine current state and parent_config: from input, checkpoint (by id or latest), or error
        let (current_state, parent_config) = match initial_state {
            Some(state) => {
                // If checkpoint_id is provided, it takes precedence (time-travel)
                if let Some(checkpoint_id) = &checkpoint_config.checkpoint_id {
                    if let Some(checkpointer) = &self.checkpointer {
                        let snapshot = checkpointer
                            .get(thread_id, Some(checkpoint_id))
                            .await
                            .map_err(|e| {
                                GraphError::ExecutionError(format!(
                                    "Failed to load checkpoint: {}",
                                    e
                                ))
                            })?;

                        let snapshot = snapshot.ok_or_else(|| {
                            GraphError::ExecutionError(format!(
                                "Checkpoint not found: {}",
                                checkpoint_id
                            ))
                        })?;
                        (snapshot.values, Some(checkpoint_config.clone()))
                    } else {
                        return Err(GraphError::ExecutionError(
                            "Checkpointer not configured but checkpoint_id provided".to_string(),
                        ));
                    }
                } else {
                    (state, None)
                }
            }
            None => {
                // Resume from checkpoint: either by checkpoint_id or from latest (crash recovery)
                let checkpointer = self.checkpointer.as_ref().ok_or_else(|| {
                    GraphError::ExecutionError(
                        "Checkpointer is required to resume from checkpoint".to_string(),
                    )
                })?;

                let snapshot = if let Some(checkpoint_id) = &checkpoint_config.checkpoint_id {
                    checkpointer
                        .get(thread_id, Some(checkpoint_id))
                        .await
                        .map_err(|e| {
                            GraphError::ExecutionError(format!("Failed to load checkpoint: {}", e))
                        })?
                        .ok_or_else(|| {
                            GraphError::ExecutionError(format!(
                                "Checkpoint not found: {}",
                                checkpoint_id
                            ))
                        })?
                } else {
                    // Resume from latest checkpoint (e.g. after process crash)
                    checkpointer
                        .get(thread_id, None)
                        .await
                        .map_err(|e| {
                            GraphError::ExecutionError(format!(
                                "Failed to load latest checkpoint: {}",
                                e
                            ))
                        })?
                        .ok_or_else(|| {
                            GraphError::ExecutionError(format!(
                                "No checkpoint found for thread_id: {}",
                                thread_id
                            ))
                        })?
                };

                (snapshot.values.clone(), Some(snapshot.config.clone()))
            }
        };

        // Use super-step executor for parallel execution
        let scheduler = NodeScheduler::new(self.adjacency.clone());
        let executor = SuperStepExecutor::new(
            self.nodes.clone(),
            scheduler,
            self.checkpointer.clone(),
            durability_mode,
        );

        // Create new checkpoint config without checkpoint_id for new fork
        let mut new_checkpoint_config = checkpoint_config.clone();
        new_checkpoint_config.checkpoint_id = None;

        // Pass config and store to executor for nodes to access
        executor
            .execute(
                current_state,
                &new_checkpoint_config,
                parent_config.as_ref(),
                Some(config),       // Pass config to nodes
                self.store.clone(), // Pass store to nodes
            )
            .await
    }

    /// Stream the graph execution with config and stream mode
    ///
    /// This method supports checkpointing, resuming from checkpoints, and
    /// configurable stream modes.
    ///
    /// # Arguments
    ///
    /// * `initial_state` - The initial state to start execution with
    /// * `config` - The runnable configuration (thread_id, checkpoint_id, etc.)
    /// * `mode` - The stream mode (values, updates, messages, custom, or debug)
    ///
    /// # Returns
    ///
    /// A stream of `StreamChunk` values filtered by the stream mode
    pub fn astream_with_config_and_mode<'a>(
        &'a self,
        initial_state: S,
        config: &RunnableConfig,
        mode: StreamMode,
    ) -> Pin<Box<dyn Stream<Item = StreamChunk<S>> + Send + 'a>> {
        let checkpoint_config = match CheckpointConfig::from_config(config) {
            Ok(cfg) => cfg,
            Err(e) => {
                return Box::pin(stream! {
                    yield StreamChunk::Debug {
                        info: super::streaming::metadata::DebugInfo::new("Error")
                            .with_info("error".to_string(), serde_json::json!(e.to_string())),
                    };
                });
            }
        };
        let _thread_id = &checkpoint_config.thread_id;

        // If checkpoint_id is provided, load state from checkpoint
        let current_state = if let Some(_checkpoint_id) = &checkpoint_config.checkpoint_id {
            if let Some(_checkpointer) = &self.checkpointer {
                // For now, we'll use the initial state
                // In a full implementation, we'd load from checkpoint
                initial_state
            } else {
                initial_state
            }
        } else {
            initial_state
        };

        // Pass stream modes to enable LLM streaming if messages mode is requested
        let stream_modes = if mode == StreamMode::Messages {
            Some(vec![StreamMode::Messages])
        } else {
            None
        };
        let event_stream = self.stream_internal(current_state, stream_modes, false);

        Box::pin(stream! {
            use futures::StreamExt;
            let mut event_stream = event_stream;

            while let Some(event) = event_stream.next().await {
                if let Some(chunk) = Self::convert_event_to_chunk(&event, mode) {
                    yield chunk;
                }
            }
        })
    }

    /// Get the current state for a thread
    ///
    /// Returns the latest checkpoint for the given thread_id.
    pub async fn get_state(&self, config: &RunnableConfig) -> Result<StateSnapshot<S>, GraphError> {
        let checkpoint_config = CheckpointConfig::from_config(config)?;
        let thread_id = &checkpoint_config.thread_id;

        let checkpointer = self
            .checkpointer
            .as_ref()
            .ok_or_else(|| GraphError::ExecutionError("Checkpointer not configured".to_string()))?;

        let snapshot = checkpointer
            .get(thread_id, checkpoint_config.checkpoint_id.as_deref())
            .await
            .map_err(|e| GraphError::ExecutionError(format!("Failed to get state: {}", e)))?;

        snapshot.ok_or_else(|| {
            GraphError::ExecutionError(format!("No state found for thread: {}", thread_id))
        })
    }

    /// Get the state history for a thread
    ///
    /// Returns all checkpoints for the given thread_id in chronological order.
    pub async fn get_state_history(
        &self,
        config: &RunnableConfig,
    ) -> Result<Vec<StateSnapshot<S>>, GraphError> {
        let checkpoint_config = CheckpointConfig::from_config(config)?;
        let thread_id = &checkpoint_config.thread_id;

        let checkpointer = self
            .checkpointer
            .as_ref()
            .ok_or_else(|| GraphError::ExecutionError("Checkpointer not configured".to_string()))?;

        checkpointer
            .list(thread_id, None)
            .await
            .map_err(|e| GraphError::ExecutionError(format!("Failed to get state history: {}", e)))
    }

    /// Update the state for a thread
    ///
    /// This creates a new checkpoint with updated state values.
    /// The new checkpoint will be associated with the same thread, but will have
    /// a new checkpoint_id and will record the original checkpoint as its parent
    /// (for fork tracking in time-travel scenarios).
    ///
    /// # Arguments
    ///
    /// * `config` - The runnable configuration pointing to the checkpoint to update
    /// * `values` - State updates to apply
    /// * `as_node` - Optional node name to record in metadata
    ///
    /// # Returns
    ///
    /// A new `StateSnapshot` with the updated state and new checkpoint_id
    pub async fn update_state(
        &self,
        config: &RunnableConfig,
        values: &StateUpdate,
        as_node: Option<&str>,
    ) -> Result<StateSnapshot<S>, GraphError> {
        // Get current state
        let original_snapshot = self.get_state(config).await?;

        // Apply updates to state
        let updated_values = self.merge_state_update(&original_snapshot.values, values)?;

        // Create new checkpoint config (new checkpoint_id will be generated)
        let mut new_config = CheckpointConfig::new(original_snapshot.thread_id());
        new_config.checkpoint_ns = original_snapshot.config.checkpoint_ns.clone();

        // Create new snapshot with parent config for fork tracking
        let mut new_snapshot = StateSnapshot::with_parent(
            updated_values,
            original_snapshot.next.clone(),
            new_config,
            original_snapshot.config.clone(), // Parent config
        );

        // Update metadata
        if let Some(node) = as_node {
            new_snapshot
                .metadata
                .insert("as_node".to_string(), serde_json::json!(node));
        }

        // Save updated checkpoint
        let checkpointer = self
            .checkpointer
            .as_ref()
            .ok_or_else(|| GraphError::ExecutionError("Checkpointer not configured".to_string()))?;

        let checkpoint_id = checkpointer
            .put(new_snapshot.thread_id(), &new_snapshot)
            .await
            .map_err(|e| GraphError::ExecutionError(format!("Failed to update state: {}", e)))?;

        // Update snapshot with new checkpoint_id
        new_snapshot.config.checkpoint_id = Some(checkpoint_id);

        Ok(new_snapshot)
    }
}

/// Stream options for controlling streaming behavior
#[derive(Clone, Debug, Default)]
pub struct StreamOptions {
    /// Stream modes to enable
    pub stream_modes: Option<Vec<StreamMode>>,
    /// Whether to include subgraph events in the stream
    pub subgraphs: bool,
}

impl StreamOptions {
    /// Create default stream options
    pub fn new() -> Self {
        Self::default()
    }

    /// Create stream options with subgraphs enabled
    pub fn with_subgraphs(mut self, subgraphs: bool) -> Self {
        self.subgraphs = subgraphs;
        self
    }

    /// Create stream options with stream modes
    pub fn with_modes(mut self, stream_modes: Vec<StreamMode>) -> Self {
        self.stream_modes = Some(stream_modes);
        self
    }
}

/// Stream event type - represents different types of events during graph execution
#[derive(Clone, Debug)]
pub enum StreamEvent<S: State> {
    /// A node is about to be executed
    NodeStart {
        node: String,
        state: S,
        /// Path for subgraph nodes (e.g., ["parent_node:uuid", "subgraph_node"])
        path: Vec<String>,
    },
    /// A node has completed execution
    NodeEnd {
        node: String,
        state: S,
        update: StateUpdate,
        /// Path for subgraph nodes
        path: Vec<String>,
    },
    /// The graph has completed execution
    GraphEnd { final_state: S },
    /// An error occurred during execution
    Error { error: std::sync::Arc<GraphError> },
    /// A message chunk from an LLM node (for messages stream mode)
    MessageChunk {
        node: String,
        chunk: crate::schemas::StreamData,
        metadata: MessageMetadata,
        /// Path for subgraph nodes
        path: Vec<String>,
    },
    /// Custom data from a node (for custom stream mode)
    CustomData {
        node: String,
        data: serde_json::Value,
        /// Path for subgraph nodes
        path: Vec<String>,
    },
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::graph::{function_node, state::MessagesState, StateGraph, END, START};
    use futures::StreamExt;

    #[tokio::test]
    async fn test_invoke_simple_graph() {
        let mut graph = StateGraph::<MessagesState>::new();

        graph
            .add_node(
                "node1",
                function_node("node1", |_state| async move {
                    let mut update = HashMap::new();
                    update.insert(
                        "messages".to_string(),
                        serde_json::to_value(vec![
                            crate::schemas::messages::Message::new_ai_message("Hello"),
                        ])?,
                    );
                    Ok(update)
                }),
            )
            .unwrap();

        graph.add_edge(START, "node1");
        graph.add_edge("node1", END);

        let compiled = graph.compile().unwrap();
        let initial_state = MessagesState::new();
        let result = compiled.invoke(initial_state).await;

        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_stream_simple_graph() {
        let mut graph = StateGraph::<MessagesState>::new();

        graph
            .add_node(
                "node1",
                function_node("node1", |_state| async move {
                    let mut update = HashMap::new();
                    update.insert(
                        "messages".to_string(),
                        serde_json::to_value(vec![
                            crate::schemas::messages::Message::new_ai_message("Hello"),
                        ])?,
                    );
                    Ok(update)
                }),
            )
            .unwrap();

        graph.add_edge(START, "node1");
        graph.add_edge("node1", END);

        let compiled = graph.compile().unwrap();
        let initial_state = MessagesState::new();
        let mut stream = compiled.stream(initial_state);

        let mut events = Vec::new();
        while let Some(event) = stream.next().await {
            events.push(event);
        }

        // Should have at least NodeStart, NodeEnd, and GraphEnd events
        assert!(events.len() >= 3);
    }

    #[tokio::test]
    async fn step_once_pure_graph_default_succeeds() {
        let mut graph = StateGraph::<MessagesState>::new();
        graph
            .add_node(
                "node1",
                function_node("node1", |_s: &MessagesState| async move {
                    Ok(std::collections::HashMap::new())
                }),
            )
            .unwrap();
        graph.add_edge(START, "node1");
        graph.add_edge("node1", END);
        let compiled = graph.compile().unwrap();
        let state = MessagesState::new();
        let r = compiled.step_once(&state, START, None).await.unwrap();
        assert!(matches!(r, GraphStepOnceResult::Complete { .. }));
    }

    #[tokio::test]
    async fn step_once_non_pure_without_allow_errors() {
        let mut graph = StateGraph::<MessagesState>::new();
        graph
            .add_node(
                "node1",
                function_node("node1", |_s: &MessagesState| async move {
                    Ok(std::collections::HashMap::new())
                }),
            )
            .unwrap();
        graph.add_edge(START, "node1");
        graph.add_edge("node1", END);
        let compiled = graph.compile().unwrap().with_pure_guard(false);
        let state = MessagesState::new();
        let r = compiled.step_once(&state, START, None).await;
        assert!(r.is_err());
        assert!(r.unwrap_err().to_string().contains("pure graph"));
    }

    #[tokio::test]
    async fn step_once_non_pure_with_allow_succeeds() {
        let mut graph = StateGraph::<MessagesState>::new();
        graph
            .add_node(
                "node1",
                function_node("node1", |_s: &MessagesState| async move {
                    Ok(std::collections::HashMap::new())
                }),
            )
            .unwrap();
        graph.add_edge(START, "node1");
        graph.add_edge("node1", END);
        let compiled = graph.compile().unwrap().with_pure_guard(false);
        let config = RunnableConfig::new().with_allow_non_pure_step_once(true);
        let state = MessagesState::new();
        let r = compiled
            .step_once(&state, START, Some(&config))
            .await
            .unwrap();
        assert!(matches!(r, GraphStepOnceResult::Complete { .. }));
    }
}