memstead-base 0.1.0

Engine internals for Memstead — store, parser, validators, filesystem-mem engine.
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
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
//! `Engine::update_entity` and `Engine::batch_update` — rewrite an
//! entity's sections / metadata in place, optimistically-locked.

use std::path::Path;

use crate::engine_fallback_type;
use crate::entity::EntityId;
use crate::entity::generator::generate_markdown;
use crate::entity::parser::parse_markdown;
use crate::entity::store_builder::push_entities_into_store;
use crate::ops::{ModifiedMetadata, ModifiedSections, WarningHint};
use crate::provenance::{Provenance, ProvenanceKind};
use crate::runtime_validator::{
    parse_metadata_value, validate_section_content, validate_section_keys,
    validate_updatable_section, validate_writable_metadata_key,
};
use crate::vcs::{Actor, ClientId, CommitContext};
use crate::workspace::MountCapability;

use super::super::{Engine, EngineError, UpdateEntityArgs, UpdateEntityOutcome};
use super::{
    PATCH_OLD_NOT_FOUND_CONTENT_CAP, make_stub, today_iso, unknown_type_error,
    validate_relation_target_grammar,
};
use crate::engine::outcomes::RelationDeclared;
use crate::entity::{Entity, Relationship};

use std::sync::Arc;

/// Result of [`Engine::prepare_update`] — the validation + markdown
/// step split out of the commit so the batch path can prepare every
/// item before committing the whole set atomically.
enum PrepareOutcome {
    /// No commit is needed: the no-op short-circuit (content unchanged)
    /// and the dry-run preview both return a finished outcome here.
    Done(UpdateEntityOutcome),
    /// A real change whose post-mutation markdown is ready to stage +
    /// commit.
    Prepared(PreparedUpdate),
}

/// Everything the commit step needs to stage one prepared update's
/// disk write and build its outcome. Carries no commit SHA — that's
/// produced when the (single or batched) commit lands.
struct PreparedUpdate {
    mount_idx: usize,
    id: EntityId,
    mem: String,
    type_def: Arc<memstead_schema::TypeDefinition>,
    file_path: String,
    markdown: String,
    /// Body wiki-link targets the entity had *before* this mutation —
    /// the GC sweep scopes orphan-stub detection to these.
    prev_body_targets: std::collections::HashSet<EntityId>,
    modified_date: String,
    modified_sections: ModifiedSections,
    modified_metadata: ModifiedMetadata,
    warnings: Vec<WarningHint>,
    relations_declared: Vec<RelationDeclared>,
}

/// The store-side results of applying a prepared write — filled in
/// after the commit lands by [`Engine::apply_prepared_to_store`].
struct AppliedWrite {
    content_hash: String,
    title: String,
    orphan_stubs_removed: Vec<EntityId>,
}

impl Engine {
    /// Update an entity's sections and/or metadata.
    ///
    /// Same six-concern shape as [`Engine::create_entity`]. Optimistic
    /// locking via `args.expected_hash`: when `Some`, must match the
    /// store's current `content_hash` or returns
    /// [`EngineError::HashMismatch`]. The new engine's MCP-facing
    /// callers should always pass the hash; `None` is the
    /// `--force`-style escape hatch.
    ///
    /// Internally a two-step pipeline: [`Self::prepare_update`] runs
    /// all validation and computes the post-mutation markdown without
    /// committing, then [`Self::commit_prepared_update`] stages and
    /// commits the result. The split lets [`Self::batch_update`]
    /// prepare every item up front and commit the whole batch as one
    /// atomic unit.
    pub fn update_entity(
        &mut self,
        args: UpdateEntityArgs,
        actor: Actor,
        client: Option<&ClientId>,
        note: Option<&str>,
    ) -> Result<UpdateEntityOutcome, EngineError> {
        // Reload-before-operation: probe the mem ref and reload if a
        // sibling advanced it, so the `expected_hash` compare inside
        // `prepare_update` runs against current truth. A stale hash for
        // the targeted entity then trips a real `HASH_MISMATCH`; an
        // unrelated concurrent write leaves this entity's hash intact
        // and the update proceeds. The drift notice rides the outcome.
        let mut drift_warnings = self.reload_if_stale(Some(args.id.mem()));
        let mut outcome = match self.prepare_update(args)? {
            PrepareOutcome::Done(outcome) => outcome,
            PrepareOutcome::Prepared(prepared) => {
                self.commit_prepared_update(prepared, actor, client, note)?
            }
        };
        drift_warnings.append(&mut outcome.warnings);
        outcome.warnings = drift_warnings;
        Ok(outcome)
    }

    /// Stage the prepared disk write, commit it as one commit, append
    /// provenance, and apply the change to the in-memory store — the
    /// single-update tail of [`Self::update_entity`]. The batch path
    /// drives the same steps but commits once across all items.
    fn commit_prepared_update(
        &mut self,
        prepared: PreparedUpdate,
        actor: Actor,
        client: Option<&ClientId>,
        note: Option<&str>,
    ) -> Result<UpdateEntityOutcome, EngineError> {
        let backend = self.mounts[prepared.mount_idx].backend.as_ref();
        backend.write_entity(Path::new(&prepared.file_path), prepared.markdown.as_bytes())?;
        let commit_subject = format!("memstead: update {}", prepared.id);
        let ctx = CommitContext {
            actor,
            client: client.cloned(),
            tool: Some("update_entity"),
            note: note.map(String::from),
            logical_operation_id: None,
            entity_ids: None,
        };
        let commit_sha = backend.commit(&commit_subject, &ctx)?;
        backend.append_provenance(&Provenance::new(
            std::time::SystemTime::now(),
            ProvenanceKind::Update,
            Some(prepared.id.to_string()),
            actor,
            client.cloned(),
            note.map(String::from),
        ))?;
        self.record_self_write(prepared.mount_idx, &commit_sha);

        let applied = self.apply_prepared_to_store(&prepared)?;

        self.invalidate_communities();
        self.invalidate_search_indexes();

        // `require_notes` provenance nudge — single engine-level
        // enforcement point. Only reached on the real-commit path; the
        // no-op and dry-run prepare outcomes never demand a note.
        let mut warnings = prepared.warnings;
        if let Some(w) = self.note_missing_warning("update_entity", note) {
            warnings.push(w);
        }

        Ok(UpdateEntityOutcome {
            id: prepared.id.clone(),
            title: applied.title,
            file_path: prepared.file_path,
            content_hash: applied.content_hash,
            commit_sha,
            modified_date: prepared.modified_date,
            orphan_stubs_removed: applied.orphan_stubs_removed,
            modified_sections: prepared.modified_sections,
            modified_metadata: prepared.modified_metadata,
            prospective_hash: None,
            warnings,
            relations_declared: prepared.relations_declared,
        })
    }

    /// Parse the prepared markdown, push it into the in-memory store,
    /// re-map alias-target edge sources, and GC any stub the mutation
    /// orphaned. Shared post-commit store-application step for the
    /// single-update and batch paths — does NOT touch the backend or
    /// commit (the caller has already staged + committed the disk
    /// write).
    fn apply_prepared_to_store(
        &mut self,
        prepared: &PreparedUpdate,
    ) -> Result<AppliedWrite, EngineError> {
        let parse_result = parse_markdown(
            &prepared.markdown,
            &prepared.file_path,
            prepared.type_def.as_ref(),
            &prepared.mem,
        )
        .map_err(|e| EngineError::ParseAfterWrite(e.to_string()))?;
        let content_hash = parse_result.entity.content_hash.clone();
        let title = parse_result.entity.title.clone();
        let fallback = engine_fallback_type();
        push_entities_into_store(&mut self.store, vec![parse_result], fallback.as_ref(), None);
        crate::entity::store_builder::remap_alias_target_edge_sources(
            &mut self.store,
            &self.schemas,
        );
        let orphan_stubs_removed =
            super::gc_orphan_stubs_among(&mut self.store, &prepared.prev_body_targets);
        Ok(AppliedWrite {
            content_hash,
            title,
            orphan_stubs_removed,
        })
    }

    /// Validate an update and compute its post-mutation markdown
    /// *without* committing. Returns [`PrepareOutcome::Done`] for the
    /// no-op / dry-run short-circuits (which never commit) and
    /// [`PrepareOutcome::Prepared`] for a real change whose write the
    /// caller stages + commits. May mutate the store in place via the
    /// alias-synthesis auto-stub upsert; the batch path snapshots the
    /// store before preparing so a refused batch can roll that back.
    fn prepare_update(&mut self, args: UpdateEntityArgs) -> Result<PrepareOutcome, EngineError> {
        let id = &args.id;
        let mem = id.mem().to_string();

        let mount_idx = self
            .mounts
            .iter()
            .position(|m| m.mount.mem == mem)
            .ok_or_else(|| EngineError::UnknownMem(mem.clone()))?;
        if self.mounts[mount_idx].mount.capability != MountCapability::Write {
            return Err(EngineError::ReadOnlyMount(mem));
        }

        let entity = self
            .store
            .get(id)
            .ok_or_else(|| EngineError::NotFound { id: id.to_string() })?;

        // Snapshot the prev entity's body wiki-link set before any
        // subsequent `&mut self` reborrow burns the `entity` borrow.
        // Fed to the alias-synthesis pass so the GC step can compare
        // prev vs. next body links and drop pointer-rel-type relations
        // whose target was a body link before but isn't any more.
        let prev_body_targets = super::collect_body_link_targets(entity);

        // Stub guard — stubs have no body, no metadata, no
        // schema-resolved type to validate against. The recovery is
        // `memstead_create` (stub adoption preserves incoming
        // references). Pre-Item-02 the update path fell through to
        // the `type_def` lookup below and surfaced the cryptic
        // `UnknownType { name: "" }` cascade. Mirrors the
        // `StubCannotRelate` guard on `memstead_relate`.
        if entity.stub {
            return Err(EngineError::StubNotUpdatable { id: id.to_string() });
        }

        // Skip the hash check on dry_run — full's dry_run is the
        // designated stale-hash recovery path. Agents preview a
        // change without holding a fresh hash, get back the current
        // `content_hash` and a `prospective_hash`, then call the
        // real update with `expected_hash = content_hash`.
        if !args.dry_run
            && let Some(expected) = args.expected_hash.as_deref()
            && entity.content_hash != expected
        {
            return Err(EngineError::HashMismatch {
                id: id.to_string(),
                current: entity.content_hash.clone(),
                is_stub: entity.stub,
            });
        }

        // Empty-mutation guard. After existence/stub/hash
        // gates so the more-specific errors fire first. A payload
        // with no recognised mutation content refuses BEFORE any
        // mutation work runs so a misspelled or omitted mutation
        // key (which deserialised to empty defaults under the
        // lenient pre-fix posture) doesn't silently land as
        // `succeeded: N, action: "updated", commit_sha: ""`.
        // Distinct from `UPDATE_NOOP` (a warning that fires when
        // mutation content was provided but matched the current
        // entity state) — the two are different states and ship
        // different envelopes.
        if args.sections.is_empty()
            && args.append_sections.is_empty()
            && args.patch_sections.is_empty()
            && args.metadata.is_empty()
            && args.metadata_unset.is_empty()
            && args.declare_relations.is_empty()
            && args.relations_unset.is_empty()
        {
            return Err(EngineError::EmptyUpdate { id: id.to_string() });
        }

        let schema = self
            .schemas
            .get(&mem)
            .expect("schema present for every registered mount")
            .clone();
        let type_def = schema
            .get_type(&entity.entity_type)
            .ok_or_else(|| unknown_type_error(schema.as_ref(), &entity.entity_type))?;

        // Mode-conflict: the same section key may not appear in
        // more than one of `sections`, `append_sections`,
        // `patch_sections`. Mirrors full's
        // `EngineError::ConflictingSectionModes`. Three-way check:
        // build the conflict list per key and reject when ≥2 modes
        // claim it.
        for key in args.sections.keys() {
            let mut modes = vec!["sections".to_string()];
            if args.append_sections.contains_key(key) {
                modes.push("append_sections".to_string());
            }
            if args.patch_sections.contains_key(key) {
                modes.push("patch_sections".to_string());
            }
            if modes.len() > 1 {
                return Err(EngineError::ConflictingSectionModes {
                    section: key.clone(),
                    modes,
                });
            }
        }
        for key in args.append_sections.keys() {
            if args.patch_sections.contains_key(key) {
                return Err(EngineError::ConflictingSectionModes {
                    section: key.clone(),
                    modes: vec!["append_sections".to_string(), "patch_sections".to_string()],
                });
            }
        }

        validate_section_keys(
            args.sections
                .keys()
                .chain(args.append_sections.keys())
                .chain(args.patch_sections.keys())
                .map(String::as_str),
            type_def.as_ref(),
        )?;
        // Refuse embedded `^## ` in section content on every update path
        // that writes section bodies: `sections` (replace) and
        // `append_sections` (append). `patch_sections` replaces a
        // substring — its `new` text feeds into the eventual section
        // body so it gets the same gate.
        validate_section_content(
            args.sections
                .iter()
                .map(|(k, v)| (k.as_str(), v.as_str()))
                .chain(
                    args.append_sections
                        .iter()
                        .map(|(k, v)| (k.as_str(), v.as_str())),
                )
                .chain(
                    args.patch_sections
                        .iter()
                        .map(|(k, p)| (k.as_str(), p.new.as_str())),
                ),
        )?;
        for key in args.sections.keys() {
            validate_updatable_section(key.as_str(), type_def.as_ref())?;
        }
        for key in args.append_sections.keys() {
            validate_updatable_section(key.as_str(), type_def.as_ref())?;
        }
        for key in args.patch_sections.keys() {
            validate_updatable_section(key.as_str(), type_def.as_ref())?;
        }
        for key in args.metadata.keys() {
            validate_writable_metadata_key(key.as_str(), type_def.as_ref())?;
        }
        for key in &args.metadata_unset {
            validate_writable_metadata_key(key.as_str(), type_def.as_ref())?;
        }

        // Reject the same key appearing in `metadata` (set) and
        // `metadata_unset` — the wire contract is that the conflict is
        // a hard error. Caught before any required-field /
        // parse-metadata check so the resolution ("pick one map") is
        // unambiguous regardless of whether the overlapping key is
        // required.
        let mut overlap: Vec<String> = args
            .metadata
            .keys()
            .filter(|k| args.metadata_unset.iter().any(|u| u == k.as_str()))
            .cloned()
            .collect();
        if !overlap.is_empty() {
            overlap.sort();
            overlap.dedup();
            return Err(EngineError::SetAndUnsetConflict { keys: overlap });
        }

        // Repair-power gate:
        // repair-shaped input is accepted only when the entity
        // currently fails the conformance check against the effective
        // schema. Conformance is per-entity-local and cheap, so the
        // gate runs in pre-validation; no agent-settable flag exists —
        // the entity's own state is the evidence. A pure-consistency
        // break does not open the gate (those have ungated repair
        // paths: `memstead_relate(remove)` and the additive params).
        if !args.relations_unset.is_empty() {
            let findings = crate::ops::integrity::entity_conformance_findings(
                &self.store,
                entity,
                schema.as_ref(),
                &self.schemas,
            );
            if findings.is_empty() {
                return Err(EngineError::RepairNotNeeded {
                    id: id.to_string(),
                    recovery: "use memstead_relate(remove=true) to detach an edge from a                                conformant entity, or the additive memstead_update params                                to evolve it"
                        .to_string(),
                });
            }
        }

        let mut next = entity.clone();

        // Repair-shaped removals, applied before declarations so a
        // repair can drop and re-shape relations in one atomic
        // update. Absent (rel_type, target) pairs are silent no-ops,
        // symmetric with `metadata_unset`. The strict post-state
        // validation below still runs — repair widens accepted
        // inputs, never admissible outputs.
        for unset in &args.relations_unset {
            let canonical = crate::entity::id::validate_rel_type(&unset.rel_type)
                .unwrap_or_else(|_| unset.rel_type.clone());
            next.relationships
                .retain(|r| !(r.rel_type == canonical && r.target == unset.target));
        }

        // Atomic batched relation declarations. Validated and applied
        // before the section/metadata changes so the strict
        // wiki-link/relation validator at the end of this fn sees
        // the freshly-declared relations as part of the post-state.
        // Same vocabulary + shape + grammar gates `memstead_relate`
        // runs; auto-stubs absent Write-mem targets identically
        // to the relate path. Returns the (rel_type, target,
        // target_was_stubbed) triples in `relations_declared` on
        // the outcome so the agent sees what landed.
        let relations_declared = apply_declare_relations(
            self,
            &mut next,
            &args.declare_relations,
            &mem,
            mount_idx,
            type_def.as_ref(),
            schema.as_ref(),
        )?;

        let mut modified_sections: Vec<String> = Vec::new();
        for (key, body) in args.sections {
            modified_sections.push(key.clone());
            next.sections.insert(key, body);
        }

        // Apply append_sections after replace. Empty/absent body
        // is replaced wholesale with the append value; otherwise
        // a `\n` separator joins the two. Mirrors full.
        let mut modified_sections_appended: Vec<String> = Vec::new();
        for (key, value) in args.append_sections {
            let existing = next.sections.get(&key).cloned().unwrap_or_default();
            let new_content = if existing.trim().is_empty() {
                value
            } else {
                format!("{existing}\n{value}")
            };
            next.sections.insert(key.clone(), new_content);
            modified_sections_appended.push(key);
        }

        // Apply patch_sections after append. Find-and-replace the
        // `old` substring with `new`; `all` flips between
        // first-occurrence (replacen 1) and every-occurrence
        // (replace). Empty/absent section is rejected with
        // PatchSectionEmpty; missing-`old` rejected with
        // PatchOldNotFound carrying a UTF-8-safe truncated snapshot
        // of the current body. Mirrors full.
        let mut modified_sections_patched: Vec<String> = Vec::new();
        for (key, patch) in args.patch_sections {
            let existing = next
                .sections
                .get(&key)
                .ok_or_else(|| EngineError::PatchSectionEmpty {
                    section: key.clone(),
                })?
                .clone();
            if !existing.contains(&patch.old) {
                let cap = PATCH_OLD_NOT_FOUND_CONTENT_CAP;
                let truncated = existing.len() > cap;
                // Truncate at a UTF-8 char boundary to avoid
                // splitting a code point.
                let mut cut = cap.min(existing.len());
                while cut > 0 && !existing.is_char_boundary(cut) {
                    cut -= 1;
                }
                let current_content = if truncated {
                    existing[..cut].to_string()
                } else {
                    existing.clone()
                };
                return Err(EngineError::PatchOldNotFound {
                    section: key,
                    current_content,
                    truncated,
                });
            }
            let patched = if patch.all {
                existing.replace(&patch.old, &patch.new)
            } else {
                existing.replacen(&patch.old, &patch.new, 1)
            };
            next.sections.insert(key.clone(), patched);
            modified_sections_patched.push(key);
        }

        let mut modified_metadata_set: Vec<String> = Vec::new();
        for (key, value) in &args.metadata {
            let parsed = parse_metadata_value(key.as_str(), value.as_str(), type_def.as_ref())?;
            modified_metadata_set.push(key.clone());
            next.metadata.insert(key.clone(), parsed);
        }

        let mut modified_metadata_unset: Vec<String> = Vec::new();
        for key in args.metadata_unset {
            // Reject unset on required fields — the pre-remove check
            // carries the recovery payload (field_description,
            // enum_values, type_write_rules) so MCP envelopes surface
            // the full REQUIRED_FIELD_UNSET shape.
            let field_def = type_def.metadata_field(&key);
            let is_required = field_def.map(|f| !f.optional).unwrap_or(false);
            if is_required {
                let (field_description, enum_values) = match field_def {
                    Some(f) => (
                        Some(f.description.clone()),
                        f.enum_values.clone().unwrap_or_default(),
                    ),
                    None => (None, Vec::new()),
                };
                return Err(EngineError::RequiredFieldUnset {
                    field: key,
                    entity_type: type_def.name.clone(),
                    field_description,
                    enum_values,
                    type_write_rules: type_def.write_rules.clone(),
                    // Update path — caller passed
                    // `metadata_unset: ["field"]` against a required
                    // field. The wording ("cannot unset required
                    // field …") is semantically correct for this
                    // path.
                    on_create: false,
                    // The unset path targets one field per call by
                    // definition, so the multi-field accumulator
                    // stays empty here — the singular fields above
                    // are authoritative.
                    missing: Vec::new(),
                });
            }
            if next.metadata.shift_remove(&key).is_some() {
                modified_metadata_unset.push(key);
            }
        }

        // Delay the auto-stamp until AFTER the no-op short-circuit. Pre-fix
        // this branch overwrote `last_modified` with `today_iso()`
        // before the bytes-compare, so the prospective markdown
        // always differed from the on-disk bytes (the schema's
        // `last_modified` was already populated with a full ISO
        // timestamp on the prior write, but `today_iso()` returns
        // date-only), and the no-op compare never matched. Compute
        // `today` for later use but don't stamp `next` yet.
        let today = today_iso();

        // Alias-synthesis pass: for schemas declaring
        // `alias_target_rel_type`, append engine-emitted relations of
        // that rel-type for every body wiki-link not already backed,
        // and GC pointer-rel-type relations whose target was a body
        // wiki-link in the prev state but isn't in next. Cross-mem
        // refusal aborts the update — no partial state.
        //
        // The returned `Vec<Relationship>` is the per-call set of
        // synthesised relations; feed it into the auto-stub warning
        // emission below.
        let (synthesised_relations, self_link_ignored) =
            super::synthesise_alias_relations(self, &prev_body_targets, &mut next)?;

        // Alias-existence invariant: every body wiki-link must be
        // backed by an entry in `entity.relationships`. The validator
        // runs against the *full* post-mutation state (not just the
        // delta), so a mutation that leaves an existing unbacked link
        // in place still fails — forcing cleanup of historical drift.
        let missing = super::scan_wikilinks_without_relation(&next)?;
        if !missing.is_empty() {
            return Err(EngineError::WikiLinkWithoutRelation {
                from_id: id.to_string(),
                missing: missing
                    .into_iter()
                    .map(|(section_key, target)| crate::engine::MissingWikiLink {
                        section_key,
                        target_id: target.to_string(),
                    })
                    .collect(),
            });
        }

        let file_path = next.file_path.clone();

        // The bytes-compare runs against the pre-stamp markdown so the
        // auto-timestamp doesn't synthesise a false delta. When the
        // user-visible payload (sections, user-set metadata,
        // declared relations) didn't change, the pre-stamp markdown
        // matches the on-disk bytes byte-for-byte; we short-circuit
        // and return with `last_modified` preserved at its pre-call
        // value. Real changes fall through; we stamp + regenerate
        // below.
        let markdown_pre_stamp = generate_markdown(&next, type_def.as_ref());

        // No-op short-circuit. When the pre-stamp markdown's hash
        // matches the entity's current `content_hash`, the
        // post-mutation user-visible state equals the on-disk state.
        // Skip the disk write, the commit, the provenance append,
        // and the store re-parse. Mirrors `relate.rs`'s
        // `NoOpAlreadyPresent` / `NoOpAbsent` and `rename.rs`'s
        // slug-noop short-circuits: empty `commit_sha`, unchanged
        // `content_hash`, preserved `last_modified`, typed
        // `UpdateNoop` warning. Skipped on the dry_run path because
        // the dry_run preview semantics document a separate
        // non-committing shape with `prospective_hash: Some(_)` and
        // the unchanged `content_hash`; conflating the two would
        // lose the prospective-hash channel callers use to chain a
        // follow-up real update with `expected_hash`.
        if !args.dry_run {
            let prospective_hash = crate::entity::parser::compute_hash(&markdown_pre_stamp);
            // `next` was cloned from `entity` at the top of this fn
            // and its `content_hash` field has not been recomputed
            // since — equals the on-disk value. Reading from `next`
            // rather than `entity` avoids extending the `self.store`
            // immutable borrow past the mutable borrow taken by
            // `apply_declare_relations`.
            if prospective_hash == next.content_hash {
                // No-op: report the preserved `last_modified` from
                // the pre-stamp `next` (which still carries the
                // entity's on-disk value because we haven't run the
                // auto-stamp yet on this branch).
                let modified_date = next
                    .metadata
                    .get("last_modified")
                    .and_then(|v| v.as_str().map(str::to_string))
                    .unwrap_or_default();
                return Ok(PrepareOutcome::Done(UpdateEntityOutcome {
                    id: id.clone(),
                    title: next.title.clone(),
                    file_path,
                    content_hash: next.content_hash.clone(),
                    commit_sha: String::new(),
                    modified_date,
                    // No-op: the prospective hash equals the on-disk hash,
                    // so nothing landed. `modified_*` report the *applied*
                    // delta (empty), consistent with the empty `commit_sha`
                    // and unchanged hash — not the request-derived keys,
                    // which would claim a change that did not happen
                    // (F1). The request vecs
                    // (`modified_metadata_set` etc.) are intentionally
                    // dropped on this branch.
                    modified_sections: ModifiedSections::default(),
                    modified_metadata: ModifiedMetadata::default(),
                    prospective_hash: None,
                    // No write happened on the no-op path, so nothing
                    // could have orphaned a stub.
                    orphan_stubs_removed: Vec::new(),
                    warnings: vec![WarningHint::UpdateNoop { id: id.clone() }],
                    relations_declared,
                }));
            }
        }

        // Real change: apply the auto-stamp now and regenerate the
        // markdown so the subsequent hash + write reflect it.
        super::auto_stamp_timestamps(&mut next, type_def.as_ref(), &today);
        let markdown = generate_markdown(&next, type_def.as_ref());

        let mut warnings: Vec<WarningHint> = Vec::new();

        // Mirror the create-path emission shape — drive the warning from
        // the synthesised relations the alias pass just emitted, not
        // from a re-parse of the generated markdown. `parse_markdown`
        // filters its `inline_links` against the entity's
        // `relationships` vec (which the synthesis pass has already
        // appended to), so the pre-fix path saw `inline_links: []`
        // and silently dropped the warning the docstring promises.
        let auto_stubbed: Vec<EntityId> = synthesised_relations
            .iter()
            .filter_map(|rel| {
                if !self.store.contains(&rel.target) {
                    Some(rel.target.clone())
                } else {
                    None
                }
            })
            .collect();
        if !auto_stubbed.is_empty() {
            warnings.push(WarningHint::InlineWikiLinkAutoStubbed {
                from: id.clone(),
                stubs: auto_stubbed,
            });
        }
        // F11: surface a dropped self-referential body link (the alias
        // pass omitted the vacuous self-edge).
        if self_link_ignored {
            warnings.push(WarningHint::SelfLinkIgnored { id: id.clone() });
        }

        // Dry-run: compute prospective hash from the in-memory
        // entity and return without touching disk, store, or
        // commits. Mirrors full's `UpdateArgs.dry_run` semantics —
        // `content_hash` carries the unchanged on-disk hash so the
        // caller can use it as `expected_hash` on the follow-up
        // real call (designated stale-hash recovery path).
        if args.dry_run {
            let prospective = crate::entity::parser::compute_hash(&markdown);
            // `next.content_hash` was cloned from the source
            // entity and not modified since; equals the on-disk
            // value.
            let current_hash = next.content_hash.clone();
            let modified_date = if type_def.metadata_fields.iter().any(|f| f.auto_timestamp) {
                today.clone()
            } else {
                String::new()
            };
            return Ok(PrepareOutcome::Done(UpdateEntityOutcome {
                id: id.clone(),
                title: next.title.clone(),
                file_path,
                content_hash: current_hash,
                commit_sha: String::new(),
                modified_date,
                modified_sections: ModifiedSections {
                    replaced: modified_sections,
                    appended: modified_sections_appended,
                    patched: modified_sections_patched,
                },
                modified_metadata: ModifiedMetadata {
                    set: modified_metadata_set,
                    unset: modified_metadata_unset,
                },
                prospective_hash: Some(prospective),
                // Dry-run touches neither store nor disk, so no stub
                // could have been GC'd.
                orphan_stubs_removed: Vec::new(),
                warnings,
                relations_declared: relations_declared.clone(),
            }));
        }

        // Real change prepared. Compute `modified_date` (mirrors full's
        // UpdateResult.modified_date — the `today` the auto-stamp loop
        // used; empty when the schema has no auto_timestamp field), then
        // hand the staged write to the caller to commit. The single
        // path commits immediately; the batch path commits the whole
        // set at once.
        let modified_date = if type_def.metadata_fields.iter().any(|f| f.auto_timestamp) {
            today.clone()
        } else {
            String::new()
        };

        Ok(PrepareOutcome::Prepared(PreparedUpdate {
            mount_idx,
            id: id.clone(),
            mem,
            type_def,
            file_path,
            markdown,
            prev_body_targets,
            modified_date,
            modified_sections: ModifiedSections {
                replaced: modified_sections,
                appended: modified_sections_appended,
                patched: modified_sections_patched,
            },
            modified_metadata: ModifiedMetadata {
                set: modified_metadata_set,
                unset: modified_metadata_unset,
            },
            // F5: `InlineWikiLinkAutoStubbed` rides on the outcome so the
            // update path matches create's contract.
            warnings,
            relations_declared,
        }))
    }

