wavepeek 2.0.0

Command-line tool for RTL waveform inspection with deterministic machine-friendly output.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
#!/usr/bin/env python3

from __future__ import annotations

import argparse
from datetime import datetime, timezone
import json
import math
import pathlib
import re
import shlex
import shutil
import subprocess
import sys
from collections.abc import Mapping, Sequence
from typing import Any, NamedTuple, NoReturn


SCRIPT_DIR = pathlib.Path(__file__).resolve().parent
REPO_ROOT = SCRIPT_DIR.parents[1]
TESTS_PATH = SCRIPT_DIR / "tests.json"
DEFAULT_RUNS_DIR = SCRIPT_DIR / "runs"
README_NAME = "README.md"
BINARY_LABEL_RE = re.compile(r"^[A-Za-z0-9_.-]+$")
EMOJI_THRESHOLD_PCT = 3.0
METRICS = ("mean", "stddev", "median", "min", "max")
COMPARE_TIMING_METRIC = "median"
HYPERFINE_SUFFIX = ".hyperfine.json"
WAVEPEEK_SUFFIX = ".wavepeek.json"
FAILURE_SUFFIX = ".failure.json"
FAILURE_KIND = "wavepeek-e2e-bench-failure"
FAILURE_SUMMARY_LIMIT = 4096
FUNCTIONAL_MATCH_MARKER = ""
FUNCTIONAL_MISMATCH_MARKER = "⚠️"
FUNCTIONAL_MISSING_MARKER = "?"
FUNCTIONAL_TIMEOUT_MARKER = "⏱T"
DEFAULT_WAVEPEEK_TIMEOUT_SECONDS = 300
DIAGNOSTIC_CODE_RE = re.compile(r"^WPK-[WE][0-9]{4}$")


class BinarySpec(NamedTuple):
    label: str
    path: str


class RunOutcome(NamedTuple):
    status: str
    test_name: str
    failure: dict[str, Any] | None
    errors: list[str]


def fail(message: str) -> NoReturn:
    raise SystemExit(message)


def as_int(value: Any, field: str) -> int:
    try:
        return int(value)
    except (TypeError, ValueError):
        fail(f"error: tests: `{field}` must be integer")


def require_nonempty_str(value: Any, field: str) -> str:
    if not isinstance(value, str) or not value:
        fail(f"error: tests: `{field}` must be non-empty string")
    return value


def require_object(value: Any, field: str) -> dict[str, Any]:
    if not isinstance(value, dict):
        fail(f"error: tests: `{field}` must be object")
    return dict(value)


def require_nonempty_list(value: Any, field: str) -> list[Any]:
    if not isinstance(value, list) or not value:
        fail(f"error: tests: `{field}` must be a non-empty list")
    return list(value)


def normalize_path(path_value: str) -> pathlib.Path:
    return pathlib.Path(path_value).expanduser().resolve()


def ensure_existing_dir(path: pathlib.Path, label: str) -> None:
    if not path.exists() or not path.is_dir():
        fail(f"error: {label}: directory does not exist: {path}")


def load_tests(tests_path: pathlib.Path) -> list[dict[str, Any]]:
    if not tests_path.exists():
        fail(f"error: tests: missing file {tests_path}")

    try:
        payload = json.loads(tests_path.read_text(encoding="utf-8"))
    except json.JSONDecodeError as error:
        fail(f"error: tests: invalid JSON in {tests_path}: {error}")

    if not isinstance(payload, dict):
        fail(f"error: tests: root of {tests_path} must be object")

    tests = require_nonempty_list(payload.get("tests"), "tests")

    validated: list[dict[str, Any]] = []
    seen: set[str] = set()

    for index, raw in enumerate(tests):
        if not isinstance(raw, dict):
            fail(f"error: tests: `tests[{index}]` must be object")

        name = require_nonempty_str(raw.get("name"), f"tests[{index}].name")
        if name in seen:
            fail(f"error: tests: duplicate test name `{name}`")
        category = require_nonempty_str(raw.get("category"), f"tests[{index}].category")
        runs = raw.get("runs")
        warmup = raw.get("warmup")
        command = require_nonempty_list(raw.get("command"), f"tests[{index}].command")
        meta = require_object(raw.get("meta", {}), f"tests[{index}].meta")

        runs_value = as_int(runs, f"tests[{index}].runs")
        warmup_value = as_int(warmup, f"tests[{index}].warmup")
        if runs_value < 1:
            fail(f"error: tests: `{name}` has runs < 1")
        if warmup_value < 0:
            fail(f"error: tests: `{name}` has warmup < 0")

        command_tokens: list[str] = []
        for token_index, token in enumerate(command):
            if not isinstance(token, str) or not token:
                fail(
                    "error: tests: "
                    f"`tests[{index}].command[{token_index}]` must be non-empty string"
                )
            command_tokens.append(token)

        validated.append(
            {
                "name": name,
                "category": category,
                "runs": runs_value,
                "warmup": warmup_value,
                "command": command_tokens,
                "meta": meta,
            }
        )
        seen.add(name)

    return sorted(validated, key=lambda test: str(test["name"]))


def select_tests(tests: list[dict[str, Any]], pattern: str | None) -> list[dict[str, Any]]:
    if pattern is None:
        return list(tests)
    try:
        regex = re.compile(pattern)
    except re.error as error:
        fail(f"error: filter: invalid regex {pattern!r}: {error}")
    return [test for test in tests if regex.search(str(test["name"]))]


def resolve_run_dir(run_dir_arg: str | None, out_dir_arg: str) -> pathlib.Path:
    if run_dir_arg:
        run_dir = normalize_path(run_dir_arg)
        run_dir.mkdir(parents=True, exist_ok=True)
        return run_dir

    out_dir = normalize_path(out_dir_arg)
    out_dir.mkdir(parents=True, exist_ok=True)

    stamp = datetime.now(timezone.utc).strftime("%Y-%m-%d_%H-%M-%SZ")
    for attempt in range(1000):
        suffix = "" if attempt == 0 else f"-{attempt}"
        candidate = out_dir / f"{stamp}{suffix}"
        try:
            candidate.mkdir(parents=True, exist_ok=False)
            return candidate
        except FileExistsError:
            continue

    fail(f"error: run: failed to create run directory in {out_dir}")
    raise AssertionError("unreachable")


def parse_binary_specs(values: list[str] | None) -> list[BinarySpec]:
    if not values:
        fail("error: run: at least one --binary label=path argument is required")

    specs: list[BinarySpec] = []
    seen: set[str] = set()
    for raw in values:
        if "=" not in raw:
            fail(f"error: run: --binary must use label=path form: {raw}")
        label, path_text = raw.split("=", 1)
        if not label or not BINARY_LABEL_RE.fullmatch(label):
            fail(
                "error: run: --binary label must contain only letters, digits, dot, dash, or underscore"
            )
        if label in {".", "..", README_NAME, "manifest.json"}:
            fail(f"error: run: --binary label `{label}` is reserved")
        if label in seen:
            fail(f"error: run: duplicate --binary label `{label}`")
        path = normalize_path(path_text)
        if not path.exists() or not path.is_file():
            fail(f"error: run: --binary `{label}` points to missing file: {path}")
        specs.append(BinarySpec(label=label, path=str(path)))
        seen.add(label)
    return specs


def ensure_hyperfine() -> None:
    if shutil.which("hyperfine") is None:
        fail("error: run: `hyperfine` is not available in PATH")


def resolve_test_command(test: dict[str, Any], wavepeek_bin: str) -> list[str]:
    command_tokens: list[str] = []
    for token in test["command"]:
        try:
            command_tokens.append(str(token).format(wavepeek_bin=wavepeek_bin))
        except KeyError as error:
            fail(f"error: tests: missing placeholder {error!s} in test `{test['name']}`")
    return command_tokens


def build_functional_command(command_args: list[str]) -> list[str]:
    if "--json" in command_args:
        return list(command_args)
    return [*command_args, "--json"]


def build_timed_benchmark_command(
    command_args: list[str],
    timeout_seconds: int,
    test_name: str,
) -> str:
    wrapper = SCRIPT_DIR / "wavepeek_timeout_wrapper.py"
    wrapper_args = [
        sys.executable,
        str(wrapper),
        "--timeout-seconds",
        str(timeout_seconds),
        "--label",
        test_name,
        "--",
        *command_args,
    ]
    return shlex.join(wrapper_args)


def is_timeout_functional_payload(payload: Any) -> bool:
    return isinstance(payload, dict) and len(payload) == 0


def artifact_test_name(path: pathlib.Path, suffix: str) -> str:
    if not path.name.endswith(suffix):
        fail(f"error: report: artifact has unexpected suffix in {path}")
    test_name = path.name[: -len(suffix)]
    if not test_name:
        fail(f"error: report: artifact has empty test name: {path}")
    return test_name


def hyperfine_result_path(run_dir: pathlib.Path, test_name: str) -> pathlib.Path:
    return run_dir / f"{test_name}{HYPERFINE_SUFFIX}"


def wavepeek_result_path(run_dir: pathlib.Path, test_name: str) -> pathlib.Path:
    return run_dir / f"{test_name}{WAVEPEEK_SUFFIX}"


