jev-repl 0.8.0

Terminal REPL for shaping TypeSafe AI System One requests: noul, choice and score questions, with a live sketch editor
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
//! Scoring a rubric against states someone has already labelled.
//!
//! One call tells you what the model said about one state; that is what `jev run` is for. It does
//! not tell you where to put the threshold, or how confident a choice has to be before a script
//! may act on it — the README leaves both calls to you, and this is the module that turns them
//! into a table. Feed it a page and a file of labelled states and it reports what the rubric got
//! right, at every threshold worth trying.
//!
//! Everything here is pure: cases come in as text, the answers come from a function you pass, and
//! the report goes out as lines and JSON. Reading files, hashing request bodies and talking to the
//! API belong to the caller.

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

use ratatui::style::Style;
use ratatui::text::{Line, Span};
use serde_json::{Value, json};
use tokio::sync::Semaphore;
use tokio::task::JoinSet;
use typesafe::{Answer, Choice, Question, Usage};

use crate::cost::{self, Cost, Rates};
use crate::format::{BAD, CHOICE, DIM, SCORE, bold, color_for, dim, text_of};
use crate::headless::Answered;
use crate::session::{self, Session};

/// One labelled state: what to judge, and what the rubric should say about it.
#[derive(Debug, Clone, PartialEq)]
pub struct Case {
    /// 1-based line in the cases file, for messages.
    pub line: usize,
    pub id: Option<String>,
    pub state: Value,
    /// Question name → expectation, in the order the file gave them, already checked against the
    /// session's questions. A `Vec` and not a map, because that is the shape `Session` uses for
    /// questions and it saves a dependency.
    pub expect: Vec<(String, Expectation)>,
    /// For a case labelled per turn: which prefix of the conversation this is (1-based), and how
    /// many turns the whole conversation has. Such a case is sent once per turn, and each is a case.
    pub turn: Option<usize>,
    pub turns: Option<usize>,
}

/// A per-turn label: the turn a noul becomes true from, or never.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ByTurn {
    Turn(usize),
    Never,
}

impl ByTurn {
    /// The turn, or `None` for never — what the JSON report writes.
    pub fn turn(self) -> Option<usize> {
        match self {
            ByTurn::Turn(k) => Some(k),
            ByTurn::Never => None,
        }
    }
}

/// What one question is expected to answer, in the shape its kind is scored in.
#[derive(Debug, Clone, PartialEq)]
pub enum Expectation {
    Noul {
        yes: bool,
        /// Set when the label was `{"by_turn": k}`: the turn it becomes true, or never.
        by_turn: Option<ByTurn>,
    },
    Choice {
        label: String,
    },
    Score {
        level: usize,
    },
}

impl Expectation {
    /// The wire `type` of the answer this expectation can be compared with.
    pub fn kind(&self) -> &'static str {
        match self {
            Expectation::Noul { .. } => "noul",
            Expectation::Choice { .. } => "choice",
            Expectation::Score { .. } => "score",
        }
    }
}

/// Parse JSON Lines into cases, checking every expectation against `session`.
///
/// Blank lines are skipped and everything else has to be a case, because a file of labels is worth
/// nothing if a typo silently drops a row. The line number travels with the case: it is what the
/// report names a case by, so a bad row is found by the same number that reported it.
pub fn parse_cases(text: &str, session: &Session) -> Result<Vec<Case>, String> {
    let mut cases = Vec::new();
    read_cases(text, |one| {
        let mut expect = Vec::with_capacity(one.wanted.len());
        for (name, value) in one.wanted {
            let question = question_of(session, name)
                .ok_or_else(|| format!("no question named {name:?} on the page."))?;
            expect.push((
                name.clone(),
                expected(name, question, value, turn_count(one.state))?,
            ));
        }
        cases.extend(cases_of(&one, expect));
        Ok(())
    })?;
    Ok(cases)
}

/// What the two pages of a comparison are called in its messages and its report.
#[derive(Debug, Clone, Copy)]
pub struct Labels<'a> {
    pub a: &'a str,
    pub b: &'a str,
}

/// Parse one cases file for two pages at once: a case per page, each holding the expectations for
/// that page's questions.
///
/// A label may name a question on either page, which is what lets a page that adds a question be
/// compared with one that does not. It is still checked against every page that has the question —
/// a case one page cannot even express is not a paired observation, it is a typo.
pub fn parse_compare_cases(
    text: &str,
    a: &Session,
    b: &Session,
    labels: Labels<'_>,
) -> Result<(Vec<Case>, Vec<Case>), String> {
    let mut left = Vec::new();
    let mut right = Vec::new();
    read_cases(text, |one| {
        let mut expect_a = Vec::new();
        let mut expect_b = Vec::new();
        for (name, value) in one.wanted {
            let on_a = question_of(a, name);
            let on_b = question_of(b, name);
            if on_a.is_none() && on_b.is_none() {
                return Err(format!("no question named {name:?} on either page."));
            }
            for (question, into, label) in [
                (on_a, &mut expect_a, labels.a),
                (on_b, &mut expect_b, labels.b),
            ] {
                let Some(question) = question else { continue };
                let expectation = expected(name, question, value, turn_count(one.state))
                    .map_err(|e| format!("{label}: {e}"))?;
                into.push((name.clone(), expectation));
            }
        }
        left.extend(cases_of(&one, expect_a));
        right.extend(cases_of(&one, expect_b));
        Ok(())
    })?;
    Ok((left, right))
}

/// A line of the cases file that is a case in shape, before its labels meet a page.
struct RawCase<'a> {
    line: usize,
    id: Option<String>,
    state: &'a Value,
    wanted: &'a serde_json::Map<String, Value>,
}

/// Hand every non-blank line to `visit` as a case, stopping at the first line that is not one or
/// that `visit` refuses; the message that comes back already names the line.
fn read_cases(
    text: &str,
    mut visit: impl FnMut(RawCase<'_>) -> Result<(), String>,
) -> Result<(), String> {
    let mut seen = 0usize;
    for (i, raw) in text.split('\n').enumerate() {
        let raw = raw.trim();
        if raw.is_empty() {
            continue;
        }
        let line = i + 1;
        let value: Value = serde_json::from_str(raw)
            .map_err(|e| format!("cases line {line}: not valid JSON: {e}"))?;
        let one = read_case(&value, line).map_err(|e| format!("cases line {line}: {e}"))?;
        visit(one).map_err(|e| format!("cases line {line}: {e}"))?;
        seen += 1;
    }
    if seen == 0 {
        return Err("the cases file holds no cases.".to_owned());
    }
    Ok(())
}

fn read_case(value: &Value, line: usize) -> Result<RawCase<'_>, String> {
    let object = value
        .as_object()
        .ok_or("expected a JSON object with `state` and `expect`.")?;

    let id = match object.get("id") {
        None => None,
        Some(Value::String(s)) => Some(s.clone()),
        Some(_) => return Err("`id` must be a string.".to_owned()),
    };

    let state = object
        .get("state")
        .ok_or("missing `state`: a case has to say what to judge.")?;
    if session::is_empty_value(state) {
        return Err("the `state` is empty: there is nothing to judge.".to_owned());
    }

    let wanted = object
        .get("expect")
        .ok_or("missing `expect`: a case has to say what the answer is.")?;
    let wanted = wanted
        .as_object()
        .filter(|map| !map.is_empty())
        .ok_or("`expect` has to name at least one question.")?;
    Ok(RawCase {
        line,
        id,
        state,
        wanted,
    })
}

/// The cases one line becomes: itself, or — when a noul is labelled per turn — one case per prefix
/// of the conversation. A per-turn noul expects `turn >= k` at every prefix; the line's other
/// labels were written about the whole conversation, so they go on the last prefix only. A case
/// left with nothing to score is not sent at all.
fn cases_of(one: &RawCase<'_>, expect: Vec<(String, Expectation)>) -> Vec<Case> {
    let named = |state: Value, expect, turn, turns| Case {
        line: one.line,
        id: one.id.clone(),
        state,
        expect,
        turn,
        turns,
    };
    if expect.is_empty() {
        return Vec::new();
    }
    let per_turn = expect.iter().any(|(_, e)| {
        matches!(
            e,
            Expectation::Noul {
                by_turn: Some(_),
                ..
            }
        )
    });
    let turns = session::turns_of(one.state);
    let (true, Some(turns)) = (per_turn, turns) else {
        return vec![named(one.state.clone(), expect, None, None)];
    };
    let n = turns.len();
    let mut out = Vec::new();
    for turn in 1..=n {
        let mut at = Vec::new();
        for (name, e) in &expect {
            match e {
                Expectation::Noul {
                    by_turn: Some(by_turn),
                    ..
                } => {
                    let yes = matches!(by_turn, ByTurn::Turn(k) if turn >= *k);
                    at.push((
                        name.clone(),
                        Expectation::Noul {
                            yes,
                            by_turn: Some(*by_turn),
                        },
                    ));
                }
                _ if turn == n => at.push((name.clone(), e.clone())),
                _ => {}
            }
        }
        if at.is_empty() {
            continue;
        }
        let state = session::turns_to_json(&turns[..turn]);
        out.push(named(state, at, Some(turn), Some(n)));
    }
    out
}

/// How many turns a state has, when it is a conversation.
fn turn_count(state: &Value) -> Option<usize> {
    session::turns_of(state).map(|turns| turns.len())
}

/// A JSON value the way `JSON.stringify` writes it, whole numbers without a `.0`.
fn compact(value: &Value) -> String {
    match value.as_f64() {
        Some(n) if value.is_f64() && n.fract() == 0.0 && n.abs() < 9e15 => (n as i64).to_string(),
        _ => value.to_string(),
    }
}

fn question_of<'a>(session: &'a Session, name: &str) -> Option<&'a Question> {
    session
        .questions
        .iter()
        .find(|(n, _)| n == name)
        .map(|(_, q)| q)
}

/// Check one expected value against the question it names, and store it the way it is scored.
fn expected(
    name: &str,
    question: &Question,
    value: &Value,
    turns: Option<usize>,
) -> Result<Expectation, String> {
    if let (Question::Choice(_) | Question::Score(_), Value::Object(object)) = (question, value)
        && object.contains_key("by_turn")
    {
        let kind = if matches!(question, Question::Choice(_)) {
            "choice"
        } else {
            "score"
        };
        return Err(format!("by_turn is for a noul, and {name} is a {kind}."));
    }
    match question {
        Question::Noul(_) => match value {
            Value::Object(object) => by_turn(name, object, value, turns),
            Value::Bool(yes) => Ok(Expectation::Noul {
                yes: *yes,
                by_turn: None,
            }),
            other => Err(format!(
                "{name} is a noul: expected true or false, got {other}."
            )),
        },
        Question::Choice(q) => {
            let labels: Vec<&str> = q.criteria.keys().map(String::as_str).collect();
            match value.as_str() {
                Some(label) if labels.contains(&label) => Ok(Expectation::Choice {
                    label: label.to_owned(),
                }),
                _ => Err(format!(
                    "{name} is a choice between {}; got {value}.",
                    labels.join(", ")
                )),
            }
        }
        Question::Score(q) => {
            let top = q.criteria.len().saturating_sub(1);
            if let Value::Number(number) = value {
                let n = number.as_f64().unwrap_or(f64::NAN);
                if n.fract() == 0.0 && (0.0..=top as f64).contains(&n) {
                    return Ok(Expectation::Score { level: n as usize });
                }
                return Err(format!(
                    "{name} is a score: expected a level from 0 to {top}, got {number}."
                ));
            }
            // A level's own text reads better in a cases file than its index does; the first wins.
            let wanted = text_of(value);
            match q.criteria.iter().position(|level| text_of(level) == wanted) {
                Some(at) => Ok(Expectation::Score { level: at }),
                None => Err(format!(
                    "{name} is a score: expected a level from 0 to {top}, or one of its levels; got {value}."
                )),
            }
        }
        // A hand-built question object has no shape to score against, and neither has a kind this
        // build does not know.
        _ => Err(format!(
            "{name} is a raw question: raw questions cannot be scored."
        )),
    }
}

