shodh-memory 0.2.0

Persistent cognitive memory for AI agents and robots — Hebbian learning, knowledge graph, spatial recall. Zenoh/ROS2 native. Single binary, runs offline.
Documentation
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
//! Recall and Retrieval Handlers
//!
//! Memory retrieval endpoints including:
//! - Semantic recall (vector similarity)
//! - Hybrid recall (semantic + graph)
//! - Proactive context surfacing
//! - Tag-based and date-based recall
//! - Tracked retrieval with Hebbian feedback

use axum::{extract::State, response::Json};
use serde::{Deserialize, Serialize};
use tracing::info;

use super::state::MultiUserMemoryManager;
use super::types::{
    MemoryEvent, RecallExperience, RecallFact, RecallLineageEdge, RecallMemory, RecallRequest,
    RecallResponse, RecallTodo, ReinforceFeedbackRequest, RetrieveResponse, TrackedRetrieveRequest,
    TrackedRetrieveResponse,
};
use super::utils::{
    has_sufficient_alpha_ratio, is_bare_question, is_boilerplate_response,
    is_formatted_recall_output, is_tool_output_noise, strip_system_noise,
};
use crate::errors::{AppError, ValidationErrorExt};
use crate::memory::feedback;
use crate::similarity::cosine_similarity;
use dashmap::DashMap;
// Note: compute_relevance removed - using unified 5-layer pipeline scoring instead
use crate::memory::segmentation::{InputSource, SegmentationEngine};
use crate::memory::sessions::SessionEvent;
use crate::memory::storage::SearchCriteria;
use crate::memory::types::GeoFilter;
use crate::memory::types::MemoryId;
use crate::memory::{
    Experience, ExperienceType, Query as MemoryQuery, RetrievalMode, SharedMemory,
};
use crate::memory::{ProspectiveTrigger, TodoStatus};
use crate::metrics;
use crate::relevance;
use crate::validation;

/// Application state type alias
pub type AppState = std::sync::Arc<MultiUserMemoryManager>;

/// Map API mode string to RetrievalMode enum.
/// Defaults to Hybrid for unknown values (backward compat).
fn parse_retrieval_mode(mode: &str) -> RetrievalMode {
    match mode {
        "semantic" | "similarity" => RetrievalMode::Similarity,
        "associative" => RetrievalMode::Associative,
        "temporal" => RetrievalMode::Temporal,
        "causal" => RetrievalMode::Causal,
        "spatial" => RetrievalMode::Spatial,
        "mission" => RetrievalMode::Mission,
        "action_outcome" | "action-outcome" => RetrievalMode::ActionOutcome,
        _ => RetrievalMode::Hybrid,
    }
}

// =============================================================================
// CONTEXT SUMMARY TYPES
// =============================================================================

/// Context summary request
#[derive(Debug, Deserialize)]
pub struct ContextSummaryRequest {
    pub user_id: String,
    #[serde(default = "default_true")]
    pub include_decisions: bool,
    #[serde(default = "default_true")]
    pub include_learnings: bool,
    #[serde(default = "default_true")]
    pub include_context: bool,
    #[serde(default = "default_max_items")]
    pub max_items: usize,
}

fn default_true() -> bool {
    true
}

fn default_max_items() -> usize {
    5
}

/// Summary item - simplified memory for context
#[derive(Debug, Serialize)]
pub struct SummaryItem {
    pub id: String,
    pub content: String,
    pub importance: f32,
    pub created_at: String,
}

/// Context summary response - categorized memories for session bootstrap
#[derive(Debug, Serialize)]
pub struct ContextSummaryResponse {
    pub total_memories: usize,
    pub decisions: Vec<SummaryItem>,
    pub learnings: Vec<SummaryItem>,
    pub context: Vec<SummaryItem>,
    pub patterns: Vec<SummaryItem>,
    pub errors: Vec<SummaryItem>,
}

// =============================================================================
// PROACTIVE CONTEXT TYPES
// =============================================================================

/// Request for proactive context - returns relevant memories + triggered reminders
#[derive(Debug, Deserialize)]
pub struct ProactiveContextRequest {
    pub user_id: String,
    pub context: String,
    #[serde(default = "default_proactive_max_results")]
    pub max_results: usize,
    /// Minimum semantic similarity threshold (0.0-1.0)
    #[serde(default = "default_semantic_threshold")]
    pub semantic_threshold: f32,
    /// Weight for entity matching in relevance scoring
    #[serde(default = "default_entity_weight")]
    pub entity_match_weight: f32,
    /// Weight for recency boost
    #[serde(default = "default_recency_weight")]
    pub recency_weight: f32,
    /// Filter to specific memory types
    #[serde(default)]
    pub memory_types: Vec<String>,
    /// Whether to auto-ingest the context as a Conversation memory
    #[serde(default = "default_true")]
    pub auto_ingest: bool,
    /// Agent's previous response (for implicit feedback extraction)
    #[serde(default)]
    pub previous_response: Option<String>,
    /// User's followup message after agent response (for delayed signals)
    #[serde(default)]
    pub user_followup: Option<String>,
    /// Tool/actuator actions performed since last proactive_context call.
    /// Used for tool-aware feedback attribution: matches actions against
    /// previously surfaced memories to detect concrete usage.
    /// Claude Code: collected by hooks (Read, Edit, Bash calls).
    /// Robotics: constructed from action-outcome Experience fields.
    #[serde(default)]
    pub tool_actions: Vec<crate::memory::feedback::ToolAction>,
}

fn default_proactive_max_results() -> usize {
    5
}

fn default_semantic_threshold() -> f32 {
    0.05 // Minimum absolute score for quality gate on composite pipeline scores
}

fn default_entity_weight() -> f32 {
    0.4
}

fn default_recency_weight() -> f32 {
    0.2
}

/// Feedback processing results
#[derive(Debug, Serialize)]
pub struct FeedbackProcessed {
    pub memories_evaluated: usize,
    pub reinforced: Vec<String>,
    pub weakened: Vec<String>,
}

/// Surfaced memory in proactive context response
#[derive(Debug, Serialize)]
pub struct ProactiveSurfacedMemory {
    pub id: String,
    pub content: String,
    pub memory_type: String,
    pub score: f32,
    pub importance: f32,
    pub created_at: String,
    pub tags: Vec<String>,
    pub tier: String,
    /// Why this memory was surfaced ("semantic", "entity", "combined")
    pub relevance_reason: String,
    /// Entities from this memory that matched the query context
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub matched_entities: Vec<String>,
    /// Embedding for semantic feedback (not serialized to response)
    #[serde(skip)]
    pub embedding: Vec<f32>,
}

/// Entity detected in the query context
#[derive(Debug, Clone, Serialize)]
pub struct DetectedEntityInfo {
    pub name: String,
    pub entity_type: String,
}

/// Todo item in proactive context response
#[derive(Debug, Serialize)]
pub struct ProactiveTodoItem {
    pub id: String,
    pub short_id: String,
    pub content: String,
    pub status: String,
    pub priority: String,
    pub project: Option<String>,
    pub due_date: Option<String>,
    pub relevance_reason: String,
    /// Semantic similarity score (0.0 - 1.0)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub similarity_score: Option<f32>,
}

/// Individual reminder in response
#[derive(Debug, Serialize)]
pub struct ReminderItem {
    pub id: String,
    pub content: String,
    pub trigger_type: String,
    pub status: String,
    pub due_at: Option<chrono::DateTime<chrono::Utc>>,
    pub created_at: chrono::DateTime<chrono::Utc>,
    pub triggered_at: Option<chrono::DateTime<chrono::Utc>>,
    pub dismissed_at: Option<chrono::DateTime<chrono::Utc>>,
    pub priority: u8,
    pub tags: Vec<String>,
    pub overdue_seconds: Option<i64>,
}

/// Consolidated fact surfaced in proactive context
#[derive(Debug, Serialize)]
pub struct ProactiveFact {
    pub id: String,
    pub fact: String,
    pub confidence: f32,
    pub support_count: usize,
    pub related_entities: Vec<String>,
}

/// Response for proactive context
#[derive(Debug, Serialize)]
pub struct ProactiveContextResponse {
    /// Relevant memories based on context
    pub memories: Vec<ProactiveSurfacedMemory>,
    /// Due time-based reminders
    pub due_reminders: Vec<ReminderItem>,
    /// Context-triggered reminders (keyword match)
    pub context_reminders: Vec<ReminderItem>,
    /// Total counts
    pub memory_count: usize,
    pub reminder_count: usize,
    /// ID of auto-ingested memory (if auto_ingest=true)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub ingested_memory_id: Option<String>,
    /// Feedback processing results (if previous_response was provided)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub feedback_processed: Option<FeedbackProcessed>,
    /// Relevant todos based on context
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub relevant_todos: Vec<ProactiveTodoItem>,
    /// Todo count
    #[serde(default)]
    pub todo_count: usize,
    /// Consolidated facts from knowledge graph
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub relevant_facts: Vec<ProactiveFact>,
    /// Processing latency in milliseconds
    pub latency_ms: f64,
    /// Entities detected in the query context
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub detected_entities: Vec<DetectedEntityInfo>,
    /// Number of temporal credits applied from multi-turn feedback window
    #[serde(skip_serializing_if = "Option::is_none")]
    pub temporal_credits_applied: Option<u32>,
}

// =============================================================================
// REINFORCE FEEDBACK TYPES
// =============================================================================

/// Response from reinforcement
#[derive(Debug, Serialize)]
pub struct ReinforceFeedbackResponse {
    pub memories_processed: usize,
    pub associations_strengthened: usize,
    pub importance_boosts: usize,
    pub importance_decays: usize,
}

// =============================================================================
// RECALL BY TAGS/DATE TYPES (local - not in shared types.rs)
// =============================================================================

/// Recall memories by tags
#[derive(Debug, Deserialize)]
pub struct RecallByTagsRequest {
    pub user_id: String,
    /// Tags to search for (returns memories matching ANY of these tags)
    pub tags: Vec<String>,
    /// Maximum number of results (default: 50)
    pub limit: Option<usize>,
}

/// Recall memories by date range
#[derive(Debug, Deserialize)]
pub struct RecallByDateRequest {
    pub user_id: String,
    /// Start of date range (inclusive) - ISO 8601 format
    pub start: chrono::DateTime<chrono::Utc>,
    /// End of date range (inclusive) - ISO 8601 format
    pub end: chrono::DateTime<chrono::Utc>,
    /// Maximum number of results (default: 50)
    pub limit: Option<usize>,
}

// =============================================================================
// MAIN RECALL HANDLER
// =============================================================================

