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
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
use crate::error::Result;
use crate::graph::activation;
use crate::retrieval::{bm25, fusion, rerank, vector};
use crate::store::{episodic, strengths};
use crate::types::*;
use rusqlite::Connection;
#[cfg(feature = "tracing")]
use tracing::{debug, trace};
/// Execute a full hybrid retrieval query.
pub fn execute_query(conn: &Connection, query: &Query) -> Result<Vec<ScoredMemory>> {
#[cfg(feature = "tracing")]
debug!(query = %query.text, max_results = query.max_results, "executing retrieval pipeline");
let now = query
.context
.current_timestamp
.unwrap_or_else(crate::db::now);
let fetch_limit = query.max_results * 3;
// Stage 1: Parallel retrieval (BM25 + vector + graph)
#[cfg(feature = "tracing")]
let _bm25_span = tracing::info_span!("bm25_search").entered();
let bm25_results: Vec<(NodeRef, f64)> =
bm25::search_bm25(conn, &query.text, fetch_limit, &query.context)?
.into_iter()
.map(|(eid, score)| (NodeRef::Episode(eid), score))
.collect();
#[cfg(feature = "tracing")]
trace!(bm25_count = bm25_results.len(), "BM25 search complete");
#[cfg(feature = "tracing")]
drop(_bm25_span);
#[cfg(feature = "tracing")]
let _vector_span = tracing::info_span!("vector_search").entered();
let vector_results: Vec<(NodeRef, f64)> = match &query.embedding {
Some(emb) => vector::search_vector(conn, emb, fetch_limit)?,
None => vec![],
};
#[cfg(feature = "tracing")]
trace!(
vector_count = vector_results.len(),
"vector search complete"
);
#[cfg(feature = "tracing")]
drop(_vector_span);
// Graph: seed from BM25 + vector top results, spread 1 hop
#[cfg(feature = "tracing")]
let _graph_span = tracing::info_span!("graph_activation").entered();
let seed_nodes: Vec<NodeRef> = bm25_results
.iter()
.take(3)
.chain(vector_results.iter().take(3))
.map(|(nr, _)| *nr)
.collect();
let graph_activation = if !seed_nodes.is_empty() {
activation::spread_activation(conn, &seed_nodes, 1, 0.1, 0.6)?
} else {
std::collections::HashMap::new()
};
let graph_results: Vec<(NodeRef, f64)> = graph_activation
.into_iter()
.filter(|(nr, _)| !seed_nodes.contains(nr)) // exclude seeds
.map(|(nr, act)| (nr, act as f64))
.collect();
#[cfg(feature = "tracing")]
drop(_graph_span);
// Stage 2: RRF fusion
#[cfg(feature = "tracing")]
let _rrf_span = tracing::info_span!("rrf_fusion").entered();
let boost = query.boost_weights.as_ref();
let mut sets: Vec<Vec<(NodeRef, f64)>> = vec![bm25_results];
let mut weights: Vec<f32> = vec![boost.map_or(1.0, |b| b.bm25)];
if !vector_results.is_empty() {
sets.push(vector_results);
weights.push(boost.map_or(1.0, |b| b.vector));
}
if !graph_results.is_empty() {
sets.push(graph_results);
weights.push(boost.map_or(1.0, |b| b.graph));
}
let fused = fusion::rrf_merge_weighted(&sets, 60, Some(&weights));
#[cfg(feature = "tracing")]
trace!(fused_count = fused.len(), "RRF fusion complete");
#[cfg(feature = "tracing")]
drop(_rrf_span);
// Stage 3: Enrich candidates with content and context for reranking
let candidates: Vec<(NodeRef, f64, String, Option<Role>, i64, EpisodeContext)> =
fused
.into_iter()
.take(fetch_limit)
.filter_map(|(node_ref, score)| match node_ref {
NodeRef::Episode(eid) => episodic::get_episode(conn, eid).ok().map(|ep| {
(
node_ref,
score,
ep.content,
Some(ep.role),
ep.timestamp,
ep.context,
)
}),
NodeRef::Semantic(nid) => crate::store::semantic::get_semantic_node(conn, nid)
.ok()
.filter(|node| node.confidence > 0.0) // superseded nodes have confidence 0.0
.filter(|node| match query.category_id {
Some(cat) => node.category_id == Some(cat),
None => true,
})
.map(|node| {
(
node_ref,
score,
node.content,
None,
node.created_at,
EpisodeContext::default(),
)
}),
NodeRef::Preference(pid) => crate::store::implicit::get_preference(conn, pid)
.ok()
.map(|pref| {
(
node_ref,
score,
format!("preference: {}: {}", pref.domain, pref.preference),
None,
pref.first_observed,
EpisodeContext::default(),
)
}),
_ => None, // Categories don't carry retrievable content
})
.collect();
#[cfg(feature = "tracing")]
let _rerank_span = tracing::info_span!("rerank").entered();
let mut results = rerank::rerank(candidates, &query.context, now, query.max_results);
#[cfg(feature = "tracing")]
drop(_rerank_span);
// Stage 3b: Post-filter — remove results containing any exclude_terms (case-insensitive)
if !query.context.exclude_terms.is_empty() {
let exclude_lower: Vec<String> = query
.context
.exclude_terms
.iter()
.map(|t| t.to_lowercase())
.collect();
results.retain(|scored| {
let content_lower = scored.content.to_lowercase();
!exclude_lower
.iter()
.any(|term| content_lower.contains(term))
});
}
// Stage 4: Post-retrieval updates (RIF + strength tracking)
for scored in &results {
let _ = strengths::on_access(conn, scored.node);
}
// Co-retrieval Hebbian strengthening between all retrieved pairs
let retrieved_nodes: Vec<NodeRef> = results.iter().map(|r| r.node).collect();
for i in 0..retrieved_nodes.len() {
for j in (i + 1)..retrieved_nodes.len() {
let _ =
crate::graph::links::on_co_retrieval(conn, retrieved_nodes[i], retrieved_nodes[j]);
}
}
// RIF: suppress competing memories from the same session
let rif_suppression_factor = 0.9;
let retrieved_set: std::collections::HashSet<NodeRef> =
results.iter().map(|r| r.node).collect();
let mut suppressed_sessions: std::collections::HashSet<String> =
std::collections::HashSet::new();
// Collect session IDs from retrieved episodes
for scored in &results {
if let NodeRef::Episode(eid) = scored.node {
if let Ok(ep) = episodic::get_episode(conn, eid) {
suppressed_sessions.insert(ep.session_id.clone());
}
}
}
// For each session, suppress non-retrieved episodes
for session_id in &suppressed_sessions {
if let Ok(session_episodes) = episodic::get_episodes_by_session(conn, session_id) {
for ep in &session_episodes {
let node = NodeRef::Episode(ep.id);
if !retrieved_set.contains(&node) {
let _ = strengths::suppress_retrieval(conn, node, rif_suppression_factor);
}
}
}
}
Ok(results)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::schema::open_memory_db;
use crate::store::episodic;
// ---------------------------------------------------------------------------
// Helper: build a NewEpisode with minimal boilerplate
// ---------------------------------------------------------------------------
fn ep(content: &str, session: &str, ts: i64) -> NewEpisode {
NewEpisode {
content: content.to_string(),
role: Role::User,
session_id: session.to_string(),
timestamp: ts,
context: EpisodeContext::default(),
embedding: None,
}
}
fn ep_with_ctx(content: &str, session: &str, ts: i64, ctx: EpisodeContext) -> NewEpisode {
NewEpisode {
content: content.to_string(),
role: Role::User,
session_id: session.to_string(),
timestamp: ts,
context: ctx,
embedding: None,
}
}
// ---------------------------------------------------------------------------
// Empty store — BM25 + no seeds => empty graph activation branch
// ---------------------------------------------------------------------------
#[test]
fn test_query_empty_store_returns_empty() {
let conn = open_memory_db().unwrap();
let results = execute_query(
&conn,
&Query {
text: "anything".to_string(),
embedding: None,
context: QueryContext {
current_timestamp: Some(1000),
..Default::default()
},
max_results: 5,
category_id: None,
boost_categories: None,
boost_weights: None,
},
)
.unwrap();
assert!(results.is_empty(), "empty store must return no results");
}
// ---------------------------------------------------------------------------
// BM25-only (no embedding) — exercises None arm of vector_results match
// ---------------------------------------------------------------------------
#[test]
fn test_query_bm25_only_no_embedding() {
let conn = open_memory_db().unwrap();
episodic::store_episode(&conn, &ep("Rust memory safety is great", "s1", 1000)).unwrap();
episodic::store_episode(&conn, &ep("Python data science workflow", "s2", 2000)).unwrap();
let results = execute_query(
&conn,
&Query {
text: "Rust memory safety".to_string(),
embedding: None, // explicitly no embedding
context: QueryContext {
current_timestamp: Some(5000),
..Default::default()
},
max_results: 5,
category_id: None,
boost_categories: None,
boost_weights: None,
},
)
.unwrap();
assert!(!results.is_empty(), "should find BM25 results");
assert!(
results[0].content.contains("Rust"),
"top result should match query; got: {}",
results[0].content
);
}
// ---------------------------------------------------------------------------
// Multiple results — verify descending score ordering
// ---------------------------------------------------------------------------
#[test]
fn test_query_multiple_results_descending_order() {
let conn = open_memory_db().unwrap();
// Store several episodes about "Rust" with different timestamps
for i in 0..5u32 {
episodic::store_episode(
&conn,
&ep(
&format!("Rust programming episode {i}"),
"s1",
1000 + i as i64 * 100,
),
)
.unwrap();
}
let results = execute_query(
&conn,
&Query {
text: "Rust programming".to_string(),
embedding: None,
context: QueryContext {
current_timestamp: Some(10000),
..Default::default()
},
max_results: 5,
category_id: None,
boost_categories: None,
boost_weights: None,
},
)
.unwrap();
assert!(results.len() >= 2, "should return multiple results");
// Verify descending order
for w in results.windows(2) {
assert!(
w[0].score >= w[1].score,
"results must be in descending score order: {} < {}",
w[0].score,
w[1].score
);
}
}
// ---------------------------------------------------------------------------
// max_results limit is honoured — never returns more than max_results
// ---------------------------------------------------------------------------
#[test]
fn test_query_max_results_honored() {
let conn = open_memory_db().unwrap();
for i in 0..10u32 {
episodic::store_episode(
&conn,
&ep(
&format!("Rust topic episode {i}"),
"s1",
1000 + i as i64 * 50,
),
)
.unwrap();
}
let results = execute_query(
&conn,
&Query {
text: "Rust topic".to_string(),
embedding: None,
context: QueryContext {
current_timestamp: Some(9000),
..Default::default()
},
max_results: 3,
category_id: None,
boost_categories: None,
boost_weights: None,
},
)
.unwrap();
assert!(
results.len() <= 3,
"max_results=3 must not be exceeded; got {}",
results.len()
);
}
// ---------------------------------------------------------------------------
// max_results=1 — minimal slice
// ---------------------------------------------------------------------------
#[test]
fn test_query_max_results_one() {
let conn = open_memory_db().unwrap();
episodic::store_episode(&conn, &ep("Rust ownership model", "s1", 1000)).unwrap();
episodic::store_episode(&conn, &ep("Rust borrow checker explained", "s1", 2000)).unwrap();
let results = execute_query(
&conn,
&Query {
text: "Rust ownership".to_string(),
embedding: None,
context: QueryContext {
current_timestamp: Some(5000),
..Default::default()
},
max_results: 1,
category_id: None,
boost_categories: None,
boost_weights: None,
},
)
.unwrap();
assert!(results.len() <= 1, "should not exceed max_results=1");
}
// ---------------------------------------------------------------------------
// Query with context topics/entities — exercises reranking context_similarity
// An episode stored with matching context should score higher than one without
// ---------------------------------------------------------------------------
#[test]
fn test_query_with_context_boosts_matching_episode() {
let conn = open_memory_db().unwrap();
// Episode with matching topic context
let matching_ctx = EpisodeContext {
topics: vec!["rust".to_string(), "async".to_string()],
mentioned_entities: vec!["tokio".to_string()],
sentiment: 0.5,
conversation_turn: 1,
preceding_episode: None,
};
episodic::store_episode(
&conn,
&ep_with_ctx("async Rust with tokio runtime", "s1", 1000, matching_ctx),
)
.unwrap();
// Episode without matching context (stored slightly later for same recency)
episodic::store_episode(&conn, &ep("async Rust with tokio runtime", "s2", 1100)).unwrap();
let results = execute_query(
&conn,
&Query {
text: "async Rust tokio".to_string(),
embedding: None,
context: QueryContext {
topics: vec!["rust".to_string(), "async".to_string()],
mentioned_entities: vec!["tokio".to_string()],
sentiment: 0.5,
current_timestamp: Some(5000),
..Default::default()
},
max_results: 10,
category_id: None,
boost_categories: None,
boost_weights: None,
},
)
.unwrap();
assert!(!results.is_empty(), "should return results");
// All we can guarantee: pipeline runs without error and order is valid
for w in results.windows(2) {
assert!(w[0].score >= w[1].score, "must be ordered descending");
}
}
// ---------------------------------------------------------------------------
// Context: current_timestamp provided vs None (exercises both branches of
// the unwrap_or_else for `now`)
// ---------------------------------------------------------------------------
#[test]
fn test_query_no_current_timestamp_uses_system_time() {
let conn = open_memory_db().unwrap();
episodic::store_episode(&conn, &ep("Rust lifetime elision", "s1", 1000)).unwrap();
// current_timestamp: None => code falls through to SystemTime::now()
let results = execute_query(
&conn,
&Query {
text: "Rust lifetime".to_string(),
embedding: None,
context: QueryContext {
current_timestamp: None, // triggers the SystemTime branch
..Default::default()
},
max_results: 5,
category_id: None,
boost_categories: None,
boost_weights: None,
},
)
.unwrap();
// As long as no panic, the branch was exercised
assert!(!results.is_empty());
}
// ---------------------------------------------------------------------------
// Graph boost path — episodes linked to preferences via Topical link;
// graph spreading should activate the preference node
// (mirrors test_query_returns_preferences_via_graph but asserts co-retrieval
// Hebbian strengthening side-effect via link weight increase)
// ---------------------------------------------------------------------------
#[test]
fn test_graph_boost_co_retrieval_strengthens_link() {
let conn = open_memory_db().unwrap();
use crate::graph::links;
use crate::store::{implicit, strengths};
let ep_id = episodic::store_episode(
&conn,
&ep("I love dark mode for coding at night", "s1", 1000),
)
.unwrap();
let pref_id = implicit::store_preference(&conn, "ui", "dark mode", 0.9).unwrap();
strengths::init_strength(&conn, NodeRef::Preference(pref_id)).unwrap();
links::create_link(
&conn,
NodeRef::Episode(ep_id),
NodeRef::Preference(pref_id),
LinkType::Topical,
0.9,
)
.unwrap();
// Before query: record initial link weight
let before_links = links::get_links_from(&conn, NodeRef::Episode(ep_id)).unwrap();
let before_weight = before_links
.first()
.map(|l| l.forward_weight)
.unwrap_or(0.0);
execute_query(
&conn,
&Query {
text: "dark mode coding".to_string(),
embedding: None,
context: QueryContext {
current_timestamp: Some(2000),
..Default::default()
},
max_results: 10,
category_id: None,
boost_categories: None,
boost_weights: None,
},
)
.unwrap();
// After query: if both episode and preference were co-retrieved,
// on_co_retrieval fires, strengthening the link.
// (The test validates the co-retrieval path ran without error)
let after_links = links::get_links_from(&conn, NodeRef::Episode(ep_id)).unwrap();
let after_weight = after_links.first().map(|l| l.forward_weight).unwrap_or(0.0);
// Weight should be >= before (co-retrieval only increases or holds)
assert!(
after_weight >= before_weight,
"co-retrieval should not decrease link weight"
);
}
// ---------------------------------------------------------------------------
// Seed-node deduplication — graph_results excludes seed nodes
// (exercises the filter in graph_results construction)
// ---------------------------------------------------------------------------
#[test]
fn test_graph_results_exclude_seed_nodes() {
let conn = open_memory_db().unwrap();
use crate::graph::links;
use crate::store::strengths;
// Store two episodes and link them so graph spreading finds the second
let ep1 = episodic::store_episode(&conn, &ep("Rust async seed node", "s1", 1000)).unwrap();
let ep2 =
episodic::store_episode(&conn, &ep("Rust async neighbor node", "s1", 2000)).unwrap();
strengths::init_strength(&conn, NodeRef::Episode(ep1)).unwrap();
strengths::init_strength(&conn, NodeRef::Episode(ep2)).unwrap();
links::create_link(
&conn,
NodeRef::Episode(ep1),
NodeRef::Episode(ep2),
LinkType::Topical,
0.8,
)
.unwrap();
let results = execute_query(
&conn,
&Query {
text: "Rust async".to_string(),
embedding: None,
context: QueryContext {
current_timestamp: Some(5000),
..Default::default()
},
max_results: 10,
category_id: None,
boost_categories: None,
boost_weights: None,
},
)
.unwrap();
// Should have results — both episodes reachable
assert!(!results.is_empty(), "should find episodes");
// No result should appear twice (seed dedup worked)
let ids: Vec<i64> = results.iter().map(|r| r.node.id()).collect();
let mut unique = ids.clone();
unique.dedup();
// Sort before dedup for correctness
let mut sorted_ids = ids.clone();
sorted_ids.sort_unstable();
sorted_ids.dedup();
assert_eq!(
ids.len(),
sorted_ids.len(),
"duplicate nodes in results: {ids:?}"
);
}
// ---------------------------------------------------------------------------
// Vector search branch — embedding provided => Some(emb) arm exercised
// ---------------------------------------------------------------------------
#[test]
fn test_query_with_embedding_exercises_vector_branch() {
let conn = open_memory_db().unwrap();
use crate::store::{embeddings, semantic, strengths};
// Store semantic node + embedding
let nid = semantic::store_semantic_node(
&conn,
&NewSemanticNode {
content: "Rust zero-cost abstractions".to_string(),
node_type: SemanticType::Fact,
confidence: 0.9,
source_episodes: vec![],
embedding: None,
},
)
.unwrap();
embeddings::store_embedding(&conn, "semantic", nid.0, &[1.0f32, 0.0, 0.0], "").unwrap();
strengths::init_strength(&conn, NodeRef::Semantic(nid)).unwrap();
// Also store an episode for BM25 baseline
episodic::store_episode(
&conn,
&ep("Rust zero-cost abstractions are elegant", "s1", 1000),
)
.unwrap();
let results = execute_query(
&conn,
&Query {
text: "Rust abstractions".to_string(),
embedding: Some(vec![0.9f32, 0.1, 0.0]), // triggers vector search
context: QueryContext {
current_timestamp: Some(5000),
..Default::default()
},
max_results: 10,
category_id: None,
boost_categories: None,
boost_weights: None,
},
)
.unwrap();
assert!(!results.is_empty(), "vector query must return results");
// At least one semantic node should appear
assert!(
results
.iter()
.any(|r| matches!(r.node, NodeRef::Semantic(_))),
"should include semantic node from vector search"
);
}
// ---------------------------------------------------------------------------
// RIF suppression: non-retrieved same-session episodes get suppressed
// (already partially tested but extended to check strength init required)
// ---------------------------------------------------------------------------
#[test]
fn test_rif_suppression_same_session() {
let conn = open_memory_db().unwrap();
use crate::store::strengths;
// 4 episodes in same session — only one should match query strongly
let ids: Vec<_> = (0..4)
.map(|i| {
let eid = episodic::store_episode(
&conn,
&ep(
&format!("session memory item {i} about Rust coding"),
"sess_rif",
1000 + i as i64 * 10,
),
)
.unwrap();
strengths::init_strength(&conn, NodeRef::Episode(eid)).unwrap();
eid
})
.collect();
// Query retrieving only 1 result
let results = execute_query(
&conn,
&Query {
text: "memory item 0 Rust".to_string(),
embedding: None,
context: QueryContext {
current_timestamp: Some(5000),
..Default::default()
},
max_results: 1,
category_id: None,
boost_categories: None,
boost_weights: None,
},
)
.unwrap();
assert_eq!(results.len(), 1);
let retrieved_id = match results[0].node {
NodeRef::Episode(eid) => eid.0,
_ => panic!("expected episode"),
};
// At least one non-retrieved episode should have suppressed strength < 1.0
let suppressed = ids.iter().filter(|eid| eid.0 != retrieved_id).any(|eid| {
strengths::get_strength(&conn, NodeRef::Episode(*eid))
.map(|s| s.retrieval_strength < 1.0)
.unwrap_or(false)
});
assert!(
suppressed,
"at least one non-retrieved episode should be suppressed"
);
}
// ---------------------------------------------------------------------------
// Edge case: empty query string — FTS5 with empty string returns nothing
// ---------------------------------------------------------------------------
#[test]
fn test_empty_query_string_returns_empty() {
let conn = open_memory_db().unwrap();
episodic::store_episode(&conn, &ep("Rust programming", "s1", 1000)).unwrap();
let results = execute_query(&conn, &Query::simple("")).unwrap();
assert!(
results.is_empty(),
"empty query string must return no results"
);
}
// ---------------------------------------------------------------------------
// Edge case: single-character query
// ---------------------------------------------------------------------------
#[test]
fn test_single_char_query() {
let conn = open_memory_db().unwrap();
episodic::store_episode(&conn, &ep("Rust is great", "s1", 1000)).unwrap();
// FTS5 may or may not return results for 'R' — either way must not panic
let result = execute_query(&conn, &Query::simple("R"));
assert!(result.is_ok(), "single-char query must not error");
}
// ---------------------------------------------------------------------------
// Edge case: very long query string
// ---------------------------------------------------------------------------
#[test]
fn test_very_long_query_string() {
let conn = open_memory_db().unwrap();
episodic::store_episode(&conn, &ep("Rust programming language", "s1", 1000)).unwrap();
let long_query = "Rust ".repeat(200); // 1000 chars
let result = execute_query(&conn, &Query::simple(long_query.trim()));
assert!(result.is_ok(), "very long query must not error");
}
// ---------------------------------------------------------------------------
// on_access is called for each retrieved result (strength tracking path)
// Verify retrieval_strength is refreshed (= 1.0) after query
// ---------------------------------------------------------------------------
#[test]
fn test_on_access_updates_strength_after_query() {
let conn = open_memory_db().unwrap();
use crate::store::strengths;
let eid =
episodic::store_episode(&conn, &ep("Rust traits and generics", "s1", 1000)).unwrap();
strengths::init_strength(&conn, NodeRef::Episode(eid)).unwrap();
// Manually decay to a low value first
strengths::suppress_retrieval(&conn, NodeRef::Episode(eid), 0.5).unwrap();
let before = strengths::get_strength(&conn, NodeRef::Episode(eid))
.unwrap()
.retrieval_strength;
assert!(before < 1.0, "strength should be decayed before query");
execute_query(
&conn,
&Query {
text: "Rust traits generics".to_string(),
embedding: None,
context: QueryContext {
current_timestamp: Some(5000),
..Default::default()
},
max_results: 5,
category_id: None,
boost_categories: None,
boost_weights: None,
},
)
.unwrap();
let after = strengths::get_strength(&conn, NodeRef::Episode(eid))
.unwrap()
.retrieval_strength;
assert!(
after > before,
"on_access should refresh strength: before={before}, after={after}"
);
}
// ---------------------------------------------------------------------------
// Cross-session: multiple sessions — RIF suppression is per-session scoped
// Episodes from different sessions are not suppressed by each other
// ---------------------------------------------------------------------------
#[test]
fn test_rif_does_not_suppress_across_sessions() {
let conn = open_memory_db().unwrap();
use crate::store::strengths;
// One episode in session A
let ep_a =
episodic::store_episode(&conn, &ep("Rust ownership rules", "session_a", 1000)).unwrap();
strengths::init_strength(&conn, NodeRef::Episode(ep_a)).unwrap();
// One episode in session B — completely different session
let ep_b =
episodic::store_episode(&conn, &ep("Rust ownership rules copy", "session_b", 2000))
.unwrap();
strengths::init_strength(&conn, NodeRef::Episode(ep_b)).unwrap();
// Query returns episode from session_a
execute_query(
&conn,
&Query {
text: "Rust ownership".to_string(),
embedding: None,
context: QueryContext {
current_timestamp: Some(5000),
..Default::default()
},
max_results: 5,
category_id: None,
boost_categories: None,
boost_weights: None,
},
)
.unwrap();
// episode in session_b has no competing episodes in session_b,
// so it should NOT be suppressed by RIF (no unretrieved session_b siblings)
let strength_b = strengths::get_strength(&conn, NodeRef::Episode(ep_b))
.unwrap()
.retrieval_strength;
// Initial strength (no suppress applied), either 1.0 (init) or may have been on_accessed.
// Key invariant: it was NOT suppressed to < 0.9 * init
assert!(
strength_b >= 0.9,
"cross-session episode should not be suppressed; got {strength_b}"
);
}
// ---------------------------------------------------------------------------
// Result contains ScoredMemory with correct fields (content, role, timestamp)
// ---------------------------------------------------------------------------
#[test]
fn test_result_fields_populated_correctly() {
let conn = open_memory_db().unwrap();
episodic::store_episode(
&conn,
&NewEpisode {
content: "Rust lifetimes explained clearly".to_string(),
role: Role::Assistant,
session_id: "s_fields".to_string(),
timestamp: 42000,
context: EpisodeContext::default(),
embedding: None,
},
)
.unwrap();
let results = execute_query(
&conn,
&Query {
text: "Rust lifetimes".to_string(),
embedding: None,
context: QueryContext {
current_timestamp: Some(50000),
..Default::default()
},
max_results: 5,
category_id: None,
boost_categories: None,
boost_weights: None,
},
)
.unwrap();
assert!(!results.is_empty());
let r = &results[0];
assert!(
r.content.contains("Rust lifetimes"),
"content must be populated"
);
assert_eq!(r.role, Some(Role::Assistant), "role must be preserved");
assert_eq!(r.timestamp, 42000, "timestamp must match stored value");
assert!(r.score > 0.0, "score must be positive");
}
// ---------------------------------------------------------------------------
// Original test kept unchanged
// ---------------------------------------------------------------------------
#[test]
fn test_basic_query() {
let conn = open_memory_db().unwrap();
episodic::store_episode(
&conn,
&NewEpisode {
content: "I love Rust programming".to_string(),
role: Role::User,
session_id: "s1".to_string(),
timestamp: 1000,
context: EpisodeContext::default(),
embedding: None,
},
)
.unwrap();
episodic::store_episode(
&conn,
&NewEpisode {
content: "Python is great for ML".to_string(),
role: Role::User,
session_id: "s1".to_string(),
timestamp: 2000,
context: EpisodeContext::default(),
embedding: None,
},
)
.unwrap();
let results = execute_query(
&conn,
&Query {
text: "Rust programming".to_string(),
embedding: None,
context: QueryContext {
current_timestamp: Some(3000),
..Default::default()
},
max_results: 5,
category_id: None,
boost_categories: None,
boost_weights: None,
},
)
.unwrap();
assert!(!results.is_empty());
assert!(results[0].content.contains("Rust"));
}
#[test]
fn test_query_returns_semantic_nodes() {
let conn = open_memory_db().unwrap();
use crate::store::{embeddings, semantic, strengths};
// Store a semantic node with embedding
let node_id = semantic::store_semantic_node(
&conn,
&NewSemanticNode {
content: "Rust has zero-cost abstractions".to_string(),
node_type: SemanticType::Fact,
confidence: 0.9,
source_episodes: vec![],
embedding: None,
},
)
.unwrap();
// Store embedding for vector search
let emb = vec![1.0, 0.0, 0.0];
embeddings::store_embedding(&conn, "semantic", node_id.0, &emb, "").unwrap();
strengths::init_strength(&conn, NodeRef::Semantic(node_id)).unwrap();
// Also store an episode so BM25 can potentially match
episodic::store_episode(
&conn,
&NewEpisode {
content: "Rust has zero-cost abstractions and great memory safety".to_string(),
role: Role::User,
session_id: "s1".to_string(),
timestamp: 1000,
context: EpisodeContext::default(),
embedding: None,
},
)
.unwrap();
// Query with embedding that matches the semantic node
let results = execute_query(
&conn,
&Query {
text: "Rust abstractions".to_string(),
embedding: Some(vec![0.9, 0.1, 0.0]),
context: QueryContext {
current_timestamp: Some(2000),
..Default::default()
},
max_results: 10,
category_id: None,
boost_categories: None,
boost_weights: None,
},
)
.unwrap();
// Should find results — at minimum the episode via BM25
assert!(!results.is_empty(), "should have results");
// Check if any result is a semantic node
let has_semantic = results
.iter()
.any(|r| matches!(r.node, NodeRef::Semantic(_)));
assert!(
has_semantic,
"should include semantic node in results, got: {:?}",
results.iter().map(|r| r.node).collect::<Vec<_>>()
);
}
#[test]
fn test_rif_suppresses_competing_memories() {
let conn = open_memory_db().unwrap();
use crate::store::strengths;
// Store 3 episodes in the same session
for i in 0..3 {
episodic::store_episode(
&conn,
&NewEpisode {
content: format!("session topic {i} about Rust programming"),
role: Role::User,
session_id: "s1".to_string(),
timestamp: 1000 + i as i64,
context: EpisodeContext::default(),
embedding: None,
},
)
.unwrap();
}
// Init strengths for all 3
for id in 1..=3 {
strengths::init_strength(&conn, NodeRef::Episode(EpisodeId(id))).unwrap();
}
// Query should retrieve some but not all episodes from the session
// The query "Rust programming 0" should match episode 0 most strongly
let results = execute_query(
&conn,
&Query {
text: "topic 0 Rust".to_string(),
embedding: None,
context: QueryContext {
current_timestamp: Some(2000),
..Default::default()
},
max_results: 1, // Only retrieve 1
category_id: None,
boost_categories: None,
boost_weights: None,
},
)
.unwrap();
assert!(!results.is_empty(), "should have at least 1 result");
// The retrieved episode(s) should have RS = 1.0 (refreshed by on_access)
let retrieved_ids: Vec<i64> = results
.iter()
.filter_map(|r| match r.node {
NodeRef::Episode(eid) => Some(eid.0),
_ => None,
})
.collect();
// Check that at least one NON-retrieved same-session episode got suppressed
let mut any_suppressed = false;
for id in 1..=3i64 {
if !retrieved_ids.contains(&id) {
let s = strengths::get_strength(&conn, NodeRef::Episode(EpisodeId(id))).unwrap();
if s.retrieval_strength < 1.0 {
any_suppressed = true;
}
}
}
assert!(
any_suppressed,
"at least one non-retrieved same-session episode should have suppressed RS"
);
}
#[test]
fn test_empty_query() {
let conn = open_memory_db().unwrap();
let results = execute_query(&conn, &Query::simple("")).unwrap();
assert!(results.is_empty());
}
#[test]
fn test_query_excludes_superseded_semantic_nodes() {
let conn = open_memory_db().unwrap();
use crate::store::{conflicts, embeddings, semantic, strengths};
let winner = semantic::store_semantic_node(
&conn,
&NewSemanticNode {
content: "Rust has zero-cost abstractions".to_string(),
node_type: SemanticType::Fact,
confidence: 0.9,
source_episodes: vec![],
embedding: None,
},
)
.unwrap();
embeddings::store_embedding(&conn, "semantic", winner.0, &[1.0, 0.0, 0.0], "").unwrap();
strengths::init_strength(&conn, NodeRef::Semantic(winner)).unwrap();
let loser = semantic::store_semantic_node(
&conn,
&NewSemanticNode {
content: "Rust has high-cost abstractions".to_string(),
node_type: SemanticType::Fact,
confidence: 0.8,
source_episodes: vec![],
embedding: None,
},
)
.unwrap();
embeddings::store_embedding(&conn, "semantic", loser.0, &[0.9, 0.1, 0.0], "").unwrap();
strengths::init_strength(&conn, NodeRef::Semantic(loser)).unwrap();
// Supersede loser
conflicts::supersede_node(&conn, loser, winner).unwrap();
let results = execute_query(
&conn,
&Query {
text: "Rust abstractions".to_string(),
embedding: Some(vec![0.95, 0.05, 0.0]),
context: QueryContext {
current_timestamp: Some(5000),
..Default::default()
},
max_results: 10,
category_id: None,
boost_categories: None,
boost_weights: None,
},
)
.unwrap();
// Loser should not appear in results
let has_loser = results.iter().any(|r| r.node == NodeRef::Semantic(loser));
assert!(
!has_loser,
"superseded node should be excluded from retrieval"
);
}
#[test]
fn test_query_returns_preferences_via_graph() {
let conn = open_memory_db().unwrap();
use crate::graph::links;
use crate::store::{implicit, strengths};
// Store an episode mentioning "dark mode"
let ep_id = episodic::store_episode(
&conn,
&NewEpisode {
content: "I prefer dark mode for coding".to_string(),
role: Role::User,
session_id: "s1".to_string(),
timestamp: 1000,
context: EpisodeContext::default(),
embedding: None,
},
)
.unwrap();
// Store a preference about dark mode
let pref_id = implicit::store_preference(&conn, "ui", "dark mode", 0.8).unwrap();
strengths::init_strength(&conn, NodeRef::Preference(pref_id)).unwrap();
// Link episode to preference to enable graph spreading activation
links::create_link(
&conn,
NodeRef::Episode(ep_id),
NodeRef::Preference(pref_id),
LinkType::Topical,
0.9,
)
.unwrap();
// Query for "dark mode" - episode should be found via BM25,
// then graph spreading should activate the preference
let results = execute_query(
&conn,
&Query {
text: "dark mode coding".to_string(),
embedding: None,
context: QueryContext {
current_timestamp: Some(2000),
..Default::default()
},
max_results: 10,
category_id: None,
boost_categories: None,
boost_weights: None,
},
)
.unwrap();
assert!(!results.is_empty(), "should have results");
// Check if any result is a preference (graph activation path)
let has_preference = results
.iter()
.any(|r| matches!(r.node, NodeRef::Preference(_)));
if has_preference {
let pref_result = results
.iter()
.find(|r| matches!(r.node, NodeRef::Preference(_)))
.unwrap();
assert!(
pref_result.content.contains("dark mode"),
"preference content should contain dark mode"
);
}
}
// ---------------------------------------------------------------------------
// Category-scoped retrieval — semantic nodes outside the requested category
// are excluded from results (inspired by MemPalace's palace scoping)
// ---------------------------------------------------------------------------
#[test]
fn test_category_scoped_query_excludes_other_categories() {
let conn = open_memory_db().unwrap();
use crate::store::{categories, embeddings, semantic, strengths};
// Create two semantic nodes in different categories
let node_rust = semantic::store_semantic_node(
&conn,
&NewSemanticNode {
content: "Rust has zero-cost abstractions".to_string(),
node_type: SemanticType::Fact,
confidence: 0.9,
source_episodes: vec![],
embedding: None,
},
)
.unwrap();
embeddings::store_embedding(&conn, "semantic", node_rust.0, &[1.0, 0.0, 0.0], "").unwrap();
strengths::init_strength(&conn, NodeRef::Semantic(node_rust)).unwrap();
let node_python = semantic::store_semantic_node(
&conn,
&NewSemanticNode {
content: "Python has dynamic typing with zero-cost abstractions mindset".to_string(),
node_type: SemanticType::Fact,
confidence: 0.9,
source_episodes: vec![],
embedding: None,
},
)
.unwrap();
embeddings::store_embedding(&conn, "semantic", node_python.0, &[0.9, 0.1, 0.0], "")
.unwrap();
strengths::init_strength(&conn, NodeRef::Semantic(node_python)).unwrap();
// Create categories and assign nodes
let cat_rust =
categories::store_category(&conn, "rust-lang", node_rust, None, None).unwrap();
categories::assign_node_to_category(&conn, node_rust, cat_rust).unwrap();
let cat_python =
categories::store_category(&conn, "python", node_python, None, None).unwrap();
categories::assign_node_to_category(&conn, node_python, cat_python).unwrap();
// Query scoped to rust-lang category
let results = execute_query(
&conn,
&Query {
text: "zero-cost abstractions".to_string(),
embedding: Some(vec![0.95, 0.05, 0.0]),
context: QueryContext {
current_timestamp: Some(5000),
..Default::default()
},
max_results: 10,
category_id: Some(cat_rust.0),
boost_categories: None,
boost_weights: None,
},
)
.unwrap();
// Only the rust node should appear — python node filtered out
let semantic_nodes: Vec<_> = results
.iter()
.filter(|r| matches!(r.node, NodeRef::Semantic(_)))
.collect();
assert!(
!semantic_nodes.is_empty(),
"should find the rust semantic node"
);
for r in &semantic_nodes {
assert_eq!(
r.node,
NodeRef::Semantic(node_rust),
"only rust-category nodes should appear; got {:?}",
r.node
);
}
}
#[test]
fn test_category_scoped_query_none_returns_all() {
let conn = open_memory_db().unwrap();
use crate::store::{categories, embeddings, semantic, strengths};
// Create two semantic nodes
let node_a = semantic::store_semantic_node(
&conn,
&NewSemanticNode {
content: "Rust ownership model explained".to_string(),
node_type: SemanticType::Fact,
confidence: 0.9,
source_episodes: vec![],
embedding: None,
},
)
.unwrap();
embeddings::store_embedding(&conn, "semantic", node_a.0, &[1.0, 0.0, 0.0], "").unwrap();
strengths::init_strength(&conn, NodeRef::Semantic(node_a)).unwrap();
let node_b = semantic::store_semantic_node(
&conn,
&NewSemanticNode {
content: "Rust borrow checker ownership rules".to_string(),
node_type: SemanticType::Fact,
confidence: 0.9,
source_episodes: vec![],
embedding: None,
},
)
.unwrap();
embeddings::store_embedding(&conn, "semantic", node_b.0, &[0.9, 0.1, 0.0], "").unwrap();
strengths::init_strength(&conn, NodeRef::Semantic(node_b)).unwrap();
// Assign to different categories
let cat_a = categories::store_category(&conn, "cat-a", node_a, None, None).unwrap();
categories::assign_node_to_category(&conn, node_a, cat_a).unwrap();
let cat_b = categories::store_category(&conn, "cat-b", node_b, None, None).unwrap();
categories::assign_node_to_category(&conn, node_b, cat_b).unwrap();
// Query with category_id: None — should return both
let results = execute_query(
&conn,
&Query {
text: "Rust ownership".to_string(),
embedding: Some(vec![0.95, 0.05, 0.0]),
context: QueryContext {
current_timestamp: Some(5000),
..Default::default()
},
max_results: 10,
category_id: None,
boost_categories: None,
boost_weights: None,
},
)
.unwrap();
let semantic_nodes: Vec<_> = results
.iter()
.filter(|r| matches!(r.node, NodeRef::Semantic(_)))
.collect();
assert!(
semantic_nodes.len() >= 2,
"with no category filter, both semantic nodes should appear; got {}",
semantic_nodes.len()
);
}
#[test]
fn test_category_scoped_query_does_not_filter_episodes() {
let conn = open_memory_db().unwrap();
use crate::store::{categories, semantic};
// Create a semantic node and category
let node = semantic::store_semantic_node(
&conn,
&NewSemanticNode {
content: "prototype".to_string(),
node_type: SemanticType::Fact,
confidence: 0.9,
source_episodes: vec![],
embedding: None,
},
)
.unwrap();
let cat = categories::store_category(&conn, "scoped-cat", node, None, None).unwrap();
// Store an episode (episodes have no category)
episodic::store_episode(&conn, &ep("Rust async programming patterns", "s1", 1000)).unwrap();
// Query scoped to the category — episodes should still appear
let results = execute_query(
&conn,
&Query {
text: "Rust async programming".to_string(),
embedding: None,
context: QueryContext {
current_timestamp: Some(5000),
..Default::default()
},
max_results: 10,
category_id: Some(cat.0),
boost_categories: None,
boost_weights: None,
},
)
.unwrap();
let has_episodes = results
.iter()
.any(|r| matches!(r.node, NodeRef::Episode(_)));
assert!(
has_episodes,
"category scoping should not filter out episodes"
);
}
#[test]
fn test_category_node_filtered_out_of_results() {
// Covers line 119: _ => None for Category nodes in the filter_map
let conn = open_memory_db().unwrap();
// Create an episode that matches our query
let ep1 =
episodic::store_episode(&conn, &ep("Rust programming language", "s1", 1000)).unwrap();
// Create a semantic node for use as prototype (FK)
crate::store::semantic::store_semantic_node(
&conn,
&NewSemanticNode {
content: "prototype node".to_string(),
node_type: SemanticType::Fact,
confidence: 0.9,
source_episodes: vec![],
embedding: None,
},
)
.unwrap();
// Create a category with that prototype
let cat_id =
crate::store::categories::store_category(&conn, "programming", NodeId(1), None, None)
.unwrap();
// Create a strong link from the episode to the category
// This ensures spreading activation includes the Category node
crate::graph::links::create_link(
&conn,
NodeRef::Episode(ep1),
NodeRef::Category(cat_id),
LinkType::MemberOf,
1.0,
)
.unwrap();
let results = execute_query(
&conn,
&Query {
text: "Rust programming".to_string(),
embedding: None,
context: QueryContext {
current_timestamp: Some(5000),
..Default::default()
},
max_results: 10,
category_id: None,
boost_categories: None,
boost_weights: None,
},
)
.unwrap();
// Category nodes should be filtered out — only episodes/semantic/preferences in results
for r in &results {
assert!(
!matches!(r.node, NodeRef::Category(_)),
"Category nodes should be filtered out of results"
);
}
}
// ---------------------------------------------------------------------------
// Tracing coverage — exercises trace!() macro format args in pipeline
// ---------------------------------------------------------------------------
#[cfg(feature = "tracing")]
#[test]
fn test_query_with_tracing_subscriber() {
// Initialize a subscriber at TRACE level so trace!() args are evaluated.
// try_init because other tests may have already set one.
let _ = tracing_subscriber::fmt()
.with_max_level(tracing::Level::TRACE)
.with_test_writer()
.try_init();
let conn = open_memory_db().unwrap();
episodic::store_episode(&conn, &ep("Rust memory safety", "s1", 1000)).unwrap();
let results = execute_query(
&conn,
&Query {
text: "Rust".to_string(),
embedding: Some(vec![0.1, 0.2, 0.3]),
context: QueryContext {
current_timestamp: Some(2000),
..Default::default()
},
max_results: 5,
category_id: None,
boost_categories: None,
boost_weights: None,
},
)
.unwrap();
assert!(results.len() <= 5);
}
}