eidetic-engine 0.15.2

Durable, local-first, explainable memory for coding agents.
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
//! Shadow-tuning label extraction and replay evaluator (ADR 0070,
//! bd-2tehh.2 S1+S2).
//!
//! Joins outcome-labeled retrieval triples `(query, memory, signal, weight,
//! age)` from persisted state only:
//!
//! - **Dense source** (weight 1.0): feedback events whose `evidence_json`
//!   carries the `ee.outcome.pack_item_evidence.v1` linkage written by the
//!   `ee outcome --pack/--item` path. The referenced pack record's task text
//!   is the query. Historical events recorded before that linkage shipped
//!   have no pack evidence and are never guessed dense.
//! - **Weak source** (weight 0.5): remaining memory-target feedback events
//!   temporally associated with a `search.returned_mem` audit row for the
//!   same memory. The audit row must precede the outcome (a search after the
//!   outcome cannot have caused the use) within
//!   [`LabelExtractionConfig::label_window_minutes`]; the nearest preceding
//!   row wins, with deterministic ties.
//!
//! Search audits deliberately persist only a query *hash*
//! (`redaction.strategy = query_hash_only_v1`), so weak triples resolve
//! their query text by the ratified hash-join: the audit `queryHash` is
//! matched against persisted pack-record query texts hashed with the same
//! [`audit_query_hash`] function. Weak candidates whose hash matches no
//! persisted text are counted as unreplayable — an honest denominator in
//! the tuning report — never guessed and never silently dropped.
//!
//! Quarantined feedback is excluded by construction: quarantine screening
//! happens at record time, so poisoned events land in `feedback_quarantine`
//! instead of `feedback_events` and never reach this join.
//!
//! Extraction is offline, read-only, cancellable (`&Cx` checkpoints between
//! phases and event chunks), and deterministic: triples are sorted by
//! `(query, memory_id, feedback_event_id)` and the label set is fingerprinted
//! with a length-prefixed BLAKE3 hash so evaluation reports are reproducible.

use std::collections::{BTreeMap, BTreeSet};
use std::path::Path;

use asupersync::Cx;
use chrono::{DateTime, Duration, Utc};

use crate::core::search::{
    SearchDedupMode, SearchFusionWeights, SearchHit, SearchOptions, SearchSourceMode,
    resolved_search_fusion_weights, run_search_with_read_connection_seeded_with_cx,
    search_hit_meets_relevance_floor, sort_search_hits_by_score_order,
};
use crate::db::{DbConnection, StoredAuditEntry, StoredFeedbackEvent, audit_actions};
use crate::models::MemoryScope;
use crate::obs::audit_events::query_hash as audit_query_hash;
use crate::runtime::determinism::Deterministic;
use crate::search::SpeedMode;

/// Evidence schema stamped on pack-item outcome events by the CLI outcome
/// path; the dense label source keys on it.
pub const PACK_ITEM_EVIDENCE_SCHEMA_V1: &str = "ee.outcome.pack_item_evidence.v1";

/// Default `[shadow.retrieval] label_window_minutes` (ADR 0070).
pub const DEFAULT_LABEL_WINDOW_MINUTES: u32 = 30;

/// Domain separator for the label-set fingerprint.
const LABEL_SET_HASH_DOMAIN: &str = "ee.shadow.label_set.v1";

/// Freshness half-life in days: label weight is discounted by
/// `2^(-age_days / 90)`.
const FRESHNESS_HALF_LIFE_DAYS: f64 = 90.0;

const DENSE_BASE_WEIGHT: f64 = 1.0;
const WEAK_BASE_WEIGHT: f64 = 0.5;

/// Events processed between cooperative-cancellation checkpoints.
const CANCELLATION_CHUNK: usize = 256;

/// SQL character cap applied by
/// `list_recent_pack_record_metadata_for_workspace`; query texts at the cap
/// may be truncated and must be reloaded from the full record before hashing.
const PACK_METADATA_QUERY_CHAR_CAP: usize = 2048;

/// Tuning knobs for label extraction (config-file wiring lands with the CLI
/// slice; core takes explicit values so extraction stays deterministic).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LabelExtractionConfig {
    /// Maximum minutes a `search.returned_mem` audit row may precede a
    /// memory outcome and still label it (weak source).
    pub label_window_minutes: u32,
}

impl Default for LabelExtractionConfig {
    fn default() -> Self {
        Self {
            label_window_minutes: DEFAULT_LABEL_WINDOW_MINUTES,
        }
    }
}

/// Which persisted linkage produced a labeled triple.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LabelSource {
    /// Pack-item outcome with persisted pack linkage (dense, base 1.0).
    PackItemOutcome,
    /// Temporal association with a `search.returned_mem` audit row (weak,
    /// base 0.5).
    SearchWindowAssociation,
}

impl LabelSource {
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::PackItemOutcome => "dense",
            Self::SearchWindowAssociation => "weak",
        }
    }
}

/// One outcome-labeled retrieval example with full provenance.
#[derive(Debug, Clone, PartialEq)]
pub struct LabeledTriple {
    /// Replayable query text (pack task for dense; hash-joined pack query
    /// text for weak).
    pub query: String,
    pub memory_id: String,
    /// Outcome signal as stored on the feedback event (`helpful`,
    /// `harmful`, ...). Gain mapping is the evaluator's concern.
    pub signal: String,
    /// Source base weight before the freshness discount.
    pub base_weight: f64,
    /// `base_weight * 2^(-age_days / 90)`.
    pub weight: f64,
    /// Whole-signal age of the outcome at `as_of`, in fractional days.
    pub age_days: f64,
    pub source: LabelSource,
    /// Provenance: the feedback event that produced this label.
    pub feedback_event_id: String,
    /// Provenance: dense labels carry the linked pack record id.
    pub pack_record_id: Option<String>,
    /// Provenance: weak labels carry the matched audit row id.
    pub audit_row_id: Option<String>,
}

/// Deterministic label-extraction result with honest denominators.
#[derive(Debug, Clone, PartialEq)]
pub struct LabelExtractionReport {
    /// Sorted by `(query, memory_id, feedback_event_id)`.
    pub triples: Vec<LabeledTriple>,
    pub distinct_queries: usize,
    /// Memory-target feedback events inspected.
    pub memory_event_count: usize,
    pub dense_count: usize,
    pub weak_count: usize,
    /// Events claiming pack-item linkage that could not be resolved to a
    /// persisted pack record (malformed linkage or missing record). Counted,
    /// never guessed dense and never demoted to weak.
    pub dense_unresolvable: usize,
    /// Weak candidates whose matched audit row's query hash resolved to no
    /// persisted query text — the ratified honest denominator.
    pub weak_unreplayable: usize,
    /// Weak candidates with no `search.returned_mem` row for their memory
    /// inside the label window.
    pub weak_unmatched: usize,
    /// `blake3:<hex>` fingerprint of the sorted label set.
    pub label_set_hash: String,
}

/// Label-extraction failure.
#[derive(Debug)]
pub enum ShadowTuningError {
    /// Cooperative cancellation observed at a checkpoint.
    Cancelled(asupersync::CancelReason),
    /// Storage read or stored-state integrity failure.
    Storage { message: String },
}

impl std::fmt::Display for ShadowTuningError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Cancelled(reason) => {
                write!(f, "shadow-tuning label extraction cancelled: {reason:?}")
            }
            Self::Storage { message } => {
                write!(f, "shadow-tuning storage error: {message}")
            }
        }
    }
}

impl std::error::Error for ShadowTuningError {}

fn shadow_checkpoint(cx: &Cx) -> Result<(), ShadowTuningError> {
    cx.checkpoint().map_err(|_| {
        ShadowTuningError::Cancelled(cx.cancel_reason().unwrap_or_else(|| {
            crate::core::outcome::attributed_cancel_reason(
                cx,
                asupersync::CancelKind::User,
                "shadow-tuning label extraction cancelled without a recorded reason",
            )
        }))
    })
}

fn storage_error(context: &str, error: &dyn std::fmt::Display) -> ShadowTuningError {
    ShadowTuningError::Storage {
        message: format!("{context}: {error}"),
    }
}

/// Extract the labeled-triple set for one workspace from persisted state.
///
/// Reads feedback events, `search.returned_mem` audit rows, and pack records
/// through the supplied connection, then delegates to
/// [`join_labeled_triples`]. `as_of` is an explicit input so freshness
/// discounts (and therefore reports) are reproducible.
pub fn extract_labeled_triples(
    cx: &Cx,
    connection: &DbConnection,
    workspace_id: &str,
    config: &LabelExtractionConfig,
    as_of: DateTime<Utc>,
) -> Result<LabelExtractionReport, ShadowTuningError> {
    shadow_checkpoint(cx)?;
    let events = connection
        .list_feedback_events(workspace_id)
        .map_err(|error| storage_error("list feedback events", &error))?;

    shadow_checkpoint(cx)?;
    let returned_mem_audits: Vec<StoredAuditEntry> = connection
        .list_audit_by_action(audit_actions::SEARCH_RETURNED_MEM, None)
        .map_err(|error| storage_error("list search.returned_mem audit rows", &error))?
        .into_iter()
        .filter(|row| row.workspace_id.as_deref() == Some(workspace_id))
        .collect();

    shadow_checkpoint(cx)?;
    let metadata = connection
        .list_recent_pack_record_metadata_for_workspace(workspace_id, u32::MAX)
        .map_err(|error| storage_error("list pack record metadata", &error))?;

    let mut pack_queries: BTreeMap<String, String> = BTreeMap::new();
    let mut query_text_by_hash: BTreeMap<String, String> = BTreeMap::new();
    for (index, meta) in metadata.into_iter().enumerate() {
        if index % CANCELLATION_CHUNK == 0 {
            shadow_checkpoint(cx)?;
        }
        // The metadata projection caps query text in SQL; a text at the cap
        // may be truncated, and hashing truncated text would silently break
        // the hash-join. Reload the full record for exact text.
        let query = if meta.query.chars().count() >= PACK_METADATA_QUERY_CHAR_CAP {
            connection
                .get_pack_record(&meta.id)
                .map_err(|error| storage_error("load full pack record", &error))?
                .map_or(meta.query, |record| record.query)
        } else {
            meta.query
        };
        let hash = audit_query_hash(&query);
        // Keep the lexicographically smallest text per hash so the map is
        // deterministic regardless of enumeration order.
        match query_text_by_hash.get(&hash) {
            Some(existing) if existing <= &query => {}
            _ => {
                query_text_by_hash.insert(hash, query.clone());
            }
        }
        pack_queries.insert(meta.id, query);
    }

    join_labeled_triples(
        cx,
        &events,
        &returned_mem_audits,
        &pack_queries,
        &query_text_by_hash,
        config,
        as_of,
    )
}

/// Dense-linkage classification of one event's `evidence_json`.
enum DenseLinkage {
    /// No pack-item linkage: the event is a weak candidate.
    NotDense,
    /// The evidence claims the pack-item schema but the linkage cannot be
    /// used (malformed `packId`). Never guessed either way.
    Unresolvable,
    Linked {
        pack_id: String,
    },
}

fn parse_pack_item_linkage(evidence_json: Option<&str>) -> DenseLinkage {
    let Some(raw) = evidence_json else {
        return DenseLinkage::NotDense;
    };
    let Ok(value) = serde_json::from_str::<serde_json::Value>(raw) else {
        return DenseLinkage::NotDense;
    };
    if value.get("schema").and_then(serde_json::Value::as_str) != Some(PACK_ITEM_EVIDENCE_SCHEMA_V1)
    {
        return DenseLinkage::NotDense;
    }
    match value.get("packId").and_then(serde_json::Value::as_str) {
        Some(pack_id) if !pack_id.trim().is_empty() => DenseLinkage::Linked {
            pack_id: pack_id.to_owned(),
        },
        _ => DenseLinkage::Unresolvable,
    }
}

/// One pre-parsed `search.returned_mem` audit row.
struct ParsedReturnedMem {
    id: String,
    memory_id: String,
    timestamp: DateTime<Utc>,
    query_hash: Option<String>,
}

fn parse_returned_mem_rows(
    rows: &[StoredAuditEntry],
) -> Result<Vec<ParsedReturnedMem>, ShadowTuningError> {
    let mut parsed = Vec::with_capacity(rows.len());
    for row in rows {
        if row.action != audit_actions::SEARCH_RETURNED_MEM {
            continue;
        }
        let Some(memory_id) = row.target_id.as_deref() else {
            continue;
        };
        // Audit timestamps are written by this binary as RFC 3339; a row
        // that no longer parses is stored-state corruption and silently
        // skipping it could flip a window match. Fail loudly.
        let timestamp = DateTime::parse_from_rfc3339(&row.timestamp)
            .map_err(|error| {
                storage_error(
                    &format!("audit row {} has an unparsable timestamp", row.id),
                    &error,
                )
            })?
            .with_timezone(&Utc);
        let query_hash = row
            .details
            .as_deref()
            .and_then(|details| serde_json::from_str::<serde_json::Value>(details).ok())
            .and_then(|value| {
                value
                    .get("queryHash")
                    .and_then(serde_json::Value::as_str)
                    .map(str::to_owned)
            });
        parsed.push(ParsedReturnedMem {
            id: row.id.clone(),
            memory_id: memory_id.to_owned(),
            timestamp,
            query_hash,
        });
    }
    Ok(parsed)
}

