yantrikdb-server 0.8.13

YantrikDB database server — multi-tenant cognitive memory with wire protocol, HTTP gateway, replication, auto-failover, and at-rest encryption
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
//! HTTP/JSON gateway on port 7438.
//!
//! Thin translation layer: JSON → Command → handler → JSON response.

use std::sync::Arc;

use axum::{
    extract::{Path as AxumPath, Query, State},
    http::{HeaderValue, StatusCode},
    response::IntoResponse,
    routing::{delete, get, post},
    Json, Router,
};
use serde_json::{json, Value};

use crate::auth;
use crate::command::Command;
use crate::handler::{self, CommandResult};
use crate::server::AppState;

type AppResult = Result<Json<Value>, (StatusCode, Json<Value>)>;

/// v0.8.7 (issue #28 follow-up): canonical cluster-state view that
/// prefers openraft when active, falls back to legacy raft-lite.
///
/// Without this helper, every endpoint that exposed cluster state
/// independently chose between the two layers — leading to split-state
/// bugs where /v1/health reports openraft truth while /v1/remember
/// rejects writes citing legacy raft-lite (which has no quorum once
/// openraft is the real write path).
#[derive(Debug, Clone)]
struct ClusterStateView {
    node_id: u64,
    role: String,
    term: u64,
    leader: Option<u64>,
    leader_addr: Option<String>,
    accepts_writes: bool,
    healthy: bool,
    raft_mode: &'static str,

    // PR 6.9 — replication-state visibility.
    //
    // These fields surface the openraft state machine's progress so
    // operators don't have to hit the separate /v1/cluster/raft endpoint
    // to reason about whether a follower is keeping up. All four are
    // additive — clients that don't know about them ignore them.
    //
    // **Honest values today:** through v0.8.13, handlers bypass
    // `MutationCommitter`, so the only entries openraft sees are
    // cluster bookkeeping (membership, init). `last_log_index` will be
    // small and constant; `replication_lag_log_entries` will read 0
    // even on a structurally broken cluster. The fields become
    // load-bearing once PR 6.4 (handler migration) ships at v0.8.13.
    /// Highest log index this node knows about, from openraft metrics.
    /// `None` for raft-lite or single-node deployments.
    last_log_index: Option<u64>,
    /// Highest log index this node's state machine has applied. `None`
    /// when no entries have been applied yet (or raft-lite / single-node).
    last_applied_index: Option<u64>,
    /// `last_log_index.saturating_sub(last_applied_index)` — entries
    /// received but not yet applied. On a healthy follower this stays
    /// near 0; growing means the apply path is stuck. On the leader
    /// this is also 0 (leader applies before commit). `None` only when
    /// neither index is known.
    replication_lag_log_entries: Option<u64>,
    /// Stable label for the local node's role-within-cluster, used
    /// by metric exporters that need a low-cardinality dimension.
    /// `Some("leader" | "follower" | "candidate" | "learner" | "shutdown")`
    /// in openraft mode; `None` otherwise.
    role_label: Option<&'static str>,
}

fn cluster_state_view(state: &AppState) -> Option<ClusterStateView> {
    if let Some(ref assembly) = state.raft {
        let m = assembly.raft.metrics().borrow().clone();
        let is_leader = matches!(m.state, openraft::ServerState::Leader);
        let leader_id = m.current_leader.map(u64::from);
        let leader_addr = leader_id.and_then(|lid| {
            m.membership_config
                .nodes()
                .find(|(id, _)| u64::from(**id) == lid)
                .map(|(_, n)| n.addr.clone())
        });
        let last_log_index = m.last_log_index;
        let last_applied_index = m.last_applied.as_ref().map(|l| l.index);
        // saturating_sub keeps the lag at 0 if the (rare) ordering
        // momentarily reads applied > log_index between updates.
        let replication_lag = match (last_log_index, last_applied_index) {
            (Some(log), Some(applied)) => Some(log.saturating_sub(applied)),
            (Some(log), None) => Some(log),
            (None, _) => None,
        };
        let role_label: &'static str = match m.state {
            openraft::ServerState::Leader => "leader",
            openraft::ServerState::Follower => "follower",
            openraft::ServerState::Candidate => "candidate",
            openraft::ServerState::Learner => "learner",
            openraft::ServerState::Shutdown => "shutdown",
        };
        return Some(ClusterStateView {
            node_id: u64::from(m.id),
            role: format!("{:?}", m.state),
            term: m.current_term,
            leader: leader_id,
            leader_addr,
            accepts_writes: is_leader,
            healthy: m.current_leader.is_some(),
            raft_mode: "openraft",
            last_log_index,
            last_applied_index,
            replication_lag_log_entries: replication_lag,
            role_label: Some(role_label),
        });
    }
    if let Some(ref cluster) = state.cluster {
        return Some(ClusterStateView {
            node_id: cluster.node_id() as u64,
            role: format!("{:?}", cluster.state.leader_role()),
            term: cluster.state.current_term(),
            leader: cluster.state.current_leader().map(|id| id as u64),
            leader_addr: None,
            accepts_writes: cluster.state.accepts_writes(),
            healthy: cluster.is_healthy(),
            raft_mode: "raft-lite",
            last_log_index: None,
            last_applied_index: None,
            replication_lag_log_entries: None,
            role_label: None,
        });
    }
    None
}

/// Shared engine handle. Type alias keeps the complex nested generic out
/// of function signatures and avoids clippy::type_complexity.
type EngineHandle = Arc<yantrikdb::YantrikDB>;

/// Error tuple returned by auth-checking helpers.
type AppError = (StatusCode, Json<Value>);

fn app_error(status: StatusCode, message: impl Into<String>) -> AppError {
    (status, Json(json!({ "error": message.into() })))
}

/// Translate a [`crate::commit::CommitError`] into an HTTP response per
/// RFC 010 PR-6 §9. Centralized so every handler that consumes a
/// committer surfaces consistent status codes + body shapes.
///
/// PR 6.6 ships the translator. PR 6.4 wires handlers to call it as
/// they migrate from `engine.record()` to `submitter.submit()`.
///
/// Status code rationale:
///
/// | Variant | Status | Why |
/// |---|---|---|
/// | `NotLeader` | **307** Temporary Redirect | Standard HTTP clients follow redirects. The body carries `leader_id`/`leader_addr` for clients that don't. PR 6.4 will populate the `Location` response header at the call site (we can't here without restructuring `AppError`). |
/// | `OpIdCollision` | **409** Conflict | Client bug: the same op_id was used with a different mutation. Don't retry. |
/// | `UnexpectedLogIndex` | **409** Conflict | Concurrent write race. Re-read state and retry. |
/// | `Version` | **426** Upgrade Required | Wire-version mismatch in a rolling upgrade. Operator runbook: bring the rest of the cluster to the new version. |
/// | `NotYetImplemented` | **501** Not Implemented | Variant exists in the grammar but the apply path isn't ready (e.g. `PurgeMemory` until RFC 011 PR-3). |
/// | `StorageFailure` | **503** Service Unavailable | Transient SQLite / disk error. Retry. |
/// | `Shutdown` | **503** Service Unavailable | Don't retry on this node. Hit a peer. |
/// | `CommitTimeout` | **503** Service Unavailable | Retry — but reuse the op_id (in the body) so the retry is idempotent. |
fn commit_error_to_app_error(err: crate::commit::CommitError) -> AppError {
    use crate::commit::CommitError as C;
    match err {
        C::NotLeader {
            leader_id,
            leader_addr,
        } => (
            StatusCode::TEMPORARY_REDIRECT,
            Json(json!({
                "error": "not_leader",
                "leader_id": leader_id,
                "leader_addr": leader_addr,
            })),
        ),
        C::OpIdCollision {
            op_id,
            tenant_id,
            existing_index,
        } => (
            StatusCode::CONFLICT,
            Json(json!({
                "error": "op_id_collision",
                "op_id": op_id.to_string(),
                "tenant_id": tenant_id.0,
                "existing_index": existing_index,
            })),
        ),
        C::UnexpectedLogIndex {
            tenant_id,
            expected,
            actual,
        } => (
            StatusCode::CONFLICT,
            Json(json!({
                "error": "unexpected_log_index",
                "tenant_id": tenant_id.0,
                "expected": expected,
                "actual": actual,
            })),
        ),
        C::Version(verr) => (
            StatusCode::UPGRADE_REQUIRED,
            Json(json!({
                "error": "wire_version_mismatch",
                "detail": verr.to_string(),
            })),
        ),
        C::NotYetImplemented {
            variant,
            planned_rfc,
        } => (
            StatusCode::NOT_IMPLEMENTED,
            Json(json!({
                "error": "not_implemented",
                "variant": variant,
                "planned_rfc": planned_rfc,
            })),
        ),
        C::StorageFailure { message } => (
            StatusCode::SERVICE_UNAVAILABLE,
            Json(json!({
                "error": "storage_failure",
                "detail": message,
                "retry_after_ms": 1000,
            })),
        ),
        C::Shutdown => (
            StatusCode::SERVICE_UNAVAILABLE,
            Json(json!({
                "error": "shutting_down",
                "retry_after_ms": 5000,
            })),
        ),
        C::CommitTimeout { op_id } => (
            StatusCode::SERVICE_UNAVAILABLE,
            Json(json!({
                "error": "commit_timeout",
                "op_id": op_id.to_string(),
                "retry_after_ms": 1000,
            })),
        ),
    }
}

