memstead-mcp 0.7.0

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

use std::path::PathBuf;
use std::sync::{Arc, Mutex, OnceLock};

use rmcp::handler::server::wrapper::Parameters;
use rmcp::model::{
    CallToolRequestParams, CallToolResult, ContentBlock, InitializeRequestParams, InitializeResult,
    ListToolsResult, PaginatedRequestParams, Tool,
};
use rmcp::service::RequestContext;
use rmcp::{ErrorData as McpError, RoleServer, ServerHandler, tool, tool_handler, tool_router};

use indexmap::IndexMap;

use memstead_base::EntityId;
use memstead_base::ops::SearchScope;
use memstead_base::render::{render_entity_markdown, render_search_markdown};
use memstead_base::vcs::{Actor, ClientId};
use memstead_base::{
    BootError, CreateEntityArgs, DeleteEntityArgs, Engine, EngineError, RelateAction,
    RelateEntityArgs, RenameEntityArgs, UpdateEntityArgs,
};
use std::path::Path;

use crate::tools::admin::{ChangesSinceParams, DiffParams, HealthParams};
use crate::tools::graph::{EntityParams, OverviewParams, SchemaParams, SearchParams};
use crate::tools::mutation::{
    CheckParams, CreateParams, DeleteParams, RelateParams, RenameParams, UpdateParams,
};

/// MCP server backed by the unified [`memstead_base::Engine`].
///
/// Constructed via [`Self::from_workspace_root`].
#[derive(Clone)]
pub struct FilesystemMcpServer {
    /// Persistent unified engine. Mutations invalidate its memo
    /// caches via the engine's own hooks; reads see fresh state on
    /// every lock without needing a re-init from disk.
    engine: Arc<Mutex<Engine>>,
    /// Workspace root captured at construction time. Used by
    /// `memstead_changes_since` (which still reads JSONL directly off
    /// disk) — the unified engine does not expose a workspace_root
    /// accessor because mounts can be heterogeneous.
    workspace_root: PathBuf,
    /// Captured `clientInfo` from the initialize handshake. Used to
    /// stamp the changelog `client` field on every mutation. Same
    /// `OnceLock` shape as `crate::server::McpServer::client`.
    client: Arc<OnceLock<ClientId>>,
}

impl FilesystemMcpServer {
    /// Construct from a workspace root. Boots the unified
    /// [`Engine`] via [`Engine::from_workspace_root`] (lean path —
    /// folder + archive backends only). The error envelope wraps
    /// every layer (layout dispatch, store load, backend
    /// instantiation, engine construction) under one [`BootError`].
    ///
    /// Production callers (main.rs, every test fixture) reach the
    /// server through this constructor directly.
    pub fn from_workspace_root(workspace_root: &Path) -> Result<Self, BootError> {
        let workspace_root = workspace_root.to_path_buf();
        let engine = Engine::from_workspace_root(&workspace_root)?;
        Ok(Self {
            engine: Arc::new(Mutex::new(engine)),
            workspace_root,
            client: Arc::new(OnceLock::new()),
        })
    }

    /// Construct directly from a pre-built [`Engine`] — e.g. a sealed
    /// read-only archive mount stood up by an embedding service. `workspace_root`
    /// is consulted only by `memstead_changes_since` (which reads JSONL off
    /// disk); surfaces that do not expose that tool may pass any path.
    pub fn from_engine(engine: Engine, workspace_root: PathBuf) -> Self {
        Self {
            engine: Arc::new(Mutex::new(engine)),
            workspace_root,
            client: Arc::new(OnceLock::new()),
        }
    }

    /// Export the server's single mem as `.mem` archive bytes. Used by
    /// embedding services (the session server) to hand a visitor a
    /// self-describing copy of the mem their agent built. The server's
    /// engine carries exactly one mem (filesystem / session mems are
    /// single-mem by design); this exports it.
    pub fn export_mem_to_bytes(&self) -> Result<Vec<u8>, memstead_base::EngineError> {
        let engine = self
            .engine
            .lock()
            .expect("filesystem MCP engine mutex poisoned");
        let mem = engine
            .mem_names()
            .into_iter()
            .next()
            .map(String::from)
            .ok_or_else(|| {
                memstead_base::EngineError::InvalidInput("no mem to export".to_string())
            })?;
        engine.export_mem_to_bytes(&mem)
    }

    /// Count of real (non-stub) entities across the server's mem. Used
    /// by embedding services (the session server) to enforce a
    /// per-session resource cap before admitting a create.
    pub fn entity_count(&self) -> usize {
        self.engine
            .lock()
            .expect("filesystem MCP engine mutex poisoned")
            .status()
            .entity_count
    }

    /// Run a read closure against the locked engine. The escape hatch for
    /// embedding services that need engine reads the tool surface does not
    /// expose — e.g. the session server's live graph projection and its
    /// change-event subscription. Keeps the engine itself private; callers
    /// get a borrow only for the duration of `f`.
    pub fn with_engine<R>(&self, f: impl FnOnce(&Engine) -> R) -> R {
        let engine = self
            .engine
            .lock()
            .expect("filesystem MCP engine mutex poisoned");
        f(&engine)
    }

    fn actor_and_client(&self) -> (Actor, Option<ClientId>) {
        match self.client.get() {
            Some(c) => (Actor::Agent, Some(c.clone())),
            None => (Actor::Agent, None),
        }
    }
}

/// Build a typed tool-error envelope. The text channel is
/// `ERROR [<CODE>]: <message>` (consumers reading only
/// `result.content[0].text` recover the code with one regex) and the
/// `structured_content` channel carries `{code, message}` so agents
/// branching on the structured shape get the typed code without parsing
/// text. Mirror of `crate::error_envelope::tool_error_with_payload`'s
/// payload-less shape — the per-flavour symmetry is what makes the
/// wire-byte contract uniform across lean and full. Pre-fix the text
/// channel emitted a JSON-stringified `{code, message}` payload; that
/// form parsed for machine consumers but missed the documented
/// prefix-form contract.
fn tool_error(code: &str, message: &str) -> CallToolResult {
    tool_error_with_details(code, message, None)
}

/// Same as [`tool_error`] but additionally embeds a structured `details`
/// payload under `structured_content.details`. Text channel format is
/// identical (`ERROR [<CODE>]: <message>`); recovery payloads (current
/// hash, declared sections, referrer list) live exclusively on the
/// structured channel.
fn tool_error_with_details(
    code: &str,
    message: &str,
    details: Option<serde_json::Value>,
) -> CallToolResult {
    let payload = match details {
        Some(d) => serde_json::json!({ "code": code, "message": message, "details": d }),
        None => serde_json::json!({ "code": code, "message": message }),
    };
    let text = format!("ERROR [{code}]: {message}");
    let mut result = CallToolResult::error(vec![ContentBlock::text(text)]);
    result.structured_content = Some(payload);
    result
}

fn md_response(markdown: String) -> CallToolResult {
    CallToolResult::success(vec![ContentBlock::text(markdown)])
}

/// Pair rendered markdown on the text channel with a structured
/// envelope on `structured_content`:
/// tools whose response has a canonical human-readable form (entity,
/// search) ship the markdown to terminal/inline consumers and the
/// typed JSON to branching agents in one call.
fn md_with_structured(markdown: String, structured: serde_json::Value) -> CallToolResult {
    let mut result = CallToolResult::success(vec![ContentBlock::text(markdown)]);
    result.structured_content = Some(structured);
    result
}

fn json_response<T: serde::Serialize>(data: &T) -> CallToolResult {
    let value = serde_json::to_value(data).unwrap_or(serde_json::Value::Null);
    let text = serde_json::to_string_pretty(&value).unwrap_or_default();
    let mut result = CallToolResult::success(vec![ContentBlock::text(text)]);
    result.structured_content = Some(value);
    result
}

/// Whether the named mem's storage is durable (persists past restart /
/// session-TTL eviction), derived from its mount's `MountStorage` kind.
/// On the ephemeral in-memory sketch this returns `false` — the per-write
/// `durable` echo on every mutation response is how an agent learns its
/// `commit_sha` denotes nothing durable. Defaults to `false` for an
/// unresolvable mem: the engine never claims a durability it can't vouch
/// for.
fn mem_is_durable(engine: &memstead_base::Engine, mem: &str) -> bool {
    engine
        .mounts()
        .iter()
        .find(|m| m.mem == mem)
        .map(|m| m.storage.is_durable())
        .unwrap_or(false)
}

/// Refuse params the filesystem-mem MCP surface does not honour rather
/// than silently dropping them (Plan 03, Part B). Each `(name, meaningful)`
/// pair flags a param this surface hardwires off; `meaningful` is true when
/// the caller passed it with an effect they expect (a non-empty map/list, or
/// `dry_run: true`). When any such param was meaningfully supplied the call
/// refuses UP FRONT — before any mutation — with `UNSUPPORTED_PARAM` naming
/// every dropped param in `details.params`, so an agent can never believe a
/// no-op succeeded (the worst case being `dry_run: true`, which would
/// otherwise commit a real write the agent thought was a preview). A
/// defaulted-empty / absent / `false` param is left alone — the caller
/// intended no effect, so the call proceeds unchanged (backward-compatible).
/// Returns `None` when nothing meaningful was dropped.
/// Resolve a per-call `role` parameter (agent-trust plan 13) on the
/// lean flavour. No session default here (the lean binary carries no
/// `--role`); absent records unspecified. Unknown values refuse typed
/// with the declarable vocabulary named — same contract as the full
/// flavour.
fn resolve_role_lean(raw: Option<&str>) -> Result<memstead_base::vcs::Role, Box<CallToolResult>> {
    match raw {
        None => Ok(memstead_base::vcs::Role::Unspecified),
        Some(s) => memstead_base::vcs::Role::from_wire(s).ok_or_else(|| {
            let msg = format!(
                "unknown role {s:?} — declarable roles: {}",
                memstead_base::vcs::Role::DECLARABLE.join(", ")
            );
            Box::new(tool_error_with_details(
                "INVALID_ROLE",
                &msg,
                Some(serde_json::json!({
                    "role": s,
                    "allowed": memstead_base::vcs::Role::DECLARABLE,
                })),
            ))
        }),
    }
}

fn reject_unsupported_params(params: &[(&str, bool)]) -> Option<CallToolResult> {
    let dropped: Vec<&str> = params
        .iter()
        .filter_map(|(name, meaningful)| meaningful.then_some(*name))
        .collect();
    if dropped.is_empty() {
        return None;
    }
    let msg = format!(
        "the filesystem-mem surface does not implement: {}. These params were \
         refused, not silently ignored — pass them only to the unified engine \
         (mem-repo MCP / CLI), or omit them.",
        dropped.join(", ")
    );
    Some(tool_error_with_details(
        "UNSUPPORTED_PARAM",
        &msg,
        Some(serde_json::json!({ "params": dropped })),
    ))
}

