memstead-base 0.26.1

Engine internals for Memstead — store, parser, validators, mem lifecycle, workspace policy. Published as a dependency of the memstead-cli and memstead-mcp products, not as a supported library — pre-1.0, no API stability promise.
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
//! Health checks — missing required fields, staleness, scoring.
//!
//! Checks each entity against its schema's requirements:
//! - Required metadata fields present and non-empty
//! - Required sections present and non-empty
//! - Staleness: days since last_modified > schema threshold
//! - Undeclared relationships — existing entities whose
//!   `relationships:` include a name that is not in the per-mem
//!   schema's vocabulary surface as soft warnings rather than hard
//!   load-time failures. Agents can fix either the entity or the
//!   schema; undeclared *types* on load are decision-3 hard errors
//!   and covered elsewhere.

use std::collections::HashMap;
use std::sync::Arc;

use memstead_schema::{Schema, TypeDefinition, type_by_name};

use super::{
    DanglingLink, FoldedTag, HealthIssue, HealthReport, HealthSummary, StaleEntity,
    TagDistribution, TagVariant, UntaggedStats,
};
use crate::entity::MetadataValue;
use crate::graph::query;
use crate::store::Store;

/// Allowed `include` keys for `memstead_health` — the single source of
/// truth shared across the MCP server and the
/// CLI's `health` command. Adding a new include key here lights
/// it up uniformly; agents see the same `UNKNOWN_INCLUDE_KEY` warning
/// shape whether they reach health via MCP or CLI.
pub const HEALTH_INCLUDE_KEYS: &[&str] = &[
    "orphans",
    "stubs",
    "most_connected",
    "missing_fields",
    "stale",
    "dangling_links",
    "tags",
    "missing_required_outgoing",
    "constraints",
    "signals",
    "labelling",
    "conformance",
    "integrity",
    "config",
    "anchors",
    "friction",
    "open_questions",
    "stale_derivations",
    "checks",
    "ledger",
    "vital_signs",
    "due",
];

/// Item cap per vital-signs list.
pub const VITAL_SIGNS_ITEM_CAP: usize = 20;

/// The `include=["vital_signs"]` axis: per mem, the cheap,
/// engine-countable model-truth signals the `/remodel` skill reads — each
/// a count plus a capped list with an explicit `more` remainder, never a
/// verdict, threshold or recommendation (the engine names what is there;
/// the `/remodel` skill decides). Five signals:
///
/// - `type_share_by_community`: per community, how many of its entities
///   sit on the schema's declared last-resort type (`last_resort: true`
///   on exactly one type); `not_declared` when the schema declares none,
///   never a guess from type names.
/// - `unclaimed_source_files`: files of the mem's bound sources that no
///   entity's anchor claims, largest first with their sizes (the size
///   threshold that makes one "large" stays in the skill).
/// - `contested_unowned_files`: files two or more entities claim through
///   anchors while none owns them (no `anchored` class anchor).
/// - `zero_outgoing_entities`: entities with no outgoing edge, folded into
///   the community of their subject (their own cluster when it has more
///   members than themselves, else the cluster of an entity that links to
///   them, else `unplaced`) rather than ranked as singletons.
/// - `empty_declared_sections`: sections the type declares that an
///   entity carries empty.
///
/// Reuse, never recompute: the community partition, the anchor sidecar
/// reads and the source enumeration are the ones the other axes use.
pub fn health_vital_signs_axis(
    engine: &crate::engine::Engine,
    mem_filter: Option<&str>,
) -> serde_json::Value {
    let cap = VITAL_SIGNS_ITEM_CAP;
    let capped = |mut items: Vec<serde_json::Value>| -> serde_json::Value {
        let count = items.len();
        let more = count.saturating_sub(cap);
        items.truncate(cap);
        let mut o = serde_json::Map::new();
        o.insert("count".into(), serde_json::json!(count));
        o.insert("items".into(), serde_json::Value::Array(items));
        if more > 0 {
            o.insert("more".into(), serde_json::json!(more));
        }
        serde_json::Value::Object(o)
    };

    let mut mems: Vec<String> = engine.mem_names().iter().map(|s| s.to_string()).collect();
    mems.sort();
    let communities = engine.communities();
    let mut out = serde_json::Map::new();
    for mem in &mems {
        if let Some(f) = mem_filter
            && f != mem
        {
            continue;
        }
        let entities: Vec<&crate::entity::Entity> = engine
            .store()
            .all_entities()
            .filter(|e| !e.stub && e.id.mem() == mem)
            .collect();
        let schema = engine.schema_for(mem);

        // --- 1. type share per community ---
        let last_resort: Option<String> = schema.as_ref().and_then(|s| {
            s.types
                .values()
                .find(|t| t.last_resort)
                .map(|t| t.name.clone())
        });
        let type_share = match &last_resort {
            None => serde_json::json!({ "status": "not_declared" }),
            Some(lr) => {
                let mut per: std::collections::BTreeMap<String, (usize, usize)> =
                    std::collections::BTreeMap::new();
                for e in &entities {
                    let cluster = communities
                        .entity_cluster_map
                        .get(&e.id.0)
                        .cloned()
                        .unwrap_or_else(|| "unplaced".to_string());
                    let slot = per.entry(cluster).or_insert((0, 0));
                    slot.0 += 1;
                    if e.entity_type == *lr {
                        slot.1 += 1;
                    }
                }
                let mut rows: Vec<serde_json::Value> = per
                    .into_iter()
                    .map(|(community, (total, on_last_resort))| {
                        serde_json::json!({
                            "community": community,
                            "entities": total,
                            "on_last_resort_type": on_last_resort,
                        })
                    })
                    .collect();
                // Most concentrated first: the reader sees the flattest
                // cluster without ranking being a judgement.
                rows.sort_by(|a, b| {
                    let share = |v: &serde_json::Value| {
                        let t = v["entities"].as_u64().unwrap_or(1).max(1) as f64;
                        v["on_last_resort_type"].as_u64().unwrap_or(0) as f64 / t
                    };
                    share(b)
                        .partial_cmp(&share(a))
                        .unwrap_or(std::cmp::Ordering::Equal)
                        .then_with(|| a["community"].as_str().cmp(&b["community"].as_str()))
                });
                let mut v = capped(rows);
                v["status"] = serde_json::json!("declared");
                v["last_resort_type"] = serde_json::json!(lr);
                v
            }
        };

        // --- 2 and 3. the file-to-entity map of the bound sources ---
        // artifact -> (claiming entities, owned by an `anchored` anchor)
        let mut claims: std::collections::BTreeMap<
            String,
            (std::collections::BTreeSet<String>, bool),
        > = std::collections::BTreeMap::new();
        for e in &entities {
            for a in engine.entity_anchors(&e.id) {
                let slot = claims.entry(a.artifact.clone()).or_default();
                slot.0.insert(e.id.0.clone());
                if a.class == crate::anchor::AnchorProvenanceClass::Anchored {
                    slot.1 = true;
                }
            }
        }
        let roots = engine.anchor_source_roots(mem);
        let mut unclaimed: Vec<serde_json::Value> = Vec::new();
        let mut sources_enumerated = 0usize;
        if let Some(ws) = engine.workspace_root() {
            let mut seen: std::collections::BTreeSet<String> = std::collections::BTreeSet::new();
            for join in roots.values() {
                sources_enumerated += 1;
                for file in crate::source_scope::enumerate_source_artifacts(
                    engine,
                    &join.source,
                    &join.deny_paths,
                    ws,
                ) {
                    if !seen.insert(file.clone()) || claims.contains_key(&file) {
                        continue;
                    }
                    let size = std::fs::metadata(ws.join(&file))
                        .map(|m| m.len())
                        .unwrap_or(0);
                    unclaimed.push(serde_json::json!({ "artifact": file, "bytes": size }));
                }
            }
        }
        unclaimed.sort_by(|a, b| {
            b["bytes"]
                .as_u64()
                .cmp(&a["bytes"].as_u64())
                .then_with(|| a["artifact"].as_str().cmp(&b["artifact"].as_str()))
        });
        let unclaimed_v = if sources_enumerated == 0 {
            serde_json::json!({ "status": "no_bound_source" })
        } else {
            let mut v = capped(unclaimed);
            v["status"] = serde_json::json!("enumerated");
            v
        };
        let contested: Vec<serde_json::Value> = claims
            .iter()
            .filter(|(_, (who, owned))| who.len() >= 2 && !owned)
            .map(|(artifact, (who, _))| {
                serde_json::json!({
                    "artifact": artifact,
                    "claimed_by": who.iter().cloned().collect::<Vec<_>>(),
                })
            })
            .collect();

        // --- 4. zero-outgoing entities, folded into their subject ---
        let cluster_size = |c: &str| -> usize {
            communities
                .clusters
                .get(c)
                .map(|ci| ci.entities.len())
                .unwrap_or(0)
        };
        let mut by_community: std::collections::BTreeMap<String, Vec<String>> =
            std::collections::BTreeMap::new();
        for e in &entities {
            if !engine.store().outgoing(&e.id).is_empty() {
                continue;
            }
            let own = communities.entity_cluster_map.get(&e.id.0).cloned();
            let community = match own {
                Some(c) if cluster_size(&c) > 1 => c,
                _ => engine
                    .store()
                    .incoming(&e.id)
                    .iter()
                    .find_map(|edge| communities.entity_cluster_map.get(&edge.from.0).cloned())
                    .unwrap_or_else(|| "unplaced".to_string()),
            };
            by_community
                .entry(community)
                .or_default()
                .push(e.id.0.clone());
        }
        let zero_total: usize = by_community.values().map(Vec::len).sum();
        let zero_rows: Vec<serde_json::Value> = by_community
            .into_iter()
            .map(|(community, mut ids)| {
                ids.sort();
                let count = ids.len();
                let more = count.saturating_sub(cap);
                ids.truncate(cap);
                let mut o = serde_json::json!({
                    "community": community,
                    "count": count,
                    "items": ids,
                });
                if more > 0 {
                    o["more"] = serde_json::json!(more);
                }
                o
            })
            .collect();
        let mut zero_v = capped(zero_rows);
        zero_v["entities"] = serde_json::json!(zero_total);

        // --- 5. declared sections carried empty ---
        let mut empty_sections: Vec<serde_json::Value> = Vec::new();
        if let Some(s) = &schema {
            for e in &entities {
                let Some(td) = s.types.get(&e.entity_type) else {
                    continue;
                };
                for sec in &td.sections {
                    if e.sections
                        .get(&sec.key)
                        .is_some_and(|body| body.trim().is_empty())
                    {
                        empty_sections.push(serde_json::json!({
                            "id": e.id.0,
                            "section": sec.key,
                        }));
                    }
                }
            }
        }

        out.insert(
            mem.clone(),
            serde_json::json!({
                "type_share_by_community": type_share,
                "unclaimed_source_files": unclaimed_v,
                "contested_unowned_files": capped(contested),
                "zero_outgoing_entities": zero_v,
                "empty_declared_sections": capped(empty_sections),
            }),
        );
    }
    let mut top = serde_json::Map::new();
    top.insert("_item_cap".into(), serde_json::json!(cap));
    for (k, v) in out {
        top.insert(k, v);
    }
    serde_json::Value::Object(top)
}

/// The `include=["anchors"]` axis — per-mem counts of the four
/// standalone-verification states, computed through the same
/// per-anchor mechanism `verify-anchors` and the binding verify use.
/// Shared by the health composer, the CLI health command, and the
/// MCP server so the axis cannot drift between surfaces.
/// The check records of one source (the workspace ledger, or one
/// archive's sealed member), folded the way the checks axis reads them.
#[derive(Default)]
struct CheckMaps {
    /// Newest `verification` record per entity.
    latest: std::collections::BTreeMap<String, crate::check::CheckRecord>,
    /// Newest `conformance` record per entity.
    latest_conformance: std::collections::BTreeMap<String, crate::check::CheckRecord>,
    /// Foreign `x-<name>` kinds per entity: recorded verbatim, never
    /// aggregated into a state, listed by count per mem.
    foreign_by_entity: std::collections::BTreeMap<String, Vec<String>>,
    /// Newest record of any kind per entity, for the finding it may carry.
    newest_any: std::collections::BTreeMap<String, crate::check::CheckRecord>,
    /// Every verification record per entity, in standing order: the per-record
    /// readings show each check's standing, not only the newest one's.
    all_verification: std::collections::BTreeMap<String, Vec<crate::check::CheckRecord>>,
}

impl CheckMaps {
    fn from_records<'a>(records: impl Iterator<Item = &'a crate::check::CheckRecord>) -> Self {
        let mut maps = Self::default();
        for rec in records {
            maps.newest_any.insert(rec.entity.clone(), rec.clone());
            match rec.resolved_kind() {
                Some(crate::check::CheckKind::Verification) => {
                    maps.all_verification
                        .entry(rec.entity.clone())
                        .or_default()
                        .push(rec.clone());
                    maps.latest.insert(rec.entity.clone(), rec.clone());
                }
                Some(crate::check::CheckKind::Conformance) => {
                    maps.latest_conformance
                        .insert(rec.entity.clone(), rec.clone());
                }
                None => {
                    if let Some(k) = rec.foreign_kind() {
                        maps.foreign_by_entity
                            .entry(rec.entity.clone())
                            .or_default()
                            .push(k.to_string());
                    }
                }
            }
        }
        maps
    }
}

