do-memory-storage-turso 0.1.34

Turso/libSQL storage backend for the do-memory-core episodic learning system
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
use super::*;
use do_memory_core::episode::Episode;
use do_memory_core::{TaskContext, TaskType};
use tempfile::TempDir;

async fn create_test_storage() -> (TursoStorage, TempDir) {
    let dir = tempfile::tempdir().expect("Failed to create temp dir");
    let path = dir.path().join("test_memory.db");
    let storage = TursoStorage::new_local(&path)
        .await
        .expect("Failed to create local storage");
    storage.initialize_schema().await.unwrap();
    (storage, dir)
}

async fn create_test_episode(storage: &TursoStorage) -> Uuid {
    let episode = Episode::new(
        "Test episode".to_string(),
        TaskContext::default(),
        TaskType::Testing,
    );
    let episode_id = episode.episode_id;
    storage
        .store_episode(&episode)
        .await
        .expect("Failed to store episode");
    episode_id
}

#[tokio::test]
async fn test_add_relationship() {
    let (storage, _dir) = create_test_storage().await;
    let from_id = create_test_episode(&storage).await;
    let to_id = create_test_episode(&storage).await;

    let metadata = RelationshipMetadata::with_reason("Test relationship".to_string());
    let rel_id = storage
        .add_relationship(from_id, to_id, RelationshipType::ParentChild, metadata)
        .await
        .expect("Failed to add relationship");

    assert_ne!(rel_id, Uuid::nil());
}

#[tokio::test]
async fn test_store_relationship() {
    let (storage, _dir) = create_test_storage().await;
    let from_id = create_test_episode(&storage).await;
    let to_id = create_test_episode(&storage).await;

    let metadata = RelationshipMetadata::with_reason("Test store_relationship".to_string());
    let relationship =
        EpisodeRelationship::new(from_id, to_id, RelationshipType::RelatedTo, metadata);

    storage
        .store_relationship(&relationship)
        .await
        .expect("Failed to store relationship");

    let rels = storage
        .get_relationships(from_id, Direction::Outgoing)
        .await
        .expect("Failed to get relationships");
    assert_eq!(rels.len(), 1);
    assert_eq!(rels[0].id, relationship.id);
}

#[tokio::test]
async fn test_get_relationship_by_id() {
    let (storage, _dir) = create_test_storage().await;
    let from_id = create_test_episode(&storage).await;
    let to_id = create_test_episode(&storage).await;

    let metadata = RelationshipMetadata::with_reason("ID lookup test".to_string());
    let rel_id = storage
        .add_relationship(from_id, to_id, RelationshipType::DependsOn, metadata)
        .await
        .expect("Failed to add relationship");

    let retrieved = storage
        .get_relationship_by_id(rel_id)
        .await
        .expect("Failed to get relationship by ID");

    assert!(retrieved.is_some());
    assert_eq!(retrieved.unwrap().id, rel_id);
}

#[tokio::test]
async fn test_get_all_relationships() {
    let (storage, _dir) = create_test_storage().await;
    let ep1 = create_test_episode(&storage).await;
    let ep2 = create_test_episode(&storage).await;

    storage
        .add_relationship(
            ep1,
            ep2,
            RelationshipType::DependsOn,
            RelationshipMetadata::default(),
        )
        .await
        .unwrap();

    let all = storage.get_all_relationships().await.unwrap();
    assert!(!all.is_empty());
}

#[tokio::test]
async fn test_get_relationships() {
    let (storage, _dir) = create_test_storage().await;
    let from_id = create_test_episode(&storage).await;
    let to_id = create_test_episode(&storage).await;

    let metadata = RelationshipMetadata::with_reason("Test".to_string());
    storage
        .add_relationship(from_id, to_id, RelationshipType::DependsOn, metadata)
        .await
        .expect("Failed to add relationship");

    let outgoing = storage
        .get_relationships(from_id, Direction::Outgoing)
        .await
        .expect("Failed to get outgoing relationships");
    assert_eq!(outgoing.len(), 1);
    assert_eq!(outgoing[0].from_episode_id, from_id);
    assert_eq!(outgoing[0].to_episode_id, to_id);

    let incoming = storage
        .get_relationships(to_id, Direction::Incoming)
        .await
        .expect("Failed to get incoming relationships");
    assert_eq!(incoming.len(), 1);

    // Test Direction::Both
    let both = storage
        .get_relationships(from_id, Direction::Both)
        .await
        .unwrap();
    assert_eq!(both.len(), 1);
}