/// Join labeled triples from pre-loaded rows (pure core of the extractor).
///
/// `pack_queries` maps pack record id → task text (dense resolution);
/// `query_text_by_hash` maps [`audit_query_hash`] output → query text (weak
/// hash-join resolution).
#[allow(clippy::too_many_lines)]
pub fn join_labeled_triples(
    cx: &Cx,
    events: &[StoredFeedbackEvent],
    returned_mem_audits: &[StoredAuditEntry],
    pack_queries: &BTreeMap<String, String>,
    query_text_by_hash: &BTreeMap<String, String>,
    config: &LabelExtractionConfig,
    as_of: DateTime<Utc>,
) -> Result<LabelExtractionReport, ShadowTuningError> {
    shadow_checkpoint(cx)?;
    let window = Duration::minutes(i64::from(config.label_window_minutes));

    let parsed_audits = parse_returned_mem_rows(returned_mem_audits)?;
    let mut audits_by_memory: BTreeMap<&str, Vec<&ParsedReturnedMem>> = BTreeMap::new();
    for audit in &parsed_audits {
        audits_by_memory
            .entry(audit.memory_id.as_str())
            .or_default()
            .push(audit);
    }

    let mut triples: Vec<LabeledTriple> = Vec::new();
    let mut memory_event_count = 0_usize;
    let mut dense_unresolvable = 0_usize;
    let mut weak_unreplayable = 0_usize;
    let mut weak_unmatched = 0_usize;

    for (index, event) in events.iter().enumerate() {
        if index % CANCELLATION_CHUNK == 0 {
            shadow_checkpoint(cx)?;
        }
        if event.target_type != "memory" {
            continue;
        }
        memory_event_count += 1;

        let created_at = DateTime::parse_from_rfc3339(&event.created_at)
            .map_err(|error| {
                storage_error(
                    &format!("feedback event {} has an unparsable created_at", event.id),
                    &error,
                )
            })?
            .with_timezone(&Utc);
        let age_days = age_in_days(created_at, as_of);
        let freshness = (-age_days / FRESHNESS_HALF_LIFE_DAYS).exp2();

        match parse_pack_item_linkage(event.evidence_json.as_deref()) {
            DenseLinkage::Linked { pack_id } => match pack_queries.get(&pack_id) {
                Some(query) => triples.push(LabeledTriple {
                    query: query.clone(),
                    memory_id: event.target_id.clone(),
                    signal: event.signal.clone(),
                    base_weight: DENSE_BASE_WEIGHT,
                    weight: DENSE_BASE_WEIGHT * freshness,
                    age_days,
                    source: LabelSource::PackItemOutcome,
                    feedback_event_id: event.id.clone(),
                    pack_record_id: Some(pack_id),
                    audit_row_id: None,
                }),
                // Linked to a pack record that no longer resolves: the
                // event asserted pack context, so demoting it to the weak
                // path would double-interpret it. Count it honestly.
                None => dense_unresolvable += 1,
            },
            DenseLinkage::Unresolvable => dense_unresolvable += 1,
            DenseLinkage::NotDense => {
                let nearest = audits_by_memory
                    .get(event.target_id.as_str())
                    .into_iter()
                    .flatten()
                    .filter(|audit| {
                        audit.timestamp <= created_at && created_at - audit.timestamp <= window
                    })
                    .max_by(|a, b| a.timestamp.cmp(&b.timestamp).then_with(|| a.id.cmp(&b.id)));
                match nearest {
                    None => weak_unmatched += 1,
                    Some(audit) => {
                        let resolved = audit
                            .query_hash
                            .as_deref()
                            .and_then(|hash| query_text_by_hash.get(hash));
                        match resolved {
                            None => weak_unreplayable += 1,
                            Some(query) => triples.push(LabeledTriple {
                                query: query.clone(),
                                memory_id: event.target_id.clone(),
                                signal: event.signal.clone(),
                                base_weight: WEAK_BASE_WEIGHT,
                                weight: WEAK_BASE_WEIGHT * freshness,
                                age_days,
                                source: LabelSource::SearchWindowAssociation,
                                feedback_event_id: event.id.clone(),
                                pack_record_id: None,
                                audit_row_id: Some(audit.id.clone()),
                            }),
                        }
                    }
                }
            }
        }
    }

    shadow_checkpoint(cx)?;
    triples.sort_by(|a, b| {
        a.query
            .cmp(&b.query)
            .then_with(|| a.memory_id.cmp(&b.memory_id))
            .then_with(|| a.feedback_event_id.cmp(&b.feedback_event_id))
    });

    let distinct_queries = triples
        .iter()
        .map(|triple| triple.query.as_str())
        .collect::<BTreeSet<&str>>()
        .len();
    let dense_count = triples
        .iter()
        .filter(|triple| triple.source == LabelSource::PackItemOutcome)
        .count();
    let weak_count = triples.len() - dense_count;
    let label_set_hash = label_set_hash(&triples);

    Ok(LabelExtractionReport {
        triples,
        distinct_queries,
        memory_event_count,
        dense_count,
        weak_count,
        dense_unresolvable,
        weak_unreplayable,
        weak_unmatched,
        label_set_hash,
    })
}

fn age_in_days(created_at: DateTime<Utc>, as_of: DateTime<Utc>) -> f64 {
    let seconds = (as_of - created_at).num_seconds().max(0);
    #[allow(clippy::cast_precision_loss)]
    let seconds = seconds as f64;
    seconds / 86_400.0
}

fn append_len_prefixed(out: &mut Vec<u8>, bytes: &[u8]) {
    let len = u32::try_from(bytes.len()).unwrap_or(u32::MAX);
    out.extend_from_slice(&len.to_be_bytes());
    out.extend_from_slice(bytes);
}

/// Length-prefixed BLAKE3 fingerprint of a sorted label set.
fn label_set_hash(triples: &[LabeledTriple]) -> String {
    let mut input = Vec::new();
    append_len_prefixed(&mut input, LABEL_SET_HASH_DOMAIN.as_bytes());
    input.extend_from_slice(
        &u32::try_from(triples.len())
            .unwrap_or(u32::MAX)
            .to_be_bytes(),
    );
    for triple in triples {
        append_len_prefixed(&mut input, triple.query.as_bytes());
        append_len_prefixed(&mut input, triple.memory_id.as_bytes());
        append_len_prefixed(&mut input, triple.signal.as_bytes());
        append_len_prefixed(&mut input, triple.source.as_str().as_bytes());
        append_len_prefixed(&mut input, triple.feedback_event_id.as_bytes());
        append_len_prefixed(
            &mut input,
            triple.pack_record_id.as_deref().unwrap_or("").as_bytes(),
        );
        append_len_prefixed(
            &mut input,
            triple.audit_row_id.as_deref().unwrap_or("").as_bytes(),
        );
        input.extend_from_slice(&triple.base_weight.to_bits().to_be_bytes());
        input.extend_from_slice(&triple.weight.to_bits().to_be_bytes());
        input.extend_from_slice(&triple.age_days.to_bits().to_be_bytes());
    }
    format!("blake3:{}", blake3::hash(&input).to_hex())
}

// ===================== S2: replay evaluator (ADR 0070 §2–3) =====================
//
// Fact-check correction folded in (banked on bd-2tehh.2): the ADR names
// `SearchScoringConfig` as the tunable, but that struct has no production
// consumer — live ranking moves through `SearchFusionWeights`
// (`search.lexical_weight` / `search.semantic_weight` / `search.graph_weight`),
// passed directly to Frankensearch's weighted RRF surface. The evaluator
// therefore tunes those same upstream fusion weights, and the ADR's
// recency-tau axis is dropped: live search has no recency knob, and sweeping a
// parameter ranking ignores would report fake capability. The graph axis is
// kept but its sensitivity is degenerate today because no graph retrieval arm
// is attached to the live searcher; consumers of the evaluation must read
// `graph_axis_degenerate` honestly.
//
// Mechanism: single-replay offline re-fusion. Each distinct labeled query is
// replayed ONCE against the current index (read-only, seeded, floor disabled,
// pool capped); per-hit arm components ride on the returned `SearchHit`s, so
// every candidate vector re-scores the same pool with the production
// adjustment function, re-applies the production relevance floor, and re-sorts
// with the production ordering. No candidate can reach live ranking: the
// injection exists only inside this evaluator, and the CLI search surface has
// no weight argument (frozen by the golden help contracts).

/// ADR §3 clamps — deliberately in code next to the policy, not user config.
const FUSION_LEXICAL_CLAMP: (f32, f32) = (0.2, 0.7);
const FUSION_SEMANTIC_CLAMP: (f32, f32) = (0.2, 0.7);
const FUSION_GRAPH_CLAMP: (f32, f32) = (0.0, 0.3);
/// Fixed per-axis offset grid around the incumbent (ADR §3).
const FUSION_GRID_OFFSETS: [f32; 4] = [-0.10, -0.05, 0.05, 0.10];
const DESCENT_MAX_ROUNDS: u32 = 2;
const DESCENT_INITIAL_STEP: f32 = 0.025;
/// Replayed hit-pool cap per query. Labeled memories outside the pool count
/// as unranked (contribute 0), exactly like results beyond a live limit.
pub const REPLAY_POOL_LIMIT: u32 = 200;
const EVALUATION_HASH_DOMAIN: &str = "ee.shadow.retrieval_tuning_evaluation.v1";

/// One candidate fusion-weight vector.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct TuningWeights {
    pub lexical: f32,
    pub semantic: f32,
    pub graph: f32,
}

impl TuningWeights {
    /// The compiled-in fusion defaults (used when no workspace overlay is set).
    #[must_use]
    pub fn compiled_defaults() -> Self {
        Self::from_fusion(SearchFusionWeights::default())
    }

    /// The incumbent vector actually in effect for a workspace.
    #[must_use]
    pub fn incumbent_for_workspace(workspace_path: &Path) -> Self {
        Self::from_fusion(resolved_search_fusion_weights(workspace_path))
    }

    fn from_fusion(weights: SearchFusionWeights) -> Self {
        Self {
            lexical: weights.lexical,
            semantic: weights.semantic,
            graph: weights.graph,
        }
    }

    fn to_fusion(self) -> SearchFusionWeights {
        SearchFusionWeights {
            lexical: self.lexical,
            semantic: self.semantic,
            graph: self.graph,
        }
    }

    fn clamped(self) -> Self {
        Self {
            lexical: self
                .lexical
                .clamp(FUSION_LEXICAL_CLAMP.0, FUSION_LEXICAL_CLAMP.1),
            semantic: self
                .semantic
                .clamp(FUSION_SEMANTIC_CLAMP.0, FUSION_SEMANTIC_CLAMP.1),
            graph: self.graph.clamp(FUSION_GRAPH_CLAMP.0, FUSION_GRAPH_CLAMP.1),
        }
    }

    /// Exact-bit identity key for deduplication and deterministic ordering.
    fn key(self) -> (u32, u32, u32) {
        (
            self.lexical.to_bits(),
            self.semantic.to_bits(),
            self.graph.to_bits(),
        )
    }
}

/// One replayed query with its raw-score (pre-adjustment) hit pool.
#[derive(Debug, Clone)]
pub struct QueryReplay {
    pub query: String,
    /// Bounded final-pool hits retaining the lexical/semantic arm scores that
    /// Frankensearch exposes for native weighted-RRF replay.
    pub hits: Vec<SearchHit>,
}

/// Replay collection result with honest denominators.
#[derive(Debug, Clone)]
pub struct ReplayCollection {
    pub replays: Vec<QueryReplay>,
    /// Retained for the stable report contract. Native upstream RRF replay no
    /// longer needs to divide out a local multiplier, so this is always zero.
    pub unrecoverable_hits: usize,
}