/// `{"by_turn": k}`: false before turn `k` of the conversation and true from it on, or never true
/// when `k` is null. It only means something over a conversation, and only for a turn it has.
fn by_turn(
    name: &str,
    object: &serde_json::Map<String, Value>,
    value: &Value,
    turns: Option<usize>,
) -> Result<Expectation, String> {
    if object.len() != 1 || !object.contains_key("by_turn") {
        return Err(format!(
            "{name}: a per-turn expectation is {{\"by_turn\": n}}, the turn it becomes true, or null for never; got {}.",
            compact(value)
        ));
    }
    let Some(turns) = turns else {
        return Err(format!(
            "{name} gives by_turn, but the state is not a conversation of turns."
        ));
    };
    let k = &object["by_turn"];
    if k.is_null() {
        return Ok(Expectation::Noul {
            yes: false,
            by_turn: Some(ByTurn::Never),
        });
    }
    match k.as_f64() {
        Some(n) if n.fract() == 0.0 && n >= 1.0 && n <= turns as f64 => Ok(Expectation::Noul {
            yes: false,
            by_turn: Some(ByTurn::Turn(n as usize)),
        }),
        _ => Err(format!(
            "{name} by_turn must be a whole turn from 1 to {turns}, or null for never; got {}.",
            compact(k)
        )),
    }
}

/// The session as one case sends it: the page's questions, the case's state.
pub fn with_state(session: &Session, state: Value) -> Session {
    Session {
        state,
        questions: session.questions.clone(),
        model: session.model.clone(),
        bars: session.bars.clone(),
    }
}

/// What one case's request came back as.
#[derive(Debug, Clone)]
pub enum Outcome {
    Ok {
        answers: Vec<Answered>,
        usage: Option<Usage>,
    },
    Failed {
        error: String,
    },
}

/// Send every case through `ask`, at most `concurrency` at a time; results are in case order.
///
/// A slow case holds up nothing but itself, and each result is written to its own slot: a file of
/// a thousand labels keeps its order however the calls come back.
pub async fn run<F, Fut>(
    session: &Session,
    cases: &[Case],
    ask: F,
    concurrency: usize,
) -> Vec<Outcome>
where
    F: Fn(Session) -> Fut + Send + Sync + Clone + 'static,
    Fut: Future<Output = Outcome> + Send + 'static,
{
    let permits = Arc::new(Semaphore::new(concurrency.max(1)));
    let mut workers = JoinSet::new();
    spawn_leg(&mut workers, &permits, 0, session, cases, ask).await;
    let [outcomes] = collect(workers, [cases.len()]).await;
    outcomes
}

/// One page's share of a comparison: its session, its cases, and how to ask it.
pub struct Leg<'a, F> {
    pub session: &'a Session,
    pub cases: &'a [Case],
    pub ask: F,
}

/// Both pages of a comparison through one pool of workers: page `a`'s cases first, then `b`'s.
///
/// One pool rather than one per page, so `--concurrency` still means what it says — that many
/// requests in the air, whichever page they are for.
pub async fn run_compare<FA, FutA, FB, FutB>(
    a: Leg<'_, FA>,
    b: Leg<'_, FB>,
    concurrency: usize,
) -> (Vec<Outcome>, Vec<Outcome>)
where
    FA: Fn(Session) -> FutA + Send + Sync + Clone + 'static,
    FutA: Future<Output = Outcome> + Send + 'static,
    FB: Fn(Session) -> FutB + Send + Sync + Clone + 'static,
    FutB: Future<Output = Outcome> + Send + 'static,
{
    let permits = Arc::new(Semaphore::new(concurrency.max(1)));
    let mut workers = JoinSet::new();
    let sizes = [a.cases.len(), b.cases.len()];
    spawn_leg(&mut workers, &permits, 0, a.session, a.cases, a.ask).await;
    spawn_leg(&mut workers, &permits, 1, b.session, b.cases, b.ask).await;
    let [left, right] = collect(workers, sizes).await;
    (left, right)
}

/// Queue one leg's cases on the pool, in order: a case is spawned once a permit is free, so the
/// cases go out in file order and never more than the pool allows are in the air.
async fn spawn_leg<F, Fut>(
    workers: &mut JoinSet<(usize, usize, Outcome)>,
    permits: &Arc<Semaphore>,
    leg: usize,
    session: &Session,
    cases: &[Case],
    ask: F,
) where
    F: Fn(Session) -> Fut + Send + Sync + Clone + 'static,
    Fut: Future<Output = Outcome> + Send + 'static,
{
    for (at, one) in cases.iter().enumerate() {
        let session = with_state(session, one.state.clone());
        let ask = ask.clone();
        let permit = Arc::clone(permits).acquire_owned().await;
        workers.spawn(async move {
            let _permit = permit;
            (leg, at, ask(session).await)
        });
    }
}

/// Wait for every worker and put each outcome in its leg's slot for its case.
async fn collect<const N: usize>(
    mut workers: JoinSet<(usize, usize, Outcome)>,
    sizes: [usize; N],
) -> [Vec<Outcome>; N] {
    let mut slots: [Vec<Option<Outcome>>; N] = sizes.map(|n| vec![None; n]);
    while let Some(joined) = workers.join_next().await {
        // A worker that panicked leaves its slot empty; the run is not lost to one case.
        if let Ok((leg, at, outcome)) = joined {
            slots[leg][at] = Some(outcome);
        }
    }
    slots.map(|outcomes| {
        outcomes
            .into_iter()
            .map(|outcome| {
                outcome.unwrap_or_else(|| Outcome::Failed {
                    error: "nothing was sent for this case.".to_owned(),
                })
            })
            .collect()
    })
}

/// One row of a noul's threshold sweep: the confusion counts, and what they come to.
#[derive(Debug, Clone, PartialEq)]
pub struct SweepRow {
    pub threshold: f64,
    pub tp: usize,
    pub fp: usize,
    pub r#fn: usize,
    pub tn: usize,
    pub accuracy: f64,
    /// `None` when nothing was predicted a yes, because a rate over nothing is not zero.
    pub precision: Option<f64>,
    /// `None` when nothing was expected to be a yes.
    pub recall: Option<f64>,
    pub f1: f64,
}

/// One cut of the confidence gate: how much of the set survives it, and how right it is.
#[derive(Debug, Clone, PartialEq)]
pub struct GateRow {
    pub confidence: f64,
    pub coverage: f64,
    /// `None` when no case is confident enough to be counted.
    pub accuracy: Option<f64>,
}

/// The threshold that scored best, and what it scored.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Best {
    pub threshold: f64,
    pub f1: f64,
}

/// What one question scored, in the numbers its kind is judged by.
#[derive(Debug, Clone, PartialEq)]
pub enum QuestionReport {
    Noul {
        name: String,
        cases: usize,
        /// Mean squared error of the probability itself, threshold or no threshold.
        brier: f64,
        /// What it was read at: the page's `@threshold`, or the run's threshold when it has none.
        threshold: f64,
        /// Accuracy at that threshold.
        accuracy: f64,
        best: Best,
        sweep: Vec<SweepRow>,
        /// When it noticed, for the conversations labelled per turn; `None` when none were.
        latency: Option<Latency>,
    },
    Choice {
        name: String,
        cases: usize,
        accuracy: f64,
        /// The page's options, plus `other` when the model answered something else.
        labels: Vec<String>,
        /// Rows expected, columns predicted.
        confusion: Vec<Vec<usize>>,
        gate: Vec<GateRow>,
    },
    Score {
        name: String,
        cases: usize,
        exact: f64,
        within_one: f64,
        mae: f64,
        gate: Vec<GateRow>,
    },
}

impl QuestionReport {
    pub fn name(&self) -> &str {
        match self {
            QuestionReport::Noul { name, .. }
            | QuestionReport::Choice { name, .. }
            | QuestionReport::Score { name, .. } => name,
        }
    }

    /// The wire `type` of the question this reports on.
    pub fn kind(&self) -> &'static str {
        match self {
            QuestionReport::Noul { .. } => "noul",
            QuestionReport::Choice { .. } => "choice",
            QuestionReport::Score { .. } => "score",
        }
    }

    pub fn cases(&self) -> usize {
        match self {
            QuestionReport::Noul { cases, .. }
            | QuestionReport::Choice { cases, .. }
            | QuestionReport::Score { cases, .. } => *cases,
        }
    }

    /// The accuracy `--min-accuracy` holds a question to: exact agreement at the chosen threshold.
    pub fn accuracy_of(&self) -> f64 {
        match self {
            QuestionReport::Noul { accuracy, .. } | QuestionReport::Choice { accuracy, .. } => {
                *accuracy
            }
            QuestionReport::Score { exact, .. } => *exact,
        }
    }
}

/// A case that never produced a full set of answers, and why.
#[derive(Debug, Clone, PartialEq)]
pub struct CaseError {
    pub case: usize,
    /// The prefix of a per-turn case that failed.
    pub turn: Option<usize>,
    pub id: Option<String>,
    pub message: String,
}

/// The tokens the run spent, counted when the API counted them and estimated when it did not.
#[derive(Debug, Clone, PartialEq)]
pub struct ReportUsage {
    pub input_tokens: u64,
    pub output_tokens: u64,
    pub estimated: bool,
    pub cost: Option<Cost>,
}

/// Everything the run found out, with the numbers unrounded.
#[derive(Debug, Clone, PartialEq)]
pub struct Report {
    pub model: String,
    pub threshold: f64,
    pub cases: usize,
    pub answered: usize,
    pub errors: Vec<CaseError>,
    pub questions: Vec<QuestionReport>,
    pub usage: ReportUsage,
}

/// What the run was asked for, which the report repeats back.
#[derive(Debug, Clone, Copy)]
pub struct ReportOptions<'a> {
    pub model: &'a str,
    pub threshold: f64,
    pub rates: Option<Rates>,
}

/// The thresholds a sweep always covers; the chosen one joins them when it is not one of these.
const SWEEP: [f64; 9] = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9];

/// The cuts the confidence gate is read at.
const CUTS: [f64; 5] = [0.0, 0.2, 0.4, 0.6, 0.8];