/// POST /api/recall - Semantic + associative hybrid recall
///
/// Uses a hybrid retrieval strategy:
/// 1. Semantic search via vector similarity
/// 2. Graph traversal via spreading activation
/// 3. Hebbian boosting for frequently co-retrieved memories
#[tracing::instrument(skip(state), fields(user_id = %req.user_id, query = %req.query))]
pub async fn recall(
    State(state): State<AppState>,
    Json(req): Json<RecallRequest>,
) -> Result<Json<RecallResponse>, AppError> {
    let op_start = std::time::Instant::now();
    validation::validate_user_id(&req.user_id).map_validation_err("user_id")?;
    validation::validate_max_results(req.limit).map_validation_err("limit")?;

    // Validate and build geo_filter from lat/lon/radius triple
    let geo_filter = match (req.geo_lat, req.geo_lon, req.geo_radius_meters) {
        (Some(lat), Some(lon), Some(radius)) => {
            validation::validate_geo_filter(lat, lon, radius).map_validation_err("geo_filter")?;
            Some(GeoFilter::new(lat, lon, radius))
        }
        (None, None, None) => None,
        _ => {
            return Err(AppError::InvalidInput {
                field: "geo_filter".to_string(),
                reason: "geo_lat, geo_lon, and geo_radius_meters must all be provided together"
                    .to_string(),
            });
        }
    };

    // Build reward range from min/max pair
    let reward_range = match (req.reward_min, req.reward_max) {
        (Some(min), Some(max)) => Some((min, max)),
        (Some(min), None) => Some((min, 1.0)),
        (None, Some(max)) => Some((-1.0, max)),
        (None, None) => None,
    };

    let memory = state
        .get_user_memory(&req.user_id)
        .map_err(AppError::Internal)?;

    let _graph = state
        .get_user_graph(&req.user_id)
        .map_err(AppError::Internal)?;

    let limit = req.limit;
    let mode = req.mode.clone();
    let retrieval_mode_for_recall = parse_retrieval_mode(&mode);

    // SESSION-SCOPED RETRIEVAL: resolve session_id → time_range before spawn_blocking.
    // When session_id is provided, look up the session's time window and set
    // retrieval_mode to Temporal so temporal_search() uses the date range.
    let session_time_range = req.session_id.as_ref().and_then(|sid| {
        use crate::memory::sessions::SessionId;
        let session_id = SessionId(uuid::Uuid::parse_str(sid).ok()?);
        state.session_store().get_session_time_range(&session_id)
    });
    let session_id_for_recall = req.session_id.clone();

    // If session_id resolved to a time range, force Temporal mode
    let retrieval_mode_for_recall = if session_time_range.is_some() {
        RetrievalMode::Temporal
    } else {
        retrieval_mode_for_recall
    };

    // Clone robotics filter fields for the spawn_blocking move boundary
    let robot_id_for_recall = req.robot_id.clone();
    let mission_id_for_recall = req.mission_id.clone();
    let action_type_for_recall = req.action_type.clone();
    let outcome_type_for_recall = req.outcome_type.clone();
    let terrain_type_for_recall = req.terrain_type.clone();
    let tags_for_recall = req.tags.clone();
    let failures_only_for_recall = req.failures_only.unwrap_or(false);

    // PROSPECTIVE MEMORY + RECALL: Run inside a single spawn_blocking to share
    // the computed query embedding between prospective semantic matching and recall.
    // This fixes C5 (keyword-only → semantic) and sets up prospective_signals for boosting.
    let prospective_for_recall = state.prospective_store.clone();
    let memory_for_recall = memory.clone();
    let user_id_for_recall = req.user_id.clone();
    let query_for_recall = req.query.clone();

    let (mut memories, triggered_reminders, _prospective_signals) =
        tokio::task::spawn_blocking(move || {
            let memory_guard = memory_for_recall.read();

            // 1. Compute query embedding (reused for prospective + recall)
            let query_embedding_opt = memory_guard
                .compute_embedding(&query_for_recall)
                .ok();

            // 2. Semantic prospective matching (fixes C5: was keyword-only)
            // Skip semantic matching if embedding failed — keyword matching still works
            let matched_tasks = if let Some(ref query_embedding) = query_embedding_opt {
                let embed_fn =
                    |text: &str| -> Option<Vec<f32>> { memory_guard.compute_embedding(text).ok() };
                prospective_for_recall
                    .check_context_triggers_semantic(
                        &user_id_for_recall,
                        &query_for_recall,
                        query_embedding,
                    embed_fn,
                )
                .unwrap_or_default()
            } else {
                tracing::warn!("Embedding failed — skipping semantic prospective matching, recall proceeds via text");
                Vec::new()
            };

            // 3. Build signals and response reminders from matched tasks
            let mut signals: Vec<String> = Vec::new();
            let reminders: Vec<super::types::RecallReminder> = matched_tasks
                .into_iter()
                .map(|(task, score)| {
                    let keywords =
                        if let ProspectiveTrigger::OnContext { keywords, .. } = &task.trigger {
                            keywords.clone()
                        } else {
                            vec![]
                        };

                    signals.push(task.content.clone());
                    for kw in &keywords {
                        signals.push(kw.clone());
                    }

                    let match_type = if score >= 1.0 {
                        "keyword_match".to_string()
                    } else {
                        format!("semantic ({:.2})", score)
                    };

                    super::types::RecallReminder {
                        id: task.id.0.to_string(),
                        content: task.content,
                        keywords,
                        match_type,
                        priority: task.priority,
                        created_at: task.created_at.to_rfc3339(),
                    }
                })
                .collect();

            let prospective_signals = if signals.is_empty() {
                None
            } else {
                Some(signals)
            };

            // 4. Execute recall with prospective signals + robotics filters
            let query = MemoryQuery {
                user_id: Some(user_id_for_recall),
                query_text: Some(query_for_recall),
                max_results: limit,
                retrieval_mode: retrieval_mode_for_recall,
                prospective_signals: prospective_signals.clone(),
                session_id: session_id_for_recall,
                time_range: session_time_range,
                robot_id: robot_id_for_recall,
                mission_id: mission_id_for_recall,
                geo_filter,
                action_type: action_type_for_recall,
                reward_range,
                outcome_type: outcome_type_for_recall,
                failures_only: failures_only_for_recall,
                terrain_type: terrain_type_for_recall,
                tags: tags_for_recall,
                ..Default::default()
            };

            let memories = memory_guard.recall(&query).unwrap_or_default();

            (memories, reminders, prospective_signals)
        })
        .await
        .map_err(|e| AppError::Internal(anyhow::anyhow!("Blocking task panicked: {e}")))?;

    let triggered_reminder_count = triggered_reminders.len();
    if triggered_reminder_count > 0 {
        tracing::debug!(
            user_id = %req.user_id,
            count = triggered_reminder_count,
            "Context-triggered reminders found - future intentions will boost related memories"
        );
    }

    // Convert to response format with normalized scores.
    // Raw pipeline scores (RRF fusion × hebbian × recency × feedback) cluster in
    // 0.01-0.10 range, making percentage display useless (everything shows 1-5%).
    // Normalize relative to top score so results span 0-95% for meaningful display.
    let mut raw_scores: Vec<f32> = memories
        .iter()
        .map(|m| m.score.unwrap_or_else(|| m.salience_score_with_access()))
        .collect();

    // Lineage-aware retrieval boost: memories connected by causal chains to
    // other recalled memories receive a score boost proportional to edge confidence.
    // This makes causally related memories cluster together in results.
    if memories.len() >= 2 {
        let memory_for_lineage = memory.clone();
        let user_id_for_lineage = req.user_id.clone();
        let memory_ids: Vec<String> = memories.iter().map(|m| m.id.0.to_string()).collect();

        if let Ok(boosts) = tokio::task::spawn_blocking({
            let memory_ids = memory_ids.clone();
            move || -> Vec<f32> {
                let memory_guard = memory_for_lineage.read();
                let lineage = memory_guard.lineage_graph();
                let mut boosts = vec![0.0_f32; memory_ids.len()];
                let id_to_idx: std::collections::HashMap<&str, usize> = memory_ids
                    .iter()
                    .enumerate()
                    .map(|(i, id)| (id.as_str(), i))
                    .collect();

                for (idx, id_str) in memory_ids.iter().enumerate() {
                    let Ok(uuid) = uuid::Uuid::parse_str(id_str) else {
                        continue;
                    };
                    let mid = crate::memory::MemoryId(uuid);

                    // Check outgoing edges to other recalled memories
                    if let Ok(edges) = lineage.get_edges_from(&user_id_for_lineage, &mid) {
                        for edge in &edges {
                            if edge.confidence < crate::constants::LINEAGE_RETRIEVAL_MIN_CONFIDENCE
                            {
                                continue;
                            }
                            let to_str = edge.to.0.to_string();
                            if let Some(&target_idx) = id_to_idx.get(to_str.as_str()) {
                                if target_idx != idx {
                                    let boost = edge.confidence
                                        * crate::constants::LINEAGE_RETRIEVAL_BOOST_SCALE;
                                    boosts[target_idx] = (boosts[target_idx] + boost)
                                        .min(crate::constants::LINEAGE_RETRIEVAL_MAX_BOOST);
                                }
                            }
                        }
                    }

                    // Check incoming edges from other recalled memories
                    if let Ok(edges) = lineage.get_edges_to(&user_id_for_lineage, &mid) {
                        for edge in &edges {
                            if edge.confidence < crate::constants::LINEAGE_RETRIEVAL_MIN_CONFIDENCE
                            {
                                continue;
                            }
                            let from_str = edge.from.0.to_string();
                            if let Some(&source_idx) = id_to_idx.get(from_str.as_str()) {
                                if source_idx != idx {
                                    let boost = edge.confidence
                                        * crate::constants::LINEAGE_RETRIEVAL_BOOST_SCALE;
                                    boosts[source_idx] = (boosts[source_idx] + boost)
                                        .min(crate::constants::LINEAGE_RETRIEVAL_MAX_BOOST);
                                }
                            }
                        }
                    }
                }
                boosts
            }
        })
        .await
        {
            for (score, boost) in raw_scores.iter_mut().zip(boosts.iter()) {
                *score += boost;
            }
        }
    }

    // Lineage candidate expansion: inject causally-connected memories not in results.
    // Higher confidence bar (0.7) than boost (0.5) since we're adding new results.
    if !memories.is_empty() {
        let existing_ids: std::collections::HashSet<String> =
            memories.iter().map(|m| m.id.0.to_string()).collect();
        let memory_for_expansion = memory.clone();
        let user_id_for_expansion = req.user_id.clone();
        let existing_ids_clone = existing_ids.clone();
        let memory_ids_for_expansion: Vec<(String, f32)> = memories
            .iter()
            .zip(raw_scores.iter())
            .map(|(m, &s)| (m.id.0.to_string(), s))
            .collect();

        if let Ok(expanded) = tokio::task::spawn_blocking(move || {
            let memory_guard = memory_for_expansion.read();
            let lineage = memory_guard.lineage_graph();
            // Collect (memory, derived_score) pairs for connected memories not in results
            let mut candidates: Vec<(crate::memory::Memory, f32)> = Vec::new();

            for (id_str, source_score) in &memory_ids_for_expansion {
                let Ok(uuid) = uuid::Uuid::parse_str(id_str) else {
                    continue;
                };
                let mid = crate::memory::MemoryId(uuid);

                // Check both directions
                let mut edges = Vec::new();
                if let Ok(out) = lineage.get_edges_from(&user_id_for_expansion, &mid) {
                    edges.extend(out);
                }
                if let Ok(inc) = lineage.get_edges_to(&user_id_for_expansion, &mid) {
                    edges.extend(inc);
                }

                for edge in &edges {
                    if edge.confidence < crate::constants::LINEAGE_EXPANSION_MIN_CONFIDENCE {
                        continue;
                    }
                    // Get the other end of the edge
                    let other_id = if edge.from.0.to_string() == *id_str {
                        &edge.to
                    } else {
                        &edge.from
                    };
                    let other_str = other_id.0.to_string();
                    if existing_ids_clone.contains(&other_str) {
                        continue; // already in results
                    }
                    // Check we haven't already added this candidate
                    if candidates.iter().any(|(m, _)| m.id == *other_id) {
                        continue;
                    }
                    // Fetch from storage
                    if let Ok(mem) = memory_guard.get_memory(other_id) {
                        // Injected memories get 50% of source score scaled by edge confidence.
                        // A perfect edge (1.0) from the top result gives 50% of top score,
                        // ensuring injected memories sort below their source.
                        let derived_score = source_score * edge.confidence * 0.5;
                        candidates.push((mem, derived_score));
                        if candidates.len() >= crate::constants::LINEAGE_EXPANSION_MAX {
                            return candidates;
                        }
                    }
                }
                if candidates.len() >= crate::constants::LINEAGE_EXPANSION_MAX {
                    break;
                }
            }
            candidates
        })
        .await
        {
            for (mem, score) in expanded {
                raw_scores.push(score);
                memories.push(std::sync::Arc::new(mem));
            }
        }
    }

    let top_score = raw_scores.iter().cloned().fold(0.0_f32, f32::max);

    let recall_memories: Vec<RecallMemory> = memories
        .iter()
        .zip(raw_scores.iter())
        .map(|(m, &raw)| {
            let score = if top_score > 0.0 {
                (raw / top_score) * 0.95
            } else {
                0.0
            };
            RecallMemory {
                id: m.id.0.to_string(),
                experience: RecallExperience {
                    content: m.experience.content.clone(),
                    memory_type: Some(format!("{:?}", m.experience.experience_type)),
                    tags: m.experience.entities.clone(),
                },
                importance: m.importance(),
                created_at: m.created_at.to_rfc3339(),
                score,
                tier: format!("{:?}", m.tier),
            }
        })
        .collect();

    // Search todos semantically if query provided
    let todos: Vec<RecallTodo> = {
        // Compute embedding for todo search
        let query_for_embed = req.query.clone();
        let memory_for_embed = memory.clone();
        let embedding: Option<Vec<f32>> = tokio::task::spawn_blocking(move || {
            let guard = memory_for_embed.read();
            guard.compute_embedding(&query_for_embed).ok()
        })
        .await
        .ok()
        .flatten();

        if let Some(emb) = embedding {
            state
                .todo_store
                .search_similar(&req.user_id, &emb, 5)
                .unwrap_or_default()
                .into_iter()
                .filter(|(t, _)| {
                    matches!(
                        t.status,
                        TodoStatus::Todo | TodoStatus::InProgress | TodoStatus::Blocked
                    )
                })
                .map(|(t, score)| {
                    let project_name = t.project_id.as_ref().and_then(|pid| {
                        state
                            .todo_store
                            .get_project(&req.user_id, pid)
                            .ok()
                            .flatten()
                            .map(|p| p.name)
                    });
                    RecallTodo {
                        id: t.id.0.to_string(),
                        short_id: t.short_id(),
                        content: t.content.clone(),
                        status: format!("{:?}", t.status).to_lowercase(),
                        priority: t.priority.indicator().to_string(),
                        project: project_name,
                        due_date: t.due_date.map(|d| d.format("%Y-%m-%d").to_string()),
                        score,
                    }
                })
                .collect()
        } else {
            Vec::new()
        }
    };
    let todo_count = if todos.is_empty() {
        None
    } else {
        Some(todos.len())
    };

    // Fetch related semantic facts from entities mentioned in recalled memories
    let facts: Vec<RecallFact> = {
        // Collect all unique entities from recalled memories
        let mut all_entities: std::collections::HashSet<String> = std::collections::HashSet::new();
        for mem in &recall_memories {
            for tag in &mem.experience.tags {
                all_entities.insert(tag.to_lowercase());
            }
        }

        // Also extract simple entities from the query itself
        for word in req.query.split_whitespace() {
            let clean_word = word
                .trim_matches(|c: char| !c.is_alphanumeric())
                .to_lowercase();
            if clean_word.len() > 2 {
                all_entities.insert(clean_word);
            }
        }

        // Query the per-user fact store for facts related to these entities.
        // Uses MemorySystem's fact_store (same DB that list_facts/search_facts read from),
        // NOT state.fact_store which is a separate standalone DB.
        let entity_list: Vec<String> = all_entities.into_iter().take(10).collect();
        let found_facts = {
            let memory = memory.clone();
            let user_id = req.user_id.clone();
            tokio::task::spawn_blocking(move || {
                let memory_guard = memory.read();
                let mut facts = Vec::new();
                for entity in &entity_list {
                    if let Ok(entity_facts) = memory_guard.get_facts_by_entity(&user_id, entity, 5)
                    {
                        for fact in entity_facts {
                            facts.push(RecallFact {
                                id: fact.id.clone(),
                                fact: fact.fact.clone(),
                                confidence: fact.confidence,
                                support_count: fact.support_count,
                                related_entities: fact.related_entities.clone(),
                            });
                        }
                    }
                }
                facts
            })
            .await
            .unwrap_or_default()
        };

        // Deduplicate by fact ID and take top 5 by confidence
        let mut unique_facts: std::collections::HashMap<String, RecallFact> =
            std::collections::HashMap::new();
        for fact in found_facts {
            unique_facts.entry(fact.id.clone()).or_insert(fact);
        }
        let mut sorted_facts: Vec<RecallFact> = unique_facts.into_values().collect();
        sorted_facts.sort_by(|a, b| b.confidence.total_cmp(&a.confidence));
        sorted_facts.truncate(5);
        sorted_facts
    };
    let fact_count = if facts.is_empty() {
        None
    } else {
        Some(facts.len())
    };

    // Fetch lineage edges connecting recalled memories
    let lineage: Vec<RecallLineageEdge> = {
        let recalled_ids: std::collections::HashSet<String> =
            recall_memories.iter().map(|m| m.id.clone()).collect();
        if recalled_ids.len() >= 2 {
            let memory = memory.clone();
            let user_id = req.user_id.clone();
            let ids = recalled_ids.clone();
            tokio::task::spawn_blocking(move || {
                let memory_guard = memory.read();
                let lineage_graph = memory_guard.lineage_graph();
                let mut edges = Vec::new();
                let mut seen = std::collections::HashSet::new();
                for id in &ids {
                    let Ok(uuid) = uuid::Uuid::parse_str(id) else {
                        continue;
                    };
                    let mid = crate::memory::MemoryId(uuid);
                    if let Ok(from_edges) = lineage_graph.get_edges_from(&user_id, &mid) {
                        for edge in from_edges {
                            let to_str = edge.to.0.to_string();
                            if ids.contains(&to_str) && seen.insert(edge.id.clone()) {
                                edges.push(RecallLineageEdge {
                                    from: edge.from.0.to_string(),
                                    to: to_str,
                                    relation: format!("{:?}", edge.relation),
                                    confidence: edge.confidence,
                                });
                            }
                        }
                    }
                    if let Ok(to_edges) = lineage_graph.get_edges_to(&user_id, &mid) {
                        for edge in to_edges {
                            let from_str = edge.from.0.to_string();
                            if ids.contains(&from_str) && seen.insert(edge.id.clone()) {
                                edges.push(RecallLineageEdge {
                                    from: from_str,
                                    to: edge.to.0.to_string(),
                                    relation: format!("{:?}", edge.relation),
                                    confidence: edge.confidence,
                                });
                            }
                        }
                    }
                }
                edges
            })
            .await
            .unwrap_or_default()
        } else {
            Vec::new()
        }
    };
    let lineage_count = if lineage.is_empty() {
        None
    } else {
        Some(lineage.len())
    };

    let count = recall_memories.len();

    // Note: Coactivation for Hebbian learning is already recorded inside recall()
    // No need for explicit record_memory_coactivation call

    // Record metrics
    let duration = op_start.elapsed().as_secs_f64();
    metrics::MEMORY_RETRIEVE_DURATION
        .with_label_values(&[&mode])
        .observe(duration);
    metrics::MEMORY_RETRIEVE_TOTAL
        .with_label_values(&[&mode, &"success".to_string()])
        .inc();
    metrics::MEMORY_RETRIEVE_RESULTS
        .with_label_values(&[&mode])
        .observe(count as f64);

    // Broadcast RETRIEVE event for real-time dashboard with full results
    let results_json = serde_json::json!({
        "query": req.query,
        "mode": mode,
        "count": count,
        "latency_ms": duration * 1000.0,
        "memories": recall_memories.iter().map(|m| serde_json::json!({
            "id": m.id,
            "content": m.experience.content,
            "memory_type": m.experience.memory_type,
            "tags": m.experience.tags,
            "score": m.score,
            "importance": m.importance,
            "tier": m.tier,
            "created_at": m.created_at,
        })).collect::<Vec<_>>(),
        "facts": facts.iter().map(|f| serde_json::json!({
            "id": f.id,
            "fact": f.fact,
            "confidence": f.confidence,
            "support_count": f.support_count,
            "related_entities": f.related_entities,
        })).collect::<Vec<_>>(),
        "todos": todos.iter().map(|t| serde_json::json!({
            "short_id": t.short_id,
            "content": t.content,
            "status": t.status,
            "priority": t.priority,
            "project": t.project,
            "score": t.score,
        })).collect::<Vec<_>>(),
        "reminders": triggered_reminders.iter().map(|r| serde_json::json!({
            "id": r.id,
            "content": r.content,
            "keywords": r.keywords,
            "priority": r.priority,
        })).collect::<Vec<_>>(),
        "lineage": lineage.iter().map(|l| serde_json::json!({
            "from": l.from,
            "to": l.to,
            "relation": l.relation,
            "confidence": l.confidence,
        })).collect::<Vec<_>>(),
    });
    state.emit_event(MemoryEvent {
        event_type: "RETRIEVE".to_string(),
        timestamp: chrono::Utc::now(),
        user_id: req.user_id.clone(),
        memory_id: None,
        content_preview: Some(req.query.chars().take(50).collect()),
        memory_type: Some(mode),
        importance: None,
        count: Some(count),
        entities: None,
        results: Some(results_json),
    });

    // Track session event
    if count > 0 {
        let session_id = state.session_store.get_or_create_session(&req.user_id);
        let memory_ids: Vec<String> = recall_memories.iter().map(|m| m.id.clone()).collect();
        let avg_score = if !recall_memories.is_empty() {
            recall_memories.iter().map(|m| m.score).sum::<f32>() / recall_memories.len() as f32
        } else {
            0.0
        };
        state.session_store.add_event(
            &session_id,
            SessionEvent::MemoriesSurfaced {
                timestamp: chrono::Utc::now(),
                query_preview: req.query.chars().take(100).collect(),
                memory_count: count,
                memory_ids,
                avg_score,
            },
        );
    }

    // Build reminder count for response
    let reminder_count = if triggered_reminders.is_empty() {
        None
    } else {
        Some(triggered_reminders.len())
    };

    Ok(Json(RecallResponse {
        memories: recall_memories,
        count,
        retrieval_stats: None, // Retrieval stats not exposed in new API
        todos,
        todo_count,
        facts,
        fact_count,
        triggered_reminders,
        reminder_count,
        lineage,
        lineage_count,
    }))
}

