second-brain-api 0.13.5

HTTP API server for second-brain: REST endpoints for recall, remember, and ingest
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
use std::collections::HashMap;
use std::sync::RwLock;

use anyhow::Result;
use uuid::Uuid;

use second_brain_core::schema::{Conversation, Entity, Memory, MemoryType, Relation, RelationType};
use second_brain_core::store::Store;

type RelationsCache = HashMap<(Uuid, Option<RelationType>), Vec<Relation>>;

// get_relations(memory_id, rel_type) is deterministic and query-independent, so
// caching it is sound: the eval replays the same snapshot read-only and recall
// asks for the same (id, rel_type) pairs across all 836 queries x 2 arms.
// Kuzu serializes these reads, so memoizing turns repeated graph hits into
// HashMap lookups without changing any recall output.
pub struct CachingStore<'a> {
    inner: &'a dyn Store,
    relations_cache: RwLock<RelationsCache>,
    vector_corpus: RwLock<Option<Vec<Memory>>>,
}

impl<'a> CachingStore<'a> {
    pub fn new(inner: &'a dyn Store) -> Self {
        Self {
            inner,
            relations_cache: RwLock::new(HashMap::new()),
            vector_corpus: RwLock::new(None),
        }
    }

    /// Serve vector_search from an in-memory corpus instead of re-reading and
    /// re-parsing every embedding blob through the store's connection mutex on
    /// each call. The caller must supply exactly the rows the store's own
    /// vector_search would scan: embedding present, tier hot, not superseded.
    pub fn prewarm_vectors(&self, corpus: Vec<Memory>) {
        *self.vector_corpus.write().unwrap() = Some(corpus);
    }

    // Populate the relation cache in two bulk scans (ids + all edges) instead of
    // letting recall trigger ~corpus_size x 7 serialized Kuzu point reads. Every
    // (memory_id, Some(rt)) the recall path queries is seeded, empty included, so
    // the parallel arms run entirely from the in-memory map. get_relations is
    // query-independent, so a prewarmed cache is interchangeable with live reads.
    pub fn prewarm(&self) -> Result<()> {
        let scored_types = [
            RelationType::Reinforces,
            RelationType::RelatesTo,
            RelationType::DistilledFrom,
            RelationType::Mentions,
            RelationType::DerivedFrom,
            RelationType::Contradicts,
            RelationType::Supersedes,
        ];

        let ids = self.inner.all_memory_ids()?;
        let mut cache = self.relations_cache.write().unwrap();
        cache.reserve(ids.len() * scored_types.len());
        for id in &ids {
            for rt in &scored_types {
                cache.entry((*id, Some(*rt))).or_default();
            }
        }

        for rel in self.inner.all_relations()? {
            // get_relations only matches edges whose source is the queried node,
            // so key on from_id; an unseeded type (none of the scored set) is
            // ignored here and would fall through to a live read if ever asked.
            if let Some(bucket) = cache.get_mut(&(rel.from_id, Some(rel.relation_type))) {
                bucket.push(rel);
            }
        }

        Ok(())
    }
}

