magi-cli 0.266.1

Blind multi-agent implementation competition: N agents implement, M judges rank blind, deliberate, vote privately, winner survives double review + E2E gate
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
//! Pure terminal-failure extraction, comparison, and formatting for verification commands.
//!
//! When `graph::run_e2e_with_retry` runs a failed `verify.e2e` command a second time on
//! the same commit and both attempts exit non-zero, exiting non-zero twice proves only
//! that verification stays red — not that the same test failed twice. This module parses
//! raw command output (before `run::summarize_verify` reduces it) to extract confirmed,
//! count-reconciled terminal test failures, separates intermediate `TRY` failures,
//! `FLAKY` passes, and cancellations from terminal verdicts, compares both attempts
//! conservatively (`Matching`, `Distinct`, or `Unknown`), and formats the saved record
//! consistently across events, CLI/web reports, fixer prompts, recovery evidence, stop
//! reasons, and automatic task chat reports.

use std::collections::BTreeSet;
use std::fmt::Write as _;

use serde::{Deserialize, Serialize};

use crate::run::{CommandOutcome, E2eStatus, RunState, summarize_verify};

/// Maximum number of terminal failures captured in a confirmed set. Exceeding this
/// yields [`FailureExtraction::Unknown`] so truncated sets are never compared as complete.
pub const MAX_TERMINAL_FAILURES: usize = 32;

/// Maximum number of test names retained per intermediate category (`TRY`, `FLAKY`, etc.).
pub const MAX_INTERMEDIATE_NAMES: usize = 8;

/// Maximum byte length of the bounded excerpt stored per attempt inside [`RerunComparison`].
pub const ATTEMPT_EXCERPT_MAX: usize = 4_000;

/// Maximum number of failure names listed inline in one-line summaries before `+N more`.
pub const DISPLAY_FAILURE_LIMIT: usize = 6;

/// Legacy prefix previously injected into `output_tail` on double failures.
/// Older `run.json` records carrying this prefix have no structured attempt comparison
/// and must be treated as an unknown comparison rather than proof of a reproduced failure.
pub const LEGACY_FAILED_TWICE_PREFIX: &str =
    "[this command failed twice on the same commit; the output below is the second attempt's]";

/// One confirmed terminal test failure (optional test binary/suite + test name).
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct TerminalFailure {
    /// Test binary or target identifier when known (e.g. `magi-cli::isolated_build_cache`).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub suite: Option<String>,
    /// Test name within the suite.
    pub test: String,
}

impl TerminalFailure {
    /// Human-readable display label (`<suite> <test>` or `<test>`).
    pub fn display_name(&self) -> String {
        match &self.suite {
            Some(suite) if !suite.is_empty() => format!("{suite} {}", self.test),
            _ => self.test.clone(),
        }
    }
}

/// Intermediate / non-terminal test activity observed in raw output, kept strictly
/// separate from terminal failures.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct IntermediateNotes {
    /// Number of intermediate retry failure lines (`TRY N FAIL` / `TRY N TIMEOUT` / ...).
    #[serde(default)]
    pub try_failure_count: usize,
    /// Bounded sample of test identifiers seen in `TRY N` failure lines.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub try_failures: Vec<String>,
    /// Number of tests that passed on retry (`FLAKY`).
    #[serde(default)]
    pub flaky_count: usize,
    /// Bounded sample of test identifiers marked `FLAKY`.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub flaky: Vec<String>,
    /// Whether the test runner reported cancellations or partial execution (`M/N tests run`).
    #[serde(default)]
    pub cancelled: bool,
    /// Number of tests not run due to cancellation, when reported in the summary.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub not_run_count: Option<usize>,
    /// Bounded sample of long-running test notices (`has been running for over ...`).
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub long_running: Vec<String>,
}

impl IntermediateNotes {
    /// True when no intermediate retries, flaky passes, cancellations, or hangs were observed.
    pub fn is_empty(&self) -> bool {
        self.try_failure_count == 0
            && self.try_failures.is_empty()
            && self.flaky_count == 0
            && self.flaky.is_empty()
            && !self.cancelled
            && self.not_run_count.is_none()
            && self.long_running.is_empty()
    }

    fn record_try(&mut self, label: String) {
        self.try_failure_count += 1;
        push_bounded_unique(&mut self.try_failures, label, MAX_INTERMEDIATE_NAMES);
    }

    fn record_flaky(&mut self, label: String) {
        if !self.flaky.contains(&label) {
            self.flaky_count += 1;
            push_bounded_unique(&mut self.flaky, label, MAX_INTERMEDIATE_NAMES);
        }
    }

    fn record_long_running(&mut self, label: String) {
        push_bounded_unique(&mut self.long_running, label, MAX_INTERMEDIATE_NAMES);
    }

    /// Compact human-readable summary of separated non-terminal activity, or `None` when empty.
    pub fn summary(&self) -> Option<String> {
        let mut parts = Vec::new();
        if self.try_failure_count > 0 {
            if self.try_failures.is_empty() {
                parts.push(format!(
                    "{} intermediate TRY failure(s)",
                    self.try_failure_count
                ));
            } else {
                parts.push(format!(
                    "{} intermediate TRY failure(s) ({})",
                    self.try_failure_count,
                    join_bounded(&self.try_failures, 3)
                ));
            }
        }
        if self.flaky_count > 0 {
            if self.flaky.is_empty() {
                parts.push(format!("{} FLAKY pass(es)", self.flaky_count));
            } else {
                parts.push(format!(
                    "{} FLAKY pass(es) ({})",
                    self.flaky_count,
                    join_bounded(&self.flaky, 3)
                ));
            }
        }
        if self.cancelled {
            match self.not_run_count {
                Some(n) if n > 0 => parts.push(format!("cancelled ({n} test(s) not run)")),
                _ => parts.push("cancelled".to_owned()),
            }
        }
        if !self.long_running.is_empty() {
            parts.push(format!(
                "long-running test(s): {}",
                join_bounded(&self.long_running, 3)
            ));
        }
        (!parts.is_empty()).then(|| parts.join("; "))
    }
}

/// Result of extracting terminal test failures from one attempt's raw output.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum FailureExtraction {
    /// Complete, count-reconciled, non-empty set of terminal failing tests.
    Confirmed {
        /// Sorted, deduplicated terminal failing tests.
        failures: Vec<TerminalFailure>,
    },
    /// Terminal failing tests could not be confirmed completely.
    Unknown {
        /// Why completeness could not be confirmed.
        reason: String,
    },
}

/// Extracted terminal failure set paired with separated intermediate activity for one attempt.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct FailureEvidence {
    /// Confirmed terminal failures or why they are unknown.
    pub extraction: FailureExtraction,
    /// Intermediate retries (`TRY`), `FLAKY` passes, and cancellations, kept separate.
    #[serde(default)]
    pub intermediate: IntermediateNotes,
}

/// Bounded evidence preserved for one verification attempt in a two-attempt rerun pair.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct AttemptEvidence {
    /// 1-based attempt number (`1` = first failure, `2` = rerun).
    pub attempt: u8,
    /// Process exit code, or `None` on timeout/signal/spawn error.
    pub code: Option<i32>,
    /// Wall-clock duration in milliseconds.
    #[serde(default)]
    pub duration_ms: u64,
    /// Run-relative artifact path (e.g. `artifacts/e2e-3.out`), when saved.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub artifact: Option<String>,
    /// Bounded output excerpt (within [`ATTEMPT_EXCERPT_MAX`]).
    #[serde(default)]
    pub excerpt: String,
    /// Terminal failure extraction from raw output.
    pub extraction: FailureExtraction,
    /// Intermediate retries, flaky passes, and cancellations, kept separate.
    #[serde(default)]
    pub intermediate: IntermediateNotes,
}

impl AttemptEvidence {
    /// Build bounded attempt evidence from a finished [`CommandOutcome`].
    pub fn from_outcome(attempt: u8, outcome: &CommandOutcome) -> Self {
        let (extraction, intermediate) = match &outcome.failure_evidence {
            Some(ev) => (ev.extraction.clone(), ev.intermediate.clone()),
            None => (
                FailureExtraction::Unknown {
                    reason: "no raw failure extraction recorded".to_owned(),
                },
                IntermediateNotes::default(),
            ),
        };
        Self {
            attempt,
            code: outcome.code,
            duration_ms: outcome.duration_ms,
            artifact: outcome.artifact.clone(),
            excerpt: summarize_verify(&outcome.output_tail, ATTEMPT_EXCERPT_MAX),
            extraction,
            intermediate,
        }
    }

    /// Return a copy with `excerpt` scrubbed through `scrub`.
    pub fn scrubbed(&self, scrub: impl Fn(&str) -> String) -> Self {
        let mut copy = self.clone();
        copy.excerpt = scrub(&copy.excerpt);
        copy
    }
}