def failure_result_path(run_dir: pathlib.Path, test_name: str) -> pathlib.Path:
    return run_dir / f"{test_name}{FAILURE_SUFFIX}"


def test_has_complete_artifacts(run_dir: pathlib.Path, test_name: str) -> bool:
    return hyperfine_result_path(run_dir, test_name).is_file() and wavepeek_result_path(
        run_dir, test_name
    ).is_file()


def test_has_terminal_artifact(run_dir: pathlib.Path, test_name: str) -> bool:
    return test_has_complete_artifacts(run_dir, test_name) or failure_result_path(
        run_dir,
        test_name,
    ).is_file()


def remove_test_artifacts(run_dir: pathlib.Path, test_name: str) -> None:
    for path in (
        hyperfine_result_path(run_dir, test_name),
        wavepeek_result_path(run_dir, test_name),
        failure_result_path(run_dir, test_name),
    ):
        try:
            path.unlink()
        except FileNotFoundError:
            pass


def binary_run_dir(run_dir: pathlib.Path, binary: BinarySpec) -> pathlib.Path:
    return run_dir / binary.label


def write_run_manifest(
    *,
    run_dir: pathlib.Path,
    tests_path: pathlib.Path,
    binaries: list[BinarySpec],
    selected_tests: list[dict[str, Any]],
    schedule: str,
    timeout_seconds: int,
) -> None:
    binary_entries: list[dict[str, Any]] = []
    for binary in binaries:
        label_dir = binary_run_dir(run_dir, binary)
        hyperfine_count = len(list(label_dir.glob(f"*{HYPERFINE_SUFFIX}")))
        wavepeek_count = len(list(label_dir.glob(f"*{WAVEPEEK_SUFFIX}")))
        failure_results = load_failure_results(label_dir) if label_dir.is_dir() else {}
        failures_by_phase: dict[str, int] = {}
        for failure in failure_results.values():
            phase = str(failure.get("phase", "unknown"))
            failures_by_phase[phase] = failures_by_phase.get(phase, 0) + 1
        binary_entries.append(
            {
                "label": binary.label,
                "path": binary.path,
                "run_dir": binary.label,
                "hyperfine_json_count": hyperfine_count,
                "wavepeek_json_count": wavepeek_count,
                "failure_count": len(failure_results),
                "failures_by_phase": failures_by_phase,
            }
        )

    payload = {
        "kind": "wavepeek-e2e-bench-run",
        "schema_version": 1,
        "generated_at_utc": datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ"),
        "tests_path": str(tests_path),
        "schedule": schedule,
        "timeout_seconds": timeout_seconds,
        "test_count": len(selected_tests),
        "binaries": binary_entries,
    }
    (run_dir / "manifest.json").write_text(
        json.dumps(payload, ensure_ascii=True, indent=2, sort_keys=True) + "\n",
        encoding="utf-8",
    )


def write_run_index(run_dir: pathlib.Path, binaries: list[BinarySpec]) -> pathlib.Path:
    lines = [f"# CLI E2E Bench Run: {run_dir.name}", "", "## Binaries", ""]
    for binary in binaries:
        lines.append(f"- `{binary.label}`: `{binary.path}` (`{binary.label}/`)" )
    lines.append("")
    index_path = run_dir / README_NAME
    index_path.write_text("\n".join(lines), encoding="utf-8")
    return index_path


def partition_missing_only_tests(
    selected: list[dict[str, Any]],
    run_dir: pathlib.Path,
) -> tuple[list[dict[str, Any]], list[str]]:
    runnable: list[dict[str, Any]] = []
    skipped: list[str] = []
    for test in selected:
        test_name = str(test["name"])
        if test_has_terminal_artifact(run_dir, test_name):
            skipped.append(test_name)
        else:
            runnable.append(test)
    return runnable, skipped


def summarize_process_text(value: Any) -> str:
    if value is None:
        return ""
    if isinstance(value, bytes):
        text = value.decode("utf-8", errors="replace")
    else:
        text = str(value)
    if len(text) <= FAILURE_SUMMARY_LIMIT:
        return text
    return text[:FAILURE_SUMMARY_LIMIT] + "\n... truncated ..."


def make_failure_artifact(
    *,
    test_name: str,
    phase: str,
    command: Sequence[str],
    exit_code: int | None,
    timed_out: bool = False,
    stdout: Any = None,
    stderr: Any = None,
    message: str | None = None,
    binary_label: str | None = None,
) -> dict[str, Any]:
    payload: dict[str, Any] = {
        "kind": FAILURE_KIND,
        "schema_version": 1,
        "generated_at_utc": datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ"),
        "test_name": test_name,
        "phase": phase,
        "command": [str(arg) for arg in command],
        "exit_code": exit_code,
        "timed_out": timed_out,
        "stdout_summary": summarize_process_text(stdout),
        "stderr_summary": summarize_process_text(stderr),
    }
    if message:
        payload["message"] = message
    if binary_label:
        payload["binary_label"] = binary_label
    return payload


def write_failure_artifact(run_dir: pathlib.Path, test_name: str, payload: dict[str, Any]) -> None:
    output_path = failure_result_path(run_dir, test_name)
    output_path.write_text(
        json.dumps(payload, ensure_ascii=True, indent=2, sort_keys=True) + "\n",
        encoding="utf-8",
    )


def parse_failure_result_file(path: pathlib.Path) -> dict[str, Any]:
    try:
        payload = json.loads(path.read_text(encoding="utf-8"))
    except json.JSONDecodeError as error:
        fail(f"error: report: invalid JSON in {path}: {error}")

    if not isinstance(payload, dict):
        fail(f"error: report: expected object in {path}")
    if payload.get("kind") != FAILURE_KIND:
        fail(f"error: report: unexpected failure artifact kind in {path}")
    artifact_name = artifact_test_name(path, FAILURE_SUFFIX)
    test_name = payload.get("test_name")
    if test_name != artifact_name:
        fail(f"error: report: failure artifact test name mismatch in {path}")
    if payload.get("phase") not in {"preflight", "benchmark"}:
        fail(f"error: report: invalid failure artifact phase in {path}")
    if not isinstance(payload.get("command"), list):
        fail(f"error: report: failure artifact command must be list in {path}")
    return dict(payload)


def load_failure_results(run_dir: pathlib.Path) -> dict[str, dict[str, Any]]:
    result_map: dict[str, dict[str, Any]] = {}
    for path in sorted(run_dir.glob(f"*{FAILURE_SUFFIX}")):
        parsed = parse_failure_result_file(path)
        result_map[str(parsed["test_name"])] = parsed
    return result_map


def run_test(
    test: dict[str, Any],
    run_dir: pathlib.Path,
    wavepeek_bin: str,
    timeout_seconds: int,
    verbose: bool,
) -> dict[str, Any] | None:
    command_args = resolve_test_command(test, wavepeek_bin)
    benchmark_command = build_timed_benchmark_command(
        command_args,
        timeout_seconds,
        str(test["name"]),
    )
    output_path = hyperfine_result_path(run_dir, str(test["name"]))

    hyperfine_cmd = [
        "hyperfine",
        "-N",
        "--style",
        "basic",
        "--warmup",
        str(test["warmup"]),
        "--runs",
        str(test["runs"]),
        "--command-name",
        str(test["name"]),
        "--export-json",
        str(output_path),
        benchmark_command,
    ]
    if verbose:
        result = subprocess.run(hyperfine_cmd, check=False, cwd=REPO_ROOT)
    else:
        result = subprocess.run(
            hyperfine_cmd,
            check=False,
            cwd=REPO_ROOT,
            capture_output=True,
            text=True,
        )
    if result.returncode != 0:
        try:
            output_path.unlink()
        except FileNotFoundError:
            pass
        message = f"hyperfine failed for `{test['name']}`"
        return make_failure_artifact(
            test_name=str(test["name"]),
            phase="benchmark",
            command=hyperfine_cmd,
            exit_code=result.returncode,
            stdout=getattr(result, "stdout", None),
            stderr=getattr(result, "stderr", None),
            message=message,
        )
    return None


def validate_functional_diagnostic(diagnostic: Any, source: str, index: int) -> None:
    label = f"{source} field `diagnostics[{index}]`"
    if not isinstance(diagnostic, dict):
        raise ValueError(f"{label} must be object")
    extra = sorted(set(diagnostic) - {"kind", "code", "message"})
    if extra:
        raise ValueError(f"{label} has unexpected key `{extra[0]}`")
    kind = diagnostic.get("kind")
    message = diagnostic.get("message")
    code = diagnostic.get("code")
    if kind not in {"info", "warning", "error"}:
        raise ValueError(f"{label} field `kind` must be info, warning, or error")
    if not isinstance(message, str):
        raise ValueError(f"{label} field `message` must be string")
    if kind == "info":
        if "code" in diagnostic:
            raise ValueError(f"{label} field `code` must be omitted for info")
        return
    if not isinstance(code, str):
        raise ValueError(f"{label} field `code` must be string")
    if not DIAGNOSTIC_CODE_RE.fullmatch(code):
        raise ValueError(f"{label} field `code` must match WPK-[WE]####")
    expected_prefix = "WPK-W" if kind == "warning" else "WPK-E"
    if not code.startswith(expected_prefix):
        raise ValueError(f"{label} field `code` must use {expected_prefix} for {kind}")


