oris-runtime 0.15.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
#[cfg(feature = "sqlite-persistence")]
use std::marker::PhantomData;
#[cfg(feature = "sqlite-persistence")]
use std::sync::Arc;

#[cfg(feature = "sqlite-persistence")]
use async_trait::async_trait;
#[cfg(feature = "sqlite-persistence")]
use chrono::{DateTime, Utc};
#[cfg(feature = "sqlite-persistence")]
use rusqlite::{params, Connection, OptionalExtension};
#[cfg(feature = "sqlite-persistence")]
use serde_json::Value;
#[cfg(feature = "sqlite-persistence")]
use tokio::sync::Mutex;

#[cfg(feature = "sqlite-persistence")]
use crate::graph::state::State;

#[cfg(feature = "sqlite-persistence")]
use super::{
    checkpointer::Checkpointer, config::CheckpointConfig, error::PersistenceError,
    snapshot::StateSnapshot,
};

#[cfg(feature = "sqlite-persistence")]
/// SQLite-based checkpointer implementation
///
/// This provides persistent storage for checkpoints using SQLite.
/// Checkpoints are stored in a local database file.
pub struct SqliteSaver<S: State> {
    connection: Arc<Mutex<Connection>>,
    #[allow(dead_code)]
    state: PhantomData<S>,
}

#[cfg(feature = "sqlite-persistence")]
impl<S: State> SqliteSaver<S>
where
    S: serde::Serialize + for<'de> serde::Deserialize<'de>,
{
    /// Create a new SqliteSaver with a database file path
    pub fn new(path: &str) -> Result<Self, PersistenceError> {
        let connection = Connection::open(path)?;
        let saver = Self {
            connection: Arc::new(Mutex::new(connection)),
            state: PhantomData,
        };
        saver.setup()?;
        Ok(saver)
    }

    /// Create a new SqliteSaver with an in-memory database
    pub fn new_in_memory() -> Result<Self, PersistenceError> {
        let connection = Connection::open_in_memory()?;
        let saver = Self {
            connection: Arc::new(Mutex::new(connection)),
            state: PhantomData,
        };
        saver.setup()?;
        Ok(saver)
    }

    /// Setup the database schema
    fn setup(&self) -> Result<(), PersistenceError> {
        let conn = self.connection.blocking_lock();
        conn.execute(
            "CREATE TABLE IF NOT EXISTS checkpoints (
                thread_id TEXT NOT NULL,
                checkpoint_id TEXT PRIMARY KEY,
                checkpoint_ns TEXT,
                parent_checkpoint_id TEXT,
                state_values BLOB NOT NULL,
                next_nodes TEXT NOT NULL,
                metadata TEXT NOT NULL,
                created_at TEXT NOT NULL,
                at_seq INTEGER
            )",
            [],
        )?;

        let has_at_seq = conn
            .query_row(
                "SELECT 1 FROM pragma_table_info('checkpoints') WHERE name = 'at_seq' LIMIT 1",
                [],
                |row| row.get::<_, i64>(0),
            )
            .optional()?
            .is_some();
        if !has_at_seq {
            conn.execute("ALTER TABLE checkpoints ADD COLUMN at_seq INTEGER", [])?;
        }

        conn.execute(
            "CREATE INDEX IF NOT EXISTS idx_thread_id ON checkpoints(thread_id)",
            [],
        )?;

        conn.execute(
            "CREATE INDEX IF NOT EXISTS idx_created_at ON checkpoints(created_at)",
            [],
        )?;

        Ok(())
    }
}

