matra 0.3.0

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

use std::path::{Path, PathBuf};

use serde::{Deserialize, Serialize};

// ---------------------------------------------------------------------------
// Resource bounds
// ---------------------------------------------------------------------------

/// Every bound matra holds its work to, in one place: the caps on what it
/// reads, the caps each outcome method checks before it runs, and the
/// minimum a paragraph needs before a metric is measured on it.
///
/// Published so a caller can size its work before the call rather than
/// learn the bound from a refusal: split a long input under
/// [`limits::MAX_INPUT_BYTES`], or chunk a document's sentences under the
/// cap of the method it selects ([`limits::MAX_TFIDF_SENTENCES`] for
/// `SummaryMethod::TfIdf`, [`limits::MAX_RAKE_TOKENS`] for
/// `KeyphraseMethod::Rake`, and so on). Each method cap is named for the
/// method a caller names, as the selectors are.
///
/// Constants, not settings: no configuration, argument or environment
/// variable changes any of them. They are this release's values and may
/// change in any 0.x release, so read them from here rather than copying
/// the number. A cap that is exceeded returns [`Error::InputTooLarge`],
/// whose `limit` detail is the constant here and whose `what` detail
/// names the gate. A paragraph under a minimum, or a paragraph over
/// [`limits::MAX_COMPRESSION_PARAGRAPH_BYTES`], is not an error: its metric slot
/// stays `None`.
///
/// Each cap is defined once and read where it applies. Two caps with the
/// same value stay two constants where their costs differ, so tuning one
/// does not move the other.
pub mod limits {
    /// The most text one input may hold, in bytes: 8 MiB.
    ///
    /// The gate whose `what` is `"input"`. `Engine::analyze` and
    /// `Engine::analyze_all` check it on every `Input` (text, bytes, a
    /// file, blocks), on the bytes handed in and again on the analysed
    /// text a reader produces from them; reading a file checks it on the
    /// file's size before reading (`"file_read"`), and the command line
    /// on stdin as the bytes arrive.
    ///
    /// 8 MiB holds book-length English (a typical novel is ~1.5 MiB,
    /// 200k words) with headroom. Beyond it the parser's intermediate
    /// memory grows past safe limits on a typical workstation (UDPipe's
    /// per-token allocations cross ~1 GiB resident at this size).
    pub const MAX_INPUT_BYTES: usize = 8 * 1024 * 1024;

    /// The largest configuration file read, in bytes: 64 KiB.
    ///
    /// The gate whose `what` is `"config_file_read"`, checked on the file's
    /// size before any read. A config file is small by construction;
    /// anything larger is a mistake or an attack, and either way it is
    /// not read into memory first and rejected second.
    pub const MAX_CONFIG_FILE_BYTES: usize = 64 * 1024;

    /// The most bytes read from any one model download: 64 MiB.
    ///
    /// The gates whose `what` is `"parser_model_download"` (the parsing model)
    /// and `"embedding_model_download"` (each embedding artifact). One number,
    /// because it answers one question: how much a redirected or
    /// misbehaving server can make the process hold. The pinned artifacts
    /// are 16.3 MB and at most 30.2 MB, so this is headroom for a later
    /// version that still costs a bounded read. The read stops one byte
    /// past it.
    pub const MAX_DOWNLOAD_BYTES: usize = 64 * 1024 * 1024;

    /// The most sentences a TF-IDF summary ranks: 2,000.
    ///
    /// The gate whose `what` is `"tfidf"`. TF-IDF scores each sentence in
    /// linear time; the working set is the per-sentence term counts, about
    /// 60k entries (~2 MiB) at this size. It shares the value with
    /// [`MAX_TEXTRANK_SENTENCES`] but not the cost model, so it is a
    /// separate constant: tuning TextRank does not move it.
    pub const MAX_TFIDF_SENTENCES: usize = 2000;

    /// The most sentences a TextRank summary ranks: 2,000.
    ///
    /// The gate whose `what` is `"textrank"`. TextRank builds a dense
    /// sentence similarity matrix, quadratic in sentences: about 32 MB of
    /// `f64` at this size, the ceiling for unattended use.
    pub const MAX_TEXTRANK_SENTENCES: usize = 2000;

    /// The most sentences, or vectors, clustered: TextRank's bound.
    ///
    /// The gate whose `what` is `"clusters"`, checked on a document's
    /// sentences before anything is embedded and on the vectors
    /// themselves. Clustering builds the same quadratic graph TextRank
    /// does, so it is defined as [`MAX_TEXTRANK_SENTENCES`] rather than as
    /// a second copy of its value, and the two cannot drift apart.
    pub const MAX_CLUSTER_SENTENCES: usize = MAX_TEXTRANK_SENTENCES;

    /// The most tokens RAKE extracts keyphrases from: 200,000, counted
    /// across every sentence, punctuation included.
    ///
    /// The gate whose `what` is `"rake"`. RAKE's co-occurrence map grows
    /// with tokens, not sentences (a chat log can hold 50k one-token
    /// sentences), so the cap is on tokens: at this size the candidate map
    /// holds at most ~50k entries, about 3 MiB.
    pub const MAX_RAKE_TOKENS: usize = 200_000;

    /// The most tokens YAKE extracts keyphrases from: 200,000, counted as
    /// RAKE's are.
    ///
    /// The gate whose `what` is `"yake"`. YAKE's one- to three-word
    /// candidate map is bounded by token count: at this size at most ~600k
    /// entries, about 40 MiB. Its own constant, because its cost model is
    /// not RAKE's.
    pub const MAX_YAKE_TOKENS: usize = 200_000;

    /// The largest paragraph whose compression ratio is measured, in
    /// bytes: 256 KiB.
    ///
    /// Above it `compression_ratio` stays `None` rather than failing, to
    /// bound worst-case CPU on adversarial input. It matches the
    /// compressor's 256 KiB window, so one paragraph never costs more
    /// than one window's work.
    pub const MAX_COMPRESSION_PARAGRAPH_BYTES: usize = 256 * 1024;

    /// The fewest words a paragraph needs for a readability grade: 11
    /// (more than 10), counted by whitespace.
    ///
    /// Below it `readability_grade` stays `None`.
    pub const MIN_READABILITY_WORDS: usize = 11;

    /// The fewest words a paragraph needs for a compression ratio: 51
    /// (more than 50), counted by whitespace.
    ///
    /// Below it `compression_ratio` stays `None`.
    pub const MIN_COMPRESSION_WORDS: usize = 51;
}

// ---------------------------------------------------------------------------
// Provisioning
// ---------------------------------------------------------------------------

/// What a constructor is about to download, reported before the transfer
/// begins.
///
/// A first run fetches a model over a link whose speed matra cannot know.
/// Measured cold starts ran from 3 to 35 seconds with nothing on screen,
/// which is indistinguishable from a hung process, and the natural
/// response to a hung process makes it worse. The library writes to no
/// terminal, so it hands the facts to whoever does: the constructors that
/// provision carry a `_with_notice` form that calls a closure once per
/// fetch, and only when the artifact is not already on disk.
///
/// That is `Engine::from_config_with_notice` for the parsing model, and
/// `Model2Vec::from_config_with_notice` for the embedding model, whose
/// three files are fetched and verified as one set and so are one notice.
///
/// Facts rather than a sentence. The caller decides the wording, the
/// stream, and whether to say anything at all.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct ProvisionNotice {
    /// What is being fetched: the artifact's filename as it will appear
    /// in `destination`, or, for a model fetched as a set of files, the
    /// model's name (`potion-base-8M`).
    pub artifact: String,
    /// The artifact's pinned size in bytes, the set's total for a model
    /// fetched as a set. Known before the fetch because it is a constant
    /// in the source; the pinned digest decides whether what arrives is
    /// trusted.
    pub bytes: u64,
    /// The directory the artifact is being fetched into.
    pub destination: PathBuf,
    /// The artifact's license, named for a reader, for example
    /// `CC BY-NC-SA 4.0 (non-commercial)`. A model is a separate work
    /// from matra and carries its own terms, which need not be matra's
    /// MIT. Pinned in the source beside the URL, so a change of model
    /// changes both.
    pub license: String,
    /// Where the full text of `license` is published.
    pub license_url: String,
}

// ---------------------------------------------------------------------------
// Embedding carrier (Tier 2)
// ---------------------------------------------------------------------------

/// A dense vector representation of a text, produced by an implementation
/// of the embed port's `Embedder` trait.
///
/// Model opinion, not verifiable structure: an embedding cannot be checked
/// against the source bytes, so it never appears as a field on [`Document`]
/// or any other type the deterministic pipeline returns (RFC-0010). Values
/// derived from embeddings carry their provenance (model identity, parameters)
/// in their own standalone types.
///
/// Serde treats the newtype transparently, so the wire form is the bare
/// array. Deliberately not `#[non_exhaustive]`: on a tuple struct that
/// attribute makes the constructor crate-private, and external `Embedder`
/// implementors must construct these values; RFC-0010 records the
/// departure from the convention.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Embedding(pub Vec<f32>);

// ---------------------------------------------------------------------------
// Errors
// ---------------------------------------------------------------------------

/// What an [`Error::InputTooLarge`] message says was too large: the bare
/// word `input` for the main gate, whose label is `"input"`, and the
/// label as a qualifier (`tfidf input`) for every other gate.
struct GateSubject<'a>(&'a str);

impl std::fmt::Display for GateSubject<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self.0 {
            "input" => f.write_str("input"),
            label => write!(f, "{label} input"),
        }
    }
}

/// What an [`Error::UnsupportedFormat`] message says could not be read,
/// and what would read it.
struct FormatSubject<'a>(Option<&'a Format>, Option<&'static str>);

impl std::fmt::Display for FormatSubject<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match (self.0, self.1) {
            (Some(format), Some(feature)) => write!(
                f,
                "unsupported format: {}; enable the `{feature}` feature to read it",
                format.wire_name()
            ),
            (Some(Format::Other(name)), None) => write!(
                f,
                "unsupported format: {name}; no reader is registered for it"
            ),
            (Some(format), None) => write!(
                f,
                "unsupported format: {}; no feature of this matra version reads it",
                format.wire_name()
            ),
            (None, _) => f.write_str(
                "unsupported format: the input is not UTF-8 text and carries no signature matra recognises",
            ),
        }
    }
}

/// All errors matra can produce. Matchable, not opaque.
///
/// Every channel reads a failure the same way: a stable [`kind`](Error::kind)
/// to branch on, a message for a person, and [`details`](Error::details),
/// the values a caller acts on as named data. The serde form is
/// those three, `{kind, message, details}`, and is what the Python
/// binding and the JSON carry, so no reader has to parse a message.
#[derive(thiserror::Error, Debug)]
#[non_exhaustive]
pub enum Error {
    /// Model file does not exist at the given path.
    #[error("model not found: {}", .0.display())]
    ModelNotFound(PathBuf),
    /// Model file exists but could not be loaded (corrupt, wrong format).
    #[error("invalid model: {0}")]
    ModelInvalid(String),
    /// The parser or a reader failed on the input, or returned what its
    /// contract forbids.
    ///
    /// A failure the component reports itself carries only its `cause`.
    /// When the engine's checks on what a parser or a reader returned
    /// fail (a head out of range, a span outside its paragraph), `reason`
    /// names the check, a stable snake_case name, and `cause` names the
    /// token or block and what was wrong with it. The remedy is the
    /// component, not the input: the same input through the same parser
    /// fails the same way.
    #[error("parse failed: {cause}")]
    ParseFailed {
        /// Which of the engine's checks failed, a stable snake_case name:
        /// the `reason` detail. `None` when the parser or the reader
        /// reported the failure itself; a provider leaves it `None`. The
        /// names in use are listed in the errors reference
        /// (`site/content/reference/errors.md`); a reader treats the set
        /// as open.
        reason: Option<&'static str>,
        /// What went wrong, for a person: the `cause` detail.
        cause: String,
    },
    /// Input exceeded a bounded limit (e.g. too many sentences for
    /// an O(n^2) algorithm like TextRank).
    ///
    /// The message reads `{what} input too large: {actual} > limit
    /// {limit}`, except for the main gate: its label is `"input"`
    /// already, so it reads `input too large: {actual} > limit {limit}`.
    #[error("{} too large: {actual} > limit {limit}", GateSubject(.what))]
    InputTooLarge {
        /// The size cap that was exceeded.
        limit: usize,
        /// The actual size that exceeded the cap.
        actual: usize,
        /// Discriminator naming which gate fired, so consumers can route
        /// differently per gate. The labels in use: `"input"`,
        /// `"file_read"`, `"tfidf"`, `"textrank"`, `"rake"`, `"yake"`,
        /// `"clusters"`, `"parser_model_download"`,
        /// `"embedding_model_download"` and `"config_file_read"`.
        what: &'static str,
    },
    /// The input is in a format this build cannot read: a format matra
    /// recognises but has no reader for here (`pdf`, `docx`), or bytes
    /// that are not UTF-8 text and carry no signature matra recognises.
    /// A text file with an extension matra does not know is not this
    /// error: it reads as plain text.
    #[error("{}", FormatSubject(.format.as_ref(), *.feature))]
    UnsupportedFormat {
        /// The format recognised, or `None` when the bytes are neither
        /// UTF-8 text nor a signature matra recognises.
        format: Option<Format>,
        /// The Cargo feature that would read `format`, when one exists.
        /// No feature of this version reads `pdf` or `docx`, so it is
        /// `None` for both today.
        feature: Option<&'static str>,
    },
    /// The call cannot be honored as made: a documented contract broken
    /// (disagreeing dimensions, a non-finite parameter), a configuration
    /// that does not read, input data that is not the shape its format
    /// names. The remedy is to change the call, its configuration or the
    /// data it was handed, never to retry it as it is.
    ///
    /// One kind, many checks: `reason` names which one failed, so a caller
    /// routes on it without reading `message`.
    #[error("invalid input: {message}")]
    InvalidInput {
        /// Which check failed, a stable snake_case name: the `reason`
        /// detail. The names in use are listed in the errors reference
        /// (`site/content/reference/errors.md`), each with the check it
        /// names; a reader treats the set as open.
        reason: &'static str,
        /// What was wrong, for a person, with the values that made it so.
        message: String,
    },
    /// File I/O error.
    #[error("io error: {0}")]
    Io(#[from] std::io::Error),
}

impl Error {
    /// The stable kind string for this variant: the key a consumer
    /// branches on instead of matching against a message.
    ///
    /// The vocabulary is part of the public contract. A binding that
    /// cannot carry a Rust enum across carries this string instead, in
    /// the serde form and on the Python exception, so every language
    /// names a failure the same way. `spec/tests/corpus/items.json` pins
    /// the list.
    ///
    /// Exhaustive with no wildcard: a new variant fails to compile until
    /// someone names it here, which is what stops a new failure mode
    /// from quietly inheriting another one's key.
    pub fn kind(&self) -> &'static str {
        match self {
            Error::ModelNotFound(_) => "model_not_found",
            Error::ModelInvalid(_) => "model_invalid",
            Error::ParseFailed { .. } => "parse_failed",
            Error::InputTooLarge { .. } => "input_too_large",
            Error::UnsupportedFormat { .. } => "unsupported_format",
            Error::InvalidInput { .. } => "invalid_input",
            Error::Io(_) => "io",
        }
    }

    /// The values a caller acts on, as named data, so a caller acts on a
    /// failure without parsing its message: every dynamic value in the
    /// message, except on `invalid_input`, whose `reason` names the check
    /// that failed and whose message adds, for a person, the values that
    /// failed it.
    ///
    /// The keys per kind:
    ///
    /// | Kind | Keys |
    /// |---|---|
    /// | `model_not_found` | `path` |
    /// | `model_invalid` | `cause` |
    /// | `parse_failed` | `cause`; `reason` when one of the engine's checks failed |
    /// | `invalid_input` | `reason` |
    /// | `input_too_large` | `what`, `limit`, `actual` |
    /// | `unsupported_format` | `format` when one was recognised, `feature` when one would read it |
    /// | `io` | `io_kind` and `cause`; `path` when the failure concerns a path matra named; `url` and `retryable` when a download did not arrive |
    ///
    /// A key that does not apply is absent, never empty. A key, once a
    /// kind carries it, keeps appearing in later versions. One word, one
    /// meaning: `reason` is always a stable snake_case name of a check to
    /// branch on (`threshold_not_finite`, `config_malformed`,
    /// `head_out_of_range`, ...), and `cause` is always free text for a
    /// person: the message a payload holds, or the operating system's
    /// account of an I/O failure. `io_kind` names the
    /// I/O failure's class (`not_found`, `permission_denied`,
    /// `timed_out`, ...; [`std::io::ErrorKind`] in Rust), a set a reader
    /// treats as open. `retryable` is `true` when the same call can be made
    /// again as it is and may succeed: a download that did not arrive
    /// left nothing behind. Its absence makes no claim either way.
    ///
    /// ```
    /// # use matra::domain::{DetailValue, Error};
    /// let err = Error::InputTooLarge { limit: 8, actual: 9, what: "input" };
    /// let details = err.details();
    /// assert_eq!(details.get("limit"), Some(&DetailValue::Integer(8)));
    /// assert_eq!(details.get("what"), Some(&DetailValue::Text("input".into())));
    /// ```
    pub fn details(&self) -> ErrorDetails {
        let details = ErrorDetails::default();
        match self {
            Error::ModelNotFound(path) => details.text("path", path.to_string_lossy()),
            Error::ModelInvalid(cause) => details.text("cause", cause.as_str()),
            Error::ParseFailed { reason, cause } => {
                let details = match reason {
                    Some(reason) => details.text("reason", *reason),
                    None => details,
                };
                details.text("cause", cause.as_str())
            }
            Error::InvalidInput { reason, .. } => details.text("reason", *reason),
            Error::InputTooLarge {
                limit,
                actual,
                what,
            } => details
                .text("what", *what)
                .integer("limit", *limit)
                .integer("actual", *actual),
            Error::UnsupportedFormat { format, feature } => {
                let details = match format {
                    Some(format) => details.text("format", format.wire_name()),
                    None => details,
                };
                match feature {
                    Some(feature) => details.text("feature", *feature),
                    None => details,
                }
            }
            Error::Io(error) => {
                let details = details.text("io_kind", io_kind_name(error.kind()));
                let context = error
                    .get_ref()
                    .and_then(|inner| inner.downcast_ref::<IoContext>());
                let details = match context {
                    Some(context) => context.add_to(details),
                    None => details,
                };
                details.text("cause", error.to_string())
            }
        }
    }
}

/// `{kind, message, details}`: the [`kind`](Error::kind), the `Display`
/// text, and the [`details`](Error::details). There is no `Deserialize`:
/// an error is reported, never read back into a value.
impl Serialize for Error {
    fn serialize<S: serde::Serializer>(
        &self,
        serializer: S,
    ) -> std::result::Result<S::Ok, S::Error> {
        use serde::ser::SerializeStruct;
        let mut state = serializer.serialize_struct("Error", 3)?;
        state.serialize_field("kind", self.kind())?;
        state.serialize_field("message", &self.to_string())?;
        state.serialize_field("details", &self.details())?;
        state.end()
    }
}

/// Result type for matra operations.
pub type Result<T> = std::result::Result<T, Error>;

