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
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
//! # MenteDB: The Mind Database for AI Agents
//!
//! MenteDB is a purpose-built database engine for AI agent memory.
//! It's a cognition preparation engine that pre-digests knowledge
//! for single-pass transformer consumption.
//!
//! ## Core Concepts
//!
//! - **MemoryNode**: The atomic unit of knowledge (embeddings, graph, temporal, attributes)
//! - **MemoryEdge**: Typed, weighted relationships between memories
//! - **MemoryTier**: Cognitive inspired storage hierarchy (working, episodic, semantic, procedural, archival)
//! - **Context Assembly**: Token budget aware context building that respects attention patterns
//! - **MQL**: Mente Query Language for memory retrieval and manipulation
//!
//! ## Quick Start
//!
//! ```rust,no_run
//! use mentedb::prelude::*;
//! use mentedb::MenteDb;
//! use std::path::Path;
//!
//! let mut db = MenteDb::open(Path::new("./my-agent-memory")).unwrap();
//! // store, recall, relate, forget...
//! db.close().unwrap();
//! ```
//!
//! ## Feature Highlights
//!
//! - **Unified `process_turn`** pipeline: single call handles context retrieval,
//! pain signals, episodic storage, write inference, action detection, sentiment,
//! phantom tracking, trajectory, speculative caching, fact extraction, and
//! auto-maintenance (decay / archival / consolidation)
//! - Seven cognitive features: interference detection, pain signals, phantom tracking,
//! speculative caching, stream monitoring, trajectory tracking, write inference
//! - HNSW vector index with hybrid search (vector + tags + temporal + salience)
//! - CSR/CSC knowledge graph with belief propagation
//! - Token budget aware context assembly with attention curve optimization
//! - MQL query language with vector, tag, temporal, and graph traversal support
//! - WAL based crash recovery with LZ4 compressed pages
//!
//! ## Repository
//!
//! Source code: <https://github.com/nambok/mentedb>
use std::path::{Path, PathBuf};
use mentedb_cognitive::EntityResolver;
use mentedb_cognitive::interference::{InterferenceDetector, InterferencePair};
use mentedb_cognitive::llm::EntityMergeGroup;
use mentedb_cognitive::pain::{PainRegistry, PainSignal};
use mentedb_cognitive::phantom::{PhantomConfig, PhantomMemory, PhantomTracker};
use mentedb_cognitive::speculative::{CacheEntry, CacheStats, SpeculativeCache};
use mentedb_cognitive::stream::{CognitionStream, StreamAlert, StreamConfig};
use mentedb_cognitive::trajectory::{TrajectoryNode, TrajectoryTracker};
use mentedb_cognitive::write_inference::{
InferredAction, WriteInferenceConfig, WriteInferenceEngine,
};
use mentedb_consolidation::archival::{ArchivalConfig, ArchivalDecision, ArchivalPipeline};
use mentedb_consolidation::compression::{CompressedMemory, MemoryCompressor};
use mentedb_consolidation::consolidation::{ConsolidationCandidate, ConsolidationEngine};
use mentedb_consolidation::decay::{DecayConfig, DecayEngine};
use mentedb_context::{AssemblyConfig, ContextAssembler, ContextWindow, ScoredMemory};
use mentedb_core::edge::EdgeType;
use mentedb_core::error::MenteResult;
use mentedb_core::memory::MemoryType;
use mentedb_core::types::{AgentId, MemoryId, Timestamp, UserId};
use mentedb_core::{MemoryEdge, MemoryNode, MenteError};
use mentedb_embedding::provider::EmbeddingProvider;
use mentedb_graph::GraphManager;
use mentedb_index::IndexManager;
use mentedb_query::{Condition, Field, Filter, Mql, Operator, OrderBy, QueryPlan, Value};
use mentedb_storage::StorageEngine;
use parking_lot::RwLock;
use tracing::{debug, info, warn};
// Re-export sub-crates for direct access.
/// Engine version, derived from Cargo.toml at compile time.
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
/// Cognitive pipeline: speculative caching, trajectory tracking, inference.
pub use mentedb_cognitive as cognitive;
/// Consolidation, decay, and memory lifecycle management.
pub use mentedb_consolidation as consolidation;
/// Context assembly engine.
pub use mentedb_context as context;
/// Core types: MemoryNode, MemoryEdge, errors, config.
pub use mentedb_core as core;
/// Knowledge graph engine.
pub use mentedb_graph as graph;
/// Index structures for vector, tag, temporal, and salience search.
pub use mentedb_index as index;
/// MQL parser and query planner.
pub use mentedb_query as query;
/// Page based storage engine with WAL and buffer pool.
pub use mentedb_storage as storage;
/// Unified process_turn orchestration.
pub mod process_turn;
/// Engine-native injection attention (selection policy for context injection).
pub mod injection;
/// LLM-driven memory consolidation (semantic dedup via a pluggable LlmJudge).
pub mod llm_consolidation;
pub use llm_consolidation::ConsolidationParams;
/// Sleeptime enrichment pipeline (requires `enrichment` feature).
#[cfg(feature = "enrichment")]
pub mod enrichment;
/// Optional second-pass reranking of recall results (off by default).
pub mod reranker;
/// Maximal Marginal Relevance: an optional diversity pass over recall results
/// (off by default; enabled by `CognitiveConfig::mmr_lambda` below 1.0).
pub(crate) mod mmr;
/// Structured export: fill a JSON schema from memories via an embedder-supplied LLM.
pub mod export;
/// Lease-based elastic sharding: places each account on exactly one node and
/// coordinates ownership so a fleet can scale horizontally. The engine owns the
/// placement and coordination logic; the embedder supplies the lease and
/// membership storage via the `LeaseStore` and `NodeRegistry` traits.
pub mod sharding;
/// Commonly used types, re-exported for convenience.
pub mod prelude {
pub use mentedb_core::edge::EdgeType;
pub use mentedb_core::error::MenteResult;
pub use mentedb_core::memory::MemoryType;
pub use mentedb_core::types::*;
pub use mentedb_core::{MemoryEdge, MemoryNode, MemoryTier, MenteError};
pub use crate::MenteDb;
}
use mentedb_storage::PageId;
/// Mapping from MemoryId to the storage PageId where it lives.
use std::collections::HashMap;
/// Configuration for sleeptime enrichment pipeline.
///
/// Enrichment runs BETWEEN conversations, never in the hot path.
/// The engine tracks state and provides candidates; callers invoke
/// the async LLM pipeline when ready.
#[derive(Debug, Clone)]
pub struct EnrichmentConfig {
/// Whether enrichment is enabled. Default: false (opt-in).
pub enabled: bool,
/// Run enrichment after this many process_turn calls. Default: 50.
pub trigger_interval: u64,
/// Minimum confidence for extracted memories to be stored. Default: 0.6.
pub min_confidence: f32,
/// Maximum confidence for enrichment-generated memories. Default: 0.7.
pub max_enrichment_confidence: f32,
/// Whether to generate a user model summary. Default: false.
pub enable_user_model: bool,
/// Embedding similarity threshold to merge entities. Default: 0.7.
pub entity_merge_threshold: f32,
/// Embedding similarity below which entities are kept separate. Default: 0.4.
pub entity_separate_threshold: f32,
}
impl Default for EnrichmentConfig {
fn default() -> Self {
Self {
enabled: false,
trigger_interval: 50,
min_confidence: 0.6,
max_enrichment_confidence: 0.7,
enable_user_model: false,
entity_merge_threshold: 0.7,
entity_separate_threshold: 0.4,
}
}
}
/// Result of running the enrichment pipeline.
#[derive(Debug, Clone, Default)]
pub struct EnrichmentResult {
/// Number of new memories stored from extraction.
pub memories_stored: usize,
/// Number of entity nodes created or updated.
pub entities_processed: usize,
/// Number of edges created (Derived, Related, PartOf).
pub edges_created: usize,
/// Number of memories skipped as duplicates.
pub duplicates_skipped: usize,
/// Number of contradictions detected.
pub contradictions_found: usize,
/// Turn ID at which enrichment was completed.
pub completed_at_turn: u64,
/// Number of entity links created (Related edges between same-name entities).
pub entities_linked: usize,
/// Number of entity pairs left ambiguous (below merge threshold).
pub entities_ambiguous: usize,
}
/// Result of a single entity linking run.
#[derive(Debug, Clone, Default)]
pub struct EntityLinkResult {
/// Number of entity pairs linked with Related edges.
pub linked: usize,
/// Number of entity pairs tagged as ambiguous (MaybeRelated).
pub ambiguous: usize,
/// Number of edges created.
pub edges_created: usize,
}
/// Outcome of a standing-rules (`scope:always`) cleanup pass.
#[derive(Debug, Clone, Default)]
pub struct PruneReport {
/// Total `scope:always` memories found before cleanup.
pub total_always: usize,
/// Number of exact-content duplicate groups collapsed.
pub duplicate_groups: usize,
/// Exact-duplicate always-rules removed (the healthiest copy was kept).
pub pruned: Vec<MemoryId>,
/// Auto-pinned always-rules un-pinned (the `scope:always` tag removed, the
/// memory kept so it is still recalled by relevance).
pub unpinned: Vec<MemoryId>,
/// Standing rules demoted because the list exceeded `always_max` (lowest
/// salience first). Included in `unpinned`; counted separately for visibility.
pub capped: usize,
}
/// EMA smoothing window for the op-latency metrics:
/// `new = (cur * (N-1) + sample) / N`. Larger N is smoother and lets a one-off
/// spike (e.g. a cold path) decay more slowly. Metrics-only, not a cognitive
/// heuristic.
const LATENCY_EMA_WINDOW: u64 = 8;
/// A point-in-time snapshot of engine metrics for a `/metrics` exporter. Cheap to
/// take: counters are atomic loads and sizes are O(1) index/graph/page lookups, so
/// a scrape does not scan the corpus.
#[derive(Debug, Clone, Copy, Default)]
pub struct DbMetrics {
/// Memories stored on this node.
pub memory_count: u64,
/// Lifetime store operations (writes).
pub stores: u64,
/// Lifetime recall/query operations (reads).
pub recalls: u64,
/// EMA of recent store op latency, microseconds. 0 until the first store.
pub avg_store_latency_us: u64,
/// EMA of recent hybrid-search op latency, microseconds. 0 until the first
/// search.
pub avg_search_latency_us: u64,
/// Buffer-pool page cache hits.
pub buffer_pool_hits: u64,
/// Buffer-pool page cache misses (disk reads).
pub buffer_pool_misses: u64,
/// Buffer-pool CLOCK evictions.
pub buffer_pool_evictions: u64,
/// Frames currently holding a page.
pub buffer_pool_pages: u64,
/// On-disk data size in bytes (page_count * page size).
pub storage_bytes: u64,
/// Pages in the store.
pub page_count: u64,
/// Vectors in the HNSW index.
pub vector_index_size: u64,
/// Nodes in the memory graph.
pub graph_nodes: u64,
/// Pinned standing rules (scope:always).
pub standing_rules: u64,
}
/// A confirmed entity resolution from an external resolver (LLM).
///
/// Used to feed LLM entity resolution results back into the engine
/// so it can create graph edges and update the EntityResolver cache.
#[derive(Debug, Clone)]
pub struct EntityLinkResolution {
/// The canonical entity name decided by the resolver.
pub canonical: String,
/// All aliases that map to this canonical name.
pub aliases: Vec<String>,
/// Confidence in this resolution (0.0 to 1.0).
pub confidence: f32,
}
/// A pair of entity names that the LLM confirmed are DIFFERENT entities.
#[derive(Debug, Clone)]
pub struct EntitySeparation {
pub name_a: String,
pub name_b: String,
}
/// Configuration for the cognitive engine subsystems.
#[derive(Debug, Clone)]
pub struct CognitiveConfig {
/// Whether write inference (auto-edges, contradiction detection) is enabled on store.
pub write_inference: bool,
/// Whether salience decay is applied during retrieval.
pub decay_on_recall: bool,
/// Whether pain tracking is enabled.
pub pain_tracking: bool,
/// Whether interference detection is available.
pub interference_detection: bool,
/// Whether phantom tracking is enabled.
pub phantom_tracking: bool,
/// Whether speculative caching is enabled.
pub speculative_cache: bool,
/// Whether archival evaluation is available.
pub archival_evaluation: bool,
/// Configuration for the write inference engine.
pub inference_config: WriteInferenceConfig,
/// Configuration for the decay engine.
pub decay_config: DecayConfig,
/// Configuration for phantom tracking.
pub phantom_config: PhantomConfig,
/// Configuration for the archival pipeline.
pub archival_config: ArchivalConfig,
/// Configuration for the cognition stream.
pub stream_config: StreamConfig,
/// Configuration for sleeptime enrichment.
pub enrichment_config: EnrichmentConfig,
/// Similarity threshold for interference detection.
pub interference_threshold: f32,
/// Maximum trajectory turns to track.
pub trajectory_max_turns: usize,
/// Maximum speculative cache entries.
pub speculative_cache_size: usize,
/// Cosine similarity threshold for a query to hit a speculative cache entry.
pub speculative_embedding_threshold: f32,
/// Keyword-overlap threshold for a query to hit a speculative cache entry.
pub speculative_keyword_threshold: f32,
/// Recall relevance penalty for a memory from a DIFFERENT project than the
/// query's current project (0.0 = off, 1.0 = hard isolation). A weighted
/// signal, not a filter: a strongly-relevant cross-project memory still
/// surfaces, ranked below the current project's; global (un-projected) and
/// same-project memories are never penalized. Applied only when the caller
/// passes a current project (None = search across all projects).
pub project_scope_penalty: f32,
/// Candidate-pool multiplier for the optional hybrid-recall reranker: when a
/// reranker is installed, recall fetches `rerank_pool_factor * k` candidates
/// before reranking and truncating to k, so the reranker can promote a
/// relevant hit that the first pass ranked outside the top k. Ignored when no
/// reranker is installed. Minimum effective value is 1.
pub rerank_pool_factor: usize,
/// Diversity weight for the optional MMR pass on hybrid recall, in `[0, 1]`.
/// `1.0` (the default) disables it, ranking on relevance alone. Below 1.0,
/// recall greedily balances relevance against novelty so near-duplicate
/// memories do not all fill the context budget: `0.7` trims obvious
/// duplicates, `0.5` diversifies harder. Runs after any reranker, over the
/// same over-fetched pool, so it can pull a distinct memory into the top k.
pub mmr_lambda: f32,
/// Maximum pain signals to retain.
pub pain_max_warnings: usize,
/// Configuration for injection attention selection.
pub injection_config: injection::InjectionConfig,
/// How many hot flushes may pass before index and graph snapshots are
/// rewritten. Durability comes from the WAL checkpoint on every flush;
/// snapshots only accelerate reopen, and open reconciles stale ones, so
/// rewriting them per flush just multiplies fsync cost.
pub flush_snapshot_interval: u32,
/// Whether recall boosts memories linked (in the graph) to an entity named in
/// the query. Off by default: it only helps corpora that have run enrichment
/// (so entity nodes and their `Derived` edges exist), and its ranking effect
/// should be A/B measured before it is turned on broadly.
pub entity_boost_enabled: bool,
/// Score added to a candidate that a query-named entity links to. Applied on
/// top of the fused recall score, so it is calibrated against that scale.
pub entity_boost_weight: f32,
/// Whether storing a `scope:always` (standing rule) memory that is near
/// identical to an existing standing rule is skipped instead of inserted, so
/// re-pinning the same rule can never grow the always-list.
pub always_dedup: bool,
/// Cosine similarity at or above which a new standing rule counts as a
/// duplicate of an existing one.
pub always_dedup_threshold: f32,
/// Maximum standing rules (`scope:always`) to keep pinned. `prune_standing_rules`
/// demotes the excess (lowest salience first) to ordinary memories, and
/// injection never force-injects more than this, so the always-list, and the
/// prompt budget it consumes, stay bounded. The `user_profile` rule is always
/// kept and does not count against this.
pub always_max: usize,
/// Whether storing a regular memory that is a near-identical paraphrase of
/// an existing same-owner memory is skipped instead of inserted. Repeated
/// extraction of the same conversation produces reworded copies of one fact
/// ("User is building TaskPilot with Next.js" / "The user builds TaskPilot
/// using Next.js"); without this they pile up, dilute recall, and waste
/// context tokens. Two gates must BOTH pass, so a genuine value update
/// (high cosine but a changed value token) is never swallowed and still
/// reaches the write-inference supersession rule.
pub memory_dedup: bool,
/// Cosine similarity at or above which a regular memory can count as a
/// paraphrase duplicate (first gate).
pub memory_dedup_threshold: f32,
/// Minimum token-set jaccard overlap between the two contents (second
/// gate). Value updates share a frame but swap a value token, which drops
/// them below this and keeps them storable.
pub memory_dedup_token_overlap: f32,
}
impl Default for CognitiveConfig {
fn default() -> Self {
Self {
write_inference: true,
decay_on_recall: true,
pain_tracking: true,
interference_detection: true,
phantom_tracking: true,
speculative_cache: true,
archival_evaluation: true,
inference_config: WriteInferenceConfig::default(),
decay_config: DecayConfig::default(),
phantom_config: PhantomConfig::default(),
archival_config: ArchivalConfig::default(),
stream_config: StreamConfig::default(),
enrichment_config: EnrichmentConfig::default(),
interference_threshold: 0.8,
trajectory_max_turns: 100,
speculative_cache_size: 32,
speculative_embedding_threshold: 0.5,
speculative_keyword_threshold: 0.4,
project_scope_penalty: 0.5,
rerank_pool_factor: 4,
mmr_lambda: 1.0,
pain_max_warnings: 5,
injection_config: injection::InjectionConfig::default(),
flush_snapshot_interval: 8,
entity_boost_enabled: false,
entity_boost_weight: 0.15,
always_dedup: true,
always_dedup_threshold: 0.95,
always_max: 30,
memory_dedup: true,
memory_dedup_threshold: 0.97,
memory_dedup_token_overlap: 0.85,
}
}
}
/// The unified database facade for MenteDB.
///
/// `MenteDb` coordinates storage, indexing, graph relationships, query parsing,
/// context assembly, and cognitive subsystems into a single coherent API.
///
/// All internal state is protected by fine-grained locks, so every public method
/// takes `&self`. This allows `Arc<MenteDb>` to be shared across threads without
/// an external `RwLock`.
pub struct MenteDb {
storage: StorageEngine,
index: IndexManager,
graph: GraphManager,
/// Maps memory IDs to their storage page IDs for retrieval.
page_map: RwLock<HashMap<MemoryId, PageId>>,
/// Hot flushes since the last snapshot write; see flush_snapshot_interval.
flushes_since_snapshot: std::sync::atomic::AtomicU32,
/// Lifetime count of store operations (metrics).
stores: std::sync::atomic::AtomicU64,
/// Lifetime count of recall/query operations (metrics).
recalls: std::sync::atomic::AtomicU64,
/// EMA of store op latency, microseconds (metrics). 0 until the first store.
store_latency_us: std::sync::atomic::AtomicU64,
/// EMA of hybrid-search op latency, microseconds (metrics). 0 until the first
/// search.
search_latency_us: std::sync::atomic::AtomicU64,
/// Expected embedding dimension (0 = no validation).
embedding_dim: usize,
/// Database directory path for persistence.
path: PathBuf,
/// Optional embedding provider for auto-embedding on store and search.
embedder: Option<Box<dyn EmbeddingProvider>>,
/// Optional second-pass reranker applied to the fused candidate pool on the
/// hybrid recall path. None (the default) leaves recall a pure vector/BM25 +
/// decay ranking; when set, a larger pool is fetched and reordered by this
/// reranker before truncating to k. See [`reranker`](crate::reranker).
reranker: Option<Box<dyn crate::reranker::Reranker>>,
/// Cognitive engine configuration.
cognitive_config: CognitiveConfig,
/// Write inference engine for auto-edge creation and contradiction detection.
write_inference: WriteInferenceEngine,
/// Decay engine for salience management.
decay: DecayEngine,
/// Consolidation engine for memory merging.
consolidation: ConsolidationEngine,
/// Pain registry for tracking recurring failures.
pain: RwLock<PainRegistry>,
/// Trajectory tracker for conversation patterns.
trajectory: RwLock<TrajectoryTracker>,
/// Cognition stream for token-level monitoring.
stream: CognitionStream,
/// Phantom tracker for detecting referenced-but-missing knowledge.
phantom: RwLock<PhantomTracker>,
/// Speculative cache for pre-fetching likely-needed memories.
speculative: RwLock<SpeculativeCache>,
/// Interference detector for finding confusable memories.
interference: InterferenceDetector,
/// Entity resolver for canonical name resolution.
entity_resolver: RwLock<EntityResolver>,
/// Memory compressor for content summarization.
compressor: MemoryCompressor,
/// Archival pipeline for lifecycle evaluation.
archival: ArchivalPipeline,
/// Turn ID of the last completed enrichment cycle.
last_enrichment_turn: RwLock<u64>,
/// Whether enrichment is currently pending (set by maintenance trigger).
enrichment_pending: RwLock<bool>,
}
/// Agent visibility rule for scoped retrieval: a node is visible to an agent
/// when it is owned by that agent or owned by no agent (nil, shared
/// knowledge). No scope means global visibility.
pub(crate) fn agent_visible(owner: AgentId, scope: Option<AgentId>) -> bool {
match scope {
None => true,
Some(a) => owner == a || owner.is_nil(),
}
}
/// User visibility rule for scoped retrieval, orthogonal to [`agent_visible`]:
/// a node is visible to a user when it is owned by that user or owned by no
/// user (nil, shared knowledge). No scope means global visibility. A scoped
/// query at (user U, agent A) requires BOTH `user_visible(owner_user, U)` and
/// `agent_visible(owner_agent, A)`.
pub(crate) fn user_visible(owner: UserId, scope: Option<UserId>) -> bool {
match scope {
None => true,
Some(u) => owner == u || owner.is_nil(),
}
}
impl MenteDb {
/// Opens (or creates) a MenteDB instance at the given path.
pub fn open(path: &Path) -> MenteResult<Self> {
Self::open_with_config(path, CognitiveConfig::default())
}
/// Opens a MenteDB instance with custom cognitive configuration.
pub fn open_with_config(path: &Path, cognitive_config: CognitiveConfig) -> MenteResult<Self> {
info!("Opening MenteDB at {}", path.display());
let storage = StorageEngine::open(path)?;
let index_dir = path.join("indexes");
let graph_dir = path.join("graph");
let index = if index_dir.join("hnsw.bin").exists() || index_dir.join("hnsw.json").exists() {
debug!("Loading indexes from {}", index_dir.display());
IndexManager::load(&index_dir)?
} else {
IndexManager::default()
};
// Directory-backed graph: loads the snapshot and replays the edge log,
// so edges created since the last flush survive a crash.
let graph = GraphManager::open(&graph_dir)?;
// Rebuild page map by scanning all pages
let entries = storage.scan_all_memories();
let mut page_map = HashMap::new();
for (memory_id, page_id) in &entries {
page_map.insert(*memory_id, *page_id);
}
if !page_map.is_empty() {
info!(memories = page_map.len(), "rebuilt page map from storage");
}
// Every stored memory must be a graph node, even when the graph
// snapshot/log is missing or behind storage (e.g. after a crash);
// otherwise relate() and write inference fail for surviving memories.
for memory_id in page_map.keys() {
if !graph.read_graph().contains_node(*memory_id) {
graph.add_memory(*memory_id);
}
}
// Snapshots are written every few flushes, not every flush, so they
// can trail storage. Heal both directions: index memories the
// snapshot missed, and tombstone vectors whose pages are gone
// (forgotten after the last snapshot).
let mut reindexed = 0usize;
for (memory_id, page_id) in &page_map {
if !index.contains_vector(*memory_id)
&& let Ok(node) = storage.load_memory(*page_id)
&& !node.embedding.is_empty()
{
index.index_memory(&node);
reindexed += 1;
}
}
let mut retired = 0usize;
for id in index.vector_ids() {
if !page_map.contains_key(&id) {
index.remove_vector_only(id);
retired += 1;
}
}
if reindexed > 0 || retired > 0 {
info!(reindexed, retired, "reconciled index with storage");
}
let write_inference =
WriteInferenceEngine::with_config(cognitive_config.inference_config.clone());
let decay = DecayEngine::new(cognitive_config.decay_config.clone());
let consolidation = ConsolidationEngine::new();
let pain = RwLock::new(PainRegistry::new(cognitive_config.pain_max_warnings));
let trajectory = RwLock::new(TrajectoryTracker::new(
cognitive_config.trajectory_max_turns,
));
let stream = CognitionStream::with_config(cognitive_config.stream_config.clone());
let phantom = RwLock::new(PhantomTracker::new(cognitive_config.phantom_config.clone()));
let speculative = RwLock::new(SpeculativeCache::new(
cognitive_config.speculative_cache_size,
cognitive_config.speculative_embedding_threshold,
cognitive_config.speculative_keyword_threshold,
));
let interference = InterferenceDetector::new(cognitive_config.interference_threshold);
let entity_resolver = RwLock::new(EntityResolver::new());
let compressor = MemoryCompressor::new();
let archival = ArchivalPipeline::new(cognitive_config.archival_config.clone());
// Load persisted state for subsystems that support it.
let cognitive_dir = path.join("cognitive");
if cognitive_dir.exists() {
let _ = trajectory
.write()
.transitions
.load(&cognitive_dir.join("transitions.json"));
let _ = speculative
.write()
.load(&cognitive_dir.join("speculative.json"));
let _ = entity_resolver
.write()
.load(&cognitive_dir.join("entities.json"));
}
Ok(Self {
storage,
index,
graph,
page_map: RwLock::new(page_map),
flushes_since_snapshot: std::sync::atomic::AtomicU32::new(0),
stores: std::sync::atomic::AtomicU64::new(0),
recalls: std::sync::atomic::AtomicU64::new(0),
store_latency_us: std::sync::atomic::AtomicU64::new(0),
search_latency_us: std::sync::atomic::AtomicU64::new(0),
embedding_dim: 0,
path: path.to_path_buf(),
embedder: None,
reranker: None,
cognitive_config,
write_inference,
decay,
consolidation,
pain,
trajectory,
stream,
phantom,
speculative,
interference,
entity_resolver,
compressor,
archival,
last_enrichment_turn: RwLock::new(0),
enrichment_pending: RwLock::new(false),
})
}
/// Opens a MenteDB instance with a configured embedding provider.
pub fn open_with_embedder(
path: &Path,
embedder: Box<dyn EmbeddingProvider>,
) -> MenteResult<Self> {
let mut db = Self::open(path)?;
db.embedding_dim = embedder.dimensions();
db.embedder = Some(embedder);
Ok(db)
}
/// Opens a MenteDB instance with both embedder and cognitive config.
pub fn open_with_embedder_and_config(
path: &Path,
embedder: Box<dyn EmbeddingProvider>,
cognitive_config: CognitiveConfig,
) -> MenteResult<Self> {
let mut db = Self::open_with_config(path, cognitive_config)?;
db.embedding_dim = embedder.dimensions();
db.embedder = Some(embedder);
Ok(db)
}
/// Set the embedding provider after construction.
pub fn set_embedder(&mut self, embedder: Box<dyn EmbeddingProvider>) {
self.embedding_dim = embedder.dimensions();
self.embedder = Some(embedder);
}
/// Install a second-pass [`Reranker`](crate::reranker::Reranker) applied to
/// the fused candidate pool on the hybrid recall path. Once set, hybrid recall
/// over-fetches `rerank_pool_factor * k` candidates, reorders them by the
/// reranker over the query text, then truncates to k, so exact-term or
/// model-scored relevance can lift results that vector/BM25 ranking buried.
/// The engine ships a dependency-free [`LexicalReranker`](crate::reranker::LexicalReranker);
/// an embedder can supply a stronger model without the engine taking the dependency.
pub fn set_reranker(&mut self, reranker: Box<dyn crate::reranker::Reranker>) {
self.reranker = Some(reranker);
}
/// Remove any installed reranker, returning hybrid recall to its first-pass
/// vector/BM25 + decay ranking.
pub fn clear_reranker(&mut self) {
self.reranker = None;
}
/// Whether a second-pass reranker is currently installed.
pub fn has_reranker(&self) -> bool {
self.reranker.is_some()
}
/// Generate an embedding for the given text using the configured provider.
/// Returns None if no provider is configured.
pub fn embed_text(&self, text: &str) -> MenteResult<Option<Vec<f32>>> {
match &self.embedder {
Some(e) => Ok(Some(e.embed(text)?)),
None => Ok(None),
}
}
/// Stores a memory node into the database.
///
/// The node is persisted to storage, added to all indexes, and registered
/// in the graph for relationship traversal.
///
/// When cognitive features are enabled (the default), write inference
/// automatically runs to:
/// - Detect contradictions with existing memories
/// - Create relationship edges (Related, Supersedes, Contradicts)
/// - Invalidate superseded memories
/// - Propagate confidence changes through the graph
pub fn store(&self, node: MemoryNode) -> MenteResult<()> {
let started = std::time::Instant::now();
let id = node.id;
debug!("Storing memory {}", id);
// Validate embedding dimension when configured.
if self.embedding_dim > 0
&& !node.embedding.is_empty()
&& node.embedding.len() != self.embedding_dim
{
return Err(MenteError::EmbeddingDimensionMismatch {
got: node.embedding.len(),
expected: self.embedding_dim,
});
}
// Standing-rule dedup: re-pinning a rule that already exists must not grow
// the always-list. Only the rare scope:always write pays for the lookup.
if self.cognitive_config.always_dedup
&& !node.embedding.is_empty()
&& node.tags.iter().any(|t| t == "scope:always")
&& self.is_duplicate_standing_rule(&node)
{
debug!("scope:always dedup: {id} matches an existing standing rule, skipping insert");
return Ok(());
}
// Regular-memory paraphrase dedup: repeated extraction produces reworded
// copies of the same fact; skip the insert when a near-identical
// same-owner memory already exists. Both gates (embedding cosine AND
// token overlap) must pass, so a value update, which shares the frame
// but swaps a value token, stays storable and reaches the
// write-inference supersession rule instead.
if self.cognitive_config.memory_dedup
&& !node.embedding.is_empty()
&& !node.tags.iter().any(|t| t == "scope:always")
&& self.is_paraphrase_duplicate(&node)
{
debug!("memory dedup: {id} is a paraphrase of an existing memory, skipping insert");
return Ok(());
}
let page_id = self.storage.store_memory(&node)?;
self.page_map.write().insert(id, page_id);
self.index.index_memory(&node);
self.graph.add_memory(id);
self.stores
.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
// Run write inference to auto-create edges and detect contradictions.
if self.cognitive_config.write_inference {
self.run_write_inference(&node);
}
Self::record_latency(&self.store_latency_us, started.elapsed());
Ok(())
}
/// Whether `node` (a `scope:always` memory) duplicates an existing standing
/// rule, by exact content or embedding cosine at/above the dedup threshold.
/// Compared only against the standing-rule set (fetched directly from the tag
/// index), which the cap keeps small, so this stays cheap and exact rather
/// than relying on approximate recall ranking.
fn is_duplicate_standing_rule(&self, node: &MemoryNode) -> bool {
let threshold = self.cognitive_config.always_dedup_threshold;
for id in self.index.bitmap.query_tag("scope:always") {
if id == node.id {
continue;
}
let Ok(existing) = self.get_memory(id) else {
continue;
};
// Dedup only within the same owner: another user or agent pinning the
// same rule is a separate standing rule, and collapsing across owners
// would break isolation.
if existing.user_id != node.user_id || existing.agent_id != node.agent_id {
continue;
}
if existing.content.trim() == node.content.trim() {
return true;
}
if !existing.embedding.is_empty()
&& Self::cosine(&node.embedding, &existing.embedding) >= threshold
{
return true;
}
}
false
}
/// Whether `node` is a near-identical paraphrase of an existing memory with
/// the SAME owner on both axes. Candidates come from a small owner-scoped
/// vector search; a candidate counts only when embedding cosine reaches
/// `memory_dedup_threshold` AND token-set jaccard reaches
/// `memory_dedup_token_overlap`, the two-gate design that keeps genuine
/// value updates storable.
fn is_paraphrase_duplicate(&self, node: &MemoryNode) -> bool {
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_micros() as u64;
let Ok(candidates) = self.recall_hybrid_scoped_at_mode(
&node.embedding,
None,
5,
now,
None,
false,
None,
Some(node.agent_id),
Some(node.user_id),
None,
) else {
return false;
};
let new_tokens = Self::token_set(&node.content);
if new_tokens.is_empty() {
return false;
}
for (id, _) in candidates {
if id == node.id {
continue;
}
let Ok(existing) = self.get_memory(id) else {
continue;
};
// Exact-owner only: a shared/global memory must never absorb an
// owned one, or vice versa.
if existing.agent_id != node.agent_id || existing.user_id != node.user_id {
continue;
}
// Scope tags are part of a memory's identity: the same sentence
// scoped globally and scoped to a project are two memories, never
// duplicates of each other.
let existing_scopes: std::collections::BTreeSet<&str> = existing
.tags
.iter()
.filter(|t| t.starts_with("scope:"))
.map(|t| t.as_str())
.collect();
let node_scopes: std::collections::BTreeSet<&str> = node
.tags
.iter()
.filter(|t| t.starts_with("scope:"))
.map(|t| t.as_str())
.collect();
if existing_scopes != node_scopes {
continue;
}
if existing.embedding.is_empty()
|| Self::cosine(&node.embedding, &existing.embedding)
< self.cognitive_config.memory_dedup_threshold
{
continue;
}
let old_tokens = Self::token_set(&existing.content);
if old_tokens.is_empty() {
continue;
}
let inter = new_tokens.intersection(&old_tokens).count() as f32;
let union = new_tokens.union(&old_tokens).count() as f32;
if union > 0.0 && inter / union >= self.cognitive_config.memory_dedup_token_overlap {
return true;
}
}
false
}
/// Lowercased alphanumeric token set of a content string, for the
/// paraphrase-dedup overlap gate.
fn token_set(s: &str) -> std::collections::HashSet<String> {
s.split(|c: char| !c.is_alphanumeric())
.filter(|t| !t.is_empty())
.map(|t| t.to_lowercase())
.collect()
}
/// Cosine similarity of two equal-length vectors; 0 for mismatched or zero
/// vectors. Used for standing-rule dedup where a true similarity (not the
/// fused recall score) is needed.
fn cosine(a: &[f32], b: &[f32]) -> f32 {
if a.len() != b.len() || a.is_empty() {
return 0.0;
}
let (mut dot, mut na, mut nb) = (0.0f32, 0.0f32, 0.0f32);
for i in 0..a.len() {
dot += a[i] * b[i];
na += a[i] * a[i];
nb += b[i] * b[i];
}
if na == 0.0 || nb == 0.0 {
return 0.0;
}
dot / (na.sqrt() * nb.sqrt())
}
/// Store multiple memories in a single batch transaction.
///
/// Uses a single WAL lock for all writes, avoiding per-write overhead of
/// flock acquisition, header reload, and LSN scan. Significantly faster
/// for bulk inserts.
pub fn store_batch(&self, nodes: Vec<MemoryNode>) -> MenteResult<Vec<MemoryId>> {
// Validate all embeddings upfront
for node in &nodes {
if self.embedding_dim > 0
&& !node.embedding.is_empty()
&& node.embedding.len() != self.embedding_dim
{
return Err(MenteError::EmbeddingDimensionMismatch {
got: node.embedding.len(),
expected: self.embedding_dim,
});
}
}
let page_ids = self.storage.store_memory_batch(&nodes)?;
let mut ids = Vec::with_capacity(nodes.len());
let mut page_map = self.page_map.write();
for (node, page_id) in nodes.iter().zip(page_ids.iter()) {
page_map.insert(node.id, *page_id);
self.index.index_memory(node);
self.graph.add_memory(node.id);
ids.push(node.id);
}
drop(page_map);
self.stores
.fetch_add(ids.len() as u64, std::sync::atomic::Ordering::Relaxed);
// Batch inserts get the same auto-linking, contradiction detection,
// and invalidation as single stores.
if self.cognitive_config.write_inference {
for node in &nodes {
self.run_write_inference(node);
}
}
Ok(ids)
}
/// Recalls memories using an MQL query string.
///
/// Parses the query, builds an execution plan, runs it against the
/// appropriate indexes/graph, and assembles the results into a
/// token-budget-aware context window.
pub fn recall(&self, query: &str) -> MenteResult<ContextWindow> {
debug!("Recalling with query: {}", query);
self.recalls
.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
let plan = Mql::parse(query)?;
let scored = self.execute_plan(&plan)?;
let config = AssemblyConfig::default();
let window = ContextAssembler::assemble(scored, vec![], &config);
Ok(window)
}
/// Run an MQL query and return the scored matches directly, without assembling
/// a context window. This is the raw query primitive behind `recall` (which
/// additionally packs the results into a token-budgeted context), and it powers
/// the `mentedb` CLI and the admin query endpoint.
pub fn query(&self, mql: &str) -> MenteResult<Vec<ScoredMemory>> {
self.recalls
.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
let plan = Mql::parse(mql)?;
self.execute_plan(&plan)
}
/// Like [`recall`](Self::recall), but runs an optional second pass through a
/// [`Reranker`](crate::reranker::Reranker) before assembling the context
/// window. The reranker reorders the first-pass candidates by `query_text`
/// (typically the natural-language query behind the MQL), so exact-term or
/// model-scored relevance can lift results that pure vector/BM25 ranking
/// buried. Reranking is entirely opt-in: plain `recall` never invokes it.
pub fn recall_reranked(
&self,
query: &str,
query_text: &str,
reranker: &dyn crate::reranker::Reranker,
) -> MenteResult<ContextWindow> {
use crate::reranker::RerankCandidate;
let plan = Mql::parse(query)?;
let mut scored = self.execute_plan(&plan)?;
let candidates: Vec<RerankCandidate<'_>> = scored
.iter()
.map(|s| RerankCandidate {
id: s.memory.id,
content: &s.memory.content,
score: s.score,
})
.collect();
let new_scores: std::collections::HashMap<MemoryId, f32> = reranker
.rerank(query_text, &candidates)
.into_iter()
.collect();
for s in &mut scored {
if let Some(&ns) = new_scores.get(&s.memory.id) {
s.score = ns;
}
}
scored.sort_by(|a, b| b.score.total_cmp(&a.score));
let config = AssemblyConfig::default();
Ok(ContextAssembler::assemble(scored, vec![], &config))
}
/// Shortcut for vector similarity search.
///
/// Returns the top-k most similar memory IDs with their scores.
/// Memories that have been superseded, contradicted, or temporally
/// invalidated are automatically excluded from results.
pub fn recall_similar(&self, embedding: &[f32], k: usize) -> MenteResult<Vec<(MemoryId, f32)>> {
self.recall_similar_filtered(embedding, k, None, None)
}
/// Vector similarity search with optional tag and time range filters.
pub fn recall_similar_filtered(
&self,
embedding: &[f32],
k: usize,
tags: Option<&[&str]>,
time_range: Option<(Timestamp, Timestamp)>,
) -> MenteResult<Vec<(MemoryId, f32)>> {
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_micros() as u64;
self.recall_similar_filtered_at(embedding, k, now, tags, time_range)
}
/// Vector similarity search at a specific point in time.
///
/// Only returns memories that were temporally valid at the given timestamp.
/// Superseded/contradicted memories are excluded unless the edge itself
/// was not yet valid at that time.
pub fn recall_similar_at(
&self,
embedding: &[f32],
k: usize,
at: Timestamp,
) -> MenteResult<Vec<(MemoryId, f32)>> {
self.recall_similar_filtered_at(embedding, k, at, None, None)
}
/// Vector similarity search at a specific point in time with optional filters.
///
/// Only returns memories that were temporally valid at the given timestamp.
/// Superseded/contradicted memories are excluded unless the edge itself
/// was not yet valid at that time. Optionally filters by tags and time range.
pub fn recall_similar_filtered_at(
&self,
embedding: &[f32],
k: usize,
at: Timestamp,
tags: Option<&[&str]>,
time_range: Option<(Timestamp, Timestamp)>,
) -> MenteResult<Vec<(MemoryId, f32)>> {
self.recall_hybrid_at(embedding, None, k, at, tags, time_range, None)
}
/// Hybrid search combining vector similarity and BM25 keyword matching.
///
/// When `query_text` is provided, BM25 results are fused with vector
/// results via Reciprocal Rank Fusion (RRF) for better recall on
/// exact entity names, dates, and specific terms.
#[allow(clippy::too_many_arguments)]
pub fn recall_hybrid_at(
&self,
embedding: &[f32],
query_text: Option<&str>,
k: usize,
at: Timestamp,
tags: Option<&[&str]>,
time_range: Option<(Timestamp, Timestamp)>,
current_project: Option<&str>,
) -> MenteResult<Vec<(MemoryId, f32)>> {
self.recall_hybrid_at_mode(
embedding,
query_text,
k,
at,
tags,
false,
time_range,
current_project,
)
}
/// Hybrid recall with configurable tag mode (AND vs OR).
#[allow(clippy::too_many_arguments)]
pub fn recall_hybrid_at_mode(
&self,
embedding: &[f32],
query_text: Option<&str>,
k: usize,
at: Timestamp,
tags: Option<&[&str]>,
tags_or: bool,
time_range: Option<(Timestamp, Timestamp)>,
current_project: Option<&str>,
) -> MenteResult<Vec<(MemoryId, f32)>> {
self.recall_hybrid_scoped_at_mode(
embedding,
query_text,
k,
at,
tags,
tags_or,
time_range,
None,
None,
current_project,
)
}
/// Hybrid recall visible to one (user, agent) scope: a node passes only
/// when it is visible on BOTH axes, i.e. owned by `agent` or no agent AND
/// owned by `user` or no user (nil, shared knowledge). `agent: None` /
/// `user: None` recall globally on that axis, preserving single-owner
/// behavior. The two axes are orthogonal: agent scoping never widens or
/// narrows user scoping, and vice versa.
#[allow(clippy::too_many_arguments)]
pub fn recall_hybrid_scoped_at_mode(
&self,
embedding: &[f32],
query_text: Option<&str>,
k: usize,
at: Timestamp,
tags: Option<&[&str]>,
tags_or: bool,
time_range: Option<(Timestamp, Timestamp)>,
agent: Option<AgentId>,
user: Option<UserId>,
current_project: Option<&str>,
) -> MenteResult<Vec<(MemoryId, f32)>> {
let started = std::time::Instant::now();
debug!(
"Recall hybrid, k={}, at={}, bm25={}, tags_or={}",
k,
at,
query_text.is_some(),
tags_or
);
// Project scope weight: precompute the current project's scope tag and the
// retained fraction for cross-project memories. None (or empty) disables
// the weighting entirely, which is the "search across all projects"
// override.
let current_project_tag = current_project
.filter(|p| !p.is_empty())
.map(|p| format!("scope:project:{p}"));
let cross_project_keep = 1.0 - self.cognitive_config.project_scope_penalty;
// When a reranker or the MMR diversity pass is active, over-fetch a larger
// candidate pool so it can promote (or diversify in) a relevant hit the
// first pass ranked outside the top k; otherwise the usual 3x over-fetch
// covers results filtered out below.
let want_rerank =
self.reranker.is_some() && query_text.map(|q| !q.is_empty()).unwrap_or(false);
let want_mmr = self.cognitive_config.mmr_lambda < 1.0;
let fetch_k = if want_rerank || want_mmr {
k.saturating_mul(self.cognitive_config.rerank_pool_factor.max(1))
} else {
k * 3
};
let results = self.index.hybrid_search_with_query_mode(
embedding, query_text, tags, tags_or, time_range, fetch_k,
);
let graph = self.graph.graph();
let pm = self.page_map.read();
// (id, score, content for a reranker, embedding for the MMR pass); the
// last two are captured only when their stage is active.
type ScoredCandidate = (MemoryId, f32, Option<String>, Option<Vec<f32>>);
let mut scored: Vec<ScoredCandidate> = results
.into_iter()
.filter_map(|(id, raw_score)| {
// Exclude memories that an active Supersedes/Contradicts edge
// has invalidated.
let incoming = graph.incoming(id);
let has_active_supersede = incoming.iter().any(|(_, e)| {
(e.edge_type == EdgeType::Supersedes || e.edge_type == EdgeType::Contradicts)
&& e.is_valid_at(at)
});
if has_active_supersede {
return None;
}
// Without a page entry we cannot check validity or decay; keep
// the raw score rather than silently dropping the hit.
let Some(&page_id) = pm.get(&id) else {
return Some((id, raw_score, None, None));
};
let Ok(node) = self.storage.load_memory(page_id) else {
return Some((id, raw_score, None, None));
};
// Drop memories outside their validity window or not visible to
// this (user, agent) scope. Both owner axes must pass.
let visible =
agent_visible(node.agent_id, agent) && user_visible(node.user_id, user);
if !node.is_valid_at(at) || !visible {
return None;
}
// Decay at recall time: the index salience cache is frozen at
// insert, so recomputing decayed salience here is what actually
// lets decay affect ranking on this hot path. Blend 70%
// similarity with 30% freshly decayed salience, matching the
// MQL path, so old, unaccessed memories rank lower.
let score = if self.cognitive_config.decay_on_recall {
let decayed = self.decay.compute_decay(
node.salience,
node.created_at,
node.accessed_at,
node.access_count,
at,
);
raw_score * 0.7 + decayed * 0.3
} else {
raw_score
};
// Weight down a memory that belongs to a DIFFERENT project than
// the query's current project. Same-project and global (no
// scope:project tag) memories are unaffected, so cross-project
// recall still works when a memory is strongly relevant, it is
// just ranked below the current project's.
let score = match ¤t_project_tag {
Some(cpt)
if node
.tags
.iter()
.any(|t| t.starts_with("scope:project:") && t != cpt) =>
{
score * cross_project_keep
}
_ => score,
};
// Capture the content only when a reranker will consume it, and
// the embedding only when the MMR pass will, so the default path
// pays no extra allocation.
let content = if want_rerank {
Some(node.content.clone())
} else {
None
};
let embedding = if want_mmr && !node.embedding.is_empty() {
Some(node.embedding.clone())
} else {
None
};
Some((id, score, content, embedding))
})
.collect();
// Optional second pass: reorder the whole fetched pool by the installed
// reranker over the query text, before the sort cuts to k, so it can lift
// a candidate the first pass ranked outside the top k.
if want_rerank
&& let Some(reranker) = &self.reranker
&& let Some(qt) = query_text
{
use crate::reranker::RerankCandidate;
let new_scores: std::collections::HashMap<MemoryId, f32> = {
let cands: Vec<RerankCandidate<'_>> = scored
.iter()
.map(|(id, sc, content, _)| RerankCandidate {
id: *id,
content: content.as_deref().unwrap_or(""),
score: *sc,
})
.collect();
reranker.rerank(qt, &cands).into_iter().collect()
};
for (id, sc, _, _) in scored.iter_mut() {
if let Some(&ns) = new_scores.get(id) {
*sc = ns;
}
}
}
// Re-rank by the (decay-adjusted, then optionally reranked) score before
// cutting to k, so the ranking reorders results rather than relabeling them.
scored.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));
let scored: Vec<(MemoryId, f32)> = if want_mmr && scored.len() > 1 {
// Diversity pass: greedily pick k relevant-yet-distinct memories from
// the sorted pool, so near-duplicates do not all fill the budget.
let rel: Vec<f32> = scored.iter().map(|(_, sc, _, _)| *sc).collect();
let emb: Vec<Option<&[f32]>> = scored.iter().map(|(_, _, _, e)| e.as_deref()).collect();
mmr::mmr_select(&rel, &emb, self.cognitive_config.mmr_lambda, k)
.into_iter()
.map(|i| (scored[i].0, scored[i].1))
.collect()
} else {
scored.truncate(k);
scored.into_iter().map(|(id, sc, _, _)| (id, sc)).collect()
};
// Time only (do not increment recalls here): recall()/query() already
// count the read, and they reach this hybrid core via execute_plan, so
// counting here too would double-count them.
Self::record_latency(&self.search_latency_us, started.elapsed());
Ok(scored)
}
/// Multi-query search with Reciprocal Rank Fusion (RRF).
///
/// Runs multiple vector searches (one per embedding) and merges results
/// using RRF: score = Σ 1/(k + rank_i). This improves recall by matching
/// on different semantic aspects of a query.
/// When `query_texts` is provided, each search also runs BM25 matching.
pub fn recall_similar_multi(
&self,
embeddings: &[Vec<f32>],
k: usize,
tags: Option<&[&str]>,
time_range: Option<(Timestamp, Timestamp)>,
) -> MenteResult<Vec<(MemoryId, f32)>> {
self.recall_hybrid_multi(embeddings, None, k, tags, time_range)
}
/// Multi-query hybrid search with BM25 + vector fusion.
///
/// Each query text is searched via both BM25 and vector, then all results
/// are merged via RRF.
pub fn recall_hybrid_multi(
&self,
embeddings: &[Vec<f32>],
query_texts: Option<&[String]>,
k: usize,
tags: Option<&[&str]>,
time_range: Option<(Timestamp, Timestamp)>,
) -> MenteResult<Vec<(MemoryId, f32)>> {
self.recall_hybrid_multi_mode(embeddings, query_texts, k, tags, false, time_range)
}
/// Multi-query hybrid search with configurable tag mode.
pub fn recall_hybrid_multi_mode(
&self,
embeddings: &[Vec<f32>],
query_texts: Option<&[String]>,
k: usize,
tags: Option<&[&str]>,
tags_or: bool,
time_range: Option<(Timestamp, Timestamp)>,
) -> MenteResult<Vec<(MemoryId, f32)>> {
use std::collections::HashMap;
let rrf_k: f32 = 60.0;
let mut rrf_scores: HashMap<MemoryId, f32> = HashMap::new();
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_micros() as u64;
for (i, emb) in embeddings.iter().enumerate() {
let qt = query_texts.and_then(|texts| texts.get(i).map(|s| s.as_str()));
let results =
self.recall_hybrid_at_mode(emb, qt, k, now, tags, tags_or, time_range, None)?;
for (rank, (id, _score)) in results.iter().enumerate() {
*rrf_scores.entry(*id).or_insert(0.0) += 1.0 / (rrf_k + rank as f32);
}
}
let mut merged: Vec<(MemoryId, f32)> = rrf_scores.into_iter().collect();
merged.sort_unstable_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));
merged.truncate(k);
Ok(merged)
}
/// Invalidate a memory by setting its valid_until timestamp.
///
/// The memory remains in storage for historical queries but is excluded
/// from current recall results.
pub fn invalidate_memory(&self, id: MemoryId, at: Timestamp) -> MenteResult<()> {
debug!("Invalidating memory {} at {}", id, at);
let page_id = self
.page_map
.read()
.get(&id)
.copied()
.ok_or(MenteError::MemoryNotFound(id))?;
let mut node = self.storage.load_memory(page_id)?;
node.invalidate(at);
self.storage.update_memory(page_id, &node)?;
Ok(())
}
/// Adds a typed, weighted edge between two memories in the graph.
pub fn relate(&self, edge: MemoryEdge) -> MenteResult<()> {
debug!("Relating {} -> {}", edge.source, edge.target);
self.graph.add_relationship(&edge)?;
Ok(())
}
/// Retrieves a single memory by its ID.
pub fn get_memory(&self, id: MemoryId) -> MenteResult<MemoryNode> {
let page_id = self
.page_map
.read()
.get(&id)
.copied()
.ok_or(MenteError::MemoryNotFound(id))?;
self.storage.load_memory(page_id)
}
/// The supersession history of a memory: the chain of older versions this
/// fact replaced, newest predecessor first, walked over `Supersedes` edges
/// (source = newer, target = replaced) up to `max_depth` hops.
///
/// The engine keeps superseded memories bi-temporally but recall hides
/// them, which makes "what was my previous X" unanswerable downstream even
/// though the answer is stored. This surfaces it: callers can attach the
/// predecessors of a recalled fact ("phone number is 4321, previously
/// 1234") without re-deriving graph internals. Cycle-guarded; a memory
/// with no history returns an empty vec.
pub fn supersession_history(
&self,
id: MemoryId,
max_depth: usize,
) -> MenteResult<Vec<MemoryNode>> {
let graph = self.graph.graph();
let mut history = Vec::new();
let mut seen = std::collections::HashSet::from([id]);
let mut current = id;
for _ in 0..max_depth {
let mut next: Option<MemoryId> = None;
for (target, edge) in graph.outgoing(current) {
if edge.edge_type == EdgeType::Supersedes && !seen.contains(&target) {
next = Some(target);
break;
}
}
let Some(prev) = next else { break };
seen.insert(prev);
match self.get_memory(prev) {
Ok(node) => history.push(node),
// A dangling edge (predecessor forgotten) ends the chain.
Err(_) => break,
}
current = prev;
}
Ok(history)
}
/// Returns all memory IDs currently stored in the database.
pub fn memory_ids(&self) -> Vec<MemoryId> {
self.page_map.read().keys().copied().collect()
}
/// Returns the number of memories currently stored.
pub fn memory_count(&self) -> usize {
self.page_map.read().len()
}
/// Fold one op's latency into an exponentially-weighted moving average, in
/// microseconds. An EMA (not a lifetime mean) so a one-off slow op, e.g. right
/// after a cold open, decays out over the next few ops instead of permanently
/// skewing the number; it tracks recent, warm engine performance. Lock-free.
fn record_latency(ema: &std::sync::atomic::AtomicU64, elapsed: std::time::Duration) {
use std::sync::atomic::Ordering::Relaxed;
let sample = elapsed.as_micros() as u64;
let n = LATENCY_EMA_WINDOW;
let mut cur = ema.load(Relaxed);
loop {
let next = if cur == 0 {
sample
} else {
(cur * (n - 1) + sample) / n
};
match ema.compare_exchange_weak(cur, next, Relaxed, Relaxed) {
Ok(_) => break,
Err(actual) => cur = actual,
}
}
}
/// Snapshot the engine's operational metrics (writes, reads, cache, storage,
/// index, graph, standing rules) for a `/metrics` exporter. O(1), no scan.
pub fn metrics(&self) -> DbMetrics {
use std::sync::atomic::Ordering::Relaxed;
let bp = self.storage.buffer_stats();
let page_count = self.storage.page_count();
DbMetrics {
memory_count: self.memory_count() as u64,
stores: self.stores.load(Relaxed),
recalls: self.recalls.load(Relaxed),
avg_store_latency_us: self.store_latency_us.load(Relaxed),
avg_search_latency_us: self.search_latency_us.load(Relaxed),
buffer_pool_hits: bp.hits,
buffer_pool_misses: bp.misses,
buffer_pool_evictions: bp.evictions,
buffer_pool_pages: bp.resident_pages,
storage_bytes: page_count * mentedb_storage::PAGE_SIZE as u64,
page_count,
vector_index_size: self.index.hnsw.len() as u64,
graph_nodes: self.graph.graph().node_count() as u64,
standing_rules: self.count_standing_rules() as u64,
}
}
/// Accurate count of pinned standing rules. The tag index can over-count after
/// an un-pin re-indexed on an older engine (the tag was removed from the node
/// but lingered in the bitmap), so verify each candidate still carries the tag.
/// Cheap: it loads only the (few, capped) candidates, not the whole corpus.
fn count_standing_rules(&self) -> usize {
self.index
.bitmap
.query_tag("scope:always")
.into_iter()
.filter(|id| {
self.get_memory(*id)
.map(|n| n.tags.iter().any(|t| t == "scope:always"))
.unwrap_or(false)
})
.count()
}
/// A bounded, paginated page of stored memories for admin browsing, ordered by
/// id so pagination is stable. Only `limit` nodes are loaded from storage per
/// call (paginate over the id set first, then load), so it stays cheap even on
/// a large store. Optional filters narrow the loaded page by owning agent,
/// memory type, and a case-insensitive content substring. Returns the total
/// memory count (for the pager) alongside the page.
pub fn list_memories(
&self,
limit: usize,
offset: usize,
agent: Option<AgentId>,
memory_type: Option<MemoryType>,
content_query: Option<&str>,
) -> MenteResult<(usize, Vec<MemoryNode>)> {
let pm = self.page_map.read();
let total = pm.len();
let mut ids: Vec<MemoryId> = pm.keys().copied().collect();
ids.sort();
let needle = content_query.map(|q| q.to_lowercase());
let mut out = Vec::new();
for id in ids.into_iter().skip(offset).take(limit) {
if let Some(&page_id) = pm.get(&id)
&& let Ok(node) = self.storage.load_memory(page_id)
{
if agent.is_some_and(|a| node.agent_id != a) {
continue;
}
if memory_type.is_some_and(|t| node.memory_type != t) {
continue;
}
if let Some(n) = &needle
&& !node.content.to_lowercase().contains(n)
{
continue;
}
out.push(node);
}
}
Ok((total, out))
}
/// Removes a memory from storage, indexes, and the graph.
pub fn forget(&self, id: MemoryId) -> MenteResult<()> {
debug!("Forgetting memory {}", id);
if let Some(&page_id) = self.page_map.read().get(&id) {
if let Ok(node) = self.storage.load_memory(page_id) {
self.index.remove_memory(id, &node);
}
// Durable delete: WAL-logged page free, so the memory does not
// resurrect when the page map is rebuilt on reopen.
self.storage.delete_memory(page_id)?;
}
self.graph.remove_memory(id);
self.page_map.write().remove(&id);
Ok(())
}
/// Clean up the standing-rules (`scope:always`) set: remove exact-content
/// duplicates (keeping the healthiest copy) and un-pin every auto-pinned
/// always-rule, keeping only rules the user explicitly pinned
/// (`source:manual`) and the user profile (which the engine pins deliberately
/// so it injects every session).
///
/// `scope:always` force-injects a memory into every assembled context
/// regardless of relevance, so the set must stay tiny; distilled facts belong
/// in relevance-based recall, not a fixed always-list. This is the single
/// source of truth for the policy, shared by the hosted platform, the local
/// daemon, and any SDK consumer, so behavior cannot drift between them.
pub fn prune_standing_rules(&self) -> MenteResult<PruneReport> {
use std::collections::{HashMap, HashSet};
let always: Vec<MemoryNode> = self
.memory_ids()
.into_iter()
.filter_map(|id| self.get_memory(id).ok())
.filter(|n| n.tags.iter().any(|t| t == "scope:always"))
.collect();
let mut report = PruneReport {
total_always: always.len(),
..Default::default()
};
// Collapse exact-content duplicates: keep the healthiest copy, forget
// the rest.
let mut by_content: HashMap<&str, Vec<&MemoryNode>> = HashMap::new();
for n in &always {
by_content.entry(n.content.as_str()).or_default().push(n);
}
let mut prune_ids: HashSet<MemoryId> = HashSet::new();
for (_content, mut group) in by_content {
if group.len() > 1 {
report.duplicate_groups += 1;
group.sort_by(|a, b| {
b.salience
.partial_cmp(&a.salience)
.unwrap_or(std::cmp::Ordering::Equal)
});
for n in group.into_iter().skip(1) {
prune_ids.insert(n.id);
}
}
}
// Un-pin every auto-pinned always-rule (not source:manual, not the
// profile). Keep the memory, drop the pin.
let mut to_unpin: Vec<MemoryNode> = Vec::new();
for n in &always {
if prune_ids.contains(&n.id) {
continue;
}
let is_manual = n.tags.iter().any(|t| t == "source:manual");
let is_profile = n.tags.iter().any(|t| t == "user_profile");
if !is_manual && !is_profile {
to_unpin.push(n.clone());
}
}
// Cap the count. Whatever survives dedup and un-pinning is manual pins
// plus the profile; the profile is always kept and does not count. If the
// remaining manual pins exceed always_max, demote the lowest-salience
// excess to ordinary memories so the always-list stays bounded even when a
// user has pinned hundreds of rules by hand.
let unpin_ids: HashSet<MemoryId> = to_unpin.iter().map(|n| n.id).collect();
let mut survivors: Vec<&MemoryNode> = always
.iter()
.filter(|n| !prune_ids.contains(&n.id) && !unpin_ids.contains(&n.id))
.filter(|n| !n.tags.iter().any(|t| t == "user_profile"))
.collect();
if survivors.len() > self.cognitive_config.always_max {
survivors.sort_by(|a, b| {
b.salience
.partial_cmp(&a.salience)
.unwrap_or(std::cmp::Ordering::Equal)
});
for n in survivors.into_iter().skip(self.cognitive_config.always_max) {
report.capped += 1;
to_unpin.push(n.clone());
}
}
for id in prune_ids {
self.forget(id)?;
report.pruned.push(id);
}
for mut n in to_unpin {
n.tags.retain(|t| t != "scope:always");
let id = n.id;
self.store(n)?;
report.unpinned.push(id);
}
Ok(report)
}
/// Returns a reference to the underlying graph manager.
pub fn graph(&self) -> &GraphManager {
&self.graph
}
/// Returns a mutable reference to the underlying graph manager.
#[deprecated(note = "GraphManager now uses interior mutability; use graph() instead")]
pub fn graph_mut(&mut self) -> &mut GraphManager {
&mut self.graph
}
/// Returns a reference to the cognitive configuration.
pub fn cognitive_config(&self) -> &CognitiveConfig {
&self.cognitive_config
}
// -----------------------------------------------------------------------
// Cognitive Engine: Write Inference
// -----------------------------------------------------------------------
/// Run write inference on a newly stored memory.
///
/// Finds semantically similar existing memories, runs the inference engine
/// to detect contradictions and relationships, then applies the actions
/// (creating edges, invalidating superseded memories, etc.).
fn run_write_inference(&self, new_memory: &MemoryNode) {
// Find candidate memories to compare against via vector search.
// We load a small set of the most similar memories.
let candidates = if !new_memory.embedding.is_empty() {
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_micros() as u64;
// Scope to the new memory's owner on BOTH axes: contradiction and
// relationship inference must never compare against another user's
// or another agent's memories. Nil owned (shared/global) memories
// stay in scope.
self.recall_hybrid_scoped_at_mode(
&new_memory.embedding,
None,
20,
now,
None,
false,
None,
Some(new_memory.agent_id),
Some(new_memory.user_id),
None,
)
.unwrap_or_default()
} else {
vec![]
};
if candidates.is_empty() {
return;
}
// Load the actual MemoryNode data for each candidate.
let pm = self.page_map.read();
let existing: Vec<MemoryNode> = candidates
.iter()
.filter(|(id, _)| *id != new_memory.id)
.filter_map(|(id, _)| {
pm.get(id)
.and_then(|&pid| self.storage.load_memory(pid).ok())
})
.collect();
drop(pm);
if existing.is_empty() {
return;
}
let actions = self
.write_inference
.infer_on_write(new_memory, &existing, &[]);
let action_count = actions.len();
for action in actions {
if let Err(e) = self.apply_inferred_action(action) {
warn!("Failed to apply inferred action: {}", e);
}
}
if action_count > 0 {
debug!(
"Write inference for {} produced {} actions",
new_memory.id, action_count
);
}
}
/// Apply a single inferred action from the write inference engine.
fn apply_inferred_action(&self, action: InferredAction) -> MenteResult<()> {
match action {
InferredAction::CreateEdge {
source,
target,
edge_type,
weight,
} => {
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_micros() as u64;
let edge = MemoryEdge {
source,
target,
edge_type,
weight,
created_at: now,
valid_from: None,
valid_until: None,
label: None,
};
debug!(
"Auto-creating {:?} edge {} -> {}",
edge_type, source, target
);
self.graph.add_relationship(&edge)?;
}
InferredAction::InvalidateMemory {
memory,
superseded_by,
valid_until,
} => {
debug!(
"Invalidating memory {} (superseded by {})",
memory, superseded_by
);
self.invalidate_memory(memory, valid_until)?;
// Also create the Supersedes edge.
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_micros() as u64;
let edge = MemoryEdge {
source: superseded_by,
target: memory,
edge_type: EdgeType::Supersedes,
weight: 1.0,
created_at: now,
valid_from: None,
valid_until: None,
label: None,
};
self.graph.add_relationship(&edge)?;
}
InferredAction::DeduplicateExact { duplicate, keeper } => {
debug!("Deduplicating exact copy {duplicate} into {keeper}");
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_micros() as u64;
self.invalidate_memory(duplicate, now)?;
// Derived edge (dedup lineage), NOT Supersedes: an exact copy is
// deduplicated, not a meaningful supersession, so it must not
// surface in the contradiction/supersession view.
let edge = MemoryEdge {
source: keeper,
target: duplicate,
edge_type: EdgeType::Derived,
weight: 1.0,
created_at: now,
valid_from: None,
valid_until: None,
label: None,
};
self.graph.add_relationship(&edge)?;
}
InferredAction::MarkObsolete {
memory,
superseded_by,
} => {
debug!(
"Marking {} obsolete (superseded by {})",
memory, superseded_by
);
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_micros() as u64;
self.invalidate_memory(memory, now)?;
let edge = MemoryEdge {
source: superseded_by,
target: memory,
edge_type: EdgeType::Supersedes,
weight: 1.0,
created_at: now,
valid_from: None,
valid_until: None,
label: None,
};
self.graph.add_relationship(&edge)?;
}
InferredAction::FlagContradiction {
existing,
new,
reason,
} => {
debug!(
"Contradiction detected: {} vs {} — {}",
existing, new, reason
);
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_micros() as u64;
let edge = MemoryEdge {
source: new,
target: existing,
edge_type: EdgeType::Contradicts,
weight: 1.0,
created_at: now,
valid_from: None,
valid_until: None,
label: Some(reason),
};
self.graph.add_relationship(&edge)?;
}
InferredAction::UpdateConfidence {
memory,
new_confidence,
} => {
debug!("Updating confidence for {} to {}", memory, new_confidence);
if let Ok(mut node) = self.get_memory(memory) {
node.confidence = new_confidence;
if let Some(&pid) = self.page_map.read().get(&memory) {
self.storage.update_memory(pid, &node)?;
}
}
}
InferredAction::PropagateBeliefChange { root, delta } => {
debug!("Propagating belief change from {} (delta={})", root, delta);
if let Ok(node) = self.get_memory(root) {
let new_confidence = (node.confidence + delta).clamp(0.0, 1.0);
let affected = self.graph.propagate_belief_change(root, new_confidence);
for (affected_id, new_conf) in affected {
if let Ok(mut affected_node) = self.get_memory(affected_id) {
affected_node.confidence = new_conf;
if let Some(&pid) = self.page_map.read().get(&affected_id)
&& let Err(e) = self.storage.update_memory(pid, &affected_node)
{
warn!("Failed to persist belief update for {affected_id}: {e}");
}
}
}
}
}
InferredAction::UpdateContent {
memory,
new_content,
reason,
} => {
debug!("Updating content of {}: {}", memory, reason);
if let Ok(mut node) = self.get_memory(memory) {
node.content = new_content;
if let Some(&pid) = self.page_map.read().get(&memory) {
self.storage.update_memory(pid, &node)?;
}
self.index.remove_memory(memory, &node);
self.index.index_memory(&node);
}
}
}
Ok(())
}
// -----------------------------------------------------------------------
// Cognitive Engine: Salience Decay
// -----------------------------------------------------------------------
/// Apply salience decay to a batch of memories in-place.
///
/// Call this during retrieval to ensure scores reflect temporal relevance,
/// or periodically to maintain salience accuracy across the database.
pub fn apply_decay(&self, memories: &mut [MemoryNode]) {
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_micros() as u64;
self.decay.apply_decay_batch(memories, now);
}
/// Compute the decayed salience for a single memory at the current time.
pub fn compute_decayed_salience(&self, memory: &MemoryNode) -> f32 {
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_micros() as u64;
self.decay.compute_decay(
memory.salience,
memory.created_at,
memory.accessed_at,
memory.access_count,
now,
)
}
/// Retained for API compatibility. Does nothing and persists nothing.
///
/// Salience decay is derived on read, not materialized. The stored
/// `salience` field is the base value as of a memory's last reinforcement
/// (`accessed_at`); the current, decayed strength is computed on demand via
/// `compute_decayed_salience` (used by recall scoring and archival).
///
/// This method previously recomputed decay from the stored salience and
/// wrote the result back. That fed each pass's output in as the next pass's
/// input, so the effective decay rate scaled with how often maintenance ran
/// rather than with elapsed time: frequent passes aged and forgot memories
/// far faster than the configured half-life. Deriving on read removes that
/// coupling entirely, so it is safe to run maintenance at any cadence.
pub fn apply_decay_global(&self) -> MenteResult<usize> {
Ok(0)
}
/// One-time repair for databases whose stored salience was corrupted by the
/// old compounding decay pass, which overwrote each memory's base salience
/// with a repeatedly re-decayed value. Reset every memory's salience to the
/// configured maximum and restart its decay clock (`accessed_at = now`), in
/// place, without re-running write inference, so every surviving memory gets
/// a fresh, correct decay lease from now. Access counts and every other field
/// are preserved. Returns the number of memories reset.
///
/// This is idempotent in effect (running it twice just resets an already
/// healthy base again) and safe to gate behind a one-shot migration flag.
pub fn reset_decay_state(&self) -> MenteResult<usize> {
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_micros() as u64;
let max_salience = self.decay.config.max_salience;
let page_ids: Vec<PageId> = self.page_map.read().values().copied().collect();
let mut reset = 0;
for pid in &page_ids {
if let Ok(mut node) = self.storage.load_memory(*pid) {
node.salience = max_salience;
node.accessed_at = now;
self.storage.update_memory(*pid, &node)?;
reset += 1;
}
}
if reset > 0 {
info!("Reset decay state for {} memories", reset);
}
Ok(reset)
}
/// Re-embed every memory at the embedder's current dimension and reindex it.
///
/// This migrates a database whose stored vectors were produced by an
/// embedder of a different dimension (for example 256 to 1024): each memory's
/// content is re-embedded with the currently configured embedder, its stored
/// vector is replaced, and it is removed from and re-added to the vector
/// index. Memories already at the current dimension are skipped, so it is
/// idempotent and safe to re-run.
///
/// The migration is safe to run on a live database: the HNSW index checks
/// vector dimension at query time, so a memory that has not been re-embedded
/// yet is simply skipped by recall (never returned, never crashing) until
/// this reaches it. Runs synchronously and re-embeds one memory at a time;
/// the caller should run it on a blocking pool.
pub fn reembed_all(&self) -> MenteResult<usize> {
let target = self.embedding_dim;
if target == 0 {
return Ok(0);
}
let page_ids: Vec<PageId> = self.page_map.read().values().copied().collect();
let mut reembedded = 0usize;
for pid in &page_ids {
let Ok(mut node) = self.storage.load_memory(*pid) else {
continue;
};
// Already at the target dimension: nothing to do.
let text = node.indexed_text();
if node.embedding.len() == target || text.is_empty() {
continue;
}
let Some(embedding) = self.embed_text(&text)? else {
continue;
};
// The embedder is not yet producing the target dimension; leave the
// memory as-is rather than store a second wrong-dimension vector.
if embedding.len() != target {
continue;
}
self.index.remove_vector_only(node.id);
node.embedding = embedding;
self.storage.update_memory(*pid, &node)?;
self.index.index_memory(&node);
reembedded += 1;
}
if reembedded > 0 {
info!("Re-embedded {reembedded} memories at dimension {target}");
}
Ok(reembedded)
}
// -----------------------------------------------------------------------
// Cognitive Engine: Consolidation
// -----------------------------------------------------------------------
/// Find groups of similar memories that are candidates for consolidation.
///
/// Returns clusters of memories that share high semantic similarity and
/// could be merged into unified knowledge.
pub fn find_consolidation_candidates(
&self,
min_cluster_size: usize,
similarity_threshold: f32,
) -> MenteResult<Vec<ConsolidationCandidate>> {
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_micros() as u64;
// Load all memories eligible for consolidation.
let pm = self.page_map.read();
let eligible: Vec<MemoryNode> = pm
.values()
.filter_map(|pid| self.storage.load_memory(*pid).ok())
.filter(|node| ConsolidationEngine::should_consolidate(node, now))
.collect();
drop(pm);
if eligible.is_empty() {
return Ok(vec![]);
}
Ok(self
.consolidation
.find_candidates(&eligible, min_cluster_size, similarity_threshold))
}
/// Consolidate a cluster of memories into a single merged memory.
///
/// The source memories are invalidated (not deleted) and a new consolidated
/// semantic memory is stored with Derived edges back to the sources.
pub fn consolidate_cluster(&self, memory_ids: &[MemoryId]) -> MenteResult<MemoryId> {
let pm = self.page_map.read();
let cluster: Vec<MemoryNode> = memory_ids
.iter()
.filter_map(|id| {
pm.get(id)
.and_then(|&pid| self.storage.load_memory(pid).ok())
})
.collect();
drop(pm);
if cluster.len() < 2 {
return Err(MenteError::Query(
"consolidation requires at least 2 memories".into(),
));
}
// Never merge across owners. find_candidates already groups by both
// owner axes, but if a caller hands us a hand-built mixed cluster,
// refuse rather than leak one owner's content into another's
// consolidated memory. Both axes are checked independently.
let owner_agent = cluster[0].agent_id;
if cluster.iter().any(|m| m.agent_id != owner_agent) {
return Err(MenteError::Query(
"consolidation cluster mixes agents; refusing to merge across owners".into(),
));
}
let owner_user = cluster[0].user_id;
if cluster.iter().any(|m| m.user_id != owner_user) {
return Err(MenteError::Query(
"consolidation cluster mixes users; refusing to merge across owners".into(),
));
}
let result = self.consolidation.consolidate(&cluster);
// Create the consolidated memory node, stamped with BOTH owner axes of
// the cluster so the derived memory stays scoped exactly as its sources.
let agent_id = cluster[0].agent_id;
let user_id = cluster[0].user_id;
let mut consolidated = MemoryNode::new(
agent_id,
result.new_type,
result.summary,
result.combined_embedding,
)
.with_user_id(user_id);
consolidated.confidence = result.combined_confidence;
let consolidated_id = consolidated.id;
self.store(consolidated)?;
// Invalidate source memories and create Derived edges.
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_micros() as u64;
for source_id in &result.source_memories {
let _ = self.invalidate_memory(*source_id, now);
let edge = MemoryEdge {
source: consolidated_id,
target: *source_id,
edge_type: EdgeType::Derived,
weight: 1.0,
created_at: now,
valid_from: None,
valid_until: None,
label: None,
};
let _ = self.graph.add_relationship(&edge);
}
info!(
"Consolidated {} memories into {}",
result.source_memories.len(),
consolidated_id
);
Ok(consolidated_id)
}
/// Flushes all data and closes the database.
pub fn close(&self) -> MenteResult<()> {
info!("Closing MenteDB");
self.flush_full()?;
self.storage.close()?;
Ok(())
}
/// Close with durability only: checkpoint the WAL and release the
/// process lock without rewriting snapshots. Reopen reconciles stale
/// snapshots, so this trades a slower next open for a shutdown fast
/// enough to release every user's lock inside a deploy drain window.
pub fn close_quick(&self) -> MenteResult<()> {
info!("Quick closing MenteDB");
self.storage.checkpoint()?;
self.storage.close()?;
Ok(())
}
/// Simulate a process crash for tests: releases the storage process lock
/// exactly as the operating system would when a process dies, then drops
/// the instance without flushing or closing anything.
#[doc(hidden)]
pub fn simulate_crash(self) {
self.storage.release_process_lock();
std::mem::forget(self);
}
/// Rebuild all indexes by scanning every memory in storage.
///
/// Use this after index corruption or when index files were overwritten.
/// Returns the number of memories re-indexed.
pub fn rebuild_indexes(&self) -> MenteResult<usize> {
info!("Rebuilding indexes from storage...");
let ids: Vec<MemoryId> = self.page_map.read().keys().copied().collect();
let total = ids.len();
let mut indexed = 0usize;
for id in ids {
if let Ok(node) = self.get_memory(id) {
self.index.index_memory(&node);
indexed += 1;
}
}
self.index.save(&self.path.join("indexes"))?;
info!(indexed, total, "index rebuild complete");
Ok(indexed)
}
/// Flush indexes, graph, and storage to disk without closing.
///
/// Call this periodically to ensure cross-session persistence.
/// Unlike `close()`, the database remains usable after flushing.
pub fn flush(&self) -> MenteResult<()> {
debug!("Flushing MenteDB to disk");
// Durability is the WAL checkpoint, paid on every flush. Index,
// graph, and cognitive snapshots only accelerate reopen (open
// reconciles stale ones against storage), so they are amortized
// across flush_snapshot_interval hot flushes instead of multiplying
// fsync cost on every write batch.
self.storage.checkpoint()?;
let n = self
.flushes_since_snapshot
.fetch_add(1, std::sync::atomic::Ordering::Relaxed)
+ 1;
if n >= self.cognitive_config.flush_snapshot_interval {
self.write_snapshots()?;
}
Ok(())
}
/// Flush everything including index, graph, and cognitive snapshots.
/// Used by close and by maintenance, where reopen speed matters more
/// than write latency.
pub fn flush_full(&self) -> MenteResult<()> {
debug!("Full flush of MenteDB to disk");
self.storage.checkpoint()?;
self.write_snapshots()
}
fn write_snapshots(&self) -> MenteResult<()> {
self.index.save(&self.path.join("indexes"))?;
self.graph.save(&self.path.join("graph"))?;
// Persist cognitive subsystem state.
let cognitive_dir = self.path.join("cognitive");
if std::fs::create_dir_all(&cognitive_dir).is_ok() {
let _ = self
.trajectory
.read()
.transitions
.save(&cognitive_dir.join("transitions.json"), 1);
let _ = self
.speculative
.read()
.save(&cognitive_dir.join("speculative.json"), 0);
let _ = self
.entity_resolver
.read()
.save(&cognitive_dir.join("entities.json"));
}
self.flushes_since_snapshot
.store(0, std::sync::atomic::Ordering::Relaxed);
Ok(())
}
/// Executes a query plan against the indexes and graph, returning scored memories.
fn execute_plan(&self, plan: &QueryPlan) -> MenteResult<Vec<ScoredMemory>> {
match plan {
QueryPlan::VectorSearch {
query,
k,
filters,
condition,
order_by,
} => {
// `content ~> "text"` arrives with an empty vector and the
// SimilarTo filter left in place for us to embed now (no embedder
// at plan time). NEAR arrives with the vector already filled in.
let embedded;
let qvec: &[f32] = if query.is_empty() {
match filters
.iter()
.find(|f| f.op == Operator::SimilarTo)
.map(|f| &f.value)
{
Some(Value::Text(text)) => {
match self.embedder.as_ref().and_then(|e| e.embed(text).ok()) {
Some(v) => {
embedded = v;
&embedded
}
// No embedder configured: a semantic query can't run.
None => return Ok(vec![]),
}
}
_ => query,
}
} else {
query
};
let hits = self.index.hybrid_search(qvec, None, None, *k);
let mut scored = self.load_scored_memories(&hits)?;
// The SimilarTo filter is the query itself; apply any others. A
// boolean tree (NEAR combined with OR/NOT) takes over the whole
// post-filter when present.
match condition {
Some(cond) => {
scored.retain(|sm| Self::condition_matches(cond, &sm.memory));
}
None => {
scored.retain(|sm| {
filters
.iter()
.filter(|f| f.op != Operator::SimilarTo)
.all(|f| Self::filter_matches(f, &sm.memory))
});
}
}
// Optional entity leg: boost candidates linked to an entity the
// query names. The SimilarTo filter carries the query text.
if self.cognitive_config.entity_boost_enabled
&& let Some(qtext) = filters
.iter()
.find(|f| f.op == Operator::SimilarTo)
.and_then(|f| match &f.value {
Value::Text(t) => Some(t.clone()),
_ => None,
})
{
self.apply_entity_boost(&mut scored, &qtext);
}
if let Some(ob) = order_by {
Self::apply_order_by(&mut scored, ob);
}
Ok(scored)
}
QueryPlan::TagScan {
tags,
filters,
limit,
condition,
order_by,
} => {
let k = limit.unwrap_or(20);
let mut scored = if tags.is_empty() {
// No tag constraint: a bare RECALL, or a metadata-only filter
// like `WHERE type = semantic`. Full-scan and let the filters
// below narrow it, instead of returning nothing.
let pm = self.page_map.read();
pm.values()
.filter_map(|&pid| self.storage.load_memory(pid).ok())
.map(|memory| ScoredMemory { memory, score: 1.0 })
.collect::<Vec<_>>()
} else {
let tag_refs: Vec<&str> = tags.iter().map(|s| s.as_str()).collect();
// Zero-vector for tag-only search; salience+bitmap still apply.
let hits = self.index.hybrid_search(&[], Some(&tag_refs), None, k);
self.load_scored_memories(&hits)?
};
// Apply the WHERE clause: a boolean tree when present (OR/NOT/
// grouping), otherwise the flat AND of metadata filters.
match condition {
Some(cond) => {
scored.retain(|sm| Self::condition_matches(cond, &sm.memory));
}
None => {
scored.retain(|sm| {
filters.iter().all(|f| Self::filter_matches(f, &sm.memory))
});
}
}
// ORDER BY sorts before LIMIT so the top-k reflects the requested
// order, not relevance.
if let Some(ob) = order_by {
Self::apply_order_by(&mut scored, ob);
}
scored.truncate(k);
Ok(scored)
}
QueryPlan::TemporalScan {
start,
end,
order_by,
..
} => {
let hits = self
.index
.hybrid_search(&[], None, Some((*start, *end)), 100);
let mut scored = self.load_scored_memories(&hits)?;
if let Some(ob) = order_by {
Self::apply_order_by(&mut scored, ob);
}
Ok(scored)
}
QueryPlan::GraphTraversal { start, depth, .. } => {
let (ids, _edges) = self.graph.get_context_subgraph(*start, *depth);
let pm = self.page_map.read();
let scored: Vec<ScoredMemory> = ids
.iter()
.filter_map(|id| {
pm.get(id).and_then(|&pid| {
self.storage.load_memory(pid).ok().map(|node| ScoredMemory {
memory: node,
score: 1.0,
})
})
})
.collect();
Ok(scored)
}
QueryPlan::PointLookup { id } => {
let page_id = self
.page_map
.read()
.get(id)
.copied()
.ok_or(MenteError::MemoryNotFound(*id))?;
let node = self.storage.load_memory(page_id)?;
Ok(vec![ScoredMemory {
memory: node,
score: 1.0,
}])
}
_ => Ok(vec![]),
}
}
/// Memory ids that an entity named in the query links to. Tokenizes the query,
/// looks up each token as an `entity:<name>` node in the tag index, and
/// collects the memories those entity nodes were derived from (their outgoing
/// `Derived` neighbors). Cheap: a bitmap lookup per token plus one graph
/// adjacency read per hit, no LLM. Only non-empty once enrichment has created
/// entity nodes and linked them, so a raw store simply gets no boost.
fn entity_boost_ids(&self, query_text: &str) -> std::collections::HashSet<MemoryId> {
let mut ids = std::collections::HashSet::new();
let graph = self.graph.graph();
let mut seen_tokens = std::collections::HashSet::new();
for token in query_text
.split(|c: char| !c.is_alphanumeric())
.map(|w| w.to_lowercase())
.filter(|w| w.len() >= 2)
{
if !seen_tokens.insert(token.clone()) {
continue;
}
for entity_id in self.index.bitmap.query_tag(&format!("entity:{token}")) {
for (mid, edge) in graph.outgoing(entity_id) {
if edge.edge_type == EdgeType::Derived {
ids.insert(mid);
}
}
}
}
ids
}
/// Add `entity_boost_weight` to any candidate a query-named entity links to,
/// then re-sort. A no-op when nothing in the query resolves to an entity.
fn apply_entity_boost(&self, scored: &mut [ScoredMemory], query_text: &str) {
let ids = self.entity_boost_ids(query_text);
if ids.is_empty() {
return;
}
let w = self.cognitive_config.entity_boost_weight;
for sm in scored.iter_mut() {
if ids.contains(&sm.memory.id) {
sm.score += w;
}
}
scored.sort_by(|a, b| {
b.score
.partial_cmp(&a.score)
.unwrap_or(std::cmp::Ordering::Equal)
});
}
/// Sort recall results by an `ORDER BY` clause, applied before `LIMIT`. Only
/// the numeric and temporal fields (salience, confidence, created, accessed)
/// carry a meaningful order; ordering by any other field is a no-op that keeps
/// the existing (relevance) order.
fn apply_order_by(scored: &mut [ScoredMemory], ob: &OrderBy) {
fn key(n: &MemoryNode, field: Field) -> f64 {
match field {
Field::Salience => n.salience as f64,
Field::Confidence => n.confidence as f64,
Field::Created => n.created_at as f64,
Field::Accessed => n.access_count as f64,
_ => 0.0,
}
}
scored.sort_by(|a, b| {
let ord = key(&a.memory, ob.field)
.partial_cmp(&key(&b.memory, ob.field))
.unwrap_or(std::cmp::Ordering::Equal);
if ob.descending { ord.reverse() } else { ord }
});
}
/// Whether a memory satisfies a boolean WHERE tree (OR/NOT/grouping). Leaves
/// defer to `filter_matches`, so a leaf that `filter_matches` treats as
/// permissive stays permissive; `And`/`Or`/`Not` compose those results.
fn condition_matches(c: &Condition, node: &MemoryNode) -> bool {
match c {
Condition::Leaf(f) => Self::filter_matches(f, node),
Condition::And(children) => children.iter().all(|ch| Self::condition_matches(ch, node)),
Condition::Or(children) => children.iter().any(|ch| Self::condition_matches(ch, node)),
Condition::Not(inner) => !Self::condition_matches(inner, node),
}
}
/// Whether a memory satisfies a single MQL filter. Applies the metadata
/// filters the planner attaches to a plan (type, tag, content, time), so
/// `WHERE type = semantic` and similar clauses actually narrow results
/// instead of being ignored. Unhandled field/value combinations are
/// permissive (they do not exclude), so a supported query is never dropped
/// by a clause this does not understand.
fn filter_matches(f: &Filter, node: &MemoryNode) -> bool {
// IN: the field matches any element of the list (reusing per-field
// equality, so `type IN [...]`, `tag IN [...]`, `content IN [...]` all work).
if let Value::List(items) = &f.value {
return items.iter().any(|v| {
Self::filter_matches(
&Filter {
field: f.field,
op: Operator::Eq,
value: v.clone(),
},
node,
)
});
}
match (&f.field, &f.value) {
(Field::Type, Value::MemoryType(t)) => {
if f.op == Operator::Neq {
node.memory_type != *t
} else {
node.memory_type == *t
}
}
(Field::Tag, Value::Text(tag)) => {
let has = node.tags.iter().any(|x| x == tag);
if f.op == Operator::Neq { !has } else { has }
}
(Field::Content, Value::Text(s)) => {
let hit = node.content.to_lowercase().contains(&s.to_lowercase());
if f.op == Operator::Neq { !hit } else { hit }
}
(Field::Created, v) => Self::num_cmp(node.created_at as f64, f.op, Self::value_f64(v)),
(Field::Accessed, v) => {
Self::num_cmp(node.accessed_at as f64, f.op, Self::value_f64(v))
}
// AS OF <t>: keep only memories whose validity window contains t.
(Field::ValidAt, v) => Self::value_f64(v)
.map(|t| node.is_valid_at(t as u64))
.unwrap_or(true),
_ => true,
}
}
fn value_f64(v: &Value) -> Option<f64> {
match v {
Value::Number(n) => Some(*n),
Value::Integer(i) => Some(*i as f64),
_ => None,
}
}
fn num_cmp(a: f64, op: Operator, b: Option<f64>) -> bool {
let Some(b) = b else { return true };
match op {
Operator::Gt => a > b,
Operator::Gte => a >= b,
Operator::Lt => a < b,
Operator::Lte => a <= b,
Operator::Eq => (a - b).abs() < f64::EPSILON,
Operator::Neq => (a - b).abs() >= f64::EPSILON,
Operator::SimilarTo => true,
// Set/substring operators do not apply to a numeric comparison; the
// `IN` list case is handled before num_cmp is ever reached.
Operator::In | Operator::Contains => false,
}
}
/// Loads MemoryNodes from storage and pairs them with their search scores.
///
/// When decay is enabled, salience is recomputed and factored into the
/// final score to prioritize temporally relevant memories.
fn load_scored_memories(&self, hits: &[(MemoryId, f32)]) -> MenteResult<Vec<ScoredMemory>> {
let pm = self.page_map.read();
let now = if self.cognitive_config.decay_on_recall {
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_micros() as u64
} else {
0
};
let mut scored = Vec::with_capacity(hits.len());
for &(id, score) in hits {
if let Some(&page_id) = pm.get(&id)
&& let Ok(node) = self.storage.load_memory(page_id)
{
let final_score = if self.cognitive_config.decay_on_recall {
let decayed_salience = self.decay.compute_decay(
node.salience,
node.created_at,
node.accessed_at,
node.access_count,
now,
);
// Blend search similarity with decayed salience.
// 70% similarity, 30% salience — keeps search relevance
// primary but rewards recently active memories.
score * 0.7 + decayed_salience * 0.3
} else {
score
};
scored.push(ScoredMemory {
memory: node,
score: final_score,
});
}
}
// Re-sort by blended score when decay is applied.
if self.cognitive_config.decay_on_recall {
scored.sort_unstable_by(|a, b| {
b.score
.partial_cmp(&a.score)
.unwrap_or(std::cmp::Ordering::Equal)
});
}
Ok(scored)
}
// -----------------------------------------------------------------------
// Cognitive Engine: Pain Registry
// -----------------------------------------------------------------------
/// Record a pain signal — a recurring failure or frustration pattern.
///
/// Pain signals are tracked by keywords and surfaced as warnings when
/// similar contexts arise in future queries.
pub fn record_pain(&self, signal: PainSignal) {
if self.cognitive_config.pain_tracking {
self.pain.write().record_pain(signal);
}
}
/// Get pain warnings relevant to the given context keywords.
///
/// Returns formatted warning text if any pain signals match the keywords.
/// Use this before answering to warn about past failures.
pub fn get_pain_warnings(&self, context_keywords: &[String]) -> Vec<PainSignal> {
if !self.cognitive_config.pain_tracking {
return vec![];
}
let registry = self.pain.read();
registry
.get_pain_for_context(context_keywords)
.into_iter()
.cloned()
.collect()
}
/// Format pain warnings as a human-readable string.
pub fn format_pain_warnings(&self, signals: &[&PainSignal]) -> String {
self.pain.read().format_pain_warnings(signals)
}
/// Decay all pain signals to reduce intensity over time.
pub fn decay_pain(&self) {
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_micros() as u64;
self.pain.write().decay_all(now);
}
/// Get all recorded pain signals.
pub fn all_pain_signals(&self) -> Vec<PainSignal> {
self.pain.read().all_signals().to_vec()
}
// -----------------------------------------------------------------------
// Cognitive Engine: Trajectory Tracking
// -----------------------------------------------------------------------
/// Record a conversation turn in the trajectory tracker.
///
/// Tracks the evolution of topics, decisions, and open questions across
/// a conversation. Used for resume context and topic prediction.
pub fn record_trajectory_turn(&self, turn: TrajectoryNode) {
self.trajectory.write().record_turn(turn);
}
/// Canonicalize recent trajectory topics via the LLM, populating the learned
/// topic cache so future turns collapse phrasings to one canonical label.
/// That label then flows into topic prediction, resume context, and the
/// speculative pre-assembly cache, which is why raw message text stops
/// showing up as a "topic".
///
/// Runs OFF the per-turn hot path: call it from an async post-turn step or
/// the maintenance sweep. No lock is held across the LLM calls. The engine
/// stays LLM-optional: pass any judge, or do not call this and topics simply
/// stay at their normalized-raw form. Returns the number of new canonical
/// labels learned this pass.
pub async fn canonicalize_trajectory_topics<J: mentedb_cognitive::llm::LlmJudge>(
&self,
judge: J,
limit: usize,
) -> usize {
// 1. Collect recent uncached topics + the known label set (read lock).
let (pending, mut known) = {
let traj = self.trajectory.read();
(traj.pending_canonicalization(limit), traj.known_topics())
};
if pending.is_empty() {
return 0;
}
// 2. Ask the LLM for a canonical label per topic (no lock held).
let svc = mentedb_cognitive::llm::CognitiveLlmService::new(judge);
let mut learned = Vec::new();
for raw in pending {
if let Ok(label) = svc.canonicalize_topic(&raw, &known).await {
known.push(label.topic.clone());
learned.push((raw, label.topic));
}
}
// 3. Store the learned mappings (write lock).
let n = learned.len();
if n > 0 {
let mut traj = self.trajectory.write();
for (raw, canonical) in learned {
traj.learn_canonical(&raw, &canonical);
}
}
n
}
/// Get a resume context string summarizing the conversation so far.
///
/// Returns None if no trajectory has been recorded.
pub fn get_resume_context(&self) -> Option<String> {
self.trajectory.read().get_resume_context()
}
/// Predict the next likely topics based on conversation trajectory.
///
/// Returns up to 3 predicted topic strings based on transition patterns.
pub fn predict_next_topics(&self) -> Vec<String> {
self.trajectory.read().predict_next_topics()
}
/// Get the full trajectory of recorded turns.
pub fn get_trajectory(&self) -> Vec<TrajectoryNode> {
self.trajectory.read().get_trajectory().to_vec()
}
/// Reinforce a transition that led to a speculative cache hit.
pub fn reinforce_transition(&self, hit_topic: &str) {
self.trajectory.write().reinforce_transition(hit_topic);
}
// -----------------------------------------------------------------------
// Cognitive Engine: Cognition Stream
// -----------------------------------------------------------------------
/// Feed a token to the cognition stream for real-time monitoring.
///
/// Tokens are buffered and analyzed for contradictions with known facts
/// when `check_stream_alerts()` is called.
pub fn feed_stream_token(&self, token: &str) {
self.stream.feed_token(token);
}
/// Check for stream alerts against known facts.
///
/// Compares the buffered token stream against the provided known facts
/// to detect contradictions, corrections, and reinforcements.
pub fn check_stream_alerts(&self, known_facts: &[(MemoryId, String)]) -> Vec<StreamAlert> {
self.stream.check_alerts(known_facts)
}
/// Drain the token buffer, returning accumulated text.
pub fn drain_stream_buffer(&self) -> String {
self.stream.drain_buffer()
}
// -----------------------------------------------------------------------
// Cognitive Engine: Phantom Tracking
// -----------------------------------------------------------------------
/// Detect phantom memories — entities referenced in content but not stored.
///
/// Scans content for entity mentions that don't exist in the known entities
/// list, flagging them as knowledge gaps that should be filled.
pub fn detect_phantoms(
&self,
content: &str,
known_entities: &[String],
turn_id: u64,
) -> Vec<PhantomMemory> {
if !self.cognitive_config.phantom_tracking {
return vec![];
}
self.phantom
.write()
.detect_gaps(content, known_entities, turn_id)
}
/// Resolve a phantom memory (mark it as no longer a gap).
pub fn resolve_phantom(&self, phantom_id: MemoryId) {
self.phantom.write().resolve(phantom_id.into());
}
/// Get all active (unresolved) phantom memories, sorted by priority.
pub fn get_active_phantoms(&self) -> Vec<PhantomMemory> {
self.phantom
.read()
.get_active_phantoms()
.into_iter()
.cloned()
.collect()
}
/// Format phantom warnings as a human-readable string.
pub fn format_phantom_warnings(&self) -> String {
self.phantom.read().format_phantom_warnings()
}
/// Register an entity so the phantom tracker knows it exists.
pub fn register_entity(&self, entity: &str) {
self.phantom.write().register_entity(entity);
}
/// Register multiple entities at once.
pub fn register_entities(&self, entities: &[&str]) {
self.phantom.write().register_entities(entities);
}
// -----------------------------------------------------------------------
// Cognitive Engine: Speculative Cache
// -----------------------------------------------------------------------
/// Try to hit the speculative cache for a query.
///
/// If a previous prediction matches the current query (by keyword overlap
/// or embedding similarity), returns the pre-assembled context.
pub fn try_speculative_hit(
&self,
query: &str,
query_embedding: Option<&[f32]>,
) -> Option<CacheEntry> {
if !self.cognitive_config.speculative_cache {
return None;
}
self.speculative.write().try_hit(query, query_embedding)
}
/// Pre-assemble speculative cache entries for predicted topics.
///
/// The builder function should return `(context_text, memory_ids, optional_embedding)`
/// for each topic prediction.
pub fn pre_assemble_speculative<F>(&self, predictions: Vec<String>, builder: F)
where
F: Fn(&str) -> Option<(String, Vec<MemoryId>, Option<Vec<f32>>)>,
{
if self.cognitive_config.speculative_cache {
self.speculative.write().pre_assemble(predictions, builder);
}
}
/// Evict stale entries from the speculative cache.
pub fn evict_stale_speculative(&self, max_age_us: u64) {
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_micros() as u64;
self.speculative.write().evict_stale(max_age_us, now);
}
/// Get speculative cache statistics.
pub fn speculative_cache_stats(&self) -> CacheStats {
self.speculative.read().stats()
}
/// Get the current speculative cache entries (predicted topic, pre-assembled
/// context, source memory ids, hit count, and age), for introspection and
/// dashboards. This is the live pre-assembly cache, not aggregate stats.
pub fn speculative_cache_entries(&self) -> Vec<CacheEntry> {
self.speculative.read().entries().to_vec()
}
// -----------------------------------------------------------------------
// Cognitive Engine: Interference Detection
// -----------------------------------------------------------------------
/// Detect interference between a set of memories.
///
/// Returns pairs of memories that are similar enough to cause confusion,
/// along with disambiguation hints. Use this during context assembly to
/// add disambiguation notes or separate confusable memories.
pub fn detect_interference(&self, memories: &[MemoryNode]) -> Vec<InterferencePair> {
if !self.cognitive_config.interference_detection {
return vec![];
}
self.interference.detect_interference(memories)
}
/// Generate a disambiguation hint for two confusable memories.
pub fn generate_disambiguation(&self, a: &MemoryNode, b: &MemoryNode) -> String {
self.interference.generate_disambiguation(a, b)
}
/// Arrange memory IDs to maximize separation between interfering pairs.
pub fn arrange_with_separation(
memories: Vec<MemoryId>,
pairs: &[InterferencePair],
) -> Vec<MemoryId> {
InterferenceDetector::arrange_with_separation(memories, pairs)
}
// -----------------------------------------------------------------------
// Cognitive Engine: Entity Resolution
// -----------------------------------------------------------------------
/// Resolve an entity name to its canonical form.
///
/// Uses cached aliases and rule-based matching (no LLM).
pub fn resolve_entity(&self, name: &str) -> mentedb_cognitive::ResolvedEntity {
self.entity_resolver.read().resolve(name)
}
/// Add an alias mapping for entity resolution.
pub fn add_entity_alias(&self, alias: &str, canonical: &str, confidence: f32) {
self.entity_resolver
.write()
.add_alias(alias, canonical, confidence);
}
/// Get the canonical name for an entity, if known.
pub fn get_canonical_entity(&self, name: &str) -> Option<String> {
self.entity_resolver.read().get_canonical(name).cloned()
}
/// List all known entities in the resolver.
pub fn known_entities(&self) -> Vec<String> {
self.entity_resolver.read().known_entities()
}
// -----------------------------------------------------------------------
// Cognitive Engine: Memory Compression
// -----------------------------------------------------------------------
/// Compress a memory's content, extracting key facts and removing filler.
///
/// Returns a compressed representation with the original ID, compressed text,
/// compression ratio, and extracted key facts.
pub fn compress_memory(&self, memory: &MemoryNode) -> CompressedMemory {
self.compressor.compress(memory)
}
/// Compress a batch of memories.
pub fn compress_memories(&self, memories: &[MemoryNode]) -> Vec<CompressedMemory> {
self.compressor.compress_batch(memories)
}
/// Estimate token count for a text string.
pub fn estimate_tokens(text: &str) -> usize {
MemoryCompressor::estimate_tokens(text)
}
// -----------------------------------------------------------------------
// Cognitive Engine: Archival Evaluation
// -----------------------------------------------------------------------
/// Evaluate whether a memory should be kept, archived, or deleted.
///
/// Uses age, salience, and access patterns to make lifecycle decisions.
pub fn evaluate_archival(&self, memory: &MemoryNode) -> ArchivalDecision {
if !self.cognitive_config.archival_evaluation {
return ArchivalDecision::Keep;
}
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_micros() as u64;
// Judge on the salience decayed to `now`, not the stored base. Stored
// salience only changes on reinforcement, so a never-reinforced memory
// keeps its base value and archival must derive the current strength.
let effective = self.decay.compute_decay(
memory.salience,
memory.created_at,
memory.accessed_at,
memory.access_count,
now,
);
self.archival.evaluate_effective(
effective,
memory.memory_type,
memory.created_at,
memory.access_count,
now,
)
}
/// Evaluate archival decisions for a batch of memories.
pub fn evaluate_archival_batch(
&self,
memories: &[MemoryNode],
) -> Vec<(MemoryId, ArchivalDecision)> {
if !self.cognitive_config.archival_evaluation {
return memories
.iter()
.map(|m| (m.id, ArchivalDecision::Keep))
.collect();
}
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_micros() as u64;
memories
.iter()
.map(|m| {
let effective = self.decay.compute_decay(
m.salience,
m.created_at,
m.accessed_at,
m.access_count,
now,
);
(
m.id,
self.archival.evaluate_effective(
effective,
m.memory_type,
m.created_at,
m.access_count,
now,
),
)
})
.collect()
}
/// Run archival evaluation on all memories in the database.
///
/// Returns decisions for each memory. Does NOT apply them — call
/// `invalidate_memory` or `forget` to act on the decisions.
pub fn evaluate_archival_global(&self) -> MenteResult<Vec<(MemoryId, ArchivalDecision)>> {
let pm = self.page_map.read();
let memories: Vec<MemoryNode> = pm
.values()
.filter_map(|pid| self.storage.load_memory(*pid).ok())
.collect();
drop(pm);
// Delegate so decisions use each memory's salience decayed to now, not
// its stored base (see evaluate_archival_batch).
Ok(self.evaluate_archival_batch(&memories))
}
// -----------------------------------------------------------------------
// Sleeptime Enrichment Pipeline
// -----------------------------------------------------------------------
/// Check whether enrichment is pending (triggered by turn count or manual).
pub fn needs_enrichment(&self) -> bool {
if !self.cognitive_config.enrichment_config.enabled {
return false;
}
*self.enrichment_pending.read()
}
/// Get the turn ID when enrichment last completed.
pub fn last_enrichment_turn(&self) -> u64 {
*self.last_enrichment_turn.read()
}
/// Manually trigger enrichment on the next check.
pub fn request_enrichment(&self) {
*self.enrichment_pending.write() = true;
}
/// Distinct (user_id, agent_id) owner pairs that own at least one memory.
///
/// Enrichment partitions by BOTH owner axes: it runs once per distinct pair
/// so derived knowledge (extracted facts, entities, communities, profile)
/// stays scoped to exactly the user AND agent that owned the source
/// memories, never mixing across either axis.
fn distinct_owner_pairs(&self) -> Vec<(UserId, AgentId)> {
let page_ids: Vec<PageId> = self.page_map.read().values().copied().collect();
let mut pairs: Vec<(UserId, AgentId)> = Vec::new();
for pid in &page_ids {
if let Ok(mem) = self.storage.load_memory(*pid) {
let pair = (mem.user_id, mem.agent_id);
if !pairs.contains(&pair) {
pairs.push(pair);
}
}
}
pairs
}
/// Get episodic memories that haven't been enriched yet.
///
/// Returns all Episodic memories created after the last enrichment turn,
/// owned by exactly this (`user`, `agent`) pair (exact match on BOTH owner
/// axes, so one owner's episodics are never enriched into another's
/// knowledge), sorted by creation time. These are the candidates for LLM
/// extraction.
pub fn enrichment_candidates(&self, user: UserId, agent: AgentId) -> Vec<MemoryNode> {
let last_turn = *self.last_enrichment_turn.read();
let page_ids: Vec<PageId> = self.page_map.read().values().copied().collect();
let mut candidates: Vec<MemoryNode> = page_ids
.iter()
.filter_map(|pid| self.storage.load_memory(*pid).ok())
.filter(|m| {
m.agent_id == agent
&& m.user_id == user
&& m.memory_type == mentedb_core::memory::MemoryType::Episodic
&& !m.tags.contains(&"source:enrichment".to_string())
&& m.created_at > last_turn
})
.collect();
candidates.sort_by_key(|m| m.created_at);
candidates
}
/// All unenriched episodic candidates across every owner, sorted by creation
/// time. This is an owner-agnostic introspection view (the enrichment
/// pipeline itself scopes per owner via [`Self::enrichment_candidates`]); it
/// exists so SDK callers can list pending work without an agent argument.
pub fn all_enrichment_candidates(&self) -> Vec<MemoryNode> {
let mut candidates: Vec<MemoryNode> = self
.distinct_owner_pairs()
.into_iter()
.flat_map(|(user, agent)| self.enrichment_candidates(user, agent))
.collect();
candidates.sort_by_key(|m| m.created_at);
candidates
}
/// Store enrichment results: extracted memories with provenance tracking.
///
/// Each stored memory gets:
/// - `source:enrichment` tag for identification
/// - Confidence capped at `max_enrichment_confidence`
/// - `Derived` edges back to source episodic memories
///
/// Returns (memories_stored, edges_created).
pub fn store_enrichment_memories(
&self,
memories: Vec<MemoryNode>,
source_ids: &[MemoryId],
) -> MenteResult<(usize, usize)> {
let max_conf = self
.cognitive_config
.enrichment_config
.max_enrichment_confidence;
let mut stored = 0usize;
let mut edges = 0usize;
for mut mem in memories {
// Tag as enrichment-generated
if !mem.tags.contains(&"source:enrichment".to_string()) {
mem.tags.push("source:enrichment".to_string());
}
// Cap confidence
if mem.confidence > max_conf {
mem.confidence = max_conf;
}
let mem_id = mem.id;
self.store(mem)?;
stored += 1;
// Create Derived edges back to source episodics
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_micros() as u64;
for src_id in source_ids {
let edge = MemoryEdge {
source: mem_id,
target: *src_id,
edge_type: EdgeType::Derived,
weight: 0.8,
created_at: now,
valid_from: None,
valid_until: None,
label: Some("enrichment".to_string()),
};
if self.relate(edge).is_ok() {
edges += 1;
}
}
}
debug!(stored, edges, "enrichment memories stored");
Ok((stored, edges))
}
/// Mark enrichment as complete for the given turn.
pub fn mark_enrichment_complete(&self, turn_id: u64) {
*self.last_enrichment_turn.write() = turn_id;
*self.enrichment_pending.write() = false;
debug!(turn_id, "enrichment cycle complete");
}
/// Get the enrichment configuration.
pub fn enrichment_config(&self) -> &EnrichmentConfig {
&self.cognitive_config.enrichment_config
}
/// Get all unique entity names from stored entity memories owned by `agent`.
///
/// Returns deduplicated, normalized entity names extracted from
/// `entity:{name}` tags across this agent's stored memories only, so one
/// user's entities never bleed into another's resolution.
pub fn all_entity_names(&self, user: UserId, agent: AgentId) -> Vec<String> {
let page_ids: Vec<PageId> = self.page_map.read().values().copied().collect();
let mut names = std::collections::HashSet::new();
for pid in &page_ids {
if let Ok(mem) = self.storage.load_memory(*pid) {
if mem.agent_id != agent || mem.user_id != user {
continue;
}
for tag in &mem.tags {
if let Some(name) = tag.strip_prefix("entity:") {
names.insert(name.to_lowercase().trim().to_string());
}
}
}
}
let mut sorted: Vec<String> = names.into_iter().collect();
sorted.sort();
sorted
}
/// Get entity names that the EntityResolver hasn't resolved yet.
///
/// These are the entities that need LLM resolution. The EntityResolver
/// cache handles known entities for free.
pub fn unresolved_entity_names(&self, user: UserId, agent: AgentId) -> Vec<String> {
let all_names = self.all_entity_names(user, agent);
self.entity_resolver.read().unresolved_names(&all_names)
}
/// Get entity names with their memory content for LLM context.
///
/// Returns (name, content) pairs for entities that need resolution.
/// The content helps the LLM disambiguate (e.g., "Python" near
/// "web framework" vs "Python" near "Monty Python").
pub fn entity_names_with_context(
&self,
user: UserId,
agent: AgentId,
) -> Vec<(String, Option<String>)> {
let page_ids: Vec<PageId> = self.page_map.read().values().copied().collect();
let mut entity_contexts: HashMap<String, String> = HashMap::new();
for pid in &page_ids {
if let Ok(mem) = self.storage.load_memory(*pid) {
if mem.agent_id != agent || mem.user_id != user {
continue;
}
for tag in &mem.tags {
if let Some(name) = tag.strip_prefix("entity:") {
let normalized = name.to_lowercase().trim().to_string();
// One entity tag per memory (enrichment creates separate memories per entity)
entity_contexts
.entry(normalized)
.and_modify(|existing| {
// Append content from multiple mentions, cap at ~500 chars
if existing.len() < 300 {
existing.push_str(" | ");
let remaining = 500usize.saturating_sub(existing.len());
existing.push_str(
mentedb_core::text::truncate_on_char_boundary(
&mem.content,
remaining,
),
);
}
})
.or_insert_with(|| {
mentedb_core::text::truncate_on_char_boundary(&mem.content, 300)
.to_string()
});
break;
}
}
}
}
entity_contexts
.into_iter()
.map(|(name, ctx)| (name, Some(ctx)))
.collect()
}
/// Apply LLM entity resolution results: create graph edges and update cache.
///
/// Takes merge groups from the LLM (via `CognitiveLlmService.resolve_entities()`)
/// and confirmed-different pairs. Creates `entity_link:` edges between entity
/// memories that belong to the same group, learns aliases in the EntityResolver,
/// and negative-caches confirmed-different pairs.
pub fn apply_entity_link_resolutions(
&self,
merge_groups: &[EntityLinkResolution],
separations: &[EntitySeparation],
user: UserId,
agent: AgentId,
) -> MenteResult<EntityLinkResult> {
let mut result = EntityLinkResult::default();
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_micros() as u64;
// Build a map: normalized entity name → list of memory IDs (this
// (user, agent) pair's entities only, so links never span either owner
// axis).
let entity_memory_map = self.build_entity_memory_map(user, agent);
let mut resolver = self.entity_resolver.write();
for group in merge_groups {
// Learn the group in the resolver cache
let mut aliases: Vec<String> = group.aliases.clone();
aliases.retain(|a| a.to_lowercase() != group.canonical.to_lowercase());
resolver.learn_group(&EntityMergeGroup {
canonical: group.canonical.clone(),
aliases,
confidence: group.confidence,
});
// Collect all memory IDs for this merge group
let mut group_memory_ids: Vec<MemoryId> = Vec::new();
// Add memories for the canonical name
let canonical_norm = group.canonical.to_lowercase();
if let Some(ids) = entity_memory_map.get(&canonical_norm) {
group_memory_ids.extend(ids);
}
// Add memories for each alias
for alias in &group.aliases {
let alias_norm = alias.to_lowercase();
if let Some(ids) = entity_memory_map.get(&alias_norm) {
group_memory_ids.extend(ids);
}
}
group_memory_ids.sort();
group_memory_ids.dedup();
// Create edges between all pairs in the group
let label = format!("entity_link:{}", canonical_norm);
for i in 0..group_memory_ids.len() {
for j in (i + 1)..group_memory_ids.len() {
let a_id = group_memory_ids[i];
let b_id = group_memory_ids[j];
// Check for existing edge
let graph = self.graph.read_graph();
let already_linked = graph.outgoing(a_id).iter().any(|(tid, e)| {
*tid == b_id
&& e.edge_type == EdgeType::Related
&& e.label
.as_ref()
.is_some_and(|l| l.starts_with("entity_link:"))
});
drop(graph);
if already_linked {
continue;
}
let edge = MemoryEdge {
source: a_id,
target: b_id,
edge_type: EdgeType::Related,
weight: group.confidence,
created_at: now,
valid_from: None,
valid_until: None,
label: Some(label.clone()),
};
if self.relate(edge).is_ok() {
result.edges_created += 1;
}
result.linked += 1;
}
}
debug!(
canonical = group.canonical,
aliases = ?group.aliases,
memories = group_memory_ids.len(),
"entity resolution: merged group"
);
}
// Process negative cache entries
for sep in separations {
resolver.mark_different(&sep.name_a, &sep.name_b);
debug!(
a = sep.name_a,
b = sep.name_b,
"entity resolution: confirmed different"
);
}
// Persist resolver state
let cognitive_dir = self.path.join("cognitive");
if cognitive_dir.exists() || std::fs::create_dir_all(&cognitive_dir).is_ok() {
let _ = resolver.save(&cognitive_dir.join("entities.json"));
}
debug!(
linked = result.linked,
edges = result.edges_created,
groups = merge_groups.len(),
separations = separations.len(),
"entity link resolutions applied"
);
Ok(result)
}
/// Link entities using only the sync EntityResolver (cache + rules, no LLM).
///
/// This is the fast path — links entities that are already known to be
/// the same from previous LLM resolutions. Runs once per owner so entity
/// links never span users; each owner's entities are linked in isolation
/// (nil owned / global entities are linked among themselves).
pub fn link_entities(&self) -> MenteResult<EntityLinkResult> {
let mut total = EntityLinkResult::default();
for (user, agent) in self.distinct_owner_pairs() {
let r = self.link_entities_for(user, agent)?;
total.linked += r.linked;
total.edges_created += r.edges_created;
}
Ok(total)
}
/// Sync entity linking scoped to a single (user, agent) owner pair. See
/// [`Self::link_entities`].
pub(crate) fn link_entities_for(
&self,
user: UserId,
agent: AgentId,
) -> MenteResult<EntityLinkResult> {
let entity_memory_map = self.build_entity_memory_map(user, agent);
let resolver = self.entity_resolver.read();
// Group entity names by their resolved canonical name
let mut canonical_groups: HashMap<String, Vec<String>> = HashMap::new();
for entity_name in entity_memory_map.keys() {
let resolved = resolver.resolve(entity_name);
if resolved.source != mentedb_cognitive::ResolutionSource::Identity {
canonical_groups
.entry(resolved.canonical.clone())
.or_default()
.push(entity_name.clone());
}
}
drop(resolver);
let mut result = EntityLinkResult::default();
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_micros() as u64;
for (canonical, names) in &canonical_groups {
// Collect all memory IDs across all aliases in this group
let mut group_memory_ids: Vec<MemoryId> = Vec::new();
for name in names {
if let Some(ids) = entity_memory_map.get(name) {
group_memory_ids.extend(ids);
}
}
// Also include the canonical name itself
if let Some(ids) = entity_memory_map.get(canonical) {
group_memory_ids.extend(ids);
}
group_memory_ids.sort();
group_memory_ids.dedup();
if group_memory_ids.len() < 2 {
continue;
}
let label = format!("entity_link:{}", canonical);
for i in 0..group_memory_ids.len() {
for j in (i + 1)..group_memory_ids.len() {
let a_id = group_memory_ids[i];
let b_id = group_memory_ids[j];
let graph = self.graph.read_graph();
let already_linked = graph.outgoing(a_id).iter().any(|(tid, e)| {
*tid == b_id
&& e.edge_type == EdgeType::Related
&& e.label
.as_ref()
.is_some_and(|l| l.starts_with("entity_link:"))
});
drop(graph);
if already_linked {
continue;
}
let edge = MemoryEdge {
source: a_id,
target: b_id,
edge_type: EdgeType::Related,
weight: 1.0,
created_at: now,
valid_from: None,
valid_until: None,
label: Some(label.clone()),
};
if self.relate(edge).is_ok() {
result.edges_created += 1;
}
result.linked += 1;
}
}
}
debug!(
linked = result.linked,
edges = result.edges_created,
groups = canonical_groups.len(),
"sync entity linking complete"
);
Ok(result)
}
/// Build a map of normalized entity name → list of MemoryIds, restricted to
/// entity memories owned by exactly this (`user`, `agent`) pair so links and
/// community membership never cross either owner axis.
fn build_entity_memory_map(
&self,
user: UserId,
agent: AgentId,
) -> HashMap<String, Vec<MemoryId>> {
let page_ids: Vec<PageId> = self.page_map.read().values().copied().collect();
let mut map: HashMap<String, Vec<MemoryId>> = HashMap::new();
for pid in &page_ids {
if let Ok(mem) = self.storage.load_memory(*pid) {
if mem.agent_id != agent || mem.user_id != user {
continue;
}
for tag in &mem.tags {
if let Some(name) = tag.strip_prefix("entity:") {
let normalized = name.to_lowercase().trim().to_string();
// One entity tag per memory (enrichment creates separate memories per entity)
map.entry(normalized).or_default().push(mem.id);
break;
}
}
}
}
map
}
/// Get all entity memory nodes (memories tagged with `entity:{name}`).
pub fn entity_memories(&self) -> Vec<MemoryNode> {
let page_ids: Vec<PageId> = self.page_map.read().values().copied().collect();
page_ids
.iter()
.filter_map(|pid| self.storage.load_memory(*pid).ok())
.filter(|m| m.tags.iter().any(|t| t.starts_with("entity:")))
.collect()
}
/// Get entity categories with their member entities for community detection.
///
/// Returns a map of category → list of (entity_name, context_snippet).
/// Categories come from `entity_type:` tags on entity memories.
pub fn entity_communities(
&self,
user: UserId,
agent: AgentId,
) -> HashMap<String, Vec<(String, String)>> {
let page_ids: Vec<PageId> = self.page_map.read().values().copied().collect();
let mut categories: HashMap<String, Vec<(String, String)>> = HashMap::new();
for pid in &page_ids {
if let Ok(mem) = self.storage.load_memory(*pid) {
// Only cluster this (user, agent) pair's entities so communities
// never mix across either owner axis.
if mem.agent_id != agent || mem.user_id != user {
continue;
}
// Skip non-entity memories and existing community summaries
if mem.tags.iter().any(|t| t == "community_summary") {
continue;
}
let entity_name = mem
.tags
.iter()
.find_map(|t| t.strip_prefix("entity:"))
.map(|n| n.to_string());
if let Some(name) = entity_name {
let entity_type = mem
.tags
.iter()
.find_map(|t| t.strip_prefix("entity_type:"))
.unwrap_or("general")
.to_lowercase();
let context: String = mem.content.chars().take(200).collect();
categories
.entry(entity_type)
.or_default()
.push((name, context));
}
}
}
// Only return categories with 2+ entities (meaningful clusters)
categories.retain(|_, members| members.len() >= 2);
categories
}
/// Store a community summary memory with edges to member entities.
///
/// Creates a `community_summary` tagged memory and `Derived` edges
/// from the summary to each member entity in the category.
pub fn store_community_summary(
&self,
category: &str,
summary: &str,
member_names: &[String],
user: UserId,
agent: AgentId,
) -> MenteResult<MemoryId> {
if category.is_empty() {
return Err(MenteError::Storage(
"community category cannot be empty".into(),
));
}
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_micros() as u64;
// Check if a community summary already exists for this category AND both
// owner axes, so one owner's summary never overwrites another's.
let community_tag = format!("community:{}", category);
let page_ids: Vec<PageId> = self.page_map.read().values().copied().collect();
let mut existing_id = None;
for pid in &page_ids {
if let Ok(mem) = self.storage.load_memory(*pid)
&& mem.agent_id == agent
&& mem.user_id == user
&& mem.tags.iter().any(|t| t == &community_tag)
{
// Update existing summary content
let mut updated = mem.clone();
updated.content = summary.to_string();
if let Some(ref embedder) = self.embedder {
updated.embedding = embedder
.embed(summary)
.unwrap_or_else(|_| updated.embedding.clone());
}
self.storage.update_memory(*pid, &updated)?;
existing_id = Some(updated.id);
break;
}
}
let node_id = if let Some(id) = existing_id {
id
} else {
// Create new community summary
let embedding = self
.embedder
.as_ref()
.and_then(|e| e.embed(summary).ok())
.unwrap_or_default();
let mut node =
MemoryNode::new(agent, MemoryType::Semantic, summary.to_string(), embedding)
.with_user_id(user);
node.tags = vec![
"community_summary".to_string(),
community_tag,
"source:enrichment".to_string(),
];
node.confidence = 0.7;
let id = node.id;
self.store(node)?;
id
};
// (Re)create Derived edges from summary to member entity memories.
// On update this refreshes edges to reflect current membership.
let entity_map = self.build_entity_memory_map(user, agent);
for name in member_names {
let normalized = name.to_lowercase();
if let Some(member_ids) = entity_map.get(&normalized) {
for member_id in member_ids {
self.relate(MemoryEdge {
source: node_id,
target: *member_id,
edge_type: EdgeType::Derived,
weight: 0.8,
created_at: now,
valid_from: None,
valid_until: None,
label: Some(format!("community_member:{}", category)),
})?;
}
}
}
Ok(node_id)
}
/// Get existing community summaries owned by exactly this (`user`, `agent`)
/// pair, so each owner only sees its own community summaries.
pub fn community_summaries(&self, user: UserId, agent: AgentId) -> Vec<MemoryNode> {
let page_ids: Vec<PageId> = self.page_map.read().values().copied().collect();
page_ids
.iter()
.filter_map(|pid| self.storage.load_memory(*pid).ok())
.filter(|m| {
m.agent_id == agent
&& m.user_id == user
&& m.tags.iter().any(|t| t == "community_summary")
})
.collect()
}
/// Collect all semantic/procedural facts for user profile generation.
///
/// Returns high-confidence memories suitable for profile building.
pub fn profile_facts(&self, user: UserId, agent: AgentId) -> Vec<String> {
let page_ids: Vec<PageId> = self.page_map.read().values().copied().collect();
let mut facts = Vec::new();
for pid in &page_ids {
if let Ok(mem) = self.storage.load_memory(*pid) {
// Only this (user, agent) pair's facts, so a profile is built
// from one owner's knowledge and never mixes in another owner's
// memories on either axis.
if mem.agent_id != agent || mem.user_id != user {
continue;
}
// Only semantic and procedural memories with decent confidence
if mem.confidence < 0.5 {
continue;
}
match mem.memory_type {
MemoryType::Semantic | MemoryType::Procedural => {
// Skip community summaries and entity nodes
if mem
.tags
.iter()
.any(|t| t == "community_summary" || t.starts_with("entity:"))
{
continue;
}
facts.push(mem.content.chars().take(300).collect());
}
_ => {}
}
}
}
// Cap at 100 most relevant facts to fit in LLM context
facts.truncate(100);
facts
}
/// Store or update the user profile as an always-scoped memory owned by
/// exactly this (`user`, `agent`) pair.
///
/// There is exactly one user profile memory (tagged `user_profile`) per
/// owner pair. If one already exists for this pair, it's replaced entirely;
/// one owner's profile never overwrites another's.
pub fn store_user_profile(
&self,
profile: &str,
user: UserId,
agent: AgentId,
) -> MenteResult<MemoryId> {
// Find existing profile for this owner pair
let page_ids: Vec<PageId> = self.page_map.read().values().copied().collect();
for pid in &page_ids {
if let Ok(mem) = self.storage.load_memory(*pid)
&& mem.agent_id == agent
&& mem.user_id == user
&& mem.tags.iter().any(|t| t == "user_profile")
{
// Update in place, bumping created_at so it reflects when the
// profile was last rebuilt. `profile_updated_at` reads created_at;
// without this the "updated" time stayed frozen at first creation
// (a regenerated profile still showed "updated N days ago").
let mut updated = mem.clone();
updated.content = profile.to_string();
updated.created_at = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_micros() as u64;
if let Some(ref embedder) = self.embedder {
updated.embedding = embedder
.embed(profile)
.unwrap_or_else(|_| updated.embedding.clone());
}
self.storage.update_memory(*pid, &updated)?;
return Ok(updated.id);
}
}
// Create new profile
let embedding = self
.embedder
.as_ref()
.and_then(|e| e.embed(profile).ok())
.unwrap_or_default();
let mut node = MemoryNode::new(agent, MemoryType::Semantic, profile.to_string(), embedding)
.with_user_id(user);
node.tags = vec![
"user_profile".to_string(),
"scope:always".to_string(),
"source:enrichment".to_string(),
];
node.confidence = 0.8;
let node_id = node.id;
self.store(node)?;
Ok(node_id)
}
/// Get the account-level user profile: the one owned by neither a user nor
/// an agent (both owner axes nil).
///
/// Deterministic. Since enrichment now builds one profile per (user, agent)
/// owner pair, an account may hold many; this returns only the shared,
/// unowned one, never an arbitrary owner's. A multi-user app that keeps only
/// per-owner profiles gets `None` here; use [`user_profile_for`] to read a
/// specific owner's, and [`profile_owners`] to list which owners have one.
pub fn user_profile(&self) -> Option<MemoryNode> {
self.user_profile_for(UserId::nil(), AgentId::nil())
}
/// Get the user profile owned by exactly this (`user`, `agent`) pair, if one
/// exists. One owner's profile is never returned for another, so a dashboard
/// or a per-turn context primer can show the right profile for a scope.
pub fn user_profile_for(&self, user: UserId, agent: AgentId) -> Option<MemoryNode> {
let page_ids: Vec<PageId> = self.page_map.read().values().copied().collect();
for pid in &page_ids {
if let Ok(mem) = self.storage.load_memory(*pid)
&& mem.user_id == user
&& mem.agent_id == agent
&& mem.tags.iter().any(|t| t == "user_profile")
{
return Some(mem);
}
}
None
}
/// List the owner pairs that have a user profile, most recently built first.
/// Lets a dashboard enumerate the profiles it can show without scanning
/// memory bodies itself.
pub fn profile_owners(&self) -> Vec<(UserId, AgentId)> {
let page_ids: Vec<PageId> = self.page_map.read().values().copied().collect();
let mut owners: Vec<(UserId, AgentId, u64)> = Vec::new();
for pid in &page_ids {
if let Ok(mem) = self.storage.load_memory(*pid)
&& mem.tags.iter().any(|t| t == "user_profile")
{
owners.push((mem.user_id, mem.agent_id, mem.created_at));
}
}
owners.sort_by_key(|(_, _, ts)| std::cmp::Reverse(*ts));
owners.into_iter().map(|(u, a, _)| (u, a)).collect()
}
}
#[cfg(test)]
mod mql_execution_tests {
use super::*;
/// Regression: a bare `RECALL` and a `WHERE type = ...` filter must return
/// the matching memories. Before the executor fix these produced an empty
/// TagScan (empty tags, filters ignored) and returned nothing.
#[test]
fn recall_bare_and_by_type_return_matches() {
let dir = tempfile::tempdir().unwrap();
let db = MenteDb::open(dir.path()).unwrap();
db.store(MemoryNode::new(
AgentId::nil(),
MemoryType::Semantic,
"the sky is blue".into(),
vec![0.1_f32; 8],
))
.unwrap();
db.store(MemoryNode::new(
AgentId::nil(),
MemoryType::Semantic,
"water is wet".into(),
vec![0.2_f32; 8],
))
.unwrap();
db.store(MemoryNode::new(
AgentId::nil(),
MemoryType::Procedural,
"how to tie a knot".into(),
vec![0.3_f32; 8],
))
.unwrap();
// Bare RECALL returns all memories (previously empty).
let all = db.recall("RECALL memories LIMIT 50").unwrap();
let all_n: usize = all.blocks.iter().map(|b| b.memories.len()).sum();
assert_eq!(all_n, 3, "bare RECALL should return all three memories");
// A type filter narrows to just that type (previously empty).
let sem = db
.recall("RECALL memories WHERE type = semantic LIMIT 50")
.unwrap();
let sem_mems: Vec<&ScoredMemory> = sem.blocks.iter().flat_map(|b| &b.memories).collect();
assert_eq!(
sem_mems.len(),
2,
"type = semantic should return two memories"
);
assert!(
sem_mems
.iter()
.all(|sm| sm.memory.memory_type == MemoryType::Semantic),
"only semantic memories should come back"
);
}
/// Build a small fixture: three types, two of them tagged "keep", so boolean
/// WHERE clauses have something to include and exclude.
fn boolean_fixture() -> (tempfile::TempDir, MenteDb) {
let dir = tempfile::tempdir().unwrap();
let db = MenteDb::open(dir.path()).unwrap();
let mut sem_keep = MemoryNode::new(
AgentId::nil(),
MemoryType::Semantic,
"semantic kept".into(),
vec![0.1_f32; 8],
);
sem_keep.tags = vec!["keep".into()];
let sem_plain = MemoryNode::new(
AgentId::nil(),
MemoryType::Semantic,
"semantic plain".into(),
vec![0.2_f32; 8],
);
let mut proc_keep = MemoryNode::new(
AgentId::nil(),
MemoryType::Procedural,
"procedural kept".into(),
vec![0.3_f32; 8],
);
proc_keep.tags = vec!["keep".into()];
let anti = MemoryNode::new(
AgentId::nil(),
MemoryType::AntiPattern,
"antipattern plain".into(),
vec![0.4_f32; 8],
);
for n in [sem_keep, sem_plain, proc_keep, anti] {
db.store(n).unwrap();
}
(dir, db)
}
fn contents(db: &MenteDb, mql: &str) -> Vec<String> {
let mut v: Vec<String> = db
.query(mql)
.unwrap()
.into_iter()
.map(|s| s.memory.content)
.collect();
v.sort();
v
}
/// OR must union the branches: `type = semantic OR type = procedural` returns
/// exactly the semantic and procedural rows, never the antipattern.
#[test]
fn or_unions_branches() {
let (_d, db) = boolean_fixture();
let got = contents(
&db,
"RECALL WHERE type = semantic OR type = procedural LIMIT 50",
);
assert_eq!(
got,
vec![
"procedural kept".to_string(),
"semantic kept".to_string(),
"semantic plain".to_string()
],
"OR should return semantic and procedural, excluding antipattern"
);
}
/// NOT must exclude: `NOT type = semantic` returns everything that is not
/// semantic.
#[test]
fn not_excludes() {
let (_d, db) = boolean_fixture();
let got = contents(&db, "RECALL WHERE NOT type = semantic LIMIT 50");
assert_eq!(
got,
vec![
"antipattern plain".to_string(),
"procedural kept".to_string()
],
"NOT type = semantic should drop both semantic rows"
);
}
/// AND with NOT: `type = semantic AND NOT tag = keep` isolates the untagged
/// semantic row, proving NOT composes under AND.
#[test]
fn and_not_composes() {
let (_d, db) = boolean_fixture();
let got = contents(
&db,
r#"RECALL WHERE type = semantic AND NOT tag = "keep" LIMIT 50"#,
);
assert_eq!(
got,
vec!["semantic plain".to_string()],
"only the untagged semantic row should remain"
);
}
/// Grouping changes meaning: `(type = semantic OR type = procedural) AND tag =
/// keep` must return only the kept rows of those two types, not the plain
/// semantic one. This is the precedence case a flat AND list cannot express.
#[test]
fn grouping_binds_before_and() {
let (_d, db) = boolean_fixture();
let got = contents(
&db,
r#"RECALL WHERE (type = semantic OR type = procedural) AND tag = "keep" LIMIT 50"#,
);
assert_eq!(
got,
vec!["procedural kept".to_string(), "semantic kept".to_string()],
"grouping must apply the tag filter to both OR branches"
);
}
/// Regression: a pure AND clause still returns the intersection (and stays on
/// the flat-filter fast path, which this exercises end to end).
#[test]
fn pure_and_still_intersects() {
let (_d, db) = boolean_fixture();
let got = contents(
&db,
r#"RECALL WHERE type = semantic AND tag = "keep" LIMIT 50"#,
);
assert_eq!(got, vec!["semantic kept".to_string()]);
}
}
#[cfg(test)]
mod order_by_tests {
use super::*;
fn contents(db: &MenteDb, mql: &str) -> Vec<String> {
db.query(mql)
.unwrap()
.into_iter()
.map(|s| s.memory.content)
.collect()
}
/// ORDER BY sorts by the field and applies before LIMIT, both directions.
#[test]
fn order_by_salience_sorts_before_limit() {
let dir = tempfile::tempdir().unwrap();
let db = MenteDb::open(dir.path()).unwrap();
for (content, sal) in [("low", 0.2_f32), ("high", 0.9), ("mid", 0.5)] {
let mut n = MemoryNode::new(
AgentId::nil(),
MemoryType::Semantic,
content.into(),
vec![0.1_f32; 8],
);
n.salience = sal;
db.store(n).unwrap();
}
// DESC: highest salience first, LIMIT keeps the top 2.
assert_eq!(
contents(
&db,
"RECALL WHERE type = semantic ORDER BY salience DESC LIMIT 2"
),
vec!["high".to_string(), "mid".to_string()]
);
// ASC: lowest first.
assert_eq!(
contents(
&db,
"RECALL WHERE type = semantic ORDER BY salience ASC LIMIT 2"
),
vec!["low".to_string(), "mid".to_string()]
);
// No direction defaults to ASC.
assert_eq!(
contents(
&db,
"RECALL WHERE type = semantic ORDER BY salience LIMIT 1"
),
vec!["low".to_string()]
);
}
}
#[cfg(test)]
mod metrics_snapshot_tests {
use super::*;
/// The metrics snapshot counts writes and reads, sizes the store and index,
/// and reflects buffer-pool activity.
#[test]
fn metrics_track_writes_reads_and_cache() {
let dir = tempfile::tempdir().unwrap();
let db = MenteDb::open(dir.path()).unwrap();
for i in 0..3 {
db.store(MemoryNode::new(
AgentId::nil(),
MemoryType::Semantic,
format!("fact {i}"),
vec![0.1_f32 + i as f32 * 0.1; 8],
))
.unwrap();
}
// Read paths: recall (assembles a window) and query (raw).
let _ = db.recall("RECALL memories LIMIT 10").unwrap();
let _ = db.query("RECALL LIMIT 10").unwrap();
// Exercise the hybrid vector-search path so search latency is recorded
// (this path is not counted in recalls, only timed).
let _ = db.recall_similar(&[0.15_f32; 8], 3).unwrap();
let m = db.metrics();
assert_eq!(m.stores, 3, "three writes counted");
assert_eq!(m.recalls, 2, "recall + query counted as reads");
assert!(
m.avg_store_latency_us > 0,
"store latency EMA populated after writes"
);
assert!(
m.avg_search_latency_us > 0,
"search latency EMA populated after a hybrid search"
);
assert_eq!(m.memory_count, 3);
assert_eq!(m.vector_index_size, 3, "three vectors indexed");
assert_eq!(m.graph_nodes, 3, "three graph nodes");
assert!(m.storage_bytes > 0, "pages written to disk");
assert!(
m.buffer_pool_hits + m.buffer_pool_misses > 0,
"reads exercised the buffer pool"
);
assert_eq!(m.standing_rules, 0, "no pins yet");
}
}
#[cfg(test)]
mod entity_boost_tests {
use super::*;
/// With a query-named entity linked (via a `Derived` edge) to a lower-ranked
/// memory, the boost lifts that memory above an unrelated higher-scored one.
/// This exercises the whole leg: `entity:<name>` tag lookup, graph adjacency,
/// score bump, and re-sort, on a synthetic corpus with no LLM.
#[test]
fn boost_lifts_entity_linked_memory() {
let dir = tempfile::tempdir().unwrap();
let db = MenteDb::open(dir.path()).unwrap();
let kafka_mem = MemoryNode::new(
AgentId::nil(),
MemoryType::Episodic,
"we chose kafka for the pipeline".into(),
vec![0.1_f32; 8],
);
let kafka_id = kafka_mem.id;
let other_mem = MemoryNode::new(
AgentId::nil(),
MemoryType::Episodic,
"the sky is blue".into(),
vec![0.2_f32; 8],
);
let other_id = other_mem.id;
let mut entity = MemoryNode::new(
AgentId::nil(),
MemoryType::Semantic,
"Kafka, a message broker".into(),
vec![0.3_f32; 8],
);
entity.tags.push("entity:kafka".into());
let entity_id = entity.id;
db.store(kafka_mem.clone()).unwrap();
db.store(other_mem.clone()).unwrap();
db.store(entity).unwrap();
// Entity node -> Derived -> the episodic it was extracted from, exactly as
// enrichment records it.
db.relate(MemoryEdge {
source: entity_id,
target: kafka_id,
edge_type: EdgeType::Derived,
weight: 1.0,
created_at: 0,
valid_from: None,
valid_until: None,
label: Some("enrichment".into()),
})
.unwrap();
// Baseline ranking puts the unrelated memory ahead.
let mut scored = vec![
ScoredMemory {
memory: other_mem,
score: 0.50,
},
ScoredMemory {
memory: kafka_mem,
score: 0.40,
},
];
db.apply_entity_boost(&mut scored, "did we pick kafka");
assert_eq!(
scored[0].memory.id, kafka_id,
"the kafka-linked memory should rank first after the entity boost"
);
let boosted = scored.iter().find(|s| s.memory.id == kafka_id).unwrap();
assert!(
(boosted.score - 0.55).abs() < 1e-6,
"the linked memory should gain exactly the boost weight"
);
assert!(
scored.iter().any(|s| s.memory.id == other_id),
"the unrelated memory is kept, just reordered"
);
}
/// A query that names no known entity leaves ranking untouched.
#[test]
fn no_entity_in_query_is_a_noop() {
let dir = tempfile::tempdir().unwrap();
let db = MenteDb::open(dir.path()).unwrap();
let a = MemoryNode::new(
AgentId::nil(),
MemoryType::Episodic,
"alpha".into(),
vec![0.1_f32; 8],
);
let b = MemoryNode::new(
AgentId::nil(),
MemoryType::Episodic,
"beta".into(),
vec![0.2_f32; 8],
);
let (aid, bid) = (a.id, b.id);
let mut scored = vec![
ScoredMemory {
memory: a,
score: 0.50,
},
ScoredMemory {
memory: b,
score: 0.40,
},
];
db.apply_entity_boost(&mut scored, "nothing here resolves to an entity");
assert_eq!(scored[0].memory.id, aid);
assert_eq!(scored[1].memory.id, bid);
}
}
#[cfg(test)]
mod standing_rule_policy_tests {
use super::*;
fn always_rule(content: &str, emb: Vec<f32>, manual: bool) -> MemoryNode {
let mut n = MemoryNode::new(AgentId::nil(), MemoryType::Semantic, content.into(), emb);
n.tags.push("scope:always".into());
if manual {
n.tags.push("source:manual".into());
}
n
}
fn always_count(db: &MenteDb) -> usize {
db.memory_ids()
.iter()
.filter_map(|id| db.get_memory(*id).ok())
.filter(|n| n.tags.iter().any(|t| t == "scope:always"))
.count()
}
/// Regression: un-pinning a rule (removing scope:always and re-storing) must
/// drop the standing-rule count. The tag index used to only add tags, so the
/// count stayed stale (the admin dashboard showed 261 when the account had 0).
#[test]
fn standing_rule_count_drops_after_unpin() {
let dir = tempfile::tempdir().unwrap();
let db = MenteDb::open(dir.path()).unwrap();
let rule = always_rule("always use tabs", vec![0.2_f32; 8], false);
let id = rule.id;
db.store(rule).unwrap();
assert_eq!(db.metrics().standing_rules, 1);
// Un-pin exactly as the prune does: strip the tag, re-store the same id.
let mut m = db.get_memory(id).unwrap();
m.tags.retain(|t| t != "scope:always");
db.store(m).unwrap();
assert_eq!(
db.metrics().standing_rules,
0,
"un-pinned rule must not be counted"
);
assert_eq!(
always_count(&db),
0,
"the node itself no longer carries the tag"
);
}
/// Re-pinning a rule that already exists (same embedding, reworded) does not
/// grow the always-list.
#[test]
fn dedup_skips_near_identical_standing_rule() {
let dir = tempfile::tempdir().unwrap();
let db = MenteDb::open(dir.path()).unwrap();
db.store(always_rule("always use tabs", vec![0.5_f32; 8], true))
.unwrap();
db.store(always_rule("tabs, always", vec![0.5_f32; 8], true))
.unwrap();
assert_eq!(
always_count(&db),
1,
"a near-identical standing rule must not be inserted twice"
);
}
/// A genuinely different standing rule is still pinned.
#[test]
fn dedup_allows_a_distinct_standing_rule() {
let dir = tempfile::tempdir().unwrap();
let db = MenteDb::open(dir.path()).unwrap();
let mut e1 = vec![0.0_f32; 8];
e1[0] = 1.0;
let mut e2 = vec![0.0_f32; 8];
e2[1] = 1.0;
db.store(always_rule("always use tabs", e1, true)).unwrap();
db.store(always_rule("never deploy on friday", e2, true))
.unwrap();
assert_eq!(
always_count(&db),
2,
"a distinct standing rule is still stored"
);
}
/// A hand-pinned always-list beyond the cap is demoted down to always_max.
#[test]
fn prune_caps_the_always_list() {
let dir = tempfile::tempdir().unwrap();
let cfg = CognitiveConfig {
always_max: 3,
always_dedup: false, // isolate the cap from dedup
..Default::default()
};
let db = MenteDb::open_with_config(dir.path(), cfg).unwrap();
for i in 0..5 {
let mut e = vec![0.0_f32; 8];
e[i] = 1.0;
db.store(always_rule(&format!("manual rule {i}"), e, true))
.unwrap();
}
assert_eq!(always_count(&db), 5);
let report = db.prune_standing_rules().unwrap();
assert_eq!(report.capped, 2, "two rules over the cap are demoted");
assert_eq!(
always_count(&db),
3,
"the always-list is bounded at always_max"
);
}
}
#[cfg(test)]
mod distinct_owner_tests {
use super::*;
/// `distinct_owner_pairs` must enumerate every (user, agent) pair that holds
/// a memory, since enrichment loops over its result to scope derived
/// knowledge per owner pair. This is a unit test (not an integration test)
/// because the method is private.
#[test]
fn distinct_owner_pairs_returns_all_owner_agents() {
let dir = tempfile::tempdir().unwrap();
let db = MenteDb::open(dir.path()).unwrap();
let alice = AgentId::new();
let bob = AgentId::new();
db.store(MemoryNode::new(
alice,
MemoryType::Semantic,
"alice likes falcons".into(),
vec![0.1_f32; 8],
))
.unwrap();
db.store(MemoryNode::new(
bob,
MemoryType::Semantic,
"bob likes turtles".into(),
vec![0.2_f32; 8],
))
.unwrap();
let pairs = db.distinct_owner_pairs();
let agents: Vec<AgentId> = pairs.iter().map(|(_, a)| *a).collect();
assert!(
agents.contains(&alice),
"distinct owners must include alice"
);
assert!(agents.contains(&bob), "distinct owners must include bob");
}
/// Owner pairs are distinguished on BOTH axes: the same agent with two
/// different users yields two distinct pairs, so enrichment partitions per
/// user even under a shared agent.
#[test]
fn distinct_owner_pairs_splits_by_user_under_same_agent() {
let dir = tempfile::tempdir().unwrap();
let db = MenteDb::open(dir.path()).unwrap();
let agent = AgentId::new();
let ua = UserId::new();
let ub = UserId::new();
db.store(
MemoryNode::new(
agent,
MemoryType::Semantic,
"user a fact".into(),
vec![0.1_f32; 8],
)
.with_user_id(ua),
)
.unwrap();
db.store(
MemoryNode::new(
agent,
MemoryType::Semantic,
"user b fact".into(),
vec![0.2_f32; 8],
)
.with_user_id(ub),
)
.unwrap();
let pairs = db.distinct_owner_pairs();
assert!(
pairs.contains(&(ua, agent)),
"must include (user a, agent) pair"
);
assert!(
pairs.contains(&(ub, agent)),
"must include (user b, agent) pair"
);
assert_ne!(
(ua, agent),
(ub, agent),
"same agent, different users are distinct owner pairs"
);
}
/// A user profile belongs to exactly one (user, agent) owner. Reading one
/// owner's profile must never return another's, the account-level
/// `user_profile()` must return only the unowned/global one (never an
/// arbitrary owner's), and `profile_owners` must enumerate them all. This is
/// the fix for the owner-blind `user_profile()` that returned whichever
/// profile happened to be scanned first once several owners had one.
#[test]
fn profile_is_owner_scoped_and_enumerable() {
let dir = tempfile::tempdir().unwrap();
let db = MenteDb::open(dir.path()).unwrap();
let agent = AgentId::new();
let alice = UserId::new();
let bob = UserId::new();
db.store_user_profile("alice's profile", alice, agent)
.unwrap();
db.store_user_profile("bob's profile", bob, agent).unwrap();
db.store_user_profile("the shared profile", UserId::nil(), AgentId::nil())
.unwrap();
// Each owner reads exactly its own, never the other's.
assert_eq!(
db.user_profile_for(alice, agent).unwrap().content,
"alice's profile"
);
assert_eq!(
db.user_profile_for(bob, agent).unwrap().content,
"bob's profile"
);
// The account-level accessor returns only the unowned/global profile,
// deterministically, not alice's or bob's.
assert_eq!(db.user_profile().unwrap().content, "the shared profile");
// An owner with no profile gets None, never a fallback to someone else's.
assert!(db.user_profile_for(UserId::new(), agent).is_none());
// Every owner with a profile is enumerable.
let owners = db.profile_owners();
assert_eq!(owners.len(), 3);
assert!(owners.contains(&(alice, agent)));
assert!(owners.contains(&(bob, agent)));
assert!(owners.contains(&(UserId::nil(), AgentId::nil())));
}
}
#[cfg(test)]
mod reranker_integration_tests {
use super::*;
use crate::reranker::{RerankCandidate, Reranker};
fn now_us() -> u64 {
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_micros() as u64
}
/// A test reranker that promotes whichever candidate mentions "gardening",
/// regardless of its first-pass score, so the assertion isolates the rerank
/// stage from the vector/BM25 signals.
struct PromoteGardening;
impl Reranker for PromoteGardening {
fn rerank(&self, _query: &str, candidates: &[RerankCandidate<'_>]) -> Vec<(MemoryId, f32)> {
candidates
.iter()
.map(|c| {
let s = if c.content.contains("gardening") {
1.0
} else {
0.0
};
(c.id, s)
})
.collect()
}
}
#[test]
fn reranker_overrides_first_pass_order() {
let dir = tempfile::tempdir().unwrap();
let mut db = MenteDb::open(dir.path()).unwrap();
// A is far from the query vector and lacks the query term; B is closest to
// the query vector AND contains "espresso", so the first pass favors B on
// both signals.
let a = MemoryNode::new(
AgentId::nil(),
MemoryType::Semantic,
"gardening soil and a trowel".into(),
vec![1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
);
let b = MemoryNode::new(
AgentId::nil(),
MemoryType::Semantic,
"espresso machine and grinder".into(),
vec![0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
);
let a_id = a.id;
let b_id = b.id;
db.store(a).unwrap();
db.store(b).unwrap();
let query_emb = vec![0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0];
let at = now_us();
// First pass: no reranker installed, B wins.
let base = db
.recall_hybrid_at(&query_emb, Some("espresso"), 2, at, None, None, None)
.unwrap();
assert_eq!(
base[0].0, b_id,
"without a reranker the first pass favors B"
);
// Install the reranker; it must override the first-pass order and lift A.
db.set_reranker(Box::new(PromoteGardening));
assert!(db.has_reranker());
let reranked = db
.recall_hybrid_at(&query_emb, Some("espresso"), 2, at, None, None, None)
.unwrap();
assert_eq!(
reranked[0].0, a_id,
"the installed reranker must drive the final order"
);
// Clearing it restores the first-pass order.
db.clear_reranker();
let restored = db
.recall_hybrid_at(&query_emb, Some("espresso"), 2, at, None, None, None)
.unwrap();
assert_eq!(
restored[0].0, b_id,
"clearing the reranker restores first pass"
);
}
/// With no query text the rerank stage is skipped even when a reranker is
/// installed, so a text-blind vector recall is unaffected.
#[test]
fn no_query_text_skips_rerank() {
let dir = tempfile::tempdir().unwrap();
let mut db = MenteDb::open(dir.path()).unwrap();
let a = MemoryNode::new(
AgentId::nil(),
MemoryType::Semantic,
"gardening soil".into(),
vec![1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
);
let b = MemoryNode::new(
AgentId::nil(),
MemoryType::Semantic,
"espresso machine".into(),
vec![0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
);
let b_id = b.id;
db.store(a).unwrap();
db.store(b).unwrap();
db.set_reranker(Box::new(PromoteGardening));
// No query text: pure vector recall closest to B, rerank not applied.
let out = db
.recall_hybrid_at(
&[0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
None,
2,
now_us(),
None,
None,
None,
)
.unwrap();
assert_eq!(out[0].0, b_id, "no query text means no rerank");
}
/// With MMR enabled, recall should pull a distinct memory into the top-k
/// ahead of a near-duplicate of the top hit; with MMR off (default), the
/// near-duplicate keeps its by-relevance slot.
#[test]
fn mmr_diversifies_top_k() {
// a: closest to the query. b: a near-duplicate of a (same direction),
// slightly less relevant. c: a distinct direction, less relevant still.
let a_emb = vec![1.0, 0.1, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0];
let b_emb = vec![0.98, 0.16, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0];
let c_emb = vec![0.1, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0];
let query = vec![1.0, 0.1, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0];
let fill = |db: &MenteDb| -> (MemoryId, MemoryId, MemoryId) {
let a = MemoryNode::new(
AgentId::nil(),
MemoryType::Semantic,
"alpha".into(),
a_emb.clone(),
);
let b = MemoryNode::new(
AgentId::nil(),
MemoryType::Semantic,
"alpha prime".into(),
b_emb.clone(),
);
let c = MemoryNode::new(
AgentId::nil(),
MemoryType::Semantic,
"distinct".into(),
c_emb.clone(),
);
let (ai, bi, ci) = (a.id, b.id, c.id);
db.store(a).unwrap();
db.store(b).unwrap();
db.store(c).unwrap();
(ai, bi, ci)
};
// Default (mmr_lambda = 1.0): the two nearest fill the top 2, diverse c is out.
let dir1 = tempfile::tempdir().unwrap();
let db1 = MenteDb::open(dir1.path()).unwrap();
let (_a1, _b1, c1) = fill(&db1);
let base = db1
.recall_hybrid_at(&query, None, 2, now_us(), None, None, None)
.unwrap();
let base_ids: Vec<MemoryId> = base.iter().map(|(id, _)| *id).collect();
assert!(
!base_ids.contains(&c1),
"without MMR the diverse memory is out of the top 2"
);
// MMR on (lambda = 0.5): the diverse memory is pulled into the top 2.
let dir2 = tempfile::tempdir().unwrap();
let cfg = CognitiveConfig {
mmr_lambda: 0.5,
..CognitiveConfig::default()
};
let db2 = MenteDb::open_with_config(dir2.path(), cfg).unwrap();
let (a2, _b2, c2) = fill(&db2);
let mmr = db2
.recall_hybrid_at(&query, None, 2, now_us(), None, None, None)
.unwrap();
let mmr_ids: Vec<MemoryId> = mmr.iter().map(|(id, _)| *id).collect();
assert_eq!(
mmr_ids.first().copied(),
Some(a2),
"most relevant is still first"
);
assert!(
mmr_ids.contains(&c2),
"MMR pulls the diverse memory into the top 2"
);
}
/// The contextual-retrieval hook: a term that appears only in a memory's
/// context (not its content) still finds it via BM25, and the stored content
/// is unchanged.
#[test]
fn contextual_hook_makes_context_terms_findable() {
let dir = tempfile::tempdir().unwrap();
let db = MenteDb::open(dir.path()).unwrap();
// "kubernetes" is in the context, never in the content.
let node = MemoryNode::new(
AgentId::nil(),
MemoryType::Semantic,
"rolled the pods back to the previous tag".into(),
vec![0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8],
)
.with_context("incident in the kubernetes cluster");
let id = node.id;
db.store(node).unwrap();
// A decoy sharing neither the content nor the context terms.
db.store(MemoryNode::new(
AgentId::nil(),
MemoryType::Semantic,
"lunch plans for friday".into(),
vec![0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1],
))
.unwrap();
let hits = db
.recall_hybrid_at(
&[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8],
Some("kubernetes"),
5,
now_us(),
None,
None,
None,
)
.unwrap();
let ids: Vec<MemoryId> = hits.iter().map(|(id, _)| *id).collect();
assert!(
ids.contains(&id),
"a term only in the context must find the memory via BM25"
);
// The stored content is exactly what was written, no context leaked in.
assert_eq!(
db.get_memory(id).unwrap().content,
"rolled the pods back to the previous tag"
);
}
}