/// Conservative comparison verdict between two consecutive failing attempts on one commit.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum ComparisonVerdict {
    /// Both attempts had confirmed, non-empty, identical terminal test failure sets.
    Matching {
        /// Confirmed terminal failures present in both attempts.
        failures: Vec<TerminalFailure>,
    },
    /// Both attempts had confirmed, non-empty, differing terminal test failure sets.
    Distinct {
        /// Terminal failures present in both attempts.
        #[serde(default, skip_serializing_if = "Vec::is_empty")]
        common: Vec<TerminalFailure>,
        /// Terminal failures present only in the first attempt.
        #[serde(default, skip_serializing_if = "Vec::is_empty")]
        first_only: Vec<TerminalFailure>,
        /// Terminal failures present only in the second attempt.
        #[serde(default, skip_serializing_if = "Vec::is_empty")]
        second_only: Vec<TerminalFailure>,
    },
    /// At least one attempt's terminal test failures could not be confirmed.
    Unknown {
        /// Why the two attempts could not be compared conclusively.
        reason: String,
    },
}

/// Structured record persisted on a [`CommandOutcome`] that failed twice on one commit.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct RerunComparison {
    /// Commit SHA both attempts ran against.
    pub head: String,
    /// First failing attempt's bounded evidence.
    pub first: AttemptEvidence,
    /// Second failing attempt's bounded evidence.
    pub second: AttemptEvidence,
    /// Conservative comparison verdict (`Matching`, `Distinct`, or `Unknown`).
    pub verdict: ComparisonVerdict,
}

impl RerunComparison {
    /// Return a copy with attempt excerpts scrubbed through `scrub`.
    pub fn scrubbed(&self, scrub: impl Fn(&str) -> String) -> Self {
        Self {
            head: self.head.clone(),
            first: self.first.scrubbed(&scrub),
            second: self.second.scrubbed(&scrub),
            verdict: self.verdict.clone(),
        }
    }
}

/// True when `output_tail` begins with the legacy double-failure prefix that predates
/// structured [`RerunComparison`].
pub fn legacy_failed_twice(output_tail: &str) -> bool {
    output_tail
        .trim_start()
        .starts_with(LEGACY_FAILED_TWICE_PREFIX)
}

/// Strip ANSI CSI/OSC escape sequences and trailing `\r` from one line.
pub fn strip_ansi(line: &str) -> String {
    let line = line.strip_suffix('\r').unwrap_or(line);
    let mut out = String::with_capacity(line.len());
    let mut chars = line.chars().peekable();
    while let Some(c) = chars.next() {
        if c == '\x1b' {
            match chars.peek().copied() {
                Some('[') => {
                    chars.next();
                    for ch in chars.by_ref() {
                        if ('@'..='~').contains(&ch) {
                            break;
                        }
                    }
                }
                Some(']') => {
                    chars.next();
                    while let Some(ch) = chars.next() {
                        if ch == '\x07' {
                            break;
                        }
                        if ch == '\x1b' && chars.peek() == Some(&'\\') {
                            chars.next();
                            break;
                        }
                    }
                }
                _ => {}
            }
        } else {
            out.push(c);
        }
    }
    out
}

/// Extract terminal test failures and intermediate notes from separate raw `stdout` and
/// `stderr` streams. Neither stream's order is assumed to imply wall-clock execution order.
pub fn extract_streams(
    code: Option<i32>,
    build_failed: bool,
    stdout: &str,
    stderr: &str,
) -> FailureEvidence {
    let out_lines: Vec<String> = stdout.lines().map(strip_ansi).collect();
    let err_lines: Vec<String> = stderr.lines().map(strip_ansi).collect();
    extract_line_streams(code, build_failed, &[&out_lines, &err_lines])
}

/// Extract terminal test failures and intermediate notes from a single combined raw output.
pub fn extract_combined(code: Option<i32>, build_failed: bool, body: &str) -> FailureEvidence {
    let lines: Vec<String> = body.lines().map(strip_ansi).collect();
    extract_line_streams(code, build_failed, &[&lines])
}

fn extract_line_streams(
    code: Option<i32>,
    build_failed: bool,
    streams: &[&[String]],
) -> FailureEvidence {
    let mut intermediate = IntermediateNotes::default();
    let mut libtest_total = LibtestSummary::default();
    let mut nextest_total = NextestSummary::default();

    for lines in streams {
        let (lib, captured_ranges) = parse_libtest_stream(lines, &mut intermediate);
        libtest_total.merge(lib);
        let nx = parse_nextest_stream(lines, &captured_ranges, &mut intermediate);
        nextest_total.merge(nx);
    }

    // Cargo writes binary announcements and libtest verdicts to different pipes.
    // Without interleaving information, only a single announcement and a single
    // libtest run can be associated unambiguously.
    if libtest_total.runs == 1 && libtest_total.suites == 1 {
        for failure in &mut libtest_total.failures {
            if failure.suite.is_none() {
                failure.suite.clone_from(&libtest_total.only_suite);
            }
        }
    }

    let extraction = if code.is_none() {
        FailureExtraction::Unknown {
            reason: "command timed out or terminated without an exit code".to_owned(),
        }
    } else if code == Some(0) {
        FailureExtraction::Unknown {
            reason: "command exited 0".to_owned(),
        }
    } else if build_failed {
        FailureExtraction::Unknown {
            reason: "command failed to build or link".to_owned(),
        }
    } else if libtest_total.incomplete || nextest_total.incomplete {
        FailureExtraction::Unknown {
            reason: "test run did not finish with a final summary".to_owned(),
        }
    } else if libtest_total.overflow || nextest_total.overflow {
        FailureExtraction::Unknown {
            reason: format!(
                "more than {MAX_TERMINAL_FAILURES} terminal test failures reported; complete comparison unavailable"
            ),
        }
    } else if libtest_total.inconsistent || nextest_total.inconsistent {
        FailureExtraction::Unknown {
            reason: "reported failure count did not match listed terminal failures".to_owned(),
        }
    } else if libtest_total.failed_runs > 0 && nextest_total.failed_runs > 0 {
        FailureExtraction::Unknown {
            reason: "multiple test runners reported failures in one command; completeness cannot be confirmed"
                .to_owned(),
        }
    } else if nextest_total.failed_runs > 0 {
        finalize_confirmed(nextest_total.expected_failures, nextest_total.failures)
    } else if libtest_total.failed_runs > 0 {
        finalize_confirmed(libtest_total.expected_failures, libtest_total.failures)
    } else {
        FailureExtraction::Unknown {
            reason: "command exited non-zero without a complete failing test summary".to_owned(),
        }
    };

    FailureEvidence {
        extraction,
        intermediate,
    }
}

fn finalize_confirmed(expected: usize, raw: Vec<TerminalFailure>) -> FailureExtraction {
    if expected == 0 || raw.is_empty() {
        return FailureExtraction::Unknown {
            reason: "no terminal test failures were listed in the final summary".to_owned(),
        };
    }
    if expected > MAX_TERMINAL_FAILURES || raw.len() > MAX_TERMINAL_FAILURES {
        return FailureExtraction::Unknown {
            reason: format!(
                "more than {MAX_TERMINAL_FAILURES} terminal test failures reported; complete comparison unavailable"
            ),
        };
    }
    let set: BTreeSet<TerminalFailure> = raw.into_iter().collect();
    if set.len() != expected {
        return FailureExtraction::Unknown {
            reason: format!(
                "reported failure count ({expected}) did not match distinct terminal failures ({})",
                set.len()
            ),
        };
    }
    FailureExtraction::Confirmed {
        failures: set.into_iter().collect(),
    }
}