    /// Apply a batch of [`UpdateEntityArgs`] **atomically** — all or
    /// nothing. Surfaces `BatchResult` for `memstead batch-update`
    /// consumers.
    ///
    /// The batch validates and prepares every item first (each with
    /// its own optimistic-lock check), then commits the whole set as
    /// **one** commit per mem. If any item fails — validation error,
    /// `HASH_MISMATCH`, entity-not-found, any per-item refusal —
    /// **nothing is committed**: the on-disk mem and the in-memory
    /// store are restored to exactly their pre-call state, and the
    /// result is marked `applied: false` with the offending item
    /// carrying a typed `{code, message, details}` error envelope and
    /// every other item marked `"not_applied"`. The first failing item
    /// stops preparation (fail-fast); the caller fixes it and
    /// resubmits.
    ///
    /// On success the returned `commit_sha` is the single batch commit
    /// — an honest `memstead_changes_since` cursor / revert handle. Each
    /// item's per-entry note rides into its own provenance record.
    ///
    /// Empty batches return `applied: true` with zero counts and no
    /// commit. A batch where every item is a no-op (content unchanged)
    /// likewise applies with an empty `commit_sha`.
    ///
    /// Atomicity is per-mem: for the common single-mem batch a
    /// commit-time backend failure rolls the whole batch back. A batch
    /// spanning multiple mems commits each mem in turn; if a later
    /// mem's commit fails, already-committed mems stay committed
    /// (true cross-mem two-phase commit is out of scope) — but the
    /// dominant failure mode, a per-item validation/hash refusal, is
    /// always fully atomic because no commit happens until every item
    /// has passed.
    pub fn batch_update(
        &mut self,
        updates: Vec<(UpdateEntityArgs, Option<String>)>,
        actor: Actor,
        client: Option<&ClientId>,
    ) -> Result<crate::ops::BatchResult, EngineError> {
        if updates.is_empty() {
            return Ok(crate::ops::BatchResult {
                applied: true,
                results: Vec::new(),
                succeeded: 0,
                failed: 0,
                commit_sha: String::new(),
            });
        }

        // Reload-before-operation: refresh every mem this batch
        // touches *before* preparing items, so each item's
        // `expected_hash` check runs against current truth (the batch
        // is the one multi-op-per-process path, so a sibling commit
        // between boot and this call is plausible). Notices stash on
        // the engine for the caller to drain.
        let mut touched_mems: Vec<String> = updates
            .iter()
            .map(|(a, _)| a.id.mem().to_string())
            .collect();
        touched_mems.sort();
        touched_mems.dedup();
        for v in &touched_mems {
            self.reload_if_stale(Some(v));
        }

        // Snapshot the in-memory store so a refused batch (or a
        // commit-time backend failure) can roll back any auto-stubs
        // and store pushes that earlier items already applied during
        // preparation. The on-disk side rolls back by discarding each
        // backend's staged-but-uncommitted pending buffer.
        let store_snapshot = self.store.clone();

        // What each item is, in submission order, so the result
        // entries echo the input order. `Prepared` is a real write
        // (its `PreparedUpdate` lives in `prepared`); `Noop` is an
        // applied no-op (content unchanged, no write).
        enum Item {
            Prepared,
            Noop,
        }
        let mut items: Vec<(EntityId, Item)> = Vec::with_capacity(updates.len());
        let mut prepared: Vec<PreparedUpdate> = Vec::new();
        let mut notes: Vec<Option<String>> = Vec::new();

        // --- Phase 1: validate + prepare every item (no commits) ---
        let mut iter = updates.into_iter();
        while let Some((args, note)) = iter.next() {
            let id = args.id.clone();
            match self.prepare_update(args) {
                Ok(PrepareOutcome::Done(_)) => {
                    // No-op (batch never sets dry_run): applied, no write.
                    items.push((id, Item::Noop));
                }
                Ok(PrepareOutcome::Prepared(p)) => {
                    prepared.push(p);
                    notes.push(note);
                    items.push((id, Item::Prepared));
                }
                Err(e) => {
                    // Refuse the whole batch. Roll back store + disk.
                    self.store = store_snapshot;
                    self.discard_all_pending();
                    let mut results: Vec<crate::ops::BatchEntry> = items
                        .into_iter()
                        .map(|(prev_id, _)| crate::ops::BatchEntry {
                            id: prev_id,
                            action: "not_applied".to_string(),
                            error: None,
                        })
                        .collect();
                    results.push(crate::ops::BatchEntry {
                        id,
                        action: "error".to_string(),
                        error: Some(batch_error_envelope(&e)),
                    });
                    // Items after the failure were never prepared.
                    for (rem_args, _) in iter {
                        results.push(crate::ops::BatchEntry {
                            id: rem_args.id,
                            action: "not_applied".to_string(),
                            error: None,
                        });
                    }
                    return Ok(crate::ops::BatchResult {
                        applied: false,
                        results,
                        succeeded: 0,
                        failed: 1,
                        commit_sha: String::new(),
                    });
                }
            }
        }

        // --- Phase 2: stage every prepared write, then commit once
        // per mem. ---
        for p in &prepared {
            if let Err(e) = self.mounts[p.mount_idx]
                .backend
                .write_entity(Path::new(&p.file_path), p.markdown.as_bytes())
            {
                self.store = store_snapshot;
                self.discard_all_pending();
                return Err(e.into());
            }
        }

        // Distinct mount indices in first-seen order — one commit each.
        let mut distinct_mounts: Vec<usize> = Vec::new();
        for p in &prepared {
            if !distinct_mounts.contains(&p.mount_idx) {
                distinct_mounts.push(p.mount_idx);
            }
        }
        let mut mount_commits: Vec<(usize, String)> = Vec::with_capacity(distinct_mounts.len());
        for &m in &distinct_mounts {
            let entity_ids: Vec<String> = prepared
                .iter()
                .filter(|p| p.mount_idx == m)
                .map(|p| p.id.to_string())
                .collect();
            let count = entity_ids.len();
            let subject = format!("memstead: batch-update ({count} entities)");
            let ctx = CommitContext {
                actor,
                client: client.cloned(),
                tool: Some("batch_update"),
                note: None,
                logical_operation_id: None,
                // F13: name every entity this batch commit touched so an
                // `--include-notes` reader can recover them from the note
                // record alone — the subject only says `(N entities)`.
                entity_ids: Some(entity_ids),
            };
            match self.mounts[m].backend.commit(&subject, &ctx) {
                Ok(sha) => mount_commits.push((m, sha)),
                Err(e) => {
                    // A commit failed. Roll back the store and any
                    // still-pending backends. Mems already committed
                    // in this loop stay committed (per-mem atomicity).
                    self.store = store_snapshot;
                    self.discard_all_pending();
                    return Err(e.into());
                }
            }
        }

        // Provenance + store application per item, now that the commits
        // landed. `record_self_write` marks the commit as engine-self
        // so drift detection ignores it.
        for (p, note) in prepared.iter().zip(notes.iter()) {
            let commit_sha = mount_commits
                .iter()
                .find(|(m, _)| *m == p.mount_idx)
                .map(|(_, s)| s.clone())
                .unwrap_or_default();
            self.mounts[p.mount_idx]
                .backend
                .append_provenance(&Provenance::new(
                    std::time::SystemTime::now(),
                    ProvenanceKind::Update,
                    Some(p.id.to_string()),
                    actor,
                    client.cloned(),
                    note.clone(),
                ))?;
            self.record_self_write(p.mount_idx, &commit_sha);
            self.apply_prepared_to_store(p)?;
        }

        self.invalidate_communities();
        self.invalidate_search_indexes();

        // Single-mem batches name their one commit; multi-mem names
        // the last mem committed (see the method docstring).
        let commit_sha = mount_commits
            .last()
            .map(|(_, s)| s.clone())
            .unwrap_or_default();
        let succeeded = items.len();
        let results: Vec<crate::ops::BatchEntry> = items
            .into_iter()
            .map(|(id, item)| crate::ops::BatchEntry {
                id,
                action: match item {
                    Item::Prepared => "updated".to_string(),
                    Item::Noop => "noop".to_string(),
                },
                error: None,
            })
            .collect();

        Ok(crate::ops::BatchResult {
            applied: true,
            results,
            succeeded,
            failed: 0,
            commit_sha,
        })
    }