/// The values a caller acts on in an [`Error`], by name: what
/// [`Error::details`] returns.
///
/// Serializes as an object, its keys in the order the error names them.
/// The keys each kind carries are listed on [`Error::details`].
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct ErrorDetails {
    entries: Vec<(&'static str, DetailValue)>,
}

impl ErrorDetails {
    /// The value named `key`, or `None` when this error carries no such
    /// value.
    pub fn get(&self, key: &str) -> Option<&DetailValue> {
        self.entries
            .iter()
            .find(|(name, _)| *name == key)
            .map(|(_, value)| value)
    }

    /// Every value with its name, in the order the error names them.
    pub fn iter(&self) -> impl Iterator<Item = (&'static str, &DetailValue)> + '_ {
        self.entries.iter().map(|(name, value)| (*name, value))
    }

    /// How many values there are.
    pub fn len(&self) -> usize {
        self.entries.len()
    }

    /// Whether there are none.
    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }

    fn text(mut self, key: &'static str, value: impl Into<String>) -> Self {
        self.entries.push((key, DetailValue::Text(value.into())));
        self
    }

    fn integer(mut self, key: &'static str, value: usize) -> Self {
        // Lossless wherever Rust runs: no supported target has a `usize`
        // wider than 64 bits.
        let value = u64::try_from(value).unwrap_or(u64::MAX);
        self.entries.push((key, DetailValue::Integer(value)));
        self
    }

    fn flag(mut self, key: &'static str, value: bool) -> Self {
        self.entries.push((key, DetailValue::Bool(value)));
        self
    }
}

impl Serialize for ErrorDetails {
    fn serialize<S: serde::Serializer>(
        &self,
        serializer: S,
    ) -> std::result::Result<S::Ok, S::Error> {
        use serde::ser::SerializeMap;
        let mut map = serializer.serialize_map(Some(self.entries.len()))?;
        for (key, value) in &self.entries {
            map.serialize_entry(key, value)?;
        }
        map.end()
    }
}

/// One value in an [`ErrorDetails`].
///
/// Serializes as the bare value: a string, a number or a boolean.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(untagged)]
#[non_exhaustive]
pub enum DetailValue {
    /// Text: a path, a URL, a gate's label, a format's name, a reason.
    Text(String),
    /// A whole number, never negative: a size or a limit.
    Integer(u64),
    /// Yes or no: whether a retry may succeed.
    Bool(bool),
}

/// What matra knows about an I/O failure it raised itself, beyond the
/// operating system's kind: the path or the URL it concerns, and whether
/// the same call may succeed when made again.
///
/// It travels inside the [`std::io::Error`] that [`Error::Io`] wraps,
/// which keeps the variant's payload a plain `std::io::Error`, and
/// [`Error::details`] finds it there. Its `Display` is the whole message.
#[derive(Debug)]
pub(crate) struct IoContext {
    message: String,
    path: Option<PathBuf>,
    url: Option<String>,
    retryable: bool,
}

impl IoContext {
    /// A failure concerning `path`, whose message is `message`.
    pub(crate) fn at_path(
        kind: std::io::ErrorKind,
        path: &Path,
        message: impl Into<String>,
    ) -> std::io::Error {
        std::io::Error::new(
            kind,
            IoContext {
                message: message.into(),
                path: Some(path.to_path_buf()),
                url: None,
                retryable: false,
            },
        )
    }

    /// A filesystem `operation` on `path` that failed with `error`,
    /// keeping its kind: `cannot {operation} {path}: {error}`.
    ///
    /// `io error: No such file or directory (os error 2)` was the whole
    /// message for a missing input, with the path one line away in a
    /// variable; this is the one place the sentence that names both is
    /// written.
    pub(crate) fn cannot(operation: &str, path: &Path, error: &std::io::Error) -> std::io::Error {
        IoContext::at_path(
            error.kind(),
            path,
            format!("cannot {operation} {}: {error}", path.display()),
        )
    }

    /// A download from `url` that did not arrive, whose message is
    /// `message`. `retryable` says whether the same call may succeed when
    /// made again: true for a failure in the transfer, false where the
    /// message names a remedy that a retry cannot supply.
    ///
    /// Only the two provisioners download, and the embedder downloads
    /// nothing on `wasm32`, so a build with neither has no caller.
    #[cfg_attr(
        not(any(
            feature = "udpipe",
            all(feature = "model2vec", not(target_arch = "wasm32"))
        )),
        allow(dead_code)
    )]
    pub(crate) fn download(
        kind: std::io::ErrorKind,
        url: &str,
        message: impl Into<String>,
        retryable: bool,
    ) -> std::io::Error {
        std::io::Error::new(
            kind,
            IoContext {
                message: message.into(),
                path: None,
                url: Some(url.to_string()),
                retryable,
            },
        )
    }

    fn add_to(&self, details: ErrorDetails) -> ErrorDetails {
        let details = match &self.path {
            Some(path) => details.text("path", path.to_string_lossy()),
            None => details,
        };
        match &self.url {
            Some(url) => details
                .text("url", url.as_str())
                .flag("retryable", self.retryable),
            None => details,
        }
    }
}

impl std::fmt::Display for IoContext {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&self.message)
    }
}

impl std::error::Error for IoContext {}

/// The `io_kind` detail: an I/O failure's class in snake case.
///
/// Written out rather than derived from `Debug`, which promises no
/// stable text. A kind not named here is `other`; the operating system's
/// account is in `cause` either way.
fn io_kind_name(kind: std::io::ErrorKind) -> &'static str {
    use std::io::ErrorKind;
    match kind {
        ErrorKind::NotFound => "not_found",
        ErrorKind::PermissionDenied => "permission_denied",
        ErrorKind::AlreadyExists => "already_exists",
        ErrorKind::Unsupported => "unsupported",
        ErrorKind::InvalidInput => "invalid_input",
        ErrorKind::InvalidData => "invalid_data",
        ErrorKind::TimedOut => "timed_out",
        ErrorKind::NotConnected => "not_connected",
        ErrorKind::ConnectionRefused => "connection_refused",
        ErrorKind::ConnectionReset => "connection_reset",
        ErrorKind::UnexpectedEof => "unexpected_eof",
        ErrorKind::Interrupted => "interrupted",
        ErrorKind::NotADirectory => "not_a_directory",
        ErrorKind::IsADirectory => "is_a_directory",
        ErrorKind::StorageFull => "storage_full",
        _ => "other",
    }
}

// ---------------------------------------------------------------------------
// Core linguistic types
// ---------------------------------------------------------------------------

/// A parsed token carrying the full CoNLL-U annotation set.
///
/// One field per CoNLL-U column plus one derived convenience field.
/// Each column holds what the NLP provider emitted for it, converted
/// where the column has structure: column 6 (`features`) arrives as a
/// map rather than the pipe-separated string, so no consumer in any
/// language re-parses it. The UDPipe adapter cannot fill column 9
/// (`deps`), which its binding does not surface, and sets it to `_`.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Token {
    /// 1-based position within the sentence (CoNLL-U column 1).
    pub id: usize,
    /// Where the token's surface form lies in the analysed text, in
    /// UTF-8 bytes. The parser reports it against the paragraph text it
    /// was given, and `Engine::annotate` maps it into the analysed text.
    /// In a markdown paragraph whose text dropped inline markup, a span
    /// can cover markup inside the token (`foo**bar**` is one token whose
    /// span runs from the `f` to the last `r`), and a token read from an
    /// escape or an entity (`&amp;`) covers all of it.
    pub span: Span,
    /// Surface form (CoNLL-U column 2).
    pub text: String,
    /// Dictionary form (CoNLL-U column 3).
    pub lemma: String,
    /// Universal POS tag (CoNLL-U column 4).
    pub pos: String,
    /// Language-specific POS tag (CoNLL-U column 5).
    pub xpos: String,
    /// Morphological features (CoNLL-U column 6) as a map from feature
    /// name to value: `Mood=Ind|Tense=Pres` arrives as
    /// `{"Mood": "Ind", "Tense": "Pres"}`. A multi-valued feature keeps
    /// its value unsplit (`Case=Nom,Acc` is `{"Case": "Nom,Acc"}`), so
    /// matra exposes what the provider emitted without normalising it.
    /// Empty when the provider emitted none (the CoNLL-U placeholder
    /// `_`). Keys are an open set: a reader ignores a feature it does not
    /// know.
    pub features: std::collections::BTreeMap<String, String>,
    /// Head token id, 0 = root (CoNLL-U column 7).
    pub head: usize,
    /// Dependency relation to head (CoNLL-U column 8).
    pub dep: String,
    /// Enhanced dependency graph (CoNLL-U column 9). Always `_` from the
    /// UDPipe adapter, whose binding does not surface this column.
    pub deps: String,
    /// Miscellaneous annotations (CoNLL-U column 10).
    pub misc: String,
    /// Derived: true if this token is punctuation.
    pub is_punct: bool,
}

impl Token {
    /// Preferred construction path for external crates (fields are pub but
    /// `#[non_exhaustive]` prevents struct literal syntax outside the crate).
    pub fn builder(
        id: usize,
        text: String,
        lemma: String,
        pos: String,
        head: usize,
        dep: String,
    ) -> TokenBuilder {
        TokenBuilder {
            id,
            span: Span::default(),
            text,
            lemma,
            pos,
            head,
            dep,
            xpos: String::new(),
            features: std::collections::BTreeMap::new(),
            deps: String::new(),
            misc: String::new(),
            is_punct: false,
        }
    }

    /// Look up one morphological feature by exact key:
    /// `feat("Mood")` is `Some("Ind")` when `features` holds
    /// `Mood=Ind`. A view over [`Token::features`], which crosses FFI as
    /// a map, so it adds nothing to the wire (RFC-0009).
    pub fn feat(&self, key: &str) -> Option<&str> {
        self.features.get(key).map(String::as_str)
    }
}

/// Convert a CoNLL-U column 6 string into the [`Token::features`] map.
///
/// Splits on `|`, then each pair at its first `=`. A pair with no `=`
/// or an empty name is not a feature and is dropped; when a name repeats,
/// the first value wins. The empty string and the placeholder `_` carry
/// no pair, so both give an empty map. Values are kept as written, a
/// multi-valued `Nom,Acc` unsplit.
pub(crate) fn features_from_conllu(column: &str) -> std::collections::BTreeMap<String, String> {
    let mut features = std::collections::BTreeMap::new();
    for pair in column.split('|') {
        if let Some((name, value)) = pair.split_once('=')
            && !name.is_empty()
        {
            features
                .entry(name.to_string())
                .or_insert_with(|| value.to_string());
        }
    }
    features
}

/// Where `form` ends in `text` when it starts at byte `at`, or `None`
/// when it does not lie there: the one rule that says whether a token's
/// span lines up with its form.
///
/// The form must match the text exactly, except that a run of
/// whitespace inside the form matches any non-empty run of whitespace in
/// the text (a token a tokenizer read across a line break). The UDPipe
/// adapter finds each span with it and the engine checks every parser's
/// spans with it, so a span the adapter can produce is a span the engine
/// accepts.
pub(crate) fn form_end(text: &str, at: usize, form: &str) -> Option<usize> {
    let mut at = at;
    let mut wanted = form.chars().peekable();
    while let Some(c) = wanted.next() {
        if c.is_whitespace() {
            while wanted.peek().is_some_and(|c| c.is_whitespace()) {
                wanted.next();
            }
            let tail = text.get(at..)?;
            let skipped = tail.len() - tail.trim_start().len();
            if skipped == 0 {
                return None;
            }
            at += skipped;
        } else {
            let found = text.get(at..)?.chars().next()?;
            if found != c {
                return None;
            }
            at += found.len_utf8();
        }
    }
    Some(at)
}

/// Builder for [`Token`]. Created via [`Token::builder`].
///
/// The six CoNLL-U essentials (id, text, lemma, pos, head, dep) are required.
/// Optional fields default to empty strings; `is_punct` defaults to false.
pub struct TokenBuilder {
    id: usize,
    span: Span,
    text: String,
    lemma: String,
    pos: String,
    head: usize,
    dep: String,
    xpos: String,
    features: std::collections::BTreeMap<String, String>,
    deps: String,
    misc: String,
    is_punct: bool,
}

impl TokenBuilder {
    /// Set where the token lies in the text it was parsed from, in UTF-8
    /// bytes. Defaults to the empty span at `0`.
    pub fn span(mut self, span: Span) -> Self {
        self.span = span;
        self
    }
    /// Set the language-specific POS tag (CoNLL-U column 5).
    pub fn xpos(mut self, xpos: String) -> Self {
        self.xpos = xpos;
        self
    }
    /// Set the morphological features from a CoNLL-U column 6 string
    /// (`Mood=Ind|Tense=Pres`), converted into the [`Token::features`]
    /// map.
    pub fn feats(mut self, feats: String) -> Self {
        self.features = features_from_conllu(&feats);
        self
    }
    /// Set the enhanced dependency graph string (CoNLL-U column 9).
    pub fn deps(mut self, deps: String) -> Self {
        self.deps = deps;
        self
    }
    /// Set the miscellaneous annotations string (CoNLL-U column 10).
    pub fn misc(mut self, misc: String) -> Self {
        self.misc = misc;
        self
    }
    /// Set whether the token is punctuation.
    pub fn is_punct(mut self, is_punct: bool) -> Self {
        self.is_punct = is_punct;
        self
    }
    /// Finalize the builder and return the constructed [`Token`].
    pub fn build(self) -> Token {
        Token {
            id: self.id,
            span: self.span,
            text: self.text,
            lemma: self.lemma,
            pos: self.pos,
            xpos: self.xpos,
            features: self.features,
            head: self.head,
            dep: self.dep,
            deps: self.deps,
            misc: self.misc,
            is_punct: self.is_punct,
        }
    }
}

/// One negation cue in a sentence, referenced by token id.
///
/// Reports structure only: which token carries the cue, its lemma, and
/// the head it attaches to in the dependency graph. What the negation
/// means for the sentence is the consumer's reading, not matra's.
///
/// Derived at [`Sentence`] construction from the dependency graph
/// (see [`Sentence::new`]) and serialized with the sentence, so every
/// language binding reads the same detection (RFC-0008).
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Negation {
    /// Token id of the cue (`not`, `never`, `no`, `neither`, `nor`).
    pub cue_id: usize,
    /// Lemma of the cue token.
    pub cue_lemma: String,
    /// Token id the cue attaches to (`0` if the cue is the root).
    pub head_id: usize,
}

/// Negation cue detection over a sentence's tokens.
///
/// The single Rust implementation behind [`Sentence::negations`]
/// (RFC-0008). The shapes were verified against live UDPipe parses:
/// `not` and `never` attach as `advmod` to the word they negate,
/// determiner `no` and `neither` attach as `det` to the noun they
/// negate, and `nor` attaches as `cc` to the conjunct it links.
/// Matching on the cue lemma plus the carrying relation keeps
/// precision, and it bounds recall to those shapes by decision:
/// `nothing` as a subject (`nsubj`) does not fire, pronominal
/// `neither` ("Neither of them came.", `nsubj` on the live model
/// rather than `det`) does not fire, and tokenizer-split `cannot`
/// (`can` + `not`) fires exactly once, on the `not` token.
fn detect_negations(tokens: &[Token]) -> Vec<Negation> {
    const CUE_LEMMAS: [&str; 5] = ["not", "never", "no", "neither", "nor"];
    const CUE_DEPS: [&str; 3] = ["advmod", "det", "cc"];
    tokens
        .iter()
        .filter(|t| CUE_LEMMAS.contains(&t.lemma.as_str()) && CUE_DEPS.contains(&t.dep.as_str()))
        .map(|t| Negation {
            cue_id: t.id,
            cue_lemma: t.lemma.clone(),
            head_id: t.head,
        })
        .collect()
}

/// One modal auxiliary in a sentence, referenced by token id.
///
/// Reports structure only: which token carries the modal, its lemma,
/// and the token it attaches to in the dependency graph. English
/// modals are ambiguous across readings (`must` is obligation in one
/// sentence and inference in the next) and resolving that needs
/// context matra does not model, so the epistemic/deontic/dynamic
/// category is the consumer's reading, not matra's.
///
/// Derived at [`Sentence`] construction from the dependency graph
/// (see [`Sentence::new`]) and serialized with the sentence, so every
/// language binding reads the same detection (RFC-0008).
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Modal {
    /// Token id of the modal auxiliary.
    pub aux_id: usize,
    /// Lemma of the auxiliary, one of the ten-lemma closed class
    /// documented on [`Sentence::modals`].
    pub aux_lemma: String,
    /// Token id the auxiliary attaches to (`0` if it is the root).
    pub head_id: usize,
}

/// Modal auxiliary detection over a sentence's tokens.
///
/// The single Rust implementation behind [`Sentence::modals`]
/// (RFC-0008). The closed class is the ten lemmas the UD English
/// treebank tags `MD`, enumerated from live UDPipe parses (verified
/// 2026-08-21): `can`, `could`, `may`, `might`, `must`, `ought`,
/// `shall`, `should`, `will`, `would`. A class lemma matches when it
/// carries the `aux` relation or the `AUX` part of speech, because
/// the live model does not park every modal on `aux`: under VP
/// ellipsis ("Yes, you should.") the modal is promoted to root, and
/// with coordinated auxiliaries the first conjunct parses as root
/// with the second as `aux` ("He can and will succeed.") or `conj`
/// ("He can, and he will, succeed."), all verified live. The lemma
/// plus either signal keeps precision: the model's only other `AUX`
/// lemmas (`be`, `have`, `do`, `get`) are outside the class,
/// contracted forms fire because the model lemmatizes them into the
/// class (`wo` in `won't` carries lemma `will`, verified live), the
/// noun `will` does not fire because it never carries `aux` or
/// `AUX`, and `need`/`dare` are outside the class because the live
/// model tags them as main verbs (`VERB`, root), not modal
/// auxiliaries. In a multi-auxiliary chain (`might have been done`)
/// only the modal is in the class; `have` and `be` attach as
/// `aux`/`aux:pass` but are not modals, so the chain reports exactly
/// its modals, each with `head_id` pointing at the content verb.
fn detect_modals(tokens: &[Token]) -> Vec<Modal> {
    const MODAL_LEMMAS: [&str; 10] = [
        "can", "could", "may", "might", "must", "ought", "shall", "should", "will", "would",
    ];
    tokens
        .iter()
        .filter(|t| (t.dep == "aux" || t.pos == "AUX") && MODAL_LEMMAS.contains(&t.lemma.as_str()))
        .map(|t| Modal {
            aux_id: t.id,
            aux_lemma: t.lemma.clone(),
            head_id: t.head,
        })
        .collect()
}

/// The grammatical mood of a sentence's root clause, with the root's
/// token id.
///
/// Reports the `Mood` feature the parse assigns the root clause, read
/// off the root token itself or, when the root carries none, off its
/// first `cop`, `aux` or `aux:pass` child that does. The live model
/// parks finiteness on the auxiliary rather than the root in copular
/// ("The sky is blue.", root `ADJ`, `Mood=Ind` on the `cop`),
/// do-support ("He did leave.", root infinitive, `Mood=Ind` on the
/// `aux`) and passive ("The will was signed.", root participle,
/// `Mood=Ind` on the `aux:pass`) clauses, all verified live.
///
/// `mood` is the value as the provider emitted it (`Ind`, `Imp`, and
/// whatever else a model assigns), so a reader treats it as an open set.
/// Whether the clause is modalized is a separate fact: the modals that
/// attach to the root are in [`Sentence::modals`], each with its
/// `head_id`. What the mood commits the speaker to is the consumer's
/// reading, not matra's.
///
/// Derived at [`Sentence`] construction from the dependency graph (see
/// [`Sentence::new`]) and serialized with the sentence, so every
/// language binding reads the same detection (RFC-0008).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct RootMood {
    /// Token id of the sentence root (the token with `head == 0`).
    pub root_id: usize,
    /// The root clause's `Mood` feature value, for example `Ind`.
    pub mood: String,
}