/// Compare two consecutive failing attempts of the same command on commit `head`.
pub fn compare_attempts(
    head: &str,
    first: &CommandOutcome,
    second: &CommandOutcome,
) -> RerunComparison {
    let first_ev = AttemptEvidence::from_outcome(1, first);
    let second_ev = AttemptEvidence::from_outcome(2, second);
    let verdict = match (&first_ev.extraction, &second_ev.extraction) {
        (
            FailureExtraction::Confirmed { failures: f1 },
            FailureExtraction::Confirmed { failures: f2 },
        ) if !f1.is_empty() && !f2.is_empty() => {
            if f1.iter().chain(f2).any(|f| f.suite.is_none()) {
                ComparisonVerdict::Unknown {
                    reason: "terminal failure has no confirmed test binary".to_owned(),
                }
            } else if f1 == f2 {
                ComparisonVerdict::Matching {
                    failures: f1.clone(),
                }
            } else {
                let s1: BTreeSet<&TerminalFailure> = f1.iter().collect();
                let s2: BTreeSet<&TerminalFailure> = f2.iter().collect();
                let common = s1.intersection(&s2).map(|f| (*f).clone()).collect();
                let first_only = s1.difference(&s2).map(|f| (*f).clone()).collect();
                let second_only = s2.difference(&s1).map(|f| (*f).clone()).collect();
                ComparisonVerdict::Distinct {
                    common,
                    first_only,
                    second_only,
                }
            }
        }
        (FailureExtraction::Unknown { reason: r1 }, FailureExtraction::Unknown { reason: r2 }) => {
            ComparisonVerdict::Unknown {
                reason: if r1 == r2 {
                    format!("both attempts: {r1}")
                } else {
                    format!("attempt 1: {r1}; attempt 2: {r2}")
                },
            }
        }
        (FailureExtraction::Unknown { reason }, _) => ComparisonVerdict::Unknown {
            reason: format!("attempt 1: {reason}"),
        },
        (_, FailureExtraction::Unknown { reason }) => ComparisonVerdict::Unknown {
            reason: format!("attempt 2: {reason}"),
        },
        _ => ComparisonVerdict::Unknown {
            reason: "empty confirmed terminal failure set".to_owned(),
        },
    };
    RerunComparison {
        head: head.to_owned(),
        first: first_ev,
        second: second_ev,
        verdict,
    }
}

// ---------------------------------------------------------------------------
// Libtest parser
// ---------------------------------------------------------------------------

#[derive(Default)]
struct LibtestSummary {
    runs: usize,
    suites: usize,
    only_suite: Option<String>,
    failed_runs: usize,
    expected_failures: usize,
    failures: Vec<TerminalFailure>,
    incomplete: bool,
    inconsistent: bool,
    overflow: bool,
}

impl LibtestSummary {
    fn merge(&mut self, other: Self) {
        self.runs += other.runs;
        self.suites += other.suites;
        if other.only_suite.is_some() {
            self.only_suite = other.only_suite;
        }
        self.failed_runs += other.failed_runs;
        self.expected_failures += other.expected_failures;
        self.failures.extend(other.failures);
        self.incomplete |= other.incomplete;
        self.inconsistent |= other.inconsistent;
        self.overflow |= other.overflow;
    }
}

struct ActiveLibtest {
    expected_total: usize,
    suite: Option<String>,
    live_failed: BTreeSet<String>,
    in_failures_section: bool,
    captured_start: Option<usize>,
    collecting_list: bool,
    current_list: Vec<String>,
    last_valid_closing: Option<(usize, usize, Vec<String>)>,
    saw_overflow: bool,
}

struct LibtestResultLine {
    ok: bool,
    passed: usize,
    failed: usize,
    ignored: usize,
    measured: usize,
}

impl LibtestResultLine {
    fn total(&self) -> usize {
        self.passed + self.failed + self.ignored + self.measured
    }
}

fn parse_libtest_stream(
    lines: &[String],
    intermediate: &mut IntermediateNotes,
) -> (LibtestSummary, Vec<(usize, usize)>) {
    let mut summary = LibtestSummary::default();
    let mut captured_ranges: Vec<(usize, usize)> = Vec::new();
    let mut pending_suite: Option<String> = None;
    let mut active: Option<ActiveLibtest> = None;
    let mut standalone_collecting = false;
    let mut standalone_list: Vec<String> = Vec::new();
    let mut nextest_captured = false;

    let commit_active = |run: ActiveLibtest,
                         summary: &mut LibtestSummary,
                         captured_ranges: &mut Vec<(usize, usize)>| {
        if run.saw_overflow {
            summary.overflow = true;
        } else if let Some((end_line, expected_f, list)) = run.last_valid_closing {
            if let Some(start) = run.captured_start {
                captured_ranges.push((start, end_line));
            }
            summary.failed_runs += 1;
            summary.expected_failures += expected_f;
            for test in list {
                summary.failures.push(TerminalFailure {
                    suite: run.suite.clone(),
                    test,
                });
            }
        } else {
            summary.incomplete = true;
        }
    };

    for (idx, line) in lines.iter().enumerate() {
        let trimmed = line.trim();

        // If an active libtest run already found a valid closing summary and a new
        // top-level runner boundary begins, close the previous run first.
        if let Some(run) = active.as_ref()
            && run.in_failures_section
            && run.last_valid_closing.is_some()
            && (parse_libtest_running_header(trimmed).is_some()
                || parse_cargo_running_suite(trimmed).is_some()
                || parse_nextest_summary_header(trimmed).is_some()
                || trimmed == "------------")
            && let Some(finished) = active.take()
        {
            commit_active(finished, &mut summary, &mut captured_ranges);
        }

        if active.is_none() {
            if is_nextest_captured_header(trimmed) {
                nextest_captured = true;
                continue;
            }
            if nextest_captured {
                if trimmed == "------------" {
                    nextest_captured = false;
                } else {
                    continue;
                }
            }
        }

        if let Some(run) = active.as_mut() {
            if !run.in_failures_section {
                if trimmed == "failures:" && !line.starts_with(' ') {
                    run.in_failures_section = true;
                    run.captured_start = Some(idx);
                    run.collecting_list = true;
                    run.current_list.clear();
                } else if let Some(name) = parse_libtest_live_failed(trimmed) {
                    run.live_failed.insert(name);
                } else if let Some(name) = parse_libtest_long_running(trimmed) {
                    intermediate.record_long_running(name);
                } else if let Some(res) = parse_libtest_result(trimmed) {
                    if res.ok
                        && res.failed == 0
                        && res.total() == run.expected_total
                        && run.live_failed.is_empty()
                    {
                        active = None;
                    } else {
                        summary.inconsistent = true;
                        active = None;
                    }
                } else if parse_libtest_running_header(trimmed).is_some()
                    || parse_nextest_summary_header(trimmed).is_some()
                {
                    if let Some(unfinished) = active.take() {
                        commit_active(unfinished, &mut summary, &mut captured_ranges);
                    }
                    if let Some(total) = parse_libtest_running_header(trimmed) {
                        summary.runs += 1;
                        active = Some(new_active_libtest(total, pending_suite.take()));
                    }
                }
            } else {
                // Inside failures: section (captured output + final failures list).
                if trimmed == "failures:" && !line.starts_with(' ') {
                    run.collecting_list = true;
                    run.current_list.clear();
                } else if run.collecting_list {
                    if line.starts_with("    ")
                        && !trimmed.is_empty()
                        && !trimmed.starts_with("---- ")
                    {
                        if run.current_list.len() <= MAX_TERMINAL_FAILURES {
                            run.current_list.push(trimmed.to_owned());
                        }
                    } else if trimmed.is_empty() {
                        run.collecting_list = false;
                    } else if let Some(res) = parse_libtest_result(trimmed) {
                        run.collecting_list = false;
                        try_close_libtest(run, idx, &res);
                    } else {
                        run.collecting_list = false;
                        run.current_list.clear();
                    }
                } else if let Some(res) = parse_libtest_result(trimmed) {
                    try_close_libtest(run, idx, &res);
                }
            }
            continue;
        }

        // Idle state (outside an active `running N tests` block).
        if let Some(suite) = parse_cargo_running_suite(trimmed) {
            summary.suites += 1;
            summary.only_suite = Some(suite.clone());
            pending_suite = Some(suite);
            standalone_collecting = false;
            standalone_list.clear();
        } else if let Some(total) = parse_libtest_running_header(trimmed) {
            summary.runs += 1;
            standalone_collecting = false;
            standalone_list.clear();
            active = Some(new_active_libtest(total, pending_suite.take()));
        } else if trimmed == "failures:" && !line.starts_with(' ') {
            standalone_collecting = true;
            standalone_list.clear();
        } else if standalone_collecting {
            if line.starts_with("    ") && !trimmed.is_empty() && !trimmed.starts_with("---- ") {
                if standalone_list.len() <= MAX_TERMINAL_FAILURES {
                    standalone_list.push(trimmed.to_owned());
                }
            } else if trimmed.is_empty() {
                standalone_collecting = false;
            } else if let Some(res) = parse_libtest_result(trimmed) {
                standalone_collecting = false;
                record_standalone_libtest(&res, &standalone_list, &mut summary);
                standalone_list.clear();
            } else {
                standalone_collecting = false;
                standalone_list.clear();
            }
        } else if let Some(res) = parse_libtest_result(trimmed) {
            record_standalone_libtest(&res, &standalone_list, &mut summary);
            standalone_list.clear();
            pending_suite = None;
        } else if let Some(name) = parse_libtest_long_running(trimmed) {
            intermediate.record_long_running(name);
        }
    }

    if let Some(run) = active.take() {
        commit_active(run, &mut summary, &mut captured_ranges);
    }

    (summary, captured_ranges)
}