/// Map an [`EngineError`] to an MCP error envelope. Codes match the
/// mem-repo error-code vocabulary (`HASH_MISMATCH`, `ENTITY_NOT_FOUND`,
/// `ENTITY_ALREADY_EXISTS`, etc.) so agents that handle mem-repo
/// errors get the same shape here.
///
/// Variants that should never trip on a single-mem filesystem-mem
/// boot path (`DuplicateMem`, `UnknownMem`, `ReadOnlyMount`) still
/// fall into a generic `INTERNAL` envelope so the wire shape is total —
/// a future bug that produced one of those wouldn't crash the handler.
/// Schema-resolution failures (`SchemaNotFound`, `SchemaResolverInit`)
/// surface as their own typed codes via [`EngineError::code()`] so
/// callers see the same wire contract here as on the mem-repo server.
fn engine_op_error(err: EngineError) -> CallToolResult {
    // Pre-compute the canonical Display string. Variants that take the
    // engine's Display rendering verbatim use this — variants that build
    // their own customised message (e.g. stub-aware `HashMismatch`) keep
    // doing so in-arm.
    let display = err.to_string();
    match err {
        EngineError::UnknownType {
            name,
            schema_ref,
            declared,
            suggestion,
        } => {
            let hint = suggestion
                .as_deref()
                .map(|s| format!(". Did you mean '{s}'?"))
                .unwrap_or_default();
            tool_error(
                "UNKNOWN_ENTITY_TYPE",
                &format!(
                    "unknown entity type '{name}' in schema '{schema_ref}'. \
                     Declared types: [{}]{hint}",
                    declared.join(", ")
                ),
            )
        }
        EngineError::InvalidTitle(slug_err) => {
            use memstead_base::SlugError;
            let reason = slug_err.reason();
            let details = match &slug_err {
                SlugError::IdTooLong { input, length, max } => serde_json::json!({
                    "reason": reason,
                    "input": input,
                    "length": length,
                    "max": max,
                }),
                SlugError::TitleEmpty { input } => serde_json::json!({
                    "reason": reason,
                    "input": input,
                }),
                SlugError::TitleHasControlChars {
                    input,
                    control_chars,
                    proposed_slug,
                } => {
                    let control_chars_str: Vec<String> = control_chars
                        .iter()
                        .map(|c| c.escape_default().to_string())
                        .collect();
                    serde_json::json!({
                        "reason": reason,
                        "input": input,
                        "control_chars": control_chars_str,
                        "proposed_slug": proposed_slug,
                    })
                }
            };
            tool_error_with_details(
                "INVALID_TITLE",
                &format!("title is invalid: {slug_err}"),
                Some(details),
            )
        }
        e @ EngineError::AlreadyExists { .. } => tool_error_with_details(
            "ENTITY_ALREADY_EXISTS",
            // Display names the occupying title; ship the structured
            // payload too so the lean server matches the full wire.
            &e.to_string(),
            Some(e.details()),
        ),
        // Block-tier declared-constraint refusals — code and recovery
        // payload come from the error itself so the lean server ships
        // the same wire contract as the full server.
        e @ (EngineError::ConstraintUnsatisfied { .. }
        | EngineError::RequiredOutgoingUnsatisfied { .. }
        | EngineError::SectionFormatRefused { .. }) => {
            tool_error_with_details(e.code(), &display, Some(e.details()))
        }
        EngineError::NotFound { id } => {
            tool_error("ENTITY_NOT_FOUND", &format!("entity not found: {id}"))
        }
        EngineError::HashMismatch {
            id,
            current,
            is_stub,
        } => {
            // Stub-aware message — pre-fix code printed `current is `
            // with an empty trailing value when the entity was a stub
            // and misdirected toward hash-recovery. Surface
            // `details.is_stub` and a corrective-action message
            // ("pass expected_hash: \"\"") for stubs; the prior
            // contract holds for real entities.
            let message = if is_stub {
                format!(
                    "hash mismatch for {id} — entity is a stub (no content_hash); pass expected_hash: \"\" to operate on stubs"
                )
            } else {
                format!("hash mismatch for {id}: current is {current}")
            };
            let payload = serde_json::json!({
                "code": "HASH_MISMATCH",
                "message": message.clone(),
                "details": {
                    "id": id,
                    "current": current,
                    "is_stub": is_stub,
                },
            });
            // Same text-channel format as `tool_error_with_payload`:
            // `ERROR [<CODE>]: <message>`. Pre-Item-01 this site emitted
            // a JSON-stringified payload on the text channel.
            let text = format!("ERROR [HASH_MISMATCH]: {message}");
            let mut result = CallToolResult::error(vec![ContentBlock::text(text)]);
            result.structured_content = Some(payload);
            result
        }
        EngineError::HasIncomingRefs { id, referrers } => {
            let referrers_json: Vec<_> = referrers
                .iter()
                .map(|r| {
                    serde_json::json!({
                        "from_id": r.from_id,
                        "rel_types": r.rel_types,
                        "mem": r.mem,
                        "capability": "write",
                    })
                })
                .collect();
            let message = display;
            let payload = serde_json::json!({
                "code": "HAS_INCOMING_REFS",
                "message": message.clone(),
                "details": { "id": id, "referrers": referrers_json },
            });
            let text = format!("ERROR [HAS_INCOMING_REFS]: {message}");
            let mut result = CallToolResult::error(vec![ContentBlock::text(text)]);
            result.structured_content = Some(payload);
            result
        }
        EngineError::MemHasIncomingRefs { mem, referrers } => {
            // Single-mem filesystem boot path never produces
            // MemHasIncomingRefs in practice — mem-delete is a
            // full-only operation. The arm is here for exhaustiveness;
            // the envelope shape matches the full-side mapping so
            // wire-byte parity holds if the filesystem flavour ever
            // gains a mem-delete surface.
            let referrers_json: Vec<_> = referrers
                .iter()
                .map(|r| {
                    serde_json::json!({
                        "from_id": r.from_id,
                        "rel_types": r.rel_types,
                        "mem": r.mem,
                    })
                })
                .collect();
            let message = display;
            let payload = serde_json::json!({
                "code": "MEM_HAS_INCOMING_REFS",
                "message": message.clone(),
                "details": { "mem": mem, "referrers": referrers_json },
            });
            let text = format!("ERROR [MEM_HAS_INCOMING_REFS]: {message}");
            let mut result = CallToolResult::error(vec![ContentBlock::text(text)]);
            result.structured_content = Some(payload);
            result
        }
        EngineError::CrossMemLinkNotAllowed { from_mem, to_mem } => tool_error(
            "CROSS_MEM_LINK_NOT_ALLOWED",
            &format!(
                "cross-mem link from `{from_mem}` to `{to_mem}` is not allowed by the workspace `[cross_mem_links]` policy"
            ),
        ),
        EngineError::CrossMemTargetNotFound {
            target_id,
            target_mem,
        } => tool_error(
            "CROSS_MEM_TARGET_NOT_FOUND",
            &format!(
                "cross-mem target `{target_id}` is absent in read-only mem `{target_mem}` — auto-stub is unavailable across the read-only boundary"
            ),
        ),
        EngineError::CrossMemEdgeNotDeclared {
            source_schema,
            target_schema,
            rel_type,
            from_id,
            to_id,
        } => {
            let message = display;
            let payload = serde_json::json!({
                "code": "CROSS_MEM_EDGE_NOT_DECLARED",
                "message": message.clone(),
                "details": {
                    "source_schema": source_schema,
                    "target_schema": target_schema,
                    "rel_type": rel_type,
                    "from_id": from_id,
                    "to_id": to_id,
                },
            });
            let text = format!("ERROR [CROSS_MEM_EDGE_NOT_DECLARED]: {message}");
            let mut result = CallToolResult::error(vec![ContentBlock::text(text)]);
            result.structured_content = Some(payload);
            result
        }
        EngineError::RepairNotNeeded { id, recovery } => {
            let message = display;
            let payload = serde_json::json!({
                "code": "REPAIR_NOT_NEEDED",
                "message": message.clone(),
                "details": { "id": id, "recovery": recovery },
            });
            let text = format!("ERROR [REPAIR_NOT_NEEDED]: {message}");
            let mut result = CallToolResult::error(vec![ContentBlock::text(text)]);
            result.structured_content = Some(payload);
            result
        }
        EngineError::RenameNoOp { id, new_title } => tool_error(
            "RENAME_NO_OP",
            &format!(
                "rename would not change the id of {id} — new title {new_title:?} produces the same slug"
            ),
        ),
        EngineError::WikiLinkWithoutRelation { from_id, missing } => {
            let message = display;
            let payload = serde_json::json!({
                "code": "WIKILINK_WITHOUT_RELATION",
                "message": message.clone(),
                "details": {
                    "from_id": from_id,
                    "missing": missing,
                },
            });
            let text = format!("ERROR [WIKILINK_WITHOUT_RELATION]: {message}");
            let mut result = CallToolResult::error(vec![ContentBlock::text(text)]);
            result.structured_content = Some(payload);
            result
        }
        EngineError::RelationHasBodyLinks {
            from_id,
            to_id,
            rel_type,
            body_links,
        } => {
            let message = display;
            let payload = serde_json::json!({
                "code": "RELATION_HAS_BODY_LINKS",
                "message": message.clone(),
                "details": {
                    "from_id": from_id,
                    "to_id": to_id,
                    "rel_type": rel_type,
                    "body_links": body_links,
                },
            });
            let text = format!("ERROR [RELATION_HAS_BODY_LINKS]: {message}");
            let mut result = CallToolResult::error(vec![ContentBlock::text(text)]);
            result.structured_content = Some(payload);
            result
        }
        EngineError::RenamePartialFailure {
            committed_mems,
            failed_mem,
            failure_cause,
        } => {
            let message = format!(
                "rename partial-failure: mem `{failed_mem}` aborted with cause {failure_cause:?} after {committed_mems:?} already committed — reload and retry, or reconcile manually"
            );
            let payload = serde_json::json!({
                "code": "RENAME_PARTIAL_FAILURE",
                "message": message.clone(),
                "details": {
                    "committed_mems": committed_mems,
                    "failed_mem": failed_mem,
                    "failure_cause": failure_cause,
                },
            });
            let text = format!("ERROR [RENAME_PARTIAL_FAILURE]: {message}");
            let mut result = CallToolResult::error(vec![ContentBlock::text(text)]);
            result.structured_content = Some(payload);
            result
        }
        EngineError::RenameBlockedByCrossMemPolicy {
            ref from_mem,
            ref blocked_referrers,
        } => {
            let message = err.to_string();
            let entries: Vec<_> = blocked_referrers
                .iter()
                .map(|r| {
                    serde_json::json!({
                        "from_mem": r.from_mem,
                        "to_mem": r.to_mem,
                        "count": r.count,
                    })
                })
                .collect();
            let payload = serde_json::json!({
                "code": "RENAME_BLOCKED_BY_CROSS_MEM_POLICY",
                "message": message.clone(),
                "details": {
                    "from_mem": from_mem,
                    "blocked_referrers": entries,
                },
            });
            let text = format!("ERROR [RENAME_BLOCKED_BY_CROSS_MEM_POLICY]: {message}");
            let mut result = CallToolResult::error(vec![ContentBlock::text(text)]);
            result.structured_content = Some(payload);
            result
        }
        EngineError::StubCannotRelate { id } => {
            let message = format!(
                "source entity {id} is a stub — promote it to a real entity via memstead_create first"
            );
            let payload = serde_json::json!({
                "code": "STUB_CANNOT_RELATE",
                "message": message.clone(),
                "details": { "id": id },
            });
            let text = format!("ERROR [STUB_CANNOT_RELATE]: {message}");
            let mut result = CallToolResult::error(vec![ContentBlock::text(text)]);
            result.structured_content = Some(payload);
            result
        }
        EngineError::StubNotUpdatable { id } => {
            let message = format!(
                "entity {id} is a stub — promote it to a real entity via memstead_create first"
            );
            let payload = serde_json::json!({
                "code": "STUB_NOT_UPDATABLE",
                "message": message.clone(),
                "details": { "id": id },
            });
            let text = format!("ERROR [STUB_NOT_UPDATABLE]: {message}");
            let mut result = CallToolResult::error(vec![ContentBlock::text(text)]);
            result.structured_content = Some(payload);
            result
        }
        EngineError::StubNotRenamable { id } => {
            let message = format!(
                "entity {id} is a stub — promote it to a real entity via memstead_create before renaming"
            );
            let payload = serde_json::json!({
                "code": "STUB_NOT_RENAMABLE",
                "message": message.clone(),
                "details": { "id": id },
            });
            let text = format!("ERROR [STUB_NOT_RENAMABLE]: {message}");
            let mut result = CallToolResult::error(vec![ContentBlock::text(text)]);
            result.structured_content = Some(payload);
            result
        }
        EngineError::InvalidEntityId { id, reason } => {
            let message = format!("entity id '{id}' is malformed: {reason}");
            tool_error_with_details(
                "INVALID_ENTITY_ID",
                &message,
                Some(serde_json::json!({ "id": id, "reason": reason })),
            )
        }
        EngineError::InvalidWikiLinkTarget {
            raw,
            suggested,
            section,
            link_source,
            reason,
        } => {
            let message = format!(
                "body wiki-link target '{raw}' in section '{section}' is not slug-form: {reason}"
            );
            tool_error_with_details(
                "INVALID_WIKI_LINK_TARGET",
                &message,
                Some(serde_json::json!({
                    "raw": raw,
                    "suggested": suggested,
                    "section": section,
                    "source": link_source,
                    "reason": reason,
                })),
            )
        }
        EngineError::InvalidWikiLinkMem {
            raw,
            section,
            reason,
        } => {
            let message = format!(
                "body wiki-link mem prefix '{raw}' in section '{section}' is not a valid mem name: {reason}"
            );
            tool_error_with_details(
                "INVALID_MEM_NAME",
                &message,
                Some(serde_json::json!({
                    "raw": raw,
                    "section": section,
                    "reason": reason,
                })),
            )
        }
        EngineError::ConflictingSectionModes { section, modes } => {
            let message =
                format!("section {section:?} appears in multiple mutation modes: {modes:?}");
            tool_error_with_details(
                "CONFLICTING_SECTION_MODES",
                &message,
                Some(serde_json::json!({ "section": section, "modes": modes })),
            )
        }
        EngineError::RelationshipCycle {
            rel_type,
            from,
            to,
            existing_path,
            path_truncated,
        } => {
            let existing_path_json: Vec<String> =
                existing_path.iter().map(|id| id.to_string()).collect();
            let message = format!(
                "creating edge {rel_type} from '{from}' to '{to}' would close a cycle in the {rel_type} subgraph"
            );
            tool_error_with_details(
                "RELATIONSHIP_CYCLE",
                &message,
                Some(serde_json::json!({
                    "rel_type": rel_type,
                    "from": from.to_string(),
                    "to": to.to_string(),
                    "existing_path": existing_path_json,
                    "path_truncated": path_truncated,
                })),
            )
        }
        EngineError::SetAndUnsetConflict { keys } => {
            let message = format!("metadata keys appear in both set and unset: {keys:?}");
            tool_error_with_details(
                "SET_AND_UNSET_CONFLICT",
                &message,
                Some(serde_json::json!({ "keys": keys })),
            )
        }
        EngineError::RequiredFieldUnset {
            field,
            entity_type,
            field_description,
            enum_values,
            type_write_rules,
            on_create,
            missing,
        } => {
            // Path-aware wording — create path renders "not provided"
            // (caller never supplied the field), update path renders
            // "cannot unset" (caller asked to remove a required field).
            // The typed code stays `REQUIRED_FIELD_UNSET` on both
            // paths so code-key consumers branch unchanged.
            let message = if on_create {
                format!(
                    "required metadata field '{field}' not provided — type '{entity_type}' declares the field as required and has no default for it"
                )
            } else {
                format!("cannot unset required field '{field}' for type '{entity_type}'")
            };
            // `details.missing[]` carries every required-no-default
            // field unset on the create path.
            let missing_json: Vec<_> = missing
                .iter()
                .map(|m| {
                    serde_json::json!({
                        "field": m.key,
                        "description": m.description,
                        "enum_values": m.enum_values,
                        "write_rules": type_write_rules,
                    })
                })
                .collect();
            tool_error_with_details(
                "REQUIRED_FIELD_UNSET",
                &message,
                Some(serde_json::json!({
                    "field": field,
                    "entity_type": entity_type,
                    "field_description": field_description,
                    "enum_values": enum_values,
                    "type_write_rules": type_write_rules,
                    "missing": missing_json,
                })),
            )
        }
        EngineError::MissingRequiredSection {
            entity_type,
            missing_count,
            sections,
            type_guidance,
        } => {
            let message =
                format!("missing {missing_count} required section(s) for type '{entity_type}'");
            let sections_json: Vec<_> = sections
                .iter()
                .map(|s| {
                    serde_json::json!({
                        "entity_type": s.entity_type,
                        "key": s.key,
                        "heading": s.heading,
                        "write_rules": s.write_rules,
                    })
                })
                .collect();
            tool_error_with_details(
                "MISSING_REQUIRED_SECTION",
                &message,
                Some(serde_json::json!({
                    "entity_type": entity_type,
                    "missing_count": missing_count,
                    "sections": sections_json,
                    "type_guidance": type_guidance,
                })),
            )
        }
        EngineError::PatchSectionEmpty { section } => tool_error(
            "PATCH_SECTION_EMPTY",
            &format!("patch target section is empty: {section}"),
        ),
        EngineError::PatchOldNotFound {
            section,
            current_content,
            truncated,
        } => {
            let message = format!("patch `old` substring not found in {section}");
            tool_error_with_details(
                "PATCH_OLD_NOT_FOUND",
                &message,
                Some(serde_json::json!({
                    "section": section,
                    "current_content": current_content,
                    "truncated": truncated,
                })),
            )
        }
        // Codes follow `EngineError::code()` — the single wire-code
        // source every surface (full MCP, CLI, wasm) shares. These two
        // historically drifted (`MEM_WRITER_ERROR` / `PARSE_AFTER_WRITE`);
        // `lean_backend_and_parse_after_write_codes_follow_code_contract`
        // pins them to `code()` so a re-divergence fails the build.
        EngineError::Backend(e) => tool_error("MEM_ERROR", &e.to_string()),
        EngineError::ParseAfterWrite(e) => {
            tool_error("PARSE_ERROR", &format!("parse-after-write failed: {e}"))
        }
        EngineError::Parse(e) => tool_error("PARSE_ERROR", &e.to_string()),
        EngineError::Validation(v) => validation_envelope(v),
        // Schema-resolution, boot-path, and lifecycle variants surface
        // as their own typed codes so the wire contract matches the
        // mem-repo server. Pre-fix this set collapsed to `INTERNAL`
        // — a lean-fireable variant (multi-folder workspaces can trip
        // DuplicateMem / UnknownMem; cross_mem_links policy can
        // trip ReadOnlyMount; generic input validation produces
        // InvalidInput) shipped as INTERNAL instead of its typed code,
        // breaking the agent contract that the structured code matches
        // `EngineError::code()`.
        // Description-posture variants ship structured details so MCP
        // callers branch on `details.rel_type`/`details.from_id`/
        // `details.to_id` instead of parsing the message — bit-identical
        // wire shape with the full-server typed envelope.
        ref err @ EngineError::MissingRequiredDescription {
            ref rel_type,
            ref from_id,
            ref to_id,
        } => tool_error_with_details(
            "MISSING_REQUIRED_DESCRIPTION",
            &err.to_string(),
            Some(serde_json::json!({
                "rel_type": rel_type,
                "from_id": from_id,
                "to_id": to_id,
            })),
        ),
        ref err @ EngineError::DescriptionNotPermitted {
            ref rel_type,
            ref from_id,
            ref to_id,
        } => tool_error_with_details(
            "DESCRIPTION_NOT_PERMITTED",
            &err.to_string(),
            Some(serde_json::json!({
                "rel_type": rel_type,
                "from_id": from_id,
                "to_id": to_id,
            })),
        ),
        ref err @ EngineError::RelationManualAuthoringForbidden {
            ref rel_type,
            ref from_id,
            ref to_id,
            ref guidance,
        } => tool_error_with_details(
            "RELATION_MANUAL_AUTHORING_FORBIDDEN",
            &err.to_string(),
            Some(serde_json::json!({
                "rel_type": rel_type,
                "from_id": from_id,
                "to_id": to_id,
                "guidance": guidance,
            })),
        ),
        e @ EngineError::SchemaNotFound { .. }
        | e @ EngineError::EmbeddedSchemaInvalid { .. }
        | e @ EngineError::SchemaPackageInvalid { .. }
        | e @ EngineError::SchemaResolverInit(_)
        | e @ EngineError::DuplicateMem(_)
        | e @ EngineError::MemQuarantined { .. }
        | e @ EngineError::UnknownMem(_)
        | e @ EngineError::UnknownRef(_)
        | e @ EngineError::UnknownRemote(_)
        | e @ EngineError::LocalDivergence { .. }
        | e @ EngineError::NonFastForward { .. }
        | e @ EngineError::LocalInvalidState { .. }
        | e @ EngineError::SchemaViolationInFetch { .. }
        | e @ EngineError::PushedCommitsProtected { .. }
        | e @ EngineError::BranchResetHeadMoved { .. }
        | e @ EngineError::ReadOnlyMount(_)
        | e @ EngineError::CheckNotRecorded { .. }
        | e @ EngineError::Mem(_)
        | e @ EngineError::MemNameCollision { .. }
        | e @ EngineError::InvalidInput(_) => tool_error(e.code(), &e.to_string()),
        EngineError::RenameSimilarityOutOfRange {
            requested,
            allowed_min,
            allowed_max,
        } => tool_error_with_details(
            "INVALID_INPUT",
            &format!(
                "rename_similarity {requested} outside allowed range [{allowed_min}, {allowed_max}]"
            ),
            Some(serde_json::json!({
                "field": "rename_similarity",
                "requested": requested,
                "allowed_range": [allowed_min, allowed_max],
            })),
        ),
        EngineError::MemConfigIncomplete {
            mem,
            missing_fields,
        } => {
            let message = format!(
                "mem `{mem}` config is missing required field(s) {missing_fields:?} — \
                 set via `memstead mem set-version {mem} <version>` (e.g. 0.1.0)"
            );
            tool_error_with_details(
                "MEM_CONFIG_INCOMPLETE",
                &message,
                Some(serde_json::json!({
                    "mem": mem,
                    "missing_fields": missing_fields,
                    "set_via": format!("memstead mem set-version {mem} <version>"),
                })),
            )
        }
        EngineError::SearchUnavailable => tool_error_with_details(
            "SEARCH_UNAVAILABLE_IN_WASM",
            &display,
            Some(serde_json::json!({})),
        ),
        // Typed refusal when
        // `export_markdown` targets a mem whose active backend
        // doesn't support markdown regeneration. Filesystem-backed
        // workspaces don't reach this arm today (single folder mount),
        // but the variant must be handled to keep the match exhaustive.
        ref err @ EngineError::MarkdownExportUnsupportedBackend {
            ref mem,
            ref active_backend,
            ref supported_backends,
        } => tool_error_with_details(
            "MARKDOWN_EXPORT_UNSUPPORTED_BACKEND",
            &err.to_string(),
            Some(serde_json::json!({
                "mem": mem,
                "active_backend": active_backend,
                "supported_backends": supported_backends,
            })),
        ),
        ref err @ EngineError::EmptyUpdate { ref id } => tool_error_with_details(
            "EMPTY_UPDATE",
            &err.to_string(),
            Some(serde_json::json!({
                "id": id,
                "recognised_keys": [
                    "sections", "append_sections", "patch_sections",
                    "metadata", "metadata_unset", "declare_relations",
                ],
            })),
        ),
        // A bad `since` cursor on `memstead_changes_since`. The folder backend
        // keys `since` off timestamps rather than commit SHAs, so this
        // arm isn't reached on a filesystem-mem workspace today, but the
        // variant must be handled to keep the match exhaustive.
        ref err @ EngineError::InvalidChangesCursor { ref mem, ref since } => {
            tool_error_with_details(
                "INVALID_CURSOR",
                &err.to_string(),
                Some(serde_json::json!({ "mem": mem, "since": since })),
            )
        }
        // Review-mark diff on a markless mem — typed refusal.
        ref err @ EngineError::ReviewMarkNotSet { ref mem } => tool_error_with_details(
            "REVIEW_MARK_NOT_SET",
            &err.to_string(),
            Some(serde_json::json!({ "mem": mem })),
        ),
        // Malformed `anchors[]` element on create/update: typed
        // `INVALID_ANCHOR` with the wrapped anchor error's recovery detail.
        ref err @ EngineError::InvalidAnchor(ref anchor_err) => tool_error_with_details(
            memstead_base::anchor::INVALID_ANCHOR_CODE,
            &err.to_string(),
            Some(serde_json::Value::Object(
                anchor_err.detail().into_iter().collect(),
            )),
        ),
    }
}

/// Map a runtime [`memstead_base::runtime_validator::ValidationError`] to
/// the MCP wire envelope. Thin delegation to the shared
/// [`crate::error_envelopes::validation_envelope`] so the wire shape
/// stays bit-identical with the mem-repo `server.rs` handlers.
fn validation_envelope(err: memstead_base::runtime_validator::ValidationError) -> CallToolResult {
    crate::error_envelopes::validation_envelope(err)
}

