do-memory-storage-turso 0.1.31

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
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
//! Phase 1 Multi-Dimension Validation Tests
//!
//! Comprehensive validation of multi-dimension vector support implementation.

use anyhow::Result;
use do_memory_core::embeddings::EmbeddingStorageBackend as _;
use do_memory_test_utils::multi_dimension::{MultiDimensionTestHarness, table_for_dimension};
use tracing::info;

// ============================================================================
// Task 1: Schema Validation Tests
// ============================================================================

#[tokio::test]
#[cfg_attr(not(feature = "turso_multi_dimension"), ignore)]
async fn phase1_task1_validate_all_dimension_tables_created() -> Result<()> {
    info!("=== Task 1: Validate all dimension tables created ===");
    let harness = MultiDimensionTestHarness::new().await?;

    // Verify we can store and retrieve embeddings of all supported dimensions
    let dimensions = [384, 1024, 1536, 3072, 512];

    for dimension in dimensions {
        let (episode, _embedding) = harness.create_episode_with_embedding(dimension, 42).await?;

        info!(
            "✓ Created episode with {}-dim embedding: {}",
            dimension, episode.episode_id
        );
    }

    info!("✓ All dimension tables created successfully");
    Ok(())
}

#[tokio::test]
#[cfg_attr(not(feature = "turso_multi_dimension"), ignore)]
async fn phase1_task1_validate_vector_indexes_created() -> Result<()> {
    info!("=== Task 1: Validate vector indexes created ===");
    let harness = MultiDimensionTestHarness::new().await?;

    // Test that we can run similarity search (uses vector index if available)
    let (_episode, embedding) = harness.create_episode_with_embedding(384, 42).await?;

    let results = harness.run_similarity_search(embedding, 5, 0.5).await?;

    assert!(
        !results.is_empty(),
        "Vector index should allow similarity search"
    );

    info!("✓ Vector indexes created successfully");
    info!("  Similarity search returned {} results", results.len());

    Ok(())
}

#[tokio::test]
#[cfg_attr(not(feature = "turso_multi_dimension"), ignore)]
async fn phase1_task1_validate_item_indexes_created() -> Result<()> {
    info!("=== Task 1: Validate item indexes created ===");
    let harness = MultiDimensionTestHarness::new().await?;

    let (episode, _embedding) = harness.create_episode_with_embedding(1536, 42).await?;

    // Verify we can retrieve embedding via harness
    let retrieved = harness
        .storage
        .get_episode_embedding(episode.episode_id)
        .await?;

    assert!(
        retrieved.is_some(),
        "Item index should allow embedding retrieval"
    );
    assert_eq!(
        retrieved.unwrap().len(),
        1536,
        "Retrieved embedding should be 1536-dimensional"
    );

    info!("✓ Item indexes created successfully");

    Ok(())
}

// ============================================================================
// Task 2: Routing Logic Validation Tests
// ============================================================================

#[tokio::test]
#[cfg_attr(not(feature = "turso_multi_dimension"), ignore)]
async fn phase1_task2_384_dimension_routing() -> Result<()> {
    info!("=== Task 2: Validate 384-dimension routing ===");
    let harness = MultiDimensionTestHarness::new().await?;

    let (episode, _embedding) = harness.create_episode_with_embedding(384, 42).await?;

    // Verify routing by checking expected table name
    let expected_table = table_for_dimension(384);
    assert_eq!(
        expected_table, "embeddings_384",
        "384-dim should route to embeddings_384"
    );

    // Verify we can retrieve embedding
    let retrieved = harness
        .storage
        .get_episode_embedding(episode.episode_id)
        .await?;
    assert!(
        retrieved.is_some(),
        "Should be able to retrieve 384-dim embedding"
    );

    info!("✓ 384-dimension embedding routed correctly");

    Ok(())
}