fn new_active_libtest(expected_total: usize, suite: Option<String>) -> ActiveLibtest {
    ActiveLibtest {
        expected_total,
        suite,
        live_failed: BTreeSet::new(),
        in_failures_section: false,
        captured_start: None,
        collecting_list: false,
        current_list: Vec::new(),
        last_valid_closing: None,
        saw_overflow: false,
    }
}

fn try_close_libtest(run: &mut ActiveLibtest, idx: usize, res: &LibtestResultLine) {
    if res.ok || res.failed == 0 || res.total() != run.expected_total {
        return;
    }
    if res.failed > MAX_TERMINAL_FAILURES || run.current_list.len() > MAX_TERMINAL_FAILURES {
        run.saw_overflow = true;
        return;
    }
    if run.current_list.len() != res.failed {
        return;
    }
    if !run.live_failed.is_empty()
        && (run.live_failed.len() != res.failed
            || !run.current_list.iter().all(|n| run.live_failed.contains(n)))
    {
        return;
    }
    run.last_valid_closing = Some((idx, res.failed, run.current_list.clone()));
}

fn record_standalone_libtest(
    res: &LibtestResultLine,
    list: &[String],
    summary: &mut LibtestSummary,
) {
    summary.runs += 1;
    if res.ok && res.failed == 0 {
        return;
    }
    if res.failed > MAX_TERMINAL_FAILURES || list.len() > MAX_TERMINAL_FAILURES {
        summary.overflow = true;
        return;
    }
    if res.failed == 0 || list.len() != res.failed {
        summary.inconsistent = true;
        return;
    }
    summary.failed_runs += 1;
    summary.expected_failures += res.failed;
    for test in list {
        summary.failures.push(TerminalFailure {
            suite: None,
            test: test.clone(),
        });
    }
}

fn parse_cargo_running_suite(trimmed: &str) -> Option<String> {
    if let Some(rest) = trimmed.strip_prefix("Running ")
        && let Some((label, binary)) = rest.split_once(" (")
        && let Some(binary) = binary.strip_suffix(')')
        && let Some(name) = binary.rsplit(['/', '\\']).next()
        && !label.is_empty()
        && !name.is_empty()
    {
        return Some(format!("{label} ({name})"));
    }
    if let Some(rest) = trimmed.strip_prefix("Doc-tests ") {
        let label = rest.trim();
        if !label.is_empty() {
            return Some(format!("doc-tests {label}"));
        }
    }
    None
}

fn parse_libtest_running_header(trimmed: &str) -> Option<usize> {
    let rest = trimmed.strip_prefix("running ")?;
    let num = rest
        .strip_suffix(" tests")
        .or_else(|| rest.strip_suffix(" test"))?;
    num.parse::<usize>().ok()
}

fn parse_libtest_live_failed(trimmed: &str) -> Option<String> {
    let rest = trimmed.strip_prefix("test ")?;
    let name = rest.strip_suffix(" ... FAILED")?;
    let name = name.strip_suffix(" - should panic").unwrap_or(name).trim();
    (!name.is_empty()).then(|| name.to_owned())
}

fn parse_libtest_long_running(trimmed: &str) -> Option<String> {
    let rest = trimmed.strip_prefix("test ")?;
    let (name, _) = rest.split_once(" has been running for over ")?;
    let name = name.trim();
    (!name.is_empty()).then(|| name.to_owned())
}

fn parse_libtest_result(trimmed: &str) -> Option<LibtestResultLine> {
    let (ok, rest) = if let Some(r) = trimmed.strip_prefix("test result: ok.") {
        (true, r)
    } else {
        (false, trimmed.strip_prefix("test result: FAILED.")?)
    };
    let mut passed = None;
    let mut failed = None;
    let mut ignored = 0usize;
    let mut measured = 0usize;
    for part in rest.split(';') {
        let p = part.trim();
        if let Some(n) = p.strip_suffix(" passed").and_then(|s| s.parse().ok()) {
            passed = Some(n);
        } else if let Some(n) = p.strip_suffix(" failed").and_then(|s| s.parse().ok()) {
            failed = Some(n);
        } else if let Some(n) = p.strip_suffix(" ignored").and_then(|s| s.parse().ok()) {
            ignored = n;
        } else if let Some(n) = p.strip_suffix(" measured").and_then(|s| s.parse().ok()) {
            measured = n;
        }
    }
    Some(LibtestResultLine {
        ok,
        passed: passed?,
        failed: failed?,
        ignored,
        measured,
    })
}

/// A bounded command-output tail can confirm a contradiction, but cannot
/// establish that an unrecognized test runner actually ran tests.
pub(crate) fn contradicts_passing_test(output: &str) -> bool {
    let mut saw_result = false;
    let mut saw_pass = false;
    let mut saw_empty_run = false;
    for line in output.lines().map(str::trim) {
        if let Some(result) = parse_libtest_result(line) {
            if !result.ok || result.failed != 0 {
                return true;
            }
            saw_result = true;
            saw_pass |= result.passed != 0;
        } else if line == "running 0 tests" {
            saw_empty_run = true;
        }
    }
    !saw_pass && (saw_result || saw_empty_run)
}

// ---------------------------------------------------------------------------
// Nextest parser
// ---------------------------------------------------------------------------

#[derive(Default)]
struct NextestSummary {
    failed_runs: usize,
    expected_failures: usize,
    failures: Vec<TerminalFailure>,
    incomplete: bool,
    inconsistent: bool,
    overflow: bool,
}

impl NextestSummary {
    fn merge(&mut self, other: Self) {
        self.failed_runs += other.failed_runs;
        self.expected_failures += other.expected_failures;
        self.failures.extend(other.failures);
        self.incomplete |= other.incomplete;
        self.inconsistent |= other.inconsistent;
        self.overflow |= other.overflow;
    }
}

struct NextestHeader {
    expected_failures: usize,
    flaky_count: usize,
    cancelled: bool,
    not_run: Option<usize>,
    unrecognized_failure_token: bool,
}

enum NextestStatusKind {
    TerminalFailure,
    Try,
    Flaky,
    Cancel,
    NonFailure,
}

struct NextestStatusLine {
    kind: NextestStatusKind,
    failure: TerminalFailure,
}

fn in_ranges(idx: usize, ranges: &[(usize, usize)]) -> bool {
    ranges.iter().any(|&(a, b)| idx >= a && idx <= b)
}

fn parse_nextest_stream(
    lines: &[String],
    libtest_captured: &[(usize, usize)],
    intermediate: &mut IntermediateNotes,
) -> NextestSummary {
    let mut summary = NextestSummary::default();
    let mut in_captured = false;
    let mut started_open = false;
    let mut saw_summary_flaky = 0usize;
    let mut i = 0usize;

    while i < lines.len() {
        if in_ranges(i, libtest_captured) {
            i += 1;
            continue;
        }
        let trimmed = lines[i].trim();

        if is_nextest_captured_header(trimmed) {
            in_captured = true;
            i += 1;
            continue;
        }
        if trimmed == "------------" {
            in_captured = false;
            i += 1;
            continue;
        }
        if in_captured {
            // A genuine nextest cancellation banner or new start ends captured output.
            if trimmed.starts_with("Cancelling due to ")
                || trimmed == "test run cancelled"
                || is_nextest_starting_line(trimmed)
            {
                in_captured = false;
            } else {
                i += 1;
                continue;
            }
        }

        if is_nextest_starting_line(trimmed) {
            started_open = true;
            i += 1;
            continue;
        }

        if trimmed.starts_with("Cancelling due to ") || trimmed == "test run cancelled" {
            intermediate.cancelled = true;
            i += 1;
            continue;
        }

        if let Some(header) = parse_nextest_summary_header(trimmed) {
            started_open = false;
            if header.cancelled {
                intermediate.cancelled = true;
            }
            if let Some(not_run) = header.not_run {
                intermediate.not_run_count =
                    Some(intermediate.not_run_count.unwrap_or(0) + not_run);
            }
            saw_summary_flaky += header.flaky_count;
            if header.unrecognized_failure_token {
                summary.inconsistent = true;
            }

            let mut block_failures = Vec::new();
            let mut j = i + 1;
            while j < lines.len() && !in_ranges(j, libtest_captured) {
                let next_trimmed = lines[j].trim();
                if next_trimmed.is_empty()
                    || next_trimmed == "------------"
                    || next_trimmed.starts_with("error:")
                    || next_trimmed.starts_with("[cargo-make]")
                    || parse_nextest_summary_header(next_trimmed).is_some()
                {
                    break;
                }
                let Some(parsed) = parse_nextest_status_line(next_trimmed) else {
                    break;
                };
                match parsed.kind {
                    NextestStatusKind::TerminalFailure => {
                        if block_failures.len() <= MAX_TERMINAL_FAILURES {
                            block_failures.push(parsed.failure);
                        }
                    }
                    NextestStatusKind::Try => {
                        // Intermediate retry failures inside Summary are strictly separated
                        // from terminal failures.
                        intermediate.record_try(parsed.failure.display_name());
                    }
                    NextestStatusKind::Flaky => {
                        intermediate.record_flaky(parsed.failure.display_name());
                    }
                    NextestStatusKind::Cancel => {
                        intermediate.cancelled = true;
                    }
                    NextestStatusKind::NonFailure => {}
                }
                j += 1;
            }

            if header.expected_failures > MAX_TERMINAL_FAILURES
                || block_failures.len() > MAX_TERMINAL_FAILURES
            {
                summary.overflow = true;
            } else if header.expected_failures == 0 {
                if !block_failures.is_empty() {
                    summary.inconsistent = true;
                }
            } else if block_failures.len() != header.expected_failures {
                summary.inconsistent = true;
            } else {
                summary.failed_runs += 1;
                summary.expected_failures += header.expected_failures;
                summary.failures.extend(block_failures);
            }
            i = j;
            continue;
        }

        // Non-summary status lines during execution: record intermediate TRY / FLAKY / CANCEL
        // only, never terminal failures (only final Summary decides terminal failures).
        if let Some(parsed) = parse_nextest_status_line(trimmed) {
            match parsed.kind {
                NextestStatusKind::Try => intermediate.record_try(parsed.failure.display_name()),
                NextestStatusKind::Flaky => {
                    intermediate.record_flaky(parsed.failure.display_name())
                }
                NextestStatusKind::Cancel => intermediate.cancelled = true,
                NextestStatusKind::TerminalFailure | NextestStatusKind::NonFailure => {}
            }
        }
        i += 1;
    }

    if saw_summary_flaky > intermediate.flaky_count {
        intermediate.flaky_count = saw_summary_flaky;
    }
    if started_open {
        summary.incomplete = true;
    }
    summary
}

