second-brain-core 0.13.2

Core library for second-brain: SQLite storage with vector and lexical indexes, BGE embeddings, and hybrid retrieval
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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
use second_brain_core::{
    schema::{Entity, Memory, MemoryType, Relation, RelationType, Tier},
    sqlite_store::{RetireOutcome, SqliteStore},
    store::{Store, StoreAdmin},
};
use uuid::Uuid;

fn store() -> SqliteStore {
    SqliteStore::in_memory("test-machine".to_string()).unwrap()
}

fn memory(content: &str) -> Memory {
    Memory::new(
        content.to_string(),
        MemoryType::Semantic,
        "test".to_string(),
        String::new(),
    )
}

fn unit_embedding(hot_index: usize) -> Vec<f32> {
    let mut e = vec![0.0; 384];
    e[hot_index] = 1.0;
    e
}

fn relation(from: Uuid, to: Uuid, rt: RelationType) -> Relation {
    Relation {
        from_id: from,
        to_id: to,
        relation_type: rt,
        strength: 0.8,
        context: None,
    }
}

#[test]
fn memory_roundtrip_preserves_all_fields() {
    let store = store();
    let mut m = memory("full roundtrip memory");
    m.embedding = vec![0.25; 384];
    m.confidence = 0.7;
    m.access_count = 3;
    m.project_path = Some("/mnt/data/second-brain".to_string());
    m.machine_id = "test-machine".to_string();
    m.source_id = "conv-42".to_string();
    m.context = Some("chunk context".to_string());
    m.conversation_id = Some(Uuid::new_v4());
    m.tier = Tier::Hot;
    m.superseded_by = Some(Uuid::new_v4());
    m.archived_segment = Some("seg-0001".to_string());
    store.store_memory(&m).unwrap();

    let got = store.get_memory_with_embedding(m.id).unwrap().unwrap();
    assert_eq!(got.id, m.id);
    assert_eq!(got.content, m.content);
    assert_eq!(got.embedding, m.embedding);
    assert_eq!(got.memory_type, m.memory_type);
    assert_eq!(got.confidence, m.confidence);
    assert_eq!(got.created_at, m.created_at);
    assert_eq!(got.last_accessed, m.last_accessed);
    assert_eq!(got.access_count, m.access_count);
    assert_eq!(got.source, m.source);
    assert_eq!(got.source_id, m.source_id);
    assert_eq!(got.project_path, m.project_path);
    assert_eq!(got.machine_id, m.machine_id);
    assert_eq!(got.updated_at, m.updated_at);
    assert_eq!(got.context, m.context);
    assert_eq!(got.conversation_id, m.conversation_id);
    assert_eq!(got.tier, m.tier);
    assert_eq!(got.superseded_by, m.superseded_by);
    assert_eq!(got.archived_segment, m.archived_segment);
}

#[test]
fn get_memory_omits_embedding() {
    let store = store();
    let mut m = memory("embedding stays out of the light read");
    m.embedding = vec![0.5; 384];
    store.store_memory(&m).unwrap();

    let got = store.get_memory(m.id).unwrap().unwrap();
    assert!(got.embedding.is_empty());
}

#[test]
fn relation_pk_upsert() {
    let store = store();
    let a = memory("relation source");
    let b = memory("relation target");
    store.store_memory(&a).unwrap();
    store.store_memory(&b).unwrap();

    let mut r = relation(a.id, b.id, RelationType::RelatesTo);
    store.store_relation(&r).unwrap();
    r.strength = 0.3;
    store.store_relation(&r).unwrap();

    let rels = store
        .get_relations(a.id, Some(RelationType::RelatesTo))
        .unwrap();
    assert_eq!(
        rels.len(),
        1,
        "same (from,to,type) must upsert, not duplicate"
    );
    assert_eq!(store.all_relations().unwrap().len(), 1);
}

#[test]
fn tombstone_blocks_reinsert() {
    let store = store();
    let m = memory("delete me and keep me dead");
    store.store_memory(&m).unwrap();
    store.delete_memory(m.id).unwrap();
    assert!(store.tombstone_exists(m.id).unwrap());

    store.store_memory(&m).unwrap();
    assert!(
        store.get_memory(m.id).unwrap().is_none(),
        "a tombstoned id must not be resurrected by a re-store"
    );
}

