lellm_graph/
checkpoint.rs1use serde::{Deserialize, Serialize};
12
13use crate::state::State;
14use crate::workflow_state::WorkflowState;
15
16#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
20pub struct CheckpointId(pub uuid::Uuid);
21
22impl std::fmt::Display for CheckpointId {
23 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
24 write!(f, "{}", self.0)
25 }
26}
27
28#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
32pub struct NodeId(pub String);
33
34impl std::fmt::Display for NodeId {
35 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
36 write!(f, "{}", self.0)
37 }
38}
39
40#[derive(Debug, Clone, Serialize, Deserialize)]
47pub struct Checkpoint<S = State> {
48 pub checkpoint_id: CheckpointId,
50 pub current_node: NodeId,
52 pub state: S,
54 pub created_at: std::time::SystemTime,
56}
57
58impl<S: WorkflowState> Checkpoint<S> {
59 pub fn new(current_node: impl Into<String>, state: S) -> Self {
60 Self {
61 checkpoint_id: CheckpointId(uuid::Uuid::new_v4()),
62 current_node: NodeId(current_node.into()),
63 state,
64 created_at: std::time::SystemTime::now(),
65 }
66 }
67}
68
69#[derive(Debug, thiserror::Error)]
73pub enum CheckpointStoreError {
74 #[error("storage error: {0}")]
75 Storage(String),
76 #[error("checkpoint not found: {0}")]
77 NotFound(CheckpointId),
78 #[error("corrupted checkpoint: {0}")]
79 Corrupted(String),
80}
81
82#[async_trait::async_trait]
88pub trait CheckpointStore: Send + Sync {
89 async fn save_with_trace(
91 &self,
92 trace_id: &TraceId,
93 checkpoint: &Checkpoint,
94 ) -> Result<(), CheckpointStoreError>;
95 async fn load(&self, id: &CheckpointId) -> Result<Option<Checkpoint>, CheckpointStoreError>;
96 async fn load_latest(
97 &self,
98 trace_id: &TraceId,
99 ) -> Result<Option<Checkpoint>, CheckpointStoreError>;
100 async fn list(&self, trace_id: &TraceId) -> Result<Vec<CheckpointId>, CheckpointStoreError>;
101 async fn delete(&self, id: &CheckpointId) -> Result<bool, CheckpointStoreError>;
102 async fn prune(&self, trace_id: &TraceId, keep: usize) -> Result<usize, CheckpointStoreError>;
103}
104
105#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
109pub enum CheckpointPolicy {
110 #[default]
112 EveryNode,
113 BarrierOnly,
115 Manual,
117}
118
119pub use crate::ids::TraceId;