fn is_nextest_captured_header(trimmed: &str) -> bool {
    (trimmed.starts_with("--- STDOUT:") || trimmed.starts_with("--- STDERR:"))
        && trimmed.ends_with("---")
}

fn is_nextest_starting_line(trimmed: &str) -> bool {
    let Some(rest) = trimmed.strip_prefix("Starting ") else {
        return false;
    };
    let mut parts = rest.split_whitespace();
    let Some(count) = parts.next().and_then(|n| n.parse::<usize>().ok()) else {
        return false;
    };
    count > 0 && matches!(parts.next(), Some("test" | "tests"))
}

fn parse_nextest_summary_header(trimmed: &str) -> Option<NextestHeader> {
    let rest = trimmed.strip_prefix("Summary [")?;
    let (dur, after) = rest.split_once(']')?;
    if !is_duration_token(dur.trim()) {
        return None;
    }
    let (counts_part, details) = after.trim().split_once(':')?;
    let run_spec = counts_part
        .trim()
        .strip_suffix(" tests run")
        .or_else(|| counts_part.trim().strip_suffix(" test run"))?;

    let mut cancelled = false;
    let mut not_run = None;
    if let Some((ran, total)) = run_spec.split_once('/') {
        let ran: usize = ran.trim().parse().ok()?;
        let total: usize = total.trim().parse().ok()?;
        if ran < total {
            cancelled = true;
            not_run = Some(total - ran);
        }
    } else {
        run_spec.trim().parse::<usize>().ok()?;
    }

    let mut expected_failures = 0usize;
    let mut flaky_count = 0usize;
    let mut unrecognized_failure_token = false;

    for segment in split_top_level_commas(details) {
        let seg = segment.trim();
        if seg.is_empty() {
            continue;
        }
        let (main, paren) = match seg.split_once('(') {
            Some((m, p)) => (m.trim(), Some(p.trim_end_matches(')').trim())),
            None => (seg, None),
        };
        if let Some(p) = paren {
            for sub in p.split(',') {
                let sub = sub.trim();
                if let Some(n) = sub
                    .strip_suffix(" flaky")
                    .and_then(|s| s.parse::<usize>().ok())
                {
                    flaky_count += n;
                }
                if sub.contains("cancel") {
                    cancelled = true;
                }
            }
        }
        let mut words = main.splitn(2, char::is_whitespace);
        let Some(n) = words.next().and_then(|s| s.parse::<usize>().ok()) else {
            if main.contains("cancel") {
                cancelled = true;
            }
            continue;
        };
        let label = words.next().unwrap_or("").trim();
        match label {
            "passed" | "skipped" => {}
            "failed" | "timed out" | "exec failed" => expected_failures += n,
            "not run" => {
                if n > 0 {
                    cancelled = true;
                    not_run = Some(not_run.unwrap_or(0).max(n));
                }
            }
            other => {
                let lower = other.to_lowercase();
                if lower.contains("fail") || lower.contains("abort") || lower.contains("timeout") {
                    unrecognized_failure_token = true;
                }
            }
        }
    }

    Some(NextestHeader {
        expected_failures,
        flaky_count,
        cancelled,
        not_run,
        unrecognized_failure_token,
    })
}

fn split_top_level_commas(input: &str) -> Vec<&str> {
    let mut parts = Vec::new();
    let mut depth = 0usize;
    let mut start = 0usize;
    for (idx, ch) in input.char_indices() {
        match ch {
            '(' => depth += 1,
            ')' => depth = depth.saturating_sub(1),
            ',' if depth == 0 => {
                parts.push(&input[start..idx]);
                start = idx + 1;
            }
            _ => {}
        }
    }
    parts.push(&input[start..]);
    parts
}

fn is_duration_token(tok: &str) -> bool {
    let Some(num) = tok.strip_suffix('s') else {
        return false;
    };
    !num.is_empty() && num.bytes().all(|b| b.is_ascii_digit() || b == b'.')
}

fn parse_nextest_status_line(trimmed: &str) -> Option<NextestStatusLine> {
    let (raw_prefix, after_open) = trimmed.split_once('[')?;
    let (dur, rest) = after_open.split_once(']')?;
    if !is_duration_token(dur.trim()) {
        return None;
    }
    let prefix = raw_prefix.trim();
    let kind = if prefix.starts_with("TRY ") {
        NextestStatusKind::Try
    } else if prefix == "FLAKY" || prefix.starts_with("FLAKY ") {
        NextestStatusKind::Flaky
    } else if matches!(prefix, "CANCEL" | "CANCELLED" | "TERMINATING") {
        NextestStatusKind::Cancel
    } else if matches!(prefix, "PASS" | "SKIP" | "LEAK" | "SLOW" | "START") {
        NextestStatusKind::NonFailure
    } else if matches!(
        prefix,
        "FAIL" | "TIMEOUT" | "ABORT" | "EXECFAIL" | "LEAK-FAIL" | "FAIL+LEAK"
    ) || (prefix.starts_with("SIG")
        && prefix.len() >= 5
        && prefix
            .bytes()
            .all(|b| b.is_ascii_uppercase() || b.is_ascii_digit()))
    {
        NextestStatusKind::TerminalFailure
    } else {
        return None;
    };

    let mut rest = rest.trim();
    // Skip optional nextest progress token such as `(1/10)`.
    if let Some(after_paren) = rest.strip_prefix('(')
        && let Some((counter, tail)) = after_paren.split_once(')')
        && let Some((a, b)) = counter.split_once('/')
        && a.bytes().all(|c| c.is_ascii_digit())
        && b.bytes().all(|c| c.is_ascii_digit())
    {
        rest = tail.trim();
    }

    let mut parts = rest.split_whitespace();
    let suite = parts.next()?;
    let test = rest[suite.len()..].trim();
    if suite.is_empty() || test.is_empty() {
        return None;
    }
    Some(NextestStatusLine {
        kind,
        failure: TerminalFailure {
            suite: Some(suite.to_owned()),
            test: test.to_owned(),
        },
    })
}

// ---------------------------------------------------------------------------
// Shared formatting helpers
// ---------------------------------------------------------------------------

fn push_bounded_unique(list: &mut Vec<String>, item: String, max: usize) {
    if list.len() < max && !list.contains(&item) {
        list.push(item);
    }
}

fn join_bounded(items: &[String], max: usize) -> String {
    if items.len() <= max {
        return items.join(", ");
    }
    let shown = items[..max].join(", ");
    format!("{shown} (+{} more)", items.len() - max)
}

fn format_failure_list(failures: &[TerminalFailure], max: usize) -> String {
    let names: Vec<String> = failures.iter().map(TerminalFailure::display_name).collect();
    join_bounded(&names, max)
}