/// One case that answered everything it was labelled for.
struct Scored<'a> {
    line: usize,
    id: Option<&'a str>,
    turn: Option<usize>,
    turns: Option<usize>,
    expect: &'a [(String, Expectation)],
    answers: Vec<(String, Answer)>,
    usage: Option<Usage>,
}

impl Scored<'_> {
    fn answer(&self, name: &str) -> Option<&Answer> {
        self.answers.iter().find(|(n, _)| n == name).map(|(_, a)| a)
    }

    fn expects(&self, name: &str) -> Option<&Expectation> {
        self.expect.iter().find(|(n, _)| n == name).map(|(_, e)| e)
    }
}

/// Score the outcomes against the cases.
///
/// A case either answered everything it was labelled for or it counts as an error: a half-answered
/// case would quietly skew whichever question it did answer, and a rubric is being judged here.
pub fn report(
    session: &Session,
    cases: &[Case],
    outcomes: &[Outcome],
    options: ReportOptions<'_>,
) -> Report {
    let (errors, scored) = scored_of(cases, outcomes);
    let mut questions = Vec::new();
    for (name, question) in &session.questions {
        let rows: Vec<&Scored<'_>> = scored
            .iter()
            .filter(|one| one.expects(name).is_some())
            .collect();
        if rows.is_empty() {
            continue;
        }
        match question {
            Question::Noul(_) => questions.push(noul_report(
                name,
                &rows,
                session.threshold_of(name, options.threshold),
            )),
            Question::Choice(q) => questions.push(choice_report(name, q, &rows)),
            Question::Score(_) => questions.push(score_report(name, &rows)),
            _ => {}
        }
    }

    let usage = usage_of(session, cases, &scored, options.model, options.rates);
    Report {
        model: options.model.to_owned(),
        threshold: options.threshold,
        cases: cases.len(),
        answered: scored.len(),
        errors,
        questions,
        usage,
    }
}

/// Split the outcomes into the cases that can be scored and the ones that are errors.
fn scored_of<'a>(cases: &'a [Case], outcomes: &[Outcome]) -> (Vec<CaseError>, Vec<Scored<'a>>) {
    let mut errors: Vec<CaseError> = Vec::new();
    let mut scored: Vec<Scored<'a>> = Vec::new();
    for (at, one) in cases.iter().enumerate() {
        let mut failed = |message: String| {
            errors.push(CaseError {
                case: one.line,
                turn: one.turn,
                id: one.id.clone(),
                message,
            });
        };
        let (answers, usage) = match outcomes.get(at) {
            None => {
                failed("nothing was sent for this case.".to_owned());
                continue;
            }
            Some(Outcome::Failed { error }) => {
                failed(error.clone());
                continue;
            }
            Some(Outcome::Ok { answers, usage }) => (answers, usage),
        };
        let answers: Vec<(String, Answer)> = answers
            .iter()
            .filter_map(|(name, answer)| answer.clone().map(|a| (name.clone(), a)))
            .collect();
        if let Some(message) = unscorable(&one.expect, &answers) {
            failed(message);
            continue;
        }
        scored.push(Scored {
            line: one.line,
            id: one.id.as_deref(),
            turn: one.turn,
            turns: one.turns,
            expect: &one.expect,
            answers,
            usage: usage.clone(),
        });
    }
    (errors, scored)
}

/// Why this case cannot be scored, if it cannot: the first label that got no answer, or one whose
/// answer came back as another kind.
fn unscorable(expect: &[(String, Expectation)], answers: &[(String, Answer)]) -> Option<String> {
    for (name, expectation) in expect {
        match answers.iter().find(|(n, _)| n == name).map(|(_, a)| a) {
            None => return Some(format!("no answer came back for {name}")),
            Some(answer) if answer.kind() != expectation.kind() => {
                return Some(format!(
                    "{name} came back as a {}, not a {}",
                    answer.kind(),
                    expectation.kind()
                ));
            }
            Some(_) => {}
        }
    }
    None
}

/// Questions whose accuracy is below `bar`, for --min-accuracy.
pub fn below_bar(report: &Report, bar: f64) -> Vec<(String, f64)> {
    report
        .questions
        .iter()
        .filter(|q| q.accuracy_of() < bar)
        .map(|q| (q.name().to_owned(), q.accuracy_of()))
        .collect()
}

