semlith 0.44.0

A fast local vector store for AI agents — index files once, keep it current as you save, and answer questions across all of it in milliseconds without leaving the machine.
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
//! Five reports, generated from this machine's ledger, index and graph.
//!
//! Nothing here reaches the network, nothing here is a model's opinion, and
//! nothing here is a number without its denominator. A report is one typed
//! structure — a lede, a meta strip, KPI figures and a list of blocks (tables,
//! facts, notes, a recompute command) — and five renderers turn that one
//! structure into Markdown, CSV, JSON, HTML and PDF, so the formats cannot
//! disagree about what a report says and `semlith report`, the portal, a
//! schedule and `semlith_report` produce the same document.
//!
//! From 0.42.0 every figure is typed (`Figure`: value, unit, currency, what it
//! is over, and the display string a person saw), every number is formatted by
//! [`num`], and every renderer draws the reports-v1 design: HTML is the
//! reference, the PDF is typeset natively to match its print rendering
//! (`pdf.rs`), Markdown is built to read on GitHub, and CSV and JSON are data.

use anyhow::{Context, Result, bail};
use serde::Serialize;
use serde_json::Value;
use std::collections::{BTreeMap, BTreeSet};

mod pdf;
mod render;

pub use render::REPORT_CSP;

// ------------------------------------------------------------------ numbers

/// A count as every surface prints it: `736,397`, `-130`. The CLI's ledger and
/// stats lines call this; it is [`num::int`].
pub fn number(n: i64) -> String {
    num::int(n)
}

/// The one number formatter: the portal (`app.js`'s `n` and `pct`), the CLI and
/// every export print a figure through these rules, so `711,763` is the same
/// digits on every surface. 0.41 printed `736 397` in reports and `736,397` in
/// the portal.
pub mod num {
    /// `711,763` — US grouping, which is what the portal's
    /// `Intl.NumberFormat("en-US")` prints.
    pub fn int(value: i64) -> String {
        let digits = value.unsigned_abs().to_string();
        let mut out = String::with_capacity(digits.len() + digits.len() / 3 + 1);
        if value < 0 {
            out.push('-');
        }
        for (i, c) in digits.chars().enumerate() {
            if i > 0 && (digits.len() - i).is_multiple_of(3) {
                out.push(',');
            }
            out.push(c);
        }
        out
    }

    /// `$1.42`, two places, grouped: `$12,345.60`.
    pub fn usd(value: f64) -> String {
        let cents = (value * 100.0).round() as i64;
        let sign = if cents < 0 { "-" } else { "" };
        format!("{sign}${}.{:02}", int(cents.abs() / 100), cents.abs() % 100)
    }

    /// `80%` for a share between 0 and 1, by `app.js`'s `pct`: one decimal
    /// under 10 % so a small share is not printed as zero, whole above.
    pub fn pct(share: f64) -> String {
        let value = share * 100.0;
        if value > 0.0 && value < 10.0 {
            format!("{value:.1}%")
        } else {
            format!("{}%", value.round() as i64)
        }
    }

    /// `325.3×`.
    pub fn times(value: f64) -> String {
        format!("{}×", grouped_decimal(value, 1))
    }

    /// `67 ms`, from microseconds.
    pub fn ms(micros: i64) -> String {
        format!("{} ms", int((micros as f64 / 1000.0).round() as i64))
    }

    /// `12.3 MB`, binary units, as the portal's `bytes`.
    pub fn bytes(value: i64) -> String {
        let units = ["B", "KB", "MB", "GB", "TB"];
        let mut size = value.max(0) as f64;
        let mut unit = 0;
        while size >= 1024.0 && unit < units.len() - 1 {
            size /= 1024.0;
            unit += 1;
        }
        if unit == 0 {
            format!("{} B", int(size as i64))
        } else {
            format!("{size:.1} {}", units[unit])
        }
    }

    fn grouped_decimal(value: f64, places: usize) -> String {
        let text = format!("{:.*}", places, value.abs());
        let (whole, frac) = text.split_once('.').unwrap_or((&text, ""));
        let whole: i64 = whole.parse().unwrap_or(0);
        let sign = if value < 0.0 { "-" } else { "" };
        if frac.is_empty() {
            format!("{sign}{}", int(whole))
        } else {
            format!("{sign}{}.{frac}", int(whole))
        }
    }
}

/// `2026-10-14T20:13:04+05:30` — the ledger's own local stamp as ISO 8601.
fn iso(at: i64) -> String {
    let stamp = crate::clock::local_stamp(at);
    let mut parts = stamp.split(' ');
    match (parts.next(), parts.next(), parts.next()) {
        (Some(date), Some(time), Some(offset)) => format!("{date}T{time}{offset}"),
        _ => stamp,
    }
}

/// `2026-10-14`.
fn day(at: i64) -> String {
    crate::clock::local_stamp(at)
        .split(' ')
        .next()
        .unwrap_or_default()
        .to_string()
}

// -------------------------------------------------------------- the window

/// How far back a report looks.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Window {
    /// Everything recorded.
    All,
    /// The last N days.
    Days(i64),
}

/// The window vocabulary, and the only spellings [`window_of`] accepts.
pub const WINDOWS: [(&str, Window); 5] = [
    ("all", Window::All),
    ("day", Window::Days(1)),
    ("week", Window::Days(7)),
    ("month", Window::Days(30)),
    ("quarter", Window::Days(90)),
];

/// The window every surface uses when none is named: the CLI, the portal's
/// Reports page, `/api/report`, `semlith_report` and a schedule. The portal
/// opened at 30 days and the CLI at everything in 0.41 (R11), so the same
/// command printed two documents.
pub const DEFAULT_WINDOW: &str = "month";

/// Resolve a window name, or say which there are. `None` is [`DEFAULT_WINDOW`].
pub fn window_of(name: Option<&str>) -> Result<Window> {
    let name = name.unwrap_or(DEFAULT_WINDOW);
    match WINDOWS.iter().find(|(n, _)| *n == name) {
        Some((_, window)) => Ok(*window),
        None => bail!(
            "unknown window {name:?}; the windows are {}",
            WINDOWS
                .iter()
                .map(|(n, _)| *n)
                .collect::<Vec<_>>()
                .join(", ")
        ),
    }
}

impl Window {
    /// The epoch second a row must be at or after; 0 for everything.
    pub fn since(self, now: i64) -> i64 {
        match self {
            Window::All => 0,
            Window::Days(days) => now - days * 24 * 60 * 60,
        }
    }

    /// The name [`window_of`] takes back.
    pub fn name(self) -> &'static str {
        WINDOWS
            .iter()
            .find(|(_, w)| *w == self)
            .map_or("all", |(n, _)| *n)
    }

    /// `Last 7 days`.
    pub fn label(self) -> String {
        match self {
            Window::All => "Everything recorded".into(),
            Window::Days(1) => "Last 24 hours".into(),
            Window::Days(days) => format!("Last {days} days"),
        }
    }

    /// `in the last 7 days`, for a sentence.
    fn phrase(self) -> String {
        match self {
            Window::All => "since the ledger began".into(),
            Window::Days(1) => "in the last 24 hours".into(),
            Window::Days(days) => format!("in the last {days} days"),
        }
    }
}

/// The window a report was taken over, as every renderer prints it.
#[derive(Debug, Clone, Serialize)]
pub struct WindowInfo {
    /// `week`, as [`window_of`] takes it.
    pub name: String,
    /// `Last 7 days`.
    pub label: String,
    /// ISO 8601; absent for everything recorded.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub from: Option<String>,
    pub to: String,
    /// Whether this report's figures carry a date to narrow by. The health
    /// report is the index as it stands, and says so instead of printing a
    /// span it did not apply.
    pub applies: bool,
}

// ------------------------------------------------------------- the kinds

/// The five reports: id, title, audience.
pub const KINDS: [(&str, &str); 5] = [
    ("savings", "Retrieval savings"),
    ("access", "AI access audit"),
    ("change", "Change brief"),
    ("health", "Index health"),
    ("gaps", "Knowledge gaps"),
];

fn audience(kind: &str) -> &'static str {
    match kind {
        "savings" => "for whoever approves the spend",
        "access" => "for security review",
        "change" => "for the reviewer",
        "health" => "for whoever owns the store",
        _ => "for whoever writes the docs",
    }
}

/// The formats [`Report::render`] returns as text. CSV and PDF are bytes: a
/// report's CSV is a zip of one file per table, unless one table is named
/// ([`Report::render_table`]).
pub const FORMATS: [&str; 3] = ["markdown", "json", "html"];

/// The PDF.
pub const PDF: &str = "pdf";

/// CSV: a zip of tables, or one table by name.
pub const CSV: &str = "csv";

/// The five formats, in the order a caller is offered them.
pub const ALL_FORMATS: [&str; 5] = ["markdown", CSV, "json", "html", PDF];

/// The schema the JSON format and `/api/report` carry, published in
/// `docs/portal.md` (Reports › Formats and the JSON schema).
pub const SCHEMA: &str = "semlith.report/2";

/// The model a savings figure is priced at when nobody names one.
pub const DEFAULT_MODEL: &str = "claude-sonnet-5-5";