#[tool_router(vis = "pub")]
impl FilesystemMcpServer {
    #[tool(
        name = "memstead_entity",
        description = "Read one entity as markdown (filesystem-mem flavour). Same JSON shape as the mem-repo `memstead_entity`. Frontmatter carries `_hash` (content hash) for optimistic locking on follow-up mutations. Pass `sections` to narrow the rendered body; `include_relations` appends the entity's outgoing and incoming edges; `include_context` appends its community cluster.",
        annotations(
            read_only_hint = true,
            destructive_hint = false,
            idempotent_hint = true,
            open_world_hint = false
        )
    )]
    fn memstead_entity(&self, Parameters(p): Parameters<EntityParams>) -> CallToolResult {
        let engine = crate::lock_engine!(self.engine);
        let id = EntityId::canonical(&p.id);
        let entity = match engine.get_entity(&id) {
            Some(e) => e.clone(),
            None => {
                // A quarantined mem's entities are deliberately absent
                // — the read names the quarantine, not a phantom miss.
                if engine.quarantine_reason(id.mem()).is_some() {
                    return engine_op_error(engine.unknown_mem_error(id.mem()));
                }
                return tool_error("ENTITY_NOT_FOUND", &format!("entity not found: {id}"));
            }
        };
        let sections_filter = p.sections.as_deref();
        let mut md = render_entity_markdown(&entity, sections_filter);
        // Inject `_hash` as the first frontmatter field so callers
        // can pin it to `expected_hash` on the next mutation.
        if let Some(idx) = md.find("---\n") {
            let inject_at = idx + 4;
            let line = format!("_hash: {}\n", entity.content_hash);
            md.insert_str(inject_at, &line);
        }

        // Append `## Relations` when the caller asked for it. Mirrors
        // the mem-repo entity handler — outgoing + incoming edges
        // grouped by direction, rendered as a Markdown table.
        if p.include_relations.unwrap_or(false) {
            let outgoing = engine.store().outgoing(&id).to_vec();
            let incoming = engine.store().incoming(&id).to_vec();
            md.push_str(&memstead_base::render::render_relations_markdown(
                id.as_ref(),
                &outgoing,
                &incoming,
            ));
        }

        // Append `## Community Context` when the caller asked for it
        // — the entity's cluster + neighbour list. The community
        // detection is lazy (memoised per engine) and invalidated on
        // every successful mutation, so this is cheap on a static
        // graph and pays the Louvain cost once after each write.
        if p.include_context.unwrap_or(false)
            && let Some(ctx) = engine.context(&id)
        {
            let cluster_id = ctx.community.clone().unwrap_or_else(|| "unknown".into());
            md.push_str(&memstead_base::render::render_community_context_section(
                &ctx,
                &cluster_id,
            ));
        }

        // Structured envelope
        // alongside the markdown text channel; same shape both MCP
        // flavours emit so agents branch on `structured_content`
        // uniformly regardless of which backend served the read.
        let rendered_body_tokens = memstead_base::chunking::estimate_tokens(&md);
        let full_tokens = if sections_filter.is_some() {
            let full_body = render_entity_markdown(&entity, None);
            Some(memstead_base::chunking::estimate_tokens(&full_body))
        } else {
            None
        };
        let mut structured = memstead_base::render::build_entity_envelope(
            &entity,
            rendered_body_tokens,
            full_tokens,
            sections_filter,
            None,
            engine.store().outgoing(&entity.id),
        );
        // Mutation provenance (agent-trust plan 13), opt-in — same
        // block and key the full flavour serves; default responses
        // are byte-unchanged.
        if p.include_provenance.unwrap_or(false)
            && let Some(obj) = structured.as_object_mut()
        {
            let block = match engine.entity_provenance(id.mem(), id.as_ref()) {
                Ok(prov) => serde_json::to_value(&prov).unwrap_or(serde_json::Value::Null),
                Err(e) => serde_json::json!({ "unavailable": e.to_string() }),
            };
            obj.insert("mutation_provenance".into(), block);
        }
        md_with_structured(md, structured)
    }

    #[tool(
        name = "memstead_create",
        description = "Create a new entity in the filesystem-mem workspace. Required: `title`, `entity_type`. Titles accept any single-line text (control characters such as tab/newline are rejected); the title is stored verbatim as display text, while characters outside Unicode alphanumerics, whitespace, and hyphen are dropped from the derived slug — warning TITLE_CHARS_DROPPED_FROM_SLUG names them (`INVALID_TITLE` refusals remain for control characters, empty-deriving titles, and over-long ids). Optional `sections`, `metadata`, `note`, `mem`. `mem` selects the target mount; omit it to land in the default writable mem (the first writable mount in declaration order). A create aimed at a read-only mount is refused with READ_ONLY_MOUNT. The `note` lands in `.memstead/changes.jsonl` (the filesystem-mem analogue of the mem-repo commit body). `relations` and `dry_run` are not implemented on this surface: passing a non-empty `relations` or `dry_run: true` is REFUSED up front with `UNSUPPORTED_PARAM` (`details.params` names them), never silently ignored — so a `dry_run` preview can never accidentally land a real write. Omit them, or use the unified engine (mem-repo MCP / CLI) which honours both.",
        annotations(
            read_only_hint = false,
            destructive_hint = false,
            idempotent_hint = false,
            open_world_hint = false
        )
    )]
    fn memstead_create(&self, Parameters(p): Parameters<CreateParams>) -> CallToolResult {
        // Part B: this surface hardwires `dry_run` off and ignores inline
        // `relations`. Refuse up front when either was meaningfully supplied
        // rather than committing a real write the agent thought was a preview
        // (or dropping edges it thought it wired).
        if let Some(err) = reject_unsupported_params(&[
            ("dry_run", p.dry_run == Some(true)),
            (
                "relations",
                p.relations.as_ref().is_some_and(|r| !r.is_empty()),
            ),
        ]) {
            return err;
        }
        let mut engine = crate::lock_engine!(self.engine);
        match resolve_role_lean(p.role.as_deref()) {
            Ok(r) => engine.set_role(r),
            Err(resp) => return *resp,
        }
        let (actor, client) = self.actor_and_client();
        // Resolve the target mem. An explicit, non-empty `mem` is
        // honoured verbatim — so a multi-mount engine (e.g. a read-only
        // content mem alongside a writable sketch mem) can be targeted
        // by name, and a create aimed at a read-only mount surfaces the
        // engine's READ_ONLY_MOUNT refusal rather than being silently
        // redirected to the writable mount. Omitted → the default writable
        // mem (first writable mount in declaration order), falling back to
        // the first mount so a read-only-only engine still resolves a name
        // (the create then refuses with READ_ONLY_MOUNT, never panics on an
        // empty mem). Single-mem filesystem workspaces are unaffected:
        // the sole mem is both the first and the default writable one.
        let mem = match p.mem.as_deref() {
            Some(v) if !v.is_empty() => v.to_string(),
            _ => engine
                .default_writable_mem()
                .or_else(|| engine.mem_names().into_iter().next())
                .map(String::from)
                .unwrap_or_default(),
        };
        let args = CreateEntityArgs {
            anchors: p
                .anchors
                .unwrap_or_default()
                .into_iter()
                .map(|a| a.into_engine())
                .collect(),
            mem,
            title: p.title,
            entity_type: p.entity_type,
            sections: p.sections.unwrap_or_default(),
            metadata: p.metadata.unwrap_or_default(),
            // The filesystem-mem MCP surface doesn't (yet)
            // accept inline relations on the wire — pass empty;
            // operators wire edges via memstead_relate post-create.
            relations: Vec::new(),
            // dry_run not exposed on the filesystem-mem tool
            // surface; operators preview changes by reading first
            // and inspecting on the agent side.
            dry_run: false,
        };
        match engine.create_entity(args, actor, client.as_ref(), p.note.as_deref()) {
            Ok(outcome) => {
                // WarningHint's Serialize impl produces the same
                // `{code, message, details}` envelope the manual
                // synthesis used to emit. commit_sha + title +
                // mem are now first-class on the outcome.
                let durable = mem_is_durable(&engine, &outcome.mem);
                let body = serde_json::json!({
                    "id": outcome.id.to_string(),
                    "title": outcome.title,
                    "mem": outcome.mem,
                    "file_path": outcome.file_path,
                    "_hash": outcome.content_hash,
                    "commit_sha": outcome.commit_sha,
                    "durable": durable,
                    "warnings": outcome.warnings,
                    "type_guidance": outcome.type_guidance,
                });
                json_response(&body)
            }
            Err(e) => engine_op_error(e),
        }
    }

    #[tool(
        name = "memstead_update",
        description = "Update an existing entity in the filesystem-mem workspace. `expected_hash` (from a previous memstead_entity read) is required — mismatch returns code HASH_MISMATCH with details.current carrying the live hash. This surface honours `sections` (replace) + `metadata` (set) + `metadata_unset` + `declare_relations` + `relations_unset`. The mem-repo `append_sections` / `patch_sections` / `dry_run` shapes are NOT implemented here: passing a non-empty `append_sections` / `patch_sections`, or `dry_run: true`, is REFUSED up front with `UNSUPPORTED_PARAM` (`details.params` names them), never silently ignored — an agent that patches is told its patch was dropped instead of believing it applied. Omit them, or use the unified engine (mem-repo MCP / CLI) which honours all three.",
        annotations(
            read_only_hint = false,
            destructive_hint = false,
            idempotent_hint = false,
            open_world_hint = false
        )
    )]
    fn memstead_update(&self, Parameters(p): Parameters<UpdateParams>) -> CallToolResult {
        // Part B: this surface hardwires `append_sections` / `patch_sections`
        // off and `dry_run` off. Refuse up front when any was meaningfully
        // supplied rather than dropping the edit silently (an agent that
        // patched believes it patched). `sections` (replace), `metadata`,
        // `metadata_unset`, `declare_relations`, and `relations_unset` ARE
        // honoured and pass through untouched.
        if let Some(err) = reject_unsupported_params(&[
            ("dry_run", p.dry_run == Some(true)),
            (
                "append_sections",
                p.append_sections.as_ref().is_some_and(|m| !m.is_empty()),
            ),
            (
                "patch_sections",
                p.patch_sections.as_ref().is_some_and(|m| !m.is_empty()),
            ),
        ]) {
            return err;
        }
        let mut engine = crate::lock_engine!(self.engine);
        match resolve_role_lean(p.role.as_deref()) {
            Ok(r) => engine.set_role(r),
            Err(resp) => return *resp,
        }
        let (actor, client) = self.actor_and_client();
        let args = UpdateEntityArgs {
            anchors: p
                .anchors
                .unwrap_or_default()
                .into_iter()
                .map(|a| a.into_engine())
                .collect(),
            anchors_unset: p
                .anchors_unset
                .unwrap_or_default()
                .into_iter()
                .map(|u| u.into_engine())
                .collect(),
            relations_unset: p
                .relations_unset
                .unwrap_or_default()
                .into_iter()
                .map(|r| memstead_base::ops::RelationUnsetArg {
                    rel_type: r.rel_type,
                    target: memstead_base::EntityId(r.target),
                })
                .collect(),
            id: EntityId(p.id),
            expected_hash: Some(p.expected_hash),
            sections: p.sections.unwrap_or_default(),
            // The filesystem-mem tool doesn't expose
            // append_sections / patch_sections on the wire yet;
            // pass empty.
            append_sections: IndexMap::new(),
            patch_sections: IndexMap::new(),
            metadata: p.metadata.unwrap_or_default(),
            metadata_unset: p.metadata_unset.unwrap_or_default(),
            declare_relations: p
                .declare_relations
                .unwrap_or_default()
                .into_iter()
                .map(|r| memstead_base::ops::RelateArg {
                    rel_type: r.r#type,
                    to: EntityId(r.to),
                    description: r.description,
                })
                .collect(),
            dry_run: false,
        };
        match engine.update_entity(args, actor, client.as_ref(), p.note.as_deref()) {
            Ok(outcome) => {
                let durable = mem_is_durable(&engine, outcome.id.mem());
                let body = serde_json::json!({
                    "id": outcome.id.to_string(),
                    "file_path": outcome.file_path,
                    "_hash": outcome.content_hash,
                    "durable": durable,
                    "modified_sections": outcome.modified_sections.replaced,
                    "modified_metadata_set": outcome.modified_metadata.set,
                    "modified_metadata_unset": outcome.modified_metadata.unset,
                    // Typed warnings ride out on `outcome.warnings` —
                    // the engine emits `NOTE_MISSING` here under
                    // `[mutations].require_notes`, matching create/relate.
                    "warnings": outcome.warnings,
                    // Orphan-stub GC: when this update removed a body
                    // wiki-link that was a stub target's last referrer,
                    // the engine GC'd the stub and lists it here. Always
                    // present (empty array when nothing orphaned),
                    // matching the relate-remove and delete shape so
                    // consumers don't branch on field presence.
                    "orphan_stubs_removed": outcome
                        .orphan_stubs_removed
                        .iter()
                        .map(|i| i.to_string())
                        .collect::<Vec<_>>(),
                });
                json_response(&body)
            }
            Err(e) => engine_op_error(e),
        }
    }

    #[tool(
        name = "memstead_delete",
        description = "Remove an entity from the filesystem-mem workspace. `expected_hash` is required (read first via memstead_entity); mismatch returns HASH_MISMATCH. Refuses entities with incoming references — v1 has no per-call force toggle on the MCP surface; use `memstead delete --force` on the CLI. The `note` lands in `.memstead/changes.jsonl` (the per-mutation changelog).",
        annotations(
            read_only_hint = false,
            destructive_hint = true,
            idempotent_hint = false,
            open_world_hint = false
        )
    )]
    fn memstead_delete(&self, Parameters(p): Parameters<DeleteParams>) -> CallToolResult {
        let mut engine = crate::lock_engine!(self.engine);
        match resolve_role_lean(p.role.as_deref()) {
            Ok(r) => engine.set_role(r),
            Err(resp) => return *resp,
        }
        let (actor, client) = self.actor_and_client();
        let args = DeleteEntityArgs {
            id: EntityId(p.id),
            // The MCP delete shape carries a required `expected_hash` String;
            // an empty string is the documented "stub delete" path. The
            // engine takes Option — pass `None` only for empty
            // strings to preserve the no-hash-check semantics.
            expected_hash: if p.expected_hash.is_empty() {
                None
            } else {
                Some(p.expected_hash)
            },
        };
        match engine.delete_entity(args, actor, client.as_ref(), p.note.as_deref()) {
            Ok(outcome) => {
                let durable = mem_is_durable(&engine, outcome.id.mem());
                let body = serde_json::json!({
                    "id": outcome.id.to_string(),
                    "file_path": outcome.file_path,
                    "removed_incoming": outcome.removed_incoming,
                    "durable": durable,
                    // Engine-emitted warnings (residual-stub demotion,
                    // and `NOTE_MISSING` under `require_notes`).
                    "warnings": outcome.warnings,
                });
                json_response(&body)
            }
            Err(e) => engine_op_error(e),
        }
    }

    #[tool(
        name = "memstead_relate",
        description = "Connect or disconnect two entities with a typed relationship in the same filesystem-mem. Cross-mem targets are rejected with CROSS_MEM_RELATION (filesystem-mem is single-mem by design). `remove: true` drops the matching pair if present; otherwise the call appends. No-op paths (already present add, absent remove) succeed silently and do not append a changelog line. `dry_run` is not implemented on this surface: passing `dry_run: true` is REFUSED up front with `UNSUPPORTED_PARAM` (`details.params` names it), never silently ignored — so a rehearsal can never accidentally land a real write. Omit it, or use the unified engine (mem-repo MCP / CLI) which honours it.",
        // idempotent_hint = true: relate's duplicate-add and
        // remove-nonexistent paths are typed-warning no-ops, so a retry
        // converges. Matches the mem-repo server's annotation —
        // `relate_annotation_is_idempotent_on_lean` pins parity.
        annotations(read_only_hint = false, destructive_hint = false, idempotent_hint = true, open_world_hint = false)
    )]
    fn memstead_relate(&self, Parameters(p): Parameters<RelateParams>) -> CallToolResult {
        // This surface hardwires `dry_run` off — refuse up front when
        // meaningfully supplied so a rehearsal can never accidentally
        // land a real write (same posture as create / update).
        if let Some(refusal) = reject_unsupported_params(&[("dry_run", p.dry_run == Some(true))]) {
            return refusal;
        }
        if p.relations.is_empty() {
            return tool_error(
                "INVALID_INPUT",
                "relations must carry at least one operation",
            );
        }
        let mut engine = crate::lock_engine!(self.engine);
        match resolve_role_lean(p.role.as_deref()) {
            Ok(r) => engine.set_role(r),
            Err(resp) => return *resp,
        }
        let (actor, client) = self.actor_and_client();
        // Relate is hash-stable on the section bodies but the
        // Relationships section regenerates, so `_hash` per entry is
        // read back post-commit. `expected_hash` is omitted on relate
        // across both flavours.
        let ops: Vec<(RelateEntityArgs, Option<String>)> = p
            .relations
            .iter()
            .map(|op| {
                (
                    RelateEntityArgs {
                        source: EntityId::canonical(&op.from),
                        expected_hash: None,
                        rel_type: op.r#type.clone(),
                        target: EntityId::canonical(&op.to),
                        remove: op.remove.unwrap_or(false),
                        description: op.description.clone(),
                        dry_run: false,
                    },
                    p.note.clone(),
                )
            })
            .collect();
        let anchor_mem = ops[0].0.source.mem().to_string();

        // A list of one routes through the single-op engine path —
        // byte-identical semantics to the historical single call,
        // wrapped in the same plural envelope larger lists produce.
        if p.relations.len() == 1 {
            let (args, note) = {
                let mut it = ops.into_iter();
                it.next().expect("len checked above")
            };
            return match engine.relate_entity(args, actor, client.as_ref(), note.as_deref()) {
                Ok(outcome) => {
                    let action = match outcome.action {
                        RelateAction::Added => "added",
                        RelateAction::Removed => "removed",
                        RelateAction::NoOpAlreadyPresent | RelateAction::NoOpAbsent => "noop",
                    };
                    let durable = mem_is_durable(&engine, outcome.from.mem());
                    let body = serde_json::json!({
                        "results": [{
                            "from": outcome.from.to_string(),
                            "to": outcome.to.to_string(),
                            "rel_type": outcome.rel_type,
                            "action": action,
                            "source": outcome.source,
                            "_hash": outcome.content_hash,
                        }],
                        "commit_sha": outcome.commit_sha,
                        "durable": durable,
                        "warnings": outcome.warnings,
                        "orphan_stubs_removed": outcome
                            .orphan_stubs_removed
                            .iter()
                            .map(|i| i.to_string())
                            .collect::<Vec<_>>(),
                    });
                    json_response(&body)
                }
                Err(e) => engine_op_error(e),
            };
        }

        // Snapshot which targets are absent pre-call so applied
        // auto-stubs can surface the same AUTO_STUB_CREATED warning
        // the single call emitted.
        let absent_targets: std::collections::HashSet<String> = p
            .relations
            .iter()
            .filter(|op| !op.remove.unwrap_or(false))
            .map(|op| EntityId::canonical(&op.to))
            .filter(|to| engine.store().get(to).is_none())
            .map(|to| to.to_string())
            .collect();
        let result = match engine.batch_relate(ops, actor, client.as_ref(), false) {
            Ok(r) => r,
            Err(e) => return engine_op_error(e),
        };

        if !result.applied {
            // Report-all refusal — nothing committed. A list of one
            // surfaces its entry's own typed envelope; larger lists
            // wrap under BATCH_REFUSED with per-entry envelopes.
            let entries: Vec<serde_json::Value> = result
                .results
                .iter()
                .zip(p.relations.iter())
                .enumerate()
                .map(|(i, (entry, op))| {
                    let mut e = serde_json::json!({
                        "index": i,
                        "from": op.from,
                        "to": op.to,
                        "rel_type": op.r#type,
                        "action": entry.action,
                    });
                    if let Some(err) = &entry.error {
                        e["code"] = serde_json::json!(err.code);
                        e["message"] = serde_json::json!(err.message);
                        e["details"] = err.details.clone();
                    }
                    e
                })
                .collect();
            let msg = format!(
                "batch refused — {} of {} operation(s) failed, nothing committed",
                result.failed,
                p.relations.len(),
            );
            return tool_error_with_details(
                "BATCH_REFUSED",
                &msg,
                Some(serde_json::json!({
                    "entries": entries,
                    "failed": result.failed,
                    "errors_suppressed": result.errors_suppressed,
                })),
            );
        }

        let durable = mem_is_durable(&engine, anchor_mem.as_str());
        let mut warnings: Vec<memstead_base::ops::WarningHint> = Vec::new();
        let entries: Vec<serde_json::Value> = result
            .results
            .iter()
            .zip(p.relations.iter())
            .map(|(entry, op)| {
                let from = EntityId::canonical(&op.from);
                let to = EntityId::canonical(&op.to);
                let canonical_type = op.r#type.to_uppercase();
                if entry.action == "noop" {
                    if op.remove.unwrap_or(false) {
                        warnings.push(memstead_base::ops::WarningHint::NoSuchRelationship {
                            rel_type: canonical_type.clone(),
                            from: from.clone(),
                            to: to.clone(),
                        });
                    } else {
                        warnings.push(memstead_base::ops::WarningHint::DuplicateRelationship {
                            rel_type: canonical_type.clone(),
                            from: from.clone(),
                            to: to.clone(),
                        });
                    }
                }
                if !op.remove.unwrap_or(false)
                    && absent_targets.contains(&to.to_string())
                    && engine.store().get(&to).map(|e| e.stub).unwrap_or(false)
                {
                    // Real batch only (this flavour has no relate
                    // rehearsal): the stub exists post-commit.
                    warnings.push(memstead_base::ops::WarningHint::AutoStubCreated {
                        stub_id: to.clone(),
                        pending: false,
                    });
                }
                let source_label = engine
                    .store()
                    .outgoing(&from)
                    .iter()
                    .find(|e| e.target == to && e.rel_type.eq_ignore_ascii_case(&op.r#type))
                    .map(|e| match e.source {
                        memstead_base::EdgeSource::BodyLink => "body_link",
                        memstead_base::EdgeSource::Hierarchy => "hierarchy",
                        memstead_base::EdgeSource::Explicit => "explicit",
                    })
                    .unwrap_or("explicit");
                let hash = engine
                    .store()
                    .get(&from)
                    .map(|e| e.content_hash.clone())
                    .unwrap_or_default();
                serde_json::json!({
                    "from": from.to_string(),
                    "to": to.to_string(),
                    "rel_type": canonical_type,
                    "action": entry.action,
                    "source": source_label,
                    "_hash": hash,
                })
            })
            .collect();

        let body = serde_json::json!({
            "results": entries,
            "commit_sha": result.commit_sha,
            "durable": durable,
            "warnings": warnings,
            "orphan_stubs_removed": result
                .orphan_stubs_removed
                .iter()
                .map(|i| i.to_string())
                .collect::<Vec<_>>(),
        });
        json_response(&body)
    }

    #[tool(
        name = "memstead_search",
        description = "Search entities by lexical content + structural filters. Same JSON shape as the mem-repo `memstead_search`. The first call after engine init or any mutation pays a one-time search-index build (scales with entity count); subsequent calls reuse the cache. Pass an empty `query: {}` (or omit it) for a metadata-only structural filter — the list shape folds in here. Filters: `mem`, `entity_type`, `edge_type` (first-class engine axes), `stub`, plus `filters: { <field>: <value> }` for any schema-declared `filterable: equality` field (e.g. `{\"level\": \"M0\", \"tags\": \"auth\"}`). Strict type-narrowing: an entity whose type doesn't declare a *filterable* field is excluded (warning `FILTER_TYPE_SCOPED`); a field declared but not filterable on any reachable type is ignored — the result equals the same search without it (warning `FIELD_NOT_FILTERABLE`), never emptied, in both the scoped and unscoped case; a key no schema declares is ignored (`UNKNOWN_FILTER_KEY`). Pagination via `limit` / `offset`. Section bodies are not shipped per hit — read them with `memstead_entity`. A page is bounded to `token_budget` (default 12000): an overflowing page returns the highest-ranked hits that fit with a `SEARCH_RESULTS_TRUNCATED` warning (`kept`/`budget`) while `_total` stays the full count — page on with `offset` or raise `token_budget`.",
        annotations(
            read_only_hint = true,
            destructive_hint = false,
            idempotent_hint = true,
            open_world_hint = false
        )
    )]
    fn memstead_search(&self, Parameters(p): Parameters<SearchParams>) -> CallToolResult {
        let engine = crate::lock_engine!(self.engine);
        let filters = p.filters.unwrap_or_default();
        let scope = SearchScope {
            query: p.query,
            mem: p.mem,
            entity_type: p.entity_type,
            limit: p.limit,
            offset: p.offset,
            filters,
            // Thread the
            // agent's `range_filters` through to the engine arg —
            // mirrors the full server's wiring so both servers expose the
            // typed range-filter warnings the engine already produces.
            range_filters: p.range_filters.unwrap_or_default(),
            edge_type: p.edge_type,
            related_to: p.related_to.map(EntityId),
            depth: p.depth,
            expand_via: p.expand_via,
            expand_depth: p.expand_depth,
            direction: p.direction.unwrap_or_default(),
            stub: p.stub,
            token_budget: p.token_budget,
        };
        let offset = scope.offset.unwrap_or(0);
        let result = match engine.search(&scope) {
            Ok(r) => r,
            Err(e) => return engine_op_error(e),
        };
        let md = render_search_markdown(&result, offset);
        // Structured envelope
        // on `structured_content`, rendered markdown on the text
        // channel; lean MCP mirrors full's split so cross-flavour
        // agents see the same wire contract.
        let envelope = memstead_base::render::build_search_envelope(&result, offset);
        let structured = serde_json::to_value(&envelope).unwrap_or(serde_json::Value::Null);
        md_with_structured(md, structured)
    }

    #[tool(
        name = "memstead_health",
        description = "Return the filesystem-mem workspace's health summary: orphans, stubs, missing required fields, stale entities. Same JSON shape as the mem-repo `memstead_health` (single-mem, so `writable_mems` carries one entry). `include` accepts the shared health key set — today the lean surface dispatches `dangling_links` (matching the mem-repo response shape: `{from, target_id, target_path, section}`) and validates every key against the allowed set, emitting `UNKNOWN_INCLUDE_KEY` on the response's `warnings[]` for typos. `conformance` / `integrity` are dispatched too: `conformance` lints every entity against the effective schema (the pin, or `target_schema` when given) into a `findings` array of `{id, axis, code, detail}` with write-time typed codes; `integrity` adds the consistency axis (DANGLING_LINK, ORPHAN_STUB) to the same list; `anchors` adds per-mem counts of the four standalone anchor-verification states (resolved/drifted/recheck/unresolvable). `constraints` lists standing declared-constraint violations with `severity`. Other detail keys (`orphans`, `stubs`, …) are accepted but the v1 surface returns the full report regardless — narrowing is a follow-up.",
        annotations(
            read_only_hint = true,
            destructive_hint = false,
            idempotent_hint = true,
            open_world_hint = false
        )
    )]
    fn memstead_health(&self, Parameters(p): Parameters<HealthParams>) -> CallToolResult {
        let engine = crate::lock_engine!(self.engine);
        let mut health = engine.health();
        let include = p.include.unwrap_or_default();

        // Validate include keys against the shared catalogue. Unknown
        // keys surface as a typed `UNKNOWN_INCLUDE_KEY` warning — the
        // same shape full emits and the same shape the CLI consumes.
        for key in &include {
            if !memstead_base::ops::health::HEALTH_INCLUDE_KEYS.contains(&key.as_str()) {
                health
                    .warnings
                    .push(memstead_base::WarningHint::UnknownIncludeKey {
                        key: key.clone(),
                        allowed: memstead_base::ops::health::HEALTH_INCLUDE_KEYS
                            .iter()
                            .map(|s| s.to_string())
                            .collect(),
                    });
            }
        }

        // `dangling_links` opt-in: the engine's `HealthSummary` carries
        // a `dangling_links: Option<...>` slot that handlers populate
        // (the kernel's `compute_health` leaves it `None`). Populating
        // it from the lean surface gives agents the documented
        // include-key without forcing them through the full engine.
        if include.iter().any(|s| s == "dangling_links") {
            let dangling = memstead_base::ops::health::collect_dangling_links(engine.store(), None);
            health.dangling_links = Some(dangling);
        }

        // Conformance axis (`conformance`), or both axes
        // (`integrity`) — same `findings` slot and `{ id, axis, code,
        // detail }` shape as the mem-repo flavour. The scan iterates every
        // mounted mem's schema (`engine.schemas().keys()`), so on a
        // two-mount session engine (writable sketch + read-only content) it
        // covers both mounts — the summary counts and roster come straight
        // from `engine.health()`, which is mount-aware, so nothing is
        // misreported on a multi-mount engine.
        if include
            .iter()
            .any(|s| s == "conformance" || s == "integrity")
        {
            let target: Option<memstead_schema::SchemaRef> = match p.target_schema.as_deref() {
                None => None,
                Some(raw) => match raw.parse::<memstead_schema::SchemaRef>() {
                    Ok(r) => Some(r),
                    Err(reason) => {
                        return tool_error(
                            "INVALID_INPUT",
                            &format!("invalid target_schema {raw:?}: {reason}"),
                        );
                    }
                },
            };
            let mut mem_names: Vec<String> = engine.schemas().keys().cloned().collect();
            mem_names.sort();
            let mut findings = Vec::new();
            for v in &mem_names {
                match engine.conformance_findings(v, target.as_ref()) {
                    Ok(f) => findings.extend(f),
                    Err(e) => {
                        return tool_error(e.code(), &e.to_string());
                    }
                }
                if include.iter().any(|s| s == "integrity") {
                    match engine.consistency_findings(v) {
                        Ok(f) => findings.extend(f),
                        Err(e) => {
                            return tool_error(e.code(), &e.to_string());
                        }
                    }
                }
            }
            health.findings = Some(findings);
        }

        // `include=["anchors"]` (per-mem four-state counts),
        // `include=["constraints"]` (standing declared-constraint
        // violations), and `include=["friction"]` (the refusal
        // ledger's summary — agent-trust plan 08) — from the shared
        // base helpers, patched onto the serialized report so
        // combined includes compose.
        let wants_anchors = include.iter().any(|s| s == "anchors");
        let wants_constraints = include.iter().any(|s| s == "constraints");
        let wants_friction = include.iter().any(|s| s == "friction");
        let wants_open_questions = include.iter().any(|s| s == "open_questions");
        let wants_stale_derivations = include.iter().any(|s| s == "stale_derivations");
        let wants_checks = include.iter().any(|s| s == "checks");
        if wants_anchors
            || wants_constraints
            || wants_friction
            || wants_open_questions
            || wants_stale_derivations
            || wants_checks
        {
            let mut value = match serde_json::to_value(&health) {
                Ok(v) => v,
                Err(e) => return tool_error("INTERNAL", &format!("serialize health: {e}")),
            };
            if wants_anchors {
                value["anchors"] = memstead_base::ops::health::health_anchors_axis(&engine);
            }
            if wants_constraints {
                value["constraints"] = serde_json::to_value(engine.constraint_findings(None))
                    .unwrap_or(serde_json::Value::Null);
                let defects = engine.schema_format_defects();
                if !defects.is_empty() {
                    value["schema_format_defects"] =
                        serde_json::to_value(defects).unwrap_or(serde_json::Value::Null);
                }
            }
            if wants_friction {
                value["friction"] =
                    memstead_base::friction::FrictionLedger::for_workspace(&self.workspace_root)
                        .summarize();
            }
            if wants_open_questions {
                value["open_questions"] =
                    memstead_base::ops::health::health_open_questions_axis(&engine, None);
            }
            if wants_stale_derivations {
                value["stale_derivations"] =
                    memstead_base::ops::health::health_stale_derivations_axis(&engine, None);
            }
            if wants_checks {
                value["checks"] = memstead_base::ops::health::health_checks_axis(&engine, None);
            }
            return json_response(&value);
        }

        json_response(&health)
    }

    #[tool(
        name = "memstead_schema",
        description = "Read the workspace's pinned schema as a JSON document — `ref` (canonical `name@version`), `relationship_mode`, the relationship vocabulary, `community`, `used_by[]`, top-level `origin` (`first-party` / `third-party`; a third-party schema is served structural-only with its prose-instruction fields omitted), top-level `alias_target_rel_type` (when authored — the rel-type body wiki-links `[[target]]` auto-emit), and per-type section/field detail. Accepts either `name` (bare name or canonical pin) or `mem` (the workspace's single mem). Passing both is `INVALID_INPUT`. v1 surface returns the engine's pinned schema regardless of which form is used (filesystem-mem is single-mem, single-schema). Default `verbosity` is `\"lite\"` — a cheap cold-start skeleton: entity-type names + section keys + field shapes, relationship names + endpoints, the alias pointer, prose dropped (heavy arrays ship as `types_summary`/`relationships_summary`). Pass `verbosity: \"full\"` for the complete prose payload. An unrecognized `verbosity` returns `INVALID_INPUT`. Returns `ENTITY_NOT_FOUND` when `name` explicitly mismatches the pinned schema; `UNKNOWN_MEM` when `mem` is not the workspace's mem.",
        annotations(
            read_only_hint = true,
            destructive_hint = false,
            idempotent_hint = true,
            open_world_hint = false
        )
    )]
    fn memstead_schema(&self, Parameters(p): Parameters<SchemaParams>) -> CallToolResult {
        let engine = crate::lock_engine!(self.engine);
        // filesystem-mem is single-mem by design; the new engine
        // carries one schemas[] entry. Pick it.
        let Some((mem_name, schema)) = engine.schemas().iter().next() else {
            // Genuinely-systemic: the filesystem boot path always
            // mounts exactly one mem and pins exactly one schema. An
            // empty `schemas()` map means engine construction itself
            // is inconsistent — no agent-side recovery applies, so
            // `INTERNAL` is the honest wire code (this class of
            // genuinely-systemic failure is the legitimate `INTERNAL`
            // use case).
            return tool_error(
                "INTERNAL",
                "engine has no schemas — workspace mount list is empty",
            );
        };
        let pinned_name = &schema.manifest.name;
        let pinned_version = schema.version.to_string();
        let canon = format!("{pinned_name}@{pinned_version}");

        // Validate (`name`, `mem`) input shape; either resolves to a
        // string the lookup below checks against the pinned schema.
        // The `mem` path is provided for parity with the mem-repo
        // flavour; in filesystem-mem the single mem always maps
        // to the single schema.
        let want_owned: String = match (p.name.as_deref(), p.mem.as_deref()) {
            (Some(_), Some(_)) => {
                return tool_error(
                    "INVALID_INPUT",
                    "memstead_schema accepts exactly one of `name` or `mem`, not both.",
                );
            }
            (Some(name), None) => name.trim().to_string(),
            (None, Some(mem)) => {
                if mem != mem_name.as_str() {
                    return tool_error(
                        "UNKNOWN_MEM",
                        &format!("unknown mem: {mem:?} — workspace mounts {mem_name:?}"),
                    );
                }
                String::new() // matches pinned schema by default
            }
            (None, None) => String::new(),
        };
        let want = want_owned.as_str();
        let matches = want.is_empty() || want == pinned_name.as_str() || want == canon.as_str();
        if !matches {
            return tool_error(
                "ENTITY_NOT_FOUND",
                &format!("schema not found: {want:?} — workspace pins {canon}"),
            );
        }

        // Verbosity toggle — `lite` (default) or the full body, mirroring
        // the mem-repo server's default. An unrecognized value refuses
        // with a typed INVALID_INPUT naming the bad value rather than
        // silently falling back.
        let verbosity = match p.verbosity.as_deref() {
            None => memstead_base::render::SchemaVerbosity::Lite,
            Some(v) => match memstead_base::render::SchemaVerbosity::from_wire(v) {
                Some(sv) => sv,
                None => {
                    return tool_error(
                        "INVALID_INPUT",
                        &format!("unknown verbosity: {v:?} — expected \"full\" or \"lite\""),
                    );
                }
            },
        };

        // One shared, transport-neutral builder for every schema-read
        // surface (mem-repo MCP, the HTTP `/api/schema` endpoint, and
        // this filesystem-mem flavour) — no second divergent renderer
        // to drift. `ref` carries the canonical `name@version`, and
        // `alias_target_rel_type` rides along, so the public surface now
        // advertises the body-wiki-link edge-authoring rule the full
        // schema response always carried.
        // Trust origin governs de-framing: a third-party schema is served
        // structural-only regardless of the requested `verbosity`.
        let origin = engine.schema_origin(schema);
        let payload = memstead_base::render::build_schema_payload(
            schema,
            vec![mem_name.to_string()],
            verbosity,
            origin,
        );
        json_response(&payload)
    }

    #[tool(
        name = "memstead_diff",
        description = "Return a two-ref structural diff at entity granularity. **Filesystem-mem flavour:** folder mounts carry no git refs, so this tool refuses with `INVALID_INPUT` against folder-backed mems. Use the mem-repo flavour for the real diff; the surface stays for cross-flavour clients that hit either server.",
        annotations(
            read_only_hint = true,
            destructive_hint = false,
            idempotent_hint = true,
            open_world_hint = false
        )
    )]
    fn memstead_diff(&self, Parameters(p): Parameters<DiffParams>) -> CallToolResult {
        let engine = crate::lock_engine!(self.engine);
        let config = memstead_base::ops::DiffConfig {
            rename_similarity: p
                .rename_similarity
                .unwrap_or(memstead_base::ops::RENAME_SIMILARITY_DEFAULT),
            include_content: p.include_content,
            include_ripple: p.include_ripple,
        };
        match engine.diff(&p.mem, &p.ref_a, &p.ref_b, Some(config)) {
            Ok(diff) => json_response(&diff),
            Err(e) => engine_op_error(e),
        }
    }

    #[tool(
        name = "memstead_changes_since",
        description = "Read the per-mutation changelog at `.memstead/changes.jsonl` since a given RFC 3339 timestamp. **Diverges from the mem-repo flavour** — filesystem-mem has no commit history, so `since` is a timestamp string (e.g. `\"2026-05-08T15:30:00.000Z\"`) and the response yields the JSONL entries with `ts > since` as a structured array. Pass an empty string or the UNIX epoch (`\"1970-01-01T00:00:00.000Z\"`) for a full dump. The `mem` field is accepted for shape compatibility with the mem-repo flavour but ignored — single-mem. `rename_similarity` and `include_notes` are also accepted but ignored.",
        annotations(
            read_only_hint = true,
            destructive_hint = false,
            idempotent_hint = true,
            open_world_hint = false
        )
    )]
    fn memstead_changes_since(
        &self,
        Parameters(p): Parameters<ChangesSinceParams>,
    ) -> CallToolResult {
        // The unified engine doesn't expose a workspace_root accessor
        // (mounts can be heterogeneous); use the captured field.
        let log_path = self
            .workspace_root
            .join(memstead_base::MEM_META_DIR)
            .join("changes.jsonl");
        let raw = match std::fs::read_to_string(&log_path) {
            Ok(s) => s,
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => String::new(),
            Err(e) => {
                return tool_error("CHANGELOG_ERROR", &e.to_string());
            }
        };

        let since = p.since.trim();
        let mut entries: Vec<serde_json::Value> = Vec::new();
        for line in raw.lines() {
            let trimmed = line.trim();
            if trimmed.is_empty() {
                continue;
            }
            let value: serde_json::Value = match serde_json::from_str(trimmed) {
                Ok(v) => v,
                Err(_) => continue, // skip malformed lines silently
            };
            let ts_match = value
                .get("ts")
                .and_then(|v| v.as_str())
                .map(|s| s.to_string())
                .unwrap_or_default();
            if !since.is_empty() && ts_match.as_str() <= since {
                continue;
            }
            entries.push(value);
        }
        let payload = serde_json::json!({
            "since": since,
            "count": entries.len(),
            "entries": entries,
        });
        json_response(&payload)
    }

    #[tool(
        name = "memstead_rename",
        description = "Rename an entity by changing its title. The slug, id, and on-disk file path follow. Titles accept any single-line text (control characters such as tab/newline are rejected); the title is stored verbatim as display text, while characters outside Unicode alphanumerics, whitespace, and hyphen are dropped from the derived slug — warning TITLE_CHARS_DROPPED_FROM_SLUG names them (`INVALID_TITLE` refusals remain for control characters, empty-deriving titles, and over-long ids). `expected_hash` is required. Atomic referrer rewrite: every Write-Mem entity whose relationships or section bodies point at the old id has its `[[old-slug]]` tokens rewritten in one per-mem commit; ReadOnly referrers leave a residual stub at the old id holding the surviving incoming edges.",
        annotations(
            read_only_hint = false,
            destructive_hint = false,
            idempotent_hint = false,
            open_world_hint = false
        )
    )]
    fn memstead_rename(&self, Parameters(p): Parameters<RenameParams>) -> CallToolResult {
        let mut engine = crate::lock_engine!(self.engine);
        match resolve_role_lean(p.role.as_deref()) {
            Ok(r) => engine.set_role(r),
            Err(resp) => return *resp,
        }
        let (actor, client) = self.actor_and_client();
        let args = RenameEntityArgs {
            id: EntityId(p.id),
            expected_hash: Some(p.expected_hash),
            new_title: p.new_title,
        };
        match engine.rename_entity(args, actor, client.as_ref(), p.note.as_deref()) {
            Ok(outcome) => {
                let durable = mem_is_durable(&engine, outcome.new_id.mem());
                let body = serde_json::json!({
                    "old_id": outcome.old_id.to_string(),
                    "new_id": outcome.new_id.to_string(),
                    "old_file_path": outcome.old_path,
                    "new_file_path": outcome.new_path,
                    "_hash": outcome.content_hash,
                    "durable": durable,
                    // Engine-emitted warnings (slug-noop, and
                    // `NOTE_MISSING` under `require_notes`).
                    "warnings": outcome.warnings,
                });
                json_response(&body)
            }
            Err(e) => engine_op_error(e),
        }
    }

    #[tool(
        name = "memstead_check",
        description = "Record a check: \"entity E checked, verdict ok | failed, via method M\" — the engine-recorded act of verification (never a mutation: entity markdown, `_hash`, and the mem's change history are untouched). The record carries the caller-declared `role` plus actor/client identity and the entity's `_hash` at check time, appended to the workspace's append-only check ledger. Derived check state (`never_checked` | `checked_ok` | `check_failed` | `check_stale` — computed by hash comparison, never stamped) is served in `memstead_entity`'s opt-in `mutation_provenance` block and echoed here as `check_state`. Verdict vocabulary is closed (`ok` | `failed`); an unknown verdict refuses `INVALID_VERDICT`. Refuses typed on unknown entity (`ENTITY_NOT_FOUND`), read-only mems (`READ_ONLY_MOUNT`), and persistence failure (`CHECK_NOT_RECORDED`).",
        annotations(
            read_only_hint = false,
            destructive_hint = false,
            idempotent_hint = false,
            open_world_hint = false
        )
    )]
    fn memstead_check(&self, Parameters(p): Parameters<CheckParams>) -> CallToolResult {
        let Some(verdict) = memstead_base::check::Verdict::from_wire(&p.verdict) else {
            return tool_error_with_details(
                "INVALID_VERDICT",
                &format!(
                    "unknown verdict {:?} — the vocabulary is: {}",
                    p.verdict,
                    memstead_base::check::VERDICTS.join(", ")
                ),
                Some(serde_json::json!({ "allowed": memstead_base::check::VERDICTS })),
            );
        };
        let mut engine = crate::lock_engine!(self.engine);
        match resolve_role_lean(p.role.as_deref()) {
            Ok(r) => engine.set_role(r),
            Err(resp) => return *resp,
        }
        let (actor, client) = self.actor_and_client();
        let id = EntityId(p.entity);
        match engine.record_check(
            id.mem(),
            id.as_ref(),
            verdict,
            p.method.as_deref(),
            actor,
            client.as_ref(),
        ) {
            Ok(record) => {
                let (state, _) = match engine.entity_check_state(id.mem(), id.as_ref()) {
                    Ok(pair) => pair,
                    Err(e) => return engine_op_error(e),
                };
                json_response(&serde_json::json!({
                    "entity": record.entity,
                    "verdict": record.verdict,
                    "check_state": state.as_str(),
                    "role": record.role,
                    "ts": record.ts,
                    "method": record.method,
                }))
            }
            Err(e) => engine_op_error(e),
        }
    }

    #[tool(
        name = "memstead_overview",
        description = "Start here — the cold-start entry point for a Memstead engine. Returns the schema catalogue, the mem inventory, and the community clusters as Markdown. Every visible mem is listed under `## Mems`: a writable mem carries a `durable` flag and `storage` kind (an in-memory sketch reads `durable: false` / `storage: in-memory` — writes are volatile, evicted on session-TTL / restart), and a read-only mount carries `Access: read-only`, its deployment-declared trust `Origin` (`first-party` / `third-party`), and its own entity count. Per-mem counts, `_entity_count`, and the communities section always agree — one rendering authority. Schemas list as `{ref, description}` only — call `memstead_schema(name=<ref>)` for full per-type bodies. Token-budget-driven: hard-required content (mems, schema, community titles) always ships; heavy content greedy-fills the remaining budget by default-priority. Anything that didn't fit is advertised under `## Hints` with `estimated_tokens`; re-query by passing `key` into `include[]`. Allowed `include` keys: `community_members`, `community_bridges`, `mem_distribution`, `dangling_links`. `mem` scopes the roster, schema anchor, and communities to any one visible mem (a read-only mount included); a name matching no visible mem returns `UNKNOWN_MEM` whose list names every visible mem. Set `rebuild: true` to invalidate the community memo before computing — it recomputes the whole-graph Louvain partition (detection is global; there is no per-subgraph scoping). A small or disconnected subgraph may surface as no cluster: sparsely-connected / edge-less nodes collapse into a single catch-all rather than forming their own cluster. This surface carries no mem-lifecycle tools, so the `## Lifecycle Namespaces` section is omitted. Frontmatter `_overview_mode` is \"complete\", \"reduced\", or \"overbudget\"; `_mem_schema` appears only under a `mem` filter; `_workspace_root` is the serving engine's absolute workspace path (omitted for rootless in-memory engines).",
        annotations(
            read_only_hint = true,
            destructive_hint = false,
            idempotent_hint = true,
            open_world_hint = false
        )
    )]
    fn memstead_overview(&self, Parameters(p): Parameters<OverviewParams>) -> CallToolResult {
        // The lean surface renders the identical overview through the one shared
        // composer in `memstead_base::overview` (relocated there so this
        // no-`memstead-engine` build can reach it). The hand-rolled single-mem
        // renderer is gone: with one rendering authority the roster,
        // `_entity_count`, and the communities section can never disagree, and a
        // read-only mount appears with its `Access: read-only` / `Origin` lines
        // instead of being dropped. `suppress_lifecycle` is set because this
        // surface carries no mem-lifecycle tools, so naming them would be false.
        let mut engine = crate::lock_engine!(self.engine);
        let include = p.include.clone().unwrap_or_default();
        let args = memstead_base::overview::OverviewArgs {
            include: &include,
            mem: p.mem.as_deref(),
            rebuild: p.rebuild.unwrap_or(false) && p.chunk.unwrap_or(1) <= 1,
            token_budget: p
                .token_budget
                .unwrap_or(memstead_base::overview::DEFAULT_OVERVIEW_BUDGET),
            operator_mode: false,
            suppress_lifecycle: true,
        };
        match memstead_base::overview::compose_overview(
            &mut engine,
            args,
            memstead_base::overview::Surface::Mcp,
        ) {
            Ok(out) => md_response(out.markdown),
            Err(memstead_base::overview::ComposeOverviewError::InvalidIncludeKeySchemaTypes) => {
                tool_error(
                    "INVALID_INPUT",
                    "include key 'schema_types' was removed; \
                     call memstead_schema(name=...) for full schema bodies.",
                )
            }
            Err(memstead_base::overview::ComposeOverviewError::MemQuarantined(name)) => {
                engine_op_error(engine.unknown_mem_error(&name))
            }
            Err(memstead_base::overview::ComposeOverviewError::UnknownMem {
                name,
                writable_mems,
            }) => tool_error_with_details(
                "UNKNOWN_MEM",
                &format!(
                    "unknown mem: \"{name}\". Visible mems: [{}]",
                    writable_mems.join(", ")
                ),
                Some(serde_json::json!({ "name": name, "visible_mems": writable_mems })),
            ),
        }
    }
}