fn noul_report(name: &str, rows: &[&Scored<'_>], threshold: f64) -> QuestionReport {
    let points: Vec<(f64, bool)> = rows
        .iter()
        .map(|row| {
            let p = match row.answer(name) {
                Some(Answer::Noul(a)) => a.noul,
                _ => 0.0,
            };
            let yes = matches!(row.expects(name), Some(Expectation::Noul { yes: true, .. }));
            (p, yes)
        })
        .collect();

    let mut thresholds: Vec<f64> = SWEEP.to_vec();
    if !thresholds.contains(&threshold) {
        thresholds.push(threshold);
        thresholds.sort_by(f64::total_cmp);
    }
    let sweep: Vec<SweepRow> = thresholds
        .iter()
        .map(|at| sweep_row(&points, *at))
        .collect();
    let accuracy = sweep
        .iter()
        .find(|row| row.threshold == threshold)
        .map(|row| row.accuracy)
        .unwrap_or(0.0);
    // The sweep is in ascending order and the comparison is strict, so a tie keeps the lowest.
    let mut best = Best {
        threshold,
        f1: f64::NEG_INFINITY,
    };
    for row in &sweep {
        if row.f1 > best.f1 {
            best = Best {
                threshold: row.threshold,
                f1: row.f1,
            };
        }
    }
    let brier = mean(
        points
            .iter()
            .map(|(p, yes)| (p - if *yes { 1.0 } else { 0.0 }).powi(2)),
    );
    QuestionReport::Noul {
        name: name.to_owned(),
        cases: points.len(),
        brier,
        threshold,
        accuracy,
        best,
        sweep,
        latency: latency_of(name, rows, threshold),
    }
}

/// One conversation labelled per turn: the turn it should have said yes, and the turn it did.
#[derive(Debug, Clone, PartialEq)]
pub struct ThreadLatency {
    pub case: usize,
    pub id: Option<String>,
    /// `by_turn`; `None` when it should never have said yes.
    pub expected: Option<usize>,
    /// The first turn at or above the threshold; `None` when there was none.
    pub detected: Option<usize>,
    /// `detected − expected`, negative when early; `None` unless both are known.
    pub latency: Option<i64>,
}

/// How early or late a noul notices, over the conversations labelled per turn.
#[derive(Debug, Clone, PartialEq)]
pub struct Latency {
    pub threads: usize,
    pub on_time: usize,
    pub early: usize,
    pub late: usize,
    pub missed: usize,
    pub false_alarms: usize,
    /// Mean latency over the threads that expected a yes and got one; `None` when none did.
    pub mean: Option<f64>,
    pub cases: Vec<ThreadLatency>,
}

/// Detection latency: for each conversation labelled per turn, the first turn the noul said yes,
/// against the turn it should have. A thread with a prefix that errored is left out, because its
/// first yes might be the one that is missing.
fn latency_of(name: &str, rows: &[&Scored<'_>], threshold: f64) -> Option<Latency> {
    let mut threads: Vec<(usize, Vec<&Scored<'_>>)> = Vec::new();
    for row in rows {
        let per_turn = matches!(
            row.expects(name),
            Some(Expectation::Noul {
                by_turn: Some(_),
                ..
            })
        );
        if !per_turn || row.turn.is_none() {
            continue;
        }
        match threads.iter_mut().find(|(line, _)| *line == row.line) {
            Some((_, thread)) => thread.push(row),
            None => threads.push((row.line, vec![row])),
        }
    }
    if threads.is_empty() {
        return None;
    }
    let mut cases = Vec::new();
    let (mut on_time, mut early, mut late, mut missed, mut false_alarms) = (0, 0, 0, 0, 0);
    let mut lags: Vec<f64> = Vec::new();
    for (line, mut thread) in threads {
        let first = thread[0];
        if Some(thread.len()) != first.turns {
            continue;
        }
        thread.sort_by_key(|row| row.turn);
        let expected = match first.expects(name) {
            Some(Expectation::Noul {
                by_turn: Some(by_turn),
                ..
            }) => by_turn.turn(),
            _ => None,
        };
        let detected = thread
            .iter()
            .find(|row| matches!(row.answer(name), Some(Answer::Noul(a)) if a.noul >= threshold))
            .and_then(|row| row.turn);
        let lag = match (expected, detected) {
            (Some(k), Some(d)) => Some(d as i64 - k as i64),
            _ => None,
        };
        match (expected, lag) {
            (None, _) => {
                if detected.is_some() {
                    false_alarms += 1;
                }
            }
            (Some(_), None) => missed += 1,
            (Some(_), Some(lag)) => {
                lags.push(lag as f64);
                match lag.cmp(&0) {
                    std::cmp::Ordering::Equal => on_time += 1,
                    std::cmp::Ordering::Less => early += 1,
                    std::cmp::Ordering::Greater => late += 1,
                }
            }
        }
        cases.push(ThreadLatency {
            case: line,
            id: first.id.map(str::to_owned),
            expected,
            detected,
            latency: lag,
        });
    }
    Some(Latency {
        threads: cases.len(),
        on_time,
        early,
        late,
        missed,
        false_alarms,
        mean: (!lags.is_empty()).then(|| mean(lags.iter().copied())),
        cases,
    })
}

fn sweep_row(points: &[(f64, bool)], threshold: f64) -> SweepRow {
    let (mut tp, mut fp, mut fneg, mut tn) = (0usize, 0usize, 0usize, 0usize);
    for (p, yes) in points {
        match (*p >= threshold, *yes) {
            (true, true) => tp += 1,
            (true, false) => fp += 1,
            (false, true) => fneg += 1,
            (false, false) => tn += 1,
        }
    }
    let denominator = 2 * tp + fp + fneg;
    SweepRow {
        threshold,
        tp,
        fp,
        r#fn: fneg,
        tn,
        accuracy: (tp + tn) as f64 / points.len() as f64,
        precision: (tp + fp > 0).then(|| tp as f64 / (tp + fp) as f64),
        recall: (tp + fneg > 0).then(|| tp as f64 / (tp + fneg) as f64),
        f1: if denominator == 0 {
            0.0
        } else {
            2.0 * tp as f64 / denominator as f64
        },
    }
}

fn choice_report(name: &str, question: &Choice, rows: &[&Scored<'_>]) -> QuestionReport {
    let options: Vec<String> = question.criteria.keys().cloned().collect();
    struct Point {
        predicted: String,
        expected: String,
        confidence: f64,
        right: bool,
    }
    let points: Vec<Point> = rows
        .iter()
        .map(|row| {
            let (predicted, confidence) = match row.answer(name) {
                Some(Answer::Choice(a)) => (a.choice.clone(), a.confidence),
                _ => (String::new(), 0.0),
            };
            let expected = match row.expects(name) {
                Some(Expectation::Choice { label }) => label.clone(),
                _ => String::new(),
            };
            Point {
                right: predicted == expected,
                predicted,
                expected,
                confidence,
            }
        })
        .collect();

    // A label the page never offered still has to land somewhere, or the matrix loses cases.
    let other = points
        .iter()
        .any(|point| !options.contains(&point.predicted));
    let mut labels = options.clone();
    if other {
        labels.push("other".to_owned());
    }
    let confusion: Vec<Vec<usize>> = options
        .iter()
        .map(|expected| {
            labels
                .iter()
                .enumerate()
                .map(|(column, predicted)| {
                    points
                        .iter()
                        .filter(|point| {
                            &point.expected == expected
                                && if other && column == labels.len() - 1 {
                                    !options.contains(&point.predicted)
                                } else {
                                    &point.predicted == predicted
                                }
                        })
                        .count()
                })
                .collect()
        })
        .collect();

    QuestionReport::Choice {
        name: name.to_owned(),
        cases: points.len(),
        accuracy: mean(points.iter().map(|point| f64::from(point.right))),
        labels,
        confusion,
        gate: gate(points.iter().map(|point| (point.confidence, point.right))),
    }
}

fn score_report(name: &str, rows: &[&Scored<'_>]) -> QuestionReport {
    let points: Vec<(f64, i64)> = rows
        .iter()
        .map(|row| {
            let (level, confidence) = match row.answer(name) {
                Some(Answer::Score(a)) => (i64::from(a.rounded_level()), a.confidence),
                _ => (0, 0.0),
            };
            let expected = match row.expects(name) {
                Some(Expectation::Score { level }) => *level as i64,
                _ => 0,
            };
            (confidence, (level - expected).abs())
        })
        .collect();
    QuestionReport::Score {
        name: name.to_owned(),
        cases: points.len(),
        exact: mean(points.iter().map(|(_, off)| f64::from(*off == 0))),
        within_one: mean(points.iter().map(|(_, off)| f64::from(*off <= 1))),
        mae: mean(points.iter().map(|(_, off)| *off as f64)),
        gate: gate(points.iter().map(|(c, off)| (*c, *off == 0))),
    }
}

/// Coverage and accuracy at each cut: what you buy by only acting on confident answers.
fn gate(points: impl Iterator<Item = (f64, bool)>) -> Vec<GateRow> {
    gate_at(points, &CUTS)
}

/// The gate at any set of cuts: the report reads five, calibration twenty.
fn gate_at(points: impl Iterator<Item = (f64, bool)>, cuts: &[f64]) -> Vec<GateRow> {
    let points: Vec<(f64, bool)> = points.collect();
    cuts.iter()
        .map(|confidence| {
            let kept: Vec<bool> = points
                .iter()
                .filter(|(c, _)| c >= confidence)
                .map(|(_, right)| *right)
                .collect();
            GateRow {
                confidence: *confidence,
                coverage: if points.is_empty() {
                    0.0
                } else {
                    kept.len() as f64 / points.len() as f64
                },
                accuracy: (!kept.is_empty())
                    .then(|| mean(kept.iter().map(|right| f64::from(*right)))),
            }
        })
        .collect()
}

/// Counted tokens when every answered case carried them; the estimate, marked as one, otherwise.
fn usage_of(
    session: &Session,
    cases: &[Case],
    scored: &[Scored<'_>],
    model: &str,
    rates: Option<Rates>,
) -> ReportUsage {
    let mut input_tokens = 0u64;
    let mut output_tokens = 0u64;
    let mut counted = !scored.is_empty();
    for one in scored {
        match one
            .usage
            .as_ref()
            .map(|u| (u.input_tokens, u.output_tokens))
        {
            Some((Some(input), Some(output))) => {
                input_tokens += input;
                output_tokens += output;
            }
            _ => {
                counted = false;
                break;
            }
        }
    }
    if !counted {
        let estimate = preflight(session, cases, model, None);
        input_tokens = estimate.input_tokens as u64;
        output_tokens = estimate.output_tokens as u64;
    }
    ReportUsage {
        input_tokens,
        output_tokens,
        estimated: !counted,
        cost: rates.map(|rates| cost::price(input_tokens, output_tokens, rates)),
    }
}

/// What a whole run would send, before any of it is sent.
#[derive(Debug, Clone, PartialEq)]
pub struct Preflight {
    pub cases: usize,
    pub input_tokens: usize,
    pub output_tokens: usize,
    pub cost: Option<Cost>,
}

/// The preflight estimate: tokens summed over every case, priced when rates are known.
pub fn preflight(
    session: &Session,
    cases: &[Case],
    model: &str,
    rates: Option<Rates>,
) -> Preflight {
    let mut input_tokens = 0usize;
    let mut output_tokens = 0usize;
    for one in cases {
        let estimate = cost::estimate(&with_state(session, one.state.clone()), model);
        input_tokens += estimate.input_tokens;
        output_tokens += estimate.output_tokens;
    }
    Preflight {
        cases: cases.len(),
        input_tokens,
        output_tokens,
        cost: rates.map(|rates| cost::price(input_tokens as u64, output_tokens as u64, rates)),
    }
}

fn mean(values: impl Iterator<Item = f64>) -> f64 {
    let mut sum = 0.0;
    let mut count = 0usize;
    for value in values {
        sum += value;
        count += 1;
    }
    if count == 0 { 0.0 } else { sum / count as f64 }
}

/// Two decimals, the way the TypeScript port's `toFixed(2)` writes them.
///
/// Rust rounds an exact tie to even, so `0.125` would print `0.12` here and `0.13` there; every
/// rate and probability in a report goes through this so the two ports' reports can be compared
/// byte for byte.
pub fn two(x: f64) -> String {
    to_fixed(x, 2)
}

/// Three decimals, the way `toFixed(3)` writes them; see [`two`].
pub fn three(x: f64) -> String {
    to_fixed(x, 3)
}

/// `Number.prototype.toFixed`: the double's exact decimal value, rounded to `digits` places with an
/// exact tie going away from zero.
///
/// Scaling by a power of ten and rounding is not the same thing — `0.475 * 100` lands on `47.5`
/// although `0.475` is a hair below it, so it would print `0.48` where `toFixed` prints `0.47`.
/// Rust's `{:.N}` is already exact and differs only on a true tie, which is the one case handled
/// by hand.
fn to_fixed(x: f64, digits: usize) -> String {
    if !x.is_finite() {
        return format!("{x}");
    }
    // Wide enough to hold every digit of any double's exact expansion.
    let exact = format!("{:.1100}", x.abs());
    let point = exact.find('.').unwrap_or(exact.len());
    let tail = exact.get(point + 1 + digits..).unwrap_or("");
    let tie = tail.starts_with('5') && tail[1..].bytes().all(|b| b == b'0');
    let magnitude = if tie {
        let mut kept: Vec<u8> = exact[..point + 1 + digits].bytes().collect();
        let mut at = kept.len();
        loop {
            if at == 0 {
                kept.insert(0, b'1');
                break;
            }
            at -= 1;
            match kept[at] {
                b'.' => continue,
                b'9' => kept[at] = b'0',
                digit => {
                    kept[at] = digit + 1;
                    break;
                }
            }
        }
        let mut text = String::from_utf8(kept).unwrap_or_default();
        if digits == 0 {
            text.pop();
        }
        text
    } else {
        format!("{:.digits$}", x.abs())
    };
    if x < 0.0 {
        format!("-{magnitude}")
    } else {
        magnitude
    }
}

/// The text report, as lines the terminal draws.
///
/// One block per question, in the page's order: what it scored, the sweep or the gate that says
/// where to set the dial, and — for a choice — the matrix that says what it confuses with what.
pub fn report_lines(report: &Report) -> Vec<Line<'static>> {
    let mut out: Vec<Line<'static>> = Vec::new();
    let width = report
        .questions
        .iter()
        .map(|q| q.name().chars().count())
        .max()
        .unwrap_or(0);
    for question in &report.questions {
        if !out.is_empty() {
            out.push(Line::default());
        }
        out.push(header_line(question, width));
        match question {
            QuestionReport::Noul {
                sweep,
                best,
                threshold,
                latency,
                ..
            } => {
                out.extend(sweep_lines(sweep, *best, *threshold));
                if let Some(latency) = latency {
                    out.push(latency_line(latency));
                }
            }
            QuestionReport::Choice {
                gate,
                labels,
                confusion,
                ..
            } => {
                out.extend(gate_lines(gate, "accuracy"));
                out.extend(confusion_lines(labels, confusion));
            }
            QuestionReport::Score { gate, .. } => out.extend(gate_lines(gate, "exact")),
        }
    }

    if !report.errors.is_empty() {
        if !out.is_empty() {
            out.push(Line::default());
        }
        for failed in &report.errors {
            out.extend(error_case_lines(failed, ""));
        }
    }

    if !out.is_empty() {
        out.push(Line::default());
    }
    let errors = report.errors.len();
    out.push(Line::from(vec![
        Span::raw("  "),
        bold(format!("{} case{}", report.cases, plural(report.cases))),
        dim(format!(
            " · {} answered · {errors} error{}",
            report.answered,
            plural(errors)
        )),
    ]));
    out.push(usage_line(&report.usage));
    out
}

fn header_line(question: &QuestionReport, width: usize) -> Line<'static> {
    let count = format!("{} case{}", question.cases(), plural(question.cases()));
    let summary = match question {
        QuestionReport::Noul { brier, .. } => format!("{count} · Brier {}", two(*brier)),
        QuestionReport::Choice { accuracy, .. } => format!("{count} · accuracy {}", two(*accuracy)),
        QuestionReport::Score {
            exact,
            within_one,
            mae,
            ..
        } => format!(
            "{count} · exact {} · within one {} · mae {}",
            two(*exact),
            two(*within_one),
            two(*mae)
        ),
    };
    Line::from(vec![
        Span::raw("  "),
        bold(pad_end(question.name(), width)),
        Span::raw("  "),
        Span::styled(
            pad_end(question.kind(), 8),
            Style::new().fg(color_for(question.kind())),
        ),
        dim(summary),
    ])
}

/// The sweep: what the threshold buys, row by row, with a `*` on the one this run used.
fn sweep_lines(sweep: &[SweepRow], best: Best, threshold: f64) -> Vec<Line<'static>> {
    let mut out = vec![Line::from(vec![
        Span::raw("    "),
        dim(pad_end("threshold", 12)),
        dim(pad_end("acc", 6)),
        dim(pad_end("prec", 7)),
        dim(pad_end("rec", 7)),
        dim("f1"),
    ])];
    for row in sweep {
        let chosen = row.threshold == threshold;
        let at = pad_end(
            &format!("{}{}", two(row.threshold), if chosen { " *" } else { "" }),
            12,
        );
        out.push(Line::from(vec![
            Span::raw("    "),
            if chosen { bold(at) } else { Span::raw(at) },
            Span::raw(pad_end(&two(row.accuracy), 6)),
            Span::raw(pad_end(&rate(row.precision), 7)),
            Span::raw(pad_end(&rate(row.recall), 7)),
            Span::raw(two(row.f1)),
        ]));
    }
    out.push(Line::from(vec![
        Span::raw("    "),
        dim(format!("best f1 at {}", two(best.threshold))),
    ]));
    out
}

/// The one line that says when a noul noticed, over the conversations labelled per turn.
fn latency_line(latency: &Latency) -> Line<'static> {
    let counted = |n: usize, word: &str| format!("{n} {word}{}", plural(n));
    let mean = match latency.mean {
        None => "·".to_owned(),
        Some(m) => format!(
            "{} turn{}",
            signed(m),
            if m.abs() == 1.0 { "" } else { "s" }
        ),
    };
    Line::from(vec![
        Span::raw("    "),
        dim("by turn  "),
        Span::raw(
            [
                counted(latency.threads, "thread"),
                format!("{} on time", latency.on_time),
                format!("{} early", latency.early),
                format!("{} late", latency.late),
                format!("{} missed", latency.missed),
                counted(latency.false_alarms, "false alarm"),
                format!("mean latency {mean}"),
            ]
            .join(" · "),
        ),
    ])
}

fn gate_lines(gate: &[GateRow], accuracy: &str) -> Vec<Line<'static>> {
    let mut out = vec![Line::from(vec![
        Span::raw("    "),
        dim(pad_end("confidence ≥", 15)),
        dim(pad_end("coverage", 10)),
        dim(accuracy.to_owned()),
    ])];
    for row in gate {
        out.push(Line::from(vec![
            Span::raw("    "),
            Span::raw(pad_end(&two(row.confidence), 15)),
            Span::raw(pad_end(&two(row.coverage), 10)),
            Span::raw(rate(row.accuracy)),
        ]));
    }
    out
}

/// The matrix, which is where a rubric's real confusions show: what it calls what.
fn confusion_lines(labels: &[String], confusion: &[Vec<usize>]) -> Vec<Line<'static>> {
    let counts: Vec<usize> = confusion
        .iter()
        .flatten()
        .map(|n| n.to_string().len())
        .collect();
    let column = |label: &str| -> usize {
        counts
            .iter()
            .copied()
            .chain([label.chars().count(), 1])
            .max()
            .unwrap_or(1)
            + 2
    };
    let row_width = confusion
        .iter()
        .enumerate()
        .map(|(at, _)| labels[at].chars().count())
        .max()
        .unwrap_or(0)
        + 3;

    let heading: String = labels
        .iter()
        .map(|label| pad_end(label, column(label)))
        .collect();
    let mut out = vec![
        Line::from(vec![
            Span::raw("    "),
            dim("confusion, rows expected, columns predicted"),
        ]),
        Line::from(vec![
            Span::raw(format!("    {}", " ".repeat(row_width))),
            dim(heading.trim_end().to_owned()),
        ]),
    ];
    for (at, row) in confusion.iter().enumerate() {
        let cells: String = row
            .iter()
            .enumerate()
            .map(|(column2, count)| pad_end(&count.to_string(), column(&labels[column2])))
            .collect();
        out.push(Line::from(vec![
            Span::raw("    "),
            Span::styled(pad_end(&labels[at], row_width), Style::new().fg(CHOICE)),
            Span::raw(cells.trim_end().to_owned()),
        ]));
    }
    out
}