/// Replay every distinct labeled query once against the current index.
///
/// Read-only by construction: the underlying search entry point passes no
/// audit connection, so no audit rows can be written. The relevance floor is
/// disabled at replay and re-applied per candidate offline, because floor
/// membership depends on the adjusted score each candidate produces.
///
/// Async on the caller's `Cx` so the whole tuning pipeline runs under ONE
/// runtime root — the sync search wrappers each start their own root, and
/// nesting roots from inside a running runtime is not a supported shape.
pub async fn collect_query_replays_with_cx(
    cx: &Cx,
    read_connection: &crate::db::DbConnection,
    workspace_path: &Path,
    database_path: &Path,
    queries: &BTreeSet<String>,
    as_of: DateTime<Utc>,
) -> Result<ReplayCollection, ShadowTuningError> {
    let determinism = Deterministic::from_seed(0);
    let mut replays = Vec::with_capacity(queries.len());

    for query in queries {
        shadow_checkpoint(cx)?;
        let options = SearchOptions {
            workspace_path: workspace_path.to_path_buf(),
            database_path: Some(database_path.to_path_buf()),
            index_dir: None,
            query: query.clone(),
            limit: REPLAY_POOL_LIMIT,
            speed: SpeedMode::Default,
            explain: false,
            as_of: Some(as_of),
            include_tombstoned: false,
            include_expired: false,
            include_future: false,
            include_stale: false,
            relevance_floor: Some(0.0),
            dedup_mode: SearchDedupMode::DocId,
            source_mode: SearchSourceMode::Hybrid,
            strict_source_mode: false,
            memory_scope: MemoryScope::default(),
            strict_scope: false,
        };
        let report = run_search_with_read_connection_seeded_with_cx(
            cx,
            &options,
            read_connection,
            determinism.shared_child("search.rerank"),
        )
        .await
        .map_err(|error| storage_error("replay search failed", &error))?;
        replays.push(QueryReplay {
            query: query.clone(),
            hits: report.results,
        });
    }

    Ok(ReplayCollection {
        replays,
        unrecoverable_hits: 0,
    })
}

/// Outcome gain per ADR §2. Signals outside the ADR's mapping are excluded
/// from the metric and counted, never guessed.
fn signal_gain(signal: &str) -> Option<f64> {
    match signal {
        "helpful" | "confirmation" => Some(1.0),
        "harmful" | "contradiction" => Some(-2.0),
        _ => None,
    }
}

struct QueryLabelGains {
    /// `1 / Σ|w·gain|` — candidate-independent per-query normalizer.
    norm: f64,
    /// `(memory_id, w·gain)` pairs.
    gains: Vec<(String, f64)>,
}

struct GroupedLabels {
    by_query: BTreeMap<String, QueryLabelGains>,
    labels_unmapped_signal: usize,
    queries_without_gain: usize,
}

fn group_label_gains(labels: &[LabeledTriple]) -> GroupedLabels {
    let mut raw: BTreeMap<String, Vec<(String, f64)>> = BTreeMap::new();
    let mut labels_unmapped_signal = 0_usize;
    for label in labels {
        let Some(gain) = signal_gain(&label.signal) else {
            labels_unmapped_signal += 1;
            continue;
        };
        raw.entry(label.query.clone())
            .or_default()
            .push((label.memory_id.clone(), label.weight * gain));
    }
    let mut by_query = BTreeMap::new();
    let mut queries_without_gain = 0_usize;
    for (query, gains) in raw {
        let denom: f64 = gains.iter().map(|(_, value)| value.abs()).sum();
        if denom <= f64::EPSILON {
            queries_without_gain += 1;
            continue;
        }
        by_query.insert(
            query,
            QueryLabelGains {
                norm: 1.0 / denom,
                gains,
            },
        );
    }
    GroupedLabels {
        by_query,
        labels_unmapped_signal,
        queries_without_gain,
    }
}

/// Re-fuse one replayed pool through Frankensearch's weighted RRF primitive,
/// apply the production relevance floor, and return 1-based ranks by memory.
#[allow(clippy::cast_possible_truncation)]
fn ranked_pool(hits: &[SearchHit], weights: SearchFusionWeights) -> BTreeMap<String, usize> {
    let mut reranked = hits
        .iter()
        .filter(|hit| hit.source == crate::core::search::ScoreSource::Reranked)
        .cloned()
        .collect::<Vec<_>>();
    reranked.retain(|hit| search_hit_meets_relevance_floor(hit, None));
    sort_search_hits_by_score_order(&mut reranked);

    let mut lexical = hits
        .iter()
        .filter(|hit| hit.source != crate::core::search::ScoreSource::Reranked)
        .filter_map(|hit| {
            hit.lexical_score
                .filter(|score| score.is_finite())
                .map(|score| crate::search::ScoredResult {
                    doc_id: hit.doc_id.clone().into(),
                    score,
                    source: crate::search::ScoreSource::Lexical,
                    index: None,
                    fast_score: None,
                    quality_score: None,
                    lexical_score: Some(score),
                    rerank_score: None,
                    explanation: None,
                    metadata: None,
                })
        })
        .collect::<Vec<_>>();
    lexical.sort_by(|left, right| {
        right
            .score
            .total_cmp(&left.score)
            .then_with(|| left.doc_id.cmp(&right.doc_id))
    });

    let mut semantic = hits
        .iter()
        .filter(|hit| hit.source != crate::core::search::ScoreSource::Reranked)
        .filter_map(|hit| {
            hit.quality_score
                .or(hit.fast_score)
                .filter(|score| score.is_finite())
                .map(|score| (hit.doc_id.clone(), score))
        })
        .collect::<Vec<_>>();
    semantic.sort_by(|left, right| {
        right
            .1
            .total_cmp(&left.1)
            .then_with(|| left.0.cmp(&right.0))
    });
    let semantic = semantic
        .into_iter()
        .enumerate()
        .map(
            |(index, (doc_id, score))| frankensearch::core::types::VectorHit {
                index: u32::try_from(index).unwrap_or(u32::MAX),
                score,
                doc_id: doc_id.into(),
            },
        )
        .collect::<Vec<_>>();

    let (lexical_weight, semantic_weight) = weights.upstream_rrf_weights();
    let config = frankensearch::RrfConfig {
        lexical_weight,
        semantic_weight,
        ..frankensearch::RrfConfig::default()
    };
    let by_id = hits
        .iter()
        .map(|hit| (hit.doc_id.as_str(), hit))
        .collect::<BTreeMap<_, _>>();
    let fused_ids = frankensearch::rrf_fuse(&lexical, &semantic, hits.len(), 0, &config)
        .into_iter()
        .filter_map(|fused| {
            let doc_id = fused.doc_id.to_string();
            let mut hit = (*by_id.get(doc_id.as_str())?).clone();
            hit.score = fused.rrf_score as f32;
            hit.source = crate::core::search::ScoreSource::Hybrid;
            search_hit_meets_relevance_floor(&hit, None).then_some(hit.doc_id)
        })
        .collect::<Vec<_>>();
    reranked
        .into_iter()
        .map(|hit| hit.doc_id)
        .chain(fused_ids)
        .enumerate()
        .map(|(index, doc_id)| (doc_id, index + 1))
        .collect()
}

fn score_candidate(
    replays: &BTreeMap<&str, &QueryReplay>,
    grouped: &GroupedLabels,
    weights: SearchFusionWeights,
) -> f64 {
    let mut total = 0.0_f64;
    for (query, label_gains) in &grouped.by_query {
        let Some(replay) = replays.get(query.as_str()) else {
            // No replay pool for this query: every label is unranked and
            // contributes 0 (ADR §2).
            continue;
        };
        let ranks = ranked_pool(&replay.hits, weights);
        let mut query_score = 0.0_f64;
        for (memory_id, weighted_gain) in &label_gains.gains {
            if let Some(rank) = ranks.get(memory_id) {
                #[allow(clippy::cast_precision_loss)]
                let discount = (1.0 + *rank as f64).log2();
                query_score += weighted_gain / discount;
            }
        }
        total += label_gains.norm * query_score;
    }
    total
}

/// Score one candidate weight vector against replayed pools and labels
/// (the ADR §2 outcome-weighted rank metric). Exposed for property tests
/// and diagnostic tooling; the sweep uses the same path internally.
#[must_use]
pub fn score_fusion_candidate(
    replays: &[QueryReplay],
    labels: &[LabeledTriple],
    weights: TuningWeights,
) -> f64 {
    let replays_by_query: BTreeMap<&str, &QueryReplay> = replays
        .iter()
        .map(|replay| (replay.query.as_str(), replay))
        .collect();
    let grouped = group_label_gains(labels);
    score_candidate(&replays_by_query, &grouped, weights.to_fusion())
}

/// One evaluated candidate.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct CandidateScore {
    pub weights: TuningWeights,
    pub score: f64,
    /// `incumbent`, `grid`, or `descent` — deterministic provenance.
    pub origin: &'static str,
}

/// Deterministic sweep result (ADR §2–3). The §4 evidence gate and the
/// `ee.shadow.retrieval_tuning_report.v1` envelope land with S3.
#[derive(Debug, Clone, PartialEq)]
pub struct TuningEvaluation {
    pub incumbent: CandidateScore,
    /// Every non-incumbent candidate in evaluation order.
    pub candidates: Vec<CandidateScore>,
    /// Best strictly-improving candidate, if any.
    pub winner: Option<CandidateScore>,
    /// `(winner - incumbent) / |incumbent|`; `None` when the incumbent score
    /// is too close to zero for a relative margin to be meaningful.
    pub relative_margin: Option<f64>,
    pub queries_scored: usize,
    pub queries_without_gain: usize,
    pub labels_unmapped_signal: usize,
    pub labels_total: usize,
    /// No graph retrieval arm is attached to the live searcher, so graph-axis
    /// deltas must not be read as real graph-signal tuning.
    pub graph_axis_degenerate: bool,
    /// `blake3:<hex>` over the full evaluation (candidates + scores).
    pub evaluation_hash: String,
}

fn enumerate_grid(incumbent: TuningWeights) -> Vec<TuningWeights> {
    let mut seen = BTreeSet::new();
    let mut vectors = Vec::new();
    let mut push = |candidate: TuningWeights, vectors: &mut Vec<TuningWeights>| {
        if seen.insert(candidate.key()) {
            vectors.push(candidate);
        }
    };
    push(incumbent.clamped(), &mut vectors);
    for axis in 0..3_usize {
        for offset in FUSION_GRID_OFFSETS {
            let mut candidate = incumbent;
            match axis {
                0 => candidate.lexical += offset,
                1 => candidate.semantic += offset,
                _ => candidate.graph += offset,
            }
            push(candidate.clamped(), &mut vectors);
        }
    }
    vectors
}

fn descent_neighbors(center: TuningWeights, step: f32) -> Vec<TuningWeights> {
    let mut neighbors = Vec::with_capacity(6);
    for axis in 0..3_usize {
        for direction in [-1.0_f32, 1.0] {
            let mut candidate = center;
            let delta = step * direction;
            match axis {
                0 => candidate.lexical += delta,
                1 => candidate.semantic += delta,
                _ => candidate.graph += delta,
            }
            neighbors.push(candidate.clamped());
        }
    }
    neighbors
}

fn evaluation_hash(
    incumbent: &CandidateScore,
    candidates: &[CandidateScore],
    labels_total: usize,
) -> String {
    let mut input = Vec::new();
    append_len_prefixed(&mut input, EVALUATION_HASH_DOMAIN.as_bytes());
    input.extend_from_slice(
        &u32::try_from(labels_total)
            .unwrap_or(u32::MAX)
            .to_be_bytes(),
    );
    let push_candidate = |candidate: &CandidateScore, input: &mut Vec<u8>| {
        append_len_prefixed(input, candidate.origin.as_bytes());
        input.extend_from_slice(&candidate.weights.lexical.to_bits().to_be_bytes());
        input.extend_from_slice(&candidate.weights.semantic.to_bits().to_be_bytes());
        input.extend_from_slice(&candidate.weights.graph.to_bits().to_be_bytes());
        input.extend_from_slice(&candidate.score.to_bits().to_be_bytes());
    };
    push_candidate(incumbent, &mut input);
    for candidate in candidates {
        push_candidate(candidate, &mut input);
    }
    format!("blake3:{}", blake3::hash(&input).to_hex())
}