fn format_artifact_pair(c: &RerunComparison) -> Option<String> {
    match (&c.first.artifact, &c.second.artifact) {
        (Some(a), Some(b)) => Some(format!("attempt 1: {a}, attempt 2: {b}")),
        (Some(a), None) => Some(format!("attempt 1: {a}")),
        (None, Some(b)) => Some(format!("attempt 2: {b}")),
        (None, None) => None,
    }
}

fn short_sha(sha: &str) -> &str {
    sha.get(..8).unwrap_or(sha)
}

/// Compact one-line summary of a [`RerunComparison`], shared across events, CLI/web
/// reports, stop reasons, and automatic task chat reports.
pub fn comparison_headline(c: &RerunComparison) -> String {
    let head = short_sha(&c.head);
    let artifacts = format_artifact_pair(c)
        .map(|a| format!(" ({a})"))
        .unwrap_or_default();
    match &c.verdict {
        ComparisonVerdict::Matching { failures } => format!(
            "failed twice on {head} with matching terminal failure(s): {}{artifacts}",
            format_failure_list(failures, DISPLAY_FAILURE_LIMIT),
        ),
        ComparisonVerdict::Distinct {
            common,
            first_only,
            second_only,
        } => {
            let mut parts = Vec::new();
            if !first_only.is_empty() {
                parts.push(format!(
                    "attempt 1 only: {}",
                    format_failure_list(first_only, DISPLAY_FAILURE_LIMIT)
                ));
            }
            if !second_only.is_empty() {
                parts.push(format!(
                    "attempt 2 only: {}",
                    format_failure_list(second_only, DISPLAY_FAILURE_LIMIT)
                ));
            }
            if !common.is_empty() {
                parts.push(format!(
                    "common: {}",
                    format_failure_list(common, DISPLAY_FAILURE_LIMIT)
                ));
            }
            format!(
                "failed twice on {head} with distinct terminal failure(s) ({}){artifacts}",
                parts.join("; "),
            )
        }
        ComparisonVerdict::Unknown { reason } => {
            let mut known_sides = Vec::new();
            if let FailureExtraction::Confirmed { failures } = &c.first.extraction {
                known_sides.push(format!(
                    "attempt 1 terminal failure(s): {}",
                    format_failure_list(failures, DISPLAY_FAILURE_LIMIT)
                ));
            }
            if let FailureExtraction::Confirmed { failures } = &c.second.extraction {
                known_sides.push(format!(
                    "attempt 2 terminal failure(s): {}",
                    format_failure_list(failures, DISPLAY_FAILURE_LIMIT)
                ));
            }
            let detail = if known_sides.is_empty() {
                reason.clone()
            } else {
                format!("{reason}; {}", known_sides.join("; "))
            };
            format!(
                "exited non-zero twice on {head}; terminal failure comparison unknown ({detail}){artifacts}"
            )
        }
    }
}

/// Concise comparison or single-attempt summary line for one failing [`CommandOutcome`],
/// if structured evidence or a legacy double-failure marker is present.
pub fn outcome_comparison_summary(o: &CommandOutcome) -> Option<String> {
    if let Some(c) = &o.rerun_comparison {
        return Some(comparison_headline(c));
    }
    if legacy_failed_twice(&o.output_tail) {
        return Some(
            "legacy record: command exited non-zero twice without structured attempt comparison (comparison unknown)"
                .to_owned(),
        );
    }
    if let Some(ev) = &o.failure_evidence {
        match &ev.extraction {
            FailureExtraction::Confirmed { failures } if !failures.is_empty() => {
                let mut line = format!(
                    "terminal failure(s): {}",
                    format_failure_list(failures, DISPLAY_FAILURE_LIMIT)
                );
                if let Some(inter) = ev.intermediate.summary() {
                    line.push_str(&format!("; non-terminal: {inter}"));
                }
                if let Some(art) = &o.artifact {
                    line.push_str(&format!(" ({art})"));
                }
                return Some(line);
            }
            _ => {}
        }
    }
    None
}

/// Format a `verify` event line for one command that failed twice on the same commit.
pub fn rerun_event_message(context: &str, command: &str, c: &RerunComparison) -> String {
    let headline = comparison_headline(c);
    let suffix = match &c.verdict {
        ComparisonVerdict::Matching { .. } => "the failure reproduced — it stands",
        ComparisonVerdict::Distinct { .. } => {
            "failures did not match across attempts — verification stays red"
        }
        ComparisonVerdict::Unknown { .. } => {
            "cannot conclude the same failure reproduced — verification stays red"
        }
    };
    format!("{context}: `{command}` {headline}; {suffix}")
}

/// Format one failed [`CommandOutcome`] for fixer/reviewer/recovery prompts.
pub fn format_command_prompt_section(o: &CommandOutcome, max_output: usize) -> String {
    if let Some(c) = &o.rerun_comparison {
        let mut s = format!("$ {}\n", o.command);
        let _ = writeln!(s, "Rerun comparison: {}", comparison_headline(c));
        match &c.verdict {
            ComparisonVerdict::Matching { .. } => {
                s.push_str(
                    "Both attempts ended with the same confirmed terminal test failure(s).\n",
                );
            }
            ComparisonVerdict::Distinct { .. } => {
                s.push_str(
                    "The two attempts ended with DIFFERENT terminal test failures; do not assume one root cause covers both.\n",
                );
            }
            ComparisonVerdict::Unknown { .. } => {
                s.push_str(
                    "The command exited non-zero twice, but terminal test failures could not be confirmed for both attempts; do not assume the same failure reproduced.\n",
                );
            }
        }
        format_attempt_prompt(&mut s, &c.first, ATTEMPT_EXCERPT_MAX);
        format_attempt_prompt(&mut s, &c.second, max_output);
        return s;
    }
    if legacy_failed_twice(&o.output_tail) {
        let stripped = o
            .output_tail
            .trim_start()
            .strip_prefix(LEGACY_FAILED_TWICE_PREFIX)
            .unwrap_or(&o.output_tail)
            .trim_start();
        return format!(
            "$ {}\n[legacy record: command exited non-zero twice without structured attempt comparison; comparison unknown — do not assume the same test failed twice]\n{}\n",
            o.command,
            summarize_verify(stripped, max_output),
        );
    }
    let mut header = String::new();
    if let Some(art) = &o.artifact
        && !o.output_tail.contains(art)
    {
        let _ = writeln!(header, "[full output: {art}]");
    }
    if let Some(ev) = &o.failure_evidence {
        if let FailureExtraction::Confirmed { failures } = &ev.extraction {
            let _ = writeln!(
                header,
                "[terminal failure(s): {}]",
                format_failure_list(failures, DISPLAY_FAILURE_LIMIT)
            );
        }
        if let Some(inter) = ev.intermediate.summary() {
            let _ = writeln!(header, "[non-terminal activity: {inter}]");
        }
    }
    format!(
        "$ {}\n{}{}\n",
        o.command,
        header,
        summarize_verify(&o.output_tail, max_output),
    )
}

fn format_attempt_prompt(s: &mut String, a: &AttemptEvidence, max_excerpt: usize) {
    let code = a.code.map_or_else(|| "none".to_owned(), |c| c.to_string());
    let art = a
        .artifact
        .as_deref()
        .map(|p| format!(", artifact {p}"))
        .unwrap_or_default();
    let _ = writeln!(
        s,
        "\n--- Attempt {} (exit {code}, {}ms{art}) ---",
        a.attempt, a.duration_ms
    );
    match &a.extraction {
        FailureExtraction::Confirmed { failures } => {
            let _ = writeln!(
                s,
                "Terminal failure(s): {}",
                format_failure_list(failures, MAX_TERMINAL_FAILURES)
            );
        }
        FailureExtraction::Unknown { reason } => {
            let _ = writeln!(s, "Terminal failure(s): unknown ({reason})");
        }
    }
    if let Some(inter) = a.intermediate.summary() {
        let _ = writeln!(s, "Non-terminal activity (separate from verdict): {inter}");
    }
    let excerpt = summarize_verify(&a.excerpt, max_excerpt);
    if !excerpt.trim().is_empty() {
        let _ = writeln!(s, "{excerpt}");
    }
}