/// The lean (filesystem-mem) server's session-start instructions —
/// one named const so the registry-honesty tests read the SAME string
/// the handler serves. Built with `concat!` so the engine version is
/// baked in at compile time; the roster must name every tool this
/// flavour registers (bidirectionally test-enforced).
pub const FS_SERVER_INSTRUCTIONS: &str = concat!(
    "Memstead: schema-agnostic graph engine over typed, interconnected markdown entities. Each mem is a typed model of a chosen subject — its modal flavour (knowledge, planning, inquiry, spec, or any mix) follows from the schema the mem pins. Granularity: a mem is the packaged unit — a whole typed model, designed for 1,000-5,000 entities (operating costs measured in docs/sizing-curve.md; larger holdings work at proportionally higher load cost); an entity is never called a mem (a mem is not one 'memory'/fact). Cold-start: call memstead_overview first for the schema catalogue and mem inventory; read a mem's schema via memstead_schema before mutating.",
    " Engine version: ",
    env!("CARGO_PKG_VERSION"),
    " — serverInfo.version carries the same value; a version different from your last session means this surface may have changed: re-read the roster below. Tool roster (complete, 13 tools): READ — memstead_overview (workspace dashboard: schemas, mems, communities, quarantine roster), memstead_entity (one entity + _hash), memstead_search (text + metadata filter), memstead_schema (the pinned schema, lite/full), memstead_health (drift, conformance, quarantine roster, boot diagnosis), memstead_diff (two-ref structural diff), memstead_changes_since (change deltas for incremental sync). WRITE — memstead_create, memstead_update, memstead_relate, memstead_rename, memstead_delete (entity mutations, optimistic _hash locking). PROCESS — memstead_check (record a check of one entity: verdict ok|failed with method note; never a mutation — derived check state serves in memstead_entity's opt-in provenance block). CLI companion: the `memstead` CLI serves this same engine with verb families that deliberately live only there — bulk mutation (batch-create, batch-update, batch-relate: reach for these instead of looping single MCP mutation calls when writing many entities), archive export (export), distribution/registry (publish, unpublish, login, logout, domain), workspace bootstrap and repair (init, quickstart, projection migrate, schema install), and read/report verbs (status, list, context, due — the due-brief: open entities whose schema-declared due date falls inside a window, overdue first). If a task feels like N repetitive single-entity calls, check the CLI first."
);