/// The `include=["checks"]` axis: per mem,
/// counts of the four derived check states plus the author≠checker
/// independence gate over ok-checked entities. The gate compares
/// caller-declared IDENTITIES and nothing else:
/// the entity's created-by record against its newest ok-check record
/// — both carry an identity and they are equal → `self_checked`
/// ("twice-asserted, not verified"); both carry one and they differ →
/// `confirmed_independent`; either side lacks one → `unconfirmable`,
/// never a guessed category. Transport is not identity: the recorded
/// `(actor, client)` pair names the SURFACE a record arrived through
/// and is recorded as context only — it never participates in the
/// comparison (a same pair does not establish the same actor, and
/// different pairs do not establish different actors). An identity is
/// an honesty device, not authentication: caller-declared, unverified,
/// tamper-evident in append-only history — a consistent multi-agent
/// setup gets real independence signals, and a lying setup defeats
/// only itself. Derivation only: nothing here is stamped, and a
/// workspace without a check ledger serves all-never-checked.
/// Identity lists are capped at [`OPEN_QUESTIONS_ITEM_CAP`] with an
/// explicit `more` count.
pub fn health_checks_axis(
    engine: &crate::engine::Engine,
    mem_filter: Option<&str>,
) -> serde_json::Value {
    let cap = OPEN_QUESTIONS_ITEM_CAP;
    let capped = |mut items: Vec<String>| -> serde_json::Value {
        items.sort();
        let count = items.len();
        let more = count.saturating_sub(cap);
        items.truncate(cap);
        let mut o = serde_json::Map::new();
        o.insert("count".into(), serde_json::json!(count));
        o.insert("items".into(), serde_json::json!(items));
        if more > 0 {
            o.insert("more".into(), serde_json::json!(more));
        }
        serde_json::Value::Object(o)
    };

    // The workspace ledger, folded once for every non-archive mem: the
    // newest record per (entity, kind), every verification record, the
    // newest record of any kind (for its finding), the foreign kinds.
    // State is kind-scoped: a conformance record never supersedes a
    // verification record, or the reverse.
    let ledger_records: Vec<crate::check::CheckRecord> = engine
        .workspace_root()
        .map(|root| crate::check::CheckLedger::for_workspace(root).in_standing_order())
        .unwrap_or_default();
    let from_ledger = CheckMaps::from_records(ledger_records.iter());

    let mut mems: Vec<String> = engine.mem_names().iter().map(|s| s.to_string()).collect();
    mems.sort();
    let mut out = serde_json::Map::new();
    for mem in mems {
        if let Some(f) = mem_filter
            && f != mem
        {
            continue;
        }
        // One source per mount: an archive mount derives from its sealed
        // check records (`.memstead/checks.json`) and never from the
        // workspace ledger beside it; every other mount from the ledger.
        let sealed_maps;
        let (maps, sealed_note) = if engine.is_archive_mount(&mem) {
            match engine.archive_checks_for(&mem) {
                Some(sealed) => {
                    let records: Vec<crate::check::CheckRecord> = sealed
                        .entities
                        .iter()
                        .flat_map(|(path, kinds)| {
                            let id = crate::EntityId::new(&mem, path).0;
                            kinds.iter().map(move |(kind, sc)| sc.to_record(&id, kind))
                        })
                        .collect();
                    sealed_maps = CheckMaps::from_records(records.iter());
                    (
                        &sealed_maps,
                        Some(serde_json::json!({
                            "carried": true,
                            "records": records.len(),
                        })),
                    )
                }
                None => {
                    sealed_maps = CheckMaps::default();
                    (
                        &sealed_maps,
                        Some(serde_json::json!({
                            "carried": false,
                            "reason": "the archive carries no check records; every state reads never_checked",
                        })),
                    )
                }
            }
        } else {
            (&from_ledger, None)
        };
        let CheckMaps {
            latest,
            latest_conformance,
            foreign_by_entity,
            newest_any,
            all_verification,
        } = maps;
        let mut counts = std::collections::BTreeMap::from([
            ("never_checked", 0usize),
            ("checked_ok", 0usize),
            ("check_failed", 0usize),
            ("check_stale", 0usize),
        ]);
        // The `conformance` kind's counts, additively beside the
        // verification counts. Pin-aware: a schema re-pin stales
        // every conformance verdict recorded under the old pin. A
        // workspace with no conformance records serves all
        // `never_checked` — honestly empty, never absent.
        let current_pin = engine
            .mount(&mem)
            .and_then(|m| m.schema.as_ref())
            .map(|s| s.as_display());
        let mut conformance_counts = std::collections::BTreeMap::from([
            ("never_checked", 0usize),
            ("checked_ok", 0usize),
            ("check_failed", 0usize),
            ("check_stale", 0usize),
        ]);
        let mut self_checked: Vec<String> = Vec::new();
        let mut confirmed_independent: Vec<String> = Vec::new();
        let mut unconfirmable: Vec<String> = Vec::new();
        // Which identities each ok-checked criterion was compared against
        // (engine::independence), rendered so a reader can see who counted
        // as an executor. One provenance pass per mem.
        let mut executors = serde_json::Map::new();
        let mut readings = serde_json::Map::new();
        let touches = engine.mem_touches(&mem);
        let mut foreign_kinds: std::collections::BTreeMap<String, usize> =
            std::collections::BTreeMap::new();
        let mut findings = serde_json::Map::new();
        for e in engine.store().all_entities().filter(|e| e.mem == mem) {
            let id = e.id.0.clone();
            if let Some(kinds) = foreign_by_entity.get(&id) {
                for k in kinds {
                    *foreign_kinds.entry(k.clone()).or_insert(0) += 1;
                }
            }
            if let Some(rec) = newest_any.get(&id)
                && let Some(f) = &rec.finding
            {
                findings.insert(
                    id.clone(),
                    serde_json::json!({
                        "verdict": rec.verdict,
                        "kind": rec.kind.as_deref().unwrap_or("verification"),
                        "ts": rec.ts,
                        "observed_at": rec.observed_at,
                        "identity": rec.identity,
                        // The trust class of the prose on this row: the
                        // message is whatever agent recorded the check
                        // wrote, which in a workspace that points a foreign
                        // model or a transcribed outside finding at its
                        // ledger is not the workspace itself.
                        "origin": engine.check_row_prose_origin(&mem, &id, rec).as_wire(),
                        "finding": f,
                    }),
                );
            }
            let state = crate::check::derive_state(latest.get(&id), &e.content_hash);
            *counts.entry(state.as_str()).or_insert(0) += 1;
            if let Some(records) = all_verification.get(&id) {
                let rows: Vec<serde_json::Value> = records
                    .iter()
                    .map(|rec| {
                        let reading = if rec.verdict == "ok" {
                            engine.independence_of(e, rec, &touches).0.as_str()
                        } else {
                            "failed"
                        };
                        // The declared role rides every row, and whether
                        // it is a checking capacity: a gate closes only
                        // on a row that is fresh, independent AND
                        // declared `checker` or `verifier`.
                        let role_confirms = matches!(
                            crate::vcs::Role::from_wire(&rec.role).unwrap_or_default(),
                            crate::vcs::Role::Checker | crate::vcs::Role::Verifier
                        );
                        serde_json::json!({
                            "ts": rec.ts,
                            "observed_at": rec.observed_at,
                            "identity": rec.identity,
                            "verdict": rec.verdict,
                            "reading": reading,
                            "role": rec.role,
                            "role_confirms": role_confirms,
                        })
                    })
                    .collect();
                readings.insert(id.clone(), serde_json::Value::Array(rows));
            }
            let cstate = crate::check::derive_state_pinned(
                latest_conformance.get(&id),
                &e.content_hash,
                current_pin.as_deref(),
            );
            *conformance_counts.entry(cstate.as_str()).or_insert(0) += 1;
            if state != crate::check::CheckState::CheckedOk {
                continue;
            }
            // Identity-only comparison (comparator widened
            // 2026-09-02): the newest ok-check's identity against every
            // identity that mutated the verified plan, its criteria or
            // its session-log notes since this criterion was written
            // (engine::independence); a non-criterion compares against
            // every identity that wrote it up to the write the check saw.
            // The (actor, client) transport pair is
            // recorded context and never a comparator. Either side
            // lacking an identity is unconfirmable, never a guessed
            // category.
            let check = latest.get(&id).expect("checked_ok implies a record");
            let (reading, execs) = engine.independence_of(e, check, &touches);
            if let Some(execs) = execs {
                executors.insert(id.clone(), serde_json::json!(execs.identities));
            }
            match reading {
                crate::engine::independence::Independence::SelfChecked => self_checked.push(id),
                crate::engine::independence::Independence::ConfirmedIndependent => {
                    confirmed_independent.push(id)
                }
                crate::engine::independence::Independence::Unconfirmable => unconfirmable.push(id),
            }
        }
        let mut m = serde_json::Map::new();
        for (k, v) in counts {
            m.insert(k.to_string(), serde_json::json!(v));
        }
        let mut c = serde_json::Map::new();
        for (k, v) in conformance_counts {
            c.insert(k.to_string(), serde_json::json!(v));
        }
        m.insert("conformance".into(), serde_json::Value::Object(c));
        // Foreign kinds by count, and the structured finding each
        // entity's newest record carries — recorded, rendered, never
        // interpreted.
        m.insert(
            "foreign_kinds".into(),
            serde_json::to_value(&foreign_kinds).unwrap_or(serde_json::json!({})),
        );
        m.insert("findings".into(), serde_json::Value::Object(findings));
        // On an archive mount: whether the archive seals check records at
        // all, so an all-`never_checked` reading is explained, never bare.
        if let Some(note) = sealed_note {
            m.insert("sealed".into(), note);
        }
        m.insert(
            "independence".into(),
            serde_json::json!({
                "self_checked": capped(self_checked),
                "confirmed_independent": capped(confirmed_independent),
                "unconfirmable": capped(unconfirmable),
                "comparator": "every identity that mutated the verified plan, its criteria or its session-log notes since the criterion was written; a non-criterion compares against every identity that wrote it up to the write the check saw",
                "executors": serde_json::Value::Object(executors),
                "readings": serde_json::Value::Object(readings),
            }),
        );
        out.insert(mem, serde_json::Value::Object(m));
    }
    serde_json::Value::Object(out)
}

/// One derivation-staleness finding: an
/// explicit edge on a derivation-declared rel-type whose baseline
/// differs from the target's current hash (`stale`), or that has no
/// recorded baseline at all (`unbaselined`). Fresh edges are never
/// reported.
#[derive(Debug, Clone, serde::Serialize)]
pub struct DerivationFinding {
    pub source: crate::entity::EntityId,
    pub rel_type: String,
    pub target: crate::entity::EntityId,
    /// `"stale"` or `"unbaselined"` — never fabricated as fresh.
    pub state: String,
    /// The recorded baseline hash (`None` for unbaselined edges).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub baseline: Option<String>,
    /// The target's current content hash ("" for an absent target).
    pub current: String,
}

/// The `include=["stale_derivations"]` axis: per-mem findings from
/// [`crate::engine::Engine::derivation_report`], shared by the CLI
/// and the MCP server. A mem whose schema declares no derivation
/// rel-types contributes an empty list — never an error.
pub fn health_stale_derivations_axis(
    engine: &crate::engine::Engine,
    mem_filter: Option<&str>,
) -> serde_json::Value {
    let mut mems: Vec<String> = engine.mem_names().iter().map(|s| s.to_string()).collect();
    mems.sort();
    let mut out = serde_json::Map::new();
    for mem in mems {
        if let Some(f) = mem_filter
            && f != mem
        {
            continue;
        }
        let findings = engine.derivation_report(&mem).unwrap_or_default();
        out.insert(
            mem,
            serde_json::to_value(&findings).unwrap_or(serde_json::Value::Array(Vec::new())),
        );
    }
    serde_json::Value::Object(out)
}

/// Per-kind item cap for the `open_questions` axis — the axis is an
/// agent worklist, not a dump. Stated in the output (`_item_cap`);
/// truncation is always explicit via each list's `more` count.
pub const OPEN_QUESTIONS_ITEM_CAP: usize = 20;