def validate_functional_payload(payload: Any, source: str) -> dict[str, Any]:
    if not isinstance(payload, dict):
        raise ValueError(f"{source} must be object")
    if "warnings" in payload:
        raise ValueError(f"{source} must not contain legacy key `warnings`")
    if "data" not in payload:
        raise ValueError(f"{source} missing key `data`")
    if "diagnostics" not in payload:
        raise ValueError(f"{source} missing key `diagnostics`")
    if not isinstance(payload["data"], dict) and not isinstance(payload["data"], list):
        raise ValueError(f"{source} field `data` must be object or list")
    if not isinstance(payload["diagnostics"], list):
        raise ValueError(f"{source} field `diagnostics` must be list")
    for index, diagnostic in enumerate(payload["diagnostics"]):
        validate_functional_diagnostic(diagnostic, source, index)
    return {"data": payload["data"], "diagnostics": payload["diagnostics"]}


def run_functional_capture(
    test: dict[str, Any],
    wavepeek_bin: str,
    caller: str,
    timeout_seconds: int,
    *,
    fail_on_error: bool = True,
) -> dict[str, Any] | tuple[dict[str, Any] | None, dict[str, Any] | None]:
    test_name = str(test["name"])
    command_args = build_functional_command(resolve_test_command(test, wavepeek_bin))
    try:
        result = subprocess.run(
            command_args,
            check=False,
            cwd=REPO_ROOT,
            capture_output=True,
            text=True,
            timeout=timeout_seconds,
        )
    except subprocess.TimeoutExpired as error:
        failure = make_failure_artifact(
            test_name=test_name,
            phase="preflight",
            command=command_args,
            exit_code=None,
            timed_out=True,
            stdout=getattr(error, "stdout", None),
            stderr=getattr(error, "stderr", None),
            message=f"functional capture timed out after {timeout_seconds}s",
        )
        if fail_on_error:
            raise
        return None, failure

    if result.returncode != 0:
        details = result.stderr.strip()
        suffix = f": {details}" if details else ""
        message = (
            f"functional capture failed for `{test_name}` "
            f"(exit {result.returncode}){suffix}"
        )
        failure = make_failure_artifact(
            test_name=test_name,
            phase="preflight",
            command=command_args,
            exit_code=result.returncode,
            stdout=result.stdout,
            stderr=result.stderr,
            message=message,
        )
        if fail_on_error:
            fail(f"error: {caller}: {message}")
        return None, failure

    try:
        payload = json.loads(result.stdout)
    except json.JSONDecodeError as error:
        message = f"invalid JSON output for `{test_name}`: {error}"
        failure = make_failure_artifact(
            test_name=test_name,
            phase="preflight",
            command=command_args,
            exit_code=result.returncode,
            stdout=result.stdout,
            stderr=result.stderr,
            message=message,
        )
        if fail_on_error:
            fail(f"error: {caller}: {message}")
        return None, failure

    try:
        validated = validate_functional_payload(payload, f"functional output for `{test_name}`")
    except ValueError as error:
        failure = make_failure_artifact(
            test_name=test_name,
            phase="preflight",
            command=command_args,
            exit_code=result.returncode,
            stdout=result.stdout,
            stderr=result.stderr,
            message=str(error),
        )
        if fail_on_error:
            fail(f"error: {caller}: {error}")
        return None, failure

    if fail_on_error:
        return validated
    return validated, None


def write_wavepeek_artifact(run_dir: pathlib.Path, test_name: str, payload: dict[str, Any]) -> None:
    output_path = wavepeek_result_path(run_dir, test_name)
    output_path.write_text(
        json.dumps(payload, ensure_ascii=True, indent=2, sort_keys=True) + "\n",
        encoding="utf-8",
    )


def write_wavepeek_timeout_artifact(run_dir: pathlib.Path, test_name: str) -> None:
    output_path = wavepeek_result_path(run_dir, test_name)
    output_path.write_text("{}\n", encoding="utf-8")


def parse_hyperfine_result_file(path: pathlib.Path) -> dict[str, Any]:
    try:
        payload = json.loads(path.read_text(encoding="utf-8"))
    except json.JSONDecodeError as error:
        fail(f"error: report: invalid JSON in {path}: {error}")

    if not isinstance(payload, dict):
        fail(f"error: report: expected object in {path}")

    results = payload.get("results")
    if not isinstance(results, list) or not results or not isinstance(results[0], dict):
        fail(f"error: report: missing `results[0]` object in {path}")
    first = dict(results[0])

    row: dict[str, Any] = {
        "test_name": artifact_test_name(path, HYPERFINE_SUFFIX),
        "command": str(first.get("command", "")),
    }
    for metric in METRICS:
        raw = first.get(metric)
        if raw is None and metric == "stddev":
            row[metric] = 0.0
            continue
        if raw is None:
            fail(f"error: report: missing metric `{metric}` in {path}")
        try:
            row[metric] = float(raw)
        except (TypeError, ValueError):
            fail(f"error: report: invalid metric `{metric}` in {path}")
    return row


def parse_wavepeek_result_file(path: pathlib.Path) -> dict[str, Any]:
    try:
        payload = json.loads(path.read_text(encoding="utf-8"))
    except json.JSONDecodeError as error:
        fail(f"error: report: invalid JSON in {path}: {error}")

    if is_timeout_functional_payload(payload):
        return {}

    try:
        return validate_functional_payload(payload, f"functional artifact `{path}`")
    except ValueError as error:
        fail(f"error: report: {error}")
    raise AssertionError("unreachable")


def load_hyperfine_results(
    run_dir: pathlib.Path,
    test_names: Iterable[str] | None = None,
) -> dict[str, dict[str, Any]]:
    result_map: dict[str, dict[str, Any]] = {}
    if test_names is None:
        paths = sorted(run_dir.glob(f"*{HYPERFINE_SUFFIX}"))
    else:
        paths = [hyperfine_result_path(run_dir, name) for name in sorted(test_names)]
    for path in paths:
        parsed = parse_hyperfine_result_file(path)
        result_map[str(parsed["test_name"])] = parsed
    return result_map


def load_wavepeek_results(run_dir: pathlib.Path) -> dict[str, dict[str, Any]]:
    result_map: dict[str, dict[str, Any]] = {}
    for path in sorted(run_dir.glob(f"*{WAVEPEEK_SUFFIX}")):
        test_name = artifact_test_name(path, WAVEPEEK_SUFFIX)
        result_map[test_name] = parse_wavepeek_result_file(path)
    return result_map


def load_wavepeek_artifact_for_compare(
    run_dir: pathlib.Path,
    test_name: str,
    label: str,
) -> tuple[dict[str, Any] | None, str | None]:
    path = wavepeek_result_path(run_dir, test_name)
    if not path.exists() or not path.is_file():
        return None, f"{label}: missing artifact `{path}`"

    try:
        payload = json.loads(path.read_text(encoding="utf-8"))
    except json.JSONDecodeError as error:
        return None, f"{label}: invalid JSON in `{path}`: {error}"
    except OSError as error:
        return None, f"{label}: failed to read `{path}`: {error}"

    if is_timeout_functional_payload(payload):
        return {}, None

    try:
        normalized = validate_functional_payload(payload, f"{label} artifact `{path}`")
    except ValueError as error:
        return None, str(error)
    return normalized, None


def delta_pct(revised: float, golden: float) -> float | None:
    if golden == 0:
        return None
    return ((golden - revised) / golden) * 100.0


def allowed_slowdown(golden_time: float, threshold_pct: float, threshold_seconds: float) -> float:
    return max(golden_time * threshold_pct / 100.0, threshold_seconds)


def timing_record(
    *,
    test_name: str,
    metric: str,
    revised_time: float,
    golden_time: float,
    threshold_pct: float,
    threshold_seconds: float,
) -> dict[str, Any]:
    actual_slowdown = revised_time - golden_time
    return {
        "test_name": test_name,
        "metric": metric,
        "revised_seconds": revised_time,
        "golden_seconds": golden_time,
        "delta_pct": delta_pct(revised_time, golden_time),
        "slowdown_seconds": actual_slowdown,
        "allowed_slowdown_seconds": allowed_slowdown(
            golden_time,
            threshold_pct,
            threshold_seconds,
        ),
        "speed": format_speed_factor(revised_time, golden_time),
    }


def format_timing_record(record: dict[str, Any]) -> str:
    delta = record.get("delta_pct")
    delta_text = "n/a" if delta is None else f"{float(delta):+.2f}%"
    return (
        f"{record['test_name']}: {record['metric']} "
        f"revised={float(record['revised_seconds']):.6f}s, "
        f"golden={float(record['golden_seconds']):.6f}s, "
        f"delta={delta_text}, "
        f"slowdown={float(record['slowdown_seconds']):.6f}s, "
        f"allowed={float(record['allowed_slowdown_seconds']):.6f}s, "
        f"speed={record['speed']}"
    )


def write_result_json(path_arg: str | None, payload: dict[str, Any]) -> None:
    if not path_arg:
        return
    path = normalize_path(path_arg)
    path.parent.mkdir(parents=True, exist_ok=True)
    path.write_text(
        json.dumps(payload, ensure_ascii=True, indent=2, sort_keys=True) + "\n",
        encoding="utf-8",
    )