    /// Best-effort discard of every backend's staged-but-uncommitted
    /// pending buffer — the disk-side half of an atomic-batch rollback.
    /// Discard errors (a poisoned pending mutex) are swallowed: we are
    /// already unwinding a refused batch and have nothing better to do.
    fn discard_all_pending(&self) {
        for mount in &self.mounts {
            let _ = mount.backend.discard_pending();
        }
    }

    /// CommitContext-bundling wrapper around [`Self::update_entity`].
    /// See [`Self::create_entity_with_ctx`] for the rationale.
    pub fn update_entity_with_ctx(
        &mut self,
        args: UpdateEntityArgs,
        ctx: &CommitContext<'_>,
    ) -> Result<UpdateEntityOutcome, EngineError> {
        self.update_entity(args, ctx.actor, ctx.client.as_ref(), ctx.note.as_deref())
    }
}

/// Build a per-item structured error envelope for [`Engine::batch_update`].
/// Mirrors the `{code, message, details}` shape single-update failures
/// carry on the MCP wire so a mixed-success batch is structurally uniform.
/// Variants without a typed recovery payload (boundary / internal failures
/// like `ParseAfterWrite`, `Backend`) return an empty details object —
/// the code and message channels still discriminate.
fn batch_error_envelope(err: &EngineError) -> crate::ops::BatchError {
    // The per-item envelope reads the centralised
    // `EngineError::details()` helper so every typed variant ships the
    // same recovery payload the singleton MCP/CLI surfaces emit, so
    // agents' "fix from `details` rather than re-fetching" loop works
    // the same in batch mode.
    let code = err.code().to_string();
    let message = err.to_string();
    let details = err.details();
    crate::ops::BatchError {
        code,
        message,
        details,
    }
}

/// Validate, auto-stub, and append a batch of relation declarations
/// onto `next.relationships`. Returns the canonical
/// `RelationDeclared` summary echoed back on the outcome.
///
/// Validates each declared relation against the same gates
/// `memstead_relate` runs (target-id grammar, rel-type vocabulary,
/// schema shape, cross-mem policy, ReadOnly-target rule). On the
/// add path with an absent Write-mem target, the target is
/// auto-stubbed via `make_stub` — matching the
/// `WarningHint::AutoStubCreated` semantics of the relate flow.
///
/// Defined at module scope (rather than a method on `Engine`) so
/// the borrow on `engine.store` for the auto-stub upsert can run
/// alongside the `&mut next` borrow.
fn apply_declare_relations(
    engine: &mut Engine,
    next: &mut Entity,
    declarations: &[crate::ops::RelateArg],
    source_mem: &str,
    source_mount_idx: usize,
    type_def: &memstead_schema::TypeDefinition,
    schema: &memstead_schema::Schema,
) -> Result<Vec<RelationDeclared>, EngineError> {
    let _ = type_def; // Reserved for future per-type policy hooks.
    let _ = source_mount_idx; // Reserved for parity with delete.
    let mut declared: Vec<RelationDeclared> = Vec::with_capacity(declarations.len());
    for rel in declarations {
        // Canonicalise rel_type to UPPER_SNAKE_CASE so the validator
        // and the stored edge see the same wire-contract form.
        let canonical = crate::entity::id::validate_rel_type(&rel.rel_type)
            .unwrap_or_else(|_| rel.rel_type.clone());

        validate_relation_target_grammar(&rel.to)?;

        let target_mem = rel.to.mem().to_string();
        super::validate_cross_mem_add_policy(engine, source_mem, &target_mem)?;
        if target_mem != source_mem
            && let Some(mount) = engine.mount(&target_mem)
            && mount.capability == MountCapability::ReadOnly
            && !engine.store.contains(&rel.to)
        {
            return Err(EngineError::CrossMemTargetNotFound {
                target_id: rel.to.to_string(),
                target_mem: target_mem.clone(),
            });
        }

        // Rel-type + shape validation, routed through the engine's
        // cross-mem-aware edge validator. Cross-different-schema
        // edges check vocabulary + shape against the source schema's
        // `cross_mem_relationships:` entry; same-schema edges fall
        // through to the intra-mem `relationships.definitions`.
        // Open-mode admits unknown rel-types silently (no
        // per-declaration warning surfaced here — symmetry with the
        // pre-cross-mem behaviour).
        let target_type = engine
            .store
            .get(&rel.to)
            .map(|e| e.entity_type.clone())
            .filter(|t| !t.is_empty());
        let _ = schema; // Helper resolves the schema via the engine.
        let _ = super::route_edge_validation(
            engine,
            &canonical,
            next.entity_type.as_str(),
            target_type.as_deref(),
            source_mem,
            &target_mem,
            &next.id,
            &rel.to,
            /* check_shape = */ true,
        )?;

        // Per-edge description posture. Normalise first so
        // empty/whitespace-only inputs collapse to `None` and the
        // posture check sees a canonical input that matches what
        // the renderer will emit.
        let normalised_description =
            crate::entity::normalise_description(rel.description.as_deref());
        super::validate_description_posture(
            engine,
            &canonical,
            normalised_description.as_deref(),
            source_mem,
            &target_mem,
            &next.id,
            &rel.to,
        )?;
        // declare_relations is an explicit-author
        // boundary too — gate on manual_authoring posture.
        super::validate_manual_authoring_posture(
            engine, &canonical, source_mem, &next.id, &rel.to,
        )?;

        // Append to the entity's relationships list. Duplicate
        // declarations are idempotent — same (rel_type, target) pair
        // is a silent no-op so the agent can re-issue the same call
        // without surprise.
        let exists = next
            .relationships
            .iter()
            .any(|r| r.rel_type == canonical && r.target == rel.to);
        if !exists {
            next.relationships.push(Relationship {
                rel_type: canonical.clone(),
                target: rel.to.clone(),
                description: normalised_description,
            });
        }

        // Auto-stub absent Write-mem targets. Same mechanic as
        // `memstead_relate`'s relate path. ReadOnly cross-mem targets
        // were caught above; same-mem and cross-mem-to-Write
        // both fall through here.
        let target_was_stubbed = !engine.store.contains(&rel.to);
        if target_was_stubbed && !exists {
            engine.store.upsert(
                rel.to.clone(),
                make_stub(&rel.to, crate::entity::StubKind::ForwardReference),
            );
        }

        declared.push(RelationDeclared {
            rel_type: canonical,
            target: rel.to.clone(),
            target_was_stubbed,
        });
    }
    Ok(declared)
}

#[cfg(test)]
mod tests {

    use indexmap::IndexMap;
    use tempfile::TempDir;

    use crate::backend::MemBackend;
    use crate::engine::test_helpers::*;
    use crate::engine::{
        CreateEntityArgs, Engine, EngineError, RelateEntityArgs, UpdateEntityArgs,
    };
    use crate::entity::EntityId;

    use crate::storage::{ArchiveBackend, FilesystemMemWriter};
    use crate::vcs::Actor;

    #[test]
    fn batch_update_empty_batch_returns_zero_counts() {
        // No updates → BatchResult with zero counts + empty
        // commit_sha. No engine mutation happens.
        let tmp = TempDir::new().unwrap();
        let mem_dir = tmp.path().to_path_buf();
        let writer = FilesystemMemWriter::new(mem_dir.clone());
        let mut engine = Engine::from_mounts(vec![(
            folder_mount("specs", mem_dir),
            Box::new(writer) as Box<dyn MemBackend>,
        )])
        .unwrap();

        let result = engine.batch_update(Vec::new(), Actor::Cli, None).unwrap();
        assert!(result.applied, "empty batch is a vacuous success");
        assert_eq!(result.results.len(), 0);
        assert_eq!(result.succeeded, 0);
        assert_eq!(result.failed, 0);
        assert_eq!(result.commit_sha, "");
    }

    #[test]
    fn batch_update_refuses_whole_batch_when_one_item_fails() {
        // Atomic semantics: a 2-item batch where item 1 is valid and
        // item 2 targets a missing id refuses the WHOLE batch. Nothing
        // is committed — item 1 is NOT applied (its section change does
        // not land), `applied` is false, `commit_sha` is empty, the
        // missing item carries the typed ENTITY_NOT_FOUND envelope, and
        // the valid item is marked `not_applied`.
        let tmp = TempDir::new().unwrap();
        let mem_dir = tmp.path().to_path_buf();
        let writer = FilesystemMemWriter::new(mem_dir.clone());
        let mut engine = Engine::from_mounts(vec![(
            folder_mount("specs", mem_dir),
            Box::new(writer) as Box<dyn MemBackend>,
        )])
        .unwrap();

        // Seed: create an entity.
        let create_args = CreateEntityArgs {
            mem: "specs".to_string(),
            title: "Seed".to_string(),
            entity_type: "spec".to_string(),
            sections: IndexMap::from_iter([
                ("identity".to_string(), "seed identity".to_string()),
                ("purpose".to_string(), "seed purpose".to_string()),
            ]),
            metadata: IndexMap::new(),
            relations: Vec::new(),
            dry_run: false,
        };
        let created = engine
            .create_entity(create_args, Actor::Cli, None, None)
            .unwrap();

        // Batch: update the seed entity AND a missing id.
        let valid_update = UpdateEntityArgs {
            id: created.id.clone(),
            expected_hash: Some(created.content_hash.clone()),
            sections: IndexMap::from_iter([("identity".to_string(), "updated body".to_string())]),
            append_sections: IndexMap::new(),
            patch_sections: IndexMap::new(),
            metadata: IndexMap::new(),
            metadata_unset: Vec::new(),
            declare_relations: Vec::new(),
            dry_run: false,
            relations_unset: Vec::new(),
        };
        let missing_update = UpdateEntityArgs {
            id: EntityId("specs--nonexistent".to_string()),
            expected_hash: None,
            sections: IndexMap::new(),
            append_sections: IndexMap::new(),
            patch_sections: IndexMap::new(),
            metadata: IndexMap::new(),
            metadata_unset: Vec::new(),
            declare_relations: Vec::new(),
            dry_run: false,
            relations_unset: Vec::new(),
        };

        let result = engine
            .batch_update(
                vec![(valid_update, None), (missing_update, None)],
                Actor::Cli,
                None,
            )
            .unwrap();
        // Whole batch refused: nothing applied, no commit.
        assert!(!result.applied, "a failing item must refuse the batch");
        assert_eq!(result.results.len(), 2);
        assert_eq!(result.succeeded, 0);
        assert_eq!(result.failed, 1);
        assert_eq!(result.commit_sha, "", "refused batch must not commit");
        // First entry: the valid item, marked not_applied (the batch
        // was refused before it could land).
        assert_eq!(result.results[0].action, "not_applied");
        assert!(result.results[0].error.is_none());
        // Second entry: the failing item carries the typed envelope.
        assert_eq!(result.results[1].action, "error");
        let err = result.results[1]
            .error
            .as_ref()
            .expect("failed entry must carry a structured error envelope");
        assert_eq!(err.code, "ENTITY_NOT_FOUND");
        assert!(err.message.contains("not found"), "got: {}", err.message);

        // The valid item's section change must NOT have landed — the
        // store is byte-identical to pre-call.
        let seed = engine.get_entity(&created.id).unwrap();
        assert_eq!(
            seed.sections.get("identity").map(String::as_str),
            Some("seed identity"),
            "refused batch must leave the in-memory store untouched",
        );
        assert_eq!(
            seed.content_hash, created.content_hash,
            "refused batch must not change the entity's content hash",
        );
    }

    #[test]
    fn batch_update_applies_all_valid_items_as_one_commit() {
        // A 2-item batch where both items are valid
        // applies both and produces exactly one commit; the response's
        // commit_sha names it and both entries report "updated".
        let tmp = TempDir::new().unwrap();
        let mem_dir = tmp.path().to_path_buf();
        let writer = FilesystemMemWriter::new(mem_dir.clone());
        let mut engine = Engine::from_mounts(vec![(
            folder_mount("specs", mem_dir),
            Box::new(writer) as Box<dyn MemBackend>,
        )])
        .unwrap();

        let mk = |title: &str| CreateEntityArgs {
            mem: "specs".to_string(),
            title: title.to_string(),
            entity_type: "spec".to_string(),
            sections: IndexMap::from_iter([
                ("identity".to_string(), "id".to_string()),
                ("purpose".to_string(), "purp".to_string()),
            ]),
            metadata: IndexMap::new(),
            relations: Vec::new(),
            dry_run: false,
        };
        let a = engine
            .create_entity(mk("A"), Actor::Cli, None, None)
            .unwrap();
        let b = engine
            .create_entity(mk("B"), Actor::Cli, None, None)
            .unwrap();

        let upd = |id: EntityId, hash: String, body: &str| UpdateEntityArgs {
            id,
            expected_hash: Some(hash),
            sections: IndexMap::from_iter([("identity".to_string(), body.to_string())]),
            append_sections: IndexMap::new(),
            patch_sections: IndexMap::new(),
            metadata: IndexMap::new(),
            metadata_unset: Vec::new(),
            declare_relations: Vec::new(),
            dry_run: false,
            relations_unset: Vec::new(),
        };

        let result = engine
            .batch_update(
                vec![
                    (upd(a.id.clone(), a.content_hash.clone(), "A body"), None),
                    (upd(b.id.clone(), b.content_hash.clone(), "B body"), None),
                ],
                Actor::Cli,
                None,
            )
            .unwrap();
        assert!(result.applied);
        assert_eq!(result.succeeded, 2);
        assert_eq!(result.failed, 0);
        assert!(
            !result.commit_sha.is_empty(),
            "applied batch carries the commit"
        );
        assert!(result.results.iter().all(|e| e.action == "updated"));
        // Both section changes landed.
        assert_eq!(
            engine
                .get_entity(&a.id)
                .unwrap()
                .sections
                .get("identity")
                .map(String::as_str),
            Some("A body"),
        );
        assert_eq!(
            engine
                .get_entity(&b.id)
                .unwrap()
                .sections
                .get("identity")
                .map(String::as_str),
            Some("B body"),
        );
    }

    #[test]
    fn batch_update_rolls_back_in_memory_store_auto_stub_on_refusal() {
        // The subtle invariant: an earlier item that auto-stubs a
        // relation target during preparation must have that stub rolled
        // OUT of the in-memory store when a later item refuses the
        // batch. Item 1 declares a relation to an absent target (which
        // upserts a forward-reference stub during prepare); item 2
        // targets a missing entity and fails. The refusal must leave no
        // trace of the stub.
        let tmp = TempDir::new().unwrap();
        let mem_dir = tmp.path().to_path_buf();
        let writer = FilesystemMemWriter::new(mem_dir.clone());
        let mut engine = Engine::from_mounts(vec![(
            folder_mount("specs", mem_dir.clone()),
            Box::new(writer) as Box<dyn MemBackend>,
        )])
        .unwrap();
        engine.set_workspace_root(mem_dir);
        let (actor, client) = cli_actor();

        let a = engine
            .create_entity(
                empty_create_args("specs", "Anchor"),
                actor,
                Some(&client),
                None,
            )
            .unwrap();

        let stub_target = EntityId::new("specs", "would-be-stub");
        let item1 = UpdateEntityArgs {
            relations_unset: Vec::new(),
            id: a.id.clone(),
            expected_hash: Some(a.content_hash.clone()),
            sections: IndexMap::new(),
            append_sections: IndexMap::new(),
            patch_sections: IndexMap::new(),
            metadata: IndexMap::new(),
            metadata_unset: Vec::new(),
            declare_relations: vec![crate::ops::RelateArg {
                rel_type: "USES".to_string(),
                to: stub_target.clone(),
                description: None,
            }],
            dry_run: false,
        };
        let item2 = UpdateEntityArgs {
            id: EntityId::new("specs", "nonexistent"),
            expected_hash: None,
            sections: IndexMap::from_iter([("identity".to_string(), "x".to_string())]),
            append_sections: IndexMap::new(),
            patch_sections: IndexMap::new(),
            metadata: IndexMap::new(),
            metadata_unset: Vec::new(),
            declare_relations: Vec::new(),
            dry_run: false,
            relations_unset: Vec::new(),
        };

        // Sanity: the would-be stub does not exist before the batch.
        assert!(engine.get_entity(&stub_target).is_none());

        let result = engine
            .batch_update(vec![(item1, None), (item2, None)], actor, Some(&client))
            .unwrap();
        assert!(!result.applied, "missing item 2 must refuse the batch");

        // The auto-stub item 1 created during preparation was rolled
        // back with the store snapshot — no orphaned stub survives.
        assert!(
            engine.get_entity(&stub_target).is_none(),
            "refused batch must roll the in-memory auto-stub back out of the store",
        );
        // The anchor's relation set is unchanged too.
        let anchor = engine.get_entity(&a.id).unwrap();
        assert!(
            !anchor.relationships.iter().any(|r| r.target == stub_target),
            "refused batch must not leave the declared relation on the anchor",
        );
    }

    #[test]
    fn update_entity_replaces_a_section_and_logs_provenance() {
        let tmp = TempDir::new().unwrap();
        let (mut engine, seeded) = engine_with_seed(&tmp, "Updatable");
        let (actor, client) = cli_actor();

        let mut sections = IndexMap::new();
        sections.insert("identity".to_string(), "Updated body.".to_string());

        let outcome = engine
            .update_entity(
                UpdateEntityArgs {
                    id: seeded.id.clone(),
                    expected_hash: Some(seeded.content_hash.clone()),
                    sections,
                    append_sections: IndexMap::new(),
                    patch_sections: IndexMap::new(),
                    metadata: IndexMap::new(),
                    metadata_unset: Vec::new(),
                    declare_relations: Vec::new(),
                    dry_run: false,
                    relations_unset: Vec::new(),
                },
                actor,
                Some(&client),
                Some("section update"),
            )
            .unwrap();

        assert_eq!(
            outcome.modified_sections.replaced,
            vec!["identity".to_string()]
        );
        assert_ne!(
            outcome.content_hash, seeded.content_hash,
            "hash must change"
        );
        // Store carries the new content.
        let entity = engine.get_entity(&seeded.id).unwrap();
        assert!(
            entity
                .sections
                .get("identity")
                .unwrap()
                .contains("Updated body.")
        );
        // Provenance log records the update.
        let log = std::fs::read_to_string(tmp.path().join(".memstead/changes.jsonl")).unwrap();
        assert!(log.contains("\"kind\":\"update\""));
        assert!(log.contains("\"note\":\"section update\""));
    }