/// Format structured details for a failed command in CLI (`magi show`) output.
pub fn format_outcome_cli_lines(o: &CommandOutcome) -> Vec<String> {
    let mut lines = Vec::new();
    if let Some(c) = &o.rerun_comparison {
        lines.push(format!("comparison: {}", comparison_headline(c)));
        for a in [&c.first, &c.second] {
            let code = a.code.map_or_else(|| "none".to_owned(), |c| c.to_string());
            let art = a
                .artifact
                .as_deref()
                .map(|p| format!("  {p}"))
                .unwrap_or_default();
            let verdict = match &a.extraction {
                FailureExtraction::Confirmed { failures } => format!(
                    "terminal: {}",
                    format_failure_list(failures, DISPLAY_FAILURE_LIMIT)
                ),
                FailureExtraction::Unknown { reason } => format!("terminal: unknown ({reason})"),
            };
            let inter = a
                .intermediate
                .summary()
                .map(|m| format!("  [non-terminal: {m}]"))
                .unwrap_or_default();
            lines.push(format!(
                "attempt {} (exit {code}, {}ms{art}): {verdict}{inter}",
                a.attempt, a.duration_ms
            ));
        }
    } else {
        if legacy_failed_twice(&o.output_tail) {
            lines.push(
                "comparison: unknown (legacy record exited non-zero twice without structured attempt comparison)"
                    .to_owned(),
            );
        }
        if let Some(art) = &o.artifact {
            lines.push(format!("artifact: {art}"));
        }
        if let Some(ev) = &o.failure_evidence {
            match &ev.extraction {
                FailureExtraction::Confirmed { failures } => lines.push(format!(
                    "terminal failure(s): {}",
                    format_failure_list(failures, DISPLAY_FAILURE_LIMIT)
                )),
                FailureExtraction::Unknown { reason } => {
                    lines.push(format!("terminal failure(s): unknown ({reason})"))
                }
            }
            if let Some(inter) = ev.intermediate.summary() {
                lines.push(format!("non-terminal: {inter}"));
            }
        }
    }
    lines
}

/// Concise summary of the *current* unresolved e2e failure in `state`, if any.
///
/// Respects resolution order: never resurfaces an earlier review round's e2e failure
/// when `head_e2e` or `gate_repair_e2e` subsequently passed, and inspects `head_e2e`,
/// `gate_repair_e2e`, and the latest `ReviewRound` in precedence order.
pub fn current_e2e_failure_summary(state: &RunState) -> Option<String> {
    if let Some(e) = &state.head_e2e {
        if !e.passed {
            if let Some(line) = summarize_failed_outcomes(&e.outcomes) {
                return Some(format!("current head e2e @ {}: {line}", short_sha(&e.head)));
            }
        } else {
            return None;
        }
    }
    if let Some(e) = &state.gate_repair_e2e {
        if !e.passed {
            if let Some(line) = summarize_failed_outcomes(&e.outcomes) {
                return Some(format!("gate repair e2e @ {}: {line}", short_sha(&e.head)));
            }
        } else {
            return None;
        }
    }
    if let Some(r) = state.reviews.last()
        && r.e2e_status() == E2eStatus::Failed
        && let Some(line) = summarize_failed_outcomes(&r.e2e)
    {
        let head = r.verified_head.as_deref().unwrap_or(&r.head);
        return Some(format!(
            "round {} e2e @ {}: {line}",
            r.round,
            short_sha(head)
        ));
    }
    None
}