#[test]
fn content_prefix_dedup() {
    let store = store();
    let m = memory("the quick brown fox jumps over the lazy dog");
    store.store_memory(&m).unwrap();

    assert!(store.memory_content_exists("the quick brown fox").unwrap());
    assert!(
        !store
            .memory_content_exists("a completely different note")
            .unwrap()
    );
}

#[test]
fn delete_plants_tombstone() {
    let store = store();
    let m = memory("tombstone test memory");
    store.store_memory(&m).unwrap();
    store.delete_memory(m.id).unwrap();

    assert!(store.tombstone_exists(m.id).unwrap());
    assert!(store.get_memory(m.id).unwrap().is_none());
}

#[test]
fn live_memory_has_no_tombstone() {
    let store = store();
    let m = memory("live memory");
    store.store_memory(&m).unwrap();

    assert!(
        !store.tombstone_exists(m.id).unwrap(),
        "live memory must not have a tombstone"
    );
}

#[test]
fn entity_roundtrip_and_find_by_name() {
    let store = store();
    let mut e = Entity::new("kuzu".to_string(), "technology".to_string());
    e.embedding = vec![0.1; 384];
    e.aliases = vec!["kuzudb".to_string()];
    store.store_entity(&e).unwrap();

    let got = store.get_entity(e.id).unwrap().unwrap();
    assert_eq!(got.name, "kuzu");
    assert_eq!(got.entity_type, "technology");

    let found = store.find_entity_by_name("kuzu").unwrap().unwrap();
    assert_eq!(found.id, e.id);
    assert!(store.find_entity_by_name("absent").unwrap().is_none());
}

#[test]
fn relations_filtered_and_unfiltered() {
    let store = store();
    let a = memory("hub");
    let b = memory("spoke one");
    let c = memory("spoke two");
    for m in [&a, &b, &c] {
        store.store_memory(m).unwrap();
    }
    store
        .store_relation(&relation(a.id, b.id, RelationType::RelatesTo))
        .unwrap();
    store
        .store_relation(&relation(a.id, c.id, RelationType::Reinforces))
        .unwrap();

    let related = store
        .get_relations(a.id, Some(RelationType::RelatesTo))
        .unwrap();
    assert_eq!(related.len(), 1);
    assert_eq!(related[0].to_id, b.id);

    let all = store.get_relations(a.id, None).unwrap();
    assert_eq!(all.len(), 2);
    assert!(store.get_relations(b.id, None).unwrap().is_empty());
}

#[test]
fn vector_search_ranks_by_cosine() {
    let store = store();
    let mut near = memory("nearest neighbor");
    near.embedding = unit_embedding(0);
    let mut far = memory("orthogonal");
    far.embedding = unit_embedding(1);
    let mut mid = memory("in between");
    let mut mixed = unit_embedding(0);
    mixed[1] = 1.0;
    mid.embedding = mixed;
    for m in [&near, &far, &mid] {
        store.store_memory(m).unwrap();
    }

    let results = store.vector_search(&unit_embedding(0), 3).unwrap();
    assert_eq!(results.len(), 3);
    assert_eq!(results[0].0.id, near.id);
    assert!(results[0].1 > 0.99);
    assert_eq!(results[1].0.id, mid.id);
    assert!(results[0].1 > results[1].1 && results[1].1 > results[2].1);

    let top = store.vector_search(&unit_embedding(0), 1).unwrap();
    assert_eq!(top.len(), 1);
}

#[test]
fn traverse_bounded_by_depth() {
    let store = store();
    let a = memory("start");
    let b = memory("one hop");
    let c = memory("two hops");
    for m in [&a, &b, &c] {
        store.store_memory(m).unwrap();
    }
    store
        .store_relation(&relation(a.id, b.id, RelationType::RelatesTo))
        .unwrap();
    store
        .store_relation(&relation(b.id, c.id, RelationType::Reinforces))
        .unwrap();

    let one = store.traverse(a.id, 1).unwrap();
    let one_ids: Vec<Uuid> = one.iter().map(|(m, _)| m.id).collect();
    assert_eq!(one_ids, vec![b.id]);

    let two = store.traverse(a.id, 2).unwrap();
    let mut two_ids: Vec<Uuid> = two.iter().map(|(m, _)| m.id).collect();
    two_ids.sort();
    let mut expected = vec![b.id, c.id];
    expected.sort();
    assert_eq!(two_ids, expected);
}