/// Root-clause mood detection over a sentence's tokens.
///
/// The single Rust implementation behind [`Sentence::root_mood`]. `None`
/// when the sentence has no root, or when neither the root nor any of
/// its finite-carrier children (`cop`, `aux`, `aux:pass`) carries a
/// `Mood` feature, as in a modalized clause whose modal carries only
/// `VerbForm=Fin`. The read is of the root clause only: a mood in a
/// subordinate clause does not reach it.
fn detect_root_mood(tokens: &[Token]) -> Option<RootMood> {
    const FINITE_CARRIER_DEPS: [&str; 3] = ["cop", "aux", "aux:pass"];
    let root = tokens.iter().find(|t| t.head == 0)?;
    let mood = root.feat("Mood").or_else(|| {
        tokens
            .iter()
            .filter(|t| t.head == root.id && FINITE_CARRIER_DEPS.contains(&t.dep.as_str()))
            .find_map(|t| t.feat("Mood"))
    })?;
    Some(RootMood {
        root_id: root.id,
        mood: mood.to_string(),
    })
}

/// One clausal complement in a sentence, referenced by token id: a verb
/// governing a clause (`ccomp`).
///
/// Reports structure only: the verb, the head of the complement clause,
/// and the verb's subject when the parse has one in the same sentence.
/// The construction fires regardless of which verb fills it (`show`,
/// `report`, `suggest`, `think`, `ensure` all parse identically),
/// because verbs taking a clausal complement are an open class: any list
/// matra shipped would be incomplete while looking authoritative. Which
/// verb lemmas count as reporting or evidential is the consumer's
/// lexicon (see [`Sentence::clausal_complements_in`]), and whether the
/// source is credible or the claim attributed rather than asserted is
/// the consumer's reading, not matra's.
///
/// Derived at [`Sentence`] construction from the dependency graph
/// (see [`Sentence::new`]) and serialized with the sentence, so every
/// language binding reads the same detection (RFC-0008).
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub struct ClausalComplement {
    /// Token id of the verb governing the clausal complement.
    pub verb_id: usize,
    /// Lemma of that verb.
    pub verb_lemma: String,
    /// Token id of the head of the clausal complement (the token
    /// carrying the `ccomp` relation).
    pub ccomp_id: usize,
    /// Token id of the verb's subject (`nsubj`, `nsubj:pass`,
    /// `nsubjpass`), when the parse has one in this sentence.
    pub subject_id: Option<usize>,
    /// Lemma of that subject, when present.
    pub subject_lemma: Option<String>,
}

/// Clausal-complement detection over a sentence's tokens.
///
/// The single Rust implementation behind [`Sentence::clausal_complements`]
/// (RFC-0008). The shape was verified against live UDPipe parses
/// (2026-08-21): in "Smith reported that the effect vanished." the
/// complement head `vanished` attaches as `ccomp` to `reported`
/// (`VERB`, root) and `Smith` attaches as `nsubj` to `reported`. The
/// detector walks every `ccomp` arc whose head is a `VERB` and
/// records the verb, the complement head, and the verb's subject
/// child if it has one. The subject is optional by observed fact,
/// not caution: UDPipe splits "Smith et al. reported that ..." into
/// two sentences at the period in "et al." (verified live), leaving
/// the reporting verb with its `ccomp` but no `nsubj`, the
/// attribution stranded in the previous sentence. That is an
/// upstream segmentation defect this detector inherits and records
/// rather than papers over. A `ccomp` under a non-verb head ("I am
/// sure that it works.", `ccomp` on the adjective `sure`) is outside
/// the construction by decision: the detector reports verbal
/// complements only.
fn detect_clausal_complements(tokens: &[Token]) -> Vec<ClausalComplement> {
    const SUBJECT_DEPS: [&str; 3] = ["nsubj", "nsubj:pass", "nsubjpass"];
    tokens
        .iter()
        .filter(|c| c.dep == "ccomp")
        .filter_map(|c| {
            let verb = tokens.iter().find(|t| t.id == c.head && t.pos == "VERB")?;
            let subject = tokens
                .iter()
                .find(|t| t.head == verb.id && SUBJECT_DEPS.contains(&t.dep.as_str()));
            Some(ClausalComplement {
                verb_id: verb.id,
                verb_lemma: verb.lemma.clone(),
                ccomp_id: c.id,
                subject_id: subject.map(|s| s.id),
                subject_lemma: subject.map(|s| s.lemma.clone()),
            })
        })
        .collect()
}

/// One adverbial modifier attached to the sentence root, referenced
/// by token id.
///
/// Reports structure only: the `advmod` arc into the root, which is
/// where sentence-scope adverbs land ("Reportedly, the deal closed."
/// parses `reportedly` as `advmod` on the root `closed`, verified
/// live). The dependency graph does not distinguish sentence scope
/// from manner ("The team quickly shipped." puts `quickly` on the
/// same arc), and evidential adverbs are an open class, so matra
/// reports every root-attached adverbial and ships no lexicon: which
/// lemmas read as hearsay markers is the consumer's list (see
/// [`Sentence::root_adverbials_in`]), and what the marker does to
/// the claim is the consumer's reading, not matra's.
///
/// Derived at [`Sentence`] construction from the dependency graph
/// (see [`Sentence::new`]) and serialized with the sentence, so every
/// language binding reads the same detection (RFC-0008).
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub struct RootAdverbial {
    /// Token id of the adverbial modifier attached to the root.
    pub adv_id: usize,
    /// Lemma of the adverbial.
    pub adv_lemma: String,
}

/// Root-attached adverbial detection over a sentence's tokens.
///
/// The single Rust implementation behind [`Sentence::root_adverbials`]
/// (RFC-0008). Every token carrying `advmod` whose head is the root
/// is reported, whatever its part of speech: negation particles
/// ("not" is `PART` on the same arc) appear here and in
/// [`Sentence::negations`], and the overlap is intentional, because
/// both fields report arcs and neither assigns a reading. A sentence
/// with no root (malformed parse) reports none.
fn detect_root_adverbials(tokens: &[Token]) -> Vec<RootAdverbial> {
    let Some(root) = tokens.iter().find(|t| t.head == 0) else {
        return Vec::new();
    };
    tokens
        .iter()
        .filter(|t| t.dep == "advmod" && t.head == root.id)
        .map(|t| RootAdverbial {
            adv_id: t.id,
            adv_lemma: t.lemma.clone(),
        })
        .collect()
}

/// Which of the six Hearst (1992) lexico-syntactic patterns produced a
/// candidate hypernymy pair.
///
/// Each variant names a surface construction, not a semantic verdict:
/// the tag records which dependency shape matched, and whether the
/// hypernymy relation actually holds is the consumer's reading, not
/// matra's. Serialized in `snake_case` (`"such_as"`, `"and_other"`,
/// ...) so the tag crosses FFI as a plain string.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum HearstPattern {
    /// `NP such as NP`: "animals such as dogs".
    SuchAs,
    /// `such NP as NP`: "such authors as Herrick".
    SuchNpAs,
    /// `NP, including NP`: "countries, including Canada".
    Including,
    /// `NP, especially NP`: "countries, especially France".
    Especially,
    /// `NP and other NP`: "temples and other buildings".
    AndOther,
    /// `NP or other NP`: "bruises or other injuries".
    OrOther,
}

/// One noun phrase in a Hearst pair, referenced by token id.
///
/// `head_id` is the syntactic head noun; `first_id..=last_id` is the
/// contiguous token range of that noun plus its adjacent nominal
/// modifiers (`det`, `amod`, `compound`, `nummod`, `flat`), with the
/// pattern's own marker words (`such`, `other`) excluded. Ids are
/// sentence-scoped, so provenance holds against
/// [`Sentence::tokens`].
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct HearstSpan {
    /// Token id of the head noun.
    pub head_id: usize,
    /// Lemma of the head noun.
    pub head_lemma: String,
    /// Token id of the first token in the span.
    pub first_id: usize,
    /// Token id of the last token in the span.
    pub last_id: usize,
}

/// One candidate hypernymy pair from a Hearst pattern, as structure.
///
/// Reports the two spans and the construction that connected them.
/// It is a *candidate* by design: matra does not build a taxonomy or
/// assert the relation is true, it reports that the sentence used a
/// construction which conventionally signals one. Derived at the
/// annotate stage by the pipeline (the detector lives in
/// `matra::hearst`, outside the domain) and serialized with the
/// sentence, so every language binding reads the same detection
/// (RFC-0008).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct HearstPair {
    /// The construction that matched.
    pub pattern: HearstPattern,
    /// The general term's span (the class).
    pub hypernym: HearstSpan,
    /// The specific term's span (the member).
    pub hyponym: HearstSpan,
}

/// Passive-voice detection over a sentence's tokens: the single Rust
/// implementation behind [`Sentence::passive`]. True when any token
/// carries `nsubj:pass`, `nsubjpass` or `aux:pass`.
fn detect_passive(tokens: &[Token]) -> bool {
    tokens
        .iter()
        .any(|t| t.dep == "nsubj:pass" || t.dep == "nsubjpass" || t.dep == "aux:pass")
}

/// Dependency-tree depth over a sentence's tokens: the single Rust
/// implementation behind [`Sentence::tree_depth`].
///
/// O(n) per sentence: builds a `HashMap<id, head>` once, then walks
/// each token's ancestor chain with depth memoization in a second
/// `HashMap<id, usize>`. Each token's depth is computed at most once;
/// the visited set inside each walk detects a cycle, and a cycle
/// anywhere makes the sentence's depth `None`, surfacing the malformed
/// parse rather than silently truncating it. There is no depth ceiling.
fn measure_tree_depth(tokens: &[Token]) -> Option<usize> {
    use std::collections::{HashMap, HashSet};

    // Build id -> head once. `0` head means root.
    let head_by_id: HashMap<usize, usize> = tokens.iter().map(|t| (t.id, t.head)).collect();

    // Memoize depth per token id. `None` marks a token on or rooted in
    // a cycle.
    let mut depth_by_id: HashMap<usize, Option<usize>> = HashMap::new();

    // Compute depth for one token. Walks up the head chain, using a
    // visited set to detect cycles in this single chain. On hit,
    // memoizes via the cache to amortize across tokens that share
    // ancestors.
    fn depth_of(
        id: usize,
        head_by_id: &HashMap<usize, usize>,
        depth_by_id: &mut HashMap<usize, Option<usize>>,
    ) -> Option<usize> {
        if let Some(&d) = depth_by_id.get(&id) {
            return d;
        }
        let mut visited: HashSet<usize> = HashSet::new();
        let mut chain: Vec<usize> = Vec::new();
        let mut cur = id;

        // Walk to root or to a memoized ancestor or to a cycle.
        let base_depth = loop {
            if !visited.insert(cur) {
                // Cycle detected: every token in the chain is malformed.
                for &cid in &chain {
                    depth_by_id.insert(cid, None);
                }
                return None;
            }
            let head = head_by_id.get(&cur).copied().unwrap_or(0);
            if head == 0 {
                // cur is root or its head is missing: depth contribution from this point is 0.
                chain.push(cur);
                break 0usize;
            }
            if let Some(&d) = depth_by_id.get(&head) {
                let Some(d) = d else {
                    // The ancestor sits on a cycle, so this chain does too.
                    chain.push(cur);
                    for &cid in &chain {
                        depth_by_id.insert(cid, None);
                    }
                    return None;
                };
                chain.push(cur);
                break d.saturating_add(1);
            }
            chain.push(cur);
            cur = head;
        };

        // Walk back down the chain, assigning depths in order.
        // chain[last] is the closest-to-root token; chain[0] is the
        // original `id`. Depth of chain[last] = base_depth; each step
        // away from root adds 1.
        let n = chain.len();
        for (i, &cid) in chain.iter().enumerate() {
            let d = base_depth.saturating_add(n - 1 - i);
            depth_by_id.insert(cid, Some(d));
        }
        Some(base_depth.saturating_add(n - 1))
    }

    let mut deepest = 0usize;
    for t in tokens {
        deepest = deepest.max(depth_of(t.id, &head_by_id, &mut depth_by_id)?);
    }
    Some(deepest)
}

/// One parsed sentence: its text plus its ordered tokens.
///
/// Invariants downstream code relies on:
/// - `tokens` are id-sorted ascending.
/// - Exactly one token has `head == 0` (the syntactic root).
/// - All `head` references point to another token in the same sentence
///   or to `0`.
/// - Following `head` references never loops back on itself.
///
/// Every sentence `Engine::analyze` returns holds them: the engine
/// checks what the parser returns before it builds a sentence, and fails
/// with [`Error::ParseFailed`] naming the check otherwise. A sentence
/// read back from JSON or built by hand with [`Sentence::new`], which
/// trusts its caller, can break any of them, so nothing downstream relies
/// on them for safety: [`Sentence::tree_depth`] is `None` on a cycle,
/// [`Sentence::subtree`] carries a visited set, and every outcome over a
/// [`Document`] holds on a sentence that breaks them all.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Sentence {
    /// Where the sentence lies in the analysed text, in UTF-8 bytes:
    /// from the start of its first token to the end of its last, derived
    /// at construction from the token spans. The authority on where the
    /// sentence came from; `text` is normalized.
    pub span: Span,
    /// Sentence text, normalized: the engine rebuilds it from the token
    /// forms, joined by a space unless a token carries `SpaceAfter=No`
    /// in `misc`, so its whitespace can differ from the source; `span`
    /// locates the source.
    pub text: String,
    /// CoNLL-U tokens in id-sorted order.
    ///
    /// Read as empty when the JSON has no `tokens` key, which is how
    /// `matra analyze --json --depth sentence` writes a sentence, so a cut
    /// document reads back into this type. Empty then means "not written",
    /// not "no tokens": the sentence's derived fields still describe them.
    #[serde(default)]
    pub tokens: Vec<Token>,
    /// Count of non-punctuation tokens, derived at construction.
    ///
    /// Counts linguistic tokens as the NLP provider identified them, so
    /// it can differ from a whitespace split of the same text.
    /// Readability (Flesch-Kincaid) splits on whitespace, as the
    /// formula's specification asks.
    pub word_count: usize,
    /// Maximum depth of the dependency tree, derived at construction:
    /// the longest path from any token to the root, the root at depth 0.
    ///
    /// `None` when following `head` references from some token loops
    /// (a malformed parse), so the failure is a value a reader must
    /// handle rather than a number that reads as a very deep tree. The
    /// walk carries a visited set and has no depth ceiling: a well-formed
    /// tree of any depth reports its true depth. `Some(0)` for a sentence
    /// with no tokens.
    pub tree_depth: Option<usize>,
    /// Whether the sentence contains a passive-voice construction (a
    /// token carrying `nsubj:pass`, `nsubjpass` or `aux:pass`), derived
    /// at construction.
    pub passive: bool,
    /// Negation cues derived from the dependency graph at construction
    /// (see [`Sentence::new`]). Serialized with the sentence so the
    /// detection crosses FFI as data (RFC-0008).
    pub negations: Vec<Negation>,
    /// Modal auxiliaries derived from the dependency graph at
    /// construction (see [`Sentence::new`]): tokens from the closed
    /// class `can`, `could`, `may`, `might`, `must`, `ought`,
    /// `shall`, `should`, `will`, `would`, matched by lemma on the
    /// `aux` relation or the `AUX` part of speech (the latter catches
    /// modals the model promotes to root or `conj` under VP ellipsis
    /// and coordination). Serialized with the sentence so the
    /// detection crosses FFI as data (RFC-0008).
    pub modals: Vec<Modal>,
    /// Clausal complements derived from the dependency graph at
    /// construction (see [`Sentence::new`]): each verb governing a
    /// clause (`ccomp`), with its subject when the parse has one. The
    /// construction is reported for every verb that fills it; which
    /// verbs count as reporting or evidential is the consumer's lexicon
    /// ([`Sentence::clausal_complements_in`]). Serialized with the
    /// sentence so the detection crosses FFI as data (RFC-0008).
    pub clausal_complements: Vec<ClausalComplement>,
    /// Adverbial modifiers attached to the root, derived at
    /// construction (see [`Sentence::new`]): the arc sentence-scope
    /// adverbs land on. Every root-attached `advmod` is reported;
    /// which lemmas read as evidential is the consumer's lexicon
    /// ([`Sentence::root_adverbials_in`]). Serialized with the
    /// sentence so the detection crosses FFI as data (RFC-0008).
    pub root_adverbials: Vec<RootAdverbial>,
    /// Candidate hypernymy pairs from the six Hearst (1992) patterns.
    ///
    /// Unlike the other derived fields, this one is not computed by
    /// [`Sentence::new`]: the detector lives in `matra::hearst`, a
    /// module outside the domain (EP-0007 M5 boundary), so the pipeline
    /// fills the field at the annotate stage. A hand-built `Sentence`
    /// carries an empty vector until the caller runs the detector.
    /// Serialized with the sentence so the detection crosses FFI as
    /// data (RFC-0008).
    pub hearst_pairs: Vec<HearstPair>,
    /// The root clause's grammatical mood with the root's token id,
    /// derived at construction (see [`RootMood`]). `None` when the
    /// sentence has no root or its root clause carries no `Mood`
    /// feature. Serialized with the sentence so the detection crosses
    /// FFI as data (RFC-0008).
    pub root_mood: Option<RootMood>,
}

impl Sentence {
    /// Construct a `Sentence` from a text string and a token vector.
    ///
    /// The caller is responsible for upholding the invariants documented
    /// on [`Sentence`]; this constructor does not validate them. Derived
    /// fields (`span`, `word_count`, `tree_depth`, `passive`, `negations`,
    /// `modals`, `clausal_complements`, `root_adverbials`, `root_mood`)
    /// are computed here and reflect `tokens` as passed in; mutating
    /// `tokens` afterwards does not recompute them, and keeping them
    /// consistent is part of the same caller contract.
    /// `hearst_pairs` starts empty and is filled at the annotate stage
    /// by the pipeline, because its detector lives outside the domain
    /// (see the field's documentation).
    pub fn new(text: String, tokens: Vec<Token>) -> Self {
        let span = match (tokens.first(), tokens.last()) {
            (Some(first), Some(last)) => Span::new(first.span.byte_start, last.span.byte_end),
            _ => Span::default(),
        };
        let word_count = tokens.iter().filter(|t| !t.is_punct).count();
        let tree_depth = measure_tree_depth(&tokens);
        let passive = detect_passive(&tokens);
        let negations = detect_negations(&tokens);
        let modals = detect_modals(&tokens);
        let clausal_complements = detect_clausal_complements(&tokens);
        let root_adverbials = detect_root_adverbials(&tokens);
        let root_mood = detect_root_mood(&tokens);
        Self {
            span,
            text,
            tokens,
            word_count,
            tree_depth,
            passive,
            negations,
            modals,
            clausal_complements,
            root_adverbials,
            hearst_pairs: Vec::new(),
            root_mood,
        }
    }

    /// The sentence a parser's tokens make, as `Engine::annotate` builds
    /// it: [`Sentence::new`] over `tokens`, with the text rebuilt from
    /// their forms, joined by a space unless a token carries
    /// `SpaceAfter=No` in `misc` (the CoNLL-U convention).
    pub(crate) fn from_tokens(tokens: Vec<Token>) -> Self {
        let mut text = String::new();
        for (i, token) in tokens.iter().enumerate() {
            text.push_str(&token.text);
            if i + 1 < tokens.len() && !token.misc.contains("SpaceAfter=No") {
                text.push(' ');
            }
        }
        Self::new(text, tokens)
    }