/// Extract database engine from Bearer token.
fn resolve_engine(
    state: &AppState,
    auth_header: Option<&str>,
) -> Result<(i64, EngineHandle), AppError> {
    let token = auth_header
        .and_then(|h| h.strip_prefix("Bearer "))
        .ok_or_else(|| app_error(StatusCode::UNAUTHORIZED, "missing Bearer token"))?;

    // Cluster master token check
    if let Some(ref cluster) = state.cluster {
        if let Some(ref secret) = cluster.config.cluster_secret {
            if token == secret.as_str() {
                let control = state.control.lock();
                let db_record = control
                    .get_database("default")
                    .map_err(|e| app_error(StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?
                    .ok_or_else(|| {
                        app_error(StatusCode::NOT_FOUND, "default database not found")
                    })?;
                drop(control);
                let engine = state
                    .pool
                    .get_engine(&db_record)
                    .map_err(|e| app_error(StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
                state.workers.start_for_database(
                    db_record.id,
                    db_record.name.clone(),
                    std::sync::Arc::clone(&engine),
                );
                return Ok((db_record.id, engine));
            }
        }
    }

    let token_hash = auth::hash_token(token);
    let control = state.control.lock();
    let db_id = control
        .validate_token(&token_hash)
        .map_err(|e| app_error(StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?
        .ok_or_else(|| app_error(StatusCode::UNAUTHORIZED, "invalid or revoked token"))?;

    let db_record = control
        .get_database_by_id(db_id)
        .map_err(|e| app_error(StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?
        .ok_or_else(|| app_error(StatusCode::NOT_FOUND, "database not found"))?;
    drop(control);

    let engine = state
        .pool
        .get_engine(&db_record)
        .map_err(|e| app_error(StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;

    // Start background workers if not already running
    state.workers.start_for_database(
        db_id,
        db_record.name.clone(),
        std::sync::Arc::clone(&engine),
    );

    Ok((db_id, engine))
}

/// Execute a command on a blocking thread so a slow engine call (think,
/// consolidate, embed) cannot park a tokio worker. The engine and control
/// mutexes are `parking_lot::Mutex`, which must NEVER be held across an await
/// — running the whole call inside `spawn_blocking` makes that structurally
/// impossible.
///
/// Load shedding: if the inflight count exceeds MAX_INFLIGHT, reject with
/// 503 immediately instead of queuing. Better to fail fast than pile up.
async fn execute_cmd(
    engine: Arc<yantrikdb::YantrikDB>,
    cmd: Command,
    control: Arc<parking_lot::Mutex<crate::control::ControlDb>>,
    inflight: &std::sync::atomic::AtomicU32,
) -> AppResult {
    use std::sync::atomic::Ordering;

    // Load shed: reject if too many ops in flight.
    //
    // The decrement MUST run on every exit path including cancellation.
    // Earlier versions decremented inline after the await, which leaks
    // the counter when axum drops the future on client disconnect /
    // timeout — over hours of traffic the counter saturates at
    // MAX_INFLIGHT and the gate stays permanently closed (v0.8.9 field
    // observation: 256/256 leaked after 12h uptime, container restart
    // was the only recovery). RAII guard fixes it: Drop fires regardless
    // of how the future exits.
    struct InflightGuard<'a>(&'a std::sync::atomic::AtomicU32);
    impl Drop for InflightGuard<'_> {
        fn drop(&mut self) {
            self.0.fetch_sub(1, std::sync::atomic::Ordering::Relaxed);
        }
    }

    let current = inflight.fetch_add(1, Ordering::Relaxed);
    if current >= crate::server::MAX_INFLIGHT {
        inflight.fetch_sub(1, Ordering::Relaxed);
        return Err(app_error(
            StatusCode::SERVICE_UNAVAILABLE,
            format!(
                "server overloaded: {} inflight ops (max {}). Retry later.",
                current,
                crate::server::MAX_INFLIGHT,
            ),
        ));
    }
    let _inflight_guard = InflightGuard(inflight);

    // Extract a static op name from the command for lock-hold telemetry.
    // Strings are matched against `Command` variants; new variants need a
    // new arm here or they appear as "unknown" in metrics.
    let op_name: &'static str = match &cmd {
        Command::Remember { .. } => "remember",
        Command::RememberBatch { .. } => "remember_batch",
        Command::Recall { .. } => "recall",
        Command::Forget { .. } => "forget",
        Command::Stats => "stats",
        Command::Ping => "ping",
        _ => "other",
    };
    let result = tokio::task::spawn_blocking(move || {
        // Measure engine lock acquisition time for /metrics histograms
        let lock_start = std::time::Instant::now();
        let db = engine.as_ref();
        crate::metrics::record_engine_lock_wait(lock_start.elapsed());
        // Measure how long the engine mutex is HELD during the operation.
        // If hold > slow threshold (default 50ms), a warn-level log fires
        // with op name + duration so operators can identify which command
        // is starving concurrent requests.
        let hold_start = std::time::Instant::now();
        let result = handler::execute_with_guard(db, cmd, Some(control.as_ref()));
        crate::metrics::record_engine_lock_hold(op_name, hold_start.elapsed());
        result
    })
    .await
    .map_err(|e| {
        app_error(
            StatusCode::INTERNAL_SERVER_ERROR,
            format!("join error: {e}"),
        )
    });

    // _inflight_guard drops here on every path (success, error, panic
    // unwind from spawn_blocking) — replaces the previous inline
    // fetch_sub that leaked under future cancellation.
    let result = result?;
    match result {
        Ok(CommandResult::Json(v)) => Ok(Json(v)),
        Ok(CommandResult::RecallResults { results, total }) => {
            Ok(Json(json!({ "results": results, "total": total })))
        }
        Ok(CommandResult::Pong) => Ok(Json(json!({ "status": "ok" }))),
        Err(e) => Err(app_error(StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
    }
}

// ── Route handlers ──────────────────────────────────────────────

/// Shallow health check — always returns 200. Use for TCP-level LB probes.
async fn health(State(state): State<Arc<AppState>>) -> Json<Value> {
    let mut payload = json!({
        "status": "ok",
        "engines_loaded": state.pool.loaded_count(),
    });
    if let Some(view) = cluster_state_view(&state) {
        // PR 6.9: payload gains last_log_index, last_applied_index,
        // replication_lag_log_entries, role_label. All additive — clients
        // that don't know about them ignore them. Values today are
        // honest-zero (handlers bypass commit log, so openraft sees only
        // cluster bookkeeping); they become operationally load-bearing
        // once PR 6.4 lands.
        payload["cluster"] = json!({
            "node_id": view.node_id,
            "role": view.role,
            "term": view.term,
            "leader": view.leader,
            "accepts_writes": view.accepts_writes,
            "healthy": view.healthy,
            "raft_mode": view.raft_mode,
            "last_log_index": view.last_log_index,
            "last_applied_index": view.last_applied_index,
            "replication_lag_log_entries": view.replication_lag_log_entries,
            "role_label": view.role_label,
        });
    }
    Json(payload)
}

/// Deep health check — actively probes subsystems. Returns 200 if all
/// checks pass, 503 if any fail. Use for K8s readiness / smart LB probes.
///
/// Checks:
///   1. engine mutex acquirable within 100ms (via try_lock_for)
///   2. control.db responsive to a trivial SELECT
///   3. cluster quorum present (if clustered)
///
/// Each check reports pass/fail + latency in the response body.
async fn health_deep(State(state): State<Arc<AppState>>) -> (StatusCode, Json<Value>) {
    let mut checks = Vec::new();
    let mut all_pass = true;

    // 1. Engine mutex — can we acquire the default engine's lock within 100ms?
    //    A wedged engine would fail this check.
    {
        let engine_check = tokio::task::spawn_blocking({
            let control = state.control.clone();
            let pool = state.pool.clone();
            move || {
                let start = std::time::Instant::now();
                let db_record = {
                    let ctrl = control.lock();
                    ctrl.get_database("default").ok().flatten()
                };
                if let Some(rec) = db_record {
                    if let Ok(engine) = pool.get_engine(&rec) {
                        let timeout = std::time::Duration::from_millis(100);
                        if true
                        /* arc-shared engine always available */
                        {
                            let elapsed = start.elapsed();
                            return json!({
                                "check": "engine_lock",
                                "pass": true,
                                "latency_ms": elapsed.as_secs_f64() * 1000.0,
                            });
                        }
                    }
                }
                let elapsed = start.elapsed();
                json!({
                    "check": "engine_lock",
                    "pass": false,
                    "latency_ms": elapsed.as_secs_f64() * 1000.0,
                    "error": "could not acquire engine lock within 100ms",
                })
            }
        })
        .await
        .unwrap_or_else(|e| json!({"check": "engine_lock", "pass": false, "error": e.to_string()}));

        if !engine_check["pass"].as_bool().unwrap_or(false) {
            all_pass = false;
        }
        checks.push(engine_check);
    }

    // 2. Control DB — trivial SELECT to verify SQLite is responsive
    {
        let control_check = tokio::task::spawn_blocking({
            let control = state.control.clone();
            move || {
                let start = std::time::Instant::now();
                let ctrl = control.lock();
                match ctrl.list_databases() {
                    Ok(dbs) => {
                        let elapsed = start.elapsed();
                        json!({
                            "check": "control_db",
                            "pass": true,
                            "latency_ms": elapsed.as_secs_f64() * 1000.0,
                            "databases": dbs.len(),
                        })
                    }
                    Err(e) => {
                        let elapsed = start.elapsed();
                        json!({
                            "check": "control_db",
                            "pass": false,
                            "latency_ms": elapsed.as_secs_f64() * 1000.0,
                            "error": e.to_string(),
                        })
                    }
                }
            }
        })
        .await
        .unwrap_or_else(|e| json!({"check": "control_db", "pass": false, "error": e.to_string()}));

        if !control_check["pass"].as_bool().unwrap_or(false) {
            all_pass = false;
        }
        checks.push(control_check);
    }

    // 3. Cluster quorum (if clustered) — uses cluster_state_view so
    //    openraft is the source of truth when active.
    if let Some(view) = cluster_state_view(&state) {
        if !view.healthy {
            all_pass = false;
        }
        checks.push(json!({
            "check": "cluster_quorum",
            "pass": view.healthy,
            "node_id": view.node_id,
            "role": view.role,
            "term": view.term,
            "leader": view.leader,
            "raft_mode": view.raft_mode,
        }));
    }

    let status = if all_pass {
        StatusCode::OK
    } else {
        StatusCode::SERVICE_UNAVAILABLE
    };

    // Surface at-rest encryption status. Issue #3 (yantrikos/yantrikdb)
    // reported there was no way to verify encryption was active without
    // running `strings` on the SQLite file. The TenantPool already knows
    // whether a master key was configured at startup.
    let encryption_enabled = state.pool.is_encrypted();
    let encryption_status = if encryption_enabled {
        json!({"enabled": true, "algorithm": "AES-256-GCM"})
    } else {
        json!({"enabled": false, "algorithm": null})
    };

    // RFC 009 admission/observability snapshot — exposed in /health/deep
    // so operators can inspect admission state without scraping /metrics.
    // The CLI (`yantrikdb cluster status`) and yql (`\admission`) both
    // surface these fields.
    let in_flight_used = state.admission.cfg.max_in_flight_recall
        - state.admission.in_flight_recall.available_permits();
    let expanded_used = state.admission.cfg.max_concurrent_expanded_recall
        - state.admission.expanded_recall.available_permits();
    let admission_state = json!({
        "hard_top_k_cap": state.admission.cfg.hard_top_k_cap,
        "max_request_body_bytes": state.admission.cfg.max_request_body_bytes,
        "in_flight_recall": {
            "max": state.admission.cfg.max_in_flight_recall,
            "in_use": in_flight_used,
        },
        "expanded_recall": {
            "max": state.admission.cfg.max_concurrent_expanded_recall,
            "in_use": expanded_used,
        },
    });

    let runtime_state = json!({
        "control_runtime_isolated": state.control_runtime.is_some(),
    });

    // RFC 017-A version snapshot — rolling-upgrade visibility for operators.
    // Cluster gate state is wired when RFC 010 PR-1 attaches a VersionGate
    // to AppState; until then this block surfaces the local build's
    // version primitives only. The shape is forward-compatible: operators
    // can rely on `version.wire`, `version.min_supported_wire`, and
    // `version.table_schema_versions` from RFC 017-A onward.
    let local_snap = crate::version::VersionSnapshot::local();
    let version_block = json!({
        "build_id": local_snap.build_id,
        "wire": local_snap.wire,
        "min_supported_wire": local_snap.min_supported_wire,
        "table_schema_versions": local_snap.table_schema_versions,
    });

    (
        status,
        Json(json!({
            "status": if all_pass { "healthy" } else { "degraded" },
            "encryption": encryption_status,
            "checks": checks,
            "admission": admission_state,
            "runtime": runtime_state,
            "version": version_block,
        })),
    )
}

/// Reject if the tenant would exceed their max_memories quota after adding
/// `count` new memories. Reads quota from control.db and current memory
/// count from the engine's stats.
fn check_memory_quota(
    state: &AppState,
    db_id: i64,
    engine: &EngineHandle,
    count: usize,
) -> Result<(), (StatusCode, Json<Value>)> {
    let quota = {
        let ctrl = state.control.lock();
        ctrl.get_quota(db_id).unwrap_or_default()
    };

    // Quick check via engine stats. v0.8.9: no outer mutex anymore;
    // call directly. stats() may briefly hold internal SQLite read
    // connection but doesn't block recalls (separate connection).
    let current = engine.stats(None).map(|s| s.active_memories).unwrap_or(0);

    if current + count as i64 > quota.max_memories {
        return Err(app_error(
            StatusCode::TOO_MANY_REQUESTS,
            format!(
                "would exceed memory quota: current={}, adding={}, max={}",
                current, count, quota.max_memories,
            ),
        ));
    }
    Ok(())
}

/// Reject if cluster is enabled and this node doesn't accept writes.
/// Uses [`cluster_state_view`] so openraft is the source of truth when active.
fn check_writable(state: &AppState) -> Result<(), (StatusCode, Json<Value>)> {
    let Some(view) = cluster_state_view(state) else {
        return Ok(()); // No cluster — single-node, accepts writes.
    };
    if view.accepts_writes {
        return Ok(());
    }
    let msg = match view.leader {
        Some(id) => format!("read-only: not the leader (current leader: node {})", id),
        None => "read-only: no leader elected".into(),
    };
    Err((
        StatusCode::SERVICE_UNAVAILABLE,
        Json(json!({
            "error": msg,
            "leader_node_id": view.leader,
            "leader_addr": view.leader_addr,
            "raft_mode": view.raft_mode,
        })),
    ))
}

async fn remember(
    State(state): State<Arc<AppState>>,
    headers: axum::http::HeaderMap,
    Json(body): Json<Value>,
) -> AppResult {
    let _timer = crate::metrics::HandlerTimer::new("remember");
    check_writable(&state)?;
    let (db_id, engine) = resolve_engine(
        &state,
        headers.get("authorization").and_then(|v| v.to_str().ok()),
    )?;

    // Quota check: max_memories
    check_memory_quota(&state, db_id, &engine, 1)?;

    let text: String = body["text"]
        .as_str()
        .ok_or_else(|| app_error(StatusCode::BAD_REQUEST, "missing 'text'"))?
        .into();

    // Issue #19: pre-embed in the server before delegating to the
    // engine. If the embedder hiccups, we return 5xx synchronously so
    // the caller can retry — instead of silently storing a row with
    // `embedding=NULL` that poisons subsequent /v1/recall.
    let client_supplied: Option<Vec<f32>> = body.get("embedding").and_then(|v| {
        v.as_array().map(|a| {
            a.iter()
                .filter_map(|x| x.as_f64().map(|f| f as f32))
                .collect()
        })
    });
    let embedding = resolve_embedding(state.as_ref(), &text, client_supplied).await?;

    // RFC 010 PR-6.4: route through commit_log instead of calling
    // engine.record() directly. Single-node mode: LocalSqliteSubmitter
    // applies inline. Cluster mode: RaftCommitter routes through
    // openraft → state machine apply → engine.record_with_rid on every
    // node. RID is allocated server-side (deterministic per-mutation,
    // not per-replica) and carried in the mutation body.
    let rid = uuid7::uuid7().to_string();
    let now_micros = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|d| d.as_micros() as i64)
        .unwrap_or(0);

    let mutation = crate::commit::MemoryMutation::UpsertMemory {
        rid: rid.clone(),
        text,
        memory_type: body
            .get("memory_type")
            .and_then(|v| v.as_str())
            .unwrap_or("semantic")
            .into(),
        importance: body
            .get("importance")
            .and_then(|v| v.as_f64())
            .unwrap_or(0.5),
        valence: body.get("valence").and_then(|v| v.as_f64()).unwrap_or(0.0),
        half_life: body
            .get("half_life")
            .and_then(|v| v.as_f64())
            .unwrap_or(168.0),
        metadata: body.get("metadata").cloned().unwrap_or(json!({})),
        namespace: body
            .get("namespace")
            .and_then(|v| v.as_str())
            .unwrap_or("")
            .into(),
        certainty: body
            .get("certainty")
            .and_then(|v| v.as_f64())
            .unwrap_or(1.0),
        domain: body
            .get("domain")
            .and_then(|v| v.as_str())
            .unwrap_or("")
            .into(),
        source: body
            .get("source")
            .and_then(|v| v.as_str())
            .unwrap_or("user")
            .into(),
        emotional_state: body
            .get("emotional_state")
            .and_then(|v| v.as_str())
            .map(String::from),
        embedding,
        extracted_entities: vec![],
        created_at_unix_micros: Some(now_micros),
        embedding_model: Some("default".into()),
    };

    let receipt = state
        .commit_log
        .commit(
            crate::commit::TenantId::new(db_id),
            mutation,
            crate::commit::CommitOptions::default(),
        )
        .await
        .map_err(commit_error_to_app_error)?;

    let _ = engine; // engine handle is held only for quota check
    Ok(Json(json!({
        "rid": rid,
        "log_index": receipt.log_index,
    })))
}

/// Issue #19 helper: resolve the embedding for a `/v1/remember`-style
/// payload, failing fast if the server's built-in embedder hiccups.
///
/// Decision tree:
/// 1. Caller supplied `embedding` in the request body — use it as-is.
///    The client takes responsibility for its own embedding pipeline.
/// 2. Server has a configured embedder — call it via `spawn_blocking`
///    (the underlying ONNX model holds a `parking_lot::Mutex` that we
///    must not hold across an `await`). Cache hits return in
///    microseconds; misses pay the model cost.
/// 3. No embedder configured — return `None` and let the engine path
///    decide what to do (typically rejects the write or stores NULL,
///    depending on engine config; out of this fix's scope).
///
/// Failure mode: if the server embedder returns an error, return
/// `Err((500, ...))` immediately. The caller sees a clear failure
/// signal instead of a deceptive `200 {rid: ...}` for a row that's
/// actually broken.
async fn resolve_embedding(
    state: &AppState,
    text: &str,
    client_supplied: Option<Vec<f32>>,
) -> Result<Option<Vec<f32>>, AppError> {
    if client_supplied.is_some() {
        return Ok(client_supplied);
    }
    let Some(embedder) = state.pool.embedder().cloned() else {
        return Ok(None);
    };
    let owned_text = text.to_string();
    let result = tokio::task::spawn_blocking(move || {
        use yantrikdb::types::Embedder;
        embedder.embed(&owned_text)
    })
    .await
    .map_err(|join_err| {
        app_error(
            StatusCode::INTERNAL_SERVER_ERROR,
            format!("embed task panicked: {}", join_err),
        )
    })?;
    match result {
        Ok(v) => Ok(Some(v)),
        Err(e) => {
            crate::metrics::increment_embedder_failure("remember");
            tracing::error!(
                error = %e,
                text_len = text.len(),
                "embedder failed during /v1/remember; refusing to write a row with NULL embedding"
            );
            Err(app_error(
                StatusCode::INTERNAL_SERVER_ERROR,
                format!(
                    "embedder failed: {} (issue #19 — write refused to prevent NULL-embedding row that would poison recall on this namespace; please retry)",
                    e
                ),
            ))
        }
    }
}

async fn remember_batch(
    State(state): State<Arc<AppState>>,
    headers: axum::http::HeaderMap,
    Json(body): Json<Value>,
) -> AppResult {
    let _timer = crate::metrics::HandlerTimer::new("remember_batch");
    check_writable(&state)?;
    let (db_id, engine) = resolve_engine(
        &state,
        headers.get("authorization").and_then(|v| v.to_str().ok()),
    )?;

    let memories_arr = body
        .get("memories")
        .and_then(|v| v.as_array())
        .ok_or_else(|| app_error(StatusCode::BAD_REQUEST, "missing 'memories' array"))?;

    if memories_arr.is_empty() {
        return Ok(Json(json!({"rids": [], "count": 0})));
    }

    // Quota checks: batch size + total memory count
    let quota = {
        let ctrl = state.control.lock();
        ctrl.get_quota(db_id).unwrap_or_default()
    };

    if memories_arr.len() > quota.max_batch_size as usize {
        return Err(app_error(
            StatusCode::TOO_MANY_REQUESTS,
            format!(
                "batch size {} exceeds quota {} for this database",
                memories_arr.len(),
                quota.max_batch_size
            ),
        ));
    }

    check_memory_quota(&state, db_id, &engine, memories_arr.len())?;

    if memories_arr.len() > 10_000 {
        return Err(app_error(
            StatusCode::BAD_REQUEST,
            "batch size exceeds 10000",
        ));
    }

    let mut memories = Vec::with_capacity(memories_arr.len());
    for (i, m) in memories_arr.iter().enumerate() {
        let text = m
            .get("text")
            .and_then(|v| v.as_str())
            .ok_or_else(|| {
                app_error(
                    StatusCode::BAD_REQUEST,
                    format!("memories[{}]: missing 'text'", i),
                )
            })?
            .to_string();
        memories.push(crate::command::RememberInput {
            text,
            memory_type: m
                .get("memory_type")
                .and_then(|v| v.as_str())
                .unwrap_or("semantic")
                .into(),
            importance: m.get("importance").and_then(|v| v.as_f64()).unwrap_or(0.5),
            valence: m.get("valence").and_then(|v| v.as_f64()).unwrap_or(0.0),
            half_life: m.get("half_life").and_then(|v| v.as_f64()).unwrap_or(168.0),
            metadata: m.get("metadata").cloned().unwrap_or(json!({})),
            namespace: m
                .get("namespace")
                .and_then(|v| v.as_str())
                .unwrap_or("")
                .into(),
            certainty: m.get("certainty").and_then(|v| v.as_f64()).unwrap_or(1.0),
            domain: m
                .get("domain")
                .and_then(|v| v.as_str())
                .unwrap_or("")
                .into(),
            source: m
                .get("source")
                .and_then(|v| v.as_str())
                .unwrap_or("user")
                .into(),
            emotional_state: m
                .get("emotional_state")
                .and_then(|v| v.as_str())
                .map(String::from),
            embedding: m.get("embedding").and_then(|v| {
                v.as_array().map(|a| {
                    a.iter()
                        .filter_map(|x| x.as_f64().map(|f| f as f32))
                        .collect()
                })
            }),
        });
    }

    // Issue #19: pre-embed any memory that didn't ship with a
    // client-supplied embedding. Batch the misses through one
    // `embed_batch` call so concurrent ONNX-mutex acquisitions are
    // coalesced (the embedder cache + batch path was wired in
    // commit `e52228e`). On embedder failure we return 5xx and the
    // entire batch is rejected — partial success would land
    // NULL-embedding rows in the engine and re-create the bug.
    if let Some(embedder) = state.pool.embedder().cloned() {
        let needs_embed: Vec<usize> = memories
            .iter()
            .enumerate()
            .filter(|(_, m)| m.embedding.is_none())
            .map(|(i, _)| i)
            .collect();
        if !needs_embed.is_empty() {
            let texts: Vec<String> = needs_embed
                .iter()
                .map(|&i| memories[i].text.clone())
                .collect();
            let result = tokio::task::spawn_blocking(move || {
                use yantrikdb::types::Embedder;
                let refs: Vec<&str> = texts.iter().map(|s| s.as_str()).collect();
                embedder.embed_batch(&refs)
            })
            .await
            .map_err(|e| {
                app_error(
                    StatusCode::INTERNAL_SERVER_ERROR,
                    format!("embed batch task panicked: {}", e),
                )
            })?;
            match result {
                Ok(embeddings) => {
                    for (idx, vec) in needs_embed.iter().zip(embeddings.into_iter()) {
                        memories[*idx].embedding = Some(vec);
                    }
                }
                Err(e) => {
                    crate::metrics::increment_embedder_failure("remember_batch");
                    tracing::error!(
                        error = %e,
                        miss_count = needs_embed.len(),
                        batch_size = memories.len(),
                        "embedder failed during /v1/remember/batch; refusing partial-NULL write"
                    );
                    return Err(app_error(
                        StatusCode::INTERNAL_SERVER_ERROR,
                        format!(
                            "embedder failed: {} (issue #19 — batch refused to prevent NULL-embedding rows; please retry)",
                            e
                        ),
                    ));
                }
            }
        }
    }

    // RFC 010 PR-6.4: route every batch entry through commit_log. Each
    // entry gets its own (rid, op_id, log_index). On cluster mode this
    // is N round-trips through openraft; on single-node it's N inline
    // applier dispatches. Both paths preserve byte-determinism across
    // replicas.
    let now_micros = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|d| d.as_micros() as i64)
        .unwrap_or(0);
    let mut rids = Vec::with_capacity(memories.len());
    let mut last_log_index: u64 = 0;
    for m in memories {
        let rid = uuid7::uuid7().to_string();
        let mutation = crate::commit::MemoryMutation::UpsertMemory {
            rid: rid.clone(),
            text: m.text,
            memory_type: m.memory_type,
            importance: m.importance,
            valence: m.valence,
            half_life: m.half_life,
            metadata: m.metadata,
            namespace: m.namespace,
            certainty: m.certainty,
            domain: m.domain,
            source: m.source,
            emotional_state: m.emotional_state,
            embedding: m.embedding,
            extracted_entities: vec![],
            created_at_unix_micros: Some(now_micros),
            embedding_model: Some("default".into()),
        };
        let receipt = state
            .commit_log
            .commit(
                crate::commit::TenantId::new(db_id),
                mutation,
                crate::commit::CommitOptions::default(),
            )
            .await
            .map_err(commit_error_to_app_error)?;
        rids.push(rid);
        last_log_index = receipt.log_index;
    }
    let _ = engine; // engine handle held only for the quota check above
    let count = rids.len();
    Ok(Json(json!({
        "rids": rids,
        "count": count,
        "log_index": last_log_index,
    })))
}

async fn recall(
    State(state): State<Arc<AppState>>,
    headers: axum::http::HeaderMap,
    Json(body): Json<Value>,
) -> AppResult {
    let _timer = crate::metrics::HandlerTimer::new("recall");

    // Parse the two admission-relevant fields up front so we can reject
    // BEFORE auth or HNSW search runs. Order matters: bad requests should
    // burn the smallest possible amount of CPU.
    let top_k = body.get("top_k").and_then(|v| v.as_u64()).unwrap_or(10) as usize;
    // v1 default for `expand_entities` stays `true` (backwards-compat —
    // see RFC 009 §"Backwards-compat contract"). The /v2 endpoints
    // shipped in PR-6 will flip the default to `false`.
    let expand_entities = body
        .get("expand_entities")
        .and_then(|v| v.as_bool())
        .unwrap_or(true);

    // Observability: count + histogram every recall request, regardless
    // of fate. Dashboards key on these for traffic shape analysis.
    crate::metrics::record_recall_request("v1", expand_entities);
    crate::metrics::record_recall_top_k("v1", top_k);

    // Hard cap — RFC 009 §4 Layer 3. Reject before HNSW search so a
    // misconfigured client requesting top_k=10000 gets a 400 in
    // microseconds instead of saturating a voter for seconds.
    if let Err(reason) = crate::admission::check_top_k(top_k, state.admission.cfg.hard_top_k_cap) {
        let status = StatusCode::from_u16(reason.http_status()).unwrap_or(StatusCode::BAD_REQUEST);
        return Err((
            status,
            Json(json!({
                "error": reason.message(),
                "reason": reason.metric_label(),
                "hard_top_k_cap": state.admission.cfg.hard_top_k_cap,
            })),
        ));
    }

    // Acquire admission permits BEFORE auth/engine resolution. Rejecting
    // on capacity is cheaper than resolving the tenant for a request
    // we're going to reject anyway. RAII: permits drop on function exit.
    let _permits = match state
        .admission
        .acquire_recall_permits(expand_entities)
        .await
    {
        Ok(p) => p,
        Err(reason) => {
            let status = StatusCode::from_u16(reason.http_status())
                .unwrap_or(StatusCode::SERVICE_UNAVAILABLE);
            return Err((
                status,
                Json(json!({
                    "error": reason.message(),
                    "reason": reason.metric_label(),
                    "retry_after_ms": 200,
                })),
            ));
        }
    };

    let (_, engine) = resolve_engine(
        &state,
        headers.get("authorization").and_then(|v| v.to_str().ok()),
    )?;
    let cmd = Command::Recall {
        query: body["query"]
            .as_str()
            .ok_or_else(|| app_error(StatusCode::BAD_REQUEST, "missing 'query'"))?
            .into(),
        top_k,
        memory_type: body
            .get("memory_type")
            .and_then(|v| v.as_str())
            .map(String::from),
        include_consolidated: body
            .get("include_consolidated")
            .and_then(|v| v.as_bool())
            .unwrap_or(false),
        expand_entities,
        namespace: body
            .get("namespace")
            .and_then(|v| v.as_str())
            .map(String::from),
        domain: body
            .get("domain")
            .and_then(|v| v.as_str())
            .map(String::from),
        source: body
            .get("source")
            .and_then(|v| v.as_str())
            .map(String::from),
        query_embedding: body.get("query_embedding").and_then(|v| {
            v.as_array().map(|a| {
                a.iter()
                    .filter_map(|x| x.as_f64().map(|f| f as f32))
                    .collect()
            })
        }),
    };
    execute_cmd(engine, cmd, state.control.clone(), &state.inflight).await
}

async fn forget(
    State(state): State<Arc<AppState>>,
    headers: axum::http::HeaderMap,
    Json(body): Json<Value>,
) -> AppResult {
    let _timer = crate::metrics::HandlerTimer::new("forget");
    check_writable(&state)?;
    let (db_id, _engine) = resolve_engine(
        &state,
        headers.get("authorization").and_then(|v| v.to_str().ok()),
    )?;
    let rid: String = body["rid"]
        .as_str()
        .ok_or_else(|| app_error(StatusCode::BAD_REQUEST, "missing 'rid'"))?
        .into();
    let namespace: String = body
        .get("namespace")
        .and_then(|v| v.as_str())
        .unwrap_or("")
        .into();
    let reason = body
        .get("reason")
        .and_then(|v| v.as_str())
        .map(String::from);
    let now_micros = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|d| d.as_micros() as i64)
        .unwrap_or(0);

    // RFC 010 PR-6.4: route TombstoneMemory through commit_log so the
    // mutation replicates to followers via openraft (cluster) or applies
    // inline via LocalSqliteSubmitter (single-node). Engine state is
    // updated by the applier dispatch (engine.tombstone_with_rid).
    let mutation = crate::commit::MemoryMutation::TombstoneMemory {
        rid: rid.clone(),
        reason,
        requested_at_unix_micros: now_micros,
        namespace,
    };

    let receipt = state
        .commit_log
        .commit(
            crate::commit::TenantId::new(db_id),
            mutation,
            crate::commit::CommitOptions::default(),
        )
        .await
        .map_err(commit_error_to_app_error)?;

    Ok(Json(json!({
        "rid": rid,
        "found": true,
        "log_index": receipt.log_index,
    })))
}

async fn relate(
    State(state): State<Arc<AppState>>,
    headers: axum::http::HeaderMap,
    Json(body): Json<Value>,
) -> Result<impl IntoResponse, AppError> {
    let _timer = crate::metrics::HandlerTimer::new("relate");
    check_writable(&state)?;
    let (db_id, _engine) = resolve_engine(
        &state,
        headers.get("authorization").and_then(|v| v.to_str().ok()),
    )?;
    let entity: String = body["entity"]
        .as_str()
        .ok_or_else(|| app_error(StatusCode::BAD_REQUEST, "missing 'entity'"))?
        .into();
    let target: String = body["target"]
        .as_str()
        .ok_or_else(|| app_error(StatusCode::BAD_REQUEST, "missing 'target'"))?
        .into();
    let rel_type: String = body["relationship"]
        .as_str()
        .ok_or_else(|| app_error(StatusCode::BAD_REQUEST, "missing 'relationship'"))?
        .into();
    let weight = body.get("weight").and_then(|v| v.as_f64()).unwrap_or(1.0);
    let namespace: String = body
        .get("namespace")
        .and_then(|v| v.as_str())
        .unwrap_or("")
        .into();

    // RFC 010 PR-6.4: route UpsertEntityEdge through commit_log. Edge id
    // allocated server-side as UUIDv7, carried in the mutation so every
    // replica produces byte-identical edge state.
    let edge_id = uuid7::uuid7().to_string();
    let mutation = crate::commit::MemoryMutation::UpsertEntityEdge {
        edge_id: edge_id.clone(),
        src: entity,
        dst: target,
        rel_type,
        weight,
        namespace,
    };

    let receipt = state
        .commit_log
        .commit(
            crate::commit::TenantId::new(db_id),
            mutation,
            crate::commit::CommitOptions::default(),
        )
        .await
        .map_err(commit_error_to_app_error)?;

    let mut response = Json(json!({
        "edge_id": edge_id,
        "log_index": receipt.log_index,
    }))
    .into_response();
    response
        .headers_mut()
        .insert("deprecation", HeaderValue::from_static("true"));
    response.headers_mut().insert(
        "link",
        HeaderValue::from_static(r#"</v1/claim>; rel="successor-version""#),
    );
    Ok(response)
}

async fn ingest_claim(
    State(state): State<Arc<AppState>>,
    headers: axum::http::HeaderMap,
    Json(body): Json<Value>,
) -> AppResult {
    let _timer = crate::metrics::HandlerTimer::new("ingest_claim");
    check_writable(&state)?;
    let (_, engine) = resolve_engine(
        &state,
        headers.get("authorization").and_then(|v| v.to_str().ok()),
    )?;
    let cmd = Command::IngestClaim {
        src: body["src"]
            .as_str()
            .ok_or_else(|| app_error(StatusCode::BAD_REQUEST, "missing 'src'"))?
            .into(),
        rel_type: body["rel_type"]
            .as_str()
            .ok_or_else(|| app_error(StatusCode::BAD_REQUEST, "missing 'rel_type'"))?
            .into(),
        dst: body["dst"]
            .as_str()
            .ok_or_else(|| app_error(StatusCode::BAD_REQUEST, "missing 'dst'"))?
            .into(),
        namespace: body
            .get("namespace")
            .and_then(|v| v.as_str())
            .unwrap_or("default")
            .into(),
        polarity: body.get("polarity").and_then(|v| v.as_i64()).unwrap_or(1) as i32,
        modality: body
            .get("modality")
            .and_then(|v| v.as_str())
            .unwrap_or("asserted")
            .into(),
        valid_from: body.get("valid_from").and_then(|v| v.as_f64()),
        valid_to: body.get("valid_to").and_then(|v| v.as_f64()),
        extractor: body
            .get("extractor")
            .and_then(|v| v.as_str())
            .unwrap_or("manual")
            .into(),
        extractor_version: body
            .get("extractor_version")
            .and_then(|v| v.as_str())
            .map(|s| s.to_string()),
        confidence_band: body
            .get("confidence_band")
            .and_then(|v| v.as_str())
            .unwrap_or("medium")
            .into(),
        source_memory_rid: body
            .get("source_memory_rid")
            .and_then(|v| v.as_str())
            .map(|s| s.to_string()),
        span_start: body
            .get("span_start")
            .and_then(|v| v.as_i64())
            .map(|v| v as i32),
        span_end: body
            .get("span_end")
            .and_then(|v| v.as_i64())
            .map(|v| v as i32),
        weight: body.get("weight").and_then(|v| v.as_f64()).unwrap_or(1.0),
    };
    execute_cmd(engine, cmd, state.control.clone(), &state.inflight).await
}

async fn add_alias(
    State(state): State<Arc<AppState>>,
    headers: axum::http::HeaderMap,
    Json(body): Json<Value>,
) -> AppResult {
    let _timer = crate::metrics::HandlerTimer::new("add_alias");
    check_writable(&state)?;
    let (_, engine) = resolve_engine(
        &state,
        headers.get("authorization").and_then(|v| v.to_str().ok()),
    )?;
    let cmd = Command::AddAlias {
        alias: body["alias"]
            .as_str()
            .ok_or_else(|| app_error(StatusCode::BAD_REQUEST, "missing 'alias'"))?
            .into(),
        canonical_name: body["canonical_name"]
            .as_str()
            .ok_or_else(|| app_error(StatusCode::BAD_REQUEST, "missing 'canonical_name'"))?
            .into(),
        namespace: body
            .get("namespace")
            .and_then(|v| v.as_str())
            .unwrap_or("default")
            .into(),
        source: body
            .get("source")
            .and_then(|v| v.as_str())
            .unwrap_or("explicit")
            .into(),
    };
    execute_cmd(engine, cmd, state.control.clone(), &state.inflight).await
}

async fn get_claims(
    State(state): State<Arc<AppState>>,
    headers: axum::http::HeaderMap,
    Query(params): Query<std::collections::HashMap<String, String>>,
) -> AppResult {
    let _timer = crate::metrics::HandlerTimer::new("get_claims");
    let (_, engine) = resolve_engine(
        &state,
        headers.get("authorization").and_then(|v| v.to_str().ok()),
    )?;
    let entity = params
        .get("entity")
        .cloned()
        .ok_or_else(|| app_error(StatusCode::BAD_REQUEST, "missing 'entity' query parameter"))?;
    let namespace = params.get("namespace").cloned();
    execute_cmd(
        engine,
        Command::GetClaims { entity, namespace },
        state.control.clone(),
        &state.inflight,
    )
    .await
}

async fn think(
    State(state): State<Arc<AppState>>,
    headers: axum::http::HeaderMap,
    Json(body): Json<Value>,
) -> AppResult {
    let _timer = crate::metrics::HandlerTimer::new("think");
    check_writable(&state)?;
    let (_, engine) = resolve_engine(
        &state,
        headers.get("authorization").and_then(|v| v.to_str().ok()),
    )?;
    let cmd = Command::Think {
        run_consolidation: body
            .get("run_consolidation")
            .and_then(|v| v.as_bool())
            .unwrap_or(true),
        run_conflict_scan: body
            .get("run_conflict_scan")
            .and_then(|v| v.as_bool())
            .unwrap_or(true),
        run_pattern_mining: body
            .get("run_pattern_mining")
            .and_then(|v| v.as_bool())
            .unwrap_or(false),
        run_personality: body
            .get("run_personality")
            .and_then(|v| v.as_bool())
            .unwrap_or(true),
        consolidation_limit: body
            .get("consolidation_limit")
            .and_then(|v| v.as_u64())
            .unwrap_or(50) as usize,
    };
    execute_cmd(engine, cmd, state.control.clone(), &state.inflight).await
}

async fn conflicts(
    State(state): State<Arc<AppState>>,
    headers: axum::http::HeaderMap,
    Query(params): Query<std::collections::HashMap<String, String>>,
) -> AppResult {
    let _timer = crate::metrics::HandlerTimer::new("conflicts");
    let (_, engine) = resolve_engine(
        &state,
        headers.get("authorization").and_then(|v| v.to_str().ok()),
    )?;
    let cmd = Command::Conflicts {
        status: params.get("status").cloned(),
        conflict_type: params.get("conflict_type").cloned(),
        entity: params.get("entity").cloned(),
        namespace: params.get("namespace").cloned(),
        limit: params
            .get("limit")
            .and_then(|v| v.parse().ok())
            .unwrap_or(50),
    };
    execute_cmd(engine, cmd, state.control.clone(), &state.inflight).await
}

async fn resolve_conflict(
    State(state): State<Arc<AppState>>,
    headers: axum::http::HeaderMap,
    AxumPath(conflict_id): AxumPath<String>,
    Json(body): Json<Value>,
) -> AppResult {
    let _timer = crate::metrics::HandlerTimer::new("resolve_conflict");
    check_writable(&state)?;
    let (_, engine) = resolve_engine(
        &state,
        headers.get("authorization").and_then(|v| v.to_str().ok()),
    )?;
    let cmd = Command::Resolve {
        conflict_id,
        strategy: body["strategy"]
            .as_str()
            .ok_or_else(|| app_error(StatusCode::BAD_REQUEST, "missing 'strategy'"))?
            .into(),
        winner_rid: body
            .get("winner_rid")
            .and_then(|v| v.as_str())
            .map(String::from),
        new_text: body
            .get("new_text")
            .and_then(|v| v.as_str())
            .map(String::from),
        resolution_note: body
            .get("resolution_note")
            .and_then(|v| v.as_str())
            .map(String::from),
    };
    execute_cmd(engine, cmd, state.control.clone(), &state.inflight).await
}

async fn session_start(
    State(state): State<Arc<AppState>>,
    headers: axum::http::HeaderMap,
    Json(body): Json<Value>,
) -> AppResult {
    check_writable(&state)?;
    let (_, engine) = resolve_engine(
        &state,
        headers.get("authorization").and_then(|v| v.to_str().ok()),
    )?;
    let cmd = Command::SessionStart {
        namespace: body
            .get("namespace")
            .and_then(|v| v.as_str())
            .unwrap_or("default")
            .into(),
        client_id: body
            .get("client_id")
            .and_then(|v| v.as_str())
            .unwrap_or("")
            .into(),
        metadata: body.get("metadata").cloned().unwrap_or(json!({})),
    };
    execute_cmd(engine, cmd, state.control.clone(), &state.inflight).await
}

async fn session_end(
    State(state): State<Arc<AppState>>,
    headers: axum::http::HeaderMap,
    AxumPath(session_id): AxumPath<String>,
    body: Option<Json<Value>>,
) -> AppResult {
    check_writable(&state)?;
    let (_, engine) = resolve_engine(
        &state,
        headers.get("authorization").and_then(|v| v.to_str().ok()),
    )?;
    let summary =
        body.and_then(|Json(b)| b.get("summary").and_then(|v| v.as_str()).map(String::from));
    let cmd = Command::SessionEnd {
        session_id,
        summary,
    };
    execute_cmd(engine, cmd, state.control.clone(), &state.inflight).await
}

async fn personality(
    State(state): State<Arc<AppState>>,
    headers: axum::http::HeaderMap,
) -> AppResult {
    let _timer = crate::metrics::HandlerTimer::new("personality");
    let (_, engine) = resolve_engine(
        &state,
        headers.get("authorization").and_then(|v| v.to_str().ok()),
    )?;
    execute_cmd(
        engine,
        Command::Personality,
        state.control.clone(),
        &state.inflight,
    )
    .await
}

async fn stats(State(state): State<Arc<AppState>>, headers: axum::http::HeaderMap) -> AppResult {
    let _timer = crate::metrics::HandlerTimer::new("stats");
    let (_, engine) = resolve_engine(
        &state,
        headers.get("authorization").and_then(|v| v.to_str().ok()),
    )?;
    execute_cmd(
        engine,
        Command::Stats,
        state.control.clone(),
        &state.inflight,
    )
    .await
}

async fn create_database(
    State(state): State<Arc<AppState>>,
    headers: axum::http::HeaderMap,
    Json(body): Json<Value>,
) -> AppResult {
    check_writable(&state)?;
    // For now, any valid token can create databases
    let _ = resolve_engine(
        &state,
        headers.get("authorization").and_then(|v| v.to_str().ok()),
    )?;
    let name: String = body["name"]
        .as_str()
        .ok_or_else(|| app_error(StatusCode::BAD_REQUEST, "missing 'name'"))?
        .to_string();

    // Create directly via control (no engine needed)
    let control = state.control.lock();
    if control
        .database_exists(&name)
        .map_err(|e| app_error(StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?
    {
        return Err(app_error(
            StatusCode::CONFLICT,
            format!("database '{}' already exists", name),
        ));
    }
    let id = control
        .create_database(&name, &name)
        .map_err(|e| app_error(StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
    drop(control);

    // Create the data directory
    let db_dir = state.pool.data_dir().join(&name);
    std::fs::create_dir_all(&db_dir)
        .map_err(|e| app_error(StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;

    Ok(Json(json!({
        "name": name,
        "id": id,
        "message": format!("database '{}' created", name),
    })))
}

/// POST /v1/cluster/promote — manually trigger an election from this node.
/// Useful for forced failover during ops. Requires the node to be a voter.
async fn cluster_promote(
    State(state): State<Arc<AppState>>,
    headers: axum::http::HeaderMap,
) -> AppResult {
    // Auth check (any valid token works)
    let _ = resolve_engine(
        &state,
        headers.get("authorization").and_then(|v| v.to_str().ok()),
    )?;

    let Some(ref ctx) = state.cluster else {
        return Err(app_error(
            StatusCode::BAD_REQUEST,
            "single-node mode — nothing to promote",
        ));
    };

    if !matches!(ctx.state.configured_role, crate::config::NodeRole::Voter) {
        return Err(app_error(
            StatusCode::BAD_REQUEST,
            "this node is not a voter — cannot become leader",
        ));
    }

    if ctx.state.is_leader() {
        return Ok(Json(json!({
            "status": "already_leader",
            "node_id": ctx.node_id(),
            "term": ctx.state.current_term(),
        })));
    }

    let ctx_clone = std::sync::Arc::clone(ctx);
    tokio::spawn(async move {
        if let Err(e) = crate::cluster::election::start_election(ctx_clone).await {
            tracing::error!(error = %e, "manual promotion failed");
        }
    });

    Ok(Json(json!({
        "status": "election_started",
        "node_id": ctx.node_id(),
        "current_term": ctx.state.current_term(),
        "message": "check /v1/cluster in a few seconds to see the new leader"
    })))
}

// ---------- v0.8.3: openraft membership API (issue #24) ----------
//
// These three endpoints expose openraft's add_learner / change_membership
// over HTTP so operators can grow / shrink / promote membership without
// dropping into Rust. All require the cluster master token. They MUST be
// called against the current leader; followers return 503 with a
// "current leader" hint that the CLI uses to retry.

#[derive(serde::Deserialize)]
struct AddLearnerRequest {
    node_id: u64,
    addr: String,
}

#[derive(serde::Deserialize)]
struct PromoteRequest {
    /// Final voter set after the membership change. MUST include the
    /// current leader to avoid the leader inadvertently demoting itself.
    voters: Vec<u64>,
}

#[derive(serde::Deserialize)]
struct RemoveRequest {
    node_id: u64,
}

fn require_openraft(state: &AppState) -> Result<&Arc<crate::raft::RaftAssembly>, AppError> {
    state.raft.as_ref().ok_or_else(|| {
        app_error(
            StatusCode::BAD_REQUEST,
            "openraft mode is not active on this node — set cluster.raft_mode = \"openraft\"",
        )
    })
}

/// POST /v1/cluster/add-learner — add a non-voting learner to the cluster.
/// Body: `{"node_id": <u64>, "addr": "<host:cluster_port>"}`.
/// Auth: cluster master token.
async fn cluster_add_learner(
    State(state): State<Arc<AppState>>,
    headers: axum::http::HeaderMap,
    Json(body): Json<AddLearnerRequest>,
) -> AppResult {
    require_master_token(&state, &headers)?;
    let assembly = require_openraft(&state)?;
    let node = crate::raft::types::YantrikNode::new(body.addr.clone());
    let node_id = crate::raft::types::YantrikNodeId::from(body.node_id);
    match assembly.raft.add_learner(node_id, node, false).await {
        Ok(_resp) => Ok(Json(json!({
            "status": "learner_added",
            "node_id": body.node_id,
            "addr": body.addr,
            "note": "use /v1/cluster/raft to watch catch-up; promote when last_log_index lag is acceptable",
        }))),
        Err(e) => Err(app_error(
            StatusCode::INTERNAL_SERVER_ERROR,
            format!("add_learner failed: {e}"),
        )),
    }
}

/// POST /v1/cluster/promote — change membership to the given voter set.
/// Body: `{"voters": [<u64>, ...]}`.
/// Auth: cluster master token.
async fn cluster_promote_voter(
    State(state): State<Arc<AppState>>,
    headers: axum::http::HeaderMap,
    Json(body): Json<PromoteRequest>,
) -> AppResult {
    require_master_token(&state, &headers)?;
    let assembly = require_openraft(&state)?;
    let voters: std::collections::BTreeSet<crate::raft::types::YantrikNodeId> = body
        .voters
        .iter()
        .copied()
        .map(crate::raft::types::YantrikNodeId::from)
        .collect();
    if voters.is_empty() {
        return Err(app_error(
            StatusCode::BAD_REQUEST,
            "voters list cannot be empty",
        ));
    }
    let voters_clone: Vec<u64> = voters.iter().map(|n| u64::from(*n)).collect();
    match assembly.raft.change_membership(voters, false).await {
        Ok(_resp) => Ok(Json(json!({
            "status": "membership_changed",
            "voters": voters_clone,
        }))),
        Err(e) => Err(app_error(
            StatusCode::INTERNAL_SERVER_ERROR,
            format!("change_membership failed: {e}"),
        )),
    }
}

/// POST /v1/cluster/remove — remove a node from the cluster (atomic
/// membership change to current voters minus the named node).
/// Body: `{"node_id": <u64>}`.
/// Auth: cluster master token.
async fn cluster_remove(
    State(state): State<Arc<AppState>>,
    headers: axum::http::HeaderMap,
    Json(body): Json<RemoveRequest>,
) -> AppResult {
    require_master_token(&state, &headers)?;
    let assembly = require_openraft(&state)?;
    let metrics = assembly.raft.metrics().borrow().clone();
    let mut voters: std::collections::BTreeSet<crate::raft::types::YantrikNodeId> =
        metrics.membership_config.voter_ids().collect();
    let target_id = crate::raft::types::YantrikNodeId::from(body.node_id);
    if !voters.remove(&target_id) {
        return Err(app_error(
            StatusCode::BAD_REQUEST,
            format!("node {} is not a current voter", body.node_id),
        ));
    }
    if voters.is_empty() {
        return Err(app_error(
            StatusCode::BAD_REQUEST,
            "refusing to remove the last voter — would lose quorum permanently",
        ));
    }
    let remaining: Vec<u64> = voters.iter().map(|n| u64::from(*n)).collect();
    match assembly.raft.change_membership(voters, false).await {
        Ok(_resp) => Ok(Json(json!({
            "status": "removed",
            "removed_node_id": body.node_id,
            "remaining_voters": remaining,
        }))),
        Err(e) => Err(app_error(
            StatusCode::INTERNAL_SERVER_ERROR,
            format!("change_membership failed: {e}"),
        )),
    }
}

/// POST /v1/cluster/initialize — bootstrap a fresh openraft cluster on
/// THIS node alone. Use exactly once per cluster, on the seed node.
/// Subsequent voters are added via `add-learner` + `promote`.
/// Auth: cluster master token.
async fn cluster_initialize(
    State(state): State<Arc<AppState>>,
    headers: axum::http::HeaderMap,
) -> AppResult {
    require_master_token(&state, &headers)?;
    let assembly = require_openraft(&state)?;
    let metrics = assembly.raft.metrics().borrow().clone();
    if metrics.membership_config.nodes().count() > 0 {
        return Ok(Json(json!({
            "status": "already_initialized",
            "voters": metrics.membership_config.voter_ids().map(|n| u64::from(n)).collect::<Vec<_>>(),
        })));
    }
    // Find this node's advertise address from the cluster config we
    // captured at boot. ClusterContext (legacy raft-lite) holds it.
    let node_addr = state
        .cluster
        .as_ref()
        .and_then(|c| c.config.advertise_addr.clone())
        .unwrap_or_else(|| "127.0.0.1:7440".to_string());
    match crate::raft::initialize_single_node(assembly, node_addr.clone()).await {
        Ok(()) => Ok(Json(json!({
            "status": "initialized",
            "node_id": u64::from(metrics.id),
            "addr": node_addr,
        }))),
        Err(e) => Err(app_error(
            StatusCode::INTERNAL_SERVER_ERROR,
            format!("initialize failed: {e:?}"),
        )),
    }
}

/// GET /metrics — Prometheus-format metrics for monitoring.
async fn metrics(State(state): State<Arc<AppState>>) -> String {
    let mut out = String::new();

    out.push_str("# HELP yantrikdb_engines_loaded Number of engine instances currently loaded\n");
    out.push_str("# TYPE yantrikdb_engines_loaded gauge\n");
    out.push_str(&format!(
        "yantrikdb_engines_loaded {}\n",
        state.pool.loaded_count()
    ));

    // v0.8.7: Prometheus cluster gauges read from cluster_state_view so
    // openraft is the source of truth when active. Without this, on a
    // healthy openraft cluster, `yantrikdb_cluster_is_leader` shows 0 on
    // the actual leader (because legacy raft-lite has no quorum).
    if let Some(view) = cluster_state_view(&state) {
        out.push_str("# HELP yantrikdb_cluster_term Current Raft term\n");
        out.push_str("# TYPE yantrikdb_cluster_term gauge\n");
        out.push_str(&format!(
            "yantrikdb_cluster_term {{node_id=\"{}\",raft_mode=\"{}\"}} {}\n",
            view.node_id, view.raft_mode, view.term
        ));

        out.push_str("# HELP yantrikdb_cluster_is_leader Whether this node is currently the leader (1) or not (0)\n");
        out.push_str("# TYPE yantrikdb_cluster_is_leader gauge\n");
        out.push_str(&format!(
            "yantrikdb_cluster_is_leader {{node_id=\"{}\",raft_mode=\"{}\"}} {}\n",
            view.node_id,
            view.raft_mode,
            if view.accepts_writes { 1 } else { 0 }
        ));

        out.push_str(
            "# HELP yantrikdb_cluster_healthy Whether this node has quorum (1) or not (0)\n",
        );
        out.push_str("# TYPE yantrikdb_cluster_healthy gauge\n");
        out.push_str(&format!(
            "yantrikdb_cluster_healthy {{node_id=\"{}\",raft_mode=\"{}\"}} {}\n",
            view.node_id,
            view.raft_mode,
            if view.healthy { 1 } else { 0 }
        ));
    }
    // Peer reachability is raft-lite-specific (openraft tracks this in
    // its own metrics scrape via spawn_raft_metrics_recorder). Skip on
    // openraft mode.
    if state.raft.is_none() {
        if let Some(ref cluster) = state.cluster {
            out.push_str(
                "# HELP yantrikdb_cluster_peer_reachable Whether each peer is reachable\n",
            );
            out.push_str("# TYPE yantrikdb_cluster_peer_reachable gauge\n");
            for peer in cluster.peers.snapshot() {
                out.push_str(&format!(
                    "yantrikdb_cluster_peer_reachable {{addr=\"{}\",role=\"{:?}\"}} {}\n",
                    peer.addr,
                    peer.configured_role,
                    if peer.reachable { 1 } else { 0 }
                ));
            }
        }
    }

    // Per-database stats (default DB only for now).
    // IMPORTANT: do NOT hold control.lock() across engine.lock() — that
    // serializes /metrics behind any long-running engine call AND blocks all
    // auth (which needs control). Scope the control lock tightly, then drop
    // it before touching the engine.
    let default_db = {
        let control = state.control.lock();
        control.get_database("default").ok().flatten()
    };
    if let Some(rec) = default_db {
        if let Ok(engine) = state.pool.get_engine(&rec) {
            let stats_opt = {
                // v0.8.9: Arc<YantrikDB> direct (no outer mutex); stats()
                // uses engine's internal read connection pool.
                engine.stats(None).ok()
            };
            if let Some(stats) = stats_opt {
                {
                    out.push_str("# HELP yantrikdb_active_memories Number of active memories\n");
                    out.push_str("# TYPE yantrikdb_active_memories gauge\n");
                    out.push_str(&format!(
                        "yantrikdb_active_memories {{db=\"default\"}} {}\n",
                        stats.active_memories
                    ));

                    out.push_str(
                        "# HELP yantrikdb_consolidated_memories Number of consolidated memories\n",
                    );
                    out.push_str("# TYPE yantrikdb_consolidated_memories gauge\n");
                    out.push_str(&format!(
                        "yantrikdb_consolidated_memories {{db=\"default\"}} {}\n",
                        stats.consolidated_memories
                    ));

                    out.push_str("# HELP yantrikdb_edges Number of knowledge graph edges\n");
                    out.push_str("# TYPE yantrikdb_edges gauge\n");
                    out.push_str(&format!(
                        "yantrikdb_edges {{db=\"default\"}} {}\n",
                        stats.edges
                    ));

                    out.push_str(
                        "# HELP yantrikdb_open_conflicts Number of unresolved conflicts\n",
                    );
                    out.push_str("# TYPE yantrikdb_open_conflicts gauge\n");
                    out.push_str(&format!(
                        "yantrikdb_open_conflicts {{db=\"default\"}} {}\n",
                        stats.open_conflicts
                    ));

                    out.push_str("# HELP yantrikdb_operations_total Total operations\n");
                    out.push_str("# TYPE yantrikdb_operations_total counter\n");
                    out.push_str(&format!(
                        "yantrikdb_operations_total {{db=\"default\"}} {}\n",
                        stats.operations
                    ));
                }
            }
        }
    }

    // Append per-handler histograms, lock-wait histograms, request counters
    out.push_str(&crate::metrics::global().render_prometheus());

    out
}

async fn cluster_status(State(state): State<Arc<AppState>>) -> Json<Value> {
    let Some(ref ctx) = state.cluster else {
        return Json(json!({
            "clustered": false,
            "message": "single-node mode (no replication)"
        }));
    };

    let peers: Vec<Value> = ctx
        .peers
        .snapshot()
        .into_iter()
        .map(|p| {
            json!({
                "node_id": p.node_id,
                "addr": p.addr,
                "role": format!("{:?}", p.configured_role).to_lowercase(),
                "reachable": p.reachable,
                "current_term": p.current_term,
                "last_seen_secs_ago": p.last_seen.map(|t| t.elapsed().as_secs_f64()),
            })
        })
        .collect();

    Json(json!({
        "clustered": true,
        "node_id": ctx.node_id(),
        "role": format!("{:?}", ctx.state.leader_role()),
        "configured_role": format!("{:?}", ctx.state.configured_role).to_lowercase(),
        "current_term": ctx.state.current_term(),
        "leader_id": ctx.state.current_leader(),
        "voted_for": ctx.state.voted_for(),
        "accepts_writes": ctx.state.accepts_writes(),
        "healthy": ctx.is_healthy(),
        "quorum_size": ctx.quorum_size(),
        "peers": peers,
    }))
}

async fn list_databases(
    State(state): State<Arc<AppState>>,
    headers: axum::http::HeaderMap,
) -> AppResult {
    let _ = resolve_engine(
        &state,
        headers.get("authorization").and_then(|v| v.to_str().ok()),
    )?;
    let databases = state
        .control
        .lock()
        .list_databases()
        .map_err(|e| app_error(StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
    let list: Vec<Value> = databases
        .iter()
        .map(|d| json!({ "id": d.id, "name": d.name, "created_at": d.created_at }))
        .collect();
    Ok(Json(json!({ "databases": list })))
}

/// GET /v1/admin/control-snapshot — returns a full snapshot of the control
/// plane (databases + active tokens) for replication to followers.
///
/// Authenticated by cluster master token only. Called by the follower's
/// control-sync loop, not by end users.
async fn control_snapshot(
    State(state): State<Arc<AppState>>,
    headers: axum::http::HeaderMap,
) -> AppResult {
    // Require cluster master token
    let token = headers
        .get("authorization")
        .and_then(|v| v.to_str().ok())
        .and_then(|h| h.strip_prefix("Bearer "))
        .ok_or_else(|| app_error(StatusCode::UNAUTHORIZED, "missing Bearer token"))?;

    let is_master = state
        .cluster
        .as_ref()
        .and_then(|c| c.config.cluster_secret.as_ref())
        .map(|s| token == s.as_str())
        .unwrap_or(false);

    if !is_master {
        return Err(app_error(
            StatusCode::FORBIDDEN,
            "control-snapshot requires cluster master token",
        ));
    }

    let snapshot = tokio::task::spawn_blocking({
        let control = state.control.clone();
        move || control.lock().export_snapshot()
    })
    .await
    .map_err(|e| app_error(StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?
    .map_err(|e| app_error(StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;

    Ok(Json(serde_json::to_value(snapshot).unwrap_or_default()))
}

/// POST /v1/admin/snapshot — create an online backup of a tenant database.
///
/// Takes a consistent snapshot by WAL-checkpointing then copying the SQLite
/// file while holding the engine lock. Returns the backup path + BLAKE3
/// checksum.
///
/// ## Authentication
///
/// Two acceptable tokens (issue #7 fix):
/// 1. **Cluster master token** — accepted always. Existing cluster-mode
///    behavior. Allows snapshotting any database.
/// 2. **Per-database token for the target database** — accepted in any
///    mode (single-node OR cluster). The token must authenticate against
///    the SAME database named in the request body. Single-node operators
///    have no cluster master token; this lets them snapshot their own
///    database with the token they already have.
///
/// Body: `{"database": "default", "output_dir": "/tmp/backups"}` (optional
/// output_dir, defaults to data_dir/snapshots/).
async fn admin_snapshot(
    State(state): State<Arc<AppState>>,
    headers: axum::http::HeaderMap,
    Json(body): Json<Value>,
) -> AppResult {
    let token = headers
        .get("authorization")
        .and_then(|v| v.to_str().ok())
        .and_then(|h| h.strip_prefix("Bearer "))
        .ok_or_else(|| app_error(StatusCode::UNAUTHORIZED, "missing Bearer token"))?;

    let db_name = body
        .get("database")
        .and_then(|v| v.as_str())
        .unwrap_or("default")
        .to_string();

    // Try cluster master first (preserves cluster-mode behavior).
    let is_master = state
        .cluster
        .as_ref()
        .and_then(|c| c.config.cluster_secret.as_ref())
        .map(|s| token == s.as_str())
        .unwrap_or(false);

    if !is_master {
        // Fall back to per-database token. The token must authenticate
        // against the SAME database being snapshotted — operators can't
        // use a token for db A to snapshot db B.
        let token_hash = auth::hash_token(token);
        let control = state.control.lock();
        let token_db_id = control
            .validate_token(&token_hash)
            .map_err(|e| app_error(StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?
            .ok_or_else(|| app_error(StatusCode::UNAUTHORIZED, "invalid or revoked token"))?;
        let target_db = control
            .get_database(&db_name)
            .map_err(|e| app_error(StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?
            .ok_or_else(|| {
                app_error(
                    StatusCode::NOT_FOUND,
                    format!("database '{}' not found", db_name),
                )
            })?;
        drop(control);
        if token_db_id != target_db.id {
            return Err(app_error(
                StatusCode::FORBIDDEN,
                format!(
                    "token does not authenticate database '{}' — provide cluster master token \
                     or a token for that specific database",
                    db_name
                ),
            ));
        }
    }

    let output_dir = body
        .get("output_dir")
        .and_then(|v| v.as_str())
        .map(std::path::PathBuf::from)
        .unwrap_or_else(|| state.pool.data_dir().join("snapshots"));

    let control = state.control.clone();
    let pool = state.pool.clone();

    let result = tokio::task::spawn_blocking(move || -> anyhow::Result<Value> {
        let db_record = {
            let ctrl = control.lock();
            ctrl.get_database(&db_name)?
                .ok_or_else(|| anyhow::anyhow!("database '{}' not found", db_name))?
        };

        let engine = pool.get_engine(&db_record)?;
        let db = engine.as_ref();

        // WAL checkpoint before snapshot for consistency
        let conn = db.conn();
        conn.execute_batch("PRAGMA wal_checkpoint(TRUNCATE)")?;
        drop(conn);

        // Source path
        let src_dir = pool.data_dir().join(&db_record.path);
        let src_db = src_dir.join("yantrik.db");

        if !src_db.exists() {
            anyhow::bail!("database file not found: {:?}", src_db);
        }

        // Destination
        std::fs::create_dir_all(&output_dir)?;
        let ts = chrono_ts();
        let dest_name = format!("{}-{}.db", db_name, ts);
        let dest_path = output_dir.join(&dest_name);

        // Copy the database file
        std::fs::copy(&src_db, &dest_path)?;

        // Compute checksum
        let data = std::fs::read(&dest_path)?;
        let hash = blake3::hash(&data);
        let size = data.len();

        Ok(serde_json::json!({
            "database": db_name,
            "path": dest_path.to_str().unwrap_or(""),
            "size_bytes": size,
            "checksum_blake3": hash.to_hex().to_string(),
            "timestamp": ts,
        }))
    })
    .await
    .map_err(|e| app_error(StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?
    .map_err(|e| app_error(StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;

    Ok(Json(result))
}

// ──────────────────────────────────────────────────────────────────
// RFC 008 substrate surface — exposes the Warrant Flow primitives as
// HTTP endpoints so an agent (MCP client, local LLM, etc.) can ingest
// claims with source_lineage, read mobility/contest state, record
// cognitive moves, and audit flagged propositions. See the tool-
// discovery doc for the full surface.
// ──────────────────────────────────────────────────────────────────

async fn ingest_claim_with_lineage(
    State(state): State<Arc<AppState>>,
    headers: axum::http::HeaderMap,
    Json(body): Json<Value>,
) -> AppResult {
    let _timer = crate::metrics::HandlerTimer::new("ingest_claim_with_lineage");
    check_writable(&state)?;
    let (_, engine) = resolve_engine(
        &state,
        headers.get("authorization").and_then(|v| v.to_str().ok()),
    )?;
    let source_lineage: Vec<String> = body
        .get("source_lineage")
        .and_then(|v| v.as_array())
        .map(|arr| {
            arr.iter()
                .filter_map(|x| x.as_str().map(|s| s.to_string()))
                .collect()
        })
        .unwrap_or_default();
    let cmd = Command::IngestClaimWithLineage {
        src: body["src"]
            .as_str()
            .ok_or_else(|| app_error(StatusCode::BAD_REQUEST, "missing 'src'"))?
            .into(),
        rel_type: body["rel_type"]
            .as_str()
            .ok_or_else(|| app_error(StatusCode::BAD_REQUEST, "missing 'rel_type'"))?
            .into(),
        dst: body["dst"]
            .as_str()
            .ok_or_else(|| app_error(StatusCode::BAD_REQUEST, "missing 'dst'"))?
            .into(),
        namespace: body
            .get("namespace")
            .and_then(|v| v.as_str())
            .unwrap_or("default")
            .into(),
        polarity: body.get("polarity").and_then(|v| v.as_i64()).unwrap_or(1) as i32,
        modality: body
            .get("modality")
            .and_then(|v| v.as_str())
            .unwrap_or("asserted")
            .into(),
        valid_from: body.get("valid_from").and_then(|v| v.as_f64()),
        valid_to: body.get("valid_to").and_then(|v| v.as_f64()),
        extractor: body
            .get("extractor")
            .and_then(|v| v.as_str())
            .unwrap_or("manual")
            .into(),
        extractor_version: body
            .get("extractor_version")
            .and_then(|v| v.as_str())
            .map(|s| s.to_string()),
        confidence_band: body
            .get("confidence_band")
            .and_then(|v| v.as_str())
            .unwrap_or("medium")
            .into(),
        source_memory_rid: body
            .get("source_memory_rid")
            .and_then(|v| v.as_str())
            .map(|s| s.to_string()),
        weight: body.get("weight").and_then(|v| v.as_f64()).unwrap_or(1.0),
        source_lineage,
    };
    execute_cmd(engine, cmd, state.control.clone(), &state.inflight).await
}

async fn get_mobility(
    State(state): State<Arc<AppState>>,
    headers: axum::http::HeaderMap,
    axum::extract::Query(params): axum::extract::Query<std::collections::HashMap<String, String>>,
) -> AppResult {
    let _timer = crate::metrics::HandlerTimer::new("get_mobility");
    let (_, engine) = resolve_engine(
        &state,
        headers.get("authorization").and_then(|v| v.to_str().ok()),
    )?;
    let cmd = Command::GetMobility {
        src: params
            .get("src")
            .cloned()
            .ok_or_else(|| app_error(StatusCode::BAD_REQUEST, "missing 'src'"))?,
        rel_type: params
            .get("rel_type")
            .cloned()
            .ok_or_else(|| app_error(StatusCode::BAD_REQUEST, "missing 'rel_type'"))?,
        dst: params
            .get("dst")
            .cloned()
            .ok_or_else(|| app_error(StatusCode::BAD_REQUEST, "missing 'dst'"))?,
        namespace: params
            .get("namespace")
            .cloned()
            .unwrap_or_else(|| "default".to_string()),
        regime: params
            .get("regime")
            .cloned()
            .unwrap_or_else(|| "default".to_string()),
    };
    execute_cmd(engine, cmd, state.control.clone(), &state.inflight).await
}

async fn get_contest(
    State(state): State<Arc<AppState>>,
    headers: axum::http::HeaderMap,
    axum::extract::Query(params): axum::extract::Query<std::collections::HashMap<String, String>>,
) -> AppResult {
    let _timer = crate::metrics::HandlerTimer::new("get_contest");
    let (_, engine) = resolve_engine(
        &state,
        headers.get("authorization").and_then(|v| v.to_str().ok()),
    )?;
    let cmd = Command::GetContest {
        src: params
            .get("src")
            .cloned()
            .ok_or_else(|| app_error(StatusCode::BAD_REQUEST, "missing 'src'"))?,
        rel_type: params
            .get("rel_type")
            .cloned()
            .ok_or_else(|| app_error(StatusCode::BAD_REQUEST, "missing 'rel_type'"))?,
        dst: params
            .get("dst")
            .cloned()
            .ok_or_else(|| app_error(StatusCode::BAD_REQUEST, "missing 'dst'"))?,
        namespace: params
            .get("namespace")
            .cloned()
            .unwrap_or_else(|| "default".to_string()),
        regime: params
            .get("regime")
            .cloned()
            .unwrap_or_else(|| "default".to_string()),
    };
    execute_cmd(engine, cmd, state.control.clone(), &state.inflight).await
}

async fn record_move_event(
    State(state): State<Arc<AppState>>,
    headers: axum::http::HeaderMap,
    Json(body): Json<Value>,
) -> AppResult {
    let _timer = crate::metrics::HandlerTimer::new("record_move_event");
    check_writable(&state)?;
    let (_, engine) = resolve_engine(
        &state,
        headers.get("authorization").and_then(|v| v.to_str().ok()),
    )?;
    let string_array = |key: &str| -> Vec<String> {
        body.get(key)
            .and_then(|v| v.as_array())
            .map(|arr| {
                arr.iter()
                    .filter_map(|x| x.as_str().map(|s| s.to_string()))
                    .collect()
            })
            .unwrap_or_default()
    };
    let cmd = Command::RecordMoveEvent {
        move_type: body["move_type"]
            .as_str()
            .ok_or_else(|| app_error(StatusCode::BAD_REQUEST, "missing 'move_type'"))?
            .into(),
        operator_version: body
            .get("operator_version")
            .and_then(|v| v.as_str())
            .unwrap_or("v1")
            .into(),
        context_regime: body
            .get("context_regime")
            .and_then(|v| v.as_str())
            .map(|s| s.to_string()),
        observability: body
            .get("observability")
            .and_then(|v| v.as_str())
            .unwrap_or("observed")
            .into(),
        inference_confidence: body.get("inference_confidence").and_then(|v| v.as_f64()),
        inference_basis: body
            .get("inference_basis")
            .and_then(|v| v.as_array())
            .map(|arr| {
                arr.iter()
                    .filter_map(|x| x.as_str().map(|s| s.to_string()))
                    .collect()
            }),
        input_claim_ids: string_array("input_claim_ids"),
        output_claim_ids: string_array("output_claim_ids"),
        side_effect_claim_ids: string_array("side_effect_claim_ids"),
        dependencies: string_array("dependencies"),
    };
    execute_cmd(engine, cmd, state.control.clone(), &state.inflight).await
}

async fn list_flagged(
    State(state): State<Arc<AppState>>,
    headers: axum::http::HeaderMap,
    axum::extract::Query(params): axum::extract::Query<std::collections::HashMap<String, String>>,
) -> AppResult {
    let _timer = crate::metrics::HandlerTimer::new("list_flagged");
    let (_, engine) = resolve_engine(
        &state,
        headers.get("authorization").and_then(|v| v.to_str().ok()),
    )?;
    let flag_mask = params
        .get("flag_mask")
        .and_then(|s| s.parse::<u64>().ok())
        .unwrap_or(0);
    let limit = params
        .get("limit")
        .and_then(|s| s.parse::<usize>().ok())
        .unwrap_or(50);
    let cmd = Command::ListFlaggedPropositions { flag_mask, limit };
    execute_cmd(engine, cmd, state.control.clone(), &state.inflight).await
}

// ── RFC 010 PR-5: Jepsen / debug surface ───────────────────────────

/// Verify the caller holds the cluster master token. Debug endpoints
/// are operator-only — they're destructive when used wrong (fault
/// injection drops cluster traffic), so PR-5 keeps the gate strict.
/// RFC 014-B will replace this with an RBAC scope check.
fn require_master_token(state: &AppState, headers: &axum::http::HeaderMap) -> Result<(), AppError> {
    let token = headers
        .get("authorization")
        .and_then(|h| h.to_str().ok())
        .and_then(|h| h.strip_prefix("Bearer "))
        .ok_or_else(|| app_error(StatusCode::UNAUTHORIZED, "missing Bearer token"))?;
    if let Some(ref cluster) = state.cluster {
        if let Some(ref secret) = cluster.config.cluster_secret {
            if token == secret.as_str() {
                return Ok(());
            }
        }
    }
    // Single-node mode without a configured cluster secret: deny outright.
    // Safer than auto-allowing any valid bearer; debug endpoints SHOULD
    // require explicit operator opt-in via cluster_secret.
    Err(app_error(
        StatusCode::FORBIDDEN,
        "debug endpoints require the cluster master token",
    ))
}

async fn debug_history(
    State(state): State<Arc<AppState>>,
    headers: axum::http::HeaderMap,
    AxumPath(tenant_id): AxumPath<i64>,
    axum::extract::Query(params): axum::extract::Query<crate::debug::history::HistoryParams>,
) -> AppResult {
    let _timer = crate::metrics::HandlerTimer::new("debug_history");
    require_master_token(&state, &headers)?;
    let resp = crate::debug::history::read_history(
        &state.commit_log,
        crate::commit::TenantId::new(tenant_id),
        &params,
    )
    .await
    .map_err(|e| {
        app_error(
            StatusCode::INTERNAL_SERVER_ERROR,
            format!("read_history failed: {e}"),
        )
    })?;
    Ok(Json(serde_json::to_value(resp).map_err(|e| {
        app_error(
            StatusCode::INTERNAL_SERVER_ERROR,
            format!("history serialize failed: {e}"),
        )
    })?))
}

#[derive(serde::Deserialize)]
struct DebugFaultInjectBody {
    fault: crate::debug::FaultKind,
    /// Auto-clear after this many seconds. Useful for self-cleaning
    /// chaos tests that don't want to leave a leaked fault on a node
    /// after a CI run dies.
    ttl_secs: Option<u64>,
}

async fn debug_fault_inject(
    State(state): State<Arc<AppState>>,
    headers: axum::http::HeaderMap,
    Json(body): Json<DebugFaultInjectBody>,
) -> AppResult {
    let _timer = crate::metrics::HandlerTimer::new("debug_fault_inject");
    require_master_token(&state, &headers)?;
    let id = state.fault_registry.inject(body.fault, body.ttl_secs);
    Ok(Json(json!({
        "fault_id": id.to_string(),
    })))
}

async fn debug_fault_list(
    State(state): State<Arc<AppState>>,
    headers: axum::http::HeaderMap,
) -> AppResult {
    let _timer = crate::metrics::HandlerTimer::new("debug_fault_list");
    require_master_token(&state, &headers)?;
    let faults = state.fault_registry.list();
    Ok(Json(serde_json::to_value(faults).map_err(|e| {
        app_error(
            StatusCode::INTERNAL_SERVER_ERROR,
            format!("fault list serialize failed: {e}"),
        )
    })?))
}

async fn debug_fault_clear(
    State(state): State<Arc<AppState>>,
    headers: axum::http::HeaderMap,
) -> AppResult {
    let _timer = crate::metrics::HandlerTimer::new("debug_fault_clear");
    require_master_token(&state, &headers)?;
    let n = state.fault_registry.clear();
    Ok(Json(json!({ "cleared": n })))
}

async fn debug_fault_remove(
    State(state): State<Arc<AppState>>,
    headers: axum::http::HeaderMap,
    AxumPath(fault_id): AxumPath<u64>,
) -> AppResult {
    let _timer = crate::metrics::HandlerTimer::new("debug_fault_remove");
    require_master_token(&state, &headers)?;
    let removed = state
        .fault_registry
        .remove(crate::debug::FaultId::new(fault_id));
    if removed {
        Ok(Json(json!({ "removed": true })))
    } else {
        Err(app_error(
            StatusCode::NOT_FOUND,
            format!("no fault with id fault_{fault_id}"),
        ))
    }
}

// ── Phase 1 polish: jobs + migrations admin surface ────────────────

#[derive(serde::Deserialize)]
struct JobsListParams {
    /// Filter by tenant id (optional).
    tenant: Option<i64>,
    /// Filter by state ("Pending" | "Leased" | "Succeeded" | "Failed" | "Cancelled").
    state: Option<String>,
    /// Maximum entries to return. Capped at 500 server-side.
    limit: Option<usize>,
}

const MAX_JOBS_LIST_LIMIT: usize = 500;

async fn jobs_list(
    State(state): State<Arc<AppState>>,
    headers: axum::http::HeaderMap,
    axum::extract::Query(params): axum::extract::Query<JobsListParams>,
) -> AppResult {
    let _timer = crate::metrics::HandlerTimer::new("jobs_list");
    require_master_token(&state, &headers)?;
    let tenant_filter = params.tenant.map(crate::commit::TenantId::new);
    let state_filter = params
        .state
        .as_deref()
        .and_then(crate::jobs::JobState::from_str);
    let limit = params.limit.unwrap_or(100).min(MAX_JOBS_LIST_LIMIT);
    let records = state
        .jobs
        .list(tenant_filter, state_filter, limit)
        .await
        .map_err(|e| {
            app_error(
                StatusCode::INTERNAL_SERVER_ERROR,
                format!("jobs list failed: {e}"),
            )
        })?;
    Ok(Json(serde_json::to_value(records).map_err(|e| {
        app_error(
            StatusCode::INTERNAL_SERVER_ERROR,
            format!("jobs list serialize failed: {e}"),
        )
    })?))
}

async fn jobs_get(
    State(state): State<Arc<AppState>>,
    headers: axum::http::HeaderMap,
    AxumPath(job_id_str): AxumPath<String>,
) -> AppResult {
    let _timer = crate::metrics::HandlerTimer::new("jobs_get");
    require_master_token(&state, &headers)?;
    let uuid = job_id_str.parse::<uuid7::Uuid>().map_err(|e| {
        app_error(
            StatusCode::BAD_REQUEST,
            format!("invalid job id `{job_id_str}`: {e}"),
        )
    })?;
    let record = state
        .jobs
        .get(crate::jobs::JobId(uuid))
        .await
        .map_err(|e| match e {
            crate::jobs::JobError::NotFound { .. } => {
                app_error(StatusCode::NOT_FOUND, e.to_string())
            }
            other => app_error(StatusCode::INTERNAL_SERVER_ERROR, other.to_string()),
        })?;
    Ok(Json(serde_json::to_value(record).map_err(|e| {
        app_error(
            StatusCode::INTERNAL_SERVER_ERROR,
            format!("job serialize failed: {e}"),
        )
    })?))
}

async fn jobs_cancel(
    State(state): State<Arc<AppState>>,
    headers: axum::http::HeaderMap,
    AxumPath(job_id_str): AxumPath<String>,
) -> AppResult {
    let _timer = crate::metrics::HandlerTimer::new("jobs_cancel");
    require_master_token(&state, &headers)?;
    let uuid = job_id_str.parse::<uuid7::Uuid>().map_err(|e| {
        app_error(
            StatusCode::BAD_REQUEST,
            format!("invalid job id `{job_id_str}`: {e}"),
        )
    })?;
    state
        .jobs
        .cancel(crate::jobs::JobId(uuid))
        .await
        .map_err(|e| match e {
            crate::jobs::JobError::NotFound { .. } => {
                app_error(StatusCode::NOT_FOUND, e.to_string())
            }
            crate::jobs::JobError::TerminalState { .. } => {
                app_error(StatusCode::CONFLICT, e.to_string())
            }
            other => app_error(StatusCode::INTERNAL_SERVER_ERROR, other.to_string()),
        })?;
    Ok(Json(json!({ "cancelled": job_id_str })))
}

async fn admin_migrations_list(
    State(state): State<Arc<AppState>>,
    headers: axum::http::HeaderMap,
) -> AppResult {
    let _timer = crate::metrics::HandlerTimer::new("admin_migrations");
    require_master_token(&state, &headers)?;
    // Migrations are tracked separately per SQLite file. Surface the
    // commit_log + jobs DBs (the two we know about). Future RFCs adding
    // per-tenant DBs will register them here.
    let mut all = serde_json::json!({});
    // Re-open each DB read-only via a fresh connection so we don't
    // compete with the live committer's mutex. SQLite read-only doesn't
    // lock-conflict with WAL writes.
    let commit_log_path = state.data_dir.join("commit_log.sqlite");
    let jobs_path = state.data_dir.join("jobs.sqlite");
    for (label, path) in [("commit_log", &commit_log_path), ("jobs", &jobs_path)] {
        let summary = match rusqlite::Connection::open_with_flags(
            path,
            rusqlite::OpenFlags::SQLITE_OPEN_READ_ONLY,
        ) {
            Ok(conn) => match crate::migrations::MigrationRunner::applied_summary(&conn) {
                Ok(rows) => serde_json::json!(rows
                    .iter()
                    .map(|(id, name)| serde_json::json!({"id": id, "name": name}))
                    .collect::<Vec<_>>()),
                Err(e) => serde_json::json!({"error": e.to_string()}),
            },
            Err(e) => serde_json::json!({"error": format!("open failed: {e}")}),
        };
        all[label] = summary;
    }
    Ok(Json(all))
}

/// Simple timestamp for backup filenames.
fn chrono_ts() -> String {
    use std::time::{SystemTime, UNIX_EPOCH};
    let secs = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default()
        .as_secs();
    format!("{}", secs)
}

// ─────────────────────────────────────────────────────────────────────────
// RFC 022 §1 — Skill Substrate API (v0.8.11)
//
// Five thin endpoints over the existing memory primitives, hardcoded to
// `namespace=skill_substrate, metadata.record_type=skill, memory_type=
// procedural`. Schema-validated on define (skill_id format, applies_to
// entry regex, skill_type enum), no semantic ontology (no validation
// gates, no outcome rollups). The point is to standardise the shape so
// every program stops reinventing skill_define / skill_get / skill_recall
// in agent code with subtle bugs (the cross-lane bug pattern that drove
// this RFC).
//
// In v0.8.11 `skill_get` does scan-then-filter via `engine.list_memories`
// + client-side filter on `metadata.skill_id`. v0.8.12 replaces this with
// /v1/lookup (O(log N) via indexed metadata).
// ─────────────────────────────────────────────────────────────────────────

const SKILL_NAMESPACE: &str = "skill_substrate";
const OUTCOME_NAMESPACE: &str = "outcome_substrate";
const VALID_SKILL_TYPES: &[&str] = &["procedure", "reference", "lesson", "pattern", "rule"];

/// Validate a skill_id against `^[a-z][a-z0-9_]*(\.[a-z0-9_]+)+$` by hand.
/// Returns Err with a specific reason if invalid.
fn validate_skill_id(s: &str) -> Result<(), &'static str> {
    if s.len() < 4 || s.len() > 200 {
        return Err("skill_id length must be 4..200 characters");
    }
    let bytes = s.as_bytes();
    if !bytes[0].is_ascii_lowercase() {
        return Err("skill_id must start with a lowercase letter");
    }
    let mut has_dot = false;
    let mut last_was_dot = false;
    for &b in bytes {
        let c = b as char;
        let is_valid = c.is_ascii_lowercase() || c.is_ascii_digit() || c == '_' || c == '.';
        if !is_valid {
            return Err("skill_id contains invalid character (allowed: lowercase a-z, 0-9, _, .)");
        }
        if c == '.' {
            if last_was_dot {
                return Err("skill_id contains consecutive dots");
            }
            has_dot = true;
            last_was_dot = true;
        } else {
            last_was_dot = false;
        }
    }
    if !has_dot {
        return Err("skill_id must contain at least one '.' (dotted form, e.g. skill.foo.v1)");
    }
    if last_was_dot {
        return Err("skill_id must not end with '.'");
    }
    Ok(())
}

/// Validate an `applies_to` array entry against `^[a-z][a-z0-9_]*$`.
/// Critical for catching hyphen-vs-underscore drift bugs (cf. Brainstorm 3
/// round 2 deepseek's `meta_agent` vs `meta-agent` example).
fn validate_applies_to_entry(s: &str) -> Result<(), &'static str> {
    if s.is_empty() {
        return Err("applies_to entry must be non-empty");
    }
    let bytes = s.as_bytes();
    if !bytes[0].is_ascii_lowercase() {
        return Err("applies_to entry must start with a lowercase letter");
    }
    for &b in bytes {
        let c = b as char;
        if !(c.is_ascii_lowercase() || c.is_ascii_digit() || c == '_') {
            return Err(
                "applies_to entry contains invalid character (allowed: lowercase a-z, 0-9, _)",
            );
        }
    }
    Ok(())
}

/// `POST /v1/skills/define` — write a new skill record. Strict shape
/// validation, 409 on duplicate skill_id by default. Wraps /v1/remember
/// internally with namespace=skill_substrate, metadata.record_type=skill,
/// memory_type=procedural.
async fn skill_define(
    State(state): State<Arc<AppState>>,
    headers: axum::http::HeaderMap,
    Json(body): Json<Value>,
) -> AppResult {
    let _timer = crate::metrics::HandlerTimer::new("skill_define");
    check_writable(&state)?;

    // ── Schema validation ──────────────────────────────────
    let skill_id = body["skill_id"]
        .as_str()
        .ok_or_else(|| app_error(StatusCode::BAD_REQUEST, "missing 'skill_id'"))?;
    validate_skill_id(skill_id)
        .map_err(|e| app_error(StatusCode::BAD_REQUEST, format!("INVALID_SKILL_ID: {}", e)))?;

    let body_text = body["body"]
        .as_str()
        .ok_or_else(|| app_error(StatusCode::BAD_REQUEST, "missing 'body'"))?;
    if body_text.len() < 50 || body_text.len() > 5000 {
        return Err(app_error(
            StatusCode::BAD_REQUEST,
            "INVALID_BODY_LENGTH: body must be 50..5000 characters",
        ));
    }

    let applies_to = body["applies_to"]
        .as_array()
        .ok_or_else(|| app_error(StatusCode::BAD_REQUEST, "missing or non-array 'applies_to'"))?;
    if applies_to.is_empty() {
        return Err(app_error(
            StatusCode::BAD_REQUEST,
            "EMPTY_APPLIES_TO: applies_to must be a non-empty array",
        ));
    }
    if applies_to.len() > 10 {
        return Err(app_error(
            StatusCode::BAD_REQUEST,
            "TOO_MANY_APPLIES_TO: applies_to may have at most 10 entries",
        ));
    }
    let mut applies_to_strs = Vec::with_capacity(applies_to.len());
    for v in applies_to {
        let s = v.as_str().ok_or_else(|| {
            app_error(
                StatusCode::BAD_REQUEST,
                "INVALID_APPLIES_TO_ENTRY: each entry must be a string",
            )
        })?;
        validate_applies_to_entry(s).map_err(|e| {
            app_error(
                StatusCode::BAD_REQUEST,
                format!("INVALID_APPLIES_TO_ENTRY: {}", e),
            )
        })?;
        applies_to_strs.push(s.to_string());
    }

    let skill_type = body["skill_type"]
        .as_str()
        .ok_or_else(|| app_error(StatusCode::BAD_REQUEST, "missing 'skill_type'"))?;
    if !VALID_SKILL_TYPES.contains(&skill_type) {
        return Err(app_error(
            StatusCode::BAD_REQUEST,
            format!(
                "INVALID_SKILL_TYPE: must be one of {:?}, got '{}'",
                VALID_SKILL_TYPES, skill_type
            ),
        ));
    }

    // ── Resolve engine + check duplicate ────────────────────
    let (_, engine) = resolve_engine(
        &state,
        headers.get("authorization").and_then(|v| v.to_str().ok()),
    )?;

    let on_conflict = body
        .get("on_conflict")
        .and_then(|v| v.as_str())
        .unwrap_or("reject");

    if let Some(existing_rid) = find_skill_rid(&engine, skill_id) {
        match on_conflict {
            "reject" => {
                return Err(app_error(
                    StatusCode::CONFLICT,
                    format!(
                        "SKILL_ID_CONFLICT: '{}' already exists (rid={}); pass on_conflict=update to overwrite or on_conflict=ignore to no-op",
                        skill_id, existing_rid
                    ),
                ));
            }
            "ignore" => {
                return Ok(Json(serde_json::json!({
                    "rid":         existing_rid,
                    "skill_id":    skill_id,
                    "namespace":   SKILL_NAMESPACE,
                    "memory_type": "procedural",
                    "on_conflict": "ignore"
                })));
            }
            "update" => {
                // Tombstone the existing rid; new write below proceeds.
                let _ = engine.forget(&existing_rid);
            }
            other => {
                return Err(app_error(
                    StatusCode::BAD_REQUEST,
                    format!(
                        "INVALID_ON_CONFLICT: '{}' (allowed: reject, update, ignore)",
                        other
                    ),
                ));
            }
        }
    }

    // ── Build metadata + dispatch through Command::Remember ─
    let user_metadata = body.get("metadata").cloned().unwrap_or(Value::Null);
    let mut metadata = serde_json::json!({
        "record_type": "skill",
        "skill_id":    skill_id,
        "applies_to":  applies_to_strs,
        "skill_type":  skill_type,
    });
    // Merge user-supplied extra metadata fields without overwriting reserved keys.
    if let Value::Object(user_map) = user_metadata {
        if let Value::Object(meta_map) = &mut metadata {
            for (k, v) in user_map {
                meta_map.entry(k).or_insert(v);
            }
        }
    }

    execute_cmd(
        engine,
        Command::Remember {
            text: body_text.to_string(),
            memory_type: "procedural".to_string(),
            importance: body
                .get("importance")
                .and_then(|v| v.as_f64())
                .unwrap_or(0.7),
            valence: 0.0,
            half_life: 30.0 * 24.0 * 3600.0,
            metadata,
            namespace: SKILL_NAMESPACE.to_string(),
            certainty: 0.9,
            domain: "skill".to_string(),
            source: body
                .get("source")
                .and_then(|v| v.as_str())
                .unwrap_or("agent")
                .to_string(),
            emotional_state: None,
            embedding: None,
        },
        state.control.clone(),
        &state.inflight,
    )
    .await
}

/// `GET /v1/skills/{skill_id}` — exact lookup. v0.8.11 implementation:
/// scan namespace + filter on metadata.skill_id (slow path, O(N)).
/// v0.8.12 will replace with `/v1/lookup` for O(log N).
async fn skill_get(
    State(state): State<Arc<AppState>>,
    headers: axum::http::HeaderMap,
    axum::extract::Path(skill_id): axum::extract::Path<String>,
) -> AppResult {
    let _timer = crate::metrics::HandlerTimer::new("skill_get");
    validate_skill_id(&skill_id)
        .map_err(|e| app_error(StatusCode::BAD_REQUEST, format!("INVALID_SKILL_ID: {}", e)))?;
    let (_, engine) = resolve_engine(
        &state,
        headers.get("authorization").and_then(|v| v.to_str().ok()),
    )?;

    if let Some(rid) = find_skill_rid(&engine, &skill_id) {
        if let Ok(Some(mem)) = engine.get(&rid) {
            return Ok(Json(serde_json::json!({
                "rid":         mem.rid,
                "skill_id":    skill_id,
                "body":        mem.text,
                "namespace":   mem.namespace,
                "memory_type": mem.memory_type,
                "metadata":    mem.metadata,
                "created_at":  mem.created_at,
            })));
        }
    }
    Err(app_error(
        StatusCode::NOT_FOUND,
        format!("skill_id '{}' not found", skill_id),
    ))
}

/// `POST /v1/skills/search` — semantic search over skill_substrate.
/// Optional `applies_to` and `skill_type` filters are post-fetch in
/// v0.8.11; v0.8.13 makes them prefilter via the where-clause work.
async fn skill_search(
    State(state): State<Arc<AppState>>,
    headers: axum::http::HeaderMap,
    Json(body): Json<Value>,
) -> AppResult {
    let _timer = crate::metrics::HandlerTimer::new("skill_search");
    let (_, engine) = resolve_engine(
        &state,
        headers.get("authorization").and_then(|v| v.to_str().ok()),
    )?;

    let query = body["query"]
        .as_str()
        .ok_or_else(|| app_error(StatusCode::BAD_REQUEST, "missing 'query'"))?
        .to_string();
    let top_k = body
        .get("top_k")
        .and_then(|v| v.as_u64())
        .unwrap_or(5)
        .min(50) as usize;
    let applies_to_filter: Option<String> = body
        .get("applies_to")
        .and_then(|v| v.as_array())
        .and_then(|arr| arr.first())
        .and_then(|v| v.as_str())
        .map(|s| s.to_string());
    let skill_type_filter: Option<String> = body
        .get("skill_type")
        .and_then(|v| v.as_str())
        .map(|s| s.to_string());

    // Overfetch to allow post-filter to land top_k after exclusion.
    let fetch_k = (top_k * 4).max(20);
    execute_cmd_with_post_filter(
        engine,
        Command::Recall {
            query,
            top_k: fetch_k,
            memory_type: Some("procedural".to_string()),
            include_consolidated: false,
            expand_entities: false,
            namespace: Some(SKILL_NAMESPACE.to_string()),
            domain: Some("skill".to_string()),
            source: None,
            query_embedding: None,
        },
        state.control.clone(),
        &state.inflight,
        applies_to_filter,
        skill_type_filter,
        top_k,
    )
    .await
}

/// `POST /v1/skills/{skill_id}/outcome` — append-only outcome log. Engine
/// NEVER auto-rolls-up success_count on the parent skill (architectural
/// enforcement of schema-not-semantics).
async fn skill_record_outcome(
    State(state): State<Arc<AppState>>,
    headers: axum::http::HeaderMap,
    axum::extract::Path(skill_id): axum::extract::Path<String>,
    Json(body): Json<Value>,
) -> AppResult {
    let _timer = crate::metrics::HandlerTimer::new("skill_record_outcome");
    check_writable(&state)?;
    validate_skill_id(&skill_id)
        .map_err(|e| app_error(StatusCode::BAD_REQUEST, format!("INVALID_SKILL_ID: {}", e)))?;
    let success = body["success"]
        .as_bool()
        .ok_or_else(|| app_error(StatusCode::BAD_REQUEST, "missing or non-bool 'success'"))?;
    let context = body
        .get("context")
        .and_then(|v| v.as_str())
        .unwrap_or("")
        .to_string();
    let (_, engine) = resolve_engine(
        &state,
        headers.get("authorization").and_then(|v| v.to_str().ok()),
    )?;

    // Require the skill to actually exist; otherwise outcome is dangling.
    if find_skill_rid(&engine, &skill_id).is_none() {
        return Err(app_error(
            StatusCode::NOT_FOUND,
            format!("skill_id '{}' not found", skill_id),
        ));
    }

    let metadata = serde_json::json!({
        "record_type": "skill_outcome",
        "skill_ref":   skill_id,
        "success":     success,
        "context":     context,
    });
    execute_cmd(
        engine,
        Command::Remember {
            text: format!(
                "Outcome for {}: success={}{}",
                skill_id, success, context
            ),
            memory_type: "episodic".to_string(),
            importance: 0.5,
            valence: if success { 0.3 } else { -0.3 },
            half_life: 90.0 * 24.0 * 3600.0,
            metadata,
            namespace: OUTCOME_NAMESPACE.to_string(),
            certainty: 1.0,
            domain: "skill_outcome".to_string(),
            source: "skill_api".to_string(),
            emotional_state: None,
            embedding: None,
        },
        state.control.clone(),
        &state.inflight,
    )
    .await
}

/// `POST /v1/skills/{skill_id}/forget` — tombstone the skill. Optional
/// cascade_outcomes (default false) for explicit hard-delete of outcome
/// records too.
async fn skill_forget(
    State(state): State<Arc<AppState>>,
    headers: axum::http::HeaderMap,
    axum::extract::Path(skill_id): axum::extract::Path<String>,
    Json(_body): Json<Value>,
) -> AppResult {
    let _timer = crate::metrics::HandlerTimer::new("skill_forget");
    check_writable(&state)?;
    validate_skill_id(&skill_id)
        .map_err(|e| app_error(StatusCode::BAD_REQUEST, format!("INVALID_SKILL_ID: {}", e)))?;
    let (_, engine) = resolve_engine(
        &state,
        headers.get("authorization").and_then(|v| v.to_str().ok()),
    )?;

    let rid = find_skill_rid(&engine, &skill_id).ok_or_else(|| {
        app_error(
            StatusCode::NOT_FOUND,
            format!("skill_id '{}' not found", skill_id),
        )
    })?;

    execute_cmd(
        engine,
        Command::Forget { rid },
        state.control.clone(),
        &state.inflight,
    )
    .await
}

/// Helper: scan skill_substrate and find the rid whose
/// `metadata.skill_id` equals the given id. v0.8.11 stopgap; v0.8.12
/// replaces with /v1/lookup. Bounded scan limit (10000) so the worst
/// case is bounded even with thousands of skills.
fn find_skill_rid(engine: &Arc<yantrikdb::YantrikDB>, skill_id: &str) -> Option<String> {
    let (memories, _total) = engine
        .list_memories(
            10000,
            0,
            Some("skill"),
            Some("procedural"),
            Some(SKILL_NAMESPACE),
            "created_at",
        )
        .ok()?;
    for mem in memories {
        if mem.metadata.get("skill_id").and_then(|v| v.as_str()) == Some(skill_id) {
            return Some(mem.rid);
        }
    }
    None
}

/// Variant of execute_cmd that performs post-fetch filtering on Recall
/// results before returning to the client. Used by /v1/skills/search to
/// apply applies_to / skill_type filters that are post-fetch in v0.8.11
/// (v0.8.13 makes them prefilter via the where-clause work).
async fn execute_cmd_with_post_filter(
    engine: Arc<yantrikdb::YantrikDB>,
    cmd: Command,
    control: Arc<parking_lot::Mutex<crate::control::ControlDb>>,
    inflight: &std::sync::atomic::AtomicU32,
    applies_to_filter: Option<String>,
    skill_type_filter: Option<String>,
    final_top_k: usize,
) -> AppResult {
    use std::sync::atomic::Ordering;
    struct InflightGuard<'a>(&'a std::sync::atomic::AtomicU32);
    impl Drop for InflightGuard<'_> {
        fn drop(&mut self) {
            self.0.fetch_sub(1, std::sync::atomic::Ordering::Relaxed);
        }
    }
    let current = inflight.fetch_add(1, Ordering::Relaxed);
    if current >= crate::server::MAX_INFLIGHT {
        inflight.fetch_sub(1, Ordering::Relaxed);
        return Err(app_error(
            StatusCode::SERVICE_UNAVAILABLE,
            format!(
                "server overloaded: {} inflight ops (max {}). Retry later.",
                current,
                crate::server::MAX_INFLIGHT,
            ),
        ));
    }
    let _g = InflightGuard(inflight);

    let inner = tokio::task::spawn_blocking(move || {
        let db = engine.as_ref();
        handler::execute_with_guard(db, cmd, Some(control.as_ref()))
    })
    .await
    .map_err(|e| {
        app_error(
            StatusCode::INTERNAL_SERVER_ERROR,
            format!("join error: {e}"),
        )
    })?;
    let result = inner.map_err(|e| app_error(StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;

    match result {
        crate::handler::CommandResult::RecallResults { results, total: _ } => {
            // Filter by applies_to / skill_type, then truncate to top_k.
            // RecallResults are Vec<Value> (already JSON-encoded).
            let filtered: Vec<Value> = results
                .into_iter()
                .filter(|r| {
                    let metadata = r.get("metadata");
                    if let Some(ref needed) = applies_to_filter {
                        let ok = metadata
                            .and_then(|m| m.get("applies_to"))
                            .and_then(|v| v.as_array())
                            .map(|arr| arr.iter().any(|x| x.as_str() == Some(needed.as_str())))
                            .unwrap_or(false);
                        if !ok {
                            return false;
                        }
                    }
                    if let Some(ref needed) = skill_type_filter {
                        let ok = metadata
                            .and_then(|m| m.get("skill_type"))
                            .and_then(|v| v.as_str())
                            == Some(needed.as_str());
                        if !ok {
                            return false;
                        }
                    }
                    true
                })
                .take(final_top_k)
                .collect();
            let total = filtered.len();
            Ok(Json(serde_json::json!({
                "results": filtered,
                "total":   total,
            })))
        }
        other => Err(app_error(
            StatusCode::INTERNAL_SERVER_ERROR,
            format!("unexpected command result: {:?}", other),
        )),
    }
}

/// Build the Axum router.
pub fn router(state: Arc<AppState>) -> Router {
    let body_limit = state.admission.cfg.max_request_body_bytes;
    // Build the openraft sub-router up-front so we can merge it AFTER
    // the AppState-typed routes have all been chained. Order matters:
    // axum's `.merge()` unifies state types, and merging a state=()
    // router (these openraft routes set their own state via
    // `with_state(raft)`, so they expose state=() upward) before the
    // AppState routes confuses inference. Built here, merged at the
    // bottom of the chain.
    let raft_sub_router = state.raft.as_ref().map(|assembly| {
        crate::raft::raft_status_router(assembly.raft.clone())
            .merge(crate::raft::raft_receive_router(assembly.raft.clone()))
    });
    let mut app = Router::new()
        .route("/v1/health", get(health))
        .route("/v1/health/deep", get(health_deep))
        .route("/v1/remember", post(remember))
        .route("/v1/remember/batch", post(remember_batch))
        .route("/v1/recall", post(recall))
        .route("/v1/forget", post(forget))
        // RFC 022 §1 (v0.8.11): first-class skill primitives. Thin wrappers
        // over /v1/remember + /v1/recall + scan-and-filter (v0.8.11) or
        // /v1/lookup (v0.8.12+). Schema-validated, no semantic ontology.
        .route("/v1/skills/define", post(skill_define))
        .route("/v1/skills/search", post(skill_search))
        .route("/v1/skills/{skill_id}", get(skill_get))
        .route("/v1/skills/{skill_id}/outcome", post(skill_record_outcome))
        .route("/v1/skills/{skill_id}/forget", post(skill_forget))
        .route("/v1/relate", post(relate))
        .route("/v1/claim", post(ingest_claim))
        .route("/v1/claims", get(get_claims))
        .route("/v1/alias", post(add_alias))
        .route("/v1/think", post(think))
        .route("/v1/conflicts", get(conflicts))
        .route("/v1/conflicts/{id}/resolve", post(resolve_conflict))
        .route("/v1/sessions", post(session_start))
        .route("/v1/sessions/{id}", delete(session_end))
        .route("/v1/personality", get(personality))
        .route("/v1/stats", get(stats))
        .route("/v1/databases", post(create_database))
        .route("/v1/databases", get(list_databases))
        .route("/v1/cluster", get(cluster_status))
        .route("/v1/cluster/promote", post(cluster_promote))
        // v0.8.3 #24: openraft membership management
        .route("/v1/cluster/initialize", post(cluster_initialize))
        .route("/v1/cluster/add-learner", post(cluster_add_learner))
        .route("/v1/cluster/promote-voter", post(cluster_promote_voter))
        .route("/v1/cluster/remove", post(cluster_remove))
        .route("/v1/admin/control-snapshot", get(control_snapshot))
        .route("/v1/admin/snapshot", post(admin_snapshot))
        // RFC 008 Warrant Flow substrate
        .route("/v1/claim_with_lineage", post(ingest_claim_with_lineage))
        .route("/v1/mobility", get(get_mobility))
        .route("/v1/contest", get(get_contest))
        .route("/v1/move_events", post(record_move_event))
        .route("/v1/flagged_propositions", get(list_flagged))
        // RFC 010 PR-5 Jepsen / debug surface (cluster master token required)
        .route("/v1/debug/history/{tenant_id}", get(debug_history))
        .route("/v1/debug/fault/inject", post(debug_fault_inject))
        .route("/v1/debug/fault", get(debug_fault_list))
        .route("/v1/debug/fault/clear", post(debug_fault_clear))
        .route("/v1/debug/fault/{fault_id}", delete(debug_fault_remove))
        // Phase 1 polish: RFC 019 jobs admin surface (master-token gated)
        .route("/v1/jobs", get(jobs_list))
        .route("/v1/jobs/{job_id}", get(jobs_get))
        .route("/v1/jobs/{job_id}", delete(jobs_cancel))
        // Phase 1 polish: RFC 017-B migration visibility (master-token gated)
        .route("/v1/admin/migrations", get(admin_migrations_list))
        .route("/metrics", get(metrics));
    // Apply layer + state, then merge the openraft sub-router (which
    // already binds its own State<Arc<Raft>>).
    let mut app = app
        // RFC 009 §4 Layer 3: hard request body size cap. Bodies above
        // `admission.max_request_body_bytes` get HTTP 413 from this layer
        // before any handler runs. Defends against memory-blow attacks
        // and misconfigured clients.
        .layer(tower_http::limit::RequestBodyLimitLayer::new(body_limit))
        .with_state(state);
    if let Some(raft_router) = raft_sub_router {
        app = app.merge(raft_router);
    }
    app
}

#[cfg(test)]
mod skill_validation_tests {
    use super::{validate_applies_to_entry, validate_skill_id};

    // ── validate_skill_id ──────────────────────────────────

    #[test]
    fn skill_id_valid_minimal_form() {
        // Minimum: starts with lowercase, has one dot, length >= 4.
        assert!(validate_skill_id("a.b").is_err()); // length < 4
        assert!(validate_skill_id("ab.c").is_ok()); // length 4 = boundary
        assert!(validate_skill_id("skill.foo.v1").is_ok());
        assert!(validate_skill_id("skill.invoice.validation.v3").is_ok());
    }

    #[test]
    fn skill_id_rejects_uppercase() {
        assert!(validate_skill_id("Skill.foo").is_err());
        assert!(validate_skill_id("skill.Foo").is_err());
        assert!(validate_skill_id("SKILL.FOO").is_err());
    }

    #[test]
    fn skill_id_rejects_no_dot() {
        // Must contain at least one '.' (dotted form requirement).
        let err = validate_skill_id("skillfoo").unwrap_err();
        assert!(err.contains("at least one '.'"));
    }

    #[test]
    fn skill_id_rejects_consecutive_dots() {
        assert!(validate_skill_id("skill..foo").is_err());
    }

    #[test]
    fn skill_id_rejects_trailing_dot() {
        assert!(validate_skill_id("skill.foo.").is_err());
    }

    #[test]
    fn skill_id_rejects_starts_with_digit_or_underscore_or_dot() {
        assert!(validate_skill_id("1skill.foo").is_err());
        assert!(validate_skill_id("_skill.foo").is_err());
        assert!(validate_skill_id(".skill.foo").is_err());
    }

    #[test]
    fn skill_id_rejects_invalid_chars() {
        assert!(validate_skill_id("skill-foo.v1").is_err()); // hyphen
        assert!(validate_skill_id("skill foo.v1").is_err()); // space
        assert!(validate_skill_id("skill/foo.v1").is_err()); // slash
        assert!(validate_skill_id("skill@foo.v1").is_err()); // at-sign
    }

    #[test]
    fn skill_id_length_bounds() {
        // Lower bound: 4 chars.
        assert!(validate_skill_id("a.bc").is_ok());
        assert!(validate_skill_id("a.b").is_err());
        // Upper bound: 200 chars.
        let long_ok = format!("skill.{}", "a".repeat(193)); // 6 + 193 = 199
        assert!(validate_skill_id(&long_ok).is_ok());
        let long_err = format!("skill.{}", "a".repeat(195)); // 6 + 195 = 201
        assert!(validate_skill_id(&long_err).is_err());
    }

    #[test]
    fn skill_id_allows_underscores_and_digits_in_segments() {
        assert!(validate_skill_id("skill_42.foo_bar.v1_2").is_ok());
        assert!(validate_skill_id("a1.b2").is_ok());
    }

    // ── validate_applies_to_entry ──────────────────────────

    #[test]
    fn applies_to_entry_valid() {
        assert!(validate_applies_to_entry("invoice").is_ok());
        assert!(validate_applies_to_entry("meta_agent").is_ok());
        assert!(validate_applies_to_entry("a").is_ok());
        assert!(validate_applies_to_entry("a1").is_ok());
        assert!(validate_applies_to_entry("invoice_validation_2026").is_ok());
    }

    #[test]
    fn applies_to_entry_rejects_hyphen() {
        // The whole point of this regex: catch the hyphen-vs-underscore
        // drift bug Brainstorm 2 named. `meta-agent` (hyphen) and
        // `meta_agent` (underscore) are both valid Rust strings, but only
        // the underscore form is accepted as an applies_to entry. This
        // forces consistency at write time.
        let err = validate_applies_to_entry("meta-agent").unwrap_err();
        assert!(err.contains("invalid character"));
    }

    #[test]
    fn applies_to_entry_rejects_uppercase() {
        assert!(validate_applies_to_entry("Invoice").is_err());
        assert!(validate_applies_to_entry("INVOICE").is_err());
    }

    #[test]
    fn applies_to_entry_rejects_dot_or_slash() {
        // Unlike skill_id, applies_to entries are flat (no dots).
        assert!(validate_applies_to_entry("invoice.validation").is_err());
        assert!(validate_applies_to_entry("invoice/validation").is_err());
    }

    #[test]
    fn applies_to_entry_rejects_empty() {
        assert!(validate_applies_to_entry("").is_err());
    }

    #[test]
    fn applies_to_entry_rejects_starts_with_digit_or_underscore() {
        assert!(validate_applies_to_entry("1invoice").is_err());
        assert!(validate_applies_to_entry("_invoice").is_err());
    }
}

/// PR 6.6 — HTTP error-mapping conformance tests.
///
/// Pin every `CommitError` variant's status code + body shape so client
/// SDKs can build retry / redirect / error-classification logic against
/// a stable contract.
#[cfg(test)]
mod commit_error_mapping_tests {
    use super::commit_error_to_app_error;
    use crate::commit::{CommitError, OpId, TenantId};
    use axum::http::StatusCode;

    #[test]
    fn not_leader_maps_to_307_with_leader_info_in_body() {
        let (status, body) = commit_error_to_app_error(CommitError::NotLeader {
            leader_id: Some(4),
            leader_addr: Some("https://192.168.4.140:7438".into()),
        });
        assert_eq!(status, StatusCode::TEMPORARY_REDIRECT);
        let v = body.0;
        assert_eq!(v["error"], "not_leader");
        assert_eq!(v["leader_id"], 4);
        assert_eq!(v["leader_addr"], "https://192.168.4.140:7438");
    }

    #[test]
    fn not_leader_with_unknown_leader_emits_nulls() {
        // Mid-election: openraft reports ForwardToLeader with no known
        // leader. Client SHOULD interpret this as a 503-shape signal
        // even though the status is 307.
        let (status, body) = commit_error_to_app_error(CommitError::NotLeader {
            leader_id: None,
            leader_addr: None,
        });
        assert_eq!(status, StatusCode::TEMPORARY_REDIRECT);
        assert!(body.0["leader_id"].is_null());
        assert!(body.0["leader_addr"].is_null());
    }

    #[test]
    fn op_id_collision_maps_to_409_with_existing_index() {
        let op = OpId::new_random();
        let (status, body) = commit_error_to_app_error(CommitError::OpIdCollision {
            op_id: op,
            tenant_id: TenantId::new(7),
            existing_index: 42,
        });
        assert_eq!(status, StatusCode::CONFLICT);
        let v = body.0;
        assert_eq!(v["error"], "op_id_collision");
        assert_eq!(v["op_id"], op.to_string());
        assert_eq!(v["tenant_id"], 7);
        assert_eq!(v["existing_index"], 42);
    }

    #[test]
    fn unexpected_log_index_maps_to_409_with_expected_actual() {
        let (status, body) = commit_error_to_app_error(CommitError::UnexpectedLogIndex {
            tenant_id: TenantId::new(1),
            expected: 5,
            actual: 7,
        });
        assert_eq!(status, StatusCode::CONFLICT);
        let v = body.0;
        assert_eq!(v["error"], "unexpected_log_index");
        assert_eq!(v["expected"], 5);
        assert_eq!(v["actual"], 7);
    }

    #[test]
    fn not_yet_implemented_maps_to_501_with_planned_rfc() {
        let (status, body) = commit_error_to_app_error(CommitError::NotYetImplemented {
            variant: "PurgeMemory",
            planned_rfc: "011",
        });
        assert_eq!(status, StatusCode::NOT_IMPLEMENTED);
        let v = body.0;
        assert_eq!(v["error"], "not_implemented");
        assert_eq!(v["variant"], "PurgeMemory");
        assert_eq!(v["planned_rfc"], "011");
    }

    #[test]
    fn storage_failure_maps_to_503_with_retry_after() {
        let (status, body) = commit_error_to_app_error(CommitError::StorageFailure {
            message: "disk full".into(),
        });
        assert_eq!(status, StatusCode::SERVICE_UNAVAILABLE);
        let v = body.0;
        assert_eq!(v["error"], "storage_failure");
        assert_eq!(v["detail"], "disk full");
        assert_eq!(v["retry_after_ms"], 1000);
    }

    #[test]
    fn shutdown_maps_to_503_with_longer_retry_after() {
        // Shutdown means "this node is going down — try a peer." Longer
        // retry_after_ms hints "don't hammer this address."
        let (status, body) = commit_error_to_app_error(CommitError::Shutdown);
        assert_eq!(status, StatusCode::SERVICE_UNAVAILABLE);
        assert_eq!(body.0["error"], "shutting_down");
        assert_eq!(body.0["retry_after_ms"], 5000);
    }

    #[test]
    fn commit_timeout_maps_to_503_with_op_id_for_idempotent_retry() {
        // The load-bearing PR 6.6 invariant: timeout responses carry
        // the op_id so client retries are idempotent. Without this,
        // network-partition recovery duplicates writes.
        let op = OpId::new_random();
        let (status, body) = commit_error_to_app_error(CommitError::CommitTimeout { op_id: op });
        assert_eq!(status, StatusCode::SERVICE_UNAVAILABLE);
        let v = body.0;
        assert_eq!(v["error"], "commit_timeout");
        assert_eq!(v["op_id"], op.to_string());
        assert_eq!(v["retry_after_ms"], 1000);
    }

    // ── PR 6.9 — replication-lag derivation tests ─────────────────
    //
    // The value computation lives inside `cluster_state_view`, which
    // holds an `AppState` reference and isn't easy to mock. The lag
    // arithmetic is the only piece that has correctness implications
    // for clients reading the field; the rest is field shuffling. Pin
    // it as a free function that mirrors the logic.

    fn replication_lag(
        last_log_index: Option<u64>,
        last_applied_index: Option<u64>,
    ) -> Option<u64> {
        match (last_log_index, last_applied_index) {
            (Some(log), Some(applied)) => Some(log.saturating_sub(applied)),
            (Some(log), None) => Some(log),
            (None, _) => None,
        }
    }

    #[test]
    fn pr_6_9_lag_is_zero_when_log_and_applied_are_equal() {
        // Healthy follower (or leader): everything received has been
        // applied. The number clients read should be exactly 0, not
        // null — the data was observed.
        assert_eq!(replication_lag(Some(18), Some(18)), Some(0));
    }

    #[test]
    fn pr_6_9_lag_reflects_unapplied_entries() {
        // 5 entries received but not yet applied. This is the value
        // a Grafana alert / health probe consumes to detect a stuck
        // apply path.
        assert_eq!(replication_lag(Some(100), Some(95)), Some(5));
    }

    #[test]
    fn pr_6_9_lag_clamps_at_zero_under_inversion() {
        // Race: the metric snapshot may briefly observe applied > log_index
        // (e.g. during membership change or snapshot install). Clients
        // would treat a giant negative-flipped-to-u64 as "lag of 18
        // exabytes" and page the operator. saturating_sub clamps at 0.
        assert_eq!(replication_lag(Some(10), Some(15)), Some(0));
    }

    #[test]
    fn pr_6_9_lag_is_log_index_when_nothing_applied_yet() {
        // Cold-start follower: log entries received but state machine
        // hasn't applied any yet. Lag = entire log.
        assert_eq!(replication_lag(Some(7), None), Some(7));
    }

    #[test]
    fn pr_6_9_lag_is_none_when_no_log_index_known() {
        // Pre-bootstrap or single-node: no openraft, no metrics.
        // Field reads as JSON null, distinguishable from an actual 0.
        assert_eq!(replication_lag(None, None), None);
        assert_eq!(replication_lag(None, Some(5)), None);
    }

    #[test]
    fn version_mismatch_maps_to_426_upgrade_required() {
        // 426 is the canonical HTTP status for "upgrade required" —
        // tells the client (or its operator) the cluster is rolling
        // through a wire-version transition and this peer is behind.
        let verr = crate::version::VersionError::WireMajorMismatch {
            node: crate::version::WireVersion::new(1, 0),
            event: crate::version::WireVersion::new(2, 0),
        };
        let (status, body) = commit_error_to_app_error(CommitError::Version(verr));
        assert_eq!(status, StatusCode::UPGRADE_REQUIRED);
        assert_eq!(body.0["error"], "wire_version_mismatch");
    }

    #[test]
    fn every_variant_produces_a_response() {
        // Belt-and-suspenders: a future maintainer who adds a new
        // CommitError variant must update commit_error_to_app_error.
        // The match is exhaustive at compile time, but we also assert
        // here that every existing variant produces a valid status code
        // (not zero, not panic).
        let cases = vec![
            CommitError::NotLeader {
                leader_id: None,
                leader_addr: None,
            },
            CommitError::OpIdCollision {
                op_id: OpId::new_random(),
                tenant_id: TenantId::new(1),
                existing_index: 0,
            },
            CommitError::UnexpectedLogIndex {
                tenant_id: TenantId::new(1),
                expected: 1,
                actual: 2,
            },
            CommitError::NotYetImplemented {
                variant: "X",
                planned_rfc: "Y",
            },
            CommitError::StorageFailure {
                message: "x".into(),
            },
            CommitError::Shutdown,
            CommitError::CommitTimeout {
                op_id: OpId::new_random(),
            },
        ];
        for err in cases {
            let label = err.metric_label();
            let (status, body) = commit_error_to_app_error(err);
            // 307 (NotLeader redirect) is the only 3xx case; everything
            // else is 4xx or 5xx. No CommitError should map to 1xx/2xx.
            let s = status.as_u16();
            assert!(
                s == 307 || s >= 400,
                "{label} unexpected status {s} (want 307 or 4xx/5xx)"
            );
            assert!(
                body.0.get("error").is_some(),
                "{label} body must include `error` key"
            );
        }
    }
}