    #[test]
    fn update_entity_rejects_hash_mismatch() {
        let tmp = TempDir::new().unwrap();
        let (mut engine, seeded) = engine_with_seed(&tmp, "Hash Guarded");
        let (actor, client) = cli_actor();
        let err = engine
            .update_entity(
                UpdateEntityArgs {
                    id: seeded.id.clone(),
                    expected_hash: Some("wrong-hash".to_string()),
                    sections: IndexMap::new(),
                    append_sections: IndexMap::new(),
                    patch_sections: IndexMap::new(),
                    metadata: IndexMap::new(),
                    metadata_unset: Vec::new(),
                    declare_relations: Vec::new(),
                    dry_run: false,
                    relations_unset: Vec::new(),
                },
                actor,
                Some(&client),
                None,
            )
            .unwrap_err();
        match err {
            EngineError::HashMismatch {
                id,
                current,
                is_stub,
            } => {
                assert_eq!(id, seeded.id.to_string());
                assert_eq!(current, seeded.content_hash);
                assert!(!is_stub, "real entity must not flag as stub");
            }
            other => panic!("expected HashMismatch, got {other:?}"),
        }
    }

    #[test]
    fn update_entity_rejects_unknown_id() {
        let tmp = TempDir::new().unwrap();
        let (mut engine, _) = engine_with_seed(&tmp, "Anchor");
        let (actor, client) = cli_actor();
        let err = engine
            .update_entity(
                UpdateEntityArgs {
                    id: crate::EntityId::new("specs", "ghost"),
                    expected_hash: None,
                    sections: IndexMap::new(),
                    append_sections: IndexMap::new(),
                    patch_sections: IndexMap::new(),
                    metadata: IndexMap::new(),
                    metadata_unset: Vec::new(),
                    declare_relations: Vec::new(),
                    dry_run: false,
                    relations_unset: Vec::new(),
                },
                actor,
                Some(&client),
                None,
            )
            .unwrap_err();
        assert!(matches!(err, EngineError::NotFound { .. }));
    }

    #[test]
    fn update_entity_rejects_read_only_mount() {
        let tmp = TempDir::new().unwrap();
        let archive_path = build_archive(
            tmp.path(),
            "ext",
            &[(
                "a.md",
                b"---\ntype: spec\n---\n# A\n\n## Identity\n\nbody.\n",
            )],
        );
        let mut engine = Engine::from_mounts(vec![(
            archive_mount("external", archive_path.clone()),
            Box::new(ArchiveBackend::new(archive_path)),
        )])
        .unwrap();
        let (actor, client) = cli_actor();
        let id = crate::EntityId::new("external", "a");
        let err = engine
            .update_entity(
                UpdateEntityArgs {
                    id,
                    expected_hash: None,
                    sections: IndexMap::new(),
                    append_sections: IndexMap::new(),
                    patch_sections: IndexMap::new(),
                    metadata: IndexMap::new(),
                    metadata_unset: Vec::new(),
                    declare_relations: Vec::new(),
                    dry_run: false,
                    relations_unset: Vec::new(),
                },
                actor,
                Some(&client),
                None,
            )
            .unwrap_err();
        assert!(matches!(err, EngineError::ReadOnlyMount(v) if v == "external"));
    }

    #[test]
    fn update_entity_patches_section_with_find_and_replace() {
        let tmp = TempDir::new().unwrap();
        let (mut engine, seeded) = engine_with_seed(&tmp, "Patch Subject");
        let (actor, client) = cli_actor();

        // Pre-write a known body via the replace path so the patch
        // test has a deterministic substring to target.
        let mut replace = IndexMap::new();
        replace.insert("identity".to_string(), "hello world hello".to_string());
        let replaced = engine
            .update_entity(
                UpdateEntityArgs {
                    id: seeded.id.clone(),
                    expected_hash: Some(seeded.content_hash.clone()),
                    sections: replace,
                    append_sections: IndexMap::new(),
                    patch_sections: IndexMap::new(),
                    metadata: IndexMap::new(),
                    metadata_unset: Vec::new(),
                    declare_relations: Vec::new(),
                    dry_run: false,
                    relations_unset: Vec::new(),
                },
                actor,
                Some(&client),
                None,
            )
            .unwrap();

        // First-occurrence patch (all = false).
        let mut patches = IndexMap::new();
        patches.insert(
            "identity".to_string(),
            crate::ops::PatchArg {
                old: "hello".to_string(),
                new: "HI".to_string(),
                all: false,
            },
        );
        let outcome = engine
            .update_entity(
                UpdateEntityArgs {
                    id: seeded.id.clone(),
                    expected_hash: Some(replaced.content_hash.clone()),
                    sections: IndexMap::new(),
                    append_sections: IndexMap::new(),
                    patch_sections: patches,
                    metadata: IndexMap::new(),
                    metadata_unset: Vec::new(),
                    declare_relations: Vec::new(),
                    dry_run: false,
                    relations_unset: Vec::new(),
                },
                actor,
                Some(&client),
                None,
            )
            .unwrap();
        assert_eq!(outcome.modified_sections.patched, vec!["identity"]);
        let body = engine
            .get_entity(&seeded.id)
            .unwrap()
            .sections
            .get("identity")
            .unwrap()
            .clone();
        assert!(body.contains("HI world hello"), "first-only: {body:?}");
    }

    #[test]
    fn update_entity_patch_rejects_missing_old_substring() {
        let tmp = TempDir::new().unwrap();
        let (mut engine, seeded) = engine_with_seed(&tmp, "Patch Miss");
        let (actor, client) = cli_actor();
        let mut patches = IndexMap::new();
        patches.insert(
            "identity".to_string(),
            crate::ops::PatchArg {
                old: "this-substring-does-not-exist".to_string(),
                new: "nope".to_string(),
                all: false,
            },
        );
        let err = engine
            .update_entity(
                UpdateEntityArgs {
                    id: seeded.id.clone(),
                    expected_hash: Some(seeded.content_hash.clone()),
                    sections: IndexMap::new(),
                    append_sections: IndexMap::new(),
                    patch_sections: patches,
                    metadata: IndexMap::new(),
                    metadata_unset: Vec::new(),
                    declare_relations: Vec::new(),
                    dry_run: false,
                    relations_unset: Vec::new(),
                },
                actor,
                Some(&client),
                None,
            )
            .unwrap_err();
        match err {
            EngineError::PatchOldNotFound { section, .. } => {
                assert_eq!(section, "identity");
            }
            other => panic!("expected PatchOldNotFound, got {other:?}"),
        }
    }

    #[test]
    fn update_entity_appends_to_existing_section_with_newline_separator() {
        let tmp = TempDir::new().unwrap();
        let (mut engine, seeded) = engine_with_seed(&tmp, "Append Subject");
        let (actor, client) = cli_actor();

        let mut appends = IndexMap::new();
        appends.insert("identity".to_string(), "appended tail.".to_string());

        let outcome = engine
            .update_entity(
                UpdateEntityArgs {
                    id: seeded.id.clone(),
                    expected_hash: Some(seeded.content_hash.clone()),
                    sections: IndexMap::new(),
                    append_sections: appends,
                    patch_sections: IndexMap::new(),
                    metadata: IndexMap::new(),
                    metadata_unset: Vec::new(),
                    declare_relations: Vec::new(),
                    dry_run: false,
                    relations_unset: Vec::new(),
                },
                actor,
                Some(&client),
                None,
            )
            .unwrap();

        // modified_sections.appended carries the append key;
        // modified_sections.replaced stays empty.
        assert_eq!(outcome.modified_sections.appended, vec!["identity"]);
        assert!(outcome.modified_sections.replaced.is_empty());

        // The section body now contains the appended tail.
        let updated = engine.get_entity(&seeded.id).unwrap();
        let body = updated.sections.get("identity").expect("identity section");
        assert!(
            body.contains("appended tail."),
            "appended body missing: {body:?}"
        );
    }

    /// Item 02: `memstead_update` against a stub must surface a typed
    /// `StubNotUpdatable` envelope rather than the pre-fix
    /// `UnknownType { name: "" }` cascade. Mirrors the
    /// `StubCannotRelate` guard that `memstead_relate` already runs;
    /// before Item 02 the docstring list advertised the
    /// `STUB_NOT_UPDATABLE` code but no engine path emitted it.
    #[test]
    fn update_entity_against_stub_surfaces_typed_stub_not_updatable() {
        let tmp = TempDir::new().unwrap();
        let (mut engine, source) = engine_with_seed(&tmp, "Source");
        let (actor, client) = cli_actor();
        // Materialise a stub by relating from a real entity to an
        // absent target. The relate path upserts the stub.
        let stub_id = crate::EntityId::new("specs", "stub-update-target");
        engine
            .relate_entity(
                RelateEntityArgs {
                    source: source.id.clone(),
                    expected_hash: Some(source.content_hash.clone()),
                    rel_type: "USES".to_string(),
                    target: stub_id.clone(),
                    remove: false,
                    description: None,
                },
                actor,
                Some(&client),
                None,
            )
            .unwrap();

        let err = engine
            .update_entity(
                UpdateEntityArgs {
                    id: stub_id.clone(),
                    expected_hash: Some(String::new()),
                    sections: IndexMap::from_iter([("identity".to_string(), "body".to_string())]),
                    append_sections: IndexMap::new(),
                    patch_sections: IndexMap::new(),
                    metadata: IndexMap::new(),
                    metadata_unset: Vec::new(),
                    declare_relations: Vec::new(),
                    dry_run: false,
                    relations_unset: Vec::new(),
                },
                actor,
                Some(&client),
                None,
            )
            .unwrap_err();
        match err {
            EngineError::StubNotUpdatable { id } => assert_eq!(id, stub_id.to_string()),
            other => panic!("expected StubNotUpdatable, got {other:?}"),
        }
    }

    #[test]
    fn update_entity_rejects_conflicting_section_modes() {
        let tmp = TempDir::new().unwrap();
        let (mut engine, seeded) = engine_with_seed(&tmp, "Conflict");
        let (actor, client) = cli_actor();

        let mut sections = IndexMap::new();
        sections.insert("identity".to_string(), "replace".to_string());
        let mut appends = IndexMap::new();
        appends.insert("identity".to_string(), "append".to_string());

        let err = engine
            .update_entity(
                UpdateEntityArgs {
                    id: seeded.id.clone(),
                    expected_hash: Some(seeded.content_hash.clone()),
                    sections,
                    append_sections: appends,
                    patch_sections: IndexMap::new(),
                    metadata: IndexMap::new(),
                    metadata_unset: Vec::new(),
                    declare_relations: Vec::new(),
                    dry_run: false,
                    relations_unset: Vec::new(),
                },
                actor,
                Some(&client),
                None,
            )
            .unwrap_err();

        match err {
            EngineError::ConflictingSectionModes { section, modes } => {
                assert_eq!(section, "identity");
                assert_eq!(modes, vec!["sections", "append_sections"]);
            }
            other => panic!("expected ConflictingSectionModes, got {other:?}"),
        }
    }

    #[test]
    fn update_entity_rejects_overlapping_metadata_and_metadata_unset_keys() {
        // Wire contract: setting and unsetting the same key is a hard
        // error. The check runs before the required-field check so the
        // resolution (pick one map) is unambiguous regardless of
        // whether the conflicting key is required.
        let tmp = TempDir::new().unwrap();
        let (mut engine, seeded) = engine_with_seed(&tmp, "Overlap Subject");
        let (actor, client) = cli_actor();

        let mut metadata = IndexMap::new();
        // `tags` is a non-required field on the default `spec` schema —
        // so this conflict is purely about the overlap, not about
        // unsetting-a-required-field.
        metadata.insert("tags".to_string(), "foo".to_string());

        let err = engine
            .update_entity(
                UpdateEntityArgs {
                    id: seeded.id.clone(),
                    expected_hash: Some(seeded.content_hash.clone()),
                    sections: IndexMap::new(),
                    append_sections: IndexMap::new(),
                    patch_sections: IndexMap::new(),
                    metadata,
                    metadata_unset: vec!["tags".to_string()],
                    declare_relations: Vec::new(),
                    dry_run: false,
                    relations_unset: Vec::new(),
                },
                actor,
                Some(&client),
                None,
            )
            .unwrap_err();
        match err {
            EngineError::SetAndUnsetConflict { keys } => {
                assert_eq!(keys, vec!["tags".to_string()]);
            }
            other => panic!("expected SetAndUnsetConflict, got {other:?}"),
        }
    }

    #[test]
    fn update_entity_pointer_schema_auto_synthesises_references_from_body_link() {
        // Under the default schema's `alias_target_rel_type: REFERENCES`
        // pointer, a body wiki-link no longer trips the strict validator
        // — the alias-synthesis pass emits the REFERENCES relation
        // first, the validator finds the link backed, the body lands.
        // (Schemas without the pointer continue to refuse with
        // `WIKILINK_WITHOUT_RELATION`; that path is covered by the
        // dedicated no-pointer fixture test elsewhere.)
        use crate::EntityId;
        use crate::engine::UpdateEntityArgs;
        use indexmap::IndexMap;
        use tempfile::TempDir;

        let tmp = TempDir::new().unwrap();
        let mem_dir = tmp.path().to_path_buf();
        let writer = FilesystemMemWriter::new(mem_dir.clone());
        let mut engine = Engine::from_mounts(vec![(
            folder_mount("specs", mem_dir.clone()),
            Box::new(writer) as Box<dyn MemBackend>,
        )])
        .unwrap();
        engine.set_workspace_root(mem_dir.clone());
        let (actor, client) = cli_actor();

        let target = engine
            .create_entity(
                empty_create_args("specs", "Target"),
                actor,
                Some(&client),
                None,
            )
            .unwrap();
        let source = engine
            .create_entity(
                empty_create_args("specs", "Source"),
                actor,
                Some(&client),
                None,
            )
            .unwrap();

        let mut sections: IndexMap<String, String> = IndexMap::new();
        sections.insert(
            "purpose".to_string(),
            "see [[target]] for context".to_string(),
        );
        let outcome = engine
            .update_entity(
                UpdateEntityArgs {
                    id: source.id.clone(),
                    expected_hash: Some(source.content_hash.clone()),
                    sections,
                    append_sections: IndexMap::new(),
                    patch_sections: IndexMap::new(),
                    metadata: IndexMap::new(),
                    metadata_unset: Vec::new(),
                    declare_relations: Vec::new(),
                    dry_run: false,
                    relations_unset: Vec::new(),
                },
                actor,
                Some(&client),
                None,
            )
            .expect("auto-synthesis must satisfy the alias-existence invariant");
        // Body landed.
        assert!(
            outcome
                .modified_sections
                .replaced
                .iter()
                .any(|s| s == "purpose"),
        );
        let in_mem = engine.get_entity(&source.id).unwrap();
        assert_eq!(
            in_mem
                .sections
                .get("purpose")
                .map(String::as_str)
                .unwrap_or(""),
            "see [[target]] for context",
        );
        // REFERENCES relation synthesised from the body wiki-link.
        assert!(
            in_mem
                .relationships
                .iter()
                .any(|r| r.rel_type == "REFERENCES" && r.target == target.id),
            "synthesis must emit REFERENCES → target; relationships: {:?}",
            in_mem.relationships,
        );
        // Defeat unused-import warnings for the helper imports.
        let _ = EntityId::new("specs", "x");
    }

    #[test]
    fn update_entity_declare_relations_passes_strict_validator_in_one_call() {
        // The agent declares the relation + adds the body wiki-link
        // in a single `memstead_update` call. Without
        // `declare_relations`, the strict validator would refuse
        // (no backing relation yet); with the batched declaration,
        // the relation lands *before* the strict validator runs so
        // the body link passes the gate.
        use crate::engine::UpdateEntityArgs;
        use crate::ops::RelateArg;
        use indexmap::IndexMap;
        use tempfile::TempDir;

        let tmp = TempDir::new().unwrap();
        let mem_dir = tmp.path().to_path_buf();
        let writer = FilesystemMemWriter::new(mem_dir.clone());
        let mut engine = Engine::from_mounts(vec![(
            folder_mount("specs", mem_dir.clone()),
            Box::new(writer) as Box<dyn MemBackend>,
        )])
        .unwrap();
        engine.set_workspace_root(mem_dir.clone());
        let (actor, client) = cli_actor();

        let target = engine
            .create_entity(
                empty_create_args("specs", "Target"),
                actor,
                Some(&client),
                None,
            )
            .unwrap();
        let source = engine
            .create_entity(
                empty_create_args("specs", "Source"),
                actor,
                Some(&client),
                None,
            )
            .unwrap();

        // Atomic declare + body update. USES (not REFERENCES) — under
        // the default schema's `alias_target_rel_type: REFERENCES`
        // pointer, explicit declare_relations type=REFERENCES is
        // refused; the body wiki-link is auto-emitted via synthesis.
        // The test's intent — that declare_relations atomically lands
        // alongside body changes — holds for any rel-type that admits
        // explicit authoring.
        let mut sections: IndexMap<String, String> = IndexMap::new();
        sections.insert(
            "purpose".to_string(),
            "see [[target]] for context".to_string(),
        );
        let outcome = engine
            .update_entity(
                UpdateEntityArgs {
                    relations_unset: Vec::new(),
                    id: source.id.clone(),
                    expected_hash: Some(source.content_hash.clone()),
                    sections,
                    append_sections: IndexMap::new(),
                    patch_sections: IndexMap::new(),
                    metadata: IndexMap::new(),
                    metadata_unset: Vec::new(),
                    dry_run: false,
                    declare_relations: vec![RelateArg {
                        rel_type: "USES".to_string(),
                        to: target.id.clone(),
                        description: None,
                    }],
                },
                actor,
                Some(&client),
                None,
            )
            .expect("declare_relations + body update must succeed in one call");

        assert_eq!(outcome.relations_declared.len(), 1);
        assert_eq!(outcome.relations_declared[0].rel_type, "USES");
        assert_eq!(outcome.relations_declared[0].target, target.id);
        assert!(
            !outcome.relations_declared[0].target_was_stubbed,
            "target was already present in store; target_was_stubbed must be false"
        );

        let in_mem = engine.get_entity(&source.id).unwrap();
        assert!(
            in_mem.relationships.iter().any(|r| r.target == target.id),
            "declared relation must land in entity.relationships; got {:?}",
            in_mem.relationships
        );
    }