    /// The clausal complements whose verb lemma is in the
    /// caller-supplied lexicon.
    ///
    /// Verbs taking a clausal complement are an open class, so matra
    /// ships no default lexicon: an incomplete list that looks
    /// authoritative is worse than none. The structural detection
    /// ([`Sentence::clausal_complements`]) crosses FFI as data; this is a
    /// Rust-side convenience view over it, per RFC-0008's criterion
    /// (derivations cross as fields, views over crossing data stay
    /// methods). Non-Rust consumers filter the field by `verb_lemma` the
    /// same way.
    pub fn clausal_complements_in(&self, lexicon: &[&str]) -> Vec<&ClausalComplement> {
        self.clausal_complements
            .iter()
            .filter(|r| lexicon.contains(&r.verb_lemma.as_str()))
            .collect()
    }

    /// The root-attached adverbials whose lemma is in the
    /// caller-supplied lexicon.
    ///
    /// Evidential adverbs are an open class, so matra ships no default
    /// lexicon; see [`Sentence::clausal_complements_in`] for the
    /// reasoning and the RFC-0008 classification. Non-Rust consumers
    /// filter [`Sentence::root_adverbials`] by `adv_lemma` the same way.
    pub fn root_adverbials_in(&self, lexicon: &[&str]) -> Vec<&RootAdverbial> {
        self.root_adverbials
            .iter()
            .filter(|a| lexicon.contains(&a.adv_lemma.as_str()))
            .collect()
    }

    /// Tokens excluding punctuation.
    pub fn content_tokens(&self) -> Vec<&Token> {
        self.tokens.iter().filter(|t| !t.is_punct).collect()
    }

    /// The root token (head = 0). Returns None if no root found.
    pub fn root_token(&self) -> Option<&Token> {
        self.tokens.iter().find(|t| t.head == 0)
    }

    /// Direct children of the token with the given id.
    pub fn children_of(&self, id: usize) -> Vec<&Token> {
        self.tokens.iter().filter(|t| t.head == id).collect()
    }

    /// The head token of the token with the given id.
    pub fn head_of(&self, id: usize) -> Option<&Token> {
        self.tokens.iter().find(|t| t.id == id).and_then(|t| {
            if t.head == 0 {
                None
            } else {
                self.tokens.iter().find(|h| h.id == t.head)
            }
        })
    }

    /// All tokens in the subtree rooted at the given id (including the root).
    /// Returns tokens sorted by id (document order).
    /// Safe on cyclic graphs: visited set prevents infinite loops.
    pub fn subtree(&self, id: usize) -> Vec<&Token> {
        let mut result = Vec::new();
        let mut stack = vec![id];
        let mut visited = std::collections::HashSet::new();
        while let Some(current) = stack.pop() {
            if !visited.insert(current) {
                continue;
            }
            if let Some(token) = self.tokens.iter().find(|t| t.id == current) {
                result.push(token);
                for child in &self.tokens {
                    if child.head == current && child.id != current {
                        stack.push(child.id);
                    }
                }
            }
        }
        result.sort_by_key(|t| t.id);
        result
    }
}

// ---------------------------------------------------------------------------
// Document output -- what the annotate and compose stages fill
// ---------------------------------------------------------------------------

/// The paragraph-level metric record: what `Engine::compose` measures
/// over one paragraph.
///
/// Each slot gates on its own condition and is `None` when the
/// paragraph did not meet it (or when the metric suite has not run), so
/// no outer `Option` wraps the record.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct ParagraphMetrics {
    /// Flesch-Kincaid grade level, when the paragraph holds more than
    /// ten words.
    pub readability_grade: Option<f64>,
    /// Content-word ratio, when the paragraph holds a word.
    pub lexical_density: Option<f64>,
    /// Brotli compression ratio (a redundancy proxy), when the paragraph
    /// holds more than fifty words and is within the compression size
    /// bound.
    pub compression_ratio: Option<f64>,
}

/// What kind of block a [`Paragraph`] was read from.
///
/// Serialized as `paragraph`, `quote` and `list_item`. A reader treats
/// the set as open: a later reader can add a kind.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum ParagraphKind {
    /// An ordinary paragraph, and every paragraph of plain text and of
    /// block input.
    Paragraph,
    /// A paragraph inside a block quote, at any depth, a list item
    /// inside a quote included. Quoted text is not the author's own, so
    /// a quote is never parsed and never measured: it keeps no sentences
    /// and its metric slots stay `None`.
    Quote,
    /// The text of a list item outside any block quote.
    ListItem,
}

/// One paragraph of prose. `Engine::annotate` attaches its sentences and
/// `Engine::compose`, which runs the metric suite, fills its
/// [`ParagraphMetrics`].
///
/// A [`ParagraphKind::Quote`] paragraph is skipped by both stages: it
/// keeps no sentences, and its metric slots stay `None`.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Paragraph {
    /// Where the paragraph lies in the analysed text, in UTF-8 bytes,
    /// with trailing whitespace excluded. For markdown it covers the
    /// block quote and list markers on the paragraph's lines, so every
    /// byte of the input that is not whitespace lies in exactly one
    /// heading, paragraph or skipped span.
    pub span: Span,
    /// The paragraph's text, normalized: for markdown, the inline text
    /// the parser reads, with the markup removed (emphasis markers, link
    /// and image syntax, the backticks of a code span, block quote and
    /// list markers), soft line breaks as `\n`, escapes and entities
    /// decoded; for plain text and block input, a verbatim slice. `span`
    /// is the authority on where it came from.
    pub text: String,
    /// The kind of block the paragraph was read from.
    pub kind: ParagraphKind,
    /// Sentences produced by parsing this paragraph (attached by
    /// `Engine::annotate`).
    ///
    /// Read as empty when the JSON has no `sentences` key, as
    /// `matra analyze --json --depth paragraph` writes a paragraph; the
    /// paragraph's metrics still describe them.
    #[serde(default)]
    pub sentences: Vec<Sentence>,
    /// The paragraph's metrics, filled by `Engine::compose`.
    pub metrics: ParagraphMetrics,
}

impl Paragraph {
    /// Construct a new `Paragraph` with empty sentences and an empty
    /// metric record. `Engine::annotate` attaches the sentences and
    /// `Engine::compose` fills the record.
    pub fn new(text: String, kind: ParagraphKind, span: Span) -> Self {
        Self {
            span,
            text,
            kind,
            sentences: Vec::new(),
            metrics: ParagraphMetrics::default(),
        }
    }

    /// Total word count across all sentences.
    ///
    /// Saturates at `usize::MAX` rather than overflowing: the counts are
    /// whatever the sentences carry, and a document read back from JSON
    /// can carry any, so a sum past the largest count reads as that.
    pub fn word_count(&self) -> usize {
        self.sentences
            .iter()
            .fold(0usize, |total, s| total.saturating_add(s.word_count))
    }

    /// Number of sentences.
    pub fn sentence_count(&self) -> usize {
        self.sentences.len()
    }
}

/// A structural section of a document (heading + paragraphs).
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Section {
    /// Section heading, if any, its text normalized as a paragraph's is.
    /// `None` for the section holding what precedes the first heading,
    /// and for plain text (which produces one heading-less section).
    pub heading: Option<String>,
    /// Heading depth (0 with no heading, 1+ for a heading: markdown `#`
    /// is 1, `##` is 2, and a block input heading keeps its level).
    pub level: usize,
    /// Where the heading lies in the analysed text, in UTF-8 bytes,
    /// markers included (`## Title` from the first `#`); `None` with no
    /// heading.
    pub span: Option<Span>,
    /// Paragraphs in document order.
    pub paragraphs: Vec<Paragraph>,
}

impl Section {
    /// Construct a new section with the given heading, level, heading
    /// span and paragraphs.
    pub fn new(
        heading: Option<String>,
        level: usize,
        span: Option<Span>,
        paragraphs: Vec<Paragraph>,
    ) -> Self {
        Self {
            heading,
            level,
            span,
            paragraphs,
        }
    }
}

/// The document-level metric record: what `Engine::compose` measures
/// over the whole document.
///
/// The counts are what the command line's table prints and what a
/// binding would otherwise rebuild, materialized so they cross FFI as
/// data (methods do not). The ratios gate on their own conditions and
/// are `None` when unmet, so no outer `Option` wraps the record. Before
/// the metric suite runs, every count is `0` and every ratio `None`.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct DocumentMetrics {
    /// Non-punctuation tokens across every sentence.
    pub total_words: usize,
    /// Sentences across every paragraph.
    pub total_sentences: usize,
    /// Paragraphs across every section, blockquotes included.
    pub paragraph_count: usize,
    /// Mean sentence length in words, when the document holds a
    /// sentence.
    pub mean_sentence_length: Option<f64>,
    /// Sample standard deviation of sentence length in words, when the
    /// document holds at least two sentences (the sample deviation of
    /// one value is undefined).
    pub sentence_length_std: Option<f64>,
    /// Vocabulary type-token ratio over lemmas, when the document holds
    /// a non-punctuation token.
    ///
    /// A raw ratio with no length correction: it falls as a document
    /// grows, because repetition accumulates, so two documents of
    /// different lengths are not comparable on it.
    pub vocabulary_ttr: Option<f64>,
    /// Share of non-punctuation tokens that are nouns ending in a
    /// nominalizing suffix, when the document holds a non-punctuation
    /// token.
    pub nominalization_ratio: Option<f64>,
    /// Fraction of sentences containing a passive-voice construction,
    /// when the document holds a sentence.
    pub passive_ratio: Option<f64>,
}

/// The version of the document schema this build emits, as
/// `"major.minor"`: the value of [`Provenance::schema`] and nowhere else.
///
/// A minor adds optional fields only, which a reader that ignores fields
/// it does not recognize never notices. A major is anything else: a
/// field removed, renamed or retyped, or a field whose meaning changes.
/// A reader rejects a major it does not know. The published JSON Schema
/// for each major is in `spec/schema/`.
pub const SCHEMA_VERSION: &str = "2.0";

/// Who produced a result: a model-backed component named by what it is,
/// which model it ran and that model's digest and licence.
///
/// The parser reports its own, so a consumer sees which model produced a
/// parse rather than discovering a change of model as a shift in the
/// numbers. A component loaded from a model matra does not pin knows the
/// model's digest but not its name or licence, and leaves those `None`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Identity {
    /// The component, for example `udpipe`.
    pub name: String,
    /// The model's name, when the component runs a model matra pins,
    /// for example `english-ewt-ud-2.5-191206`.
    pub model: Option<String>,
    /// SHA-256 of the model artifact, lowercase hex, when the component
    /// runs a model.
    pub sha256: Option<String>,
    /// The model's licence, named for a reader, when it is known. A model
    /// is a separate work from matra and carries its own terms.
    pub license: Option<String>,
}

impl Identity {
    /// Construct an identity from its four facts.
    pub fn new(
        name: impl Into<String>,
        model: Option<String>,
        sha256: Option<String>,
        license: Option<String>,
    ) -> Self {
        Self {
            name: name.into(),
            model,
            sha256,
            license,
        }
    }
}

/// The input a [`Document`] was analysed from: where it came from, how
/// many bytes it held, and their digest.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct ProvenanceSource {
    /// The path the input was read from, `None` for in-memory text.
    ///
    /// In JSON a path that is not valid UTF-8 has its undecodable bytes
    /// replaced with U+FFFD, since a JSON string cannot carry them; the
    /// Rust value keeps the path exact.
    #[serde(serialize_with = "serialize_path_lossy")]
    pub path: Option<PathBuf>,
    /// The input's length in bytes.
    pub bytes: usize,
    /// SHA-256 of the input's bytes, lowercase hex, computed once when
    /// the document is annotated.
    pub sha256: String,
}

impl ProvenanceSource {
    /// Construct the record of one input.
    pub fn new(path: Option<PathBuf>, bytes: usize, sha256: impl Into<String>) -> Self {
        Self {
            path,
            bytes,
            sha256: sha256.into(),
        }
    }
}

/// A path as a JSON string, lossily where the path is not UTF-8. serde's
/// own form refuses such a path, which would fail the whole document
/// over a name.
fn serialize_path_lossy<S: serde::Serializer>(
    path: &Option<PathBuf>,
    serializer: S,
) -> std::result::Result<S::Ok, S::Error> {
    match path {
        Some(p) => serializer.serialize_some(&p.to_string_lossy()),
        None => serializer.serialize_none(),
    }
}

/// Where a result came from: the matra build and schema version that
/// produced it, the parser that read the document, the format it was read
/// as, and the input itself.
///
/// A [`Document`] carries it as recorded when the document was read. A
/// result derived from a document ([`Summary`], [`Keyphrases`],
/// [`Clusters`]) carries a copy of the document's, plus the `method` that
/// ran and the `parameters` that shaped it; on a document both are
/// `None`, and absent from the JSON.
///
/// No timestamp: matra's outputs are deterministic, and a timestamp would
/// make two identical analyses differ.
///
/// A record, not proof. It says how a result says it was made: a
/// document read back from JSON, or built with [`Provenance::new`], carries
/// whatever provenance it was given, and an outcome derived from it copies
/// that on. What it can be checked against is `source.sha256`, the digest
/// of the analysed text every span points into.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Provenance {
    /// The matra version that produced the result, for example `0.3.0`.
    pub matra: String,
    /// The schema version of the result, `"major.minor"`
    /// ([`SCHEMA_VERSION`]). The one place a schema version appears.
    pub schema: String,
    /// The parser that produced the sentences and tokens.
    pub parser: Identity,
    /// The format the input was read as.
    pub format: Format,
    /// The input.
    pub source: ProvenanceSource,
    /// The method that produced a derived result, for example `tfidf`:
    /// `None` on a document. A reader treats the set as open.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub method: Option<String>,
    /// The parameters that shaped a derived result: `None` on a
    /// document.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub parameters: Option<Parameters>,
}

/// The parameters that shaped a derived result, as it ran: only those
/// the method takes are `Some`, and only those appear in the JSON.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Parameters {
    /// How many sentences a summary keeps, or how many phrases keyphrase
    /// extraction returns at most.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub n: Option<usize>,
    /// The cosine similarity an edge between two sentences cleared to
    /// join them in a cluster.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub threshold: Option<f64>,
    /// The embedder whose vectors were clustered, as it reports itself
    /// (`Embedder::identity`): its name, its model, the digest over the
    /// model's artifacts and its licence, so the scores stay attributable
    /// to one geometry and the model's terms travel with them.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub embedder: Option<Identity>,
}

impl Parameters {
    /// The parameters of a method that takes a count.
    pub(crate) fn count(n: usize) -> Self {
        Self {
            n: Some(n),
            ..Self::default()
        }
    }

    /// The parameters of clustering: the threshold and the embedder.
    pub(crate) fn clustering(threshold: f64, embedder: Identity) -> Self {
        Self {
            threshold: Some(threshold),
            embedder: Some(embedder),
            ..Self::default()
        }
    }
}

impl Provenance {
    /// The provenance of a result this build produces: `matra` and
    /// `schema` are this build's own, the rest is what the caller names,
    /// unchecked. A caller building a document by hand names a parser
    /// that did not run at its own word; see [`Provenance`].
    pub fn new(parser: Identity, format: Format, source: ProvenanceSource) -> Self {
        Self {
            matra: env!("CARGO_PKG_VERSION").to_string(),
            schema: SCHEMA_VERSION.to_string(),
            parser,
            format,
            source,
            method: None,
            parameters: None,
        }
    }

    /// This provenance, copied onto a result derived from the document it
    /// describes, with the method that ran and its parameters.
    pub(crate) fn derived(&self, method: &str, parameters: Parameters) -> Self {
        Self {
            method: Some(method.to_string()),
            parameters: Some(parameters),
            ..self.clone()
        }
    }

    /// The provenance of a hand-built document in a unit test: a parser
    /// named `test`, plain text, an empty in-memory input.
    #[cfg(test)]
    pub(crate) fn for_test() -> Self {
        Self::new(
            Identity::new("test", None, None, None),
            Format::PlainText,
            ProvenanceSource::new(None, 0, ""),
        )
    }
}

/// The full analysis output. `Engine::annotate` builds the section tree,
/// attaches each paragraph's parsed sentences, records what the reader
/// skipped and records the provenance; `Engine::compose` fills the metric
/// records.
///
/// Paragraphs live in sections (single source of truth). The document's
/// aggregates are a record, [`DocumentMetrics`], rather than methods, so
/// they cross FFI as data.
///
/// A document is data. It can come from `Engine::analyze`, from JSON a
/// caller saved and read back (`--json` output, cut by `--depth` or
/// whole), or from the public constructors, and nothing tells those
/// apart. So every outcome (`summarize`, `keyphrases`, `clusters` and
/// their `_with` forms) and every view on these types holds on a document
/// whose parts contradict each other (spans out of range or inverted,
/// heads pointing nowhere or in a cycle, empty sentences, texts that do
/// not match their tokens): it returns a result that carries the
/// contradiction through, or an error, and never panics or loops.
/// `tests/hostile_documents.rs` holds them to it.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Document {
    /// Where the document came from and what produced it.
    pub provenance: Provenance,
    /// Section tree (the single source of truth for paragraph ownership).
    ///
    /// Read as empty when the JSON has no `sections` key, as
    /// `matra analyze --json --depth document` writes a document; its
    /// metrics still describe them.
    #[serde(default)]
    pub sections: Vec<Section>,
    /// The document's metrics, filled by `Engine::compose`.
    pub metrics: DocumentMetrics,
    /// What the reader saw and did not analyse (a table, a code block,
    /// front matter), in document order. Never silently dropped.
    pub skipped: Vec<Skipped>,
}

impl Document {
    /// Construct a new `Document` from its provenance, a section tree and
    /// what the reader skipped, with an empty metric record;
    /// `Engine::compose` fills it in.
    pub fn new(provenance: Provenance, sections: Vec<Section>, skipped: Vec<Skipped>) -> Self {
        Self {
            provenance,
            sections,
            metrics: DocumentMetrics::default(),
            skipped,
        }
    }

    /// Flat iterator over all paragraphs.
    pub fn paragraphs(&self) -> impl Iterator<Item = &Paragraph> {
        self.sections.iter().flat_map(|s| s.paragraphs.iter())
    }

    /// Flat mutable iterator over all paragraphs.
    pub fn paragraphs_mut(&mut self) -> impl Iterator<Item = &mut Paragraph> {
        self.sections
            .iter_mut()
            .flat_map(|s| s.paragraphs.iter_mut())
    }

    /// Flat iterator over all sentences across all paragraphs.
    pub fn sentences(&self) -> impl Iterator<Item = &Sentence> {
        self.sections
            .iter()
            .flat_map(|s| s.paragraphs.iter())
            .flat_map(|p| p.sentences.iter())
    }

    /// Flat iterator over all tokens across all sentences.
    pub fn tokens(&self) -> impl Iterator<Item = &Token> {
        self.sections
            .iter()
            .flat_map(|s| s.paragraphs.iter())
            .flat_map(|p| p.sentences.iter())
            .flat_map(|s| s.tokens.iter())
    }
}

// ---------------------------------------------------------------------------
// Outcomes: what the outcome functions derive from a document
// ---------------------------------------------------------------------------