/// The `include=["open_questions"]` axis: per
/// mem, a composed worklist of what the holding does not know — its
/// stubs, its never-confirmed (`recheck`) and `unresolvable` anchors,
/// its unsatisfied constraints, its dangling links, and, when a
/// paired process mem is resolvable for the destination, that
/// process mem's open entries. Negative findings ride under the
/// DISTINCT `already_searched` heading — their operational meaning is
/// "done, keep off", never todo.
///
/// Composition only: every signal is read from the same source its
/// own axis serves (store stub flags, `verify_mem_anchors`,
/// `constraint_findings`, `collect_dangling_links`, the pipeline
/// store), so this axis can never disagree with the per-signal axes.
/// Best-effort on the process leg: an unreadable pipeline store means
/// no process sections, never an axis failure.
pub fn health_open_questions_axis(
    engine: &crate::engine::Engine,
    mem_filter: Option<&str>,
) -> serde_json::Value {
    let cap = OPEN_QUESTIONS_ITEM_CAP;
    let capped = |mut items: Vec<serde_json::Value>| -> serde_json::Value {
        let count = items.len();
        let more = count.saturating_sub(cap);
        items.truncate(cap);
        let mut o = serde_json::Map::new();
        o.insert("count".into(), serde_json::json!(count));
        o.insert("items".into(), serde_json::Value::Array(items));
        if more > 0 {
            o.insert("more".into(), serde_json::json!(more));
        }
        serde_json::Value::Object(o)
    };

    // Bindings by destination mem. The pairing is not declarative yet;
    // until it is, the ingest-name convention (process mem
    // named after the binding) is the resolution mechanism.
    let bindings: Vec<(String, String)> = engine
        .workspace_root()
        .and_then(|root| crate::pipeline_store::load_pipeline_configs(root).ok())
        .map(|c| {
            c.bindings
                .iter()
                .map(|r| (r.config.destination_mem.clone(), r.name.clone()))
                .collect()
        })
        .unwrap_or_default();
    let mounted: Vec<String> = engine.mem_names().iter().map(|s| s.to_string()).collect();

    let mut mems: Vec<String> = mounted.clone();
    mems.sort();
    let mut out = serde_json::Map::new();
    for mem in &mems {
        if let Some(f) = mem_filter
            && f != mem
        {
            continue;
        }

        // Stubs — same source as the stubs axis (store stub flag).
        let stubs = capped(
            engine
                .store()
                .all_entities()
                .filter(|e| e.stub && e.id.mem() == mem)
                .map(|e| serde_json::json!({ "kind": "stub", "id": e.id.to_string() }))
                .collect(),
        );

        // Anchors — same per-anchor mechanism as the anchors axis;
        // only the never-confirmed and unreachable states are holes.
        // A dangling sidecar row is a hole too, and
        // it gets its OWN bucket. `unresolvable` is the wire form of the
        // orphaned state, whose repair is the opposite one: an orphaned anchor
        // asks whether its entity should be re-anchored or pruned, a dangling
        // row asks why the entity went missing. Folding the two together here
        // would reproduce, on the health axis, exactly the collapse the axis
        // itself refuses.
        //
        // `entity_end_unreconciled` rides along both ways: an empty dangling
        // bucket means "none found" only when the check could run at all.
        let (mut recheck, mut unresolvable, mut unobserved, mut dangling_rows) =
            (Vec::new(), Vec::new(), Vec::new(), Vec::new());
        // Span rows whose quoted words are gone: the claim's basis moved,
        // so someone has to re-read the source. Its own bucket, like the
        // others: it is neither a gone artifact nor an unmeasured row.
        let mut span_absent = Vec::new();
        // Rows resting on a recorded observation older than today: open
        // work too (someone has to look again), stated as its age.
        let mut aging = Vec::new();
        let mut entity_end_unreconciled: Option<String> = None;
        if let Ok(report) = engine.verify_mem_anchors(mem) {
            entity_end_unreconciled = report.unreconciled.clone();
            for a in &report.anchors {
                if let Some(days) = a.unobserved_for_days
                    && days > 0
                {
                    aging.push(serde_json::json!({
                        "kind": "anchor_aging",
                        "id": a.entity_id,
                        "artifact": a.artifact,
                        "state": a.state,
                        "observed_at": a.observed_at,
                        "unobserved_for_days": days,
                        "note": format!("unobserved for {days} days"),
                    }));
                }
                let item = serde_json::json!({
                    "kind": format!("anchor_{}", a.state),
                    "id": a.entity_id,
                    "artifact": a.artifact,
                });
                match a.state.as_str() {
                    "recheck" => recheck.push(item),
                    // The enum's wire name for a gone artifact; the bucket
                    // keeps the axis's count name (`anchors_unresolvable`).
                    "orphaned" => unresolvable.push(item),
                    "span_absent" => span_absent.push(item),
                    "unobserved" => unobserved.push(item),
                    "dangling" => dangling_rows.push(item),
                    _ => {}
                }
            }
        }

        // Unsatisfied constraints — same collector as the
        // constraints axis.
        let constraints = capped(
            engine
                .constraint_findings(Some(mem))
                .iter()
                .map(|r| {
                    serde_json::json!({
                        "kind": "unsatisfied_constraint",
                        "id": r.id.to_string(),
                        "violations": r.violations.len(),
                    })
                })
                .collect(),
        );

        // Dangling links — same collector as the overview include, and the
        // SAME three names rather than a parallel vocabulary of its own.
        // This axis emitted one `dangling_link` kind
        // over all three conditions, which is the fused code by another
        // spelling; a second vocabulary is the one that drifts first.
        let dangling = capped(
            collect_dangling_links(engine.store(), Some(mem))
                .iter()
                .map(|d| {
                    serde_json::json!({
                        "kind": d.kind.code(),
                        "id": d.from.to_string(),
                        "target": d.target_id.to_string(),
                        "repair": d.kind.repair(),
                    })
                })
                .collect(),
        );

        // Paired process mems: open entries are work; negative
        // findings are the opposite — already searched, keep off.
        // Pairing runs through the ONE resolution function the brief
        // renderer uses: a destination's
        // declaration wins regardless of naming — and pairs even
        // with no binding at all (the process tier stands without
        // one); the binding-name convention remains the fallback. A
        // declaration naming an unmounted mem is a typed finding,
        // never a silent fallback.
        let mut process = Vec::new();
        let mem_bindings: Vec<&String> = bindings
            .iter()
            .filter(|(d, _)| d == mem)
            .map(|(_, b)| b)
            .collect();
        let mut resolutions: Vec<(Option<String>, crate::binding_run::ProcessMemResolution)> =
            Vec::new();
        if mem_bindings.is_empty() {
            let r = crate::binding_run::resolve_process_mem(engine, mem, "");
            if r.declared {
                resolutions.push((None, r));
            }
        } else {
            for binding in &mem_bindings {
                // `binding` is the stem; the convention derives from the
                // canonical id `<mem>/<stem>`, the same input the brief
                // renderer hands the derivation.
                let derived =
                    crate::binding_run::derived_process_mem_name(&format!("{mem}/{binding}"));
                resolutions.push((
                    Some((*binding).clone()),
                    crate::binding_run::resolve_process_mem(engine, mem, &derived),
                ));
            }
        }
        for (binding, r) in resolutions {
            if r.mounted {
                let mut open = Vec::new();
                let mut searched = Vec::new();
                for e in engine
                    .store()
                    .all_entities()
                    .filter(|e| !e.stub && e.id.mem() == r.mem.as_str())
                {
                    let item = serde_json::json!({
                        "kind": e.entity_type,
                        "id": e.id.to_string(),
                        "title": e.title,
                    });
                    if e.entity_type == "negative_finding" {
                        searched.push(item);
                    } else {
                        open.push(item);
                    }
                }
                // The store iterates in hash order; a byte-parity gate over
                // this axis (the CLI-versus-MCP pin, a fixture diff between
                // two builds) needs the same bytes every run, so both lists
                // are ordered by id before the cap takes the head.
                let by_id = |a: &serde_json::Value, b: &serde_json::Value| {
                    a["id"].as_str().cmp(&b["id"].as_str())
                };
                open.sort_by(by_id);
                searched.sort_by(by_id);
                process.push(serde_json::json!({
                    "binding": binding,
                    "process_mem": r.mem,
                    "declared": r.declared,
                    "resolvable": true,
                    "open_entries": capped(open),
                    "already_searched": capped(searched),
                }));
            } else if r.declared {
                process.push(serde_json::json!({
                    "binding": binding,
                    "process_mem": r.mem,
                    "declared": true,
                    "resolvable": false,
                    "finding": "DECLARED_PROCESS_MEM_MISSING",
                }));
            } else {
                process.push(serde_json::json!({
                    "binding": binding,
                    "resolvable": false,
                }));
            }
        }

        // The resolution readings: for every type of this mem
        // that declares `resolution`, the open entities with no condition
        // written, and the open entities whose condition nobody has
        // checked under the declared kind. The ledger is read as it is;
        // an `x-` kind counts by name, an engine kind by derived state.
        let (mut missing, mut unchecked) = (Vec::new(), Vec::new());
        if let Some(schema) = engine.schema_for(mem) {
            for entity in engine
                .store()
                .all_entities()
                .filter(|e| !e.stub && e.id.mem() == mem)
            {
                let Some(td) = schema.types.get(&entity.entity_type) else {
                    continue;
                };
                let Some(res) = &td.resolution else { continue };
                if let Some(field) = &res.status_field {
                    let open = match entity.metadata.get(field) {
                        Some(crate::entity::MetadataValue::String(s)) => {
                            res.open_values.contains(s)
                        }
                        _ => false,
                    };
                    if !open {
                        continue;
                    }
                }
                let has_condition = entity
                    .sections
                    .get(&res.condition_section)
                    .is_some_and(|body| !body.trim().is_empty());
                if !has_condition {
                    missing.push(serde_json::json!({
                        "kind": "resolution_missing",
                        "id": entity.id.to_string(),
                        "section": res.condition_section,
                    }));
                    continue;
                }
                let kind = res.check_kind.as_deref().unwrap_or("verification");
                // The newest record of the declared kind, from the mount's
                // one source (sealed member or workspace ledger), read the
                // way the entity read derives its state.
                let checked = crate::check::RecordKind::from_wire(kind).is_some()
                    && engine
                        .latest_check_record(mem, &entity.id.0, kind)
                        .is_some_and(|r| r.verdict == "ok" && r.entity_hash == entity.content_hash);
                if !checked {
                    unchecked.push(serde_json::json!({
                        "kind": "resolution_unchecked",
                        "id": entity.id.to_string(),
                        "section": res.condition_section,
                        "check_kind": kind,
                    }));
                }
            }
        }
        let resolution_missing = capped(missing);
        let resolution_unchecked = capped(unchecked);

        let total_open = stubs["count"].as_u64().unwrap_or(0)
            + resolution_missing["count"].as_u64().unwrap_or(0)
            + resolution_unchecked["count"].as_u64().unwrap_or(0)
            + recheck.len() as u64
            + unresolvable.len() as u64
            + span_absent.len() as u64
            + unobserved.len() as u64
            + dangling_rows.len() as u64
            + aging.len() as u64
            + constraints["count"].as_u64().unwrap_or(0)
            + dangling["count"].as_u64().unwrap_or(0)
            + process
                .iter()
                .filter_map(|p| p["open_entries"]["count"].as_u64())
                .sum::<u64>();

        let mut entry = serde_json::Map::new();
        entry.insert("stubs".into(), stubs);
        entry.insert("anchors_recheck".into(), capped(recheck));
        entry.insert("anchors_unresolvable".into(), capped(unresolvable));
        entry.insert("anchors_span_absent".into(), capped(span_absent));
        // Its own bucket here too. The comment above argues that folding two
        // anchor conditions together reproduces the collapse the axis refuses,
        // and a first version then did exactly that eight lines further down.
        entry.insert("anchors_unobserved".into(), capped(unobserved));
        entry.insert("anchors_dangling".into(), capped(dangling_rows));
        entry.insert("anchors_aging".into(), capped(aging));
        if let Some(why) = entity_end_unreconciled {
            entry.insert("entity_end_unreconciled".into(), serde_json::json!(why));
        }
        entry.insert("unsatisfied_constraints".into(), constraints);
        entry.insert("dangling_links".into(), dangling);
        entry.insert("resolution_missing".into(), resolution_missing);
        entry.insert("resolution_unchecked".into(), resolution_unchecked);
        if !process.is_empty() {
            entry.insert("process".into(), serde_json::Value::Array(process));
        } else {
            // No binding targets this mem: the absence of a process
            // section is stated, never silent.
            entry.insert("process_mem_resolvable".into(), serde_json::json!(false));
        }
        entry.insert("total_open".into(), serde_json::json!(total_open));
        out.insert(mem.clone(), serde_json::Value::Object(entry));
    }
    let mut top = serde_json::Map::new();
    top.insert("_item_cap".into(), serde_json::json!(cap));
    for (k, v) in out {
        top.insert(k, v);
    }
    serde_json::Value::Object(top)
}

pub fn health_anchors_axis(
    engine: &crate::engine::Engine,
    mem_filter: Option<&str>,
) -> serde_json::Value {
    let mut mems: Vec<String> = engine.mem_names().iter().map(|s| s.to_string()).collect();
    mems.retain(|m| mem_filter.is_none_or(|v| m == v));
    mems.sort();
    let mut out = serde_json::Map::new();
    for mem in mems {
        let Ok(report) = engine.verify_mem_anchors(&mem) else {
            continue;
        };
        let condition = report.sidecar_error.as_ref().map(|why| {
            serde_json::json!({
                "code": "ANCHORS_SIDECAR_UNREADABLE",
                "mem": mem,
                "reason": why,
            })
        });
        // The figure travels as its type's own three fields (`resolves`,
        // `population`, `fully_adjudicated`), merged into the row.
        let mut row = serde_json::json!({
                // The one condition that replaces the counts: an unreadable
                // sidecar. Absent when the sidecar read cleanly.
                "condition": condition,
                "drifted": report.drifted,
                "recheck": report.recheck,
                // Split from `unresolvable`: the artifact
                // being gone is a measurement, the pass not reaching it is the
                // absence of one, and this is the surface a reader arrives at
                // without a binding in hand.
                "unresolvable": report.unresolvable,
                // Span rows whose quoted words are gone from a document
                // that is still there.
                "span_absent": report.span_absent,
                "unobserved": report.unobserved,
                // The entity end (03/02). Carried here too, both ways: four
                // counts over a mem whose sidecar has outlived its entities
                // read as a healthy axis, and so does a zero over a mem whose
                // entity end was never reconciled.
                "dangling": report.dangling,
                "entity_end_unreconciled": report.unreconciled,
                // Rows whose state rests on a recorded observation (url
                // rows), each with how long it has gone unobserved. A state
                // observed months ago is not today's state, and the axis
                // says so beside the count it contributed to.
                "aging": report
                    .anchors
                    .iter()
                    .filter(|a| a.observed_at.is_some())
                    .map(|a| {
                        let days = a.unobserved_for_days.unwrap_or(0);
                        serde_json::json!({
                            "id": a.entity_id,
                            "artifact": a.artifact,
                            "state": a.state,
                            "observed_at": a.observed_at,
                            "unobserved_for_days": days,
                            "note": format!("unobserved for {days} days"),
                        })
                    })
                    .collect::<Vec<_>>(),
        });
        if let (Some(o), Ok(serde_json::Value::Object(f))) =
            (row.as_object_mut(), serde_json::to_value(&report.figure))
        {
            o.extend(f);
        }
        out.insert(mem, row);
    }
    serde_json::Value::Object(out)
}