    #[test]
    fn update_entity_declare_relations_auto_stubs_absent_target() {
        // When the declared target doesn't exist yet, the engine
        // auto-stubs it (same mechanic as `memstead_relate`) and flags
        // `target_was_stubbed: true` in the outcome.
        use crate::EntityId;
        use crate::engine::UpdateEntityArgs;
        use crate::ops::RelateArg;
        use indexmap::IndexMap;

        let tmp = TempDir::new().unwrap();
        let (mut engine, source) = engine_with_seed(&tmp, "Source");
        let (actor, client) = cli_actor();
        let absent_target = EntityId::new("specs", "not-yet-existing");
        assert!(!engine.store().contains(&absent_target));

        let outcome = engine
            .update_entity(
                UpdateEntityArgs {
                    relations_unset: Vec::new(),
                    id: source.id.clone(),
                    expected_hash: Some(source.content_hash.clone()),
                    sections: IndexMap::new(),
                    append_sections: IndexMap::new(),
                    patch_sections: IndexMap::new(),
                    metadata: IndexMap::new(),
                    metadata_unset: Vec::new(),
                    dry_run: false,
                    declare_relations: vec![RelateArg {
                        rel_type: "USES".to_string(),
                        to: absent_target.clone(),
                        description: None,
                    }],
                },
                actor,
                Some(&client),
                None,
            )
            .unwrap();

        assert_eq!(outcome.relations_declared.len(), 1);
        assert!(
            outcome.relations_declared[0].target_was_stubbed,
            "absent target must be auto-stubbed; got target_was_stubbed=false"
        );
        // Stub now exists in the store.
        assert!(engine.store().contains(&absent_target));
        let stub = engine.get_entity(&absent_target).unwrap();
        assert!(stub.stub);
    }

    #[test]
    fn update_entity_alias_synthesis_runs_unconditionally_for_pointer_schemas() {
        // Under the alias model with a pointer-set schema (default
        // schema's `alias_target_rel_type: REFERENCES`), a fresh
        // workspace's first body-wiki-link write triggers the
        // alias-synthesis pass and the mutation lands with the
        // REFERENCES relation auto-emitted.
        use crate::engine::UpdateEntityArgs;
        use indexmap::IndexMap;
        use tempfile::TempDir;

        let tmp = TempDir::new().unwrap();
        let mem_dir = tmp.path().to_path_buf();
        let writer = FilesystemMemWriter::new(mem_dir.clone());
        let mut engine = Engine::from_mounts(vec![(
            folder_mount("specs", mem_dir.clone()),
            Box::new(writer) as Box<dyn MemBackend>,
        )])
        .unwrap();
        engine.set_workspace_root(mem_dir.clone());
        let (actor, client) = cli_actor();
        let target = engine
            .create_entity(
                empty_create_args("specs", "Target"),
                actor,
                Some(&client),
                None,
            )
            .unwrap();
        let source = engine
            .create_entity(
                empty_create_args("specs", "Source"),
                actor,
                Some(&client),
                None,
            )
            .unwrap();

        let mut sections: IndexMap<String, String> = IndexMap::new();
        sections.insert(
            "purpose".to_string(),
            "see [[target]] for context".to_string(),
        );
        engine
            .update_entity(
                UpdateEntityArgs {
                    id: source.id.clone(),
                    expected_hash: Some(source.content_hash.clone()),
                    sections,
                    append_sections: IndexMap::new(),
                    patch_sections: IndexMap::new(),
                    metadata: IndexMap::new(),
                    metadata_unset: Vec::new(),
                    declare_relations: Vec::new(),
                    dry_run: false,
                    relations_unset: Vec::new(),
                },
                actor,
                Some(&client),
                None,
            )
            .expect("synthesis must back the wiki-link and let the body land");
        let in_mem = engine.get_entity(&source.id).unwrap();
        assert!(
            in_mem
                .relationships
                .iter()
                .any(|r| r.rel_type == "REFERENCES" && r.target == target.id),
            "synthesis must emit REFERENCES → target; relationships: {:?}",
            in_mem.relationships,
        );
    }

    #[test]
    fn update_entity_dry_run_returns_prospective_hash_without_writing() {
        let tmp = TempDir::new().unwrap();
        let (mut engine, seeded) = engine_with_seed(&tmp, "Preview Subject");
        let (actor, client) = cli_actor();
        let original_hash = seeded.content_hash.clone();

        let mut sections = IndexMap::new();
        sections.insert("identity".to_string(), "preview body".to_string());

        let outcome = engine
            .update_entity(
                UpdateEntityArgs {
                    id: seeded.id.clone(),
                    // Stale-hash recovery path — dry_run skips the
                    // hash check, so a wrong expected_hash is OK.
                    expected_hash: Some("wrong-hash".to_string()),
                    sections,
                    append_sections: IndexMap::new(),
                    patch_sections: IndexMap::new(),
                    metadata: IndexMap::new(),
                    metadata_unset: Vec::new(),
                    declare_relations: Vec::new(),
                    dry_run: true,
                    relations_unset: Vec::new(),
                },
                actor,
                Some(&client),
                None,
            )
            .unwrap();

        // Wire shape: content_hash = current; prospective_hash =
        // what the write would produce; commit_sha empty.
        assert_eq!(outcome.content_hash, original_hash);
        let prospective = outcome
            .prospective_hash
            .expect("prospective_hash populated on dry_run");
        assert_ne!(prospective, original_hash);
        assert!(outcome.commit_sha.is_empty());
        // Store entity unchanged.
        let store_entity = engine.get_entity(&seeded.id).unwrap();
        assert_eq!(store_entity.content_hash, original_hash);
    }

    /// Edge-count round-trip lock. Captures the REFERENCES-drift
    /// finding:
    /// a mutation cycle (create body wiki-links → relate → update
    /// body → rename → delete) must return both `total_edges` and the
    /// REFERENCES counter to the pre-cycle values exactly. The bug
    /// was in `push_entities_into_store`: `upsert` preserved the
    /// entity's pre-existing out-edges, so `add_edge` (idempotent on
    /// `(from, to, rel_type)`) couldn't remove edges that the new
    /// parse no longer emits. Dropping a wiki-link from a body or
    /// absorbing one into an explicit relationship leaked the stale
    /// REFERENCES edge.
    ///
    /// Under the alias model the leak is structurally impossible —
    /// body wiki-links no longer emit edges, so the cleanup-on-reparse
    /// path the original test exercised has no premise. The test
    /// keeps the CRUD cycle but routes edges through atomic
    /// `relations:` declarations and explicit `memstead_relate`, which is
    /// what the model now treats as the only edge source.
    #[test]
    fn references_edges_round_trip_across_full_crud_cycle() {
        let tmp = TempDir::new().unwrap();
        let mem_dir = tmp.path().to_path_buf();
        let writer = FilesystemMemWriter::new(mem_dir.clone());
        let mut engine = Engine::from_mounts(vec![(
            folder_mount("specs", mem_dir),
            Box::new(writer) as Box<dyn MemBackend>,
        )])
        .unwrap();
        let (actor, client) = cli_actor();

        // Seed two link targets so the wiki-links inside the
        // probe entity's body resolve to real entities (not auto-
        // stubs we'd then have to GC).
        let foo = engine
            .create_entity(
                empty_create_args("specs", "Foo"),
                actor,
                Some(&client),
                None,
            )
            .unwrap();
        let bar = engine
            .create_entity(
                empty_create_args("specs", "Bar"),
                actor,
                Some(&client),
                None,
            )
            .unwrap();

        let count_references = |engine: &Engine| -> usize {
            engine
                .store()
                .all_ids()
                .flat_map(|id| engine.store().outgoing(id))
                .filter(|e| e.rel_type == "REFERENCES")
                .count()
        };

        let baseline_edges = engine.store().edge_count();
        let baseline_refs = count_references(&engine);

        // Step 1: create entity with body wiki-links — the
        // alias-synthesis pass auto-emits one REFERENCES per body
        // wiki-link (default schema's `alias_target_rel_type` →
        // REFERENCES), so the explicit `relations:` slot stays
        // empty. Net: 2 REFERENCES.
        let mut sections = IndexMap::new();
        sections.insert(
            "identity".to_string(),
            "See [[foo]] and [[bar]] inline.".to_string(),
        );
        sections.insert("purpose".to_string(), "probe purpose".to_string());
        let probe = engine
            .create_entity(
                CreateEntityArgs {
                    mem: "specs".to_string(),
                    title: "Probe".to_string(),
                    entity_type: "spec".to_string(),
                    sections,
                    metadata: IndexMap::new(),
                    relations: Vec::new(),
                    dry_run: false,
                },
                actor,
                Some(&client),
                None,
            )
            .unwrap();
        assert_eq!(count_references(&engine), baseline_refs + 2);

        // Step 2: relate INFORMED_BY → foo as a second relation to the
        // same target. The body wiki-link `[[foo]]` aliases the set of
        // relations to foo, so adding INFORMED_BY does not affect the
        // REFERENCES count — both relations coexist.
        let relate1 = engine
            .relate_entity(
                RelateEntityArgs {
                    source: probe.id.clone(),
                    expected_hash: Some(probe.content_hash.clone()),
                    rel_type: "INFORMED_BY".to_string(),
                    target: foo.id.clone(),
                    remove: false,
                    description: None,
                },
                actor,
                Some(&client),
                None,
            )
            .unwrap();
        assert_eq!(
            count_references(&engine),
            baseline_refs + 2,
            "set-membership aliasing — adding INFORMED_BY does not \
             absorb the REFERENCES relation"
        );

        // Step 3: drop the [[bar]] body link. The alias-synthesis pass
        // GCs the synthesised REFERENCES → bar atomically with the
        // body update — no second `memstead_relate --remove` needed.
        let mut sections = IndexMap::new();
        sections.insert("identity".to_string(), "See [[foo]] inline.".to_string());
        let updated = engine
            .update_entity(
                UpdateEntityArgs {
                    id: probe.id.clone(),
                    expected_hash: Some(relate1.content_hash.clone()),
                    sections,
                    append_sections: IndexMap::new(),
                    patch_sections: IndexMap::new(),
                    metadata: IndexMap::new(),
                    metadata_unset: Vec::new(),
                    declare_relations: Vec::new(),
                    dry_run: false,
                    relations_unset: Vec::new(),
                },
                actor,
                Some(&client),
                None,
            )
            .unwrap();
        assert_eq!(
            count_references(&engine),
            baseline_refs + 1,
            "REFERENCES → bar must be auto-GC'd when its body link drops"
        );

        // Step 4: rename the entity. Edges follow via remove + push.
        let renamed = engine
            .rename_entity(
                crate::engine::RenameEntityArgs {
                    id: probe.id.clone(),
                    expected_hash: Some(updated.content_hash.clone()),
                    new_title: "Probe Renamed".to_string(),
                },
                actor,
                Some(&client),
                None,
            )
            .unwrap();
        assert_eq!(count_references(&engine), baseline_refs + 1);

        // Step 5: delete the renamed entity. The INFORMED_BY → foo
        // edge cascades; REFERENCES count unchanged.
        engine
            .delete_entity(
                crate::engine::DeleteEntityArgs {
                    id: renamed.new_id.clone(),
                    expected_hash: Some(renamed.content_hash.clone()),
                },
                actor,
                Some(&client),
                None,
            )
            .unwrap();

        // Final assertion: every counter back to baseline.
        assert_eq!(
            engine.store().edge_count(),
            baseline_edges,
            "total edges must round-trip to baseline"
        );
        assert_eq!(
            count_references(&engine),
            baseline_refs,
            "REFERENCES counter must round-trip to baseline"
        );

        // Cross-check: a full reload of the mem produces the same
        // post-cycle counts. If the in-memory store and the on-disk
        // bytes drift, reload uncovers it.
        engine.reload_one_mem("specs").unwrap();
        assert_eq!(
            engine.store().edge_count(),
            baseline_edges,
            "total edges must match disk after reload"
        );
        assert_eq!(
            count_references(&engine),
            baseline_refs,
            "REFERENCES must match disk after reload"
        );
        // Sanity: foo + bar still in the store (they were not deleted).
        assert!(engine.store().contains(&foo.id));
        assert!(engine.store().contains(&bar.id));
    }

    #[test]
    fn update_entity_returns_commit_sha_title_modified_date_warnings_shape() {
        let tmp = TempDir::new().unwrap();
        let (mut engine, seeded) = engine_with_seed(&tmp, "Subject");
        let (actor, client) = cli_actor();

        let mut sections = IndexMap::new();
        sections.insert("identity".to_string(), "edited body".to_string());

        let outcome = engine
            .update_entity(
                UpdateEntityArgs {
                    id: seeded.id.clone(),
                    expected_hash: Some(seeded.content_hash.clone()),
                    sections,
                    append_sections: IndexMap::new(),
                    patch_sections: IndexMap::new(),
                    metadata: IndexMap::new(),
                    metadata_unset: Vec::new(),
                    declare_relations: Vec::new(),
                    dry_run: false,
                    relations_unset: Vec::new(),
                },
                actor,
                Some(&client),
                None,
            )
            .unwrap();

        // Folder backend produces a synthetic CommitId.
        assert!(
            !outcome.commit_sha.is_empty(),
            "commit_sha must be populated on a real update"
        );
        // Title echoed from the parsed entity post-write.
        assert_eq!(outcome.title, "Subject");
        // The default `spec` schema declares `modified_date` with
        // `auto_timestamp: true`; the unified update path
        // auto-stamps it. Asserting non-empty pins the
        // wire-shape parity with full's UpdateResult.modified_date.
        assert!(
            !outcome.modified_date.is_empty(),
            "modified_date must be auto-stamped on update for the default spec schema",
        );
        // V1: warnings always empty (typed warning surfaces are
        // separate session work). The vec is present on the outcome
        // so the wire shape parity with full's UpdateResult holds.
        assert!(outcome.warnings.is_empty());
        // Section was modified (existing behaviour, sanity check).
        assert_eq!(
            outcome.modified_sections.replaced,
            vec!["identity".to_string()]
        );
    }

    // ---- Engine::update_entity no-op detection ---------------------

    /// Re-setting a
    /// section to its current on-disk value short-circuits to
    /// `UPDATE_NOOP` and preserves `last_modified` at its pre-call
    /// value. Pre-fix the auto-timestamp stamped `last_modified` to
    /// `today_iso()` before the bytes-compare ran; the stamp
    /// synthesised a delta and the no-op never matched.
    #[test]
    fn update_entity_noop_resetting_section_to_current_value_preserves_last_modified() {
        let tmp = TempDir::new().unwrap();
        let (mut engine, seeded) = engine_with_seed(&tmp, "Section Resetter");
        let (actor, client) = cli_actor();

        // Read the pre-update `last_modified` so we can assert it
        // survives the no-op.
        let pre_last_modified = engine
            .get_entity(&seeded.id)
            .and_then(|e| e.metadata.get("last_modified"))
            .map(|v| v.to_frontmatter_string())
            .expect("seeded entity has last_modified");

        // Re-set `identity` to its current on-disk body. The seed
        // helper writes "fixture identity body" — passing the same
        // string back must be a no-op.
        let mut sections = IndexMap::new();
        sections.insert("identity".to_string(), "fixture identity body".to_string());
        let outcome = engine
            .update_entity(
                UpdateEntityArgs {
                    id: seeded.id.clone(),
                    expected_hash: Some(seeded.content_hash.clone()),
                    sections,
                    append_sections: IndexMap::new(),
                    patch_sections: IndexMap::new(),
                    metadata: IndexMap::new(),
                    metadata_unset: Vec::new(),
                    declare_relations: Vec::new(),
                    dry_run: false,
                    relations_unset: Vec::new(),
                },
                actor,
                Some(&client),
                None,
            )
            .unwrap();

        assert_eq!(outcome.commit_sha, "", "no-op must not commit");
        assert_eq!(
            outcome.content_hash, seeded.content_hash,
            "no-op must not advance content_hash",
        );
        assert!(
            outcome.warnings.iter().any(|w| w.code() == "UPDATE_NOOP"),
            "UPDATE_NOOP must fire on bytes-identical re-set",
        );
        assert_eq!(
            outcome.modified_date, pre_last_modified,
            "no-op must preserve last_modified at the pre-call value",
        );
        // The applied delta is empty on a
        // no-op — `modified_sections` must not claim `identity` was
        // replaced when nothing landed (matching the empty commit_sha
        // and unchanged hash above).
        assert!(
            outcome.modified_sections.replaced.is_empty()
                && outcome.modified_sections.appended.is_empty()
                && outcome.modified_sections.patched.is_empty(),
            "no-op must report an empty section delta, got {:?}",
            outcome.modified_sections,
        );

        // The on-disk entity also still carries the pre-update
        // last_modified — the no-op didn't bump it through some
        // other path.
        let post_last_modified = engine
            .get_entity(&seeded.id)
            .and_then(|e| e.metadata.get("last_modified"))
            .map(|v| v.to_frontmatter_string())
            .expect("entity still in store");
        assert_eq!(post_last_modified, pre_last_modified);
    }

    /// A payload with no
    /// recognised mutation content refuses with `EMPTY_UPDATE` BEFORE
    /// any engine work runs. Previously this same input short-
    /// circuited as a success-with-`UPDATE_NOOP`-warning, which
    /// hid a boundary-discipline failure mode (a
    /// misspelled mutation key deserialises to empty defaults and
    /// looks like a no-op success on the wire). The new refusal
    /// makes "no mutation content provided" structurally distinct
    /// from "mutation content provided but matched current state"
    /// (which still surfaces `UPDATE_NOOP` — see
    /// `update_entity_noop_same_content_surfaces_warning` below).
    #[test]
    fn update_entity_empty_payload_refuses_with_typed_code() {
        let tmp = TempDir::new().unwrap();
        let (mut engine, seeded) = engine_with_seed(&tmp, "Empty Payload");
        let (actor, client) = cli_actor();

        let err = engine
            .update_entity(
                UpdateEntityArgs {
                    id: seeded.id.clone(),
                    expected_hash: Some(seeded.content_hash.clone()),
                    sections: IndexMap::new(),
                    append_sections: IndexMap::new(),
                    patch_sections: IndexMap::new(),
                    metadata: IndexMap::new(),
                    metadata_unset: Vec::new(),
                    declare_relations: Vec::new(),
                    dry_run: false,
                    relations_unset: Vec::new(),
                },
                actor,
                Some(&client),
                None,
            )
            .unwrap_err();
        match err {
            EngineError::EmptyUpdate { id } => {
                assert_eq!(id, seeded.id.to_string());
            }
            other => panic!("expected EMPTY_UPDATE, got {other:?}"),
        }
        // No provenance row landed — the refusal preempts any write.
        let log_path = tmp.path().join(".memstead/changes.jsonl");
        if let Ok(log) = std::fs::read_to_string(&log_path) {
            let updates = log.matches("\"kind\":\"update\"").count();
            assert_eq!(updates, 0, "EMPTY_UPDATE refusal must not log an update");
        }
    }

