alix 0.8.0

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

use alix::{
    deck::{AtRewrite, Deck},
    source::{CitationIntegrity, SourceBase},
    trace::{DriftKind, Trace},
    workspace,
};
use anyhow::{Context, Result, bail};

use crate::{
    DoctorArgs,
    common::{one_line, truncate},
};

fn val_name<T: clap::ValueEnum>(value: T) -> String {
    value
        .to_possible_value()
        .map(|p| p.get_name().to_string())
        .unwrap_or_default()
}

#[derive(Default)]
struct Report {
    errors: Vec<String>,
    warnings: Vec<String>,
    notes: Vec<String>,
}

impl Report {
    fn error(&mut self, msg: impl Into<String>) {
        self.errors.push(msg.into());
    }
    fn warn(&mut self, msg: impl Into<String>) {
        self.warnings.push(msg.into());
    }
    fn note(&mut self, msg: impl Into<String>) {
        self.notes.push(msg.into());
    }
    fn render(&self) -> bool {
        for e in &self.errors {
            eprintln!("error: {e}");
        }
        for w in &self.warnings {
            eprintln!("warning: {w}");
        }
        for note in &self.notes {
            eprintln!("note: {note}");
        }
        if !self.errors.is_empty() || !self.warnings.is_empty() {
            eprintln!(
                "{} error(s), {} warning(s)",
                self.errors.len(),
                self.warnings.len()
            );
        }
        !self.errors.is_empty()
    }
}

fn deck_findings(path: &Path, report: &mut Report) {
    let name = path
        .file_name()
        .and_then(|n| n.to_str())
        .unwrap_or("deck.md");
    let text = match std::fs::read_to_string(path) {
        Ok(t) => t,
        Err(e) => {
            report.error(format!("{}: {e}", path.display()));
            return;
        }
    };
    let deck = match alix::parser::parse(name, &text) {
        Ok(deck) => deck,
        Err(e) => {
            if alix::parser::is_deck_content(&text) {
                report.error(format!("{}: {e}", path.display()));
            }
            return;
        }
    };
    let initialized = deck.deck_token.is_some();
    if !initialized && alix::parser::is_deck_content(&text) {
        report.warn(format!(
            "{}: deck-like Markdown is not initialized; run `alix deck init {}` if this is an intended deck",
            path.display(),
            path.display()
        ));
    }

    if deck.frontmatter.sampling.is_some() && deck.tables.is_empty() {
        report.warn(format!(
            "{}: `sampling:` has no effect here; the deck holds no card table",
            path.display()
        ));
    }
    for card in &deck.cards {
        if card.sampling.is_some() && card.row.is_none() {
            report.warn(format!(
                "{}: line {}: `sampling:` has no effect on a card that is not a table row",
                path.display(),
                card.line
            ));
            break;
        }
    }

    invisible_byte_findings(path, &text, report);

    let augment = Deck::load(path)
        .ok()
        .and_then(|deck| alix::augment::AugmentCache::open_for_deck(&deck).ok());
    for diagnostic in alix::math::diagnostics(&deck.cards, augment.as_ref()) {
        report.warn(format!("{}: {diagnostic}", path.display()));
    }
    inline_emphasis_findings(&deck.cards, report);
    span_anchor_findings(path, &deck.cards, report);

    for lint in &deck.lints {
        let msg = lint_message(path, lint);
        if matches!(lint.kind, alix::parser::LintKind::BadValue { .. }) {
            report.error(msg);
        } else {
            report.warn(msg);
        }
    }

    let mut tokens: Vec<String> = Vec::new();
    if let Some(t) = &deck.deck_token {
        tokens.push(t.clone());
    }
    for card in &deck.cards {
        if let Some(t) = card.token.as_deref()
            && !tokens.iter().any(|x| x == t)
        {
            tokens.push(t.to_string());
        }
    }
    for tok in &tokens {
        if let Some((_, token, _, _, _)) = alix::token::parse_id(tok)
            && !alix::token::is_canonical(token)
        {
            report.warn(format!(
                "{}: id `{tok}` is valid but its token is not canonical (not 26 base32 chars)",
                path.display()
            ));
        }
    }

    for line in alix::stamp::misplaced_id_markers(&text) {
        report.warn(format!(
            "{}: card id marker at line {line} is not the last line of its card; move it \
             there (the position stamping mints at)",
            path.display()
        ));
    }

    if deck.deck_token.is_none() && deck.frontmatter_span.is_some() && deck.frontmatter.unspliceable
    {
        report.warn(format!(
            "{}: cannot stamp: frontmatter is not a block mapping, so no `id:` can be spliced in",
            path.display()
        ));
    }

    // Cloze holes and a reversed twin share one heading; dedup by line so one
    // stamp isn't counted twice.
    let mut unstamped: Vec<usize> = deck
        .cards
        .iter()
        .filter(|c| c.token.is_none())
        .map(|c| c.line)
        .collect();
    unstamped.sort_unstable();
    unstamped.dedup();
    if initialized && !unstamped.is_empty() {
        report.warn(format!(
            "{}: {} entries are card content without ids; open the deck to assign them",
            path.display(),
            unstamped.len()
        ));
    }

    deck_diagram_findings(
        path,
        deck.deck_token.as_deref(),
        &deck.cards,
        &text,
        deck.frontmatter_span,
        report,
    );
    if let Ok(deck) = Deck::load(path) {
        deck_resource_findings(&deck, report);
    }
}

fn span_anchor_findings(path: &Path, cards: &[alix::card::Card], report: &mut Report) {
    let mut seen = HashSet::new();
    for card in cards {
        for region in &card.span_regions {
            let (Some(minted), Some(bound)) = (region.minted_position, region.bound_position)
            else {
                continue;
            };
            if minted == bound || !seen.insert(region.line) {
                continue;
            }
            let hidden = region.hidden.as_deref().unwrap_or_default();
            let keep_old = match region.minted_occurrence {
                Some(occurrence) => {
                    format!("; to keep the old target instead, set `occurrence={occurrence}`")
                }
                None => String::new(),
            };
            report.warn(format!(
                "{}:{} the span `{hidden}` binds at position:{bound} but its anchor says \
                 position:{minted}; keep the authored occurrence with `alix doctor \
                 --repair-positions` or set position:{bound} yourself{keep_old}",
                path.display(),
                region.line,
            ));
        }
    }
}

fn invisible_byte_findings(path: &Path, text: &str, report: &mut Report) {
    let found = alix::invisible::survey_prose(text.lines());
    if found.stray_tags > 0 {
        report.warn(format!(
            "{}: {} tag character(s) outside a flag sequence: they encode invisible text",
            path.display(),
            found.stray_tags
        ));
    }
    if let Some(line) = alix::invisible::first_fenced_reversal_override(text) {
        report.warn(format!(
            "{}: line {line}: a fence contains bidirectional override characters; \
             rendered order may differ from stored order",
            path.display()
        ));
    }
    let named: Vec<String> = found
        .counts
        .iter()
        .filter(|(label, _)| **label != "TAG")
        .map(|(label, n)| format!("{label} {n}"))
        .collect();
    if !named.is_empty() {
        report.note(format!(
            "{}: invisible characters kept as authored: {} (rendered, never graded)",
            path.display(),
            named.join(", ")
        ));
    }
}

fn inline_emphasis_findings(cards: &[alix::card::Card], report: &mut Report) {
    let mut seen_lines = HashSet::new();
    for card in cards {
        if !seen_lines.insert(card.line) {
            continue;
        }
        let emphasized = std::iter::once(&card.front)
            .chain(card.back.iter())
            .flat_map(|text| alix::inline::parse_inline(text))
            .find(|run| run.bold || run.italic);
        if let Some(run) = emphasized {
            let front = truncate(&one_line(&card.front), 72);
            let snippet = truncate(&one_line(&run.text), 72);
            report.note(format!(
                "inline emphasis will render in \"{front}\": {snippet} \
                 (escape or backtick if unintended)"
            ));
        }
    }
}

/// `source:` must point at real material, never into `assets/`: asset objects
/// are excerpt fragments, and an examiner or tutor grounded on fragments would
/// confidently fill the gaps around them.
fn source_points_into_assets(source: &str) -> bool {
    if alix::deck::is_url(source) {
        return false;
    }
    let root = format!("{}/", alix::assets::ROOT);
    let source = source.trim();
    source.starts_with(&root) || source.contains(&format!("/{root}"))
}

/// Diagram stamps and fences must agree with each other and with their
/// frozen objects. Standalone decks are exempt: they never freeze, and a
/// bare fence there is the designed fallback, not a finding.
fn deck_diagram_findings(
    path: &Path,
    deck_token: Option<&str>,
    cards: &[alix::card::Card],
    text: &str,
    frontmatter_span: Option<(usize, usize)>,
    report: &mut Report,
) {
    let Some(deck_token) = deck_token else {
        return;
    };
    if workspace::root_for_deck(path).is_none() {
        return;
    }
    let found = alix::diagram::fences_in_document(text, frontmatter_span);
    if found.unclosed {
        report.warn(format!(
            "{}: an unclosed mermaid fence cannot be frozen",
            path.display()
        ));
    }
    // A stamp ATTACHED to a fence belongs to it (freeze replaces it in
    // place when stale); a stamp attached to nothing is an orphan only
    // `--repair-diagrams` can remove. The same per-attachment freshness
    // accounting decides the deck-load warning.
    let attached = alix::diagram::attached_stamp_freshness(&found, text);
    for stamp in cards.iter().flat_map(|card| &card.diagrams) {
        match attached.get(&stamp.line) {
            None => report.warn(format!(
                "{}:{}: diagram stamp `{}` is attached to no fence; `alix doctor --repair-diagrams` removes it",
                path.display(),
                stamp.line,
                stamp.fingerprint
            )),
            Some(false) => report.warn(format!(
                "{}:{}: diagram stamp `{}` is stale (its fence was edited); re-run `alix deck init {}` to re-freeze",
                path.display(),
                stamp.line,
                stamp.fingerprint,
                path.display()
            )),
            Some(true) => {
                if let Err(error) = geometry_agreement(path, deck_token, stamp) {
                    report.error(format!(
                        "{}:{}: frozen diagram is inconsistent: {error:#}",
                        path.display(),
                        stamp.line
                    ));
                }
            }
        }
    }
    let unfrozen = found
        .fences
        .iter()
        .filter(|fence| fence.stamp.is_none())
        .count();
    if unfrozen > 0 {
        report.warn(format!(
            "{}: {unfrozen} mermaid fence(s) not frozen; run `alix deck init {}` (needs {})",
            path.display(),
            path.display(),
            alix::diagram::COMMAND
        ));
    }
    // Review silently falls back on a span that cannot project; doctor is
    // the loud channel and judges from the STAMPED geometry itself, so a
    // geometry the load path already refused still gets its precise
    // finding. Records ride every card of a block, so findings dedupe.
    let mut bind_findings: std::collections::BTreeSet<String> = Default::default();
    for card in cards {
        for fence in &card.answer_fences {
            let Some(stamp) = card
                .diagrams
                .iter()
                .find(|stamp| stamp.fingerprint == fence.fingerprint)
            else {
                continue;
            };
            let Ok(geometry) = stamped_geometry(path, deck_token, stamp) else {
                // geometry_agreement above already speaks for unreadable or
                // disagreeing objects.
                continue;
            };
            if let Err(failure) = alix::diagram::validate_label_sources(&geometry, &fence.interior)
            {
                bind_findings.insert(format!(
                    "{}: frozen diagram {}: {failure}; re-run `alix deck init {}` to re-freeze",
                    path.display(),
                    fence.fingerprint,
                    path.display()
                ));
                continue;
            }
            for span in &fence.spans {
                if let Err(failure) =
                    alix::diagram::bind_span(&geometry, span.line, span.start, span.end)
                {
                    bind_findings.insert(format!("{}: {failure}", path.display()));
                }
            }
        }
    }
    for finding in bind_findings {
        report.warn(finding);
    }
}

fn stamped_geometry(
    path: &Path,
    deck_id: &str,
    stamp: &alix::card::DiagramStamp,
) -> anyhow::Result<alix::diagram::DiagramGeometry> {
    let root = workspace::root_for_deck(path).context("not a workspace member")?;
    let owned = alix::assets::deck_dir(&root, deck_id)?;
    let bytes = std::fs::read(owned.join(&stamp.geometry))?;
    Ok(serde_json::from_slice(&bytes)?)
}

/// The stamp names the raster twice: directly (`asset:`) and through the
/// geometry's `image` field. Both routes must name the same object, and the
/// geometry must parse, or serving would pair a raster with the wrong map.
fn geometry_agreement(
    path: &Path,
    deck_id: &str,
    stamp: &alix::card::DiagramStamp,
) -> anyhow::Result<()> {
    let root = workspace::root_for_deck(path).context("not a workspace member")?;
    let owned = alix::assets::deck_dir(&root, deck_id)?;
    if !owned.join(&stamp.asset).is_file() {
        anyhow::bail!("image `{}` is missing from the deck's assets", stamp.asset);
    }
    // Content addressing is only a guarantee if someone re-hashes: existence
    // and name agreement pass for an object whose bytes were replaced.
    for name in [&stamp.asset, &stamp.geometry] {
        alix::assets::verify_object(&owned.join(name))
            .with_context(|| format!("object `{name}`"))?;
    }
    let bytes = std::fs::read(owned.join(&stamp.geometry))
        .with_context(|| format!("geometry `{}` cannot be read", stamp.geometry))?;
    let geometry: alix::diagram::DiagramGeometry = serde_json::from_slice(&bytes)
        .with_context(|| format!("geometry `{}` does not parse", stamp.geometry))?;
    if geometry.image != stamp.asset {
        anyhow::bail!(
            "the geometry names image `{}` but the stamp says `{}`",
            geometry.image,
            stamp.asset
        );
    }
    Ok(())
}