#[tool_handler(router = FilesystemMcpServer::tool_router())]
impl ServerHandler for FilesystemMcpServer {
    /// Hand-written so `instructions` can be the named
    /// [`FS_SERVER_INSTRUCTIONS`] const (the macro only accepts string
    /// literals) and the serverInfo version is the engine's full
    /// build version (semver + git build sha for dev builds) by
    /// construction — the historical hardcoded `"0.1.0"` cannot recur,
    /// and two dev builds between releases stay distinguishable.
    fn get_info(&self) -> rmcp::model::ServerInfo {
        rmcp::model::ServerInfo::new(
            rmcp::model::ServerCapabilities::builder()
                .enable_tools()
                .build(),
        )
        .with_server_info(rmcp::model::Implementation::new(
            "memstead-mcp",
            memstead_base::build_info::full_version(),
        ))
        .with_instructions(FS_SERVER_INSTRUCTIONS.to_string())
    }

    async fn initialize(
        &self,
        request: InitializeRequestParams,
        context: RequestContext<RoleServer>,
    ) -> Result<InitializeResult, McpError> {
        let info = request.client_info.clone();
        let cid = ClientId {
            name: info.name.clone(),
            version: info.version.clone(),
        };
        let _ = self.client.set(cid);
        if context.peer.peer_info().is_none() {
            context.peer.set_peer_info(request);
        }
        Ok(self.get_info())
    }

    async fn list_tools(
        &self,
        _request: Option<PaginatedRequestParams>,
        _context: RequestContext<RoleServer>,
    ) -> Result<ListToolsResult, McpError> {
        let tools: Vec<Tool> = Self::tool_router().list_all();
        Ok(ListToolsResult {
            tools,
            meta: None,
            next_cursor: None,
        })
    }