/// The sentences that best represent a document, from `matra::summarize`.
///
/// `provenance` is the document's, with the method that ranked the
/// sentences and the `n` it was asked for. Each method scores on its own
/// scale, so a score compares with another score from the same method
/// only.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Summary {
    /// The document's provenance, plus `method` and `parameters`.
    pub provenance: Provenance,
    /// At most `n` sentences, in document order.
    pub sentences: Vec<SummarySentence>,
}

/// One sentence a summary keeps.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct SummarySentence {
    /// The sentence's position among the document's sentences
    /// ([`Document::sentences`]), counting from 0.
    pub index: usize,
    /// Where the sentence lies in the analysed text, as on
    /// [`Sentence::span`].
    pub span: Span,
    /// The sentence's text, as on [`Sentence::text`].
    pub text: String,
    /// The method's score for it, higher is more representative.
    pub score: f64,
}

impl SummarySentence {
    /// The sentence at `index` of `sentence`, with its score.
    pub(crate) fn new(index: usize, sentence: &Sentence, score: f64) -> Self {
        Self {
            index,
            span: sentence.span,
            text: sentence.text.clone(),
            score,
        }
    }
}

/// The phrases that best name what a document is about, from
/// `matra::keyphrases`.
///
/// `provenance` is the document's, with the method that ranked the
/// phrases and the `n` it was asked for. Each method scores on its own
/// scale, so a score compares with another score from the same method
/// only.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Keyphrases {
    /// The document's provenance, plus `method` and `parameters`.
    pub provenance: Provenance,
    /// At most `n` phrases, highest score first. Equal scores are ordered
    /// the same way on every run.
    pub phrases: Vec<Keyphrase>,
}

/// One ranked keyphrase and where it occurs.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Keyphrase {
    /// The phrase: the lowercased lemmas of its words, joined by a space.
    pub phrase: String,
    /// The method's score for it, higher is more relevant.
    pub score: f64,
    /// Every stretch of the analysed text the phrase was found in, in
    /// document order: from its first word's start to its last word's
    /// end, so a word the method passes over (a stop word between two
    /// content words) lies inside the span.
    pub occurrences: Vec<Span>,
}

/// Sentences of a document grouped by the similarity of their
/// embeddings, from `matra::clusters`: model opinion, standing alone,
/// never attached to [`Document`] (RFC-0010).
///
/// `provenance` is the document's, with the method and the `threshold`
/// and `embedder` that shaped the result, so the scores cannot be
/// attributed to a model that did not produce them.
///
/// A cluster is a connected component of the graph whose edges are the
/// sentence pairs with a cosine similarity of at least `threshold`, so
/// membership is transitive: two sentences can share a cluster without
/// sharing an edge, and co-membership must never be read as pairwise
/// similarity. `edges` lists every pair that cleared the bar; a pair not
/// listed is no claim, not low similarity.
///
/// A sentence with no edge appears in no cluster: singletons are excluded
/// by construction, so "not in any cluster" is a meaningful count.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Clusters {
    /// The document's provenance, plus `method` and `parameters`.
    pub provenance: Provenance,
    /// The clusters, ordered by their smallest member.
    pub clusters: Vec<Cluster>,
    /// Every pair of sentences whose similarity cleared the threshold,
    /// ordered by `a`, then `b`. Both ends of an edge are members of the
    /// same cluster.
    pub edges: Vec<SimilarityEdge>,
}

/// One connected component of the above-threshold similarity graph.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Cluster {
    /// The sentence indices ([`Document::sentences`] positions) in the
    /// component, ascending.
    pub members: Vec<usize>,
}

impl Cluster {
    /// A cluster of `members`, ascending.
    pub(crate) fn new(members: Vec<usize>) -> Self {
        Self { members }
    }
}

/// A similarity between two sentences that cleared the threshold,
/// `a < b`.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct SimilarityEdge {
    /// The lower sentence index.
    pub a: usize,
    /// The higher sentence index.
    pub b: usize,
    /// Their cosine similarity, in the embedder's geometry.
    pub score: f64,
}

impl SimilarityEdge {
    /// The edge between sentences `a < b`.
    pub(crate) fn new(a: usize, b: usize, score: f64) -> Self {
        Self { a, b, score }
    }
}

// ---------------------------------------------------------------------------
// Source / corpus types
// ---------------------------------------------------------------------------

/// A document's format: which reader reads its bytes.
///
/// `Pdf` and `Docx` are recognised, by extension and by their bytes, but
/// no reader for either ships, so analysing one returns
/// [`Error::UnsupportedFormat`] unless the caller registered a reader for
/// it with `Engine::with_reader`. [`Format::Other`] names any other
/// format, the one a caller's own reader reads.
///
/// Serialized in the format vocabulary every surface shares:
/// `markdown`, `plain_text`, `blocks`, `pdf`, `docx`, and an
/// [`Format::Other`] as its own name. A reader treats the set as open,
/// and reading a name back gives the variant matra has for it, or
/// [`Format::Other`].
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum Format {
    /// Markdown (CommonMark, with tables, strikethrough, task lists and
    /// front matter).
    Markdown,
    /// Plain text: paragraphs separated by blank lines.
    PlainText,
    /// Headings and paragraphs the caller supplies: the JSON array whose
    /// schema is `spec/schema/v2/blocks.schema.json`, or [`Block`]s.
    Blocks,
    /// PDF (recognised; no reader ships).
    Pdf,
    /// DOCX (recognised; no reader ships).
    Docx,
    /// A format matra has no variant for, by its name in the shared
    /// vocabulary (`html`, `org`): what a caller names on an input to
    /// reach a reader it registered for that name. No reader ships for
    /// one, and nothing detects one: no signature or extension implies
    /// it. Build one with [`Format::named`], which gives the variant
    /// matra has for a name it knows, so `Other("markdown")` never
    /// stands in for [`Format::Markdown`].
    #[serde(untagged)]
    Other(String),
}

impl Format {
    /// The format a name in the shared vocabulary names: the variant
    /// matra has for it (`"markdown"` is [`Format::Markdown`]), or
    /// [`Format::Other`] holding the name.
    ///
    /// ```
    /// # use matra::domain::Format;
    /// assert_eq!(Format::named("markdown"), Format::Markdown);
    /// assert_eq!(Format::named("html"), Format::Other("html".to_string()));
    /// ```
    pub fn named(name: impl Into<String>) -> Format {
        let name = name.into();
        match name.as_str() {
            "markdown" => Format::Markdown,
            "plain_text" => Format::PlainText,
            "blocks" => Format::Blocks,
            "pdf" => Format::Pdf,
            "docx" => Format::Docx,
            _ => Format::Other(name),
        }
    }

    /// The format's name in the shared vocabulary, as it serializes.
    pub(crate) fn wire_name(&self) -> &str {
        match self {
            Format::Markdown => "markdown",
            Format::PlainText => "plain_text",
            Format::Blocks => "blocks",
            Format::Pdf => "pdf",
            Format::Docx => "docx",
            Format::Other(name) => name,
        }
    }

    /// The format a path's extension implies.
    ///
    /// The mapping lives here, on the type it produces, because two
    /// callers need it and a second copy is a second answer waiting to
    /// happen: the file source assigns a format to a file on disk, and
    /// the command line assigns one to the name given for stdin. Both
    /// read this function, so `notes.md` cannot mean markdown on one
    /// route and plain text on the other.
    ///
    /// An extension this build does not know, and a path carrying no
    /// extension at all, are [`Format::PlainText`]: the format selects a
    /// reader, and plain text reads any UTF-8 text. No extension implies
    /// [`Format::Blocks`]; a caller names it. When the bytes are read, a
    /// PDF or DOCX signature outranks the extension.
    ///
    /// ```
    /// # use matra::domain::Format;
    /// assert_eq!(Format::from_path("notes.md"), Format::Markdown);
    /// assert_eq!(Format::from_path("notes"), Format::PlainText);
    /// ```
    pub fn from_path(path: impl AsRef<Path>) -> Format {
        match path.as_ref().extension().and_then(|e| e.to_str()) {
            Some("md" | "markdown") => Format::Markdown,
            Some("pdf") => Format::Pdf,
            Some("docx") => Format::Docx,
            _ => Format::PlainText,
        }
    }
}

/// One heading or paragraph a caller supplies, for a document matra has
/// no reader for: text extracted from a PDF, a CMS, a transcript.
///
/// Input only, and opaque: built by [`Block::heading`] and
/// [`Block::paragraph`], handed to `Input::blocks`, and never returned.
/// The analysed text of block input is the blocks' texts joined by a
/// blank line (`"\n\n"`), in order, and every span in the result points
/// into that text. The same blocks as JSON are [`Format::Blocks`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Block(pub(crate) BlockContent);

/// What a [`Block`] holds. Crate-private, so the block vocabulary can
/// grow without a breaking change.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum BlockContent {
    /// A heading opening a section.
    Heading {
        /// Its level, 1 or more.
        level: u8,
        /// Its text.
        text: String,
    },
    /// A paragraph of prose.
    Paragraph {
        /// Its text.
        text: String,
    },
}

impl Block {
    /// A heading at `level` opening a new section: 1 for a top-level
    /// heading, 2 below it, and so on. A level of 0 is refused with
    /// [`Error::InvalidInput`] when the input is analysed, since 0 is the
    /// level of a section with no heading.
    pub fn heading(level: u8, text: impl Into<String>) -> Block {
        Block(BlockContent::Heading {
            level,
            text: text.into(),
        })
    }

    /// A paragraph of prose, parsed into sentences.
    pub fn paragraph(text: impl Into<String>) -> Block {
        Block(BlockContent::Paragraph { text: text.into() })
    }
}

/// A stretch of the analysed text, in UTF-8 bytes: `byte_start`
/// inclusive, `byte_end` exclusive, so
/// `&text[span.byte_start..span.byte_end]` is the stretch.
///
/// For markdown and plain text the analysed text is the input itself;
/// for block input it is the blocks' texts joined by a blank line.
/// Bytes, not characters: a caller in a language that indexes strings by
/// character converts. The unit is in the field names because a byte
/// span used to slice a character-indexed string (a Python `str`) is
/// right on ASCII and silently wrong after the first character outside
/// it.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Span {
    /// The first byte.
    pub byte_start: usize,
    /// One past the last byte.
    pub byte_end: usize,
}

impl Span {
    /// The span from byte `byte_start` (inclusive) to byte `byte_end`
    /// (exclusive).
    pub fn new(byte_start: usize, byte_end: usize) -> Self {
        Self {
            byte_start,
            byte_end,
        }
    }
}

/// What kind of construct a reader saw and did not analyse.
///
/// Each names the construct, never a judgment about it. Serialized in
/// snake case; a reader treats the set as open, since a later reader can
/// add a kind.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum SkippedKind {
    /// A table.
    Table,
    /// A fenced or indented code block.
    Code,
    /// A front matter block (YAML between `---` lines, or TOML between
    /// `+++` lines) at the very start of the document.
    FrontMatter,
    /// An HTML block.
    Html,
    /// A thematic break (`---`, `***`, `___`).
    ThematicBreak,
    /// A link reference definition (`[label]: https://example.com`).
    LinkDefinition,
}

/// One construct the reader saw and did not analyse, with where it lies.
///
/// Skipping is specified behaviour reported beside a correct result, not
/// an error: a table is not prose, so it is not parsed, but the result
/// says it was there.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Skipped {
    /// What the construct is.
    pub kind: SkippedKind,
    /// Where it lies in the analysed text, in UTF-8 bytes, with trailing
    /// whitespace excluded.
    pub span: Span,
}

impl Skipped {
    /// A construct of `kind` at `span`.
    pub fn new(kind: SkippedKind, span: Span) -> Self {
        Self { kind, span }
    }
}

/// A per-document failure: the error plus the path it occurred at, if
/// the document came from disk.
///
/// `path` is `Option` because in-memory text has no path. This closes a
/// real hole: the previous corpus surface fabricated an empty `PathBuf`
/// for a path-less document, which collided with a genuinely empty path.
///
/// Serializes as the error's own form plus the path:
/// `{path, kind, message, details}`, with `path` null for an input that
/// never came from disk, and written lossily where it is not UTF-8.
#[derive(Debug)]
#[non_exhaustive]
pub struct DocumentError {
    /// Source path, if the document came from disk.
    pub path: Option<PathBuf>,
    /// What went wrong.
    pub error: Error,
}

impl DocumentError {
    /// Construct a new `DocumentError`.
    pub fn new(path: Option<PathBuf>, error: Error) -> Self {
        Self { path, error }
    }
}

impl std::fmt::Display for DocumentError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match &self.path {
            Some(p) => write!(f, "{}: {}", p.display(), self.error),
            None => write!(f, "{}", self.error),
        }
    }
}

impl std::error::Error for DocumentError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        Some(&self.error)
    }
}