def parse_ignored_functional_tests(values: Sequence[str] | None) -> dict[str, str]:
    ignored: dict[str, str] = {}
    for raw in values or []:
        if "=" not in raw:
            fail(
                "error: compare: --ignore-functional-test must use "
                "NAME=REASON"
            )
        name, reason = raw.split("=", 1)
        name = name.strip()
        reason = reason.strip()
        if not name or not reason:
            fail(
                "error: compare: --ignore-functional-test requires non-empty "
                "NAME and REASON"
            )
        if name in ignored:
            fail(f"error: compare: duplicate ignored functional test `{name}`")
        ignored[name] = reason
    return ignored


def ignored_functional_test_records(
    ignored: Mapping[str, str],
    revised_names: set[str],
    golden_names: set[str],
) -> list[dict[str, Any]]:
    return [
        {
            "test_name": name,
            "reason": ignored[name],
            "present_in_revised": name in revised_names,
            "present_in_golden": name in golden_names,
        }
        for name in sorted(ignored)
    ]


def parse_hyperfine_times_file(path: pathlib.Path, caller: str) -> list[float]:
    try:
        payload = json.loads(path.read_text(encoding="utf-8"))
    except FileNotFoundError:
        fail(f"error: {caller}: missing hyperfine JSON file: {path}")
    except json.JSONDecodeError as error:
        fail(f"error: {caller}: invalid JSON in {path}: {error}")

    if not isinstance(payload, dict):
        fail(f"error: {caller}: expected object in {path}")
    results = payload.get("results")
    if not isinstance(results, list) or not results or not isinstance(results[0], dict):
        fail(f"error: {caller}: missing `results[0]` object in {path}")
    raw_times = results[0].get("times")
    if not isinstance(raw_times, list) or not raw_times:
        fail(f"error: {caller}: missing non-empty `results[0].times` array in {path}")

    times: list[float] = []
    for index, raw in enumerate(raw_times):
        try:
            value = float(raw)
        except (TypeError, ValueError):
            fail(f"error: {caller}: invalid time at `results[0].times[{index}]` in {path}")
        if not math.isfinite(value) or value < 0:
            fail(f"error: {caller}: invalid time at `results[0].times[{index}]` in {path}")
        times.append(value)
    return times


def hyperfine_sample_times(run_dir: pathlib.Path, test_name: str, caller: str) -> list[float]:
    return parse_hyperfine_times_file(hyperfine_result_path(run_dir, test_name), caller)


def speed_factor(revised: float, golden: float) -> tuple[float, str]:
    if revised == golden:
        return 1.0, "same"
    if revised < golden:
        if revised == 0:
            return float("inf"), "faster"
        return golden / revised, "faster"
    if golden == 0:
        return float("inf"), "slower"
    return revised / golden, "slower"


def format_speed_factor(revised: float, golden: float) -> str:
    factor, direction = speed_factor(revised, golden)
    factor_text = "infx" if math.isinf(factor) else f"{factor:.2f}x"
    if direction == "same":
        return factor_text
    return f"{factor_text} {direction}"


def format_metric(value: float, baseline: dict[str, Any] | None, metric: str) -> str:
    base = f"{value:.6f}"
    if baseline is None:
        return base

    baseline_value = float(baseline[metric])
    delta = delta_pct(value, baseline_value)
    speed = format_speed_factor(value, baseline_value)
    if delta is None:
        return f"{base} (n/a, {speed})"

    emoji = ""
    if delta >= EMOJI_THRESHOLD_PCT:
        emoji = " 🟢"
    elif delta <= -EMOJI_THRESHOLD_PCT:
        emoji = " 🔴"
    return f"{base} ({delta:+.2f}%, {speed}){emoji}"


def escape_md(value: Any) -> str:
    return str(value).replace("\n", " ").replace("|", "\\|")


def format_meta(meta: dict[str, Any]) -> str:
    if not meta:
        return "-"
    pairs = []
    for key, value in meta.items():
        if isinstance(value, (dict, list)):
            value_text = json.dumps(value, ensure_ascii=True, separators=(",", ":"))
        else:
            value_text = str(value)
        pairs.append(f"{key}={value_text}")
    return " ".join(pairs)


def functional_diff_fields(revised_payload: dict[str, Any], golden_payload: dict[str, Any]) -> list[str]:
    if is_timeout_functional_payload(revised_payload) or is_timeout_functional_payload(
        golden_payload
    ):
        return []
    fields = []
    if revised_payload.get("data") != golden_payload.get("data"):
        fields.append("data")
    if revised_payload.get("diagnostics") != golden_payload.get("diagnostics"):
        fields.append("diagnostics")
    return fields


def is_empty_data(value: Any) -> bool:
    if isinstance(value, list) or isinstance(value, dict):
        return len(value) == 0
    return False


def report_functional_status(
    test_name: str,
    revised_functional: dict[str, dict[str, Any]],
    compare_functional: dict[str, dict[str, Any]],
) -> str:
    revised_payload = revised_functional.get(test_name)
    compare_payload = compare_functional.get(test_name)
    if revised_payload is None or compare_payload is None:
        return FUNCTIONAL_MISSING_MARKER
    if is_timeout_functional_payload(revised_payload) or is_timeout_functional_payload(
        compare_payload
    ):
        return FUNCTIONAL_TIMEOUT_MARKER
    if revised_payload.get("data") != compare_payload.get("data"):
        return f"{FUNCTIONAL_MISMATCH_MARKER}D"
    if revised_payload.get("diagnostics") != compare_payload.get("diagnostics"):
        return f"{FUNCTIONAL_MISMATCH_MARKER}X"
    if is_empty_data(revised_payload.get("data")):
        return f"{FUNCTIONAL_MATCH_MARKER}E"
    return FUNCTIONAL_MATCH_MARKER


def render_report(
    run_dir: pathlib.Path,
    results: dict[str, dict[str, Any]],
    tests_by_name: dict[str, dict[str, Any]],
    compare_results: dict[str, dict[str, Any]],
    compare_dir: pathlib.Path | None,
    functional_results: dict[str, dict[str, Any]],
    compare_functional_results: dict[str, dict[str, Any]],
    failure_results: dict[str, dict[str, Any]] | None = None,
) -> str:
    lines = [
        f"# CLI E2E Bench Run: {run_dir.name}",
        "",
        f"- Generated at (UTC): {datetime.now(timezone.utc).strftime('%Y-%m-%dT%H:%M:%SZ')}",
        f"- Hyperfine JSON files: {len(results)}",
        f"- Wavepeek JSON files: {len(functional_results)}",
        f"- Failure JSON files: {len(failure_results or {})}",
    ]
    if compare_dir is not None:
        lines.extend(
            [
                f"- Compare baseline: `{compare_dir}`",
                "- Delta formula: `((golden - revised) / golden) * 100`",
                "- Speed factor: `golden/revised` when faster, `revised/golden` when slower",
                f"- Emoji threshold: abs(delta) >= {EMOJI_THRESHOLD_PCT:.2f}% (`🟢` faster, `🔴` slower)",
                "- Functional status: `✅` match, `✅E` match with empty data, `⚠️D` data mismatch, `⚠️X` diagnostic mismatch, `⏱T` timeout artifact, `?` missing counterpart",
            ]
        )
    lines.append("")

    failures = failure_results or {}
    if failures:
        lines.extend(["## Explicit Failures", ""])
        lines.extend(["| test | phase | exit | timed out | message |", "| --- | --- | --- | --- | --- |"])
        for failure in sorted(failures.values(), key=lambda item: str(item.get("test_name", ""))):
            exit_code = failure.get("exit_code")
            lines.append(
                "| "
                + " | ".join(
                    [
                        escape_md(failure.get("test_name", "")),
                        escape_md(failure.get("phase", "")),
                        escape_md("-" if exit_code is None else exit_code),
                        escape_md(str(bool(failure.get("timed_out", False))).lower()),
                        escape_md(failure.get("message", "")),
                    ]
                )
                + " |"
            )
        lines.append("")

    if not results:
        lines.append("No hyperfine JSON artifacts found in this run directory.")
        lines.append("")
        return "\n".join(lines)

    grouped: dict[str, list[dict[str, Any]]] = {}
    for row in results.values():
        test = tests_by_name.get(str(row["test_name"]))
        category = str(test["category"]) if test is not None else "unknown"
        grouped.setdefault(category, []).append(row)

    for category in sorted(grouped):
        lines.extend([f"## {category}", ""])
        if compare_dir is not None:
            lines.extend(
                [
                    "| test | mean_s | functional | meta |",
                    "| --- | --- | --- | --- |",
                ]
            )
        else:
            lines.extend(
                [
                    "| test | mean_s | meta |",
                    "| --- | --- | --- |",
                ]
            )

        for row in sorted(
            grouped[category],
            key=lambda item: (-float(item["mean"]), str(item["test_name"])),
        ):
            test_name = str(row["test_name"])
            test = tests_by_name.get(test_name)
            baseline = compare_results.get(test_name)
            line = [
                escape_md(test_name),
                escape_md(format_metric(float(row["mean"]), baseline, "mean")),
            ]
            if compare_dir is not None:
                status = report_functional_status(
                    test_name,
                    functional_results,
                    compare_functional_results,
                )
                line.append(escape_md(status))
            line.append(escape_md(format_meta(dict(test.get("meta", {})) if test is not None else {})))
            lines.append("| " + " | ".join(line) + " |")
        lines.append("")

    return "\n".join(lines)


