vipune 0.19.1

A minimal memory layer for AI agents
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
//! Tests for search operations.

use crate::memory::lifecycle::{MemoryStatus, MemoryType};
use crate::memory::{MemoryStore, UpdateParams};
use crate::sqlite::Database;

#[test]
fn test_search_basic() {
    let (_dir, path) = MemoryStore::test_db_path();

    let db = Database::open(&path).unwrap();
    let embedding_match = vec![1.0f32; 384];
    let embedding_other = vec![0.0f32; 384];

    let id_match = db
        .insert(
            "test-project",
            "matching content",
            &embedding_match,
            None,
            "fact",
            "active",
        )
        .unwrap();
    db.insert(
        "test-project",
        "other content",
        &embedding_other,
        None,
        "fact",
        "active",
    )
    .unwrap();

    let results = db
        .search("test-project", &embedding_match, 5, None, None)
        .unwrap();
    assert!(!results.is_empty());
    assert_eq!(results[0].id, id_match);
    assert!(results[0].similarity.unwrap() > 0.9);
}

#[test]
fn test_search_sorting() {
    let (_dir, path) = MemoryStore::test_db_path();

    let db = Database::open(&path).unwrap();
    let embedding_high = vec![1.0f32; 384];
    let embedding_medium = vec![0.5f32; 384];
    let embedding_low = vec![0.0f32; 384];

    db.insert(
        "test-project",
        "low",
        &embedding_low,
        None,
        "fact",
        "active",
    )
    .unwrap();
    db.insert(
        "test-project",
        "high",
        &embedding_high,
        None,
        "fact",
        "active",
    )
    .unwrap();
    db.insert(
        "test-project",
        "medium",
        &embedding_medium,
        None,
        "fact",
        "active",
    )
    .unwrap();

    let results = db
        .search("test-project", &embedding_high, 10, None, None)
        .unwrap();
    assert!(results[0].similarity.unwrap() >= results[1].similarity.unwrap());
    assert!(results[1].similarity.unwrap() >= results[2].similarity.unwrap());
}

#[test]
fn test_negative_similarity() {
    let (_dir, path) = MemoryStore::test_db_path();

    let db = Database::open(&path).unwrap();
    let embedding_pos = vec![1.0f32; 384];
    let embedding_neg = vec![-1.0f32; 384];

    db.insert(
        "test-project",
        "positive",
        &embedding_pos,
        None,
        "fact",
        "active",
    )
    .unwrap();

    let results = db
        .search("test-project", &embedding_neg, 10, None, None)
        .unwrap();
    assert!(!results.is_empty());
    assert!(results[0].similarity.unwrap() < 0.0);
}

#[test]
fn test_hybrid_search_basic() {
    let (_dir, path) = MemoryStore::test_db_path();

    let db = Database::open(&path).unwrap();
    db.initialize_fts().unwrap();

    // Add memories with different embeddings for semantic search
    let embedding_rust: Vec<f32> = vec![1.0, 0.0, 0.0f32]
        .into_iter()
        .cycle()
        .take(384)
        .collect();
    let embedding_python: Vec<f32> = vec![0.0, 1.0, 0.0f32]
        .into_iter()
        .cycle()
        .take(384)
        .collect();
    let embedding_general: Vec<f32> = vec![0.5, 0.5, 0.5f32]
        .into_iter()
        .cycle()
        .take(384)
        .collect();

    let id_rust = db
        .insert(
            "test-project",
            "rust programming language",
            &embedding_rust,
            None,
            "fact",
            "active",
        )
        .unwrap();
    let _id_python = db
        .insert(
            "test-project",
            "python code examples",
            &embedding_python,
            None,
            "fact",
            "active",
        )
        .unwrap();
    let _id_general = db
        .insert(
            "test-project",
            "general software development",
            &embedding_general,
            None,
            "fact",
            "active",
        )
        .unwrap();

    // Test hybrid search combines semantic and BM25
    let semantic_results = db
        .search("test-project", &embedding_rust, 50, None, None)
        .unwrap();
    let bm25_results = db
        .search_bm25("rust", "test-project", 50, None, None)
        .unwrap();

    // Verify semantic search finds rust-related content
    assert!(!semantic_results.is_empty());
    assert!(semantic_results.iter().any(|m| m.id == id_rust));
    assert!(semantic_results.iter().any(|m| m.content.contains("rust")));

    // BM25 should find matches for "rust" query
    assert!(!bm25_results.is_empty());

    // Rust memory should appear in both semantic and BM25 results
    assert!(semantic_results.iter().any(|m| m.id == id_rust));
    assert!(
        bm25_results
            .iter()
            .any(|m| m.content.to_lowercase().contains("rust"))
    );
}