/// Compute health reports for all entities in the store.
///
/// `mem_schemas` maps mem name → `Arc<Schema>`. Entities whose mem
/// is missing from this map fall back to the builtin `default` schema
/// relationship vocabulary (keeps legacy fixtures green; real production
/// paths always register a mem schema).
///
/// `mem_filter` scopes the per-entity scans and the structural counts
/// (orphans, stubs, leaf population) to one mem; `None` is the classic
/// engine-wide sweep. Validating that the name exists is the caller's
/// job ([`crate::Engine::health_scoped`] refuses `UNKNOWN_MEM` before
/// reaching here) — an unknown name at this level just scans nothing.
pub fn compute_health(
    store: &Store,
    default_schema: &TypeDefinition,
    mem_schemas: &HashMap<String, Arc<Schema>>,
    mem_filter: Option<&str>,
) -> HealthSummary {
    let mut missing_fields = Vec::new();
    let mut stale_entities = Vec::new();

    let today_days = days_since_epoch();

    let in_scope = |mem: &str| mem_filter.is_none_or(|v| mem == v);

    for entity in store.all_entities() {
        if entity.stub || !in_scope(&entity.mem) {
            continue;
        }

        // Resolve the entity's `TypeDefinition` against the entity's
        // own mem's schema first. `type_by_name` only knows the
        // builtin `default` schema; falling through to it on a mem
        // pinned to a non-default schema (e.g. `planning@0.1.0`) would
        // silently use `default_schema` (effectively `spec`) for every
        // entity and report `spec`'s `health_required_fields` —
        // `[identity, purpose]` — even on entities of types like
        // `goal` / `option` / `decision`.
        let resolved = mem_schemas
            .get(entity.mem.as_str())
            .and_then(|s| s.types.get(entity.entity_type.as_str()).cloned())
            .or_else(|| type_by_name(&entity.entity_type));
        let schema: &TypeDefinition = resolved.as_deref().unwrap_or(default_schema);
        let mut issues = Vec::new();

        // Check health_required_fields
        for field in &schema.health_required_fields {
            // Check if it's a section or metadata field
            if schema.section(field).is_some() {
                // It's a section. When the content is present in the
                // file but sits under a non-deriving heading, report
                // the distinct mismatch finding instead of "missing" —
                // the two conditions must never collapse.
                let content = entity.sections.get(field.as_str());
                if content.is_none_or(|c| c.trim().is_empty()) {
                    if let Some(issue) = section_heading_mismatch_issue(entity, schema, field) {
                        issues.push(issue);
                    } else {
                        issues.push(HealthIssue {
                            field: field.clone(),
                            code: super::HealthIssueCode::Missing,
                            message: format!("required section '{field}' is empty"),
                        });
                    }
                }
            } else {
                // It's a metadata field. Treat missing AND empty /
                // whitespace-only values as gaps so the scan matches
                // the section branch's `trim().is_empty()` semantics
                // — an empty `MetadataValue::String("")` is just as
                // unhelpful to an agent as an absent key.
                let value = entity.metadata.get(field.as_str());
                let is_empty = match value {
                    None => true,
                    Some(v) => v.to_frontmatter_string().trim().is_empty(),
                };
                if is_empty {
                    issues.push(HealthIssue {
                        field: field.clone(),
                        code: super::HealthIssueCode::Missing,
                        message: format!("required field '{field}' is missing"),
                    });
                }
            }
        }

        // The heading-mismatch condition is drift worth surfacing on
        // every declared section, not only the health-required ones.
        for s in schema.sections.iter().filter(|s| !s.catch_all) {
            if schema.health_required_fields.contains(&s.key) {
                continue; // already handled above
            }
            let content = entity.sections.get(s.key.as_str());
            if content.is_none_or(|c| c.trim().is_empty())
                && let Some(issue) = section_heading_mismatch_issue(entity, schema, &s.key)
            {
                issues.push(issue);
            }
        }

        // Undeclared-relationship warning. Scan the entity's
        // relationship list against the mem's schema vocabulary; every
        // unknown name becomes a soft HealthIssue (same severity as a
        // missing section) so agents running a health sweep after a
        // schema version bump see drift without a crashed load.
        //
        // Shape-violation scan: when the mem's schema declares
        // `source_types` / `target_types` on a relationship and an
        // existing edge violates the shape, surface as a soft
        // HealthIssue. The relate-add path enforces shape going
        // forward; this scan catches edges authored before the
        // constraint landed (or via inline `relations:` on
        // memstead_create, which does not yet shape-check). The
        // remove-path on `memstead_relate` skips shape validation so the
        // cleanup is always reachable.
        if let Some(mem_schema) = mem_schemas.get(entity.mem.as_str()) {
            let mut seen_unknown = std::collections::HashSet::new();
            for rel in &entity.relationships {
                if !mem_schema.relationship_known(&rel.rel_type) {
                    if seen_unknown.insert(rel.rel_type.clone()) {
                        let suggestion = mem_schema
                            .suggest_relationship(&rel.rel_type)
                            .map(|s| format!(" Did you mean '{s}'?"))
                            .unwrap_or_default();
                        let (schema_name, schema_version) = mem_schema.id();
                        issues.push(HealthIssue {
                            field: "relationships".to_string(),
                            code: super::HealthIssueCode::UndeclaredRelationship,
                            message: format!(
                                "relationship '{}' is not declared in schema \
                                 '{schema_name}@{schema_version}'.{suggestion}",
                                rel.rel_type
                            ),
                        });
                    }
                    continue;
                }

                let target_type = store
                    .get(&rel.target)
                    .map(|t| t.entity_type.clone())
                    .filter(|t| !t.is_empty());
                if let Err(crate::runtime_validator::ValidationError::InvalidRelationshipShape {
                    rel_type,
                    from_type,
                    to_type,
                    allowed_source_types,
                    allowed_target_types,
                    ..
                }) = crate::runtime_validator::validate_rel_shape(
                    &rel.rel_type,
                    entity.entity_type.as_str(),
                    target_type.as_deref(),
                    mem_schema.as_ref(),
                ) {
                    let allowed_src = if allowed_source_types.is_empty() {
                        "<any>".to_string()
                    } else {
                        allowed_source_types.join(", ")
                    };
                    let allowed_tgt = if allowed_target_types.is_empty() {
                        "<any>".to_string()
                    } else {
                        allowed_target_types.join(", ")
                    };
                    issues.push(HealthIssue {
                        field: "relationships".to_string(),
                        code: super::HealthIssueCode::InvalidRelShape,
                        message: format!(
                            "INVALID_REL_SHAPE: edge '{rel_type}' from \
                             '{from_type}' to '{to_type}' (target {target}) \
                             violates declared shape — allowed_source_types: \
                             [{allowed_src}], allowed_target_types: \
                             [{allowed_tgt}]. Remove via \
                             `memstead_relate from={from_id} to={target} \
                             type={rel_type} remove=true`.",
                            target = rel.target,
                            from_id = entity.id,
                        ),
                    });
                }
            }
        }

        // Staleness check
        let auto_ts_field = schema.metadata_fields.iter().find(|f| f.auto_timestamp);

        if let Some(ts_field) = auto_ts_field
            && let Some(val) = entity.metadata.get(ts_field.key.as_str())
        {
            let date_str = val.to_frontmatter_string();
            if let Some(modified_days) = parse_iso_to_days(&date_str) {
                let days_since = today_days.saturating_sub(modified_days);
                if days_since > schema.staleness_threshold_days as u64 {
                    stale_entities.push(StaleEntity {
                        id: entity.id.clone(),
                        title: entity.title.clone(),
                        days_since_modified: days_since,
                        anchor_state: None,
                    });
                }
            }
        }

        if !issues.is_empty() {
            // Compute a simple health score: (total_fields - issues) / total_fields.
            // `issues.len()` can exceed `total_fields` once the
            // relationship-vocabulary issues are added on top, so saturate
            // the subtraction rather than underflow. A score of 0.0 is the
            // natural floor — agents treat it as "maximally broken".
            let total = schema.health_required_fields.len();
            let score = if total > 0 {
                (total.saturating_sub(issues.len()) as f32) / (total as f32)
            } else {
                1.0
            };

            missing_fields.push(HealthReport {
                id: entity.id.clone(),
                title: entity.title.clone(),
                score,
                issues,
            });
        }
    }

    // Sort stale entities by days_since_modified descending
    stale_entities.sort_by_key(|e| std::cmp::Reverse(e.days_since_modified));

    // Structural counts — scoped by the same filter as the entity scans
    // above so a `mem`-scoped summary is internally consistent.
    let orphan_count = query::find_orphans_with_schemas(store, mem_schemas)
        .into_iter()
        .filter(|id| store.get(id).is_some_and(|e| in_scope(&e.mem)))
        .count();
    let leaf_entities_by_type = match mem_filter {
        None => query::leaf_population(store, mem_schemas),
        Some(v) => {
            let scoped: HashMap<String, Arc<Schema>> = mem_schemas
                .iter()
                .filter(|(mem, _)| mem.as_str() == v)
                .map(|(mem, s)| (mem.clone(), s.clone()))
                .collect();
            query::leaf_population(store, &scoped)
        }
    };
    let stub_count = query::find_stubs(store)
        .iter()
        .filter(|(id, _)| store.get(id).is_some_and(|e| in_scope(&e.mem)))
        .count();

    // The store iterates a hash map: order the per-entity lists by id so
    // two processes (the CLI and the MCP server) render the same bytes.
    stale_entities.sort_by(|a, b| a.id.0.cmp(&b.id.0));
    missing_fields.sort_by(|a, b| a.id.0.cmp(&b.id.0));

    HealthSummary {
        stale_entities,
        anchor_fresh: Vec::new(),
        missing_fields,
        orphan_count,
        stub_count,
        warnings: Vec::new(),
        quarantined: Vec::new(),
        load_errors: Vec::new(),
        boot_diagnosis: None,
        leaf_entities_by_type,
        dangling_links: None,
        findings: None,
        tag_distribution: None,
        tag_distribution_folded: None,
        untagged_entities: None,
    }
}

/// Scan every non-stub entity's `tags` metadata and aggregate (tag → count,
/// per-entity-type breakdown) plus untagged coverage. Comma-separated parser
/// with per-segment trim; empty segments drop. Comparison is case-sensitive
/// on the primary surface — case drift is surfaced separately via
/// [`TagDistribution`] siblings folded by the caller if desired.
///
/// `mem_filter` narrows both aggregation passes to entities in that mem;
/// `limit` caps the returned `tag_distribution` array after sorting by count
/// descending (tie-break by tag ascending for deterministic output).
///
/// Also returns `FoldedTag` entries for any canonical (lowercase) tag where
/// two or more authored casings appear — drift-flag only; empty when no
/// collisions exist.
pub fn collect_tag_distribution(
    store: &Store,
    mem_filter: Option<&str>,
    limit: usize,
) -> (Vec<TagDistribution>, Vec<FoldedTag>, UntaggedStats) {
    // tag → (count, per_type_count)
    let mut counts: HashMap<String, (usize, HashMap<String, usize>)> = HashMap::new();
    let mut untagged = UntaggedStats {
        total: 0,
        by_entity_type: HashMap::new(),
    };

    for entity in store.all_entities() {
        if entity.stub {
            continue;
        }
        if let Some(v) = mem_filter
            && entity.mem != v
        {
            continue;
        }

        let tags_raw = entity
            .metadata
            .get("tags")
            .and_then(|v| match v {
                MetadataValue::String(s) => Some(s.as_str()),
                _ => None,
            })
            .unwrap_or("");

        let mut any_tag = false;
        for tag in tags_raw.split(',').map(str::trim).filter(|s| !s.is_empty()) {
            any_tag = true;
            let entry = counts
                .entry(tag.to_string())
                .or_insert_with(|| (0, HashMap::new()));
            entry.0 += 1;
            *entry.1.entry(entity.entity_type.clone()).or_insert(0) += 1;
        }
        if !any_tag {
            untagged.total += 1;
            *untagged
                .by_entity_type
                .entry(entity.entity_type.clone())
                .or_insert(0) += 1;
        }
    }

    // Primary distribution — case-sensitive.
    let mut entries: Vec<TagDistribution> = counts
        .iter()
        .map(|(tag, (count, by_type))| TagDistribution {
            tag: tag.clone(),
            count: *count,
            by_entity_type: by_type.clone(),
        })
        .collect();
    entries.sort_by(|a, b| b.count.cmp(&a.count).then_with(|| a.tag.cmp(&b.tag)));
    entries.truncate(limit);

    // Case-drift sidecar: group by lowercase canonical; surface only entries
    // with ≥2 distinct authored casings. Operates on the full counts map, not
    // the truncated primary surface, so drift hidden below `limit` still
    // surfaces.
    let mut by_canonical: HashMap<String, Vec<(String, usize)>> = HashMap::new();
    for (tag, (count, _)) in counts.iter() {
        by_canonical
            .entry(tag.to_lowercase())
            .or_default()
            .push((tag.clone(), *count));
    }
    let mut folded: Vec<FoldedTag> = by_canonical
        .into_iter()
        .filter(|(_, v)| v.len() > 1)
        .map(|(canonical, mut variants)| {
            variants.sort_by(|a, b| b.1.cmp(&a.1).then_with(|| a.0.cmp(&b.0)));
            let total = variants.iter().map(|(_, c)| *c).sum();
            FoldedTag {
                canonical,
                total,
                variants: variants
                    .into_iter()
                    .map(|(tag, count)| TagVariant { tag, count })
                    .collect(),
            }
        })
        .collect();
    folded.sort_by(|a, b| {
        b.total
            .cmp(&a.total)
            .then_with(|| a.canonical.cmp(&b.canonical))
    });

    (entries, folded, untagged)
}