#[cfg(feature = "sqlite-persistence")]
#[async_trait]
impl<S: State> Checkpointer<S> for SqliteSaver<S>
where
    S: serde::Serialize + for<'de> serde::Deserialize<'de>,
{
    async fn put(
        &self,
        thread_id: &str,
        checkpoint: &StateSnapshot<S>,
    ) -> Result<String, PersistenceError> {
        let checkpoint_id = checkpoint.checkpoint_id().cloned().unwrap_or_else(|| {
            #[cfg(feature = "uuid")]
            {
                uuid::Uuid::new_v4().to_string()
            }
            #[cfg(not(feature = "uuid"))]
            {
                use std::time::{SystemTime, UNIX_EPOCH};
                format!(
                    "checkpoint-{}",
                    SystemTime::now()
                        .duration_since(UNIX_EPOCH)
                        .unwrap()
                        .as_nanos()
                )
            }
        });

        // Serialize state using serde_json
        let state_bytes =
            serde_json::to_vec(&checkpoint.values).map_err(PersistenceError::SerializationError)?;

        // Serialize next nodes and metadata
        let next_nodes_json = serde_json::to_string(&checkpoint.next)?;
        let metadata_json = serde_json::to_string(&checkpoint.metadata)?;

        let created_at = checkpoint.created_at.to_rfc3339();
        let parent_checkpoint_id = checkpoint
            .parent_config
            .as_ref()
            .and_then(|c| c.checkpoint_id.as_ref())
            .map(|s| s.as_str());

        let conn = self.connection.lock().await;
        conn.execute(
            "INSERT INTO checkpoints (
                thread_id, checkpoint_id, checkpoint_ns, parent_checkpoint_id,
                state_values, next_nodes, metadata, created_at, at_seq
            ) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9)",
            params![
                thread_id,
                checkpoint_id,
                checkpoint.config.checkpoint_ns,
                parent_checkpoint_id,
                state_bytes,
                next_nodes_json,
                metadata_json,
                created_at,
                checkpoint.at_seq.map(|seq| seq as i64),
            ],
        )?;

        Ok(checkpoint_id)
    }

    async fn get(
        &self,
        thread_id: &str,
        checkpoint_id: Option<&str>,
    ) -> Result<Option<StateSnapshot<S>>, PersistenceError> {
        let conn = self.connection.lock().await;

        let query = if let Some(_cp_id) = checkpoint_id {
            "SELECT checkpoint_id, checkpoint_ns, parent_checkpoint_id, state_values, 
                    next_nodes, metadata, created_at, at_seq
             FROM checkpoints 
             WHERE thread_id = ?1 AND checkpoint_id = ?2
             ORDER BY created_at DESC LIMIT 1"
        } else {
            "SELECT checkpoint_id, checkpoint_ns, parent_checkpoint_id, state_values, 
                    next_nodes, metadata, created_at, at_seq
             FROM checkpoints 
             WHERE thread_id = ?1
             ORDER BY created_at DESC LIMIT 1"
        };

        let mut stmt = conn.prepare(query)?;
        let params = if checkpoint_id.is_some() {
            params![thread_id, checkpoint_id]
        } else {
            params![thread_id]
        };

        let result = stmt.query_row(params, |row| {
            let checkpoint_id: String = row.get(0)?;
            let checkpoint_ns: Option<String> = row.get(1)?;
            let parent_checkpoint_id: Option<String> = row.get(2)?;
            let state_bytes: Vec<u8> = row.get(3)?;
            let next_nodes_json: String = row.get(4)?;
            let metadata_json: String = row.get(5)?;
            let created_at_str: String = row.get(6)?;
            let at_seq: Option<i64> = row.get(7)?;

            // Deserialize state using serde_json (map to rusqlite::Error for closure return type)
            let values: S = serde_json::from_slice(&state_bytes).map_err(|_e| {
                rusqlite::Error::InvalidColumnType(
                    3,
                    "state_values".to_string(),
                    rusqlite::types::Type::Blob,
                )
            })?;

            // Deserialize next nodes and metadata
            let next: Vec<String> = serde_json::from_str(&next_nodes_json).map_err(|_e| {
                rusqlite::Error::InvalidColumnType(
                    4,
                    "next_nodes".to_string(),
                    rusqlite::types::Type::Text,
                )
            })?;
            let metadata: std::collections::HashMap<String, Value> =
                serde_json::from_str(&metadata_json).map_err(|_e| {
                    rusqlite::Error::InvalidColumnType(
                        5,
                        "metadata".to_string(),
                        rusqlite::types::Type::Text,
                    )
                })?;

            // Parse created_at
            let created_at = DateTime::parse_from_rfc3339(&created_at_str)
                .map_err(|_e| {
                    rusqlite::Error::InvalidColumnType(
                        7,
                        "created_at".to_string(),
                        rusqlite::types::Type::Text,
                    )
                })?
                .with_timezone(&Utc);

            // Build config
            let config = CheckpointConfig {
                thread_id: thread_id.to_string(),
                checkpoint_id: Some(checkpoint_id.clone()),
                checkpoint_ns,
            };

            // Build parent config if exists
            let parent_config = parent_checkpoint_id.map(|parent_id| CheckpointConfig {
                thread_id: thread_id.to_string(),
                checkpoint_id: Some(parent_id),
                checkpoint_ns: None,
            });

            Ok(StateSnapshot {
                values,
                next,
                config,
                metadata,
                created_at,
                parent_config,
                at_seq: at_seq.map(|seq| seq as u64),
            })
        });

        match result {
            Ok(snapshot) => Ok(Some(snapshot)),
            Err(rusqlite::Error::QueryReturnedNoRows) => Ok(None),
            Err(e) => Err(PersistenceError::DatabaseError(e.to_string())),
        }
    }

    async fn list(
        &self,
        thread_id: &str,
        limit: Option<usize>,
    ) -> Result<Vec<StateSnapshot<S>>, PersistenceError> {
        let conn = self.connection.lock().await;

        let limit_clause = limit.map(|l| format!("LIMIT {}", l)).unwrap_or_default();
        let query = format!(
            "SELECT checkpoint_id, checkpoint_ns, parent_checkpoint_id, state_values, 
                    next_nodes, metadata, created_at, at_seq
             FROM checkpoints 
             WHERE thread_id = ?1
             ORDER BY created_at ASC {}",
            limit_clause
        );

        let mut stmt = conn.prepare(&query)?;
        let rows = stmt.query_map(params![thread_id], |row| {
            let checkpoint_id: String = row.get(0)?;
            let checkpoint_ns: Option<String> = row.get(1)?;
            let parent_checkpoint_id: Option<String> = row.get(2)?;
            let state_bytes: Vec<u8> = row.get(3)?;
            let next_nodes_json: String = row.get(4)?;
            let metadata_json: String = row.get(5)?;
            let created_at_str: String = row.get(6)?;
            let at_seq: Option<i64> = row.get(7)?;

            // Deserialize state using serde_json (map to rusqlite::Error for closure return type)
            let values: S = serde_json::from_slice(&state_bytes).map_err(|_e| {
                rusqlite::Error::InvalidColumnType(
                    3,
                    "state_values".to_string(),
                    rusqlite::types::Type::Blob,
                )
            })?;

            // Deserialize next nodes and metadata
            let next: Vec<String> = serde_json::from_str(&next_nodes_json).map_err(|_e| {
                rusqlite::Error::InvalidColumnType(
                    4,
                    "next_nodes".to_string(),
                    rusqlite::types::Type::Text,
                )
            })?;
            let metadata: std::collections::HashMap<String, Value> =
                serde_json::from_str(&metadata_json).map_err(|_e| {
                    rusqlite::Error::InvalidColumnType(
                        5,
                        "metadata".to_string(),
                        rusqlite::types::Type::Text,
                    )
                })?;

            // Parse created_at
            let created_at = DateTime::parse_from_rfc3339(&created_at_str)
                .map_err(|_e| {
                    rusqlite::Error::InvalidColumnType(
                        7,
                        "created_at".to_string(),
                        rusqlite::types::Type::Text,
                    )
                })?
                .with_timezone(&Utc);

            // Build config
            let config = CheckpointConfig {
                thread_id: thread_id.to_string(),
                checkpoint_id: Some(checkpoint_id.clone()),
                checkpoint_ns,
            };

            // Build parent config if exists
            let parent_config = parent_checkpoint_id.map(|parent_id| CheckpointConfig {
                thread_id: thread_id.to_string(),
                checkpoint_id: Some(parent_id),
                checkpoint_ns: None,
            });

            Ok(StateSnapshot {
                values,
                next,
                config,
                metadata,
                created_at,
                parent_config,
                at_seq: at_seq.map(|seq| seq as u64),
            })
        })?;

        let mut snapshots = Vec::new();
        for row in rows {
            snapshots.push(row.map_err(|e| PersistenceError::DatabaseError(e.to_string()))?);
        }

        Ok(snapshots)
    }
}

