llm-memory-graph 0.1.0

Graph-based context-tracking and prompt-lineage database for LLM systems
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
//! Async Sled-based storage backend implementation using Tokio
//!
//! This module provides an async wrapper around the synchronous SledBackend,
//! using `tokio::task::spawn_blocking` to run blocking operations on a dedicated
//! thread pool without blocking the async runtime.

use super::{AsyncStorageBackend, SerializationFormat, SledBackend, StorageBackend, StorageStats};
use crate::error::Result;
use crate::types::{Edge, EdgeId, Node, NodeId, SessionId};
use async_trait::async_trait;
use std::path::Path;
use std::sync::Arc;

/// Async wrapper around Sled-based storage backend
///
/// This struct provides async versions of all storage operations by wrapping
/// the synchronous SledBackend and using Tokio's blocking task pool.
#[derive(Clone)]
pub struct AsyncSledBackend {
    /// Shared reference to the underlying synchronous backend
    inner: Arc<SledBackend>,
}

impl AsyncSledBackend {
    /// Open or create a new async Sled backend at the specified path
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use llm_memory_graph::storage::AsyncSledBackend;
    ///
    /// #[tokio::main]
    /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
    ///     let backend = AsyncSledBackend::open("./data/graph.db").await?;
    ///     Ok(())
    /// }
    /// ```
    pub async fn open<P: AsRef<Path>>(path: P) -> Result<Self> {
        let path_buf = path.as_ref().to_path_buf();

        // Run the synchronous open operation in a blocking task
        let inner = tokio::task::spawn_blocking(move || SledBackend::open(path_buf))
            .await
            .map_err(|e| crate::error::Error::RuntimeError(e.to_string()))??;

        Ok(Self {
            inner: Arc::new(inner),
        })
    }

    /// Open with a custom serialization format
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use llm_memory_graph::storage::{AsyncSledBackend, SerializationFormat};
    ///
    /// #[tokio::main]
    /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
    ///     let backend = AsyncSledBackend::open_with_format(
    ///         "./data/graph.db",
    ///         SerializationFormat::Json
    ///     ).await?;
    ///     Ok(())
    /// }
    /// ```
    pub async fn open_with_format<P: AsRef<Path>>(
        path: P,
        format: SerializationFormat,
    ) -> Result<Self> {
        let path_buf = path.as_ref().to_path_buf();

        let inner =
            tokio::task::spawn_blocking(move || SledBackend::open_with_format(path_buf, format))
                .await
                .map_err(|e| crate::error::Error::RuntimeError(e.to_string()))??;

        Ok(Self {
            inner: Arc::new(inner),
        })
    }
}

#[async_trait]
impl AsyncStorageBackend for AsyncSledBackend {
    async fn store_node(&self, node: &Node) -> Result<()> {
        let inner = Arc::clone(&self.inner);
        let node = node.clone();

        tokio::task::spawn_blocking(move || inner.store_node(&node))
            .await
            .map_err(|e| crate::error::Error::RuntimeError(e.to_string()))?
    }

    async fn get_node(&self, id: &NodeId) -> Result<Option<Node>> {
        let inner = Arc::clone(&self.inner);
        let id = *id;

        tokio::task::spawn_blocking(move || inner.get_node(&id))
            .await
            .map_err(|e| crate::error::Error::RuntimeError(e.to_string()))?
    }

    async fn delete_node(&self, id: &NodeId) -> Result<()> {
        let inner = Arc::clone(&self.inner);
        let id = *id;

        tokio::task::spawn_blocking(move || inner.delete_node(&id))
            .await
            .map_err(|e| crate::error::Error::RuntimeError(e.to_string()))?
    }

    async fn store_edge(&self, edge: &Edge) -> Result<()> {
        let inner = Arc::clone(&self.inner);
        let edge = edge.clone();

        tokio::task::spawn_blocking(move || inner.store_edge(&edge))
            .await
            .map_err(|e| crate::error::Error::RuntimeError(e.to_string()))?
    }

    async fn get_edge(&self, id: &EdgeId) -> Result<Option<Edge>> {
        let inner = Arc::clone(&self.inner);
        let id = *id;

        tokio::task::spawn_blocking(move || inner.get_edge(&id))
            .await
            .map_err(|e| crate::error::Error::RuntimeError(e.to_string()))?
    }

    async fn delete_edge(&self, id: &EdgeId) -> Result<()> {
        let inner = Arc::clone(&self.inner);
        let id = *id;

        tokio::task::spawn_blocking(move || inner.delete_edge(&id))
            .await
            .map_err(|e| crate::error::Error::RuntimeError(e.to_string()))?
    }

