ktstr 0.4.9

Test harness for Linux process schedulers
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
//! Join, aggregate, and render the comparison between two
//! [`HostStateSnapshot`]s.
//!
//! Design summary: the per-thread profiler emits
//! one snapshot per run. Comparison groups threads within each
//! snapshot by `(pcomm, comm)` (or by cgroup / comm, see
//! [`GroupBy`]), aggregates every metric per the rule on its
//! [`HostStateMetricDef`], then matches groups across the two
//! snapshots and emits one row per `(group, metric)` pair. Groups
//! present on only one side surface as unmatched entries rather
//! than imaginary zero-valued rows — a row is missing because
//! the process did not exist, not because it did zero work.
//!
//! No judgment labels. The comparison prints raw numbers and
//! percent delta; interpretation (regression vs improvement) is
//! scheduler-specific and left to the user. This mirrors the
//! no-label principle for the broader stats comparison pipeline
//! (see the `stats.rs` module doc).

use std::collections::BTreeMap;
use std::fmt;
use std::path::Path;

use anyhow::Context;

use crate::host_state::{CgroupStats, HostStateSnapshot, ThreadState};

/// Grouping key for the host-state compare.
///
/// The default is [`GroupBy::Pcomm`] — aggregate every thread
/// belonging to the same process name together. The other
/// variants exist for operators who want to slice along a
/// different axis: `Cgroup` groups by cgroup path (useful for
/// container-per-workload deployments), `Comm` groups by thread
/// name across every process (useful when a thread-pool name
/// like `tokio-worker` spans many binaries).
#[derive(Debug, Clone, Copy, PartialEq, Eq, clap::ValueEnum)]
pub enum GroupBy {
    /// Group by process name (`pcomm`). Default.
    Pcomm,
    /// Group by cgroup path. Cgroup-level enrichment is surfaced
    /// in the output alongside the aggregated thread metrics.
    Cgroup,
    /// Group by thread name (`comm`) across every process.
    Comm,
}

/// Options controlling [`compare`].
#[derive(Debug, Clone, Default)]
#[non_exhaustive]
pub struct CompareOptions {
    pub group_by: GroupByOrDefault,
    /// Glob patterns that collapse dynamic cgroup path segments
    /// to a canonical form before grouping. Applied in listed
    /// order; each pattern that matches a thread's cgroup path
    /// rewrites the matched segments with the literal portions
    /// of the pattern. See [`flatten_cgroup_path`] for the
    /// rewrite rule and examples.
    pub cgroup_flatten: Vec<String>,
}

/// Newtype wrapper around [`GroupBy`] that defaults to
/// [`GroupBy::Pcomm`]. Separate type so `CompareOptions::default()`
/// does not need to spell out every field.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct GroupByOrDefault(pub GroupBy);

impl Default for GroupByOrDefault {
    fn default() -> Self {
        Self(GroupBy::Pcomm)
    }
}

impl From<GroupBy> for GroupByOrDefault {
    fn from(g: GroupBy) -> Self {
        Self(g)
    }
}

/// Aggregation rule for a single metric.
///
/// Encoded as an enum rather than a trait object so the registry
/// table ([`HOST_STATE_METRICS`]) can live in static memory. Each
/// variant carries the reader that extracts the per-thread value
/// — the reader and rule are paired by construction so a new
/// metric cannot register a sum rule against a string accessor.
#[derive(Debug, Clone, Copy)]
pub enum AggRule {
    /// Sum across the group. Used for cumulative counters
    /// (run_time, faults, I/O). Delta is the signed difference
    /// between candidate and baseline sums.
    Sum(fn(&ThreadState) -> u64),
    /// Ordinal integer, aggregated as the observed [min, max].
    /// Delta uses the midpoint of each range as the scalar;
    /// output prints both the range and the delta.
    OrdinalRange(fn(&ThreadState) -> i64),
    /// Categorical string, aggregated as the mode (most frequent)
    /// value. Delta is textual: "same" if both modes agree,
    /// "differs" otherwise — there is no arithmetic on a policy
    /// name.
    Mode(fn(&ThreadState) -> String),
    /// CPU affinity set. Aggregated as num_cpus range across the
    /// group plus a uniform-cpuset rendering when every thread
    /// shared the same allowed set.
    Affinity(fn(&ThreadState) -> Vec<u32>),
}

/// One metric exposed by the comparison pipeline.
#[derive(Debug, Clone, Copy)]
#[non_exhaustive]
pub struct HostStateMetricDef {
    pub name: &'static str,
    /// Display unit appended to the numeric value in output
    /// cells. Empty string means "unitless count".
    pub unit: &'static str,
    pub rule: AggRule,
}

/// Registry of per-thread metrics. Order here is the default
/// display order for rows that have no numeric delta to sort by
/// (ties fall back to registry order). Names are the ASCII
/// short-form used in capture code; long-form display is the
/// same — no translation layer.
pub static HOST_STATE_METRICS: &[HostStateMetricDef] = &[
    // identity / structural (non-numeric aggregation)
    HostStateMetricDef {
        name: "policy",
        unit: "",
        rule: AggRule::Mode(|t| t.policy.clone()),
    },
    HostStateMetricDef {
        name: "nice",
        unit: "",
        rule: AggRule::OrdinalRange(|t| t.nice as i64),
    },
    HostStateMetricDef {
        name: "cpu_affinity",
        unit: "",
        rule: AggRule::Affinity(|t| t.cpu_affinity.clone()),
    },
    // scheduling
    HostStateMetricDef {
        name: "run_time_ns",
        unit: "ns",
        rule: AggRule::Sum(|t| t.run_time_ns),
    },
    HostStateMetricDef {
        name: "wait_time_ns",
        unit: "ns",
        rule: AggRule::Sum(|t| t.wait_time_ns),
    },
    HostStateMetricDef {
        name: "timeslices",
        unit: "",
        rule: AggRule::Sum(|t| t.timeslices),
    },
    HostStateMetricDef {
        name: "voluntary_csw",
        unit: "",
        rule: AggRule::Sum(|t| t.voluntary_csw),
    },
    HostStateMetricDef {
        name: "nonvoluntary_csw",
        unit: "",
        rule: AggRule::Sum(|t| t.nonvoluntary_csw),
    },
    HostStateMetricDef {
        name: "nr_wakeups",
        unit: "",
        rule: AggRule::Sum(|t| t.nr_wakeups),
    },
    HostStateMetricDef {
        name: "nr_wakeups_local",
        unit: "",
        rule: AggRule::Sum(|t| t.nr_wakeups_local),
    },
    HostStateMetricDef {
        name: "nr_wakeups_remote",
        unit: "",
        rule: AggRule::Sum(|t| t.nr_wakeups_remote),
    },
    HostStateMetricDef {
        name: "nr_wakeups_sync",
        unit: "",
        rule: AggRule::Sum(|t| t.nr_wakeups_sync),
    },
    HostStateMetricDef {
        name: "nr_wakeups_migrate",
        unit: "",
        rule: AggRule::Sum(|t| t.nr_wakeups_migrate),
    },
    HostStateMetricDef {
        name: "nr_wakeups_idle",
        unit: "",
        rule: AggRule::Sum(|t| t.nr_wakeups_idle),
    },
    HostStateMetricDef {
        name: "nr_migrations",
        unit: "",
        rule: AggRule::Sum(|t| t.nr_migrations),
    },
    HostStateMetricDef {
        name: "wait_sum",
        unit: "ns",
        rule: AggRule::Sum(|t| t.wait_sum),
    },
    HostStateMetricDef {
        name: "wait_count",
        unit: "",
        rule: AggRule::Sum(|t| t.wait_count),
    },
    HostStateMetricDef {
        name: "sleep_sum",
        unit: "ns",
        rule: AggRule::Sum(|t| t.sleep_sum),
    },
    // No `sleep_count` metric: the kernel does not emit
    // that counter. The prior entry matched a ghost field
    // that never populated; removed alongside the parser
    // key fix (`sum_sleep_runtime` is the real key for
    // sleep_sum, and there is no per-event counter pair).
    HostStateMetricDef {
        name: "block_sum",
        unit: "ns",
        rule: AggRule::Sum(|t| t.block_sum),
    },
    HostStateMetricDef {
        name: "block_count",
        unit: "",
        rule: AggRule::Sum(|t| t.block_count),
    },
    HostStateMetricDef {
        name: "iowait_sum",
        unit: "ns",
        rule: AggRule::Sum(|t| t.iowait_sum),
    },
    HostStateMetricDef {
        name: "iowait_count",
        unit: "",
        rule: AggRule::Sum(|t| t.iowait_count),
    },
    // memory
    HostStateMetricDef {
        name: "allocated_bytes",
        unit: "B",
        rule: AggRule::Sum(|t| t.allocated_bytes),
    },
    HostStateMetricDef {
        name: "deallocated_bytes",
        unit: "B",
        rule: AggRule::Sum(|t| t.deallocated_bytes),
    },
    HostStateMetricDef {
        name: "minflt",
        unit: "",
        rule: AggRule::Sum(|t| t.minflt),
    },
    HostStateMetricDef {
        name: "majflt",
        unit: "",
        rule: AggRule::Sum(|t| t.majflt),
    },
    // I/O
    HostStateMetricDef {
        name: "rchar",
        unit: "B",
        rule: AggRule::Sum(|t| t.rchar),
    },
    HostStateMetricDef {
        name: "wchar",
        unit: "B",
        rule: AggRule::Sum(|t| t.wchar),
    },
    HostStateMetricDef {
        name: "syscr",
        unit: "",
        rule: AggRule::Sum(|t| t.syscr),
    },
    HostStateMetricDef {
        name: "syscw",
        unit: "",
        rule: AggRule::Sum(|t| t.syscw),
    },
    HostStateMetricDef {
        name: "read_bytes",
        unit: "B",
        rule: AggRule::Sum(|t| t.read_bytes),
    },
    HostStateMetricDef {
        name: "write_bytes",
        unit: "B",
        rule: AggRule::Sum(|t| t.write_bytes),
    },
];