    /// Complement: a
    /// payload with mutation content that matches the current entity
    /// state continues to land as success-with-`UPDATE_NOOP`-warning.
    /// The new `EMPTY_UPDATE` refusal applies only when no mutation
    /// content was provided; this path is structurally distinct.
    #[test]
    fn update_entity_noop_same_content_surfaces_warning() {
        let tmp = TempDir::new().unwrap();
        let (mut engine, seeded) = engine_with_seed(&tmp, "Same Content Noop");
        let (actor, client) = cli_actor();

        // `empty_create_args` seeds `identity` with this exact body.
        let mut sections = IndexMap::new();
        sections.insert("identity".to_string(), "fixture identity body".to_string());

        let outcome = engine
            .update_entity(
                UpdateEntityArgs {
                    id: seeded.id.clone(),
                    expected_hash: Some(seeded.content_hash.clone()),
                    sections,
                    append_sections: IndexMap::new(),
                    patch_sections: IndexMap::new(),
                    metadata: IndexMap::new(),
                    metadata_unset: Vec::new(),
                    declare_relations: Vec::new(),
                    dry_run: false,
                    relations_unset: Vec::new(),
                },
                actor,
                Some(&client),
                None,
            )
            .unwrap();

        assert_eq!(outcome.commit_sha, "");
        assert_eq!(outcome.content_hash, seeded.content_hash);
        let codes: Vec<&str> = outcome.warnings.iter().map(|w| w.code()).collect();
        assert!(
            codes.contains(&"UPDATE_NOOP"),
            "same-content update must surface UPDATE_NOOP; got {codes:?}",
        );
    }

    #[test]
    fn update_entity_noop_metadata_unset_on_absent_key() {
        // `metadata_unset=["never-set-key"]`
        // where the key was never set is a no-op — no field actually
        // changed, no commit advances, follow-up calls can chain
        // `expected_hash` without `HASH_MISMATCH`.
        let tmp = TempDir::new().unwrap();
        let (mut engine, seeded) = engine_with_seed(&tmp, "Absent Key Noop");
        let (actor, client) = cli_actor();

        let outcome = engine
            .update_entity(
                UpdateEntityArgs {
                    id: seeded.id.clone(),
                    expected_hash: Some(seeded.content_hash.clone()),
                    sections: IndexMap::new(),
                    append_sections: IndexMap::new(),
                    patch_sections: IndexMap::new(),
                    metadata: IndexMap::new(),
                    // `tags` is declared on the `spec` schema but
                    // unset on the seeded entity. Unsetting it should
                    // be a no-op rather than producing a fresh commit.
                    metadata_unset: vec!["tags".to_string()],
                    declare_relations: Vec::new(),
                    dry_run: false,
                    relations_unset: Vec::new(),
                },
                actor,
                Some(&client),
                None,
            )
            .unwrap();

        assert_eq!(outcome.commit_sha, "");
        assert_eq!(outcome.content_hash, seeded.content_hash);
        assert!(
            outcome.warnings.iter().any(|w| w.code() == "UPDATE_NOOP"),
            "absent-key metadata_unset must surface UPDATE_NOOP",
        );
        // Empty applied delta on the no-op —
        // `unset` must not claim `tags` was removed when nothing landed.
        assert!(
            outcome.modified_metadata.set.is_empty() && outcome.modified_metadata.unset.is_empty(),
            "no-op must report an empty metadata delta, got {:?}",
            outcome.modified_metadata,
        );

        // Follow-up real change against the unchanged hash succeeds —
        // no HASH_MISMATCH cascade from a phantom advance.
        let mut sections = IndexMap::new();
        sections.insert("identity".to_string(), "real change".to_string());
        let real = engine
            .update_entity(
                UpdateEntityArgs {
                    id: seeded.id.clone(),
                    expected_hash: Some(seeded.content_hash.clone()),
                    sections,
                    append_sections: IndexMap::new(),
                    patch_sections: IndexMap::new(),
                    metadata: IndexMap::new(),
                    metadata_unset: Vec::new(),
                    declare_relations: Vec::new(),
                    dry_run: false,
                    relations_unset: Vec::new(),
                },
                actor,
                Some(&client),
                None,
            )
            .unwrap();
        assert!(!real.commit_sha.is_empty());
        assert_ne!(real.content_hash, seeded.content_hash);
    }

    /// The exact MCP repro — re-setting a
    /// metadata key to its current value no-ops, and the response's
    /// `modified_metadata` reports the applied delta (empty), not the
    /// requested key. Pre-fix the no-op short-circuit echoed
    /// `set: ["level"]` while `commit_sha` was empty and the hash
    /// unchanged — a self-contradictory response.
    #[test]
    fn update_entity_noop_setting_metadata_to_current_value_reports_empty_delta() {
        let tmp = TempDir::new().unwrap();
        let (mut engine, seeded) = engine_with_seed(&tmp, "Stability Resetter");
        let (actor, client) = cli_actor();

        // `level` defaults to "M0" on the spec schema, so the seed
        // carries it. Re-setting it to "M0" changes nothing.
        let mut metadata = IndexMap::new();
        metadata.insert("level".to_string(), "M0".to_string());
        let outcome = engine
            .update_entity(
                UpdateEntityArgs {
                    id: seeded.id.clone(),
                    expected_hash: Some(seeded.content_hash.clone()),
                    sections: IndexMap::new(),
                    append_sections: IndexMap::new(),
                    patch_sections: IndexMap::new(),
                    metadata,
                    metadata_unset: Vec::new(),
                    declare_relations: Vec::new(),
                    dry_run: false,
                    relations_unset: Vec::new(),
                },
                actor,
                Some(&client),
                None,
            )
            .unwrap();

        assert_eq!(outcome.commit_sha, "", "no-op must not commit");
        assert_eq!(
            outcome.content_hash, seeded.content_hash,
            "no-op must not advance hash"
        );
        assert!(
            outcome.warnings.iter().any(|w| w.code() == "UPDATE_NOOP"),
            "re-set to current value must surface UPDATE_NOOP",
        );
        assert!(
            outcome.modified_metadata.set.is_empty() && outcome.modified_metadata.unset.is_empty(),
            "no-op must not claim `level` was set — applied delta is empty, got {:?}",
            outcome.modified_metadata,
        );
    }

    #[test]
    fn update_entity_noop_declare_already_related_edge() {
        // Re-declare an already-related edge
        // via `declare_relations` — no field, section, metadata or
        // relations list actually changes, so the bytes are identical
        // and the call no-ops.
        use crate::ops::RelateArg;
        let tmp = TempDir::new().unwrap();
        let mem_dir = tmp.path().to_path_buf();
        let writer = FilesystemMemWriter::new(mem_dir.clone());
        let mut engine = Engine::from_mounts(vec![(
            folder_mount("specs", mem_dir),
            Box::new(writer) as Box<dyn MemBackend>,
        )])
        .unwrap();
        let (actor, client) = cli_actor();
        let target = engine
            .create_entity(
                empty_create_args("specs", "Target Already Related"),
                actor,
                Some(&client),
                None,
            )
            .unwrap();
        let source = engine
            .create_entity(
                empty_create_args("specs", "Source Already Related"),
                actor,
                Some(&client),
                None,
            )
            .unwrap();
        let after_relate = engine
            .relate_entity(
                RelateEntityArgs {
                    source: source.id.clone(),
                    expected_hash: Some(source.content_hash.clone()),
                    rel_type: "USES".to_string(),
                    target: target.id.clone(),
                    remove: false,
                    description: None,
                },
                actor,
                Some(&client),
                None,
            )
            .unwrap();
        // Now re-declare the same edge via update.declare_relations.
        let outcome = engine
            .update_entity(
                UpdateEntityArgs {
                    relations_unset: Vec::new(),
                    id: source.id.clone(),
                    expected_hash: Some(after_relate.content_hash.clone()),
                    sections: IndexMap::new(),
                    append_sections: IndexMap::new(),
                    patch_sections: IndexMap::new(),
                    metadata: IndexMap::new(),
                    metadata_unset: Vec::new(),
                    declare_relations: vec![RelateArg {
                        rel_type: "USES".to_string(),
                        to: target.id.clone(),
                        description: None,
                    }],
                    dry_run: false,
                },
                actor,
                Some(&client),
                None,
            )
            .unwrap();

        assert_eq!(outcome.commit_sha, "");
        assert_eq!(outcome.content_hash, after_relate.content_hash);
        assert!(
            outcome.warnings.iter().any(|w| w.code() == "UPDATE_NOOP"),
            "duplicate declare must surface UPDATE_NOOP",
        );
        // `relations_declared` still records the entry — the per-
        // relation outcome is part of the surface, even for no-ops.
        assert_eq!(outcome.relations_declared.len(), 1);
        assert_eq!(outcome.relations_declared[0].rel_type, "USES");
        assert_eq!(outcome.relations_declared[0].target, target.id);
        assert!(!outcome.relations_declared[0].target_was_stubbed);
    }

    #[test]
    fn update_entity_real_change_still_commits_and_advances_hash() {
        // Regression: the no-op short-circuit must not short-circuit
        // real changes. A section replacement still produces a
        // non-empty `commit_sha`, advances `content_hash`, and does
        // NOT surface UPDATE_NOOP.
        let tmp = TempDir::new().unwrap();
        let (mut engine, seeded) = engine_with_seed(&tmp, "Real Change Subject");
        let (actor, client) = cli_actor();

        let mut sections = IndexMap::new();
        sections.insert("identity".to_string(), "definitely new body".to_string());

        let outcome = engine
            .update_entity(
                UpdateEntityArgs {
                    id: seeded.id.clone(),
                    expected_hash: Some(seeded.content_hash.clone()),
                    sections,
                    append_sections: IndexMap::new(),
                    patch_sections: IndexMap::new(),
                    metadata: IndexMap::new(),
                    metadata_unset: Vec::new(),
                    declare_relations: Vec::new(),
                    dry_run: false,
                    relations_unset: Vec::new(),
                },
                actor,
                Some(&client),
                None,
            )
            .unwrap();

        assert!(!outcome.commit_sha.is_empty(), "real change must commit");
        assert_ne!(
            outcome.content_hash, seeded.content_hash,
            "real change must advance content_hash",
        );
        assert!(
            !outcome.warnings.iter().any(|w| w.code() == "UPDATE_NOOP"),
            "real change must not surface UPDATE_NOOP",
        );
    }

    #[test]
    fn update_entity_noop_preserves_expected_hash_across_chain() {
        // A follow-up update with the original
        // hash after one or more no-ops succeeds because the hash
        // never advanced. Demonstrates the `expected_hash`-caching
        // posture the agent surface relies on.
        let tmp = TempDir::new().unwrap();
        let (mut engine, seeded) = engine_with_seed(&tmp, "Chained Noops Subject");
        let (actor, client) = cli_actor();

        // Two no-op calls in a row — both must return the same hash.
        // Pass same-content mutation
        // so UPDATE_NOOP fires (rather than EMPTY_UPDATE) and the
        // hash-chain invariant is exercised on the warning path.
        let mut noop_sections = IndexMap::new();
        noop_sections.insert("identity".to_string(), "fixture identity body".to_string());
        for _ in 0..2 {
            let outcome = engine
                .update_entity(
                    UpdateEntityArgs {
                        id: seeded.id.clone(),
                        expected_hash: Some(seeded.content_hash.clone()),
                        sections: noop_sections.clone(),
                        append_sections: IndexMap::new(),
                        patch_sections: IndexMap::new(),
                        metadata: IndexMap::new(),
                        metadata_unset: Vec::new(),
                        declare_relations: Vec::new(),
                        dry_run: false,
                        relations_unset: Vec::new(),
                    },
                    actor,
                    Some(&client),
                    None,
                )
                .unwrap();
            assert_eq!(outcome.commit_sha, "");
            assert_eq!(outcome.content_hash, seeded.content_hash);
        }

        // Real follow-up with the original hash still works — no
        // HASH_MISMATCH cascade because the hash never advanced.
        let mut sections = IndexMap::new();
        sections.insert(
            "identity".to_string(),
            "third call: real change".to_string(),
        );
        let real = engine
            .update_entity(
                UpdateEntityArgs {
                    id: seeded.id.clone(),
                    expected_hash: Some(seeded.content_hash.clone()),
                    sections,
                    append_sections: IndexMap::new(),
                    patch_sections: IndexMap::new(),
                    metadata: IndexMap::new(),
                    metadata_unset: Vec::new(),
                    declare_relations: Vec::new(),
                    dry_run: false,
                    relations_unset: Vec::new(),
                },
                actor,
                Some(&client),
                None,
            )
            .unwrap();
        assert!(!real.commit_sha.is_empty());
        assert_ne!(real.content_hash, seeded.content_hash);
    }

    // ---- Engine::delete_entity --------------------------------------

    // ---------------------------------------------------------------------
    // Alias-synthesis pass. Body wiki-links auto-emit
    // relations of the source schema's `alias_target_rel_type` pointer
    // and are garbage-collected when the body wiki-link disappears.
    // ---------------------------------------------------------------------

    #[test]
    fn synthesis_gc_drops_auto_emitted_reference_when_body_link_removed() {
        // Create with `[[target]]` in body → synthesis emits
        // REFERENCES. Update body to drop the wiki-link → GC drops
        // the auto-emitted REFERENCES.
        use crate::engine::{CreateEntityArgs, UpdateEntityArgs};
        use indexmap::IndexMap;
        use tempfile::TempDir;

        let tmp = TempDir::new().unwrap();
        let mem_dir = tmp.path().to_path_buf();
        let writer = FilesystemMemWriter::new(mem_dir.clone());
        let mut engine = Engine::from_mounts(vec![(
            folder_mount("specs", mem_dir.clone()),
            Box::new(writer) as Box<dyn MemBackend>,
        )])
        .unwrap();
        engine.set_workspace_root(mem_dir.clone());
        let (actor, client) = cli_actor();

        let target = engine
            .create_entity(
                empty_create_args("specs", "Target"),
                actor,
                Some(&client),
                None,
            )
            .unwrap();
        // Create source with the body wiki-link already present —
        // synthesis fires inside create.
        let mut sections: IndexMap<String, String> = IndexMap::new();
        sections.insert("identity".to_string(), "source identity".to_string());
        sections.insert(
            "purpose".to_string(),
            "see [[target]] for context".to_string(),
        );
        let source = engine
            .create_entity(
                CreateEntityArgs {
                    mem: "specs".to_string(),
                    title: "Source".to_string(),
                    entity_type: "spec".to_string(),
                    sections,
                    metadata: IndexMap::new(),
                    relations: Vec::new(),
                    dry_run: false,
                },
                actor,
                Some(&client),
                None,
            )
            .unwrap();
        assert!(
            engine
                .get_entity(&source.id)
                .unwrap()
                .relationships
                .iter()
                .any(|r| r.rel_type == "REFERENCES" && r.target == target.id),
            "create-time synthesis must emit REFERENCES → target",
        );

        // Update: drop the body wiki-link. GC should remove the
        // synthesised REFERENCES.
        let mut new_sections: IndexMap<String, String> = IndexMap::new();
        new_sections.insert("purpose".to_string(), "no link any more".to_string());
        engine
            .update_entity(
                UpdateEntityArgs {
                    id: source.id.clone(),
                    expected_hash: Some(source.content_hash.clone()),
                    sections: new_sections,
                    append_sections: IndexMap::new(),
                    patch_sections: IndexMap::new(),
                    metadata: IndexMap::new(),
                    metadata_unset: Vec::new(),
                    declare_relations: Vec::new(),
                    dry_run: false,
                    relations_unset: Vec::new(),
                },
                actor,
                Some(&client),
                None,
            )
            .expect("update must succeed; GC drops the now-orphan REFERENCES");
        let in_mem = engine.get_entity(&source.id).unwrap();
        assert!(
            !in_mem
                .relationships
                .iter()
                .any(|r| r.rel_type == "REFERENCES" && r.target == target.id),
            "GC must drop the auto-emitted REFERENCES after body link removal; got {:?}",
            in_mem.relationships,
        );
    }

    #[test]
    fn update_gc_removes_orphan_stub_when_last_body_link_dropped() {
        // Create source with `[[ghost]]` body link → alias synthesis
        // auto-stubs `ghost` and emits REFERENCES → ghost. The update
        // drops the link, so the REFERENCES edge (the stub's only
        // referrer) disappears; the orphan-stub GC sweep removes the
        // stub and surfaces it in `orphan_stubs_removed`. A reload from
        // disk shows the same (decremented) stub count — proving the GC
        // was a real store mutation, not a session-local view fix.
        use crate::engine::{CreateEntityArgs, UpdateEntityArgs};
        use indexmap::IndexMap;
        use tempfile::TempDir;

        let tmp = TempDir::new().unwrap();
        let mem_dir = tmp.path().to_path_buf();
        let writer = FilesystemMemWriter::new(mem_dir.clone());
        let mut engine = Engine::from_mounts(vec![(
            folder_mount("specs", mem_dir.clone()),
            Box::new(writer) as Box<dyn MemBackend>,
        )])
        .unwrap();
        engine.set_workspace_root(mem_dir.clone());
        let (actor, client) = cli_actor();

        let ghost = crate::EntityId::new("specs", "ghost");
        let mut sections: IndexMap<String, String> = IndexMap::new();
        sections.insert("identity".to_string(), "source identity".to_string());
        sections.insert(
            "purpose".to_string(),
            "see [[ghost]] for context".to_string(),
        );
        let source = engine
            .create_entity(
                CreateEntityArgs {
                    mem: "specs".to_string(),
                    title: "Source".to_string(),
                    entity_type: "spec".to_string(),
                    sections,
                    metadata: IndexMap::new(),
                    relations: Vec::new(),
                    dry_run: false,
                },
                actor,
                Some(&client),
                None,
            )
            .unwrap();
        assert!(
            engine.store().contains(&ghost) && engine.get_entity(&ghost).unwrap().stub,
            "body wiki-link to an absent target must auto-stub it",
        );
        assert_eq!(
            engine.health().stub_count,
            1,
            "one stub before the link drop"
        );

        let mut new_sections: IndexMap<String, String> = IndexMap::new();
        new_sections.insert("purpose".to_string(), "no link any more".to_string());
        let outcome = engine
            .update_entity(
                UpdateEntityArgs {
                    id: source.id.clone(),
                    expected_hash: Some(source.content_hash.clone()),
                    sections: new_sections,
                    append_sections: IndexMap::new(),
                    patch_sections: IndexMap::new(),
                    metadata: IndexMap::new(),
                    metadata_unset: Vec::new(),
                    declare_relations: Vec::new(),
                    dry_run: false,
                    relations_unset: Vec::new(),
                },
                actor,
                Some(&client),
                None,
            )
            .expect("update must succeed and GC the now-orphan stub");

        assert_eq!(
            outcome.orphan_stubs_removed,
            vec![ghost.clone()],
            "the update that dropped the last body link must report the GC'd stub",
        );
        assert!(
            !engine.store().contains(&ghost),
            "orphan stub must be gone from the in-memory store",
        );
        assert_eq!(
            engine.health().stub_count,
            0,
            "stub count decremented in-session"
        );

        // Reload from disk: the source's on-disk markdown no longer
        // carries the link, so the parser re-emits no stub. The
        // decremented count holds across the reload — the GC was real.
        engine.reload_each_writable_mem().unwrap();
        assert!(
            !engine.store().contains(&ghost),
            "stub stays gone after reload-from-disk",
        );
        assert_eq!(
            engine.health().stub_count,
            0,
            "reloaded-from-disk store carries the same stub count as the in-session post-update state",
        );
    }