/// Collect the three separately-repaired conditions the diagnostic
/// covers, each tagged with its own [`DanglingLinkKind`]:
///
/// - `LinkTargetMissing` — a body wiki-link whose target has no markdown
///   file: either absent from the store entirely, or present only as a
///   stub. Repair: write the entity, or drop the link.
/// - `LinkNotRelated` — a body wiki-link to a fully written entity that
///   the referrer's relationships list omits (an alias orphan under the
///   alias model). The target is fine; the row is missing. Repair: a
///   write of the referrer re-synthesises the row.
/// - `RelationTargetMissing` — a `## Relationships` row naming an entity
///   absent from the store entirely, not even as a stub. Auto-stubbing
///   means this only arises from out-of-band file edits or historical
///   cross-mem corruption (an earlier mem-delete path). Repair: restore
///   the target or remove the edge.
///
/// The three used to share one code, and only the first was identifiable
/// from the payload; the other two were told apart, if at all, by
/// whether `section` happened to be null. `section` still marks the
/// source axis (`None` for the relationship table), but the condition is
/// named, not inferred.
///
/// `mem_filter` narrows *scanning* to entities in that mem; resolution
/// stays global so cross-mem links whose target is a real entity
/// elsewhere are not flagged as missing.
pub fn collect_dangling_links(store: &Store, mem_filter: Option<&str>) -> Vec<DanglingLink> {
    use crate::entity::parser::extract_inline_links_lenient;
    use std::collections::HashSet;

    let mut out = Vec::new();
    for entity in store.all_entities() {
        if entity.stub {
            continue;
        }
        if let Some(v) = mem_filter
            && entity.mem != v
        {
            continue;
        }
        let explicit_targets: HashSet<_> = entity
            .relationships
            .iter()
            .map(|r| r.target.clone())
            .collect();
        for (section_key, section_body) in &entity.sections {
            for target_id in extract_inline_links_lenient(section_body, &entity.mem) {
                let target_missing = store.get(&target_id).map(|e| e.stub).unwrap_or(true);
                let alias_orphan = !target_missing && !explicit_targets.contains(&target_id);
                // The one place the three conditions are distinguished, so the
                // one place the discriminator is set. Consumers read `kind`;
                // none re-derives it against the store (04/06).
                let kind = if target_missing {
                    crate::ops::DanglingLinkKind::LinkTargetMissing
                } else {
                    crate::ops::DanglingLinkKind::LinkNotRelated
                };
                if target_missing || alias_orphan {
                    out.push(DanglingLink {
                        kind,
                        from: entity.id.clone(),
                        target_id: target_id.clone(),
                        target_path: target_id.path().to_string(),
                        section: Some(section_key.clone()),
                    });
                }
            }
        }
        // Relationship-table dangler scan. The `## Relationships`
        // section is structurally distinct from body sections — its
        // rows materialise from `entity.relationships` rather than a
        // free-text body — so `section: None` marks the source axis.
        //
        // Discrimination differs from the body scan: a relationship
        // target that resolves to a stub is a legitimate forward
        // reference (the alias machinery auto-stubs absent targets
        // by design), not corruption. Only a target that's *fully
        // absent* from the store — neither stub nor real — flags as
        // dangling. In practice this only fires for out-of-band file
        // edits or historical cross-mem-delete corruption that
        // dropped the stub along with the deleted mem.
        //
        // Dedup against the body-scan output so a target that
        // surfaces from both axes doesn't double-emit.
        for rel in &entity.relationships {
            if store.get(&rel.target).is_some() {
                continue;
            }
            let already_reported = out
                .iter()
                .any(|d| d.from == entity.id && d.target_id == rel.target);
            if already_reported {
                continue;
            }
            out.push(DanglingLink {
                kind: crate::ops::DanglingLinkKind::RelationTargetMissing,
                from: entity.id.clone(),
                target_id: rel.target.clone(),
                target_path: rel.target.path().to_string(),
                section: None,
            });
        }
    }
    // Deterministic output — the store iterates a HashMap, so without a
    // sort two identical runs can serve the same findings in different
    // orders. Sort by (from, target, section) so successive sweeps diff
    // cleanly.
    out.sort_by(|a, b| {
        (&a.from.0, &a.target_id.0, &a.section).cmp(&(&b.from.0, &b.target_id.0, &b.section))
    });
    out
}

/// Collect every non-stub entity whose type declares `required_outgoing`
/// blocks that the entity's current outgoing edges leave unsatisfied.
/// Results are deterministic — sorted
/// by `(mem, id)` — so the agent can diff successive sweeps without
/// the underlying HashMap iteration order leaking through.
///
/// `mem_filter` narrows scanning to entities in that mem when set;
/// `mem_schemas` resolves the entity's type definition against the
/// mem's pinned schema. Entities whose mem has no schema in the
/// map are skipped (no schema → no `required_outgoing` to evaluate).
pub fn collect_missing_required_outgoing(
    store: &Store,
    mem_filter: Option<&str>,
    mem_schemas: &HashMap<String, Arc<memstead_schema::Schema>>,
) -> Vec<MissingRequiredOutgoingReport> {
    let mut out = Vec::new();
    for entity in store.all_entities() {
        if entity.stub {
            continue;
        }
        if let Some(v) = mem_filter
            && entity.mem != v
        {
            continue;
        }
        let Some(mem_schema) = mem_schemas.get(entity.mem.as_str()) else {
            continue;
        };
        let Some(td) = mem_schema.types.get(entity.entity_type.as_str()) else {
            continue;
        };
        if td.required_outgoing.is_empty() {
            continue;
        }
        let unsatisfied = unsatisfied_required_outgoing(entity, td);
        if unsatisfied.is_empty() {
            continue;
        }
        out.push(MissingRequiredOutgoingReport {
            id: entity.id.clone(),
            title: entity.title.clone(),
            entity_type: entity.entity_type.clone(),
            mem: entity.mem.clone(),
            missing: unsatisfied,
        });
    }
    out.sort_by(|a, b| a.mem.cmp(&b.mem).then_with(|| a.id.0.cmp(&b.id.0)));
    out
}

/// Evaluate one entity's declared `required_outgoing` blocks against
/// its current outgoing edges, returning the unsatisfied blocks in
/// declaration order. THE single evaluation — shared by the health
/// sweep ([`collect_missing_required_outgoing`]) and the per-mutation
/// `MISSING_REQUIRED_OUTGOING` warning on create/update. A second
/// implementation of the block check is a defect: the two surfaces
/// must never disagree about what counts as unsatisfied.
pub fn unsatisfied_required_outgoing(
    entity: &crate::entity::Entity,
    td: &TypeDefinition,
) -> Vec<super::MissingRequiredOutgoingBlock> {
    td.required_outgoing
        .iter()
        .filter(|block| {
            // A conditional block applies only while `when_field`
            // holds `when_value` (same comparison the `requires_when`
            // constraint uses). Unset field or any other value = the
            // block is unarmed and never unsatisfied.
            if let (Some(when_field), Some(when_value)) = (&block.when_field, &block.when_value) {
                let armed = entity
                    .metadata
                    .get(when_field.as_str())
                    .is_some_and(|v| v.to_frontmatter_string() == *when_value);
                if !armed {
                    return false;
                }
            }
            let count = entity
                .relationships
                .iter()
                .filter(|rel| block.relationships.iter().any(|name| name == &rel.rel_type))
                .count();
            !block.admits(count)
        })
        .map(|block| super::MissingRequiredOutgoingBlock {
            relationships: block.relationships.clone(),
            cardinality: block.cardinality.to_string(),
            severity: block.severity,
            when_field: block.when_field.clone(),
            when_value: block.when_value.clone(),
        })
        .collect()
}

/// One violated declared constraint on one entity — the wire entry
/// shared by the write-path surface (the `CONSTRAINT_UNSATISFIED`
/// warning or refusal, tier decided by the declared severity) and the
/// health `constraints` include. The serde `kind` tag names the form;
/// the remaining fields restate the declaration (plus the observed
/// offense — the colliding entity, the unbacked value, the tainting
/// ancestor) so a consumer can repair without re-fetching the schema.
#[derive(Debug, Clone, serde::Serialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum UnsatisfiedConstraint {
    RequiresWhen {
        field: String,
        when_field: String,
        when_value: String,
        severity: memstead_schema::ConstraintSeverity,
    },
    Unique {
        fields: Vec<String>,
        /// The entity's values for `fields`, in declaration order.
        values: Vec<String>,
        /// The other entity holding the same tuple (lexically smallest
        /// when several collide).
        colliding: String,
        severity: memstead_schema::ConstraintSeverity,
    },
    EnumFromNeighbour {
        field: String,
        /// The set value no reached neighbour's section backs.
        value: String,
        relationships: Vec<String>,
        section: String,
        severity: memstead_schema::ConstraintSeverity,
    },
    StatusPropagation {
        field: String,
        /// The terminal value the ancestor holds.
        value: String,
        /// The relation set the taint travels along.
        relationships: Vec<String>,
        /// The tainting ancestor — the entity holding the terminal
        /// value that this entity (transitively) reaches.
        tainted_by: String,
        /// The chain of ids the taint travelled, from `tainted_by` first
        /// to this entity last, one shortest path (a stub on the way is
        /// named: it carried the edge). A reader of a finding on an
        /// entity with several parents can say through which parent the
        /// fall arrived instead of guessing; with several fallen sources
        /// each finding carries its own path.
        path: Vec<String>,
        severity: memstead_schema::ConstraintSeverity,
    },
    /// Form 6 — the entity holds the gated `to_value` while related
    /// entities lack a fresh confirming check record.
    TransitionRequiresChecks {
        field: String,
        to_value: String,
        relationships: Vec<String>,
        direction: memstead_schema::ReachDirection,
        /// Every related entity NOT at derived state `checked_ok`,
        /// each with the state it derived, sorted by id.
        unchecked: Vec<UncheckedRelated>,
        /// How many related entities the declared edges reach.
        related: usize,
        /// The declared floor; a violation with `related < min_related`
        /// is the vacuous case the floor exists to refuse.
        min_related: usize,
        severity: memstead_schema::ConstraintSeverity,
    },
    /// Form 7 — the entity holds the gated `to_value` while it carries
    /// no fresh, independent, confirming check record of `check_kind`.
    TransitionRequiresSelfCheck {
        field: String,
        to_value: String,
        check_kind: String,
        /// The standing the entity's own record derived instead of an
        /// independent `checked_ok` (`never_checked`, `check_stale`,
        /// `check_failed`, `self_checked`, `unconfirmable`).
        state: String,
        /// On a refused transition write: what the write changed beside
        /// the gated field and the engine-stamped timestamps
        /// (`section:<key>`, `metadata:<key>`, `title`, `type`,
        /// `relationships`). A self-check transition write may change
        /// nothing else, because the record it rests on checked the
        /// content as stored. Empty on the health axis.
        #[serde(default, skip_serializing_if = "Vec::is_empty")]
        changed: Vec<String>,
        severity: memstead_schema::ConstraintSeverity,
    },
    /// The entity reaches no non-stub entity of a terminal type along
    /// the declared relation set — the declaration is echoed whole so
    /// the reader sees which obligation went unmet without re-fetching
    /// the schema. Health-sweep only, always warn-tier.
    MustReach {
        relationships: Vec<String>,
        direction: memstead_schema::ReachDirection,
        terminal_types: Vec<String>,
        #[serde(skip_serializing_if = "Option::is_none")]
        max_depth: Option<u32>,
        severity: memstead_schema::ConstraintSeverity,
    },
}

impl UnsatisfiedConstraint {
    pub fn severity(&self) -> memstead_schema::ConstraintSeverity {
        match self {
            Self::RequiresWhen { severity, .. }
            | Self::Unique { severity, .. }
            | Self::EnumFromNeighbour { severity, .. }
            | Self::StatusPropagation { severity, .. }
            | Self::MustReach { severity, .. }
            | Self::TransitionRequiresChecks { severity, .. }
            | Self::TransitionRequiresSelfCheck { severity, .. } => *severity,
        }
    }