/// Evaluate the incumbent plus the deterministic candidate set against the
/// replayed pools (ADR §2–3): fixed offset grid, then ≤2 rounds of bounded
/// coordinate descent with step halving around the best vector so far.
/// Cancellable between candidate evaluations; pure — no partial state.
pub fn evaluate_fusion_candidates(
    cx: &Cx,
    replays: &[QueryReplay],
    labels: &[LabeledTriple],
    incumbent: TuningWeights,
) -> Result<TuningEvaluation, ShadowTuningError> {
    shadow_checkpoint(cx)?;
    let replays_by_query: BTreeMap<&str, &QueryReplay> = replays
        .iter()
        .map(|replay| (replay.query.as_str(), replay))
        .collect();
    let grouped = group_label_gains(labels);

    let incumbent = incumbent.clamped();
    let incumbent_score = CandidateScore {
        weights: incumbent,
        score: score_candidate(&replays_by_query, &grouped, incumbent.to_fusion()),
        origin: "incumbent",
    };

    let mut evaluated: BTreeSet<(u32, u32, u32)> = BTreeSet::new();
    evaluated.insert(incumbent.key());
    let mut candidates: Vec<CandidateScore> = Vec::new();
    for weights in enumerate_grid(incumbent) {
        if !evaluated.insert(weights.key()) {
            continue;
        }
        shadow_checkpoint(cx)?;
        candidates.push(CandidateScore {
            weights,
            score: score_candidate(&replays_by_query, &grouped, weights.to_fusion()),
            origin: "grid",
        });
    }

    let mut best = candidates
        .iter()
        .copied()
        .fold(incumbent_score, |best, candidate| {
            if candidate.score > best.score {
                candidate
            } else {
                best
            }
        });
    for round in 0..DESCENT_MAX_ROUNDS {
        let step = DESCENT_INITIAL_STEP / 2.0_f32.powi(i32::try_from(round).unwrap_or(0));
        let mut improved = false;
        for weights in descent_neighbors(best.weights, step) {
            if !evaluated.insert(weights.key()) {
                continue;
            }
            shadow_checkpoint(cx)?;
            let candidate = CandidateScore {
                weights,
                score: score_candidate(&replays_by_query, &grouped, weights.to_fusion()),
                origin: "descent",
            };
            candidates.push(candidate);
            if candidate.score > best.score {
                best = candidate;
                improved = true;
            }
        }
        if !improved {
            break;
        }
    }

    let winner = (best.origin != "incumbent" && best.score > incumbent_score.score).then_some(best);
    let relative_margin = winner.and_then(|winner| {
        (incumbent_score.score.abs() > f64::EPSILON)
            .then(|| (winner.score - incumbent_score.score) / incumbent_score.score.abs())
    });
    let hash = evaluation_hash(&incumbent_score, &candidates, labels.len());

    Ok(TuningEvaluation {
        incumbent: incumbent_score,
        candidates,
        winner,
        relative_margin,
        queries_scored: grouped.by_query.len(),
        queries_without_gain: grouped.queries_without_gain,
        labels_unmapped_signal: grouped.labels_unmapped_signal,
        labels_total: labels.len(),
        graph_axis_degenerate: true,
        evaluation_hash: hash,
    })
}

// ================ S3: evidence gate + tuning report (ADR 0070 §4) ================

/// Schema id of the persisted tuning report (normative draft in ADR 0070's
/// appendix; honest diagnostics ride under `labelSet` and `diagnostics`).
pub const RETRIEVAL_TUNING_REPORT_SCHEMA_V1: &str = "ee.shadow.retrieval_tuning_report.v1";
/// Policy id registered in `SHADOW_POLICY_INVENTORY` (src/shadow.rs).
pub const RETRIEVAL_TUNING_POLICY_ID: &str = "candidate.retrieval.outcome_tuned_weights";
/// Abstention code (response_time class). The `degraded[]` emission and its
/// failure-mode fixture land with the CLI surface in bd-2tehh.3.
pub const INSUFFICIENT_OUTCOME_EVIDENCE_CODE: &str = "insufficient_outcome_evidence";
const REPORT_HASH_DOMAIN: &str = "ee.shadow.retrieval_tuning_report.hash.v1";

/// ADR §4 evidence gate. Tune the thresholds with data; never remove them.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct RetrievalTuningGateConfig {
    pub min_triples: usize,
    pub min_queries: usize,
    /// Minimum relative winner-over-incumbent margin for `promotable`.
    pub promote_margin: f64,
}

impl Default for RetrievalTuningGateConfig {
    fn default() -> Self {
        Self {
            min_triples: 50,
            min_queries: 15,
            promote_margin: 0.03,
        }
    }
}

/// Assembled tuning report (core shape; bd-2tehh.3 persists and renders it
/// through the shadow CLI surface).
#[derive(Debug, Clone, PartialEq)]
pub struct RetrievalTuningReport {
    pub db_generation: u64,
    pub labels: LabelExtractionReport,
    pub abstained: bool,
    pub abstention_reason: Option<&'static str>,
    /// Present exactly when the gate passed.
    pub evaluation: Option<TuningEvaluation>,
    pub promotable: bool,
    /// `blake3:<hex>` over the canonical report JSON (without this field).
    pub report_hash: String,
}

/// Apply the ADR §4 evidence gate and assemble the report.
///
/// `evaluation` must be `Some` exactly when the gate passes — the caller
/// runs the sweep only after the gate admits the label set (see
/// [`run_retrieval_tuning`]); a mismatch is reported as a storage-integrity
/// error rather than guessed around.
pub fn assemble_retrieval_tuning_report(
    labels: LabelExtractionReport,
    evaluation: Option<TuningEvaluation>,
    db_generation: u64,
    gate: &RetrievalTuningGateConfig,
) -> Result<RetrievalTuningReport, ShadowTuningError> {
    let abstained =
        labels.triples.len() < gate.min_triples || labels.distinct_queries < gate.min_queries;
    if abstained != evaluation.is_none() {
        return Err(ShadowTuningError::Storage {
            message: format!(
                "evidence gate and evaluation presence disagree (abstained={abstained}, evaluation={})",
                if evaluation.is_some() {
                    "present"
                } else {
                    "absent"
                }
            ),
        });
    }
    let promotable = !abstained
        && evaluation.as_ref().is_some_and(|evaluation| {
            evaluation.winner.is_some()
                && evaluation
                    .relative_margin
                    .is_some_and(|margin| margin >= gate.promote_margin)
        });
    let mut report = RetrievalTuningReport {
        db_generation,
        labels,
        abstained,
        abstention_reason: abstained.then_some(INSUFFICIENT_OUTCOME_EVIDENCE_CODE),
        evaluation,
        promotable,
        report_hash: String::new(),
    };
    let canonical = retrieval_tuning_report_json_value(&report, false).to_string();
    let mut input = Vec::new();
    append_len_prefixed(&mut input, REPORT_HASH_DOMAIN.as_bytes());
    append_len_prefixed(&mut input, canonical.as_bytes());
    report.report_hash = format!("blake3:{}", blake3::hash(&input).to_hex());
    Ok(report)
}

/// Full offline tuning pass: extract labels, gate, replay, sweep, assemble.
///
/// Read-only against the workspace; deterministic given the same database,
/// index, config, and `as_of`.
pub async fn run_retrieval_tuning_with_cx(
    cx: &Cx,
    connection: &DbConnection,
    workspace_path: &Path,
    database_path: &Path,
    workspace_id: &str,
    as_of: DateTime<Utc>,
    extraction: &LabelExtractionConfig,
    gate: &RetrievalTuningGateConfig,
) -> Result<RetrievalTuningReport, ShadowTuningError> {
    let labels = extract_labeled_triples(cx, connection, workspace_id, extraction, as_of)?;
    let db_generation = connection
        .get_workspace_generation(workspace_id)
        .map_err(|error| storage_error("read workspace generation", &error))?
        .unwrap_or(0);
    if labels.triples.len() < gate.min_triples || labels.distinct_queries < gate.min_queries {
        return assemble_retrieval_tuning_report(labels, None, db_generation, gate);
    }
    let queries: BTreeSet<String> = labels
        .triples
        .iter()
        .map(|triple| triple.query.clone())
        .collect();
    let replays = collect_query_replays_with_cx(
        cx,
        connection,
        workspace_path,
        database_path,
        &queries,
        as_of,
    )
    .await?;
    let incumbent = TuningWeights::incumbent_for_workspace(workspace_path);
    let evaluation = evaluate_fusion_candidates(cx, &replays.replays, &labels.triples, incumbent)?;
    assemble_retrieval_tuning_report(labels, Some(evaluation), db_generation, gate)
}

/// Synchronous entry for CLI handlers: runs the full tuning pass under one
/// fresh runtime root (never nest this inside an existing root).
pub fn run_retrieval_tuning(
    workspace_path: &Path,
    database_path: &Path,
    workspace_id: &str,
    as_of: DateTime<Utc>,
    extraction: &LabelExtractionConfig,
    gate: &RetrievalTuningGateConfig,
) -> Result<RetrievalTuningReport, ShadowTuningError> {
    crate::core::run_cli_with_cx(std::time::Duration::from_secs(600), |cx| async move {
        let connection =
            DbConnection::open_file(database_path).map_err(|error| ShadowTuningError::Storage {
                message: format!("open workspace database: {error}"),
            })?;
        run_retrieval_tuning_with_cx(
            &cx,
            &connection,
            workspace_path,
            database_path,
            workspace_id,
            as_of,
            extraction,
            gate,
        )
        .await
    })
    .map_err(|error| ShadowTuningError::Storage {
        message: format!("start shadow-tuning runtime: {error}"),
    })?
}

fn weights_json(weights: TuningWeights) -> serde_json::Value {
    serde_json::json!({
        "lexical": f64::from(weights.lexical),
        "semantic": f64::from(weights.semantic),
        "graph": f64::from(weights.graph),
    })
}

fn candidate_json(candidate: &CandidateScore) -> serde_json::Value {
    serde_json::json!({
        "weights": weights_json(candidate.weights),
        "score": candidate.score,
        "origin": candidate.origin,
    })
}

fn retrieval_tuning_report_json_value(
    report: &RetrievalTuningReport,
    include_hash: bool,
) -> serde_json::Value {
    let labels = &report.labels;
    #[allow(clippy::cast_precision_loss)]
    let dense_share = if labels.triples.is_empty() {
        0.0
    } else {
        labels.dense_count as f64 / labels.triples.len() as f64
    };
    let incumbent = report
        .evaluation
        .as_ref()
        .map(|evaluation| candidate_json(&evaluation.incumbent));
    let candidates = report.evaluation.as_ref().map(|evaluation| {
        evaluation
            .candidates
            .iter()
            .map(candidate_json)
            .collect::<Vec<_>>()
    });
    let winner = report.evaluation.as_ref().and_then(|evaluation| {
        evaluation.winner.map(|winner| {
            let mut value = candidate_json(&winner);
            if let Some(object) = value.as_object_mut() {
                object.insert(
                    "relativeMargin".to_owned(),
                    evaluation
                        .relative_margin
                        .map_or(serde_json::Value::Null, serde_json::Value::from),
                );
            }
            value
        })
    });
    let mut value = serde_json::json!({
        "schema": RETRIEVAL_TUNING_REPORT_SCHEMA_V1,
        "policyId": RETRIEVAL_TUNING_POLICY_ID,
        "dbGeneration": report.db_generation,
        "labelSet": {
            "triples": labels.triples.len(),
            "distinctQueries": labels.distinct_queries,
            "hash": labels.label_set_hash,
            "denseShare": dense_share,
            "denseUnresolvable": labels.dense_unresolvable,
            "weakUnreplayable": labels.weak_unreplayable,
            "weakUnmatched": labels.weak_unmatched,
        },
        "abstained": report.abstained,
        "abstentionReason": report.abstention_reason,
        "incumbent": incumbent,
        "candidates": candidates,
        "winner": winner,
        "promotable": report.promotable,
        "diagnostics": report.evaluation.as_ref().map(|evaluation| {
            serde_json::json!({
                "evaluationHash": evaluation.evaluation_hash,
                "graphAxisDegenerate": evaluation.graph_axis_degenerate,
                "queriesScored": evaluation.queries_scored,
                "queriesWithoutGain": evaluation.queries_without_gain,
                "labelsUnmappedSignal": evaluation.labels_unmapped_signal,
            })
        }),
    });
    if include_hash {
        if let Some(object) = value.as_object_mut() {
            object.insert(
                "reportHash".to_owned(),
                serde_json::Value::from(report.report_hash.clone()),
            );
        }
    }
    value
}

/// Stable JSON rendering of the tuning report.
#[must_use]
pub fn render_retrieval_tuning_report_json(report: &RetrievalTuningReport) -> String {
    retrieval_tuning_report_json_value(report, true).to_string()
}

// ============ S4: promotion mechanics (ADR 0070 §5, bd-2tehh.3) ============
//
// Live ranking changes ONLY through this explicit promote step: it validates
// the persisted report (schema, abstention, promotability, dbGeneration
// freshness), writes the [search] fusion-weight overlay into the workspace
// config via toml_edit, and records an audit row carrying the ENTIRE prior
// config.toml bytes — so demote restores the file byte-identically (RULE 1:
// nothing lost; a config that did not exist restores as an empty file, never
// a deletion).

/// Persisted report location under the workspace store directory.
pub const RETRIEVAL_TUNING_REPORT_FILENAME: &str = "retrieval_tuning_report.json";
pub const PROMOTE_RETRIEVAL_WEIGHTS_AUDIT_ACTION: &str = "shadow.promote_retrieval_weights";
pub const DEMOTE_RETRIEVAL_WEIGHTS_AUDIT_ACTION: &str = "shadow.demote_retrieval_weights";