#[test]
fn test_hybrid_search_empty_results() {
    let (_dir, path) = MemoryStore::test_db_path();

    let db = Database::open(&path).unwrap();
    db.initialize_fts().unwrap();

    // Add one memory
    let embedding = vec![0.5f32; 384];
    db.insert(
        "test-project",
        "some content",
        &embedding,
        None,
        "fact",
        "active",
    )
    .unwrap();

    // Query with non-matching text
    let semantic_results = db
        .search("test-project", &vec![0.1f32; 384], 50, None, None)
        .unwrap();
    let bm25_results = db
        .search_bm25("nonexistent term xyz", "test-project", 50, None, None)
        .unwrap();

    // One may be empty, but fusion should handle it
    assert!(!semantic_results.is_empty() || !bm25_results.is_empty());
}

#[test]
fn test_hybrid_search_with_recency() {
    use chrono::{Duration, Utc};
    let (_dir, path) = MemoryStore::test_db_path();

    let db = Database::open(&path).unwrap();
    db.initialize_fts().unwrap();

    // Add memories with known embedding
    let embedding_good = vec![1.0f32; 384];
    let embedding_bad = vec![0.0f32; 384];

    let now = Utc::now();
    let old_time = (now - Duration::days(100)).to_rfc3339();
    let new_time = now.to_rfc3339();

    // Old high-similarity memory
    let id_old = db
        .insert_with_time(
            "test-project",
            "old but relevant",
            &embedding_good,
            None,
            &old_time,
            &old_time,
            "fact",
            "active",
        )
        .unwrap();

    // New low-similarity memory (same content for BM25 relevance)
    let _id_new = db
        .insert_with_time(
            "test-project",
            "new but less relevant",
            &embedding_bad,
            None,
            &new_time,
            &new_time,
            "fact",
            "active",
        )
        .unwrap();

    // Search without recency weighting - should find high-similarity first
    let semantic_results = db
        .search("test-project", &embedding_good, 10, None, None)
        .unwrap();
    if !semantic_results.is_empty() {
        let top_id = &semantic_results[0].id;
        // With zero recency weight, similarity dominates
        // Either the high-similarity old memory is first, or we have a single result
        assert!(top_id == &id_old || semantic_results.len() == 1);
    }
}

#[ignore]
#[test]
fn test_integration_add_search_roundtrip() {
    // Full integration test with real model
    // Requires: cargo test -- --ignored
    use crate::config::Config;
    use tempfile::TempDir;
    let dir = TempDir::new().unwrap();
    let path = dir.path().join("test.db");
    let config = Config::default();

    let mut store = MemoryStore::new(&path, "BAAI/bge-small-en-v1.5", config).unwrap();

    let id = match store
        .add_with_conflict(
            "test-project",
            "semantic search is useful",
            None,
            false,
            MemoryType::Fact,
            MemoryStatus::Active,
        )
        .unwrap()
    {
        crate::memory_types::AddResult::Added { id } => id,
        _ => panic!("Expected AddResult::Added"),
    };

    let results = store
        .search(
            "test-project",
            "finding information",
            5,
            0.0,
            crate::memory::SearchOptions::default(),
        )
        .unwrap();
    assert!(!results.is_empty());

    let memory = store.get(&id, "test-project").unwrap().unwrap();
    assert_eq!(memory.content, "semantic search is useful");
}

