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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
use std::marker::PhantomData;
use std::path::Path;
use std::sync::Arc;
use sha2::Digest;
use crate::context::{ContextAssembly, ContextBudget, ContextItem, PRIORITY_LEARNING};
use crate::embeddings::EmbeddingBackend;
use crate::error::{FemindError, Result};
use crate::memory::store::StoreResult;
use crate::memory::MemoryStore;
use crate::scoring::{CompositeScorer, ImportanceScorer, MemoryTypeScorer, RecencyScorer};
use crate::search::builder::SearchBuilder;
use crate::storage::migrations;
use crate::storage::Database;
use crate::traits::{MemoryRecord, ScoringStrategy};
/// Result of a store_with_extraction() operation.
#[derive(Debug, Clone)]
pub struct StoreExtractionResult {
/// Number of facts the LLM extracted from the raw text.
pub facts_extracted: usize,
/// Number of new memories stored (after deduplication).
pub memories_stored: usize,
/// Number of duplicate facts skipped.
pub duplicates_skipped: usize,
/// Number of graph edges created (SupersededBy + RelatedTo).
pub graph_edges_created: usize,
/// Number of superseded (outdated) facts detected.
pub superseded_count: usize,
/// Approximate LLM tokens used for extraction.
pub tokens_used: usize,
}
/// Runtime feature configuration for femind.
///
/// Two-level config:
/// - **EngineConfig** controls WHAT features are active (store-time toggles).
/// These are system-wide settings that affect all operations.
/// - **AssemblyConfig** (nested inside) controls HOW search behaves (query-time tuning).
/// These can vary per query or per dataset.
///
/// All toggles are independent — any combination is valid.
///
/// ## Store-time features (EngineConfig)
/// - `embedding_enabled` — whether store()/store_batch()/store_with_extraction() compute vectors
/// - `graph_enabled` — whether store_with_extraction() creates graph edges, and search uses them
/// - `dedup_enabled` — whether store operations check content hash for duplicates
/// - `vector_search_mode` — "exact" (brute-force), "ann" (approximate), or "off" (FTS5 only)
///
/// ## Query-time tuning (AssemblyConfig)
/// - `max_per_session` — diversification limit (1 for multi-session, 0 for single-document)
/// - `recency_boost` — score boost for newer content (0.0-1.0)
/// - `search_limit` — max results from multi-query search
/// - `graph_depth` — how many hops to traverse in graph filtering
#[derive(Debug, Clone)]
pub struct EngineConfig {
/// Enable vector embedding at store time.
/// When false, memories are stored with FTS5 indexing only (no vectors).
pub embedding_enabled: bool,
/// Enable graph edge creation (at store time) and graph filtering (at search time).
/// Master switch — overrides AssemblyConfig.graph_depth when false.
pub graph_enabled: bool,
/// Enable content hash deduplication on store.
/// When false, duplicate content is allowed (useful for testing).
pub dedup_enabled: bool,
/// Query-time search configuration (diversification, recency, graph depth).
pub assembly: crate::context::AssemblyConfig,
/// Vector search mode: exact (brute-force), ann (approximate), or off.
pub vector_search_mode: VectorSearchMode,
}
/// Runtime vector retrieval mode.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, serde::Serialize, serde::Deserialize)]
pub enum VectorSearchMode {
/// FTS5 only. Vector search is disabled.
Off,
/// Brute-force cosine similarity over stored vectors.
#[default]
Exact,
/// ANN cosine similarity via the in-memory HNSW index.
Ann,
}
impl VectorSearchMode {
pub fn as_str(self) -> &'static str {
match self {
Self::Off => "off",
Self::Exact => "exact",
Self::Ann => "ann",
}
}
}
impl std::fmt::Display for VectorSearchMode {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_str())
}
}
impl Default for EngineConfig {
fn default() -> Self {
Self {
embedding_enabled: true,
graph_enabled: true,
dedup_enabled: true,
assembly: crate::context::AssemblyConfig::default(),
vector_search_mode: VectorSearchMode::Exact,
}
}
}
/// The primary interface to femind.
///
/// Generic over the consumer's memory type `T: MemoryRecord`.
/// All core operations are synchronous (SQLite queries).
///
/// # Example
///
/// ```rust,ignore
/// let engine = MemoryEngine::<MyMemory>::builder()
/// .database("memory.db")
/// .build()?;
///
/// engine.store(&my_record)?;
/// let results = engine.search("query").limit(5).execute()?;
/// ```
pub struct MemoryEngine<T: MemoryRecord> {
db: Database,
global_db: Option<Database>,
store: MemoryStore<T>,
scoring: Arc<dyn ScoringStrategy>,
embedding: Option<Arc<dyn EmbeddingBackend>>,
#[cfg(feature = "ann")]
ann_index: Arc<crate::search::AnnIndex>,
/// Runtime feature configuration.
pub config: EngineConfig,
}
impl<T: MemoryRecord> MemoryEngine<T> {
/// Create a new builder for configuring the engine.
pub fn builder() -> MemoryEngineBuilder<T> {
MemoryEngineBuilder::new()
}
/// Store a new memory. Returns info about what action was taken (added or duplicate).
///
/// When the `consolidation` feature is enabled and a consolidation strategy
/// is configured, the strategy is consulted before storing.
pub fn store(&self, record: &T) -> Result<StoreResult> {
let result = self.store.store(&self.db, record)?;
// Compute and store embedding for new records (if enabled)
if let StoreResult::Added(id) = &result {
if self.config.embedding_enabled {
if let Some(ref backend) = self.embedding {
if backend.is_available() {
let text = record.searchable_text();
// Skip embedding for empty/whitespace-only text
if text.trim().is_empty() {
return Ok(result);
}
// Truncate to ~8192 tokens (~32K chars) for model context window
let text = truncate_for_embedding(&text);
let hash = format!("{:x}", sha2::Sha256::digest(text.as_bytes()));
// Skip embedding if vector already exists for this content
let already_exists =
crate::search::vector::VectorSearch::vector_exists(&self.db, &hash)
.unwrap_or(false);
if !already_exists {
let embed_start = std::time::Instant::now();
match backend.embed(text) {
Ok(vec) if vec.is_empty() => {
tracing::warn!("Empty embedding returned for memory {id}");
self.set_embedding_status(*id, "failed");
}
Ok(vec) => {
let embed_ms = embed_start.elapsed().as_millis();
tracing::debug!(memory_id = id, embed_ms, "embedded memory");
match crate::search::vector::VectorSearch::store_vector(
&self.db,
*id,
&vec,
backend.model_name(),
&hash,
) {
Ok(()) => {
tracing::debug!(memory_id = id, "stored vector");
self.set_embedding_status(*id, "success");
self.invalidate_ann_index();
}
Err(e) => {
tracing::warn!(
"Failed to store embedding for memory {id}: {e}"
);
self.set_embedding_status(*id, "failed");
}
}
}
Err(e) => {
tracing::warn!(
"Failed to compute embedding for memory {id}: {e}"
);
self.set_embedding_status(*id, "failed");
}
}
} else {
self.set_embedding_status(*id, "success");
}
}
}
} // config.embedding_enabled
}
Ok(result)
}
/// Store a batch of records with optimized embedding computation.
///
/// 1. Stores all records via individual SQL inserts
/// 2. Collects texts from newly added records
/// 3. Calls `embed_batch()` once for all texts
/// 4. Stores all vectors
///
/// This is significantly faster than calling `store()` in a loop because
/// embedding inference is batched (amortizes model overhead).
pub fn store_batch(&self, records: &[T]) -> Result<Vec<StoreResult>> {
// Phase 1: Store all records, collect texts needing embeddings
let mut results = Vec::with_capacity(records.len());
let mut to_embed: Vec<(i64, String, String)> = Vec::new(); // (id, text, hash)
for record in records {
let result = self.store.store(&self.db, record)?;
if let StoreResult::Added(id) = &result {
if self.config.embedding_enabled
&& self.embedding.as_ref().is_some_and(|b| b.is_available())
{
let text = record.searchable_text();
if !text.trim().is_empty() {
let hash = format!("{:x}", sha2::Sha256::digest(text.as_bytes()));
let already_exists =
crate::search::vector::VectorSearch::vector_exists(&self.db, &hash)
.unwrap_or(false);
if !already_exists {
to_embed.push((*id, text, hash));
}
}
}
}
results.push(result);
}
// Phase 2: Batch embed all texts at once
if let Some(ref backend) = self.embedding {
if !to_embed.is_empty() && backend.is_available() {
let texts: Vec<&str> = to_embed.iter().map(|(_, t, _)| t.as_str()).collect();
let batch_start = std::time::Instant::now();
let batch_count = texts.len();
let mut stored_any_vectors = false;
match backend.embed_batch(&texts) {
Ok(embeddings) => {
let batch_ms = batch_start.elapsed().as_millis();
tracing::debug!(batch_count, batch_ms, "batch embedding complete");
// Phase 3: Store all vectors and update status
for ((id, _, hash), embedding) in to_embed.iter().zip(embeddings.iter()) {
match crate::search::vector::VectorSearch::store_vector(
&self.db,
*id,
embedding,
backend.model_name(),
hash,
) {
Ok(()) => {
self.set_embedding_status(*id, "success");
stored_any_vectors = true;
}
Err(e) => {
tracing::warn!(
"Failed to store embedding for memory {id}: {e}"
);
self.set_embedding_status(*id, "failed");
}
}
}
if stored_any_vectors {
self.invalidate_ann_index();
}
}
Err(e) => {
tracing::warn!(
"Batch embedding failed for {} records: {e}",
to_embed.len()
);
// Mark all as failed
for (id, _, _) in &to_embed {
self.set_embedding_status(*id, "failed");
}
}
}
}
}
Ok(results)
}
/// Update the embedding_status column for a memory.
fn set_embedding_status(&self, id: i64, status: &str) {
let _ = self.db.with_writer(|conn| {
conn.execute(
"UPDATE memories SET embedding_status = ?1 WHERE id = ?2",
rusqlite::params![status, id],
)?;
Ok(())
});
}
/// Retrieve a memory by ID. Returns `None` if not found.
pub fn get(&self, id: i64) -> Result<Option<T>> {
self.store.get(&self.db, id)
}
/// Update an existing memory by ID.
pub fn update(&self, id: i64, record: &T) -> Result<()> {
self.store.update(&self.db, id, record)
}
/// Delete a memory by ID. Returns `true` if a record was deleted.
pub fn delete(&self, id: i64) -> Result<bool> {
self.store.delete(&self.db, id)
}
/// Begin a search with the fluent builder API.
///
/// Post-search scoring is automatically applied using the engine's
/// configured scoring strategy. If an embedding backend is configured,
/// `SearchMode::Auto` will use hybrid FTS5 + vector search.
pub fn search(&self, query: &str) -> SearchBuilder<'_, T> {
let mut builder = SearchBuilder::new(&self.db, query)
.with_scoring(Arc::clone(&self.scoring))
.with_vector_search_mode(self.config.vector_search_mode);
if self.config.vector_search_mode != VectorSearchMode::Off {
if let Some(ref embedding) = self.embedding {
builder = builder.with_embedding(Arc::clone(embedding));
}
}
#[cfg(feature = "ann")]
if self.config.vector_search_mode == VectorSearchMode::Ann {
builder = builder.with_ann_index(Arc::clone(&self.ann_index));
}
builder
}
/// Access the embedding backend (if configured).
pub fn embedding_backend(&self) -> Option<&dyn EmbeddingBackend> {
self.embedding.as_deref()
}
/// Count total memories in the database.
pub fn count(&self) -> Result<u64> {
self.store.count(&self.db)
}
/// Store raw text with LLM extraction.
///
/// 1. Calls the LLM to extract individual facts from raw text
/// 2. Stores each fact as its own memory with embedding
/// 3. Detects contradictions and creates SupersededBy graph edges
/// 4. Deduplicates against existing memories via content hash
///
/// Returns extraction statistics including storage and graph metrics.
pub fn store_with_extraction(
&self,
raw_text: &str,
llm: &dyn crate::traits::LlmCallback,
) -> Result<StoreExtractionResult> {
use crate::ingest::llm_extract;
use crate::memory::{GraphMemory, RelationType};
// Step 1: Extract facts via LLM
// Split large text into manageable chunks for the LLM context window
const MAX_EXTRACT_CHARS: usize = 6000;
let extraction = if raw_text.len() > MAX_EXTRACT_CHARS {
let mut all_facts = Vec::new();
let mut total_tokens = 0;
let mut remaining = raw_text;
while !remaining.is_empty() {
let split_at = if remaining.len() <= MAX_EXTRACT_CHARS {
remaining.len()
} else {
remaining[..MAX_EXTRACT_CHARS]
.rfind('\n')
.map(|p| p + 1)
.unwrap_or(MAX_EXTRACT_CHARS)
};
let chunk = &remaining[..split_at];
remaining = &remaining[split_at..];
match llm_extract::extract_facts(chunk, llm) {
Ok(result) => {
total_tokens += result.tokens_used;
all_facts.extend(result.facts);
}
Err(e) => {
tracing::warn!("Extraction chunk failed: {e}");
}
}
}
llm_extract::ExtractionResult {
facts: all_facts,
tokens_used: total_tokens,
}
} else {
llm_extract::extract_facts(raw_text, llm)?
};
let facts_extracted = extraction.facts.len();
let tokens_used = extraction.tokens_used;
if extraction.facts.is_empty() {
return Ok(StoreExtractionResult {
facts_extracted: 0,
memories_stored: 0,
duplicates_skipped: 0,
graph_edges_created: 0,
superseded_count: 0,
tokens_used,
});
}
// Step 2: Store each fact as individual memory
let mut stored_ids: Vec<(i64, &llm_extract::ExtractedFact)> = Vec::new();
let mut duplicates_skipped = 0usize;
for fact in &extraction.facts {
let hash = format!("{:x}", sha2::Sha256::digest(fact.text.as_bytes()));
// Dedup check
let existing_id: Option<i64> = self.db.with_reader(|conn| {
let result = conn.query_row(
"SELECT id FROM memories WHERE content_hash = ?1",
[&hash],
|row| row.get::<_, i64>(0),
);
match result {
Ok(id) => Ok(Some(id)),
Err(rusqlite::Error::QueryReturnedNoRows) => Ok(None),
Err(e) => Err(e.into()),
}
})?;
if self.config.dedup_enabled && existing_id.is_some() {
duplicates_skipped += 1;
continue;
}
// Insert the memory
let importance = fact.importance as i32;
let category = Some(fact.category.as_str());
let metadata_json = if !fact.entities.is_empty() || !fact.relationships.is_empty() {
let meta = serde_json::json!({
"entities": fact.entities,
"relationships": fact.relationships,
});
Some(serde_json::to_string(&meta).unwrap_or_default())
} else {
None
};
let id = self.db.with_writer(|conn| {
conn.execute(
"INSERT INTO memories (
searchable_text, memory_type, importance, category,
metadata_json, content_hash, created_at, record_json
) VALUES (?1, ?2, ?3, ?4, ?5, ?6, datetime('now'), ?7)",
rusqlite::params![
fact.text,
"semantic",
importance,
category,
metadata_json,
hash,
serde_json::json!({"text": fact.text}).to_string(),
],
)?;
Ok(conn.last_insert_rowid())
})?;
// Compute and store embedding (A3: gated by config)
if self.config.embedding_enabled {
if let Some(ref backend) = self.embedding {
if backend.is_available() && !fact.text.trim().is_empty() {
let text = truncate_for_embedding(&fact.text);
match backend.embed(text) {
Ok(vec) if !vec.is_empty() => {
match crate::search::vector::VectorSearch::store_vector(
&self.db,
id,
&vec,
backend.model_name(),
&hash,
) {
Ok(()) => {
self.set_embedding_status(id, "success");
self.invalidate_ann_index();
}
Err(_) => self.set_embedding_status(id, "failed"),
}
}
Ok(_) => self.set_embedding_status(id, "failed"),
Err(_) => self.set_embedding_status(id, "failed"),
}
}
}
} // config.embedding_enabled
stored_ids.push((id, fact));
}
// Step 3: Create graph edges for relationships (A4: gated by config)
let mut graph_edges_created = 0usize;
let mut superseded_count = 0usize;
if self.config.graph_enabled {
for (id, fact) in &stored_ids {
for (subject, relation, _object) in &fact.relationships {
// Search existing memories for same subject + relation with different value
let existing = self.db.with_reader(|conn| {
let mut stmt = conn.prepare(
"SELECT id, searchable_text FROM memories
WHERE id != ?1
AND searchable_text LIKE ?2
ORDER BY id ASC",
)?;
let pattern = format!("%{}%", subject);
let results: Vec<(i64, String)> = stmt
.query_map(rusqlite::params![id, pattern], |row| {
Ok((row.get(0)?, row.get(1)?))
})?
.filter_map(|r| r.ok())
.collect();
Ok::<_, crate::error::FemindError>(results)
})?;
for (existing_id, existing_text) in &existing {
if existing_text
.to_lowercase()
.contains(&subject.to_lowercase())
&& existing_text
.to_lowercase()
.contains(&relation.replace('_', " ").to_lowercase())
{
// Same subject + relation → SupersededBy (older is superseded)
if GraphMemory::relate(
&self.db,
*existing_id,
*id,
&RelationType::SupersededBy,
)
.is_ok()
{
graph_edges_created += 1;
superseded_count += 1;
}
}
}
}
}
} // config.graph_enabled
Ok(StoreExtractionResult {
facts_extracted,
memories_stored: stored_ids.len(),
duplicates_skipped,
graph_edges_created,
superseded_count,
tokens_used,
})
}
/// Returns (memories_with_embeddings, total_memories) for diagnostic purposes.
///
/// Counts memories where `embedding_status = 'success'` vs total count.
pub fn embedding_coverage(&self) -> Result<(u64, u64)> {
let total = self.store.count(&self.db)?;
let with_embeddings: i64 = self.db.with_reader(|conn| {
conn.query_row(
"SELECT COUNT(*) FROM memories WHERE embedding_status = 'success'",
[],
|row| row.get(0),
)
.map_err(Into::into)
})?;
Ok((with_embeddings as u64, total))
}
/// Multi-query search: run original + key-phrase variant, merge, diversify.
fn multi_query_search(
&self,
query: &str,
config: &crate::context::AssemblyConfig,
) -> Result<Vec<crate::search::builder::SearchResult>> {
let limit = config.search_limit;
use crate::search::fts5::strip_stop_words;
use std::collections::HashMap;
// Query variant 1: original
let results1 = self.search(query).limit(limit).execute()?;
// Query variant 2: key-phrase only (stop words removed)
let key_phrases = strip_stop_words(query);
let results2 = if key_phrases != query && !key_phrases.is_empty() {
self.search(&key_phrases).limit(limit).execute()?
} else {
Vec::new()
};
// Merge: keep highest score per memory_id
let mut best: HashMap<i64, crate::search::builder::SearchResult> = HashMap::new();
for r in results1.into_iter().chain(results2.into_iter()) {
best.entry(r.memory_id)
.and_modify(|existing| {
if r.score > existing.score {
*existing = r.clone();
}
})
.or_insert(r);
}
let mut merged: Vec<_> = best.into_values().collect();
// Recency weighting: boost later chunks (higher turn_index = more recent)
if config.recency_boost > 0.0 {
// Find max turn_index across all results for normalization
let max_index = merged
.iter()
.filter_map(|r| {
self.db
.with_reader(|conn| {
conn.query_row(
"SELECT metadata_json FROM memories WHERE id = ?1",
[r.memory_id],
|row| row.get::<_, Option<String>>(0),
)
.map_err(crate::error::FemindError::Database)
})
.ok()
.flatten()
.and_then(|json| {
serde_json::from_str::<HashMap<String, String>>(&json).ok()
})
.and_then(|meta| meta.get("turn_index").and_then(|v| v.parse::<f32>().ok()))
})
.fold(1.0_f32, f32::max);
for r in &mut merged {
let turn_index = self
.db
.with_reader(|conn| {
conn.query_row(
"SELECT metadata_json FROM memories WHERE id = ?1",
[r.memory_id],
|row| row.get::<_, Option<String>>(0),
)
.map_err(crate::error::FemindError::Database)
})
.ok()
.flatten()
.and_then(|json| serde_json::from_str::<HashMap<String, String>>(&json).ok())
.and_then(|meta| meta.get("turn_index").and_then(|v| v.parse::<f32>().ok()))
.unwrap_or(0.0);
// position_ratio: 0.0 (oldest) to 1.0 (newest)
let position_ratio = turn_index / max_index;
r.score *= 1.0 + config.recency_boost * position_ratio;
}
}
merged.sort_by(|a, b| {
b.score
.partial_cmp(&a.score)
.unwrap_or(std::cmp::Ordering::Equal)
});
// Graph filtering: demote results that have been superseded by newer facts.
// Only if graph is enabled (A6: EngineConfig master switch + AssemblyConfig depth)
if self.config.graph_enabled && config.graph_depth > 0 {
let mut superseded_ids: std::collections::HashSet<i64> =
std::collections::HashSet::new();
// Check each result: does anything supersede it?
for r in &merged {
// Look for incoming SupersededBy edges (this memory IS the old one)
let is_superseded = self
.db
.with_reader(|conn| {
let count: i64 = conn
.query_row(
"SELECT COUNT(*) FROM memory_relations
WHERE source_id = ?1 AND relation = 'superseded_by'",
[r.memory_id],
|row| row.get(0),
)
.unwrap_or(0);
Ok::<bool, crate::error::FemindError>(count > 0)
})
.unwrap_or(false);
if is_superseded {
superseded_ids.insert(r.memory_id);
}
}
if !superseded_ids.is_empty() {
tracing::debug!(
"Graph filtering: {} results demoted as superseded",
superseded_ids.len()
);
for r in &mut merged {
if superseded_ids.contains(&r.memory_id) {
r.score *= 0.1; // Heavily demote outdated facts
}
}
merged.sort_by(|a, b| {
b.score
.partial_cmp(&a.score)
.unwrap_or(std::cmp::Ordering::Equal)
});
}
}
// Diversification: limit chunks per session (0 = unlimited)
let max_per = config.max_per_session;
let diversified: Vec<_> = if max_per == 0 {
merged // No diversification
} else {
let mut session_counts: HashMap<String, usize> = HashMap::new();
merged
.into_iter()
.filter(|r| {
let session_key = self
.db
.with_reader(|conn| {
conn.query_row(
"SELECT metadata_json FROM memories WHERE id = ?1",
[r.memory_id],
|row| row.get::<_, Option<String>>(0),
)
.map_err(crate::error::FemindError::Database)
})
.ok()
.flatten()
.and_then(|json| {
serde_json::from_str::<HashMap<String, String>>(&json).ok()
})
.and_then(|meta| meta.get("session_date").cloned())
.unwrap_or_else(|| format!("unknown_{}", r.memory_id));
let count = session_counts.entry(session_key).or_insert(0);
*count += 1;
*count <= max_per
})
.collect()
};
Ok(diversified)
}
/// Assemble context for an LLM prompt within a token budget.
///
/// Uses default AssemblyConfig (max 1/session, no recency boost).
pub fn assemble_context(&self, query: &str, budget: &ContextBudget) -> Result<ContextAssembly> {
self.assemble_context_with_config(query, budget, &crate::context::AssemblyConfig::default())
}
/// Assemble context with custom assembly configuration.
///
/// Allows tuning diversification, recency weighting, and search limits
/// per dataset or question type.
pub fn assemble_context_with_config(
&self,
query: &str,
budget: &ContextBudget,
config: &crate::context::AssemblyConfig,
) -> Result<ContextAssembly> {
// Multi-query retrieval: run original + key-phrase variant, merge results
let results = self.multi_query_search(query, config)?;
// Convert search results to context items
let candidates: Vec<ContextItem> = results
.iter()
.filter_map(|sr| {
// Load the memory to get its content
self.db
.with_reader(|conn| {
let row = conn.query_row(
"SELECT searchable_text, memory_type, category, metadata_json FROM memories WHERE id = ?1",
[sr.memory_id],
|row| {
Ok((
row.get::<_, String>(0)?,
row.get::<_, String>(1)?,
row.get::<_, Option<String>>(2)?,
row.get::<_, Option<String>>(3)?,
))
},
);
match row {
Ok((text, type_str, category, metadata_json)) => {
let memory_type = crate::traits::MemoryType::from_str(&type_str)
.unwrap_or(crate::traits::MemoryType::Episodic);
// Prepend session date if available in metadata
let content = prepend_date_from_metadata(&text, metadata_json.as_deref());
Ok(Some(ContextItem {
memory_id: sr.memory_id,
content: content.clone(),
priority: PRIORITY_LEARNING,
estimated_tokens: budget.estimate_tokens(&content),
relevance_score: sr.score,
memory_type,
category,
}))
}
Err(rusqlite::Error::QueryReturnedNoRows) => Ok(None),
Err(e) => Err(e.into()),
}
})
.ok()
.flatten()
})
.collect();
Ok(ContextAssembly::assemble(candidates, budget))
}
/// Direct access to the project database (for advanced consumers).
pub fn database(&self) -> &Database {
&self.db
}
/// Direct access to the global database (if configured).
pub fn global_database(&self) -> Option<&Database> {
self.global_db.as_ref()
}
#[cfg(feature = "ann")]
fn invalidate_ann_index(&self) {
self.ann_index.invalidate();
}
#[cfg(not(feature = "ann"))]
fn invalidate_ann_index(&self) {}
}
impl<T: MemoryRecord> std::fmt::Debug for MemoryEngine<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("MemoryEngine")
.field("db", &self.db)
.finish()
}
}
/// Builder for constructing a `MemoryEngine`.
pub struct MemoryEngineBuilder<T: MemoryRecord> {
database_path: Option<String>,
global_database_path: Option<String>,
scoring: Option<Arc<dyn ScoringStrategy>>,
embedding: Option<Arc<dyn EmbeddingBackend>>,
config: EngineConfig,
_phantom: PhantomData<T>,
}
impl<T: MemoryRecord> MemoryEngineBuilder<T> {
fn new() -> Self {
Self {
database_path: None,
global_database_path: None,
scoring: None,
embedding: None,
config: EngineConfig::default(),
_phantom: PhantomData,
}
}
/// Set the path to the SQLite database file.
///
/// If not set, uses an in-memory database (useful for testing).
pub fn database(mut self, path: impl Into<String>) -> Self {
self.database_path = Some(path.into());
self
}
/// Set the global database path for two-tier memory.
///
/// When set, the engine maintains both a project database (set via `.database()`)
/// and a global database for cross-project memories.
pub fn global_database(mut self, path: impl Into<String>) -> Self {
self.global_database_path = Some(path.into());
self
}
/// Set the scoring strategy for post-search ranking.
///
/// If not set, uses the default composite scorer (recency, importance,
/// and cognitive memory type).
pub fn scoring(mut self, strategy: impl ScoringStrategy + 'static) -> Self {
self.scoring = Some(Arc::new(strategy));
self
}
/// Set the embedding backend for vector search.
///
/// When set, `SearchMode::Auto` uses hybrid FTS5 + vector search.
/// Without this, all search modes fall back to FTS5 keyword search.
pub fn embedding_backend(mut self, backend: impl EmbeddingBackend + 'static) -> Self {
self.embedding = Some(Arc::new(backend));
self
}
/// Set the embedding backend from an existing `Arc`.
///
/// Use this to share a single backend instance across multiple engines
/// (e.g., to avoid re-loading model weights on reset).
pub fn embedding_backend_arc(mut self, backend: Arc<dyn EmbeddingBackend>) -> Self {
self.embedding = Some(backend);
self
}
/// Override the runtime engine configuration.
pub fn config(mut self, config: EngineConfig) -> Self {
self.config = config;
self
}
/// Build the engine, creating or opening the database.
///
/// Runs schema migrations to ensure the database is at the current version.
pub fn build(self) -> Result<MemoryEngine<T>> {
let db = match &self.database_path {
Some(path) => {
// Ensure parent directory exists
if let Some(parent) = Path::new(path).parent() {
if !parent.as_os_str().is_empty() {
std::fs::create_dir_all(parent).map_err(|e| {
FemindError::Migration(format!(
"failed to create database directory {}: {e}",
parent.display()
))
})?;
}
}
Database::open(path)?
}
None => Database::open_in_memory()?,
};
// Run migrations
db.with_writer(|conn| {
migrations::migrate(conn)?;
Ok(())
})?;
// Open global database if configured
let global_db = match &self.global_database_path {
Some(path) => {
if let Some(parent) = Path::new(path).parent() {
if !parent.as_os_str().is_empty() {
std::fs::create_dir_all(parent).map_err(|e| {
FemindError::Migration(format!(
"failed to create global database directory {}: {e}",
parent.display()
))
})?;
}
}
let gdb = Database::open(path)?;
gdb.with_writer(|conn| {
migrations::migrate(conn)?;
Ok(())
})?;
Some(gdb)
}
None => None,
};
let scoring = self
.scoring
.unwrap_or_else(|| Arc::new(default_composite_scorer()));
Ok(MemoryEngine {
db,
global_db,
store: MemoryStore::new(),
scoring,
embedding: self.embedding,
#[cfg(feature = "ann")]
ann_index: Arc::new(crate::search::AnnIndex::default()),
config: self.config,
})
}
}
fn default_composite_scorer() -> CompositeScorer {
CompositeScorer::new(vec![
Box::new(RecencyScorer::default_half_life()),
Box::new(ImportanceScorer::default()),
Box::new(MemoryTypeScorer::default()),
])
}
/// Truncate text to fit within the embedding model's context window.
///
/// Granite-small-r2 supports 8192 tokens. At ~4 chars/token, we cap at 32K chars.
/// Truncates on a word boundary to avoid splitting tokens.
fn truncate_for_embedding(text: &str) -> &str {
const MAX_CHARS: usize = 32_000;
if text.len() <= MAX_CHARS {
return text;
}
// Find a word boundary near the limit
match text[..MAX_CHARS].rfind(' ') {
Some(pos) => &text[..pos],
None => &text[..MAX_CHARS],
}
}
/// Prepend session date from metadata JSON to content text for temporal grounding.
///
/// If metadata contains a "session_date" field, prepends "[Date: <date>] " to the text.
/// This makes dates visible in retrieved context, helping LLMs answer temporal questions.
fn prepend_date_from_metadata(text: &str, metadata_json: Option<&str>) -> String {
let Some(json_str) = metadata_json else {
return text.to_string();
};
// Parse metadata JSON to extract session_date
if let Ok(meta) = serde_json::from_str::<std::collections::HashMap<String, String>>(json_str) {
if let Some(date) = meta.get("session_date") {
if !date.is_empty() {
return format!("[Date: {date}] {text}");
}
}
}
text.to_string()
}
#[cfg(test)]
mod tests {
use super::*;
use crate::embeddings::EmbeddingBackend;
use crate::traits::MemoryType;
use chrono::Utc;
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
struct TestMem {
id: Option<i64>,
text: String,
created_at: chrono::DateTime<Utc>,
}
impl MemoryRecord for TestMem {
fn id(&self) -> Option<i64> {
self.id
}
fn searchable_text(&self) -> String {
self.text.clone()
}
fn memory_type(&self) -> MemoryType {
MemoryType::Semantic
}
fn created_at(&self) -> chrono::DateTime<Utc> {
self.created_at
}
}
fn mem(text: &str) -> TestMem {
TestMem {
id: None,
text: text.into(),
created_at: Utc::now(),
}
}
struct ModeTestEmbedder;
impl ModeTestEmbedder {
fn encode(text: &str) -> Vec<f32> {
let lower = text.to_lowercase();
let raw = if lower.contains("apple")
|| lower.contains("banana")
|| lower.contains("fruit")
{
vec![1.0, 0.0, 0.0]
} else if lower.contains("truck") || lower.contains("car") || lower.contains("vehicle")
{
vec![0.0, 1.0, 0.0]
} else {
vec![0.0, 0.0, 1.0]
};
crate::embeddings::pooling::normalize_l2(&raw)
}
}
impl EmbeddingBackend for ModeTestEmbedder {
fn embed(&self, text: &str) -> Result<Vec<f32>> {
Ok(Self::encode(text))
}
fn dimensions(&self) -> usize {
3
}
fn is_available(&self) -> bool {
true
}
fn model_name(&self) -> &str {
"mode-test"
}
}
#[test]
fn builder_in_memory() {
let engine = MemoryEngine::<TestMem>::builder().build();
assert!(engine.is_ok());
}
#[test]
fn builder_with_file() {
let dir = tempfile::tempdir().expect("tempdir");
let path = dir.path().join("test.db");
let engine = MemoryEngine::<TestMem>::builder()
.database(path.to_string_lossy().to_string())
.build();
assert!(engine.is_ok());
}
#[test]
fn builder_creates_parent_dirs() {
let dir = tempfile::tempdir().expect("tempdir");
let path = dir.path().join("deep/nested/dir/test.db");
let engine = MemoryEngine::<TestMem>::builder()
.database(path.to_string_lossy().to_string())
.build();
assert!(engine.is_ok());
}
#[test]
fn store_and_get_via_engine() {
let engine = MemoryEngine::<TestMem>::builder().build().expect("build");
let record = mem("hello from engine");
let result = engine.store(&record).expect("store");
let StoreResult::Added(id) = result else {
panic!("expected Added")
};
let retrieved = engine.get(id).expect("get");
assert!(retrieved.is_some());
assert_eq!(
retrieved.as_ref().map(|r| r.text.as_str()),
Some("hello from engine")
);
}
#[test]
fn update_via_engine() {
let engine = MemoryEngine::<TestMem>::builder().build().expect("build");
let StoreResult::Added(id) = engine.store(&mem("original")).expect("store") else {
panic!("expected Added");
};
let updated = TestMem {
id: Some(id),
text: "updated".into(),
created_at: Utc::now(),
};
engine.update(id, &updated).expect("update");
let r = engine.get(id).expect("get").expect("not found");
assert_eq!(r.text, "updated");
}
#[test]
fn delete_via_engine() {
let engine = MemoryEngine::<TestMem>::builder().build().expect("build");
let StoreResult::Added(id) = engine.store(&mem("to delete")).expect("store") else {
panic!("expected Added");
};
assert!(engine.delete(id).expect("delete"));
assert!(engine.get(id).expect("get").is_none());
}
#[test]
fn search_via_engine() {
let engine = MemoryEngine::<TestMem>::builder().build().expect("build");
engine
.store(&mem("authentication error JWT"))
.expect("store");
engine
.store(&mem("database connection timeout"))
.expect("store");
let results = engine.search("authentication").execute().expect("search");
assert_eq!(results.len(), 1);
}
#[test]
fn default_scorer_prefers_more_recent_fact() {
use chrono::Duration;
let engine = MemoryEngine::<TestMem>::builder().build().expect("build");
let old = TestMem {
id: None,
text: "The repo is still named mindcore.".into(),
created_at: Utc::now() - Duration::days(20),
};
let new = TestMem {
id: None,
text: "The repo is now fe-mind.".into(),
created_at: Utc::now(),
};
let StoreResult::Added(old_id) = engine.store(&old).expect("store old") else {
panic!("expected Added");
};
let StoreResult::Added(new_id) = engine.store(&new).expect("store new") else {
panic!("expected Added");
};
let results = engine.search("repo").execute().expect("search");
assert_eq!(results.first().map(|r| r.memory_id), Some(new_id));
assert!(
results.iter().any(|r| r.memory_id == old_id),
"stale fact should still be retrievable, just not top-ranked"
);
}
#[test]
fn count_via_engine() {
let engine = MemoryEngine::<TestMem>::builder().build().expect("build");
assert_eq!(engine.count().expect("count"), 0);
engine.store(&mem("one")).expect("store");
engine.store(&mem("two")).expect("store");
assert_eq!(engine.count().expect("count"), 2);
}
#[test]
fn dedup_via_engine() {
let engine = MemoryEngine::<TestMem>::builder().build().expect("build");
let r1 = engine.store(&mem("same text")).expect("store 1");
let r2 = engine.store(&mem("same text")).expect("store 2");
assert!(matches!(r1, StoreResult::Added(_)));
assert!(matches!(r2, StoreResult::Duplicate(_)));
assert_eq!(engine.count().expect("count"), 1);
}
#[test]
fn store_with_embedding_disabled() {
use crate::embeddings::NoopBackend;
let backend = NoopBackend::new(384);
let mut engine = MemoryEngine::<TestMem>::builder()
.embedding_backend(backend)
.build()
.expect("build");
// Disable embedding
engine.config.embedding_enabled = false;
let result = engine
.store(&mem("test memory without embedding"))
.expect("store");
assert!(matches!(result, StoreResult::Added(_)));
// FTS5 should still work
let search = engine
.search("test memory")
.limit(5)
.execute()
.expect("search");
assert!(!search.is_empty(), "FTS5 search should find the memory");
// No vector should be stored
let db = engine.database();
let vec_count: i64 = db
.with_reader(|conn| {
conn.query_row("SELECT COUNT(*) FROM memory_vectors", [], |row| row.get(0))
.map_err(Into::into)
})
.expect("count");
assert_eq!(
vec_count, 0,
"no vectors should be stored when embedding disabled"
);
}
#[test]
fn store_with_embedding_enabled() {
use crate::embeddings::NoopBackend;
let backend = NoopBackend::new(384);
let engine = MemoryEngine::<TestMem>::builder()
.embedding_backend(backend)
.build()
.expect("build");
// Default: embedding enabled
assert!(engine.config.embedding_enabled);
let result = engine
.store(&mem("test memory with embedding"))
.expect("store");
assert!(matches!(result, StoreResult::Added(_)));
// Vector should be stored
let db = engine.database();
let vec_count: i64 = db
.with_reader(|conn| {
conn.query_row("SELECT COUNT(*) FROM memory_vectors", [], |row| row.get(0))
.map_err(Into::into)
})
.expect("count");
assert_eq!(
vec_count, 1,
"vector should be stored when embedding enabled"
);
}
#[test]
fn vector_search_mode_off_uses_keyword_only() {
let mut engine = MemoryEngine::<TestMem>::builder()
.embedding_backend(ModeTestEmbedder)
.build()
.expect("build");
engine.config.vector_search_mode = VectorSearchMode::Off;
engine.store(&mem("apple orchard notes")).expect("store");
engine.store(&mem("truck repair log")).expect("store");
let results = engine
.search("banana")
.mode(crate::search::SearchMode::Auto)
.execute()
.expect("search");
assert!(
results.is_empty(),
"off mode should not use vector similarity to surface semantic-only matches"
);
}
#[test]
fn vector_search_mode_exact_enables_semantic_match() {
let mut engine = MemoryEngine::<TestMem>::builder()
.embedding_backend(ModeTestEmbedder)
.build()
.expect("build");
engine.config.vector_search_mode = VectorSearchMode::Exact;
let StoreResult::Added(apple_id) =
engine.store(&mem("apple orchard notes")).expect("store")
else {
panic!("expected Added");
};
engine.store(&mem("truck repair log")).expect("store");
let results = engine
.search("banana")
.mode(crate::search::SearchMode::Vector)
.execute()
.expect("search");
assert_eq!(results.first().map(|r| r.memory_id), Some(apple_id));
}
#[cfg(feature = "ann")]
#[test]
fn vector_search_mode_ann_builds_and_queries_index() {
let mut engine = MemoryEngine::<TestMem>::builder()
.embedding_backend(ModeTestEmbedder)
.build()
.expect("build");
engine.config.vector_search_mode = VectorSearchMode::Ann;
let StoreResult::Added(apple_id) =
engine.store(&mem("apple orchard notes")).expect("store")
else {
panic!("expected Added");
};
engine.store(&mem("truck repair log")).expect("store");
let results = engine
.search("banana")
.mode(crate::search::SearchMode::Vector)
.execute()
.expect("search");
assert_eq!(results.first().map(|r| r.memory_id), Some(apple_id));
assert!(
engine.ann_index.is_built(),
"ANN mode should build the shared index"
);
assert_eq!(engine.ann_index.model_name().as_deref(), Some("mode-test"));
}
#[test]
fn store_with_extraction_splits_large_text() {
use crate::traits::LlmCallback;
// Mock LLM that returns one fact per call
struct CountingLlm {
call_count: std::sync::atomic::AtomicUsize,
}
impl LlmCallback for CountingLlm {
fn generate(&self, _prompt: &str, _max_tokens: usize) -> Result<String> {
let n = self
.call_count
.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
Ok(format!("fact|5|Extracted fact number {}||", n))
}
fn model_name(&self) -> &str {
"mock"
}
}
let engine = MemoryEngine::<TestMem>::builder().build().expect("build");
let llm = CountingLlm {
call_count: std::sync::atomic::AtomicUsize::new(0),
};
// Create text larger than MAX_EXTRACT_CHARS (6000)
let large_text = "Some fact statement.\n".repeat(500); // ~10000 chars
assert!(large_text.len() > 6000);
let result = engine
.store_with_extraction(&large_text, &llm)
.expect("extract");
// Should have made multiple LLM calls (text was split)
let calls = llm.call_count.load(std::sync::atomic::Ordering::SeqCst);
assert!(
calls >= 2,
"large text should be split into multiple LLM calls, got {calls}"
);
// Should have extracted facts from each chunk
assert!(
result.facts_extracted >= 2,
"should extract from multiple chunks"
);
assert!(
result.memories_stored >= 2,
"should store facts from multiple chunks"
);
}
#[test]
fn store_with_extraction_result_counts() {
use crate::traits::LlmCallback;
struct MockLlm;
impl LlmCallback for MockLlm {
fn generate(&self, _prompt: &str, _max_tokens: usize) -> Result<String> {
Ok(
"fact|7|The sky is blue|sky|sky>color>blue\nfact|5|Water is wet|water|"
.to_string(),
)
}
fn model_name(&self) -> &str {
"mock"
}
}
let engine = MemoryEngine::<TestMem>::builder().build().expect("build");
let result = engine
.store_with_extraction("Some text about nature", &MockLlm)
.expect("extract");
assert_eq!(result.facts_extracted, 2);
assert_eq!(result.memories_stored, 2);
assert_eq!(result.duplicates_skipped, 0);
assert!(result.tokens_used > 0);
}
}