#[tokio::test]
#[cfg_attr(not(feature = "turso_multi_dimension"), ignore)]
async fn phase1_task2_1536_dimension_routing() -> Result<()> {
    info!("=== Task 2: Validate 1536-dimension routing ===");
    let harness = MultiDimensionTestHarness::new().await?;

    let (episode, _embedding) = harness.create_episode_with_embedding(1536, 42).await?;

    // Verify routing by checking expected table name
    let expected_table = table_for_dimension(1536);
    assert_eq!(
        expected_table, "embeddings_1536",
        "1536-dim should route to embeddings_1536"
    );

    // Verify we can retrieve embedding
    let retrieved = harness
        .storage
        .get_episode_embedding(episode.episode_id)
        .await?;
    assert!(
        retrieved.is_some(),
        "Should be able to retrieve 1536-dim embedding"
    );

    info!("✓ 1536-dimension embedding routed correctly");

    Ok(())
}

#[tokio::test]
#[cfg_attr(not(feature = "turso_multi_dimension"), ignore)]
async fn phase1_task2_unsupported_dimension_routing() -> Result<()> {
    info!("=== Task 2: Validate unsupported dimension routing ===");
    let harness = MultiDimensionTestHarness::new().await?;

    let (episode, _embedding) = harness.create_episode_with_embedding(512, 42).await?;

    // Verify routing by checking expected table name
    let expected_table = table_for_dimension(512);
    assert_eq!(
        expected_table, "embeddings_other",
        "512-dim should route to embeddings_other"
    );

    // Verify we can retrieve embedding
    let retrieved = harness
        .storage
        .get_episode_embedding(episode.episode_id)
        .await?;
    assert!(
        retrieved.is_some(),
        "Should be able to retrieve 512-dim embedding"
    );

    info!("✓ Unsupported dimension routed correctly to embeddings_other");

    Ok(())
}

#[tokio::test]
#[cfg_attr(not(feature = "turso_multi_dimension"), ignore)]
async fn phase1_task2_mixed_dimension_routing() -> Result<()> {
    info!("=== Task 2: Validate mixed dimension routing ===");
    let harness = MultiDimensionTestHarness::new().await?;

    let test_cases = [
        (384, "embeddings_384"),
        (1024, "embeddings_1024"),
        (1536, "embeddings_1536"),
        (3072, "embeddings_3072"),
        (500, "embeddings_other"),
    ];

    for (dimension, expected_table) in test_cases {
        let (episode, _embedding) = harness
            .create_episode_with_embedding(dimension, 42 + dimension as u64)
            .await?;

        // Verify routing by checking expected table name
        let actual_table = table_for_dimension(dimension);
        assert_eq!(
            actual_table, expected_table,
            "{}-dim should route to {}",
            dimension, expected_table
        );

        // Verify we can retrieve embedding
        let retrieved = harness
            .storage
            .get_episode_embedding(episode.episode_id)
            .await?;
        assert!(
            retrieved.is_some(),
            "Should be able to retrieve {}-dim embedding",
            dimension
        );

        info!(
            "✓ {}-dimension embedding routed to {}",
            dimension, expected_table
        );
    }

    info!("✓ Mixed dimension routing validated successfully");
    Ok(())
}

#[tokio::test]
#[cfg_attr(not(feature = "turso_multi_dimension"), ignore)]
async fn phase1_task2_native_vector_stored_for_supported() -> Result<()> {
    info!("=== Task 2: Validate native vectors stored for supported dimensions ===");
    let harness = MultiDimensionTestHarness::new().await?;

    let supported_dimensions = [384, 1024, 1536, 3072];

    for dimension in supported_dimensions {
        let (episode, embedding) = harness.create_episode_with_embedding(dimension, 42).await?;

        // Retrieve and verify dimension matches
        let retrieved = harness
            .storage
            .get_episode_embedding(episode.episode_id)
            .await?;
        assert!(
            retrieved.is_some(),
            "Native vector should be stored for {} dimension",
            dimension
        );

        let retrieved_embedding = retrieved.unwrap();
        assert_eq!(
            retrieved_embedding.len(),
            dimension,
            "Retrieved embedding should have {} dimensions",
            dimension
        );
        assert_eq!(
            retrieved_embedding, embedding,
            "Retrieved embedding should match original"
        );

        info!("✓ Native vector stored for {} dimension", dimension);
    }

    info!("✓ Native vectors stored for all supported dimensions");
    Ok(())
}