/// The models a savings figure is offered at, at least one per vendor the
/// supported clients reach, from the same models.dev table the ledger's usage
/// columns are priced by (`crate::prices`). Any model in that table can be
/// asked for by name; these are the ones a picker lists first.
pub const SAVINGS_MODELS: &[&str] = &[
    "claude-sonnet-5-5",
    "claude-opus-5-5",
    "claude-haiku-4-5",
    "claude-fable-5-1",
    "gpt-5.6",
    "gpt-5.5",
    "gpt-5.4-mini",
    "gpt-5.3-codex",
    "gemini-3.1-pro-preview",
    "gemini-3.5-flash",
    "grok-4.7",
    "deepseek-v4-pro",
    "deepseek-v4-flash",
    "kimi-k3",
    "glm-5.3",
    "mistral-large-latest",
];

/// The three names this binary priced by before 0.34.0, kept so a schedule or
/// a script that saved one still runs — now at that model's table price.
const ALIASES: [(&str, &str, &str); 3] = [
    ("sonnet5", "Sonnet 5", "claude-sonnet-5"),
    ("opus5", "Opus 5", "claude-opus-5"),
    ("haiku45", "Haiku 4.5", "claude-haiku-4-5"),
];

/// A model name reduced to what is worth comparing: letters and digits only,
/// folded to lower case. `Opus 5`, `opus-5` and `opus_5` are one model.
fn key_of(model: &str) -> String {
    model
        .chars()
        .filter(|c| c.is_ascii_alphanumeric())
        .map(|c| c.to_ascii_lowercase())
        .collect()
}

/// A model's name and what a million of its input tokens cost, if the price
/// table carries it. Input pricing, because a retrieval is something an agent
/// reads. A spelling nobody prices is `None`, never the default's figure.
pub fn price_named(model: &str) -> Option<(String, f64)> {
    let wanted = key_of(model);
    if wanted.is_empty() {
        return None;
    }
    let table = crate::prices::table();
    if let Some((_, label, id)) = ALIASES.iter().find(|(key, ..)| *key == wanted) {
        return crate::prices::lookup(&table, None, id)
            .map(|(_, p)| (label.to_string(), p.rates.input));
    }
    let (key, price) = crate::prices::lookup(&table, None, model)?;
    let name = key
        .split_once('/')
        .map_or(key.as_str(), |(_, m)| m)
        .to_string();
    Some((name, price.rates.input))
}

/// The price of a model by name, or the default's.
pub fn price_of(model: &str) -> (String, f64) {
    price_named(model)
        .or_else(|| price_named(DEFAULT_MODEL))
        .unwrap_or_else(|| (DEFAULT_MODEL.to_string(), 0.0))
}

/// What a refusal names as working instead.
pub fn price_names() -> String {
    format!(
        "any model in the price table (`semlith prices --model NAME`), such as {}",
        SAVINGS_MODELS[..6].join(", ")
    )
}

/// The picker's list: each of [`SAVINGS_MODELS`] the table carries, with its
/// input price.
pub fn savings_models() -> Vec<(String, f64)> {
    SAVINGS_MODELS
        .iter()
        .filter_map(|m| price_named(m))
        .collect()
}

fn unix_now() -> i64 {
    std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|d| d.as_secs() as i64)
        .unwrap_or(0)
}

/// Resolve a report name, or say which five there are — before a store is
/// opened, so a typo is a question about the argument.
pub fn kind_of(kind: &str) -> Result<(&'static str, &'static str)> {
    match KINDS.iter().find(|(k, _)| *k == kind) {
        Some((kind, title)) => Ok((kind, title)),
        None => bail!(
            "unknown report {kind:?}; the reports are {}",
            KINDS.iter().map(|(k, _)| *k).collect::<Vec<_>>().join(", ")
        ),
    }
}

/// Check a format a text-only surface can print: markdown, json or html.
pub fn check_format(format: &str) -> Result<()> {
    if FORMATS.contains(&format) || format == "md" {
        return Ok(());
    }
    bail!(
        "unknown format {format:?}; the formats are {}",
        FORMATS.join(", ")
    )
}

/// Check a format name against all five, CSV and PDF included.
pub fn check_any_format(format: &str) -> Result<()> {
    if ALL_FORMATS.contains(&format) || format == "md" {
        return Ok(());
    }
    bail!(
        "unknown format {format:?}; the formats are {}",
        ALL_FORMATS.join(", ")
    )
}

/// Where `--out` writes: the path itself, or for a folder (one that exists,
/// or one named with a trailing separator) `name` inside it, the folder made
/// first. `--out ./reports/` failed both ways before 0.43.0 (cloud audit C30):
/// "No such file or directory" when missing, "Is a directory" when there.
pub fn out_path(out: &std::path::Path, name: &str) -> Result<std::path::PathBuf> {
    let folder = out.is_dir()
        || out
            .as_os_str()
            .to_string_lossy()
            .ends_with(std::path::is_separator);
    if !folder {
        return Ok(out.to_path_buf());
    }
    std::fs::create_dir_all(out).with_context(|| format!("making the folder {}", out.display()))?;
    Ok(out.join(name))
}

// ------------------------------------------------------------- the model

/// A number with what it means: the value as data, its unit, the currency
/// where it is money, what it is over, and the string a person saw.
#[derive(Debug, Clone, Serialize)]
pub struct Figure {
    pub value: Value,
    pub unit: &'static str,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub currency: Option<&'static str>,
    pub over: String,
    pub display: String,
}

impl Figure {
    pub fn count(value: i64, unit: &'static str, over: impl Into<String>) -> Self {
        Self {
            value: value.into(),
            unit,
            currency: None,
            over: over.into(),
            display: num::int(value),
        }
    }

    pub fn usd(value: f64, over: impl Into<String>) -> Self {
        Self {
            value: round(value, 6),
            unit: "money",
            currency: Some("USD"),
            over: over.into(),
            display: num::usd(value),
        }
    }

    /// A share: `part` of `whole`, as a ratio, displayed as a percentage.
    pub fn share(part: i64, whole: i64, over: impl Into<String>) -> Self {
        let ratio = if whole == 0 {
            0.0
        } else {
            part as f64 / whole as f64
        };
        Self {
            value: round(ratio, 4),
            unit: "ratio",
            currency: None,
            over: over.into(),
            display: num::pct(ratio),
        }
    }

    pub fn times(value: Option<f64>, over: impl Into<String>) -> Self {
        Self {
            value: value.map_or(Value::Null, |v| round(v, 1)),
            unit: "times",
            currency: None,
            over: over.into(),
            display: value.map_or("—".into(), num::times),
        }
    }

    pub fn ms(micros: i64, over: impl Into<String>) -> Self {
        Self {
            value: round(micros as f64 / 1000.0, 1),
            unit: "ms",
            currency: None,
            over: over.into(),
            display: num::ms(micros),
        }
    }

    pub fn bytes(value: i64, over: impl Into<String>) -> Self {
        Self {
            value: value.into(),
            unit: "bytes",
            currency: None,
            over: over.into(),
            display: num::bytes(value),
        }
    }

    pub fn text(value: impl Into<String>, over: impl Into<String>) -> Self {
        let value = value.into();
        Self {
            value: Value::String(value.clone()),
            unit: "text",
            currency: None,
            over: over.into(),
            display: value,
        }
    }

    /// The same figure with a display a person reads better, the value kept.
    fn shown(mut self, display: impl Into<String>) -> Self {
        self.display = display.into();
        self
    }
}

fn round(value: f64, places: i32) -> Value {
    let scale = 10f64.powi(places);
    let rounded = (value * scale).round() / scale;
    serde_json::Number::from_f64(rounded).map_or(Value::Null, Value::Number)
}

/// A table cell: the value as data and the display a person reads.
#[derive(Debug, Clone)]
pub struct Cell {
    pub value: Value,
    pub display: String,
}

impl Cell {
    pub fn int(value: i64) -> Self {
        Self {
            value: value.into(),
            display: num::int(value),
        }
    }
    pub fn text(value: impl Into<String>) -> Self {
        let value = value.into();
        Self {
            value: Value::String(value.clone()),
            display: value,
        }
    }
    pub fn usd(value: f64) -> Self {
        Self {
            value: round(value, 6),
            display: num::usd(value),
        }
    }
    /// A share, 0 to 1, printed as a percentage.
    pub fn share(ratio: f64) -> Self {
        Self {
            value: round(ratio, 4),
            display: num::pct(ratio),
        }
    }
    /// A moment: ISO 8601 with its offset as data; `2026-10-14 19:59:41` for
    /// a person. The offset is the same on every row, so a table drops it and
    /// the footer's generated stamp carries it.
    pub fn stamp(at: i64) -> Self {
        let local = crate::clock::local_stamp(at);
        let display = local
            .rsplit_once(' ')
            .map_or(local.as_str(), |(stamp, _)| stamp)
            .to_string();
        Self {
            value: Value::String(iso(at)),
            display,
        }
    }
    pub fn empty() -> Self {
        Self {
            value: Value::Null,
            display: "—".into(),
        }
    }
}

impl Serialize for Cell {
    fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
        self.value.serialize(s)
    }
}

