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
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
//! Memory store operations: save/load, index management, search.
//!
//! Integrates HNSW index (usearch) for fast approximate nearest neighbor search
//! alongside the existing file-based state store for persistence.
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::atomic::{AtomicU64, Ordering};
use anyhow::Result;
use chrono::{DateTime, Utc};
use parking_lot::RwLock;
use serde::{Deserialize, Serialize};
use crate::embedding::EmbeddingVector;
use super::hnsw::HnswIndex;
use super::normalizer::l2_normalize_f32;
use super::{content_hash, dedup_by_id, extract_keywords, MemoryEntry, MemoryManager, MemoryType};
// ---------------------------------------------------------------------------
// VectorIndexSnapshot
// ---------------------------------------------------------------------------
/// Snapshot of the vector index for persistence.
#[derive(Debug, Clone, Serialize, Deserialize)]
struct VectorIndexSnapshot {
/// Snapshot creation timestamp.
created_at: DateTime<Utc>,
/// Number of entries in the snapshot.
entry_count: usize,
/// Map of entry ID to embedding vector.
entries: HashMap<String, EmbeddingVector>,
}
// ---------------------------------------------------------------------------
// Store & search operations
// ---------------------------------------------------------------------------
impl MemoryManager {
/// Returns total entries across all memory types (from disk).
pub async fn total_entries(&self) -> usize {
let mut total = 0;
for mt in [
MemoryType::Conversation,
MemoryType::Session,
MemoryType::Fact,
MemoryType::Episode,
MemoryType::Knowledge,
] {
// Use a large fixed limit to avoid overflow with usize::MAX
if let Ok(entries) = self.list(mt, 1_000_000).await {
total += entries.len();
}
}
total
}
/// Rebuild the vector index from all stored memories.
///
/// Call once at startup to populate the in-memory index from
/// persisted memory entries.
pub async fn rebuild_index(&self) -> Result<()> {
// Collect all entries outside the lock
let mut entries_to_index: Vec<(String, EmbeddingVector)> = Vec::new();
for mt in &[
MemoryType::Conversation,
MemoryType::Session,
MemoryType::Fact,
MemoryType::Episode,
MemoryType::Knowledge,
] {
if let Ok(names) = self.state_store.list_category(mt.category()).await {
for name in names {
if let Ok(Some(entry)) = self
.state_store
.load_json::<MemoryEntry>(mt.category(), &name)
.await
{
let vector = self.embedding.embed(&entry.content).await?;
entries_to_index.push((entry.id.clone(), vector));
}
}
}
}
// Now acquire the lock only for the write
{
let mut index = self.vector_index.write();
index.clear();
for (id, vector) in entries_to_index {
index.insert(id, vector);
}
}
tracing::info!(
entries = self.vector_index.read().len(),
"Memory vector index rebuilt"
);
Ok(())
}
/// Save the current vector index to disk as a snapshot.
pub async fn save_index_snapshot(&self) -> Result<()> {
let snapshot = {
let index = self.vector_index.read();
VectorIndexSnapshot {
created_at: chrono::Utc::now(),
entry_count: index.len(),
entries: index.clone(),
}
};
self.state_store
.save_json("memory", "vector_index_snapshot", &snapshot)
.await?;
self.git_commit("memory/vector_index_snapshot.json", "memory: snapshot save");
tracing::debug!(
entries = snapshot.entry_count,
"Vector index snapshot saved"
);
Ok(())
}
/// Load a previously saved vector index snapshot from disk.
pub async fn load_index_snapshot(&self) -> Result<usize> {
let snapshot: Option<VectorIndexSnapshot> = self
.state_store
.load_json("memory", "vector_index_snapshot")
.await?;
match snapshot {
Some(snap) => {
let count = snap.entry_count;
let mut index = self.vector_index.write();
*index = snap.entries;
tracing::info!(entries = count, "Vector index snapshot loaded");
Ok(count)
}
None => {
tracing::debug!("No vector index snapshot found");
Ok(0)
}
}
}
/// Store a memory entry. Returns the entry ID.
///
/// Also computes and stores the entry's text vector in the in-memory
/// index for future semantic search.
pub async fn remember(&self, entry: MemoryEntry) -> Result<String> {
let id = entry.id.clone();
let vector = self.embedding.embed(&entry.content).await?;
let category = entry.memory_type.category();
self.state_store.save_json(category, &id, &entry).await?;
self.git_commit(
&format!("{}/{}.json", category, id),
&format!("memory: store {}", id),
);
// Update vector index
{
let mut index = self.vector_index.write();
index.insert(id.clone(), vector.clone());
}
// Update HNSW index if attached
if let Some(f32_vec) = vector.to_f32_dense() {
let hnsw = self.hnsw_index.read();
if let Some(ref hnsw) = *hnsw {
if let Err(e) = hnsw.add_entry(&id, &f32_vec) {
tracing::warn!(id = %id, error = %e, "Failed to update HNSW index on remember");
}
}
}
tracing::debug!(id = %id, ty = entry.memory_type.label(), "Memory stored");
Ok(id)
}
/// Retrieve a single memory by ID.
pub async fn get(&self, id: &str, memory_type: MemoryType) -> Result<Option<MemoryEntry>> {
self.state_store.load_json(memory_type.category(), id).await
}
/// Delete a memory entry.
pub async fn forget(&self, id: &str, memory_type: MemoryType) -> Result<bool> {
let result = self
.state_store
.delete_file(memory_type.category(), id)
.await?;
// Remove from HNSW index if attached
{
let hnsw = self.hnsw_index.read();
if let Some(ref hnsw) = *hnsw {
if let Err(e) = hnsw.remove_entry(id) {
tracing::warn!(id = %id, error = %e, "Failed to remove from HNSW index on forget");
}
}
}
Ok(result)
}
/// List memories of a given type, most recent first.
pub async fn list(&self, memory_type: MemoryType, limit: usize) -> Result<Vec<MemoryEntry>> {
let category = memory_type.category();
let names = self.state_store.list_category(category).await?;
let mut entries = Vec::new();
for name in names.into_iter().take(limit.saturating_mul(2)) {
if let Ok(Some(entry)) = self
.state_store
.load_json::<MemoryEntry>(category, &name)
.await
{
entries.push(entry);
}
}
// Sort by created_at descending (most recent first)
entries.sort_by_key(|b| std::cmp::Reverse(b.created_at));
entries.truncate(limit);
Ok(entries)
}
/// Search memories by semantic similarity (vector search).
///
/// Falls back to keyword search when the vector index is empty or
/// yields no results above the similarity threshold.
pub async fn search(
&self,
query: &str,
memory_type: Option<MemoryType>,
limit: usize,
) -> Result<Vec<MemoryEntry>> {
let query_vector = self.embedding.embed(query).await?;
// Scope the read lock: compute scores, then drop before any await.
let scored: Vec<(String, f64)> = {
let index = self.vector_index.read();
let mut scored: Vec<(String, f64)> = index
.iter()
.map(|(id, vector)| {
let score = query_vector.cosine_similarity(vector);
(id.clone(), score)
})
.filter(|(_, score)| *score > 0.1)
.collect();
scored.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));
scored.truncate(limit);
scored
}; // lock dropped here, before any .await
// If index was empty, scored will be empty — fall back immediately
if scored.is_empty() {
return self.keyword_search(query, memory_type, limit).await;
}
// Determine which memory types to search
let all_types: &[MemoryType] = &[
MemoryType::Conversation,
MemoryType::Session,
MemoryType::Fact,
MemoryType::Episode,
MemoryType::Knowledge,
];
let types: &[MemoryType] = match memory_type {
Some(ref t) => std::slice::from_ref(t),
None => all_types,
};
// Load entries from state store (no lock held)
let mut results = Vec::new();
for (id, score) in scored {
for mt in types {
if let Ok(Some(mut entry)) = self
.state_store
.load_json::<MemoryEntry>(mt.category(), &id)
.await
{
entry.access_count += 1;
entry.accessed_at = chrono::Utc::now();
tracing::debug!(id = %id, score, "Vector search hit");
results.push(entry);
break;
}
}
}
// Fall back to keyword search if no results
if results.is_empty() {
return self.keyword_search(query, memory_type, limit).await;
}
Ok(results)
}
/// Keyword-based search (original algorithm, used as fallback).
async fn keyword_search(
&self,
query: &str,
memory_type: Option<MemoryType>,
limit: usize,
) -> Result<Vec<MemoryEntry>> {
let keywords = extract_keywords(query);
let types = match memory_type {
Some(t) => vec![t],
None => vec![
MemoryType::Conversation,
MemoryType::Fact,
MemoryType::Episode,
MemoryType::Knowledge,
],
};
let mut results = Vec::new();
for ty in &types {
let entries = self.list(*ty, limit * 2).await?;
for entry in entries {
let matches = keywords.iter().any(|k| {
let k_lower = k.to_lowercase();
entry.content.to_lowercase().contains(&k_lower)
|| entry
.tags
.iter()
.any(|t| t.to_lowercase().contains(&k_lower))
});
if matches {
results.push(entry);
}
}
}
results.sort_by(|a, b| {
b.importance
.partial_cmp(&a.importance)
.unwrap_or(std::cmp::Ordering::Equal)
});
results.truncate(limit);
Ok(results)
}
/// Recall relevant memories for a new session.
///
/// Combines recent conversation summaries, session summaries,
/// and keyword-matched facts/episodes.
pub async fn recall(&self, query: &str) -> Result<Vec<MemoryEntry>> {
let limit = self.max_recall;
// 1. Recent conversation summaries (always include)
let recent = self
.list(MemoryType::Conversation, 3)
.await
.unwrap_or_default();
// 2. Recent session summaries
let sessions = self.list(MemoryType::Session, 2).await.unwrap_or_default();
// 3. Keyword-matched facts and episodes
let relevant = self.search(query, None, limit).await.unwrap_or_default();
// 4. Combine and deduplicate
let mut combined = recent;
combined.extend(sessions);
combined.extend(relevant);
dedup_by_id(&mut combined);
combined.truncate(limit);
Ok(combined)
}
/// Blend recalled memories into the system prompt.
pub fn blend_into_prompt(&self, memories: &[MemoryEntry], system_prompt: &str) -> String {
if memories.is_empty() {
return system_prompt.to_string();
}
let memory_block = memories
.iter()
.map(|m| format!("- [{}] {}", m.memory_type.label(), m.content))
.collect::<Vec<_>>()
.join("\n");
format!("{system_prompt}\n\n## Relevant Memory\n\n{memory_block}")
}
/// Create a session summary memory entry from a completed session.
///
/// This does NOT use LLM — it records key metadata from the session
/// as a structured memory entry for future reference.
pub async fn summarize_session(
&self,
session: &crate::state_store::Session,
) -> Result<Option<String>> {
if session.user_messages.is_empty() {
return Ok(None);
}
// Build a summary from the session metadata
let mut summary_parts = Vec::new();
// Include the first user message as context
if let Some(first_msg) = session.user_messages.first() {
summary_parts.push(format!("User: {}", first_msg.content));
}
// Include the last agent response
if let Some(last_response) = session.agent_responses.last() {
let truncated = if last_response.content.len() > 500 {
format!("{}...", &last_response.content[..500])
} else {
last_response.content.clone()
};
summary_parts.push(format!("Agent: {}", truncated));
}
// Include metadata
if let Some(ref seed_id) = session.active_seed_id {
summary_parts.push(format!("Seed: {}", seed_id));
}
if let Some(ref persona_id) = session.active_persona_id {
summary_parts.push(format!("Persona: {}", persona_id));
}
let content = summary_parts.join("\n");
let entry = MemoryEntry {
id: format!(
"session-{}-{}",
session.id.0,
chrono::Utc::now().timestamp()
),
memory_type: MemoryType::Session,
content,
source: "session_summary".to_string(),
session_id: Some(session.id.0.clone()),
tags: vec![],
importance: 0.6,
created_at: chrono::Utc::now(),
accessed_at: chrono::Utc::now(),
access_count: 0,
};
let id = self.remember(entry).await?;
Ok(Some(id))
}
/// Check if a memory entry with identical content already exists.
///
/// Uses a fast hash comparison against the in-memory vector index.
pub async fn is_duplicate(&self, content: &str) -> bool {
let hash = content_hash(content);
// Check semantic similarity via vector index first (fast)
let query_vector = match self.embedding.embed(content).await {
Ok(v) => v,
Err(_) => return false,
};
let similar = {
let index = self.vector_index.read();
index
.iter()
.any(|(_, vector)| query_vector.cosine_similarity(vector) > 0.95)
};
if similar {
return true;
}
// Then check exact content hash across all types
for mt in &[
MemoryType::Conversation,
MemoryType::Session,
MemoryType::Fact,
MemoryType::Episode,
MemoryType::Knowledge,
] {
if let Ok(entries) = self.list(*mt, 1000).await {
for entry in entries {
if content_hash(&entry.content) == hash {
return true;
}
}
}
}
false
}
/// Store a memory entry only if no duplicate content exists.
///
/// Returns the entry ID if stored, or `None` if duplicate.
pub async fn remember_unique(&self, entry: MemoryEntry) -> Result<Option<String>> {
if self.is_duplicate(&entry.content).await {
tracing::debug!(id = %entry.id, "Skipping duplicate memory");
return Ok(None);
}
let id = self.remember(entry).await?;
Ok(Some(id))
}
}
// ---------------------------------------------------------------------------
// HNSW-augmented operations
// ---------------------------------------------------------------------------
/// Result of a semantic search hit.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SemanticHit {
/// Memory entry.
pub entry: MemoryEntry,
/// Cosine distance (0.0 = identical).
pub distance: f32,
/// Cosine similarity (1.0 = identical).
pub similarity: f32,
}
/// HNSW index manager for memory entries.
///
/// Maintains a mapping from u64 keys to String IDs, and the HNSW index
/// itself. Thread-safe via `RwLock`.
pub struct HnswMemoryIndex {
/// The HNSW index.
index: RwLock<HnswIndex>,
/// Map: u64 key → String memory ID.
key_to_id: RwLock<HashMap<u64, String>>,
/// Map: String memory ID → u64 key.
id_to_key: RwLock<HashMap<String, u64>>,
/// Next key counter.
next_key: AtomicU64,
/// Base path for index persistence.
persist_path: Option<PathBuf>,
}
impl std::fmt::Debug for HnswMemoryIndex {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("HnswMemoryIndex")
.field("size", &self.len())
.field("dimensions", &self.index.read().dimensions())
.finish()
}
}
impl HnswMemoryIndex {
/// Create a new HNSW memory index.
///
/// # Arguments
/// * `dimensions` — Embedding vector dimensions.
/// * `capacity` — Initial capacity hint.
/// * `persist_path` — Optional directory for index file persistence.
pub fn new(dimensions: usize, capacity: usize, persist_path: Option<PathBuf>) -> Result<Self> {
let index = HnswIndex::new(dimensions, capacity)?;
Ok(Self {
index: RwLock::new(index),
key_to_id: RwLock::new(HashMap::new()),
id_to_key: RwLock::new(HashMap::new()),
next_key: AtomicU64::new(1), // 0 is used by usearch as sentinel
persist_path,
})
}
/// Try to restore from disk, fall back to new index.
pub fn restore_or_new(
dimensions: usize,
capacity: usize,
persist_path: Option<PathBuf>,
) -> Result<Self> {
if let Some(ref path) = persist_path {
let index_path = path.join("memory.usearch");
let mapping_path = path.join("key_map.json");
if index_path.exists() && mapping_path.exists() {
tracing::info!(path = %index_path.display(), "Restoring HNSW index from disk");
if let Ok(index) = HnswIndex::load(&index_path) {
if let Ok(data) = std::fs::read_to_string(&mapping_path) {
if let Ok((k2i, i2k)) = serde_json::from_str::<(
HashMap<u64, String>,
HashMap<String, u64>,
)>(&data)
{
let max_key = k2i.keys().max().copied().unwrap_or(0);
return Ok(Self {
index: RwLock::new(index),
key_to_id: RwLock::new(k2i),
id_to_key: RwLock::new(i2k),
next_key: AtomicU64::new(max_key + 1),
persist_path,
});
}
}
}
tracing::warn!("Failed to restore HNSW index, creating new one");
}
}
Self::new(dimensions, capacity, persist_path)
}
/// Get or create a u64 key for a String ID.
fn get_or_create_key(&self, id: &str) -> u64 {
// Fast path: check read lock
{
let i2k = self.id_to_key.read();
if let Some(&key) = i2k.get(id) {
return key;
}
}
// Slow path: write lock
let mut i2k = self.id_to_key.write();
let mut k2i = self.key_to_id.write();
// Double-check after acquiring write lock
if let Some(&key) = i2k.get(id) {
return key;
}
let key = self.next_key.fetch_add(1, Ordering::Relaxed);
i2k.insert(id.to_string(), key);
k2i.insert(key, id.to_string());
key
}
/// Add an entry to the HNSW index.
pub fn add_entry(&self, id: &str, vector: &[f32]) -> Result<()> {
let key = self.get_or_create_key(id);
let mut normalized = vector.to_vec();
l2_normalize_f32(&mut normalized);
self.index.write().add(key, &normalized)?;
Ok(())
}
/// Remove an entry from the index.
pub fn remove_entry(&self, id: &str) -> Result<()> {
let key = {
let i2k = self.id_to_key.read();
i2k.get(id).copied()
};
if let Some(key) = key {
self.index.write().remove(key)?;
let mut k2i = self.key_to_id.write();
let mut i2k = self.id_to_key.write();
k2i.remove(&key);
i2k.remove(id);
}
Ok(())
}
/// Search for k nearest neighbors.
///
/// Returns (String ID, distance) pairs.
pub fn search(&self, query: &[f32], k: usize) -> Result<Vec<(String, f32)>> {
let mut normalized = query.to_vec();
l2_normalize_f32(&mut normalized);
let raw = self.index.read().search(&normalized, k)?;
let k2i = self.key_to_id.read();
let results = raw
.into_iter()
.filter_map(|(key, dist)| k2i.get(&key).map(|id| (id.clone(), dist)))
.collect();
Ok(results)
}
/// Number of entries in the index.
pub fn len(&self) -> usize {
self.index.read().len()
}
/// Whether the index is empty.
pub fn is_empty(&self) -> bool {
self.index.read().is_empty()
}
/// Save the index and key mappings to disk.
pub fn persist(&self) -> Result<()> {
if let Some(ref path) = self.persist_path {
std::fs::create_dir_all(path)?;
let index_path = path.join("memory.usearch");
let mapping_path = path.join("key_map.json");
// Save index
self.index.read().save(&index_path)?;
// Save key mappings
let k2i = self.key_to_id.read();
let i2k = self.id_to_key.read();
let data = serde_json::to_string(&(k2i.clone(), &*i2k))?;
std::fs::write(&mapping_path, data)?;
tracing::debug!(path = %path.display(), entries = self.len(), "HNSW index persisted");
}
Ok(())
}
}
// ---------------------------------------------------------------------------
// Semantic search on MemoryManager
// ---------------------------------------------------------------------------
impl MemoryManager {
/// Semantic search using HNSW index.
///
/// Unlike `search()` which uses brute-force cosine similarity over the
/// in-memory HashMap, `semantic_search()` uses the HNSW approximate
/// nearest neighbor index for sub-linear time complexity.
///
/// This is the preferred search method when the HNSW index is available
/// and populated with dense vectors.
///
/// # Arguments
/// * `query` — Search query text.
/// * `memory_type` — Optional filter by memory type.
/// * `limit` — Maximum results to return.
/// * `hnsw_index` — The HNSW index to search against.
///
/// # Returns
/// A list of `SemanticHit` with entry and similarity score.
pub async fn semantic_search(
&self,
query: &str,
memory_type: Option<MemoryType>,
limit: usize,
hnsw_index: &HnswMemoryIndex,
) -> Result<Vec<SemanticHit>> {
// Skip if index is empty
if hnsw_index.is_empty() {
tracing::debug!("HNSW index empty, falling back to keyword search");
return self
.keyword_search(query, memory_type, limit)
.await
.map(|entries| {
entries
.into_iter()
.map(|entry| SemanticHit {
entry,
distance: 0.0,
similarity: 0.0,
})
.collect()
});
}
// Generate embedding for query
let query_vector = self.embedding.embed(query).await?;
let query_f32 = match query_vector.to_f32_dense() {
Some(v) => v,
None => {
tracing::debug!("Query embedding is sparse, falling back to keyword search");
return self
.keyword_search(query, memory_type, limit)
.await
.map(|entries| {
entries
.into_iter()
.map(|entry| SemanticHit {
entry,
distance: 0.0,
similarity: 0.0,
})
.collect()
});
}
};
// Search HNSW index
let raw_hits = hnsw_index.search(&query_f32, limit * 2)?;
// Determine which memory types to search
let all_types: &[MemoryType] = &[
MemoryType::Conversation,
MemoryType::Session,
MemoryType::Fact,
MemoryType::Episode,
MemoryType::Knowledge,
];
let types: &[MemoryType] = match memory_type {
Some(ref t) => std::slice::from_ref(t),
None => all_types,
};
// Load entries and build results
let mut results = Vec::new();
for (id, distance) in raw_hits {
for mt in types {
if let Ok(Some(mut entry)) = self
.state_store
.load_json::<MemoryEntry>(mt.category(), &id)
.await
{
// Update access stats
entry.access_count += 1;
entry.accessed_at = chrono::Utc::now();
let similarity = 1.0 - distance;
results.push(SemanticHit {
entry,
distance,
similarity,
});
break;
}
}
if results.len() >= limit {
break;
}
}
// Sort by similarity descending
results.sort_by(|a, b| {
b.similarity
.partial_cmp(&a.similarity)
.unwrap_or(std::cmp::Ordering::Equal)
});
tracing::debug!(
query = %query,
hits = results.len(),
"Semantic search complete"
);
// Fall back if no results
if results.is_empty() {
return self
.keyword_search(query, memory_type, limit)
.await
.map(|entries| {
entries
.into_iter()
.map(|entry| SemanticHit {
entry,
distance: 0.0,
similarity: 0.0,
})
.collect()
});
}
Ok(results)
}
/// Rebuild the HNSW index from all stored memories.
///
/// Call this at startup or after bulk operations.
pub async fn rebuild_hnsw_index(&self, hnsw_index: &HnswMemoryIndex) -> Result<usize> {
let mut count = 0;
for mt in &[
MemoryType::Conversation,
MemoryType::Session,
MemoryType::Fact,
MemoryType::Episode,
MemoryType::Knowledge,
] {
if let Ok(names) = self.state_store.list_category(mt.category()).await {
for name in names {
if let Ok(Some(entry)) = self
.state_store
.load_json::<MemoryEntry>(mt.category(), &name)
.await
{
let vector = self.embedding.embed(&entry.content).await?;
if let Some(f32_vec) = vector.to_f32_dense() {
if let Err(e) = hnsw_index.add_entry(&entry.id, &f32_vec) {
tracing::warn!(
id = %entry.id,
error = %e,
"Failed to add entry to HNSW index"
);
continue;
}
count += 1;
}
}
}
}
}
tracing::info!(entries = count, "HNSW index rebuilt");
Ok(count)
}
}