    /// One-line human rendering for warning/refusal message text.
    pub fn describe(&self) -> String {
        match self {
            Self::RequiresWhen {
                field,
                when_field,
                when_value,
                ..
            } => format!(
                "requires_when: '{field}' is required when {when_field}={when_value} and is unset"
            ),
            Self::Unique {
                fields, colliding, ..
            } => format!(
                "unique: tuple ({}) collides with '{colliding}'",
                fields.join(", ")
            ),
            Self::EnumFromNeighbour {
                field,
                value,
                relationships,
                section,
                ..
            } => format!(
                "enum_from_neighbour: '{field}' value '{value}' has no backing entry in any \
                 `{section}` section reached via {}",
                relationships.join(", ")
            ),
            Self::StatusPropagation {
                field,
                value,
                tainted_by,
                path,
                ..
            } => {
                format!(
                    "status_propagation: tainted by '{tainted_by}' ({field}={value}) via {}",
                    path.join(" → ")
                )
            }
            Self::MustReach {
                relationships,
                direction,
                terminal_types,
                max_depth,
                ..
            } => {
                let depth = match max_depth {
                    Some(d) => format!(" within {d} hop(s)"),
                    None => String::new(),
                };
                format!(
                    "must_reach: no path via [{}] ({direction}) reaches a [{}] entity{depth}",
                    relationships.join(", "),
                    terminal_types.join(", ")
                )
            }
            Self::TransitionRequiresChecks {
                field,
                to_value,
                relationships,
                unchecked,
                related,
                min_related,
                ..
            } => {
                if related < min_related {
                    return format!(
                        "transition_requires_checks: {field}={to_value} requires at least \
                         {min_related} related entit{} via [{}] — found {related}",
                        if *min_related == 1 { "y" } else { "ies" },
                        relationships.join(", ")
                    );
                }
                let listed: Vec<String> = unchecked
                    .iter()
                    .map(|u| format!("'{}' ({})", u.id, u.state))
                    .collect();
                format!(
                    "transition_requires_checks: {field}={to_value} requires a fresh confirming \
                     check record on every entity related via [{}] — unconfirmed: {}",
                    relationships.join(", "),
                    listed.join(", ")
                )
            }
            Self::TransitionRequiresSelfCheck {
                field,
                to_value,
                check_kind,
                state,
                changed,
                ..
            } => {
                let mut m = format!(
                    "transition_requires_self_check: {field}={to_value} requires a fresh confirming \
                     `{check_kind}` check record on this entity under an identity other than its \
                     author — derived: {state}"
                );
                if !changed.is_empty() {
                    m.push_str(&format!(
                        "; the write also changes {}, and a self-check transition write may \
                         change nothing but `{field}`: write those changes first, check the \
                         result, then set `{field}`",
                        changed.join(", ")
                    ));
                }
                m
            }
        }
    }
}

/// One related entity blocking a `transition_requires_checks` gate:
/// its id and the derived verification state it reads instead of the
/// required `checked_ok`.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize)]
pub struct UncheckedRelated {
    pub id: String,
    pub state: String,
}

/// The gated-transition evaluators' window into the check ledger:
/// the derived standing of one entity's newest check record of one
/// wire kind (`verification`, `conformance`, or a foreign `x-<name>`).
/// Callers with an engine build it from the workspace check ledger;
/// `None` (no ledger access — a workspace-less engine, or a call path
/// that cannot reach one) derives every entity as `never_checked`, so
/// a declared gate refuses honestly rather than passing unverified.
pub type CheckStateProvider<'a> =
    &'a dyn Fn(&crate::entity::Entity, &str) -> crate::engine::independence::CheckStanding;

/// Evaluate one entity's declared per-entity `constraints` against its
/// current state (and, for the store-aware forms, against the rest of
/// its mem), returning the violated ones in declaration order. THE
/// single evaluation — shared by the health sweep
/// ([`collect_constraint_findings`]) and the per-mutation
/// `CONSTRAINT_UNSATISFIED` surface on create/update/relate; a second
/// implementation of any form is a defect.
///
/// Form semantics:
/// - `requires_when` triggers when `when_field`'s frontmatter value
///   equals `when_value` exactly; a triggered constraint is satisfied
///   when `field` — a metadata field or a section key — is present
///   with non-blank content.
/// - `unique`: the entity's tuple of `fields` values (skipped when any
///   field is unset/blank) must not equal another non-stub entity's
///   tuple within the same mem and type. `exclude` names the entity's
///   own id so an update does not collide with its stored self.
/// - `enum_from_neighbour`: a set `field` value must appear as a
///   bullet entry (`- value` / `* value` line) in the `section` body
///   of at least one entity reached via an outgoing edge of one of `relationships`.
/// - `status_propagation` is a reachability property of the graph,
///   not of one write — it is evaluated only by the health sweep
///   ([`collect_constraint_findings`]), never here.
pub fn unsatisfied_constraints(
    store: &Store,
    entity: &crate::entity::Entity,
    td: &TypeDefinition,
    exclude: Option<&crate::entity::EntityId>,
    checks: Option<CheckStateProvider<'_>>,
) -> Vec<UnsatisfiedConstraint> {
    use memstead_schema::ConstraintDef;
    td.constraints
        .iter()
        .filter_map(|c| match c {
            ConstraintDef::RequiresWhen {
                field,
                when_field,
                when_value,
                severity,
            } => {
                let triggered = entity
                    .metadata
                    .get(when_field.as_str())
                    .is_some_and(|v| v.to_frontmatter_string() == *when_value);
                if !triggered {
                    return None;
                }
                let satisfied = entity
                    .metadata
                    .get(field.as_str())
                    .is_some_and(|v| !v.to_frontmatter_string().trim().is_empty())
                    || entity
                        .sections
                        .get(field.as_str())
                        .is_some_and(|body| !body.trim().is_empty());
                if satisfied {
                    return None;
                }
                Some(UnsatisfiedConstraint::RequiresWhen {
                    field: field.clone(),
                    when_field: when_field.clone(),
                    when_value: when_value.clone(),
                    severity: *severity,
                })
            }
            ConstraintDef::Unique { fields, severity } => {
                let tuple = tuple_of(entity, fields)?;
                let mut colliding: Vec<&str> = store
                    .all_entities()
                    .filter(|other| {
                        !other.stub
                            && other.mem == entity.mem
                            && other.entity_type == entity.entity_type
                            && Some(&other.id) != exclude
                            && other.id != entity.id
                            && tuple_of(other, fields).as_ref() == Some(&tuple)
                    })
                    .map(|other| other.id.0.as_str())
                    .collect();
                colliding.sort_unstable();
                let first = colliding.first()?;
                Some(UnsatisfiedConstraint::Unique {
                    fields: fields.clone(),
                    values: tuple,
                    colliding: first.to_string(),
                    severity: *severity,
                })
            }
            ConstraintDef::EnumFromNeighbour {
                field,
                relationships,
                section,
                severity,
            } => {
                let value = entity
                    .metadata
                    .get(field.as_str())
                    .map(|v| v.to_frontmatter_string())
                    .filter(|v| !v.trim().is_empty())?;
                let backed = entity
                    .relationships
                    .iter()
                    .filter(|rel| relationships.contains(&rel.rel_type))
                    .filter_map(|rel| store.get(&rel.target))
                    .filter_map(|neighbour| neighbour.sections.get(section.as_str()))
                    .any(|body| bullet_entries(body).contains(&value));
                if backed {
                    return None;
                }
                Some(UnsatisfiedConstraint::EnumFromNeighbour {
                    field: field.clone(),
                    value,
                    relationships: relationships.clone(),
                    section: section.clone(),
                    severity: *severity,
                })
            }
            ConstraintDef::StatusPropagation { .. } => None,
            ConstraintDef::TransitionRequiresChecks {
                field,
                to_value,
                relationships,
                direction,
                min_related,
                severity,
            } => {
                let triggered = entity
                    .metadata
                    .get(field.as_str())
                    .is_some_and(|v| v.to_frontmatter_string() == *to_value);
                if !triggered {
                    return None;
                }
                let (related, unchecked) = transition_gate_standing(
                    store,
                    entity,
                    relationships,
                    *direction,
                    exclude,
                    checks,
                );
                if unchecked.is_empty() && related >= *min_related {
                    return None;
                }
                Some(UnsatisfiedConstraint::TransitionRequiresChecks {
                    field: field.clone(),
                    to_value: to_value.clone(),
                    relationships: relationships.clone(),
                    direction: *direction,
                    unchecked,
                    related,
                    min_related: *min_related,
                    severity: *severity,
                })
            }
            ConstraintDef::TransitionRequiresSelfCheck {
                field,
                to_value,
                check_kind,
                severity,
            } => {
                let triggered = entity
                    .metadata
                    .get(field.as_str())
                    .is_some_and(|v| v.to_frontmatter_string() == *to_value);
                if !triggered {
                    return None;
                }
                // The entity's own record of the declared kind, read
                // through the same provider form 6 reads related
                // entities with: no ledger derives never_checked, the
                // author's own ok reads self_checked, neither confirms.
                let standing = match checks {
                    Some(provider) => provider(entity, check_kind),
                    None => crate::engine::independence::CheckStanding {
                        state: crate::check::CheckState::NeverChecked,
                        independence: None,
                        role: crate::vcs::Role::Unspecified,
                    },
                };
                if standing.confirms() {
                    return None;
                }
                Some(UnsatisfiedConstraint::TransitionRequiresSelfCheck {
                    field: field.clone(),
                    to_value: to_value.clone(),
                    check_kind: check_kind.clone(),
                    state: standing.label().to_string(),
                    changed: Vec::new(),
                    severity: *severity,
                })
            }
        })
        .collect()
}

/// The standing of one gated-transition constraint's related set
/// against one entity, independent of whether the entity currently
/// holds the gated value: `(total related, those lacking a fresh
/// confirming check record)`, the unconfirmed sorted by id. THE single
/// related-set enumeration — shared by the write-time evaluator arm
/// above and the gates-brief renderer, so the brief can never disagree
/// with the refusal. Outgoing reads the entity's own edges (its
/// written state); incoming scans the store for edge sources pointing
/// at it, excluding the entity's own stored copy (`exclude`) so an
/// update never gates on its pre-write self.
pub fn transition_gate_standing(
    store: &Store,
    entity: &crate::entity::Entity,
    relationships: &[String],
    direction: memstead_schema::ReachDirection,
    exclude: Option<&crate::entity::EntityId>,
    checks: Option<CheckStateProvider<'_>>,
) -> (usize, Vec<UncheckedRelated>) {
    let related: Vec<&crate::entity::Entity> = match direction {
        memstead_schema::ReachDirection::Out => entity
            .relationships
            .iter()
            .filter(|rel| relationships.contains(&rel.rel_type))
            .filter_map(|rel| store.get(&rel.target))
            .collect(),
        memstead_schema::ReachDirection::In => store
            .all_entities()
            .filter(|other| {
                other.id != entity.id
                    && Some(&other.id) != exclude
                    && other
                        .relationships
                        .iter()
                        .any(|rel| rel.target == entity.id && relationships.contains(&rel.rel_type))
            })
            .collect(),
    };
    let total = related.len();
    let mut unchecked: Vec<UncheckedRelated> = related
        .into_iter()
        .filter_map(|rel_entity| {
            // An ok check confirms only when it is independent of the
            // executors (engine::independence): the executor's own ok
            // reads `self_checked` here and does not close the gate.
            let standing = match checks {
                Some(provider) => {
                    provider(rel_entity, crate::check::CheckKind::Verification.as_str())
                }
                None => crate::engine::independence::CheckStanding {
                    state: crate::check::CheckState::NeverChecked,
                    independence: None,
                    role: crate::vcs::Role::Unspecified,
                },
            };
            if standing.confirms() {
                None
            } else {
                Some(UncheckedRelated {
                    id: rel_entity.id.0.clone(),
                    state: standing.label().to_string(),
                })
            }
        })
        .collect();
    unchecked.sort_by(|a, b| a.id.cmp(&b.id));
    (total, unchecked)
}

/// The entity's tuple of frontmatter values for `fields`, in
/// declaration order — `None` when any field is unset or blank (no
/// tuple, nothing to compare).
fn tuple_of(entity: &crate::entity::Entity, fields: &[String]) -> Option<Vec<String>> {
    fields
        .iter()
        .map(|f| {
            entity
                .metadata
                .get(f.as_str())
                .map(|v| v.to_frontmatter_string())
                .filter(|v| !v.trim().is_empty())
        })
        .collect()
}

/// The bullet entries of a section body — trimmed text of `- item` /
/// `* item` lines. The legal-value shape `enum_from_neighbour` reads.
fn bullet_entries(body: &str) -> Vec<String> {
    // A bullet inside a code block is an example of the list, not a
    // member of it — the same referee every other content reader uses
    // ([`crate::markdown`]). Masking preserves byte offsets and line
    // count, so each masked line pairs with its original.
    let masked = crate::markdown::mask_code_blocks_and_spans(body);
    body.lines()
        .zip(masked.lines())
        .filter_map(|(line, masked_line)| {
            let m = masked_line.trim_start();
            if m.starts_with("- ") || m.starts_with("* ") {
                let t = line.trim_start();
                t.strip_prefix("- ")
                    .or_else(|| t.strip_prefix("* "))
                    .map(|e| e.trim().to_string())
            } else {
                None
            }
        })
        .collect()
}

/// One entity's violated declared constraints, surfaced from the
/// health-time scan (`include=["constraints"]`). Mirrors
/// [`MissingRequiredOutgoingReport`]'s envelope shape — the two
/// includes read the same way.
#[derive(Debug, Clone, serde::Serialize)]
pub struct ConstraintFindingReport {
    pub id: crate::entity::EntityId,
    pub title: String,
    pub entity_type: String,
    pub mem: String,
    pub violations: Vec<UnsatisfiedConstraint>,
    /// Standing violations of the entity's declared section formats
    /// (additive: consumers of the pre-format shape see an
    /// absent key, never an empty list.
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub format_violations: Vec<crate::section_format::SectionFormatViolation>,
}