def write_report(
    run_dir: pathlib.Path,
    tests_by_name: dict[str, dict[str, Any]],
    compare_dir: pathlib.Path | None,
) -> pathlib.Path:
    results = load_hyperfine_results(run_dir)
    compare_results = load_hyperfine_results(compare_dir) if compare_dir is not None else {}
    functional_results = load_wavepeek_results(run_dir)
    compare_functional_results = (
        load_wavepeek_results(compare_dir) if compare_dir is not None else {}
    )
    failure_results = load_failure_results(run_dir)
    markdown = render_report(
        run_dir,
        results,
        tests_by_name,
        compare_results,
        compare_dir,
        functional_results,
        compare_functional_results,
        failure_results,
    )
    report_path = run_dir / README_NAME
    report_path.write_text(markdown, encoding="utf-8")
    return report_path


def preview_command(test: dict[str, Any]) -> str:
    try:
        parts = [str(token).format(wavepeek_bin="<binary>") for token in test["command"]]
    except KeyError as error:
        fail(f"error: tests: missing placeholder {error!s} in test `{test['name']}`")
    return shlex.join(parts)


def cmd_list(args: argparse.Namespace) -> int:
    tests_path = normalize_path(getattr(args, "tests", str(TESTS_PATH)))
    tests = select_tests(load_tests(tests_path), args.filter)
    for test in tests:
        print(str(test["name"]))
    return 0


def cmd_run(args: argparse.Namespace) -> int:
    tests_path = normalize_path(getattr(args, "tests", str(TESTS_PATH)))
    tests = load_tests(tests_path)
    selected = select_tests(tests, args.filter)
    if not selected:
        fail("error: run: no tests matched the provided filter")
    verbose = bool(getattr(args, "verbose", False))
    fail_fast = bool(getattr(args, "fail_fast", False))

    timeout_seconds = int(args.wavepeek_timeout_seconds)
    if timeout_seconds < 1:
        fail("error: run: --wavepeek-timeout-seconds must be >= 1")

    compare_dir = normalize_path(args.compare) if args.compare else None
    if compare_dir is not None:
        ensure_existing_dir(compare_dir, "run")

    binaries = parse_binary_specs(args.binary)
    schedule = str(args.schedule)
    run_dir = resolve_run_dir(args.run_dir, args.out_dir)
    for binary in binaries:
        binary_run_dir(run_dir, binary).mkdir(parents=True, exist_ok=True)
    if verbose:
        print(f"info: run directory: {run_dir}")
        print("info: binaries: " + ", ".join(f"{b.label}={b.path}" for b in binaries))
        print(f"info: schedule: {schedule}")

    tests_by_name = {str(test["name"]): test for test in tests}
    runnable_by_label: dict[str, list[dict[str, Any]]] = {}
    if args.missing_only:
        for binary in binaries:
            runnable, skipped = partition_missing_only_tests(
                selected,
                binary_run_dir(run_dir, binary),
            )
            runnable_by_label[binary.label] = runnable
            if verbose:
                for test_name in skipped:
                    print(
                        f"info: skip `{test_name}` for `{binary.label}` "
                        "(missing-only: artifacts already exist)"
                    )
    else:
        runnable_by_label = {binary.label: list(selected) for binary in binaries}

    total_jobs = sum(len(jobs) for jobs in runnable_by_label.values())
    if total_jobs:
        ensure_hyperfine()
        completed = 0

        def run_one(binary: BinarySpec, test: dict[str, Any]) -> None:
            nonlocal completed
            completed += 1
            label_dir = binary_run_dir(run_dir, binary)
            if verbose:
                print(
                    f"[{completed}/{total_jobs}] {binary.label}/{test['name']} "
                    f"(runs={test['runs']}, warmup={test['warmup']})"
                )
            test_name = str(test["name"])
            remove_test_artifacts(label_dir, test_name)
            functional_result = run_functional_capture(
                test,
                binary.path,
                "run",
                timeout_seconds,
                fail_on_error=False,
            )
            if isinstance(functional_result, tuple):
                functional_payload, functional_failure = functional_result
            else:
                functional_payload = functional_result
                functional_failure = None
            if functional_failure is not None:
                functional_failure["binary_label"] = binary.label
                write_failure_artifact(label_dir, test_name, functional_failure)
                if fail_fast:
                    fail(
                        f"error: run: preflight failed for "
                        f"`{binary.label}/{test_name}`; see {failure_result_path(label_dir, test_name)}"
                    )
                if verbose:
                    print(
                        f"warning: run: preflight failed for "
                        f"`{binary.label}/{test_name}`; wrote failure artifact"
                    )
                return

            benchmark_failure = run_test(test, label_dir, binary.path, timeout_seconds, verbose)
            if isinstance(benchmark_failure, dict):
                benchmark_failure["binary_label"] = binary.label
                write_failure_artifact(label_dir, test_name, benchmark_failure)
                if fail_fast:
                    fail(
                        f"error: run: benchmark failed for "
                        f"`{binary.label}/{test_name}`; see {failure_result_path(label_dir, test_name)}"
                    )
                if verbose:
                    print(
                        f"warning: run: benchmark failed for "
                        f"`{binary.label}/{test_name}`; wrote failure artifact"
                    )
                return
            write_wavepeek_artifact(label_dir, test_name, functional_payload)

        if schedule == "round-robin":
            for test in selected:
                for binary in binaries:
                    if test in runnable_by_label[binary.label]:
                        run_one(binary, test)
        elif schedule == "grouped":
            for binary in binaries:
                for test in runnable_by_label[binary.label]:
                    run_one(binary, test)
        else:
            fail(f"error: run: unsupported schedule `{schedule}`")
    elif verbose:
        print("info: no tests to run after --missing-only filter")

    report_paths: list[pathlib.Path] = []
    for binary in binaries:
        report_paths.append(write_report(binary_run_dir(run_dir, binary), tests_by_name, compare_dir))
    write_run_manifest(
        run_dir=run_dir,
        tests_path=tests_path,
        binaries=binaries,
        selected_tests=selected,
        schedule=schedule,
        timeout_seconds=timeout_seconds,
    )
    index_path = write_run_index(run_dir, binaries)
    if verbose:
        print(f"info: run artifacts written to {run_dir}")
        for report_path in report_paths:
            print(f"info: report updated at {report_path}")
        print(f"info: run index updated at {index_path}")
    else:
        print("ok: run: completed successfully (use --verbose for detailed logs)")
    return 0


def cmd_report(args: argparse.Namespace) -> int:
    run_dir = normalize_path(args.run_dir)
    ensure_existing_dir(run_dir, "report")

    compare_dir = normalize_path(args.compare) if args.compare else None
    if compare_dir is not None:
        ensure_existing_dir(compare_dir, "report")

    tests_path = normalize_path(getattr(args, "tests", str(TESTS_PATH)))
    tests = load_tests(tests_path)
    tests_by_name = {str(test["name"]): test for test in tests}

    manifest_path = run_dir / "manifest.json"
    if manifest_path.is_file():
        try:
            manifest = json.loads(manifest_path.read_text(encoding="utf-8"))
        except json.JSONDecodeError as error:
            fail(f"error: report: invalid JSON in {manifest_path}: {error}")
        if isinstance(manifest, dict) and manifest.get("kind") == "wavepeek-e2e-bench-run":
            binaries = manifest.get("binaries")
            if not isinstance(binaries, list) or not binaries:
                fail(f"error: report: invalid labeled run manifest in {manifest_path}")
            specs: list[BinarySpec] = []
            for item in binaries:
                if not isinstance(item, dict) or not isinstance(item.get("label"), str):
                    fail(f"error: report: invalid binary entry in {manifest_path}")
                label = str(item["label"])
                path = str(item.get("path", ""))
                label_dir = run_dir / label
                ensure_existing_dir(label_dir, "report")
                report_path = write_report(label_dir, tests_by_name, compare_dir)
                print(f"info: report updated at {report_path}")
                specs.append(BinarySpec(label, path))
            index_path = write_run_index(run_dir, specs)
            print(f"info: run index updated at {index_path}")
            return 0

    report_path = write_report(run_dir, tests_by_name, compare_dir)
    print(f"info: report updated at {report_path}")
    return 0


def wavepeek_artifact_names(run_dir: pathlib.Path) -> set[str]:
    return {
        artifact_test_name(path, WAVEPEEK_SUFFIX)
        for path in sorted(run_dir.glob(f"*{WAVEPEEK_SUFFIX}"))
    }