#[test]
fn memories_by_source_and_type() {
    let store = store();
    let mut ingested = memory("from the watcher");
    ingested.source = "claude_code".to_string();
    let mut decided = memory("a decision was made");
    decided.memory_type = MemoryType::Decision;
    store.store_memory(&ingested).unwrap();
    store.store_memory(&decided).unwrap();

    let by_source = store.memories_by_source("claude_code").unwrap();
    assert_eq!(by_source.len(), 1);
    assert_eq!(by_source[0].id, ingested.id);

    let by_type = store.memories_by_type(MemoryType::Decision).unwrap();
    assert_eq!(by_type.len(), 1);
    assert_eq!(by_type[0].id, decided.id);
}

#[test]
fn update_memory_persists_mutable_fields() {
    let store = store();
    let mut m = memory("mutate me");
    store.store_memory(&m).unwrap();

    m.access_count += 1;
    m.last_accessed = chrono::Utc::now();
    m.confidence = 0.7;
    m.project_path = Some("/tmp/project".to_string());
    m.updated_at = chrono::Utc::now();
    m.machine_id = "other-machine".to_string();
    store.update_memory(&m).unwrap();

    let got = store.get_memory(m.id).unwrap().unwrap();
    assert_eq!(got.confidence, m.confidence);
    assert_eq!(got.access_count, m.access_count);
    assert_eq!(got.last_accessed, m.last_accessed);
    assert_eq!(got.project_path, m.project_path);
    assert_eq!(got.updated_at, m.updated_at);
    assert_eq!(got.machine_id, m.machine_id);
}

#[test]
fn store_ops_append_sync_log_but_record_access_does_not() {
    let store = store();
    let log_rows = || store.sync_log_since(0).unwrap().len();
    assert_eq!(log_rows(), 0);

    let mut m = memory("sync logged");
    store.store_memory(&m).unwrap();
    assert_eq!(log_rows(), 1);

    m.access_count += 1;
    m.last_accessed = chrono::Utc::now();
    store.record_access(&m).unwrap();
    assert_eq!(
        log_rows(),
        1,
        "record_access is local-only and must not enter the sync log"
    );
    let got = store.get_memory(m.id).unwrap().unwrap();
    assert_eq!(got.access_count, m.access_count);
    assert_eq!(got.last_accessed, m.last_accessed);

    store.delete_memory(m.id).unwrap();
    assert_eq!(log_rows(), 2);
}

#[test]
fn bm25_search_matches_substring() {
    let store = store();
    let m = memory("Rust makes systems programming approachable");
    store.store_memory(&m).unwrap();
    store
        .store_memory(&memory("unrelated gardening notes"))
        .unwrap();

    let hits = store.bm25_search("SYSTEMS PROGRAMMING", 10).unwrap();
    assert_eq!(hits.len(), 1);
    assert_eq!(hits[0].id, m.id);
    assert!(store.bm25_search("nonexistent", 10).unwrap().is_empty());
}

#[test]
fn memory_count_and_all_ids() {
    let store = store();
    assert_eq!(store.memory_count().unwrap(), 0);
    let a = memory("first");
    let b = memory("second");
    store.store_memory(&a).unwrap();
    store.store_memory(&b).unwrap();

    assert_eq!(store.memory_count().unwrap(), 2);
    let mut ids = store.all_memory_ids().unwrap();
    ids.sort();
    let mut expected = vec![a.id, b.id];
    expected.sort();
    assert_eq!(ids, expected);
}

#[test]
fn persists_to_disk_across_reopen() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("brain.db");
    let m = memory("survives a reopen");
    {
        let store = SqliteStore::open(&path, "test-machine".to_string()).unwrap();
        store.store_memory(&m).unwrap();
    }
    let store = SqliteStore::open(&path, "test-machine".to_string()).unwrap();
    assert_eq!(store.get_memory(m.id).unwrap().unwrap().content, m.content);
}

#[test]
fn scoped_text_search_applies_the_scope_before_the_limit() {
    let store = store();
    for i in 0..10 {
        let m = memory(&format!("needle in project a, number {i}"))
            .with_project_path(Some("/a".into()));
        store.store_memory(&m).unwrap();
    }
    // Matches come back newest first, so the project b row must be the
    // oldest for an unscoped LIMIT 5 to cut it off.
    let mut in_b = memory("a NEEDLE filed under project b").with_project_path(Some("/b".into()));
    in_b.created_at -= chrono::Duration::days(1);
    store.store_memory(&in_b).unwrap();

    assert!(
        !store
            .bm25_search("needle", 5)
            .unwrap()
            .iter()
            .any(|m| m.id == in_b.id),
        "fixture is not exercising the bug: the unscoped LIMIT already reached project b"
    );

    let hits = store
        .scoped_text_search("needle", None, None, Some("/b"), 5)
        .unwrap();
    assert_eq!(hits.iter().map(|m| m.id).collect::<Vec<_>>(), vec![in_b.id]);
}