/// Collect every non-stub entity whose declared `constraints` its
/// current state violates. Two passes: the per-entity forms
/// (`requires_when`, `unique`, `enum_from_neighbour`) through the
/// shared [`unsatisfied_constraints`] evaluation, then the
/// `status_propagation` graph sweep — for each entity holding a
/// declared terminal value, every entity reaching it (transitively)
/// via the declared rel-type and direction gains a finding naming that
/// tainting ancestor. Deterministic — reports sorted by `(mem, id)`,
/// violations in declaration order then by tainting ancestor.
pub fn collect_constraint_findings(
    store: &Store,
    mem_filter: Option<&str>,
    mem_schemas: &HashMap<String, Arc<memstead_schema::Schema>>,
    checks: Option<CheckStateProvider<'_>>,
) -> Vec<ConstraintFindingReport> {
    use memstead_schema::ConstraintDef;
    type Bucket = (
        Vec<UnsatisfiedConstraint>,
        Vec<crate::section_format::SectionFormatViolation>,
    );
    let mut by_entity: std::collections::BTreeMap<String, Bucket> = Default::default();

    // Reverse adjacency for `must_reach` incoming walks — built once
    // per sweep, and only when some pinned schema declares one (a
    // workspace without the form pays nothing).
    let needs_reverse = mem_schemas.values().any(|s| {
        s.types.values().any(|t| {
            t.must_reach
                .iter()
                .any(|ob| ob.direction == memstead_schema::ReachDirection::In)
        })
    });
    let reverse: ReverseIndex = if needs_reverse {
        build_reverse_index(store)
    } else {
        ReverseIndex::default()
    };

    for entity in store.all_entities() {
        if entity.stub {
            continue;
        }
        if let Some(v) = mem_filter
            && entity.mem != v
        {
            continue;
        }
        let Some(mem_schema) = mem_schemas.get(entity.mem.as_str()) else {
            continue;
        };
        let Some(td) = mem_schema.types.get(entity.entity_type.as_str()) else {
            continue;
        };

        // Section-format sweep — standing violations of a
        // declared markdown shape, every severity (block-tier
        // pre-existing violations are health findings too; the next
        // write of the section is the sanctioned repair point).
        for def in &td.sections {
            if def.compiled_content.is_none() {
                continue;
            }
            let Some(body) = entity.sections.get(def.key.as_str()) else {
                continue;
            };
            let violations = crate::section_format::check_section_format(def, body);
            if !violations.is_empty() {
                by_entity
                    .entry(entity.id.0.clone())
                    .or_default()
                    .1
                    .extend(violations);
            }
        }

        // Reachability obligations — health-sweep only by design (no
        // single write completes a transitive absence, so the write
        // path never evaluates these). The finding echoes the whole
        // declaration.
        for ob in &td.must_reach {
            if !reaches_terminal(store, &reverse, &entity.id, ob) {
                by_entity.entry(entity.id.0.clone()).or_default().0.push(
                    UnsatisfiedConstraint::MustReach {
                        relationships: ob.relationships.clone(),
                        direction: ob.direction,
                        terminal_types: ob.terminal_types.clone(),
                        max_depth: ob.max_depth,
                        severity: ob.severity,
                    },
                );
            }
        }

        if td.constraints.is_empty() {
            continue;
        }

        // Pass 1 — per-entity forms.
        let violations = unsatisfied_constraints(store, entity, td, None, checks);
        if !violations.is_empty() {
            by_entity
                .entry(entity.id.0.clone())
                .or_default()
                .0
                .extend(violations);
        }
    }

    // Pass 2 — every entity as a taint source, whatever mem it lives
    // in: it holds a declared terminal value, so sweep its dependents.
    // The taint walks the declared relation set's union subgraph (a
    // single `rel_type` is a one-element set) and crosses mem
    // boundaries like any walk here; the mem filter narrows which
    // TAINTED entities are reported, never which sources are swept.
    // Folded into the per-entity loop above, the filter skipped a
    // source in another mem before its dependents in the filtered mem
    // were ever computed, so a mem resting on a fallen card elsewhere
    // read clean under `--mem` and tainted without it. Sources are
    // swept in id order, so an entity tainted by several ancestors
    // carries its findings in the same order on every run.
    let mut sources: Vec<&crate::entity::Entity> =
        store.all_entities().filter(|e| !e.stub).collect();
    sources.sort_by(|a, b| a.id.0.cmp(&b.id.0));
    for entity in sources {
        let Some(mem_schema) = mem_schemas.get(entity.mem.as_str()) else {
            continue;
        };
        let Some(td) = mem_schema.types.get(entity.entity_type.as_str()) else {
            continue;
        };
        for c in &td.constraints {
            let ConstraintDef::StatusPropagation {
                field,
                value,
                relationships,
                direction,
                severity,
            } = c
            else {
                continue;
            };
            let terminal = entity
                .metadata
                .get(field.as_str())
                .is_some_and(|v| v.to_frontmatter_string() == *value);
            if !terminal {
                continue;
            }
            for (tainted, path) in reach_transitively(store, &entity.id, relationships, *direction)
            {
                if let Some(v) = mem_filter
                    && tainted.mem() != v
                {
                    continue;
                }
                by_entity.entry(tainted.0.clone()).or_default().0.push(
                    UnsatisfiedConstraint::StatusPropagation {
                        field: field.clone(),
                        value: value.clone(),
                        relationships: relationships.clone(),
                        tainted_by: entity.id.to_string(),
                        path: path.iter().map(ToString::to_string).collect(),
                        severity: *severity,
                    },
                );
            }
        }
    }

    let mut out: Vec<ConstraintFindingReport> = by_entity
        .into_iter()
        .filter_map(|(id, (violations, format_violations))| {
            let id = crate::entity::EntityId(id);
            let entity = store.get(&id)?;
            Some(ConstraintFindingReport {
                id,
                title: entity.title.clone(),
                entity_type: entity.entity_type.clone(),
                mem: entity.mem.clone(),
                violations,
                format_violations,
            })
        })
        .collect();
    out.sort_by(|a, b| a.mem.cmp(&b.mem).then_with(|| a.id.0.cmp(&b.id.0)));
    out
}

/// Transitive reachability along one rel-type from `start`, excluding
/// `start` itself, each reached entity with the path the walk took to
/// it (`start` first, the entity last). `In` walks against edge
/// direction (the entities whose `rel_type` edges point at the frontier
/// — "what stands on this"); `Out` follows the frontier's own
/// edges. Breadth first with neighbours in id order, so the path is one
/// shortest path and the same one on every run. Stubs are traversed (an
/// edge through a stub still transmits the taint) and named on the
/// paths that cross them, but stubs themselves are not returned.
fn reach_transitively(
    store: &Store,
    start: &crate::entity::EntityId,
    rel_types: &[String],
    direction: memstead_schema::ReachDirection,
) -> Vec<(crate::entity::EntityId, Vec<crate::entity::EntityId>)> {
    use memstead_schema::ReachDirection;
    let mut parent: std::collections::HashMap<crate::entity::EntityId, crate::entity::EntityId> =
        Default::default();
    let mut seen: std::collections::HashSet<crate::entity::EntityId> =
        std::iter::once(start.clone()).collect();
    let mut frontier = std::collections::VecDeque::from([start.clone()]);
    let mut reached = Vec::new();
    while let Some(current) = frontier.pop_front() {
        let mut next: Vec<crate::entity::EntityId> = match direction {
            ReachDirection::In => store
                .all_entities()
                .filter(|e| {
                    e.relationships
                        .iter()
                        .any(|r| rel_types.iter().any(|n| n == &r.rel_type) && r.target == current)
                })
                .map(|e| e.id.clone())
                .collect(),
            ReachDirection::Out => store
                .get(&current)
                .map(|e| {
                    e.relationships
                        .iter()
                        .filter(|r| rel_types.iter().any(|n| n == &r.rel_type))
                        .map(|r| r.target.clone())
                        .collect()
                })
                .unwrap_or_default(),
        };
        next.sort_by(|a, b| a.0.cmp(&b.0));
        for id in next {
            if seen.insert(id.clone()) {
                parent.insert(id.clone(), current.clone());
                if store.get(&id).is_some_and(|e| !e.stub) {
                    let mut path = vec![id.clone()];
                    let mut at = id.clone();
                    while let Some(p) = parent.get(&at) {
                        path.push(p.clone());
                        at = p.clone();
                    }
                    path.reverse();
                    reached.push((id.clone(), path));
                }
                frontier.push_back(id);
            }
        }
    }
    reached
}

/// Reverse adjacency for `must_reach` incoming walks: target id →
/// `(rel_type, source id)` pairs. Built once per sweep so the
/// incoming direction stays O(edges) instead of re-scanning the store
/// per frontier node.
type ReverseIndex =
    std::collections::HashMap<crate::entity::EntityId, Vec<(String, crate::entity::EntityId)>>;

fn build_reverse_index(store: &Store) -> ReverseIndex {
    let mut idx = ReverseIndex::default();
    for entity in store.all_entities() {
        for rel in &entity.relationships {
            idx.entry(rel.target.clone())
                .or_default()
                .push((rel.rel_type.clone(), entity.id.clone()));
        }
    }
    idx
}

/// Whether `start` reaches at least one non-stub entity of a terminal
/// type along the obligation's relation set, direction, and depth
/// bound. Breadth-first with visited-set discipline (cycles along the
/// walked set terminate); stubs terminate no obligation — they carry
/// no outgoing edges and never count as reached terminals. Cross-mem
/// edges are followed like any edge, matching the propagation walk
/// and the cycle check (the engine's established traversal posture);
/// the start entity itself never satisfies its own obligation.
fn reaches_terminal(
    store: &Store,
    reverse: &ReverseIndex,
    start: &crate::entity::EntityId,
    ob: &memstead_schema::MustReach,
) -> bool {
    use memstead_schema::ReachDirection;
    let mut seen: std::collections::HashSet<crate::entity::EntityId> =
        std::iter::once(start.clone()).collect();
    let mut frontier = vec![start.clone()];
    let mut depth: u32 = 0;
    while !frontier.is_empty() {
        if let Some(max) = ob.max_depth
            && depth >= max
        {
            return false;
        }
        depth += 1;
        let mut next_frontier = Vec::new();
        for current in frontier {
            let next: Vec<crate::entity::EntityId> = match ob.direction {
                ReachDirection::Out => store
                    .get(&current)
                    .map(|e| {
                        e.relationships
                            .iter()
                            .filter(|r| ob.relationships.iter().any(|n| n == &r.rel_type))
                            .map(|r| r.target.clone())
                            .collect()
                    })
                    .unwrap_or_default(),
                ReachDirection::In => reverse
                    .get(&current)
                    .map(|sources| {
                        sources
                            .iter()
                            .filter(|(rel, _)| ob.relationships.iter().any(|n| n == rel))
                            .map(|(_, src)| src.clone())
                            .collect()
                    })
                    .unwrap_or_default(),
            };
            for id in next {
                if seen.insert(id.clone()) {
                    if store.get(&id).is_some_and(|e| {
                        !e.stub && ob.terminal_types.iter().any(|t| t == &e.entity_type)
                    }) {
                        return true;
                    }
                    next_frontier.push(id);
                }
            }
        }
        frontier = next_frontier;
    }
    false
}

/// One entity's above-`none` signals, surfaced from the include-gated
/// `signals` health axis. Mirrors [`ConstraintFindingReport`]'s
/// envelope shape; the `signals` entries carry value, level, and
/// contributors (the evidence ships with the number, always).
#[derive(Debug, Clone, serde::Serialize)]
pub struct SignalReport {
    pub id: crate::entity::EntityId,
    pub title: String,
    pub entity_type: String,
    pub mem: String,
    /// Only signals whose level is not `none`, in declaration order.
    pub signals: Vec<super::signals::ComputedSignal>,
}

impl SignalReport {
    /// Whether any entry is `warn`-level — the `--strict`
    /// participation test (a `notice` never participates; that is the
    /// whole difference between the two levels).
    pub fn has_warn(&self) -> bool {
        self.signals
            .iter()
            .any(|s| s.level == Some(memstead_schema::SignalLevel::Warn))
    }
}

/// Collect every non-stub entity carrying at least one declared
/// signal above `none`. Deterministic — sorted by `(mem, id)`;
/// signals in declaration order, contributors sorted.
pub fn collect_signal_reports(
    store: &Store,
    mem_filter: Option<&str>,
    mem_schemas: &HashMap<String, Arc<memstead_schema::Schema>>,
) -> Vec<SignalReport> {
    let mut out = Vec::new();
    for entity in store.all_entities() {
        if entity.stub {
            continue;
        }
        if let Some(v) = mem_filter
            && entity.mem != v
        {
            continue;
        }
        let Some(mem_schema) = mem_schemas.get(entity.mem.as_str()) else {
            continue;
        };
        let Some(td) = mem_schema.types.get(entity.entity_type.as_str()) else {
            continue;
        };
        if td.signals.is_empty() {
            continue;
        }
        let above: Vec<super::signals::ComputedSignal> =
            super::signals::compute_signals(store, td, &entity.id)
                .into_iter()
                .filter(|s| s.level.is_some())
                .collect();
        if above.is_empty() {
            continue;
        }
        out.push(SignalReport {
            id: entity.id.clone(),
            title: entity.title.clone(),
            entity_type: entity.entity_type.clone(),
            mem: entity.mem.clone(),
            signals: above,
        });
    }
    out.sort_by(|a, b| a.mem.cmp(&b.mem).then_with(|| a.id.0.cmp(&b.id.0)));
    out
}