// =============================================================================
// CONTEXT SUMMARY HANDLER
// =============================================================================

/// POST /api/context_summary - Get categorized memories for session bootstrap
///
/// Returns memories grouped by type (decisions, learnings, context, patterns, errors)
/// for quick session initialization.
#[tracing::instrument(skip(state), fields(user_id = %req.user_id))]
pub async fn context_summary(
    State(state): State<AppState>,
    Json(req): Json<ContextSummaryRequest>,
) -> Result<Json<ContextSummaryResponse>, AppError> {
    validation::validate_user_id(&req.user_id).map_validation_err("user_id")?;
    validation::validate_max_results(req.max_items).map_validation_err("max_items")?;

    let memory = state
        .get_user_memory(&req.user_id)
        .map_err(AppError::Internal)?;

    let max_items = req.max_items;
    let include_decisions = req.include_decisions;
    let include_learnings = req.include_learnings;
    let include_context = req.include_context;

    let response = {
        let memory = memory.clone();
        tokio::task::spawn_blocking(move || {
            let memory_guard = memory.read();

            let stats = memory_guard.stats();
            let total_memories = stats.total_memories;

            // Helper to search by type using advanced_search
            let search_by_type = |exp_type: ExperienceType, limit: usize| -> Vec<SummaryItem> {
                memory_guard
                    .advanced_search(SearchCriteria::ByType(exp_type))
                    .unwrap_or_default()
                    .into_iter()
                    .take(limit)
                    .map(|m| SummaryItem {
                        id: m.id.0.to_string(),
                        content: m.experience.content.clone(),
                        importance: m.importance(),
                        created_at: m.created_at.to_rfc3339(),
                    })
                    .collect()
            };

            // Get memories by type using advanced_search
            let decisions = if include_decisions {
                search_by_type(ExperienceType::Decision, max_items)
            } else {
                Vec::new()
            };

            let learnings = if include_learnings {
                search_by_type(ExperienceType::Learning, max_items)
            } else {
                Vec::new()
            };

            let context = if include_context {
                search_by_type(ExperienceType::Context, max_items)
            } else {
                Vec::new()
            };

            let patterns = search_by_type(ExperienceType::Pattern, max_items);
            let errors = search_by_type(ExperienceType::Error, max_items);

            ContextSummaryResponse {
                total_memories,
                decisions,
                learnings,
                context,
                patterns,
                errors,
            }
        })
        .await
        .map_err(|e| AppError::Internal(anyhow::anyhow!("Blocking task panicked: {e}")))?
    };

    Ok(Json(response))
}

// =============================================================================
// PROACTIVE CONTEXT HANDLER
// =============================================================================