fn error_case_lines(failed: &CaseError, prefix: &str) -> Vec<Line<'static>> {
    let name = format!(
        "{prefix}{}",
        case_name(failed.case, failed.id.as_deref(), failed.turn)
    );
    let mut parts = failed.message.split('\n');
    let first = parts.next().unwrap_or("").trim().to_owned();
    let mut out = vec![Line::from(vec![
        Span::raw("  "),
        Span::styled(format!("{name}: "), Style::new().fg(BAD)),
        Span::raw(first),
    ])];
    for more in parts {
        out.push(Line::from(vec![
            Span::raw("    "),
            dim(more.trim().to_owned()),
        ]));
    }
    out
}

fn usage_line(usage: &ReportUsage) -> Line<'static> {
    let money = match usage.cost {
        Some(cost) => format!(" · {}", cost::usd(cost.total)),
        None => String::new(),
    };
    let tokens = format!(
        "{} in / {} out tokens{money}",
        usage.input_tokens, usage.output_tokens
    );
    if usage.estimated {
        Line::from(vec![
            Span::raw("  "),
            dim(format!("≈ {tokens} — estimated, nothing was counted")),
        ])
    } else {
        Line::from(vec![Span::raw("  "), dim(tokens)])
    }
}

/// The JSON report, ready for `to_string_pretty`. Numbers keep their precision; what is undefined
/// is null.
pub fn report_json(report: &Report) -> Value {
    let mut questions = serde_json::Map::new();
    for question in &report.questions {
        questions.insert(question.name().to_owned(), question_json(question));
    }
    let mut usage = serde_json::Map::new();
    usage.insert("inputTokens".to_owned(), json!(report.usage.input_tokens));
    usage.insert("outputTokens".to_owned(), json!(report.usage.output_tokens));
    usage.insert("estimated".to_owned(), json!(report.usage.estimated));
    if let Some(cost) = report.usage.cost {
        usage.insert("cost".to_owned(), number(cost.total));
    }
    let errors: Vec<Value> = report
        .errors
        .iter()
        .map(|failed| {
            let mut out = serde_json::Map::new();
            out.insert("case".to_owned(), json!(failed.case));
            if let Some(turn) = failed.turn {
                out.insert("turn".to_owned(), json!(turn));
            }
            if let Some(id) = &failed.id {
                out.insert("id".to_owned(), json!(id));
            }
            out.insert("message".to_owned(), json!(failed.message));
            Value::Object(out)
        })
        .collect();
    json!({
        "model": report.model,
        "threshold": number(report.threshold),
        "cases": report.cases,
        "answered": report.answered,
        "errors": errors,
        "questions": Value::Object(questions),
        "usage": Value::Object(usage),
    })
}

fn question_json(question: &QuestionReport) -> Value {
    match question {
        QuestionReport::Noul {
            cases,
            brier,
            threshold,
            accuracy,
            best,
            sweep,
            latency,
            ..
        } => {
            let mut out = json!({
            "kind": question.kind(),
            "cases": cases,
            "brier": number(*brier),
            "threshold": number(*threshold),
            "accuracy": number(*accuracy),
            "best": {"threshold": number(best.threshold), "f1": number(best.f1)},
            "sweep": sweep.iter().map(|row| json!({
                "threshold": number(row.threshold),
                "tp": row.tp,
                "fp": row.fp,
                "fn": row.r#fn,
                "tn": row.tn,
                "accuracy": number(row.accuracy),
                "precision": maybe(row.precision),
                "recall": maybe(row.recall),
                "f1": number(row.f1),
            })).collect::<Vec<_>>(),
            });
            if let (Some(latency), Value::Object(object)) = (latency, &mut out) {
                object.insert("latency".to_owned(), latency_json(latency));
            }
            out
        }
        QuestionReport::Choice {
            cases,
            accuracy,
            labels,
            confusion,
            gate,
            ..
        } => json!({
            "kind": question.kind(),
            "cases": cases,
            "accuracy": number(*accuracy),
            "labels": labels,
            "confusion": confusion,
            "gate": gate.iter().map(gate_json).collect::<Vec<_>>(),
        }),
        QuestionReport::Score {
            cases,
            exact,
            within_one,
            mae,
            gate,
            ..
        } => json!({
            "kind": question.kind(),
            "cases": cases,
            "exact": number(*exact),
            "withinOne": number(*within_one),
            "mae": number(*mae),
            "gate": gate.iter().map(gate_json).collect::<Vec<_>>(),
        }),
    }
}

fn latency_json(latency: &Latency) -> Value {
    let cases: Vec<Value> = latency
        .cases
        .iter()
        .map(|one| {
            let mut out = serde_json::Map::new();
            out.insert("case".to_owned(), json!(one.case));
            if let Some(id) = &one.id {
                out.insert("id".to_owned(), json!(id));
            }
            out.insert("expected".to_owned(), json!(one.expected));
            out.insert("detected".to_owned(), json!(one.detected));
            out.insert("latency".to_owned(), json!(one.latency));
            Value::Object(out)
        })
        .collect();
    json!({
        "threads": latency.threads,
        "onTime": latency.on_time,
        "early": latency.early,
        "late": latency.late,
        "missed": latency.missed,
        "falseAlarms": latency.false_alarms,
        "mean": maybe(latency.mean),
        "cases": cases,
    })
}

fn gate_json(row: &GateRow) -> Value {
    json!({
        "confidence": number(row.confidence),
        "coverage": number(row.coverage),
        "accuracy": maybe(row.accuracy),
    })
}

/// A number written the way `JSON.stringify` writes it: a whole float loses its `.0`, so the two
/// ports' JSON reports can be compared byte for byte the way their tables can.
fn number(x: f64) -> Value {
    if x.fract() == 0.0 && x.abs() < 9e15 {
        return json!(x as i64);
    }
    json!(x)
}

/// The same, for a rate that was never defined: `null`, so the key is always there.
fn maybe(x: Option<f64>) -> Value {
    x.map_or(Value::Null, number)
}

/// A rate that was never defined is a dot, not a zero: nothing was measured.
fn rate(n: Option<f64>) -> String {
    match n {
        Some(n) => two(n),
        None => "·".to_owned(),
    }
}

fn plural(n: usize) -> &'static str {
    if n == 1 { "" } else { "s" }
}

fn pad_end(text: &str, width: usize) -> String {
    let length = text.chars().count();
    if length >= width {
        text.to_owned()
    } else {
        format!("{text}{}", " ".repeat(width - length))
    }
}

/// Styled lines as the plain text a pipe wants.
pub fn report_text(report: &Report) -> String {
    lines_text(report_lines(report))
}

fn lines_text(lines: Vec<Line<'static>>) -> String {
    let mut out = String::new();
    for line in lines {
        for span in &line.spans {
            out.push_str(span.content.as_ref());
        }
        out.push('\n');
    }
    out
}

// ---- two pages over the same cases ------------------------------------------------------------

/// One page's run, as a comparison needs it.
#[derive(Debug, Clone, Copy)]
pub struct Side<'a> {
    /// What the page is called: the path it was read from.
    pub label: &'a str,
    pub session: &'a Session,
    pub cases: &'a [Case],
    pub outcomes: &'a [Outcome],
    pub model: &'a str,
}

/// What the exact McNemar test made of the discordant pairs.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Verdict {
    TooFew,
    Better,
    Worse,
    Same,
}