fn deck_resource_findings(deck: &Deck, report: &mut Report) {
    let managed = workspace::root_for_deck(&deck.path).is_some() && deck.deck_token.is_some();
    for source in &deck.sources {
        if source_points_into_assets(source) {
            report.warn(format!(
                "{}: `source:` points into `assets/` (`{source}`); a frozen deck keeps its \
                 real source, not its asset objects",
                deck.path.display()
            ));
        }
    }
    let mut frozen_assets_valid = true;
    let mut has_live_citations = false;
    if managed && !deck.sources.is_empty() {
        let live = deck
            .cards
            .iter()
            .flat_map(|card| &card.citations)
            .filter(|citation| citation.asset.is_none())
            .count();
        if live > 0 {
            report.error(format!(
                "{}: {live} live `at:` citation(s) in an initialized workspace member; initialize or update it to freeze deck-owned excerpts",
                deck.path.display()
            ));
            has_live_citations = true;
        }
        if let Err(error) = alix::assets::validate_member(deck) {
            report.error(format!(
                "{}: frozen source assets are invalid: {error:#}",
                deck.path.display()
            ));
            frozen_assets_valid = false;
        }
    }
    if managed && let Ok(text) = std::fs::read_to_string(&deck.path) {
        for image in alix::parser::image_references(&text) {
            if alix::deck::is_url(&image.source) {
                continue;
            }
            if let Err(error) = alix::assets::validate_image(deck, &image.source) {
                report.error(format!(
                    "{}: image `{}` is not a valid deck-owned asset: {error:#}",
                    deck.path.display(),
                    image.source
                ));
            }
        }
    }
    for card in &deck.cards {
        for image in card.images.iter().chain(&card.images_back) {
            if !image.src.exists() {
                report.warn(format!(
                    "{}: card at line {} references a missing image: {}",
                    deck.subject,
                    card.line,
                    image.src.display()
                ));
            }
        }
    }
    for drift in alix::trace::drifted_cards(deck) {
        let what = match drift.kind {
            DriftKind::SourceGone => "source file is gone".to_string(),
            DriftKind::Rewritten => "no longer found in the source".to_string(),
            DriftKind::Ambiguous => "occurs more than once, so its lines are unclear".to_string(),
            DriftKind::Moved { start, len } => format!(
                "is intact but now at lines {start}-{}; `--repair-source-locators` rebases it",
                start + len.saturating_sub(1)
            ),
        };
        report.warn(format!(
            "{}: card at line {}: frozen excerpt {} ({})",
            deck.subject, drift.line, what, drift.at
        ));
    }
    if deck.is_trace() && !deck.cards.is_empty() {
        match Trace::from_deck(deck) {
            Ok(trace) => {
                for issue in trace.lint_locators() {
                    let line = deck.cards.get(issue.checkpoint).map_or(0, |c| c.line);
                    report.warn(format!(
                        "{}: checkpoint at line {}: {}",
                        deck.subject, line, issue.message
                    ));
                }
            }
            Err(e) => report.warn(format!("{}: {e:#}", deck.subject)),
        }
    }
    if frozen_assets_valid {
        let base = SourceBase::for_deck(deck);
        // One authored `at:` line serves every card its block produced (a
        // table's rows, a cloze block's holes), so the report is per authored
        // citation. Its identity is its own directive line: a card may carry
        // several `at:` lines, and keying on the card would let a healthy one
        // hide a stale sibling.
        let mut reported = std::collections::HashSet::new();
        for card in &deck.cards {
            for citation in &card.citations {
                if !reported.insert(citation.line) {
                    continue;
                }

                let counted_by_the_live_error = has_live_citations && citation.asset.is_none();
                let detail = match base.inspect_citation(citation) {
                    Ok(CitationIntegrity::Current(_)) => None,
                    Ok(CitationIntegrity::Unfingerprinted { .. }) => (!counted_by_the_live_error)
                        .then(|| {
                            "has no excerpt fingerprint; review it, then run \
                             `alix doctor --repair-source-locators`"
                                .to_string()
                        }),
                    Ok(CitationIntegrity::Relocated { locator, .. }) => Some(format!(
                        "the exact excerpt moved to `{locator}`; run \
                         `alix doctor --repair-source-locators` to rebase it"
                    )),
                    Ok(CitationIntegrity::Changed) => {
                        Some("the excerpt changed or disappeared; review it manually".to_string())
                    }
                    Ok(CitationIntegrity::Ambiguous { locators }) => Some(format!(
                        "the excerpt matches several ranges ({}); review it manually",
                        locators.join(", ")
                    )),
                    Err(error) => Some(format!("{error:#}")),
                };
                if let Some(detail) = detail {
                    report.warn(format!(
                        "{}: card at line {}: `at: {}` {detail}",
                        deck.subject, card.line, citation.locator
                    ));
                }
            }
        }
    }
    if deck.sources.len() > 3 {
        report.warn(format!(
            "{}: `source:` lists {} expressions; point at their common root instead \
             (a directory source covers its files)",
            deck.path.display(),
            deck.sources.len()
        ));
    }
    for prereq in alix::deck::nongating_prerequisites(deck) {
        report.warn(format!(
            "{}: requires ungrounded `{prereq}`: this edge doesn't gate its exam; \
             give it a `source:` (deck or workspace) to make it a real prerequisite",
            deck.subject
        ));
    }
    let dir = deck.path.parent();
    let unparseable_prereq = |report: &mut Report, req: &str, path: &Path| {
        if Deck::load(path).is_err() {
            report.warn(format!(
                "{}: requires `{req}` but that deck fails to parse; its errors name the fix",
                deck.subject
            ));
        }
    };
    for req in &deck.requires {
        match alix::deck::classify_require(req) {
            alix::deck::RequiresMode::DeckId => {
                match alix::deck::resolve_dep_by_id(req, dir, dir) {
                    None => report.warn(format!(
                        "{}: requires deck id `{req}` but no deck here carries it \
                         (dangling prerequisite); the prerequisite may have been deleted \
                         or live elsewhere; if you meant a file of that name, write the \
                         `.md` extension (`{req}.md`)",
                        deck.subject
                    )),
                    Some(path) => unparseable_prereq(report, req, &path),
                }
                if let Some(shadow) = alix::deck::resolve_dep(req, dir, dir) {
                    report.warn(format!(
                        "{}: the file {} is named like the required deck id `{req}`; \
                         the id wins, the file is not the prerequisite (write `{req}.md` \
                         to require the file itself)",
                        deck.subject,
                        shadow.display()
                    ));
                }
            }
            alix::deck::RequiresMode::WrongTypeCardId => {
                report.error(format!(
                    "{}: requires `{req}` which is a card id, likely pasted by mistake; \
                     a card is never a prerequisite, name the deck's `deck-<token>` id or \
                     its filename",
                    deck.subject
                ));
            }
            alix::deck::RequiresMode::Filename => match alix::deck::resolve_dep(req, dir, dir) {
                None => {
                    report.warn(format!(
                        "{}: requires `{req}` but no such deck exists here (dangling \
                             prerequisite); a filename edge breaks when the prerequisite is \
                             renamed or deleted (a `deck-<token>` id edge survives renames)",
                        deck.subject
                    ));
                    if req.starts_with("deck-") {
                        report.note(format!(
                            "{}: `requires: {req}` looks like a truncated or malformed \
                                 deck id (an id is `deck-` plus 26 base32 chars); it was read \
                                 as a filename",
                            deck.subject
                        ));
                    }
                }
                Some(path) => unparseable_prereq(report, req, &path),
            },
        }
    }
}

fn lint_message(path: &Path, lint: &alix::parser::Lint) -> String {
    use alix::parser::LintKind;
    let detail = match &lint.kind {
        LintKind::UnknownKey { key } => format!("unknown key `{key}` (ignored)"),
        LintKind::BadValue { key, value } => format!("`{key}` has an invalid value `{value}`"),
        LintKind::EmptyValue { key } => format!("`{key}` has an empty value"),
        LintKind::IndentedH2 => {
            "an indented `##` line is content, not a card front (likely a mistype)".to_string()
        }
        LintKind::NoteContainsBlankAnswer { blank, answer } => format!(
            "the note contains the text of blank {blank}'s answer (`{answer}`); \
             the block's other cards show this note"
        ),
        LintKind::NoteNamesNoBlank { name } => format!(
            "`{name}:` names no named blank of this block, so the line is \
             shown as the block note"
        ),
        LintKind::UntypableSpan { answer } => format!(
            "the hidden span `{answer}` asks the learner to type a LaTeX \
             command; hide a piece that can be typed, or let it be drawn"
        ),
        LintKind::UnclosedComment => {
            "a `<!--` line that never closes with `-->` stays content".to_string()
        }
        LintKind::UnrecognizedComment => "a deck's `<!-- -->` is alix machinery, and this \
             one is not recognized, so it is ignored; write a directive (`key: value`), a \
             locator, or an invocation (`plain`, `cards`, `choices: single`, \
             `choices: multiple`), and put prose in `description:` or a card"
            .to_string(),
        LintKind::BadgeShape { text } => format!(
            "`{text}` is not one of the five alert badges, so this blockquote is a quote \
             and not a note; write `[!NOTE]`, `[!TIP]`, `[!IMPORTANT]`, `[!WARNING]`, or \
             `[!CAUTION]` alone on the line"
        ),
        LintKind::EmptyNote => "this alert badge opens a note with no body, so the card \
             shows nothing for it"
            .to_string(),
        LintKind::UnclosedFence => "a fence opened here never closes; everything after it \
             (cards included) was swallowed as its content"
            .to_string(),
        LintKind::ImageMalformed => "an image embed here is malformed; write \
             `![alt](file.png)` (or escape a literal `![` as `\\![`)"
            .to_string(),
        LintKind::UndecidedTable => "this table declares no mapping; add \
             `<!-- cards -->` below it to make each row a card, or \
             `<!-- plain -->` to keep it a literal table"
            .to_string(),
        LintKind::ChoiceAnswerMixed => {
            "task-list choices are mixed with other answer content; treating the card as plain"
                .to_string()
        }
        LintKind::ChoiceNeedsBothSides => {
            "a checkbox card needs one checked answer and at least one unchecked distractor"
                .to_string()
        }
        LintKind::DuplicateChoiceOption => {
            "a checkbox option repeats earlier option text; keeping the first".to_string()
        }
        LintKind::ChoiceMultiCorrectUnsupported => {
            "multiple checked answers are not supported yet; treating the card as plain".to_string()
        }
        LintKind::ChoiceNoteNamesPosition => {
            "the note identifies an option by number, letter, or position, but choices are \
             shuffled; name the option's claim or mistaken premise instead"
                .to_string()
        }
    };
    format!("{}: line {}: {detail}", path.display(), lint.line)
}

fn sidecar_findings(dir: &Path, report: &mut Report) {
    let paths = alix::workspace::listing_with_sidecars(dir).unwrap_or_default();
    let mut entries = Vec::new();
    for path in &paths {
        let Some(name) = path.file_name().and_then(|n| n.to_str()) else {
            continue;
        };
        let Ok(text) = std::fs::read_to_string(path) else {
            continue;
        };
        entries.push(alix::FileEntry {
            name: name.to_string(),
            deck_id: alix::parser::deck_identity(&text).ok().flatten(),
            personal_for: alix::parser::personal_parent(&text).ok().flatten(),
            card_ids: alix::parser::parse_str(name, &alix::parser::without_notes(&text))
                .unwrap_or_default()
                .iter()
                .filter_map(alix::card::Card::id)
                .collect(),
        });
    }

    for finding in alix::classify(&entries).1 {
        report.warn(match finding {
            alix::Finding::ParentMissing { file } => {
                format!("{file}: names a deck that is not in this folder")
            }
            alix::Finding::ParentMismatch {
                file,
                named,
                neighbour,
            } => format!(
                "{file}: `{key}: {named}` but the deck it sits beside is {neighbour}",
                key = alix::parser::PERSONAL_PARENT_KEY
            ),
            alix::Finding::DuplicateCardId {
                deck,
                sidecar,
                card,
            } => format!("{sidecar}: card `{card}` is already in {deck}; one schedule, two cards"),
            alix::Finding::SuffixMissing { file } => {
                format!(
                    "{file}: carries `{key}:` but is not named `<deck>.personal.md`",
                    key = alix::parser::PERSONAL_PARENT_KEY
                )
            }
        });
    }

    for path in paths.iter().filter(|path| {
        path.file_name()
            .and_then(|n| n.to_str())
            .is_some_and(alix::workspace::is_sidecar_name)
    }) {
        orphan_note_findings(path, report);
    }
}

fn orphan_note_findings(sidecar: &Path, report: &mut Report) {
    let deck_path = sidecar.with_file_name(
        sidecar
            .file_name()
            .and_then(|n| n.to_str())
            .and_then(|n| n.strip_suffix(".personal.md"))
            .map(|stem| format!("{stem}.md"))
            .unwrap_or_default(),
    );
    let Ok(deck) = Deck::load(&deck_path) else {
        return;
    };
    let personal = alix::personal::read(&deck_path, &deck.subject);
    let deck_blocks: Vec<alix::DeckCard> = deck
        .cards
        .iter()
        .filter_map(|card| {
            Some(alix::DeckCard {
                id: card.id()?,
                notes: Vec::new(),
            })
        })
        .collect();
    let (_, orphans) = alix::merge(&deck_blocks, &personal.blocks());
    for orphan in orphans {
        report.warn(format!(
            "{}: a note addresses `{}`, which is in neither the deck nor this file",
            sidecar.display(),
            orphan.card
        ));
    }
}

fn workspace_findings(dir: &Path) -> Report {
    let mut report = Report::default();
    for root in alix::workspace::roots_under(dir) {
        let nested = findings_in(&root);
        report.errors.extend(nested.errors);
        report.warnings.extend(nested.warnings);
        report.notes.extend(nested.notes);
    }
    report
}