/// `<workspace>/.ee/shadow/retrieval_tuning_report.json`.
#[must_use]
pub fn shadow_report_path(workspace_path: &Path) -> std::path::PathBuf {
    workspace_path
        .join(".ee")
        .join("shadow")
        .join(RETRIEVAL_TUNING_REPORT_FILENAME)
}

/// Persist the rendered report for the promote step to validate later.
pub fn persist_retrieval_tuning_report(
    workspace_path: &Path,
    report: &RetrievalTuningReport,
) -> Result<std::path::PathBuf, ShadowTuningError> {
    let path = shadow_report_path(workspace_path);
    if let Some(parent) = path.parent() {
        std::fs::create_dir_all(parent)
            .map_err(|error| storage_error("create shadow report directory", &error))?;
    }
    std::fs::write(&path, render_retrieval_tuning_report_json(report))
        .map_err(|error| storage_error("write shadow report", &error))?;
    Ok(path)
}

/// Typed promote refusal (exit-7 material at the CLI).
#[derive(Debug, Clone, PartialEq)]
pub enum PromoteRefusal {
    ReportMissing { path: String },
    ReportInvalid { reason: String },
    Abstained,
    NotPromotable { relative_margin: Option<f64> },
    StaleGeneration { report: u64, current: u64 },
    NoPriorPromotion,
}

impl PromoteRefusal {
    #[must_use]
    pub fn message(&self) -> String {
        match self {
            Self::ReportMissing { path } => {
                format!("no persisted tuning report at {path}; run ee shadow run first")
            }
            Self::ReportInvalid { reason } => format!("persisted tuning report unusable: {reason}"),
            Self::Abstained => {
                "the persisted report abstained (insufficient outcome evidence); nothing to promote"
                    .to_owned()
            }
            Self::NotPromotable { relative_margin } => format!(
                "the persisted report is not promotable (relative margin {:?} below the gate or no strict winner)",
                relative_margin
            ),
            Self::StaleGeneration { report, current } => format!(
                "the persisted report was evaluated at db generation {report} but the workspace is now at {current}; re-run ee shadow run"
            ),
            Self::NoPriorPromotion => {
                "no prior promotion audit found for this workspace; nothing to demote".to_owned()
            }
        }
    }
}

/// Outcome of a promote/demote apply (or dry-run plan).
#[derive(Debug, Clone, PartialEq)]
pub struct OverlayChange {
    pub applied: bool,
    /// Full prior config.toml bytes ("" when the file did not exist).
    pub prior_config: String,
    pub new_config: String,
    /// Human-readable per-key diff lines.
    pub diff: Vec<String>,
    pub report_hash: Option<String>,
}

fn workspace_config_path(workspace_path: &Path) -> std::path::PathBuf {
    workspace_path.join(".ee").join("config.toml")
}

fn read_config_bytes(workspace_path: &Path) -> Result<(String, bool), ShadowTuningError> {
    let path = workspace_config_path(workspace_path);
    match std::fs::read_to_string(&path) {
        Ok(contents) => Ok((contents, true)),
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok((String::new(), false)),
        Err(error) => Err(storage_error("read workspace config.toml", &error)),
    }
}

/// Validate the persisted report and apply the `[search]` fusion-weight
/// overlay (ADR 0070 §5). Policy refusals come back as `Ok(Err(refusal))`;
/// storage failures are hard errors.
#[allow(clippy::type_complexity)]
pub fn promote_retrieval_weights(
    workspace_path: &Path,
    connection: &DbConnection,
    workspace_id: &str,
    dry_run: bool,
) -> Result<Result<OverlayChange, PromoteRefusal>, ShadowTuningError> {
    let report_path = shadow_report_path(workspace_path);
    let raw = match std::fs::read_to_string(&report_path) {
        Ok(raw) => raw,
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => {
            return Ok(Err(PromoteRefusal::ReportMissing {
                path: report_path.display().to_string(),
            }));
        }
        Err(error) => return Err(storage_error("read shadow report", &error)),
    };
    let report: serde_json::Value = match serde_json::from_str(&raw) {
        Ok(value) => value,
        Err(error) => {
            return Ok(Err(PromoteRefusal::ReportInvalid {
                reason: format!("not valid JSON: {error}"),
            }));
        }
    };
    if report.get("schema").and_then(serde_json::Value::as_str)
        != Some(RETRIEVAL_TUNING_REPORT_SCHEMA_V1)
    {
        return Ok(Err(PromoteRefusal::ReportInvalid {
            reason: "schema is not ee.shadow.retrieval_tuning_report.v1".to_owned(),
        }));
    }
    if report.get("abstained").and_then(serde_json::Value::as_bool) == Some(true) {
        return Ok(Err(PromoteRefusal::Abstained));
    }
    if report
        .get("promotable")
        .and_then(serde_json::Value::as_bool)
        != Some(true)
    {
        let relative_margin = report
            .pointer("/winner/relativeMargin")
            .and_then(serde_json::Value::as_f64);
        return Ok(Err(PromoteRefusal::NotPromotable { relative_margin }));
    }
    let report_generation = report
        .get("dbGeneration")
        .and_then(serde_json::Value::as_u64)
        .unwrap_or(0);
    let current_generation = connection
        .get_workspace_generation(workspace_id)
        .map_err(|error| storage_error("read workspace generation", &error))?
        .unwrap_or(0);
    if report_generation != current_generation {
        return Ok(Err(PromoteRefusal::StaleGeneration {
            report: report_generation,
            current: current_generation,
        }));
    }
    let Some(weights) = report.pointer("/winner/weights") else {
        return Ok(Err(PromoteRefusal::ReportInvalid {
            reason: "promotable report has no winner weights".to_owned(),
        }));
    };
    let (Some(lexical), Some(semantic), Some(graph)) = (
        weights.get("lexical").and_then(serde_json::Value::as_f64),
        weights.get("semantic").and_then(serde_json::Value::as_f64),
        weights.get("graph").and_then(serde_json::Value::as_f64),
    ) else {
        return Ok(Err(PromoteRefusal::ReportInvalid {
            reason: "winner weights are not numeric".to_owned(),
        }));
    };
    let report_hash = report
        .get("reportHash")
        .and_then(serde_json::Value::as_str)
        .map(str::to_owned);

    let (prior_config, prior_existed) = read_config_bytes(workspace_path)?;
    let mut document = prior_config
        .parse::<toml_edit::DocumentMut>()
        .map_err(|error| storage_error("parse workspace config.toml", &error))?;
    let mut diff = Vec::new();
    {
        let search_item = document.entry("search").or_insert(toml_edit::table());
        let Some(search) = search_item.as_table_like_mut() else {
            return Ok(Err(PromoteRefusal::ReportInvalid {
                reason: "[search] in config.toml is not a table".to_owned(),
            }));
        };
        for (key, value) in [
            ("lexical_weight", lexical),
            ("semantic_weight", semantic),
            ("graph_weight", graph),
        ] {
            let prior = search
                .get(key)
                .and_then(toml_edit::Item::as_value)
                .map(std::string::ToString::to_string);
            diff.push(format!(
                "search.{key}: {} -> {value}",
                prior.as_deref().unwrap_or("(unset)")
            ));
            search.insert(key, toml_edit::value(value));
        }
    }
    let new_config = document.to_string();

    let change = OverlayChange {
        applied: !dry_run,
        prior_config: prior_config.clone(),
        new_config: new_config.clone(),
        diff,
        report_hash: report_hash.clone(),
    };
    if dry_run {
        return Ok(Ok(change));
    }

    std::fs::write(workspace_config_path(workspace_path), &new_config)
        .map_err(|error| storage_error("write workspace config.toml", &error))?;
    let details = serde_json::json!({
        "schema": "ee.shadow.retrieval_weights_promotion.v1",
        "policyId": RETRIEVAL_TUNING_POLICY_ID,
        "reportHash": report_hash,
        "priorExisted": prior_existed,
        "priorConfigToml": prior_config,
        "newValues": { "lexical": lexical, "semantic": semantic, "graph": graph },
    })
    .to_string();
    connection
        .insert_audit(
            &crate::db::generate_audit_id(),
            &crate::db::CreateAuditInput {
                workspace_id: Some(workspace_id.to_owned()),
                actor: None,
                action: PROMOTE_RETRIEVAL_WEIGHTS_AUDIT_ACTION.to_owned(),
                target_type: Some("workspace".to_owned()),
                target_id: Some(workspace_id.to_owned()),
                details: Some(details),
            },
        )
        .map_err(|error| storage_error("record promotion audit", &error))?;
    Ok(Ok(change))
}

/// Restore the prior config from the most recent promotion audit
/// (byte-identical; a previously-absent file restores as empty, never a
/// deletion) and record the demotion.
#[allow(clippy::type_complexity)]
pub fn demote_retrieval_weights(
    workspace_path: &Path,
    connection: &DbConnection,
    workspace_id: &str,
    dry_run: bool,
) -> Result<Result<OverlayChange, PromoteRefusal>, ShadowTuningError> {
    let audits = connection
        .list_audit_by_action(PROMOTE_RETRIEVAL_WEIGHTS_AUDIT_ACTION, None)
        .map_err(|error| storage_error("list promotion audits", &error))?;
    let Some(promotion) = audits
        .iter()
        .find(|entry| entry.workspace_id.as_deref() == Some(workspace_id))
    else {
        return Ok(Err(PromoteRefusal::NoPriorPromotion));
    };
    let details: serde_json::Value = promotion
        .details
        .as_deref()
        .and_then(|raw| serde_json::from_str(raw).ok())
        .unwrap_or(serde_json::Value::Null);
    let Some(prior_config) = details
        .get("priorConfigToml")
        .and_then(serde_json::Value::as_str)
    else {
        return Ok(Err(PromoteRefusal::ReportInvalid {
            reason: "promotion audit carries no priorConfigToml".to_owned(),
        }));
    };
    let (current_config, _) = read_config_bytes(workspace_path)?;
    let change = OverlayChange {
        applied: !dry_run,
        prior_config: current_config,
        new_config: prior_config.to_owned(),
        diff: vec![format!(
            "config.toml restored to pre-promotion bytes ({} bytes)",
            prior_config.len()
        )],
        report_hash: details
            .get("reportHash")
            .and_then(serde_json::Value::as_str)
            .map(str::to_owned),
    };
    if dry_run {
        return Ok(Ok(change));
    }
    std::fs::write(workspace_config_path(workspace_path), prior_config)
        .map_err(|error| storage_error("restore workspace config.toml", &error))?;
    let demote_details = serde_json::json!({
        "schema": "ee.shadow.retrieval_weights_demotion.v1",
        "policyId": RETRIEVAL_TUNING_POLICY_ID,
        "restoredFromAudit": promotion.id,
        "restoredBytes": prior_config.len(),
    })
    .to_string();
    connection
        .insert_audit(
            &crate::db::generate_audit_id(),
            &crate::db::CreateAuditInput {
                workspace_id: Some(workspace_id.to_owned()),
                actor: None,
                action: DEMOTE_RETRIEVAL_WEIGHTS_AUDIT_ACTION.to_owned(),
                target_type: Some("workspace".to_owned()),
                target_id: Some(workspace_id.to_owned()),
                details: Some(demote_details),
            },
        )
        .map_err(|error| storage_error("record demotion audit", &error))?;
    Ok(Ok(change))
}

#[cfg(test)]
mod tests {
    #![allow(clippy::unwrap_used)]

    use super::*;
    use crate::db::{
        CreateAuditInput, CreateFeedbackEventInput, CreateFeedbackQuarantineInput,
        CreatePackRecordInput, CreateWorkspaceInput,
    };
    use asupersync::CancelReason;
    use chrono::TimeZone;

    type TestResult = Result<(), String>;

    const WORKSPACE: &str = "wsp_00000000000000000000000901";

    fn ts(minute: i64) -> DateTime<Utc> {
        Utc.with_ymd_and_hms(2026, 8, 1, 12, 0, 0).unwrap() + Duration::minutes(minute)
    }

    fn event(
        id: &str,
        memory_id: &str,
        created_at: DateTime<Utc>,
        evidence_json: Option<String>,
    ) -> StoredFeedbackEvent {
        StoredFeedbackEvent {
            id: id.to_owned(),
            workspace_id: WORKSPACE.to_owned(),
            target_type: "memory".to_owned(),
            target_id: memory_id.to_owned(),
            signal: "helpful".to_owned(),
            weight: 1.0,
            source_type: "outcome_observed".to_owned(),
            source_id: None,
            reason: None,
            evidence_json,
            session_id: None,
            applied_at: None,
            created_at: created_at.to_rfc3339(),
        }
    }