/// Summarize the first failing non-resource-blocked outcome in `outcomes`, preferring
/// structured rerun comparisons when present.
pub fn summarize_failed_outcomes(outcomes: &[CommandOutcome]) -> Option<String> {
    let failing: Vec<&CommandOutcome> = outcomes
        .iter()
        .filter(|o| !o.ok() && !o.resource_blocked)
        .collect();
    if failing.is_empty() {
        return None;
    }
    let parts: Vec<String> = failing
        .iter()
        .take(2)
        .map(|o| match outcome_comparison_summary(o) {
            Some(detail) => format!("`{}` {detail}", o.command),
            None => format!(
                "`{}` failed (exit {})",
                o.command,
                o.code.map_or_else(|| "none".to_owned(), |c| c.to_string())
            ),
        })
        .collect();
    Some(parts.join("; "))
}

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

    fn outcome(code: Option<i32>, raw: &str, artifact: Option<&str>) -> CommandOutcome {
        let build_failed = code != Some(0) && raw.contains("error: could not compile");
        let evidence = extract_combined(code, build_failed, raw);
        CommandOutcome {
            command: "cargo make test".to_owned(),
            code,
            output_tail: summarize_verify(raw, crate::run::VERIFY_SUMMARY),
            duration_ms: 1200,
            resource_blocked: false,
            artifact: artifact.map(str::to_owned),
            failure_evidence: Some(evidence),
            rerun_comparison: None,
        }
    }

    #[test]
    fn nextest_separates_terminal_failures_from_try_flaky_and_cancellations() {
        let raw = "\
Starting 12 tests across 3 binaries
\x1b[33m  TRY 1 FAIL\x1b[0m [   0.500s] magi-cli::chat_notify test_flaky_one
\x1b[33m   FLAKY 2/3\x1b[0m [   0.400s] magi-cli::chat_notify test_flaky_one
  TRY 1 FAIL [   1.000s] magi-cli::isolated_build_cache test_cache
  TRY 2 FAIL [   1.000s] magi-cli::isolated_build_cache test_cache
\x1b[31m        FAIL\x1b[0m [   1.200s] magi-cli::isolated_build_cache test_cache
Cancelling due to test failure: 2 tests still running
      CANCEL [   0.000s] magi-cli::web_render test_other
------------
     Summary [  10.500s] 5/12 tests run: 4 passed (1 flaky), 1 failed, 0 skipped
        FAIL [   1.200s] magi-cli::isolated_build_cache test_cache
  TRY 1 FAIL [   1.000s] magi-cli::isolated_build_cache test_cache
   FLAKY 2/3 [   0.400s] magi-cli::chat_notify test_flaky_one
error: test run failed
";
        let ev = extract_combined(Some(100), false, raw);
        assert_eq!(
            ev.extraction,
            FailureExtraction::Confirmed {
                failures: vec![TerminalFailure {
                    suite: Some("magi-cli::isolated_build_cache".to_owned()),
                    test: "test_cache".to_owned(),
                }]
            }
        );
        assert!(ev.intermediate.try_failure_count >= 2);
        assert_eq!(ev.intermediate.flaky_count, 1);
        assert!(ev.intermediate.cancelled);
        assert_eq!(ev.intermediate.not_run_count, Some(7));
    }

    #[test]
    fn typography_0b11_scenario_distinguishes_unit_failure_from_nextest_failure() {
        // Attempt 1: chat unit test failed in cargo test --lib (nextest never ran).
        let attempt1_out = "\
running 2 tests
test ok_test ... ok
test talk::tests::chat_timeout ... FAILED

failures:

---- talk::tests::chat_timeout stdout ----
thread 'talk::tests::chat_timeout' panicked at src/talk.rs:42:9:
timed out waiting for turn

failures:
    talk::tests::chat_timeout

test result: FAILED. 1 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 5.01s
";
        let attempt1_err =
            "Running unittests src/lib.rs (target/debug/deps/magi-abc)\nerror: test failed\n";
        let ev1 = extract_streams(Some(101), false, attempt1_out, attempt1_err);

        // Attempt 2: libtest passed on stdout; nextest failed on stderr naming isolated_build_cache.
        let attempt2_out = "\
running 2 tests
test ok_test ... ok
test talk::tests::chat_timeout ... ok

test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.12s
";
        let attempt2_err = "\
Running unittests src/lib.rs (target/debug/deps/magi-abc)
Starting 10 tests across 2 binaries
  TRY 1 FAIL [   0.300s] magi-cli::other test_transient
   FLAKY 2/3 [   0.200s] magi-cli::other test_transient
Cancelling due to test failure: 1 test still running
------------
     Summary [  12.000s] 6/10 tests run: 5 passed (1 flaky), 1 failed, 0 skipped
        FAIL [   2.500s] magi-cli::isolated_build_cache test_isolation
error: test run failed
";
        let ev2 = extract_streams(Some(100), false, attempt2_out, attempt2_err);

        let o1 = CommandOutcome {
            command: "cargo make test".to_owned(),
            code: Some(101),
            output_tail: attempt1_out.to_owned(),
            duration_ms: 5000,
            resource_blocked: false,
            artifact: Some("artifacts/e2e-3.out".to_owned()),
            failure_evidence: Some(ev1),
            rerun_comparison: None,
        };
        let o2 = CommandOutcome {
            command: "cargo make test".to_owned(),
            code: Some(100),
            output_tail: attempt2_err.to_owned(),
            duration_ms: 12000,
            resource_blocked: false,
            artifact: Some("artifacts/e2e-4.out".to_owned()),
            failure_evidence: Some(ev2),
            rerun_comparison: None,
        };
        let cmp = compare_attempts("0b11aaaa1234", &o1, &o2);
        assert_eq!(
            cmp.verdict,
            ComparisonVerdict::Distinct {
                common: vec![],
                first_only: vec![TerminalFailure {
                    suite: Some("unittests src/lib.rs (magi-abc)".to_owned()),
                    test: "talk::tests::chat_timeout".to_owned(),
                }],
                second_only: vec![TerminalFailure {
                    suite: Some("magi-cli::isolated_build_cache".to_owned()),
                    test: "test_isolation".to_owned(),
                }],
            }
        );
        let headline = comparison_headline(&cmp);
        assert!(
            headline.contains("distinct terminal failure(s)"),
            "{headline}"
        );
        assert!(headline.contains("talk::tests::chat_timeout"), "{headline}");
        assert!(headline.contains("isolated_build_cache"), "{headline}");
        assert!(headline.contains("artifacts/e2e-3.out"), "{headline}");
        assert!(headline.contains("artifacts/e2e-4.out"), "{headline}");
        let event = rerun_event_message("round 1", "cargo make test", &cmp);
        assert!(!event.contains("reproduced"), "{event}");
    }

    #[test]
    fn hang_without_final_summary_is_unknown_not_matching() {
        let attempt1_out = "\
running 2 tests
test ok_test ... ok
test talk::tests::chat_timeout has been running for over 60 seconds
";
        let ev1 = extract_streams(Some(1), false, attempt1_out, "");
        assert!(matches!(ev1.extraction, FailureExtraction::Unknown { .. }));
        assert_eq!(
            ev1.intermediate.long_running,
            vec!["talk::tests::chat_timeout"]
        );

        let attempt2_err = "\
------------
     Summary [   5.000s] 3 tests run: 2 passed, 1 failed, 0 skipped
        FAIL [   1.000s] magi-cli::isolated_build_cache test_isolation
";
        let ev2 = extract_streams(Some(100), false, "", attempt2_err);
        assert!(matches!(
            ev2.extraction,
            FailureExtraction::Confirmed { .. }
        ));

        let o1 = CommandOutcome {
            command: "cargo make test".to_owned(),
            code: Some(1),
            output_tail: attempt1_out.to_owned(),
            duration_ms: 60_000,
            resource_blocked: false,
            artifact: Some("artifacts/e2e-3.out".to_owned()),
            failure_evidence: Some(ev1),
            rerun_comparison: None,
        };
        let o2 = CommandOutcome {
            command: "cargo make test".to_owned(),
            code: Some(100),
            output_tail: attempt2_err.to_owned(),
            duration_ms: 5_000,
            resource_blocked: false,
            artifact: Some("artifacts/e2e-4.out".to_owned()),
            failure_evidence: Some(ev2),
            rerun_comparison: None,
        };
        let cmp = compare_attempts("deadbeef", &o1, &o2);
        assert!(matches!(cmp.verdict, ComparisonVerdict::Unknown { .. }));
        let headline = comparison_headline(&cmp);
        assert!(headline.contains("comparison unknown"), "{headline}");
        assert!(headline.contains("isolated_build_cache"), "{headline}");
        assert!(headline.contains("artifacts/e2e-3.out"), "{headline}");
        assert!(headline.contains("artifacts/e2e-4.out"), "{headline}");
    }

    #[test]
    fn captured_stdout_cannot_spoof_libtest_or_nextest_verdict() {
        let raw = "\
running 2 tests
test ok_test ... ok
test real_test ... FAILED

failures:

---- real_test stdout ----
failures:
    fake_test

test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.01s
------------
     Summary [   0.100s] 1 tests run: 0 passed, 1 failed, 0 skipped
        FAIL [   0.100s] fake::bin fake_nextest

failures:
    real_test

test result: FAILED. 1 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.05s
";
        let ev = extract_combined(Some(101), false, raw);
        assert_eq!(
            ev.extraction,
            FailureExtraction::Confirmed {
                failures: vec![TerminalFailure {
                    suite: None,
                    test: "real_test".to_owned(),
                }]
            }
        );
    }

    #[test]
    fn count_mismatch_and_cap_overflow_yield_unknown() {
        let mismatch = "\
------------
     Summary [   1.000s] 2 tests run: 0 passed, 2 failed, 0 skipped
        FAIL [   0.500s] pkg::bin only_one
";
        assert!(matches!(
            extract_combined(Some(100), false, mismatch).extraction,
            FailureExtraction::Unknown { .. }
        ));

        let mut many =
            "------------\n     Summary [   1.000s] 40 tests run: 0 passed, 40 failed, 0 skipped\n"
                .to_owned();
        for i in 0..40 {
            let _ = writeln!(many, "        FAIL [   0.010s] pkg::bin test_{i}");
        }
        assert!(matches!(
            extract_combined(Some(100), false, &many).extraction,
            FailureExtraction::Unknown { .. }
        ));
    }

    #[test]
    fn matching_a_to_a_and_multi_binary_libtest() {
        let raw = "\
Running tests/alpha.rs (target/debug/deps/alpha-1234)
running 2 tests
test a_ok ... ok
test a_fail ... FAILED

failures:
    a_fail

test result: FAILED. 1 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.01s

Running tests/beta.rs (target/debug/deps/beta-5678)
running 1 test
test b_fail ... FAILED

failures:
    b_fail

test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.01s
";
        let o1 = outcome(Some(101), raw, Some("artifacts/e2e-1.out"));
        let o2 = outcome(Some(101), raw, Some("artifacts/e2e-2.out"));
        let cmp = compare_attempts("abcdef12", &o1, &o2);
        assert_eq!(
            cmp.verdict,
            ComparisonVerdict::Matching {
                failures: vec![
                    TerminalFailure {
                        suite: Some("tests/alpha.rs (alpha-1234)".to_owned()),
                        test: "a_fail".to_owned(),
                    },
                    TerminalFailure {
                        suite: Some("tests/beta.rs (beta-5678)".to_owned()),
                        test: "b_fail".to_owned(),
                    },
                ]
            }
        );
        let event = rerun_event_message("round 1", "cargo test", &cmp);
        assert!(event.contains("the failure reproduced"), "{event}");
    }

    #[test]
    fn split_cargo_streams_require_unambiguous_binary_identity() {
        let raw = "\
running 1 test
test test_layout ... FAILED

failures:
    test_layout

test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.01s
";
        let alpha = "Running tests/alpha.rs (target/debug/deps/alpha-1234)\n";
        let beta = "Running tests/beta.rs (target/debug/deps/beta-5678)\n";
        let make = |stderr: &str| {
            let mut o = outcome(Some(101), raw, None);
            o.failure_evidence = Some(extract_streams(Some(101), false, raw, stderr));
            o
        };
        let a = make(alpha);
        assert!(matches!(
            compare_attempts("head", &a, &make(alpha)).verdict,
            ComparisonVerdict::Matching { .. }
        ));
        assert!(matches!(
            compare_attempts("head", &a, &make(beta)).verdict,
            ComparisonVerdict::Distinct { .. }
        ));
        let same_label = "Running tests/alpha.rs (target/debug/deps/alpha-9999)\n";
        assert!(matches!(
            compare_attempts("head", &a, &make(same_label)).verdict,
            ComparisonVerdict::Distinct { .. }
        ));
        let ambiguous = make(&format!("{alpha}{beta}"));
        for unknown in [make(""), ambiguous] {
            assert!(matches!(
                compare_attempts("head", &unknown, &unknown).verdict,
                ComparisonVerdict::Unknown { .. }
            ));
        }
    }

    #[test]
    fn nextest_captured_libtest_failure_is_not_an_independent_run() {
        let raw = "\
Starting 1 test across 1 binary
        FAIL [   0.100s] pkg::bin outer
--- STDOUT: pkg::bin outer ---
running 1 test
test inner ... FAILED

failures:
    inner

test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.01s
------------
     Summary [   0.100s] 1 tests run: 0 passed, 1 failed, 0 skipped
        FAIL [   0.100s] pkg::bin outer
error: test run failed
";
        assert_eq!(
            extract_streams(Some(100), false, "", raw).extraction,
            FailureExtraction::Confirmed {
                failures: vec![TerminalFailure {
                    suite: Some("pkg::bin".to_owned()),
                    test: "outer".to_owned(),
                }],
            }
        );
    }

    #[test]
    fn legacy_failed_twice_is_treated_as_unknown() {
        let o = CommandOutcome {
            command: "cargo make test".to_owned(),
            code: Some(1),
            output_tail: format!("{LEGACY_FAILED_TWICE_PREFIX}\nsome output"),
            duration_ms: 100,
            resource_blocked: false,
            artifact: None,
            failure_evidence: None,
            rerun_comparison: None,
        };
        let summary = outcome_comparison_summary(&o).unwrap();
        assert!(summary.contains("comparison unknown"), "{summary}");
        assert!(!summary.contains("reproduced"), "{summary}");
    }
}