#[ignore]
#[test]
fn test_integration_update_changes_embedding() {
    // Full integration test with real model
    // Requires: cargo test -- --ignored
    use crate::config::Config;
    use tempfile::TempDir;
    let dir = TempDir::new().unwrap();
    let path = dir.path().join("test.db");
    let config = Config::default();

    let mut store = MemoryStore::new(&path, "BAAI/bge-small-en-v1.5", config).unwrap();

    let id = match store
        .add_with_conflict(
            "test-project",
            "original content",
            None,
            false,
            MemoryType::Fact,
            MemoryStatus::Active,
        )
        .unwrap()
    {
        crate::memory_types::AddResult::Added { id } => id,
        _ => panic!("Expected AddResult::Added"),
    };

    store
        .update(
            &id,
            "test-project",
            UpdateParams {
                text: Some("completely different content"),
                metadata: None,
                memory_type: None,
                status: None,
                importance: None,
            },
        )
        .unwrap();

    let memory = store.get(&id, "test-project").unwrap().unwrap();
    assert_eq!(memory.content, "completely different content");
}

#[test]
fn a_corrupt_blob_reaches_the_public_error_with_its_id() {
    // The Domain arm of `sqlite::classify_db_error` is what promotes a row's
    // decoding failure into the public `CorruptEmbedding`, and `errors.rs`
    // says only the structured decoding variants may be trusted as a
    // row-conversion payload. `sqlite::search`'s own test builds that error
    // from the decoder; this drives the whole chain the CLI takes — a real
    // store, a real corrupt BLOB, the store-level `search` that owns the
    // identity gate and the recency pass — so a public lock- or
    // classifier-shaped regression anywhere between the decoder and the caller
    // shows up here rather than only at the storage boundary.
    use crate::memory::SearchOptions;

    let (_dir, path) = MemoryStore::test_db_path();

    let id = {
        let db = Database::open(&path).unwrap();
        db.insert(
            "test-project",
            "corrupt embedding",
            &vec![0.1f32; 384],
            None,
            "fact",
            "active",
        )
        .unwrap()
    };

    // Corrupt the stored BLOB: a real row whose embedding is not a valid f32
    // buffer (3 bytes), written through the same connection the store owns.
    {
        let db = Database::open(&path).unwrap();
        db.conn()
            .execute(
                "UPDATE memories SET embedding = X'000102' WHERE id = ?1",
                rusqlite::params![id.clone()],
            )
            .unwrap();
    }

    // `from_db_with_test_embedder` keeps the model identity the insert wrote,
    // so the identity gate passes and the failure under test is the decode.
    let mut store = MemoryStore::from_db_with_test_embedder(Database::open(&path).unwrap());
    let err = store
        .search(
            "test-project",
            "corrupt embedding",
            10,
            0.0,
            SearchOptions::default(),
        )
        .expect_err("a row whose BLOB cannot be decoded must fail the search");

    assert!(
        matches!(&err, crate::errors::Error::CorruptEmbedding { id: corrupt_id, .. }
            if corrupt_id == &id),
        "the public error must stay CorruptEmbedding and name the row, got {err:?}"
    );
    // And it was not the classifier's Locked arm that produced it: the same
    // row read through the non-search path (the shared `map_row_to_memory`
    // decoder) fails the same way, so the verdict is the decode and nothing
    // upstream of it.
    assert!(
        !err.is_locked(),
        "a corrupt BLOB must not classify as a lock, got {err:?}"
    );
    let listed = store
        .list("test-project", 10, None, None)
        .expect_err("the same corrupt BLOB breaks the plain read path");
    assert!(
        matches!(&listed, crate::errors::Error::CorruptEmbedding { id: corrupt_id, .. }
            if corrupt_id == &id),
        "the read path must name the same row, got {listed:?}"
    );
}