impl Store for CachingStore<'_> {
    fn get_relations(
        &self,
        node_id: Uuid,
        relation_type: Option<RelationType>,
    ) -> Result<Vec<Relation>> {
        let key = (node_id, relation_type);
        if let Some(hit) = self.relations_cache.read().unwrap().get(&key) {
            return Ok(hit.clone());
        }
        let fetched = self.inner.get_relations(node_id, relation_type)?;
        self.relations_cache
            .write()
            .unwrap()
            .insert(key, fetched.clone());
        Ok(fetched)
    }

    fn store_memory(&self, memory: &Memory) -> Result<()> {
        self.inner.store_memory(memory)
    }

    fn get_memory(&self, id: Uuid) -> Result<Option<Memory>> {
        self.inner.get_memory(id)
    }

    fn delete_memory(&self, id: Uuid) -> Result<()> {
        self.inner.delete_memory(id)
    }

    // Every method the Store trait gives a default body must be forwarded
    // too: the default would otherwise run here instead of the inner
    // store's override, and the eval would measure a different recall.
    fn remove_memory(&self, id: Uuid) -> Result<bool> {
        self.inner.remove_memory(id)
    }

    fn source_admits(&self, memory: &Memory, source: &str) -> Result<bool> {
        self.inner.source_admits(memory, source)
    }

    fn record_accesses(&self, ids: &[Uuid]) -> Result<()> {
        self.inner.record_accesses(ids)
    }

    fn chunk_window(&self, memory_id: Uuid, ord: usize) -> Result<Option<String>> {
        self.inner.chunk_window(memory_id, ord)
    }

    fn store_entity(&self, entity: &Entity) -> Result<()> {
        self.inner.store_entity(entity)
    }

    fn get_entity(&self, id: Uuid) -> Result<Option<Entity>> {
        self.inner.get_entity(id)
    }

    fn find_entity_by_name(&self, name: &str) -> Result<Option<Entity>> {
        self.inner.find_entity_by_name(name)
    }

    fn store_conversation(&self, conversation: &Conversation) -> Result<()> {
        self.inner.store_conversation(conversation)
    }

    fn store_relation(&self, relation: &Relation) -> Result<()> {
        self.inner.store_relation(relation)
    }

    fn vector_search(&self, embedding: &[f32], limit: usize) -> Result<Vec<(Memory, f32)>> {
        let corpus = self.vector_corpus.read().unwrap();
        let Some(corpus) = corpus.as_ref() else {
            return self.inner.vector_search(embedding, limit);
        };
        let mut scored: Vec<(usize, f32)> = corpus
            .iter()
            .enumerate()
            .map(|(i, m)| {
                (
                    i,
                    second_brain_core::retrieval::cosine(embedding, &m.embedding),
                )
            })
            .collect();
        scored.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));
        scored.truncate(limit);
        Ok(scored
            .into_iter()
            .map(|(i, sim)| {
                let mut m = corpus[i].clone();
                // The store's vector_search strips embeddings from results.
                m.embedding = Vec::new();
                (m, sim)
            })
            .collect())
    }

    fn filtered_vector_search(
        &self,
        embedding: &[f32],
        source: Option<&str>,
        memory_type: Option<MemoryType>,
        project_path: Option<&str>,
        limit: usize,
    ) -> Result<Vec<(Memory, f32)>> {
        self.inner
            .filtered_vector_search(embedding, source, memory_type, project_path, limit)
    }

    fn traverse(&self, start_id: Uuid, depth: u32) -> Result<Vec<(Memory, Vec<Relation>)>> {
        self.inner.traverse(start_id, depth)
    }

    fn memories_by_source(&self, source: &str) -> Result<Vec<Memory>> {
        self.inner.memories_by_source(source)
    }

    fn memories_by_type(&self, memory_type: MemoryType) -> Result<Vec<Memory>> {
        self.inner.memories_by_type(memory_type)
    }

    fn update_memory(&self, memory: &Memory) -> Result<()> {
        self.inner.update_memory(memory)
    }

    fn record_access(&self, memory: &Memory) -> Result<()> {
        self.inner.record_access(memory)
    }

    fn bm25_search(&self, query: &str, limit: usize) -> Result<Vec<Memory>> {
        self.inner.bm25_search(query, limit)
    }
    fn get_memory_with_embedding(&self, id: Uuid) -> Result<Option<Memory>> {
        self.inner.get_memory_with_embedding(id)
    }

    fn memory_count(&self) -> Result<usize> {
        self.inner.memory_count()
    }

    fn all_memory_ids(&self) -> Result<Vec<Uuid>> {
        self.inner.all_memory_ids()
    }

    fn all_relations(&self) -> Result<Vec<Relation>> {
        self.inner.all_relations()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use chrono::{Duration, Utc};
    use second_brain_core::query::{QueryEngine, QueryFilters, QueryRequest};
    use second_brain_core::schema::MemoryType;
    use second_brain_core::sqlite_store::SqliteStore;
    use second_brain_core::store::StoreAdmin;

    struct InMemoryStore {
        vector_results: Vec<(Memory, f32)>,
        relations: Vec<Relation>,
        get_relations_calls: std::sync::atomic::AtomicUsize,
    }

    impl Store for InMemoryStore {
        fn vector_search(&self, _embedding: &[f32], _limit: usize) -> Result<Vec<(Memory, f32)>> {
            Ok(self.vector_results.clone())
        }

        fn filtered_vector_search(
            &self,
            _embedding: &[f32],
            source: Option<&str>,
            memory_type: Option<MemoryType>,
            project_path: Option<&str>,
            limit: usize,
        ) -> Result<Vec<(Memory, f32)>> {
            Ok(self
                .vector_results
                .iter()
                .filter(|(m, _)| source.is_none_or(|s| m.source == s))
                .filter(|(m, _)| memory_type.is_none_or(|t| m.memory_type == t))
                .filter(|(m, _)| project_path.is_none_or(|p| m.project_path.as_deref() == Some(p)))
                .take(limit)
                .cloned()
                .collect())
        }

        fn get_relations(
            &self,
            node_id: Uuid,
            relation_type: Option<RelationType>,
        ) -> Result<Vec<Relation>> {
            self.get_relations_calls
                .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
            Ok(self
                .relations
                .iter()
                .filter(|r| r.from_id == node_id)
                .filter(|r| {
                    relation_type
                        .map(|rt| rt == r.relation_type)
                        .unwrap_or(true)
                })
                .cloned()
                .collect())
        }

        fn store_memory(&self, _m: &Memory) -> Result<()> {
            unimplemented!()
        }
        fn get_memory(&self, _id: Uuid) -> Result<Option<Memory>> {
            unimplemented!()
        }
        fn delete_memory(&self, _id: Uuid) -> Result<()> {
            unimplemented!()
        }
        fn store_entity(&self, _e: &Entity) -> Result<()> {
            unimplemented!()
        }
        fn get_entity(&self, _id: Uuid) -> Result<Option<Entity>> {
            unimplemented!()
        }
        fn find_entity_by_name(&self, _name: &str) -> Result<Option<Entity>> {
            unimplemented!()
        }
        fn store_conversation(&self, _c: &Conversation) -> Result<()> {
            unimplemented!()
        }
        fn store_relation(&self, _r: &Relation) -> Result<()> {
            unimplemented!()
        }
        fn traverse(&self, _id: Uuid, _depth: u32) -> Result<Vec<(Memory, Vec<Relation>)>> {
            unimplemented!()
        }
        fn memories_by_source(&self, _s: &str) -> Result<Vec<Memory>> {
            unimplemented!()
        }
        fn memories_by_type(&self, _mt: MemoryType) -> Result<Vec<Memory>> {
            unimplemented!()
        }
        fn update_memory(&self, _m: &Memory) -> Result<()> {
            unimplemented!()
        }
        fn record_access(&self, _memory: &Memory) -> Result<()> {
            unimplemented!()
        }
        fn bm25_search(&self, _q: &str, _limit: usize) -> Result<Vec<Memory>> {
            unimplemented!()
        }
        fn get_memory_with_embedding(&self, _id: Uuid) -> Result<Option<Memory>> {
            unimplemented!()
        }
        fn memory_count(&self) -> Result<usize> {
            unimplemented!()
        }
        fn all_memory_ids(&self) -> Result<Vec<Uuid>> {
            Ok(self.vector_results.iter().map(|(m, _)| m.id).collect())
        }
        fn all_relations(&self) -> Result<Vec<Relation>> {
            Ok(self.relations.clone())
        }
    }

    #[test]
    fn methods_with_trait_defaults_reach_the_inner_store() {
        let sqlite = SqliteStore::in_memory("m".to_string()).unwrap();
        let raw = Memory::new(
            "a transcript chunk".to_string(),
            MemoryType::Semantic,
            "claude-code".to_string(),
            String::new(),
        );
        let distillate = Memory::new(
            "its distillate".to_string(),
            MemoryType::Semantic,
            "consolidation".to_string(),
            String::new(),
        );
        sqlite.store_memory(&raw).unwrap();
        sqlite.store_memory(&distillate).unwrap();
        sqlite
            .mark_consolidated(raw.id, distillate.id, "model")
            .unwrap();

        let caching = CachingStore::new(&sqlite);
        assert!(
            caching.source_admits(&distillate, "claude-code").unwrap(),
            "a distillate answers for its raws' source, as it does in the live store"
        );

        caching.record_accesses(&[raw.id]).unwrap();
        let touched = sqlite.get_memory(raw.id).unwrap().unwrap();
        assert_eq!(touched.access_count, 1);
        assert!((touched.confidence - raw.confidence).abs() < f32::EPSILON);

        assert!(!caching.remove_memory(Uuid::new_v4()).unwrap());
        assert!(caching.remove_memory(raw.id).unwrap());
    }

    fn memory(content: &str, days_old: i64) -> Memory {
        let when = Utc::now() - Duration::days(days_old);
        let mut m = Memory::new(
            content.to_string(),
            MemoryType::Semantic,
            "test".to_string(),
            String::new(),
        );
        m.created_at = when;
        m.last_accessed = when;
        m
    }

    fn fixture() -> (Vec<(Memory, f32)>, Vec<Relation>) {
        let a = memory("kuzu was chosen as the embedded graph store", 10);
        let b = memory("sync runs bidirectionally over ssh", 40);
        let c = memory("embeddings use the bge model", 5);
        let rel = |from: Uuid, rt: RelationType, strength: f32| Relation {
            from_id: from,
            to_id: Uuid::new_v4(),
            relation_type: rt,
            strength,
            context: None,
        };
        let relations = vec![
            rel(a.id, RelationType::Reinforces, 1.0),
            rel(a.id, RelationType::RelatesTo, 0.7),
            rel(b.id, RelationType::Mentions, 1.0),
            rel(c.id, RelationType::RelatesTo, 0.4),
            rel(c.id, RelationType::Supersedes, 1.0),
        ];
        let vector_results = vec![(a, 0.91), (b, 0.78), (c, 0.66)];
        (vector_results, relations)
    }

    fn request() -> QueryRequest {
        QueryRequest {
            text: "graph store choice".to_string(),
            embedding: vec![0.1_f32; 384],
            limit: 10,
            filters: QueryFilters::default(),
        }
    }

    #[test]
    fn caching_store_recall_matches_raw_store() {
        let (vector_results, relations) = fixture();
        let raw = InMemoryStore {
            vector_results,
            relations,
            get_relations_calls: std::sync::atomic::AtomicUsize::new(0),
        };

        let baseline = QueryEngine::new(&raw).recall(&request()).unwrap();

        let cached = CachingStore::new(&raw);
        let first = QueryEngine::new(&cached).recall(&request()).unwrap();
        let second = QueryEngine::new(&cached).recall(&request()).unwrap();

        assert_eq!(baseline.len(), first.len());
        assert_eq!(first.len(), second.len());
        for (b, c) in baseline.iter().zip(first.iter()) {
            assert_eq!(b.memory.id, c.memory.id, "result order must match");
            assert!(
                (b.score - c.score).abs() < 1e-6,
                "scores must match: {} vs {}",
                b.score,
                c.score
            );
        }
        // The second recall through the cache must produce the identical
        // ranking, proving the memoized path is faithful across repeats.
        for (a, c) in first.iter().zip(second.iter()) {
            assert_eq!(a.memory.id, c.memory.id);
            assert!((a.score - c.score).abs() < 1e-6);
        }
    }

    #[test]
    fn prewarmed_store_recall_matches_raw_and_skips_live_reads() {
        use std::sync::atomic::Ordering;
        let (vector_results, relations) = fixture();
        let raw = InMemoryStore {
            vector_results,
            relations,
            get_relations_calls: std::sync::atomic::AtomicUsize::new(0),
        };

        let baseline = QueryEngine::new(&raw).recall(&request()).unwrap();
        let calls_after_baseline = raw.get_relations_calls.load(Ordering::Relaxed);
        assert!(calls_after_baseline > 0, "baseline must hit the live store");

        let cached = CachingStore::new(&raw);
        cached.prewarm().unwrap();
        let prewarm_calls = raw.get_relations_calls.load(Ordering::Relaxed);

        let recalled = QueryEngine::new(&cached).recall(&request()).unwrap();

        // prewarm uses all_relations, never get_relations, and seeds every
        // (id, scored_type) pair, so recall must trigger zero live reads.
        assert_eq!(
            raw.get_relations_calls.load(Ordering::Relaxed),
            prewarm_calls,
            "prewarmed recall must not call inner.get_relations"
        );
        assert_eq!(
            prewarm_calls, calls_after_baseline,
            "prewarm must not read via get_relations"
        );

        assert_eq!(baseline.len(), recalled.len());
        for (b, c) in baseline.iter().zip(recalled.iter()) {
            assert_eq!(b.memory.id, c.memory.id, "result order must match");
            assert!(
                (b.score - c.score).abs() < 1e-6,
                "scores must match: {} vs {}",
                b.score,
                c.score
            );
        }
    }
}