def scan_artifact_outcomes(run_dir: pathlib.Path, *, functional_only: bool) -> dict[str, RunOutcome]:
    hyperfine_names = {
        artifact_test_name(path, HYPERFINE_SUFFIX)
        for path in sorted(run_dir.glob(f"*{HYPERFINE_SUFFIX}"))
    }
    wavepeek_names = wavepeek_artifact_names(run_dir)
    failure_names = {
        artifact_test_name(path, FAILURE_SUFFIX)
        for path in sorted(run_dir.glob(f"*{FAILURE_SUFFIX}"))
    }
    names = hyperfine_names | wavepeek_names | failure_names
    failures = load_failure_results(run_dir)
    outcomes: dict[str, RunOutcome] = {}
    for name in sorted(names):
        has_hyperfine = name in hyperfine_names
        has_wavepeek = name in wavepeek_names
        has_failure = name in failure_names
        errors: list[str] = []
        if functional_only:
            has_success = has_wavepeek
            if has_hyperfine and not has_wavepeek and not has_failure:
                errors.append(f"{name}: hyperfine artifact without wavepeek or failure artifact")
        else:
            has_success = has_hyperfine and has_wavepeek
            if has_hyperfine != has_wavepeek and not has_failure:
                missing = "wavepeek" if has_hyperfine else "hyperfine"
                errors.append(f"{name}: missing {missing} artifact")
        if has_failure and (has_hyperfine or has_wavepeek):
            errors.append(f"{name}: has normal artifacts and a failure artifact")
        if errors:
            outcomes[name] = RunOutcome("invalid", name, failures.get(name), errors)
        elif has_failure:
            outcomes[name] = RunOutcome("failure", name, failures.get(name), [])
        elif has_success:
            outcomes[name] = RunOutcome("success", name, None, [])
        else:
            outcomes[name] = RunOutcome("missing", name, None, [f"{name}: missing outcome artifacts"])
    return outcomes


def failure_summary_record(
    test_name: str,
    side: str,
    outcome: RunOutcome | None,
) -> dict[str, Any]:
    failure = outcome.failure if outcome is not None and outcome.failure is not None else {}
    return {
        "test_name": test_name,
        "side": side,
        "phase": failure.get("phase"),
        "exit_code": failure.get("exit_code"),
        "timed_out": bool(failure.get("timed_out", False)),
        "message": failure.get("message", ""),
    }


def classify_compare_outcomes(
    revised_dir: pathlib.Path,
    golden_dir: pathlib.Path,
    *,
    functional_only: bool,
    allow_golden_extra: bool = False,
) -> dict[str, Any]:
    revised_outcomes = scan_artifact_outcomes(revised_dir, functional_only=functional_only)
    golden_outcomes = scan_artifact_outcomes(golden_dir, functional_only=functional_only)
    revised_names = set(revised_outcomes)
    golden_names = set(golden_outcomes)
    names = revised_names | golden_names

    integrity_errors: list[str] = []
    baseline_unsupported: list[dict[str, Any]] = []
    revised_failures: list[dict[str, Any]] = []
    both_side_failures: list[dict[str, Any]] = []
    comparable: list[str] = []
    golden_extra: list[str] = []
    revised_only: list[str] = []
    golden_only: list[str] = []

    for name in sorted(names):
        revised = revised_outcomes.get(name)
        golden = golden_outcomes.get(name)
        if revised is not None and revised.status == "invalid":
            integrity_errors.extend(f"revised: {error}" for error in revised.errors)
        if golden is not None and golden.status == "invalid":
            integrity_errors.extend(f"golden: {error}" for error in golden.errors)

        revised_status = revised.status if revised is not None else "missing"
        golden_status = golden.status if golden is not None else "missing"
        if revised_status == "invalid" or golden_status == "invalid":
            continue
        if revised_status == "success" and golden_status == "success":
            comparable.append(name)
            continue
        if revised_status == "success" and golden_status == "failure":
            baseline_unsupported.append(failure_summary_record(name, "golden", golden))
            continue
        if revised_status == "failure" and golden_status == "failure":
            both_side_failures.append(
                {
                    "test_name": name,
                    "revised": failure_summary_record(name, "revised", revised),
                    "golden": failure_summary_record(name, "golden", golden),
                }
            )
            continue
        if revised_status == "failure" and golden_status == "success":
            revised_failures.append(failure_summary_record(name, "revised", revised))
            continue
        if revised_status == "missing" and golden_status != "missing":
            golden_only.append(name)
            if not allow_golden_extra:
                integrity_errors.append(f"{name}: missing outcome in revised run")
            else:
                golden_extra.append(name)
            continue
        if golden_status == "missing" and revised_status != "missing":
            revised_only.append(name)
            integrity_errors.append(f"{name}: missing outcome in golden run")
            continue

    return {
        "revised_outcomes": revised_outcomes,
        "golden_outcomes": golden_outcomes,
        "comparable": comparable,
        "baseline_unsupported": baseline_unsupported,
        "revised_failures": revised_failures,
        "both_side_failures": both_side_failures,
        "integrity_errors": integrity_errors,
        "revised_only": revised_only,
        "golden_only": golden_only,
        "golden_extra": golden_extra,
    }


def compare_functional_only(
    revised_dir: pathlib.Path,
    golden_dir: pathlib.Path,
    allow_golden_extra: bool,
    verbose: bool,
    result_json: str | None = None,
    ignored_tests: Mapping[str, str] | None = None,
) -> int:
    classification = classify_compare_outcomes(
        revised_dir,
        golden_dir,
        functional_only=True,
        allow_golden_extra=allow_golden_extra,
    )
    revised_outcomes: dict[str, RunOutcome] = classification["revised_outcomes"]
    golden_outcomes: dict[str, RunOutcome] = classification["golden_outcomes"]
    raw_revised_names = set(revised_outcomes)
    raw_golden_names = set(golden_outcomes)
    if not raw_revised_names:
        fail(f"error: compare: no wavepeek or failure JSON files found in {revised_dir}")
    if not raw_golden_names:
        fail(f"error: compare: no wavepeek or failure JSON files found in {golden_dir}")

    ignored = dict(ignored_tests or {})
    ignored_names = set(ignored)
    ignored_records = ignored_functional_test_records(
        ignored,
        raw_revised_names,
        raw_golden_names,
    )
    matched = list(classification["comparable"])
    revised_only = list(classification["revised_only"])
    golden_only = list(classification["golden_only"])
    baseline_unsupported = list(classification["baseline_unsupported"])
    revised_failures = list(classification["revised_failures"])
    both_side_failures = list(classification["both_side_failures"])
    integrity_errors = list(classification["integrity_errors"])

    functional_mismatches: list[str] = []
    functional_artifact_errors: list[str] = list(integrity_errors)
    functional_timeouts: list[str] = []

    if not matched:
        functional_artifact_errors.append("no comparable tests between revised and golden")
    if revised_only:
        functional_artifact_errors.append(
            "tests only in revised run: " + ", ".join(revised_only)
        )
    if golden_only and not allow_golden_extra:
        functional_artifact_errors.append(
            "tests only in golden run: " + ", ".join(golden_only)
        )

    for name in sorted(ignored):
        missing_sides: list[str] = []
        if name not in raw_revised_names:
            missing_sides.append("revised")
        if name not in raw_golden_names:
            missing_sides.append("golden")
        if missing_sides:
            functional_artifact_errors.append(
                f"ignored functional test `{name}` missing from "
                f"{', '.join(missing_sides)}"
            )
        elif name not in matched:
            functional_artifact_errors.append(
                f"ignored functional test `{name}` is not comparable on both sides"
            )

    for test_name in matched:
        revised_payload, revised_error = load_wavepeek_artifact_for_compare(
            revised_dir,
            test_name,
            "revised",
        )
        golden_payload, golden_error = load_wavepeek_artifact_for_compare(
            golden_dir,
            test_name,
            "golden",
        )

        if revised_error is not None:
            functional_artifact_errors.append(f"{test_name}: {revised_error}")
        if golden_error is not None:
            functional_artifact_errors.append(f"{test_name}: {golden_error}")
        if revised_payload is None or golden_payload is None:
            continue

        revised_timed_out = is_timeout_functional_payload(revised_payload)
        golden_timed_out = is_timeout_functional_payload(golden_payload)
        if revised_timed_out or golden_timed_out:
            timeout_sides: list[str] = []
            if revised_timed_out:
                timeout_sides.append("revised")
            if golden_timed_out:
                timeout_sides.append("golden")
            functional_timeouts.append(
                f"{test_name}: timeout artifact on {', '.join(timeout_sides)}"
            )
            continue

        diff_fields = functional_diff_fields(revised_payload, golden_payload)
        if test_name in ignored_names:
            diff_fields = [field for field in diff_fields if field != "data"]
        if diff_fields:
            functional_mismatches.append(
                f"{test_name}: mismatched fields {', '.join(diff_fields)}"
            )

    if verbose:
        if ignored_records:
            print("warning: compare: ignored functional tests:", file=sys.stderr)
            for record in ignored_records:
                print(
                    f"  - {record['test_name']}: {record['reason']}",
                    file=sys.stderr,
                )
        if golden_only and allow_golden_extra:
            print(
                "warning: compare: ignored tests only in golden run: "
                + ", ".join(golden_only),
                file=sys.stderr,
            )
        if baseline_unsupported:
            print("warning: compare: skipped tests unsupported by golden:", file=sys.stderr)
            for record in baseline_unsupported:
                print(f"  - {record['test_name']}: {record.get('message', '')}", file=sys.stderr)
        if both_side_failures:
            print("warning: compare: skipped tests failed on both sides:", file=sys.stderr)
            for record in both_side_failures:
                print(f"  - {record['test_name']}", file=sys.stderr)
        if revised_failures:
            print("error: compare: revised failures where golden passed:", file=sys.stderr)
            for record in revised_failures:
                print(f"  - {record['test_name']}: {record.get('message', '')}", file=sys.stderr)
        if functional_timeouts:
            print("error: compare: timeout functional artifacts detected:", file=sys.stderr)
            for issue in functional_timeouts:
                print(f"  - {issue}", file=sys.stderr)
        if functional_mismatches:
            print("error: compare: functional mismatch detected:", file=sys.stderr)
            for issue in functional_mismatches:
                print(f"  - {issue}", file=sys.stderr)
        if functional_artifact_errors:
            print("error: compare: functional artifact errors detected:", file=sys.stderr)
            for issue in functional_artifact_errors:
                print(f"  - {issue}", file=sys.stderr)

    status = (
        "failed"
        if revised_failures or functional_timeouts or functional_mismatches or functional_artifact_errors
        else "passed"
    )
    skipped_uncomparable_count = len(baseline_unsupported) + len(both_side_failures)
    write_result_json(
        result_json,
        {
            "kind": "wavepeek-e2e-compare-result",
            "schema_version": 1,
            "status": status,
            "functional_only": True,
            "allow_golden_extra": allow_golden_extra,
            "matched_count": len(matched),
            "comparable_count": len(matched),
            "skipped_uncomparable_count": skipped_uncomparable_count,
            "failed_uncomparable_count": len(revised_failures),
            "uncomparable_count": skipped_uncomparable_count + len(revised_failures),
            "revised_only": revised_only,
            "golden_only": golden_only,
            "baseline_unsupported": baseline_unsupported,
            "revised_failures": revised_failures,
            "both_side_failures": both_side_failures,
            "integrity_errors": integrity_errors,
            "ignored_functional_tests": ignored_records,
            "functional_timeouts": functional_timeouts,
            "functional_mismatches": functional_mismatches,
            "functional_artifact_errors": functional_artifact_errors,
            "timing_failures": [],
        },
    )

    if status == "failed":
        if not verbose:
            print(
                "error: compare: functional checks failed "
                "(use --verbose for detailed logs)",
                file=sys.stderr,
            )
        return 1

    if verbose:
        print("info: compare: functional checks passed")
    else:
        print("ok: compare: functional checks passed (use --verbose for detailed logs)")
    return 0