impl Verdict {
    /// The word the report and the JSON use.
    pub fn as_str(self) -> &'static str {
        match self {
            Verdict::TooFew => "too few",
            Verdict::Better => "better",
            Verdict::Worse => "worse",
            Verdict::Same => "same",
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub struct McNemar {
    /// Cases one page got right and the other wrong: `fixed + broke`.
    pub discordant: usize,
    /// Two-sided exact p-value; 1 when there is nothing to test.
    pub p: f64,
    pub verdict: Verdict,
}

/// A case the two pages answered differently.
#[derive(Debug, Clone, PartialEq)]
pub struct Flip {
    pub case: usize,
    pub turn: Option<usize>,
    pub id: Option<String>,
    /// What the case expects, and what each page predicted, in the question's own terms.
    pub expected: Value,
    pub a: Value,
    pub b: Value,
    /// `fixed`: `b` put right what `a` got wrong. `broke`: the reverse. `changed`: both wrong.
    pub status: &'static str,
}

/// One metric on both sides, and what moved.
#[derive(Debug, Clone, PartialEq)]
pub struct Metric {
    /// The JSON key.
    pub key: &'static str,
    /// The row name in the text report.
    pub label: &'static str,
    pub a: f64,
    pub b: f64,
    /// Whether a delta means anything: a threshold is a setting, not a result.
    pub delta: bool,
}

/// A question both pages ask the same way, measured on the cases both pages scored.
#[derive(Debug, Clone, PartialEq)]
pub struct Shared {
    pub name: String,
    pub kind: &'static str,
    pub paired: usize,
    pub metrics: Vec<Metric>,
    pub fixed: usize,
    pub broke: usize,
    pub changed: usize,
    pub mcnemar: McNemar,
    pub flips: Vec<Flip>,
}

/// A name both pages use for questions of different kinds.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Mismatch {
    pub name: String,
    pub a: &'static str,
    pub b: &'static str,
}

/// One page's report, with the name it goes by.
#[derive(Debug, Clone, PartialEq)]
pub struct Labelled {
    pub label: String,
    pub report: Report,
}

/// Everything a comparison found, with the numbers unrounded.
#[derive(Debug, Clone, PartialEq)]
pub struct Comparison {
    pub a: Labelled,
    pub b: Labelled,
    pub questions: Vec<Shared>,
    pub only_a: Vec<String>,
    pub only_b: Vec<String>,
    pub mismatched: Vec<Mismatch>,
    pub unpaired: Vec<String>,
    /// Distinct cases across both pages.
    pub cases: usize,
    pub usage: ReportUsage,
}

/// What a comparison is read at, which both reports repeat back.
#[derive(Debug, Clone, Copy)]
pub struct CompareOptions {
    pub threshold: f64,
    pub rates: Option<Rates>,
}

/// The level McNemar's test is read at. Not a flag: a comparison should mean the same everywhere.
pub const ALPHA: f64 = 0.05;

/// Below this many discordant pairs no two-sided exact p can reach [`ALPHA`]: 2 / 2^5 > 0.05.
pub const MIN_DISCORDANT: usize = 6;

/// The exact McNemar test on the discordant pairs of a paired comparison.
///
/// Under "no difference" each discordant pair is a fair coin, so the p-value is a binomial tail.
/// It is summed in log space because `2^n` stops being a number long before a cases file stops
/// being a reasonable size.
pub fn mcnemar(fixed: usize, broke: usize) -> McNemar {
    let n = fixed + broke;
    let mut p = 1.0;
    if n > 0 {
        let low = fixed.min(broke);
        let ln2n = n as f64 * std::f64::consts::LN_2;
        let mut ln_choose = 0.0;
        let mut tail = (-ln2n).exp();
        for k in 1..=low {
            ln_choose += ((n - k + 1) as f64).ln() - (k as f64).ln();
            tail += (ln_choose - ln2n).exp();
        }
        p = (2.0 * tail).min(1.0);
    }
    let verdict = if n < MIN_DISCORDANT {
        Verdict::TooFew
    } else if p < ALPHA && fixed > broke {
        Verdict::Better
    } else if p < ALPHA && broke > fixed {
        Verdict::Worse
    } else {
        Verdict::Same
    };
    McNemar {
        discordant: n,
        p,
        verdict,
    }
}

/// The wire `type` of a question, with anything that is not a noul, a choice or a score as `raw`.
fn kind_of(question: &Question) -> &'static str {
    match question {
        Question::Noul(_) => "noul",
        Question::Choice(_) => "choice",
        Question::Score(_) => "score",
        _ => "raw",
    }
}

/// Put two runs of the same cases side by side.
///
/// Only the cases both pages scored count, so each delta is measured on the same states: a page
/// that errored on the hard cases must not look better for it. The full reports, over everything
/// each page scored, travel along for the JSON.
pub fn compare(a: Side<'_>, b: Side<'_>, options: CompareOptions) -> Comparison {
    let report_of = |side: &Side<'_>| {
        report(
            side.session,
            side.cases,
            side.outcomes,
            ReportOptions {
                model: side.model,
                threshold: options.threshold,
                rates: options.rates,
            },
        )
    };
    let (_, left) = scored_of(a.cases, a.outcomes);
    let (_, right) = scored_of(b.cases, b.outcomes);
    let right: HashMap<(usize, Option<usize>), &Scored<'_>> =
        right.iter().map(|one| (key_of(one), one)).collect();
    let twin_of = |one: &Scored<'_>| right.get(&key_of(one)).copied();

    let kind_in_b = |name: &str| question_of(b.session, name).map(kind_of);
    let mut questions = Vec::new();
    let mut mismatched = Vec::new();
    let mut unpaired = Vec::new();
    for (name, question) in &a.session.questions {
        let Some(other) = kind_in_b(name) else {
            continue;
        };
        let kind = kind_of(question);
        if other != kind || kind == "raw" {
            if other != kind {
                mismatched.push(Mismatch {
                    name: name.clone(),
                    a: kind,
                    b: other,
                });
            }
            continue;
        }
        let pairs: Vec<(&Scored<'_>, &Scored<'_>)> = left
            .iter()
            .filter_map(|one| {
                let twin = twin_of(one)?;
                (one.expects(name).is_some() && twin.expects(name).is_some()).then_some((one, twin))
            })
            .collect();
        if pairs.is_empty() {
            // A question nobody labelled is left out, as eval leaves it out; one that was labelled
            // and still has no pair is worth saying so about.
            let labelled = a
                .cases
                .iter()
                .chain(b.cases)
                .any(|one| one.expect.iter().any(|(n, _)| n == name));
            if labelled {
                unpaired.push(name.clone());
            }
            continue;
        }
        questions.push(shared(
            name,
            kind,
            &pairs,
            a.session.threshold_of(name, options.threshold),
            b.session.threshold_of(name, options.threshold),
        ));
    }

    let mut keys: Vec<(usize, Option<usize>)> =
        a.cases.iter().chain(b.cases).map(case_key).collect();
    keys.sort_unstable();
    keys.dedup();
    let report_a = report_of(&a);
    let report_b = report_of(&b);
    let usage = sum_usage(&report_a.usage, &report_b.usage, options.rates);
    Comparison {
        a: Labelled {
            label: a.label.to_owned(),
            report: report_a,
        },
        b: Labelled {
            label: b.label.to_owned(),
            report: report_b,
        },
        questions,
        only_a: a
            .session
            .questions
            .iter()
            .filter(|(name, _)| question_of(b.session, name).is_none())
            .map(|(name, _)| name.clone())
            .collect(),
        only_b: b
            .session
            .questions
            .iter()
            .filter(|(name, _)| question_of(a.session, name).is_none())
            .map(|(name, _)| name.clone())
            .collect(),
        mismatched,
        unpaired,
        cases: keys.len(),
        usage,
    }
}

/// Which case a scored row came from, so the same case can be found on the other page.
fn key_of(one: &Scored<'_>) -> (usize, Option<usize>) {
    (one.line, one.turn)
}

/// The same key, for a case that has not been scored.
fn case_key(one: &Case) -> (usize, Option<usize>) {
    (one.line, one.turn)
}

fn sum_usage(a: &ReportUsage, b: &ReportUsage, rates: Option<Rates>) -> ReportUsage {
    let input_tokens = a.input_tokens + b.input_tokens;
    let output_tokens = a.output_tokens + b.output_tokens;
    ReportUsage {
        input_tokens,
        output_tokens,
        estimated: a.estimated || b.estimated,
        cost: rates.map(|rates| cost::price(input_tokens, output_tokens, rates)),
    }
}

/// One paired observation: what each side predicted, and whether it was right.
struct Pair<'a> {
    one: &'a Scored<'a>,
    expected: Value,
    a: Value,
    b: Value,
    right_a: bool,
    right_b: bool,
}

fn shared(
    name: &str,
    kind: &'static str,
    pairs: &[(&Scored<'_>, &Scored<'_>)],
    threshold_a: f64,
    threshold_b: f64,
) -> Shared {
    let noul_of = |row: &Scored<'_>| match row.answer(name) {
        Some(Answer::Noul(answer)) => answer.noul,
        _ => 0.0,
    };
    let yes_of =
        |row: &Scored<'_>| matches!(row.expects(name), Some(Expectation::Noul { yes: true, .. }));
    let choice_of = |row: &Scored<'_>| match row.answer(name) {
        Some(Answer::Choice(answer)) => answer.choice.clone(),
        _ => String::new(),
    };
    let level_of = |row: &Scored<'_>| match row.answer(name) {
        Some(Answer::Score(answer)) => answer.rounded_level() as usize,
        _ => 0,
    };
    let (metrics, observed): (Vec<Metric>, Vec<Pair<'_>>) = match kind {
        "noul" => {
            let left: Vec<(f64, bool)> = pairs
                .iter()
                .map(|(one, _)| (noul_of(one), yes_of(one)))
                .collect();
            let right: Vec<(f64, bool)> = pairs
                .iter()
                .map(|(_, twin)| (noul_of(twin), yes_of(twin)))
                .collect();
            let brier = |list: &[(f64, bool)]| {
                mean(
                    list.iter()
                        .map(|(p, yes)| (p - if *yes { 1.0 } else { 0.0 }).powi(2)),
                )
            };
            let row_a = sweep_row(&left, threshold_a);
            let row_b = sweep_row(&right, threshold_b);
            let metrics = vec![
                metric("threshold", "threshold", threshold_a, threshold_b, false),
                metric("brier", "brier", brier(&left), brier(&right), true),
                metric("accuracy", "accuracy", row_a.accuracy, row_b.accuracy, true),
                metric("f1", "f1", row_a.f1, row_b.f1, true),
            ];
            let observed = pairs
                .iter()
                .zip(left.iter().zip(&right))
                .map(|((one, _), ((pa, yes), (pb, _)))| {
                    let (pred_a, pred_b) = (*pa >= threshold_a, *pb >= threshold_b);
                    Pair {
                        one,
                        expected: Value::Bool(*yes),
                        a: Value::Bool(pred_a),
                        b: Value::Bool(pred_b),
                        right_a: pred_a == *yes,
                        right_b: pred_b == *yes,
                    }
                })
                .collect();
            (metrics, observed)
        }
        "choice" => {
            let observed: Vec<Pair<'_>> = pairs
                .iter()
                .map(|(one, twin)| {
                    let expected = match one.expects(name) {
                        Some(Expectation::Choice { label }) => label.clone(),
                        _ => String::new(),
                    };
                    let (a, b) = (choice_of(one), choice_of(twin));
                    Pair {
                        one,
                        right_a: a == expected,
                        right_b: b == expected,
                        expected: Value::String(expected),
                        a: Value::String(a),
                        b: Value::String(b),
                    }
                })
                .collect();
            let metrics = vec![metric(
                "accuracy",
                "accuracy",
                mean(observed.iter().map(|pair| f64::from(pair.right_a))),
                mean(observed.iter().map(|pair| f64::from(pair.right_b))),
                true,
            )];
            (metrics, observed)
        }
        _ => {
            let mut offs: Vec<(usize, usize)> = Vec::new();
            let observed: Vec<Pair<'_>> = pairs
                .iter()
                .map(|(one, twin)| {
                    let expected = match one.expects(name) {
                        Some(Expectation::Score { level }) => *level,
                        _ => 0,
                    };
                    let (a, b) = (level_of(one), level_of(twin));
                    offs.push((a.abs_diff(expected), b.abs_diff(expected)));
                    Pair {
                        one,
                        expected: json!(expected),
                        a: json!(a),
                        b: json!(b),
                        right_a: a == expected,
                        right_b: b == expected,
                    }
                })
                .collect();
            let both = |f: &dyn Fn(usize) -> f64| {
                (
                    mean(offs.iter().map(|(a, _)| f(*a))),
                    mean(offs.iter().map(|(_, b)| f(*b))),
                )
            };
            let (exact_a, exact_b) = both(&|off| f64::from(off == 0));
            let (within_a, within_b) = both(&|off| f64::from(off <= 1));
            let (mae_a, mae_b) = both(&|off| off as f64);
            let metrics = vec![
                metric("exact", "exact", exact_a, exact_b, true),
                metric("withinOne", "within one", within_a, within_b, true),
                metric("mae", "mae", mae_a, mae_b, true),
            ];
            (metrics, observed)
        }
    };

    let mut flips = Vec::new();
    let (mut fixed, mut broke, mut changed) = (0, 0, 0);
    for pair in observed {
        if pair.a == pair.b {
            continue;
        }
        let status = if !pair.right_a && pair.right_b {
            fixed += 1;
            "fixed"
        } else if pair.right_a {
            broke += 1;
            "broke"
        } else {
            changed += 1;
            "changed"
        };
        flips.push(Flip {
            case: pair.one.line,
            turn: pair.one.turn,
            id: pair.one.id.map(str::to_owned),
            expected: pair.expected,
            a: pair.a,
            b: pair.b,
            status,
        });
    }
    Shared {
        name: name.to_owned(),
        kind,
        paired: pairs.len(),
        metrics,
        fixed,
        broke,
        changed,
        mcnemar: mcnemar(fixed, broke),
        flips,
    }
}