/// Aggregated metric value for a single [`ThreadGroup`].
///
/// Carries both a numeric projection (used for delta math and
/// sort order) and a display form. Not every rule produces a
/// numeric — [`AggRule::Mode`] aggregates a policy string, which
/// has no scalar — so the numeric is optional and rows without
/// one fall to the bottom of the default sort.
#[derive(Debug, Clone)]
pub enum Aggregated {
    Sum(u64),
    OrdinalRange {
        min: i64,
        max: i64,
    },
    Mode {
        value: String,
        count: usize,
        total: usize,
    },
    Affinity(AffinitySummary),
}

/// CPU-affinity aggregation result.
///
/// `uniform` is `Some(cpus)` when every thread in the group shared
/// the same allowed set; otherwise heterogeneous and the renderer
/// emits "N-M cpus (mixed)".
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct AffinitySummary {
    pub min_cpus: usize,
    pub max_cpus: usize,
    pub uniform: Option<Vec<u32>>,
}

impl Aggregated {
    /// Scalar projection for delta math. `None` when the rule
    /// produces no meaningful scalar (categorical mode, affinity
    /// with heterogeneous cpusets).
    pub fn numeric(&self) -> Option<f64> {
        match self {
            Aggregated::Sum(v) => Some(*v as f64),
            Aggregated::OrdinalRange { min, max } => {
                // Midpoint: keeps a min→max shift on one end visible
                // in the delta without privileging either bound.
                Some((*min as f64 + *max as f64) / 2.0)
            }
            Aggregated::Mode { .. } => None,
            Aggregated::Affinity(s) => {
                // Number of allowed CPUs is the natural scalar. When
                // the group is uniform, `min_cpus == max_cpus`; when
                // heterogeneous, midpoint parallels OrdinalRange.
                Some((s.min_cpus as f64 + s.max_cpus as f64) / 2.0)
            }
        }
    }
}

impl fmt::Display for Aggregated {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Aggregated::Sum(v) => write!(f, "{v}"),
            Aggregated::OrdinalRange { min, max } => {
                if min == max {
                    write!(f, "{min}")
                } else {
                    write!(f, "{min}..{max}")
                }
            }
            Aggregated::Mode {
                value,
                count,
                total,
            } => {
                if count == total {
                    write!(f, "{value}")
                } else {
                    write!(f, "{value} ({count}/{total})")
                }
            }
            Aggregated::Affinity(s) => {
                if let Some(cpus) = &s.uniform {
                    let n = cpus.len();
                    let range = format_cpu_range(cpus);
                    write!(f, "{n} cpus ({range})")
                } else if s.min_cpus == s.max_cpus {
                    write!(f, "{} cpus (mixed)", s.min_cpus)
                } else {
                    write!(f, "{}-{} cpus (mixed)", s.min_cpus, s.max_cpus)
                }
            }
        }
    }
}

/// Aggregated metrics for every thread matched by one group key.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct ThreadGroup {
    pub key: String,
    pub thread_count: usize,
    /// Metric name → aggregated value. Entries are created for
    /// every registered metric; absent keys signal a missed
    /// aggregation step, not a skip.
    pub metrics: BTreeMap<String, Aggregated>,
    /// Only populated when grouping by cgroup — carries the cgroup
    /// v2 enrichment counters (cpu.stat, memory.current) for that
    /// path. Nested here so the renderer can surface them
    /// alongside the thread-metric rows without a second lookup.
    pub cgroup_stats: Option<CgroupStats>,
}

/// One row in the comparison table: `(group, metric)` pair with
/// aggregated values from both sides.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct DiffRow {
    pub group_key: String,
    pub thread_count_a: usize,
    pub thread_count_b: usize,
    pub metric_name: &'static str,
    pub metric_unit: &'static str,
    pub baseline: Aggregated,
    pub candidate: Aggregated,
    /// Signed candidate − baseline for numeric-capable rules.
    pub delta: Option<f64>,
    /// `delta / baseline` as a fraction. `None` when baseline is
    /// zero or the row has no numeric projection.
    pub delta_pct: Option<f64>,
}

impl DiffRow {
    /// Sort key for "biggest absolute delta %". Numeric rows
    /// with a non-zero baseline sort by `|delta_pct|`; numeric
    /// rows with a zero baseline sort by `|delta|` scaled by a
    /// large constant so any non-zero candidate dominates
    /// percent-based rows; non-numeric rows sink to the bottom.
    fn sort_key(&self) -> f64 {
        if let Some(p) = self.delta_pct {
            p.abs()
        } else if let Some(d) = self.delta {
            // Baseline was zero (delta_pct undefined) but candidate
            // is some value — still a visible change. Inflate so it
            // beats percent-only rows in the sort.
            d.abs() * 1e9
        } else {
            f64::NEG_INFINITY
        }
    }
}

/// Full comparison result.
#[derive(Debug, Clone, Default)]
#[non_exhaustive]
pub struct HostStateDiff {
    pub rows: Vec<DiffRow>,
    /// Group keys that appeared in the baseline snapshot but not
    /// in the candidate.
    pub only_baseline: Vec<String>,
    /// Group keys that appeared in the candidate snapshot but not
    /// in the baseline.
    pub only_candidate: Vec<String>,
    /// Baseline-only cgroup-level enrichment rows, keyed by the
    /// cgroup path (after flatten). Populated only for
    /// [`GroupBy::Cgroup`].
    pub cgroup_stats_a: BTreeMap<String, CgroupStats>,
    /// Candidate-only cgroup-level enrichment rows, same shape.
    pub cgroup_stats_b: BTreeMap<String, CgroupStats>,
}

/// Compare two snapshots and produce a [`HostStateDiff`].
pub fn compare(
    baseline: &HostStateSnapshot,
    candidate: &HostStateSnapshot,
    opts: &CompareOptions,
) -> HostStateDiff {
    let flatten = compile_flatten_patterns(&opts.cgroup_flatten);
    let group_by = opts.group_by.0;
    let groups_a = build_groups(baseline, group_by, &flatten);
    let groups_b = build_groups(candidate, group_by, &flatten);

    let mut diff = HostStateDiff::default();

    for (key, group_a) in &groups_a {
        let Some(group_b) = groups_b.get(key) else {
            diff.only_baseline.push(key.clone());
            continue;
        };
        for metric in HOST_STATE_METRICS {
            let Some(a) = group_a.metrics.get(metric.name).cloned() else {
                continue;
            };
            let Some(b) = group_b.metrics.get(metric.name).cloned() else {
                continue;
            };
            diff.rows.push(build_row(
                key,
                group_a.thread_count,
                group_b.thread_count,
                metric,
                a,
                b,
            ));
        }
    }
    for key in groups_b.keys() {
        if !groups_a.contains_key(key) {
            diff.only_candidate.push(key.clone());
        }
    }
    diff.only_baseline.sort();
    diff.only_candidate.sort();

    // Stable-sort by descending |delta_pct|, ties broken by
    // ascending group_key + registry order of metric.
    diff.rows.sort_by(|a, b| {
        b.sort_key()
            .partial_cmp(&a.sort_key())
            .unwrap_or(std::cmp::Ordering::Equal)
            .then_with(|| a.group_key.cmp(&b.group_key))
    });

    if group_by == GroupBy::Cgroup {
        diff.cgroup_stats_a = flatten_cgroup_stats(&baseline.cgroup_stats, &flatten);
        diff.cgroup_stats_b = flatten_cgroup_stats(&candidate.cgroup_stats, &flatten);
    }

    diff
}

fn build_row(
    key: &str,
    n_a: usize,
    n_b: usize,
    metric: &'static HostStateMetricDef,
    a: Aggregated,
    b: Aggregated,
) -> DiffRow {
    let (delta, delta_pct) = match (a.numeric(), b.numeric()) {
        (Some(va), Some(vb)) => {
            let d = vb - va;
            let pct = if va.abs() > f64::EPSILON {
                Some(d / va)
            } else {
                None
            };
            (Some(d), pct)
        }
        _ => (None, None),
    };
    DiffRow {
        group_key: key.to_string(),
        thread_count_a: n_a,
        thread_count_b: n_b,
        metric_name: metric.name,
        metric_unit: metric.unit,
        baseline: a,
        candidate: b,
        delta,
        delta_pct,
    }
}

fn build_groups(
    snap: &HostStateSnapshot,
    group_by: GroupBy,
    flatten: &[glob::Pattern],
) -> BTreeMap<String, ThreadGroup> {
    let mut buckets: BTreeMap<String, Vec<&ThreadState>> = BTreeMap::new();
    for t in &snap.threads {
        let key = match group_by {
            GroupBy::Pcomm => t.pcomm.clone(),
            GroupBy::Comm => t.comm.clone(),
            GroupBy::Cgroup => flatten_cgroup_path(&t.cgroup, flatten),
        };
        buckets.entry(key).or_default().push(t);
    }

    let mut out = BTreeMap::new();
    for (key, threads) in buckets {
        let mut metrics = BTreeMap::new();
        for m in HOST_STATE_METRICS {
            metrics.insert(m.name.to_string(), aggregate(m.rule, &threads));
        }
        let cgroup_stats = if group_by == GroupBy::Cgroup {
            // Pick the first sampled thread's (flattened) cgroup
            // path and look up its enrichment. All threads in the
            // bucket share the flattened key by construction, so
            // the first is representative.
            threads
                .first()
                .and_then(|t| snap.cgroup_stats.get(&t.cgroup).cloned())
        } else {
            None
        };
        out.insert(
            key.clone(),
            ThreadGroup {
                key,
                thread_count: threads.len(),
                metrics,
                cgroup_stats,
            },
        );
    }
    out
}