#[cfg(all(test, feature = "sqlite-persistence"))]
mod tests {
    use super::*;
    use crate::graph::persistence::Checkpointer;
    use crate::graph::state::MessagesState;
    use crate::schemas::messages::Message;
    use std::fs;

    #[test]
    fn test_sqlite_saver_roundtrip_at_seq() {
        let saver = SqliteSaver::<MessagesState>::new_in_memory().unwrap();
        let rt = tokio::runtime::Runtime::new().unwrap();
        rt.block_on(async {
            let state = MessagesState::with_messages(vec![Message::new_ai_message("hello")]);
            let config = CheckpointConfig::new("thread-at-seq");
            let snapshot =
                StateSnapshot::new(state, vec!["node1".to_string()], config).with_at_seq(42);

            let checkpoint_id = saver.put("thread-at-seq", &snapshot).await.unwrap();
            let loaded = saver
                .get("thread-at-seq", Some(&checkpoint_id))
                .await
                .unwrap()
                .unwrap();

            assert_eq!(loaded.at_seq, Some(42));
        });
    }

    #[tokio::test]
    #[ignore = "SqliteSaver::new blocks; cannot run inside tokio runtime"]
    async fn test_sqlite_saver_file() {
        let db_path = "test_checkpoints.db";
        let _ = fs::remove_file(db_path);

        let saver = SqliteSaver::<MessagesState>::new(db_path).unwrap();

        let state = MessagesState::new();
        let config = CheckpointConfig::new("thread-1");
        let snapshot = StateSnapshot::new(state, vec!["node1".to_string()], config);

        let checkpoint_id = saver.put("thread-1", &snapshot).await.unwrap();
        assert!(!checkpoint_id.is_empty());

        let retrieved = saver.get("thread-1", None).await.unwrap();
        assert!(retrieved.is_some());
        assert_eq!(retrieved.unwrap().thread_id(), "thread-1");

        let list = saver.list("thread-1", None).await.unwrap();
        assert_eq!(list.len(), 1);

        let _ = fs::remove_file(db_path);
    }