    /// Friction-ledger seam (agent-trust plan 08), mirroring the full
    /// flavour: every dispatched typed refusal appends one
    /// content-free ledger entry, best-effort, after the response is
    /// built.
    async fn call_tool(
        &self,
        request: CallToolRequestParams,
        context: RequestContext<RoleServer>,
    ) -> Result<CallToolResult, McpError> {
        let verb = request.name.to_string();
        let tcc = rmcp::handler::server::tool::ToolCallContext::new(self, request, context);
        let result = Self::tool_router().call(tcc).await;
        if let Ok(r) = &result
            && r.is_error.unwrap_or(false)
            && let Some(code) = r
                .structured_content
                .as_ref()
                .and_then(|v| v.get("code"))
                .and_then(|c| c.as_str())
        {
            let details = r.structured_content.as_ref().and_then(|v| v.get("details"));
            memstead_base::friction::FrictionLedger::for_workspace(&self.workspace_root).record(
                "mcp",
                &verb,
                code,
                memstead_base::friction::closed_reason(code, details),
            );
        }
        result
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::tools::mutation::RelateOpInput;
    use indexmap::IndexMap;
    use memstead_base::filesystem::config::{WorkspaceConfig, write_workspace_config};
    use memstead_schema::SchemaRef;
    use rmcp::handler::server::wrapper::Parameters;
    use tempfile::TempDir;

    /// The lean MCP error map must emit the same wire code as
    /// `EngineError::code()` — the single source every surface (full MCP,
    /// CLI, wasm) follows. `Backend` and `ParseAfterWrite` historically
    /// shipped `MEM_WRITER_ERROR` / `PARSE_AFTER_WRITE` here, diverging
    /// from `code()`'s `MEM_ERROR` / `PARSE_ERROR`. Pin them so a
    /// re-divergence fails the build.
    #[test]
    fn lean_backend_and_parse_after_write_codes_follow_code_contract() {
        let cases: Vec<EngineError> = vec![
            EngineError::Backend(memstead_base::backend::BackendError::Other("disk".into())),
            EngineError::ParseAfterWrite("boom".into()),
        ];
        for err in cases {
            let expected = err.code();
            let result = engine_op_error(err);
            let code = result
                .structured_content
                .as_ref()
                .and_then(|v| v.get("code"))
                .and_then(|c| c.as_str())
                .expect("error envelope carries structured.code")
                .to_string();
            assert_eq!(
                code, expected,
                "lean error-map code drifted from EngineError::code()"
            );
        }
    }

    /// `memstead_relate` is idempotent on the lean surface, matching the
    /// mem-repo server: duplicate-add and remove-nonexistent are
    /// typed-warning no-ops, so a retry converges. The full-side
    /// annotation meta-test is `mem-repo`-gated; this lean-side test
    /// pins the parity so the two flavours cannot silently re-diverge.
    #[test]
    fn relate_annotation_is_idempotent_on_lean() {
        let tools = FilesystemMcpServer::tool_router().list_all();
        let relate = tools
            .iter()
            .find(|t| t.name == "memstead_relate")
            .expect("memstead_relate is on the lean surface");
        let ann = relate
            .annotations
            .as_ref()
            .expect("memstead_relate sets annotation hints");
        assert_eq!(
            ann.idempotent_hint,
            Some(true),
            "lean memstead_relate idempotent_hint must match the mem-repo server's `true`"
        );
    }

    fn write_workspace(tmp: &TempDir, name: &str) {
        let pin: SchemaRef = "default@1.0.0".parse().unwrap();
        let cfg = WorkspaceConfig::new(name, pin.clone());
        write_workspace_config(tmp.path(), &cfg).unwrap();
        // Two-layer file adapter markers — `Engine::from_workspace_root`
        // recognises a workspace by `.memstead/workspace.toml` plus the
        // mount list in `.memstead/state/mounts.json`.
        let memstead = tmp.path().join(".memstead");
        std::fs::write(
            memstead.join("workspace.toml"),
            "format = \"memstead-git-branch-2\"\n\n[persistence_adapter]\nname = \"file-two-layer\"\n",
        )
        .unwrap();
        let workspace = memstead_base::Workspace {
            mounts: vec![memstead_base::Mount {
                mem: name.to_string(),
                schema: Some(pin),
                storage: memstead_base::MountStorage::Folder {
                    path: tmp.path().to_path_buf(),
                },
                capability: memstead_base::MountCapability::Write,
                lifecycle: memstead_base::MountLifecycle::Eager,
                cross_linkable: true,
                migration_target: None,
            }],
            settings: memstead_base::WorkspaceSettings::default(),
        };
        use memstead_base::WorkspaceStoreAdapter;
        memstead_base::FileWorkspaceStore::new()
            .save_state(tmp.path(), &workspace)
            .unwrap();
    }

    #[test]
    fn poisoned_engine_lock_returns_typed_envelope_not_panic() {
        let tmp = TempDir::new().unwrap();
        write_workspace(&tmp, "demo");
        let server = FilesystemMcpServer::from_workspace_root(tmp.path()).unwrap();

        // Poison the engine mutex for real: a thread panics while
        // holding the guard.
        let engine = server.engine.clone();
        std::thread::spawn(move || {
            let _guard = engine.lock().unwrap();
            panic!("deliberate poison");
        })
        .join()
        .unwrap_err();

        let result = server.memstead_overview(Parameters(OverviewParams {
            rebuild: None,
            chunk: None,
            mem: None,
            include: None,
            token_budget: None,
        }));
        assert_eq!(result.is_error, Some(true));
        let code = result
            .structured_content
            .as_ref()
            .and_then(|v| v.get("code"))
            .and_then(|c| c.as_str())
            .unwrap();
        assert_eq!(code, "ENGINE_LOCK_POISONED");
    }

    #[test]
    fn create_then_entity_round_trip() {
        let tmp = TempDir::new().unwrap();
        write_workspace(&tmp, "demo");
        let server = FilesystemMcpServer::from_workspace_root(tmp.path()).unwrap();

        // memstead_create — the engine refuses on missing required
        // sections, so seed `identity` + `purpose` so the spec lands.
        let mut sections = IndexMap::new();
        sections.insert("identity".to_string(), "first identity".to_string());
        sections.insert("purpose".to_string(), "first purpose".to_string());
        let create_params = CreateParams {
            anchors: None,
            title: "First".to_string(),
            entity_type: "spec".to_string(),
            mem: None,
            sections: Some(sections),
            metadata: None,
            relations: None,
            dry_run: None,
            note: Some("first via mcp".to_string()),
            role: None,
        };
        let create_result = server.memstead_create(Parameters(create_params));
        assert!(
            !create_result.is_error.unwrap_or(false),
            "create must succeed: {:?}",
            create_result.structured_content,
        );
        let create_body = create_result
            .structured_content
            .as_ref()
            .expect("structured content");
        let id = create_body["id"].as_str().unwrap().to_string();
        assert_eq!(id, "demo--first");

        // Changelog has the note.
        let log =
            std::fs::read_to_string(tmp.path().join(".memstead").join("changes.jsonl")).unwrap();
        assert!(log.contains("\"note\":\"first via mcp\""));

        // memstead_entity
        let entity_params = EntityParams {
            id: id.clone(),
            sections: None,
            include_relations: None,
            include_context: None,
            token_budget: None,
            chunk: None,
            include_provenance: None,
        };
        let entity_result = server.memstead_entity(Parameters(entity_params));
        assert!(!entity_result.is_error.unwrap_or(false));
        let text = match entity_result.content.first() {
            Some(c) => match c.as_text() {
                Some(t) => t.text.clone(),
                None => panic!("expected text"),
            },
            None => panic!("expected at least one content"),
        };
        assert!(text.contains("# First"));
        assert!(text.contains("_hash:"));
    }

    /// Build a two-mount engine — a writable in-memory `sketch` mem
    /// (declared first) and a read-only in-memory `content` mem — so the
    /// create handler's multi-mount mem resolution is exercised at this
    /// layer. Mirrors the session server's two-tier shape.
    fn two_mount_engine() -> memstead_base::Engine {
        use memstead_base::backend::MemBackend;
        use memstead_base::storage::InMemoryBackend;
        use memstead_base::{Mount, MountCapability, MountLifecycle, MountStorage};
        let pin: SchemaRef = "default@1.0.0".parse().unwrap();
        let build = |name: &str, cap: MountCapability| -> (Mount, Box<dyn MemBackend>) {
            let backend = InMemoryBackend::new();
            let cfg = format!(r#"{{"version":"0.1.0","schema":"{pin}"}}"#).into_bytes();
            backend.write_mem_config(&cfg).unwrap();
            let mount = Mount {
                mem: name.to_string(),
                schema: Some(pin.clone()),
                storage: MountStorage::InMemory,
                capability: cap,
                lifecycle: MountLifecycle::Eager,
                cross_linkable: true,
                migration_target: None,
            };
            (mount, Box::new(backend) as Box<dyn MemBackend>)
        };
        memstead_base::Engine::from_mounts(vec![
            build("sketch", MountCapability::Write),
            build("content", MountCapability::ReadOnly),
        ])
        .unwrap()
    }

    fn spec_sections() -> Option<IndexMap<String, String>> {
        let mut s = IndexMap::new();
        s.insert("identity".to_string(), "i".to_string());
        s.insert("purpose".to_string(), "p".to_string());
        Some(s)
    }

    fn create_params(title: &str, mem: Option<&str>) -> CreateParams {
        CreateParams {
            anchors: None,
            title: title.to_string(),
            entity_type: "spec".to_string(),
            mem: mem.map(String::from),
            sections: spec_sections(),
            metadata: None,
            relations: None,
            dry_run: None,
            note: None,
            role: None,
        }
    }

    /// Multi-mount create-targeting: with a writable `sketch` and a
    /// read-only `content` mem mounted together, an omitted `mem` lands
    /// in the writable mount (not the alphabetically-first read-only one);
    /// an explicit read-only target is refused with READ_ONLY_MOUNT rather
    /// than silently redirected; an explicit writable target is honoured.
    #[test]
    fn create_resolves_target_mem_across_multiple_mounts() {
        let server =
            FilesystemMcpServer::from_engine(two_mount_engine(), std::path::PathBuf::new());

        // Omitted mem → default writable mount (`sketch`).
        let r = server.memstead_create(Parameters(create_params("Default Target", None)));
        assert!(
            !r.is_error.unwrap_or(false),
            "omitted-mem create must land in the writable mount: {:?}",
            r.structured_content
        );
        assert_eq!(
            r.structured_content.as_ref().unwrap()["id"].as_str(),
            Some("sketch--default-target"),
            "create defaults to the writable mem, not the read-only one"
        );

        // Explicit read-only mem → typed refusal, not a redirect.
        let r = server.memstead_create(Parameters(create_params("Into Content", Some("content"))));
        assert!(
            r.is_error.unwrap_or(false),
            "write to a read-only mount must refuse"
        );
        assert_eq!(
            r.structured_content.unwrap()["code"],
            "READ_ONLY_MOUNT",
            "the engine capability layer refuses the read-only target"
        );

        // Explicit writable mem → honoured.
        let r =
            server.memstead_create(Parameters(create_params("Explicit Sketch", Some("sketch"))));
        assert!(
            !r.is_error.unwrap_or(false),
            "explicit writable target must land: {:?}",
            r.structured_content
        );
        assert_eq!(
            r.structured_content.as_ref().unwrap()["id"].as_str(),
            Some("sketch--explicit-sketch")
        );
    }

    /// Plan 03, Part B: params this surface hardwires off are REFUSED with
    /// a typed `UNSUPPORTED_PARAM` naming them — never silently dropped. The
    /// `dry_run` case is the load-bearing one: silently treating it as a real
    /// write would land an entity the agent thought was a preview.
    #[test]
    fn unsupported_write_params_refuse_rather_than_silently_drop() {
        use crate::tools::mutation::RelationInput;
        let tmp = TempDir::new().unwrap();
        write_workspace(&tmp, "demo");
        let server = FilesystemMcpServer::from_workspace_root(tmp.path()).unwrap();

        let dropped = |r: &CallToolResult| -> Vec<String> {
            r.structured_content.as_ref().unwrap()["details"]["params"]
                .as_array()
                .unwrap()
                .iter()
                .map(|v| v.as_str().unwrap().to_string())
                .collect()
        };
        let is_unsupported = |r: &CallToolResult| {
            r.is_error.unwrap_or(false)
                && r.structured_content.as_ref().unwrap()["code"] == "UNSUPPORTED_PARAM"
        };

        // create + dry_run: true → refused up front; nothing lands.
        let mut p = create_params("Preview Me", None);
        p.dry_run = Some(true);
        let r = server.memstead_create(Parameters(p));
        assert!(is_unsupported(&r), "dry_run create must refuse: {r:?}");
        assert!(dropped(&r).contains(&"dry_run".to_string()));
        // The refusal precedes the engine, so the preview entity never lands.
        let entity = server.memstead_entity(Parameters(EntityParams {
            id: "demo--preview-me".into(),
            include_relations: None,
            include_context: None,
            sections: None,
            token_budget: None,
            chunk: None,
            include_provenance: None,
        }));
        assert!(
            entity.is_error.unwrap_or(false),
            "a refused dry_run must NOT have created the entity"
        );

        // create + non-empty relations → refused, naming relations.
        let mut p = create_params("With Edges", None);
        p.relations = Some(vec![RelationInput {
            to: "demo--target".into(),
            r#type: "REFERENCES".into(),
            description: None,
        }]);
        let r = server.memstead_create(Parameters(p));
        assert!(is_unsupported(&r));
        assert!(dropped(&r).contains(&"relations".to_string()));

        // update + each unsupported param → refused naming it.
        let base = || UpdateParams {
            anchors: None,
            id: "demo--anything".into(),
            expected_hash: "deadbeef".into(),
            sections: None,
            append_sections: None,
            patch_sections: None,
            metadata: None,
            metadata_unset: None,
            dry_run: None,
            declare_relations: None,
            relations_unset: None,
            anchors_unset: None,
            note: None,
            role: None,
        };
        let mut u = base();
        u.append_sections = Some(IndexMap::from([("purpose".to_string(), "x".to_string())]));
        let r = server.memstead_update(Parameters(u));
        assert!(is_unsupported(&r));
        assert!(dropped(&r).contains(&"append_sections".to_string()));

        let mut u = base();
        u.patch_sections = Some(IndexMap::from([(
            "purpose".to_string(),
            crate::tools::mutation::PatchInput {
                old: "a".into(),
                new: "b".into(),
                all: None,
            },
        )]));
        let r = server.memstead_update(Parameters(u));
        assert!(is_unsupported(&r));
        assert!(dropped(&r).contains(&"patch_sections".to_string()));

        let mut u = base();
        u.dry_run = Some(true);
        let r = server.memstead_update(Parameters(u));
        assert!(is_unsupported(&r));
        assert!(dropped(&r).contains(&"dry_run".to_string()));

        // relate + dry_run: true → refused up front (the unified
        // engine's rehearsal contract does NOT silently erode into a
        // real write here); dry_run absent / false stays served.
        let relate = |dry: Option<bool>| {
            server.memstead_relate(Parameters(crate::tools::mutation::RelateParams {
                relations: vec![crate::tools::mutation::RelateOpInput {
                    from: "demo--anything".into(),
                    to: "demo--other".into(),
                    r#type: "REFERENCES".into(),
                    remove: None,
                    description: None,
                }],
                note: None,
                role: None,
                dry_run: dry,
            }))
        };
        let r = relate(Some(true));
        assert!(is_unsupported(&r), "dry_run relate must refuse: {r:?}");
        assert!(dropped(&r).contains(&"dry_run".to_string()));
        // dry_run: false / absent is not a meaningful supply — the
        // call proceeds to the engine (and fails only on the missing
        // source entity, not on UNSUPPORTED_PARAM).
        let r = relate(Some(false));
        assert!(
            !(r.is_error.unwrap_or(false)
                && r.structured_content.as_ref().unwrap()["code"] == "UNSUPPORTED_PARAM"),
            "dry_run: false must not refuse: {r:?}"
        );
    }

    /// The `open_questions` axis on the lean flavour (agent-trust
    /// plan 11): include-gated — absent without the include, an
    /// empty per-mem worklist with it, never an error on a hole-free
    /// mem.
    #[test]
    fn open_questions_axis_is_include_gated_on_lean() {
        let tmp = TempDir::new().unwrap();
        write_workspace(&tmp, "demo");
        let server = FilesystemMcpServer::from_workspace_root(tmp.path()).unwrap();

        let plain = server.memstead_health(Parameters(HealthParams::default()));
        assert!(!plain.is_error.unwrap_or(false));
        assert!(
            plain
                .structured_content
                .as_ref()
                .unwrap()
                .get("open_questions")
                .is_none(),
            "axis must be include-gated on lean"
        );

        let served = server.memstead_health(Parameters(HealthParams {
            include: Some(vec!["open_questions".to_string()]),
            ..Default::default()
        }));
        assert!(!served.is_error.unwrap_or(false));
        let axis = &served.structured_content.as_ref().unwrap()["open_questions"];
        assert_eq!(axis["_item_cap"], 20, "{axis}");
        assert_eq!(axis["demo"]["total_open"], 0, "{axis}");
    }

    /// Refusal complement (Part B): a defaulted-empty / absent unsupported
    /// param is left alone — the surface stays backward-compatible for a
    /// caller that harmlessly passes nothing. A plain create/update with the
    /// supported params still succeeds.
    #[test]
    fn absent_unsupported_params_do_not_refuse() {
        let tmp = TempDir::new().unwrap();
        write_workspace(&tmp, "demo");
        let server = FilesystemMcpServer::from_workspace_root(tmp.path()).unwrap();

        // create with dry_run: None / relations: None → no UNSUPPORTED_PARAM.
        let r = server.memstead_create(Parameters(create_params("Plain Create", None)));
        assert!(
            !r.is_error.unwrap_or(false),
            "a default create must not be refused: {r:?}"
        );
        // dry_run: Some(false) is the caller intending no preview — also fine.
        let mut p = create_params("Explicit No Preview", None);
        p.dry_run = Some(false);
        let r = server.memstead_create(Parameters(p));
        assert!(
            !r.is_error.unwrap_or(false),
            "dry_run: false must not be refused: {r:?}"
        );
    }

    #[test]
    fn create_rejects_unknown_type_with_typed_code() {
        let tmp = TempDir::new().unwrap();
        write_workspace(&tmp, "demo");
        let server = FilesystemMcpServer::from_workspace_root(tmp.path()).unwrap();

        let result = server.memstead_create(Parameters(CreateParams {
            anchors: None,
            title: "X".into(),
            entity_type: "totally-not-a-type".into(),
            mem: None,
            sections: None,
            metadata: None,
            relations: None,
            dry_run: None,
            note: None,
            role: None,
        }));
        assert!(result.is_error.unwrap_or(false));
        let body = result.structured_content.unwrap();
        assert_eq!(body["code"], "UNKNOWN_ENTITY_TYPE");
    }

    /// Smoke-test probe A: a memo created with sections that belong
    /// to a different type (here `identity` + `purpose`, which are
    /// `spec` sections, not `memo`'s `claim` + `context`) must reject
    /// with `UNKNOWN_SECTION` before any disk write lands.
    #[test]
    fn create_rejects_unknown_section_keys() {
        let tmp = TempDir::new().unwrap();
        write_workspace(&tmp, "demo");
        let server = FilesystemMcpServer::from_workspace_root(tmp.path()).unwrap();

        let mut sections = IndexMap::new();
        sections.insert("identity".to_string(), "Some text".to_string());
        sections.insert("purpose".to_string(), "Other text".to_string());

        let result = server.memstead_create(Parameters(CreateParams {
            anchors: None,
            title: "Stray Memo".into(),
            entity_type: "memo".into(),
            mem: None,
            sections: Some(sections),
            metadata: None,
            relations: None,
            dry_run: None,
            note: None,
            role: None,
        }));
        assert!(result.is_error.unwrap_or(false));
        let body = result.structured_content.unwrap();
        assert_eq!(body["code"], "UNKNOWN_SECTION");
        // The first offender hits first; either key would be valid.
        let bad_key = body["details"]["key"].as_str().unwrap();
        assert!(
            bad_key == "identity" || bad_key == "purpose",
            "expected identity/purpose, got {bad_key}"
        );
        // No file should have been written.
        assert!(
            !tmp.path().join("stray-memo.md").exists(),
            "stray memo should not have been persisted"
        );
    }

    /// Smoke-test probe B: a memo with no sections at all should
    /// succeed (required-section gaps are Tier-2 warnings, not hard
    /// errors), but the response must carry a `MISSING_REQUIRED_SECTION`
    /// warning per missing required section so the agent can
    /// self-correct.
    #[test]
    fn create_refuses_missing_required_section_with_typed_envelope() {
        // `memstead_create` refuses on missing required sections instead
        // of emitting warnings. Pre-fix the entity landed with empty
        // placeholders for each missing required section and the
        // install-time strict validator could later refuse the
        // resulting archive — the export-then-install round-trip
        // broke. Now the refusal fires at the write boundary.
        let tmp = TempDir::new().unwrap();
        write_workspace(&tmp, "demo");
        let server = FilesystemMcpServer::from_workspace_root(tmp.path()).unwrap();

        let result = server.memstead_create(Parameters(CreateParams {
            anchors: None,
            title: "Empty Memo".into(),
            entity_type: "memo".into(),
            mem: None,
            sections: Some(IndexMap::new()),
            metadata: None,
            relations: None,
            dry_run: None,
            note: None,
            role: None,
        }));
        assert!(result.is_error.unwrap_or(false), "create must refuse");
        let body = result.structured_content.unwrap();
        assert_eq!(body["code"], "MISSING_REQUIRED_SECTION");
        assert_eq!(body["details"]["entity_type"], "memo");
        assert!(
            body["details"]["sections"]
                .as_array()
                .map_or(0, |s| s.len())
                >= 1,
            "details.sections must list at least one missing key, got: {body}"
        );
        assert!(
            body["details"]["type_guidance"].is_object(),
            "details.type_guidance must be a map, got: {body}"
        );
    }

    /// Smoke-test probe C: an out-of-enum metadata value (`level: "Z3"`
    /// against the `M0|M1|M2|M3` allowed set) must reject with
    /// `INVALID_ENUM_VALUE`.
    #[test]
    fn create_rejects_out_of_enum_metadata_value() {
        let tmp = TempDir::new().unwrap();
        write_workspace(&tmp, "demo");
        let server = FilesystemMcpServer::from_workspace_root(tmp.path()).unwrap();

        let mut metadata = IndexMap::new();
        metadata.insert("level".to_string(), "Z3".to_string());

        let result = server.memstead_create(Parameters(CreateParams {
            anchors: None,
            title: "Bad Level".into(),
            entity_type: "spec".into(),
            mem: None,
            sections: None,
            metadata: Some(metadata),
            relations: None,
            dry_run: None,
            note: None,
            role: None,
        }));
        assert!(result.is_error.unwrap_or(false));
        let body = result.structured_content.unwrap();
        assert_eq!(body["code"], "INVALID_ENUM_VALUE");
        assert_eq!(body["details"]["field"], "level");
        assert_eq!(body["details"]["value"], "Z3");
    }

    /// Unknown metadata fields surface `UNKNOWN_METADATA_FIELD`. The
    /// mem-repo path emits the same code; this guard pins the
    /// filesystem-mem contract so future refactors don't silently
    /// drop the gate.
    #[test]
    fn create_rejects_unknown_metadata_field() {
        let tmp = TempDir::new().unwrap();
        write_workspace(&tmp, "demo");
        let server = FilesystemMcpServer::from_workspace_root(tmp.path()).unwrap();

        let mut metadata = IndexMap::new();
        metadata.insert("nonsense".to_string(), "value".to_string());

        let result = server.memstead_create(Parameters(CreateParams {
            anchors: None,
            title: "Stray Field".into(),
            entity_type: "spec".into(),
            mem: None,
            sections: None,
            metadata: Some(metadata),
            relations: None,
            dry_run: None,
            note: None,
            role: None,
        }));
        assert!(result.is_error.unwrap_or(false));
        let body = result.structured_content.unwrap();
        assert_eq!(body["code"], "UNKNOWN_METADATA_FIELD");
        assert_eq!(body["details"]["key"], "nonsense");
    }

    #[test]
    fn entity_not_found_returns_typed_code() {
        let tmp = TempDir::new().unwrap();
        write_workspace(&tmp, "demo");
        let server = FilesystemMcpServer::from_workspace_root(tmp.path()).unwrap();

        let result = server.memstead_entity(Parameters(EntityParams {
            id: "demo--ghost".into(),
            sections: None,
            include_relations: None,
            include_context: None,
            token_budget: None,
            chunk: None,
            include_provenance: None,
        }));
        assert!(result.is_error.unwrap_or(false));
        let body = result.structured_content.unwrap();
        assert_eq!(body["code"], "ENTITY_NOT_FOUND");
    }

    fn seed_via_mcp(server: &FilesystemMcpServer, title: &str) -> (String, String) {
        // The
        // engine refuses on missing required sections. Seed the
        // `spec` type's required `identity` + `purpose` sections so
        // wire-shape tests using this helper continue to land valid
        // entities.
        let mut seeded_sections = IndexMap::new();
        seeded_sections.insert("identity".to_string(), "seed identity".to_string());
        seeded_sections.insert("purpose".to_string(), "seed purpose".to_string());
        let result = server.memstead_create(Parameters(CreateParams {
            anchors: None,
            title: title.into(),
            entity_type: "spec".into(),
            mem: None,
            sections: Some(seeded_sections),
            metadata: None,
            relations: None,
            dry_run: None,
            note: None,
            role: None,
        }));
        assert!(
            !result.is_error.unwrap_or(false),
            "seed_via_mcp must succeed; got error: {:?}",
            result.structured_content,
        );
        let body = result.structured_content.unwrap();
        (
            body["id"].as_str().unwrap().to_string(),
            body["_hash"].as_str().unwrap().to_string(),
        )
    }

    #[test]
    fn update_replaces_section_and_returns_new_hash() {
        let tmp = TempDir::new().unwrap();
        write_workspace(&tmp, "demo");
        let server = FilesystemMcpServer::from_workspace_root(tmp.path()).unwrap();
        let (id, hash) = seed_via_mcp(&server, "Updatable");

        let mut sections = indexmap::IndexMap::new();
        sections.insert("identity".to_string(), "Updated body.".to_string());
        let result = server.memstead_update(Parameters(UpdateParams {
            anchors: None,
            relations_unset: None,
            anchors_unset: None,
            id: id.clone(),
            expected_hash: hash.clone(),
            sections: Some(sections),
            append_sections: None,
            patch_sections: None,
            metadata: None,
            metadata_unset: None,
            dry_run: None,
            note: Some("touched body".into()),
            role: None,
            declare_relations: None,
        }));
        assert!(!result.is_error.unwrap_or(false));
        let body = result.structured_content.unwrap();
        let new_hash = body["_hash"].as_str().unwrap();
        assert_ne!(new_hash, hash);
        assert_eq!(body["modified_sections"][0], "identity");
    }

    #[test]
    fn update_rejects_stale_hash_with_typed_code() {
        let tmp = TempDir::new().unwrap();
        write_workspace(&tmp, "demo");
        let server = FilesystemMcpServer::from_workspace_root(tmp.path()).unwrap();
        let (id, _hash) = seed_via_mcp(&server, "Pinned");

        let result = server.memstead_update(Parameters(UpdateParams {
            anchors: None,
            relations_unset: None,
            anchors_unset: None,
            id,
            expected_hash: "0000000000".into(),
            sections: None,
            append_sections: None,
            patch_sections: None,
            metadata: None,
            metadata_unset: None,
            dry_run: None,
            note: None,
            role: None,
            declare_relations: None,
        }));
        assert!(result.is_error.unwrap_or(false));
        let body = result.structured_content.unwrap();
        assert_eq!(body["code"], "HASH_MISMATCH");
        assert!(body["details"]["current"].is_string());
    }

    /// `memstead_update` must reject any attempt to mutate the read-only
    /// metadata triple (`mem`, `id`, `type`) on either set or unset
    /// — the entity-id contract depends on those staying stable.
    #[test]
    fn update_rejects_read_only_metadata_set() {
        let tmp = TempDir::new().unwrap();
        write_workspace(&tmp, "demo");
        let server = FilesystemMcpServer::from_workspace_root(tmp.path()).unwrap();
        let (id, hash) = seed_via_mcp(&server, "Locked");

        for field in ["mem", "id", "type"] {
            let mut metadata = IndexMap::new();
            metadata.insert(field.to_string(), "garbage".to_string());
            let result = server.memstead_update(Parameters(UpdateParams {
                anchors: None,
                relations_unset: None,
                anchors_unset: None,
                id: id.clone(),
                expected_hash: hash.clone(),
                sections: None,
                append_sections: None,
                patch_sections: None,
                metadata: Some(metadata),
                metadata_unset: None,
                dry_run: None,
                note: None,
                role: None,
                declare_relations: None,
            }));
            assert!(
                result.is_error.unwrap_or(false),
                "set of {field} should error"
            );
            let body = result.structured_content.unwrap();
            assert_eq!(body["code"], "READ_ONLY_FIELD");
            assert_eq!(body["details"]["field"], field);
        }
    }

    /// `metadata_unset` is the asymmetric half of the reservation:
    /// unsetting a reserved key is ALLOWED (the sanctioned repair for a
    /// historically smuggled key — on a healthy entity it is a
    /// committed-nothing no-op, and `type` is engine-re-seeded so the
    /// entity never goes typeless), while the engine-stamped timestamp
    /// fields stay refused on unset.
    #[test]
    fn update_allows_reserved_metadata_unset_but_refuses_timestamp_unset() {
        let tmp = TempDir::new().unwrap();
        write_workspace(&tmp, "demo");
        let server = FilesystemMcpServer::from_workspace_root(tmp.path()).unwrap();
        let (id, hash) = seed_via_mcp(&server, "Unsettable");

        let unset_params = |keys: Vec<&str>| {
            Parameters(UpdateParams {
                anchors: None,
                relations_unset: None,
                anchors_unset: None,
                id: id.clone(),
                expected_hash: hash.clone(),
                sections: None,
                append_sections: None,
                patch_sections: None,
                metadata: None,
                metadata_unset: Some(keys.into_iter().map(String::from).collect()),
                dry_run: None,
                note: None,
                role: None,
                declare_relations: None,
            })
        };

        // Reserved triple: unset succeeds. On this healthy entity it is
        // a no-op (nothing was smuggled), surfaced as UPDATE_NOOP.
        for field in ["type", "mem", "id"] {
            let result = server.memstead_update(unset_params(vec![field]));
            assert!(
                !result.is_error.unwrap_or(false),
                "unset of reserved '{field}' must be allowed (repair route)"
            );
            let body = result.structured_content.unwrap();
            assert!(
                body["warnings"]
                    .as_array()
                    .is_some_and(|w| w.iter().any(|e| e["code"] == "UPDATE_NOOP")),
                "healthy-entity reserved unset is a no-op: {body}"
            );
        }

        // Engine-stamped timestamps: unset still refuses.
        let result = server.memstead_update(unset_params(vec!["last_modified"]));
        assert!(result.is_error.unwrap_or(false));
        let body = result.structured_content.unwrap();
        assert_eq!(body["code"], "READ_ONLY_FIELD");
        assert_eq!(body["details"]["field"], "last_modified");
    }

    /// The virtual `relationships` surface is managed by `memstead_relate`,
    /// not `memstead_update`. Writes there reject with
    /// `SECTION_NOT_UPDATABLE` so an agent does not bypass the
    /// rel-validation pipeline by treating relationships as a section.
    #[test]
    fn update_rejects_relationships_section_as_not_updatable() {
        let tmp = TempDir::new().unwrap();
        write_workspace(&tmp, "demo");
        let server = FilesystemMcpServer::from_workspace_root(tmp.path()).unwrap();
        let (id, hash) = seed_via_mcp(&server, "Sneaky");

        let mut sections = IndexMap::new();
        sections.insert("relationships".to_string(), "- KIND: target".to_string());
        let result = server.memstead_update(Parameters(UpdateParams {
            anchors: None,
            relations_unset: None,
            anchors_unset: None,
            id,
            expected_hash: hash,
            sections: Some(sections),
            append_sections: None,
            patch_sections: None,
            metadata: None,
            metadata_unset: None,
            dry_run: None,
            note: None,
            role: None,
            declare_relations: None,
        }));
        assert!(result.is_error.unwrap_or(false));
        let body = result.structured_content.unwrap();
        // The relationships sneak path is gated by validate_section_keys
        // first (the schema doesn't declare a `relationships` section),
        // so it fires UNKNOWN_SECTION. Either gate is correct — both
        // close the bypass.
        let code = body["code"].as_str().unwrap();
        assert!(
            code == "SECTION_NOT_UPDATABLE" || code == "UNKNOWN_SECTION",
            "expected SECTION_NOT_UPDATABLE or UNKNOWN_SECTION, got {code}"
        );
    }

    #[test]
    fn delete_removes_entity_and_logs_change() {
        let tmp = TempDir::new().unwrap();
        write_workspace(&tmp, "demo");
        let server = FilesystemMcpServer::from_workspace_root(tmp.path()).unwrap();
        let (id, hash) = seed_via_mcp(&server, "Doomed");

        let result = server.memstead_delete(Parameters(DeleteParams {
            id: id.clone(),
            expected_hash: hash,
            note: Some("retired".into()),
            role: None,
        }));
        assert!(!result.is_error.unwrap_or(false));
        let body = result.structured_content.unwrap();
        assert_eq!(body["id"], id);

        let log =
            std::fs::read_to_string(tmp.path().join(".memstead").join("changes.jsonl")).unwrap();
        assert!(log.contains("\"kind\":\"delete\""));
        assert!(log.contains("\"note\":\"retired\""));
    }

    #[test]
    fn relate_appends_then_no_op_on_duplicate() {
        let tmp = TempDir::new().unwrap();
        write_workspace(&tmp, "demo");
        let server = FilesystemMcpServer::from_workspace_root(tmp.path()).unwrap();
        let (from, _) = seed_via_mcp(&server, "Source");
        let (to, _) = seed_via_mcp(&server, "Target");

        let added = server.memstead_relate(Parameters(RelateParams {
            relations: vec![RelateOpInput {
                from: from.clone(),
                to: to.clone(),
                r#type: "USES".into(),
                remove: None,
                description: None,
            }],
            note: Some("first".into()),
            role: None,
            dry_run: None,
        }));
        assert!(!added.is_error.unwrap_or(false));
        assert_eq!(
            added.structured_content.unwrap()["results"][0]["action"],
            "added"
        );

        let dup = server.memstead_relate(Parameters(RelateParams {
            relations: vec![RelateOpInput {
                from: from.clone(),
                to: to.clone(),
                r#type: "USES".into(),
                remove: None,
                description: None,
            }],
            note: None,
            role: None,
            dry_run: None,
        }));
        assert!(!dup.is_error.unwrap_or(false));
        let dup_body = dup.structured_content.unwrap();
        assert_eq!(dup_body["results"][0]["action"], "noop");
        assert!(
            dup_body["warnings"]
                .as_array()
                .is_some_and(|w| w.iter().any(|x| x["code"] == "DUPLICATE_RELATIONSHIP")),
            "duplicate add must warn typed: {dup_body}"
        );
    }

    /// Strict-mode schemas (the default) reject undeclared
    /// relationship names with `INVALID_REL_TYPE`. The recovery
    /// envelope carries the canonical vocabulary on
    /// `details.allowed[]` so the agent can self-correct in one
    /// round trip without a follow-up `memstead_overview`.
    #[test]
    fn relate_rejects_undeclared_rel_type() {
        let tmp = TempDir::new().unwrap();
        write_workspace(&tmp, "demo");
        let server = FilesystemMcpServer::from_workspace_root(tmp.path()).unwrap();
        let (from, _) = seed_via_mcp(&server, "Source");
        let (to, _) = seed_via_mcp(&server, "Target");

        let result = server.memstead_relate(Parameters(RelateParams {
            relations: vec![RelateOpInput {
                from: from.clone(),
                to: to.clone(),
                r#type: "TOTALLY_MADE_UP".into(),
                remove: None,
                description: None,
            }],
            note: None,
            role: None,
            dry_run: None,
        }));
        assert!(result.is_error.unwrap_or(false));
        let body = result.structured_content.unwrap();
        assert_eq!(body["code"], "INVALID_REL_TYPE");
        assert_eq!(body["details"]["input"], "TOTALLY_MADE_UP");
        let allowed = body["details"]["allowed"]
            .as_array()
            .expect("allowed should be array");
        assert!(!allowed.is_empty(), "allowed[] must list real edges");
    }

    #[test]
    fn relate_rejects_cross_mem_target() {
        let tmp = TempDir::new().unwrap();
        write_workspace(&tmp, "demo");
        let server = FilesystemMcpServer::from_workspace_root(tmp.path()).unwrap();
        let (from, _) = seed_via_mcp(&server, "Source");

        let result = server.memstead_relate(Parameters(RelateParams {
            relations: vec![RelateOpInput {
                from: from.clone(),
                to: "other--thing".into(),
                r#type: "USES".into(),
                remove: None,
                description: None,
            }],
            note: None,
            role: None,
            dry_run: None,
        }));
        assert!(result.is_error.unwrap_or(false));
        assert_eq!(
            result.structured_content.unwrap()["code"],
            "CROSS_MEM_LINK_NOT_ALLOWED"
        );
    }

    #[test]
    fn search_with_empty_query_returns_seeded_entities() {
        let tmp = TempDir::new().unwrap();
        write_workspace(&tmp, "demo");
        let server = FilesystemMcpServer::from_workspace_root(tmp.path()).unwrap();
        for title in ["Alpha", "Beta", "Gamma"] {
            seed_via_mcp(&server, title);
        }

        let result = server.memstead_search(Parameters(SearchParams {
            query: None,
            mem: None,
            entity_type: None,
            expand_via: None,
            expand_depth: None,
            related_to: None,
            depth: None,
            edge_type: None,
            limit: None,
            offset: None,
            filters: None,
            range_filters: None,
            stub: Some(false),
            token_budget: None,
            direction: None,
        }));
        assert!(!result.is_error.unwrap_or(false));
        let md = result
            .content
            .iter()
            .filter_map(|c| c.as_text().map(|t| t.text.as_str()))
            .collect::<Vec<_>>()
            .join("\n");
        assert!(md.contains("_total: 3"), "expected _total: 3 in: {md}");
        for title in ["Alpha", "Beta", "Gamma"] {
            assert!(md.contains(title), "expected title {title} in: {md}");
        }
    }

    /// Malformed `range_filters` key (no `min_`/`max_`/`*_before`/`*_after`
    /// shape) refuses with `RANGE_FILTER_KEY_MALFORMED`. An earlier
    /// MCP `SearchParams` had no `range_filters` field at all so the
    /// engine never saw the input — silent no-op.
    #[test]
    fn search_range_filter_malformed_key_surfaces_typed_warning() {
        let tmp = TempDir::new().unwrap();
        write_workspace(&tmp, "demo");
        let server = FilesystemMcpServer::from_workspace_root(tmp.path()).unwrap();
        seed_via_mcp(&server, "Seed");

        let mut range_filters = std::collections::HashMap::new();
        range_filters.insert("malformedkey".to_string(), "10".to_string());

        let result = server.memstead_search(Parameters(SearchParams {
            query: None,
            mem: None,
            entity_type: None,
            expand_via: None,
            expand_depth: None,
            related_to: None,
            depth: None,
            edge_type: None,
            limit: None,
            offset: None,
            filters: None,
            range_filters: Some(range_filters),
            stub: Some(false),
            token_budget: None,
            direction: None,
        }));
        let sc = result
            .structured_content
            .as_ref()
            .expect("range-filter warning must ride on structured_content");
        let warnings = sc["warnings"]
            .as_array()
            .expect("warnings array on the structured envelope");
        assert!(
            warnings
                .iter()
                .any(|w| w["code"] == "RANGE_FILTER_KEY_MALFORMED"),
            "expected RANGE_FILTER_KEY_MALFORMED warning, got: {warnings:?}",
        );
    }

    /// Unknown range-filter field surfaces
    /// `UNKNOWN_RANGE_FILTER_FIELD` with the derived field name.
    #[test]
    fn search_range_filter_unknown_field_surfaces_typed_warning() {
        let tmp = TempDir::new().unwrap();
        write_workspace(&tmp, "demo");
        let server = FilesystemMcpServer::from_workspace_root(tmp.path()).unwrap();
        seed_via_mcp(&server, "Seed");

        let mut range_filters = std::collections::HashMap::new();
        range_filters.insert("min_fake_field".to_string(), "10".to_string());

        let result = server.memstead_search(Parameters(SearchParams {
            query: None,
            mem: None,
            entity_type: Some("spec".to_string()),
            expand_via: None,
            expand_depth: None,
            related_to: None,
            depth: None,
            edge_type: None,
            limit: None,
            offset: None,
            filters: None,
            range_filters: Some(range_filters),
            stub: Some(false),
            token_budget: None,
            direction: None,
        }));
        let sc = result.structured_content.as_ref().unwrap();
        let warnings = sc["warnings"].as_array().unwrap();
        assert!(
            warnings
                .iter()
                .any(|w| w["code"] == "UNKNOWN_RANGE_FILTER_FIELD"),
            "expected UNKNOWN_RANGE_FILTER_FIELD warning, got: {warnings:?}",
        );
    }

    /// Range-filter against a field that exists on the
    /// type's schema but is not declared `filterable: range` surfaces
    /// `FIELD_NOT_RANGE_FILTERABLE`. `level` exists on the default
    /// `spec` type with `filterable: equality` — perfect probe.
    #[test]
    fn search_range_filter_field_not_range_filterable_surfaces_typed_warning() {
        let tmp = TempDir::new().unwrap();
        write_workspace(&tmp, "demo");
        let server = FilesystemMcpServer::from_workspace_root(tmp.path()).unwrap();
        seed_via_mcp(&server, "Seed");

        let mut range_filters = std::collections::HashMap::new();
        range_filters.insert("min_level".to_string(), "M0".to_string());

        let result = server.memstead_search(Parameters(SearchParams {
            query: None,
            mem: None,
            entity_type: Some("spec".to_string()),
            expand_via: None,
            expand_depth: None,
            related_to: None,
            depth: None,
            edge_type: None,
            limit: None,
            offset: None,
            filters: None,
            range_filters: Some(range_filters),
            stub: Some(false),
            token_budget: None,
            direction: None,
        }));
        let sc = result.structured_content.as_ref().unwrap();
        let warnings = sc["warnings"].as_array().unwrap();
        assert!(
            warnings
                .iter()
                .any(|w| w["code"] == "FIELD_NOT_RANGE_FILTERABLE"),
            "expected FIELD_NOT_RANGE_FILTERABLE warning, got: {warnings:?}",
        );
    }

    /// Omitting `range_filters` produces no
    /// range-filter warnings (the parameter is optional).
    #[test]
    fn search_without_range_filters_produces_no_range_warnings() {
        let tmp = TempDir::new().unwrap();
        write_workspace(&tmp, "demo");
        let server = FilesystemMcpServer::from_workspace_root(tmp.path()).unwrap();
        seed_via_mcp(&server, "Seed");

        let result = server.memstead_search(Parameters(SearchParams {
            query: None,
            mem: None,
            entity_type: None,
            expand_via: None,
            expand_depth: None,
            related_to: None,
            depth: None,
            edge_type: None,
            limit: None,
            offset: None,
            filters: None,
            range_filters: None,
            stub: Some(false),
            token_budget: None,
            direction: None,
        }));
        let sc = result.structured_content.as_ref().unwrap();
        let warnings = sc["warnings"].as_array().unwrap_or(&Vec::new()).clone();
        for w in warnings {
            let code = w["code"].as_str().unwrap_or("");
            assert!(
                !code.starts_with("RANGE_FILTER_")
                    && code != "UNKNOWN_RANGE_FILTER_FIELD"
                    && code != "FIELD_NOT_RANGE_FILTERABLE",
                "no range-filter warning expected when range_filters omitted, got: {w}",
            );
        }
    }

    #[test]
    fn search_filters_by_entity_type() {
        let tmp = TempDir::new().unwrap();
        write_workspace(&tmp, "demo");
        let server = FilesystemMcpServer::from_workspace_root(tmp.path()).unwrap();
        seed_via_mcp(&server, "OnlySpec");

        // Filter to a non-existent type → empty hits.
        let result = server.memstead_search(Parameters(SearchParams {
            query: None,
            mem: None,
            entity_type: Some("totally-not-a-type".into()),
            expand_via: None,
            expand_depth: None,
            related_to: None,
            depth: None,
            edge_type: None,
            limit: None,
            offset: None,
            filters: None,
            range_filters: None,
            stub: Some(false),
            token_budget: None,
            direction: None,
        }));
        let md = result
            .content
            .iter()
            .filter_map(|c| c.as_text().map(|t| t.text.as_str()))
            .collect::<Vec<_>>()
            .join("\n");
        assert!(md.contains("_total: 0"), "expected _total: 0 in: {md}");
    }

    #[test]
    fn health_returns_workspace_summary() {
        let tmp = TempDir::new().unwrap();
        write_workspace(&tmp, "demo");
        let server = FilesystemMcpServer::from_workspace_root(tmp.path()).unwrap();
        seed_via_mcp(&server, "Healthy");

        let result = server.memstead_health(Parameters(HealthParams {
            include: None,
            limit: None,
            mem: None,
            include_config: false,
            target_schema: None,
            token_budget: None,
            chunk: None,
        }));
        assert!(!result.is_error.unwrap_or(false));
        let body = result.structured_content.unwrap();
        // The summary structure carries totals and a per-mem map.
        // We do not pin field names here (memstead-base owns the shape),
        // just assert the response is a non-empty JSON object.
        assert!(body.is_object());
        assert!(!body.as_object().unwrap().is_empty());
    }

    /// Plan 08 duplicate check (MCP leg): an identifier-shaped value
    /// living only in an entity's metadata is findable by a plain
    /// free-text `memstead_search`, and the hit reports the metadata
    /// match in its matched-terms breakdown.
    #[test]
    fn search_finds_identifier_shaped_metadata_value() {
        let tmp = TempDir::new().unwrap();
        write_workspace(&tmp, "demo");
        // Seed a file carrying the identifier in an UNDECLARED
        // metadata field (tolerated on load; now findable).
        std::fs::write(
            tmp.path().join("akte.md"),
            "---\ntype: spec\naktenzeichen: 20/54/033\n---\n# Akte\n\n\
             ## Identity\n\nDie Akte selbst.\n\n## Purpose\n\nNachweis.\n",
        )
        .unwrap();
        let server = FilesystemMcpServer::from_workspace_root(tmp.path()).unwrap();

        let result = server.memstead_search(Parameters(SearchParams {
            query: Some(memstead_base::ops::Query {
                any: vec!["20/54/033".into()],
                not: vec![],
                phrase: None,
                field: None,
            }),
            direction: None,
            mem: None,
            entity_type: None,
            expand_via: None,
            expand_depth: None,
            related_to: None,
            depth: None,
            edge_type: None,
            limit: None,
            offset: None,
            filters: None,
            range_filters: None,
            stub: None,
            token_budget: None,
        }));
        assert!(!result.is_error.unwrap_or(false), "{result:?}");
        let body = result.structured_content.unwrap();
        let hits = body["hits"].as_array().expect("hits array");
        assert_eq!(hits.len(), 1, "identifier found over MCP: {body}");
        assert_eq!(hits[0]["id"], "demo--akte");
        assert!(
            hits[0]["matched_terms"]
                .as_object()
                .into_iter()
                .flat_map(|m| m.values())
                .flat_map(|v| v.as_array().cloned().unwrap_or_default())
                .any(|tm| tm["field"] == "metadata"),
            "hit identifiable as a metadata match: {}",
            hits[0]
        );
    }

    #[test]
    fn health_via_new_engine_reflects_post_boot_mutations() {
        // Pins the migration template's "boot fresh per call" property:
        // seed entities through the legacy engine after server boot,
        // then assert memstead_health (which now routes through a fresh
        // memstead_base::Engine) reflects them. Without the per-call boot
        // the health response would be stale relative to the legacy
        // engine's mutations.
        //
        // `memstead_create` refuses on missing required sections,
        // so `seed_via_mcp` seeds `identity` + `purpose`. The
        // seeded entity no longer surfaces as missing_fields, but
        // the broader invariant — health reflects post-boot
        // mutations — still holds via the total entity count.
        let tmp = TempDir::new().unwrap();
        write_workspace(&tmp, "demo");
        let server = FilesystemMcpServer::from_workspace_root(tmp.path()).unwrap();

        seed_via_mcp(&server, "Post Boot Spec");

        // Health must reflect the post-boot mutation — the seeded
        // entity is visible via the stats projection (stub_count and
        // missing_fields can both be 0 on a fresh mem with a
        // well-formed seed). Use a query for the entity directly:
        // memstead_search by title term proves the engine re-boot sees
        // the new entity. If `health` had been boot-cached, the
        // search index would lag.
        let result = server.memstead_search(Parameters(SearchParams {
            query: Some(memstead_base::ops::Query {
                any: vec!["post-boot".into()],
                not: vec![],
                phrase: None,
                field: None,
            }),
            direction: None,
            mem: None,
            entity_type: None,
            expand_via: None,
            expand_depth: None,
            related_to: None,
            depth: None,
            edge_type: None,
            limit: None,
            offset: None,
            filters: None,
            range_filters: None,
            stub: None,
            token_budget: None,
        }));
        assert!(!result.is_error.unwrap_or(false));
        let text = result
            .content
            .first()
            .unwrap()
            .as_text()
            .unwrap()
            .text
            .clone();
        assert!(
            text.contains("demo--post-boot-spec") || text.contains("Post Boot"),
            "post-seed search must surface the seeded entity, got: {text}"
        );
    }

    #[test]
    fn schema_returns_pinned_schema_when_name_matches() {
        let tmp = TempDir::new().unwrap();
        write_workspace(&tmp, "demo");
        let server = FilesystemMcpServer::from_workspace_root(tmp.path()).unwrap();

        // Bare-name and canonical pin both work.
        for name in ["default", "default@1.0.0"] {
            let result = server.memstead_schema(Parameters(SchemaParams {
                verbosity: None,
                name: Some(name.into()),
                mem: None,
            }));
            assert!(!result.is_error.unwrap_or(false), "name={name:?}");
            let body = result.structured_content.unwrap();
            // Converged onto the shared `build_schema_payload`: the
            // canonical `ref` subsumes the former top-level `name`/`version`.
            // The omitted-verbosity default is the lite skeleton.
            assert_eq!(body["ref"], "default@1.0.0");
            assert!(body["types_summary"].is_array());
            assert!(body.get("types").is_none(), "default is lite, not full");
            assert!(body["used_by"].is_array());
            assert_eq!(body["used_by"][0], "demo");
        }

        // Explicit full verbosity still ships the rich catalogue.
        let result = server.memstead_schema(Parameters(SchemaParams {
            verbosity: Some("full".into()),
            name: Some("default".into()),
            mem: None,
        }));
        assert!(!result.is_error.unwrap_or(false));
        let body = result.structured_content.unwrap();
        assert!(body["types"].is_array());

        // `mem` shortcut resolves the same schema.
        let result = server.memstead_schema(Parameters(SchemaParams {
            verbosity: None,
            name: None,
            mem: Some("demo".into()),
        }));
        assert!(!result.is_error.unwrap_or(false));
        let body = result.structured_content.unwrap();
        assert_eq!(body["ref"], "default@1.0.0");
    }

    /// Surface parity: the public filesystem `/mcp` flavour honours the
    /// same `verbosity` toggle as the mem-repo surface (Plan 01) — it
    /// converged onto the shared `build_schema_payload`, so lite is the
    /// identical structural skeleton and an unknown value refuses typed.
    #[test]
    fn schema_honours_verbosity_toggle() {
        let tmp = TempDir::new().unwrap();
        write_workspace(&tmp, "demo");
        let server = FilesystemMcpServer::from_workspace_root(tmp.path()).unwrap();

        let lite = server.memstead_schema(Parameters(SchemaParams {
            verbosity: Some("lite".into()),
            name: None,
            mem: Some("demo".into()),
        }));
        assert!(!lite.is_error.unwrap_or(false));
        let lite_body = lite.structured_content.unwrap();
        assert!(
            lite_body["types_summary"].is_array(),
            "lite skeleton present"
        );
        assert!(lite_body.get("types").is_none(), "lite omits rich types");
        assert!(lite_body.get("description").is_none(), "lite drops prose");
        assert_eq!(lite_body["ref"], "default@1.0.0");

        let unknown = server.memstead_schema(Parameters(SchemaParams {
            verbosity: Some("brief".into()),
            name: None,
            mem: Some("demo".into()),
        }));
        assert!(
            unknown.is_error.unwrap_or(false),
            "unknown verbosity refuses"
        );
        let env = unknown.structured_content.unwrap();
        assert_eq!(env["code"], "INVALID_INPUT");
    }

    #[test]
    fn schema_rejects_both_name_and_mem() {
        let tmp = TempDir::new().unwrap();
        write_workspace(&tmp, "demo");
        let server = FilesystemMcpServer::from_workspace_root(tmp.path()).unwrap();

        let result = server.memstead_schema(Parameters(SchemaParams {
            verbosity: None,
            name: Some("default".into()),
            mem: Some("demo".into()),
        }));
        assert!(result.is_error.unwrap_or(false));
        let body = result.content[0]
            .as_text()
            .map(|t| t.text.clone())
            .unwrap_or_default();
        assert!(body.contains("INVALID_INPUT"), "got: {body}");
    }

    #[test]
    fn schema_rejects_unknown_mem() {
        let tmp = TempDir::new().unwrap();
        write_workspace(&tmp, "demo");
        let server = FilesystemMcpServer::from_workspace_root(tmp.path()).unwrap();

        let result = server.memstead_schema(Parameters(SchemaParams {
            verbosity: None,
            name: None,
            mem: Some("nope".into()),
        }));
        assert!(result.is_error.unwrap_or(false));
        let body = result.content[0]
            .as_text()
            .map(|t| t.text.clone())
            .unwrap_or_default();
        assert!(body.contains("UNKNOWN_MEM"), "got: {body}");
    }

    #[test]
    fn schema_rejects_unknown_name_with_typed_code() {
        let tmp = TempDir::new().unwrap();
        write_workspace(&tmp, "demo");
        let server = FilesystemMcpServer::from_workspace_root(tmp.path()).unwrap();

        let result = server.memstead_schema(Parameters(SchemaParams {
            verbosity: None,
            name: Some("totally-not-a-schema".into()),
            mem: None,
        }));
        assert!(result.is_error.unwrap_or(false));
        let body = result.structured_content.unwrap();
        assert_eq!(body["code"], "ENTITY_NOT_FOUND");
    }

    #[test]
    fn changes_since_returns_entries_after_timestamp() {
        let tmp = TempDir::new().unwrap();
        write_workspace(&tmp, "demo");
        let server = FilesystemMcpServer::from_workspace_root(tmp.path()).unwrap();

        // Three creates → three changelog lines.
        for title in ["A", "B", "C"] {
            seed_via_mcp(&server, title);
        }

        // Empty `since` → all three entries.
        let result = server.memstead_changes_since(Parameters(ChangesSinceParams {
            mem: "demo".into(),
            since: "".into(),
            rename_similarity: None,
            include_notes: false,
        }));
        assert!(!result.is_error.unwrap_or(false));
        let body = result.structured_content.unwrap();
        assert_eq!(body["count"], 3);
        let entries = body["entries"].as_array().unwrap();
        assert_eq!(entries.len(), 3);
        for entry in entries {
            assert_eq!(entry["kind"], "create");
        }

        // `since` set to a far-future timestamp → empty.
        let result = server.memstead_changes_since(Parameters(ChangesSinceParams {
            mem: "demo".into(),
            since: "9999-01-01T00:00:00.000Z".into(),
            rename_similarity: None,
            include_notes: false,
        }));
        let body = result.structured_content.unwrap();
        assert_eq!(body["count"], 0);
    }

    #[test]
    fn changes_since_handles_missing_changelog_file() {
        // Fresh workspace with no mutations → no changelog file
        // exists. Must return an empty result, not an error.
        let tmp = TempDir::new().unwrap();
        write_workspace(&tmp, "demo");
        let server = FilesystemMcpServer::from_workspace_root(tmp.path()).unwrap();
        let result = server.memstead_changes_since(Parameters(ChangesSinceParams {
            mem: "demo".into(),
            since: "".into(),
            rename_similarity: None,
            include_notes: false,
        }));
        assert!(!result.is_error.unwrap_or(false));
        let body = result.structured_content.unwrap();
        assert_eq!(body["count"], 0);
    }

    #[test]
    fn entity_includes_relations_when_flag_set() {
        let tmp = TempDir::new().unwrap();
        write_workspace(&tmp, "demo");
        let server = FilesystemMcpServer::from_workspace_root(tmp.path()).unwrap();
        let (from, _) = seed_via_mcp(&server, "Source");
        let (to, _) = seed_via_mcp(&server, "Target");

        // Add a relation.
        server.memstead_relate(Parameters(RelateParams {
            relations: vec![RelateOpInput {
                from: from.clone(),
                to: to.clone(),
                r#type: "USES".into(),
                remove: None,
                description: None,
            }],
            note: None,
            role: None,
            dry_run: None,
        }));

        // Read with include_relations.
        let result = server.memstead_entity(Parameters(EntityParams {
            id: from,
            sections: None,
            include_relations: Some(true),
            include_context: None,
            token_budget: None,
            chunk: None,
            include_provenance: None,
        }));
        assert!(!result.is_error.unwrap_or(false));
        let text = result
            .content
            .first()
            .unwrap()
            .as_text()
            .unwrap()
            .text
            .clone();
        assert!(text.contains("## Relations"));
    }

    #[test]
    fn entity_includes_context_when_flag_set() {
        let tmp = TempDir::new().unwrap();
        write_workspace(&tmp, "demo");
        let server = FilesystemMcpServer::from_workspace_root(tmp.path()).unwrap();
        let (id, _) = seed_via_mcp(&server, "Lonely");

        // Read with include_context. The community cache runs Louvain
        // on first call; a single-node graph has a trivial cluster.
        let result = server.memstead_entity(Parameters(EntityParams {
            id,
            sections: None,
            include_relations: None,
            include_context: Some(true),
            token_budget: None,
            chunk: None,
            include_provenance: None,
        }));
        assert!(!result.is_error.unwrap_or(false));
        let text = result
            .content
            .first()
            .unwrap()
            .as_text()
            .unwrap()
            .text
            .clone();
        assert!(text.contains("## Community Context"));
    }

    #[test]
    fn search_returns_results_for_text_query() {
        let tmp = TempDir::new().unwrap();
        write_workspace(&tmp, "demo");
        let server = FilesystemMcpServer::from_workspace_root(tmp.path()).unwrap();

        // Seed two entities — one matches the query body, one does not.
        // The
        // engine refuses on missing required sections, so seed both
        // `identity` + `purpose` for each create.
        let mut secs = indexmap::IndexMap::new();
        secs.insert(
            "identity".to_string(),
            "Discusses the architecture of the universe.".to_string(),
        );
        secs.insert("purpose".to_string(), "match purpose".to_string());
        server.memstead_create(Parameters(CreateParams {
            anchors: None,
            title: "Match".into(),
            entity_type: "spec".into(),
            mem: None,
            sections: Some(secs),
            metadata: None,
            relations: None,
            dry_run: None,
            note: None,
            role: None,
        }));
        let mut other_secs = indexmap::IndexMap::new();
        other_secs.insert("identity".to_string(), "other identity".to_string());
        other_secs.insert("purpose".to_string(), "other purpose".to_string());
        server.memstead_create(Parameters(CreateParams {
            anchors: None,
            title: "Other".into(),
            entity_type: "spec".into(),
            mem: None,
            sections: Some(other_secs),
            metadata: None,
            relations: None,
            dry_run: None,
            note: None,
            role: None,
        }));

        // Issue a search using the structured query shape.
        use memstead_base::ops::Query;
        let result = server.memstead_search(Parameters(SearchParams {
            query: Some(Query {
                any: vec!["architecture".into()],
                not: vec![],
                phrase: None,
                field: None,
            }),
            direction: None,
            mem: None,
            entity_type: None,
            expand_via: None,
            expand_depth: None,
            related_to: None,
            depth: None,
            edge_type: None,
            limit: None,
            offset: None,
            filters: None,
            range_filters: None,
            stub: Some(false),
            token_budget: None,
        }));
        assert!(!result.is_error.unwrap_or(false));
        let text = result
            .content
            .first()
            .unwrap()
            .as_text()
            .unwrap()
            .text
            .clone();
        // Markdown response — rendered by `render_search_markdown`.
        // The matching entity's title or id appears; the
        // non-matching one should not (asserts that the text
        // predicate actually filtered).
        assert!(text.contains("demo--match") || text.contains("Match"));
        assert!(!text.contains("demo--other"));
    }

    #[test]
    fn search_metadata_only_returns_seeded_entities() {
        // Empty query → falls through to metadata-only / list semantics.
        // Asserts the no-text-predicate branch returns hits without
        // tripping on the index path.
        let tmp = TempDir::new().unwrap();
        write_workspace(&tmp, "demo");
        let server = FilesystemMcpServer::from_workspace_root(tmp.path()).unwrap();
        seed_via_mcp(&server, "Alpha");
        seed_via_mcp(&server, "Beta");

        let result = server.memstead_search(Parameters(SearchParams {
            query: None,
            mem: None,
            entity_type: None,
            expand_via: None,
            expand_depth: None,
            related_to: None,
            depth: None,
            edge_type: None,
            limit: None,
            offset: None,
            filters: None,
            range_filters: None,
            stub: Some(false),
            token_budget: None,
            direction: None,
        }));
        assert!(!result.is_error.unwrap_or(false));
        let text = result
            .content
            .first()
            .unwrap()
            .as_text()
            .unwrap()
            .text
            .clone();
        // Both seeded entities appear when there is no text filter.
        assert!(text.contains("demo--alpha") || text.contains("Alpha"));
        assert!(text.contains("demo--beta") || text.contains("Beta"));
    }

    #[test]
    fn overview_returns_schema_and_mem_sections() {
        let tmp = TempDir::new().unwrap();
        write_workspace(&tmp, "demo");
        let server = FilesystemMcpServer::from_workspace_root(tmp.path()).unwrap();
        seed_via_mcp(&server, "Alpha");
        seed_via_mcp(&server, "Beta");

        let result = server.memstead_overview(Parameters(OverviewParams {
            rebuild: None,
            chunk: None,
            mem: None,
            include: None,
            token_budget: None,
        }));
        assert!(!result.is_error.unwrap_or(false));
        let text = result
            .content
            .first()
            .unwrap()
            .as_text()
            .unwrap()
            .text
            .clone();
        // Produced by the shared composer now: an `_overview_mode` frontmatter
        // line and the standard section headings, with the single mem in the
        // roster and its own entity count. `_mem_schema` is emitted only under a
        // `mem` filter (composer behaviour), so it is absent from this
        // unscoped call — asserted below. The lean surface carries no
        // mem-lifecycle tools, so the lifecycle section is suppressed.
        assert!(text.contains("_overview_mode:"));
        assert!(text.contains("## Schemas"));
        assert!(text.contains("## Mems"));
        assert!(text.contains("### demo"));
        assert!(text.contains("- **Entities:** 2"));
        assert!(text.contains("## Communities"));
        assert!(
            !text.contains("## Lifecycle Namespaces"),
            "lean surface has no mem-lifecycle tools: {text}"
        );
        assert!(
            !text.contains("_mem_schema:"),
            "_mem_schema is emitted only under a mem filter: {text}"
        );

        // Scoping to the one visible mem succeeds and now anchors `_mem_schema`.
        let scoped = server.memstead_overview(Parameters(OverviewParams {
            rebuild: None,
            chunk: None,
            mem: Some("demo".into()),
            include: None,
            token_budget: None,
        }));
        assert!(!scoped.is_error.unwrap_or(false));
        let stext = scoped
            .content
            .first()
            .unwrap()
            .as_text()
            .unwrap()
            .text
            .clone();
        assert!(
            stext.contains("_mem_schema: default@1.0.0"),
            "a mem-scoped overview anchors the schema: {stext}"
        );
    }

    #[test]
    fn overview_rejects_unknown_mem_filter() {
        let tmp = TempDir::new().unwrap();
        write_workspace(&tmp, "demo");
        let server = FilesystemMcpServer::from_workspace_root(tmp.path()).unwrap();

        let result = server.memstead_overview(Parameters(OverviewParams {
            rebuild: None,
            chunk: None,
            mem: Some("not-the-mem".into()),
            include: None,
            token_budget: None,
        }));
        assert!(result.is_error.unwrap_or(false));
        let body = result.structured_content.unwrap();
        // The shared composer returns the typed unknown-mem error, and its mem
        // list names every visible mem (here, the workspace's single mem).
        assert_eq!(body["code"], "UNKNOWN_MEM");
        assert_eq!(body["details"]["visible_mems"][0], "demo");
    }

    #[test]
    fn overview_include_community_members_renders_member_ids() {
        let tmp = TempDir::new().unwrap();
        write_workspace(&tmp, "demo");
        let server = FilesystemMcpServer::from_workspace_root(tmp.path()).unwrap();
        let (a, _) = seed_via_mcp(&server, "Alpha");
        let (b, _) = seed_via_mcp(&server, "Beta");
        // Edge so the cluster has structure to discuss.
        server.memstead_relate(Parameters(RelateParams {
            relations: vec![RelateOpInput {
                from: a.clone(),
                to: b.clone(),
                r#type: "USES".into(),
                remove: None,
                description: None,
            }],
            note: None,
            role: None,
            dry_run: None,
        }));

        let result = server.memstead_overview(Parameters(OverviewParams {
            rebuild: None,
            chunk: None,
            mem: None,
            include: Some(vec!["community_members".into()]),
            token_budget: None,
        }));
        assert!(!result.is_error.unwrap_or(false));
        let text = result
            .content
            .first()
            .unwrap()
            .as_text()
            .unwrap()
            .text
            .clone();
        // With community_members forced, the rendered cluster lists
        // each member entity id as a bullet — both Alpha and Beta
        // should appear under Communities.
        assert!(text.contains(&format!("- {a}")));
        assert!(text.contains(&format!("- {b}")));
    }

    /// `memstead_overview include=["dangling_links"]` lists every non-stub
    /// entity whose section body wiki-links resolve to a stub or
    /// missing target. Pre-fix the lean overview surface hardcoded
    /// `[]` here and the `## Dangling Links` block never rendered,
    /// even when the health surface populated the same
    /// view. The test fails against that state.
    #[test]
    fn overview_dangling_links_surfaces_stub_targets() {
        let tmp = TempDir::new().unwrap();
        write_workspace(&tmp, "demo");
        let server = FilesystemMcpServer::from_workspace_root(tmp.path()).unwrap();
        let (id, hash) = seed_via_mcp(&server, "Anchor");

        // Rewrite Identity to carry a body wiki-link to a slug with
        // no on-disk file, backed by an atomic REFERENCES declaration
        // (forward-reference auto-stub). The stub is the dangling
        // signal — the dangling-links surface flags wiki-links whose
        // target resolves to a stub entity.
        let mut sections = indexmap::IndexMap::new();
        sections.insert(
            "identity".to_string(),
            "Refers to [[gone]] in prose.".to_string(),
        );
        let upd = server.memstead_update(Parameters(UpdateParams {
            anchors: None,
            relations_unset: None,
            anchors_unset: None,
            id: id.clone(),
            expected_hash: hash,
            sections: Some(sections),
            append_sections: None,
            patch_sections: None,
            metadata: None,
            metadata_unset: None,
            dry_run: None,
            note: Some("seed dangling link".into()),
            role: None,
            // Body wiki-link `[[gone]]` is auto-emitted as REFERENCES
            // via the alias-synthesis pass — explicit author refused
            // under the schema's `manual_authoring: forbidden` posture.
            declare_relations: None,
        }));
        assert!(!upd.is_error.unwrap_or(false), "{upd:?}");

        let result = server.memstead_overview(Parameters(OverviewParams {
            rebuild: None,
            chunk: None,
            mem: None,
            include: Some(vec!["dangling_links".into()]),
            token_budget: None,
        }));
        assert!(!result.is_error.unwrap_or(false));
        let text = result
            .content
            .first()
            .unwrap()
            .as_text()
            .unwrap()
            .text
            .clone();
        assert!(
            text.contains("## Dangling Links"),
            "overview must render the Dangling Links section when an opt-in caller finds one: {text}"
        );
        assert!(
            text.contains(&id),
            "Dangling Links must name the linking entity ({id}): {text}"
        );
        assert!(
            text.contains("demo--gone"),
            "Dangling Links must name the dangling target: {text}"
        );
    }

    #[test]
    fn overview_unknown_include_key_emits_warning() {
        let tmp = TempDir::new().unwrap();
        write_workspace(&tmp, "demo");
        let server = FilesystemMcpServer::from_workspace_root(tmp.path()).unwrap();
        seed_via_mcp(&server, "Alpha");

        let result = server.memstead_overview(Parameters(OverviewParams {
            rebuild: None,
            chunk: None,
            mem: None,
            include: Some(vec!["totally-bogus".into()]),
            token_budget: None,
        }));
        assert!(!result.is_error.unwrap_or(false));
        let text = result
            .content
            .first()
            .unwrap()
            .as_text()
            .unwrap()
            .text
            .clone();
        assert!(text.contains("## Warnings"));
        assert!(text.contains("UNKNOWN_INCLUDE_KEY"));
    }

    #[test]
    fn overview_rejects_legacy_schema_types_include() {
        let tmp = TempDir::new().unwrap();
        write_workspace(&tmp, "demo");
        let server = FilesystemMcpServer::from_workspace_root(tmp.path()).unwrap();

        let result = server.memstead_overview(Parameters(OverviewParams {
            rebuild: None,
            chunk: None,
            mem: None,
            include: Some(vec!["schema_types".into()]),
            token_budget: None,
        }));
        assert!(result.is_error.unwrap_or(false));
        let body = result.structured_content.unwrap();
        assert_eq!(body["code"], "INVALID_INPUT");
    }

    #[test]
    fn rename_changes_id_and_persists_through_disk() {
        let tmp = TempDir::new().unwrap();
        write_workspace(&tmp, "demo");
        let server = FilesystemMcpServer::from_workspace_root(tmp.path()).unwrap();
        let (id, hash) = seed_via_mcp(&server, "Old Title");

        let result = server.memstead_rename(Parameters(RenameParams {
            id,
            new_title: "New Title".into(),
            expected_hash: hash,
            note: Some("renamed".into()),
            role: None,
        }));
        assert!(!result.is_error.unwrap_or(false));
        let body = result.structured_content.unwrap();
        assert_eq!(body["new_id"], "demo--new-title");
        assert_eq!(body["new_file_path"], "new-title.md");
        assert!(tmp.path().join("new-title.md").is_file());
        assert!(!tmp.path().join("old-title.md").exists());
    }
}