fn metric(key: &'static str, label: &'static str, a: f64, b: f64, delta: bool) -> Metric {
    Metric {
        key,
        label,
        a,
        b,
        delta,
    }
}

/// The questions `b` is significantly worse at, for `--fail-on-regression`.
pub fn regressions(comparison: &Comparison) -> Vec<&Shared> {
    comparison
        .questions
        .iter()
        .filter(|q| q.mcnemar.verdict == Verdict::Worse)
        .collect()
}

/// How many flipped cases the text report lists per question before it points at the JSON.
const FLIPS_SHOWN: usize = 10;

/// The comparison as lines: a legend, a block per shared question, what could not be compared.
pub fn compare_lines(comparison: &Comparison) -> Vec<Line<'static>> {
    let mut out: Vec<Line<'static>> = Vec::new();
    let sides = [("a", &comparison.a), ("b", &comparison.b)];
    let label_width = sides
        .iter()
        .map(|(_, side)| side.label.chars().count())
        .max()
        .unwrap_or(0);
    for (letter, side) in sides {
        let n = side.report.cases;
        out.push(Line::from(vec![
            Span::raw("  "),
            bold(letter),
            Span::raw("  "),
            Span::raw(pad_end(&side.label, label_width)),
            Span::raw("  "),
            dim(format!("{} · {n} case{}", side.report.model, plural(n))),
        ]));
    }

    let width = comparison
        .questions
        .iter()
        .map(|q| q.name.chars().count())
        .max()
        .unwrap_or(0);
    for question in &comparison.questions {
        out.push(Line::default());
        out.extend(shared_lines(question, width));
    }

    let mut lists: Vec<Line<'static>> = Vec::new();
    if !comparison.only_a.is_empty() {
        lists.push(Line::from(vec![
            Span::raw("  "),
            dim("only in a: "),
            Span::raw(comparison.only_a.join(", ")),
        ]));
    }
    if !comparison.only_b.is_empty() {
        lists.push(Line::from(vec![
            Span::raw("  "),
            dim("only in b: "),
            Span::raw(comparison.only_b.join(", ")),
        ]));
    }
    for odd in &comparison.mismatched {
        lists.push(Line::from(vec![
            Span::raw("  "),
            dim("mismatched: "),
            Span::raw(format!(
                "{} is a {} in a and a {} in b",
                odd.name, odd.a, odd.b
            )),
        ]));
    }
    for name in &comparison.unpaired {
        lists.push(Line::from(vec![
            Span::raw("  "),
            dim("unpaired: "),
            Span::raw(format!("{name} — no case was scored for it on both pages")),
        ]));
    }
    if !lists.is_empty() {
        out.push(Line::default());
        out.extend(lists);
    }

    let mut failures: Vec<Line<'static>> = Vec::new();
    for (letter, side) in sides {
        for failed in &side.report.errors {
            failures.extend(error_case_lines(failed, &format!("{letter} ")));
        }
    }
    if !failures.is_empty() {
        out.push(Line::default());
        out.extend(failures);
    }

    out.push(Line::default());
    let tally = |report: &Report| {
        let errors = report.errors.len();
        format!(
            "{} answered, {errors} error{}",
            report.answered,
            plural(errors)
        )
    };
    out.push(Line::from(vec![
        Span::raw("  "),
        bold(format!(
            "{} case{}",
            comparison.cases,
            plural(comparison.cases)
        )),
        dim(format!(
            " · a {} · b {}",
            tally(&comparison.a.report),
            tally(&comparison.b.report)
        )),
    ]));
    out.push(usage_line(&comparison.usage));
    out
}

fn shared_lines(question: &Shared, width: usize) -> Vec<Line<'static>> {
    let n = question.paired;
    let mut out = vec![
        Line::from(vec![
            Span::raw("  "),
            bold(pad_end(&question.name, width)),
            Span::raw("  "),
            Span::styled(
                pad_end(question.kind, 8),
                Style::new().fg(color_for(question.kind)),
            ),
            dim(format!("{n} paired case{}", plural(n))),
        ]),
        Line::from(vec![
            Span::raw(format!("    {}", " ".repeat(14))),
            dim(format!("{}{}Δ", pad_end("a", 8), pad_end("b", 8))),
        ]),
    ];
    for metric in &question.metrics {
        let mut cells = vec![two(metric.a), two(metric.b)];
        if metric.delta {
            cells.push(signed(metric.b - metric.a));
        }
        let cells: String = cells.iter().map(|cell| pad_end(cell, 8)).collect();
        out.push(Line::from(vec![
            Span::raw("    "),
            Span::raw(pad_end(metric.label, 14)),
            Span::raw(cells.trim_end().to_owned()),
        ]));
    }
    out.push(Line::from(vec![
        Span::raw("    "),
        Span::raw(format!(
            "{} fixed · {} broke · {} changed",
            question.fixed, question.broke, question.changed
        )),
    ]));
    let verdict = question.mcnemar.verdict;
    let text = mcnemar_text(&question.mcnemar);
    out.push(Line::from(vec![
        Span::raw("    "),
        match verdict {
            Verdict::Better => Span::styled(text, Style::new().fg(SCORE)),
            Verdict::Worse => Span::styled(text, Style::new().fg(BAD)),
            _ => dim(text),
        },
    ]));

    let shown = &question.flips[..question.flips.len().min(FLIPS_SHOWN)];
    let names: Vec<String> = shown
        .iter()
        .map(|flip| case_name(flip.case, flip.id.as_deref(), flip.turn))
        .collect();
    let moves: Vec<String> = shown
        .iter()
        .map(|flip| {
            format!(
                "{} → {}",
                reading(question.kind, &flip.a),
                reading(question.kind, &flip.b)
            )
        })
        .collect();
    let name_width = names.iter().map(|n| n.chars().count()).max().unwrap_or(0);
    let move_width = moves.iter().map(|m| m.chars().count()).max().unwrap_or(0);
    for ((flip, name), change) in shown.iter().zip(&names).zip(&moves) {
        let color = match flip.status {
            "fixed" => SCORE,
            "broke" => BAD,
            _ => DIM,
        };
        out.push(Line::from(vec![
            Span::raw("    "),
            Span::raw(pad_end(name, name_width)),
            Span::raw("   "),
            Span::raw(pad_end(change, move_width)),
            Span::raw("   "),
            Span::styled(flip.status, Style::new().fg(color)),
        ]));
    }
    let more = question.flips.len() - shown.len();
    if more > 0 {
        out.push(Line::from(vec![
            Span::raw("    "),
            dim(format!("… {more} more flipped; --json lists them all")),
        ]));
    }
    out
}

/// The significance line, which says in words what the p-value allows and what it does not.
fn mcnemar_text(test: &McNemar) -> String {
    if test.discordant == 0 {
        return "McNemar: no discordant pairs, nothing to test".to_owned();
    }
    if test.verdict == Verdict::TooFew {
        return format!(
            "McNemar: too few discordant pairs to call ({}; {MIN_DISCORDANT} are needed for p < {ALPHA})",
            test.discordant
        );
    }
    let head = format!(
        "McNemar p {} over {} discordant pairs: ",
        three(test.p),
        test.discordant
    );
    match test.verdict {
        Verdict::Better => format!("{head}b is significantly better"),
        Verdict::Worse => format!("{head}b is significantly worse"),
        _ => format!("{head}no significant difference"),
    }
}

/// A prediction as the report says it: yes or no, a label, a level.
fn reading(kind: &str, value: &Value) -> String {
    match (kind, value) {
        ("noul", Value::Bool(true)) => "yes".to_owned(),
        ("noul", _) => "no".to_owned(),
        ("score", other) => format!("level {other}"),
        (_, Value::String(label)) => label.clone(),
        (_, other) => other.to_string(),
    }
}

/// `case 7 turn 2 (t-007)`: the line in the cases file, the prefix of a case labelled per turn, and
/// the id when the case has one.
fn case_name(line: usize, id: Option<&str>, turn: Option<usize>) -> String {
    let turn = turn.map(|t| format!(" turn {t}")).unwrap_or_default();
    let id = id.map(|id| format!(" ({id})")).unwrap_or_default();
    format!("case {line}{turn}{id}")
}

/// A change, signed either way, so a regression reads as one; `-0.00` is no change, so `+0.00`.
pub fn signed(n: f64) -> String {
    let text = two(n);
    if text == "-0.00" {
        return "+0.00".to_owned();
    }
    if text.starts_with('-') {
        text
    } else {
        format!("+{text}")
    }
}