#[test]
fn scoped_text_search_filters_on_source_and_type_too() {
    let store = store();
    let mut wanted = memory("needle decision from ask");
    wanted.source = "ask".into();
    wanted.memory_type = MemoryType::Decision;
    let mut other_source = memory("needle decision from api");
    other_source.source = "api".into();
    other_source.memory_type = MemoryType::Decision;
    let mut other_type = memory("needle note from ask");
    other_type.source = "ask".into();
    for m in [&wanted, &other_source, &other_type] {
        store.store_memory(m).unwrap();
    }

    let hits = store
        .scoped_text_search("NEEDLE", Some("ask"), Some(MemoryType::Decision), None, 10)
        .unwrap();
    assert_eq!(
        hits.iter().map(|m| m.id).collect::<Vec<_>>(),
        vec![wanted.id]
    );
}

#[test]
fn exact_duplicate_is_found_past_any_number_of_substring_matches() {
    let store = store();
    for i in 0..6 {
        store
            .store_memory(&memory(&format!("user prefers tea over coffee, noted {i}")))
            .unwrap();
    }
    let exact = memory("User prefers tea");
    store.store_memory(&exact).unwrap();

    let found = store
        .find_exact_duplicate("  user prefers TEA \n", None)
        .unwrap();
    assert_eq!(found.map(|m| m.id), Some(exact.id));
    assert!(
        store
            .find_exact_duplicate("user prefers", None)
            .unwrap()
            .is_none(),
        "a prefix of a memory is not a duplicate of it"
    );
}

#[test]
fn exact_duplicate_trims_unicode_whitespace_on_the_stored_side() {
    let store = store();
    let padded = memory("\u{3000}Remember the milk\u{a0}\t");
    store.store_memory(&padded).unwrap();

    let found = store
        .find_exact_duplicate("remember the milk", None)
        .unwrap();
    assert_eq!(found.map(|m| m.id), Some(padded.id));
}

#[test]
fn exact_duplicate_matches_only_within_the_given_project() {
    let store = store();
    let in_a = memory("deploys go through the staging gate").with_project_path(Some("/a".into()));
    store.store_memory(&in_a).unwrap();
    let content = "Deploys go through the staging gate";

    let in_project = store.find_exact_duplicate(content, Some("/a")).unwrap();
    assert_eq!(in_project.map(|m| m.id), Some(in_a.id));
    assert!(
        store
            .find_exact_duplicate(content, Some("/b"))
            .unwrap()
            .is_none()
    );
    let anywhere = store.find_exact_duplicate(content, None).unwrap();
    assert_eq!(anywhere.map(|m| m.id), Some(in_a.id));
}

#[test]
fn a_superseded_memory_is_not_a_duplicate() {
    let store = store();
    let old = memory("the cache lives in redis");
    let new = memory("the cache lives in valkey");
    store.store_memory(&old).unwrap();
    store.store_memory(&new).unwrap();
    store.supersede_memory(old.id, new.id).unwrap();

    assert!(
        store
            .find_exact_duplicate("the cache lives in redis", None)
            .unwrap()
            .is_none()
    );
}

#[test]
fn a_duplicate_of_an_excluded_source_is_not_returned() {
    let store = store();
    let mut note = memory("the cache lives in redis");
    note.source = "markdown".to_string();
    store.store_memory(&note).unwrap();

    let outside = |project| {
        store
            .find_exact_duplicate_outside_source("The cache lives in Redis", project, "markdown")
            .unwrap()
    };
    assert!(outside(None).is_none());
    assert!(outside(Some("/a")).is_none());

    let explicit = memory("the cache lives in redis");
    store.store_memory(&explicit).unwrap();
    assert_eq!(outside(None).map(|m| m.id), Some(explicit.id));
    assert_eq!(
        store
            .find_exact_duplicate("the cache lives in redis", None)
            .unwrap()
            .map(|m| m.id),
        Some(note.id)
    );
}