/// Aggregate one metric across a slice of threads per its rule.
pub fn aggregate(rule: AggRule, threads: &[&ThreadState]) -> Aggregated {
    match rule {
        AggRule::Sum(f) => {
            let s = threads.iter().map(|t| f(t)).fold(0u64, u64::saturating_add);
            Aggregated::Sum(s)
        }
        AggRule::OrdinalRange(f) => {
            let mut it = threads.iter().map(|t| f(t));
            let first = it.next().unwrap_or(0);
            let (mut min, mut max) = (first, first);
            for v in it {
                if v < min {
                    min = v;
                }
                if v > max {
                    max = v;
                }
            }
            Aggregated::OrdinalRange { min, max }
        }
        AggRule::Mode(f) => {
            let total = threads.len();
            let mut counts: BTreeMap<String, usize> = BTreeMap::new();
            for t in threads {
                *counts.entry(f(t)).or_default() += 1;
            }
            // Largest count wins; ties broken by lexicographic
            // order so the output is deterministic.
            let (value, count) = counts
                .into_iter()
                .max_by(|a, b| a.1.cmp(&b.1).then(b.0.cmp(&a.0)))
                .unwrap_or_else(|| (String::new(), 0));
            Aggregated::Mode {
                value,
                count,
                total,
            }
        }
        AggRule::Affinity(f) => {
            let mut seen: Vec<Vec<u32>> = Vec::new();
            let mut min_cpus = usize::MAX;
            let mut max_cpus = 0usize;
            for t in threads {
                let cpus = f(t);
                min_cpus = min_cpus.min(cpus.len());
                max_cpus = max_cpus.max(cpus.len());
                if !seen.iter().any(|s| s == &cpus) {
                    seen.push(cpus);
                }
            }
            if threads.is_empty() {
                min_cpus = 0;
            }
            let uniform = if seen.len() == 1 {
                seen.into_iter().next()
            } else {
                None
            };
            Aggregated::Affinity(AffinitySummary {
                min_cpus,
                max_cpus,
                uniform,
            })
        }
    }
}

/// Collapse dynamic segments of a cgroup path per every pattern
/// in `patterns`. A pattern is a glob (`*` matches one segment,
/// `**` matches multiple) where the literal portions are preserved
/// and the wildcard portions are replaced with the wildcard token
/// itself. Example: pattern `/kubepods/*/workload` applied to
/// `/kubepods/pod-abc/workload` produces `/kubepods/*/workload`,
/// so two runs with different pod IDs collapse onto the same key.
///
/// Patterns are tried in listed order; the first match wins and
/// subsequent patterns are not applied. A path that matches no
/// pattern is returned verbatim.
pub fn flatten_cgroup_path(path: &str, patterns: &[glob::Pattern]) -> String {
    for p in patterns {
        if p.matches(path) {
            // The pattern itself becomes the canonical key: every
            // path matching `/kubepods/*/workload` collapses onto
            // the literal pattern string.
            return p.as_str().to_string();
        }
    }
    path.to_string()
}

fn compile_flatten_patterns(raw: &[String]) -> Vec<glob::Pattern> {
    raw.iter()
        .filter_map(|s| glob::Pattern::new(s).ok())
        .collect()
}

fn flatten_cgroup_stats(
    stats: &BTreeMap<String, CgroupStats>,
    patterns: &[glob::Pattern],
) -> BTreeMap<String, CgroupStats> {
    // When multiple input paths flatten to the same key, sum the
    // cpu/throttle counters (cumulative) and keep the max of
    // memory.current (instantaneous — summing overstates the
    // instantaneous RSS of the shared bucket). This mirrors the
    // per-thread aggregation: counters sum, instantaneous values
    // take a representative scalar.
    let mut out: BTreeMap<String, CgroupStats> = BTreeMap::new();
    for (path, cs) in stats {
        let key = flatten_cgroup_path(path, patterns);
        let agg = out.entry(key).or_default();
        agg.cpu_usage_usec = agg.cpu_usage_usec.saturating_add(cs.cpu_usage_usec);
        agg.nr_throttled = agg.nr_throttled.saturating_add(cs.nr_throttled);
        agg.throttled_usec = agg.throttled_usec.saturating_add(cs.throttled_usec);
        agg.memory_current = agg.memory_current.max(cs.memory_current);
    }
    out
}

fn format_cpu_range(cpus: &[u32]) -> String {
    // Collapse contiguous runs to `a-b`, join with commas. Assumes
    // sorted ascending; capture layer stores sorted cpusets.
    if cpus.is_empty() {
        return String::new();
    }
    let mut out = String::new();
    let mut start = cpus[0];
    let mut prev = cpus[0];
    for &c in &cpus[1..] {
        if c == prev + 1 {
            prev = c;
            continue;
        }
        if !out.is_empty() {
            out.push(',');
        }
        if start == prev {
            out.push_str(&start.to_string());
        } else {
            out.push_str(&format!("{start}-{prev}"));
        }
        start = c;
        prev = c;
    }
    if !out.is_empty() {
        out.push(',');
    }
    if start == prev {
        out.push_str(&start.to_string());
    } else {
        out.push_str(&format!("{start}-{prev}"));
    }
    out
}

/// Arguments for the `ktstr host-state compare` subcommand.
#[derive(Debug, clap::Args)]
pub struct HostStateCompareArgs {
    /// Baseline snapshot (`.hst.zst`) from `ktstr host-state -o`.
    pub baseline: std::path::PathBuf,
    /// Candidate snapshot (`.hst.zst`) from `ktstr host-state -o`.
    pub candidate: std::path::PathBuf,
    /// Grouping key. `pcomm` (default) aggregates per process
    /// name; `cgroup` per cgroup path; `comm` per thread name
    /// across every process.
    #[arg(long, value_enum, default_value_t = GroupBy::Pcomm)]
    pub group_by: GroupBy,
    /// Glob patterns that collapse dynamic cgroup path segments
    /// so structurally-equivalent cgroups across runs join
    /// correctly. Example:
    /// `--cgroup-flatten '/kubepods/*/workload'` treats different
    /// pod IDs as the same group. Repeatable.
    #[arg(long)]
    pub cgroup_flatten: Vec<String>,
}

/// Entry point for the compare CLI. Loads both snapshots,
/// computes the diff, prints the table, and returns `0` on
/// success. Exits non-zero only on I/O or parse errors; a
/// non-empty diff is data, not a failure.
pub fn run_compare(args: &HostStateCompareArgs) -> anyhow::Result<i32> {
    let baseline = HostStateSnapshot::load(&args.baseline)
        .with_context(|| format!("load baseline {}", args.baseline.display()))?;
    let candidate = HostStateSnapshot::load(&args.candidate)
        .with_context(|| format!("load candidate {}", args.candidate.display()))?;

    let opts = CompareOptions {
        group_by: args.group_by.into(),
        cgroup_flatten: args.cgroup_flatten.clone(),
    };
    let diff = compare(&baseline, &candidate, &opts);
    print_diff(&diff, &args.baseline, &args.candidate, args.group_by);
    Ok(0)
}

/// Render [`HostStateDiff`] as a table on stdout. Thin wrapper
/// over [`write_diff`] so the non-test caller keeps the
/// ergonomics of a one-line call; tests drive [`write_diff`]
/// into a `String` buffer.
pub fn print_diff(
    diff: &HostStateDiff,
    baseline_path: &Path,
    candidate_path: &Path,
    group_by: GroupBy,
) {
    let mut out = String::new();
    // Infallible: writing into a String cannot fail.
    let _ = write_diff(&mut out, diff, baseline_path, candidate_path, group_by);
    print!("{out}");
}

/// Render [`HostStateDiff`] into `w`. The formatter layer lives
/// here so tests can inspect exactly what `print_diff` would
/// emit without shelling through stdout capture. Write errors
/// propagate as [`std::fmt::Error`] — callers that write into an
/// infallible sink (`String`) can unwrap or ignore.
pub fn write_diff<W: fmt::Write>(
    w: &mut W,
    diff: &HostStateDiff,
    baseline_path: &Path,
    candidate_path: &Path,
    group_by: GroupBy,
) -> fmt::Result {
    let group_header = match group_by {
        GroupBy::Pcomm => "pcomm",
        GroupBy::Cgroup => "cgroup",
        GroupBy::Comm => "comm",
    };

    let mut table = crate::cli::new_table();
    table.set_header(vec![
        group_header,
        "threads",
        "metric",
        "baseline",
        "candidate",
        "delta",
        "%",
    ]);
    for row in &diff.rows {
        let delta_cell = match row.delta {
            Some(d) => format!("{:+.3}{}", d, row.metric_unit),
            None => match (&row.baseline, &row.candidate) {
                (Aggregated::Mode { value: a, .. }, Aggregated::Mode { value: b, .. }) => {
                    if a == b {
                        "same".to_string()
                    } else {
                        "differs".to_string()
                    }
                }
                _ => "-".to_string(),
            },
        };
        let pct_cell = match row.delta_pct {
            Some(p) => format!("{:+.1}%", p * 100.0),
            None => "-".to_string(),
        };
        let threads_cell = if row.thread_count_a == row.thread_count_b {
            row.thread_count_a.to_string()
        } else {
            format!("{}\u{2192}{}", row.thread_count_a, row.thread_count_b)
        };
        table.add_row(vec![
            row.group_key.clone(),
            threads_cell,
            row.metric_name.to_string(),
            format!("{}{}", row.baseline, row.metric_unit),
            format!("{}{}", row.candidate, row.metric_unit),
            delta_cell,
            pct_cell,
        ]);
    }
    writeln!(w, "{table}")?;

    if group_by == GroupBy::Cgroup
        && (!diff.cgroup_stats_a.is_empty() || !diff.cgroup_stats_b.is_empty())
    {
        writeln!(w)?;
        let mut ct = crate::cli::new_table();
        ct.set_header(vec![
            "cgroup",
            "cpu_usage_usec",
            "nr_throttled",
            "throttled_usec",
            "memory_current",
        ]);
        let mut all_keys: std::collections::BTreeSet<&String> =
            diff.cgroup_stats_a.keys().collect();
        all_keys.extend(diff.cgroup_stats_b.keys());
        for key in all_keys {
            let a = diff.cgroup_stats_a.get(key);
            let b = diff.cgroup_stats_b.get(key);
            ct.add_row(vec![
                key.clone(),
                cgroup_cell(a.map(|s| s.cpu_usage_usec), b.map(|s| s.cpu_usage_usec)),
                cgroup_cell(a.map(|s| s.nr_throttled), b.map(|s| s.nr_throttled)),
                cgroup_cell(a.map(|s| s.throttled_usec), b.map(|s| s.throttled_usec)),
                cgroup_cell(a.map(|s| s.memory_current), b.map(|s| s.memory_current)),
            ]);
        }
        writeln!(w, "{ct}")?;
    }

    if !diff.only_baseline.is_empty() {
        writeln!(
            w,
            "\n{} group(s) only in baseline ({}):",
            diff.only_baseline.len(),
            baseline_path.display()
        )?;
        for k in &diff.only_baseline {
            writeln!(w, "  {k}")?;
        }
    }
    if !diff.only_candidate.is_empty() {
        writeln!(
            w,
            "\n{} group(s) only in candidate ({}):",
            diff.only_candidate.len(),
            candidate_path.display()
        )?;
        for k in &diff.only_candidate {
            writeln!(w, "  {k}")?;
        }
    }
    Ok(())
}