/// How a column is drawn. The renderers read this and nothing else to decide
/// alignment, face and wrapping, so every format lays a table out the same way.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum Kind {
    /// Prose that may wrap.
    Text,
    /// The row's name, in the heavier face.
    Strong,
    /// A description, in the muted ink.
    Muted,
    /// An id, a path, a version or a stamp: mono, never wrapped on screen —
    /// printed, or in a PDF, one wider than its column wraps (after a `/`
    /// where it has one) rather than being shortened.
    Id,
    /// A number: mono, right-aligned, never wrapped.
    Num,
    /// A share drawn as a bar beside its percentage.
    Bar,
}

#[derive(Debug, Clone, Serialize)]
pub struct Column {
    /// The stable snake_case key a CSV header and a JSON reader use; carries
    /// its unit (`net_tokens`, `cost_usd`, `coverage_ratio`).
    pub key: &'static str,
    pub label: String,
    pub kind: Kind,
}

fn col(key: &'static str, label: &str, kind: Kind) -> Column {
    Column {
        key,
        label: label.to_string(),
        kind,
    }
}

#[derive(Debug, Clone, Serialize)]
pub struct Table {
    /// What `--table` names it by and what its CSV file is called.
    pub id: &'static str,
    pub title: String,
    /// The small line at the right of the card's header.
    pub meta: String,
    pub columns: Vec<Column>,
    pub rows: Vec<Vec<Cell>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub total: Option<Vec<Cell>>,
    /// Rows the table does not list, said rather than implied.
    #[serde(skip_serializing_if = "is_zero")]
    pub more: usize,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub note: Option<String>,
    /// What it says when there are no rows.
    pub empty: String,
}

fn is_zero(n: &usize) -> bool {
    *n == 0
}

#[derive(Debug, Clone, Serialize)]
pub struct Fact {
    pub label: String,
    #[serde(flatten)]
    pub figure: Figure,
}

#[derive(Debug, Clone, Serialize)]
pub struct Kpi {
    pub label: String,
    #[serde(flatten)]
    pub figure: Figure,
    /// The headline figure, drawn with the accent edge.
    pub lead: bool,
}

/// A pill: a store name, a chain state, a redaction state.
#[derive(Debug, Clone, Serialize)]
pub struct Chip {
    pub text: String,
    /// `green`, `amber`, `blue` or empty for the neutral pill.
    #[serde(skip_serializing_if = "str::is_empty")]
    pub tone: &'static str,
}

/// One cell of the meta strip.
#[derive(Debug, Clone, Serialize)]
pub struct Meta {
    pub label: String,
    /// The first line in ink, the rest muted.
    pub lines: Vec<String>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub chips: Vec<Chip>,
}

/// One block of a report, in the order it is drawn.
#[derive(Debug, Clone, Serialize)]
#[serde(tag = "block", rename_all = "lowercase")]
pub enum Block {
    Table(Table),
    /// Secondary figures, three to a row.
    Facts {
        title: String,
        meta: String,
        facts: Vec<Fact>,
        #[serde(skip_serializing_if = "Option::is_none")]
        note: Option<String>,
    },
    /// A sentence that explains a rule.
    Note {
        text: String,
    },
    /// The exact commands that reproduce the document.
    Code {
        title: String,
        meta: String,
        comment: String,
        lines: Vec<String>,
    },
}

/// A run of the lede, bold or not.
#[derive(Debug, Clone)]
pub struct Run {
    pub text: String,
    pub bold: bool,
}

fn runs(parts: &[(&str, bool)]) -> Vec<Run> {
    parts
        .iter()
        .map(|(text, bold)| Run {
            text: text.to_string(),
            bold: *bold,
        })
        .collect()
}

fn lede_text<S: serde::Serializer>(lede: &[Run], s: S) -> Result<S::Ok, S::Error> {
    s.serialize_str(&lede.iter().map(|r| r.text.as_str()).collect::<String>())
}

/// The ledger's hash chain, as a report states it.
#[derive(Debug, Clone, Serialize)]
pub struct Chain {
    /// `verifies`, `broken` or `empty`.
    pub state: &'static str,
    pub rows: i64,
    /// The newest row's hash, of the store holding the newest row.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub head: Option<String>,
}

/// One generated report.
#[derive(Debug, Clone, Serialize)]
pub struct Report {
    pub schema: &'static str,
    pub kind: &'static str,
    pub title: String,
    pub audience: String,
    #[serde(serialize_with = "lede_text")]
    pub lede: Vec<Run>,
    /// The local stamp a person reads, and its ISO form.
    pub generated: String,
    pub generated_at: String,
    pub version: String,
    pub window: WindowInfo,
    /// The stores this was generated over, after narrowing.
    pub stores: Vec<String>,
    pub redacted: bool,
    pub meta: Vec<Meta>,
    pub kpis: Vec<Kpi>,
    pub blocks: Vec<Block>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub chain: Option<Chain>,
    /// Where it was generated, when not on this machine. Absent when unset.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub origin: Option<String>,
}

impl Report {
    /// A report with a title and nothing in it: what a renderer test, or a
    /// program embedding the library, starts from.
    pub fn new(kind: &'static str, title: &str) -> Self {
        let now = unix_now();
        Self {
            schema: SCHEMA,
            kind,
            title: title.to_string(),
            audience: audience(kind).to_string(),
            lede: Vec::new(),
            generated: crate::clock::local_stamp(now),
            generated_at: iso(now),
            version: env!("CARGO_PKG_VERSION").to_string(),
            window: WindowInfo {
                name: "all".into(),
                label: Window::All.label(),
                from: None,
                to: iso(now),
                applies: false,
            },
            stores: Vec::new(),
            redacted: false,
            meta: Vec::new(),
            kpis: Vec::new(),
            blocks: Vec::new(),
            chain: None,
            origin: None,
        }
    }

    /// Every table, in order, as `--table` names them.
    pub fn tables(&self) -> impl Iterator<Item = &Table> {
        self.blocks.iter().filter_map(|b| match b {
            Block::Table(t) => Some(t),
            _ => None,
        })
    }

    /// The lede as plain text.
    pub fn lede_plain(&self) -> String {
        self.lede.iter().map(|r| r.text.as_str()).collect()
    }
}

/// The builder's two content toggles.
///
/// Not three: the design draws a `Sign the report` toggle as well, and signing
/// needs a key this product does not have. A toggle drawn with no wire behind
/// it is worse than one not drawn.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct Options {
    /// Attach the per-retrieval rows behind the session totals: who asked,
    /// when, with which tool, how many hits and what they cost. The ledger
    /// keeps counts, not the lines an agent was shown, so this attaches rows
    /// and never lines.
    pub excerpts: bool,
    /// Replace every query text with a keyed digest of it.
    pub redact: bool,
    /// The key the digest is made with; `None` is this machine's
    /// (`home::report_salt`). A test names its own rather than writing one
    /// into a real home.
    pub salt: Option<[u8; 32]>,
}

// ------------------------------------------------------------ generating

/// The stores one report is over: the scope, already narrowed.
type Scope<'a> = [(&'a str, &'a crate::Semlith)];

/// Generate one report over every open store, in the default window.
pub fn generate(fleet: &crate::fleet::Fleet, kind: &str, model: &str) -> Result<Report> {
    generate_over(fleet, kind, model, window_of(None)?, &[])
}

/// Generate one report over a window and a scope.
pub fn generate_over(
    fleet: &crate::fleet::Fleet,
    kind: &str,
    model: &str,
    window: Window,
    only: &[String],
) -> Result<Report> {
    generate_with(fleet, kind, model, window, only, Options::default())
}

/// What every report is generated from: the scope, the window, the toggles.
struct Ctx<'a> {
    scope: &'a Scope<'a>,
    window: Window,
    now: i64,
    since: i64,
    options: Options,
    model: String,
    short: crate::Shortener,
    salt: [u8; 32],
    /// Whether the caller named stores, so the recompute command does too.
    narrowed: bool,
}

impl Ctx<'_> {
    /// A query text as it reaches paper: itself, or a keyed digest of it.
    ///
    /// Keyed with a per-machine salt (R7): an unkeyed digest of a symbol name
    /// is a dictionary lookup away from the name, so `detect_binary` and
    /// `blueprint registration` were reversed by guessing. Report-wide rather
    /// than per block: a toggle that hid a query in one table and printed it
    /// in another would be a promise broken by the document that made it.
    fn said(&self, query: &str) -> String {
        if !self.options.redact {
            return query.to_string();
        }
        format!(
            "hash:{}",
            &blake3::keyed_hash(&self.salt, query.as_bytes()).to_hex()[..16]
        )
    }

    fn path(&self, path: &str) -> String {
        crate::plain(&self.short.short(path))
    }

    /// `--window month --scope a --scope b`, as the recompute card prints it:
    /// only the flags this report reads.
    fn flags(&self, kind: &str) -> String {
        let mut out = format!("semlith report {kind}");
        if kind != "health" {
            out.push_str(&format!(" --window {}", self.window.name()));
        }
        if self.narrowed {
            for (label, _) in self.scope {
                out.push_str(&format!(" --scope {}", shell_word(label)));
            }
        }
        if kind == "savings" {
            out.push_str(&format!(" --model {}", shell_word(&self.model)));
        }
        if self.options.redact && matches!(kind, "access" | "gaps") {
            out.push_str(" --redact");
        }
        if self.options.excerpts && kind == "access" {
            out.push_str(" --excerpts");
        }
        out
    }
}