fn findings_in(dir: &Path) -> Report {
    let mut report = Report::default();
    if alix::workspace::has_manifest(dir)
        && let Err(error) = alix::workspace::Workspace::load(dir)
    {
        report.error(format!("{error}"));
    }
    for path in alix::workspace::misplaced_deck_files(dir).unwrap_or_default() {
        report.warn(format!(
            "{}: workspace decks belong in {}; this file is not discovered",
            path.display(),
            dir.join(alix::workspace::DECKS).display()
        ));
    }
    let found = alix::workspace::classify_deck_files(dir).unwrap_or_default();
    let (deck_files, uninitialized) = (found.initialized, found.uninitialized);
    for path in &deck_files {
        deck_findings(path, &mut report);
    }
    for path in uninitialized {
        deck_findings(&path, &mut report);
    }

    sidecar_findings(dir, &mut report);

    let map = alix::dedup::scan_dir(dir);
    for (kept, excluded, token) in &map.excluded_decks {
        report.warn(format!(
            "duplicate deck token `{token}`: {} is excluded (kept {}); delete the `id:` line in the copy",
            excluded.display(),
            kept.display()
        ));
    }
    for dupe in &map.card_dupes {
        let losers: Vec<String> = dupe
            .losers
            .iter()
            .map(|(p, l)| format!("{}:{}", p.display(), l))
            .collect();
        report.warn(format!(
            "duplicate card token `{}`: {}:{} keeps the progress; also at {}",
            dupe.base,
            dupe.keeper.0.display(),
            dupe.keeper.1,
            losers.join(", ")
        ));
    }

    let store_path = alix::workspace::root_store_path(dir);
    let mut known_deck_ids = HashSet::new();
    for path in &deck_files {
        if let Ok(deck) = Deck::load(path)
            && let Some(deck_id) = deck.deck_token
        {
            known_deck_ids.insert(deck_id);
        }
    }
    match alix::state::open_aggregate_store(&store_path) {
        Ok(store) => {
            let mut known_cards: HashSet<String> = HashSet::new();
            let mut any_fresh = false;
            for path in &deck_files {
                if let Ok(deck) = Deck::load(path) {
                    for card in &deck.cards {
                        if let Some(id) = card.id() {
                            if store.get(&id).is_none() {
                                any_fresh = true;
                            }
                            known_cards.insert(id);
                        }
                    }
                    known_cards.extend(alix::personal::card_ids(&deck));
                    known_cards.extend(deck.dormant_base_ids());
                }
            }
            let orphans = store.orphans(&known_cards, &known_deck_ids);
            for key in &orphans.cards {
                report.warn(format!(
                    "orphaned store key (card) `{key}` matches no card in {}",
                    dir.display()
                ));
            }
            for key in &orphans.decks {
                report.warn(format!(
                    "orphaned store key (deck) `{key}` matches no deck in {}",
                    dir.display()
                ));
            }
            if !orphans.cards.is_empty() && any_fresh {
                report.warn(
                    "orphaned card progress exists and fresh tokens were minted: a card may have \
                 lost its `<!-- id: -->` comment (e.g. a formatter stripped it) and been \
                 re-stamped, orphaning its old progress; the old progress stays until you run \
                 `alix reset --orphans`"
                        .to_string(),
                );
            }
        }
        Err(error) => report.error(format!("user files: {error}")),
    }
    if let Err(error) = alix::augment::AugmentCache::open_for_workspace(dir) {
        report.error(format!("workspace augmentation: {error}"));
    }
    let user_files = alix::state::UserFiles::new(&store_path);
    let workspace_files = alix::workspace::WorkspaceFiles::new(dir);
    for (kind, data_dir) in [
        ("progress", user_files.progress()),
        ("augmentation", workspace_files.augment()),
    ] {
        for entry in std::fs::read_dir(data_dir).into_iter().flatten().flatten() {
            let path = entry.path();
            let name = path
                .file_name()
                .and_then(|name| name.to_str())
                .unwrap_or("");
            if !path.is_file()
                || path.extension().is_none_or(|extension| extension != "json")
                || alix::workspace::is_conflict_name(name)
            {
                continue;
            }
            let Some(deck_id) = alix::state::deck_id_from_document(&path) else {
                report.warn(format!("unrecognized {kind} document {}", path.display()));
                continue;
            };
            let document_error = match kind {
                "progress" => alix::store::Store::open_deck(&path, deck_id, "")
                    .err()
                    .map(|error| error.to_string()),
                _ => alix::augment::AugmentCache::open_deck(&path, deck_id)
                    .err()
                    .map(|error| error.to_string()),
            };
            if let Some(error) = document_error {
                report.error(format!("{kind} document: {error}"));
                continue;
            }
            if !known_deck_ids.contains(deck_id) {
                report.warn(format!(
                    "orphaned {kind} document {} belongs to no deck in {}",
                    path.display(),
                    dir.display()
                ));
            }
        }
    }
    for conflict in alix::store::sync_conflicts(&store_path) {
        report.warn(format!(
            "synchronization conflict needs deliberate recovery: {}",
            conflict.display()
        ));
    }
    for conflict in alix::augment::sync_conflicts(dir) {
        report.warn(format!(
            "synchronization conflict needs deliberate recovery: {}",
            conflict.display()
        ));
    }
    report
}

fn check(decks: Vec<PathBuf>) -> Result<()> {
    let mut report = Report::default();
    for path in &decks {
        // Deck::load would error on a directory, so a workspace target is
        // handled separately here.
        if path.is_dir() && path.join(alix::workspace::MANIFEST).is_file() {
            if let Some(rel) = alix::workspace::manifest_icon(path)
                && !path.join(&rel).is_file()
            {
                report.warn(format!(
                    "{}: `icon = \"{rel}\"` points at a missing file",
                    path.display()
                ));
            }
            for complaint in alix::config::local_review_lint(path) {
                report.warn(format!("{}: {complaint}", path.display()));
            }
            continue;
        }
        if let Ok(deck) = Deck::load(path) {
            println!("{}: {} cards", deck.subject, deck.cards.len());
            if std::fs::read_to_string(path).is_ok_and(|text| {
                matches!(alix::parser::normalize(&text), std::borrow::Cow::Owned(_))
            }) {
                report.warn(format!(
                    "{}: not in canonical bytes; run `alix doctor <dir-or-deck> --normalize`",
                    path.display()
                ));
            }
            let s = &deck.settings;
            let declared: Vec<String> = [
                s.reveal.map(|r| format!("reveal: {}", val_name(r))),
                s.order.map(|o| format!("review: {}", val_name(o))),
                s.exam_strictness
                    .map(|v| format!("strictness: {}", val_name(v))),
            ]
            .into_iter()
            .flatten()
            .collect();
            if !declared.is_empty() {
                println!("  settings: {}", declared.join(", "));
            }
            if !deck.requires.is_empty() {
                println!("  requires: {}", deck.requires.join(", "));
            }
            if !deck.sources.is_empty() {
                println!("  sources:  {}", deck.sources.join(", "));
            }
            if let Some(desc) = &deck.trace {
                println!("  trace:    {desc}");
            }
        }
        deck_findings(path, &mut report);
    }
    if report.render() {
        bail!("{} error(s) found", report.errors.len());
    }
    Ok(())
}

/// The parsed `position:` token's byte offset in a directive line: outside
/// quoted values (a hidden answer may look exactly like the token) and
/// bounded so `position:1` never matches inside `position:13`.
fn anchor_token_offset(line: &str, token: &str) -> Option<usize> {
    let mut in_quote = false;
    let mut escaped = false;
    for (at, ch) in line.char_indices() {
        if escaped {
            escaped = false;
            continue;
        }
        match ch {
            '\\' if in_quote => escaped = true,
            '"' => in_quote = !in_quote,
            _ if in_quote => {}
            _ => {
                if line[at..].starts_with(token)
                    && (at == 0 || line[..at].ends_with(char::is_whitespace))
                    && !line[at + token.len()..].starts_with(|c: char| c.is_ascii_digit())
                {
                    return Some(at);
                }
            }
        }
    }
    None
}

fn repair_positions(paths: &[PathBuf]) -> Result<()> {
    for path in paths {
        let deck = Deck::load(path)?;
        let mut edits: Vec<(usize, u32, u32)> = Vec::new();
        let mut seen = HashSet::new();
        for card in &deck.cards {
            for region in &card.span_regions {
                if let (Some(minted), Some(bound)) = (region.minted_position, region.bound_position)
                    && minted != bound
                    && seen.insert(region.line)
                {
                    edits.push((region.line, minted, bound));
                }
            }
        }
        if edits.is_empty() {
            continue;
        }
        let text = std::fs::read_to_string(path)?;
        let mut lines: Vec<String> = text.split_inclusive('\n').map(str::to_string).collect();
        for (line, minted, bound) in &edits {
            let Some(raw) = lines.get_mut(line - 1) else {
                bail!("{}:{line} disappeared while repairing", path.display());
            };
            let old_token = format!("position:{minted}");
            let Some(at) = anchor_token_offset(raw, &old_token) else {
                bail!(
                    "{}:{line} lost `{old_token}` while repairing",
                    path.display()
                );
            };
            raw.replace_range(at..at + old_token.len(), &format!("position:{bound}"));
            println!(
                "rebased {}:{line} position:{minted} -> position:{bound}",
                path.display()
            );
        }
        let repaired: String = lines.concat();
        alix::parser::parse(
            path.file_stem().and_then(|s| s.to_str()).unwrap_or("deck"),
            &repaired,
        )
        .with_context(|| format!("{}: the repair would not parse", path.display()))?;
        alix::deck::write_deck_text(path, &repaired)?;
    }
    Ok(())
}

fn normalize_decks(paths: &[PathBuf]) -> Result<()> {
    for path in paths {
        let text = std::fs::read_to_string(path)?;
        let std::borrow::Cow::Owned(normalized) = alix::parser::normalize(&text) else {
            continue;
        };
        alix::parser::parse(
            path.file_stem().and_then(|s| s.to_str()).unwrap_or("deck"),
            &normalized,
        )
        .with_context(|| format!("{}: the normalized deck would not parse", path.display()))?;
        alix::deck::write_deck_text(path, &normalized)?;
        println!("normalized {}", path.display());
    }
    Ok(())
}

fn repair_frontmatter_order(paths: &[PathBuf]) -> Result<()> {
    use alix::parser::Reorder;
    for path in paths {
        let text = std::fs::read_to_string(path)?;
        match alix::parser::reorder_frontmatter(&text) {
            Reorder::Unchanged => {}
            Reorder::Reordered(repaired) => {
                alix::parser::parse(
                    path.file_stem().and_then(|s| s.to_str()).unwrap_or("deck"),
                    &repaired,
                )
                .with_context(|| format!("{}: the repair would not parse", path.display()))?;
                alix::deck::write_deck_text(path, &repaired)?;
                println!("reordered frontmatter in {}", path.display());
            }
            Reorder::Skipped(reason) => {
                eprintln!(
                    "warning: {}: frontmatter left as-is: {reason}",
                    path.display()
                );
            }
        }
    }
    Ok(())
}

fn repair_comment_order(paths: &[PathBuf]) -> Result<()> {
    use alix::parser::Reorder;
    for path in paths {
        let text = std::fs::read_to_string(path)?;
        match alix::parser::reorder_card_comments(&text) {
            Reorder::Unchanged => {}
            Reorder::Reordered(repaired) => {
                alix::parser::parse(
                    path.file_stem().and_then(|s| s.to_str()).unwrap_or("deck"),
                    &repaired,
                )
                .with_context(|| format!("{}: the repair would not parse", path.display()))?;
                alix::deck::write_deck_text(path, &repaired)?;
                println!("reordered comment machinery in {}", path.display());
            }
            Reorder::Skipped(reason) => {
                eprintln!("warning: {}: comments left as-is: {reason}", path.display());
            }
        }
    }
    Ok(())
}

fn repair_diagrams(paths: &[PathBuf]) -> Result<()> {
    for path in paths {
        let (removed, report) = alix::assets::repair_diagrams(path)?;
        if removed > 0 {
            println!(
                "removed {removed} orphan diagram stamp(s) from {}",
                path.display()
            );
        }
        if report.diagrams > 0 {
            println!(
                "re-froze {} diagram(s) in {}",
                report.diagrams,
                path.display()
            );
        }
        for warning in &report.diagram_warnings {
            eprintln!("warning: {}: {warning}", path.display());
        }
    }
    Ok(())
}

fn repair_source_locators(paths: &[PathBuf]) -> Result<()> {
    let mut unresolved = 0;
    for path in paths {
        let deck = Deck::load(path)?;
        let base = SourceBase::for_deck(&deck);
        let mut rewrites = Vec::new();
        let mut changed = false;
        // A frozen citation's fingerprint verifies the asset, not the source, so
        // `inspect_citation` cannot see that its `at:` range moved. Rebasing the
        // address while the evidence stays byte-identical is the one repair that
        // adds no claim: ADR 0026 forbids rewriting the frozen fingerprint, not
        // correcting the provenance lines.
        let moved: HashMap<String, (usize, usize)> = alix::trace::drifted_cards(&deck)
            .into_iter()
            .filter_map(|drift| match drift.kind {
                DriftKind::Moved { start, len } => Some((drift.at, (start, len))),
                _ => None,
            })
            .collect();
        for card in &deck.cards {
            for citation in &card.citations {
                if let Some((start, len)) = moved.get(&citation.locator) {
                    let (file, _) = alix::source::parse_locator(&citation.locator);
                    let at = alix::source::relocated_locator(file.as_deref(), *start, *len);
                    println!(
                        "rebased {}:{} `{}` -> `{at}`",
                        path.display(),
                        citation.line,
                        citation.locator
                    );
                    changed = true;
                    rewrites.push(AtRewrite {
                        at,
                        fingerprint: citation.fingerprint,
                        asset: citation.asset.clone(),
                        line: citation.line,
                    });
                    continue;
                }
                let (at, fingerprint) = match base.inspect_citation(citation)? {
                    CitationIntegrity::Current(_) => {
                        (citation.locator.clone(), citation.fingerprint)
                    }
                    CitationIntegrity::Unfingerprinted { fingerprint, .. } => {
                        println!(
                            "stamped {}:{} `{}`",
                            path.display(),
                            citation.line,
                            citation.locator
                        );
                        changed = true;
                        (citation.locator.clone(), Some(fingerprint))
                    }
                    CitationIntegrity::Relocated { locator, .. } => {
                        println!(
                            "rebased {}:{} `{}` -> `{locator}`",
                            path.display(),
                            citation.line,
                            citation.locator
                        );
                        changed = true;
                        (locator, citation.fingerprint)
                    }
                    CitationIntegrity::Changed => {
                        eprintln!(
                            "warning: {}:{} `{}` changed or disappeared; not repaired",
                            path.display(),
                            citation.line,
                            citation.locator
                        );
                        unresolved += 1;
                        (citation.locator.clone(), citation.fingerprint)
                    }
                    CitationIntegrity::Ambiguous { locators } => {
                        eprintln!(
                            "warning: {}:{} `{}` matches several ranges ({}); not repaired",
                            path.display(),
                            citation.line,
                            citation.locator,
                            locators.join(", ")
                        );
                        unresolved += 1;
                        (citation.locator.clone(), citation.fingerprint)
                    }
                };
                rewrites.push(AtRewrite {
                    at,
                    fingerprint,
                    asset: citation.asset.clone(),
                    line: citation.line,
                });
            }
        }
        if changed {
            alix::deck::set_source_citations(path, &rewrites)?;
        }
    }
    if unresolved > 0 {
        bail!("{unresolved} source citation(s) need manual review");
    }
    Ok(())
}