#[tokio::test]
async fn test_remove_relationship() {
    let (storage, _dir) = create_test_storage().await;
    let from_id = create_test_episode(&storage).await;
    let to_id = create_test_episode(&storage).await;

    let metadata = RelationshipMetadata::with_reason("Test".to_string());
    let rel_id = storage
        .add_relationship(from_id, to_id, RelationshipType::ParentChild, metadata)
        .await
        .expect("Failed to add relationship");

    storage
        .remove_relationship(rel_id)
        .await
        .expect("Failed to remove relationship");

    let relationships = storage
        .get_relationships(from_id, Direction::Outgoing)
        .await
        .expect("Failed to get relationships");
    assert_eq!(relationships.len(), 0);
}

#[tokio::test]
async fn test_get_relationships_by_type() {
    let (storage, _dir) = create_test_storage().await;
    let from_id = create_test_episode(&storage).await;
    let to_id = create_test_episode(&storage).await;

    storage
        .add_relationship(
            from_id,
            to_id,
            RelationshipType::DependsOn,
            RelationshipMetadata::default(),
        )
        .await
        .unwrap();

    let rels = storage
        .get_relationships_by_type(from_id, RelationshipType::DependsOn, Direction::Outgoing)
        .await
        .unwrap();
    assert_eq!(rels.len(), 1);

    let incoming = storage
        .get_relationships_by_type(to_id, RelationshipType::DependsOn, Direction::Incoming)
        .await
        .unwrap();
    assert_eq!(incoming.len(), 1);

    let both = storage
        .get_relationships_by_type(from_id, RelationshipType::DependsOn, Direction::Both)
        .await
        .unwrap();
    assert_eq!(both.len(), 1);

    let other = storage
        .get_relationships_by_type(from_id, RelationshipType::ParentChild, Direction::Outgoing)
        .await
        .unwrap();
    assert_eq!(other.len(), 0);
}

#[tokio::test]
async fn test_relationship_exists() {
    let (storage, _dir) = create_test_storage().await;
    let from_id = create_test_episode(&storage).await;
    let to_id = create_test_episode(&storage).await;

    let exists_before = storage
        .relationship_exists(from_id, to_id, RelationshipType::DependsOn)
        .await
        .expect("Failed to check relationship existence");
    assert!(!exists_before);

    let metadata = RelationshipMetadata::with_reason("Test".to_string());
    storage
        .add_relationship(from_id, to_id, RelationshipType::DependsOn, metadata)
        .await
        .expect("Failed to add relationship");

    let exists_after = storage
        .relationship_exists(from_id, to_id, RelationshipType::DependsOn)
        .await
        .expect("Failed to check relationship existence");
    assert!(exists_after);
}

#[tokio::test]
async fn test_get_dependent_episodes() {
    let (storage, _dir) = create_test_storage().await;
    let ep1 = create_test_episode(&storage).await;
    let ep2 = create_test_episode(&storage).await;

    // ep1 depends on ep2 -> ep2 is depended on by ep1
    storage
        .add_relationship(
            ep1,
            ep2,
            RelationshipType::DependsOn,
            RelationshipMetadata::default(),
        )
        .await
        .unwrap();

    let dependents = storage.get_dependent_episodes(ep2).await.unwrap();
    assert_eq!(dependents.len(), 1);
    assert_eq!(dependents[0], ep1);
}

#[tokio::test]
async fn test_get_dependencies() {
    let (storage, _dir) = create_test_storage().await;
    let ep1 = create_test_episode(&storage).await;
    let ep2 = create_test_episode(&storage).await;
    let ep3 = create_test_episode(&storage).await;

    // ep1 depends on ep2 and ep3
    let metadata = RelationshipMetadata::with_reason("Dependency".to_string());
    storage
        .add_relationship(ep1, ep2, RelationshipType::DependsOn, metadata.clone())
        .await
        .expect("Failed to add relationship");
    storage
        .add_relationship(ep1, ep3, RelationshipType::DependsOn, metadata)
        .await
        .expect("Failed to add relationship");

    let deps = storage
        .get_dependencies(ep1)
        .await
        .expect("Failed to get dependencies");
    assert_eq!(deps.len(), 2);
    assert!(deps.contains(&ep2));
    assert!(deps.contains(&ep3));
}