    fn returned_mem_audit(
        id: &str,
        memory_id: &str,
        timestamp: DateTime<Utc>,
        query_hash: Option<&str>,
    ) -> StoredAuditEntry {
        let details = query_hash.map(|hash| {
            serde_json::json!({
                "queryHash": hash,
                "rank": 1,
                "score": 0.9,
                "source": "semantic",
            })
            .to_string()
        });
        StoredAuditEntry {
            id: id.to_owned(),
            workspace_id: Some(WORKSPACE.to_owned()),
            timestamp: timestamp.to_rfc3339(),
            actor: None,
            action: audit_actions::SEARCH_RETURNED_MEM.to_owned(),
            target_type: Some("memory".to_owned()),
            target_id: Some(memory_id.to_owned()),
            details,
            surface: "search".to_owned(),
            mutation_kind: audit_actions::SEARCH_RETURNED_MEM.to_owned(),
            before_hash: None,
            after_hash: None,
            prev_row_hash: None,
            this_row_hash: None,
        }
    }

    fn pack_item_evidence(pack_id: &str) -> String {
        serde_json::json!({
            "schema": PACK_ITEM_EVIDENCE_SCHEMA_V1,
            "packId": pack_id,
            "itemRank": 2,
        })
        .to_string()
    }

    fn join(
        events: &[StoredFeedbackEvent],
        audits: &[StoredAuditEntry],
        pack_queries: &BTreeMap<String, String>,
        query_text_by_hash: &BTreeMap<String, String>,
        as_of: DateTime<Utc>,
    ) -> Result<LabelExtractionReport, String> {
        let cx = Cx::for_testing();
        join_labeled_triples(
            &cx,
            events,
            audits,
            pack_queries,
            query_text_by_hash,
            &LabelExtractionConfig::default(),
            as_of,
        )
        .map_err(|error| error.to_string())
    }

    fn approx_eq(actual: f64, expected: f64) -> bool {
        (actual - expected).abs() < 1e-9
    }

    #[test]
    fn dense_pack_item_event_yields_weight_one_triple() -> TestResult {
        let created = ts(0);
        let events = [event(
            "fev-1",
            "mem-1",
            created,
            Some(pack_item_evidence("pack-1")),
        )];
        let pack_queries = BTreeMap::from([(
            "pack-1".to_owned(),
            "fix failing release workflow".to_owned(),
        )]);
        let report = join(&events, &[], &pack_queries, &BTreeMap::new(), created)?;

        if report.triples.len() != 1 {
            return Err(format!("expected one dense triple, got {report:?}"));
        }
        let triple = &report.triples[0];
        if triple.query != "fix failing release workflow"
            || triple.memory_id != "mem-1"
            || triple.source != LabelSource::PackItemOutcome
            || triple.pack_record_id.as_deref() != Some("pack-1")
            || triple.audit_row_id.is_some()
        {
            return Err(format!("dense triple fields wrong: {triple:?}"));
        }
        // Zero age at as_of == created_at: no freshness discount.
        if !approx_eq(triple.base_weight, 1.0) || !approx_eq(triple.weight, 1.0) {
            return Err(format!("dense weight wrong: {triple:?}"));
        }
        if report.dense_count != 1 || report.weak_count != 0 || report.distinct_queries != 1 {
            return Err(format!("report counts wrong: {report:?}"));
        }
        Ok(())
    }

    #[test]
    fn dense_linkage_with_missing_pack_record_is_counted_not_guessed() -> TestResult {
        let created = ts(0);
        let events = [event(
            "fev-1",
            "mem-1",
            created,
            Some(pack_item_evidence("pack-gone")),
        )];
        // A matching weak-side audit row exists; the dense-claiming event
        // must NOT fall back to it.
        let query = "weak query";
        let hash = audit_query_hash(query);
        let audits = [returned_mem_audit("aud-1", "mem-1", ts(-5), Some(&hash))];
        let query_text_by_hash = BTreeMap::from([(hash.clone(), query.to_owned())]);

        let report = join(
            &events,
            &audits,
            &BTreeMap::new(),
            &query_text_by_hash,
            created,
        )?;
        if !report.triples.is_empty()
            || report.dense_unresolvable != 1
            || report.weak_unmatched != 0
            || report.weak_unreplayable != 0
        {
            return Err(format!("missing pack record mishandled: {report:?}"));
        }
        Ok(())
    }

    #[test]
    fn weak_event_joins_nearest_preceding_returned_mem_within_window() -> TestResult {
        let created = ts(0);
        let near_query = "near query";
        let far_query = "far query";
        let near_hash = audit_query_hash(near_query);
        let far_hash = audit_query_hash(far_query);
        let events = [event("fev-1", "mem-1", created, None)];
        let audits = [
            returned_mem_audit("aud-far", "mem-1", ts(-25), Some(&far_hash)),
            returned_mem_audit("aud-near", "mem-1", ts(-10), Some(&near_hash)),
        ];
        let query_text_by_hash = BTreeMap::from([
            (near_hash.clone(), near_query.to_owned()),
            (far_hash.clone(), far_query.to_owned()),
        ]);

        let report = join(
            &events,
            &audits,
            &BTreeMap::new(),
            &query_text_by_hash,
            created,
        )?;
        if report.triples.len() != 1 {
            return Err(format!("expected one weak triple, got {report:?}"));
        }
        let triple = &report.triples[0];
        if triple.query != near_query
            || triple.source != LabelSource::SearchWindowAssociation
            || triple.audit_row_id.as_deref() != Some("aud-near")
            || !approx_eq(triple.base_weight, 0.5)
        {
            return Err(format!("weak triple fields wrong: {triple:?}"));
        }
        Ok(())
    }

    #[test]
    fn weak_window_edge_is_inclusive_and_beyond_is_unmatched() -> TestResult {
        let created = ts(0);
        let query = "edge query";
        let hash = audit_query_hash(query);
        let query_text_by_hash = BTreeMap::from([(hash.clone(), query.to_owned())]);

        // Exactly at the 30-minute default window edge: included.
        let events = [event("fev-1", "mem-1", created, None)];
        let at_edge = [returned_mem_audit("aud-1", "mem-1", ts(-30), Some(&hash))];
        let report = join(
            &events,
            &at_edge,
            &BTreeMap::new(),
            &query_text_by_hash,
            created,
        )?;
        if report.weak_count != 1 || report.weak_unmatched != 0 {
            return Err(format!("window edge must be inclusive: {report:?}"));
        }

        // One second beyond the window: unmatched.
        let beyond = [returned_mem_audit(
            "aud-1",
            "mem-1",
            ts(-30) - Duration::seconds(1),
            Some(&hash),
        )];
        let report = join(
            &events,
            &beyond,
            &BTreeMap::new(),
            &query_text_by_hash,
            created,
        )?;
        if report.weak_count != 0 || report.weak_unmatched != 1 {
            return Err(format!("beyond-window audit must not label: {report:?}"));
        }
        Ok(())
    }

    #[test]
    fn search_after_outcome_never_labels() -> TestResult {
        let created = ts(0);
        let query = "later query";
        let hash = audit_query_hash(query);
        let events = [event("fev-1", "mem-1", created, None)];
        let audits = [returned_mem_audit("aud-1", "mem-1", ts(5), Some(&hash))];
        let query_text_by_hash = BTreeMap::from([(hash.clone(), query.to_owned())]);

        let report = join(
            &events,
            &audits,
            &BTreeMap::new(),
            &query_text_by_hash,
            created,
        )?;
        if report.weak_count != 0 || report.weak_unmatched != 1 {
            return Err(format!(
                "an audit row after the outcome cannot have caused it: {report:?}"
            ));
        }
        Ok(())
    }

    #[test]
    fn weak_hash_miss_is_counted_unreplayable_not_guessed() -> TestResult {
        let created = ts(0);
        let events = [event("fev-1", "mem-1", created, None)];
        let audits = [returned_mem_audit(
            "aud-1",
            "mem-1",
            ts(-5),
            Some("blake3:0000000000000000"),
        )];
        let report = join(
            &events,
            &audits,
            &BTreeMap::new(),
            &BTreeMap::new(),
            created,
        )?;
        if !report.triples.is_empty() || report.weak_unreplayable != 1 || report.weak_unmatched != 0
        {
            return Err(format!("hash miss must count unreplayable: {report:?}"));
        }
        Ok(())
    }

    #[test]
    fn freshness_discount_halves_weight_at_ninety_days() -> TestResult {
        let created = ts(0);
        let as_of = created + Duration::days(90);
        let events = [event(
            "fev-1",
            "mem-1",
            created,
            Some(pack_item_evidence("pack-1")),
        )];
        let pack_queries = BTreeMap::from([("pack-1".to_owned(), "aged query".to_owned())]);
        let report = join(&events, &[], &pack_queries, &BTreeMap::new(), as_of)?;
        let triple = &report.triples[0];
        if !approx_eq(triple.age_days, 90.0) || !approx_eq(triple.weight, 0.5) {
            return Err(format!("90-day freshness discount wrong: {triple:?}"));
        }
        Ok(())
    }

    #[test]
    fn non_memory_targets_are_ignored() -> TestResult {
        let created = ts(0);
        let mut pack_event = event(
            "fev-1",
            "pack-1",
            created,
            Some(pack_item_evidence("pack-1")),
        );
        pack_event.target_type = "pack".to_owned();
        let pack_queries = BTreeMap::from([("pack-1".to_owned(), "some query".to_owned())]);
        let report = join(&[pack_event], &[], &pack_queries, &BTreeMap::new(), created)?;
        if report.memory_event_count != 0 || !report.triples.is_empty() {
            return Err(format!("non-memory targets must be ignored: {report:?}"));
        }
        Ok(())
    }

    #[test]
    fn triples_are_sorted_and_hash_is_order_independent() -> TestResult {
        let created = ts(0);
        let pack_queries = BTreeMap::from([
            ("pack-a".to_owned(), "alpha query".to_owned()),
            ("pack-b".to_owned(), "beta query".to_owned()),
        ]);
        let forward = [
            event(
                "fev-1",
                "mem-1",
                created,
                Some(pack_item_evidence("pack-a")),
            ),
            event(
                "fev-2",
                "mem-2",
                created,
                Some(pack_item_evidence("pack-b")),
            ),
        ];
        let reversed = [forward[1].clone(), forward[0].clone()];

        let report_a = join(&forward, &[], &pack_queries, &BTreeMap::new(), created)?;
        let report_b = join(&reversed, &[], &pack_queries, &BTreeMap::new(), created)?;
        if report_a != report_b {
            return Err("label extraction must be input-order independent".to_owned());
        }
        let queries: Vec<&str> = report_a
            .triples
            .iter()
            .map(|triple| triple.query.as_str())
            .collect();
        if queries != ["alpha query", "beta query"] {
            return Err(format!("triples must sort by query: {queries:?}"));
        }
        if !report_a.label_set_hash.starts_with("blake3:") {
            return Err(format!(
                "hash must be prefixed: {}",
                report_a.label_set_hash
            ));
        }

        // A different label set must fingerprint differently.
        let smaller = join(&forward[..1], &[], &pack_queries, &BTreeMap::new(), created)?;
        if smaller.label_set_hash == report_a.label_set_hash {
            return Err("different label sets must not share a fingerprint".to_owned());
        }
        Ok(())
    }

    #[test]
    fn cancelled_cx_aborts_extraction() -> TestResult {
        let cx = Cx::for_testing();
        cx.set_cancel_reason(CancelReason::user("shadow tuning cancellation test"));
        let created = ts(0);
        let events = [event("fev-1", "mem-1", created, None)];
        let outcome = join_labeled_triples(
            &cx,
            &events,
            &[],
            &BTreeMap::new(),
            &BTreeMap::new(),
            &LabelExtractionConfig::default(),
            created,
        );
        match outcome {
            Err(ShadowTuningError::Cancelled(_)) => Ok(()),
            other => Err(format!("cancelled Cx must abort extraction: {other:?}")),
        }
    }