/// The comparison as JSON, ready for `to_string_pretty`: both reports whole, and what moved
/// between them.
pub fn compare_json(comparison: &Comparison) -> Value {
    let side = |one: &Labelled| {
        let mut out = serde_json::Map::new();
        out.insert("page".to_owned(), json!(one.label));
        if let Value::Object(report) = report_json(&one.report) {
            out.extend(report);
        }
        Value::Object(out)
    };
    let mut questions = serde_json::Map::new();
    for question in &comparison.questions {
        let pick = |f: &dyn Fn(&Metric) -> Option<f64>| {
            let mut out = serde_json::Map::new();
            for metric in &question.metrics {
                if let Some(value) = f(metric) {
                    out.insert(metric.key.to_owned(), number(value));
                }
            }
            Value::Object(out)
        };
        let flips: Vec<Value> = question
            .flips
            .iter()
            .map(|flip| {
                let mut out = serde_json::Map::new();
                out.insert("case".to_owned(), json!(flip.case));
                if let Some(turn) = flip.turn {
                    out.insert("turn".to_owned(), json!(turn));
                }
                if let Some(id) = &flip.id {
                    out.insert("id".to_owned(), json!(id));
                }
                out.insert("expected".to_owned(), flip.expected.clone());
                out.insert("a".to_owned(), flip.a.clone());
                out.insert("b".to_owned(), flip.b.clone());
                out.insert("status".to_owned(), json!(flip.status));
                Value::Object(out)
            })
            .collect();
        questions.insert(
            question.name.clone(),
            json!({
                "kind": question.kind,
                "paired": question.paired,
                "a": pick(&|m| Some(m.a)),
                "b": pick(&|m| Some(m.b)),
                "delta": pick(&|m| m.delta.then_some(m.b - m.a)),
                "fixed": question.fixed,
                "broke": question.broke,
                "changed": question.changed,
                "mcnemar": {
                    "discordant": question.mcnemar.discordant,
                    "p": number(question.mcnemar.p),
                    "verdict": question.mcnemar.verdict.as_str(),
                },
                "flips": flips,
            }),
        );
    }
    let mut usage = serde_json::Map::new();
    usage.insert(
        "inputTokens".to_owned(),
        json!(comparison.usage.input_tokens),
    );
    usage.insert(
        "outputTokens".to_owned(),
        json!(comparison.usage.output_tokens),
    );
    usage.insert("estimated".to_owned(), json!(comparison.usage.estimated));
    if let Some(cost) = comparison.usage.cost {
        usage.insert("cost".to_owned(), number(cost.total));
    }
    json!({
        "a": side(&comparison.a),
        "b": side(&comparison.b),
        "questions": Value::Object(questions),
        "onlyA": comparison.only_a,
        "onlyB": comparison.only_b,
        "mismatched": comparison.mismatched.iter().map(|odd| json!({
            "name": odd.name,
            "a": odd.a,
            "b": odd.b,
        })).collect::<Vec<_>>(),
        "unpaired": comparison.unpaired,
        "regressions": regressions(comparison).iter().map(|q| q.name.clone()).collect::<Vec<_>>(),
        "usage": Value::Object(usage),
    })
}

/// The comparison as the plain text a pipe wants.
pub fn compare_text(comparison: &Comparison) -> String {
    lines_text(compare_lines(comparison))
}

// ---- writing the bars back --------------------------------------------------------------------

/// The accuracy a choice's or score's bar has to reach when `--target-accuracy` is not given.
pub const DEFAULT_TARGET: f64 = 0.9;

/// The confidence bars calibration tries, `k / 20` for `k` from 0 to 19: finer than the report's
/// gate, and computed by division so each prints as the short decimal it is.
pub fn calibration_cuts() -> Vec<f64> {
    (0..20).map(|k| f64::from(k) / 20.0).collect()
}

/// What calibration made of one question: the bar it found, or why it left the question alone.
#[derive(Debug, Clone, PartialEq)]
pub struct CalibratedQuestion {
    pub name: String,
    pub kind: &'static str,
    /// The new bar; `None` when the question is left alone.
    pub bar: Option<f64>,
    /// The bar the page had before.
    pub was: Option<f64>,
    /// For a noul: the F1 at the new threshold.
    pub f1: Option<f64>,
    /// For a choice or a score: the accuracy over the cases that clear the new bar, and how many
    /// do.
    pub accuracy: Option<f64>,
    pub coverage: Option<f64>,
    /// Why the question was left alone.
    pub reason: Option<String>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct Calibration {
    pub target: f64,
    pub questions: Vec<CalibratedQuestion>,
    /// Only the bars that changed: what `sketch::set_bars` has to write.
    pub changed: Vec<(String, f64)>,
}

/// The bars a run supports, one per scored question.
///
/// A noul gets the threshold with the best F1, which the report has already found. A choice or a
/// score gets the lowest confidence bar at which the answers it lets through are right at least
/// `target` of the time: the lowest, because every step up sends more of the work to a person.
pub fn calibrate(
    session: &Session,
    cases: &[Case],
    outcomes: &[Outcome],
    scored_report: &Report,
    target: f64,
) -> Calibration {
    let (_, scored) = scored_of(cases, outcomes);
    let mut questions = Vec::new();
    let mut changed = Vec::new();
    for question in &scored_report.questions {
        let name = question.name();
        let mut found = CalibratedQuestion {
            name: name.to_owned(),
            kind: question.kind(),
            bar: None,
            was: session.bar(name),
            f1: None,
            accuracy: None,
            coverage: None,
            reason: None,
        };
        if let QuestionReport::Noul { best, .. } = question {
            if best.f1 > 0.0 {
                found.bar = Some(best.threshold);
                found.f1 = Some(best.f1);
            } else {
                found.reason = Some("no threshold gives an F1 above 0".to_owned());
            }
        } else {
            let points = scored.iter().filter_map(|one| {
                let expectation = one.expects(name)?;
                match (one.answer(name)?, expectation) {
                    (Answer::Choice(answer), Expectation::Choice { label }) => {
                        Some((answer.confidence, answer.choice == *label))
                    }
                    (Answer::Score(answer), Expectation::Score { level }) => {
                        Some((answer.confidence, answer.rounded_level() as usize == *level))
                    }
                    _ => None,
                }
            });
            let rows = gate_at(points, &calibration_cuts());
            let reached = rows
                .iter()
                .find(|row| row.accuracy.is_some_and(|accuracy| accuracy >= target));
            match reached {
                Some(row) => {
                    found.bar = Some(row.confidence);
                    found.accuracy = row.accuracy;
                    found.coverage = Some(row.coverage);
                }
                None => {
                    let mut best: Option<&GateRow> = None;
                    for row in &rows {
                        if let Some(accuracy) = row.accuracy
                            && best.is_none_or(|b| accuracy > b.accuracy.unwrap_or(0.0))
                        {
                            best = Some(row);
                        }
                    }
                    found.reason = Some(match best {
                        None => format!("no confidence bar reaches accuracy {}", two(target)),
                        Some(row) => format!(
                            "no confidence bar reaches accuracy {} (best {} at {})",
                            two(target),
                            two(row.accuracy.unwrap_or(0.0)),
                            two(row.confidence)
                        ),
                    });
                }
            }
        }
        if let Some(bar) = found.bar
            && found.was != Some(bar)
        {
            changed.push((found.name.clone(), bar));
        }
        questions.push(found);
    }
    Calibration {
        target,
        questions,
        changed,
    }
}

/// Why a run with errors writes nothing back: the bars would be fitted to the cases that worked.
pub fn not_calibrating(errors: usize) -> String {
    format!(
        "not calibrating: {errors} case{} back with errors, so the numbers are incomplete.",
        if errors == 1 { " came" } else { "s came" }
    )
}

/// The directive a question's bar is written with.
fn directive_of(kind: &str) -> &'static str {
    if kind == "noul" {
        "@threshold"
    } else {
        "@confidence"
    }
}

/// What calibration changed, as lines for under the report: one per question, the new directive as
/// it now reads on the page, what it replaced, and the evidence for it.
pub fn calibration_lines(calibration: &Calibration, page: &str) -> Vec<Line<'static>> {
    let mut out = vec![Line::from(vec![
        Span::raw("  "),
        bold("calibration"),
        dim(format!("  target accuracy {}", two(calibration.target))),
    ])];
    let width = calibration
        .questions
        .iter()
        .map(|q| q.name.chars().count())
        .max()
        .unwrap_or(0);
    for question in &calibration.questions {
        let mut spans = vec![
            Span::raw("    "),
            bold(pad_end(&question.name, width)),
            Span::raw("  "),
        ];
        match question.bar {
            None => {
                spans.push(dim(pad_end("left alone", 19)));
                spans.push(dim(question.reason.clone().unwrap_or_default()));
            }
            Some(bar) => {
                let was = match question.was {
                    Some(was) if was == bar => "unchanged".to_owned(),
                    Some(was) => format!("was {was}"),
                    None => "was none".to_owned(),
                };
                let evidence = if question.kind == "noul" {
                    format!("f1 {}", two(question.f1.unwrap_or(0.0)))
                } else {
                    format!(
                        "accuracy {} over {} of cases",
                        two(question.accuracy.unwrap_or(0.0)),
                        two(question.coverage.unwrap_or(0.0))
                    )
                };
                spans.push(Span::styled(
                    pad_end(&format!("{} {bar}", directive_of(question.kind)), 19),
                    Style::new().fg(color_for(question.kind)),
                ));
                spans.push(dim(pad_end(&was, 11)));
                spans.push(Span::raw(evidence));
            }
        }
        out.push(Line::from(spans));
    }
    let n = calibration.changed.len();
    out.push(Line::from(vec![
        Span::raw("  "),
        if n == 0 {
            dim(format!("nothing to write: {page} already holds these bars"))
        } else {
            Span::raw(format!("wrote {n} bar{} to {page}", plural(n)))
        },
    ]));
    out
}

/// The calibration as plain text, for under the report.
pub fn calibration_text(calibration: &Calibration, page: &str) -> String {
    lines_text(calibration_lines(calibration, page))
}

/// The calibration as JSON, for the report's `calibration` key.
pub fn calibration_json(calibration: &Calibration, page: &str) -> serde_json::Map<String, Value> {
    let mut questions = serde_json::Map::new();
    for question in &calibration.questions {
        let mut out = serde_json::Map::new();
        out.insert("kind".to_owned(), json!(question.kind));
        out.insert("bar".to_owned(), maybe(question.bar));
        out.insert("was".to_owned(), maybe(question.was));
        if let Some(f1) = question.f1 {
            out.insert("f1".to_owned(), number(f1));
        }
        if let Some(accuracy) = question.accuracy {
            out.insert("accuracy".to_owned(), number(accuracy));
        }
        if let Some(coverage) = question.coverage {
            out.insert("coverage".to_owned(), number(coverage));
        }
        if let Some(reason) = &question.reason {
            out.insert("reason".to_owned(), json!(reason));
        }
        questions.insert(question.name.clone(), Value::Object(out));
    }
    let mut out = serde_json::Map::new();
    out.insert("page".to_owned(), json!(page));
    out.insert("target".to_owned(), number(calibration.target));
    out.insert("written".to_owned(), json!(!calibration.changed.is_empty()));
    out.insert("questions".to_owned(), Value::Object(questions));
    out
}