vipune 0.4.0

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
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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
#[cfg(test)]
mod tests {
    use crate::embedding::EMBEDDING_DIMS;
    use crate::sqlite::Database;
    use tempfile::TempDir;

    fn create_test_db() -> Database {
        let dir = TempDir::new().unwrap();
        let path = dir.path().join("test.db");
        let db = Database::open(&path).unwrap();
        std::mem::forget(dir);
        db
    }

    #[test]
    fn test_list_since_basic() {
        let db = create_test_db();
        let embedding = vec![0.1f32; 384];

        let _id1 = db
            .insert_with_time(
                "proj1",
                "old",
                &embedding,
                None,
                "2024-01-01T00:00:00Z",
                "2024-01-01T00:00:00Z",
                "fact",
                "active",
            )
            .unwrap();
        let _id2 = db
            .insert_with_time(
                "proj1",
                "new",
                &embedding,
                None,
                "2024-01-02T00:00:00Z",
                "2024-01-02T00:00:00Z",
                "fact",
                "active",
            )
            .unwrap();

        // List since 2024-01-01T12:00:00Z should only return "new"
        let results = db.list_since("proj1", "2024-01-01T12:00:00Z", 10, None, None).unwrap();
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].content, "new");
    }

    #[test]
    fn test_list_since_exclusive_boundary() {
        let db = create_test_db();
        let embedding = vec![0.1f32; 384];

        let id = db
            .insert_with_time(
                "proj1",
                "boundary",
                &embedding,
                None,
                "2024-01-01T00:00:00Z",
                "2024-01-01T00:00:00Z",
                "fact",
                "active",
            )
            .unwrap();

        // List since exact timestamp should NOT return the memory (exclusive)
        let results = db.list_since("proj1", "2024-01-01T00:00:00Z", 10, None, None).unwrap();
        assert_eq!(results.len(), 0);

        // But list since 1ms before SHOULD return it
        let results = db.list_since("proj1", "2023-12-31T23:59:59Z", 10, None, None).unwrap();
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].id, id);
    }

    #[test]
    fn test_list_since_invalid_timestamp() {
        let db = create_test_db();
        let result = db.list_since("proj1", "invalid-timestamp", 10, None, None);
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("Invalid RFC3339"));
    }

    #[test]
    fn test_list_since_limit() {
        let db = create_test_db();
        let embedding = vec![0.1f32; 384];

        for i in 0..5 {
            db.insert_with_time(
                "proj1",
                &format!("content {}", i),
                &embedding,
                None,
                &format!("2024-01-{:02}T00:00:00Z", i + 1),
                &format!("2024-01-{:02}T00:00:00Z", i + 1),
                "fact",
                "active",
            )
            .unwrap();
        }

        // Should only return 2 most recent
        let results = db.list_since("proj1", "2024-01-01T00:00:00Z", 2, None, None).unwrap();
        assert_eq!(results.len(), 2);
    }

    #[test]
    fn test_list_since_ordering() {
        let db = create_test_db();
        let embedding = vec![0.1f32; 384];

        let id1 = db
            .insert_with_time(
                "proj1",
                "first",
                &embedding,
                None,
                "2024-01-01T00:00:00Z",
                "2024-01-01T00:00:00Z",
                "fact",
                "active",
            )
            .unwrap();
        let id2 = db
            .insert_with_time(
                "proj1",
                "second",
                &embedding,
                None,
                "2024-01-02T00:00:00Z",
                "2024-01-02T00:00:00Z",
                "fact",
                "active",
            )
            .unwrap();
        let id3 = db
            .insert_with_time(
                "proj1",
                "third",
                &embedding,
                None,
                "2024-01-03T00:00:00Z",
                "2024-01-03T00:00:00Z",
                "fact",
                "active",
            )
            .unwrap();

        // Should return newest first
        let results = db.list_since("proj1", "2023-12-31T00:00:00Z", 10, None, None).unwrap();
        assert_eq!(results.len(), 3);
        assert_eq!(results[0].id, id3);
        assert_eq!(results[1].id, id2);
        assert_eq!(results[2].id, id1);
    }

    #[test]
    fn test_get_many_basic() {
        let db = create_test_db();
        let embedding = vec![0.1f32; 384];

        let id1 = db.insert("proj1", "content 1", &embedding, None, "fact", "active").unwrap();
        let id2 = db.insert("proj1", "content 2", &embedding, None, "fact", "active").unwrap();

        let results = db.get_many(&[&id1, &id2]).unwrap();
        assert_eq!(results.len(), 2);
        assert!(results[0].is_some());
        assert!(results[1].is_some());
        assert_eq!(results[0].as_ref().unwrap().content, "content 1");
        assert_eq!(results[1].as_ref().unwrap().content, "content 2");
    }

    #[test]
    fn test_get_many_preserves_ordering() {
        let db = create_test_db();
        let embedding = vec![0.1f32; 384];

        let id1 = db.insert("proj1", "first", &embedding, None, "fact", "active").unwrap();
        let id2 = db.insert("proj1", "second", &embedding, None, "fact", "active").unwrap();
        let id3 = db.insert("proj1", "third", &embedding, None, "fact", "active").unwrap();

        // Query in reverse order
        let results = db.get_many(&[&id3, &id1, &id2]).unwrap();
        assert_eq!(results[0].as_ref().unwrap().id, id3);
        assert_eq!(results[1].as_ref().unwrap().id, id1);
        assert_eq!(results[2].as_ref().unwrap().id, id2);
    }

    #[test]
    fn test_get_many_with_missing_ids() {
        let db = create_test_db();
        let embedding = vec![0.1f32; 384];

        let id1 = db.insert("proj1", "content 1", &embedding, None, "fact", "active").unwrap();
        let id2 = db.insert("proj1", "content 2", &embedding, None, "fact", "active").unwrap();

        // Mix of valid and invalid IDs
        let results = db
            .get_many(&[&id1, "nonexistent-id", &id2, "another-missing"])
            .unwrap();
        assert_eq!(results.len(), 4);
        assert!(results[0].is_some());
        assert!(results[1].is_none());
        assert!(results[2].is_some());
        assert!(results[3].is_none());
        assert_eq!(results[0].as_ref().unwrap().id, id1);
        assert_eq!(results[2].as_ref().unwrap().id, id2);
    }

    #[test]
    fn test_get_many_all_missing() {
        let db = create_test_db();

        let results = db.get_many(&["missing1", "missing2", "missing3"]).unwrap();
        assert_eq!(results.len(), 3);
        assert!(results.iter().all(|r| r.is_none()));
    }

    #[test]
    fn test_get_many_empty_input() {
        let db = create_test_db();

        let results = db.get_many(&[]).unwrap();
        assert_eq!(results.len(), 0);
    }

    #[test]
    fn test_get_many_single_id() {
        let db = create_test_db();
        let embedding = vec![0.1f32; 384];

        let id = db.insert("proj1", "content", &embedding, None, "fact", "active").unwrap();

        let results = db.get_many(&[&id]).unwrap();
        assert_eq!(results.len(), 1);
        assert!(results[0].is_some());
        assert_eq!(results[0].as_ref().unwrap().content, "content");
    }

    #[test]
    fn test_get_many_with_duplicate_ids() {
        // Test that duplicate IDs return duplicate results (stable behavior)
        let db = create_test_db();
        let embedding = vec![0.1f32; 384];

        let id1 = db.insert("proj1", "content 1", &embedding, None, "fact", "active").unwrap();
        let id2 = db.insert("proj1", "content 2", &embedding, None, "fact", "active").unwrap();

        // Query with duplicate IDs
        let results = db.get_many(&[&id1, &id2, &id1, &id2]).unwrap();
        assert_eq!(results.len(), 4);
        assert_eq!(results[0].as_ref().unwrap().id, id1);
        assert_eq!(results[1].as_ref().unwrap().id, id2);
        assert_eq!(results[2].as_ref().unwrap().id, id1);
        assert_eq!(results[3].as_ref().unwrap().id, id2);
    }

    #[test]
    fn test_list_since_with_timezone_offset() {
        // Test that RFC3339 with timezone offsets is accepted.
        // Note: SQLite does string comparison, not timezone-aware comparison.
        // This test documents the actual behavior: timestamps are compared as strings.
        let db = create_test_db();
        let embedding = vec![0.1f32; 384];

        // Insert memories at specific UTC times
        let _id1 = db
            .insert_with_time(
                "proj1",
                "old",
                &embedding,
                None,
                "2024-01-01T00:00:00Z",
                "2024-01-01T00:00:00Z",
                "fact",
                "active",
            )
            .unwrap();
        let _id2 = db
            .insert_with_time(
                "proj1",
                "new",
                &embedding,
                None,
                "2024-01-02T00:00:00Z",
                "2024-01-02T00:00:00Z",
                "fact",
                "active",
            )
            .unwrap();

        // Query with UTC+01:00 offset works (RFC3339 parsing succeeds)
        // SQLite compares as strings: "2024-01-02T00:00:00Z" > "2024-01-01T11:00:00+01:00"
        let results = db
            .list_since("proj1", "2024-01-01T11:00:00+01:00", 10, None, None)
            .unwrap();
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].content, "new");

        // Query with UTC-05:00 offset also works
        // SQLite compares as strings: "2024-01-02T00:00:00Z" > "2024-01-01T19:00:00-05:00"
        let results = db
            .list_since("proj1", "2024-01-01T19:00:00-05:00", 10, None, None)
            .unwrap();
        // String comparison: "2024-01-02..." is lexicographically greater than "2024-01-01..."
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].content, "new");

        // Query after the stored timestamp returns nothing (Exclusive bound)
        // SQLite string comparison: "2024-01-02T00:00:00Z" is NOT > "2024-01-02T00:00:00Z"
        let results = db.list_since("proj1", "2024-01-02T00:00:00Z", 10, None, None).unwrap();
        assert_eq!(results.len(), 0);
    }

    #[test]
    fn test_list_since_timestamp_precision_equivalence() {
        // Test that different timestamp precisions with same instant behave identically
        let db = create_test_db();
        let embedding = vec![0.1f32; 384];

        let _id = db
            .insert_with_time(
                "proj1",
                "test",
                &embedding,
                None,
                "2024-01-01T00:00:00Z",
                "2024-01-01T00:00:00Z",
                "fact",
                "active",
            )
            .unwrap();

        // Query with and without fractional seconds - should behave identically
        let results1 = db.list_since("proj1", "2023-12-31T23:59:59Z", 10, None, None).unwrap();
        let results2 = db
            .list_since("proj1", "2023-12-31T23:59:59.000Z", 10, None, None)
            .unwrap();

        assert_eq!(results1.len(), results2.len());
        if results1.len() > 0 {
            assert_eq!(results1[0].id, results2[0].id);
        }

        // Precision equivalence with milliseconds
        let results3 = db
            .list_since("proj1", "2023-12-31T23:59:59.999Z", 10, None, None)
            .unwrap();
        // Should include the memory since it's before the timestamp
        assert_eq!(results3.len(), 1);
    }

    #[test]
    fn test_list_regression_coverage() {
        // Verify list() regression coverage for existing behavior
        let db = create_test_db();
        let embedding = vec![0.1f32; 384];

        // Insert multiple memories
        let id1 = db.insert("proj1", "first", &embedding, None, "fact", "active").unwrap();
        let id2 = db.insert("proj1", "second", &embedding, None, "fact", "active").unwrap();
        let id3 = db.insert("proj1", "third", &embedding, None, "fact", "active").unwrap();

        // Test ordering (newest first)
        let results = db.list("proj1", 10, None, None).unwrap();
        assert_eq!(results.len(), 3);
        assert_eq!(results[0].id, id3);
        assert_eq!(results[1].id, id2);
        assert_eq!(results[2].id, id1);

        // Test limit
        let results = db.list("proj1", 2, None, None).unwrap();
        assert_eq!(results.len(), 2);
        assert_eq!(results[0].id, id3);

        // Test project isolation
        db.insert("proj2", "other", &embedding, None, "fact", "active").unwrap();
        let proj1_results = db.list("proj1", 10, None, None).unwrap();
        assert_eq!(proj1_results.len(), 3);

        // Test empty project
        let empty_results = db.list("nonexistent", 10, None, None).unwrap();
        assert_eq!(empty_results.len(), 0);
    }

    #[test]
    fn test_insert_and_get() {
        let db = create_test_db();
        let embedding = vec![0.1f32; 384];
        let id = db
            .insert("proj1", "test content", &embedding, None, "fact", "active")
            .unwrap();

        let memory = db.get(&id).unwrap();
        assert!(memory.is_some());
        let m = memory.unwrap();
        assert_eq!(m.content, "test content");
        assert_eq!(m.project_id, "proj1");
    }

    #[test]
    fn test_insert_with_metadata() {
        let db = create_test_db();
        let embedding = vec![0.1f32; 384];
        let id = db
            .insert(
                "proj1",
                "test content",
                &embedding,
                Some(r#"{"key": "value"}"#),
                "fact",
                "active",
            )
            .unwrap();

        let m = db.get(&id).unwrap().unwrap();
        assert_eq!(m.metadata, Some(r#"{"key": "value"}"#.to_string()));
    }

    #[test]
    fn test_insert_invalid_embedding() {
        let db = create_test_db();
        let embedding = vec![0.1f32; 256];
        let result = db.insert("proj1", "test", &embedding, None, "fact", "active");
        assert!(result.is_err());
    }

    #[test]
    fn test_get_nonexistent() {
        let db = create_test_db();
        let memory = db.get("nonexistent").unwrap();
        assert!(memory.is_none());
    }

    #[test]
    fn test_list_ordering() {
        let db = create_test_db();
        let embedding = vec![0.1f32; 384];
        let id1 = db
            .insert_with_time(
                "proj1",
                "first",
                &embedding,
                None,
                "2024-01-01T00:00:00Z",
                "2024-01-01T00:00:00Z",
                "fact",
                "active",
            )
            .unwrap();
        let id2 = db
            .insert_with_time(
                "proj1",
                "second",
                &embedding,
                None,
                "2024-01-02T00:00:00Z",
                "2024-01-02T00:00:00Z",
                "fact",
                "active",
            )
            .unwrap();

        let memories = db.list("proj1", 10, None, None).unwrap();
        assert_eq!(memories.len(), 2);
        assert_eq!(memories[0].id, id2); // Newest first
        assert_eq!(memories[1].id, id1);
    }

    #[test]
    fn test_list_limit() {
        let db = create_test_db();
        let embedding = vec![0.1f32; 384];
        for i in 0..5 {
            db.insert("proj1", &format!("content {}", i), &embedding, None, "fact", "active")
                .unwrap();
        }

        let memories = db.list("proj1", 2, None, None).unwrap();
        assert_eq!(memories.len(), 2);
    }

    #[test]
    fn test_update() {
        let db = create_test_db();
        let embedding = vec![0.1f32; 384];
        let id = db.insert("proj1", "original", &embedding, None, "fact", "active").unwrap();

        db.update(&id, Some("updated"), Some(&embedding), None, None, None).unwrap();

        let m = db.get(&id).unwrap().unwrap();
        assert_eq!(m.content, "updated");
    }

    #[test]
    fn test_update_nonexistent() {
        let db = create_test_db();
        let embedding = vec![0.1f32; 384];
        let result = db.update("nonexistent", Some("content"), Some(&embedding), None, None, None);
        assert!(result.is_err());
    }

    #[test]
    fn test_delete() {
        let db = create_test_db();
        let embedding = vec![0.1f32; 384];
        let id = db.insert("proj1", "content", &embedding, None, "fact", "active").unwrap();

        let deleted = db.delete(&id).unwrap();
        assert!(deleted);

        let memory = db.get(&id).unwrap();
        assert!(memory.is_none());
    }

    #[test]
    fn test_delete_nonexistent() {
        let db = create_test_db();
        let deleted = db.delete("nonexistent").unwrap();
        assert!(!deleted);
    }

    #[test]
    fn test_project_isolation() {
        let db = create_test_db();
        let embedding = vec![0.1f32; 384];
        db.insert("proj1", "proj1 content", &embedding, None, "fact", "active")
            .unwrap();
        db.insert("proj2", "proj2 content", &embedding, None, "fact", "active")
            .unwrap();

        let list1 = db.list("proj1", 10, None, None).unwrap();
        let list2 = db.list("proj2", 10, None, None).unwrap();

        assert_eq!(list1.len(), 1);
        assert_eq!(list2.len(), 1);
        assert_eq!(list1[0].project_id, "proj1");
        assert_eq!(list2[0].project_id, "proj2");
    }

    #[test]
    fn test_get_includes_embedding() {
        let db = create_test_db();
        let embedding = vec![0.1f32; EMBEDDING_DIMS];
        let id = db
            .insert("proj1", "test content", &embedding, None, "fact", "active")
            .unwrap();

        let memory = db.get(&id).unwrap().unwrap();
        assert_eq!(memory.embedding.len(), EMBEDDING_DIMS);
        for (i, &val) in embedding.iter().enumerate() {
            assert!((memory.embedding[i] - val).abs() < 1e-6);
        }
    }

    #[test]
    fn test_list_includes_embeddings() {
        let db = create_test_db();
        let embedding1 = vec![0.1f32; EMBEDDING_DIMS];
        let embedding2 = vec![0.2f32; EMBEDDING_DIMS];

        db.insert("proj1", "first", &embedding1, None, "fact", "active").unwrap();
        db.insert("proj1", "second", &embedding2, None, "fact", "active").unwrap();

        let memories = db.list("proj1", 10, None, None).unwrap();
        assert_eq!(memories.len(), 2);

        for memory in &memories {
            assert_eq!(memory.embedding.len(), EMBEDDING_DIMS);
        }
    }

    #[test]
    fn test_embedding_roundtrip() {
        let db = create_test_db();
        let original = vec![0.123f32, 0.456f32, 0.789f32];
        let mut full_embedding = vec![0.1f32; EMBEDDING_DIMS];
        full_embedding[0] = original[0];
        full_embedding[1] = original[1];
        full_embedding[EMBEDDING_DIMS - 1] = original[2];

        let id = db
            .insert("proj1", "test", &full_embedding, None, "fact", "active")
            .unwrap();

        let memory = db.get(&id).unwrap().unwrap();
        assert_eq!(memory.embedding.len(), EMBEDDING_DIMS);
        assert!((memory.embedding[0] - original[0]).abs() < 1e-6);
        assert!((memory.embedding[1] - original[1]).abs() < 1e-6);
        assert!((memory.embedding[EMBEDDING_DIMS - 1] - original[2]).abs() < 1e-6);
    }
}