fn repair_after_explicit_path(path: Option<&Path>) -> bool {
    path.is_none_or(|path| !workspace::is_workspace(path))
}

// Exits non-zero only on a hard fail; a missing optional binary (a warn)
// never breaks a script.
pub(crate) fn doctor_cmd(args: DoctorArgs) -> Result<()> {
    use alix::doctor::{self, Status};
    let repair_after_explicit_path = repair_after_explicit_path(args.dir.as_deref());
    if let Some(path) = &args.dir {
        if path.is_file() {
            if args.normalize {
                normalize_decks(std::slice::from_ref(path))?;
            }
            if args.repair_source_locators {
                repair_source_locators(std::slice::from_ref(path))?;
            }
            if args.repair_positions {
                repair_positions(std::slice::from_ref(path))?;
            }
            if args.repair_diagrams {
                repair_diagrams(std::slice::from_ref(path))?;
            }
            if args.repair_frontmatter_order {
                repair_frontmatter_order(std::slice::from_ref(path))?;
            }
            if args.repair_comment_order {
                repair_comment_order(std::slice::from_ref(path))?;
            }
            return check(vec![path.clone()]);
        }
        if alix::workspace::is_workspace(path) {
            if args.normalize {
                normalize_decks(&alix::workspace::diagnosable_deck_files(path))?;
            }
            if args.repair_source_locators {
                repair_source_locators(&alix::workspace::diagnosable_deck_files(path))?;
            }
            if args.repair_positions {
                repair_positions(&alix::workspace::diagnosable_deck_files(path))?;
            }
            if args.repair_diagrams {
                repair_diagrams(&alix::workspace::diagnosable_deck_files(path))?;
            }
            if args.repair_frontmatter_order {
                repair_frontmatter_order(&alix::workspace::diagnosable_deck_files(path))?;
            }
            if args.repair_comment_order {
                repair_comment_order(&alix::workspace::diagnosable_deck_files(path))?;
            }
            check(vec![path.clone()])?;
        }
    }
    let (config_finding, config) = doctor::check_config(args.config.as_deref());
    let mut findings = vec![config_finding];
    let instance =
        crate::profile::instance_name_for_launch(args.config.as_deref(), args.dir.as_deref());
    findings.push(doctor::check_log(alix::log::log_path(&instance)));
    let (decks_dir, store_path) = match &args.dir {
        Some(path) => (path.clone(), workspace::root_store_path(path)),
        None => {
            let dir = config.decks_dir().context("cannot determine ~/decks")?;
            let store = workspace::root_store_path(&dir);
            (dir, store)
        }
    };
    if args.normalize && repair_after_explicit_path {
        normalize_decks(&alix::workspace::diagnosable_deck_files(&decks_dir))?;
    }
    if args.repair_source_locators && repair_after_explicit_path {
        repair_source_locators(&alix::workspace::diagnosable_deck_files(&decks_dir))?;
    }
    if args.repair_positions && repair_after_explicit_path {
        repair_positions(&alix::workspace::diagnosable_deck_files(&decks_dir))?;
    }
    if args.repair_diagrams && repair_after_explicit_path {
        repair_diagrams(&alix::workspace::diagnosable_deck_files(&decks_dir))?;
    }
    if args.repair_frontmatter_order && repair_after_explicit_path {
        repair_frontmatter_order(&alix::workspace::diagnosable_deck_files(&decks_dir))?;
    }
    if args.repair_comment_order && repair_after_explicit_path {
        repair_comment_order(&alix::workspace::diagnosable_deck_files(&decks_dir))?;
    }
    if args.remove_backup_files {
        let baks = doctor::backup_files(&decks_dir);
        if baks.is_empty() {
            println!("No backup files under {}.", decks_dir.display());
        } else {
            println!("{} backup file(s):", baks.len());
            for bak in &baks {
                println!("  {}", bak.display());
            }
            if crate::common::confirm(
                "Delete them? `alix deck restore` will then have nothing to swap in.",
                args.yes,
            )? {
                for bak in &baks {
                    std::fs::remove_file(bak)
                        .with_context(|| format!("cannot remove {}", bak.display()))?;
                }
                println!("Deleted {} backup file(s).", baks.len());
            } else {
                println!("Kept them.");
            }
        }
    }
    findings.push(doctor::check_store(Some(store_path)));
    findings.push(doctor::check_decks(&decks_dir));
    if let Some(finding) = doctor::check_backups(&decks_dir) {
        findings.push(finding);
    }
    findings.push(doctor::check_binary(
        "backend",
        &config.ask.command,
        "the AI features (tutor, exam, generate)",
        "install it and log in — or switch `[ask] backend` in the config",
    ));
    findings.push(doctor::check_binary(
        "share",
        "wormhole",
        "sharing (`alix share`/`receive`)",
        "install magic-wormhole (e.g. `pipx install magic-wormhole`, or your package manager)",
    ));
    findings.push(doctor::check_binary(
        "diagrams",
        alix::diagram::COMMAND,
        "rendering mermaid diagrams into decks",
        alix::diagram::REMEDY,
    ));
    let mut failed = false;
    for f in &findings {
        let glyph = match f.status {
            Status::Ok => "✓",
            Status::Warn => "!",
            Status::Fail => {
                failed = true;
                "✗"
            }
        };
        println!("{glyph} {:<8} {}", f.name, f.detail);
        if let Some(remedy) = &f.remedy {
            println!("  ↳ {remedy}");
        }
    }
    // A standalone deck-file target returned above and skips this: it stays
    // dedup-blind by design.
    if workspace_findings(&decks_dir).render() {
        failed = true;
    }
    if args.backends || args.all_backends {
        println!();
        alix::backend::health::check(&config.ask, args.all_backends)?;
    }
    // Probe outcomes never flip the exit code: they're a spot check on the
    // model, not a broken setup. Only an infrastructure error fails the run.
    if args.grading {
        println!();
        grading_spot_check(&config)?;
    }
    if failed {
        bail!("doctor found problems (✗ above)");
    }
    Ok(())
}

