forked from 1jehuang/jcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory.rs
More file actions
1820 lines (1611 loc) · 62.3 KB
/
memory.rs
File metadata and controls
1820 lines (1611 loc) · 62.3 KB
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
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! Memory system for cross-session learning
//!
//! Provides persistent memory that survives across sessions, organized by:
//! - Project (per working directory)
//! - Global (user-level preferences)
//!
//! Integrates with the Haiku sidecar for relevance verification and extraction.
use crate::memory_graph::{GRAPH_VERSION, MemoryGraph};
use crate::memory_types::{
InjectedMemoryItem, MemoryActivity, MemoryEvent, MemoryEventKind, MemoryState, StepResult,
StepStatus,
ranking::{top_k_by_ord, top_k_by_score},
};
use crate::sidecar::Sidecar;
use crate::storage;
use anyhow::Result;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
use std::sync::Arc;
use std::time::Instant;
#[path = "memory/activity.rs"]
mod activity;
mod cache;
#[path = "memory/pending.rs"]
mod pending;
#[path = "memory_prompt.rs"]
mod prompt_support;
pub use crate::memory_types::{
MemoryCategory, MemoryEntry, MemoryScope, MemoryStore, Reinforcement, TrustLevel,
format_relevant_display_prompt, format_relevant_prompt,
};
use crate::memory_types::{
collect_skill_query_terms, format_entries_for_prompt, memory_matches_search, memory_score,
normalize_memory_search_text, normalize_search_text, skill_retrieval_bonus,
};
pub use activity::{
activity_snapshot, add_event, apply_remote_activity_snapshot, check_staleness, clear_activity,
get_activity, pipeline_start, pipeline_update, record_injected_prompt, set_state,
};
use cache::{cache_graph, cached_graph};
#[cfg(test)]
use pending::insert_pending_memory_for_test;
pub use pending::{
PendingMemory, clear_all_injected_memories, clear_all_pending_memory, clear_injected_memories,
clear_pending_memory, has_any_pending_memory, has_pending_memory, is_memory_injected,
is_memory_injected_any, mark_memories_injected, set_pending_memory,
set_pending_memory_with_ids, set_pending_memory_with_ids_and_display, sync_injected_memories,
take_pending_memory,
};
use pending::{begin_memory_check, finish_memory_check};
pub(crate) use prompt_support::{format_context_for_extraction, format_context_for_relevance};
const LEGACY_NOTE_CATEGORY: &str = "note";
const MEMORY_RELEVANCE_MAX_CANDIDATES: usize = 30;
const MEMORY_RELEVANCE_MAX_RESULTS: usize = 10;
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
struct LegacyNotesFile {
#[serde(default)]
entries: Vec<LegacyNoteEntry>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
struct LegacyNoteEntry {
id: String,
content: String,
created_at: DateTime<Utc>,
#[serde(default, skip_serializing_if = "Option::is_none")]
tag: Option<String>,
}
pub type MemoryEventSink = Arc<dyn Fn(crate::protocol::ServerEvent) + Send + Sync>;
pub fn memory_sidecar_enabled() -> bool {
crate::config::config().agents.memory_sidecar_enabled
}
fn emit_memory_activity(event_tx: Option<&MemoryEventSink>) {
let (Some(event_tx), Some(activity)) = (event_tx, activity_snapshot()) else {
return;
};
(event_tx)(crate::protocol::ServerEvent::MemoryActivity { activity });
}
trait MemoryEntryEmbeddingExt {
fn ensure_embedding(&mut self) -> bool;
}
impl MemoryEntryEmbeddingExt for MemoryEntry {
/// Generate and set embedding if not already present.
/// Returns true if embedding was generated, false if already exists or failed.
fn ensure_embedding(&mut self) -> bool {
if self.embedding.is_some() {
return false;
}
match crate::embedding::embed(&self.content) {
Ok(embedding) => {
self.embedding = Some(embedding);
true
}
Err(err) => {
crate::logging::info(&format!("Failed to generate embedding: {err}"));
false
}
}
}
}
#[derive(Debug, Clone)]
pub struct MemoryManager {
project_dir: Option<PathBuf>,
/// When true, use isolated test storage instead of real memory
test_mode: bool,
include_skills: bool,
}
impl MemoryManager {
pub fn new() -> Self {
Self {
project_dir: None,
test_mode: false,
include_skills: true,
}
}
pub fn with_project_dir(mut self, project_dir: impl Into<PathBuf>) -> Self {
self.project_dir = Some(project_dir.into());
self
}
pub fn with_skills(mut self, include_skills: bool) -> Self {
self.include_skills = include_skills;
self
}
/// Create a memory manager in test mode (isolated storage)
pub fn new_test() -> Self {
Self {
project_dir: None,
test_mode: true,
include_skills: true,
}
}
/// Check if running in test mode
pub fn is_test_mode(&self) -> bool {
self.test_mode
}
/// Set test mode (for debug sessions)
pub fn set_test_mode(&mut self, test_mode: bool) {
self.test_mode = test_mode;
}
/// Clear all test memories (only works in test mode)
pub fn clear_test_storage(&self) -> Result<()> {
if !self.test_mode {
anyhow::bail!("clear_test_storage only allowed in test mode");
}
let test_dir = storage::jcode_dir()?.join("memory").join("test");
if test_dir.exists() {
std::fs::remove_dir_all(&test_dir)?;
crate::logging::info("Cleared test memory storage");
}
Ok(())
}
fn get_project_dir(&self) -> Option<PathBuf> {
self.project_dir
.clone()
.or_else(|| std::env::current_dir().ok())
}
fn project_memory_path(&self) -> Result<Option<PathBuf>> {
// In test mode, use test directory
if self.test_mode {
let test_dir = storage::jcode_dir()?.join("memory").join("test");
std::fs::create_dir_all(&test_dir)?;
return Ok(Some(test_dir.join("test_project.json")));
}
let project_dir = match self.get_project_dir() {
Some(d) => d,
None => return Ok(None),
};
let project_hash = {
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
let mut hasher = DefaultHasher::new();
project_dir.hash(&mut hasher);
format!("{:016x}", hasher.finish())
};
let memory_dir = storage::jcode_dir()?.join("memory").join("projects");
Ok(Some(memory_dir.join(format!("{}.json", project_hash))))
}
fn legacy_notes_path(&self) -> Result<Option<PathBuf>> {
if self.test_mode {
let test_dir = storage::jcode_dir()?.join("notes").join("test");
std::fs::create_dir_all(&test_dir)?;
return Ok(Some(test_dir.join("test_notes.json")));
}
let project_dir = match self.get_project_dir() {
Some(d) => d,
None => return Ok(None),
};
let project_hash = {
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
let mut hasher = DefaultHasher::new();
project_dir.hash(&mut hasher);
format!("{:016x}", hasher.finish())
};
Ok(Some(
storage::jcode_dir()?
.join("notes")
.join(format!("{}.json", project_hash)),
))
}
fn normalize_graph_search_text(graph: &mut MemoryGraph) -> bool {
let mut changed = false;
for memory in graph.memories.values_mut() {
let expected = normalize_memory_search_text(&memory.content, &memory.tags);
if memory.search_text != expected {
memory.search_text = expected;
changed = true;
}
}
changed
}
fn import_legacy_notes_into_graph(&self, graph: &mut MemoryGraph) -> Result<bool> {
let Some(path) = self.legacy_notes_path()? else {
return Ok(false);
};
if !path.exists() {
return Ok(false);
}
let legacy: LegacyNotesFile = storage::read_json(&path)?;
if legacy.entries.is_empty() {
return Ok(false);
}
let mut changed = false;
for note in legacy.entries {
if graph.memories.contains_key(¬e.id) {
continue;
}
let mut entry = MemoryEntry::new(
MemoryCategory::Custom(LEGACY_NOTE_CATEGORY.to_string()),
note.content,
);
entry.id = note.id;
entry.created_at = note.created_at;
entry.updated_at = note.created_at;
entry.source = Some("legacy_remember_migration".to_string());
if let Some(tag) = note.tag {
entry.tags.push(tag);
}
entry.ensure_embedding();
graph.add_memory(entry);
changed = true;
}
Ok(changed)
}
fn global_memory_path(&self) -> Result<PathBuf> {
if self.test_mode {
let test_dir = storage::jcode_dir()?.join("memory").join("test");
std::fs::create_dir_all(&test_dir)?;
Ok(test_dir.join("test_global.json"))
} else {
Ok(storage::jcode_dir()?.join("memory").join("global.json"))
}
}
pub fn load_project(&self) -> Result<MemoryStore> {
match self.project_memory_path()? {
Some(path) if path.exists() => storage::read_json(&path),
_ => Ok(MemoryStore::new()),
}
}
pub fn load_global(&self) -> Result<MemoryStore> {
let path = self.global_memory_path()?;
if path.exists() {
storage::read_json(&path)
} else {
Ok(MemoryStore::new())
}
}
pub fn save_project(&self, store: &MemoryStore) -> Result<()> {
if let Some(path) = self.project_memory_path()? {
storage::write_json(&path, store)?;
}
Ok(())
}
pub fn save_global(&self, store: &MemoryStore) -> Result<()> {
let path = self.global_memory_path()?;
storage::write_json(&path, store)
}
/// Similarity threshold for storage-layer dedup.
/// Memories above this threshold are considered duplicates and reinforced instead.
const STORAGE_DEDUP_THRESHOLD: f32 = 0.85;
pub fn remember_project(&self, entry: MemoryEntry) -> Result<String> {
let mut entry = entry;
if self.should_generate_embedding_for_entry(&entry) {
entry.ensure_embedding();
}
let mut graph = self.load_project_graph()?;
if let Some(ref emb) = entry.embedding {
if let Some(existing_id) =
Self::find_duplicate_in_graph(&graph, emb, Self::STORAGE_DEDUP_THRESHOLD)
&& let Some(existing) = graph.get_memory_mut(&existing_id)
{
existing.reinforce(entry.source.as_deref().unwrap_or("dedup"), 0);
self.save_project_graph(&graph)?;
return Ok(existing_id);
}
// Cross-store dedup: also check global graph
if let Ok(mut global_graph) = self.load_global_graph()
&& let Some(existing_id) =
Self::find_duplicate_in_graph(&global_graph, emb, Self::STORAGE_DEDUP_THRESHOLD)
&& let Some(existing) = global_graph.get_memory_mut(&existing_id)
{
existing.reinforce(entry.source.as_deref().unwrap_or("cross-dedup"), 0);
self.save_global_graph(&global_graph)?;
return Ok(existing_id);
}
}
let id = graph.add_memory(entry);
self.save_project_graph(&graph)?;
Ok(id)
}
pub fn remember_global(&self, entry: MemoryEntry) -> Result<String> {
let mut entry = entry;
if self.should_generate_embedding_for_entry(&entry) {
entry.ensure_embedding();
}
let mut graph = self.load_global_graph()?;
if let Some(ref emb) = entry.embedding {
if let Some(existing_id) =
Self::find_duplicate_in_graph(&graph, emb, Self::STORAGE_DEDUP_THRESHOLD)
&& let Some(existing) = graph.get_memory_mut(&existing_id)
{
existing.reinforce(entry.source.as_deref().unwrap_or("dedup"), 0);
self.save_global_graph(&graph)?;
return Ok(existing_id);
}
// Cross-store dedup: also check project graph
if let Ok(mut project_graph) = self.load_project_graph()
&& let Some(existing_id) = Self::find_duplicate_in_graph(
&project_graph,
emb,
Self::STORAGE_DEDUP_THRESHOLD,
)
&& let Some(existing) = project_graph.get_memory_mut(&existing_id)
{
existing.reinforce(entry.source.as_deref().unwrap_or("cross-dedup"), 0);
self.save_project_graph(&project_graph)?;
return Ok(existing_id);
}
}
let id = graph.add_memory(entry);
self.save_global_graph(&graph)?;
Ok(id)
}
/// Insert or update a memory with a stable ID in the project graph.
/// Preserves existing inbound/outbound graph relationships while refreshing
/// content and tags.
pub fn upsert_project_memory(&self, entry: MemoryEntry) -> Result<String> {
let mut graph = self.load_project_graph()?;
let id = self.upsert_memory_in_graph(&mut graph, entry);
self.save_project_graph(&graph)?;
Ok(id)
}
/// Insert or update a memory with a stable ID in the global graph.
/// Preserves existing inbound/outbound graph relationships while refreshing
/// content and tags.
pub fn upsert_global_memory(&self, entry: MemoryEntry) -> Result<String> {
let mut graph = self.load_global_graph()?;
let id = self.upsert_memory_in_graph(&mut graph, entry);
self.save_global_graph(&graph)?;
Ok(id)
}
fn upsert_memory_in_graph(
&self,
graph: &mut crate::memory_graph::MemoryGraph,
mut entry: MemoryEntry,
) -> String {
let id = entry.id.clone();
let should_generate_embedding = self.should_generate_embedding_for_entry(&entry);
if should_generate_embedding {
entry.ensure_embedding();
}
let Some(existing_snapshot) = graph.get_memory(&id).cloned() else {
return graph.add_memory(entry);
};
let old_tags: std::collections::HashSet<String> =
existing_snapshot.tags.iter().cloned().collect();
let new_tags: std::collections::HashSet<String> = entry.tags.iter().cloned().collect();
for tag in old_tags.difference(&new_tags) {
graph.untag_memory(&id, tag);
}
for tag in new_tags.difference(&old_tags) {
graph.tag_memory(&id, tag);
}
if let Some(existing) = graph.get_memory_mut(&id) {
let content_changed = existing.content != entry.content;
existing.category = entry.category;
existing.content = entry.content;
existing.tags = entry.tags;
existing.updated_at = entry.updated_at;
existing.source = entry.source;
existing.trust = entry.trust;
existing.active = entry.active;
existing.superseded_by = entry.superseded_by;
existing.confidence = entry.confidence;
if content_changed && should_generate_embedding {
existing.embedding = None;
existing.ensure_embedding();
} else if content_changed {
existing.embedding = None;
}
}
id
}
fn should_generate_embedding_for_entry(&self, entry: &MemoryEntry) -> bool {
if self.test_mode {
return false;
}
#[cfg(test)]
if std::env::var_os("JCODE_TEST_ALLOW_MEMORY_EMBEDDINGS").is_none() {
return false;
}
!matches!(&entry.category, MemoryCategory::Custom(category) if category == "goal")
}
fn find_duplicate_in_graph(
graph: &crate::memory_graph::MemoryGraph,
query_emb: &[f32],
threshold: f32,
) -> Option<String> {
let mut best: Option<(String, f32)> = None;
for entry in graph.active_memories() {
if let Some(ref emb) = entry.embedding {
let sim = crate::embedding::cosine_similarity(query_emb, emb);
if sim >= threshold && best.as_ref().map(|(_, s)| sim > *s).unwrap_or(true) {
best = Some((entry.id.clone(), sim));
}
}
}
best.map(|(id, _)| id)
}
/// Find memories similar to the given text using embedding search
/// Returns memories with similarity above threshold, sorted by similarity
pub fn find_similar(
&self,
text: &str,
threshold: f32,
limit: usize,
) -> Result<Vec<(MemoryEntry, f32)>> {
// Generate embedding for query text
let query_embedding = match crate::embedding::embed(text) {
Ok(emb) => emb,
Err(e) => {
crate::logging::info(&format!(
"Embedding failed, falling back to keyword search: {}",
e
));
return Ok(Vec::new());
}
};
self.find_similar_with_embedding(&query_embedding, threshold, limit)
}
pub fn find_similar_scoped(
&self,
text: &str,
threshold: f32,
limit: usize,
scope: MemoryScope,
) -> Result<Vec<(MemoryEntry, f32)>> {
let query_embedding = match crate::embedding::embed(text) {
Ok(emb) => emb,
Err(e) => {
crate::logging::info(&format!(
"Embedding failed, falling back to keyword search: {}",
e
));
return Ok(Vec::new());
}
};
self.find_similar_with_embedding_scoped(&query_embedding, threshold, limit, scope)
}
/// Find memories similar to the given embedding
pub fn find_similar_with_embedding(
&self,
query_embedding: &[f32],
threshold: f32,
limit: usize,
) -> Result<Vec<(MemoryEntry, f32)>> {
let entries_with_emb = self.collect_all_memories_with_embeddings()?;
Self::score_and_filter(entries_with_emb, query_embedding, "", threshold, limit)
}
pub fn find_similar_with_embedding_scoped(
&self,
query_embedding: &[f32],
threshold: f32,
limit: usize,
scope: MemoryScope,
) -> Result<Vec<(MemoryEntry, f32)>> {
let entries_with_emb = self.collect_memories_with_embeddings_scoped(scope)?;
Self::score_and_filter(entries_with_emb, query_embedding, "", threshold, limit)
}
fn collect_all_memories_with_embeddings(&self) -> Result<Vec<MemoryEntry>> {
self.collect_memories_with_embeddings_scoped(MemoryScope::All)
}
fn collect_memories_with_embeddings_scoped(
&self,
scope: MemoryScope,
) -> Result<Vec<MemoryEntry>> {
let mut entries: Vec<MemoryEntry> = Vec::new();
if scope.includes_project()
&& let Ok(project) = self.load_project_graph()
{
entries.extend(
project
.active_memories()
.filter(|m| m.embedding.is_some())
.cloned(),
);
}
if scope.includes_global()
&& let Ok(global) = self.load_global_graph()
{
entries.extend(
global
.active_memories()
.filter(|m| m.embedding.is_some())
.cloned(),
);
}
Ok(entries)
}
fn collect_memories_scoped(&self, scope: MemoryScope) -> Result<Vec<MemoryEntry>> {
let mut entries = Vec::new();
if scope.includes_project()
&& let Ok(project) = self.load_project_graph()
{
entries.extend(project.all_memories().cloned());
}
if scope.includes_global()
&& let Ok(global) = self.load_global_graph()
{
entries.extend(global.all_memories().cloned());
}
Ok(entries)
}
fn synthetic_skill_entries(&self) -> Vec<MemoryEntry> {
if !self.include_skills {
return Vec::new();
}
crate::skill::SkillRegistry::shared_snapshot()
.list()
.into_iter()
.map(|skill| skill.as_memory_entry())
.collect()
}
fn collect_retrieval_candidates_scoped(&self, scope: MemoryScope) -> Result<Vec<MemoryEntry>> {
let mut entries = self.collect_memories_scoped(scope)?;
if scope.includes_global() {
entries.extend(self.synthetic_skill_entries());
}
Ok(entries)
}
fn collect_retrieval_candidates_with_embeddings_scoped(
&self,
scope: MemoryScope,
) -> Result<Vec<MemoryEntry>> {
let mut entries = self.collect_memories_with_embeddings_scoped(scope)?;
if scope.includes_global() {
entries.extend(
self.synthetic_skill_entries()
.into_iter()
.filter_map(|mut entry| entry.ensure_embedding().then_some(entry)),
);
}
Ok(entries)
}
fn find_retrieval_candidates_similar_scoped(
&self,
text: &str,
threshold: f32,
limit: usize,
scope: MemoryScope,
) -> Result<Vec<(MemoryEntry, f32)>> {
let query_embedding = match crate::embedding::embed(text) {
Ok(emb) => emb,
Err(e) => {
crate::logging::info(&format!(
"Embedding failed for retrieval candidates, falling back to keyword search: {}",
e
));
return Ok(Vec::new());
}
};
let entries = self.collect_retrieval_candidates_with_embeddings_scoped(scope)?;
Self::score_and_filter(entries, &query_embedding, text, threshold, limit)
}
fn score_and_filter(
entries: Vec<MemoryEntry>,
query_embedding: &[f32],
query_text: &str,
threshold: f32,
limit: usize,
) -> Result<Vec<(MemoryEntry, f32)>> {
if entries.is_empty() {
return Ok(Vec::new());
}
let mut filtered_entries = Vec::with_capacity(entries.len());
let mut skipped_missing_embeddings = 0usize;
for entry in entries {
if entry.embedding.is_some() {
filtered_entries.push(entry);
} else {
skipped_missing_embeddings += 1;
}
}
if skipped_missing_embeddings > 0 {
crate::logging::warn(&format!(
"Skipped {} retrieval candidate(s) without embeddings during similarity scoring",
skipped_missing_embeddings
));
}
if filtered_entries.is_empty() {
return Ok(Vec::new());
}
let emb_refs: Vec<&[f32]> = filtered_entries
.iter()
.filter_map(|entry| entry.embedding.as_deref())
.collect();
let scores = crate::embedding::batch_cosine_similarity(query_embedding, &emb_refs);
let skill_query_terms = collect_skill_query_terms(query_text);
let scored = top_k_by_score(
filtered_entries
.into_iter()
.zip(scores)
.map(|(entry, sim)| {
let adjusted = sim + skill_retrieval_bonus(&entry, &skill_query_terms);
(entry, adjusted)
})
.filter(|(_, sim)| *sim >= threshold),
limit,
);
let scored = Self::apply_gap_filter(scored);
Ok(scored)
}
/// Drop trailing low-relevance results by detecting natural gaps in the
/// score distribution. If the top hit is 0.85 and the next cluster is
/// 0.40-0.42, the 0.15+ gap tells us those lower results are noise.
///
/// Algorithm: walk the sorted scores and cut when the drop from one score
/// to the next exceeds `GAP_FACTOR` of the range (top - floor_threshold).
fn apply_gap_filter(scored: Vec<(MemoryEntry, f32)>) -> Vec<(MemoryEntry, f32)> {
if scored.len() <= 1 {
return scored;
}
const GAP_FACTOR: f32 = 0.25;
const MIN_KEEP: usize = 1;
let top_score = scored[0].1;
let range = (top_score - EMBEDDING_SIMILARITY_THRESHOLD).max(0.01);
let max_gap = range * GAP_FACTOR;
let mut keep = scored.len();
for i in 1..scored.len() {
let drop = scored[i - 1].1 - scored[i].1;
if drop > max_gap && i >= MIN_KEEP {
keep = i;
break;
}
}
scored.into_iter().take(keep).collect()
}
/// Ensure all memories have embeddings (backfill for existing memories)
pub fn backfill_embeddings(&self) -> Result<(usize, usize)> {
let mut generated = 0;
let mut failed = 0;
// Process project memories
if let Ok(mut graph) = self.load_project_graph() {
let mut changed = false;
for entry in graph.memories.values_mut() {
if entry.embedding.is_none() {
if entry.ensure_embedding() {
generated += 1;
changed = true;
} else {
failed += 1;
}
}
}
if changed {
self.save_project_graph(&graph)?;
}
}
// Process global memories
if let Ok(mut graph) = self.load_global_graph() {
let mut changed = false;
for entry in graph.memories.values_mut() {
if entry.embedding.is_none() {
if entry.ensure_embedding() {
generated += 1;
changed = true;
} else {
failed += 1;
}
}
}
if changed {
self.save_global_graph(&graph)?;
}
}
Ok((generated, failed))
}
fn touch_entries(&self, ids: &[String]) -> Result<()> {
if ids.is_empty() {
return Ok(());
}
let id_set: std::collections::HashSet<&str> = ids.iter().map(|id| id.as_str()).collect();
let mut project = self.load_project_graph()?;
let mut project_changed = false;
for entry in project.memories.values_mut() {
if id_set.contains(entry.id.as_str()) {
entry.touch();
project_changed = true;
}
}
if project_changed {
self.save_project_graph(&project)?;
}
let mut global = self.load_global_graph()?;
let mut global_changed = false;
for entry in global.memories.values_mut() {
if id_set.contains(entry.id.as_str()) {
entry.touch();
global_changed = true;
}
}
if global_changed {
self.save_global_graph(&global)?;
}
Ok(())
}
pub fn get_prompt_memories(&self, limit: usize) -> Option<String> {
self.get_prompt_memories_scoped(limit, MemoryScope::All)
}
pub fn get_prompt_memories_scoped(&self, limit: usize, scope: MemoryScope) -> Option<String> {
let all_entries: Vec<_> = top_k_by_ord(
self.collect_memories_scoped(scope)
.ok()?
.into_iter()
.map(|entry| {
let updated_at = entry.updated_at.timestamp_millis();
(entry, updated_at)
}),
limit,
)
.into_iter()
.map(|(entry, _)| entry)
.collect();
if all_entries.is_empty() {
return None;
}
format_entries_for_prompt(&all_entries, limit)
}
pub async fn relevant_prompt_for_messages(
&self,
messages: &[crate::message::Message],
) -> Result<Option<String>> {
let context = format_context_for_relevance(messages);
if context.is_empty() {
return Ok(None);
}
self.relevant_prompt_for_context(
&context,
MEMORY_RELEVANCE_MAX_CANDIDATES,
MEMORY_RELEVANCE_MAX_RESULTS,
)
.await
}
pub async fn relevant_prompt_for_context(
&self,
context: &str,
max_candidates: usize,
limit: usize,
) -> Result<Option<String>> {
let relevant = self
.get_relevant_for_context(context, max_candidates)
.await?;
if relevant.is_empty() {
return Ok(None);
}
Ok(format_relevant_prompt(&relevant, limit))
}
pub fn search(&self, query: &str) -> Result<Vec<MemoryEntry>> {
self.search_scoped(query, MemoryScope::All)
}
pub fn search_scoped(&self, query: &str, scope: MemoryScope) -> Result<Vec<MemoryEntry>> {
let query_lower = normalize_search_text(query);
if query_lower.is_empty() {
return Ok(Vec::new());
}
let mut results = Vec::new();
for memory in self.collect_memories_scoped(scope)? {
if memory_matches_search(&memory, &query_lower) {
results.push(memory);
}
}
Ok(results)
}
pub fn list_all(&self) -> Result<Vec<MemoryEntry>> {
self.list_all_scoped(MemoryScope::All)
}
pub fn list_all_scoped(&self, scope: MemoryScope) -> Result<Vec<MemoryEntry>> {
let mut all = self.collect_memories_scoped(scope)?;
all.sort_by(|a, b| b.updated_at.cmp(&a.updated_at));
Ok(all)
}
pub fn forget(&self, id: &str) -> Result<bool> {
// Try graph-based removal first (new format)
let mut project_graph = self.load_project_graph()?;
if project_graph.remove_memory(id).is_some() {
self.save_project_graph(&project_graph)?;
return Ok(true);
}
let mut global_graph = self.load_global_graph()?;
if global_graph.remove_memory(id).is_some() {
self.save_global_graph(&global_graph)?;
return Ok(true);
}
Ok(false)
}
// === Sidecar Integration ===
/// Extract memories from a session transcript using the Haiku sidecar
pub async fn extract_from_transcript(
&self,
transcript: &str,
session_id: &str,
) -> Result<Vec<String>> {
if !memory_sidecar_enabled() {
crate::logging::info("Memory transcript extraction skipped: memory sidecar disabled");
return Ok(Vec::new());
}
let sidecar = Sidecar::new();
let extracted = sidecar.extract_memories(transcript).await?;
let mut ids = Vec::new();
for memory in extracted {
let category: MemoryCategory = memory.category.parse().unwrap_or(MemoryCategory::Fact);
let trust = match memory.trust.as_str() {
"high" => TrustLevel::High,
"medium" => TrustLevel::Medium,
_ => TrustLevel::Low,
};
let entry = MemoryEntry::new(category, memory.content)
.with_source(session_id)
.with_trust(trust);
// Store in project scope by default
let id = self.remember_project(entry)?;
ids.push(id);
}
Ok(ids)
}
/// Check if stored memories are relevant to the current context
/// Returns memories that the sidecar deems relevant
pub async fn get_relevant_for_context(
&self,
context: &str,
max_candidates: usize,
) -> Result<Vec<MemoryEntry>> {
// Get top candidate memories by score
let candidates: Vec<_> = top_k_by_score(
self.collect_retrieval_candidates_scoped(MemoryScope::All)?
.into_iter()
.filter(|entry| entry.active)
.map(|entry| {
let score = memory_score(&entry) as f32;
(entry, score)
}),
max_candidates,
)
.into_iter()
.map(|(entry, _)| entry)
.collect();
if candidates.is_empty() {
return Ok(Vec::new());
}
// Update activity state - checking memories
set_state(MemoryState::SidecarChecking {
count: candidates.len(),
});
add_event(MemoryEventKind::SidecarStarted);
let sidecar = Sidecar::new();