    #[test]
    fn update_gc_noop_when_section_edit_changes_no_body_link() {
        // An update that edits one section while leaving the `[[ghost]]`
        // link standing in another orphans nothing: `orphan_stubs_removed`
        // is present and empty (stable shape, no spurious GC), and the
        // stub survives because its referrer survives.
        use crate::engine::{CreateEntityArgs, UpdateEntityArgs};
        use indexmap::IndexMap;
        use tempfile::TempDir;

        let tmp = TempDir::new().unwrap();
        let mem_dir = tmp.path().to_path_buf();
        let writer = FilesystemMemWriter::new(mem_dir.clone());
        let mut engine = Engine::from_mounts(vec![(
            folder_mount("specs", mem_dir.clone()),
            Box::new(writer) as Box<dyn MemBackend>,
        )])
        .unwrap();
        engine.set_workspace_root(mem_dir.clone());
        let (actor, client) = cli_actor();

        let ghost = crate::EntityId::new("specs", "ghost");
        let mut sections: IndexMap<String, String> = IndexMap::new();
        sections.insert("identity".to_string(), "original identity".to_string());
        sections.insert(
            "purpose".to_string(),
            "see [[ghost]] for context".to_string(),
        );
        let source = engine
            .create_entity(
                CreateEntityArgs {
                    mem: "specs".to_string(),
                    title: "Source".to_string(),
                    entity_type: "spec".to_string(),
                    sections,
                    metadata: IndexMap::new(),
                    relations: Vec::new(),
                    dry_run: false,
                },
                actor,
                Some(&client),
                None,
            )
            .unwrap();
        assert!(engine.store().contains(&ghost), "ghost stub materialised");

        // Replace `identity` only; the `[[ghost]]` link in `purpose`
        // stays, so no edge drops.
        let mut edit: IndexMap<String, String> = IndexMap::new();
        edit.insert("identity".to_string(), "edited identity".to_string());
        let outcome = engine
            .update_entity(
                UpdateEntityArgs {
                    id: source.id.clone(),
                    expected_hash: Some(source.content_hash.clone()),
                    sections: edit,
                    append_sections: IndexMap::new(),
                    patch_sections: IndexMap::new(),
                    metadata: IndexMap::new(),
                    metadata_unset: Vec::new(),
                    declare_relations: Vec::new(),
                    dry_run: false,
                    relations_unset: Vec::new(),
                },
                actor,
                Some(&client),
                None,
            )
            .expect("update must succeed");
        assert!(
            outcome.orphan_stubs_removed.is_empty(),
            "an edit that keeps every body wiki-link orphans nothing; got {:?}",
            outcome.orphan_stubs_removed,
        );
        assert!(
            engine.store().contains(&ghost),
            "the still-referenced stub survives the unrelated section edit",
        );
    }

    #[test]
    fn update_gc_preserves_stub_with_surviving_referrer() {
        // Two sources both body-link `[[ghost]]`. Dropping the link from
        // one leaves `ghost` referenced by the other — set-membership
        // semantics keep the stub alive and `orphan_stubs_removed` empty.
        use crate::engine::{CreateEntityArgs, UpdateEntityArgs};
        use indexmap::IndexMap;
        use tempfile::TempDir;

        let tmp = TempDir::new().unwrap();
        let mem_dir = tmp.path().to_path_buf();
        let writer = FilesystemMemWriter::new(mem_dir.clone());
        let mut engine = Engine::from_mounts(vec![(
            folder_mount("specs", mem_dir.clone()),
            Box::new(writer) as Box<dyn MemBackend>,
        )])
        .unwrap();
        engine.set_workspace_root(mem_dir.clone());
        let (actor, client) = cli_actor();

        let ghost = crate::EntityId::new("specs", "ghost");
        let make_with_link = |title: &str| {
            let mut sections: IndexMap<String, String> = IndexMap::new();
            sections.insert("identity".to_string(), format!("{title} identity"));
            sections.insert("purpose".to_string(), "see [[ghost]]".to_string());
            CreateEntityArgs {
                mem: "specs".to_string(),
                title: title.to_string(),
                entity_type: "spec".to_string(),
                sections,
                metadata: IndexMap::new(),
                relations: Vec::new(),
                dry_run: false,
            }
        };
        let source_a = engine
            .create_entity(make_with_link("Source A"), actor, Some(&client), None)
            .unwrap();
        engine
            .create_entity(make_with_link("Source B"), actor, Some(&client), None)
            .unwrap();
        assert!(engine.store().contains(&ghost), "ghost stub materialised");

        // Drop the link from source A only.
        let mut drop_link: IndexMap<String, String> = IndexMap::new();
        drop_link.insert("purpose".to_string(), "no link here".to_string());
        let outcome = engine
            .update_entity(
                UpdateEntityArgs {
                    id: source_a.id.clone(),
                    expected_hash: Some(source_a.content_hash.clone()),
                    sections: drop_link,
                    append_sections: IndexMap::new(),
                    patch_sections: IndexMap::new(),
                    metadata: IndexMap::new(),
                    metadata_unset: Vec::new(),
                    declare_relations: Vec::new(),
                    dry_run: false,
                    relations_unset: Vec::new(),
                },
                actor,
                Some(&client),
                None,
            )
            .expect("update must succeed");
        assert!(
            outcome.orphan_stubs_removed.is_empty(),
            "the stub keeps a referrer (source B), so nothing is GC'd; got {:?}",
            outcome.orphan_stubs_removed,
        );
        assert!(
            engine.store().contains(&ghost),
            "stub survives via the surviving referrer",
        );
    }

    #[test]
    fn synthesis_gc_preserves_non_pointer_explicit_relation_across_body_update() {
        // Explicit USES to target (USES is not the schema's
        // alias_target_rel_type pointer). A subsequent body-changing
        // update must NOT drop the USES edge — GC only touches
        // relations of the pointer rel-type. Under Option C, REFERENCES
        // can't be authored explicitly (`manual_authoring: forbidden`),
        // so the analogous "explicit REFERENCES preserved" scenario is
        // structurally impossible; USES exercises the same invariant
        // from the rel-type-discrimination side.
        use crate::engine::{RelateEntityArgs, UpdateEntityArgs};
        use indexmap::IndexMap;
        use tempfile::TempDir;

        let tmp = TempDir::new().unwrap();
        let mem_dir = tmp.path().to_path_buf();
        let writer = FilesystemMemWriter::new(mem_dir.clone());
        let mut engine = Engine::from_mounts(vec![(
            folder_mount("specs", mem_dir.clone()),
            Box::new(writer) as Box<dyn MemBackend>,
        )])
        .unwrap();
        engine.set_workspace_root(mem_dir.clone());
        let (actor, client) = cli_actor();

        let target = engine
            .create_entity(
                empty_create_args("specs", "Target"),
                actor,
                Some(&client),
                None,
            )
            .unwrap();
        let source = engine
            .create_entity(
                empty_create_args("specs", "Source"),
                actor,
                Some(&client),
                None,
            )
            .unwrap();

        // Explicit relate — no body wiki-link.
        let relate = engine
            .relate_entity(
                RelateEntityArgs {
                    source: source.id.clone(),
                    expected_hash: Some(source.content_hash.clone()),
                    rel_type: "USES".to_string(),
                    target: target.id.clone(),
                    remove: false,
                    description: None,
                },
                actor,
                Some(&client),
                None,
            )
            .unwrap();

        // Update an unrelated section. The explicit USES must survive
        // — it's not the alias_target_rel_type, GC ignores it.
        let mut sections: IndexMap<String, String> = IndexMap::new();
        sections.insert("purpose".to_string(), "unrelated edit".to_string());
        engine
            .update_entity(
                UpdateEntityArgs {
                    id: source.id.clone(),
                    expected_hash: Some(relate.content_hash.clone()),
                    sections,
                    append_sections: IndexMap::new(),
                    patch_sections: IndexMap::new(),
                    metadata: IndexMap::new(),
                    metadata_unset: Vec::new(),
                    declare_relations: Vec::new(),
                    dry_run: false,
                    relations_unset: Vec::new(),
                },
                actor,
                Some(&client),
                None,
            )
            .expect("update must succeed");
        let in_mem = engine.get_entity(&source.id).unwrap();
        assert!(
            in_mem
                .relationships
                .iter()
                .any(|r| r.rel_type == "USES" && r.target == target.id),
            "explicit USES must survive an unrelated body update; got {:?}",
            in_mem.relationships,
        );
    }

    #[test]
    fn synthesis_dedupes_repeated_body_links_to_same_target() {
        // Two `[[target]]` wiki-links in one body — synthesis must
        // not double-add. Result: exactly one REFERENCES.
        use crate::engine::UpdateEntityArgs;
        use indexmap::IndexMap;
        use tempfile::TempDir;

        let tmp = TempDir::new().unwrap();
        let mem_dir = tmp.path().to_path_buf();
        let writer = FilesystemMemWriter::new(mem_dir.clone());
        let mut engine = Engine::from_mounts(vec![(
            folder_mount("specs", mem_dir.clone()),
            Box::new(writer) as Box<dyn MemBackend>,
        )])
        .unwrap();
        engine.set_workspace_root(mem_dir.clone());
        let (actor, client) = cli_actor();

        let target = engine
            .create_entity(
                empty_create_args("specs", "Target"),
                actor,
                Some(&client),
                None,
            )
            .unwrap();
        let source = engine
            .create_entity(
                empty_create_args("specs", "Source"),
                actor,
                Some(&client),
                None,
            )
            .unwrap();

        let mut sections: IndexMap<String, String> = IndexMap::new();
        sections.insert(
            "purpose".to_string(),
            "see [[target]] and again [[target]]".to_string(),
        );
        engine
            .update_entity(
                UpdateEntityArgs {
                    id: source.id.clone(),
                    expected_hash: Some(source.content_hash.clone()),
                    sections,
                    append_sections: IndexMap::new(),
                    patch_sections: IndexMap::new(),
                    metadata: IndexMap::new(),
                    metadata_unset: Vec::new(),
                    declare_relations: Vec::new(),
                    dry_run: false,
                    relations_unset: Vec::new(),
                },
                actor,
                Some(&client),
                None,
            )
            .unwrap();
        let in_mem = engine.get_entity(&source.id).unwrap();
        let count = in_mem
            .relationships
            .iter()
            .filter(|r| r.rel_type == "REFERENCES" && r.target == target.id)
            .count();
        assert_eq!(
            count, 1,
            "dedupe must leave exactly one REFERENCES → target; got {:?}",
            in_mem.relationships,
        );
    }

    #[test]
    fn synthesis_coexists_with_explicit_uses_to_same_target() {
        // Explicit `USES` to target AND body wiki-link to target →
        // entity carries both USES and REFERENCES edges; synthesis
        // dedupes on `(rel_type, target)` so USES never suppresses
        // REFERENCES.
        use crate::engine::{RelateEntityArgs, UpdateEntityArgs};
        use indexmap::IndexMap;
        use tempfile::TempDir;

        let tmp = TempDir::new().unwrap();
        let mem_dir = tmp.path().to_path_buf();
        let writer = FilesystemMemWriter::new(mem_dir.clone());
        let mut engine = Engine::from_mounts(vec![(
            folder_mount("specs", mem_dir.clone()),
            Box::new(writer) as Box<dyn MemBackend>,
        )])
        .unwrap();
        engine.set_workspace_root(mem_dir.clone());
        let (actor, client) = cli_actor();

        let target = engine
            .create_entity(
                empty_create_args("specs", "Target"),
                actor,
                Some(&client),
                None,
            )
            .unwrap();
        let source = engine
            .create_entity(
                empty_create_args("specs", "Source"),
                actor,
                Some(&client),
                None,
            )
            .unwrap();
        // Explicit USES.
        let relate = engine
            .relate_entity(
                RelateEntityArgs {
                    source: source.id.clone(),
                    expected_hash: Some(source.content_hash.clone()),
                    rel_type: "USES".to_string(),
                    target: target.id.clone(),
                    remove: false,
                    description: None,
                },
                actor,
                Some(&client),
                None,
            )
            .unwrap();
        // Body wiki-link to the same target — synthesis emits REFERENCES.
        let mut sections: IndexMap<String, String> = IndexMap::new();
        sections.insert(
            "purpose".to_string(),
            "we also reference [[target]]".to_string(),
        );
        engine
            .update_entity(
                UpdateEntityArgs {
                    id: source.id.clone(),
                    expected_hash: Some(relate.content_hash.clone()),
                    sections,
                    append_sections: IndexMap::new(),
                    patch_sections: IndexMap::new(),
                    metadata: IndexMap::new(),
                    metadata_unset: Vec::new(),
                    declare_relations: Vec::new(),
                    dry_run: false,
                    relations_unset: Vec::new(),
                },
                actor,
                Some(&client),
                None,
            )
            .unwrap();
        let in_mem = engine.get_entity(&source.id).unwrap();
        assert!(
            in_mem
                .relationships
                .iter()
                .any(|r| r.rel_type == "USES" && r.target == target.id),
            "USES must survive — synthesis dedupes on (rel_type, target)",
        );
        assert!(
            in_mem
                .relationships
                .iter()
                .any(|r| r.rel_type == "REFERENCES" && r.target == target.id),
            "REFERENCES must be synthesised even though USES already targets the same entity",
        );
    }

    // ---------------------------------------------------------------------
    // Alias-synthesis integration tests against custom-schema engines.
    // The default schema pins `alias_target_rel_type: REFERENCES`; these
    // tests verify the engine doesn't hardcode that name by mounting
    // schemas with a non-REFERENCES pointer (proves name-agnosticism)
    // and schemas with no pointer at all (proves the strict
    // `WIKILINK_WITHOUT_RELATION` refusal still fires for opt-out
    // schemas). Both build the engine via `from_mounts_with_schemas_dir`
    // — the production path for workspace-authored schemas.
    // ---------------------------------------------------------------------

    mod alias_synthesis_custom_schema {
        use std::path::Path;

        use indexmap::IndexMap;
        use memstead_schema::SchemaRef;
        use tempfile::TempDir;

        use crate::backend::MemBackend;
        use crate::engine::test_helpers::*;
        use crate::engine::{CreateEntityArgs, Engine, EngineError, UpdateEntityArgs};
        use crate::storage::FilesystemMemWriter;
        use crate::workspace::{Mount, MountCapability, MountLifecycle, MountStorage};

        const TYPE_BODY: &str = r#"description: t
when_to_use: tests
sections:
  - key: body
    heading: Body
    required: true
    search_weight: 10.0
    catch_all: true
    write_rules: []
metadata_fields: []
title_weight: 100.0
text_fields:
  - body
hierarchy_relationship: _default
propagating_relationships: []
updatable_fields:
  - title
  - body
health_required_fields:
  - body
staleness_threshold_days: 90
write_rules: []
"#;

        fn write_schema_files(root: &Path, name: &str, manifest: &str, types: &[(&str, &str)]) {
            let dir = root.join(name);
            std::fs::create_dir_all(dir.join("types")).unwrap();
            std::fs::write(dir.join("schema.yaml"), manifest).unwrap();
            for (type_name, body) in types {
                std::fs::write(dir.join("types").join(format!("{type_name}.yaml")), body).unwrap();
            }
        }

        fn make_type_yaml(name: &str) -> String {
            format!("name: {name}\n{TYPE_BODY}")
        }

        fn folder_mount_with_pin(mem: &str, path: std::path::PathBuf, pin: SchemaRef) -> Mount {
            Mount {
                mem: mem.to_string(),
                schema: Some(pin),
                storage: MountStorage::Folder { path },
                capability: MountCapability::Write,
                lifecycle: MountLifecycle::Eager,
                cross_linkable: true,
                migration_target: None,
            }
        }

        fn engine_with_schema(
            manifest: &str,
            type_yaml_name: &str,
            schema_name: &str,
            schema_version: semver::Version,
        ) -> (Engine, TempDir) {
            let tmp = TempDir::new().unwrap();
            let schemas_dir = tmp.path().join("schemas");
            std::fs::create_dir_all(&schemas_dir).unwrap();
            write_schema_files(
                &schemas_dir,
                schema_name,
                manifest,
                &[(type_yaml_name, &make_type_yaml(type_yaml_name))],
            );
            let mem_dir = tmp.path().join("mem");
            std::fs::create_dir_all(&mem_dir).unwrap();
            let writer = FilesystemMemWriter::new(mem_dir.clone());
            let pin = SchemaRef::new(schema_name, schema_version);
            let mount = folder_mount_with_pin("v", mem_dir, pin);
            let mut engine = Engine::from_mounts_with_schemas_dir(
                vec![(mount, Box::new(writer) as Box<dyn MemBackend>)],
                Some(&schemas_dir),
            )
            .expect("engine with custom schema constructs");
            engine.set_workspace_root(tmp.path().to_path_buf());
            (engine, tmp)
        }