fn grading_spot_check(config: &alix::config::Config) -> Result<()> {
    use alix::calibrate::{self, ProbeKind};
    println!(
        "grading spot-check: {} probes, 3 real calls to the configured backend…",
        calibrate::PROBES.len()
    );
    let results = calibrate::run(&config.exam, &config.ask)?;
    let (mut safety_bad, mut fairness_bad) = (0, 0);
    for r in &results {
        let expect = match r.kind {
            ProbeKind::Safety => "must not pass",
            ProbeKind::Fairness => "should pass",
        };
        let glyph = if r.ok {
            "✓"
        } else {
            match r.kind {
                ProbeKind::Safety => {
                    safety_bad += 1;
                    "✗"
                }
                ProbeKind::Fairness => {
                    fairness_bad += 1;
                    "!"
                }
            }
        };
        println!("{glyph} {:<20} {expect}: got {:?}", r.name, r.verdict);
    }
    if safety_bad > 0 {
        println!(
            "✗ the model passed {safety_bad} answer(s) that must not pass: exam grades from \
             this model may be too lenient; consider a stronger `[ask]` model for grading"
        );
    } else if fairness_bad > 0 {
        println!(
            "! safe, but stricter than intended: {fairness_bad} should-pass probe(s) did not \
             pass. Passing an exam stays honest; it may just be harder than calibrated."
        );
    } else {
        println!("✓ grading looks trustworthy (a spot check, not a guarantee)");
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    const ANCHOR_DECK_HEAD: &str =
        "---\nformat-version: 1\nid: \"deck-regionregionregionregion\"\n---\n\n";

    fn anchor_deck(dir: &tempfile::TempDir, body: &str) -> PathBuf {
        let path = dir.path().join("d.md");
        std::fs::write(
            &path,
            format!(
                "{ANCHOR_DECK_HEAD}## q\n---\n{body}<!-- id: card-regionregionregionregionre -->\n"
            ),
        )
        .unwrap();
        path
    }

    #[test]
    fn kept_invisibles_draw_one_calm_note_and_nothing_warns() {
        let dir = tempfile::tempdir().unwrap();
        let path = anchor_deck(&dir, "an\u{200B}swer with a soft\u{00AD}hyphen\n");
        let mut report = Report::default();
        deck_findings(&path, &mut report);
        let note = report
            .notes
            .iter()
            .find(|n| n.contains("invisible"))
            .expect("one calm note names the invisible bytes");
        assert!(
            note.contains("ZSWP 1") || note.contains("ZWSP 1"),
            "counts by class: {note}"
        );
        assert!(note.contains("SHY 1"), "counts by class: {note}");
        assert!(
            report.warnings.iter().all(|w| !w.contains("invisible")),
            "legitimate invisibles never warn: {:?}",
            report.warnings
        );
    }

    #[test]
    fn an_emoji_rich_deck_draws_no_invisible_note() {
        let dir = tempfile::tempdir().unwrap();
        let path = anchor_deck(
            &dir,
            "\u{1F469}\u{200D}\u{1F680} and \u{1F44D}\u{FE0F} and \u{1F3F4}\u{E0067}\u{E0062}\u{E0073}\u{E0063}\u{E0074}\u{E007F}\n",
        );
        let mut report = Report::default();
        deck_findings(&path, &mut report);
        assert!(
            report.notes.iter().all(|n| !n.contains("invisible")),
            "an emoji-only deck is supposed to look like that: {:?}",
            report.notes
        );
    }

    #[test]
    fn stray_tag_characters_warn_about_invisible_text() {
        let dir = tempfile::tempdir().unwrap();
        let path = anchor_deck(&dir, "pay\u{E0067}\u{E0061}load\n");
        let mut report = Report::default();
        deck_findings(&path, &mut report);
        assert!(
            report.warnings.iter().any(|w| w.contains("invisible text")),
            "tag characters outside a flag are the stealth shape: {:?}",
            report.warnings
        );
    }

    #[test]
    fn a_fence_holding_reversal_overrides_warns_and_embeddings_do_not() {
        let dir = tempfile::tempdir().unwrap();
        let path = anchor_deck(&dir, "```\nx\u{202E}y\n```\n");
        let mut report = Report::default();
        deck_findings(&path, &mut report);
        assert!(
            report.warnings.iter().any(|w| w.contains("rendered order")),
            "the Trojan Source shape warns factually: {:?}",
            report.warnings
        );

        let calm = anchor_deck(&dir, "```\nx\u{202A}y\u{202C}\n```\n");
        let mut calm_report = Report::default();
        deck_findings(&calm, &mut calm_report);
        assert!(
            calm_report
                .warnings
                .iter()
                .all(|w| !w.contains("rendered order")),
            "embeddings in a fence are not the reversal shape: {:?}",
            calm_report.warnings
        );
    }

    #[test]
    fn explicit_workspace_repairs_are_not_scheduled_twice() {
        let workspace_dir = tempfile::tempdir().unwrap();
        std::fs::write(workspace_dir.path().join(workspace::MANIFEST), "").unwrap();
        let plain_dir = tempfile::tempdir().unwrap();

        assert!(!repair_after_explicit_path(Some(workspace_dir.path())));
        assert!(repair_after_explicit_path(Some(plain_dir.path())));
        assert!(repair_after_explicit_path(None));
    }

    /// Ruled D13(ii): a filename-named deck is sanctioned, so doctor must
    /// stay silent about a missing `title:`. A finding here would nag on
    /// 444 of the 621 initialized decks in the maintainer's own library.
    #[test]
    fn an_untitled_deck_draws_no_missing_title_finding() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("Eng-Sayings.md");
        std::fs::write(
            &path,
            "---\nformat-version: 1\nid: \"deck-3nmmy2qkrw2trvvrmbm7ajry9t\"\n---\n## q <!-- id: card-4jkya9q3m8z0tw5v9y2b4n6d8f -->\na\n",
        )
        .unwrap();
        let mut report = Report::default();
        deck_findings(&path, &mut report);
        let mentions_title = report
            .warnings
            .iter()
            .chain(report.errors.iter())
            .any(|line| line.to_lowercase().contains("title"));
        assert!(
            !mentions_title,
            "no title finding may exist: {:?} {:?}",
            report.warnings, report.errors
        );
    }

    #[test]
    fn a_diverged_anchor_is_reported_with_both_edits() {
        let dir = tempfile::tempdir().unwrap();
        let path = anchor_deck(
            &dir,
            "target and target\n<!-- blank: span hidden=\"target\" occurrence=2 b:a1b2c3 position:1 -->\n",
        );
        let mut report = Report::default();
        deck_findings(&path, &mut report);
        let warning = report
            .warnings
            .iter()
            .find(|w| w.contains("--repair-positions"))
            .expect("the divergence warns");
        assert!(warning.contains("position:12"), "{warning}");
        assert!(
            warning.contains("`occurrence=1`"),
            "the anchor still starts occurrence 1, so the keep-old-target edit prints: {warning}"
        );
    }

    #[test]
    fn a_stale_anchor_offers_only_the_keep_authored_edit() {
        let dir = tempfile::tempdir().unwrap();
        let path = anchor_deck(
            &dir,
            "alpha target\n<!-- blank: span hidden=\"target\" b:a1b2c3 position:2 -->\n",
        );
        let mut report = Report::default();
        deck_findings(&path, &mut report);
        let warning = report
            .warnings
            .iter()
            .find(|w| w.contains("--repair-positions"))
            .expect("the stale anchor warns");
        assert!(
            !warning.contains("occurrence="),
            "a stale offset has no old target to keep: {warning}"
        );
    }

    #[test]
    fn an_aligned_anchor_is_silent() {
        let dir = tempfile::tempdir().unwrap();
        let path = anchor_deck(
            &dir,
            "prose target\n<!-- blank: span hidden=\"target\" b:a1b2c3 position:7 -->\n",
        );
        let mut report = Report::default();
        deck_findings(&path, &mut report);
        assert!(
            !report
                .warnings
                .iter()
                .any(|w| w.contains("--repair-positions")),
            "{:?}",
            report.warnings
        );
    }

    #[test]
    fn repair_positions_applies_the_keep_authored_edit_and_settles() {
        let dir = tempfile::tempdir().unwrap();
        let path = anchor_deck(
            &dir,
            "target and target\n<!-- blank: span hidden=\"target\" occurrence=2 b:a1b2c3 position:1 -->\n",
        );
        repair_positions(std::slice::from_ref(&path)).unwrap();
        let text = std::fs::read_to_string(&path).unwrap();
        assert!(
            text.contains("occurrence=2 b:a1b2c3 position:12 -->"),
            "{text}"
        );
        let mut report = Report::default();
        deck_findings(&path, &mut report);
        assert!(
            !report
                .warnings
                .iter()
                .any(|w| w.contains("--repair-positions")),
            "repair settles the anchor: {:?}",
            report.warnings
        );
    }

    #[test]
    fn repair_positions_rewrites_only_the_anchor_token_when_hidden_contains_it() {
        let dir = tempfile::tempdir().unwrap();
        let path = anchor_deck(
            &dir,
            "position:13 position:1\n<!-- blank: span hidden=\"position:1\" b:a1b2c3 position:1 -->\n",
        );

        repair_positions(std::slice::from_ref(&path)).unwrap();

        let text = std::fs::read_to_string(&path).unwrap();
        assert!(
            text.contains("<!-- blank: span hidden=\"position:1\" b:a1b2c3 position:13 -->"),
            "repair must preserve the authored hidden text and rewrite the parsed anchor: {text}"
        );
    }

    #[test]
    fn repair_positions_accepts_the_same_unicode_whitespace_as_region_parser() {
        let dir = tempfile::tempdir().unwrap();
        let path = anchor_deck(
            &dir,
            "target and target\n<!-- blank: span hidden=\"target\" occurrence=2 b:a1b2c3\u{a0}position:1 -->\n",
        );

        repair_positions(std::slice::from_ref(&path)).unwrap();

        let text = std::fs::read_to_string(&path).unwrap();
        assert!(text.contains("b:a1b2c3\u{a0}position:12 -->"), "{text}");
    }

    #[test]
    fn every_offered_keep_old_target_edit_keeps_the_deck_parseable() {
        let dir = tempfile::tempdir().unwrap();
        let path = anchor_deck(
            &dir,
            "target and target\n<!-- blank: span hidden=\"target\" occurrence=2 b:a1b2c3 position:1 -->\n<!-- blank: span hidden=\"target\" occurrence=1 b:d4e5f6 position:1 -->\n",
        );
        let mut report = Report::default();

        deck_findings(&path, &mut report);

        let warning = report
            .warnings
            .iter()
            .find(|warning| warning.contains("--repair-positions"))
            .expect("the first span diverges");
        if warning.contains("set `occurrence=1`") {
            let text = std::fs::read_to_string(&path).unwrap();
            let edited = text.replacen("occurrence=2", "occurrence=1", 1);
            alix::parser::parse("d", &edited).expect(
                "an edit doctor offers as the keep-old-target resolution must keep the deck parseable",
            );
        }
    }

    #[test]
    fn value_names_are_the_documented_clap_spellings() {
        assert_eq!("typeline", val_name(alix::answer::Mode::TypeLine));
        assert_eq!("sequential", val_name(alix::session::Order::Sequential));
    }

    #[test]
    fn report_render_child() {
        if std::env::var_os("ALIX_DOCTOR_REPORT_RENDER_CHILD").is_none() {
            return;
        }

        let mut errors = Report::default();
        errors.error("broken");
        assert!(errors.render());

        let mut warnings = Report::default();
        warnings.warn("risky");
        assert!(!warnings.render());

        let mut notes = Report::default();
        notes.note("informational");
        assert!(!notes.render());
    }

    #[test]
    fn report_summaries_cover_errors_and_warnings_but_not_notes() {
        let output = std::process::Command::new(std::env::current_exe().unwrap())
            .args([
                "--exact",
                "doctor::tests::report_render_child",
                "--nocapture",
            ])
            .env("ALIX_DOCTOR_REPORT_RENDER_CHILD", "1")
            .output()
            .unwrap();
        assert!(output.status.success(), "{output:?}");
        let stderr = String::from_utf8(output.stderr).unwrap();

        assert!(stderr.contains("1 error(s), 0 warning(s)"), "{stderr}");
        assert!(stderr.contains("0 error(s), 1 warning(s)"), "{stderr}");
        assert_eq!(2, stderr.matches("error(s),").count(), "{stderr}");
    }

    /// A decks folder holds what the user puts there. Doctor reports a
    /// parse failure only for a file that claims to be a deck.
    #[test]
    fn a_parse_failure_is_reported_only_for_files_that_claim_deckhood() {
        for (name, text, reported) in [
            ("notes.md", "Just my notes.\n\nNothing about alix.\n", false),
            (
                "broken.md",
                "---\ntitle: Broken\n---\n\nstray prose\n\n## q\na\n",
                true,
            ),
        ] {
            let dir = tempfile::tempdir().unwrap();
            w(dir.path(), name, text);
            let mut report = Report::default();
            deck_findings(&dir.path().join(name), &mut report);
            assert_eq!(
                reported,
                !report.errors.is_empty(),
                "{name} should{} report, got {:#?}",
                if reported { "" } else { " not" },
                report.errors
            );
        }
    }

    #[test]
    fn initialized_deck_content_is_not_reported_as_uninitialized() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("ready.md");
        w(
            dir.path(),
            "ready.md",
            "---\nformat-version: 1\nid: deck-ready\n---\n## q <!-- id: card-ready -->\na\n",
        );

        let mut report = Report::default();
        deck_findings(&path, &mut report);

        assert!(
            report
                .warnings
                .iter()
                .all(|warning| !warning.contains("deck-like Markdown is not initialized")),
            "{:#?}",
            report.warnings
        );
    }

    #[test]
    fn unique_noncanonical_card_tokens_are_still_checked() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("tokens.md");
        w(
            dir.path(),
            "tokens.md",
            "---\nformat-version: 1\nid: deck-ready\n---\n## q\na\n<!-- id: card-short -->\n",
        );

        let mut report = Report::default();
        deck_findings(&path, &mut report);

        assert!(
            report
                .warnings
                .iter()
                .any(|warning| warning.contains("id `card-short`")
                    && warning.contains("not canonical")),
            "{:#?}",
            report.warnings
        );
    }

    #[test]
    fn initialized_flow_frontmatter_is_not_reported_as_unspliceable() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("flow.md");
        w(
            dir.path(),
            "flow.md",
            "---\n{format-version: 1, id: deck-ready}\n---\n## q <!-- id: card-ready -->\na\n",
        );

        let mut report = Report::default();
        deck_findings(&path, &mut report);

        assert!(
            report
                .warnings
                .iter()
                .all(|warning| !warning.contains("cannot stamp: frontmatter")),
            "{:#?}",
            report.warnings
        );
    }

    #[test]
    fn initialized_fully_stamped_decks_have_no_unstamped_warning() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("stamped.md");
        w(
            dir.path(),
            "stamped.md",
            "---\nformat-version: 1\nid: deck-ready\n---\n## q <!-- id: card-ready -->\na\n",
        );

        let mut report = Report::default();
        deck_findings(&path, &mut report);

        assert!(
            report
                .warnings
                .iter()
                .all(|warning| !warning.contains("card content without ids")),
            "{:#?}",
            report.warnings
        );
    }

    #[test]
    fn only_local_paths_inside_assets_are_asset_sources() {
        assert!(source_points_into_assets("assets/deck-one/object.md"));
        assert!(source_points_into_assets(
            "workspace/assets/deck-one/object.md"
        ));
        assert!(!source_points_into_assets("notes/assets-overview.md"));
        assert!(!source_points_into_assets(
            "https://example.test/assets/deck-one/object.md"
        ));
    }

    #[test]
    fn trace_and_source_count_warnings_obey_their_exact_boundaries() {
        let dir = tempfile::tempdir().unwrap();
        let ordinary = dir.path().join("ordinary.md");
        w(dir.path(), "ordinary.md", "## q\na\n");
        let mut ordinary_report = Report::default();
        deck_findings(&ordinary, &mut ordinary_report);
        assert!(
            ordinary_report
                .warnings
                .iter()
                .all(|warning| !warning.contains("not a trace")),
            "{:#?}",
            ordinary_report.warnings
        );

        let three = dir.path().join("three.md");
        w(
            dir.path(),
            "three.md",
            "---\nsource: [a, b, c]\n---\n## q\na\n",
        );
        let four = dir.path().join("four.md");
        w(
            dir.path(),
            "four.md",
            "---\nsource: [a, b, c, d]\n---\n## q\na\n",
        );
        let mut report = Report::default();
        deck_findings(&three, &mut report);
        deck_findings(&four, &mut report);
        let common_root: Vec<&String> = report
            .warnings
            .iter()
            .filter(|warning| warning.contains("point at their common root"))
            .collect();
        assert_eq!(1, common_root.len(), "{:#?}", report.warnings);
        assert!(common_root[0].contains("four.md"), "{common_root:?}");
    }

    #[test]
    fn coarse_restamp_warning_needs_both_orphans_and_fresh_cards() {
        let dir = tempfile::tempdir().unwrap();
        let deck = dir.path().join("facts.md");
        w(
            dir.path(),
            "facts.md",
            "---\nformat-version: 1\nid: deck-facts\n---\n## q\na\n<!-- id: card-live -->\n",
        );
        let mut store = alix::state::open_store(&deck, dir.path()).unwrap();
        store.get_or_insert("card-live");
        store.get_or_insert("card-orphan");
        store.save().unwrap();

        let report = workspace_findings(dir.path());

        assert!(
            report
                .warnings
                .iter()
                .all(|warning| !warning.contains("fresh tokens were minted")),
            "{:#?}",
            report.warnings
        );
    }

    #[test]
    fn document_scan_skips_directories_non_json_files_and_conflicts() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::create_dir_all(dir.path().join("progress/folder.json")).unwrap();
        w(&dir.path().join("progress"), "notes.txt", "not a document");
        w(
            &dir.path().join("progress"),
            "deck-x.sync-conflict-1.json",
            "not json",
        );

        let report = workspace_findings(dir.path());
        let findings = report
            .warnings
            .iter()
            .chain(&report.errors)
            .cloned()
            .collect::<Vec<_>>()
            .join("\n");

        assert!(!findings.contains("folder.json"), "{findings}");
        assert!(!findings.contains("notes.txt"), "{findings}");
        assert!(
            !findings.contains("unrecognized progress document"),
            "{findings}"
        );
    }

    #[test]
    fn an_augmentation_document_in_progress_is_validated_as_progress() {
        let dir = tempfile::tempdir().unwrap();
        let progress = dir.path().join("progress/deck-orphan.json");
        alix::augment::AugmentCache::open_deck(&progress, "deck-orphan")
            .unwrap()
            .save()
            .unwrap();

        let report = workspace_findings(dir.path());

        assert!(
            report
                .errors
                .iter()
                .any(|error| error.contains("progress document")),
            "{:#?}",
            report.errors
        );
    }

    #[test]
    fn plain_nested_directories_are_not_recursed_as_workspaces() {
        let dir = tempfile::tempdir().unwrap();
        let nested = dir.path().join("plain-folder");
        std::fs::create_dir(&nested).unwrap();
        w(&nested, "loose.md", "## q\na\n");

        let report = workspace_findings(dir.path());

        assert!(
            report
                .warnings
                .iter()
                .all(|warning| !warning.contains("plain-folder")),
            "{:#?}",
            report.warnings
        );
    }

    #[test]
    fn check_rejects_a_directory_without_a_workspace_manifest() {
        let dir = tempfile::tempdir().unwrap();

        assert!(check(vec![dir.path().to_path_buf()]).is_err());
    }

    #[test]
    fn check_output_child() {
        if std::env::var_os("ALIX_DOCTOR_CHECK_OUTPUT_CHILD").is_none() {
            return;
        }
        let dir = tempfile::tempdir().unwrap();
        let declared = dir.path().join("declared.md");
        w(
            dir.path(),
            "declared.md",
            "---\nformat-version: 1\nid: deck-declared\nreveal: line\nreview: sequential\nrequires: ghost\nsource: https://example.test/source\n---\n## q\na\n<!-- id: card-q -->\n",
        );
        let plain = dir.path().join("plain.md");
        w(dir.path(), "plain.md", "## q\na\n");
        let empty = dir.path().join("empty.md");
        w(
            dir.path(),
            "empty.md",
            "---\nformat-version: 1\nid: deck-empty\n---\n## q\na\n<!-- id: card-qempty -->\n",
        );
        check(vec![declared, empty, plain]).unwrap();

        let workspace = dir.path().join("workspace");
        std::fs::create_dir(&workspace).unwrap();
        w(
            &workspace,
            alix::workspace::MANIFEST,
            "icon = \"gone.svg\"\n",
        );
        check(vec![workspace]).unwrap();
    }

    #[test]
    fn check_prints_only_declared_sections_and_warns_for_a_missing_icon() {
        let output = std::process::Command::new(std::env::current_exe().unwrap())
            .args([
                "--exact",
                "doctor::tests::check_output_child",
                "--nocapture",
            ])
            .env("ALIX_DOCTOR_CHECK_OUTPUT_CHILD", "1")
            .output()
            .unwrap();
        assert!(output.status.success(), "{output:?}");
        let stdout = String::from_utf8(output.stdout).unwrap();
        let stderr = String::from_utf8(output.stderr).unwrap();

        assert_eq!(1, stdout.matches("  settings:").count(), "{stdout}");
        assert!(
            stdout.contains("reveal: line, review: sequential"),
            "{stdout}"
        );
        assert_eq!(1, stdout.matches("  requires:").count(), "{stdout}");
        assert_eq!(1, stdout.matches("  sources:").count(), "{stdout}");
        assert!(stderr.contains("points at a missing file"), "{stderr}");
    }

    fn one_line_fingerprint(text: &str) -> String {
        let excerpt = alix::source::Excerpt {
            path: PathBuf::from("source.txt"),
            lines: vec![(1, text.to_string())],
            truncated: false,
        };
        alix::source::format_locator_fingerprint(alix::source::excerpt_fingerprint(&excerpt))
    }

    #[test]
    fn one_authored_locator_is_diagnosed_once_however_many_cards_it_serves() {
        let dir = tempfile::tempdir().unwrap();
        w(dir.path(), "source.txt", "first\nsecond\n");
        w(
            dir.path(),
            "cloze.md",
            "---\nformat-version: 1\nid: deck-cloze\nsource: .\n---\n\
             ## q\nthe first and the second\n\
             <!-- blank: span hidden=\"first\" b:a1b2c3 -->\n\
             <!-- blank: span hidden=\"second\" b:d4e5f6 -->\n\
             <!-- at: source.txt:1-2 -->\n<!-- id: card-q -->\n",
        );
        let deck = Deck::load(dir.path().join("cloze.md")).unwrap();
        assert_eq!(2, deck.cards.len(), "one card per span: {deck:?}");

        let mut report = Report::default();
        deck_resource_findings(&deck, &mut report);

        let citation_warnings = report
            .warnings
            .iter()
            .filter(|warning| warning.contains("at: source.txt:1-2"))
            .count();
        assert_eq!(
            1, citation_warnings,
            "two cards share one authored `at:` line, so it is reported once: {:#?}",
            report.warnings
        );
    }

    #[test]
    fn a_live_citation_never_silences_the_drift_check_on_its_frozen_neighbour() {
        let dir = tempfile::tempdir().unwrap();
        w(dir.path(), workspace::MANIFEST, "title = \"WS\"\n");
        std::fs::create_dir(dir.path().join(workspace::DECKS)).unwrap();
        w(dir.path(), "source.txt", "first\n");
        let deck_path = dir.path().join(workspace::DECKS).join("member.md");
        std::fs::write(
            &deck_path,
            format!(
                "---\nid: deck-1xpgnc8f1mypv80cgzyxrn2cqf\nsource: ..\n---\n\
                 ## stale\na\n<!-- at: source.txt:1 fingerprint: {} -->\n\
                 <!-- id: card-1xpgnc8f1mypv80cgzyxrn2cqf -->\n\n\
                 ## live\nb\n<!-- at: source.txt:1 -->\n\
                 <!-- id: card-2xpgnc8f1mypv80cgzyxrn2cqf -->\n",
                one_line_fingerprint("gone"),
            ),
        )
        .unwrap();
        let deck = Deck::load(&deck_path).unwrap();
        assert!(
            deck.deck_token.is_some(),
            "the fixture is a member: {deck:?}"
        );

        let mut report = Report::default();
        deck_resource_findings(&deck, &mut report);

        assert!(
            report
                .errors
                .iter()
                .any(|error| error.contains("live `at:` citation")),
            "the member still reports its unfrozen citations: {:#?}",
            report.errors
        );
        assert!(
            report
                .warnings
                .iter()
                .any(|warning| warning.contains("changed or disappeared")),
            "a live citation elsewhere in the deck must not turn off drift \
             detection for a fingerprinted one: {:#?}",
            report.warnings
        );
        assert!(
            !report
                .warnings
                .iter()
                .any(|warning| warning.contains("has no excerpt fingerprint")),
            "the live citations are already one error, not one warning each: {:#?}",
            report.warnings
        );
    }

    #[test]
    fn a_frozen_citation_that_lost_its_fingerprint_is_not_covered_by_the_live_error() {
        let dir = tempfile::tempdir().unwrap();
        w(dir.path(), workspace::MANIFEST, "title = \"WS\"\n");
        std::fs::create_dir(dir.path().join(workspace::DECKS)).unwrap();
        w(dir.path(), "source.txt", "first\nsecond\n");
        let deck_path = dir.path().join(workspace::DECKS).join("member.md");
        std::fs::write(
            &deck_path,
            "---\nid: deck-1xpgnc8f1mypv80cgzyxrn2cqf\nsource: ..\n---\n\
             ## frozen\na\n<!-- at: source.txt:1 -->\n\
             <!-- id: card-1xpgnc8f1mypv80cgzyxrn2cqf -->\n\n\
             ## live\nb\n<!-- at: source.txt:2 -->\n\
             <!-- id: card-2xpgnc8f1mypv80cgzyxrn2cqf -->\n",
        )
        .unwrap();
        alix::assets::freeze_member(&deck_path).unwrap();

        let frozen = std::fs::read_to_string(&deck_path).unwrap();
        let unfingerprinted: String = frozen
            .lines()
            .map(|line| match line.split_once(" fingerprint: ") {
                Some((head, rest)) if line.contains("source.txt:1") => {
                    let asset = rest
                        .split_once(" asset: ")
                        .expect("freezing wrote an asset")
                        .1;
                    format!("{head} asset: {asset}")
                }
                _ if line.contains("source.txt:2") => {
                    let (head, _) = line.split_once(" fingerprint: ").expect("frozen too");
                    format!("{head} -->")
                }
                _ => line.to_string(),
            })
            .collect::<Vec<_>>()
            .join("\n");
        std::fs::write(&deck_path, format!("{unfingerprinted}\n")).unwrap();
        let deck = Deck::load(&deck_path).unwrap();

        let mut report = Report::default();
        deck_resource_findings(&deck, &mut report);

        assert!(
            report
                .errors
                .iter()
                .any(|error| error.contains("1 live `at:` citation")),
            "one citation lost both fields, so exactly one is live: {:#?}",
            report.errors
        );
        assert!(
            report
                .warnings
                .iter()
                .any(|warning| warning.contains("has no excerpt fingerprint")),
            "the citation that kept its asset is not one of the live ones, so the \
             deck-level error does not cover it: {:#?}",
            report.warnings
        );
    }

    #[test]
    fn a_healthy_citation_never_hides_a_stale_one_on_the_same_card() {
        let dir = tempfile::tempdir().unwrap();
        w(dir.path(), "source.txt", "first\n");
        w(
            dir.path(),
            "twin.md",
            &format!(
                "---\nformat-version: 1\nid: deck-twin\nsource: .\n---\n\
                 ## q\na\n<!-- at: source.txt:1 fingerprint: {} -->\n\
                 <!-- at: source.txt:1 fingerprint: {} -->\n<!-- id: card-q -->\n",
                one_line_fingerprint("first"),
                one_line_fingerprint("gone"),
            ),
        );
        let deck = Deck::load(dir.path().join("twin.md")).unwrap();

        let mut report = Report::default();
        deck_resource_findings(&deck, &mut report);

        assert!(
            report
                .warnings
                .iter()
                .any(|warning| warning.contains("changed or disappeared")),
            "the healthy first citation must not silence the stale second \
             authored line: {:#?}",
            report.warnings
        );
    }

    #[test]
    fn repair_counts_a_changed_citation_as_one_unresolved_item() {
        let dir = tempfile::tempdir().unwrap();
        w(dir.path(), "source.txt", "changed\n");
        let deck = dir.path().join("changed.md");
        w(
            dir.path(),
            "changed.md",
            &format!(
                "---\nformat-version: 1\nid: deck-changed\nsource: .\n---\n## q\na\n<!-- at: source.txt:1 fingerprint: {} -->\n<!-- id: card-q -->\n",
                one_line_fingerprint("gone")
            ),
        );

        let error = repair_source_locators(std::slice::from_ref(&deck)).unwrap_err();

        assert!(
            error
                .to_string()
                .contains("1 source citation(s) need manual review"),
            "{error:#}"
        );
    }

    #[test]
    fn repair_counts_an_ambiguous_citation_as_one_unresolved_item() {
        let dir = tempfile::tempdir().unwrap();
        w(dir.path(), "source.txt", "other\ntarget\nother\ntarget\n");
        let deck = dir.path().join("ambiguous.md");
        w(
            dir.path(),
            "ambiguous.md",
            &format!(
                "---\nformat-version: 1\nid: deck-ambiguous\nsource: .\n---\n## q\na\n<!-- at: source.txt:1 fingerprint: {} -->\n<!-- id: card-q -->\n",
                one_line_fingerprint("target")
            ),
        );

        let error = repair_source_locators(std::slice::from_ref(&deck)).unwrap_err();

        assert!(
            error
                .to_string()
                .contains("1 source citation(s) need manual review"),
            "{error:#}"
        );
    }

    #[test]
    #[cfg(unix)]
    fn grading_spot_check_child() {
        use std::os::unix::fs::PermissionsExt;

        let Some(mode) = std::env::var_os("ALIX_GRADING_SPOT_CHECK_CHILD") else {
            return;
        };
        let mode = mode.to_string_lossy();
        let dir = tempfile::tempdir().unwrap();
        let script = dir.path().join("fake-grader");
        let body = format!(
            r#"#!/bin/sh
cat > "{dir}/prompt.$$"
echo x >> "{dir}/calls.log"
call=$(wc -l < "{dir}/calls.log")
n=$(grep -c '^Question ' "{dir}/prompt.$$")
printf '{{"grades":['
i=1
while [ "$i" -le "$n" ]; do
  [ "$i" -gt 1 ] && printf ','
  verdict="{mode}"
  if [ "$verdict" = mixed ]; then
    if [ "$n" -gt 1 ]; then
      case "$i" in
        4|5|6) verdict=pass ;;
        *) verdict=partial ;;
      esac
    elif [ "$call" -eq 2 ]; then
      verdict=partial
    else
      verdict=pass
    fi
  fi
  printf '{{"verdict":"%s","feedback":"f","missed":[]}}' "$verdict"
  i=$((i+1))