#[test]
fn retire_archives_with_a_reason_and_replicates_the_row() {
    let store = store();
    let m = memory("retired by forget");
    store.store_memory(&m).unwrap();
    let before = store.max_sync_seq().unwrap();

    assert_eq!(
        store.retire_memory(m.id, "outdated").unwrap(),
        RetireOutcome::Retired
    );
    let got = store.get_memory(m.id).unwrap().unwrap();
    assert_eq!(got.tier, Tier::Archived);
    assert_eq!(got.retired_reason.as_deref(), Some("outdated"));
    assert!(!store.tombstone_exists(m.id).unwrap());

    let logged = store.sync_log_since(before).unwrap();
    assert_eq!(logged.len(), 1);
    let data: Memory = serde_json::from_str(logged[0].data.as_deref().unwrap()).unwrap();
    assert_eq!(data.retired_reason.as_deref(), Some("outdated"));
    assert!(
        store
            .bm25_search("retired by forget", 10)
            .unwrap()
            .is_empty()
    );

    assert_eq!(
        store.retire_memory(m.id, "again").unwrap(),
        RetireOutcome::AlreadyRetired
    );
    assert_eq!(store.sync_log_since(before).unwrap().len(), 1);
}

#[test]
fn an_archived_row_can_still_be_retired_once() {
    let store = store();
    let m = memory("consolidation archived this chunk");
    store.store_memory(&m).unwrap();
    store.archive_memory(m.id).unwrap();
    let before = store.max_sync_seq().unwrap();

    assert_eq!(
        store.retire_memory(m.id, "wrong").unwrap(),
        RetireOutcome::Retired
    );
    let row = store.get_memory(m.id).unwrap().unwrap();
    assert_eq!(row.tier, Tier::Archived);
    assert_eq!(row.retired_reason.as_deref(), Some("wrong"));
    assert_eq!(store.sync_log_since(before).unwrap().len(), 1);
    assert_eq!(
        store.retire_memory(m.id, "again").unwrap(),
        RetireOutcome::AlreadyRetired
    );
}

#[test]
fn retiring_an_unknown_id_changes_nothing() {
    let store = store();
    let id = Uuid::new_v4();
    assert_eq!(
        store.retire_memory(id, "nothing").unwrap(),
        RetireOutcome::NotFound
    );
    assert_eq!(store.max_sync_seq().unwrap(), 0);
    assert!(!store.tombstone_exists(id).unwrap());
}

#[test]
fn store_memory_if_absent_writes_only_a_new_id() {
    let store = store();
    let m = memory("first write");
    assert!(store.store_memory_if_absent(&m).unwrap());
    let mut again = memory("second write under the same id");
    again.id = m.id;
    assert!(!store.store_memory_if_absent(&again).unwrap());
    assert_eq!(
        store.get_memory(m.id).unwrap().unwrap().content,
        "first write"
    );
}

#[test]
fn store_memory_if_absent_leaves_a_retired_row_retired() {
    let store = store();
    let m = memory("retired note");
    store.store_memory(&m).unwrap();
    assert_eq!(
        store.retire_memory(m.id, "wrong").unwrap(),
        RetireOutcome::Retired
    );
    let mut replay = memory("retired note");
    replay.id = m.id;
    assert!(!store.store_memory_if_absent(&replay).unwrap());
    assert_eq!(
        store.get_memory(m.id).unwrap().unwrap().tier,
        Tier::Archived
    );
}

#[test]
fn store_memory_if_absent_skips_a_tombstoned_id() {
    let store = store();
    let m = memory("deleted note");
    store.store_memory(&m).unwrap();
    assert!(store.remove_memory(m.id).unwrap());
    assert!(!store.store_memory_if_absent(&m).unwrap());
    assert!(store.get_memory(m.id).unwrap().is_none());
}

#[test]
fn racing_inserts_of_one_id_write_it_once() {
    let dir = tempfile::tempdir().unwrap();
    let store = std::sync::Arc::new(
        SqliteStore::open(&dir.path().join("brain.db"), "race-machine".to_string()).unwrap(),
    );
    let note = memory("sent live and replayed at once");
    let barrier = std::sync::Arc::new(std::sync::Barrier::new(8));
    let writers: Vec<_> = (0..8)
        .map(|_| {
            let (store, note, barrier) = (store.clone(), note.clone(), barrier.clone());
            std::thread::spawn(move || {
                barrier.wait();
                store.store_memory_if_absent(&note).unwrap()
            })
        })
        .collect();
    let wrote = writers
        .into_iter()
        .map(|w| w.join().unwrap())
        .filter(|&wrote| wrote)
        .count();
    assert_eq!(wrote, 1);
    assert_eq!(store.memory_count().unwrap(), 1);
}