// ============================================================================
// Task 3: Provider Integration Validation Tests
// ============================================================================

#[tokio::test]
#[cfg_attr(not(feature = "turso_multi_dimension"), ignore)]
async fn phase1_task3_embedding_retrieval_by_dimension() -> Result<()> {
    info!("=== Task 3: Validate embedding retrieval by dimension ===");
    let harness = MultiDimensionTestHarness::new().await?;

    // Store embeddings of different dimensions
    let dimensions = [384, 1536, 500];

    for dimension in dimensions {
        let (episode, _embedding) = harness.create_episode_with_embedding(dimension, 42).await?;

        // Retrieve and verify dimension
        let retrieved = harness
            .storage
            .get_episode_embedding(episode.episode_id)
            .await?;

        assert!(
            retrieved.is_some(),
            "Should retrieve {}-dim embedding",
            dimension
        );

        let retrieved_embedding = retrieved.unwrap();
        assert_eq!(
            retrieved_embedding.len(),
            dimension,
            "Retrieved embedding should have {} dimensions",
            dimension
        );

        info!("✓ Retrieved {}-dimension embedding correctly", dimension);
    }

    info!("✓ Embedding retrieval validated for all dimensions");
    Ok(())
}

// ============================================================================
// Task 4: Vector Search Validation Tests
// ============================================================================

#[tokio::test]
#[cfg_attr(not(feature = "turso_multi_dimension"), ignore)]
async fn phase1_task4_384_dimension_vector_search() -> Result<()> {
    info!("=== Task 4: Validate 384-dimension vector search ===");
    let harness = MultiDimensionTestHarness::new().await?;

    // Create base episode with embedding
    let (base_episode, base_embedding) = harness.create_episode_with_embedding(384, 42).await?;

    // Create additional episodes
    for i in 1..=5 {
        let (_episode, _embedding) = harness
            .create_episode_with_embedding(384, 100 + i as u64)
            .await?;
    }

    // Run similarity search
    let results = harness
        .run_similarity_search(base_embedding, 5, 0.5)
        .await?;

    assert!(!results.is_empty(), "Search should return results");
    assert!(
        results.len() <= 5,
        "Should return at most 5 results, got {}",
        results.len()
    );

    // Check that base episode is found
    let base_found = results.iter().any(|(id, _)| *id == base_episode.episode_id);
    assert!(base_found, "Base episode should be found in search results");

    // Check similarity scores are in valid range
    for (_, similarity) in &results {
        assert!(
            (0.0..=1.0).contains(similarity),
            "Similarity score should be between 0 and 1, got {}",
            similarity
        );
    }

    info!("✓ 384-dimension vector search works correctly");
    info!("  Found {} results", results.len());

    Ok(())
}

#[tokio::test]
#[cfg_attr(not(feature = "turso_multi_dimension"), ignore)]
async fn phase1_task4_1536_dimension_vector_search() -> Result<()> {
    info!("=== Task 4: Validate 1536-dimension vector search ===");
    let harness = MultiDimensionTestHarness::new().await?;

    // Create base episode with embedding
    let (base_episode, base_embedding) = harness.create_episode_with_embedding(1536, 42).await?;

    // Create additional episodes
    for i in 1..=5 {
        let (_episode, _embedding) = harness
            .create_episode_with_embedding(1536, 100 + i as u64)
            .await?;
    }

    // Run similarity search
    let results = harness
        .run_similarity_search(base_embedding, 5, 0.5)
        .await?;

    assert!(!results.is_empty(), "Search should return results");
    let base_found = results.iter().any(|(id, _)| *id == base_episode.episode_id);
    assert!(base_found, "Base episode should be found in search results");

    info!("✓ 1536-dimension vector search works correctly");
    info!("  Found {} results", results.len());

    Ok(())
}