fn cgroup_cell(a: Option<u64>, b: Option<u64>) -> String {
    match (a, b) {
        (Some(a), Some(b)) => {
            let d = b as i128 - a as i128;
            format!("{a}{b} ({d:+})")
        }
        (Some(a), None) => format!("{a} → -"),
        (None, Some(b)) => format!("- → {b}"),
        (None, None) => "-".to_string(),
    }
}

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

    fn make_thread(pcomm: &str, comm: &str) -> ThreadState {
        ThreadState {
            tid: 1,
            tgid: 1,
            pcomm: pcomm.into(),
            comm: comm.into(),
            cgroup: "/".into(),
            start_time_clock_ticks: 0,
            policy: "SCHED_OTHER".into(),
            nice: 0,
            cpu_affinity: vec![0, 1, 2, 3],
            ..ThreadState::default()
        }
    }

    fn snap_with(threads: Vec<ThreadState>) -> HostStateSnapshot {
        HostStateSnapshot {
            captured_at_unix_ns: 0,
            host: None,
            threads,
            cgroup_stats: BTreeMap::new(),
        }
    }

    #[test]
    fn sum_aggregation_totals_across_group() {
        let mut a = make_thread("app", "w1");
        a.run_time_ns = 1_000;
        let mut b = make_thread("app", "w2");
        b.run_time_ns = 3_000;
        let v = aggregate(AggRule::Sum(|t| t.run_time_ns), &[&a, &b]);
        match v {
            Aggregated::Sum(s) => assert_eq!(s, 4_000),
            other => panic!("expected Sum, got {other:?}"),
        }
    }

    #[test]
    fn sum_saturates_on_overflow() {
        let mut a = make_thread("app", "w1");
        a.run_time_ns = u64::MAX;
        let mut b = make_thread("app", "w2");
        b.run_time_ns = 5;
        let v = aggregate(AggRule::Sum(|t| t.run_time_ns), &[&a, &b]);
        match v {
            Aggregated::Sum(s) => assert_eq!(s, u64::MAX),
            other => panic!("expected Sum, got {other:?}"),
        }
    }

    #[test]
    fn ordinal_range_picks_extremes() {
        let mut a = make_thread("app", "w1");
        a.nice = -5;
        let mut b = make_thread("app", "w2");
        b.nice = 10;
        let v = aggregate(AggRule::OrdinalRange(|t| t.nice as i64), &[&a, &b]);
        match v {
            Aggregated::OrdinalRange { min, max } => {
                assert_eq!(min, -5);
                assert_eq!(max, 10);
            }
            other => panic!("expected OrdinalRange, got {other:?}"),
        }
    }

    #[test]
    fn mode_aggregation_picks_most_frequent() {
        let mut a = make_thread("app", "w1");
        a.policy = "SCHED_OTHER".into();
        let mut b = make_thread("app", "w2");
        b.policy = "SCHED_OTHER".into();
        let mut c = make_thread("app", "w3");
        c.policy = "SCHED_FIFO".into();
        let v = aggregate(AggRule::Mode(|t| t.policy.clone()), &[&a, &b, &c]);
        match v {
            Aggregated::Mode {
                value,
                count,
                total,
            } => {
                assert_eq!(value, "SCHED_OTHER");
                assert_eq!(count, 2);
                assert_eq!(total, 3);
            }
            other => panic!("expected Mode, got {other:?}"),
        }
    }

    #[test]
    fn affinity_uniform_preserves_cpuset() {
        let a = make_thread("app", "w1");
        let b = make_thread("app", "w2");
        let v = aggregate(AggRule::Affinity(|t| t.cpu_affinity.clone()), &[&a, &b]);
        match v {
            Aggregated::Affinity(s) => {
                assert_eq!(s.min_cpus, 4);
                assert_eq!(s.max_cpus, 4);
                assert_eq!(s.uniform, Some(vec![0, 1, 2, 3]));
            }
            other => panic!("expected Affinity, got {other:?}"),
        }
    }

    #[test]
    fn affinity_heterogeneous_drops_uniform() {
        let a = make_thread("app", "w1");
        let mut b = make_thread("app", "w2");
        b.cpu_affinity = vec![4, 5];
        let v = aggregate(AggRule::Affinity(|t| t.cpu_affinity.clone()), &[&a, &b]);
        match v {
            Aggregated::Affinity(s) => {
                assert_eq!(s.min_cpus, 2);
                assert_eq!(s.max_cpus, 4);
                assert!(s.uniform.is_none());
            }
            other => panic!("expected Affinity, got {other:?}"),
        }
    }

    #[test]
    fn format_cpu_range_collapses_contiguous_runs() {
        assert_eq!(format_cpu_range(&[0, 1, 2, 3]), "0-3");
        assert_eq!(format_cpu_range(&[0, 1, 4, 5, 7]), "0-1,4-5,7");
        assert_eq!(format_cpu_range(&[3]), "3");
        assert_eq!(format_cpu_range(&[]), "");
    }

    #[test]
    fn flatten_cgroup_path_collapses_via_pattern() {
        let pats = compile_flatten_patterns(&["/kubepods/*/workload".into()]);
        let out = flatten_cgroup_path("/kubepods/pod-abc-123/workload", &pats);
        assert_eq!(out, "/kubepods/*/workload");
    }

    #[test]
    fn flatten_cgroup_path_falls_through_unmatched() {
        let pats = compile_flatten_patterns(&["/kubepods/*/workload".into()]);
        assert_eq!(
            flatten_cgroup_path("/system.slice/sshd.service", &pats),
            "/system.slice/sshd.service",
        );
    }

    #[test]
    fn compare_emits_rows_for_matched_groups() {
        let mut ta = make_thread("app", "w1");
        ta.run_time_ns = 1_000;
        let mut tb = make_thread("app", "w1");
        tb.run_time_ns = 2_000;
        let a = snap_with(vec![ta]);
        let b = snap_with(vec![tb]);
        let diff = compare(&a, &b, &CompareOptions::default());
        let run_time = diff
            .rows
            .iter()
            .find(|r| r.metric_name == "run_time_ns")
            .expect("run_time_ns row");
        assert_eq!(run_time.group_key, "app");
        assert_eq!(run_time.delta, Some(1_000.0));
        assert!((run_time.delta_pct.unwrap() - 1.0).abs() < 1e-9);
    }

    #[test]
    fn compare_reports_unmatched_groups() {
        let a = snap_with(vec![make_thread("only_a", "w1")]);
        let b = snap_with(vec![make_thread("only_b", "w1")]);
        let diff = compare(&a, &b, &CompareOptions::default());
        assert_eq!(diff.only_baseline, vec!["only_a".to_string()]);
        assert_eq!(diff.only_candidate, vec!["only_b".to_string()]);
    }

    #[test]
    fn compare_sorts_by_abs_delta_pct_descending() {
        // Build two baseline threads and two candidate threads:
        // "big" swings 10x, "small" swings 1.1x. After compare,
        // the "big" row must sort before "small".
        let mut a1 = make_thread("big", "w");
        a1.run_time_ns = 100;
        let mut a2 = make_thread("small", "w");
        a2.run_time_ns = 1_000;
        let mut b1 = make_thread("big", "w");
        b1.run_time_ns = 1_000;
        let mut b2 = make_thread("small", "w");
        b2.run_time_ns = 1_100;
        let diff = compare(
            &snap_with(vec![a1, a2]),
            &snap_with(vec![b1, b2]),
            &CompareOptions::default(),
        );
        let run_rows: Vec<&DiffRow> = diff
            .rows
            .iter()
            .filter(|r| r.metric_name == "run_time_ns")
            .collect();
        assert_eq!(run_rows[0].group_key, "big");
        assert_eq!(run_rows[1].group_key, "small");
    }

    #[test]
    fn group_by_cgroup_applies_flatten_patterns() {
        let mut ta = make_thread("app", "w1");
        ta.cgroup = "/kubepods/pod-xxx/workload".into();
        ta.run_time_ns = 1_000;
        let mut tb = make_thread("app", "w1");
        tb.cgroup = "/kubepods/pod-yyy/workload".into();
        tb.run_time_ns = 2_000;
        let opts = CompareOptions {
            group_by: GroupBy::Cgroup.into(),
            cgroup_flatten: vec!["/kubepods/*/workload".into()],
        };
        let diff = compare(&snap_with(vec![ta]), &snap_with(vec![tb]), &opts);
        assert!(diff.only_baseline.is_empty(), "{:?}", diff.only_baseline);
        assert!(diff.only_candidate.is_empty(), "{:?}", diff.only_candidate,);
        assert!(
            diff.rows
                .iter()
                .any(|r| r.group_key == "/kubepods/*/workload"),
            "rows={:?}",
            diff.rows.iter().map(|r| &r.group_key).collect::<Vec<_>>(),
        );
    }

    #[test]
    fn group_by_cgroup_surfaces_enrichment_on_diff() {
        let mut ta = make_thread("app", "w1");
        ta.cgroup = "/app".into();
        let mut snap_a = snap_with(vec![ta]);
        snap_a.cgroup_stats.insert(
            "/app".into(),
            CgroupStats {
                cpu_usage_usec: 100,
                nr_throttled: 1,
                throttled_usec: 50,
                memory_current: 1 << 20,
            },
        );
        let mut tb = make_thread("app", "w1");
        tb.cgroup = "/app".into();
        let mut snap_b = snap_with(vec![tb]);
        snap_b.cgroup_stats.insert(
            "/app".into(),
            CgroupStats {
                cpu_usage_usec: 500,
                nr_throttled: 3,
                throttled_usec: 250,
                memory_current: 2 << 20,
            },
        );
        let opts = CompareOptions {
            group_by: GroupBy::Cgroup.into(),
            cgroup_flatten: vec![],
        };
        let diff = compare(&snap_a, &snap_b, &opts);
        assert_eq!(diff.cgroup_stats_a["/app"].cpu_usage_usec, 100);
        assert_eq!(diff.cgroup_stats_b["/app"].cpu_usage_usec, 500);
    }

    #[test]
    fn categorical_row_labels_same_or_differs() {
        let mut ta = make_thread("app", "w1");
        ta.policy = "SCHED_OTHER".into();
        let mut tb = make_thread("app", "w1");
        tb.policy = "SCHED_FIFO".into();
        let diff = compare(
            &snap_with(vec![ta]),
            &snap_with(vec![tb]),
            &CompareOptions::default(),
        );
        let policy_row = diff
            .rows
            .iter()
            .find(|r| r.metric_name == "policy")
            .expect("policy row");
        assert!(policy_row.delta.is_none());
        match (&policy_row.baseline, &policy_row.candidate) {
            (Aggregated::Mode { value: a, .. }, Aggregated::Mode { value: b, .. }) => {
                assert_eq!(a, "SCHED_OTHER");
                assert_eq!(b, "SCHED_FIFO");
            }
            _ => panic!("expected two Mode aggregates"),
        }
    }

    #[test]
    fn delta_pct_absent_when_baseline_zero() {
        // Baseline=0, candidate=100 → numeric delta is 100 but
        // percent is undefined (division by zero). The row must
        // still appear (the absolute-delta inflation in sort_key
        // keeps it visible).
        let mut ta = make_thread("app", "w1");
        ta.run_time_ns = 0;
        let mut tb = make_thread("app", "w1");
        tb.run_time_ns = 100;
        let diff = compare(
            &snap_with(vec![ta]),
            &snap_with(vec![tb]),
            &CompareOptions::default(),
        );
        let row = diff
            .rows
            .iter()
            .find(|r| r.metric_name == "run_time_ns")
            .expect("row");
        assert_eq!(row.delta, Some(100.0));
        assert!(row.delta_pct.is_none());
    }

    // -- Additional coverage per team-lead directive --

    /// Two empty snapshots (no threads, no cgroup enrichment)
    /// produce an empty diff with zero rows and zero unmatched
    /// groups. Gate against a silent panic or spurious
    /// "only in baseline" entries driven by inserting keys into
    /// the group map from empty inputs.
    #[test]
    fn empty_snapshots_produce_empty_diff() {
        let diff = compare(
            &snap_with(vec![]),
            &snap_with(vec![]),
            &CompareOptions::default(),
        );
        assert!(diff.rows.is_empty());
        assert!(diff.only_baseline.is_empty());
        assert!(diff.only_candidate.is_empty());
    }

    /// Baseline empty, candidate populated: every candidate
    /// group surfaces as `only_candidate`; `rows` stays empty
    /// because there is no matched group to produce a delta.
    #[test]
    fn baseline_empty_surfaces_only_candidate_groups() {
        let t = make_thread("new_proc", "t1");
        let diff = compare(
            &snap_with(vec![]),
            &snap_with(vec![t]),
            &CompareOptions::default(),
        );
        assert!(diff.rows.is_empty());
        assert!(diff.only_baseline.is_empty());
        assert_eq!(diff.only_candidate, vec!["new_proc".to_string()]);
    }

    /// Identical snapshots produce rows whose delta is
    /// uniformly zero (for every numeric rule) and whose
    /// delta_pct is zero (for every non-zero baseline) —
    /// categorical rows still get the "same" treatment via
    /// `Aggregated::Mode` equality. Pin a representative
    /// subset: every delta field in `rows` must be `Some(0.0)`
    /// or `None` (the `None` branch belongs only to categorical
    /// / all-zero-baseline cases).
    #[test]
    fn identical_snapshots_produce_zero_deltas() {
        let mut t = make_thread("app", "w1");
        t.run_time_ns = 1_000;
        t.voluntary_csw = 50;
        let snap = snap_with(vec![t]);
        let diff = compare(&snap, &snap, &CompareOptions::default());
        for row in &diff.rows {
            match row.delta {
                Some(d) => assert_eq!(d, 0.0, "metric {} had nonzero delta", row.metric_name),
                None => {
                    // Only policy (Mode) should surface here for
                    // a populated thread.
                    assert_eq!(row.metric_name, "policy");
                }
            }
        }
    }

    /// Single-thread group: registry emits exactly one row per
    /// registered metric. Defends against a future "skip if
    /// only one thread" short-circuit sneaking into
    /// `aggregate`.
    #[test]
    fn single_thread_group_yields_one_row_per_metric() {
        let a = make_thread("solo", "t");
        let mut b = make_thread("solo", "t");
        b.run_time_ns = 1;
        let diff = compare(
            &snap_with(vec![a]),
            &snap_with(vec![b]),
            &CompareOptions::default(),
        );
        let solo_rows: Vec<&DiffRow> = diff.rows.iter().filter(|r| r.group_key == "solo").collect();
        assert_eq!(solo_rows.len(), HOST_STATE_METRICS.len());
    }

    /// All-zero cumulative counters on both sides still produce
    /// a row for each Sum metric (delta=0, delta_pct=None
    /// because baseline=0). Gate against a "skip zero" filter
    /// hiding newly-introduced metrics that the workload never
    /// exercises.
    #[test]
    fn all_zero_metrics_emit_zero_delta_rows() {
        let a = make_thread("quiet", "t");
        let b = make_thread("quiet", "t");
        let diff = compare(
            &snap_with(vec![a]),
            &snap_with(vec![b]),
            &CompareOptions::default(),
        );
        let run_time = diff
            .rows
            .iter()
            .find(|r| r.metric_name == "run_time_ns")
            .expect("row");
        assert_eq!(run_time.delta, Some(0.0));
        assert!(run_time.delta_pct.is_none());
    }

    /// `GroupBy::Comm` lumps threads with the same thread name
    /// across processes.
    #[test]
    fn group_by_comm_aggregates_across_processes() {
        let mut ta = make_thread("procA", "worker");
        ta.run_time_ns = 100;
        let mut tb = make_thread("procB", "worker");
        tb.run_time_ns = 200;
        let mut candidate = make_thread("procA", "worker");
        candidate.run_time_ns = 500;
        let mut candidate2 = make_thread("procB", "worker");
        candidate2.run_time_ns = 500;
        let diff = compare(
            &snap_with(vec![ta, tb]),
            &snap_with(vec![candidate, candidate2]),
            &CompareOptions {
                group_by: GroupBy::Comm.into(),
                cgroup_flatten: vec![],
            },
        );
        let row = diff
            .rows
            .iter()
            .find(|r| r.metric_name == "run_time_ns" && r.group_key == "worker")
            .expect("worker row");
        // Summed across both processes: baseline=300, candidate=1000, delta=700.
        assert_eq!(row.thread_count_a, 2);
        assert_eq!(row.thread_count_b, 2);
        assert_eq!(row.delta, Some(700.0));
    }

    /// Thread-count change between baseline and candidate
    /// renders "a\u{2192}b" in the row. Gate against silent
    /// collapse to a single value when the group grows or
    /// shrinks.
    #[test]
    fn thread_count_diff_surfaces_when_group_grows() {
        let ta = make_thread("pool", "t");
        let tb1 = make_thread("pool", "t");
        let tb2 = make_thread("pool", "t");
        let diff = compare(
            &snap_with(vec![ta]),
            &snap_with(vec![tb1, tb2]),
            &CompareOptions::default(),
        );
        let row = diff
            .rows
            .iter()
            .find(|r| r.metric_name == "run_time_ns")
            .expect("row");
        assert_eq!(row.thread_count_a, 1);
        assert_eq!(row.thread_count_b, 2);
        let mut out = String::new();
        write_diff(
            &mut out,
            &diff,
            Path::new("a"),
            Path::new("b"),
            GroupBy::Pcomm,
        )
        .unwrap();
        assert!(
            out.contains("1\u{2192}2"),
            "expected thread-count diff rendering, got:\n{out}",
        );
    }

    /// Earlier flatten pattern wins when multiple patterns
    /// match the same path. Gate against a later pattern
    /// silently stealing the collapse when an operator layers
    /// broad and narrow patterns.
    #[test]
    fn flatten_first_match_wins_over_later_pattern() {
        let pats =
            compile_flatten_patterns(&["/kubepods/*/workload".into(), "/kubepods/**".into()]);
        assert_eq!(
            flatten_cgroup_path("/kubepods/pod-abc/workload", &pats),
            "/kubepods/*/workload",
        );
    }

    /// Multi-pattern collapse: several distinct cgroup paths
    /// flatten to the same key → their enrichment counters
    /// aggregate (sum for counters, max for memory.current).
    #[test]
    fn flatten_cgroup_stats_collapses_overlapping_paths() {
        let mut stats = BTreeMap::new();
        stats.insert(
            "/kubepods/pod-a/workload".into(),
            CgroupStats {
                cpu_usage_usec: 100,
                nr_throttled: 1,
                throttled_usec: 10,
                memory_current: 500,
            },
        );
        stats.insert(
            "/kubepods/pod-b/workload".into(),
            CgroupStats {
                cpu_usage_usec: 200,
                nr_throttled: 2,
                throttled_usec: 20,
                memory_current: 800,
            },
        );
        let pats = compile_flatten_patterns(&["/kubepods/*/workload".into()]);
        let out = flatten_cgroup_stats(&stats, &pats);
        let agg = &out["/kubepods/*/workload"];
        assert_eq!(agg.cpu_usage_usec, 300);
        assert_eq!(agg.nr_throttled, 3);
        assert_eq!(agg.throttled_usec, 30);
        // Instantaneous value: max, not sum.
        assert_eq!(agg.memory_current, 800);
    }

    /// Malformed glob patterns are silently dropped by the
    /// compiler (they never match so they never collapse
    /// anything). Gate against a future change that accidentally
    /// starts rejecting valid-looking patterns.
    #[test]
    fn compile_flatten_patterns_skips_malformed() {
        let pats = compile_flatten_patterns(&["[invalid".into(), "/ok/*".into()]);
        assert_eq!(pats.len(), 1);
        assert_eq!(pats[0].as_str(), "/ok/*");
    }

    /// Every `ThreadState` field that names a registered metric
    /// in the registry has a reachable accessor: sum one unit of
    /// that field through a single-thread aggregate and confirm
    /// the Sum result is 1. Defends against a typo in a new
    /// `AggRule::Sum` accessor pointing at the wrong field.
    ///
    /// The test is metric-registry-driven rather than field-
    /// driven because new metrics have to land through the
    /// registry; a drift between the test and the registry
    /// would catch itself.
    #[test]
    fn sum_metric_accessors_read_expected_field() {
        type MetricSetter = fn(&mut ThreadState);
        let cases: &[(&str, MetricSetter)] = &[
            ("run_time_ns", |t| t.run_time_ns = 1),
            ("wait_time_ns", |t| t.wait_time_ns = 1),
            ("timeslices", |t| t.timeslices = 1),
            ("voluntary_csw", |t| t.voluntary_csw = 1),
            ("nonvoluntary_csw", |t| t.nonvoluntary_csw = 1),
            ("nr_wakeups", |t| t.nr_wakeups = 1),
            ("nr_wakeups_local", |t| t.nr_wakeups_local = 1),
            ("nr_wakeups_remote", |t| t.nr_wakeups_remote = 1),
            ("nr_wakeups_sync", |t| t.nr_wakeups_sync = 1),
            ("nr_wakeups_migrate", |t| t.nr_wakeups_migrate = 1),
            ("nr_wakeups_idle", |t| t.nr_wakeups_idle = 1),
            ("nr_migrations", |t| t.nr_migrations = 1),
            ("wait_sum", |t| t.wait_sum = 1),
            ("wait_count", |t| t.wait_count = 1),
            ("sleep_sum", |t| t.sleep_sum = 1),
            ("allocated_bytes", |t| t.allocated_bytes = 1),
            ("deallocated_bytes", |t| t.deallocated_bytes = 1),
            ("minflt", |t| t.minflt = 1),
            ("majflt", |t| t.majflt = 1),
            ("rchar", |t| t.rchar = 1),
            ("wchar", |t| t.wchar = 1),
            ("syscr", |t| t.syscr = 1),
            ("syscw", |t| t.syscw = 1),
            ("read_bytes", |t| t.read_bytes = 1),
            ("write_bytes", |t| t.write_bytes = 1),
        ];
        for (name, set) in cases {
            let mut t = make_thread("p", "w");
            set(&mut t);
            let def = HOST_STATE_METRICS
                .iter()
                .find(|m| m.name == *name)
                .unwrap_or_else(|| panic!("metric {name} not in registry"));
            let agg = aggregate(def.rule, &[&t]);
            match agg {
                Aggregated::Sum(v) => {
                    assert_eq!(v, 1, "accessor for {name} did not read the {name} field",)
                }
                other => panic!("expected Sum for {name}, got {other:?}"),
            }
        }
    }

    /// Every registered metric name must be unique. A
    /// collision would silently shadow the earlier entry in
    /// lookups and still "work" for fields that happen to
    /// match — a slow-burn correctness bug.
    #[test]
    fn host_state_metric_names_are_unique() {
        let mut seen = std::collections::BTreeSet::new();
        for m in HOST_STATE_METRICS {
            assert!(
                seen.insert(m.name),
                "duplicate metric name in registry: {}",
                m.name,
            );
        }
    }

    /// Mode rule with a deterministic tie-break: when two
    /// values share the top count, the lexicographically
    /// smaller one wins. Pin the rule so the rendered output
    /// is reproducible across runs.
    #[test]
    fn mode_rule_tie_break_is_lexicographic() {
        let mut a = make_thread("app", "w1");
        a.policy = "SCHED_FIFO".into();
        let mut b = make_thread("app", "w2");
        b.policy = "SCHED_OTHER".into();
        let v = aggregate(AggRule::Mode(|t| t.policy.clone()), &[&a, &b]);
        match v {
            Aggregated::Mode { value, count, .. } => {
                assert_eq!(value, "SCHED_FIFO");
                assert_eq!(count, 1);
            }
            other => panic!("expected Mode, got {other:?}"),
        }
    }

    /// Affinity aggregate on an empty thread slice returns
    /// `min_cpus == max_cpus == 0` and no uniform cpuset — the
    /// compare engine cannot produce an empty group today, but
    /// this defends against an upstream refactor that permits
    /// one.
    #[test]
    fn affinity_aggregate_on_empty_threads_is_zero() {
        let empty: Vec<&ThreadState> = vec![];
        let v = aggregate(AggRule::Affinity(|t| t.cpu_affinity.clone()), &empty);
        match v {
            Aggregated::Affinity(s) => {
                assert_eq!(s.min_cpus, 0);
                assert_eq!(s.max_cpus, 0);
                assert!(s.uniform.is_none());
            }
            other => panic!("expected Affinity, got {other:?}"),
        }
    }

    /// Ordinal range collapses `min == max` to a single number
    /// in display. Defends against `nice=0` single-thread
    /// groups rendering as `0..0`.
    #[test]
    fn ordinal_display_collapses_degenerate_range() {
        let r = Aggregated::OrdinalRange { min: 0, max: 0 };
        assert_eq!(r.to_string(), "0");
        let r = Aggregated::OrdinalRange { min: -5, max: 10 };
        assert_eq!(r.to_string(), "-5..10");
    }

    /// Mode display omits the minority ratio when the mode is
    /// unanimous (count == total). Keeps the table compact for
    /// homogeneous groups.
    #[test]
    fn mode_display_hides_ratio_when_unanimous() {
        let m = Aggregated::Mode {
            value: "SCHED_OTHER".into(),
            count: 4,
            total: 4,
        };
        assert_eq!(m.to_string(), "SCHED_OTHER");
        let m = Aggregated::Mode {
            value: "SCHED_OTHER".into(),
            count: 3,
            total: 5,
        };
        assert_eq!(m.to_string(), "SCHED_OTHER (3/5)");
    }

    // -- write_diff: output rendering --

    #[test]
    fn write_diff_emits_expected_column_headers() {
        let diff = compare(
            &snap_with(vec![make_thread("p", "w")]),
            &snap_with(vec![make_thread("p", "w")]),
            &CompareOptions::default(),
        );
        let mut out = String::new();
        write_diff(
            &mut out,
            &diff,
            Path::new("a"),
            Path::new("b"),
            GroupBy::Pcomm,
        )
        .unwrap();
        for h in [
            "pcomm",
            "threads",
            "metric",
            "baseline",
            "candidate",
            "delta",
            "%",
        ] {
            assert!(out.contains(h), "missing header {h}:\n{out}");
        }
    }

    #[test]
    fn write_diff_header_switches_on_group_by() {
        let empty = HostStateDiff::default();
        let mut out = String::new();
        write_diff(
            &mut out,
            &empty,
            Path::new("a"),
            Path::new("b"),
            GroupBy::Cgroup,
        )
        .unwrap();
        assert!(out.contains("cgroup"));
        let mut out = String::new();
        write_diff(
            &mut out,
            &empty,
            Path::new("a"),
            Path::new("b"),
            GroupBy::Comm,
        )
        .unwrap();
        assert!(out.contains("comm"));
        // "comm" must render as the column header, not as a
        // substring of "pcomm" left over from the Pcomm variant.
        assert!(!out.contains("pcomm"));
    }

    #[test]
    fn write_diff_prints_only_baseline_section() {
        let diff = HostStateDiff {
            only_baseline: vec!["missing_proc".into()],
            ..HostStateDiff::default()
        };
        let mut out = String::new();
        write_diff(
            &mut out,
            &diff,
            Path::new("/tmp/a.hst.zst"),
            Path::new("/tmp/b.hst.zst"),
            GroupBy::Pcomm,
        )
        .unwrap();
        assert!(out.contains("only in baseline"));
        assert!(out.contains("missing_proc"));
        assert!(out.contains("/tmp/a.hst.zst"));
    }

    #[test]
    fn write_diff_prints_only_candidate_section() {
        let diff = HostStateDiff {
            only_candidate: vec!["new_proc".into()],
            ..HostStateDiff::default()
        };
        let mut out = String::new();
        write_diff(
            &mut out,
            &diff,
            Path::new("/tmp/a.hst.zst"),
            Path::new("/tmp/b.hst.zst"),
            GroupBy::Pcomm,
        )
        .unwrap();
        assert!(out.contains("only in candidate"));
        assert!(out.contains("new_proc"));
        assert!(out.contains("/tmp/b.hst.zst"));
    }

    #[test]
    fn write_diff_cgroup_enrichment_section_for_cgroup_mode() {
        let mut diff = HostStateDiff::default();
        diff.cgroup_stats_a.insert(
            "/app".into(),
            CgroupStats {
                cpu_usage_usec: 10,
                nr_throttled: 0,
                throttled_usec: 0,
                memory_current: 100,
            },
        );
        diff.cgroup_stats_b.insert(
            "/app".into(),
            CgroupStats {
                cpu_usage_usec: 50,
                nr_throttled: 0,
                throttled_usec: 0,
                memory_current: 200,
            },
        );
        let mut out = String::new();
        write_diff(
            &mut out,
            &diff,
            Path::new("a"),
            Path::new("b"),
            GroupBy::Cgroup,
        )
        .unwrap();
        assert!(
            out.contains("cpu_usage_usec"),
            "missing enrichment header:\n{out}"
        );
        assert!(out.contains("10"), "missing baseline usec:\n{out}");
        assert!(out.contains("50"), "missing candidate usec:\n{out}");
        assert!(out.contains("+40"), "missing delta:\n{out}");
    }

    #[test]
    fn write_diff_enrichment_section_absent_when_group_by_pcomm() {
        let mut diff = HostStateDiff::default();
        // Populate enrichment; renderer must ignore it under
        // GroupBy::Pcomm.
        diff.cgroup_stats_a.insert(
            "/app".into(),
            CgroupStats {
                cpu_usage_usec: 10,
                ..CgroupStats::default()
            },
        );
        let mut out = String::new();
        write_diff(
            &mut out,
            &diff,
            Path::new("a"),
            Path::new("b"),
            GroupBy::Pcomm,
        )
        .unwrap();
        assert!(!out.contains("cpu_usage_usec"), "enrichment leaked:\n{out}");
    }

    #[test]
    fn write_diff_delta_cell_has_plus_minus_sign() {
        let mut ta = make_thread("app", "w");
        ta.run_time_ns = 100;
        let mut tb = make_thread("app", "w");
        tb.run_time_ns = 50;
        let diff = compare(
            &snap_with(vec![ta]),
            &snap_with(vec![tb]),
            &CompareOptions::default(),
        );
        let mut out = String::new();
        write_diff(
            &mut out,
            &diff,
            Path::new("a"),
            Path::new("b"),
            GroupBy::Pcomm,
        )
        .unwrap();
        // 50 - 100 = -50ns → renders with minus sign + unit.
        assert!(
            out.contains("-50.000ns"),
            "missing signed delta with unit:\n{out}",
        );
        assert!(out.contains("-50.0%"), "missing signed pct:\n{out}");
    }

    #[test]
    fn write_diff_categorical_delta_labels_same_or_differs() {
        let mut ta = make_thread("app", "w");
        ta.policy = "SCHED_OTHER".into();
        let mut tb = make_thread("app", "w");
        tb.policy = "SCHED_FIFO".into();
        let diff = compare(
            &snap_with(vec![ta]),
            &snap_with(vec![tb]),
            &CompareOptions::default(),
        );
        let mut out = String::new();
        write_diff(
            &mut out,
            &diff,
            Path::new("a"),
            Path::new("b"),
            GroupBy::Pcomm,
        )
        .unwrap();
        assert!(out.contains("differs"), "missing 'differs' label:\n{out}");
    }

    /// Full round-trip via the public loader: two snapshots
    /// written to disk via `HostStateSnapshot::write`, loaded
    /// via `HostStateSnapshot::load`, compared, and the
    /// rendered output inspected. This stitches together the
    /// serialization layer, the comparison engine, and the
    /// formatter — the components `run_compare` composes in
    /// production.
    #[test]
    fn load_compare_render_pipeline_end_to_end() {
        let mut a = make_thread("e2e_proc", "thread_a");
        a.run_time_ns = 1_000_000;
        a.voluntary_csw = 10;
        a.policy = "SCHED_OTHER".into();
        let snap_a = snap_with(vec![a]);
        let mut b = make_thread("e2e_proc", "thread_a");
        b.run_time_ns = 3_000_000;
        b.voluntary_csw = 30;
        b.policy = "SCHED_FIFO".into();
        let snap_b = snap_with(vec![b]);

        let tmp_a = tempfile::NamedTempFile::new().unwrap();
        let tmp_b = tempfile::NamedTempFile::new().unwrap();
        snap_a.write(tmp_a.path()).unwrap();
        snap_b.write(tmp_b.path()).unwrap();
        let loaded_a = HostStateSnapshot::load(tmp_a.path()).unwrap();
        let loaded_b = HostStateSnapshot::load(tmp_b.path()).unwrap();

        let diff = compare(&loaded_a, &loaded_b, &CompareOptions::default());
        let mut out = String::new();
        write_diff(&mut out, &diff, tmp_a.path(), tmp_b.path(), GroupBy::Pcomm).unwrap();

        // Column headers present.
        assert!(out.contains("pcomm"));
        assert!(out.contains("metric"));
        // Group key made it through.
        assert!(out.contains("e2e_proc"));
        // run_time_ns delta: +2_000_000 → rendered with plus sign
        // and the `ns` unit.
        assert!(
            out.contains("+2000000.000ns"),
            "run_time delta missing in:\n{out}",
        );
        // Policy row renders "differs" because SCHED_FIFO vs
        // SCHED_OTHER — non-numeric delta path exercised.
        assert!(out.contains("differs"));
    }

    // -- comparison coverage expansion --

    /// Pin all four branches of `cgroup_cell` directly. Existing
    /// tests only exercise the (Some, Some) path transitively via
    /// `write_diff_cgroup_enrichment_section_for_cgroup_mode`; the
    /// other three branches (baseline-only, candidate-only,
    /// both-missing) are rendering-critical for the one-sided
    /// enrichment row path (`all_keys` union at the enrichment
    /// table site) and have no current pin.
    #[test]
    fn cgroup_cell_renders_all_four_branches() {
        // (Some, Some) → "a → b (+d)" where d = b - a (signed).
        assert_eq!(cgroup_cell(Some(10), Some(42)), "10 → 42 (+32)");
        // Negative delta uses the signed formatter to keep the
        // sign explicit.
        assert_eq!(cgroup_cell(Some(50), Some(5)), "50 → 5 (-45)");
        // (Some, None) → baseline value then en-dash placeholder.
        assert_eq!(cgroup_cell(Some(7), None), "7 → -");
        // (None, Some) → leading en-dash placeholder.
        assert_eq!(cgroup_cell(None, Some(99)), "- → 99");
        // (None, None) → single en-dash (both sides absent).
        assert_eq!(cgroup_cell(None, None), "-");
    }

    /// Enrichment renderer must union `cgroup_stats_a` and
    /// `cgroup_stats_b` keys so a cgroup that appeared in only one
    /// run still surfaces a row. Drives the one-sided paths of
    /// `cgroup_cell` through `write_diff` so the rendered output
    /// carries the `"X → -"` / `"- → Y"` strings.
    #[test]
    fn write_diff_enrichment_handles_one_sided_cgroup_keys() {
        let mut diff = HostStateDiff::default();
        diff.cgroup_stats_a.insert(
            "/only-baseline".into(),
            CgroupStats {
                cpu_usage_usec: 111,
                ..CgroupStats::default()
            },
        );
        diff.cgroup_stats_b.insert(
            "/only-candidate".into(),
            CgroupStats {
                cpu_usage_usec: 222,
                ..CgroupStats::default()
            },
        );
        let mut out = String::new();
        write_diff(
            &mut out,
            &diff,
            Path::new("a"),
            Path::new("b"),
            GroupBy::Cgroup,
        )
        .unwrap();
        // Both keys present.
        assert!(
            out.contains("/only-baseline"),
            "baseline-only key missing:\n{out}",
        );
        assert!(
            out.contains("/only-candidate"),
            "candidate-only key missing:\n{out}",
        );
        // Each one-sided row emits the en-dash placeholder for
        // the absent side (per `cgroup_cell`'s Some/None branch).
        assert!(
            out.contains("111 → -"),
            "baseline-only row missing '111 → -' cell:\n{out}",
        );
        assert!(
            out.contains("- → 222"),
            "candidate-only row missing '- → 222' cell:\n{out}",
        );
    }

    /// Rows with equal `sort_key()` break ties by ascending
    /// `group_key`. Build two groups that move the same metric by
    /// the same percentage (so their sort keys are identical) and
    /// verify the output order is alphabetical.
    #[test]
    fn write_diff_stable_sort_tie_breaks_by_group_key_ascending() {
        // Same percentage swing, distinct group keys "alpha" and
        // "bravo". Both rise 1_000 → 2_000 (+100%).
        let mut a1 = make_thread("alpha", "w");
        a1.run_time_ns = 1_000;
        let mut a2 = make_thread("bravo", "w");
        a2.run_time_ns = 1_000;
        let mut b1 = make_thread("alpha", "w");
        b1.run_time_ns = 2_000;
        let mut b2 = make_thread("bravo", "w");
        b2.run_time_ns = 2_000;
        let diff = compare(
            &snap_with(vec![a1, a2]),
            &snap_with(vec![b1, b2]),
            &CompareOptions::default(),
        );
        // Filter to run_time_ns rows across the two groups; the
        // tie-break must put "alpha" before "bravo".
        let run_rows: Vec<&DiffRow> = diff
            .rows
            .iter()
            .filter(|r| r.metric_name == "run_time_ns")
            .collect();
        assert_eq!(run_rows.len(), 2);
        assert!(
            (run_rows[0].delta_pct.unwrap() - 1.0).abs() < 1e-9
                && (run_rows[1].delta_pct.unwrap() - 1.0).abs() < 1e-9,
            "test fixture must produce identical delta_pct for both groups",
        );
        assert_eq!(
            run_rows[0].group_key, "alpha",
            "ascending group_key tie-break expected alpha first",
        );
        assert_eq!(run_rows[1].group_key, "bravo");
    }

    /// `sort_key` inflates the zero-baseline-nonzero-candidate
    /// branch (delta=Some, delta_pct=None) by 1e9 so it sorts
    /// above pure zero-delta rows but still below any nonzero
    /// percentage row. Two rows: one zero-delta (delta_pct=0.0),
    /// one zero-baseline (delta=100, delta_pct=None) — the zero-
    /// baseline row must sort FIRST.
    #[test]
    fn sort_key_zero_delta_rows_sink_below_nonzero() {
        // Group "calm": identical values → delta 0, pct 0.0.
        let mut a1 = make_thread("calm", "w");
        a1.run_time_ns = 500;
        let mut b1 = make_thread("calm", "w");
        b1.run_time_ns = 500;
        // Group "birth": baseline 0 → candidate 100 → delta 100,
        // pct undefined (None). sort_key inflates to 100 * 1e9.
        let a2 = make_thread("birth", "w");
        let mut b2 = make_thread("birth", "w");
        b2.run_time_ns = 100;
        let diff = compare(
            &snap_with(vec![a1, a2]),
            &snap_with(vec![b1, b2]),
            &CompareOptions::default(),
        );
        let run_rows: Vec<&DiffRow> = diff
            .rows
            .iter()
            .filter(|r| r.metric_name == "run_time_ns")
            .collect();
        // "birth" row (zero-baseline branch of sort_key) sorts
        // ahead of "calm" (zero-delta branch).
        assert_eq!(run_rows[0].group_key, "birth");
        assert_eq!(run_rows[1].group_key, "calm");
        // Pin the exact shape each branch is meant to carry, so a
        // regression that swapped the inflation with the zero
        // arm surfaces here with a precise diagnostic rather than
        // just "wrong order".
        assert_eq!(run_rows[0].delta, Some(100.0));
        assert!(run_rows[0].delta_pct.is_none());
        assert_eq!(run_rows[1].delta, Some(0.0));
        assert_eq!(run_rows[1].delta_pct, Some(0.0));
    }

    /// Rows with no numeric delta (categorical Mode) sort to the
    /// bottom via `sort_key`'s `f64::NEG_INFINITY` arm. Pin that a
    /// nonzero numeric row sorts ahead of a Mode row whose inputs
    /// differ, and that the Mode row still appears (sinks, not
    /// dropped).
    #[test]
    fn sort_key_none_delta_rows_sink_to_bottom() {
        let mut a = make_thread("app", "w");
        a.run_time_ns = 100;
        a.policy = "SCHED_OTHER".into();
        let mut b = make_thread("app", "w");
        b.run_time_ns = 200;
        b.policy = "SCHED_FIFO".into();
        let diff = compare(
            &snap_with(vec![a]),
            &snap_with(vec![b]),
            &CompareOptions::default(),
        );
        // Locate the positions of run_time_ns (numeric) and
        // policy (Mode, delta=None) in the sorted rows.
        let run_idx = diff
            .rows
            .iter()
            .position(|r| r.metric_name == "run_time_ns")
            .expect("run_time_ns row");
        let policy_idx = diff
            .rows
            .iter()
            .position(|r| r.metric_name == "policy")
            .expect("policy row");
        assert!(
            run_idx < policy_idx,
            "numeric row at {run_idx} must sort above Mode row at {policy_idx}",
        );
        // Mode row really is None-delta — otherwise the ordering
        // wouldn't prove the NEG_INFINITY branch.
        assert!(diff.rows[policy_idx].delta.is_none());
    }

    /// `aggregate(OrdinalRange, &[])` returns `OrdinalRange {
    /// min: 0, max: 0 }` via the `unwrap_or(0)` in the first-value
    /// init. Sibling to the empty-affinity test.
    #[test]
    fn aggregate_ordinal_range_on_empty_threads_is_zero() {
        let empty: Vec<&ThreadState> = vec![];
        let v = aggregate(AggRule::OrdinalRange(|t| t.nice as i64), &empty);
        match v {
            Aggregated::OrdinalRange { min, max } => {
                assert_eq!(min, 0);
                assert_eq!(max, 0);
            }
            other => panic!("expected OrdinalRange, got {other:?}"),
        }
    }

    /// `aggregate(Mode, &[])` returns `Mode { value: "", count:
    /// 0, total: 0 }` via the `unwrap_or_else((String::new(), 0))`
    /// tail.
    #[test]
    fn aggregate_mode_on_empty_threads_is_empty() {
        let empty: Vec<&ThreadState> = vec![];
        let v = aggregate(AggRule::Mode(|t| t.policy.clone()), &empty);
        match v {
            Aggregated::Mode {
                value,
                count,
                total,
            } => {
                assert!(value.is_empty());
                assert_eq!(count, 0);
                assert_eq!(total, 0);
            }
            other => panic!("expected Mode, got {other:?}"),
        }
    }

    /// `aggregate(Sum, &[])` returns `Sum(0)` via the `fold(0u64,
    /// ...)` accumulator. Completes empty-slice coverage across
    /// all four AggRules.
    #[test]
    fn aggregate_sum_on_empty_threads_is_zero() {
        let empty: Vec<&ThreadState> = vec![];
        let v = aggregate(AggRule::Sum(|t| t.run_time_ns), &empty);
        match v {
            Aggregated::Sum(s) => assert_eq!(s, 0),
            other => panic!("expected Sum, got {other:?}"),
        }
    }

    /// `Aggregated::numeric` returns `None` for `Mode` — a
    /// policy name has no scalar projection. Pin the contract
    /// directly rather than via the diff pipeline because the
    /// pipeline only reads numeric through `build_row`'s `(a.numeric(),
    /// b.numeric())` pair and a regression could silently flip the
    /// return to `Some(0.0)` without any currently-visible symptom.
    #[test]
    fn numeric_returns_none_for_mode() {
        let m = Aggregated::Mode {
            value: "SCHED_OTHER".into(),
            count: 4,
            total: 4,
        };
        assert!(m.numeric().is_none());
    }

    /// `Aggregated::numeric` for a heterogeneous `Affinity`
    /// returns `(min_cpus + max_cpus) / 2.0` — the midpoint
    /// projection. Existing affinity tests only exercise uniform
    /// cpusets where `min == max`, so the arithmetic path is
    /// unpinned.
    #[test]
    fn numeric_returns_midpoint_for_affinity_heterogeneous() {
        let a = Aggregated::Affinity(AffinitySummary {
            min_cpus: 2,
            max_cpus: 8,
            uniform: None,
        });
        assert_eq!(a.numeric(), Some(5.0));
        // Single-element (uniform) heterogeneous check is the
        // degenerate case where the midpoint equals either bound.
        let b = Aggregated::Affinity(AffinitySummary {
            min_cpus: 4,
            max_cpus: 4,
            uniform: None,
        });
        assert_eq!(b.numeric(), Some(4.0));
    }

    /// Uniform non-contiguous cpuset `[0, 2]` renders as
    /// `"2 cpus (0,2)"` — exercises the comma-separated branch of
    /// `format_cpu_range` from the Affinity display impl. Existing
    /// uniform test uses `[0,1,2,3]` which collapses to a single
    /// range token.
    #[test]
    fn affinity_display_uniform_noncontiguous_renders_comma_separated() {
        let a = Aggregated::Affinity(AffinitySummary {
            min_cpus: 2,
            max_cpus: 2,
            uniform: Some(vec![0, 2]),
        });
        assert_eq!(a.to_string(), "2 cpus (0,2)");
    }

    /// Heterogeneous affinity where `min_cpus == max_cpus` (every
    /// thread has the same cpuset SIZE but different SETS) renders
    /// as `"N cpus (mixed)"` — pins the specific branch in the
    /// display impl. Current heterogeneous test has min != max so
    /// this branch was unpinned.
    #[test]
    fn affinity_display_heterogeneous_same_count_renders_mixed() {
        let a = Aggregated::Affinity(AffinitySummary {
            min_cpus: 3,
            max_cpus: 3,
            uniform: None,
        });
        assert_eq!(a.to_string(), "3 cpus (mixed)");
    }

    /// `flatten_cgroup_stats` with zero patterns preserves the
    /// input map verbatim — no entry merges, no key rewrites. A
    /// regression that accidentally ran the aggregation step on
    /// the empty-pattern path would collapse distinct cgroup paths
    /// together.
    #[test]
    fn flatten_cgroup_stats_with_no_patterns_preserves_keys() {
        let mut stats = BTreeMap::new();
        stats.insert(
            "/alpha".into(),
            CgroupStats {
                cpu_usage_usec: 10,
                nr_throttled: 1,
                throttled_usec: 5,
                memory_current: 100,
            },
        );
        stats.insert(
            "/beta".into(),
            CgroupStats {
                cpu_usage_usec: 20,
                nr_throttled: 2,
                throttled_usec: 15,
                memory_current: 200,
            },
        );
        let out = flatten_cgroup_stats(&stats, &[]);
        assert_eq!(out.len(), 2);
        assert_eq!(out["/alpha"].cpu_usage_usec, 10);
        assert_eq!(out["/alpha"].memory_current, 100);
        assert_eq!(out["/beta"].cpu_usage_usec, 20);
        assert_eq!(out["/beta"].memory_current, 200);
    }
}