/// A word as a shell reads it back: bare when it is plain, single-quoted when
/// it is not (`Sonnet 5`, a store label with a space), so the recompute
/// command pastes and runs.
fn shell_word(word: &str) -> String {
    if !word.is_empty()
        && word
            .chars()
            .all(|c| c.is_ascii_alphanumeric() || "-_.:/@+".contains(c))
    {
        word.to_string()
    } else {
        format!("'{}'", word.replace('\'', r"'\''"))
    }
}

/// Generate one report with the builder's toggles applied.
///
/// `only` is store labels; empty is every open store. A label no open store
/// answers to is refused rather than dropped.
pub fn generate_with(
    fleet: &crate::fleet::Fleet,
    kind: &str,
    model: &str,
    window: Window,
    only: &[String],
    options: Options,
) -> Result<Report> {
    let (kind, title) = kind_of(kind)?;
    let open: Vec<&str> = fleet.each().map(|(label, _)| label).collect();
    for name in only {
        if !open.contains(&name.as_str()) {
            bail!(
                "no open store is named {name:?}; the open stores are {}",
                if open.is_empty() {
                    "none".to_string()
                } else {
                    open.join(", ")
                }
            );
        }
    }
    let scope: Vec<(&str, &crate::Semlith)> = fleet
        .each()
        .filter(|(label, _)| only.is_empty() || only.iter().any(|n| n == label))
        .collect();
    let salt = match (options.redact, options.salt) {
        (_, Some(salt)) => salt,
        (true, None) => {
            crate::home::report_salt().context("making this machine's key for query digests")?
        }
        (false, None) => [0; 32],
    };
    // Paths print relative to the root that holds them: the registry's roots,
    // and for a store the registry does not name, the directory every one of
    // its files sits under.
    let mut roots = fleet.roots();
    for (_, store) in &scope {
        if let Some((first, last)) = crate::store::path_bounds(store.db())? {
            let common: std::path::PathBuf = std::path::Path::new(&first)
                .components()
                .zip(std::path::Path::new(&last).components())
                .take_while(|(a, b)| a == b)
                .map(|(a, _)| a)
                .collect();
            let common = if common.as_os_str() == first.as_str() {
                common
                    .parent()
                    .map(std::path::Path::to_path_buf)
                    .unwrap_or(common)
            } else {
                common
            };
            if common.components().count() > 1 {
                roots.push(common);
            }
        }
    }
    let now = unix_now();
    let ctx = Ctx {
        scope: &scope,
        window,
        now,
        since: window.since(now),
        options,
        model: price_of(model).0,
        short: crate::Shortener::new(roots),
        salt,
        narrowed: !only.is_empty(),
    };
    let mut report = Report::new(kind, title);
    report.stores = scope.iter().map(|(l, _)| l.to_string()).collect();
    report.redacted = options.redact && matches!(kind, "access" | "gaps");
    report.window = WindowInfo {
        name: window.name().into(),
        label: window.label(),
        from: (window != Window::All).then(|| iso(ctx.since)),
        to: iso(now),
        applies: kind != "health",
    };
    match kind {
        "savings" => savings(&ctx, &mut report)?,
        "access" => access(&ctx, &mut report)?,
        "change" => change(&ctx, &mut report)?,
        "health" => health(&ctx, &mut report)?,
        _ => gaps(&ctx, &mut report)?,
    }
    Ok(report)
}

// ----------------------------------------------------- shared meta cells

fn window_meta(ctx: &Ctx<'_>, note: Option<&str>) -> Meta {
    if note.is_some_and(|n| n.starts_with("the index as it stands")) {
        return Meta {
            label: "Window".into(),
            lines: vec![
                "The index as it stands".into(),
                format!("on {} · no date to narrow by", day(ctx.now)),
            ],
            chips: Vec::new(),
        };
    }
    let mut lines = vec![ctx.window.label()];
    match (ctx.window, note) {
        (_, Some(note)) => lines.push(note.to_string()),
        (Window::All, None) => lines.push(format!("to {}", day(ctx.now))),
        (Window::Days(_), None) => lines.push(format!("{} → {}", day(ctx.since), day(ctx.now))),
    }
    Meta {
        label: "Window".into(),
        lines,
        chips: Vec::new(),
    }
}

fn stores_meta(ctx: &Ctx<'_>) -> Meta {
    Meta {
        label: "Stores".into(),
        lines: if ctx.scope.is_empty() {
            vec!["no open store".into()]
        } else {
            Vec::new()
        },
        chips: ctx
            .scope
            .iter()
            .map(|(label, _)| Chip {
                text: label.to_string(),
                tone: "",
            })
            .collect(),
    }
}

/// The chain over every store in scope: it verifies only if each store's does.
fn chain_of(ctx: &Ctx<'_>) -> Result<Chain> {
    let (mut rows, mut intact, mut newest) = (0i64, true, None::<(i64, String)>);
    for (_, store) in ctx.scope {
        let db = store.db();
        rows += crate::store::ledger_rows(db)?;
        intact = intact && crate::store::ledger_break(db)?.is_none();
        if let Some(head) = crate::store::ledger_head(db)? {
            let at: i64 = db
                .query_row("SELECT COALESCE(MAX(at), 0) FROM retrievals", [], |r| {
                    r.get(0)
                })
                .unwrap_or(0);
            if newest.as_ref().is_none_or(|(t, _)| at > *t) {
                newest = Some((at, head));
            }
        }
    }
    Ok(Chain {
        state: if rows == 0 {
            "empty"
        } else if intact {
            "verifies"
        } else {
            "broken"
        },
        rows,
        head: newest.map(|(_, h)| h),
    })
}

fn short_hash(hash: &str) -> String {
    let bare = hash.split_once(':').map_or(hash, |(_, h)| h);
    if bare.len() > 14 {
        format!("{}…{}", &bare[..8], &bare[bare.len() - 4..])
    } else {
        bare.to_string()
    }
}

fn chain_meta(chain: &Chain) -> Meta {
    let (tone, text) = match chain.state {
        "verifies" => ("green", "verifies"),
        "broken" => ("amber", "broken"),
        _ => ("", "no rows yet"),
    };
    let mut lines = Vec::new();
    lines.push(format!(
        "{} rows{}",
        num::int(chain.rows),
        chain
            .head
            .as_deref()
            .map(|h| format!(" · head {}", short_hash(h)))
            .unwrap_or_default()
    ));
    Meta {
        label: "Ledger chain".into(),
        lines,
        chips: vec![Chip {
            text: text.into(),
            tone,
        }],
    }
}

fn recompute(ctx: &Ctx<'_>, kind: &str, verify: bool) -> Block {
    let mut lines = vec![ctx.flags(kind)];
    if verify {
        lines.push("semlith ledger --verify".into());
    }
    Block::Code {
        title: "Recompute it".into(),
        meta: "the same document, without the browser".into(),
        comment: if verify {
            "every figure above comes from this machine's ledger rows".into()
        } else {
            "every figure above comes from this machine's index".into()
        },
        lines,
    }
}

// ------------------------------------------------------- the ledger tally

/// Every figure a ledger report prints, and the rows it may attach.
///
/// The figures are `ledger::figures` — the one aggregation the Ledger page,
/// Home, `semlith ledger` and `semlith_report` print (0.42.0 item 5) — over
/// the report's window and scope, so a report cannot count differently from
/// the page it was exported from.
struct Tally {
    f: crate::ledger::Figures,
    /// Every agent retrieval row in the window, newest first, for the
    /// attachment: a listing, not a figure.
    rows: Vec<(String, crate::store::ReportRow)>,
}

fn tally(ctx: &Ctx<'_>) -> Result<Tally> {
    let members: Vec<(&str, &rusqlite::Connection)> = ctx
        .scope
        .iter()
        .map(|(label, store)| (*label, store.db()))
        .collect();
    let f = crate::ledger::figures(
        &members,
        crate::ledger::Window {
            since: ctx.since,
            until: None,
        },
        false,
    )?;
    let mut rows = Vec::new();
    for (label, store) in ctx.scope {
        for row in crate::store::report_rows_since(store.db(), label, ctx.since)? {
            if crate::ledger::is_retrieval(&row.tool) && !crate::ledger::is_human(&row.client) {
                rows.push((label.to_string(), row));
            }
        }
    }
    rows.sort_by(|a, b| b.1.at.cmp(&a.1.at).then(b.1.id.cmp(&a.1.id)));
    Ok(Tally { f, rows })
}

fn s(n: usize, one: &str, many: &str) -> String {
    format!("{} {}", num::int(n as i64), if n == 1 { one } else { many })
}

// --------------------------------------------------------------- savings