    async fn get_session_nodes(&self, session_id: &SessionId) -> Result<Vec<Node>> {
        let inner = Arc::clone(&self.inner);
        let session_id = *session_id;

        tokio::task::spawn_blocking(move || inner.get_session_nodes(&session_id))
            .await
            .map_err(|e| crate::error::Error::RuntimeError(e.to_string()))?
    }

    async fn get_outgoing_edges(&self, node_id: &NodeId) -> Result<Vec<Edge>> {
        let inner = Arc::clone(&self.inner);
        let node_id = *node_id;

        tokio::task::spawn_blocking(move || inner.get_outgoing_edges(&node_id))
            .await
            .map_err(|e| crate::error::Error::RuntimeError(e.to_string()))?
    }

    async fn get_incoming_edges(&self, node_id: &NodeId) -> Result<Vec<Edge>> {
        let inner = Arc::clone(&self.inner);
        let node_id = *node_id;

        tokio::task::spawn_blocking(move || inner.get_incoming_edges(&node_id))
            .await
            .map_err(|e| crate::error::Error::RuntimeError(e.to_string()))?
    }

    async fn flush(&self) -> Result<()> {
        let inner = Arc::clone(&self.inner);

        tokio::task::spawn_blocking(move || inner.flush())
            .await
            .map_err(|e| crate::error::Error::RuntimeError(e.to_string()))?
    }

    async fn stats(&self) -> Result<StorageStats> {
        let inner = Arc::clone(&self.inner);

        tokio::task::spawn_blocking(move || inner.stats())
            .await
            .map_err(|e| crate::error::Error::RuntimeError(e.to_string()))?
    }

    async fn store_nodes_batch(&self, nodes: &[Node]) -> Result<Vec<NodeId>> {
        let inner = Arc::clone(&self.inner);
        let nodes = nodes.to_vec();

        tokio::task::spawn_blocking(move || {
            let mut ids = Vec::with_capacity(nodes.len());
            for node in &nodes {
                inner.store_node(node)?;
                ids.push(node.id());
            }
            Ok(ids)
        })
        .await
        .map_err(|e| crate::error::Error::RuntimeError(e.to_string()))?
    }

    async fn store_edges_batch(&self, edges: &[Edge]) -> Result<Vec<EdgeId>> {
        let inner = Arc::clone(&self.inner);
        let edges = edges.to_vec();

        tokio::task::spawn_blocking(move || {
            let mut ids = Vec::with_capacity(edges.len());
            for edge in &edges {
                inner.store_edge(edge)?;
                ids.push(edge.id);
            }
            Ok(ids)
        })
        .await
        .map_err(|e| crate::error::Error::RuntimeError(e.to_string()))?
    }

    fn get_session_nodes_stream(
        &self,
        session_id: &SessionId,
    ) -> std::pin::Pin<Box<dyn futures::stream::Stream<Item = Result<Node>> + Send + '_>> {
        let inner = Arc::clone(&self.inner);
        let session_id = *session_id;