/// POST /api/proactive_context - Combined recall + reminders for AI agents
///
/// Returns relevant memories based on semantic similarity and entity matching,
/// plus any due or context-triggered reminders. Optionally stores the context
/// as a Conversation memory for future recall.
#[tracing::instrument(skip(state), fields(user_id = %req.user_id))]
pub async fn proactive_context(
    State(state): State<AppState>,
    Json(mut req): Json<ProactiveContextRequest>,
) -> Result<Json<ProactiveContextResponse>, AppError> {
    validation::validate_user_id(&req.user_id).map_validation_err("user_id")?;
    validation::validate_max_results(req.max_results).map_validation_err("max_results")?;
    validation::validate_weight("semantic_threshold", req.semantic_threshold)
        .map_validation_err("semantic_threshold")?;
    validation::validate_weight("entity_match_weight", req.entity_match_weight)
        .map_validation_err("entity_match_weight")?;
    validation::validate_weight("recency_weight", req.recency_weight)
        .map_validation_err("recency_weight")?;

    // Strip system noise BEFORE any processing — <task-notification>, <system-reminder>,
    // <shodh-context>, code blocks, file contents, etc. This ensures embedding, NER, BM25,
    // and auto-ingest all operate on meaningful user content, not XML scaffolding.
    let raw_len = req.context.len();
    req.context = strip_system_noise(&req.context);
    if raw_len > 0 && req.context.len() < raw_len {
        tracing::debug!(
            "proactive_context: stripped system noise from context ({} -> {} bytes)",
            raw_len,
            req.context.len()
        );
    }

    // Validate context: must be non-empty and have meaningful content
    let trimmed_context = req.context.trim();
    if trimmed_context.is_empty()
        || trimmed_context
            .chars()
            .filter(|c| c.is_alphabetic())
            .count()
            < 3
    {
        tracing::debug!(
            "proactive_context: empty or meaningless context, returning empty response"
        );
        return Ok(Json(ProactiveContextResponse {
            memories: Vec::new(),
            due_reminders: Vec::new(),
            context_reminders: Vec::new(),
            memory_count: 0,
            reminder_count: 0,
            ingested_memory_id: None,
            feedback_processed: None,
            relevant_todos: Vec::new(),
            todo_count: 0,
            relevant_facts: Vec::new(),
            latency_ms: 0.0,
            detected_entities: Vec::new(),
            temporal_credits_applied: None,
        }));
    }

    let op_start = std::time::Instant::now();

    let memory_system = state
        .get_user_memory(&req.user_id)
        .map_err(AppError::Internal)?;

    let graph_memory = state
        .get_user_graph(&req.user_id)
        .map_err(AppError::Internal)?;

    let t_init = op_start.elapsed();
    tracing::info!(
        user_id = %req.user_id,
        init_ms = format!("{:.2}", t_init.as_secs_f64() * 1000.0),
        "proactive_context [phase:init] user memory + graph acquired"
    );

    // 0. Process pending feedback if previous_response is provided
    let mut temporal_credits_total: u32 = 0;
    let feedback_processed = if let Some(ref prev_response) = req.previous_response {
        let feedback_store = state.feedback_store.clone();
        let user_id_for_feedback = req.user_id.clone();
        let response_text = super::utils::strip_mcp_response_noise(prev_response);
        let followup = req.user_followup.clone();
        let memory_for_embed = memory_system.clone();
        let tool_actions_for_feedback = std::mem::take(&mut req.tool_actions);

        // Process feedback and collect memory IDs for reinforcement.
        // Split into 3 phases to minimize write-lock hold time on the shared FeedbackStore:
        //   Phase 1: Take pending + detect context pattern (write lock, <1ms)
        //   Phase 2: Compute embedding + signals (NO lock, 10-30ms)
        //   Phase 3: Apply momentum updates + flush (write lock, <5ms)
        let (result, helpful_ids, misleading_ids, temporal_credits_count) = tokio::task::spawn_blocking(move || {
            // Phase 1: Extract pending data under brief write lock.
            // Also push the taken pending into the FeedbackWindow as a historical
            // entry for multi-turn temporal credit assignment (Issue #125).
            let (pending, context_pattern, window_entries, current_turn) = {
                let mut store = feedback_store.write();
                let pending = store.take_pending(&user_id_for_feedback).map(|mut p| {
                    // Attach tool actions from current request to previous pending.
                    // These actions happened AFTER memories were surfaced (previous call)
                    // and BEFORE this call — exactly the attribution window.
                    if !tool_actions_for_feedback.is_empty() {
                        p.tool_actions = tool_actions_for_feedback;
                    }
                    p
                });
                let context_pattern = pending.as_ref().and_then(|p| {
                    if p.context_embedding.is_empty() {
                        return None;
                    }
                    store.detect_context_pattern(&user_id_for_feedback, &p.context_embedding)
                });

                // Push the consumed pending into the window as a historical entry.
                // This entry is now eligible for multi-turn credit from future turns.
                if let Some(ref p) = pending {
                    let window = store.get_or_create_window(&user_id_for_feedback);
                    let turn_number = window.turn_counter;
                    let entry = feedback::WindowEntry {
                        turn_number,
                        surfaced_memories: p.surfaced_memories.clone(),
                        surfaced_at: p.surfaced_at,
                        context_embedding: p.context_embedding.clone(),
                        context_preview: p.context.chars().take(200).collect(),
                        tool_actions: p.tool_actions.clone(),
                    };
                    store.push_window_entry(&user_id_for_feedback, entry);
                }

                // Snapshot window entries for Phase 2 (computed without lock)
                let window_entries = store.snapshot_window_entries(&user_id_for_feedback);
                let current_turn = store.window_turn_counter(&user_id_for_feedback);

                (pending, context_pattern, window_entries, current_turn)
                // write lock released here
            };

            if let Some(pending) = pending {
                // Phase 2: Pure computation — no lock held
                let response_embedding: Option<Vec<f32>> = {
                    let memory_guard = memory_for_embed.read();
                    memory_guard.compute_embedding(&response_text).ok()
                };

                let mut signals = feedback::process_implicit_feedback_with_semantics(
                    &pending,
                    &response_text,
                    followup.as_deref(),
                    response_embedding.as_deref(),
                );

                // Apply context pattern adjustments (computed in Phase 1)
                if let Some((is_repetition, is_topic_change, similarity)) = context_pattern {
                    if is_repetition || is_topic_change {
                        feedback::apply_context_pattern_signals(
                            &mut signals,
                            is_repetition,
                            is_topic_change,
                            similarity,
                        );
                        tracing::debug!(
                            user_id = %user_id_for_feedback,
                            is_repetition,
                            is_topic_change,
                            similarity,
                            "Applied context pattern signals to feedback"
                        );
                    }
                }

                let context_entities: Vec<String> =
                    feedback::extract_entities_simple(&pending.context)
                        .into_iter()
                        .collect();
                let context_embedding = pending.context_embedding.clone();

                // Phase 2b: Compute temporally discounted signals for older window entries.
                // The most recent window entry was just pushed in Phase 1 (it IS the taken
                // pending), so we skip it — it was already processed above as the immediate
                // signal. We process entries 0..N-1 (older turns).
                let mut deferred_credits: Vec<(MemoryId, feedback::DeferredCredit)> = Vec::new();
                let gamma = crate::constants::TEMPORAL_DISCOUNT_GAMMA;

                if window_entries.len() > 1 {
                    // Iterate all entries except the last one (which is the just-pushed pending)
                    for entry in window_entries.iter().take(window_entries.len().saturating_sub(1)) {
                        let turns_elapsed = current_turn.saturating_sub(entry.turn_number);
                        if turns_elapsed == 0 {
                            continue;
                        }

                        let discount = gamma.powi(turns_elapsed as i32);
                        if discount < 0.05 {
                            continue; // Below meaningful contribution
                        }

                        // Build a synthetic PendingFeedback from the window entry
                        // to reuse existing signal computation
                        let synthetic_pending = feedback::PendingFeedback::new(
                            user_id_for_feedback.clone(),
                            entry.context_preview.clone(),
                            entry.context_embedding.clone(),
                            entry.surfaced_memories.clone(),
                        );

                        let window_signals = feedback::process_implicit_feedback_with_semantics(
                            &synthetic_pending,
                            &response_text,
                            None, // No followup for historical entries
                            response_embedding.as_deref(),
                        );

                        for (memory_id, signal) in window_signals {
                            let discounted_value = signal.value * discount;
                            if discounted_value.abs() >= crate::constants::TEMPORAL_CREDIT_MIN_THRESHOLD {
                                deferred_credits.push((
                                    memory_id,
                                    feedback::DeferredCredit {
                                        raw_signal: signal.value,
                                        confidence: signal.confidence,
                                        trigger: signal.trigger,
                                        turns_elapsed,
                                        discounted_value,
                                        computed_at: chrono::Utc::now(),
                                    },
                                ));
                            }
                        }
                    }
                }

                // Detect session-level outcomes from the window
                let session_outcome = if window_entries.len() >= 2 {
                    // Build a temporary window to detect outcomes
                    let mut temp_window = feedback::FeedbackWindow::new(user_id_for_feedback.clone());
                    temp_window.entries = window_entries.into_iter().collect();
                    temp_window.detect_session_outcome()
                } else {
                    None
                };

                // Classify signals before acquiring lock
                let mut reinforced = Vec::new();
                let mut weakened = Vec::new();
                let mut helpful_ids: Vec<MemoryId> = Vec::new();
                let mut misleading_ids: Vec<MemoryId> = Vec::new();

                // Collect signal classifications for Phase 3
                let classified: Vec<_> = signals
                    .into_iter()
                    .map(|(memory_id, signal)| {
                        let is_helpful = signal.value > 0.3;
                        let is_misleading = signal.value < -0.3;
                        (memory_id, signal, is_helpful, is_misleading)
                    })
                    .collect();

                // Phase 3: Apply momentum updates under brief write lock
                let temporal_credits_count;
                {
                    let mut store = feedback_store.write();

                    // 3a: Apply immediate signals (same as before)
                    for (memory_id, signal, is_helpful, is_misleading) in &classified {
                        let momentum = store.get_or_create_momentum(
                            memory_id.clone(),
                            crate::memory::types::ExperienceType::Context,
                        );

                        let old_ema = momentum.ema;
                        let new_ema = {
                            momentum.update(signal.clone());
                            momentum.ema
                        };

                        if *is_helpful || *is_misleading {
                            let fingerprint = feedback::ContextFingerprint::new(
                                context_entities.clone(),
                                &context_embedding,
                                *is_helpful,
                            );
                            momentum.add_context(fingerprint);
                        }

                        if *is_helpful || new_ema > old_ema + 0.05 {
                            reinforced.push(memory_id.0.to_string());
                            helpful_ids.push(memory_id.clone());
                        } else if *is_misleading || new_ema < old_ema - 0.05 {
                            weakened.push(memory_id.0.to_string());
                            misleading_ids.push(memory_id.clone());
                        }

                        store.mark_dirty(memory_id);
                    }

                    // 3b: Accumulate deferred credits from multi-turn window
                    temporal_credits_count = deferred_credits.len();
                    for (memory_id, credit) in deferred_credits {
                        store.accumulate_deferred_credit(
                            &user_id_for_feedback,
                            &memory_id,
                            credit,
                        );
                    }

                    // 3c: Apply session-level outcome signals
                    if let Some(ref outcome) = session_outcome {
                        match outcome {
                            feedback::SessionOutcome::TaskCompletion { turns_engaged, .. } => {
                                // Boost all memories in window
                                let window = store.get_or_create_window(&user_id_for_feedback);
                                let all_ids = window.all_memory_ids();
                                for id in all_ids {
                                    let credit = feedback::DeferredCredit {
                                        raw_signal: crate::constants::SESSION_COMPLETION_BOOST,
                                        confidence: 0.7,
                                        trigger: feedback::SignalTrigger::TopicChange {
                                            similarity: 0.0,
                                        },
                                        turns_elapsed: 0,
                                        discounted_value: crate::constants::SESSION_COMPLETION_BOOST,
                                        computed_at: chrono::Utc::now(),
                                    };
                                    store.accumulate_deferred_credit(
                                        &user_id_for_feedback,
                                        &id,
                                        credit,
                                    );
                                }
                                tracing::info!(
                                    user_id = %user_id_for_feedback,
                                    turns = turns_engaged,
                                    "Session outcome: task completion detected, boosting window memories"
                                );
                            }
                            feedback::SessionOutcome::Abandonment { gap_seconds, frustration_detected } => {
                                // Penalize last 2 entries
                                let window = store.get_or_create_window(&user_id_for_feedback);
                                let recent_ids: Vec<MemoryId> = window.entries
                                    .iter()
                                    .rev()
                                    .take(2)
                                    .flat_map(|e| e.surfaced_memories.iter().map(|m| m.id.clone()))
                                    .collect();
                                for id in recent_ids {
                                    let credit = feedback::DeferredCredit {
                                        raw_signal: crate::constants::SESSION_ABANDONMENT_PENALTY,
                                        confidence: 0.5,
                                        trigger: feedback::SignalTrigger::Ignored {
                                            overlap_ratio: 0.0,
                                        },
                                        turns_elapsed: 0,
                                        discounted_value: crate::constants::SESSION_ABANDONMENT_PENALTY,
                                        computed_at: chrono::Utc::now(),
                                    };
                                    store.accumulate_deferred_credit(
                                        &user_id_for_feedback,
                                        &id,
                                        credit,
                                    );
                                }
                                tracing::info!(
                                    user_id = %user_id_for_feedback,
                                    gap_seconds,
                                    frustration_detected,
                                    "Session outcome: abandonment detected, penalizing recent memories"
                                );
                            }
                            feedback::SessionOutcome::ReEngagement { gap_turns, topic_similarity } => {
                                // Boost memories from the first entry (re-engaged topic)
                                let window = store.get_or_create_window(&user_id_for_feedback);
                                if let Some(first) = window.entries.front() {
                                    let topic_ids: Vec<MemoryId> = first
                                        .surfaced_memories
                                        .iter()
                                        .map(|m| m.id.clone())
                                        .collect();
                                    for id in topic_ids {
                                        let credit = feedback::DeferredCredit {
                                            raw_signal: crate::constants::SESSION_REENGAGEMENT_BOOST,
                                            confidence: 0.75,
                                            trigger: feedback::SignalTrigger::TopicChange {
                                                similarity: *topic_similarity,
                                            },
                                            turns_elapsed: *gap_turns,
                                            discounted_value: crate::constants::SESSION_REENGAGEMENT_BOOST,
                                            computed_at: chrono::Utc::now(),
                                        };
                                        store.accumulate_deferred_credit(
                                            &user_id_for_feedback,
                                            &id,
                                            credit,
                                        );
                                    }
                                }
                                tracing::info!(
                                    user_id = %user_id_for_feedback,
                                    gap_turns,
                                    topic_similarity,
                                    "Session outcome: re-engagement detected, boosting original topic memories"
                                );
                            }
                            feedback::SessionOutcome::NaturalEnd => {}
                        }
                    }

                    if temporal_credits_count > 0 {
                        tracing::debug!(
                            user_id = %user_id_for_feedback,
                            credits = temporal_credits_count,
                            "Accumulated temporal deferred credits from multi-turn window"
                        );
                    }

                    if let Err(e) = store.flush() {
                        tracing::warn!("Failed to flush feedback store: {}", e);
                    }
                    // write lock released here
                }

                let result = FeedbackProcessed {
                    memories_evaluated: pending.surfaced_memories.len(),
                    reinforced,
                    weakened,
                };
                (Some(result), helpful_ids, misleading_ids, temporal_credits_count)
            } else {
                (None, Vec::new(), Vec::new(), 0usize)
            }
        })
        .await
        .map_err(|e| AppError::Internal(anyhow::anyhow!("Feedback task panicked: {e}")))?;

        temporal_credits_total = temporal_credits_count as u32;

        // Apply reinforcement to memory system, graph, AND retrieval weights
        if !helpful_ids.is_empty() || !misleading_ids.is_empty() {
            let memory_sys_for_reinforce = memory_system.clone();
            let graph_for_reinforce = graph_memory.clone();
            let helpful_ids_for_graph = helpful_ids.clone();
            let relevance_engine = state.relevance_engine.clone();
            let helpful_count = helpful_ids.len();
            let misleading_count = misleading_ids.len();
            let user_id = req.user_id.clone();
            tokio::task::spawn_blocking(move || {
                let memory_guard = memory_sys_for_reinforce.read();

                // Reinforce helpful memories (importance boost)
                if !helpful_ids.is_empty() {
                    if let Err(e) = memory_guard
                        .reinforce_recall(&helpful_ids, crate::memory::RetrievalOutcome::Helpful)
                    {
                        tracing::warn!("Failed to reinforce helpful memories: {}", e);
                    }
                }

                // Weaken misleading memories (importance decay)
                if !misleading_ids.is_empty() {
                    if let Err(e) = memory_guard.reinforce_recall(
                        &misleading_ids,
                        crate::memory::RetrievalOutcome::Misleading,
                    ) {
                        tracing::warn!("Failed to weaken misleading memories: {}", e);
                    }
                }

                // Strengthen graph edges for helpful memories (Hebbian: "what fires together, wires together")
                // When feedback confirms a memory was useful, strengthen its episode's entity edges
                // so the knowledge graph learns which associations are valuable.
                if !helpful_ids_for_graph.is_empty() {
                    let graph_guard = graph_for_reinforce.read();
                    for memory_id in &helpful_ids_for_graph {
                        match graph_guard.strengthen_episode_entity_edges(&memory_id.0) {
                            Ok(count) if count > 0 => {
                                tracing::debug!(
                                    memory_id = %memory_id.0,
                                    edges = count,
                                    "Feedback-driven edge strengthening applied"
                                );
                            }
                            Err(e) => {
                                tracing::debug!(
                                    memory_id = %memory_id.0,
                                    "Feedback edge strengthening failed: {}",
                                    e
                                );
                            }
                            _ => {}
                        }
                    }
                }

                // Reinforce/weaken lineage edges connected to helpful/misleading memories.
                // This is the selection pressure that makes causal chains adaptive:
                // chains that participate in useful recalls get stronger, chains connected
                // to misleading memories weaken. Without this, all inferred edges remain
                // at their initial confidence with no learning signal.
                {
                    let lineage = memory_guard.lineage_graph();
                    let mut reinforced_edge_ids = std::collections::HashSet::new();

                    // Reinforce edges for helpful memories
                    for memory_id in &helpful_ids_for_graph {
                        for edges in [
                            lineage.get_edges_from(&user_id, memory_id),
                            lineage.get_edges_to(&user_id, memory_id),
                        ] {
                            if let Ok(edges) = edges {
                                for mut edge in edges {
                                    if reinforced_edge_ids.insert(edge.id.clone()) {
                                        edge.reinforce();
                                        let _ = lineage.store_edge(&user_id, &edge);
                                    }
                                }
                            }
                        }
                    }

                    // Weaken edges for misleading memories, pruning zombie edges
                    for memory_id in &misleading_ids {
                        for edges in [
                            lineage.get_edges_from(&user_id, memory_id),
                            lineage.get_edges_to(&user_id, memory_id),
                        ] {
                            if let Ok(edges) = edges {
                                for mut edge in edges {
                                    if reinforced_edge_ids.insert(edge.id.clone()) {
                                        let should_prune = edge.weaken();
                                        if should_prune {
                                            let _ = lineage.delete_edge(&user_id, &edge.id);
                                        } else {
                                            let _ = lineage.store_edge(&user_id, &edge);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                // Update adaptive retrieval weights via gradient descent (Rescorla-Wagner, 1972).
                // proactive_context always uses semantic retrieval; entity matching contributes
                // when entities were extracted. This closes the loop: feedback now adjusts
                // how much weight semantic vs entity vs tag signals get in future retrievals.
                let entity_contributed = helpful_count > 0; // entities always extracted in proactive_context
                for _ in 0..helpful_count {
                    relevance_engine.apply_feedback(true, entity_contributed, false, true);
                }
                for _ in 0..misleading_count {
                    relevance_engine.apply_feedback(true, entity_contributed, false, false);
                }
            })
            .await
            .map_err(|e| AppError::Internal(anyhow::anyhow!("Reinforce task panicked: {e}")))?;

            // Emit SSE event for feedback processing
            if let Some(ref feedback) = result {
                state.emit_event(MemoryEvent {
                    event_type: "FEEDBACK_PROCESSED".to_string(),
                    timestamp: chrono::Utc::now(),
                    user_id: req.user_id.clone(),
                    memory_id: None,
                    content_preview: Some(format!(
                        "Evaluated {} memories: {} reinforced, {} weakened",
                        feedback.memories_evaluated,
                        feedback.reinforced.len(),
                        feedback.weakened.len()
                    )),
                    memory_type: Some("feedback".to_string()),
                    importance: None,
                    count: Some(feedback.memories_evaluated),
                    entities: None,
                    results: None,
                });

                // Dishabituation: reset habituation counters for memories that received
                // positive feedback. This models the biological dishabituation response —
                // when a habituated stimulus produces a novel outcome, the response recovers.
                if !feedback.reinforced.is_empty() {
                    if let Some(user_map) = state.habituation_tracker.get(&req.user_id) {
                        let now = chrono::Utc::now();
                        for mem_id_str in &feedback.reinforced {
                            if let Some(mut entry) = user_map.get_mut(mem_id_str) {
                                entry.surfacings_without_utility = 0;
                                entry.last_utility = Some(now);
                            }
                        }
                    }
                }
            }
        }

        result
    } else {
        None
    };

    let t_feedback = op_start.elapsed();
    tracing::info!(
        user_id = %req.user_id,
        feedback_ms = format!("{:.2}", (t_feedback - t_init).as_secs_f64() * 1000.0),
        cumulative_ms = format!("{:.2}", t_feedback.as_secs_f64() * 1000.0),
        had_feedback = req.previous_response.is_some(),
        "proactive_context [phase:feedback] feedback processing complete"
    );

    // 1 + 1.5: Compute embedding and extract NER entities in parallel
    // Both are independent blocking tasks (~10ms + ~5ms → ~10ms parallel)
    let context_for_embedding = req.context.clone();
    let memory_for_embedding = memory_system.clone();
    let embedding_task = tokio::task::spawn_blocking(move || {
        let memory_guard = memory_for_embedding.read();
        match memory_guard.compute_embedding(&context_for_embedding) {
            Ok(emb) => (emb, true),
            Err(e) => {
                tracing::warn!("proactive_context: embedding computation failed: {e}, skipping embedding-dependent operations");
                (Vec::new(), false)
            }
        }
    });

    let ner = state.get_neural_ner();
    let context_for_ner = req.context.clone();
    let ner_task = {
        let ner = ner.clone();
        tokio::task::spawn_blocking(move || match ner.extract(&context_for_ner) {
            Ok(entities) => {
                // Build word set from context for validating NER extractions
                let context_words: std::collections::HashSet<&str> = context_for_ner
                    .split(|c: char| !c.is_alphanumeric() && c != '-' && c != '_')
                    .filter(|w| w.len() >= 3)
                    .collect();
                let context_lower = context_for_ner.to_lowercase();

                let filtered: Vec<_> = entities
                    .into_iter()
                    .filter(|e| {
                        let t = e.text.trim();
                        // Length: at least 3 chars
                        if t.len() < 3 {
                            return false;
                        }
                        // Filter known NER artifacts
                        let lower = t.to_lowercase();
                        if matches!(
                            lower.as_str(),
                            "undefined" | "null" | "none" | "true" | "false"
                        ) {
                            return false;
                        }
                        // Validate: entity must appear in context.
                        // Single-word: check word set (prevents subword fragments like "ian" from "Hebbian")
                        // Multi-word: check case-insensitive substring of original context
                        if !t.contains(' ') {
                            context_words.contains(t)
                                || context_words.iter().any(|w| w.eq_ignore_ascii_case(t))
                        } else {
                            context_lower.contains(&lower)
                        }
                    })
                    .collect();
                let infos: Vec<DetectedEntityInfo> = filtered
                    .iter()
                    .map(|e| DetectedEntityInfo {
                        name: e.text.clone(),
                        entity_type: format!("{:?}", e.entity_type),
                    })
                    .collect();
                let names: Vec<String> = filtered.iter().map(|e| e.text.to_lowercase()).collect();
                (infos, names)
            }
            Err(_) => (Vec::new(), Vec::new()),
        })
    };

    let (embedding_result, ner_result) = tokio::join!(embedding_task, ner_task);
    let (context_embedding, embedding_valid): (Vec<f32>, bool) = embedding_result
        .map_err(|e| AppError::Internal(anyhow::anyhow!("Embedding task panicked: {e}")))?;
    let (detected_entities, context_entity_names): (Vec<DetectedEntityInfo>, Vec<String>) =
        ner_result.map_err(|e| AppError::Internal(anyhow::anyhow!("NER task panicked: {e}")))?;

    let t_embed_ner = op_start.elapsed();
    tracing::info!(
        user_id = %req.user_id,
        embed_ner_ms = format!("{:.2}", (t_embed_ner - t_feedback).as_secs_f64() * 1000.0),
        cumulative_ms = format!("{:.2}", t_embed_ner.as_secs_f64() * 1000.0),
        embedding_valid,
        entity_count = detected_entities.len(),
        "proactive_context [phase:embed+ner] embedding + NER complete"
    );

    // 1.8: Check context-triggered prospective tasks — builds signals for recall boost
    // This runs before recall so that "future informs present" can influence retrieval.
    // Fast operation: scans pending tasks for this user (typically < 10 tasks).
    let ctx_trigger_uid = req.user_id.clone();
    let ctx_trigger_context = req.context.clone();
    let ctx_trigger_emb = context_embedding.clone();
    let ctx_trigger_memory = memory_system.clone();
    let ctx_trigger_prosp = state.prospective_store.clone();

    let (prospective_signals, cached_context_triggers) = tokio::task::spawn_blocking(move || {
        let embed_fn = |text: &str| -> Option<Vec<f32>> {
            let memory_guard = ctx_trigger_memory.read();
            memory_guard.compute_embedding(text).ok()
        };
        let matched = ctx_trigger_prosp
            .check_context_triggers_semantic(
                &ctx_trigger_uid,
                &ctx_trigger_context,
                &ctx_trigger_emb,
                embed_fn,
            )
            .unwrap_or_default();

        if matched.is_empty() {
            (None, Vec::new())
        } else {
            let mut signals = Vec::new();
            for (task, _score) in &matched {
                signals.push(task.content.clone());
                if let ProspectiveTrigger::OnContext { keywords, .. } = &task.trigger {
                    for kw in keywords {
                        signals.push(kw.clone());
                    }
                }
            }
            (Some(signals), matched)
        }
    })
    .await
    .map_err(|e| AppError::Internal(anyhow::anyhow!("Prospective check panicked: {e}")))?;

    let t_prospective = op_start.elapsed();
    tracing::info!(
        user_id = %req.user_id,
        prospective_ms = format!("{:.2}", (t_prospective - t_embed_ner).as_secs_f64() * 1000.0),
        cumulative_ms = format!("{:.2}", t_prospective.as_secs_f64() * 1000.0),
        triggers_found = cached_context_triggers.len(),
        "proactive_context [phase:prospective] context trigger check complete"
    );

    if !cached_context_triggers.is_empty() {
        tracing::debug!(
            user_id = %req.user_id,
            count = cached_context_triggers.len(),
            "Context-triggered prospective tasks found — will boost related memories"
        );
    }

    // 2. Retrieve memories using unified 5-layer pipeline
    // The pipeline already applies: RRF fusion + hebbian + recency + feedback (PIPE-9)
    // No double-scoring needed - just use the scores from recall() directly
    //
    // Post-pipeline: adaptive involuntary memory constraints (Berntsen 2009)
    // Applied here (not in semantic_retrieve) to keep voluntary recall untouched.
    let context_clone = req.context.clone();
    let context_lower_for_tags = req.context.to_lowercase();
    let max_results = req.max_results;
    let user_id_for_query = req.user_id.clone();
    let entity_names_for_recall = context_entity_names.clone();
    let entity_match_weight = req.entity_match_weight;
    let recency_weight = req.recency_weight;
    let semantic_threshold = req.semantic_threshold;
    let embedding_for_query = context_embedding.clone();
    let habituation_tracker = state.habituation_tracker.clone();
    let user_id_for_habituation = req.user_id.clone();
    let graph_for_habituation = state.get_user_graph(&req.user_id).ok();
    let memory_type_filter: Vec<ExperienceType> = req
        .memory_types
        .iter()
        .map(|s| super::remember::parse_experience_type(Some(s)))
        .collect();
    let memories: Vec<ProactiveSurfacedMemory> = {
        let memory = memory_system.clone();
        tokio::task::spawn_blocking(move || {
            let memory_guard = memory.read();

            // Build word set from query for anti-echo detection (borrows context_clone)
            let query_words: std::collections::HashSet<String> = context_clone
                .split_whitespace()
                .map(|w| {
                    w.trim_matches(|c: char| !c.is_alphanumeric())
                        .to_lowercase()
                })
                .filter(|w| w.len() >= 3)
                .collect();

            let query = MemoryQuery {
                user_id: Some(user_id_for_query),
                query_text: Some(context_clone),
                query_embedding: if embedding_valid {
                    Some(embedding_for_query)
                } else {
                    None
                },
                max_results,
                recency_weight: Some(recency_weight),
                prospective_signals,
                experience_types: if memory_type_filter.is_empty() {
                    None
                } else {
                    Some(memory_type_filter)
                },
                ..Default::default()
            };
            let results = memory_guard.recall(&query).unwrap_or_default();

            let candidates: Vec<(SharedMemory, f32)> = results
                .into_iter()
                .filter(|m| {
                    // Quality gate: skip garbage/truncated memories
                    let content = m.experience.content.trim();
                    if content.len() < 30 {
                        return false;
                    }
                    // Skip content that's mostly non-alphabetic (binary noise, IDs, etc.)
                    let alpha_count = content.chars().filter(|c| c.is_alphabetic()).count();
                    if alpha_count < 10 {
                        return false;
                    }
                    // Anti-echo: skip memories that are just our own context echoed back
                    // (auto-ingest stores context, which then gets retrieved for itself)
                    // Uses inline comparison to avoid allocating a HashSet<String> per candidate
                    if !query_words.is_empty() {
                        let mut mem_word_count = 0usize;
                        let mut overlap = 0usize;
                        for w in content.split_whitespace() {
                            let w = w.trim_matches(|c: char| !c.is_alphanumeric());
                            if w.len() >= 3 {
                                mem_word_count += 1;
                                if query_words.iter().any(|qw| qw.eq_ignore_ascii_case(w)) {
                                    overlap += 1;
                                }
                            }
                        }
                        let smaller = query_words.len().min(mem_word_count);
                        if smaller > 0 && overlap * 100 / smaller >= 70 {
                            return false;
                        }
                    }
                    true
                })
                .map(|m| {
                    let score = m.get_score().unwrap_or(0.0);
                    (m, score)
                })
                .collect();

            let context_lower = &context_lower_for_tags;

            // Compute entity matches and boost scores BEFORE quality gate
            let context_entity_count = entity_names_for_recall.len().max(1);
            let mut enriched: Vec<(
                std::sync::Arc<crate::memory::types::Memory>,
                f32,
                Vec<String>,
            )> = candidates
                .into_iter()
                .map(|(m, mut score)| {
                    let mut memory_terms: Vec<String> = m
                        .experience
                        .entities
                        .iter()
                        .map(|e| e.to_lowercase())
                        .collect();
                    for tag in &m.experience.tags {
                        let lower = tag.to_lowercase();
                        if !memory_terms.contains(&lower) {
                            memory_terms.push(lower);
                        }
                    }

                    let matched: Vec<String> = entity_names_for_recall
                        .iter()
                        .filter(|ctx_ent| {
                            ctx_ent.len() >= 3
                                && memory_terms.iter().any(|me| {
                                    if me.len() < 3 {
                                        return false;
                                    }
                                    if me == ctx_ent.as_str() {
                                        return true;
                                    }
                                    let (shorter, longer) = if me.len() <= ctx_ent.len() {
                                        (me.as_str(), ctx_ent.as_str())
                                    } else {
                                        (ctx_ent.as_str(), me.as_str())
                                    };
                                    shorter.len() * 100 / longer.len() >= 60
                                        && longer.contains(shorter)
                                })
                        })
                        .cloned()
                        .collect();

                    // Apply entity match boost with diminishing returns (log scaling).
                    // Linear scaling over-rewards memories with many entity matches;
                    // log scaling reflects cue distinctiveness (Berntsen 2009).
                    if !matched.is_empty() {
                        let match_ratio =
                            (matched.len() as f32 / context_entity_count as f32).min(1.0);
                        let diminishing = (1.0 + matched.len() as f32).ln() / (1.0_f32 + 3.0).ln();
                        let entity_boost = entity_match_weight * match_ratio * diminishing.min(1.0);
                        score *= 1.0 + entity_boost;
                    }

                    // Structured tag matching — boost memories whose hook-written
                    // tags (tool:*, file:*, error) align with context signals.
                    {
                        let mut tag_matches: u32 = 0;
                        for tag in &m.experience.tags {
                            let lower_tag = tag.to_lowercase();
                            if let Some(tool_name) = lower_tag.strip_prefix("tool:") {
                                if entity_names_for_recall
                                    .iter()
                                    .any(|e| e.eq_ignore_ascii_case(tool_name))
                                {
                                    tag_matches += 1;
                                }
                            } else if let Some(file_path) = lower_tag.strip_prefix("file:") {
                                let file_name = file_path
                                    .rsplit('/')
                                    .next()
                                    .or_else(|| file_path.rsplit('\\').next())
                                    .unwrap_or(file_path);
                                if file_name.len() >= 3
                                    && entity_names_for_recall.iter().any(|e| {
                                        e.contains(file_name) || file_name.contains(e.as_str())
                                    })
                                {
                                    tag_matches += 1;
                                }
                            } else if lower_tag == "error"
                                && (context_lower.contains("error")
                                    || context_lower.contains("fail")
                                    || context_lower.contains("bug")
                                    || context_lower.contains("fix"))
                            {
                                tag_matches += 1;
                            }
                        }
                        if tag_matches > 0 {
                            let capped = tag_matches.min(3) as f32;
                            score *= 1.0 + crate::constants::TAG_RELEVANCE_BOOST * capped;
                        }
                    }

                    (m, score, matched)
                })
                .collect();

            // Sort by boosted score (highest first)
            enriched.sort_by(|a, b| b.1.total_cmp(&a.1));

            // --- Adaptive involuntary memory constraints (Berntsen 2009) ---
            // These operate ONLY on proactive_context, not voluntary recall.

            // (A) Elaboration quality gate — rich memories outrank fragments.
            // Quality factor: content_len/200 scaled by structural richness.
            // Multiplicative so fragments score lower, not zero.
            {
                use crate::constants::ELABORATION_QUALITY_MIN;
                for (m, score, _) in enriched.iter_mut() {
                    let content_len = m.experience.content.len() as f32;
                    let length_factor = (content_len / 200.0).min(1.0);
                    let has_entities = if m.experience.entities.is_empty() {
                        0.0
                    } else {
                        0.1
                    };
                    let has_context = if m.experience.context.is_some() {
                        0.1
                    } else {
                        0.0
                    };
                    let quality = (length_factor * (1.0 + has_entities + has_context))
                        .max(ELABORATION_QUALITY_MIN);
                    *score *= quality;
                }
                enriched.sort_by(|a, b| b.1.total_cmp(&a.1));
            }

            // (B) Steeper proactive recency — involuntary memories favor recent events.
            // Applies an additional recency adjustment on top of Layer 5's recency boost.
            // Uses PROACTIVE_RECENCY_DECAY_RATE (0.03/h) vs voluntary's 0.01/h.
            {
                use crate::constants::PROACTIVE_RECENCY_DECAY_RATE;
                let now = chrono::Utc::now();
                // Layer 5 already applied exp(-0.01*h)*recency_weight.
                // We apply the *differential*: exp(-(0.03-0.01)*h) = exp(-0.02*h)
                let differential_rate = PROACTIVE_RECENCY_DECAY_RATE - 0.01;
                for (m, score, _) in enriched.iter_mut() {
                    let hours_old = (now - m.created_at).num_hours().max(0) as f32;
                    let proactive_recency_factor = (-differential_rate * hours_old).exp();
                    *score *= proactive_recency_factor;
                }
                enriched.sort_by(|a, b| b.1.total_cmp(&a.1));
            }

            // (C) Habituation — penalize memories surfaced repeatedly without utility.
            // Thompson & Spencer (1966): repeated stimulation without reinforcement
            // diminishes response. Logarithmic decay prevents permanent suppression.
            {
                use crate::constants::{HABITUATION_DECAY_FACTOR, HABITUATION_MAX_PENALTY};
                let user_map = habituation_tracker
                    .entry(user_id_for_habituation.clone())
                    .or_insert_with(DashMap::new);
                for (m, score, _) in enriched.iter_mut() {
                    let mem_id = m.id.0.to_string();
                    if let Some(entry) = user_map.get(&mem_id) {
                        if entry.surfacings_without_utility > 0 {
                            let penalty = (HABITUATION_DECAY_FACTOR
                                * (1.0 + entry.surfacings_without_utility as f32).ln())
                            .min(HABITUATION_MAX_PENALTY);
                            *score *= 1.0 - penalty;
                        }
                    }
                }
                enriched.sort_by(|a, b| b.1.total_cmp(&a.1));
            }

            // (D) Lateral inhibition — similar candidates suppress each other.
            // O'Reilly & McClelland (1994): pattern separation via competition.
            // Greedy top-down: each selected memory inhibits similar remaining candidates.
            {
                use crate::constants::{LATERAL_INHIBITION_STRENGTH, LATERAL_INHIBITION_THRESHOLD};
                let mut selected_embeddings: Vec<Vec<f32>> = Vec::new();
                for (m, score, _) in enriched.iter_mut() {
                    if let Some(ref emb) = m.experience.embeddings {
                        if !emb.is_empty() {
                            // Check against all already-selected memories
                            let mut max_sim: f32 = 0.0;
                            for sel_emb in &selected_embeddings {
                                if sel_emb.len() == emb.len() {
                                    let sim = cosine_similarity(emb, sel_emb);
                                    if sim > max_sim {
                                        max_sim = sim;
                                    }
                                }
                            }
                            if max_sim > LATERAL_INHIBITION_THRESHOLD {
                                // Suppress: the more similar, the stronger the inhibition
                                *score *= 1.0 - LATERAL_INHIBITION_STRENGTH * max_sim;
                            }
                            selected_embeddings.push(emb.clone());
                        }
                    }
                }
                // Final sort after all biological constraints applied
                enriched.sort_by(|a, b| b.1.total_cmp(&a.1));
            }

            // Drop results below minimum absolute score — don't pad with irrelevant filler
            // Also drop results that are < 30% of the top score (too weak relative to best)
            let top_score = enriched.first().map(|(_, s, _)| *s).unwrap_or(0.0);
            let abs_min = semantic_threshold;
            let relative_min = top_score * 0.30;
            let effective_min = abs_min.max(relative_min);
            enriched.retain(|(_, s, _)| *s >= effective_min);

            // Normalize scores for display: scale relative to top result
            if top_score > 0.0 {
                for (_, score, _) in enriched.iter_mut() {
                    *score = (*score / top_score) * 0.95;
                }
            }

            // (E) Update habituation state — record that these memories were surfaced.
            // Positive feedback resets count in Phase 0 of the next call.
            // Also penalize entities when memories exceed habituation threshold.
            {
                use crate::constants::{
                    ENTITY_SALIENCE_HABITUATION_PENALTY, ENTITY_SALIENCE_HABITUATION_THRESHOLD,
                };
                let user_map = habituation_tracker
                    .entry(user_id_for_habituation)
                    .or_insert_with(DashMap::new);
                let now = chrono::Utc::now();
                for (m, score, _) in enriched.iter() {
                    if *score >= effective_min {
                        let mem_id = m.id.0.to_string();
                        let mem_uuid = m.id.0;
                        let mut entry = user_map.entry(mem_id).or_insert_with(|| {
                            super::state::HabituationEntry {
                                surfacings_without_utility: 0,
                                last_surfaced: now,
                                last_utility: None,
                            }
                        });
                        entry.surfacings_without_utility += 1;
                        entry.last_surfaced = now;

                        // Penalize entities when memory repeatedly ignored
                        if entry.surfacings_without_utility > ENTITY_SALIENCE_HABITUATION_THRESHOLD
                        {
                            if let Some(ref graph) = graph_for_habituation {
                                let graph_guard = graph.read();
                                let _ = graph_guard.reinforce_entity_salience(
                                    &[mem_uuid],
                                    ENTITY_SALIENCE_HABITUATION_PENALTY,
                                );
                            }
                        }
                    }
                }
            }

            // Return top results with entity overlap annotation
            enriched
                .into_iter()
                .take(max_results)
                .map(|(m, score, matched)| {
                    let has_entity_match = !matched.is_empty();
                    let has_semantic_match = score > 0.0;
                    let relevance_reason = if has_entity_match && has_semantic_match {
                        "combined"
                    } else if has_entity_match {
                        "entity"
                    } else {
                        "semantic"
                    }
                    .to_string();

                    ProactiveSurfacedMemory {
                        id: m.id.0.to_string(),
                        content: m.experience.content.clone(),
                        memory_type: format!("{:?}", m.experience.experience_type),
                        score,
                        importance: m.importance(),
                        created_at: m.created_at.to_rfc3339(),
                        tags: m.experience.tags.clone(),
                        tier: format!("{:?}", m.tier),
                        relevance_reason,
                        matched_entities: matched,
                        embedding: m.experience.embeddings.clone().unwrap_or_default(),
                    }
                })
                .collect()
        })
        .await
        .map_err(|e| AppError::Internal(anyhow::anyhow!("Blocking task panicked: {e}")))?
    };

    let t_recall = op_start.elapsed();
    tracing::info!(
        user_id = %req.user_id,
        recall_ms = format!("{:.2}", (t_recall - t_prospective).as_secs_f64() * 1000.0),
        cumulative_ms = format!("{:.2}", t_recall.as_secs_f64() * 1000.0),
        memories_found = memories.len(),
        "proactive_context [phase:recall] memory retrieval complete"
    );

    // 2.5. Coactivation already recorded inside semantic_retrieve() — no duplicate call.
    // Previously this fired a second record_memory_coactivation() on the filtered subset,
    // inflating Hebbian edge weights 2x per proactive_context call.

    // 3. Store pending feedback (fast, in-memory — do before parallel block)
    if embedding_valid {
        let surfaced_infos: Vec<feedback::SurfacedMemoryInfo> = memories
            .iter()
            .map(|m| {
                let id = uuid::Uuid::parse_str(&m.id).unwrap_or_else(|_| uuid::Uuid::new_v4());
                feedback::SurfacedMemoryInfo {
                    id: MemoryId(id),
                    entities: feedback::extract_entities_simple(&m.content),
                    content_preview: m.content.chars().take(100).collect(),
                    score: m.score,
                    embedding: m.embedding.clone(),
                }
            })
            .collect();

        let surfaced_memory_ids: Vec<MemoryId> = memories
            .iter()
            .filter_map(|m| uuid::Uuid::parse_str(&m.id).ok())
            .map(MemoryId)
            .collect();

        let pending = feedback::PendingFeedback::new(
            req.user_id.clone(),
            req.context.clone(),
            context_embedding.clone(),
            surfaced_infos,
        );
        let feedback_store = state.feedback_store.clone();
        {
            let mut store = feedback_store.write();
            store.set_pending(pending);

            // Track this context for repetition/topic-change detection on the next call.
            // detect_context_pattern() compares the next call's embedding against this one.
            store.set_previous_context(
                &req.user_id,
                req.context.clone(),
                context_embedding.clone(),
                surfaced_memory_ids,
            );
        }
    }

    let t_feedback_store = op_start.elapsed();
    tracing::info!(
        user_id = %req.user_id,
        feedback_store_ms = format!("{:.2}", (t_feedback_store - t_recall).as_secs_f64() * 1000.0),
        cumulative_ms = format!("{:.2}", t_feedback_store.as_secs_f64() * 1000.0),
        "proactive_context [phase:coactivation+feedback_store] coactivation + feedback store complete"
    );

    // 4. Auto-ingest previous assistant response — fire-and-forget
    if req.auto_ingest {
        if let Some(ref prev_response) = req.previous_response {
            let response_text = prev_response.trim();
            let is_meaningful = response_text.len() > 100
                && response_text.len() < 3000
                && !response_text.starts_with("```")
                && !is_boilerplate_response(response_text);

            if is_meaningful {
                let response_text_owned = response_text.to_string();
                let memory = memory_system.clone();
                tokio::task::spawn(async move {
                    let _ = tokio::task::spawn_blocking(move || {
                        let memory_guard = memory.read();
                        let segmenter = SegmentationEngine::new();
                        let segments =
                            segmenter.segment(&response_text_owned, InputSource::AutoIngest);
                        for segment in segments {
                            let content = format!(
                                "[Assistant: {:?}] {}",
                                segment.experience_type, segment.content
                            );
                            let experience = Experience {
                                content,
                                experience_type: segment.experience_type,
                                entities: segment.entities,
                                tags: vec![
                                    "assistant-response".to_string(),
                                    "auto-captured".to_string(),
                                ],
                                context: super::remember::build_rich_context(
                                    None,
                                    None,
                                    None,
                                    Some("ai_generated".to_string()),
                                    Some(0.6),
                                    None,
                                    None,
                                    None,
                                ),
                                ..Default::default()
                            };
                            let _ = memory_guard.remember(experience, None);
                        }
                    })
                    .await;
                });
            }
        }
    }

    // 5. Auto-ingest user context — fire-and-forget with ID capture
    // Dedup: skip if identical content was ingested within the last 5 seconds
    // (prevents hook + MCP tool from double-ingesting the same user message)
    static INGEST_DEDUP: std::sync::LazyLock<
        parking_lot::Mutex<std::collections::HashMap<u64, std::time::Instant>>,
    > = std::sync::LazyLock::new(|| parking_lot::Mutex::new(std::collections::HashMap::new()));

    // req.context is already cleaned at handler entry — no redundant strip needed
    let clean_context = req.context.clone();
    let content_hash = {
        use std::hash::{Hash, Hasher};
        let mut hasher = std::collections::hash_map::DefaultHasher::new();
        req.user_id.hash(&mut hasher);
        clean_context.hash(&mut hasher);
        hasher.finish()
    };
    let is_duplicate = {
        let mut dedup = INGEST_DEDUP.lock();
        dedup.retain(|_, t| t.elapsed().as_secs() < 10);
        match dedup.entry(content_hash) {
            std::collections::hash_map::Entry::Occupied(_) => true,
            std::collections::hash_map::Entry::Vacant(e) => {
                e.insert(std::time::Instant::now());
                false
            }
        }
    };
    let should_ingest = req.auto_ingest
        && !is_duplicate
        && clean_context.len() > 50
        && clean_context.len() < 5000
        && !is_bare_question(&clean_context)
        && !is_tool_output_noise(&clean_context)
        && has_sufficient_alpha_ratio(&clean_context)
        && !is_formatted_recall_output(&clean_context);

    let ingested_memory_id = if should_ingest {
        let context = clean_context;
        let memory = memory_system.clone();
        let (tx, rx) = tokio::sync::oneshot::channel();
        tokio::task::spawn(async move {
            let result = tokio::task::spawn_blocking(move || {
                let memory_guard = memory.read();
                let segmenter = SegmentationEngine::new();
                let segments = segmenter.segment(&context, InputSource::AutoIngest);
                let mut first_id = None;
                for segment in segments {
                    let experience = Experience {
                        content: segment.content,
                        experience_type: segment.experience_type,
                        entities: segment.entities,
                        tags: vec!["auto-captured".to_string()],
                        context: super::remember::build_rich_context(
                            None,
                            None,
                            None,
                            Some("user".to_string()),
                            Some(0.9),
                            None,
                            None,
                            None,
                        ),
                        ..Default::default()
                    };
                    if let Ok(id) = memory_guard.remember(experience, None) {
                        if first_id.is_none() {
                            first_id = Some(id);
                        }
                    }
                }
                first_id
            })
            .await;
            let _ = tx.send(result);
        });
        match tokio::time::timeout(std::time::Duration::from_millis(50), rx).await {
            Ok(Ok(Ok(Some(id)))) => Some(id.0.to_string()),
            _ => None,
        }
    } else {
        None
    };

    let t_auto_ingest = op_start.elapsed();
    tracing::info!(
        user_id = %req.user_id,
        auto_ingest_ms = format!("{:.2}", (t_auto_ingest - t_feedback_store).as_secs_f64() * 1000.0),
        cumulative_ms = format!("{:.2}", t_auto_ingest.as_secs_f64() * 1000.0),
        ingested = ingested_memory_id.is_some(),
        "proactive_context [phase:auto_ingest] context + response ingest complete"
    );

    // 6. Collect fact entities synchronously (fast iteration, needed before parallel block)
    let fact_entity_list: Vec<String> = {
        let mut all_entities: std::collections::HashSet<String> = std::collections::HashSet::new();
        for name in &context_entity_names {
            all_entities.insert(name.clone());
        }
        for mem in &memories {
            for tag in &mem.tags {
                let lower = tag.to_lowercase();
                if lower.len() > 2 {
                    all_entities.insert(lower);
                }
            }
        }
        for word in req.context.split_whitespace() {
            let clean = word
                .trim_matches(|c: char| !c.is_alphanumeric())
                .to_lowercase();
            if clean.len() > 3 {
                all_entities.insert(clean);
            }
        }
        all_entities.into_iter().take(15).collect()
    };

    // 7. Parallel block: due reminders + context reminders + todos + facts
    // All 4 are independent blocking I/O operations — run concurrently via tokio::join!

    // Prepare clones for parallel tasks
    let due_uid = req.user_id.clone();
    let due_prospective = state.prospective_store.clone();

    let todo_uid = req.user_id.clone();
    let todo_emb = context_embedding.clone();
    let todo_store = state.todo_store.clone();

    let fact_uid = req.user_id.clone();
    let fact_memory = memory_system.clone();

    // B: Context-triggered reminders — reuse cached results from step 1.8
    // (already checked before recall to build prospective_signals for the boost)
    let context_reminders_from_cache: Vec<ReminderItem> = cached_context_triggers
        .into_iter()
        .map(|(t, score)| {
            let overdue = t.overdue_seconds();
            ReminderItem {
                id: t.id.0.to_string(),
                content: t.content,
                trigger_type: format!("context (score: {score:.2})"),
                status: format!("{:?}", t.status).to_lowercase(),
                due_at: t.trigger.due_at(),
                created_at: t.created_at,
                triggered_at: t.triggered_at,
                dismissed_at: t.dismissed_at,
                priority: t.priority,
                tags: t.tags,
                overdue_seconds: overdue,
            }
        })
        .collect();

    let (due_result, todo_result, fact_result) = tokio::join!(
        // A: Due reminders
        tokio::task::spawn_blocking(move || {
            due_prospective
                .get_due_tasks(&due_uid)
                .unwrap_or_default()
                .into_iter()
                .map(|t| {
                    let overdue = t.overdue_seconds();
                    let trigger_type = match &t.trigger {
                        ProspectiveTrigger::AtTime { .. } => "time".to_string(),
                        ProspectiveTrigger::AfterDuration { .. } => "duration".to_string(),
                        ProspectiveTrigger::OnContext { .. } => "context".to_string(),
                    };
                    ReminderItem {
                        id: t.id.0.to_string(),
                        content: t.content,
                        trigger_type,
                        status: format!("{:?}", t.status).to_lowercase(),
                        due_at: t.trigger.due_at(),
                        created_at: t.created_at,
                        triggered_at: t.triggered_at,
                        dismissed_at: t.dismissed_at,
                        priority: t.priority,
                        tags: t.tags,
                        overdue_seconds: overdue,
                    }
                })
                .collect::<Vec<ReminderItem>>()
        }),
        // C: Todo search (was inline blocking — now properly in spawn_blocking)
        tokio::task::spawn_blocking(move || {
            let semantic_results = todo_store
                .search_similar(&todo_uid, &todo_emb, 10)
                .unwrap_or_default();

            let mut todos_with_scores: Vec<ProactiveTodoItem> = semantic_results
                .into_iter()
                .filter(|(t, _score)| {
                    matches!(
                        t.status,
                        TodoStatus::Todo | TodoStatus::InProgress | TodoStatus::Blocked
                    )
                })
                .map(|(t, score)| {
                    let project_name = t.project_id.as_ref().and_then(|pid| {
                        todo_store
                            .get_project(&todo_uid, pid)
                            .ok()
                            .flatten()
                            .map(|p| p.name)
                    });
                    ProactiveTodoItem {
                        id: t.id.0.to_string(),
                        short_id: t.short_id(),
                        content: t.content.clone(),
                        status: format!("{:?}", t.status).to_lowercase(),
                        priority: t.priority.indicator().to_string(),
                        project: project_name,
                        due_date: t.due_date.map(|d| d.format("%Y-%m-%d").to_string()),
                        relevance_reason: format!("semantic: {:.0}%", score * 100.0),
                        similarity_score: Some(score),
                    }
                })
                .collect();

            // Also include in_progress todos for work continuity
            let in_progress_candidates = todo_store
                .list_todos_for_user(&todo_uid, None)
                .unwrap_or_default()
                .into_iter()
                .filter(|t| t.status == TodoStatus::InProgress)
                .collect::<Vec<_>>();

            let in_progress_todos: Vec<ProactiveTodoItem> = in_progress_candidates
                .into_iter()
                .filter(|t| !todos_with_scores.iter().any(|s| s.id == t.id.0.to_string()))
                .map(|t| {
                    let project_name = t.project_id.as_ref().and_then(|pid| {
                        todo_store
                            .get_project(&todo_uid, pid)
                            .ok()
                            .flatten()
                            .map(|p| p.name)
                    });
                    ProactiveTodoItem {
                        id: t.id.0.to_string(),
                        short_id: t.short_id(),
                        content: t.content.clone(),
                        status: "in_progress".to_string(),
                        priority: t.priority.indicator().to_string(),
                        project: project_name,
                        due_date: t.due_date.map(|d| d.format("%Y-%m-%d").to_string()),
                        relevance_reason: "active work".to_string(),
                        similarity_score: None,
                    }
                })
                .collect();

            todos_with_scores.extend(in_progress_todos);
            todos_with_scores.sort_by(|a, b| {
                let a_ip = a.status == "in_progress";
                let b_ip = b.status == "in_progress";
                match (a_ip, b_ip) {
                    (true, false) => std::cmp::Ordering::Less,
                    (false, true) => std::cmp::Ordering::Greater,
                    _ => b
                        .similarity_score
                        .unwrap_or(0.0)
                        .total_cmp(&a.similarity_score.unwrap_or(0.0)),
                }
            });
            todos_with_scores
                .into_iter()
                .take(5)
                .collect::<Vec<ProactiveTodoItem>>()
        }),
        // D: Fact surfacing with quality gates
        async {
            if fact_entity_list.is_empty() {
                return Ok(Vec::new());
            }
            tokio::task::spawn_blocking(move || {
                let memory_guard = fact_memory.read();
                let mut found: std::collections::HashMap<String, ProactiveFact> =
                    std::collections::HashMap::new();
                for entity in &fact_entity_list {
                    if let Ok(entity_facts) = memory_guard.get_facts_by_entity(&fact_uid, entity, 5)
                    {
                        for fact in entity_facts {
                            // Quality gate: skip low-confidence facts
                            if fact.confidence < 0.35 {
                                continue;
                            }
                            // Quality gate: skip single-source facts (unreliable)
                            if fact.support_count < 2 {
                                continue;
                            }
                            // Quality gate: skip ALL "relates to" entity-pair facts
                            // These are auto-generated from entity co-occurrence and are
                            // uniformly low-value noise (e.g., "X relates to Y")
                            if fact.fact.contains(" relates to ") {
                                continue;
                            }
                            // Skip facts that are too short to be meaningful
                            if fact.fact.trim().len() < 15 {
                                continue;
                            }
                            found.entry(fact.id.clone()).or_insert(ProactiveFact {
                                id: fact.id.clone(),
                                fact: fact.fact.clone(),
                                confidence: fact.confidence,
                                support_count: fact.support_count,
                                related_entities: fact.related_entities.clone(),
                            });
                        }
                    }
                }
                // Deduplicate by text similarity: if two facts share >80% words, keep higher confidence
                let mut sorted: Vec<ProactiveFact> = found.into_values().collect();
                sorted.sort_by(|a, b| b.confidence.total_cmp(&a.confidence));
                let mut deduped: Vec<ProactiveFact> = Vec::new();
                for fact in sorted {
                    let fact_words: std::collections::HashSet<&str> =
                        fact.fact.split_whitespace().collect();
                    let is_dup = deduped.iter().any(|existing| {
                        let existing_words: std::collections::HashSet<&str> =
                            existing.fact.split_whitespace().collect();
                        if fact_words.is_empty() || existing_words.is_empty() {
                            return false;
                        }
                        let intersection = fact_words.intersection(&existing_words).count();
                        let smaller = fact_words.len().min(existing_words.len());
                        intersection * 100 / smaller >= 80
                    });
                    if !is_dup {
                        deduped.push(fact);
                    }
                }
                deduped.truncate(5);
                deduped
            })
            .await
        }
    );

    let due_reminders: Vec<ReminderItem> = due_result
        .map_err(|e| AppError::Internal(anyhow::anyhow!("Due reminders task panicked: {e}")))?;
    let context_reminders: Vec<ReminderItem> = context_reminders_from_cache;
    let relevant_todos: Vec<ProactiveTodoItem> = todo_result
        .map_err(|e| AppError::Internal(anyhow::anyhow!("Todo search task panicked: {e}")))?;
    let relevant_facts: Vec<ProactiveFact> = fact_result
        .map_err(|e| AppError::Internal(anyhow::anyhow!("Fact surfacing task panicked: {e}")))?;
    let todo_count = relevant_todos.len();

    let t_parallel = op_start.elapsed();
    tracing::info!(
        user_id = %req.user_id,
        parallel_ms = format!("{:.2}", (t_parallel - t_auto_ingest).as_secs_f64() * 1000.0),
        cumulative_ms = format!("{:.2}", t_parallel.as_secs_f64() * 1000.0),
        reminders = due_reminders.len(),
        ctx_reminders = context_reminders.len(),
        todos = todo_count,
        facts = relevant_facts.len(),
        "proactive_context [phase:parallel] reminders + todos + facts complete"
    );

    let memory_count = memories.len();
    let reminder_count = due_reminders.len() + context_reminders.len();

    // Emit event for dashboard with full results
    let proactive_latency = op_start.elapsed().as_secs_f64() * 1000.0;
    let proactive_results = serde_json::json!({
        "context": req.context,
        "memory_count": memory_count,
        "reminder_count": reminder_count,
        "todo_count": todo_count,
        "latency_ms": proactive_latency,
        "memories": memories.iter().map(|m| serde_json::json!({
            "id": m.id,
            "content": m.content,
            "memory_type": m.memory_type,
            "tags": m.tags,
            "score": m.score,
            "importance": m.importance,
            "tier": m.tier,
            "created_at": m.created_at,
            "relevance_reason": m.relevance_reason,
        })).collect::<Vec<_>>(),
        "facts": relevant_facts.iter().map(|f| serde_json::json!({
            "id": f.id,
            "fact": f.fact,
            "confidence": f.confidence,
            "support_count": f.support_count,
            "related_entities": f.related_entities,
        })).collect::<Vec<_>>(),
        "todos": relevant_todos.iter().map(|t| serde_json::json!({
            "short_id": t.short_id,
            "content": t.content,
            "status": t.status,
            "priority": t.priority,
            "project": t.project,
        })).collect::<Vec<_>>(),
        "due_reminders": due_reminders.iter().map(|r| serde_json::json!({
            "id": r.id,
            "content": r.content,
            "trigger_type": r.trigger_type,
            "priority": r.priority,
        })).collect::<Vec<_>>(),
        "context_reminders": context_reminders.iter().map(|r| serde_json::json!({
            "id": r.id,
            "content": r.content,
            "trigger_type": r.trigger_type,
            "priority": r.priority,
        })).collect::<Vec<_>>(),
        "detected_entities": detected_entities.iter().map(|e| serde_json::json!({
            "name": e.name,
            "type": e.entity_type,
        })).collect::<Vec<_>>(),
    });
    state.emit_event(MemoryEvent {
        event_type: "PROACTIVE_CONTEXT".to_string(),
        timestamp: chrono::Utc::now(),
        user_id: req.user_id.clone(),
        memory_id: ingested_memory_id.clone(),
        content_preview: Some(req.context.chars().take(50).collect()),
        memory_type: Some("proactive".to_string()),
        importance: None,
        count: Some(memory_count + reminder_count),
        entities: None,
        results: Some(proactive_results),
    });

    // Audit log for proactive context operations
    state.log_event(
        &req.user_id,
        "PROACTIVE_CONTEXT",
        ingested_memory_id.as_deref().unwrap_or("none"),
        &format!(
            "Context='{}' surfaced {} memories, {} reminders, {} todos (auto_ingest={})",
            req.context.chars().take(50).collect::<String>(),
            memory_count,
            reminder_count,
            todo_count,
            req.auto_ingest
        ),
    );

    // Track session event for memories surfaced
    if memory_count > 0 {
        let session_id = state.session_store.get_or_create_session(&req.user_id);
        let memory_ids: Vec<String> = memories.iter().map(|m| m.id.clone()).collect();
        let avg_score = if !memories.is_empty() {
            memories.iter().map(|m| m.score).sum::<f32>() / memories.len() as f32
        } else {
            0.0
        };
        state.session_store.add_event(
            &session_id,
            SessionEvent::MemoriesSurfaced {
                timestamp: chrono::Utc::now(),
                query_preview: req.context.chars().take(100).collect(),
                memory_count,
                memory_ids,
                avg_score,
            },
        );
    }

    let latency_ms = op_start.elapsed().as_secs_f64() * 1000.0;

    tracing::info!(
        user_id = %req.user_id,
        response_assembly_ms = format!("{:.2}", (op_start.elapsed() - t_parallel).as_secs_f64() * 1000.0),
        total_ms = format!("{:.2}", latency_ms),
        memories = memory_count,
        reminders = reminder_count,
        todos = todo_count,
        facts = relevant_facts.len(),
        "proactive_context [phase:total] === COMPLETE ==="
    );

    Ok(Json(ProactiveContextResponse {
        memories,
        due_reminders,
        context_reminders,
        memory_count,
        reminder_count,
        ingested_memory_id,
        feedback_processed,
        relevant_todos,
        todo_count,
        relevant_facts,
        latency_ms,
        detected_entities,
        temporal_credits_applied: if temporal_credits_total > 0 {
            Some(temporal_credits_total)
        } else {
            None
        },
    }))
}

// =============================================================================
// SURFACE RELEVANT HANDLER
// =============================================================================

/// POST /api/relevant - Proactive memory surfacing
/// Returns relevant memories based on current context using entity matching
/// and semantic similarity. Target latency: <30ms
#[tracing::instrument(skip(state), fields(user_id = %req.user_id))]
pub async fn surface_relevant(
    State(state): State<AppState>,
    Json(req): Json<relevance::RelevanceRequest>,
) -> Result<Json<relevance::RelevanceResponse>, AppError> {
    validation::validate_user_id(&req.user_id).map_validation_err("user_id")?;
    validation::validate_max_results(req.config.max_results).map_validation_err("max_results")?;

    let memory_sys = state
        .get_user_memory(&req.user_id)
        .map_err(AppError::Internal)?;
    let graph_memory = state
        .get_user_graph(&req.user_id)
        .map_err(AppError::Internal)?;
    let engine = state.relevance_engine.clone();
    let feedback_store = state.feedback_store.clone();

    let response = {
        let memory_sys = memory_sys.clone();
        let graph_memory = graph_memory.clone();
        let context = req.context.clone();
        let config = req.config.clone();

        tokio::task::spawn_blocking(move || {
            let memory_guard = memory_sys.read();
            let graph_guard = graph_memory.read();
            engine.surface_relevant(
                &context,
                &memory_guard,
                Some(&*graph_guard),
                &config,
                Some(&feedback_store),
            )
        })
        .await
        .map_err(|e| AppError::Internal(anyhow::anyhow!("Blocking task panicked: {e}")))?
        .map_err(AppError::Internal)?
    };

    // Broadcast RETRIEVE event for real-time dashboard
    state.emit_event(MemoryEvent {
        event_type: "RETRIEVE".to_string(),
        timestamp: chrono::Utc::now(),
        user_id: req.user_id.clone(),
        memory_id: None,
        content_preview: Some(req.context.chars().take(50).collect()),
        memory_type: Some("proactive".to_string()),
        importance: None,
        count: Some(response.memories.len()),
        entities: None,
        results: None,
    });

    Ok(Json(response))
}

// =============================================================================
// TRACKED RECALL HANDLER
// =============================================================================

/// POST /api/recall/tracked - Retrieval with tracking for later feedback
///
/// Use this when you want to provide feedback later on whether memories were helpful.
/// Returns memory_ids that can be passed to /api/reinforce for Hebbian strengthening.
#[tracing::instrument(skip(state), fields(user_id = %req.user_id, query = %req.query))]
pub async fn recall_tracked(
    State(state): State<AppState>,
    Json(req): Json<TrackedRetrieveRequest>,
) -> Result<Json<TrackedRetrieveResponse>, AppError> {
    let op_start = std::time::Instant::now();
    validation::validate_user_id(&req.user_id).map_validation_err("user_id")?;
    validation::validate_max_results(req.limit).map_validation_err("limit")?;

    let memory = state
        .get_user_memory(&req.user_id)
        .map_err(AppError::Internal)?;

    let query_text = req.query.clone();
    let limit = req.limit;
    let user_id = req.user_id.clone();
    let retrieval_mode = parse_retrieval_mode(&req.mode);

    let memories = {
        let memory = memory.clone();
        tokio::task::spawn_blocking(move || {
            let memory_guard = memory.read();
            let query = MemoryQuery {
                user_id: Some(user_id),
                query_text: Some(query_text),
                max_results: limit,
                retrieval_mode,
                ..Default::default()
            };
            memory_guard.recall(&query).unwrap_or_default()
        })
        .await
        .map_err(|e| AppError::Internal(anyhow::anyhow!("Blocking task panicked: {e}")))?
    };

    // Extract memory IDs for tracking
    let memory_ids: Vec<String> = memories.iter().map(|m| m.id.0.to_string()).collect();

    // Generate tracking ID (could be stored for audit, but for now just a UUID)
    let tracking_id = uuid::Uuid::new_v4().to_string();

    // Normalize scores relative to top result (same as recall handler)
    let raw_scores: Vec<f32> = memories
        .iter()
        .map(|m| m.score.unwrap_or_else(|| m.salience_score_with_access()))
        .collect();
    let top_score = raw_scores.iter().cloned().fold(0.0_f32, f32::max);

    let recall_memories: Vec<RecallMemory> = memories
        .iter()
        .zip(raw_scores.iter())
        .map(|(m, &raw)| {
            let score = if top_score > 0.0 {
                (raw / top_score) * 0.95
            } else {
                0.0
            };
            RecallMemory {
                id: m.id.0.to_string(),
                experience: RecallExperience {
                    content: m.experience.content.clone(),
                    memory_type: Some(format!("{:?}", m.experience.experience_type)),
                    tags: m.experience.entities.clone(),
                },
                importance: m.importance(),
                created_at: m.created_at.to_rfc3339(),
                score,
                tier: format!("{:?}", m.tier),
            }
        })
        .collect();

    let count = recall_memories.len();

    // Record metrics
    let duration = op_start.elapsed().as_secs_f64();
    metrics::MEMORY_RETRIEVE_DURATION
        .with_label_values(&["tracked"])
        .observe(duration);
    metrics::MEMORY_RETRIEVE_TOTAL
        .with_label_values(&["tracked", "success"])
        .inc();
    metrics::MEMORY_RETRIEVE_RESULTS
        .with_label_values(&["tracked"])
        .observe(count as f64);

    Ok(Json(TrackedRetrieveResponse {
        tracking_id,
        ids: memory_ids,
        memories: recall_memories,
    }))
}

// =============================================================================
// REINFORCE FEEDBACK HANDLER
// =============================================================================

/// POST /api/reinforce - Hebbian reinforcement based on task outcome
///
/// Call this after using memories to complete a task:
/// - "helpful": Memories that helped → boost importance, strengthen associations
/// - "misleading": Memories that misled → reduce importance, don't strengthen
/// - "neutral": Just record access, mild strengthening
#[tracing::instrument(skip(state), fields(user_id = %req.user_id, outcome = %req.outcome, count = req.ids.len()))]
pub async fn reinforce_feedback(
    State(state): State<AppState>,
    Json(req): Json<ReinforceFeedbackRequest>,
) -> Result<Json<ReinforceFeedbackResponse>, AppError> {
    let op_start = std::time::Instant::now();

    validation::validate_user_id(&req.user_id).map_validation_err("user_id")?;

    if req.ids.is_empty() {
        return Ok(Json(ReinforceFeedbackResponse {
            memories_processed: 0,
            associations_strengthened: 0,
            importance_boosts: 0,
            importance_decays: 0,
        }));
    }

    // Parse outcome
    let outcome_label = req.outcome.to_lowercase();
    let outcome = match outcome_label.as_str() {
        "helpful" => crate::memory::RetrievalOutcome::Helpful,
        "misleading" => crate::memory::RetrievalOutcome::Misleading,
        _ => crate::memory::RetrievalOutcome::Neutral,
    };

    // Convert string IDs to MemoryId
    let memory_ids: Vec<MemoryId> = req
        .ids
        .iter()
        .filter_map(|id| uuid::Uuid::parse_str(id).ok())
        .map(MemoryId)
        .collect();

    if memory_ids.is_empty() {
        return Err(AppError::InvalidInput {
            field: "ids".to_string(),
            reason: "No valid UUIDs provided".to_string(),
        });
    }

    let memory = state
        .get_user_memory(&req.user_id)
        .map_err(AppError::Internal)?;

    // Run reinforcement in blocking task (involves RocksDB writes)
    let graph = state.get_user_graph(&req.user_id).ok();

    let memory_uuids: Vec<uuid::Uuid> = memory_ids.iter().map(|m| m.0).collect();
    let mut stats = {
        let memory = memory.clone();
        tokio::task::spawn_blocking(move || {
            let memory_guard = memory.read();
            memory_guard.reinforce_recall(&memory_ids, outcome)
        })
        .await
        .map_err(|e| AppError::Internal(anyhow::anyhow!("Blocking task panicked: {e}")))?
        .map_err(AppError::Internal)?
    };

    // Propagate feedback to entity salience in the knowledge graph
    if let Some(graph) = graph {
        let boost = match outcome {
            crate::memory::RetrievalOutcome::Helpful => {
                crate::constants::ENTITY_SALIENCE_HELPFUL_BOOST
            }
            crate::memory::RetrievalOutcome::Misleading => {
                crate::constants::ENTITY_SALIENCE_MISLEADING_PENALTY
            }
            crate::memory::RetrievalOutcome::Neutral => 0.0,
        };
        if boost != 0.0 {
            let graph_clone = graph.clone();
            let uuids = memory_uuids.clone();
            match tokio::task::spawn_blocking(move || {
                let graph_guard = graph_clone.read();
                graph_guard.reinforce_entity_salience(&uuids, boost)
            })
            .await
            {
                Ok(Ok(count)) => {
                    stats.entity_edges_reinforced = count;
                    tracing::info!(
                        entities_reinforced = count,
                        boost = boost,
                        "Entity salience reinforcement applied"
                    );
                }
                Ok(Err(e)) => tracing::warn!("Entity reinforcement failed: {}", e),
                Err(e) => tracing::warn!("Entity reinforcement task panicked: {}", e),
            }
        }
    }

    tracing::info!(
        user_id = %req.user_id,
        processed = stats.memories_processed,
        strengthened = stats.associations_strengthened,
        boosts = stats.importance_boosts,
        decays = stats.importance_decays,
        entity_salience_reinforced = stats.entity_edges_reinforced,
        "Hebbian reinforcement applied"
    );

    // Record metrics
    let duration = op_start.elapsed().as_secs_f64();
    metrics::HEBBIAN_REINFORCE_DURATION
        .with_label_values(&[&outcome_label])
        .observe(duration);
    metrics::HEBBIAN_REINFORCE_TOTAL
        .with_label_values(&[&outcome_label, &String::from("success")])
        .inc();

    Ok(Json(ReinforceFeedbackResponse {
        memories_processed: stats.memories_processed,
        associations_strengthened: stats.associations_strengthened,
        importance_boosts: stats.importance_boosts,
        importance_decays: stats.importance_decays,
    }))
}

// =============================================================================
// RECALL BY TAGS HANDLER
// =============================================================================

/// POST /api/recall/tags - Recall memories by tags
///
/// Returns memories matching ANY of the provided tags.
#[tracing::instrument(skip(state), fields(user_id = %req.user_id))]
pub async fn recall_by_tags(
    State(state): State<AppState>,
    Json(req): Json<RecallByTagsRequest>,
) -> Result<Json<RetrieveResponse>, AppError> {
    validation::validate_user_id(&req.user_id).map_validation_err("user_id")?;

    if let Some(limit) = req.limit {
        validation::validate_max_results(limit).map_validation_err("limit")?;
    }

    if req.tags.is_empty() {
        return Err(AppError::InvalidInput {
            field: "tags".to_string(),
            reason: "At least one tag must be provided".to_string(),
        });
    }

    let memory_sys = state
        .get_user_memory(&req.user_id)
        .map_err(AppError::Internal)?;

    let memory_guard = memory_sys.read();
    let limit = req.limit.unwrap_or(50);

    // Use recall_by_tags which increments the retrieval counter
    let raw_memories = memory_guard
        .recall_by_tags(&req.tags, limit)
        .map_err(AppError::Internal)?;
    let count = raw_memories.len();

    // Serialize memories to JSON for response
    let memories: Vec<serde_json::Value> = raw_memories
        .into_iter()
        .filter_map(|m| serde_json::to_value(&m).ok())
        .collect();

    info!(
        "📋 Recall by tags: user={}, tags={:?}, found={}",
        req.user_id, req.tags, count
    );

    // Broadcast RETRIEVE event for real-time dashboard
    state.emit_event(MemoryEvent {
        event_type: "RETRIEVE".to_string(),
        timestamp: chrono::Utc::now(),
        user_id: req.user_id.clone(),
        memory_id: None,
        content_preview: Some(format!("tags: {}", req.tags.join(", "))),
        memory_type: Some("by_tags".to_string()),
        importance: None,
        count: Some(count),
        entities: None,
        results: None,
    });

    Ok(Json(RetrieveResponse { memories, count }))
}

// =============================================================================
// RECALL BY DATE HANDLER
// =============================================================================

/// POST /api/recall/date - Recall memories by date range
///
/// Returns memories created within the specified date range.
#[tracing::instrument(skip(state), fields(user_id = %req.user_id))]
pub async fn recall_by_date(
    State(state): State<AppState>,
    Json(req): Json<RecallByDateRequest>,
) -> Result<Json<RetrieveResponse>, AppError> {
    validation::validate_user_id(&req.user_id).map_validation_err("user_id")?;

    if let Some(limit) = req.limit {
        validation::validate_max_results(limit).map_validation_err("limit")?;
    }

    if req.end < req.start {
        return Err(AppError::InvalidInput {
            field: "end".to_string(),
            reason: "End date must be after start date".to_string(),
        });
    }

    let memory_sys = state
        .get_user_memory(&req.user_id)
        .map_err(AppError::Internal)?;

    let memory_guard = memory_sys.read();
    let limit = req.limit.unwrap_or(50);

    // Use recall_by_date which increments the retrieval counter
    let raw_memories = memory_guard
        .recall_by_date(req.start, req.end, limit)
        .map_err(AppError::Internal)?;
    let count = raw_memories.len();

    // Serialize memories to JSON for response
    let memories: Vec<serde_json::Value> = raw_memories
        .into_iter()
        .filter_map(|m| serde_json::to_value(&m).ok())
        .collect();

    info!(
        "📅 Recall by date: user={}, start={}, end={}, found={}",
        req.user_id, req.start, req.end, count
    );

    // Broadcast RETRIEVE event for real-time dashboard
    state.emit_event(MemoryEvent {
        event_type: "RETRIEVE".to_string(),
        timestamp: chrono::Utc::now(),
        user_id: req.user_id.clone(),
        memory_id: None,
        content_preview: Some(format!(
            "{} to {}",
            req.start.format("%Y-%m-%d"),
            req.end.format("%Y-%m-%d")
        )),
        memory_type: Some("by_date".to_string()),
        importance: None,
        count: Some(count),
        entities: None,
        results: None,
    });

    Ok(Json(RetrieveResponse { memories, count }))
}