fn savings(ctx: &Ctx<'_>, report: &mut Report) -> Result<()> {
    let (name, per_million) = price_of(&ctx.model);
    let table = crate::prices::table();
    let t = tally(ctx)?;
    let chain = chain_of(ctx)?;
    let cost = |tokens: i64| tokens as f64 / 1e6 * per_million;
    let (queries, credited) = (t.f.queries as usize, t.f.credited as usize);
    let ratio = t.f.ratio;

    report.lede = if queries == 0 {
        runs(&[
            ("No agent asked semlith anything ", false),
            (&ctx.window.phrase(), false),
            (
                ". Every figure below is zero rather than missing, and still recomputable.",
                false,
            ),
        ])
    } else {
        let ratio_text = ratio.map_or("—".to_string(), num::times);
        let asked = s(queries, "question", "questions");
        let answered = num::int(credited as i64);
        let fewer = format!("{ratio_text} fewer tokens");
        let window = format!(" {}. Semlith answered ", ctx.window.phrase());
        runs(&[
            ("Agents on this machine asked ", false),
            (&asked, true),
            (&window, false),
            (&answered, true),
            (
                " of them with excerpts instead of whole files, and the excerpts cost ",
                false,
            ),
            (&fewer, true),
            (" than the files they came from.", false),
        ])
    };
    report.meta = vec![
        window_meta(ctx, None),
        stores_meta(ctx),
        Meta {
            label: "Priced at".into(),
            lines: vec![
                name.clone(),
                format!("{} / M input · {}", num::usd(per_million), table.label()),
            ],
            chips: Vec::new(),
        },
        chain_meta(&chain),
    ];
    report.kpis = vec![
        Kpi {
            label: "Net tokens not read".into(),
            figure: Figure::count(
                t.f.net_tokens,
                "tokens",
                format!(
                    "whole-file less excerpt, {}",
                    s(credited, "credited query", "credited queries")
                ),
            ),
            lead: true,
        },
        Kpi {
            label: "Net cost avoided".into(),
            figure: Figure::usd(cost(t.f.net_tokens), format!("at {name} input price")),
            lead: false,
        },
        Kpi {
            label: "Coverage".into(),
            figure: Figure::share(
                credited as i64,
                queries as i64,
                format!(
                    "{} of {} agent queries answered",
                    num::int(credited as i64),
                    num::int(queries as i64)
                ),
            ),
            lead: false,
        },
        Kpi {
            label: "Fewer tokens".into(),
            figure: Figure::times(ratio, "than reading those files whole"),
            lead: false,
        },
    ];

    let not_read = Table {
        id: "not_read",
        title: "What was not read".into(),
        meta: format!("tokens · cost at {name}"),
        columns: vec![
            col("line", "Line", Kind::Strong),
            col("tokens", "Tokens", Kind::Num),
            col("cost_usd", "Cost", Kind::Num),
            col("counts", "What it counts", Kind::Muted),
        ],
        rows: vec![
            vec![
                Cell::text("Whole files not read"),
                Cell::int(t.f.whole_file_tokens),
                Cell::usd(cost(t.f.whole_file_tokens)),
                Cell::text(
                    "reading every file a query was answered from, counted as the agent would read it",
                ),
            ],
            vec![
                Cell::text("Excerpts read instead"),
                Cell::int(t.f.excerpt_tokens),
                Cell::usd(cost(t.f.excerpt_tokens)),
                Cell::text("what the answers actually cost"),
            ],
        ],
        total: Some(vec![
            Cell::text("Net not read"),
            Cell::int(t.f.net_tokens),
            Cell::usd(cost(t.f.net_tokens)),
            Cell::text(format!(
                "over {}",
                s(credited, "credited query", "credited queries")
            )),
        ]),
        more: 0,
        note: Some(format!(
            "Refunds ({}) and zero-hit queries ({}) are counted on their own lines below and never in the saving.",
            s(
                t.f.refunds as usize,
                "file an agent read whole anyway",
                "files an agent read whole anyway"
            ),
            num::int(t.f.zero_hit)
        )),
        empty: "Nothing recorded in this window.".into(),
    };

    let mut agents: Vec<&crate::ledger::ClientFigures> = t.f.client_figures.iter().collect();
    agents.sort_by(|a, b| {
        b.net_tokens
            .cmp(&a.net_tokens)
            .then(a.client.cmp(&b.client))
    });
    let total_net = t.f.net_tokens.max(1);
    let by_agent = Table {
        id: "by_agent",
        title: "By agent".into(),
        meta: "queries · net tokens".into(),
        columns: vec![
            col("agent", "Agent", Kind::Strong),
            col("version", "Version", Kind::Id),
            col("queries", "Queries", Kind::Num),
            col("coverage_ratio", "Coverage", Kind::Num),
            col("net_tokens", "Net tokens", Kind::Num),
            col("share_ratio", "Share", Kind::Bar),
        ],
        rows: agents
            .iter()
            .map(|c| {
                let q = c.queries as f64;
                vec![
                    Cell::text(c.client.as_str()),
                    if c.version.is_empty() {
                        Cell::empty()
                    } else {
                        Cell::text(&c.version)
                    },
                    Cell::int(c.queries),
                    Cell::share(if q == 0.0 { 0.0 } else { c.credited as f64 / q }),
                    Cell::int(c.net_tokens),
                    Cell::share(c.net_tokens.max(0) as f64 / total_net as f64),
                ]
            })
            .collect(),
        total: None,
        more: 0,
        note: None,
        empty: "No agent asked anything in this window.".into(),
    };

    let refunds_over = if t.f.refunds > 0 {
        "files read whole anyway · steering hook, measured"
    } else {
        "a floor: no steering hook reported a whole-file read here"
    };
    let over_queries = format!(
        "over {} in the window",
        s(t.f.timed as usize, "query", "queries")
    );
    let facts = Block::Facts {
        title: "Counted alongside".into(),
        meta: "never summed with the saving".into(),
        facts: vec![
            Fact {
                label: "Refunds".into(),
                figure: Figure::count(t.f.refunds, "files", refunds_over),
            },
            Fact {
                label: "Zero-hit".into(),
                figure: {
                    let share = if queries == 0 {
                        0.0
                    } else {
                        t.f.zero_hit as f64 / queries as f64
                    };
                    let display = format!("{} · {}", num::int(t.f.zero_hit), num::pct(share));
                    Figure::count(
                        t.f.zero_hit,
                        "queries",
                        "queries the corpus could not answer",
                    )
                    .shown(display)
                },
            },
            Fact {
                label: "Tier".into(),
                figure: Figure::text(
                    t.f.tier.word,
                    if t.f.tier.word == "measured" {
                        "both sides counted with the store's tokenizer"
                    } else {
                        "some rows estimated at four characters a token"
                    },
                ),
            },
            Fact {
                label: "Latency p50".into(),
                figure: Figure::ms(t.f.p50_micros, &over_queries),
            },
            Fact {
                label: "Latency p95".into(),
                figure: Figure::ms(t.f.p95_micros, &over_queries),
            },
            Fact {
                label: "Human searches".into(),
                figure: Figure::count(
                    t.f.human.queries,
                    "queries",
                    "portal and CLI, recorded, not counted as agent saving",
                ),
            },
        ],
        note: Some(
            "A refund is an agent that did not ask; a zero hit is semlith that did not reach \
             the answer. They call for opposite things, so they are shown apart."
                .into(),
        ),
    };
    report.blocks = vec![
        Block::Table(not_read),
        Block::Table(by_agent),
        facts,
        recompute(ctx, "savings", true),
    ];
    report.chain = Some(chain);
    Ok(())
}

// ---------------------------------------------------------------- access

/// The most individual retrievals one access report attaches. The sessions
/// table is the whole count either way; this bounds the detail.
const ATTACHED: usize = 500;