#[tokio::test]
#[cfg_attr(not(feature = "turso_multi_dimension"), ignore)]
async fn phase1_task4_3072_dimension_vector_search() -> Result<()> {
    info!("=== Task 4: Validate 3072-dimension vector search ===");
    let harness = MultiDimensionTestHarness::new().await?;

    // Create base episode with embedding
    let (base_episode, base_embedding) = harness.create_episode_with_embedding(3072, 42).await?;

    // Create additional episodes
    for i in 1..=5 {
        let (_episode, _embedding) = harness
            .create_episode_with_embedding(3072, 100 + i as u64)
            .await?;
    }

    // Run similarity search
    let results = harness
        .run_similarity_search(base_embedding, 5, 0.5)
        .await?;

    assert!(!results.is_empty(), "Search should return results");
    let base_found = results.iter().any(|(id, _)| *id == base_episode.episode_id);
    assert!(base_found, "Base episode should be found in search results");

    info!("✓ 3072-dimension vector search works correctly");
    info!("  Found {} results", results.len());

    Ok(())
}

#[tokio::test]
#[cfg_attr(not(feature = "turso_multi_dimension"), ignore)]
async fn phase1_task4_unsupported_dimension_fallback() -> Result<()> {
    info!("=== Task 4: Validate unsupported dimension fallback ===");

    let harness = MultiDimensionTestHarness::new().await?;

    // Create episodes with 512-dim embeddings (unsupported)
    let (_base_episode, base_embedding) = harness.create_episode_with_embedding(512, 42).await?;

    for i in 1..=3 {
        let (_episode, _embedding) = harness
            .create_episode_with_embedding(512, 100 + i as u64)
            .await?;
    }

    // Run similarity search - should not crash even for unsupported dimensions
    let results = harness.run_similarity_search(base_embedding, 3, 0.5).await;

    // For unsupported dimensions, search may fail or return empty results,
    // but it should handle the case gracefully
    match results {
        Ok(results) => {
            info!(
                "✓ Search for unsupported dimension returned {} results",
                results.len()
            );
        }
        Err(e) => {
            // This is acceptable - unsupported dimensions may not support vector search
            info!("✓ Unsupported dimension handled gracefully: {}", e);
        }
    }

    Ok(())
}

#[tokio::test]
#[cfg_attr(not(feature = "turso_multi_dimension"), ignore)]
async fn phase1_task4_cross_dimension_isolation() -> Result<()> {
    info!("=== Task 4: Validate cross-dimension isolation ===");
    let harness = MultiDimensionTestHarness::new().await?;

    // Create episodes with different dimension embeddings
    let (episode_384, embedding_384) = harness.create_episode_with_embedding(384, 42).await?;
    let (episode_1536, embedding_1536) = harness.create_episode_with_embedding(1536, 43).await?;

    // Search with 384-dim embedding
    let results_384 = harness
        .run_similarity_search(embedding_384, 10, 0.5)
        .await?;

    let found_1536_in_384_search = results_384
        .iter()
        .any(|(id, _)| *id == episode_1536.episode_id);

    assert!(
        !found_1536_in_384_search,
        "384-dim search should not find 1536-dim episodes"
    );

    // Search with 1536-dim embedding
    let results_1536 = harness
        .run_similarity_search(embedding_1536, 10, 0.5)
        .await?;

    let found_384_in_1536_search = results_1536
        .iter()
        .any(|(id, _)| *id == episode_384.episode_id);

    assert!(
        !found_384_in_1536_search,
        "1536-dim search should not find 384-dim episodes"
    );

    info!("✓ Cross-dimension isolation works correctly");

    Ok(())
}