/// A defective section-format declaration a loaded schema carries
/// (recorded by the lenient boot path; install would have refused).
/// Surfaced under the health `constraints` include so a sealed schema
/// with a bad declaration is visible without bricking boot.
#[derive(Debug, Clone, serde::Serialize)]
pub struct SchemaFormatDefect {
    pub schema: String,
    pub type_name: String,
    pub section: String,
    pub problems: Vec<String>,
}

/// Collect the defective section-format declarations across the
/// mounted mems' pinned schemas, deduplicated per schema ref,
/// deterministic order.
pub fn collect_schema_format_defects(
    mem_schemas: &HashMap<String, Arc<memstead_schema::Schema>>,
) -> Vec<SchemaFormatDefect> {
    let mut seen: std::collections::BTreeSet<String> = Default::default();
    let mut out = Vec::new();
    let mut schemas: Vec<&Arc<memstead_schema::Schema>> = mem_schemas.values().collect();
    schemas.sort_by_key(|s| (s.manifest.name.clone(), s.version.clone()));
    for schema in schemas {
        let schema_ref = format!("{}@{}", schema.manifest.name, schema.version);
        if !seen.insert(schema_ref.clone()) {
            continue;
        }
        for td in schema.types.values() {
            for section in &td.sections {
                if !section.format_problems.is_empty() {
                    out.push(SchemaFormatDefect {
                        schema: schema_ref.clone(),
                        type_name: td.name.clone(),
                        section: section.key.clone(),
                        problems: section.format_problems.clone(),
                    });
                }
            }
        }
    }
    out.sort_by(|a, b| {
        (&a.schema, &a.type_name, &a.section).cmp(&(&b.schema, &b.type_name, &b.section))
    });
    out
}

/// One entity's unsatisfied `required_outgoing` blocks, surfaced from
/// the health-time scan. `missing` reuses the per-write warning's wire
/// block type — one struct, one serialized shape (`{ relationships,
/// cardinality }`) on both surfaces — and adds the `mem` name (the
/// warning's `entity_id` already encodes it via the mem prefix, but
/// health is multi-mem by default and an explicit field is cheaper for
/// downstream filters).
#[derive(Debug, Clone, serde::Serialize)]
pub struct MissingRequiredOutgoingReport {
    pub id: crate::entity::EntityId,
    pub title: String,
    pub entity_type: String,
    pub mem: String,
    pub missing: Vec<super::MissingRequiredOutgoingBlock>,
}

/// Render the workspace-config projection the health surface serves —
/// per-writable-mem detail (`origin`, storage/durability, `vcs`
/// `gitdir`/`worktree`/`head`, title/subject, `write_guidance`,
/// `extra`) plus the `mutations` and `plugin` policy values. One
/// implementation, every surface: the MCP composer reaches it through
/// `include_config: true` OR the `config` include key; the CLI through
/// `--include config`. `mutations` / `plugin` are passed prebuilt so a
/// server that owns its own copies inserts them verbatim; callers
/// without server state derive them from `Engine::settings()` (see
/// [`config_projection_from_settings`]). Returns the three top-level
/// entries (`mems`, `mutations`, `plugin`) for the caller to merge —
/// callers gate on their own opt-in flag and must render at most once.
pub fn config_projection(
    engine: &crate::Engine,
    writable_mems: &[String],
    mutations: serde_json::Value,
    plugin: serde_json::Value,
) -> serde_json::Map<String, serde_json::Value> {
    // Per-mem storage backend → durability marker, derived from the
    // mount's `MountStorage` kind. Lives alongside `vcs` so an agent
    // reading per-mem config learns whether a `write_id` this mem
    // returns is durable-on-disk or volatile-in-RAM.
    let backend_by_mem: std::collections::HashMap<&str, (&'static str, bool)> = engine
        .mounts()
        .iter()
        .map(|m| {
            (
                m.mem.as_str(),
                (m.storage.backend_id(), m.storage.is_durable()),
            )
        })
        .collect();
    let mems_detail: Vec<serde_json::Value> = writable_mems
        .iter()
        .map(|name| {
            let origin = engine
                .mem_router()
                .origin_for_mem(name)
                .map(|o| o.kind())
                .unwrap_or("explicit");
            let mut entry = serde_json::Map::new();
            entry.insert("name".into(), serde_json::json!(name));
            entry.insert("origin".into(), serde_json::json!(origin));
            if let Some((storage, durable)) = backend_by_mem.get(name.as_str()).copied() {
                entry.insert("storage".into(), serde_json::json!(storage));
                entry.insert("durable".into(), serde_json::json!(durable));
            }
            let mut vcs_obj = serde_json::Map::new();
            if let Ok(gitdir) = engine.gitdir_for(name) {
                vcs_obj.insert("gitdir".into(), serde_json::json!(gitdir));
            }
            if let Ok(worktree) = engine.worktree_for(name) {
                vcs_obj.insert("worktree".into(), serde_json::json!(worktree));
            }
            if let Some(sha) = engine.mem_head_sha(name).ok().flatten() {
                vcs_obj.insert("head".into(), serde_json::json!(sha));
            }
            if !vcs_obj.is_empty() {
                entry.insert("vcs".into(), serde_json::Value::Object(vcs_obj));
            }
            if let Some(cfg) = engine.mem_config_for(name) {
                // Display title + subject block, when set — the
                // config projection prefers the title wherever a
                // mem is printed; the name stays the identity.
                if let Some(title) = &cfg.title {
                    entry.insert("title".into(), serde_json::json!(title));
                }
                if let Some(subject) = &cfg.subject {
                    entry.insert("subject".into(), serde_json::json!(subject));
                }
                let guidance = serde_json::Map::from_iter(
                    cfg.write_guidance
                        .iter()
                        .map(|(k, v)| (k.clone(), v.clone())),
                );
                entry.insert("write_guidance".into(), serde_json::Value::Object(guidance));
                let extra = serde_json::Map::from_iter(
                    cfg.extra.iter().map(|(k, v)| (k.clone(), v.clone())),
                );
                entry.insert("extra".into(), serde_json::Value::Object(extra));
            }
            serde_json::Value::Object(entry)
        })
        .collect();

    let mut out = serde_json::Map::new();
    out.insert("mems".into(), serde_json::json!(mems_detail));
    out.insert("mutations".into(), mutations);
    out.insert("plugin".into(), plugin);
    out
}

/// The `(mutations, plugin)` pair for [`config_projection`], derived
/// from the engine's own [`crate::workspace::WorkspaceSettings`] — for
/// callers (the CLI) that carry no server-owned config copies. Produces
/// the same bytes the MCP server passes when both were loaded from the
/// same `workspace.toml`.
pub fn config_projection_from_settings(
    settings: &crate::workspace::WorkspaceSettings,
) -> (serde_json::Value, serde_json::Value) {
    let mutations = serde_json::json!({ "require_notes": settings.mutations.require_notes });
    let plugin_map: serde_json::Map<String, serde_json::Value> = settings
        .plugin
        .iter()
        .map(|(k, v)| {
            (
                k.clone(),
                serde_json::to_value(v).unwrap_or(serde_json::Value::Null),
            )
        })
        .collect();
    (mutations, serde_json::Value::Object(plugin_map))
}

/// Detect the section-fork condition for one declared section: the
/// parsed content under `key` is empty, the schema's declared heading
/// for the key does not derive back to it
/// (`derive_section_key(heading) != key`), and the file carries that
/// declared heading — so the content is present in the file but
/// unreachable under the key: absorbed into the catch-all when the
/// type declares one, dropped from the parsed sections otherwise.
///
/// Returns the distinct `SECTION_HEADING_MISMATCH` issue naming both
/// the found heading and what a deriving heading would look like. The
/// caller must NOT also report the section as missing — collapsing the
/// two conditions into the missing-section report is exactly the
/// misdirection this finding exists to prevent (the operator goes
/// hunting for absent content that is in fact present).
pub(crate) fn section_heading_mismatch_issue(
    entity: &crate::entity::Entity,
    schema: &TypeDefinition,
    key: &str,
) -> Option<HealthIssue> {
    let def = schema.section(key)?;
    let derived = memstead_schema::derive_section_key(&def.heading);
    if derived == key {
        return None;
    }
    if !entity
        .raw_section_headings
        .iter()
        .any(|h| h == &def.heading)
    {
        return None;
    }
    let landing = match schema.catch_all_section() {
        Some(c) => format!(
            "the content was absorbed into catch-all section '{}'",
            c.key
        ),
        None => "the content is unreachable under any declared key".to_string(),
    };
    Some(HealthIssue {
        field: key.to_string(),
        code: super::HealthIssueCode::SectionHeadingMismatch,
        message: format!(
            "SECTION_HEADING_MISMATCH: section '{key}' is not missing — its content sits \
             under heading '{found}', which derives to '{derived}', not '{key}'; {landing}. \
             The schema's declared heading cannot round-trip to its key (expected a heading \
             that derives to '{key}'); fix the schema's heading/key pair — new installs of \
             such a schema are refused",
            found = def.heading,
        ),
    })
}

/// Get a single entity's health report.
pub fn entity_health(entity: &crate::entity::Entity, schema: &TypeDefinition) -> HealthReport {
    let mut issues = Vec::new();

    for field in &schema.health_required_fields {
        if schema.section(field).is_some() {
            let content = entity.sections.get(field.as_str());
            if content.is_none_or(|c| c.trim().is_empty()) {
                if let Some(issue) = section_heading_mismatch_issue(entity, schema, field) {
                    issues.push(issue);
                } else {
                    issues.push(HealthIssue {
                        field: field.clone(),
                        code: super::HealthIssueCode::Missing,
                        message: format!("required section '{field}' is empty"),
                    });
                }
            }
        } else {
            let value = entity.metadata.get(field.as_str());
            if value.is_none() {
                issues.push(HealthIssue {
                    field: field.clone(),
                    code: super::HealthIssueCode::Missing,
                    message: format!("required field '{field}' is missing"),
                });
            }
        }
    }

    // The mismatch condition is drift worth surfacing on every declared
    // section, not only the health-required ones — an optional section
    // whose content forked away is just as invisible to readers.
    for s in schema.sections.iter().filter(|s| !s.catch_all) {
        if schema.health_required_fields.contains(&s.key) {
            continue; // already handled above
        }
        let content = entity.sections.get(s.key.as_str());
        if content.is_none_or(|c| c.trim().is_empty())
            && let Some(issue) = section_heading_mismatch_issue(entity, schema, &s.key)
        {
            issues.push(issue);
        }
    }

    let total = schema.health_required_fields.len();
    let score = if total > 0 {
        (total.saturating_sub(issues.len()) as f32) / (total as f32)
    } else {
        1.0
    };

    HealthReport {
        id: entity.id.clone(),
        title: entity.title.clone(),
        score,
        issues,
    }
}

// ---------------------------------------------------------------------------
// Date helpers
// ---------------------------------------------------------------------------

/// Get current days since Unix epoch.
///
/// `SystemTime::now()` is unimplemented on `wasm32-unknown-unknown` —
/// it traps with `RuntimeError: unreachable` and poisons the wasm
/// instance — so the wasm build reads the JS-backed
/// clock instead. Same value, same summary shape on every target.
/// The `MEMSTEAD_TODAY=YYYY-MM-DD` clock pin, when set to a civil date:
/// the one pin every date-dependent reading takes (the due brief, the
/// health due axis, the staleness clock, the anchors overlay).
pub fn pinned_today() -> Option<String> {
    let raw = std::env::var("MEMSTEAD_TODAY").ok()?;
    crate::engine::due::pinned_days_since_epoch(&raw)?;
    Some(raw)
}

/// Today as whole days since the Unix epoch, the clock every day-threshold
/// reading in health uses. `MEMSTEAD_TODAY=YYYY-MM-DD` pins it (the
/// injection a fixture needs to age entities without waiting), honoured
/// by every binary alike so the CLI and the MCP server read the same day.
pub fn days_since_epoch() -> u64 {
    if let Some(pinned) = std::env::var("MEMSTEAD_TODAY")
        .ok()
        .and_then(|s| crate::engine::due::pinned_days_since_epoch(&s))
    {
        return pinned;
    }
    #[cfg(target_arch = "wasm32")]
    {
        (js_sys::Date::now() / 1000.0) as u64 / 86400
    }
    #[cfg(not(target_arch = "wasm32"))]
    {
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs()
            / 86400
    }
}

/// Parse an ISO 8601 date string to days since epoch.
/// Supports `YYYY-MM-DD` and `YYYY-MM-DDTHH:MM:SSZ`.
pub fn parse_iso_to_days(date: &str) -> Option<u64> {
    let date_part = date.split('T').next()?;
    let parts: Vec<&str> = date_part.split('-').collect();
    if parts.len() != 3 {
        return None;
    }
    let year: u64 = parts[0].parse().ok()?;
    let month: u64 = parts[1].parse().ok()?;
    let day: u64 = parts[2].parse().ok()?;
    Some(ymd_to_days(year, month, day))
}

/// Convert (year, month, day) to days since Unix epoch.
/// Inverse of the algorithm in generator.rs.
fn ymd_to_days(year: u64, month: u64, day: u64) -> u64 {
    // Algorithm from http://howardhinnant.github.io/date_algorithms.html
    let y = if month <= 2 { year - 1 } else { year };
    let m = if month <= 2 { month + 9 } else { month - 3 };
    let era = y / 400;
    let yoe = y - era * 400;
    let doy = (153 * m + 2) / 5 + day - 1;
    let doe = yoe * 365 + yoe / 4 - yoe / 100 + doy;
    let days = era * 146097 + doe;
    days - 719468
}

#[cfg(test)]
mod tests;