    #[test]
    fn extract_from_database_joins_dense_and_excludes_quarantine() -> TestResult {
        let tempdir = tempfile::tempdir_in("/tmp").map_err(|error| error.to_string())?;
        let database_path = tempdir.path().join("ee.db");
        let connection =
            DbConnection::open_file(&database_path).map_err(|error| error.to_string())?;
        connection.migrate().map_err(|error| error.to_string())?;
        connection
            .insert_workspace(
                WORKSPACE,
                &CreateWorkspaceInput {
                    path: tempdir.path().display().to_string(),
                    name: None,
                },
            )
            .map_err(|error| error.to_string())?;
        connection
            .insert_pack_record(
                "pack_00000000000000000000001001",
                &CreatePackRecordInput {
                    workspace_id: WORKSPACE.to_owned(),
                    query: "prepare release".to_owned(),
                    profile: "balanced".to_owned(),
                    max_tokens: 4000,
                    used_tokens: 0,
                    item_count: 0,
                    omitted_count: 0,
                    pack_hash:
                        "blake3:0000000000000000000000000000000000000000000000000000000000000000"
                            .to_owned(),
                    degraded_json: None,
                    created_by: None,
                },
                &[],
                &[],
            )
            .map_err(|error| error.to_string())?;
        connection
            .insert_feedback_event(
                "fb_00000000000000000000001001",
                &CreateFeedbackEventInput {
                    workspace_id: WORKSPACE.to_owned(),
                    target_type: "memory".to_owned(),
                    target_id: "mem_00000000000000000000001001".to_owned(),
                    signal: "helpful".to_owned(),
                    weight: 1.0,
                    source_type: "outcome_observed".to_owned(),
                    source_id: None,
                    reason: None,
                    evidence_json: Some(pack_item_evidence("pack_00000000000000000000001001")),
                    session_id: None,
                },
            )
            .map_err(|error| error.to_string())?;
        // Quarantined feedback lands in a separate table at record time and
        // must never reach the join.
        connection
            .insert_feedback_quarantine(
                "fq_00000000000000000000001001",
                &CreateFeedbackQuarantineInput {
                    workspace_id: WORKSPACE.to_owned(),
                    source_id: "poisoned-source".to_owned(),
                    target_type: "memory".to_owned(),
                    target_id: "mem_00000000000000000000001001".to_owned(),
                    signal: "harmful".to_owned(),
                    weight: 1.0,
                    source_type: "agent_inference".to_owned(),
                    proposed_event_id: None,
                    recorded_at: Utc::now().to_rfc3339(),
                    reason: "sprt_quarantine".to_owned(),
                    event_reason: None,
                    evidence_json: None,
                    session_id: None,
                    raw_event_hash: "blake3:quarantine-fixture".to_owned(),
                },
            )
            .map_err(|error| error.to_string())?;

        let cx = Cx::for_testing();
        let report = extract_labeled_triples(
            &cx,
            &connection,
            WORKSPACE,
            &LabelExtractionConfig::default(),
            Utc::now() + Duration::minutes(1),
        )
        .map_err(|error| error.to_string())?;

        if report.triples.len() != 1 || report.dense_count != 1 {
            return Err(format!("expected exactly the dense triple: {report:?}"));
        }
        let triple = &report.triples[0];
        if triple.query != "prepare release"
            || triple.feedback_event_id != "fb_00000000000000000000001001"
            || triple.signal != "helpful"
        {
            return Err(format!("db dense triple wrong: {triple:?}"));
        }
        if report.memory_event_count != 1 {
            return Err(format!(
                "quarantined feedback must be invisible to the join: {report:?}"
            ));
        }
        Ok(())
    }

    #[test]
    fn extract_from_database_resolves_weak_query_via_hash_join() -> TestResult {
        let tempdir = tempfile::tempdir_in("/tmp").map_err(|error| error.to_string())?;
        let database_path = tempdir.path().join("ee.db");
        let connection =
            DbConnection::open_file(&database_path).map_err(|error| error.to_string())?;
        connection.migrate().map_err(|error| error.to_string())?;
        connection
            .insert_workspace(
                WORKSPACE,
                &CreateWorkspaceInput {
                    path: tempdir.path().display().to_string(),
                    name: None,
                },
            )
            .map_err(|error| error.to_string())?;
        let query = "hunt flaky mesh test";
        connection
            .insert_pack_record(
                "pack_00000000000000000000001002",
                &CreatePackRecordInput {
                    workspace_id: WORKSPACE.to_owned(),
                    query: query.to_owned(),
                    profile: "balanced".to_owned(),
                    max_tokens: 4000,
                    used_tokens: 0,
                    item_count: 0,
                    omitted_count: 0,
                    pack_hash:
                        "blake3:1111111111111111111111111111111111111111111111111111111111111111"
                            .to_owned(),
                    degraded_json: None,
                    created_by: None,
                },
                &[],
                &[],
            )
            .map_err(|error| error.to_string())?;
        // The audit row precedes the feedback event (insert order), so the
        // outcome falls inside the default label window.
        connection
            .insert_audit(
                "audit_00000000000000000000001001",
                &CreateAuditInput {
                    workspace_id: Some(WORKSPACE.to_owned()),
                    actor: None,
                    action: audit_actions::SEARCH_RETURNED_MEM.to_owned(),
                    target_type: Some("memory".to_owned()),
                    target_id: Some("mem_00000000000000000000001002".to_owned()),
                    details: Some(
                        serde_json::json!({
                            "queryHash": audit_query_hash(query),
                            "rank": 1,
                            "score": 0.8,
                            "source": "lexical",
                        })
                        .to_string(),
                    ),
                },
            )
            .map_err(|error| error.to_string())?;
        connection
            .insert_feedback_event(
                "fb_00000000000000000000001002",
                &CreateFeedbackEventInput {
                    workspace_id: WORKSPACE.to_owned(),
                    target_type: "memory".to_owned(),
                    target_id: "mem_00000000000000000000001002".to_owned(),
                    signal: "helpful".to_owned(),
                    weight: 1.0,
                    source_type: "outcome_observed".to_owned(),
                    source_id: None,
                    reason: None,
                    evidence_json: None,
                    session_id: None,
                },
            )
            .map_err(|error| error.to_string())?;

        let cx = Cx::for_testing();
        let report = extract_labeled_triples(
            &cx,
            &connection,
            WORKSPACE,
            &LabelExtractionConfig::default(),
            Utc::now() + Duration::minutes(1),
        )
        .map_err(|error| error.to_string())?;

        if report.triples.len() != 1 || report.weak_count != 1 {
            return Err(format!("expected exactly the weak triple: {report:?}"));
        }
        let triple = &report.triples[0];
        if triple.query != query
            || triple.source != LabelSource::SearchWindowAssociation
            || triple.audit_row_id.as_deref() != Some("audit_00000000000000000000001001")
            || !approx_eq(triple.base_weight, 0.5)
        {
            return Err(format!("db weak triple wrong: {triple:?}"));
        }
        if report.weak_unreplayable != 0 || report.weak_unmatched != 0 {
            return Err(format!("weak denominators must be clean: {report:?}"));
        }
        Ok(())
    }

    // ===== S2: replay evaluator tests =====

    use crate::core::search::ScoreSource;

    fn hybrid_hit(
        doc_id: &str,
        raw_score: f32,
        lexical: Option<f32>,
        semantic: Option<f32>,
    ) -> SearchHit {
        SearchHit {
            doc_id: doc_id.to_owned(),
            score: raw_score,
            source: ScoreSource::Hybrid,
            fast_score: None,
            quality_score: semantic,
            lexical_score: lexical,
            rerank_score: None,
            metadata: None,
            explanation: None,
        }
    }

    fn triple(query: &str, memory_id: &str, signal: &str, weight: f64) -> LabeledTriple {
        LabeledTriple {
            query: query.to_owned(),
            memory_id: memory_id.to_owned(),
            signal: signal.to_owned(),
            base_weight: weight,
            weight,
            age_days: 0.0,
            source: LabelSource::PackItemOutcome,
            feedback_event_id: format!("fev-{memory_id}"),
            pack_record_id: Some("pack-1".to_owned()),
            audit_row_id: None,
        }
    }

    fn incumbent() -> TuningWeights {
        TuningWeights::compiled_defaults()
    }

    #[test]
    fn evaluator_hand_computed_metric_and_rank_flip_winner() -> TestResult {
        // mem-a is lexical-only and mem-b semantic-only. Neutral upstream RRF
        // ties their rank contributions, so Frankensearch's lexical tiebreak
        // puts mem-a first. The first grid vector (`lexical -0.10`) normalizes
        // to lexical=0.875, semantic=1.125 and flips the order. The single
        // helpful label on mem-b therefore makes that first strict improver
        // the deterministic winner: incumbent = 1/log2(3), winner =
        // 1/log2(2) = 1.0. Descent cannot beat a perfect score.
        let replays = [QueryReplay {
            query: "q1".to_owned(),
            hits: vec![
                hybrid_hit("mem-a", 0.0255, Some(1.0), None),
                hybrid_hit("mem-b", 0.020, None, Some(1.0)),
            ],
        }];
        let labels = [triple("q1", "mem-b", "helpful", 1.0)];
        let cx = Cx::for_testing();
        let evaluation = evaluate_fusion_candidates(&cx, &replays, &labels, incumbent())
            .map_err(|error| error.to_string())?;

        let expected_incumbent = 1.0 / 3.0_f64.log2();
        if !approx_eq(evaluation.incumbent.score, expected_incumbent) {
            return Err(format!(
                "incumbent metric must be 1/log2(3): {evaluation:?}"
            ));
        }
        let Some(winner) = evaluation.winner else {
            return Err(format!("lexical -0.10 vector must win: {evaluation:?}"));
        };
        if !approx_eq(winner.score, 1.0)
            || (f64::from(winner.weights.lexical) - 0.35).abs() > 1e-6
            || (f64::from(winner.weights.semantic) - 0.45).abs() > 1e-6
        {
            return Err(format!(
                "winner must be lexical -0.10 at score 1.0: {winner:?}"
            ));
        }
        let Some(margin) = evaluation.relative_margin else {
            return Err("winner must carry a relative margin".to_owned());
        };
        if margin <= 0.0 {
            return Err(format!("margin must be positive: {margin}"));
        }
        Ok(())
    }

    #[test]
    fn evaluator_is_deterministic_across_runs() -> TestResult {
        let replays = [QueryReplay {
            query: "q1".to_owned(),
            hits: vec![
                hybrid_hit("mem-a", 0.021, Some(1.0), None),
                hybrid_hit("mem-b", 0.020, None, Some(1.0)),
                hybrid_hit("mem-c", 0.015, Some(0.4), Some(0.4)),
            ],
        }];
        let labels = [
            triple("q1", "mem-a", "helpful", 0.8),
            triple("q1", "mem-b", "harmful", 0.5),
            triple("q1", "mem-c", "confirmation", 1.0),
        ];
        let cx = Cx::for_testing();
        let first = evaluate_fusion_candidates(&cx, &replays, &labels, incumbent())
            .map_err(|error| error.to_string())?;
        let second = evaluate_fusion_candidates(&cx, &replays, &labels, incumbent())
            .map_err(|error| error.to_string())?;
        if first != second {
            return Err("evaluation must be byte-identical across runs".to_owned());
        }
        if !first.evaluation_hash.starts_with("blake3:") {
            return Err(format!("hash must be prefixed: {}", first.evaluation_hash));
        }
        // A different label set must fingerprint differently.
        let third = evaluate_fusion_candidates(&cx, &replays, &labels[..1], incumbent())
            .map_err(|error| error.to_string())?;
        if third.evaluation_hash == first.evaluation_hash {
            return Err("different label sets must not share an evaluation hash".to_owned());
        }
        Ok(())
    }

    #[test]
    fn candidate_grid_respects_clamps_and_dedups() -> TestResult {
        // Near-boundary incumbent: +0.05 and +0.10 on lexical both clamp to
        // 0.7 and must collapse to one candidate; graph +0.10 clamps to 0.3;
        // semantic -0.10 clamps to 0.2.
        let near_edge = TuningWeights {
            lexical: 0.65,
            semantic: 0.25,
            graph: 0.25,
        };
        let vectors = enumerate_grid(near_edge);
        for vector in &vectors {
            if vector.lexical < FUSION_LEXICAL_CLAMP.0
                || vector.lexical > FUSION_LEXICAL_CLAMP.1
                || vector.semantic < FUSION_SEMANTIC_CLAMP.0
                || vector.semantic > FUSION_SEMANTIC_CLAMP.1
                || vector.graph < FUSION_GRAPH_CLAMP.0
                || vector.graph > FUSION_GRAPH_CLAMP.1
            {
                return Err(format!("clamp violated: {vector:?}"));
            }
        }
        let mut keys = BTreeSet::new();
        for vector in &vectors {
            if !keys.insert(vector.key()) {
                return Err(format!("duplicate candidate survived dedup: {vector:?}"));
            }
        }
        if vectors != enumerate_grid(near_edge) {
            return Err("grid enumeration must be deterministic".to_owned());
        }
        Ok(())
    }

    #[test]
    fn unmapped_signals_and_zero_gain_queries_are_counted_not_guessed() -> TestResult {
        let replays = [QueryReplay {
            query: "q1".to_owned(),
            hits: vec![hybrid_hit("mem-a", 0.02, Some(1.0), None)],
        }];
        // "stale" is outside the ADR gain mapping; the zero-weight helpful
        // label makes q2's normalizer denominator zero.
        let labels = [
            triple("q1", "mem-a", "stale", 1.0),
            triple("q2", "mem-a", "helpful", 0.0),
        ];
        let cx = Cx::for_testing();
        let evaluation = evaluate_fusion_candidates(&cx, &replays, &labels, incumbent())
            .map_err(|error| error.to_string())?;
        if evaluation.labels_unmapped_signal != 1
            || evaluation.queries_without_gain != 1
            || evaluation.queries_scored != 0
        {
            return Err(format!("honest counters wrong: {evaluation:?}"));
        }
        if !approx_eq(evaluation.incumbent.score, 0.0) || evaluation.winner.is_some() {
            return Err(format!(
                "no usable labels must mean zero scores and no winner: {evaluation:?}"
            ));
        }
        Ok(())
    }