def cmd_compare(args: argparse.Namespace) -> int:
    revised_dir = normalize_path(args.revised)
    golden_dir = normalize_path(args.golden)
    ensure_existing_dir(revised_dir, "compare")
    ensure_existing_dir(golden_dir, "compare")

    functional_only = bool(getattr(args, "functional_only", False))
    allow_golden_extra = bool(getattr(args, "allow_golden_extra", False))
    verbose = bool(getattr(args, "verbose", False))

    ignored_tests = parse_ignored_functional_tests(
        getattr(args, "ignore_functional_test", None)
    )
    if allow_golden_extra and not functional_only:
        fail("error: compare: --allow-golden-extra requires --functional-only")
    if ignored_tests and not functional_only:
        fail("error: compare: --ignore-functional-test requires --functional-only")
    result_json = getattr(args, "result_json", None)

    if functional_only:
        return compare_functional_only(
            revised_dir,
            golden_dir,
            allow_golden_extra,
            verbose,
            result_json,
            ignored_tests,
        )

    if args.max_negative_delta_pct is None:
        fail("error: compare: --max-negative-delta-pct is required unless --functional-only is set")
    threshold = float(args.max_negative_delta_pct)
    if threshold < 0:
        fail("error: compare: --max-negative-delta-pct must be non-negative")
    threshold_seconds = float(getattr(args, "max_negative_delta_seconds", 0.0) or 0.0)
    if threshold_seconds < 0:
        fail("error: compare: --max-negative-delta-seconds must be non-negative")

    classification = classify_compare_outcomes(
        revised_dir,
        golden_dir,
        functional_only=False,
        allow_golden_extra=False,
    )
    revised_outcomes: dict[str, RunOutcome] = classification["revised_outcomes"]
    golden_outcomes: dict[str, RunOutcome] = classification["golden_outcomes"]
    if not revised_outcomes:
        fail(f"error: compare: no hyperfine, wavepeek, or failure JSON files found in {revised_dir}")
    if not golden_outcomes:
        fail(f"error: compare: no hyperfine, wavepeek, or failure JSON files found in {golden_dir}")

    matched = list(classification["comparable"])
    revised = load_hyperfine_results(revised_dir, matched)
    golden = load_hyperfine_results(golden_dir, matched)
    revised_only = list(classification["revised_only"])
    golden_only = list(classification["golden_only"])
    baseline_unsupported = list(classification["baseline_unsupported"])
    revised_failures = list(classification["revised_failures"])
    both_side_failures = list(classification["both_side_failures"])
    integrity_errors = list(classification["integrity_errors"])

    timing_failures: list[dict[str, Any]] = []
    functional_mismatches: list[str] = []
    functional_artifact_errors: list[str] = list(integrity_errors)
    functional_timeout_warnings: list[str] = []
    if not matched:
        functional_artifact_errors.append("no comparable tests between revised and golden")

    for test_name in matched:
        revised_row = revised[test_name]
        golden_row = golden[test_name]

        metric = COMPARE_TIMING_METRIC
        revised_time = float(revised_row[metric])
        golden_time = float(golden_row[metric])
        allowed = allowed_slowdown(golden_time, threshold, threshold_seconds)
        actual_slowdown = revised_time - golden_time
        if actual_slowdown > allowed:
            timing_failures.append(
                timing_record(
                    test_name=test_name,
                    metric=metric,
                    revised_time=revised_time,
                    golden_time=golden_time,
                    threshold_pct=threshold,
                    threshold_seconds=threshold_seconds,
                )
            )

        revised_payload, revised_error = load_wavepeek_artifact_for_compare(
            revised_dir,
            test_name,
            "revised",
        )
        golden_payload, golden_error = load_wavepeek_artifact_for_compare(
            golden_dir,
            test_name,
            "golden",
        )

        if revised_error is not None:
            functional_artifact_errors.append(f"{test_name}: {revised_error}")
        if golden_error is not None:
            functional_artifact_errors.append(f"{test_name}: {golden_error}")
        if revised_payload is None or golden_payload is None:
            continue

        revised_timed_out = is_timeout_functional_payload(revised_payload)
        golden_timed_out = is_timeout_functional_payload(golden_payload)
        if revised_timed_out or golden_timed_out:
            timeout_sides: list[str] = []
            if revised_timed_out:
                timeout_sides.append("revised")
            if golden_timed_out:
                timeout_sides.append("golden")
            functional_timeout_warnings.append(
                f"{test_name}: timeout artifact on {', '.join(timeout_sides)}"
            )

        diff_fields = functional_diff_fields(revised_payload, golden_payload)
        if diff_fields:
            functional_mismatches.append(
                f"{test_name}: mismatched fields {', '.join(diff_fields)}"
            )

    if verbose:
        if revised_only:
            print(
                "warning: compare: tests only in revised run: " + ", ".join(revised_only),
                file=sys.stderr,
            )
        if golden_only:
            print(
                "warning: compare: tests only in golden run: " + ", ".join(golden_only),
                file=sys.stderr,
            )
        if baseline_unsupported:
            print("warning: compare: skipped tests unsupported by golden:", file=sys.stderr)
            for record in baseline_unsupported:
                print(f"  - {record['test_name']}: {record.get('message', '')}", file=sys.stderr)
        if both_side_failures:
            print("warning: compare: skipped tests failed on both sides:", file=sys.stderr)
            for record in both_side_failures:
                print(f"  - {record['test_name']}", file=sys.stderr)
        if revised_failures:
            print("error: compare: revised failures where golden passed:", file=sys.stderr)
            for record in revised_failures:
                print(f"  - {record['test_name']}: {record.get('message', '')}", file=sys.stderr)
        if functional_timeout_warnings:
            print("warning: compare: timeout functional artifacts detected:", file=sys.stderr)
            for issue in functional_timeout_warnings:
                print(f"  - {issue}", file=sys.stderr)

        if timing_failures:
            print(
                "error: compare: median regression exceeds allowed slowdown "
                f"(max({threshold:.2f}%, {threshold_seconds:.6f}s)):",
                file=sys.stderr,
            )
            for issue in timing_failures:
                print(f"  - {format_timing_record(issue)}", file=sys.stderr)

        if functional_mismatches:
            print("error: compare: functional mismatch detected:", file=sys.stderr)
            for issue in functional_mismatches:
                print(f"  - {issue}", file=sys.stderr)

        if functional_artifact_errors:
            print("error: compare: functional artifact errors detected:", file=sys.stderr)
            for issue in functional_artifact_errors:
                print(f"  - {issue}", file=sys.stderr)

    status = (
        "failed"
        if revised_failures or timing_failures or functional_mismatches or functional_artifact_errors
        else "passed"
    )
    skipped_uncomparable_count = len(baseline_unsupported) + len(both_side_failures)
    write_result_json(
        result_json,
        {
            "kind": "wavepeek-e2e-compare-result",
            "schema_version": 1,
            "status": status,
            "functional_only": False,
            "allow_golden_extra": False,
            "matched_count": len(matched),
            "comparable_count": len(matched),
            "skipped_uncomparable_count": skipped_uncomparable_count,
            "failed_uncomparable_count": len(revised_failures),
            "uncomparable_count": skipped_uncomparable_count + len(revised_failures),
            "revised_only": revised_only,
            "golden_only": golden_only,
            "baseline_unsupported": baseline_unsupported,
            "revised_failures": revised_failures,
            "both_side_failures": both_side_failures,
            "integrity_errors": integrity_errors,
            "timing_metric": COMPARE_TIMING_METRIC,
            "threshold_pct": threshold,
            "threshold_seconds": threshold_seconds,
            "timing_failures": timing_failures,
            "functional_timeout_warnings": functional_timeout_warnings,
            "functional_mismatches": functional_mismatches,
            "functional_artifact_errors": functional_artifact_errors,
        },
    )

    if status == "failed":
        if not verbose:
            print(
                "error: compare: checks failed (use --verbose for detailed logs)",
                file=sys.stderr,
            )
        return 1

    if verbose:
        print("info: compare: all checks passed")
    else:
        print("ok: compare: all checks passed (use --verbose for detailed logs)")
    return 0