fn access(ctx: &Ctx<'_>, report: &mut Report) -> Result<()> {
    let t = tally(ctx)?;
    let chain = chain_of(ctx)?;
    let agents: BTreeSet<&str> = t.f.per_session.iter().map(|s| s.client.as_str()).collect();
    let sessions = t.f.per_session.len();

    report.lede = {
        let sessions_text = s(sessions, "session", "sessions");
        let agents_text = s(agents.len(), "agent", "agents");
        let tail = format!(
            " {}; nothing here has left the store it was written into.",
            ctx.window.phrase()
        );
        runs(&[
            (
                "Which agent asked what of this machine's stores, and when, from the hash-chained record. ",
                false,
            ),
            (&sessions_text, true),
            (" by ", false),
            (&agents_text, true),
            (&tail, false),
        ])
    };
    report.meta = vec![
        window_meta(ctx, Some("rows counted by their own time")),
        stores_meta(ctx),
        Meta {
            label: "Query text".into(),
            lines: vec![if ctx.options.redact {
                "keyed per machine · not reversible".into()
            } else {
                "as the agent asked it".into()
            }],
            chips: vec![Chip {
                text: if ctx.options.redact {
                    "hashed".into()
                } else {
                    "plain".into()
                },
                tone: if ctx.options.redact { "amber" } else { "" },
            }],
        },
        chain_meta(&chain),
    ];
    report.kpis = vec![
        Kpi {
            label: "Agent sessions".into(),
            figure: Figure::count(
                sessions as i64,
                "sessions",
                format!(
                    "{} · one row per conversation",
                    s(agents.len(), "agent", "agents")
                ),
            ),
            lead: true,
        },
        Kpi {
            label: "Queries".into(),
            figure: Figure::count(
                t.f.queries,
                "queries",
                format!("{} answered with excerpts", num::int(t.f.credited)),
            ),
            lead: false,
        },
        Kpi {
            label: "Read whole".into(),
            figure: Figure::count(t.f.refunds, "files", "refunds seen by the steering hook"),
            lead: false,
        },
        Kpi {
            label: "Human searches".into(),
            figure: Figure::count(
                t.f.human.queries,
                "queries",
                "portal and CLI · not agent sessions",
            ),
            lead: false,
        },
    ];

    // Newest first, as the aggregation orders them.
    let rows: Vec<&crate::ledger::SessionFigures> = t.f.per_session.iter().collect();
    let sessions_table = Table {
        id: "sessions",
        title: "Sessions".into(),
        meta: "one row per conversation · newest first".into(),
        columns: vec![
            col("last_seen", "Last seen", Kind::Id),
            col("session", "Session", Kind::Id),
            col("agent", "Agent", Kind::Strong),
            col("version", "Version", Kind::Id),
            col("stores", "Stores", Kind::Text),
            col("queries", "Queries", Kind::Num),
            col("zero_hit", "Zero-hit", Kind::Num),
            col("net_tokens", "Net tokens", Kind::Num),
        ],
        rows: rows
            .iter()
            .map(|st| {
                vec![
                    Cell::stamp(st.last),
                    if st.session.is_empty() {
                        Cell::empty()
                    } else {
                        Cell::text(st.session.as_str())
                    },
                    Cell::text(st.client.as_str()),
                    if st.client_version.is_empty() {
                        Cell::empty()
                    } else {
                        Cell::text(&st.client_version)
                    },
                    Cell::text(st.stores.join(" · ")),
                    Cell::int(st.queries),
                    Cell::int(st.zero_hit),
                    Cell::int(st.net_tokens),
                ]
            })
            .collect(),
        total: None,
        more: 0,
        note: None,
        empty: "No agent session in this window.".into(),
    };
    let mut blocks = vec![Block::Table(sessions_table)];
    if ctx.options.excerpts {
        let listed: Vec<&(String, crate::store::ReportRow)> =
            t.rows.iter().take(ATTACHED).collect();
        blocks.push(Block::Table(Table {
            id: "retrievals",
            title: "What each agent asked".into(),
            meta: "attached retrievals · counts, not lines".into(),
            columns: vec![
                col("at", "At", Kind::Id),
                col("agent", "Agent", Kind::Strong),
                col("store", "Store", Kind::Text),
                col("tool", "Tool", Kind::Id),
                col("query", "Query", if ctx.options.redact { Kind::Id } else { Kind::Text }),
                col("hits", "Hits", Kind::Num),
                col("excerpt_tokens", "Excerpt tokens", Kind::Num),
                col("whole_file_tokens", "Whole-file tokens", Kind::Num),
            ],
            rows: listed
                .iter()
                .map(|(label, row)| {
                    vec![
                        Cell::stamp(row.at),
                        Cell::text(&row.client),
                        Cell::text(label.as_str()),
                        if row.tool.is_empty() {
                            Cell::empty()
                        } else {
                            Cell::text(&row.tool)
                        },
                        Cell::text(ctx.said(&row.query)),
                        Cell::int(row.hits),
                        Cell::int(row.excerpt_tokens),
                        Cell::int(row.whole_file_tokens),
                    ]
                })
                .collect(),
            total: None,
            more: t.rows.len().saturating_sub(ATTACHED),
            note: Some(format!(
                "One row per retrieval as the ledger recorded it: who asked, when, with which tool, \
                 how many hits and what they cost. The ledger keeps counts, not the lines an agent \
                 was shown.{}",
                if ctx.options.redact {
                    " The query is a digest keyed to this machine, so this says who asked and \
                     what it cost, not what was asked."
                } else {
                    ""
                }
            )),
            empty: "No retrieval in this window.".into(),
        }));
    }
    let mut proof = Vec::new();
    for (label, store) in ctx.scope {
        let db = store.db();
        let rows = crate::store::ledger_rows(db)?;
        let broken = crate::store::ledger_break(db)?;
        proof.push(vec![
            Cell::text(*label),
            Cell::int(rows),
            Cell::text(match (rows, broken) {
                (0, _) => "no rows yet".to_string(),
                (_, None) => "verifies".to_string(),
                (_, Some(id)) => format!("breaks at row {id}"),
            }),
            crate::store::ledger_head(db)?.map_or(Cell::empty(), Cell::text),
        ]);
    }
    blocks.push(Block::Table(Table {
        id: "chain",
        title: "Chain proof".into(),
        meta: "each store's own hash chain".into(),
        columns: vec![
            col("store", "Store", Kind::Strong),
            col("rows", "Rows", Kind::Num),
            col("chain", "Chain", Kind::Text),
            col("head", "Head", Kind::Id),
        ],
        rows: proof,
        total: None,
        more: 0,
        note: Some(
            "Rows are hash-chained in each store's own database; editing or deleting one breaks \
             `semlith ledger --verify`."
                .into(),
        ),
        empty: "No store in scope.".into(),
    }));
    blocks.push(recompute(ctx, "access", true));
    report.blocks = blocks;
    report.chain = Some(chain);
    Ok(())
}

// ---------------------------------------------------------------- change

/// How many changed definitions per store the blast radius walks. The walk is
/// `graph::impact` per name; a re-index of a thousand files would otherwise
/// be a thousand walks.
const BLAST_NAMES: usize = 25;

/// Rows the changed-files table lists before saying how many more there are.
const LISTED: usize = 200;