    /// Put, get latest, get by id, list. Run with: cargo test -p oris-runtime --features sqlite-persistence -- --ignored
    #[tokio::test]
    #[ignore = "SQLite uses blocking I/O; run with --ignored"]
    async fn test_sqlite_saver_put_get_list() {
        let saver = SqliteSaver::<MessagesState>::new_in_memory().unwrap();
        let state = MessagesState::with_messages(vec![Message::new_ai_message("hello")]);
        let config = CheckpointConfig::new("thread-sqlite");
        let snapshot = StateSnapshot::new(state, vec!["node1".to_string()], config);

        let id1 = saver.put("thread-sqlite", &snapshot).await.unwrap();
        assert!(!id1.is_empty());

        let latest = saver.get("thread-sqlite", None).await.unwrap();
        assert!(latest.is_some());
        assert_eq!(latest.as_ref().unwrap().thread_id(), "thread-sqlite");
        assert_eq!(
            latest.unwrap().config.checkpoint_id.as_deref(),
            Some(id1.as_str())
        );

        let by_id = saver.get("thread-sqlite", Some(&id1)).await.unwrap();
        assert!(by_id.is_some());

        let list = saver.list("thread-sqlite", None).await.unwrap();
        assert_eq!(list.len(), 1);
    }

    #[tokio::test]
    #[ignore = "SQLite uses blocking I/O; run with --ignored"]
    async fn test_sqlite_saver_multiple_checkpoints_and_get_by_id() {
        let saver = SqliteSaver::<MessagesState>::new_in_memory().unwrap();
        let config = CheckpointConfig::new("thread-multi");

        let snap1 = StateSnapshot::new(
            MessagesState::with_messages(vec![Message::new_ai_message("one")]),
            vec!["node2".to_string()],
            config.clone(),
        );
        let id1 = saver.put("thread-multi", &snap1).await.unwrap();

        let mut config2 = config.clone();
        config2.checkpoint_id = Some(id1.clone());
        let snap2 = StateSnapshot::new(
            MessagesState::with_messages(vec![
                Message::new_ai_message("one"),
                Message::new_ai_message("two"),
            ]),
            vec!["END".to_string()],
            config2,
        );
        let id2 = saver.put("thread-multi", &snap2).await.unwrap();

        let list = saver.list("thread-multi", None).await.unwrap();
        assert_eq!(list.len(), 2);

        let latest = saver.get("thread-multi", None).await.unwrap().unwrap();
        assert_eq!(latest.config.checkpoint_id.as_deref(), Some(id2.as_str()));

        let first = saver
            .get("thread-multi", Some(&id1))
            .await
            .unwrap()
            .unwrap();
        assert_eq!(first.values.messages.len(), 1);
    }
}