        Box::pin(async_stream::stream! {
            // Load nodes in a blocking task, but stream them out
            // This provides some memory efficiency by not holding all nodes in memory at once
            let result = tokio::task::spawn_blocking(move || {
                inner.get_session_nodes(&session_id)
            })
            .await
            .map_err(|e| crate::error::Error::RuntimeError(e.to_string()));

            match result {
                Ok(Ok(nodes)) => {
                    // Stream nodes out one at a time
                    for node in nodes {
                        yield Ok(node);
                    }
                }
                Ok(Err(e)) => yield Err(e),
                Err(e) => yield Err(e),
            }
        })
    }

    async fn count_session_nodes(&self, session_id: &SessionId) -> Result<usize> {
        let inner = Arc::clone(&self.inner);
        let session_id = *session_id;

        tokio::task::spawn_blocking(move || {
            inner
                .get_session_nodes(&session_id)
                .map(|nodes| nodes.len())
        })
        .await
        .map_err(|e| crate::error::Error::RuntimeError(e.to_string()))?
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::types::{ConversationSession, PromptNode};
    use tempfile::tempdir;

    #[tokio::test]
    async fn test_async_backend_creation() {
        let dir = tempdir().unwrap();
        let backend = AsyncSledBackend::open(dir.path()).await.unwrap();

        // Should be able to get stats
        let stats = backend.stats().await.unwrap();
        assert_eq!(stats.node_count, 0);
    }

    #[tokio::test]
    async fn test_async_node_operations() {
        let dir = tempdir().unwrap();
        let backend = AsyncSledBackend::open(dir.path()).await.unwrap();

        // Create and store a session
        let session = ConversationSession::new();
        backend
            .store_node(&Node::Session(session.clone()))
            .await
            .unwrap();

        // Retrieve it
        let retrieved = backend.get_node(&session.node_id).await.unwrap();
        assert!(retrieved.is_some());

        // Check stats
        let stats = backend.stats().await.unwrap();
        assert_eq!(stats.node_count, 1);
    }

    #[tokio::test]
    async fn test_concurrent_operations() {
        let dir = tempdir().unwrap();
        let backend = AsyncSledBackend::open(dir.path()).await.unwrap();

        let session = ConversationSession::new();
        backend
            .store_node(&Node::Session(session.clone()))
            .await
            .unwrap();

        // Perform 100 concurrent write operations
        let mut handles = vec![];
        for i in 0..100 {
            let backend_clone = backend.clone();
            let session_id = session.id;

            let handle = tokio::spawn(async move {
                let prompt = PromptNode::new(session_id, format!("Prompt {}", i));
                backend_clone.store_node(&Node::Prompt(prompt)).await
            });

            handles.push(handle);
        }

        // Wait for all operations to complete
        for handle in handles {
            handle.await.unwrap().unwrap();
        }

        // Verify all prompts were stored
        let stats = backend.stats().await.unwrap();
        assert_eq!(stats.node_count, 101); // 1 session + 100 prompts
    }

    #[tokio::test]
    async fn test_batch_operations() {
        let dir = tempdir().unwrap();
        let backend = AsyncSledBackend::open(dir.path()).await.unwrap();

        let session = ConversationSession::new();

        // Create multiple nodes
        let mut nodes = vec![Node::Session(session.clone())];
        for i in 0..10 {
            let prompt = PromptNode::new(session.id, format!("Prompt {}", i));
            nodes.push(Node::Prompt(prompt));
        }

        // Batch store
        let ids = backend.store_nodes_batch(&nodes).await.unwrap();
        assert_eq!(ids.len(), 11);

        // Verify stats
        let stats = backend.stats().await.unwrap();
        assert_eq!(stats.node_count, 11);
    }

    #[tokio::test]
    async fn test_session_nodes_streaming() {
        use crate::storage::AsyncStorageBackend;
        use futures::stream::StreamExt;

        let dir = tempdir().unwrap();
        let backend = AsyncSledBackend::open(dir.path()).await.unwrap();

        let session = ConversationSession::new();
        backend
            .store_node(&Node::Session(session.clone()))
            .await
            .unwrap();

        // Add 20 prompts
        for i in 0..20 {
            let prompt = PromptNode::new(session.id, format!("Prompt {}", i));
            backend.store_node(&Node::Prompt(prompt)).await.unwrap();
        }

        // Stream nodes
        let mut stream = backend.get_session_nodes_stream(&session.id);
        let mut count = 0;
        while let Some(result) = stream.next().await {
            result.unwrap();
            count += 1;
        }

        assert_eq!(count, 21); // 1 session + 20 prompts
    }

    #[tokio::test]
    async fn test_count_session_nodes() {
        use crate::storage::AsyncStorageBackend;

        let dir = tempdir().unwrap();
        let backend = AsyncSledBackend::open(dir.path()).await.unwrap();

        let session = ConversationSession::new();
        backend
            .store_node(&Node::Session(session.clone()))
            .await
            .unwrap();

        // Add 15 prompts
        for i in 0..15 {
            let prompt = PromptNode::new(session.id, format!("Prompt {}", i));
            backend.store_node(&Node::Prompt(prompt)).await.unwrap();
        }

        // Count without loading
        let count = backend.count_session_nodes(&session.id).await.unwrap();
        assert_eq!(count, 16); // 1 session + 15 prompts
    }

    #[tokio::test]
    async fn test_streaming_vs_batch() {
        use crate::storage::AsyncStorageBackend;
        use futures::stream::StreamExt;

        let dir = tempdir().unwrap();
        let backend = AsyncSledBackend::open(dir.path()).await.unwrap();

        let session = ConversationSession::new();
        backend
            .store_node(&Node::Session(session.clone()))
            .await
            .unwrap();

        // Add 50 prompts
        for i in 0..50 {
            let prompt = PromptNode::new(session.id, format!("Prompt {}", i));
            backend.store_node(&Node::Prompt(prompt)).await.unwrap();
        }

        // Get via batch
        let batch_nodes = backend.get_session_nodes(&session.id).await.unwrap();

        // Get via streaming
        let mut stream = backend.get_session_nodes_stream(&session.id);
        let mut stream_nodes = Vec::new();
        while let Some(result) = stream.next().await {
            stream_nodes.push(result.unwrap());
        }

        // Both should return same nodes
        assert_eq!(batch_nodes.len(), stream_nodes.len());
        assert_eq!(batch_nodes.len(), 51); // 1 session + 50 prompts
    }
}