fn change(ctx: &Ctx<'_>, report: &mut Report) -> Result<()> {
    struct Changed {
        store: String,
        path: String,
        at: i64,
        added: Vec<String>,
        removed: Vec<String>,
        resized: Vec<String>,
    }
    let mut changed: Vec<Changed> = Vec::new();
    let mut first = 0usize;
    let mut blast: Vec<Vec<Cell>> = Vec::new();
    let mut reached: BTreeSet<(String, String, String)> = BTreeSet::new();
    let mut stores_changed = BTreeSet::new();
    let mut walked = 0usize;
    for (label, store) in ctx.scope {
        let mut names: Vec<(String, String, &'static str)> = Vec::new();
        for file in crate::store::files_read_since(store.db(), ctx.since)? {
            if file.before.is_empty() {
                first += 1;
                continue;
            }
            let before: BTreeMap<&str, u32> = file
                .before
                .iter()
                .map(|(n, _, a, b)| (n.as_str(), b.saturating_sub(*a)))
                .collect();
            let now: BTreeMap<&str, u32> = file
                .now
                .iter()
                .map(|(n, _, a, b)| (n.as_str(), b.saturating_sub(*a)))
                .collect();
            let added: Vec<String> = now
                .keys()
                .filter(|n| !before.contains_key(*n))
                .map(|n| n.to_string())
                .collect();
            let removed: Vec<String> = before
                .keys()
                .filter(|n| !now.contains_key(*n))
                .map(|n| n.to_string())
                .collect();
            let resized: Vec<String> = now
                .iter()
                .filter(|(n, len)| before.get(*n).is_some_and(|b| b != *len))
                .map(|(n, _)| n.to_string())
                .collect();
            // A Markdown heading or a JSON key changes as text and is called by
            // nothing, so the walk is over code definitions only.
            let navigational: BTreeSet<&str> = file
                .before
                .iter()
                .chain(&file.now)
                .filter(|(_, kind, ..)| crate::graph::NAVIGATIONAL_KINDS.contains(&kind.as_str()))
                .map(|(n, ..)| n.as_str())
                .collect();
            for (list, delta) in [
                (&resized, "changed"),
                (&added, "added"),
                (&removed, "removed"),
            ] {
                for name in list {
                    if !navigational.contains(name.as_str()) {
                        names.push((name.clone(), file.path.clone(), delta));
                    }
                }
            }
            stores_changed.insert(label.to_string());
            changed.push(Changed {
                store: label.to_string(),
                path: file.path,
                at: file.indexed_at,
                added,
                removed,
                resized,
            });
        }
        walked += names.len().saturating_sub(BLAST_NAMES);
        for (name, path, delta) in names.into_iter().take(BLAST_NAMES) {
            let impact = crate::graph::impact(
                store.db(),
                &name,
                &[],
                2,
                false,
                &crate::graph::Scope::all(),
            )?;
            for r in &impact.reached {
                reached.insert((label.to_string(), r.path.clone(), r.name.clone()));
            }
            let mut top: Vec<&crate::graph::Reached> = impact.reached.iter().collect();
            top.sort_by_key(|r| r.hop);
            blast.push(vec![
                Cell::text(&name),
                Cell::text(ctx.path(&path)),
                Cell::text(delta),
                Cell::int(impact.reached.len() as i64 + impact.hidden as i64),
                Cell::int(impact.files.len() as i64),
                Cell::text(
                    top.iter()
                        .take(3)
                        .map(|r| r.name.as_str())
                        .collect::<Vec<_>>()
                        .join(", "),
                ),
            ]);
        }
    }
    changed.sort_by(|a, b| b.at.cmp(&a.at).then(a.path.cmp(&b.path)));
    blast.sort_by(|a, b| b[3].value.as_i64().cmp(&a[3].value.as_i64()));
    let definitions: usize = changed
        .iter()
        .map(|c| c.added.len() + c.removed.len() + c.resized.len())
        .sum();

    report.lede = {
        let files = s(changed.len(), "file", "files");
        let defs = s(definitions, "definition", "definitions");
        let callers = s(reached.len(), "caller", "callers");
        let tail = format!(
            " reach. {} read for the first time {} {} not counted as changes.",
            s(first, "file", "files"),
            ctx.window.phrase(),
            if first == 1 { "is" } else { "are" }
        );
        runs(&[
            (&files, true),
            (
                &format!(
                    " this machine had indexed before {} re-read {}, changing ",
                    if changed.len() == 1 { "was" } else { "were" },
                    ctx.window.phrase()
                ),
                false,
            ),
            (&defs, true),
            (" that ", false),
            (&callers, true),
            (&tail, false),
        ])
    };
    report.meta = vec![
        window_meta(ctx, Some("by when the store re-read each file")),
        stores_meta(ctx),
        Meta {
            label: "Blast radius".into(),
            lines: vec![
                "graph::impact, 2 hops".into(),
                "resolved edges only · ambiguous not crossed".into(),
            ],
            chips: Vec::new(),
        },
    ];
    report.kpis = vec![
        Kpi {
            label: "Files re-read".into(),
            figure: Figure::count(
                changed.len() as i64,
                "files",
                format!(
                    "held definitions before · {} first reads not counted",
                    num::int(first as i64)
                ),
            ),
            lead: true,
        },
        Kpi {
            label: "Definitions changed".into(),
            figure: Figure::count(
                definitions as i64,
                "definitions",
                "added, removed or resized",
            ),
            lead: false,
        },
        Kpi {
            label: "Callers reached".into(),
            figure: Figure::count(
                reached.len() as i64,
                "definitions",
                "within two hops of a changed definition",
            ),
            lead: false,
        },
        Kpi {
            label: "Stores".into(),
            figure: Figure::count(
                stores_changed.len() as i64,
                "stores",
                format!(
                    "of {} in scope with a change",
                    num::int(ctx.scope.len() as i64)
                ),
            ),
            lead: false,
        },
    ];
    let more = changed.len().saturating_sub(LISTED);
    let files_table = Table {
        id: "changed_files",
        title: "Changed files".into(),
        meta: "re-read, not rewritten · newest first".into(),
        columns: vec![
            col("file", "File", Kind::Id),
            col("store", "Store", Kind::Text),
            col("reread_at", "Re-read", Kind::Id),
            col("added", "Added", Kind::Num),
            col("removed", "Removed", Kind::Num),
            col("changed", "Changed", Kind::Num),
        ],
        rows: changed
            .iter()
            .take(LISTED)
            .map(|c| {
                vec![
                    Cell::text(ctx.path(&c.path)),
                    Cell::text(&c.store),
                    Cell::stamp(c.at),
                    Cell::int(c.added.len() as i64),
                    Cell::int(c.removed.len() as i64),
                    Cell::int(c.resized.len() as i64),
                ]
            })
            .collect(),
        total: None,
        more,
        note: Some(
            "A file is counted as re-read when a re-index inside the window retired the \
             definitions it held. The store records when it read a file, not when anybody \
             changed it, and it cannot tell a re-read file that held no definitions from a \
             first read; those are counted as first reads."
                .into(),
        ),
        empty: "No file this machine had indexed before was re-read in this window.".into(),
    };
    let blast_table = Table {
        id: "blast_radius",
        title: "Blast radius".into(),
        meta: "changed definition → what reaches it".into(),
        columns: vec![
            col("symbol", "Symbol", Kind::Strong),
            col("file", "File", Kind::Id),
            col("delta", "Delta", Kind::Text),
            col("callers", "Callers", Kind::Num),
            col("files", "Files", Kind::Num),
            col("top_callers", "Nearest callers", Kind::Muted),
        ],
        rows: blast,
        total: None,
        more: walked,
        note: Some(format!(
            "From the impact walk over each changed definition, two hops, resolved edges only; \
             the first {BLAST_NAMES} per store are walked. A removed definition is reached only \
             where another definition of its name remains."
        )),
        empty: "No changed definition to walk from.".into(),
    };
    report.blocks = vec![
        Block::Table(files_table),
        Block::Table(blast_table),
        recompute(ctx, "change", false),
    ];
    Ok(())
}

// ---------------------------------------------------------------- health

fn health(ctx: &Ctx<'_>, report: &mut Report) -> Result<()> {
    let (mut files, mut chunks, mut bytes) = (0i64, 0i64, 0i64);
    let (mut unresolved, mut several) = (0i64, 0i64);
    let (mut settled, mut answerable) = (0usize, 0usize);
    let mut rows = Vec::new();
    let mut refused: BTreeMap<(String, String), i64> = BTreeMap::new();
    for (label, store) in ctx.scope {
        let (f, c, b) = crate::store::stats(store.db())?;
        files += f;
        chunks += c;
        bytes += b;
        let (_, distinct) = crate::store::unresolved_targets(store.db(), 5)?;
        unresolved += distinct;
        several += crate::store::names_with_several_definitions(store.db())?;
        for row in crate::store::coverage_by_language(store.db())? {
            if row.files == 0 && row.definitions == 0 {
                continue;
            }
            settled += row.extracted + row.resolved;
            answerable += row.extracted + row.resolved + row.ambiguous;
            rows.push(vec![
                Cell::text(*label),
                Cell::text(&row.language),
                Cell::int(row.files as i64),
                Cell::int(row.parser_failed as i64),
                Cell::int(row.definitions as i64),
                Cell::int(row.ambiguous as i64),
                Cell::int(row.unresolved as i64),
                // No call edge to settle (prose, data) is not 0 % settled.
                if row.extracted + row.resolved + row.ambiguous == 0 {
                    Cell::empty()
                } else {
                    Cell::share(row.settled_share() as f64 / 100.0)
                },
            ]);
        }
        for r in crate::store::refusals(store.db())? {
            *refused
                .entry((label.to_string(), r.class.clone()))
                .or_default() += i64::from(r.files);
        }
    }
    let share = if answerable == 0 {
        0.0
    } else {
        settled as f64 / answerable as f64
    };
    report.lede = {
        let files_text = s(files as usize, "file", "files");
        let chunks_text = s(chunks as usize, "chunk", "chunks");
        let share_text = num::pct(share);
        runs(&[
            ("This machine's index holds ", false),
            (&files_text, true),
            (
                &format!(" in {}, cut into ", s(ctx.scope.len(), "store", "stores")),
                false,
            ),
            (&chunks_text, true),
            ("; the graph settles ", false),
            (&share_text, true),
            (" of the call edges it can answer on one definition.", false),
        ])
    };
    report.meta = vec![
        window_meta(ctx, Some("the index as it stands · no date to narrow by")),
        stores_meta(ctx),
    ];
    report.kpis = vec![
        Kpi {
            label: "Files".into(),
            figure: Figure::count(files, "files", "indexed"),
            lead: true,
        },
        Kpi {
            label: "Chunks".into(),
            figure: Figure::count(chunks, "chunks", "embedded"),
            lead: false,
        },
        Kpi {
            label: "Source read".into(),
            figure: Figure::bytes(bytes, "the bytes the index was built from"),
            lead: false,
        },
        Kpi {
            label: "Graph settled".into(),
            figure: Figure::share(
                settled as i64,
                answerable as i64,
                "answerable call edges on one definition",
            ),
            lead: false,
        },
    ];
    let languages = Table {
        id: "languages",
        title: "What the graph covers, per language".into(),
        meta: "`semlith stats` prints these rows".into(),
        columns: vec![
            col("store", "Store", Kind::Text),
            col("language", "Language", Kind::Strong),
            col("files", "Files", Kind::Num),
            col("unparsed", "Unparsed", Kind::Num),
            col("definitions", "Definitions", Kind::Num),
            col("ambiguous", "Ambiguous", Kind::Num),
            col("unresolved", "Unresolved", Kind::Num),
            col("settled_ratio", "Settled", Kind::Bar),
        ],
        rows,
        total: None,
        more: 0,
        note: Some(
            "An absent edge and a file the parser gave up on are different problems, which is \
             why they are different columns."
                .into(),
        ),
        empty: "No language with files or definitions.".into(),
    };
    let skipped = Table {
        id: "not_indexed",
        title: "Not indexed".into(),
        meta: "refused or skipped, by reason".into(),
        columns: vec![
            col("store", "Store", Kind::Text),
            col("reason", "Reason", Kind::Strong),
            col("files", "Files", Kind::Num),
        ],
        rows: refused
            .into_iter()
            .map(|((store, class), n)| vec![Cell::text(store), Cell::text(class), Cell::int(n)])
            .collect(),
        total: None,
        more: 0,
        note: Some("`semlith refused` lists each one and what a person decided about it.".into()),
        empty: "Nothing was refused or skipped.".into(),
    };
    let facts = Block::Facts {
        title: "The graph".into(),
        meta: "what an ambiguous or missing edge is made of".into(),
        facts: vec![
            Fact {
                label: "No definition here".into(),
                figure: Figure::count(unresolved, "names", "call targets the graph cannot find"),
            },
            Fact {
                label: "Several definitions".into(),
                figure: Figure::count(several, "names", "the population of every ambiguous edge"),
            },
            Fact {
                label: "Stores".into(),
                figure: Figure::count(ctx.scope.len() as i64, "stores", "in scope"),
            },
        ],
        note: None,
    };
    report.blocks = vec![
        Block::Table(languages),
        Block::Table(skipped),
        facts,
        recompute(ctx, "health", false),
    ];
    Ok(())
}

// ------------------------------------------------------------------ gaps

/// Rows each gaps table lists.
const GAPS_LISTED: usize = 50;

fn gaps(ctx: &Ctx<'_>, report: &mut Report) -> Result<()> {
    let t = tally(ctx)?;
    // Most asked first, as the aggregation orders them.
    let asked: Vec<&crate::ledger::GapFigures> = t.f.gaps.iter().collect();
    let times: i64 = asked.iter().map(|g| g.asked).sum();
    let mut missing = Vec::new();
    let (mut distinct, mut several, mut builtins) = (0i64, 0i64, 0i64);
    for (label, store) in ctx.scope {
        let (targets, count) = crate::store::unresolved_targets(store.db(), 20)?;
        distinct += count;
        for (name, edges) in targets {
            missing.push(vec![Cell::text(name), Cell::int(edges), Cell::text(*label)]);
        }
        several += crate::store::names_with_several_definitions(store.db())?;
        builtins += crate::store::builtin_targets(store.db())?;
    }
    missing.sort_by(|a, b| b[1].value.as_i64().cmp(&a[1].value.as_i64()));
    report.lede = {
        let q = s(asked.len(), "question", "questions");
        let n = s(distinct as usize, "name", "names");
        runs(&[
            ("Agents asked ", false),
            (&q, true),
            (
                &format!(
                    " {} that semlith found nothing for, and the code calls ",
                    ctx.window.phrase()
                ),
                false,
            ),
            (&n, true),
            (
                " this corpus does not define. A docs to-do list and a dependency list, kept apart.",
                false,
            ),
        ])
    };
    report.meta = vec![
        window_meta(
            ctx,
            Some("questions by when asked · call targets as indexed now"),
        ),
        stores_meta(ctx),
        Meta {
            label: "Query text".into(),
            lines: vec![if ctx.options.redact {
                "keyed per machine · not reversible".into()
            } else {
                "as the agent asked it".into()
            }],
            chips: vec![Chip {
                text: if ctx.options.redact {
                    "hashed".into()
                } else {
                    "plain".into()
                },
                tone: if ctx.options.redact { "amber" } else { "" },
            }],
        },
    ];
    report.kpis = vec![
        Kpi {
            label: "Unanswered questions".into(),
            figure: Figure::count(
                asked.len() as i64,
                "queries",
                format!("asked {} times", num::int(times)),
            ),
            lead: true,
        },
        Kpi {
            label: "Names not found".into(),
            figure: Figure::count(distinct, "names", "called, never defined here"),
            lead: false,
        },
        Kpi {
            label: "Names that mislead".into(),
            figure: Figure::count(several, "names", "defined more than once"),
            lead: false,
        },
    ];
    let more = asked.len().saturating_sub(GAPS_LISTED);
    report.blocks =
        vec![
        Block::Table(Table {
            id: "asked",
            title: "Asked and not answered".into(),
            meta: "agent queries with no hit in any store".into(),
            columns: vec![
                col("query", "Query", if ctx.options.redact { Kind::Id } else { Kind::Strong }),
                col("times", "Times", Kind::Num),
                col("last_asked", "Last asked", Kind::Id),
                col("stores", "Stores", Kind::Text),
            ],
            rows: asked
                .iter()
                .take(GAPS_LISTED)
                .map(|g| {
                    vec![
                        Cell::text(ctx.said(&g.query)),
                        Cell::int(g.asked),
                        Cell::stamp(g.last),
                        Cell::text(g.stores.join(" · ")),
                    ]
                })
                .collect(),
            total: None,
            more,
            note: Some(
                "A question that found nothing is a document this corpus does not hold, or words \
                 nobody wrote. Refunds and human searches are not questions agents asked semlith, \
                 and are not here."
                    .into(),
            ),
            empty: "Every agent question in this window found something.".into(),
        }),
        Block::Table(Table {
            id: "called",
            title: "Called and not found".into(),
            meta: "the 20 most-called per store".into(),
            columns: vec![
                col("name", "Name", Kind::Id),
                col("edges", "Edges", Kind::Num),
                col("store", "Store", Kind::Text),
            ],
            rows: missing,
            total: None,
            more: 0,
            note: Some(format!(
                "A call target with no definition is code this corpus does not contain: a \
                 dependency nobody indexed, or a typo. Each language's builtins and standard \
                 library ({} call {}, such as Go's len and fmt.Errorf) are left out: they are \
                 not missing code.",
                num::int(builtins),
                if builtins == 1 { "target" } else { "targets" }
            )),
            empty: "Every call target has a definition here.".into(),
        }),
        recompute(ctx, "gaps", true),
    ];
    Ok(())
}

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

    #[test]
    fn one_formatter_prints_what_the_portal_prints() {
        // app.js: `new Intl.NumberFormat("en-US")` for counts and `pct` for
        // shares. The same inputs, the same strings.
        assert_eq!(num::int(736_397), "736,397");
        assert_eq!(num::int(-1_234_567), "-1,234,567");
        assert_eq!(num::int(0), "0");
        assert_eq!(num::usd(1.4235), "$1.42");
        assert_eq!(num::usd(12_345.6), "$12,345.60");
        assert_eq!(num::pct(0.8), "80%");
        assert_eq!(num::pct(0.054), "5.4%");
        assert_eq!(num::times(325.27), "325.3×");
        assert_eq!(num::times(1234.0), "1,234.0×");
        assert_eq!(num::ms(67_400), "67 ms");
        let app = include_str!("../portal/app.js");
        assert!(
            app.contains("new Intl.NumberFormat(\"en-US\")"),
            "app.js left the one rule"
        );
        assert!(
            app.contains("value < 10 && value > 0 ? value.toFixed(1) : Math.round(value)"),
            "app.js's pct no longer matches num::pct"
        );
    }

    #[test]
    fn the_portal_opens_at_the_default_window() {
        let app = include_str!("../portal/app.js");
        assert!(
            app.contains(&format!("window: \"{DEFAULT_WINDOW}\"")),
            "the Reports page's starting window is not report::DEFAULT_WINDOW"
        );
    }

    #[test]
    fn a_window_name_resolves_or_says_which_there_are() {
        assert_eq!(window_of(None).unwrap(), Window::Days(30));
        assert_eq!(window_of(Some("week")).unwrap(), Window::Days(7));
        assert_eq!(window_of(Some("all")).unwrap(), Window::All);
        let e = window_of(Some("fortnight")).unwrap_err().to_string();
        for wanted in ["all", "day", "week", "month", "quarter"] {
            assert!(e.contains(wanted), "{e} does not name {wanted}");
        }
        for (name, window) in WINDOWS {
            assert_eq!(window.name(), name);
        }
    }

    #[test]
    fn a_model_is_priced_however_its_name_is_punctuated() {
        for spelling in ["Opus 5", "opus 5", "opus-5", "opus_5", "OPUS5"] {
            let (name, per_million) = price_of(spelling);
            assert_eq!(name, "Opus 5", "{spelling} priced as {name}");
            let table = crate::prices::built_in();
            let opus = crate::prices::lookup(&table, None, "claude-opus-5")
                .unwrap()
                .1;
            assert_eq!(per_million, opus.rates.input);
        }
        assert_eq!(price_of("gpt-5.5").0, "gpt-5.5");
        assert_eq!(
            price_of("Gemini-3.1-Pro-Preview").0,
            "gemini-3.1-pro-preview"
        );
    }

    #[test]
    fn the_picker_offers_every_vendor() {
        let names: Vec<String> = savings_models().into_iter().map(|(n, _)| n).collect();
        assert_eq!(names.len(), SAVINGS_MODELS.len(), "{names:?}");
        for vendor in ["claude", "gpt", "gemini", "grok", "deepseek"] {
            assert!(names.iter().any(|n| n.starts_with(vendor)), "no {vendor}");
        }
    }

    #[test]
    fn a_model_nobody_prices_is_refused_rather_than_defaulted() {
        assert!(price_named("gpt-9").is_none());
        assert!(price_named("").is_none());
        assert_eq!(
            price_named("haiku 4.5").map(|(n, _)| n).as_deref(),
            Some("Haiku 4.5")
        );
        let names = price_names();
        for wanted in ["semlith prices --model", DEFAULT_MODEL] {
            assert!(names.contains(wanted), "{names} does not name {wanted}");
        }
    }

    #[test]
    fn a_figure_serializes_as_data() {
        let f = Figure::usd(1.4235, "at x");
        let json = serde_json::to_value(&f).unwrap();
        assert_eq!(json["value"], 1.4235);
        assert_eq!(json["unit"], "money");
        assert_eq!(json["currency"], "USD");
        assert_eq!(json["display"], "$1.42");
        let share = serde_json::to_value(Figure::share(8, 10, "of 10")).unwrap();
        assert_eq!(share["value"], 0.8);
        assert_eq!(share["display"], "80%");
    }
}