impl Serialize for DocumentError {
    fn serialize<S: serde::Serializer>(
        &self,
        serializer: S,
    ) -> std::result::Result<S::Ok, S::Error> {
        use serde::ser::SerializeStruct;

        /// The path through the lossy form [`ProvenanceSource`] uses.
        struct LossyPath<'a>(&'a Option<PathBuf>);
        impl Serialize for LossyPath<'_> {
            fn serialize<S: serde::Serializer>(
                &self,
                serializer: S,
            ) -> std::result::Result<S::Ok, S::Error> {
                serialize_path_lossy(self.0, serializer)
            }
        }

        let mut state = serializer.serialize_struct("DocumentError", 4)?;
        state.serialize_field("path", &LossyPath(&self.path))?;
        state.serialize_field("kind", self.error.kind())?;
        state.serialize_field("message", &self.error.to_string())?;
        state.serialize_field("details", &self.error.details())?;
        state.end()
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    /// The wire form RFC-0010 commits to: a newtype serializes as the
    /// bare array, not an object wrapping one.
    #[test]
    fn embedding_serializes_as_bare_array() {
        let e = Embedding(vec![1.0, -0.5, 0.25]);
        let json = serde_json::to_string(&e).expect("serialize");
        assert_eq!(json, "[1.0,-0.5,0.25]");
        let back: Embedding = serde_json::from_str(&json).expect("deserialize");
        assert_eq!(back, e);
    }

    fn make_token(id: usize, text: &str, pos: &str, dep: &str, head: usize) -> Token {
        Token {
            id,
            span: Default::default(),
            text: text.to_string(),
            lemma: text.to_lowercase(),
            pos: pos.to_string(),
            xpos: String::new(),
            features: std::collections::BTreeMap::new(),
            head,
            dep: dep.to_string(),
            deps: String::new(),
            misc: String::new(),
            is_punct: pos == "PUNCT",
        }
    }

    /// [`make_token`] with a feats string, for fixtures where the
    /// morphology is load-bearing (Mood on roots, VerbForm on
    /// auxiliaries). Lemma defaults to the lowercased text, so pass
    /// the lemma as `text` when the two differ.
    fn make_token_feats(
        id: usize,
        text: &str,
        pos: &str,
        dep: &str,
        head: usize,
        feats: &str,
    ) -> Token {
        let mut tok = make_token(id, text, pos, dep, head);
        tok.features = features_from_conllu(feats);
        tok
    }

    fn passive_sentence() -> Sentence {
        Sentence::new(
            "The system was built by the team".to_string(),
            vec![
                make_token(1, "The", "DET", "det", 2),
                make_token(2, "system", "NOUN", "nsubj:pass", 4),
                make_token(3, "was", "AUX", "aux:pass", 4),
                make_token(4, "built", "VERB", "root", 0),
                make_token(5, "by", "ADP", "case", 7),
                make_token(6, "the", "DET", "det", 7),
                make_token(7, "team", "NOUN", "obl", 4),
            ],
        )
    }

    fn active_sentence() -> Sentence {
        Sentence::new(
            "The team shipped the product".to_string(),
            vec![
                make_token(1, "The", "DET", "det", 2),
                make_token(2, "team", "NOUN", "nsubj", 3),
                make_token(3, "shipped", "VERB", "root", 0),
                make_token(4, "the", "DET", "det", 5),
                make_token(5, "product", "NOUN", "obj", 3),
            ],
        )
    }

    #[test]
    fn content_tokens_excludes_punct() {
        let sent = Sentence::new(
            "Hello, world!".to_string(),
            vec![
                make_token(1, "Hello", "INTJ", "root", 0),
                make_token(2, ",", "PUNCT", "punct", 1),
                make_token(3, "world", "NOUN", "vocative", 1),
                make_token(4, "!", "PUNCT", "punct", 1),
            ],
        );
        assert_eq!(sent.content_tokens().len(), 2);
        assert_eq!(sent.word_count, 2);
    }

    #[test]
    fn passive_detects_passive() {
        assert!(passive_sentence().passive);
        assert!(!active_sentence().passive);
    }

    fn token_with_feats(feats: &str) -> Token {
        Token::builder(
            1,
            "x".to_string(),
            "x".to_string(),
            "NOUN".to_string(),
            0,
            "root".to_string(),
        )
        .feats(feats.to_string())
        .build()
    }

    /// Feats strings harvested verbatim from a live parse with the
    /// English UDPipe model (2026-08-21), plus the empty string the
    /// udpipe adapter stores for feature-less tokens.
    const HARVESTED_FEATS: &[&str] = &[
        "",
        "Case=Nom|Gender=Neut|Number=Sing|Person=3|PronType=Prs",
        "Mood=Ind|Number=Sing|Person=3|Tense=Past|VerbForm=Fin",
        "Tense=Past|VerbForm=Part|Voice=Pass",
        "Definite=Def|PronType=Art",
        "Number=Sing",
        "Degree=Pos",
        "Mood=Ind|Tense=Past|VerbForm=Fin",
        "VerbForm=Fin",
        "Case=Nom|Number=Plur|Person=1|PronType=Prs",
        "VerbForm=Inf",
        "Number=Sing|PronType=Dem",
        "Mood=Ind|Number=Sing|Person=3|Tense=Pres|VerbForm=Fin",
        "Number=Plur",
    ];

    #[test]
    fn feat_round_trips_every_harvested_pair() {
        // Property over the harvested corpus: every key=value pair a
        // feats string carries is returned exactly by feat(key), and
        // keys the string does not carry return None.
        for feats in HARVESTED_FEATS {
            let tok = token_with_feats(feats);
            for pair in feats.split('|').filter(|p| !p.is_empty()) {
                let (k, v) = pair.split_once('=').expect("harvested pair has =");
                assert_eq!(tok.feat(k), Some(v), "feats {feats:?} key {k:?}");
            }
            assert_eq!(tok.feat("Absent"), None, "feats {feats:?}");
            assert_eq!(tok.feat(""), None, "feats {feats:?}");
        }
    }

    #[test]
    fn feat_none_on_empty_and_underscore_placeholder() {
        // The udpipe adapter stores "" for feature-less tokens
        // (verified against a live parse); "_" is the CoNLL-U
        // placeholder seen when reading CoNLL-U from disk. Both must
        // yield None for every key.
        for feats in ["", "_"] {
            let tok = token_with_feats(feats);
            for key in ["Mood", "VerbForm", "_", ""] {
                assert_eq!(tok.feat(key), None, "feats {feats:?} key {key:?}");
            }
        }
    }

    #[test]
    fn feat_single_feature() {
        let tok = token_with_feats("Number=Sing");
        assert_eq!(tok.feat("Number"), Some("Sing"));
        assert_eq!(tok.feat("number"), None, "keys match exactly");
        assert_eq!(tok.feat("Num"), None, "no prefix match");
    }

    #[test]
    fn feat_multi_value_returned_unsplit() {
        let tok = token_with_feats("Case=Nom,Acc|Number=Sing");
        assert_eq!(tok.feat("Case"), Some("Nom,Acc"));
    }

    #[test]
    fn feat_malformed_input_does_not_panic() {
        // Segments without '=' are never matched; a second '=' stays
        // in the value; duplicate keys resolve to the first match.
        let tok = token_with_feats("junk|Mood=Ind=extra|Mood=Sub|=orphan|Tense=");
        assert_eq!(tok.feat("junk"), None);
        assert_eq!(tok.feat("Mood"), Some("Ind=extra"));
        assert_eq!(tok.feat("Tense"), Some(""));
        assert_eq!(tok.feat("orphan"), None);
    }

    // Negation fixtures mirror live UDPipe parses (verified 2026-08-21):
    // `not` is PART/advmod, `never` is ADV/advmod, `no` and `neither`
    // are DET/det, `nor` is CCONJ/cc.

    #[test]
    fn negation_detects_not_as_advmod() {
        // "The plan is not ready."
        let sent = Sentence::new(
            "The plan is not ready.".to_string(),
            vec![
                make_token(1, "The", "DET", "det", 2),
                make_token(2, "plan", "NOUN", "nsubj", 5),
                make_token(3, "is", "AUX", "cop", 5),
                make_token(4, "not", "PART", "advmod", 5),
                make_token(5, "ready", "ADJ", "root", 0),
                make_token(6, ".", "PUNCT", "punct", 5),
            ],
        );
        assert_eq!(sent.negations.len(), 1);
        assert_eq!(sent.negations[0].cue_id, 4);
        assert_eq!(sent.negations[0].cue_lemma, "not");
        assert_eq!(sent.negations[0].head_id, 5);
    }

    #[test]
    fn negation_detects_never_as_advmod() {
        // "It was never reviewed."
        let sent = Sentence::new(
            "It was never reviewed.".to_string(),
            vec![
                make_token(1, "It", "PRON", "nsubj:pass", 4),
                make_token(2, "was", "AUX", "aux:pass", 4),
                make_token(3, "never", "ADV", "advmod", 4),
                make_token(4, "reviewed", "VERB", "root", 0),
                make_token(5, ".", "PUNCT", "punct", 4),
            ],
        );
        assert_eq!(sent.negations.len(), 1);
        assert_eq!(sent.negations[0].cue_lemma, "never");
        assert_eq!(sent.negations[0].cue_id, 3);
        assert_eq!(sent.negations[0].head_id, 4);
    }

    #[test]
    fn negation_detects_no_as_det() {
        // "No changes were made."
        let sent = Sentence::new(
            "No changes were made.".to_string(),
            vec![
                make_token(1, "No", "DET", "det", 2),
                make_token(2, "changes", "NOUN", "nsubj:pass", 4),
                make_token(3, "were", "AUX", "aux:pass", 4),
                make_token(4, "made", "VERB", "root", 0),
                make_token(5, ".", "PUNCT", "punct", 4),
            ],
        );
        assert_eq!(sent.negations.len(), 1);
        assert_eq!(sent.negations[0].cue_lemma, "no");
        assert_eq!(sent.negations[0].head_id, 2);
    }

    #[test]
    fn negation_detects_neither_and_nor() {
        // "Neither option worked, nor did the fallback."
        let sent = Sentence::new(
            "Neither option worked, nor did the fallback.".to_string(),
            vec![
                make_token(1, "Neither", "DET", "det", 2),
                make_token(2, "option", "NOUN", "nsubj", 3),
                make_token(3, "worked", "VERB", "root", 0),
                make_token(4, ",", "PUNCT", "punct", 6),
                make_token(5, "nor", "CCONJ", "cc", 6),
                make_token(6, "did", "VERB", "conj", 3),
                make_token(7, "the", "DET", "det", 8),
                make_token(8, "fallback", "NOUN", "obj", 6),
                make_token(9, ".", "PUNCT", "punct", 3),
            ],
        );
        let lemmas: Vec<&str> = sent
            .negations
            .iter()
            .map(|n| n.cue_lemma.as_str())
            .collect();
        assert_eq!(lemmas, vec!["neither", "nor"]);
        assert_eq!(sent.negations[0].head_id, 2);
        assert_eq!(sent.negations[1].head_id, 6);
    }

    #[test]
    fn negation_no_false_positive_on_nothing_as_subject() {
        // "Nothing happened." UDPipe lemmatizes `Nothing` to `nothing`
        // (PRON, nsubj), which is not a cue: the indefinite carries the
        // negation lexically, not as a dependency-graph cue token.
        let sent = Sentence::new(
            "Nothing happened.".to_string(),
            vec![
                make_token(1, "Nothing", "PRON", "nsubj", 2),
                make_token(2, "happened", "VERB", "root", 0),
                make_token(3, ".", "PUNCT", "punct", 2),
            ],
        );
        assert!(sent.negations.is_empty());
    }

    #[test]
    fn negation_no_fire_on_pronominal_neither() {
        // "Neither of them came." Pronominal `neither` heads the
        // subject itself (DET, `nsubj` on the live model) instead of
        // attaching as `det` to a noun it negates. Out of scope by the
        // same decision that excludes `nothing` as a subject: a cue
        // fires only on its carrying relation, and `nsubj` is not one.
        // This test records the recall boundary; widening it means
        // changing detect_negations deliberately, not by drift.
        let sent = Sentence::new(
            "Neither of them came.".to_string(),
            vec![
                make_token(1, "Neither", "DET", "nsubj", 4),
                make_token(2, "of", "ADP", "case", 3),
                make_token(3, "them", "PRON", "nmod", 1),
                make_token(4, "came", "VERB", "root", 0),
                make_token(5, ".", "PUNCT", "punct", 4),
            ],
        );
        assert!(sent.negations.is_empty());
    }

    #[test]
    fn negation_on_tokenizer_split_cannot_fires_once_on_not() {
        // "We cannot merge this." The tokenizer splits `cannot` into
        // `can` + `not` (verified against a live parse); exactly one
        // cue must fire, on the `not` token, not on `can` and not twice.
        let sent = Sentence::new(
            "We cannot merge this.".to_string(),
            vec![
                make_token(1, "We", "PRON", "nsubj", 4),
                make_token(2, "can", "AUX", "aux", 4),
                make_token(3, "not", "PART", "advmod", 4),
                make_token(4, "merge", "VERB", "root", 0),
                make_token(5, "this", "PRON", "obj", 4),
                make_token(6, ".", "PUNCT", "punct", 4),
            ],
        );
        assert_eq!(sent.negations.len(), 1);
        assert_eq!(sent.negations[0].cue_id, 3);
        assert_eq!(sent.negations[0].cue_lemma, "not");
        assert_eq!(sent.negations[0].head_id, 4);
    }

    #[test]
    fn negation_empty_on_unnegated_sentences() {
        assert!(active_sentence().negations.is_empty());
        assert!(passive_sentence().negations.is_empty());
    }

    #[test]
    fn negations_cross_the_wire() {
        // The field crosses the wire as data (RFC-0008).
        let sent = Sentence::new(
            "It was never reviewed.".to_string(),
            vec![make_token(1, "never", "ADV", "advmod", 0)],
        );
        let json = serde_json::to_value(&sent).unwrap();
        assert_eq!(json["negations"][0]["cue_lemma"], "never");
    }

    // Modal fixtures mirror live UDPipe parses (verified 2026-08-21):
    // every member of the closed class is AUX/`aux` with xpos `MD` and
    // `VerbForm=Fin`; the verb it scopes is `VerbForm=Inf` (or a
    // participle in a chain) and carries no `Mood`. Indicative roots
    // carry `Mood=Ind`; imperative roots carry `Mood=Imp`.

    /// A minimal modalized clause: `aux` with the given lemma
    /// attached to an infinitive root.
    fn modal_clause(lemma: &str) -> Sentence {
        Sentence::new(
            format!("They {lemma} leave."),
            vec![
                make_token(1, "They", "PRON", "nsubj", 3),
                make_token_feats(2, lemma, "AUX", "aux", 3, "VerbForm=Fin"),
                make_token_feats(3, "leave", "VERB", "root", 0, "VerbForm=Inf"),
                make_token(4, ".", "PUNCT", "punct", 3),
            ],
        )
    }

    #[test]
    fn modal_every_closed_class_lemma_is_found() {
        for lemma in [
            "can", "could", "may", "might", "must", "ought", "shall", "should", "will", "would",
        ] {
            let sent = modal_clause(lemma);
            assert_eq!(sent.modals.len(), 1, "lemma {lemma:?}");
            assert_eq!(sent.modals[0].aux_id, 2, "lemma {lemma:?}");
            assert_eq!(sent.modals[0].aux_lemma, lemma);
            assert_eq!(sent.modals[0].head_id, 3, "lemma {lemma:?}");
            assert_eq!(
                sent.root_mood, None,
                "the modal carries no Mood, lemma {lemma:?}"
            );
        }
    }

    #[test]
    fn modal_chain_reports_its_modal_with_the_content_verb_head() {
        // "It might have been done." The potential surface form as a
        // multi-auxiliary chain: `might`, `have`, `been` all attach to
        // `done`. Only `might` is in the closed class; the chain
        // reports exactly its modals, and the head is the content
        // verb, not the next auxiliary.
        let sent = Sentence::new(
            "It might have been done.".to_string(),
            vec![
                make_token(1, "It", "PRON", "nsubj:pass", 5),
                make_token_feats(2, "might", "AUX", "aux", 5, "VerbForm=Fin"),
                make_token_feats(3, "have", "AUX", "aux", 5, "VerbForm=Inf"),
                make_token_feats(4, "been", "AUX", "aux:pass", 5, "Tense=Past|VerbForm=Part"),
                make_token_feats(
                    5,
                    "done",
                    "VERB",
                    "root",
                    0,
                    "Tense=Past|VerbForm=Part|Voice=Pass",
                ),
                make_token(6, ".", "PUNCT", "punct", 5),
            ],
        );
        assert_eq!(sent.modals.len(), 1);
        assert_eq!(sent.modals[0].aux_id, 2);
        assert_eq!(sent.modals[0].aux_lemma, "might");
        assert_eq!(sent.modals[0].head_id, 5);
        assert_eq!(sent.root_mood, None, "participle root carries no Mood");
    }

    #[test]
    fn modal_coordinated_clauses_report_both() {
        // "He can stay, and she may leave." Two clauses, two modals,
        // each attached to its own verb.
        let sent = Sentence::new(
            "He can stay, and she may leave.".to_string(),
            vec![
                make_token(1, "He", "PRON", "nsubj", 3),
                make_token_feats(2, "can", "AUX", "aux", 3, "VerbForm=Fin"),
                make_token_feats(3, "stay", "VERB", "root", 0, "VerbForm=Inf"),
                make_token(4, ",", "PUNCT", "punct", 8),
                make_token(5, "and", "CCONJ", "cc", 8),
                make_token(6, "she", "PRON", "nsubj", 8),
                make_token_feats(7, "may", "AUX", "aux", 8, "VerbForm=Fin"),
                make_token_feats(8, "leave", "VERB", "conj", 3, "VerbForm=Inf"),
                make_token(9, ".", "PUNCT", "punct", 3),
            ],
        );
        let found: Vec<(&str, usize)> = sent
            .modals
            .iter()
            .map(|m| (m.aux_lemma.as_str(), m.head_id))
            .collect();
        assert_eq!(found, vec![("can", 3), ("may", 8)]);
    }

    #[test]
    fn modal_promoted_to_root_under_vp_ellipsis_is_found() {
        // "Yes, you should." VP ellipsis promotes the modal to root
        // (verified live), so the `aux` relation is absent and the
        // `AUX` part of speech carries the match. head_id is 0, the
        // documented root convention. A clause whose root is a modal
        // is modalized, not a bare assertion.
        let sent = Sentence::new(
            "Yes, you should.".to_string(),
            vec![
                make_token(1, "Yes", "INTJ", "discourse", 4),
                make_token(2, ",", "PUNCT", "punct", 4),
                make_token(3, "you", "PRON", "nsubj", 4),
                make_token_feats(4, "should", "AUX", "root", 0, "VerbForm=Fin"),
                make_token(5, ".", "PUNCT", "punct", 4),
            ],
        );
        assert_eq!(sent.modals.len(), 1);
        assert_eq!(sent.modals[0].aux_lemma, "should");
        assert_eq!(sent.modals[0].head_id, 0);
        assert_eq!(sent.root_mood, None, "a root modal carries no Mood");
    }

    #[test]
    fn modal_coordinated_auxiliaries_report_both() {
        // "He can and will succeed." The live model parses the first
        // coordinated auxiliary as root and the second as `aux` on
        // the shared verb, so the first is found by its `AUX` part
        // of speech and the second by its relation.
        let sent = Sentence::new(
            "He can and will succeed.".to_string(),
            vec![
                make_token(1, "He", "PRON", "nsubj", 2),
                make_token_feats(2, "can", "AUX", "root", 0, "VerbForm=Fin"),
                make_token(3, "and", "CCONJ", "cc", 5),
                make_token_feats(4, "will", "AUX", "aux", 5, "VerbForm=Fin"),
                make_token_feats(5, "succeed", "VERB", "conj", 2, "VerbForm=Inf"),
                make_token(6, ".", "PUNCT", "punct", 2),
            ],
        );
        let found: Vec<(&str, usize)> = sent
            .modals
            .iter()
            .map(|m| (m.aux_lemma.as_str(), m.head_id))
            .collect();
        assert_eq!(found, vec![("can", 0), ("will", 5)]);
        assert_eq!(sent.root_mood, None);
    }

    #[test]
    fn modal_second_coordinated_auxiliary_as_conj_is_found() {
        // "He can, and he will, succeed." With full clausal
        // coordination the live model parses the first modal as root
        // and the second as `conj`, so both are found by the `AUX`
        // part of speech and neither carries `aux`.
        let sent = Sentence::new(
            "He can, and he will, succeed.".to_string(),
            vec![
                make_token(1, "He", "PRON", "nsubj", 2),
                make_token_feats(2, "can", "AUX", "root", 0, "VerbForm=Fin"),
                make_token(3, ",", "PUNCT", "punct", 6),
                make_token(4, "and", "CCONJ", "cc", 6),
                make_token(5, "he", "PRON", "nsubj", 6),
                make_token_feats(6, "will", "AUX", "conj", 2, "VerbForm=Fin"),
                make_token(7, ",", "PUNCT", "punct", 8),
                make_token_feats(8, "succeed", "VERB", "conj", 2, "Mood=Ind|VerbForm=Fin"),
                make_token(9, ".", "PUNCT", "punct", 2),
            ],
        );
        let found: Vec<(&str, usize)> = sent
            .modals
            .iter()
            .map(|m| (m.aux_lemma.as_str(), m.head_id))
            .collect();
        assert_eq!(found, vec![("can", 0), ("will", 2)]);
        assert_eq!(sent.root_mood, None, "a root modal carries no Mood");
    }

    #[test]
    fn root_mood_of_a_copular_clause_rides_the_cop() {
        // "The sky is blue." The live model roots the predicate ADJ
        // and parks Mood=Ind on the `cop` child, so the detector reads
        // the mood off the copula, and reports the root's id.
        let sent = Sentence::new(
            "The sky is blue.".to_string(),
            vec![
                make_token(1, "The", "DET", "det", 2),
                make_token(2, "sky", "NOUN", "nsubj", 4),
                make_token_feats(
                    3,
                    "is",
                    "AUX",
                    "cop",
                    4,
                    "Mood=Ind|Number=Sing|Person=3|Tense=Pres|VerbForm=Fin",
                ),
                make_token_feats(4, "blue", "ADJ", "root", 0, "Degree=Pos"),
                make_token(5, ".", "PUNCT", "punct", 4),
            ],
        );
        assert!(sent.modals.is_empty(), "the copula be is outside the class");
        assert_eq!(mood_of(&sent), Some((4, "Ind")));
    }

    #[test]
    fn root_mood_of_do_support_rides_the_aux() {
        // "He did leave." Do-support roots the infinitive and parks
        // Mood=Ind on the `aux` child, whose lemma is outside the
        // modal class.
        let sent = Sentence::new(
            "He did leave.".to_string(),
            vec![
                make_token(1, "He", "PRON", "nsubj", 3),
                make_token_feats(2, "do", "AUX", "aux", 3, "Mood=Ind|Tense=Past|VerbForm=Fin"),
                make_token_feats(3, "leave", "VERB", "root", 0, "VerbForm=Inf"),
                make_token(4, ".", "PUNCT", "punct", 3),
            ],
        );
        assert!(sent.modals.is_empty());
        assert_eq!(mood_of(&sent), Some((3, "Ind")));
    }

    #[test]
    fn modal_contraction_fires_on_its_lemma() {
        // "They won't stop." The tokenizer splits the contraction and
        // the model lemmatizes `wo` to `will` (verified live), so the
        // lemma match fires; the `not` is the negation primitive's
        // finding, not this one's.
        let sent = Sentence::new(
            "They won't stop.".to_string(),
            vec![
                make_token(1, "They", "PRON", "nsubj", 4),
                make_token_feats(2, "will", "AUX", "aux", 4, "VerbForm=Fin"),
                make_token(3, "not", "PART", "advmod", 4),
                make_token_feats(4, "stop", "VERB", "root", 0, "VerbForm=Inf"),
                make_token(5, ".", "PUNCT", "punct", 4),
            ],
        );
        assert_eq!(sent.modals.len(), 1);
        assert_eq!(sent.modals[0].aux_lemma, "will");
        assert_eq!(sent.negations.len(), 1, "the split not still fires");
    }

    #[test]
    fn modal_no_false_positive_on_lexical_will() {
        // "The will was signed yesterday." The noun `will` shares the
        // lemma but never the `aux` relation.
        let sent = Sentence::new(
            "The will was signed yesterday.".to_string(),
            vec![
                make_token(1, "The", "DET", "det", 2),
                make_token(2, "will", "NOUN", "nsubj:pass", 4),
                make_token_feats(
                    3,
                    "was",
                    "AUX",
                    "aux:pass",
                    4,
                    "Mood=Ind|Tense=Past|VerbForm=Fin",
                ),
                make_token_feats(
                    4,
                    "signed",
                    "VERB",
                    "root",
                    0,
                    "Tense=Past|VerbForm=Part|Voice=Pass",
                ),
                make_token(5, "yesterday", "NOUN", "obl:tmod", 4),
                make_token(6, ".", "PUNCT", "punct", 4),
            ],
        );
        assert!(sent.modals.is_empty());
        // In the indicative passive, Mood=Ind rides the `aux:pass`
        // child of the participle root.
        assert_eq!(mood_of(&sent), Some((4, "Ind")));
    }

    #[test]
    fn a_modalized_directive_has_no_root_mood() {
        // "You must complete the form by Friday." The directive
        // surface form with a modal: `must` is reported, and neither
        // the infinitive root nor the modal carries a Mood. Whether
        // `must` is obligation or inference here is the consumer's
        // reading.
        let sent = Sentence::new(
            "You must complete the form by Friday.".to_string(),
            vec![
                make_token(1, "You", "PRON", "nsubj", 3),
                make_token_feats(2, "must", "AUX", "aux", 3, "VerbForm=Fin"),
                make_token_feats(3, "complete", "VERB", "root", 0, "VerbForm=Inf"),
                make_token(4, "the", "DET", "det", 5),
                make_token(5, "form", "NOUN", "obj", 3),
                make_token(6, "by", "ADP", "case", 7),
                make_token(7, "Friday", "PROPN", "obl", 3),
                make_token(8, ".", "PUNCT", "punct", 3),
            ],
        );
        assert_eq!(sent.modals.len(), 1);
        assert_eq!(sent.modals[0].aux_lemma, "must");
        assert_eq!(sent.root_mood, None);
    }

    #[test]
    fn root_mood_reads_the_indicative_root() {
        // "The committee approved it." The root carries Mood=Ind and no
        // modal attaches.
        let sent = Sentence::new(
            "The committee approved it.".to_string(),
            vec![
                make_token(1, "The", "DET", "det", 2),
                make_token(2, "committee", "NOUN", "nsubj", 3),
                make_token_feats(
                    3,
                    "approved",
                    "VERB",
                    "root",
                    0,
                    "Mood=Ind|Tense=Past|VerbForm=Fin",
                ),
                make_token(4, "it", "PRON", "obj", 3),
                make_token(5, ".", "PUNCT", "punct", 3),
            ],
        );
        assert!(sent.modals.is_empty());
        assert_eq!(mood_of(&sent), Some((3, "Ind")));
    }

    #[test]
    fn root_mood_reads_the_imperative_root() {
        // "Complete the form by Friday." The bare directive surface
        // form: Mood=Imp on the root, with no modal involved.
        let sent = Sentence::new(
            "Complete the form by Friday.".to_string(),
            vec![
                make_token_feats(1, "Complete", "VERB", "root", 0, "Mood=Imp|VerbForm=Fin"),
                make_token(2, "the", "DET", "det", 3),
                make_token(3, "form", "NOUN", "obj", 1),
                make_token(4, "by", "ADP", "case", 5),
                make_token(5, "Friday", "PROPN", "obl", 1),
                make_token(6, ".", "PUNCT", "punct", 1),
            ],
        );
        assert!(sent.modals.is_empty());
        assert_eq!(mood_of(&sent), Some((1, "Imp")));
    }

    #[test]
    fn subordinate_modal_is_reported_and_leaves_the_root_mood() {
        // "I know it might rain." The detector reads the root clause:
        // `know` carries Mood=Ind and `might` attaches to the
        // subordinate `rain`, so the root mood is the matrix's while
        // the modal is still reported with its own head. The consumer
        // sees both facts and does its own reading.
        let sent = Sentence::new(
            "I know it might rain.".to_string(),
            vec![
                make_token(1, "I", "PRON", "nsubj", 2),
                make_token_feats(
                    2,
                    "know",
                    "VERB",
                    "root",
                    0,
                    "Mood=Ind|Tense=Pres|VerbForm=Fin",
                ),
                make_token(3, "it", "PRON", "nsubj", 5),
                make_token_feats(4, "might", "AUX", "aux", 5, "VerbForm=Fin"),
                make_token(5, "rain", "NOUN", "ccomp", 2),
                make_token(6, ".", "PUNCT", "punct", 5),
            ],
        );
        assert_eq!(sent.modals.len(), 1);
        assert_eq!(sent.modals[0].aux_lemma, "might");
        assert_eq!(sent.modals[0].head_id, 5);
        assert_eq!(mood_of(&sent), Some((2, "Ind")));
    }

    /// The root mood as `(root_id, mood)`, for terse assertions.
    fn mood_of(sent: &Sentence) -> Option<(usize, &str)> {
        sent.root_mood
            .as_ref()
            .map(|m| (m.root_id, m.mood.as_str()))
    }

    #[test]
    fn modals_and_root_mood_cross_the_wire() {
        // The fields cross the wire as data (RFC-0008): the modal with
        // its lemma, and an absent root mood as null rather than a
        // missing key.
        let sent = modal_clause("might");
        let json = serde_json::to_value(&sent).unwrap();
        assert_eq!(json["modals"][0]["aux_lemma"], "might");
        assert!(json["root_mood"].is_null());
        assert!(json.as_object().unwrap().contains_key("root_mood"));
        let indicative = Sentence::new(
            "It rains.".to_string(),
            vec![
                make_token(1, "It", "PRON", "expl", 2),
                make_token_feats(2, "rains", "VERB", "root", 0, "Mood=Ind|VerbForm=Fin"),
                make_token(3, ".", "PUNCT", "punct", 2),
            ],
        );
        let json = serde_json::to_value(&indicative).unwrap();
        assert_eq!(
            json["root_mood"],
            serde_json::json!({"root_id": 2, "mood": "Ind"})
        );
    }

    #[test]
    fn features_cross_the_wire_as_a_map() {
        let tok = token_with_feats("Mood=Ind|Tense=Pres");
        let json = serde_json::to_value(&tok).unwrap();
        assert_eq!(
            json["features"],
            serde_json::json!({"Mood": "Ind", "Tense": "Pres"})
        );
        assert!(json.get("feats").is_none(), "no raw string beside the map");
        let empty = serde_json::to_value(token_with_feats("_")).unwrap();
        assert_eq!(empty["features"], serde_json::json!({}));
    }

    // Reporting and root-adverbial fixtures mirror live UDPipe parses
    // (verified 2026-08-21): the complement head attaches as `ccomp`
    // to the reporting verb, the subject as `nsubj` (or `nsubj:pass`)
    // to the same verb, and a sentence-scope adverb as `advmod` to
    // the root.

    #[test]
    fn clausal_complement_self_attribution() {
        // "We show that the method works."
        let sent = Sentence::new(
            "We show that the method works.".to_string(),
            vec![
                make_token(1, "We", "PRON", "nsubj", 2),
                make_token(2, "show", "VERB", "root", 0),
                make_token(3, "that", "SCONJ", "mark", 6),
                make_token(4, "the", "DET", "det", 5),
                make_token(5, "method", "NOUN", "nsubj", 6),
                make_token(6, "works", "VERB", "ccomp", 2),
                make_token(7, ".", "PUNCT", "punct", 2),
            ],
        );
        assert_eq!(sent.clausal_complements.len(), 1);
        let r = &sent.clausal_complements[0];
        assert_eq!(r.verb_id, 2);
        assert_eq!(r.verb_lemma, "show");
        assert_eq!(r.ccomp_id, 6);
        // The complement's own subject ("method", nsubj of token 6)
        // is not the matrix subject; only the verb's child qualifies.
        assert_eq!(r.subject_id, Some(1));
        assert_eq!(r.subject_lemma.as_deref(), Some("we"));
    }

    #[test]
    fn clausal_complement_other_attribution() {
        // "Smith reported that the effect vanished."
        let sent = Sentence::new(
            "Smith reported that the effect vanished.".to_string(),
            vec![
                make_token(1, "Smith", "PROPN", "nsubj", 2),
                make_token(2, "reported", "VERB", "root", 0),
                make_token(3, "that", "SCONJ", "mark", 6),
                make_token(4, "the", "DET", "det", 5),
                make_token(5, "effect", "NOUN", "nsubj", 6),
                make_token(6, "vanished", "VERB", "ccomp", 2),
                make_token(7, ".", "PUNCT", "punct", 2),
            ],
        );
        assert_eq!(sent.clausal_complements.len(), 1);
        let r = &sent.clausal_complements[0];
        assert_eq!((r.verb_id, r.ccomp_id), (2, 6));
        assert_eq!(r.verb_lemma, "reported");
        assert_eq!(r.subject_id, Some(1));
    }

    #[test]
    fn clausal_complement_impersonal() {
        // "These results suggest that the mechanism is shared."
        // The complement is passive; its `nsubj:pass` belongs to the
        // complement head, not to the reporting verb.
        let sent = Sentence::new(
            "These results suggest that the mechanism is shared.".to_string(),
            vec![
                make_token(1, "These", "DET", "det", 2),
                make_token(2, "results", "NOUN", "nsubj", 3),
                make_token(3, "suggest", "VERB", "root", 0),
                make_token(4, "that", "SCONJ", "mark", 8),
                make_token(5, "the", "DET", "det", 6),
                make_token(6, "mechanism", "NOUN", "nsubj:pass", 8),
                make_token(7, "is", "AUX", "aux:pass", 8),
                make_token(8, "shared", "VERB", "ccomp", 3),
                make_token(9, ".", "PUNCT", "punct", 3),
            ],
        );
        assert_eq!(sent.clausal_complements.len(), 1);
        let r = &sent.clausal_complements[0];
        assert_eq!((r.verb_id, r.ccomp_id), (3, 8));
        assert_eq!(r.subject_id, Some(2));
        assert_eq!(r.subject_lemma.as_deref(), Some("results"));
    }

    #[test]
    fn clausal_complement_survives_upstream_split_with_no_subject() {
        // Known upstream defect, recorded not fixed: UDPipe splits
        // "Smith et al. reported that the effect vanished." into two
        // sentences at the period in "et al." (verified live,
        // 2026-08-21). The reporting verb keeps its `ccomp` but its
        // attribution is stranded in the previous sentence, so the
        // construction reports with no subject. This test pins the
        // detector's behaviour on that second fragment.
        let sent = Sentence::new(
            "reported that the effect vanished.".to_string(),
            vec![
                make_token(1, "reported", "VERB", "root", 0),
                make_token(2, "that", "SCONJ", "mark", 5),
                make_token(3, "the", "DET", "det", 4),
                make_token(4, "effect", "NOUN", "nsubj", 5),
                make_token(5, "vanished", "VERB", "ccomp", 1),
                make_token(6, ".", "PUNCT", "punct", 1),
            ],
        );
        assert_eq!(sent.clausal_complements.len(), 1);
        let r = &sent.clausal_complements[0];
        assert_eq!((r.verb_id, r.ccomp_id), (1, 5));
        assert_eq!(r.subject_id, None);
        assert_eq!(r.subject_lemma, None);
    }

    #[test]
    fn clausal_complement_no_fire_without_ccomp() {
        // "Smith reported a similar finding." takes a plain object,
        // not a clausal complement; the construction is absent.
        let sent = Sentence::new(
            "Smith reported a similar finding.".to_string(),
            vec![
                make_token(1, "Smith", "PROPN", "nsubj", 2),
                make_token(2, "reported", "VERB", "root", 0),
                make_token(3, "a", "DET", "det", 5),
                make_token(4, "similar", "ADJ", "amod", 5),
                make_token(5, "finding", "NOUN", "obj", 2),
                make_token(6, ".", "PUNCT", "punct", 2),
            ],
        );
        assert!(sent.clausal_complements.is_empty());
        assert!(active_sentence().clausal_complements.is_empty());
    }

    #[test]
    fn clausal_complement_no_fire_on_non_verb_head() {
        // "I am sure that it works." parses the `ccomp` under the
        // adjective `sure`. The plan defines the reporting
        // construction as verbal, so a non-VERB head is outside it
        // by decision, not omission.
        let sent = Sentence::new(
            "I am sure that it works.".to_string(),
            vec![
                make_token(1, "I", "PRON", "nsubj", 3),
                make_token(2, "am", "AUX", "cop", 3),
                make_token(3, "sure", "ADJ", "root", 0),
                make_token(4, "that", "SCONJ", "mark", 6),
                make_token(5, "it", "PRON", "nsubj", 6),
                make_token(6, "works", "VERB", "ccomp", 3),
                make_token(7, ".", "PUNCT", "punct", 3),
            ],
        );
        assert!(sent.clausal_complements.is_empty());
    }

    #[test]
    fn root_adverbial_detects_sentence_scope_advmod() {
        // "Reportedly, the deal closed."
        let sent = Sentence::new(
            "Reportedly, the deal closed.".to_string(),
            vec![
                make_token(1, "Reportedly", "ADV", "advmod", 5),
                make_token(2, ",", "PUNCT", "punct", 5),
                make_token(3, "the", "DET", "det", 4),
                make_token(4, "deal", "NOUN", "nsubj", 5),
                make_token(5, "closed", "VERB", "root", 0),
                make_token(6, ".", "PUNCT", "punct", 5),
            ],
        );
        assert_eq!(sent.root_adverbials.len(), 1);
        assert_eq!(sent.root_adverbials[0].adv_id, 1);
        assert_eq!(sent.root_adverbials[0].adv_lemma, "reportedly");
    }

    #[test]
    fn root_adverbial_reports_manner_adverbs_too() {
        // "The team quickly shipped." puts `quickly` on the same
        // root-attached `advmod` arc as `reportedly`. The dependency
        // graph does not distinguish sentence scope from manner, so
        // the detector reports the arc and the caller's lexicon
        // decides the evidential reading (EP-0007 M4: construction, not
        // category).
        let sent = Sentence::new(
            "The team quickly shipped.".to_string(),
            vec![
                make_token(1, "The", "DET", "det", 2),
                make_token(2, "team", "NOUN", "nsubj", 4),
                make_token(3, "quickly", "ADV", "advmod", 4),
                make_token(4, "shipped", "VERB", "root", 0),
                make_token(5, ".", "PUNCT", "punct", 4),
            ],
        );
        assert_eq!(sent.root_adverbials.len(), 1);
        assert_eq!(sent.root_adverbials[0].adv_lemma, "quickly");
        // The caller's lexicon is where the evidential reading lives.
        assert!(
            sent.root_adverbials_in(&["reportedly", "allegedly"])
                .is_empty()
        );
    }

    #[test]
    fn root_adverbial_ignores_non_root_attachment() {
        // "She says he ran quickly." attaches `quickly` to `ran`
        // (the complement head), not the root, so no root adverbial;
        // the reporting construction on `says` still fires.
        let sent = Sentence::new(
            "She says he ran quickly.".to_string(),
            vec![
                make_token(1, "She", "PRON", "nsubj", 2),
                make_token(2, "says", "VERB", "root", 0),
                make_token(3, "he", "PRON", "nsubj", 4),
                make_token(4, "ran", "VERB", "ccomp", 2),
                make_token(5, "quickly", "ADV", "advmod", 4),
                make_token(6, ".", "PUNCT", "punct", 2),
            ],
        );
        assert!(sent.root_adverbials.is_empty());
        assert_eq!(sent.clausal_complements.len(), 1);
    }

    #[test]
    fn root_adverbial_overlaps_negation_by_design() {
        // "The plan is not ready." carries `not` as `advmod` on the
        // root, so it appears in both `negations` and
        // `root_adverbials`. Both report arcs; neither assigns a
        // reading; the overlap is intentional.
        let sent = Sentence::new(
            "The plan is not ready.".to_string(),
            vec![
                make_token(1, "The", "DET", "det", 2),
                make_token(2, "plan", "NOUN", "nsubj", 5),
                make_token(3, "is", "AUX", "cop", 5),
                make_token(4, "not", "PART", "advmod", 5),
                make_token(5, "ready", "ADJ", "root", 0),
                make_token(6, ".", "PUNCT", "punct", 5),
            ],
        );
        assert_eq!(sent.negations.len(), 1);
        assert_eq!(sent.root_adverbials.len(), 1);
        assert_eq!(sent.root_adverbials[0].adv_id, 4);
    }

    #[test]
    fn clausal_complements_in_filters_by_caller_lexicon() {
        // matra ships no evidential lexicon (open class); the caller
        // supplies one and the filter is a view over crossing data.
        let sent = Sentence::new(
            "Smith reported that the effect vanished.".to_string(),
            vec![
                make_token(1, "Smith", "PROPN", "nsubj", 2),
                make_token(2, "reported", "VERB", "root", 0),
                make_token(3, "that", "SCONJ", "mark", 6),
                make_token(4, "the", "DET", "det", 5),
                make_token(5, "effect", "NOUN", "nsubj", 6),
                make_token(6, "vanished", "VERB", "ccomp", 2),
                make_token(7, ".", "PUNCT", "punct", 2),
            ],
        );
        // make_token lemmatizes to the lowercased text, so the
        // caller's lexicon here uses the surface-derived lemma.
        assert_eq!(
            sent.clausal_complements_in(&["reported", "suggest"]).len(),
            1
        );
        assert!(sent.clausal_complements_in(&["say", "claim"]).is_empty());
    }

    #[test]
    fn clausal_complements_and_root_adverbials_cross_the_wire() {
        // The fields cross the wire as data (RFC-0008).
        let sent = Sentence::new(
            "Reportedly, it works.".to_string(),
            vec![
                make_token(1, "Reportedly", "ADV", "advmod", 3),
                make_token(2, "it", "PRON", "nsubj", 3),
                make_token(3, "works", "VERB", "root", 0),
                make_token(4, ".", "PUNCT", "punct", 3),
            ],
        );
        let json = serde_json::to_value(&sent).unwrap();
        assert_eq!(json["root_adverbials"][0]["adv_lemma"], "reportedly");
        assert!(json["clausal_complements"].as_array().unwrap().is_empty());
    }

    /// The derived counts cross the wire beside the tokens, so a
    /// binding reads them rather than recounting.
    #[test]
    fn sentence_counts_cross_the_wire() {
        let json = serde_json::to_value(passive_sentence()).unwrap();
        assert_eq!(json["word_count"], 7);
        assert_eq!(json["passive"], true);
        assert_eq!(json["tree_depth"], 2);
    }

    #[test]
    fn tree_depth_computes_max() {
        let sent = active_sentence();
        assert!(sent.tree_depth.is_some_and(|d| d > 0));
    }

    #[test]
    fn root_token_finds_root() {
        let sent = passive_sentence();
        let root = sent.root_token().unwrap();
        assert_eq!(root.text, "built");
    }

    #[test]
    fn children_of_returns_direct_children() {
        let sent = passive_sentence();
        let children = sent.children_of(4);
        let texts: Vec<&str> = children.iter().map(|t| t.text.as_str()).collect();
        assert!(texts.contains(&"system"));
        assert!(texts.contains(&"was"));
        assert!(texts.contains(&"team"));
    }

    #[test]
    fn head_of_returns_head() {
        let sent = passive_sentence();
        let head = sent.head_of(2).unwrap();
        assert_eq!(head.text, "built");
    }

    #[test]
    fn head_of_root_returns_none() {
        let sent = passive_sentence();
        assert!(sent.head_of(4).is_none());
    }

    #[test]
    fn subtree_includes_all_descendants() {
        let sent = passive_sentence();
        let sub = sent.subtree(4);
        assert_eq!(sub.len(), sent.tokens.len());
    }

    #[test]
    fn subtree_leaf_returns_single() {
        let sent = passive_sentence();
        let sub = sent.subtree(1);
        assert_eq!(sub.len(), 1);
        assert_eq!(sub[0].text, "The");
    }

    #[test]
    fn invalid_id_returns_empty() {
        let sent = passive_sentence();
        assert!(sent.children_of(99).is_empty());
        assert!(sent.head_of(99).is_none());
        assert!(sent.subtree(99).is_empty());
    }

    #[test]
    fn tree_depth_with_non_contiguous_ids() {
        // Token IDs 10, 20, 30 -- not contiguous, not 1-based sequential.
        // tree: 30 (root) -> 20 -> 10
        let sent = Sentence::new(
            "non contiguous ids".to_string(),
            vec![
                make_token(10, "A", "NOUN", "dep", 20),
                make_token(20, "B", "NOUN", "dep", 30),
                make_token(30, "C", "VERB", "root", 0),
            ],
        );
        // Depth from A: A->20(B)->30(C)->root = 2
        // Depth from B: B->30(C)->root = 1
        // Depth from C: root = 0
        assert_eq!(sent.tree_depth, Some(2));
        assert_eq!(sent.root_token().unwrap().text, "C");
        assert_eq!(sent.children_of(30).len(), 1);
        assert_eq!(sent.head_of(10).unwrap().text, "B");
    }

    #[test]
    fn subtree_safe_on_cyclic_graph() {
        // A -> B -> A (cycle of length 2). Must not infinite-loop.
        let sent = Sentence::new(
            "cyclic".to_string(),
            vec![
                make_token(1, "A", "NOUN", "dep", 2),
                make_token(2, "B", "NOUN", "dep", 1),
            ],
        );
        let sub = sent.subtree(1);
        assert_eq!(sub.len(), 2);
    }

    #[test]
    fn empty_sentence_is_safe() {
        let sent = Sentence::new(String::new(), vec![]);
        assert_eq!(sent.word_count, 0);
        assert!(!sent.passive);
        assert_eq!(sent.tree_depth, Some(0));
        assert!(sent.root_token().is_none());
        assert!(sent.children_of(1).is_empty());
        assert!(sent.head_of(1).is_none());
        assert!(sent.subtree(1).is_empty());
    }

    /// Build a straight head chain: token 1 is root (head=0), token 2's
    /// head is 1, ..., token n's head is n-1. Depth from token n to root
    /// is n-1.
    fn straight_chain(n: usize) -> Sentence {
        let tokens: Vec<Token> = (1..=n)
            .map(|i| make_token(i, "x", "NOUN", "dep", if i == 1 { 0 } else { i - 1 }))
            .collect();
        Sentence::new("chain".to_string(), tokens)
    }

    #[test]
    fn tree_depth_25_chain_returns_24() {
        // Verifies the previously-magic-< 20 ceiling is gone: a 25-token
        // chain has true depth 24 (token 25 is 24 hops from root).
        let sent = straight_chain(25);
        assert_eq!(sent.tree_depth, Some(24));
    }

    #[test]
    fn tree_depth_1000_chain_returns_999_in_bounded_time() {
        // Verifies O(n) complexity: a 1000-token chain returns 999, fast.
        // The pre-fix O(n^2) impl would do ~10^6 inner finds; this is well
        // under 50ms on commodity hardware.
        let tokens: Vec<Token> = (1..=1000)
            .map(|i| make_token(i, "x", "NOUN", "dep", if i == 1 { 0 } else { i - 1 }))
            .collect();
        let start = std::time::Instant::now();
        let depth = measure_tree_depth(&tokens);
        let elapsed = start.elapsed();
        assert_eq!(depth, Some(999));
        assert_eq!(straight_chain(1000).tree_depth, Some(999));
        assert!(
            elapsed < std::time::Duration::from_millis(50),
            "tree_depth on 1000-chain took {elapsed:?} (expected < 50ms; suggests non-linear complexity)"
        );
    }

    #[test]
    fn document_error_display_with_and_without_path() {
        let with_path = DocumentError::new(
            Some(PathBuf::from("essay.md")),
            Error::ParseFailed {
                reason: None,
                cause: "boom".to_string(),
            },
        );
        assert_eq!(with_path.to_string(), "essay.md: parse failed: boom");

        let without = DocumentError::new(
            None,
            Error::ParseFailed {
                reason: None,
                cause: "boom".to_string(),
            },
        );
        assert_eq!(without.to_string(), "parse failed: boom");
    }

    #[test]
    fn document_error_source_is_the_domain_error() {
        use std::error::Error as _;
        let err = DocumentError::new(
            None,
            Error::ParseFailed {
                reason: None,
                cause: "boom".to_string(),
            },
        );
        assert!(err.source().is_some());
    }

    /// The message says what could not be read and what would read it:
    /// a feature when one exists, and plainly that none does otherwise.
    #[test]
    fn unsupported_format_names_the_format_and_the_remedy() {
        let pdf = Error::UnsupportedFormat {
            format: Some(Format::Pdf),
            feature: None,
        };
        assert_eq!(
            pdf.to_string(),
            "unsupported format: pdf; no feature of this matra version reads it"
        );
        let with_feature = Error::UnsupportedFormat {
            format: Some(Format::Docx),
            feature: Some("docx"),
        };
        assert_eq!(
            with_feature.to_string(),
            "unsupported format: docx; enable the `docx` feature to read it"
        );
        let undecodable = Error::UnsupportedFormat {
            format: None,
            feature: None,
        };
        assert!(undecodable.to_string().contains("not UTF-8 text"));
    }

    /// The name a message uses is the name the format serializes as, so
    /// the two vocabularies cannot drift.
    #[test]
    fn format_wire_name_is_its_serialized_name() {
        for format in [
            Format::Markdown,
            Format::PlainText,
            Format::Blocks,
            Format::Pdf,
            Format::Docx,
            Format::Other("html".to_string()),
        ] {
            let wire = serde_json::to_value(&format).unwrap();
            assert_eq!(wire, serde_json::json!(format.wire_name()));
            assert_eq!(Format::named(format.wire_name()), format);
            let back: Format = serde_json::from_value(wire).unwrap();
            assert_eq!(back, format, "a name reads back as the variant it names");
        }
    }

    /// A name matra knows reads back as its own variant, never as an
    /// `Other` holding it, so one format cannot have two values.
    #[test]
    fn a_known_name_is_never_other() {
        let read: Format = serde_json::from_value(serde_json::json!("markdown")).unwrap();
        assert_eq!(read, Format::Markdown);
        assert_eq!(Format::named("pdf"), Format::Pdf);
        let unknown = Error::UnsupportedFormat {
            format: Some(Format::named("html")),
            feature: None,
        };
        assert_eq!(
            unknown.to_string(),
            "unsupported format: html; no reader is registered for it"
        );
        assert_eq!(
            unknown.details().get("format"),
            Some(&DetailValue::Text("html".to_string()))
        );
    }

    /// A sentence's span runs from its first token's start to its last
    /// token's end, and is empty at 0 with no tokens.
    #[test]
    fn sentence_span_is_derived_from_its_tokens() {
        let tok = |id: usize, start: usize, end: usize| {
            Token::builder(
                id,
                "w".to_string(),
                "w".to_string(),
                "X".to_string(),
                0,
                "root".to_string(),
            )
            .span(Span::new(start, end))
            .build()
        };
        let sentence = Sentence::new("a b".to_string(), vec![tok(1, 4, 5), tok(2, 6, 9)]);
        assert_eq!(sentence.span, Span::new(4, 9));
        assert_eq!(
            Sentence::new(String::new(), Vec::new()).span,
            Span::default()
        );
    }

    #[test]
    fn format_equality() {
        assert_eq!(Format::Markdown, Format::Markdown);
        assert_ne!(Format::Markdown, Format::PlainText);
    }

    /// The whole extension table, in one place, because it is the one
    /// place. Both the file source and the command line read it, so a
    /// change here changes both routes at once and this test is what
    /// says which change was intended.
    #[test]
    fn format_from_path_reads_the_extension() {
        for (path, expected) in [
            ("notes.md", Format::Markdown),
            ("notes.markdown", Format::Markdown),
            ("report.pdf", Format::Pdf),
            ("report.docx", Format::Docx),
            ("notes.txt", Format::PlainText),
            ("notes.rs", Format::PlainText),
            ("/tmp/deep/notes.md", Format::Markdown),
            ("archive.md.gz", Format::PlainText),
        ] {
            assert_eq!(Format::from_path(path), expected, "{path}");
        }
    }

    /// No extension, and no name at all, are plain text rather than an
    /// error: the format picks a decomposer, and every byte sequence has
    /// one that will read it.
    #[test]
    fn format_from_path_falls_back_to_plain_text() {
        assert_eq!(Format::from_path("README"), Format::PlainText);
        assert_eq!(Format::from_path("<stdin>"), Format::PlainText);
        assert_eq!(Format::from_path(".gitignore"), Format::PlainText);
        assert_eq!(Format::from_path(""), Format::PlainText);
    }

    #[test]
    fn tree_depth_is_none_on_a_cycle() {
        // A -> B -> A is a cycle. The depth is `None`, a value a reader
        // must handle, rather than a sentinel number that reads as a
        // very deep tree. The magic-< 20 ceiling before the visited set
        // silently truncated to 20, protective by accident, not by
        // design (and silently wrong on legitimate deep parses).
        let sent = Sentence::new(
            "cyclic".to_string(),
            vec![
                make_token(1, "A", "NOUN", "dep", 2),
                make_token(2, "B", "NOUN", "dep", 1),
            ],
        );
        assert_eq!(sent.tree_depth, None);
    }

    /// A token rooted in a cycle, reached after the cycle was memoized,
    /// still makes the sentence's depth `None`: the cycle is not hidden
    /// behind a well-formed chain that leads into it.
    #[test]
    fn tree_depth_is_none_when_a_chain_leads_into_a_cycle() {
        let sent = Sentence::new(
            "cyclic tail".to_string(),
            vec![
                make_token(1, "A", "NOUN", "dep", 2),
                make_token(2, "B", "NOUN", "dep", 1),
                make_token(3, "C", "NOUN", "dep", 1),
            ],
        );
        assert_eq!(sent.tree_depth, None);
    }

    #[test]
    fn provenance_carries_this_builds_versions_and_the_format_vocabulary() {
        let p = Provenance::for_test();
        assert_eq!(p.schema, SCHEMA_VERSION);
        assert_eq!(p.matra, env!("CARGO_PKG_VERSION"));
        let json = serde_json::to_value(&p).unwrap();
        assert_eq!(json["format"], "plain_text");
        assert!(json["source"]["path"].is_null());
        assert_eq!(
            serde_json::to_value(Format::Markdown).unwrap(),
            serde_json::json!("markdown")
        );
    }

    /// A path that is not UTF-8 serializes lossily rather than failing
    /// the whole document over its name.
    #[cfg(unix)]
    #[test]
    fn a_non_utf8_source_path_serializes_lossily() {
        use std::os::unix::ffi::OsStrExt;
        let path = PathBuf::from(std::ffi::OsStr::from_bytes(b"bad\xffname.md"));
        let source = ProvenanceSource::new(Some(path), 3, "abc");
        let json = serde_json::to_value(&source).expect("serializes");
        assert_eq!(json["path"], "bad\u{fffd}name.md");
    }

    /// The main gate's label is `"input"`, and the message once read
    /// "input input too large" because the format put the label in
    /// front of the word it already is. Every other gate keeps its label
    /// as a qualifier.
    #[test]
    fn input_too_large_names_the_main_gate_once() {
        let main_gate = Error::InputTooLarge {
            limit: 8,
            actual: 9,
            what: "input",
        };
        assert_eq!(main_gate.to_string(), "input too large: 9 > limit 8");

        let extractor_gate = Error::InputTooLarge {
            limit: 2000,
            actual: 2001,
            what: "tfidf",
        };
        assert_eq!(
            extractor_gate.to_string(),
            "tfidf input too large: 2001 > limit 2000"
        );
    }

    /// The kind vocabulary is a contract every crust reads, so it is
    /// pinned here as well as in `spec/tests/corpus/items.json`: one
    /// string per variant, all seven distinct.
    #[test]
    fn error_kind_names_every_variant_distinctly() {
        let cases: [(Error, &str); 7] = [
            (
                Error::ModelNotFound(PathBuf::from("m.udpipe")),
                "model_not_found",
            ),
            (
                Error::ModelInvalid("truncated".to_string()),
                "model_invalid",
            ),
            (
                Error::ParseFailed {
                    reason: None,
                    cause: "no parse".to_string(),
                },
                "parse_failed",
            ),
            (
                Error::InputTooLarge {
                    limit: 1,
                    actual: 2,
                    what: "input",
                },
                "input_too_large",
            ),
            (
                Error::UnsupportedFormat {
                    format: Some(Format::Pdf),
                    feature: None,
                },
                "unsupported_format",
            ),
            (
                Error::InvalidInput {
                    reason: "threshold_not_finite",
                    message: "bad argument".to_string(),
                },
                "invalid_input",
            ),
            (
                Error::Io(std::io::Error::from(std::io::ErrorKind::NotFound)),
                "io",
            ),
        ];

        let mut seen: Vec<&'static str> = Vec::new();
        for (error, expected) in &cases {
            assert_eq!(error.kind(), *expected, "{error}");
            assert!(!seen.contains(expected), "duplicate kind {expected}");
            seen.push(expected);
        }
    }

    /// Each limit is published and the docs state its number, so every
    /// value is pinned here: a change fails this test and has to be made
    /// on purpose, with the docs that state it.
    #[test]
    fn the_limits_hold_their_published_values() {
        use super::limits::*;
        assert_eq!(MAX_INPUT_BYTES, 8_388_608);
        assert_eq!(MAX_CONFIG_FILE_BYTES, 65_536);
        assert_eq!(MAX_DOWNLOAD_BYTES, 67_108_864);
        assert_eq!(MAX_TFIDF_SENTENCES, 2000);
        assert_eq!(MAX_TEXTRANK_SENTENCES, 2000);
        assert_eq!(MAX_CLUSTER_SENTENCES, MAX_TEXTRANK_SENTENCES);
        assert_eq!(MAX_RAKE_TOKENS, 200_000);
        assert_eq!(MAX_YAKE_TOKENS, 200_000);
        assert_eq!(MAX_COMPRESSION_PARAGRAPH_BYTES, 262_144);
        assert_eq!(MIN_READABILITY_WORDS, 11);
        assert_eq!(MIN_COMPRESSION_WORDS, 51);
    }

    /// The details of `error` as a JSON object, for comparing whole.
    fn details_json(error: &Error) -> serde_json::Value {
        serde_json::to_value(error.details()).expect("details serialize")
    }

    /// Every value a caller acts on is a detail, so a reader never parses
    /// the message (EPR-0006): every dynamic value in it, and on
    /// `invalid_input` the name of the check that failed. One case per variant, and per
    /// shape a variant takes, each built with values distinct enough
    /// that a detail cannot match by accident.
    #[test]
    fn every_dynamic_value_in_a_message_is_a_detail() {
        use serde_json::json;
        let cases: Vec<(Error, serde_json::Value)> = vec![
            (
                Error::ModelNotFound(PathBuf::from("models/m.udpipe")),
                json!({ "path": "models/m.udpipe" }),
            ),
            (
                Error::ModelInvalid("truncated at byte 7".to_string()),
                json!({ "cause": "truncated at byte 7" }),
            ),
            (
                Error::ParseFailed {
                    reason: None,
                    cause: "udpipe panicked: boom".to_string(),
                },
                json!({ "cause": "udpipe panicked: boom" }),
            ),
            (
                Error::ParseFailed {
                    reason: Some("head_out_of_range"),
                    cause: "token 3 of sentence 1 has head 9".to_string(),
                },
                json!({
                    "reason": "head_out_of_range",
                    "cause": "token 3 of sentence 1 has head 9",
                }),
            ),
            (
                Error::InputTooLarge {
                    limit: 2000,
                    actual: 2001,
                    what: "tfidf",
                },
                json!({ "what": "tfidf", "limit": 2000, "actual": 2001 }),
            ),
            (
                Error::UnsupportedFormat {
                    format: Some(Format::Pdf),
                    feature: None,
                },
                json!({ "format": "pdf" }),
            ),
            (
                Error::UnsupportedFormat {
                    format: Some(Format::Docx),
                    feature: Some("docx"),
                },
                json!({ "format": "docx", "feature": "docx" }),
            ),
            (
                Error::UnsupportedFormat {
                    format: None,
                    feature: None,
                },
                json!({}),
            ),
            (
                Error::InvalidInput {
                    reason: "threshold_not_finite",
                    message: "threshold must be finite, got NaN".to_string(),
                },
                json!({ "reason": "threshold_not_finite" }),
            ),
            (
                Error::Io(std::io::Error::new(
                    std::io::ErrorKind::PermissionDenied,
                    "denied",
                )),
                json!({ "io_kind": "permission_denied", "cause": "denied" }),
            ),
            (
                Error::Io(IoContext::at_path(
                    std::io::ErrorKind::NotFound,
                    Path::new("/models/x"),
                    "cannot read /models/x: gone",
                )),
                json!({
                    "io_kind": "not_found",
                    "path": "/models/x",
                    "cause": "cannot read /models/x: gone",
                }),
            ),
            (
                Error::Io(IoContext::download(
                    std::io::ErrorKind::TimedOut,
                    "https://models.example/m",
                    "download https://models.example/m: timeout",
                    true,
                )),
                json!({
                    "io_kind": "timed_out",
                    "url": "https://models.example/m",
                    "retryable": true,
                    "cause": "download https://models.example/m: timeout",
                }),
            ),
        ];
        for (error, expected) in &cases {
            assert_eq!(details_json(error), *expected, "{error}");
        }
    }

    /// The I/O payload carries what the message says without changing
    /// the message: an `Io` built with a context reads exactly as one
    /// built from the text alone.
    #[test]
    fn an_io_context_does_not_change_the_message() {
        let plain = Error::Io(std::io::Error::new(
            std::io::ErrorKind::NotFound,
            "cannot read /m: gone",
        ));
        let with_context = Error::Io(IoContext::at_path(
            std::io::ErrorKind::NotFound,
            Path::new("/m"),
            "cannot read /m: gone",
        ));
        assert_eq!(plain.to_string(), with_context.to_string());
        assert_eq!(plain.kind(), with_context.kind());
    }

    /// The serde form is `{kind, message, details}`, and a
    /// `DocumentError`'s adds `path`, null for input never on disk.
    #[test]
    fn errors_serialize_as_kind_message_and_details() {
        let error = Error::InputTooLarge {
            limit: 8,
            actual: 9,
            what: "input",
        };
        assert_eq!(
            serde_json::to_value(&error).unwrap(),
            serde_json::json!({
                "kind": "input_too_large",
                "message": "input too large: 9 > limit 8",
                "details": { "what": "input", "limit": 8, "actual": 9 },
            })
        );

        let on_disk = DocumentError::new(
            Some(PathBuf::from("essay.md")),
            Error::ParseFailed {
                reason: None,
                cause: "boom".to_string(),
            },
        );
        assert_eq!(
            serde_json::to_value(&on_disk).unwrap(),
            serde_json::json!({
                "path": "essay.md",
                "kind": "parse_failed",
                "message": "parse failed: boom",
                "details": { "cause": "boom" },
            })
        );
        let in_memory = DocumentError::new(
            None,
            Error::ParseFailed {
                reason: None,
                cause: "boom".to_string(),
            },
        );
        assert!(serde_json::to_value(&in_memory).unwrap()["path"].is_null());
    }

    /// A path that is not UTF-8 serializes lossily, as a document's
    /// source path does, rather than failing the error over its name.
    #[cfg(unix)]
    #[test]
    fn a_non_utf8_document_error_path_serializes_lossily() {
        use std::os::unix::ffi::OsStrExt;
        let path = PathBuf::from(std::ffi::OsStr::from_bytes(b"bad\xffname.md"));
        let err = DocumentError::new(
            Some(path),
            Error::ParseFailed {
                reason: None,
                cause: "boom".to_string(),
            },
        );
        let json = serde_json::to_value(&err).expect("serializes");
        assert_eq!(json["path"], "bad\u{fffd}name.md");
    }

    /// The `io_kind` names are written out, so they cannot drift with
    /// `ErrorKind`'s `Debug` text, and a kind not named is `other`.
    #[test]
    fn io_kinds_are_named_in_snake_case() {
        use std::io::ErrorKind;
        for (kind, name) in [
            (ErrorKind::NotFound, "not_found"),
            (ErrorKind::PermissionDenied, "permission_denied"),
            (ErrorKind::TimedOut, "timed_out"),
            (ErrorKind::NotConnected, "not_connected"),
            (ErrorKind::NotADirectory, "not_a_directory"),
            (ErrorKind::Other, "other"),
            (ErrorKind::WouldBlock, "other"),
        ] {
            assert_eq!(io_kind_name(kind), name, "{kind:?}");
        }
    }
}