        #[test]
        fn non_references_alias_pointer_emits_named_rel_type_from_body_link() {
            // Schema names CITES as the alias pointer — the engine
            // must emit CITES (not REFERENCES) from a body wiki-link.
            // Proves no hard-coded "REFERENCES" string anywhere in
            // the synthesis path.
            let manifest = r#"name: aliased
version: 0.1.0
description: alias-synthesis fixture using a non-REFERENCES pointer
when_to_use: tests prove the engine does not hard-code REFERENCES
types:
  - doc
relationships:
  mode: strict
  definitions:
    - name: CITES
      description: Citation — auto-emitted from body wiki-links
      default_weight: 0.5
    - name: PART_OF
      description: Hierarchy
      default_weight: 3.0
      acyclic: true
    - name: _default
      description: Fallback
      default_weight: 1.0
alias_target_rel_type: CITES
community:
  resolution: 1.0
  seed: 42
"#;
            let (mut engine, _tmp) =
                engine_with_schema(manifest, "doc", "aliased", semver::Version::new(0, 1, 0));
            let (actor, client) = cli_actor();

            let target = engine
                .create_entity(
                    CreateEntityArgs {
                        mem: "v".to_string(),
                        title: "Target".to_string(),
                        entity_type: "doc".to_string(),
                        sections: IndexMap::from_iter([(
                            "body".to_string(),
                            "target body".to_string(),
                        )]),
                        metadata: IndexMap::new(),
                        relations: Vec::new(),
                        dry_run: false,
                    },
                    actor,
                    Some(&client),
                    None,
                )
                .unwrap();

            let mut sections: IndexMap<String, String> = IndexMap::new();
            sections.insert("body".to_string(), "see [[target]]".to_string());
            let source = engine
                .create_entity(
                    CreateEntityArgs {
                        mem: "v".to_string(),
                        title: "Source".to_string(),
                        entity_type: "doc".to_string(),
                        sections,
                        metadata: IndexMap::new(),
                        relations: Vec::new(),
                        dry_run: false,
                    },
                    actor,
                    Some(&client),
                    None,
                )
                .expect("create must succeed; CITES is auto-emitted by synthesis");

            let in_mem = engine.get_entity(&source.id).unwrap();
            assert!(
                in_mem
                    .relationships
                    .iter()
                    .any(|r| r.rel_type == "CITES" && r.target == target.id),
                "synthesis must emit CITES (the pointer rel-type), not REFERENCES; got {:?}",
                in_mem.relationships,
            );
            assert!(
                !in_mem
                    .relationships
                    .iter()
                    .any(|r| r.rel_type == "REFERENCES"),
                "engine must not hard-code REFERENCES — pointer rel-type is CITES; got {:?}",
                in_mem.relationships,
            );
        }

        #[test]
        fn no_pointer_schema_refuses_unbacked_body_wiki_link() {
            // Schema declares no `alias_target_rel_type`. Body wiki-link
            // without a backing relation must refuse with
            // `WIKILINK_WITHOUT_RELATION` — the strict validator's
            // pre-Option-C semantics, preserved for opt-out schemas.
            let manifest = r#"name: no-alias
version: 0.1.0
description: schema without alias_target_rel_type pointer
when_to_use: tests prove strict validator still fires for opt-out schemas
types:
  - doc
relationships:
  mode: strict
  definitions:
    - name: USES
      description: Use
      default_weight: 1.0
    - name: PART_OF
      description: Hierarchy
      default_weight: 3.0
      acyclic: true
    - name: _default
      description: Fallback
      default_weight: 1.0
community:
  resolution: 1.0
  seed: 42
"#;
            let (mut engine, _tmp) =
                engine_with_schema(manifest, "doc", "no-alias", semver::Version::new(0, 1, 0));
            let (actor, client) = cli_actor();

            let target = engine
                .create_entity(
                    CreateEntityArgs {
                        mem: "v".to_string(),
                        title: "Target".to_string(),
                        entity_type: "doc".to_string(),
                        sections: IndexMap::from_iter([(
                            "body".to_string(),
                            "target body".to_string(),
                        )]),
                        metadata: IndexMap::new(),
                        relations: Vec::new(),
                        dry_run: false,
                    },
                    actor,
                    Some(&client),
                    None,
                )
                .unwrap();
            let source = engine
                .create_entity(
                    CreateEntityArgs {
                        mem: "v".to_string(),
                        title: "Source".to_string(),
                        entity_type: "doc".to_string(),
                        sections: IndexMap::from_iter([(
                            "body".to_string(),
                            "source body".to_string(),
                        )]),
                        metadata: IndexMap::new(),
                        relations: Vec::new(),
                        dry_run: false,
                    },
                    actor,
                    Some(&client),
                    None,
                )
                .unwrap();

            // Add a body wiki-link with no backing relation. The
            // synthesis pass is a no-op (no pointer), so the
            // validator surfaces `WIKILINK_WITHOUT_RELATION`.
            let mut sections: IndexMap<String, String> = IndexMap::new();
            sections.insert("body".to_string(), "see [[target]]".to_string());
            let err = engine
                .update_entity(
                    UpdateEntityArgs {
                        id: source.id.clone(),
                        expected_hash: Some(source.content_hash.clone()),
                        sections,
                        append_sections: IndexMap::new(),
                        patch_sections: IndexMap::new(),
                        metadata: IndexMap::new(),
                        metadata_unset: Vec::new(),
                        declare_relations: Vec::new(),
                        dry_run: false,
                        relations_unset: Vec::new(),
                    },
                    actor,
                    Some(&client),
                    None,
                )
                .unwrap_err();
            match err {
                EngineError::WikiLinkWithoutRelation { from_id, missing } => {
                    assert_eq!(from_id, source.id.to_string());
                    assert_eq!(missing.len(), 1);
                    assert_eq!(missing[0].section_key, "body");
                    assert_eq!(missing[0].target_id, target.id.to_string());
                }
                other => panic!(
                    "no-pointer schema must refuse with WikiLinkWithoutRelation; got {other:?}"
                ),
            }
        }

        /// Restoring the
        /// pre-alias-synthesis invariant that every body wiki-link
        /// target carries a grammar-valid `EntityId`. Natural-form
        /// `[[Knowledge Graph]]` no longer slips through into a
        /// malformed auto-stub; the engine refuses with the typed
        /// `InvalidWikiLinkTarget` envelope and the
        /// `title_to_slug`-derived suggestion the agent lifts
        /// directly into a retry. Covers F1 of the 2026-05-18 CLI probe.
        #[test]
        fn natural_form_body_wiki_link_refuses_with_typed_envelope() {
            let manifest = r#"name: aliased
version: 0.1.0
description: alias-synthesis fixture
when_to_use: tests prove strict wiki-link grammar at mutation entry
types:
  - doc
relationships:
  mode: strict
  definitions:
    - name: REFERENCES
      description: Reference — auto-emitted from body wiki-links
      default_weight: 0.5
    - name: PART_OF
      description: Hierarchy
      default_weight: 3.0
      acyclic: true
    - name: _default
      description: Fallback
      default_weight: 1.0
alias_target_rel_type: REFERENCES
community:
  resolution: 1.0
  seed: 42
"#;
            let (mut engine, _tmp) =
                engine_with_schema(manifest, "doc", "aliased", semver::Version::new(0, 1, 0));
            let (actor, client) = cli_actor();

            let mut sections: IndexMap<String, String> = IndexMap::new();
            sections.insert("body".to_string(), "see [[Knowledge Graph]]".to_string());
            let err = engine
                .create_entity(
                    CreateEntityArgs {
                        mem: "v".to_string(),
                        title: "Source".to_string(),
                        entity_type: "doc".to_string(),
                        sections,
                        metadata: IndexMap::new(),
                        relations: Vec::new(),
                        dry_run: false,
                    },
                    actor,
                    Some(&client),
                    None,
                )
                .unwrap_err();
            match err {
                EngineError::InvalidWikiLinkTarget {
                    raw,
                    suggested,
                    section,
                    link_source,
                    ..
                } => {
                    assert_eq!(raw, "Knowledge Graph");
                    assert_eq!(suggested.as_deref(), Some("knowledge-graph"));
                    assert_eq!(section, "body");
                    assert_eq!(link_source, "body_link");
                }
                other => panic!(
                    "natural-form body wiki-link must refuse with InvalidWikiLinkTarget; got {other:?}"
                ),
            }
        }

        /// Tier-2 body wiki-link with a non-conformant
        /// mem prefix refuses with the distinct `InvalidMemName`
        /// (wire code `INVALID_MEM_NAME`) — mems are fixed
        /// identifiers, not free-form text the agent can slugify, so
        /// the recovery path is different from `InvalidWikiLinkTarget`.
        #[test]
        fn tier_two_bad_mem_prefix_refuses_with_distinct_envelope() {
            let manifest = r#"name: aliased
version: 0.1.0
description: alias-synthesis fixture
when_to_use: tests prove strict mem-prefix grammar at mutation entry
types:
  - doc
relationships:
  mode: strict
  definitions:
    - name: REFERENCES
      description: Reference
      default_weight: 0.5
    - name: PART_OF
      description: Hierarchy
      default_weight: 3.0
      acyclic: true
    - name: _default
      description: Fallback
      default_weight: 1.0
alias_target_rel_type: REFERENCES
community:
  resolution: 1.0
  seed: 42
"#;
            let (mut engine, _tmp) =
                engine_with_schema(manifest, "doc", "aliased", semver::Version::new(0, 1, 0));
            let (actor, client) = cli_actor();

            let mut sections: IndexMap<String, String> = IndexMap::new();
            sections.insert("body".to_string(), "see [[Other Mem:foo]]".to_string());
            let err = engine
                .create_entity(
                    CreateEntityArgs {
                        mem: "v".to_string(),
                        title: "Source".to_string(),
                        entity_type: "doc".to_string(),
                        sections,
                        metadata: IndexMap::new(),
                        relations: Vec::new(),
                        dry_run: false,
                    },
                    actor,
                    Some(&client),
                    None,
                )
                .unwrap_err();
            match err {
                EngineError::InvalidWikiLinkMem { raw, section, .. } => {
                    assert_eq!(raw, "Other Mem");
                    assert_eq!(section, "body");
                }
                other => panic!(
                    "Tier-2 bad mem prefix must refuse with InvalidWikiLinkMem; got {other:?}"
                ),
            }
        }

        /// Body wiki-link
        /// containing the ambiguous `[[<segments>/<segments>--<slug>]]`
        /// form refuses with `InvalidWikiLinkTarget` carrying the
        /// colon-form (`<prefix>:<slug>`) as `suggested`. Pre-fix the
        /// dash form silently produced a same-mem phantom stub at
        /// slug `team/sub-mem--target`, losing the agent's intent.
        #[test]
        fn hierarchical_dash_form_body_link_refuses_with_colon_suggestion() {
            let manifest = r#"name: aliased
version: 0.1.0
description: alias-synthesis fixture
when_to_use: tests prove hierarchical dash-form refusal at mutation entry
types:
  - doc
relationships:
  mode: strict
  definitions:
    - name: REFERENCES
      description: Reference — auto-emitted from body wiki-links
      default_weight: 0.5
    - name: PART_OF
      description: Hierarchy
      default_weight: 3.0
      acyclic: true
    - name: _default
      description: Fallback
      default_weight: 1.0
alias_target_rel_type: REFERENCES
community:
  resolution: 1.0
  seed: 42
"#;
            let (mut engine, _tmp) =
                engine_with_schema(manifest, "doc", "aliased", semver::Version::new(0, 1, 0));
            let (actor, client) = cli_actor();

            let mut sections: IndexMap<String, String> = IndexMap::new();
            sections.insert(
                "body".to_string(),
                "see [[team/sub-mem--target]]".to_string(),
            );
            let err = engine
                .create_entity(
                    CreateEntityArgs {
                        mem: "v".to_string(),
                        title: "Source".to_string(),
                        entity_type: "doc".to_string(),
                        sections,
                        metadata: IndexMap::new(),
                        relations: Vec::new(),
                        dry_run: false,
                    },
                    actor,
                    Some(&client),
                    None,
                )
                .unwrap_err();
            match err {
                EngineError::InvalidWikiLinkTarget {
                    raw,
                    suggested,
                    section,
                    link_source,
                    ..
                } => {
                    assert_eq!(raw, "team/sub-mem--target");
                    assert_eq!(suggested.as_deref(), Some("team/sub-mem:target"));
                    assert_eq!(section, "body");
                    assert_eq!(link_source, "body_link");
                }
                other => panic!(
                    "hierarchical dash-form body link must refuse with InvalidWikiLinkTarget; got {other:?}"
                ),
            }

            // The entity did not land — no phantom stub for the source,
            // no phantom stub for the would-be target.
            let listed = engine.store().all_entities().collect::<Vec<_>>();
            assert!(
                listed.is_empty(),
                "refused create must not leave any entity behind, got: {listed:?}"
            );
        }
    }

    // ---- relations_unset repair-power gating -------------------------

    /// Markdown for a deliberately non-conformant `spec`: carries an
    /// undeclared metadata field (`zzz_bogus_field`) plus one USES
    /// relation. Written straight to disk before engine construction —
    /// the write path refuses non-conformant entities, so out-of-band
    /// state is the only way to seed one (which is exactly the
    /// repair-power scenario: drift entered outside the engine).
    const DRIFTED_MD: &str = "---\ntype: spec\ncreated_date: 2026-01-15\nlast_modified: 2026-04-12\nzzz_bogus_field: x\n---\n# Drifted\n\n## Identity\n\nNon-conformant fixture.\n\n## Purpose\n\nRepair-gate tests.\n\n## Relationships\n\n- **USES**: [[anchor]]\n";

    fn repair_engine() -> (TempDir, Engine) {
        let tmp = TempDir::new().unwrap();
        let mem_dir = tmp.path().to_path_buf();
        std::fs::write(
            mem_dir.join("anchor.md"),
            "---\ntype: spec\ncreated_date: 2026-01-15\nlast_modified: 2026-04-12\n---\n# Anchor\n\n## Identity\n\nTarget.\n\n## Purpose\n\nRelation target.\n",
        )
        .unwrap();
        std::fs::write(mem_dir.join("drifted.md"), DRIFTED_MD).unwrap();
        let writer = FilesystemMemWriter::new(mem_dir.clone());
        let engine = Engine::from_mounts(vec![(
            folder_mount("specs", mem_dir),
            Box::new(writer) as Box<dyn MemBackend>,
        )])
        .unwrap();
        (tmp, engine)
    }

    fn repair_args(id: EntityId, hash: Option<String>) -> UpdateEntityArgs {
        UpdateEntityArgs {
            id,
            expected_hash: hash,
            sections: IndexMap::new(),
            append_sections: IndexMap::new(),
            patch_sections: IndexMap::new(),
            metadata: IndexMap::new(),
            metadata_unset: Vec::new(),
            declare_relations: Vec::new(),
            dry_run: false,
            relations_unset: vec![crate::ops::RelationUnsetArg {
                rel_type: "USES".to_string(),
                target: EntityId::new("specs", "anchor"),
            }],
        }
    }

    /// A conformant entity refuses repair-shaped input with
    /// `REPAIR_NOT_NEEDED` and is not modified — even though it has a
    /// relation that `relations_unset` names. The recovery text points
    /// at the focused detach path.
    #[test]
    fn relations_unset_on_conformant_entity_refuses_repair_not_needed() {
        let (_tmp, mut engine) = repair_engine();
        // `anchor` is conformant. Give it a relation first via the
        // ordinary relate path so there is something to (not) remove.
        let anchor = EntityId::new("specs", "anchor");
        let drifted = EntityId::new("specs", "drifted");
        engine
            .relate_entity(
                RelateEntityArgs {
                    source: anchor.clone(),
                    expected_hash: None,
                    rel_type: "USES".to_string(),
                    target: drifted.clone(),
                    remove: false,
                    description: None,
                },
                Actor::Cli,
                None,
                None,
            )
            .expect("relate on conformant entity works");
        let mut args = repair_args(anchor.clone(), None);
        args.relations_unset[0].target = drifted.clone();
        let err = engine
            .update_entity(args, Actor::Cli, None, None)
            .unwrap_err();
        match err {
            EngineError::RepairNotNeeded { id, recovery } => {
                assert_eq!(id, anchor.to_string());
                assert!(
                    recovery.contains("memstead_relate"),
                    "recovery must point at the focused tool; got {recovery}"
                );
            }
            other => panic!("expected RepairNotNeeded, got {other:?}"),
        }
        // Entity unmodified — the relation is still there.
        let entity = engine.store().get(&anchor).unwrap();
        assert!(
            entity.relationships.iter().any(|r| r.target == drifted),
            "gate must not modify the entity"
        );
    }

    /// A non-conformant entity accepts `relations_unset`: the named
    /// relation is removed atomically within the same update that also
    /// repairs the conformance break (`metadata_unset` on the
    /// undeclared field). The post-write entity is integral.
    #[test]
    fn relations_unset_repairs_non_conformant_entity_atomically() {
        let (_tmp, mut engine) = repair_engine();
        let drifted = EntityId::new("specs", "drifted");
        // Pre-state really is non-conformant.
        let pre = engine.conformance_findings("specs", None).unwrap();
        assert!(
            pre.iter().any(|f| f.id == drifted.to_string()),
            "fixture must lint non-conformant; got {pre:?}"
        );
        let mut args = repair_args(drifted.clone(), None);
        args.metadata_unset = vec!["zzz_bogus_field".to_string()];
        engine
            .update_entity(args, Actor::Cli, None, None)
            .expect("repair update lands");
        let entity = engine.store().get(&drifted).unwrap();
        assert!(
            entity.relationships.is_empty(),
            "relation must be removed; got {:?}",
            entity.relationships
        );
        assert!(
            !entity.metadata.contains_key("zzz_bogus_field"),
            "conformance break must be repaired in the same update"
        );
        let post = engine.conformance_findings("specs", None).unwrap();
        assert!(
            post.iter().all(|f| f.id != drifted.to_string()),
            "post-repair entity must be conformant; got {post:?}"
        );
    }

    /// Repair widens accepted inputs, never admissible outputs: a
    /// repair write whose post-state would violate the schema refuses
    /// with the relevant write-time code and nothing lands.
    #[test]
    fn relations_unset_post_state_must_still_validate() {
        let (_tmp, mut engine) = repair_engine();
        let drifted = EntityId::new("specs", "drifted");
        let mut args = repair_args(drifted.clone(), None);
        // Post-state violation: an unknown section alongside the
        // repair input.
        args.sections = IndexMap::from_iter([("nonexistent_section".to_string(), "x".to_string())]);
        let err = engine
            .update_entity(args, Actor::Cli, None, None)
            .unwrap_err();
        assert_eq!(
            err.code(),
            "UNKNOWN_SECTION",
            "strict-write post-condition must hold during repair; got {err:?}"
        );
        // Nothing landed: the relation survives.
        let entity = engine.store().get(&drifted).unwrap();
        assert!(
            !entity.relationships.is_empty(),
            "refused repair must not partially apply"
        );
    }

    /// Absent `(rel_type, target)` pairs are silent no-ops — symmetric
    /// with `metadata_unset` — so a repair retry is idempotent.
    #[test]
    fn relations_unset_absent_pair_is_silent_noop() {
        let (_tmp, mut engine) = repair_engine();
        let drifted = EntityId::new("specs", "drifted");
        let mut args = repair_args(drifted.clone(), None);
        args.relations_unset[0].rel_type = "NEVER_DECLARED".to_string();
        // Also repair the field so the post-state is integral.
        args.metadata_unset = vec!["zzz_bogus_field".to_string()];
        engine
            .update_entity(args, Actor::Cli, None, None)
            .expect("absent pair no-ops, update lands");
        let entity = engine.store().get(&drifted).unwrap();
        assert_eq!(
            entity.relationships.len(),
            1,
            "the USES relation must survive an unmatched unset"
        );
    }
}