    #[test]
    fn reranked_hits_outrank_fusion_hits_under_every_candidate() -> TestResult {
        let reranked = SearchHit {
            doc_id: "mem-reranked".to_owned(),
            score: 0.9,
            source: ScoreSource::Reranked,
            fast_score: None,
            quality_score: None,
            lexical_score: None,
            rerank_score: Some(0.9),
            metadata: None,
            explanation: None,
        };
        let hits = vec![
            hybrid_hit("mem-a", 0.021, Some(1.0), None),
            reranked,
            hybrid_hit("mem-b", 0.020, None, Some(1.0)),
        ];
        for weights in [
            TuningWeights {
                lexical: 0.7,
                semantic: 0.2,
                graph: 0.0,
            },
            TuningWeights {
                lexical: 0.2,
                semantic: 0.7,
                graph: 0.3,
            },
        ] {
            let ranks = ranked_pool(&hits, weights.to_fusion());
            if ranks.get("mem-reranked") != Some(&1) {
                return Err(format!(
                    "reranked hit must stay rank 1 under {weights:?}: {ranks:?}"
                ));
            }
        }
        Ok(())
    }

    #[test]
    fn cancelled_cx_aborts_evaluation_sweep() -> TestResult {
        let cx = Cx::for_testing();
        cx.set_cancel_reason(CancelReason::user("shadow tuning sweep cancellation test"));
        let replays = [QueryReplay {
            query: "q1".to_owned(),
            hits: vec![hybrid_hit("mem-a", 0.02, Some(1.0), None)],
        }];
        let labels = [triple("q1", "mem-a", "helpful", 1.0)];
        match evaluate_fusion_candidates(&cx, &replays, &labels, incumbent()) {
            Err(ShadowTuningError::Cancelled(_)) => Ok(()),
            other => Err(format!("cancelled Cx must abort the sweep: {other:?}")),
        }
    }

    // ===== S3: evidence gate + report tests =====

    fn flip_fixture_label_report() -> Result<LabelExtractionReport, String> {
        // One dense triple: query "q1", mem-b, helpful, weight 1.0.
        let created = ts(0);
        let events = [event(
            "fev-1",
            "mem-b",
            created,
            Some(pack_item_evidence("pack-1")),
        )];
        let pack_queries = BTreeMap::from([("pack-1".to_owned(), "q1".to_owned())]);
        join(&events, &[], &pack_queries, &BTreeMap::new(), created)
    }

    fn flip_fixture_replays() -> [QueryReplay; 1] {
        [QueryReplay {
            query: "q1".to_owned(),
            hits: vec![
                hybrid_hit("mem-a", 0.0255, Some(1.0), None),
                hybrid_hit("mem-b", 0.020, None, Some(1.0)),
            ],
        }]
    }

    #[test]
    fn evidence_gate_abstains_below_thresholds() -> TestResult {
        let labels = join(&[], &[], &BTreeMap::new(), &BTreeMap::new(), ts(0))?;
        let report = assemble_retrieval_tuning_report(
            labels,
            None,
            7,
            &RetrievalTuningGateConfig::default(),
        )
        .map_err(|error| error.to_string())?;
        if !report.abstained
            || report.abstention_reason != Some(INSUFFICIENT_OUTCOME_EVIDENCE_CODE)
            || report.promotable
            || report.evaluation.is_some()
        {
            return Err(format!("abstention shape wrong: {report:?}"));
        }
        let rendered: serde_json::Value =
            serde_json::from_str(&render_retrieval_tuning_report_json(&report))
                .map_err(|error| error.to_string())?;
        if rendered["schema"] != RETRIEVAL_TUNING_REPORT_SCHEMA_V1
            || rendered["abstained"] != true
            || !rendered["winner"].is_null()
            || rendered["promotable"] != false
            || rendered["dbGeneration"] != 7
        {
            return Err(format!("abstention rendering wrong: {rendered}"));
        }
        Ok(())
    }

    #[test]
    fn gate_pass_produces_promotable_report() -> TestResult {
        let cx = Cx::for_testing();
        let labels = flip_fixture_label_report()?;
        let evaluation =
            evaluate_fusion_candidates(&cx, &flip_fixture_replays(), &labels.triples, incumbent())
                .map_err(|error| error.to_string())?;
        let gate = RetrievalTuningGateConfig {
            min_triples: 1,
            min_queries: 1,
            promote_margin: 0.03,
        };
        let report = assemble_retrieval_tuning_report(labels, Some(evaluation), 3, &gate)
            .map_err(|error| error.to_string())?;
        if report.abstained || !report.promotable || report.abstention_reason.is_some() {
            return Err(format!("gate-pass shape wrong: {report:?}"));
        }
        let rendered: serde_json::Value =
            serde_json::from_str(&render_retrieval_tuning_report_json(&report))
                .map_err(|error| error.to_string())?;
        if rendered["policyId"] != RETRIEVAL_TUNING_POLICY_ID
            || rendered["promotable"] != true
            || rendered["labelSet"]["triples"] != 1
        {
            return Err(format!("gate-pass rendering wrong: {rendered}"));
        }
        let margin = rendered["winner"]["relativeMargin"]
            .as_f64()
            .ok_or("winner must carry relativeMargin")?;
        if margin <= 0.03 {
            return Err(format!("relative margin must clear the gate: {margin}"));
        }
        if rendered["reportHash"].as_str().map(str::to_owned) != Some(report.report_hash.clone()) {
            return Err("rendered reportHash must match the struct".to_owned());
        }
        Ok(())
    }

    #[test]
    fn report_hash_is_deterministic_and_content_bound() -> TestResult {
        let labels_a = join(&[], &[], &BTreeMap::new(), &BTreeMap::new(), ts(0))?;
        let labels_b = join(&[], &[], &BTreeMap::new(), &BTreeMap::new(), ts(0))?;
        let gate = RetrievalTuningGateConfig::default();
        let first = assemble_retrieval_tuning_report(labels_a, None, 1, &gate)
            .map_err(|error| error.to_string())?;
        let second = assemble_retrieval_tuning_report(labels_b.clone(), None, 1, &gate)
            .map_err(|error| error.to_string())?;
        if first.report_hash != second.report_hash {
            return Err("identical reports must share a hash".to_owned());
        }
        let generation_shifted = assemble_retrieval_tuning_report(labels_b, None, 2, &gate)
            .map_err(|error| error.to_string())?;
        if generation_shifted.report_hash == first.report_hash {
            return Err("dbGeneration must be hash-bound".to_owned());
        }
        Ok(())
    }

    // ===== S4: promotion mechanics tests =====

    fn promotable_report_fixture() -> Result<RetrievalTuningReport, String> {
        let labels = flip_fixture_label_report()?;
        let cx = Cx::for_testing();
        let evaluation =
            evaluate_fusion_candidates(&cx, &flip_fixture_replays(), &labels.triples, incumbent())
                .map_err(|error| error.to_string())?;
        let gate = RetrievalTuningGateConfig {
            min_triples: 1,
            min_queries: 1,
            promote_margin: 0.03,
        };
        assemble_retrieval_tuning_report(labels, Some(evaluation), 0, &gate)
            .map_err(|error| error.to_string())
    }

    #[test]
    fn promote_applies_overlay_and_demote_restores_bytes() -> TestResult {
        let tempdir = tempfile::tempdir_in("/tmp").map_err(|error| error.to_string())?;
        let workspace = tempdir.path();
        let store_dir = workspace.join(".ee");
        std::fs::create_dir_all(&store_dir).map_err(|error| error.to_string())?;
        let database_path = store_dir.join("ee.db");
        let connection =
            DbConnection::open_file(&database_path).map_err(|error| error.to_string())?;
        connection.migrate().map_err(|error| error.to_string())?;
        connection
            .insert_workspace(
                WORKSPACE,
                &CreateWorkspaceInput {
                    path: workspace.display().to_string(),
                    name: None,
                },
            )
            .map_err(|error| error.to_string())?;

        let report = promotable_report_fixture()?;
        persist_retrieval_tuning_report(workspace, &report).map_err(|error| error.to_string())?;

        // Dry-run writes nothing.
        let plan = promote_retrieval_weights(workspace, &connection, WORKSPACE, true)
            .map_err(|error| error.to_string())?
            .map_err(|refusal| format!("unexpected refusal: {refusal:?}"))?;
        if plan.applied || workspace.join(".ee").join("config.toml").exists() {
            return Err(format!("dry-run must write nothing: {plan:?}"));
        }

        // Apply: config gains the winner weights; the promotion is audited
        // with the full prior bytes (empty — no config existed).
        let change = promote_retrieval_weights(workspace, &connection, WORKSPACE, false)
            .map_err(|error| error.to_string())?
            .map_err(|refusal| format!("unexpected refusal: {refusal:?}"))?;
        let written = std::fs::read_to_string(workspace.join(".ee").join("config.toml"))
            .map_err(|error| error.to_string())?;
        if !written.contains("lexical_weight") || !written.contains("[search]") {
            return Err(format!("overlay not written: {written}"));
        }
        if !change.prior_config.is_empty() {
            return Err("prior config must be empty for a fresh workspace".to_owned());
        }
        let audits = connection
            .list_audit_by_action(PROMOTE_RETRIEVAL_WEIGHTS_AUDIT_ACTION, None)
            .map_err(|error| error.to_string())?;
        if audits.is_empty() {
            return Err("promotion must be audited".to_owned());
        }

        // Demote: byte-identical restoration of the pre-promotion state.
        let demotion = demote_retrieval_weights(workspace, &connection, WORKSPACE, false)
            .map_err(|error| error.to_string())?
            .map_err(|refusal| format!("unexpected refusal: {refusal:?}"))?;
        let restored = std::fs::read_to_string(workspace.join(".ee").join("config.toml"))
            .map_err(|error| error.to_string())?;
        if !restored.is_empty() || !demotion.new_config.is_empty() {
            return Err(format!(
                "demote must restore the exact prior bytes: {restored:?}"
            ));
        }
        Ok(())
    }

    #[test]
    fn promote_refuses_missing_and_stale_reports() -> TestResult {
        let tempdir = tempfile::tempdir_in("/tmp").map_err(|error| error.to_string())?;
        let workspace = tempdir.path();
        std::fs::create_dir_all(workspace.join(".ee")).map_err(|error| error.to_string())?;
        let connection = DbConnection::open_file(workspace.join(".ee").join("ee.db"))
            .map_err(|error| error.to_string())?;
        connection.migrate().map_err(|error| error.to_string())?;

        match promote_retrieval_weights(workspace, &connection, "ws-missing", false)
            .map_err(|error| error.to_string())?
        {
            Err(PromoteRefusal::ReportMissing { .. }) => {}
            other => return Err(format!("missing report must refuse: {other:?}")),
        }

        // A report evaluated at a different generation is stale.
        let labels = flip_fixture_label_report()?;
        let cx = Cx::for_testing();
        let evaluation =
            evaluate_fusion_candidates(&cx, &flip_fixture_replays(), &labels.triples, incumbent())
                .map_err(|error| error.to_string())?;
        let gate = RetrievalTuningGateConfig {
            min_triples: 1,
            min_queries: 1,
            promote_margin: 0.03,
        };
        let stale = assemble_retrieval_tuning_report(labels, Some(evaluation), 5, &gate)
            .map_err(|error| error.to_string())?;
        persist_retrieval_tuning_report(workspace, &stale).map_err(|error| error.to_string())?;
        match promote_retrieval_weights(workspace, &connection, "ws-missing", false)
            .map_err(|error| error.to_string())?
        {
            Err(PromoteRefusal::StaleGeneration { report: 5, .. }) => Ok(()),
            other => Err(format!("stale report must refuse: {other:?}")),
        }
    }

    #[test]
    fn gate_evaluation_mismatch_is_loud() -> TestResult {
        let cx = Cx::for_testing();
        let labels = flip_fixture_label_report()?;
        let evaluation =
            evaluate_fusion_candidates(&cx, &flip_fixture_replays(), &labels.triples, incumbent())
                .map_err(|error| error.to_string())?;
        // One triple is below the default 50-triple gate, so supplying an
        // evaluation anyway must fail loudly instead of being guessed around.
        match assemble_retrieval_tuning_report(
            labels,
            Some(evaluation),
            1,
            &RetrievalTuningGateConfig::default(),
        ) {
            Err(ShadowTuningError::Storage { .. }) => Ok(()),
            other => Err(format!("gate/evaluation mismatch must be loud: {other:?}")),
        }
    }
}