def cmd_confirm(args: argparse.Namespace) -> int:
    revised_dir = normalize_path(args.revised)
    golden_dir = normalize_path(args.golden)
    ensure_existing_dir(revised_dir, "confirm")
    ensure_existing_dir(golden_dir, "confirm")

    tests = list(getattr(args, "test", None) or [])
    if not tests:
        fail("error: confirm: at least one --test argument is required")

    threshold = float(args.max_negative_delta_pct)
    if threshold < 0:
        fail("error: confirm: --max-negative-delta-pct must be non-negative")
    threshold_seconds = float(getattr(args, "max_negative_delta_seconds", 0.0) or 0.0)
    if threshold_seconds < 0:
        fail("error: confirm: --max-negative-delta-seconds must be non-negative")

    verbose = bool(getattr(args, "verbose", False))
    records: list[dict[str, Any]] = []
    failures: list[dict[str, Any]] = []
    for test_name in tests:
        golden_times = hyperfine_sample_times(golden_dir, test_name, "confirm")
        revised_times = hyperfine_sample_times(revised_dir, test_name, "confirm")
        golden_best = min(golden_times)
        revised_best = min(revised_times)
        record = timing_record(
            test_name=test_name,
            metric="best",
            revised_time=revised_best,
            golden_time=golden_best,
            threshold_pct=threshold,
            threshold_seconds=threshold_seconds,
        )
        record["golden_sample_count"] = len(golden_times)
        record["revised_sample_count"] = len(revised_times)
        records.append(record)
        if float(record["slowdown_seconds"]) > float(record["allowed_slowdown_seconds"]):
            failures.append(record)

    status = "failed" if failures else "passed"
    write_result_json(
        getattr(args, "result_json", None),
        {
            "kind": "wavepeek-e2e-timing-confirm-result",
            "schema_version": 1,
            "status": status,
            "metric": "best",
            "threshold_pct": threshold,
            "threshold_seconds": threshold_seconds,
            "test_count": len(tests),
            "confirmed": records,
            "failures": failures,
        },
    )

    if verbose:
        if failures:
            print(
                "error: confirm: best-sample regression exceeds allowed slowdown "
                f"(max({threshold:.2f}%, {threshold_seconds:.6f}s)):",
                file=sys.stderr,
            )
            for record in failures:
                print(f"  - {format_timing_record(record)}", file=sys.stderr)
        else:
            print(
                "info: confirm: best-sample timing confirmation passed "
                f"for {len(records)} test(s)"
            )
            for record in records:
                print(f"  - {format_timing_record(record)}")

    if failures:
        if not verbose:
            print(
                "error: confirm: best-sample timing confirmation failed "
                "(use --verbose for detailed logs)",
                file=sys.stderr,
            )
        return 1

    if not verbose:
        print("ok: confirm: best-sample timing confirmation passed")
    return 0


def build_parser() -> argparse.ArgumentParser:
    parser = argparse.ArgumentParser(description="CLI E2E benchmark harness")
    subparsers = parser.add_subparsers(dest="command", required=True)

    list_parser = subparsers.add_parser("list", help="list available benchmark tests")
    list_parser.add_argument("--filter", default=None, help="regex filter by test name")
    list_parser.add_argument(
        "--tests",
        default=str(TESTS_PATH),
        help="path to benchmark tests catalog JSON",
    )

    run_parser = subparsers.add_parser("run", help="run selected benchmarks")
    run_parser.add_argument("--filter", default=None, help="regex filter by test name")
    run_parser.add_argument(
        "--binary",
        action="append",
        metavar="LABEL=PATH",
        help="binary to benchmark; repeat for multiple labeled binaries",
    )
    run_parser.add_argument(
        "--schedule",
        choices=("round-robin", "grouped"),
        default="round-robin",
        help="test scheduling across multiple binaries",
    )
    run_parser.add_argument("--run-dir", default=None, help="existing/new run directory")
    run_parser.add_argument(
        "--out-dir",
        default=str(DEFAULT_RUNS_DIR),
        help="parent for timestamped run directories",
    )
    run_parser.add_argument("--compare", default=None, help="baseline run directory for README deltas")
    run_parser.add_argument(
        "--missing-only",
        action="store_true",
        help="run only tests missing artifacts in run directory",
    )
    run_parser.add_argument(
        "--fail-fast",
        action="store_true",
        help="stop on the first per-test preflight or benchmark failure",
    )
    run_parser.add_argument(
        "--wavepeek-timeout-seconds",
        type=int,
        default=DEFAULT_WAVEPEEK_TIMEOUT_SECONDS,
        help="max seconds per wavepeek invocation before timeout cap",
    )
    run_parser.add_argument(
        "--tests",
        default=str(TESTS_PATH),
        help="path to benchmark tests catalog JSON",
    )
    run_parser.add_argument(
        "-v",
        "--verbose",
        action="store_true",
        help="show detailed benchmark progress and diagnostics",
    )

    report_parser = subparsers.add_parser("report", help="generate/update README.md for a run")
    report_parser.add_argument("--run-dir", required=True, help="run directory")
    report_parser.add_argument("--compare", default=None, help="baseline run directory for README deltas")
    report_parser.add_argument(
        "--tests",
        default=str(TESTS_PATH),
        help="path to benchmark tests catalog JSON",
    )

    compare_parser = subparsers.add_parser("compare", help="check revised run against golden")
    compare_parser.add_argument("--revised", required=True, help="revised run directory")
    compare_parser.add_argument("--golden", required=True, help="golden run directory")
    compare_parser.add_argument(
        "--max-negative-delta-pct",
        required=False,
        type=float,
        help="relative median slowdown threshold",
    )
    compare_parser.add_argument(
        "--max-negative-delta-seconds",
        required=False,
        type=float,
        default=0.0,
        help="absolute median slowdown floor in seconds",
    )
    compare_parser.add_argument(
        "--functional-only",
        action="store_true",
        help="compare only wavepeek JSON artifacts and skip timing artifacts",
    )
    compare_parser.add_argument(
        "--allow-golden-extra",
        action="store_true",
        help="with --functional-only, allow extra artifacts in the golden directory",
    )
    compare_parser.add_argument(
        "--ignore-functional-test",
        action="append",
        help="with --functional-only, ignore one test as NAME=REASON",
    )
    compare_parser.add_argument(
        "--result-json",
        default=None,
        help="write machine-readable compare result JSON",
    )
    compare_parser.add_argument(
        "-v",
        "--verbose",
        action="store_true",
        help="show detailed compare warnings and failures",
    )

    confirm_parser = subparsers.add_parser(
        "confirm",
        help="confirm failed timing tests with best hyperfine samples",
    )
    confirm_parser.add_argument("--revised", required=True, help="revised run directory")
    confirm_parser.add_argument("--golden", required=True, help="golden run directory")
    confirm_parser.add_argument(
        "--test",
        action="append",
        help="test name to confirm; repeat for multiple tests",
    )
    confirm_parser.add_argument(
        "--max-negative-delta-pct",
        required=True,
        type=float,
        help="relative best-sample slowdown threshold",
    )
    confirm_parser.add_argument(
        "--max-negative-delta-seconds",
        required=False,
        type=float,
        default=0.0,
        help="absolute best-sample slowdown floor in seconds",
    )
    confirm_parser.add_argument(
        "--result-json",
        default=None,
        help="write machine-readable confirmation result JSON",
    )
    confirm_parser.add_argument(
        "-v",
        "--verbose",
        action="store_true",
        help="show detailed confirmation failures",
    )

    return parser


def main(argv: list[str] | None = None) -> int:
    parser = build_parser()
    args = parser.parse_args(argv)

    if args.command == "list":
        return cmd_list(args)
    if args.command == "run":
        return cmd_run(args)
    if args.command == "report":
        return cmd_report(args)
    if args.command == "compare":
        return cmd_compare(args)
    if args.command == "confirm":
        return cmd_confirm(args)

    fail(f"error: unsupported command: {args.command}")
    raise AssertionError("unreachable")


if __name__ == "__main__":
    raise SystemExit(main())