done
printf ']}}'
"#,
            dir = dir.path().display(),
            mode = mode,
        );
        std::fs::write(&script, body).unwrap();
        std::fs::set_permissions(&script, std::fs::Permissions::from_mode(0o755)).unwrap();
        let mut config = alix::config::Config::from_toml("").unwrap();
        config.ask.command = script.display().to_string();
        config.ask.timeout_secs = 10;

        grading_spot_check(&config).unwrap();
    }

    #[test]
    #[cfg(unix)]
    fn grading_spot_check_counts_each_failure_kind_and_the_all_safe_case() {
        let run = |mode: &str| {
            let output = std::process::Command::new(std::env::current_exe().unwrap())
                .args([
                    "--exact",
                    "doctor::tests::grading_spot_check_child",
                    "--nocapture",
                ])
                .env("ALIX_GRADING_SPOT_CHECK_CHILD", mode)
                .output()
                .unwrap();
            assert!(output.status.success(), "{output:?}");
            String::from_utf8(output.stdout).unwrap()
        };

        let unsafe_output = run("pass");
        assert!(
            unsafe_output.contains("passed 7 answer(s) that must not pass"),
            "{unsafe_output}"
        );
        let unfair_output = run("partial");
        assert!(
            unfair_output.contains("stricter than intended: 4 should-pass probe(s)"),
            "{unfair_output}"
        );
        let safe_output = run("mixed");
        assert!(
            safe_output.contains("grading looks trustworthy"),
            "{safe_output}"
        );
    }

    #[test]
    fn repair_flag_controls_plain_directory_repair_exactly_once() {
        let dir = tempfile::tempdir().unwrap();
        w(
            dir.path(),
            "config.toml",
            "[ask]\ncommand = \"/missing/alix-test-backend\"\n",
        );
        w(dir.path(), "source.txt", "evidence\n");
        let deck = dir.path().join("facts.md");
        w(
            dir.path(),
            "facts.md",
            "---\nformat-version: 1\nid: deck-facts\nsource: .\n---\n## q\na\n<!-- at: source.txt:1 -->\n<!-- id: card-q -->\n<!-- reveal: line -->\n",
        );
        let before = std::fs::read_to_string(&deck).unwrap();
        let args = |repair_source_locators| DoctorArgs {
            normalize: false,
            repair_positions: false,
            repair_diagrams: false,
            repair_frontmatter_order: false,
            repair_comment_order: false,
            dir: Some(dir.path().to_path_buf()),
            backends: false,
            all_backends: false,
            grading: false,
            repair_source_locators,
            remove_backup_files: false,
            yes: false,
            config: Some(dir.path().join("config.toml")),
        };

        doctor_cmd(args(false)).unwrap();
        assert_eq!(before, std::fs::read_to_string(&deck).unwrap());

        doctor_cmd(args(true)).unwrap();
        let repaired = std::fs::read_to_string(&deck).unwrap();
        assert_ne!(before, repaired);
        assert!(repaired.contains("fingerprint: xxh64-"), "{repaired}");
    }

    #[test]
    fn check_warns_on_a_missing_workspace_icon() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join("alix.toml"), "icon = \"assets/gone.svg\"\n").unwrap();
        std::fs::write(dir.path().join("a.md"), "## a\n1\n").unwrap();
        assert!(check(vec![dir.path().to_path_buf()]).is_ok());
    }

    #[test]
    fn check_warns_on_a_malformed_deadline() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join("alix.toml"), "").unwrap();
        std::fs::write(
            dir.path().join("alix.local.toml"),
            "[review]\ndeadline = \"soonish\"\n",
        )
        .unwrap();
        std::fs::write(dir.path().join("a.md"), "## a\n1\n").unwrap();
        assert!(check(vec![dir.path().to_path_buf()]).is_ok());
    }

    #[test]
    fn workspace_findings_report_a_root_deck_without_reading_it_as_a_member() {
        let dir = tempfile::tempdir().unwrap();
        w(dir.path(), "alix.toml", "");
        w(
            dir.path(),
            "misplaced.md",
            "---\nformat-version: 1\nid: deck-misplaced\n---\n## q <!-- id: card-q1 -->\na\n",
        );

        let report = workspace_findings(dir.path());
        assert!(
            report
                .warnings
                .iter()
                .any(|warning| warning.contains("workspace decks belong in")
                    && warning.contains("not discovered"))
        );
    }

    #[test]
    fn doctor_rejects_an_initialized_workspace_member_with_live_source_without_mutating_it() {
        let dir = tempfile::tempdir().unwrap();
        let decks = dir.path().join(alix::workspace::DECKS);
        std::fs::create_dir(&decks).unwrap();
        w(dir.path(), alix::workspace::MANIFEST, "");
        w(dir.path(), "notes.md", "evidence\n");
        let path = decks.join("facts.md");
        std::fs::write(
            &path,
            "---\nformat-version: 1\nid: deck-deck1\nsource: notes.md\n---\n## q\na\n<!-- at: notes.md:1 -->\n<!-- id: card-card1 -->\n",
        )
        .unwrap();
        let before = std::fs::read(&path).unwrap();

        let report = workspace_findings(dir.path());

        assert!(
            report.errors.join("\n").contains("live `at:` citation"),
            "{:#?}",
            report.errors
        );
        assert_eq!(before, std::fs::read(&path).unwrap());
        assert!(!dir.path().join(alix::assets::ROOT).exists());
    }

    #[test]
    fn doctor_rejects_a_frozen_asset_whose_bytes_do_not_match_its_name() {
        let dir = tempfile::tempdir().unwrap();
        let decks = dir.path().join(alix::workspace::DECKS);
        let assets = dir.path().join("assets/deck-deck1");
        std::fs::create_dir(&decks).unwrap();
        std::fs::create_dir_all(&assets).unwrap();
        w(dir.path(), alix::workspace::MANIFEST, "");
        let name = alix::assets::object_name(b"expected\n", "txt");
        std::fs::write(assets.join(&name), "changed\n").unwrap();
        std::fs::write(
            decks.join("facts.md"),
            format!(
                "---\nformat-version: 1\nid: deck-deck1\nsource: assets/deck-deck1/{name}\n---\n\
                 ## q\na\n<!-- id: card-card1 -->\n"
            ),
        )
        .unwrap();

        let report = workspace_findings(dir.path());

        assert!(
            report
                .errors
                .join("\n")
                .contains("does not match its content address")
        );
    }

    #[test]
    fn doctor_rejects_an_image_owned_by_another_deck() {
        let dir = tempfile::tempdir().unwrap();
        let decks = dir.path().join(alix::workspace::DECKS);
        let other_assets = dir.path().join("assets/deck-deck2");
        std::fs::create_dir(&decks).unwrap();
        std::fs::create_dir_all(&other_assets).unwrap();
        w(dir.path(), alix::workspace::MANIFEST, "");
        let name = alix::assets::object_name(b"image", "png");
        std::fs::write(other_assets.join(&name), "image").unwrap();
        std::fs::write(
            decks.join("facts.md"),
            format!(
                "---\nformat-version: 1\nid: deck-deck1\n---\n\
                 ## q\n![diagram](assets/deck-deck2/{name})\na\n<!-- id: card-card1 -->\n"
            ),
        )
        .unwrap();

        let report = workspace_findings(dir.path());

        assert!(
            report
                .errors
                .join("\n")
                .contains("is not a valid deck-owned asset")
        );
    }

    fn w(dir: &Path, name: &str, content: &str) {
        std::fs::write(dir.join(name), content).unwrap();
    }

    #[test]
    fn doctor_notes_inline_emphasis_but_not_plain_text() {
        let dir = tempfile::tempdir().unwrap();
        let affected = dir.path().join("affected.md");
        let plain = dir.path().join("plain.md");
        w(
            dir.path(),
            "affected.md",
            "## Multiply three numbers\n2*3*4\n",
        );
        w(dir.path(), "plain.md", "## Add two numbers\n2 + 3 + 4\n");

        let mut affected_report = Report::default();
        deck_findings(&affected, &mut affected_report);
        assert_eq!(affected_report.notes.len(), 1);
        assert!(affected_report.notes[0].contains("Multiply three numbers"));
        assert!(affected_report.notes[0].contains(": 3 "));

        let mut plain_report = Report::default();
        deck_findings(&plain, &mut plain_report);
        assert!(plain_report.notes.is_empty());
    }

    #[test]
    fn doctor_reports_each_malformed_formula_but_not_valid_or_literal_dollars() {
        let dir = tempfile::tempdir().unwrap();
        let malformed = dir.path().join("malformed.md");
        let valid = dir.path().join("valid.md");
        w(
            dir.path(),
            "malformed.md",
            "## Front $\\frac{1$\n\
             - [x] $\\sqrt{$\n\
             - [ ] $\\left($\n\
             > note $\\begin{pmatrix}$\n",
        );
        w(
            dir.path(),
            "valid.md",
            "## Valid $x^2$ and literal prices\n$5 and $10 with unmatched $x\n",
        );

        let mut report = Report::default();
        deck_findings(&malformed, &mut report);
        deck_findings(&valid, &mut report);

        let math_warnings: Vec<&String> = report
            .warnings
            .iter()
            .filter(|warning| warning.contains("malformed LaTeX math"))
            .collect();
        assert_eq!(4, math_warnings.len(), "{:#?}", report.warnings);
        assert!(
            math_warnings
                .iter()
                .all(|warning| warning.contains("malformed.md: card at line 1"))
        );
        assert!(
            math_warnings
                .iter()
                .any(|warning| warning.contains("\\frac{1"))
        );
        assert!(
            report
                .warnings
                .iter()
                .all(|warning| !warning.contains("valid.md: card at line"))
        );
    }

    #[test]
    fn doctor_reports_malformed_cached_choices_and_keypoints() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("cached.md");
        w(
            dir.path(),
            "cached.md",
            "---\nformat-version: 1\nid: deck-doctormathdeck\n---\n## q\na\n<!-- id: card-doctormath1 -->\n",
        );
        let parsed =
            alix::parser::parse("cached.md", &std::fs::read_to_string(&path).unwrap()).unwrap();
        let card = &parsed.cards[0];
        let id = card.id().unwrap();
        let deck = Deck::load(&path).unwrap();
        let mut augment = alix::augment::AugmentCache::open_for_deck(&deck).unwrap();
        augment.set_distractors(
            &id,
            vec![r"$\frac{1$".to_string()],
            card.content_fingerprint,
        );
        augment.set_keypoints(&id, vec![r"$\sqrt{$".to_string()], card.content_fingerprint);
        augment.save().unwrap();

        let mut report = Report::default();
        deck_findings(&path, &mut report);

        assert!(
            report
                .warnings
                .iter()
                .any(|warning| warning.contains("generated choice"))
        );
        assert!(
            report
                .warnings
                .iter()
                .any(|warning| warning.contains("generated keypoint"))
        );
    }

    #[test]
    fn doctor_flags_a_dangling_requires_but_not_a_resolvable_one() {
        let dir = tempfile::tempdir().unwrap();
        w(dir.path(), "base.md", "## a\n1\n");
        let dangling = dir.path().join("dangling.md");
        w(
            dir.path(),
            "dangling.md",
            "---\nrequires: ghost\n---\n## q\na\n",
        );
        let resolvable = dir.path().join("resolvable.md");
        w(
            dir.path(),
            "resolvable.md",
            "---\nrequires: base\n---\n## q\na\n",
        );

        let mut report = Report::default();
        deck_findings(&dangling, &mut report);
        deck_findings(&resolvable, &mut report);

        let dangling_warnings: Vec<&String> = report
            .warnings
            .iter()
            .filter(|warning| warning.contains("dangling prerequisite"))
            .collect();
        assert_eq!(1, dangling_warnings.len(), "{:#?}", report.warnings);
        assert!(
            dangling_warnings[0].contains("`ghost`"),
            "{}",
            dangling_warnings[0]
        );
    }

    const CANONICAL_ID: &str = "deck-9w2c7x4k1m8q3z5t0v6b2n4d8f";

    #[test]
    fn doctor_warns_when_a_requires_edge_resolves_to_an_unparseable_deck() {
        let dir = tempfile::tempdir().unwrap();
        w(
            dir.path(),
            "broken.md",
            &format!(
                "---\nformat-version: 1\nid: \"{CANONICAL_ID}\"\n---\n## q\na\n\
                 <!-- at: 29.rs @ xxh64:0123456789abcdef from src/x.rs:1-3 -->\n"
            ),
        );
        let by_name = dir.path().join("by-name.md");
        w(
            dir.path(),
            "by-name.md",
            "---\nrequires: broken\n---\n## q\na\n",
        );
        let by_id = dir.path().join("by-id.md");
        w(
            dir.path(),
            "by-id.md",
            &format!("---\nrequires: {CANONICAL_ID}\n---\n## q\na\n"),
        );

        let mut report = Report::default();
        deck_findings(&by_name, &mut report);
        deck_findings(&by_id, &mut report);

        let parse_warnings: Vec<&String> = report
            .warnings
            .iter()
            .filter(|warning| warning.contains("fails to parse; its errors name the fix"))
            .collect();
        assert_eq!(2, parse_warnings.len(), "{:#?}", report.warnings);
        assert!(parse_warnings[0].contains("`broken`"), "{parse_warnings:?}");
        assert!(
            parse_warnings[1].contains(&format!("`{CANONICAL_ID}`")),
            "{parse_warnings:?}"
        );
        assert!(
            !report
                .warnings
                .iter()
                .any(|warning| warning.contains("dangling prerequisite")),
            "{:#?}",
            report.warnings
        );
    }

    #[test]
    fn a_bare_token_in_requires_is_an_ordinary_dangling_filename_edge() {
        let dir = tempfile::tempdir().unwrap();
        let token = CANONICAL_ID.strip_prefix("deck-").unwrap();
        let path = dir.path().join("d.md");
        w(
            dir.path(),
            "d.md",
            &format!("---\nrequires: {token}\n---\n## q\na\n"),
        );

        let mut report = Report::default();
        deck_findings(&path, &mut report);

        assert!(
            report
                .warnings
                .iter()
                .any(|warning| warning.contains("dangling prerequisite")),
            "{:#?}",
            report.warnings
        );
        assert!(
            report.notes.iter().all(|note| !note.contains(token)),
            "no note singles the bare token out: {:#?}",
            report.notes
        );
    }

    #[test]
    fn doctor_warns_on_a_dangling_id_mode_requires_and_accepts_a_resolvable_one() {
        let dir = tempfile::tempdir().unwrap();
        w(
            dir.path(),
            "base.md",
            &format!(
                "---\nformat-version: 1\nid: \"{CANONICAL_ID}\"\n---\n## a <!-- id: card-b1 -->\n1\n"
            ),
        );
        let resolvable = dir.path().join("resolvable.md");
        w(
            dir.path(),
            "resolvable.md",
            &format!("---\nrequires: {CANONICAL_ID}\n---\n## q\na\n"),
        );
        let dangling = dir.path().join("dangling.md");
        w(
            dir.path(),
            "dangling.md",
            "---\nrequires: deck-zzzzzzzzzzzzzzzzzzzzzzzzzz\n---\n## q\na\n",
        );

        let mut report = Report::default();
        deck_findings(&resolvable, &mut report);
        deck_findings(&dangling, &mut report);

        let dangling_warnings: Vec<&String> = report
            .warnings
            .iter()
            .filter(|warning| warning.contains("dangling prerequisite"))
            .collect();
        assert_eq!(1, dangling_warnings.len(), "{:#?}", report.warnings);
        assert!(
            dangling_warnings[0].contains("deck-zzzzzzzzzzzzzzzzzzzzzzzzzz"),
            "{}",
            dangling_warnings[0]
        );
        assert!(
            dangling_warnings[0]
                .contains("write the `.md` extension (`deck-zzzzzzzzzzzzzzzzzzzzzzzzzz.md`)"),
            "{}",
            dangling_warnings[0]
        );
    }

    #[test]
    fn doctor_rejects_a_pasted_card_id_in_requires_as_wrong_type() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("d.md");
        w(
            dir.path(),
            "d.md",
            "---\nrequires: card-9w2c7x4k1m8q3z5t0v6b2n4d8f\n---\n## q\na\n",
        );

        let mut report = Report::default();
        deck_findings(&path, &mut report);

        let errors = report.errors.join("\n");
        assert!(errors.contains("card id"), "{errors}");
        assert!(errors.contains("never a prerequisite"), "{errors}");
    }

    #[test]
    fn doctor_hints_when_a_filename_mode_requires_looks_like_a_truncated_id() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("d.md");
        w(
            dir.path(),
            "d.md",
            "---\nrequires: deck-9w2c7x4k1m8q3z5t0v6b2n4d8\n---\n## q\na\n",
        );

        let mut report = Report::default();
        deck_findings(&path, &mut report);

        assert!(
            report
                .warnings
                .iter()
                .any(|warning| warning.contains("dangling prerequisite")),
            "{:#?}",
            report.warnings
        );
        assert!(
            report
                .notes
                .iter()
                .any(|note| note.contains("truncated or malformed")),
            "{:#?}",
            report.notes
        );
    }

    #[test]
    fn doctor_reports_a_file_shadowing_a_required_deck_id() {
        let dir = tempfile::tempdir().unwrap();
        w(
            dir.path(),
            "base.md",
            &format!(
                "---\nformat-version: 1\nid: \"{CANONICAL_ID}\"\n---\n## a <!-- id: card-b1 -->\n1\n"
            ),
        );
        w(
            dir.path(),
            &format!("{CANONICAL_ID}.md"),
            "## impostor\na\n",
        );
        let path = dir.path().join("d.md");
        w(
            dir.path(),
            "d.md",
            &format!("---\nrequires: {CANONICAL_ID}\n---\n## q\na\n"),
        );

        let mut report = Report::default();
        deck_findings(&path, &mut report);

        assert!(
            report
                .warnings
                .iter()
                .any(|warning| warning.contains("the id wins")
                    && warning.contains(&format!("write `{CANONICAL_ID}.md` to require the file"))),
            "{:#?}",
            report.warnings
        );
        assert!(
            !report
                .warnings
                .iter()
                .any(|warning| warning.contains("dangling prerequisite")),
            "{:#?}",
            report.warnings
        );
    }

    #[test]
    fn a_dormant_template_base_id_is_not_an_orphan() {
        let tmp = tempfile::tempdir().unwrap();
        let dir = tmp.path();
        let decks = dir.join("decks");
        std::fs::create_dir(&decks).unwrap();
        w(dir, "alix.toml", "title = \"Regions\"\n");
        w(
            &decks,
            "regions.md",
            "---\nformat-version: 1\nid: deck-regiondoc\n---\n## anatomy\nthe lunate is carpal\n<!-- blank: span hidden=\"lunate\" b:a1b2c3 -->\n<!-- id: card-parent1 -->\n",
        );
        let mut store = alix::store::Store::open_deck(
            dir.join("progress/deck-regiondoc.json"),
            "deck-regiondoc",
            "regions.md",
        )
        .unwrap();
        store.get_or_insert("card-parent1");
        store.save().unwrap();

        let report = workspace_findings(dir);
        assert!(
            report.errors.is_empty(),
            "the fixture itself must be clean: {:?}",
            report.errors
        );
        let warnings = report.warnings.join("\n");
        assert!(
            !warnings.contains("orphaned store key (card) `card-parent1`"),
            "a blank template's base id is a reserved live identity: {warnings}"
        );
    }

    #[test]
    fn doctor_flags_the_full_check_set() {
        let tmp = tempfile::tempdir().unwrap();
        let dir = tmp.path();
        let decks = dir.join("decks");
        std::fs::create_dir(&decks).unwrap();
        w(dir, "alix.toml", "title = \"Check Set\"\n");
        w(&decks, "bad-token.md", "## q\na\n<!-- id: BAD1 -->\n");
        w(
            &decks,
            "bad-value.md",
            "---\nreveal: bogus\n---\n## q\na\n<!-- id: card-bv1 -->\n",
        );
        w(
            &decks,
            "dup-deck.md",
            "---\nformat-version: 1\nid: deck-dupdeck\n---\n## q\na\n<!-- id: card-dd1 -->\n",
        );
        w(
            &decks,
            "dup-deck copy.md",
            "---\nformat-version: 1\nid: deck-dupdeck\n---\n## q\na\n<!-- id: card-dd1 -->\n",
        );
        w(
            &decks,
            "card-dup.md",
            "---\nformat-version: 1\nid: deck-cda\n---\n## q\na\n<!-- id: card-cshared -->\n",
        );
        w(
            &decks,
            "card-dup copy.md",
            "---\nformat-version: 1\nid: deck-cdb\n---\n## q\nb\n<!-- id: card-cshared -->\n",
        );
        w(
            &decks,
            "unspliceable.md",
            "---\n{source: [a]}\n---\n## q\nb\n<!-- id: card-uq1 -->\n",
        );
        w(
            &decks,
            "indented.md",
            "## real\n  ## not a front\nanswer\n<!-- id: card-ind1 -->\n",
        );
        w(
            &decks,
            "imgcard.md",
            "## pic\nphoto\n![](missing.png)\n<!-- id: card-img1 -->\n",
        );
        w(
            &decks,
            "fresh.md",
            "---\nformat-version: 1\nid: \"deck-fresh\"\n---\n## q\na\n",
        );
        w(
            &decks,
            "trace-bad.md",
            "---\ntrace: a walk\nsource: trace-src.txt\n---\n## hop\nstep\n<!-- id: card-thop1 -->\n<!-- at: 5-6 -->\n",
        );
        w(dir, "trace-src.txt", "one\ntwo\n");
        w(
            &decks,
            "at-dangling.md",
            "---\nsource: .\n---\n## cited\nb\n<!-- at: missing.rs:1-2 -->\n<!-- id: card-atd1 -->\n",
        );
        w(&decks, "sourceless.md", "## a\n1\n<!-- id: card-sla1 -->\n");
        w(
            &decks,
            "gated.md",
            "---\nsource: https://example.test\nrequires: sourceless\n---\n## b\n1\n<!-- id: card-gtd1 -->\n",
        );

        let mut store = alix::store::Store::open_deck(
            dir.join("progress/orphan-owner.json"),
            "orphan-owner",
            "orphan-owner.md",
        )
        .unwrap();
        store.get_or_insert("orphancard");
        store.set_last_depth("orphan-owner", alix::depth::Depth::Recall);
        store.save().unwrap();

        let report = workspace_findings(dir);
        let errors = report.errors.join("\n");
        let warnings = report.warnings.join("\n");

        assert!(
            errors.contains("must hold a base `card-<token>` id"),
            "invalid token: {errors}"
        );
        assert!(
            errors.contains("invalid value"),
            "bad directive value: {errors}"
        );
        assert!(warnings.contains("duplicate deck token"), "{warnings}");
        assert!(warnings.contains("duplicate card token"), "{warnings}");
        assert!(
            warnings.contains("not canonical"),
            "non-canonical token: {warnings}"
        );
        assert!(warnings.contains("orphaned store key (card)"), "{warnings}");
        assert!(warnings.contains("orphaned store key (deck)"), "{warnings}");
        assert!(
            warnings.contains("fresh tokens were minted"),
            "coarse fresh-mint: {warnings}"
        );
        assert!(
            warnings.contains("not a block mapping"),
            "unspliceable: {warnings}"
        );
        assert!(warnings.contains("indented `##`"), "{warnings}");
        assert!(warnings.contains("missing image"), "{warnings}");
        assert!(
            warnings.contains("checkpoint at line") && warnings.contains("has only 2 lines"),
            "trace-locator: {warnings}"
        );
        assert!(
            warnings.contains("cannot read the source"),
            "dangling `at:` citation: {warnings}"
        );
        assert!(
            warnings.contains("requires ungrounded") && warnings.contains("`sourceless`"),
            "dead `requires:`: {warnings}"
        );
        assert!(
            warnings.contains("card content without ids"),
            "unstamped warning: {warnings}"
        );
        assert!(
            warnings.contains("is not the last line of its card"),
            "misplaced id marker: {warnings}"
        );
    }

    #[test]
    fn doctor_accepts_a_canonically_closed_id_marker_without_warning() {
        let tmp = tempfile::tempdir().unwrap();
        let dir = tmp.path();
        w(
            dir,
            "facts.md",
            "---\nformat-version: 1\nid: deck-deck1\n---\n## q\na\n<!-- at: notes.md:1 -->\n<!-- id: card-card1 -->\n",
        );
        w(dir, "notes.md", "one\n");

        let report = workspace_findings(dir);

        assert!(
            !report
                .warnings
                .iter()
                .any(|warning| warning.contains("is not the last line of its card")),
            "canonical marker flagged: {:#?}",
            report.warnings
        );
    }

    #[test]
    fn doctor_reports_orphaned_state_documents_and_sync_conflicts() {
        let dir = tempfile::tempdir().unwrap();
        w(
            dir.path(),
            "deck.md",
            "---\nformat-version: 1\nid: deck-deck1\n---\n## q\na\n<!-- id: card-card1 -->\n",
        );
        let user_root = dir.path();
        alix::state::open_store(&dir.path().join("deck.md"), user_root)
            .unwrap()
            .save()
            .unwrap();
        let orphan_path = dir.path().join("augment/deck-orphan.json");
        alix::augment::AugmentCache::open_deck(&orphan_path, "deck-orphan")
            .unwrap()
            .save()
            .unwrap();
        let conflict = dir
            .path()
            .join("progress/deck-deck1.sync-conflict-20260725-phone.json");
        w(
            dir.path(),
            "progress/deck-deck1.sync-conflict-20260725-phone.json",
            "{}",
        );

        let report = workspace_findings(dir.path());
        let warnings = report.warnings.join("\n");

        assert!(warnings.contains("orphaned augmentation document"));
        assert!(warnings.contains(&conflict.display().to_string()));
    }

    #[test]
    fn a_bare_token_state_document_is_reported_generically() {
        let dir = tempfile::tempdir().unwrap();
        w(dir.path(), "alix.toml", "");
        std::fs::create_dir(dir.path().join("progress")).unwrap();
        w(
            &dir.path().join("progress"),
            "mathdeck.json",
            r#"{"version":1,"deck_id":"mathdeck","subject":"math.md","revision":1,"cards":{},"records":{},"writer":null}"#,
        );

        let report = workspace_findings(dir.path());

        assert!(
            report
                .warnings
                .iter()
                .chain(&report.errors)
                .any(|finding| finding.contains("mathdeck.json")),
            "the document must be reported: {:#?} {:#?}",
            report.warnings,
            report.errors
        );
        assert!(
            !report
                .warnings
                .iter()
                .chain(&report.errors)
                .chain(&report.notes)
                .any(|finding| finding.contains("un-converted")),
            "no retired-format vocabulary: {:#?} {:#?}",
            report.warnings,
            report.errors
        );
    }

    #[test]
    fn doctor_flags_a_source_pointing_into_assets() {
        let dir = tempfile::tempdir().unwrap();
        let decks = dir.path().join(alix::workspace::DECKS);
        std::fs::create_dir(&decks).unwrap();
        w(dir.path(), alix::workspace::MANIFEST, "");
        let assets = dir.path().join("assets/deck-deck1");
        std::fs::create_dir_all(&assets).unwrap();
        let name = alix::assets::object_name(b"excerpt\n", "md");
        std::fs::write(assets.join(&name), "excerpt\n").unwrap();
        w(
            &decks,
            "facts.md",
            &format!(
                "---\nformat-version: 1\nid: deck-deck1\nsource: assets/deck-deck1/{name}\n---\n\
                 ## q\na\n<!-- id: card-card1 -->\n"
            ),
        );

        let report = workspace_findings(dir.path());

        assert!(
            report
                .warnings
                .iter()
                .any(|warning| warning.contains("points into `assets/`")
                    && warning.contains("keeps its real source")),
            "source into assets: {:#?}",
            report.warnings
        );
    }

    #[test]
    fn doctor_clean_workspace_has_no_warnings() {
        let dir = tempfile::tempdir().unwrap();
        let decks = dir.path().join(alix::workspace::DECKS);
        std::fs::create_dir(&decks).unwrap();
        w(dir.path(), alix::workspace::MANIFEST, "");
        w(
            &decks,
            "facts.md",
            "---\nformat-version: 1\nid: deck-deck1\n---\n## q <!-- id: card-card1 -->\na\n",
        );
        std::fs::create_dir(dir.path().join("progress")).unwrap();
        w(
            &dir.path().join("progress"),
            "deck-deck1.json",
            r#"{"version":1,"deck_id":"deck-deck1","subject":"facts.md","revision":1,"cards":{},"records":{},"writer":null}"#,
        );

        let report = workspace_findings(dir.path());

        assert!(
            !report
                .warnings
                .iter()
                .any(|warning| warning.contains("un-converted")),
            "clean workspace flagged un-converted: {:#?}",
            report.warnings
        );
        assert!(
            !report
                .warnings
                .iter()
                .any(|warning| warning.contains("points into `assets/`")),
            "clean workspace flagged a source into assets: {:#?}",
            report.warnings
        );
    }

    #[test]
    fn source_locator_repair_is_explicit_and_preserves_card_identity() {
        let dir = tempfile::tempdir().unwrap();
        w(
            dir.path(),
            "code.rs",
            "inserted\nalpha\nfn answer() {\n    42\n}\nomega\n",
        );
        let expected = alix::source::Excerpt {
            path: PathBuf::from("code.rs"),
            lines: vec![(2, "fn answer() {".into()), (3, "    42".into())],
            truncated: false,
        };
        let fingerprint =
            alix::source::format_locator_fingerprint(alix::source::excerpt_fingerprint(&expected));
        let deck_path = dir.path().join("deck.md");
        w(
            dir.path(),
            "deck.md",
            &format!(
                "---\nformat-version: 1\nid: \"deck-deck1\"\nsource: .\n---\n\
                 ## q\nanswer\n<!-- at: code.rs:2-3 fingerprint: {fingerprint} -->\n\
                 <!-- id: card-card1 -->\n"
            ),
        );

        let before = std::fs::read_to_string(&deck_path).unwrap();
        let mut report = Report::default();
        deck_findings(&deck_path, &mut report);
        assert!(
            report
                .warnings
                .join("\n")
                .contains("moved to `code.rs:3-4`"),
            "{:?}",
            report.warnings
        );
        assert_eq!(before, std::fs::read_to_string(&deck_path).unwrap());

        repair_source_locators(std::slice::from_ref(&deck_path)).unwrap();
        let after = std::fs::read_to_string(&deck_path).unwrap();
        assert!(after.contains(&format!(
            "<!-- at: code.rs:3-4 fingerprint: {fingerprint} -->"
        )));
        assert!(after.contains("<!-- id: card-card1 -->"));
        assert_eq!(
            Some("card-card1".to_string()),
            Deck::load(&deck_path).unwrap().cards[0].id()
        );
    }

    #[test]
    fn a_sampling_key_that_can_affect_nothing_is_reported() {
        let dir = tempfile::tempdir().unwrap();
        let head = "---\nformat-version: 1\nid: \"deck-9w2c7x4k1m8q3z5t0v6b2n4d8f\"\n";
        let table = "| w | m |\n|---|---|\n| a | alpha | <!-- r:aaaaaa -->\n<!-- id: card-4jkya9q3m8z0tw5v9y2b4n6d8f -->\n";
        // (deck body, warning expected)
        let cases = [
            (
                format!("{head}sampling: off\n---\n## q\na\n<!-- id: card-q1x -->\n"),
                true,
            ),
            (format!("{head}sampling: off\n---\n{table}"), false),
            (
                format!("{head}---\n## q\na\n<!-- id: card-q1x -->\n"),
                false,
            ),
            (
                format!("{head}---\n## q\na\n<!-- sampling: off -->\n<!-- id: card-q1x -->\n"),
                true,
            ),
        ];
        for (index, (text, expected)) in cases.iter().enumerate() {
            let path = dir.path().join(format!("d{index}.md"));
            std::fs::write(&path, text).unwrap();
            let mut report = Report::default();
            deck_findings(&path, &mut report);
            let warned = report
                .warnings
                .iter()
                .any(|w| w.contains("`sampling:` has no effect"));
            assert_eq!(*expected, warned, "case {index}: {:?}", report.warnings);
        }
    }

    #[test]
    fn unstamped_table_rows_are_reported_as_content_without_ids() {
        let dir = tempfile::tempdir().unwrap();
        let head = "---\nformat-version: 1\nid: deck-tbl\n---\n";
        let rows = "| a | alpha | <!-- r:aaaaaa -->\n| b | beta |\n| c | gamma |\n";
        let container = "<!-- id: card-4jkya9q3m8z0tw5v9y2b4n6d8f -->\n";

        // With a container id, only the stamp-less rows lack identity.
        let path = dir.path().join("partly.md");
        std::fs::write(
            &path,
            format!("{head}| w | m |\n|---|---|\n{rows}<!-- cards -->\n{container}"),
        )
        .unwrap();
        let mut report = Report::default();
        deck_findings(&path, &mut report);
        assert!(
            report
                .warnings
                .iter()
                .any(|w| w.contains("2 entries are card content without ids")),
            "{:?}",
            report.warnings
        );

        // Without one, no row can compose an id, so every row is reported.
        let path = dir.path().join("none.md");
        std::fs::write(
            &path,
            format!("{head}| w | m |\n|---|---|\n{rows}<!-- cards -->\n"),
        )
        .unwrap();
        let mut report = Report::default();
        deck_findings(&path, &mut report);
        assert!(
            report
                .warnings
                .iter()
                .any(|w| w.contains("3 entries are card content without ids")),
            "{:?}",
            report.warnings
        );

        // Stamping resolves both: no unstamped warning survives an init.
        for name in ["partly.md", "none.md"] {
            let path = dir.path().join(name);
            alix::stamp::stamp_deck(&path).unwrap();
            let mut after = Report::default();
            deck_findings(&path, &mut after);
            assert!(
                !after
                    .warnings
                    .iter()
                    .any(|w| w.contains("content without ids")),
                "{name}: {:?}",
                after.warnings
            );
        }
    }
}