#[tokio::test]
async fn test_weighted_relationships() {
    let (storage, _dir) = create_test_storage().await;
    let ep1 = create_test_episode(&storage).await;
    let ep2 = create_test_episode(&storage).await;

    let mut metadata = RelationshipMetadata::with_reason("Weighted edge".to_string());
    metadata.weight = Some(0.75);

    storage
        .add_relationship(ep1, ep2, RelationshipType::RelatedTo, metadata)
        .await
        .expect("Failed to add weighted relationship");

    let rels = storage
        .get_relationships(ep1, Direction::Outgoing)
        .await
        .expect("Failed to get relationships");
    assert_eq!(rels.len(), 1);
    assert_eq!(rels[0].metadata.weight, Some(0.75));
}

#[tokio::test]
async fn test_relationship_minimal_metadata() {
    let (storage, _dir) = create_test_storage().await;
    let ep1 = create_test_episode(&storage).await;
    let ep2 = create_test_episode(&storage).await;

    // Test with minimal metadata (exercising NULL paths in parameterized queries)
    let metadata = RelationshipMetadata::default();
    let rel_id = storage
        .add_relationship(ep1, ep2, RelationshipType::RelatedTo, metadata)
        .await
        .unwrap();

    let retrieved = storage
        .get_relationship_by_id(rel_id)
        .await
        .unwrap()
        .unwrap();
    assert!(retrieved.metadata.reason.is_none());
    assert!(retrieved.metadata.created_by.is_none());
    assert!(retrieved.metadata.priority.is_none());
    assert!(retrieved.metadata.weight.is_none());
}

#[tokio::test]
async fn test_episode_pattern_relationships() {
    let (storage, _dir) = create_test_storage().await;
    let ep_id = create_test_episode(&storage).await;
    let pt_id = Uuid::new_v4();

    let pattern = do_memory_core::Pattern::ToolSequence {
        id: pt_id,
        tools: vec!["test_tool".to_string()],
        context: TaskContext::default(),
        success_rate: 1.0,
        avg_latency: chrono::Duration::milliseconds(100),
        occurrence_count: 1,
        effectiveness: Default::default(),
    };
    storage
        .store_pattern(&pattern)
        .await
        .expect("Failed to store pattern");

    let mut metadata = RelationshipMetadata::new();
    metadata.weight = Some(0.9);

    let rel = EpisodePatternRelationship::new(ep_id, pt_id, RelationshipType::References, metadata);

    storage
        .store_episode_pattern_relationship(&rel)
        .await
        .expect("Failed to store episode-pattern relationship");

    let rels = storage
        .get_episode_pattern_relationships(ep_id)
        .await
        .expect("Failed to get episode-pattern relationships");
    assert_eq!(rels.len(), 1);
    assert_eq!(rels[0].pattern_id, pt_id);
    assert_eq!(rels[0].metadata.weight, Some(0.9));
}

#[tokio::test]
async fn test_get_weighted_neighbors() {
    let (storage, _dir) = create_test_storage().await;
    let ep1 = create_test_episode(&storage).await;
    let ep2 = create_test_episode(&storage).await;
    let pt1 = Uuid::new_v4();

    // 1. Add episode-episode relationship
    let mut meta1 = RelationshipMetadata::new();
    meta1.weight = Some(0.6);
    storage
        .add_relationship(ep1, ep2, RelationshipType::RelatedTo, meta1)
        .await
        .unwrap();

    // 2. Add episode-pattern relationship
    let pattern = do_memory_core::Pattern::ToolSequence {
        id: pt1,
        tools: vec!["tool".to_string()],
        context: TaskContext::default(),
        success_rate: 1.0,
        avg_latency: chrono::Duration::milliseconds(50),
        occurrence_count: 1,
        effectiveness: Default::default(),
    };
    storage.store_pattern(&pattern).await.unwrap();

    let mut meta2 = RelationshipMetadata::new();
    meta2.weight = Some(0.4);
    let ep_pt_rel = EpisodePatternRelationship::new(ep1, pt1, RelationshipType::References, meta2);
    storage
        .store_episode_pattern_relationship(&ep_pt_rel)
        .await
        .unwrap();

    let neighbors = storage
        .get_weighted_neighbors(ep1)
        .await
        .expect("Failed to get neighbors");
    assert_eq!(neighbors.len(), 2);

    let ep2_neighbor = neighbors
        .iter()
        .find(|(id, _, is_pt)| *id == ep2 && !*is_pt)
        .unwrap();
    let pt1_neighbor = neighbors
        .iter()
        .find(|(id, _, is_pt)| *id == pt1 && *is_pt)
        .unwrap();

    assert_eq!(ep2_neighbor.1, 0.6);
    assert_eq!(pt1_neighbor.1, 0.4);
}