conproxy 0.2.1

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

// CLI parsing here is index-based for clarity, and the bench tuple type is
// local-only. Allow the deny-by-default lints that would otherwise require
// wrapping this in a helper module just to please clippy.
#![allow(
    clippy::arithmetic_side_effects,
    clippy::type_complexity,
    clippy::indexing_slicing
)]

use serde::{Deserialize, Serialize};
use std::env;
use std::fs;
use std::path::{Path, PathBuf};
use std::process::{Command, ExitCode};
use std::time::SystemTime;

// ---------------------------------------------------------------------------
// Criterion JSON shapes (subset)
// ---------------------------------------------------------------------------

#[derive(Deserialize)]
struct EstPoint {
    point_estimate: f64,
    #[allow(dead_code)]
    confidence_interval: Option<CIBounds>,
    #[allow(dead_code)]
    standard_error: Option<f64>,
}

#[derive(Deserialize)]
struct CIBounds {
    lower_bound: f64,
    upper_bound: f64,
    #[allow(dead_code)]
    confidence_level: f64,
}

#[derive(Deserialize)]
struct Est {
    mean: EstPoint,
}

#[derive(Deserialize)]
struct ChangeEst {
    mean: EstPoint,
}

// ---------------------------------------------------------------------------
// Output JSON
// ---------------------------------------------------------------------------

#[derive(Serialize)]
struct BenchEntry {
    name: String,
    group: String,
    mean_ns: f64,
    change_pct: Option<f64>,
    ci: Option<(f64, f64)>,
    has_main: bool,
    status: String, // "regression" | "improvement" | "inconclusive" | "ok" | "no_main"
    verdict_reason: String,
}

#[derive(Serialize)]
struct Summary {
    schema_version: u32,
    timestamp: String,
    mode: String,
    threshold_pct: f64,
    verdict: String,
    git_sha: String,
    rustc_version: String,
    benches: Vec<BenchEntry>,
    regressions: Vec<ChangeEntry>,
    improvements: Vec<ChangeEntry>,
    inconclusive: Vec<ChangeEntry>,
    metrics: Option<serde_json::Value>,
    artifacts: Artifacts,
    baseline_meta: Option<BaselineMeta>,
    baseline_status: String,
    baseline_age_hours: Option<f64>,
    baseline_commits_behind: Option<u32>,
    // v2 fields (additive; digest-safe)
    hitrate: Option<HitrateSummary>,
    diagnostics: Diagnostics,
    tokio: Option<TokioSnap>,
    tokio_metrics: Option<TokioMetrics>,
    tokio_dump: Option<TokioDump>,
    trend: Option<Vec<TrendPoint>>,
    chronic_regressions: Option<Vec<ChronicRegression>>,
    history: Option<Vec<HistoryEntry>>,
}

#[derive(Serialize)]
struct Artifacts {
    summary_md: String,
    report_criterion: String,
    analysis_md: String,
}

#[derive(Serialize, Deserialize, Clone)]
struct BaselineMeta {
    schema_version: u32,
    saved_at: String,
    git_sha: String,
    git_dirty: bool,
    rustc: String,
    bench_count: u32,
}

#[derive(Serialize)]
struct EnvJson {
    git_sha: String,
    rustc_version: String,
    mode: String,
    threshold_pct: f64,
    timestamp: String,
    baseline_meta: Option<BaselineMeta>,
}

#[derive(Serialize, Clone)]
struct ChangeEntry {
    name: String,
    change_pct: f64,
    ci: Option<(f64, f64)>,
}

// Hitrate summary parsed from hitrate_bench summary.json (additive — only
// present when --hitrate-summary is provided). Best-effort parse: malformed
// files yield None so the rest of the report still works.
#[derive(Serialize, Deserialize, Clone)]
struct HitrateWorkload {
    name: String,
    best_exact_hr: Option<f64>,
    best_combined_hr: Option<f64>,
    best_tau: Option<f64>,
    false_hit_rate: Option<f64>,
    semantic_hr_uplift: Option<f64>,
}

#[derive(Serialize, Deserialize, Clone)]
struct HitrateSummary {
    verdict: String,
    agentic_best: Option<f64>,
    agentic_gate: Option<f64>,
    workloads: Vec<HitrateWorkload>,
}

#[derive(Serialize, Deserialize, Clone)]
struct TokioTask {
    name: String,
    total_poll_ms: f64,
    poll_count: u64,
    avg_poll_us: f64,
    busy_ratio: Option<f64>,
}

#[derive(Serialize, Deserialize, Clone)]
struct TokioSnap {
    sampled_secs: f64,
    top_tasks: Vec<TokioTask>,
    note: Option<String>,
}

// RuntimeMetrics JSON snapshot from `GET /debug/tokio`. The proxy returns
// whatever fields are available given the build's tokio_unstable flag:
// stable: num_alive_tasks, num_workers, worker_total_busy_duration_ns,
//         global_queue_depth
// unstable: worker_poll_count, worker_mean_poll_time_ns, budget_forced_yield_count
// Best-effort parse: any missing field is None. The `available` flag is set
// by the proxy when the endpoint is reachable; the loader sets it true if the
// JSON parses at all.
#[derive(Serialize, Deserialize, Clone)]
struct TokioMetrics {
    available: bool,
    num_alive_tasks: Option<u64>,
    num_workers: Option<usize>,
    global_queue_depth: Option<usize>,
    worker_total_busy_duration_ns: Option<u64>,
    worker_poll_count: Option<u64>,
    worker_mean_poll_time_ns: Option<u64>,
    budget_forced_yield_count: Option<u64>,
}

// Handle::dump() text output. Stored as raw text; size cap avoids huge
// summary.json. `available` distinguishes "endpoint not built" from
// "endpoint built but dump empty". `hint` set when endpoint returned 503
// with the RUSTFLAGS/feature message.
#[derive(Serialize, Clone)]
struct TokioDump {
    available: bool,
    path: String,
    text: String,
    hint: Option<String>,
}

#[derive(Serialize, Clone)]
struct Diagnostics {
    flamegraph: bool,
    heaptrack: bool,
    tokio_console: bool,
    tokio_metrics: bool,
    tokio_dump: bool,
    hitrate: bool,
}

#[derive(Serialize, Clone)]
struct HistoryEntry {
    timestamp: String,
    sha: String,
    verdict: String,
    top_mover: Option<String>,
    top_mover_pct: Option<f64>,
}

#[derive(Serialize, Clone)]
struct TrendPoint {
    name: String,
    prev_mean_ns: f64,
    new_mean_ns: f64,
    delta_pct: f64,
}

#[derive(Serialize, Clone)]
struct ChronicRegression {
    name: String,
    consecutive_runs: u32,
    last_pct: f64,
}

#[derive(Serialize)]
struct ReportCriterion {
    threshold_pct: f64,
    improvements: Vec<ChangeEntry>,
    regressions: Vec<ChangeEntry>,
    inconclusive: Vec<ChangeEntry>,
}

// ---------------------------------------------------------------------------
// CLI
// ---------------------------------------------------------------------------

#[derive(Default)]
struct Args {
    results_dir: Option<String>,
    threshold: f64,
    criterion_dir: Option<String>,
    mode: String,
    since: Option<String>,
    metrics_file: Option<String>,
    metrics_before_file: Option<String>,
    flamegraph: Option<String>,
    fail_on_regression: bool,
    hitrate_summary: Option<String>,
    history_dir: Option<String>,
    tokio_snap: Option<String>,
    tokio_metrics: Option<String>,
    tokio_dump: Option<String>,
}

fn parse_args() -> Result<Args, String> {
    let mut a = Args {
        threshold: 15.0,
        mode: "quick".into(),
        ..Default::default()
    };
    let argv: Vec<String> = env::args().skip(1).collect();
    let mut i = 0;
    while i < argv.len() {
        let arg = &argv[i];
        let next = argv.get(i + 1);
        match arg.as_str() {
            "--results-dir" => {
                a.results_dir = next.cloned();
                i += 1;
            }
            "--threshold" => {
                a.threshold = next
                    .ok_or("--threshold requires value")?
                    .parse::<f64>()
                    .map_err(|e| format!("--threshold: {e}"))?;
                i += 1;
            }
            "--criterion-dir" => {
                a.criterion_dir = next.cloned();
                i += 1;
            }
            "--mode" => {
                a.mode = next.ok_or("--mode requires value")?.clone();
                i += 1;
            }
            "--since" => {
                a.since = next.cloned();
                i += 1;
            }
            "--metrics-file" => {
                a.metrics_file = next.cloned();
                i += 1;
            }
            "--metrics-before-file" => {
                a.metrics_before_file = next.cloned();
                i += 1;
            }
            "--flamegraph" => {
                a.flamegraph = next.cloned();
                i += 1;
            }
            "--fail-on-regression" => {
                a.fail_on_regression = true;
            }
            "--hitrate-summary" => {
                a.hitrate_summary = next.cloned();
                i += 1;
            }
            "--history-dir" => {
                a.history_dir = next.cloned();
                i += 1;
            }
            "--tokio-snap" => {
                a.tokio_snap = next.cloned();
                i += 1;
            }
            "--tokio-metrics" => {
                a.tokio_metrics = next.cloned();
                i += 1;
            }
            "--tokio-dump" => {
                a.tokio_dump = next.cloned();
                i += 1;
            }
            other => return Err(format!("Unknown arg: {other}")),
        }
        i += 1;
    }
    Ok(a)
}

// ---------------------------------------------------------------------------
// Bench name → group mapping
// ---------------------------------------------------------------------------

fn bench_group(name: &str) -> String {
    match name {
        n if n.starts_with("cache_") => "core_ops".into(),
        n if n.starts_with("query_") || n.starts_with("response_") => "core_ops".into(),
        n if n == "slugify" || n == "normalize_query" => "core_ops".into(),
        n if n.starts_with("fuse_rrf") || n.starts_with("scope_") => "cascade_scope".into(),
        _ => "other".into(),
    }
}

// Static map: bench name → (source file:fn, candidate rust-skills rules).
// Used in regression detail to give investigators a starting point.
// Keep entries short — one source line per bench + 1–2 rule hints.
fn hot_path_for(bench: &str) -> Option<(&'static str, &'static str)> {
    Some(match bench {
        "cache_insert" => (
            "src/proxy/cache.rs::CacheStore::insert_impl",
            "rust-skills: perf-entry-api, mem-collect-once, opt-cold-unlikely",
        ),
        "cache_lookup_hit" => (
            "src/proxy/cache.rs::CacheStore::get",
            "rust-skills: perf-iter-over-index, mem-clone-from",
        ),
        "cache_lookup_miss" => (
            "src/proxy/cache.rs::CacheStore::get (miss path)",
            "rust-skills: opt-inline-always-rare",
        ),
        "cache_eviction_pressure" => (
            "src/proxy/cache.rs::CacheStore::evict_expired",
            "rust-skills: mem-collect-once, opt-cold-unlikely",
        ),
        "cache_throughput" => (
            "src/proxy/cache.rs (insert + lookup combined)",
            "rust-skills: perf-entry-api, opt-codegen-units",
        ),
        "query_serialize_json" | "query_deserialize_json" => (
            "src/proxy/query.rs (serde derive)",
            "rust-skills: serde-flatten, opt-inline-always",
        ),
        "response_serialize_json" | "response_deserialize_json" => (
            "src/proxy/query.rs (serde derive)",
            "rust-skills: serde-flatten, opt-inline-always",
        ),
        "query_hash" => (
            "src/proxy/query.rs::compute_query_hash",
            "rust-skills: perf-collect-once",
        ),
        "slugify" => (
            "src/proxy/mod.rs::slugify",
            "rust-skills: mem-write-over-format, perf-iter-lazy",
        ),
        "normalize_query" => (
            "src/proxy/query.rs::normalize_query",
            "rust-skills: perf-iter-lazy, mem-collect-once",
        ),
        "fuse_rrf_3upstreams_k60" | "fuse_rrf_dedup_2upstreams" | "fuse_rrf_2upstreams_50each" => (
            "src/proxy/cascade.rs::fuse_rrf",
            "rust-skills: perf-collect-into, mem-collect-once, opt-codegen-units",
        ),
        "scope_best_sim_short_content"
        | "scope_best_sim_long_content"
        | "scope_best_sim_no_match"
        | "scope_filter_results_20items" => (
            "src/proxy/scope.rs::ScopeFilter::best_sim / filter_results",
            "rust-skills: perf-iter-lazy, mem-collect-once, opt-inline-always",
        ),
        _ => return None,
    })
}

// ---- History + trend loading (best-effort, additive) ----

fn load_hitrate_summary(path: &str) -> Option<HitrateSummary> {
    let text = fs::read_to_string(path).ok()?;
    let v: serde_json::Value = serde_json::from_str(&text).ok()?;
    // hitrate_bench summary.json: top-level "workloads" array + "verdict" key.
    let verdict = v
        .get("verdict")
        .and_then(|x| x.as_str())
        .unwrap_or("UNKNOWN")
        .to_string();
    let mut workloads = Vec::new();
    if let Some(arr) = v.get("workloads").and_then(|x| x.as_array()) {
        for wl in arr {
            let name = wl
                .get("name")
                .and_then(|x| x.as_str())
                .unwrap_or("?")
                .to_string();
            // Workload rows have a "sweep" array; pick the best exact_hit_rate.
            let (best_exact, best_combined, best_tau, false_hit, uplift) =
                if let Some(sweep) = wl.get("sweep").and_then(|x| x.as_array()) {
                    let mut best_exact: Option<(f64, Option<f64>, Option<f64>, Option<f64>)> = None;
                    let mut best_combined: Option<(
                        f64,
                        Option<f64>,
                        Option<f64>,
                        Option<f64>,
                        Option<f64>,
                    )> = None;
                    for p in sweep {
                        let exact = p.get("exact_hit_rate").and_then(|x| x.as_f64());
                        let combined = p.get("combined_hit_rate").and_then(|x| x.as_f64());
                        let tau = p.get("tau").and_then(|x| x.as_f64());
                        let fh = p.get("false_hit_rate").and_then(|x| x.as_f64());
                        let upl = p.get("uplift").and_then(|x| x.as_f64());
                        if let Some(e) = exact {
                            if best_exact.is_none_or(|(be, _, _, _)| e > be) {
                                best_exact = Some((e, combined, tau, fh));
                            }
                        }
                        if let Some(c) = combined {
                            if best_combined.is_none_or(|(bc, _, _, _, _)| c > bc) {
                                best_combined = Some((c, exact, tau, fh, upl));
                            }
                        }
                    }
                    (
                        best_exact.map(|(e, _, _, _)| e),
                        best_combined.map(|(c, _, _, _, _)| c),
                        best_combined.and_then(|(_, _, t, _, _)| t),
                        best_exact
                            .and_then(|(_, _, _, f)| f)
                            .or_else(|| best_combined.and_then(|(_, _, _, f, _)| f)),
                        best_combined.and_then(|(_, _, _, _, u)| u),
                    )
                } else {
                    (None, None, None, None, None)
                };
            workloads.push(HitrateWorkload {
                name,
                best_exact_hr: best_exact,
                best_combined_hr: best_combined,
                best_tau,
                false_hit_rate: false_hit,
                semantic_hr_uplift: uplift,
            });
        }
    }
    // Agentic-specific gate + best.
    let agentic = workloads.iter().find(|w| w.name == "agentic");
    Some(HitrateSummary {
        verdict,
        agentic_best: agentic.and_then(|w| w.best_exact_hr),
        agentic_gate: Some(0.40),
        workloads,
    })
}

fn load_tokio_snap(path: &str) -> Option<TokioSnap> {
    let text = fs::read_to_string(path).ok()?;
    serde_json::from_str(&text).ok()
}

// Load RuntimeMetrics JSON from the /debug/tokio endpoint. The proxy writes
// either a real object (when endpoint is reachable) or a short text
// placeholder like "(no /debug/tokio endpoint)" on curl failure. We treat
// non-object content as unavailable.
fn load_tokio_metrics(path: &str) -> Option<TokioMetrics> {
    let text = fs::read_to_string(path).ok()?;
    if !text.trim_start().starts_with('{') {
        return None;
    }
    let val: serde_json::Value = serde_json::from_str(&text).ok()?;
    let obj = val.as_object()?;
    Some(TokioMetrics {
        available: true,
        num_alive_tasks: obj.get("num_alive_tasks").and_then(|v| v.as_u64()),
        num_workers: obj
            .get("num_workers")
            .and_then(|v| v.as_u64().map(|n| n as usize)),
        global_queue_depth: obj
            .get("global_queue_depth")
            .and_then(|v| v.as_u64().map(|n| n as usize)),
        worker_total_busy_duration_ns: obj
            .get("worker_total_busy_duration_ns")
            .and_then(|v| v.as_u64()),
        worker_poll_count: obj.get("worker_poll_count").and_then(|v| v.as_u64()),
        worker_mean_poll_time_ns: obj.get("worker_mean_poll_time_ns").and_then(|v| v.as_u64()),
        budget_forced_yield_count: obj
            .get("budget_forced_yield_count")
            .and_then(|v| v.as_u64()),
    })
}

// Load Handle::dump() text. The Makefile step writes a placeholder when the
// endpoint is missing (e.g. "(no /debug/tokio/dump endpoint — needs ...").
// Such placeholders are detected and reported with the `hint` so the user
// knows what to enable.
const DUMP_MAX_BYTES: usize = 64 * 1024;
fn load_tokio_dump(path: &str) -> TokioDump {
    let text = match fs::read_to_string(path) {
        Ok(t) => t,
        Err(_) => {
            return TokioDump {
                available: false,
                path: path.to_string(),
                text: String::new(),
                hint: None,
            };
        }
    };
    // Proxy returns text/plain (or 503 with text). The 503 placeholder
    // contains "needs" — sniff for it to surface the build hint.
    let is_placeholder = !text.trim_start().starts_with('{')
        && text
            .lines()
            .any(|l| l.contains("RUSTFLAGS") || l.contains("tokio_unstable"));
    if is_placeholder {
        return TokioDump {
            available: false,
            path: path.to_string(),
            text: String::new(),
            hint: Some(text.trim().to_string()),
        };
    }
    let truncated = if text.len() > DUMP_MAX_BYTES {
        text[..DUMP_MAX_BYTES].to_string()
    } else {
        text
    };
    TokioDump {
        available: true,
        path: path.to_string(),
        text: truncated,
        hint: None,
    }
}

fn load_history(dir: &str, current_sha: &str) -> Vec<HistoryEntry> {
    let mut out = Vec::new();
    let path = Path::new(dir);
    if !path.is_dir() {
        return out;
    }
    let entries = match fs::read_dir(path) {
        Ok(e) => e,
        Err(_) => return out,
    };
    // Subdirs: <ts>-<sha8>/summary.json. Sort by mtime (newest first) and
    // take last 5 (excluding the current sha).
    let mut dirs: Vec<(std::time::SystemTime, std::path::PathBuf)> = Vec::new();
    for e in entries.flatten() {
        let p = e.path();
        if !p.is_dir() {
            continue;
        }
        let m = e.metadata().and_then(|m| m.modified()).ok();
        if let Some(m) = m {
            dirs.push((m, p));
        }
    }
    dirs.sort_by_key(|x| std::cmp::Reverse(x.0));
    for (_m, d) in dirs.into_iter().take(10) {
        let summary = d.join("summary.json");
        if !summary.is_file() {
            continue;
        }
        if let Ok(text) = fs::read_to_string(&summary) {
            if let Ok(v) = serde_json::from_str::<serde_json::Value>(&text) {
                let sha = v
                    .get("git_sha")
                    .and_then(|x| x.as_str())
                    .unwrap_or("")
                    .to_string();
                if sha.starts_with(current_sha) {
                    continue;
                }
                let ts = v
                    .get("timestamp")
                    .and_then(|x| x.as_str())
                    .unwrap_or("")
                    .to_string();
                let verdict = v
                    .get("verdict")
                    .and_then(|x| x.as_str())
                    .unwrap_or("?")
                    .to_string();
                // Top mover: max |change_pct| among benches with a change.
                let mut top: Option<(String, f64)> = None;
                if let Some(arr) = v.get("benches").and_then(|x| x.as_array()) {
                    for b in arr {
                        if let Some(p) = b.get("change_pct").and_then(|x| x.as_f64()) {
                            let name = b
                                .get("name")
                                .and_then(|x| x.as_str())
                                .unwrap_or("?")
                                .to_string();
                            if top.as_ref().is_none_or(|(_, tp)| p.abs() > tp.abs()) {
                                top = Some((name, p));
                            }
                        }
                    }
                }
                let (tm, tp) = top.map(|(n, p)| (Some(n), Some(p))).unwrap_or((None, None));
                out.push(HistoryEntry {
                    timestamp: ts,
                    sha: sha.chars().take(8).collect(),
                    verdict,
                    top_mover: tm,
                    top_mover_pct: tp,
                });
                if out.len() >= 5 {
                    break;
                }
            }
        }
    }
    out
}

fn compute_trend(
    new_benches: &[BenchEntry],
    history: &[HistoryEntry],
    history_dir: &str,
    current_sha: &str,
) -> Vec<TrendPoint> {
    // Trend: compare current mean_ns vs the most-recent prior published run
    // that has matching bench names. Only benches present in both compare.
    let Some(prior) = find_prior_bench_estimates(history_dir, current_sha) else {
        return Vec::new();
    };
    let mut out = Vec::new();
    for b in new_benches {
        if let Some(prev) = prior.get(&b.name) {
            if *prev > 0.0 {
                let delta = (b.mean_ns - *prev) / *prev * 100.0;
                out.push(TrendPoint {
                    name: b.name.clone(),
                    prev_mean_ns: *prev,
                    new_mean_ns: b.mean_ns,
                    delta_pct: delta,
                });
            }
        }
    }
    let _ = history; // history is loaded by caller separately
    out
}

fn find_prior_bench_estimates(
    history_dir: &str,
    current_sha: &str,
) -> Option<std::collections::HashMap<String, f64>> {
    let path = std::path::Path::new(history_dir);
    if !path.is_dir() {
        return None;
    }
    let entries = fs::read_dir(path).ok()?;
    let mut dirs: Vec<(std::time::SystemTime, std::path::PathBuf)> = Vec::new();
    for e in entries.flatten() {
        let p = e.path();
        if !p.is_dir() {
            continue;
        }
        if let Ok(m) = e.metadata().and_then(|m| m.modified()) {
            dirs.push((m, p));
        }
    }
    dirs.sort_by_key(|x| std::cmp::Reverse(x.0));
    for (_m, d) in dirs {
        let summary = d.join("summary.json");
        if !summary.is_file() {
            continue;
        }
        if let Ok(text) = fs::read_to_string(&summary) {
            if let Ok(v) = serde_json::from_str::<serde_json::Value>(&text) {
                let sha = v
                    .get("git_sha")
                    .and_then(|x| x.as_str())
                    .unwrap_or("")
                    .to_string();
                if sha.starts_with(current_sha) {
                    continue;
                }
                let mut map = std::collections::HashMap::new();
                if let Some(arr) = v.get("benches").and_then(|x| x.as_array()) {
                    for b in arr {
                        if let (Some(name), Some(mean)) = (
                            b.get("name").and_then(|x| x.as_str()),
                            b.get("mean_ns").and_then(|x| x.as_f64()),
                        ) {
                            map.insert(name.to_string(), mean);
                        }
                    }
                }
                if !map.is_empty() {
                    return Some(map);
                }
            }
        }
    }
    None
}

fn compute_chronic_regressions(
    new_benches: &[BenchEntry],
    history_dir: &str,
    current_sha: &str,
) -> Vec<ChronicRegression> {
    // Walk the last 2 prior runs; flag benches that are regressed in
    // *both* prior runs (suggesting a chronic pattern, not one-off drift).
    let path = std::path::Path::new(history_dir);
    if !path.is_dir() {
        return Vec::new();
    }
    let entries = match fs::read_dir(path) {
        Ok(e) => e,
        Err(_) => return Vec::new(),
    };
    let mut dirs: Vec<(std::time::SystemTime, std::path::PathBuf)> = Vec::new();
    for e in entries.flatten() {
        let p = e.path();
        if !p.is_dir() {
            continue;
        }
        if let Ok(m) = e.metadata().and_then(|m| m.modified()) {
            dirs.push((m, p));
        }
    }
    dirs.sort_by_key(|x| std::cmp::Reverse(x.0));
    let mut prior_runs: Vec<std::collections::HashMap<String, f64>> = Vec::new();
    for (_m, d) in dirs {
        if prior_runs.len() >= 2 {
            break;
        }
        let summary = d.join("summary.json");
        if !summary.is_file() {
            continue;
        }
        if let Ok(text) = fs::read_to_string(&summary) {
            if let Ok(v) = serde_json::from_str::<serde_json::Value>(&text) {
                let sha = v
                    .get("git_sha")
                    .and_then(|x| x.as_str())
                    .unwrap_or("")
                    .to_string();
                if sha.starts_with(current_sha) {
                    continue;
                }
                let mut map = std::collections::HashMap::new();
                if let Some(arr) = v.get("benches").and_then(|x| x.as_array()) {
                    for b in arr {
                        if let (Some(name), Some(pct)) = (
                            b.get("name").and_then(|x| x.as_str()),
                            b.get("change_pct").and_then(|x| x.as_f64()),
                        ) {
                            map.insert(name.to_string(), pct);
                        }
                    }
                }
                if !map.is_empty() {
                    prior_runs.push(map);
                }
            }
        }
    }
    if prior_runs.is_empty() {
        return Vec::new();
    }
    let mut chronic = Vec::new();
    for b in new_benches {
        if b.status != "regression" {
            continue;
        }
        let in_all = prior_runs
            .iter()
            .all(|m| m.get(&b.name).is_some_and(|p| *p > 0.0));
        if in_all {
            chronic.push(ChronicRegression {
                name: b.name.clone(),
                consecutive_runs: prior_runs.len() as u32 + 1,
                last_pct: b.change_pct.unwrap_or(0.0),
            });
        }
    }
    chronic
}

fn fmt_time(ns: f64) -> String {
    if ns < 1_000.0 {
        format!("{ns:.0}ns")
    } else if ns < 1_000_000.0 {
        format!("{:.1}µs", ns / 1_000.0)
    } else if ns < 1_000_000_000.0 {
        format!("{:.1}ms", ns / 1_000_000.0)
    } else {
        format!("{:.2}s", ns / 1_000_000_000.0)
    }
}

// ---------------------------------------------------------------------------
// Walk criterion dir
// ---------------------------------------------------------------------------

/// Returns (display_name, new_path, change_path_or_None, main_path_or_None).
/// display_name uses '/' separator for parameterized benches (matches criterion UI).
fn collect_benches(
    crit_path: &Path,
    since: Option<SystemTime>,
) -> Result<Vec<(String, PathBuf, Option<PathBuf>, Option<PathBuf>)>, Box<dyn std::error::Error>> {
    let mut out = Vec::new();
    walk_criterion(crit_path, crit_path, since, &mut out)?;
    out.sort_by(|a, b| a.0.cmp(&b.0));
    Ok(out)
}

fn walk_criterion(
    root: &Path,
    dir: &Path,
    since: Option<SystemTime>,
    out: &mut Vec<(String, PathBuf, Option<PathBuf>, Option<PathBuf>)>,
) -> Result<(), Box<dyn std::error::Error>> {
    if !dir.is_dir() {
        return Ok(());
    }
    // Leaf check: does this dir itself contain new/estimates.json?
    let new_path = dir.join("new/estimates.json");
    if new_path.is_file() {
        if let Some(min) = since {
            if let Ok(meta) = new_path.metadata() {
                if let Ok(mtime) = meta.modified() {
                    if mtime < min {
                        return Ok(()); // stale, skip
                    }
                }
            }
        }
        let name = dir
            .strip_prefix(root)
            .unwrap_or(dir)
            .to_string_lossy()
            .replace(std::path::MAIN_SEPARATOR, "/");
        // Skip internal criterion dirs (e.g. "report" at root has no new/ but
        // any nested dir whose name happens to be internal won't be a leaf with
        // new/ in it, so this guard is implicit).
        let change_path = dir.join("change/estimates.json");
        let main_path = dir.join("main/estimates.json");
        out.push((
            name,
            new_path,
            change_path.is_file().then_some(change_path),
            main_path.is_file().then_some(main_path),
        ));
        return Ok(());
    }
    // Otherwise recurse one level.
    for entry in fs::read_dir(dir)? {
        let entry = entry?;
        if entry.file_type()?.is_dir() {
            walk_criterion(root, &entry.path(), since, out)?;
        }
    }
    Ok(())
}

// ---------------------------------------------------------------------------
// Comparison + verdict
// ---------------------------------------------------------------------------

struct BenchResult {
    name: String,
    group: String,
    mean_ns: f64,
    change_pct: Option<f64>,
    ci: Option<(f64, f64)>,
    has_main: bool,
    status: String,
    verdict_reason: String,
}

fn evaluate(
    name: String,
    group: String,
    mean_ns: f64,
    new_se: Option<f64>,
    change_path: Option<PathBuf>,
    main_path: Option<PathBuf>,
    threshold: f64,
) -> Result<BenchResult, Box<dyn std::error::Error>> {
    // Strategy:
    //   1. If main/ exists, compute branch-vs-main delta + 95% CI from raw
    //      point estimates + standard errors. (se_Δ = sqrt(se_new² + se_main²)
    //      for independent normal samples; 1.96 ≈ z for 95%.)
    //   2. Else if change/ exists, trust criterion's precomputed delta + CI.
    //      (Caveat: change/ is new-vs-previous-run unless --baseline main was
    //      passed. With no main saved, this is the best we have.)
    //   3. Else: no baseline → status = no_main.
    let (delta_pct, ci, has_main) = if let Some(ref p) = main_path {
        let raw = fs::read_to_string(p)?;
        let m: Est = serde_json::from_str(&raw)?;
        let main_mean = m.mean.point_estimate;
        let main_se = m.mean.standard_error.unwrap_or(0.0);
        if main_mean > 0.0 && mean_ns > 0.0 {
            let delta = mean_ns - main_mean;
            let se_delta = (new_se.unwrap_or(0.0).powi(2) + main_se.powi(2)).sqrt();
            let ci_delta = 1.96 * se_delta;
            let pct = (delta / main_mean) * 100.0;
            let lo = ((delta - ci_delta) / main_mean) * 100.0;
            let hi = ((delta + ci_delta) / main_mean) * 100.0;
            (Some(pct), Some((lo, hi)), true)
        } else {
            (None, None, true)
        }
    } else if let Some(p) = change_path {
        let raw = fs::read_to_string(&p)?;
        let ch: ChangeEst = serde_json::from_str(&raw)?;
        let pct = ch.mean.point_estimate * 100.0;
        let ci = ch
            .mean
            .confidence_interval
            .map(|b| (b.lower_bound * 100.0, b.upper_bound * 100.0));
        (Some(pct), ci, false)
    } else {
        (None, None, false)
    };

    let (status, reason) = match (delta_pct, ci) {
        (None, _) => ("no_main".into(), "no main baseline + no change file".into()),
        (Some(_p), Some((lo, _hi))) if lo >= threshold => (
            "regression".into(),
            format!("CI lower bound {lo:+.1}% ≥ +{threshold}%"),
        ),
        (Some(_), Some((_lo, hi))) if hi <= -threshold => (
            "improvement".into(),
            format!("CI upper bound {hi:+.1}% ≤ -{threshold}%"),
        ),
        (Some(p), Some((lo, hi))) => (
            "inconclusive".into(),
            format!("CI [{lo:+.1}%, {hi:+.1}%] crosses ±{threshold}% (point {p:+.1}%)"),
        ),
        (Some(p), None) if p >= threshold => (
            "regression".into(),
            format!("point estimate +{p:.1}% ≥ +{threshold}% (no CI available)"),
        ),
        (Some(p), None) if p <= -threshold => (
            "improvement".into(),
            format!("point estimate {p:+.1}% ≤ -{threshold}% (no CI available)"),
        ),
        (Some(p), None) => (
            "ok".into(),
            format!("point estimate {p:+.1}% within threshold (no CI)"),
        ),
    };

    Ok(BenchResult {
        name,
        group,
        mean_ns,
        change_pct: delta_pct,
        ci,
        has_main,
        status,
        verdict_reason: reason,
    })
}

// ---------------------------------------------------------------------------
// Output writers
// ---------------------------------------------------------------------------

fn write_outputs(dir: &str, s: &Summary) -> Result<(), Box<dyn std::error::Error>> {
    fs::write(
        format!("{dir}/summary.json"),
        serde_json::to_string_pretty(s)?,
    )?;
    let report = ReportCriterion {
        threshold_pct: s.threshold_pct,
        improvements: s.improvements.clone(),
        regressions: s.regressions.clone(),
        inconclusive: s.inconclusive.clone(),
    };
    fs::write(
        format!("{dir}/report_criterion.json"),
        serde_json::to_string_pretty(&report)?,
    )?;
    fs::write(format!("{dir}/SUMMARY.md"), render_summary_md(s))?;
    fs::write(format!("{dir}/ANALYSIS.md"), render_analysis(s))?;
    fs::write(format!("{dir}/MANIFEST.md"), render_manifest(dir, s))?;
    eprintln!(
        "[perf_summarize] {} benches, {} regressions, {} improvements, {} inconclusive → {dir}/",
        s.benches.len(),
        s.regressions.len(),
        s.improvements.len(),
        s.inconclusive.len(),
    );
    Ok(())
}

fn render_summary_md(s: &Summary) -> String {
    let mut o = format!("# Perf Run {} — {}\n\n", s.timestamp, s.verdict);
    o.push_str(&format!(
        "| Field | Value |\n|-------|-------|\n\
         | Mode | {} |\n| Git | `{}` |\n| Rustc | {} |\n\
         | Threshold | ≥{}% (CI-based) |\n| Benches | {} total, {} regression(s), {} improvement(s), {} inconclusive, {} no-main |\n\n",
        s.mode,
        s.git_sha.chars().take(8).collect::<String>(),
        s.rustc_version,
        s.threshold_pct as u32,
        s.benches.len(),
        s.regressions.len(),
        s.improvements.len(),
        s.inconclusive.len(),
        s.benches.iter().filter(|b| b.status == "no_main").count(),
    ));

    // Baseline status section.
    if s.baseline_status == "no_meta" {
        o.push_str(
            "⚠️ **No baseline metadata.** Run `make bench-save` to stamp baseline context.\n\n",
        );
    } else {
        let status_icon = match s.baseline_status.as_str() {
            "ok" => "✅",
            _ => "⚠️",
        };
        o.push_str(&format!(
            "## Baseline\n\n| Field | Value |\n|-------|-------|\n\
             | Status | {status_icon} {} |\n",
            s.baseline_status,
        ));
        if let Some(ref meta) = s.baseline_meta {
            o.push_str(&format!(
                "| Saved at | {} |\n| SHA | `{}` |\n| Rustc | {} |\n| Bench count | {} |\n",
                meta.saved_at,
                meta.git_sha.chars().take(8).collect::<String>(),
                meta.rustc,
                meta.bench_count,
            ));
        }
        if let Some(h) = s.baseline_age_hours {
            o.push_str(&format!("| Age | {h:.1}h |\n"));
        }
        if let Some(n) = s.baseline_commits_behind {
            if n > 0 {
                o.push_str(&format!(
                    "| Commits behind | {n} — **drift likely if REGRESSIONS** |\n"
                ));
            } else {
                o.push_str("| Commits behind | 0 |\n");
            }
        }
        o.push('\n');
    }

    // Drift warning when REGRESSIONS + stale baseline.
    if s.verdict == "REGRESSIONS"
        && matches!(
            s.baseline_status.as_str(),
            "stale_sha" | "stale_age" | "stale_dirty"
        )
    {
        o.push_str("### ⚠️ Drift Warning\n\n");
        o.push_str("Baseline is stale (different SHA or >24h old). ");
        o.push_str("Regressions may reflect baseline drift, not code regression. ");
        o.push_str(
            "**Refresh baseline (`make bench-save`) before investigating code changes.**\n\n",
        );
    }

    if !s.regressions.is_empty() {
        o.push_str("## Regressions (CI lower bound ≥ threshold)\n\n");
        o.push_str(
            "| Bench | Group | Mean | Δ | CI | Why |\n|-------|-------|------|---|----|-----|\n",
        );
        for r in &s.regressions {
            if let Some(b) = s.benches.iter().find(|b| b.name == r.name) {
                let ci =
                    b.ci.map(|(lo, hi)| format!("[{lo:+.1}%, {hi:+.1}%]"))
                        .unwrap_or_else(|| "—".into());
                o.push_str(&format!(
                    "| {} | {} | {} | **+{:.1}%** | {} | {} |\n",
                    b.name.replace('_', " "),
                    b.group,
                    fmt_time(b.mean_ns),
                    r.change_pct,
                    ci,
                    b.verdict_reason,
                ));
            }
        }
        o.push('\n');
    }

    if !s.inconclusive.is_empty() {
        o.push_str("## Inconclusive (CI crosses threshold — needs more data)\n\n");
        o.push_str("| Bench | Δ | CI |\n|-------|---|----|\n");
        for ic in &s.inconclusive {
            if let Some(b) = s.benches.iter().find(|b| b.name == ic.name) {
                let ci =
                    b.ci.map(|(lo, hi)| format!("[{lo:+.1}%, {hi:+.1}%]"))
                        .unwrap_or_else(|| "—".into());
                o.push_str(&format!(
                    "| {} | {:+.1}% | {} |\n",
                    b.name.replace('_', " "),
                    ic.change_pct,
                    ci,
                ));
            }
        }
        o.push('\n');
    }

    if !s.improvements.is_empty() {
        o.push_str("## Top Improvements\n\n");
        o.push_str("| Bench | Group | Mean | Δ |\n|-------|-------|------|---|\n");
        for imp in &s.improvements {
            if let Some(b) = s.benches.iter().find(|b| b.name == imp.name) {
                o.push_str(&format!(
                    "| {} | {} | {} | {:+.1}% |\n",
                    b.name.replace('_', " "),
                    b.group,
                    fmt_time(b.mean_ns),
                    imp.change_pct,
                ));
            }
        }
        o.push('\n');
    }

    if s.regressions.is_empty() && s.improvements.is_empty() && s.inconclusive.is_empty() {
        o.push_str("## All Benches\n\n");
        o.push_str("| Bench | Group | Mean | Δ vs main | Status |\n|-------|-------|------|-----------|--------|\n");
        for b in &s.benches {
            let delta = b
                .change_pct
                .map(|p| format!("{p:+.1}%"))
                .unwrap_or_else(|| "—".into());
            o.push_str(&format!(
                "| {} | {} | {} | {} | {} |\n",
                b.name.replace('_', " "),
                b.group,
                fmt_time(b.mean_ns),
                delta,
                b.status,
            ));
        }
        o.push('\n');
    }

    o.push_str("## Artifacts\n\n");
    o.push_str("- `summary.json` — machine-readable (agents parse this)\n");
    o.push_str("- `report_criterion.json` — test_infra-compatible (index.html)\n");
    o.push_str("- `SUMMARY.md` — this file (human)\n");
    o.push_str("- `ANALYSIS.md` — narrative: what happened + next steps\n");
    o.push_str("- `MANIFEST.md` — what each file is\n");
    o.push_str("- `PLAN.md` — optimization plan (written by agent after analyze)\n\n");
    o.push_str("- `env.json` — run context (git SHA, mode, baseline meta copy)\n");
    o.push_str("- `baseline_meta.json` — copy of `target/criterion/.baseline_meta.json`\n");

    match s.verdict.as_str() {
        "PASS" => o.push_str("**No regressions detected.** Ready to merge.\n"),
        "NO_BASELINE" => {
            o.push_str("**No main baseline found.** Run `make bench-save` once, then re-run.\n")
        }
        "REGRESSIONS" => o.push_str("**Regressions detected.** Review `PLAN.md` before merging.\n"),
        other => o.push_str(&format!("**Verdict: {other}.**\n")),
    }

    o
}

// ---------------------------------------------------------------------------
// Narrative analysis (always generated — the "what happened" explanation)
// ---------------------------------------------------------------------------

fn render_analysis(s: &Summary) -> String {
    let mut o = String::new();
    let sha8: String = s.git_sha.chars().take(8).collect();

    // 1. What ran
    o.push_str(&format!(
        "# Performance Analysis — {}\n\n\
         **Run:** {} mode | Git `{}` | Threshold ≥{}%\n\n",
        s.verdict, s.mode, sha8, s.threshold_pct as u32,
    ));

    // 2. Verdict in plain English
    match s.verdict.as_str() {
        "PASS" => {
            o.push_str(
                "**No CI-bound regressions detected.** All measured changes \
                 are within the noise band (CI crosses zero) or below the \
                 threshold. This code change is safe from a performance \
                 perspective.\n\n",
            );
        }
        "REGRESSIONS" => {
            let n = s.regressions.len();
            o.push_str(&format!(
                "**{n} regression{} detected** with 95% CI lower bound ≥ +{}%. ",
                if n == 1 { "" } else { "s" },
                s.threshold_pct as u32,
            ));
            if matches!(
                s.baseline_status.as_str(),
                "stale_sha" | "stale_age" | "stale_dirty"
            ) {
                o.push_str(
                    "⚠️ Baseline is stale — **drift is the first hypothesis.** \
                     Refresh baseline (`make bench-save`) and re-run before \
                     investigating code.\n\n",
                );
            } else {
                o.push_str(
                    "Baseline is fresh — regressions likely reflect real \
                     code changes.\n\n",
                );
            }
        }
        "NO_BASELINE" => {
            o.push_str(
                "**No baseline found.** Run `make bench-save` to create one, \
                 then re-run perf-tuning to get actionable results.\n\n",
            );
        }
        other => {
            o.push_str(&format!("**Verdict: {other}.**\n\n"));
        }
    }

    // 3. Baseline health
    if s.baseline_status != "no_meta" {
        o.push_str("## Baseline Health\n\n");
        let icon = match s.baseline_status.as_str() {
            "ok" => "✅",
            _ => "⚠️",
        };
        o.push_str(&format!("- Status: {icon} `{}`\n", s.baseline_status));
        if let Some(ref meta) = s.baseline_meta {
            o.push_str(&format!(
                "- Saved: {} (sha `{}`)\n",
                meta.saved_at,
                meta.git_sha.chars().take(8).collect::<String>(),
            ));
            o.push_str(&format!("- Benchmarks: {}\n", meta.bench_count));
        }
        if let Some(h) = s.baseline_age_hours {
            o.push_str(&format!("- Age: {h:.1}h\n"));
        }
        if let Some(n) = s.baseline_commits_behind {
            if n > 0 {
                o.push_str(&format!("- Commits behind: {n} ⚠️ **drift likely**\n"));
            }
        }
        o.push('\n');
    }

    // 4. Run-over-run trend (vs most recent prior published run)
    if let Some(ref trend) = s.trend {
        if !trend.is_empty() {
            o.push_str("## Trend (vs most recent prior run)\n\n");
            o.push_str("| Bench | Prior mean | Current mean | Δ |\n");
            o.push_str("|-------|-----------:|-------------:|--:|\n");
            for t in trend {
                o.push_str(&format!(
                    "| {} | {} | {} | {:+.1}% |\n",
                    t.name.replace('_', " "),
                    fmt_time(t.prev_mean_ns),
                    fmt_time(t.new_mean_ns),
                    t.delta_pct,
                ));
            }
            o.push('\n');
        }
    }
    if let Some(ref chronic) = s.chronic_regressions {
        if !chronic.is_empty() {
            o.push_str("### ⚠️ Chronic regressions (flagged in prior runs too)\n\n");
            for c in chronic {
                o.push_str(&format!(
                    "- **{}** — regressed in {} consecutive runs, last +{:.1}%\n",
                    c.name.replace('_', " "),
                    c.consecutive_runs,
                    c.last_pct,
                ));
            }
            o.push('\n');
        }
    }

    // 5. Per-group deep dive (ALL benches per group, sorted)
    for group in &["core_ops", "cascade_scope"] {
        let group_benches: Vec<&BenchEntry> =
            s.benches.iter().filter(|b| b.group == *group).collect();
        if group_benches.is_empty() {
            continue;
        }
        o.push_str(&format!("## {group} benches (full)\n\n"));
        o.push_str("| Bench | Mean | Δ | CI | Status |\n");
        o.push_str("|-------|-----:|--:|----|--------|\n");
        for b in &group_benches {
            let delta = b
                .change_pct
                .map(|p| format!("{p:+.1}%"))
                .unwrap_or_else(|| "—".into());
            let ci =
                b.ci.map(|(lo, hi)| format!("[{lo:+.1}%, {hi:+.1}%]"))
                    .unwrap_or_else(|| "—".into());
            o.push_str(&format!(
                "| {} | {} | {} | {} | {} |\n",
                b.name.replace('_', " "),
                fmt_time(b.mean_ns),
                delta,
                ci,
                b.status,
            ));
        }
        o.push('\n');
    }

    // 6. Regressions detail with hot-path + repro + rule hints
    if !s.regressions.is_empty() {
        o.push_str("## Regressions (require investigation)\n\n");
        for r in &s.regressions {
            let ci_str = s
                .benches
                .iter()
                .find(|b| b.name == r.name)
                .and_then(|b| b.ci)
                .map(|(lo, hi)| format!("[{lo:+.1}%, {hi:+.1}%]"))
                .unwrap_or_else(|| "—".into());
            let reason = s
                .benches
                .iter()
                .find(|b| b.name == r.name)
                .map(|b| b.verdict_reason.as_str())
                .unwrap_or("");
            o.push_str(&format!(
                "### {} — +{:.1}% CI {}\n\n{}\n\n",
                r.name.replace('_', " "),
                r.change_pct,
                ci_str,
                reason,
            ));
            if let Some((path, rules)) = hot_path_for(&r.name) {
                o.push_str(&format!("- **Hot path:** `{path}`\n"));
                o.push_str(&format!("- **Rule hints:** {rules}\n"));
            }
            o.push_str(&format!(
                "- **Repro:** `cargo bench --bench {} -- --bench \"{}\" 2>&1 | tail -30`\n",
                if r.name.starts_with("fuse_") || r.name.starts_with("scope_") {
                    "cascade_scope"
                } else {
                    "core_ops"
                },
                r.name,
            ));
        }
        o.push('\n');
    }

    // 7. Cache effectiveness (hitrate family)
    if let Some(ref h) = s.hitrate {
        o.push_str("## Cache effectiveness (hit-rate benchmark)\n\n");
        let icon = match h.verdict.as_str() {
            "PASS" => "✅",
            "FAIL_CORE" => "🚨",
            "FAIL_TRUST" => "⚠️",
            _ => "•",
        };
        o.push_str(&format!("- Verdict: {icon} `{}`\n", h.verdict,));
        if let (Some(best), Some(gate)) = (h.agentic_best, h.agentic_gate) {
            let mark = if best >= gate { "✅" } else { "🚨" };
            o.push_str(&format!(
                "- Agentic exact HR: {mark} {:.1}% (gate {:.0}%)\n",
                best * 100.0,
                gate * 100.0,
            ));
        }
        if !h.workloads.is_empty() {
            o.push_str("\n| Workload | Best exact | Best combined (τ) | False-hit |\n");
            o.push_str("|----------|-----------:|-------------------:|----------:|\n");
            for w in &h.workloads {
                let exact = w
                    .best_exact_hr
                    .map(|v| format!("{:.1}%", v * 100.0))
                    .unwrap_or_else(|| "—".into());
                let combined = match (w.best_combined_hr, w.best_tau) {
                    (Some(c), Some(t)) => format!("{:.1}% (τ={:.2})", c * 100.0, t),
                    (Some(c), None) => format!("{:.1}%", c * 100.0),
                    _ => "—".into(),
                };
                let fh = w
                    .false_hit_rate
                    .map(|v| format!("{:.2}%", v * 100.0))
                    .unwrap_or_else(|| "—".into());
                o.push_str(&format!(
                    "| {} | {} | {} | {} |\n",
                    w.name, exact, combined, fh,
                ));
            }
            o.push('\n');
        }
        if h.verdict == "FAIL_CORE" {
            o.push_str(
                "🚨 **FAIL-CORE**: agentic exact hit rate below 40% — cache is not \
                 paying for itself in the agent workload. Investigate cache key \
                 (slugify, normalize_query) and TTL.\n\n",
            );
        }
    }

    // 8. Metrics deltas (Prometheus counters that moved)
    if let Some(ref m) = s.metrics {
        if let Some(serde_json::Value::Object(deltas)) = m.get("deltas") {
            if !deltas.is_empty() {
                o.push_str("## Metrics (counter deltas across the run)\n\n");
                o.push_str("| Metric | Delta |\n|--------|------:|\n");
                let mut entries: Vec<_> = deltas.iter().collect();
                entries.sort_by(|a, b| a.0.cmp(b.0));
                for (k, v) in entries {
                    let d = v.get("delta").and_then(|x| x.as_f64()).unwrap_or(0.0);
                    o.push_str(&format!("| `{}` | {:+.0} |\n", k, d));
                }
                o.push('\n');
            }
        }
    }

    // 9. Diagnostics status (what profiling artifacts exist)
    o.push_str("## Diagnostics\n\n");
    o.push_str(&format!(
        "- Flamegraph: {}\n",
        if s.diagnostics.flamegraph {
            "✅ present"
        } else {
            "❌ not produced"
        },
    ));
    o.push_str(&format!(
        "- Heaptrack: {}\n",
        if s.diagnostics.heaptrack {
            "✅ present"
        } else {
            "❌ not produced"
        },
    ));
    o.push_str(&format!(
        "- Tokio console: {}\n",
        if s.diagnostics.tokio_console {
            "✅ snap loaded (per-task)"
        } else {
            "❌ not produced (set TOKIO_CONSOLE=1)"
        },
    ));
    o.push_str(&format!(
        "- Tokio RuntimeMetrics: {}\n",
        if s.diagnostics.tokio_metrics {
            "✅ snapshot loaded (aggregates)"
        } else {
            "❌ not loaded (always-on /debug/tokio endpoint)"
        },
    ));
    o.push_str(&format!(
        "- Tokio Handle::dump(): {}\n",
        if s.diagnostics.tokio_dump {
            "✅ dump loaded (task backtraces)"
        } else {
            "❌ not loaded (needs RUSTFLAGS=--cfg tokio_unstable + tokio-console build)"
        },
    ));
    o.push_str(&format!(
        "- Hitrate bench: {}\n\n",
        if s.diagnostics.hitrate {
            "✅ present"
        } else {
            "❌ not run (perf-tuning-full default: HITRATE=1)"
        },
    ));
    if s.diagnostics.flamegraph {
        o.push_str("- Open flamegraph: `xdg-open flamegraph.svg` OR upload to https://profiler.firefox.com/\n");
    }

    // 10. Tokio runtime (console_snap top tasks)
    if let Some(ref t) = s.tokio {
        o.push_str("## Tokio Runtime (top tasks)\n\n");
        o.push_str(&format!("Sampled {}s.\n\n", t.sampled_secs as u32));
        if let Some(ref note) = t.note {
            o.push_str(&format!("_{note}_\n\n"));
        }
        if !t.top_tasks.is_empty() {
            o.push_str("| Task | Total poll | Polls | Avg poll | Busy ratio |\n");
            o.push_str("|------|-----------:|------:|---------:|-----------:|\n");
            for task in t.top_tasks.iter().take(5) {
                let busy = task
                    .busy_ratio
                    .map(|b| format!("{:.0}%", b * 100.0))
                    .unwrap_or_else(|| "—".into());
                o.push_str(&format!(
                    "| {} | {:.1}ms | {} | {:.1}µs | {} |\n",
                    task.name, task.total_poll_ms, task.poll_count, task.avg_poll_us, busy,
                ));
            }
            o.push('\n');
            // Interpretation: if any single task is >50% of total poll time,
            // it's dominating. Recommend investigating.
            let total: f64 = t.top_tasks.iter().map(|x| x.total_poll_ms).sum();
            if total > 0.0 {
                if let Some(top) = t.top_tasks.first() {
                    let share = top.total_poll_ms / total;
                    if share > 0.5 {
                        o.push_str(&format!(
                            "⚠️ `{}` is consuming {:.0}% of poll time — investigate\n\n",
                            top.name,
                            share * 100.0,
                        ));
                    }
                }
            }
        }
    }

    // 10b. Tokio RuntimeMetrics (B — always-on aggregates, no console stack)
    if let Some(ref m) = s.tokio_metrics {
        o.push_str("## Tokio RuntimeMetrics (B — aggregates)\n\n");
        o.push_str("Snapshot from `GET /debug/tokio` (Handle::current().metrics()). ");
        o.push_str("Always-on, no console stack required. ");
        o.push_str("Per-task detail requires section 10 above (TOKIO_CONSOLE=1).\n\n");
        let rows: Vec<(&str, String)> = vec![
            (
                "Alive tasks",
                m.num_alive_tasks
                    .map(|n| n.to_string())
                    .unwrap_or_else(|| "—".into()),
            ),
            (
                "Workers",
                m.num_workers
                    .map(|n| n.to_string())
                    .unwrap_or_else(|| "—".into()),
            ),
            (
                "Global queue depth",
                m.global_queue_depth
                    .map(|n| n.to_string())
                    .unwrap_or_else(|| "—".into()),
            ),
            (
                "Worker total busy (sum)",
                m.worker_total_busy_duration_ns
                    .map(format_ns)
                    .unwrap_or_else(|| "—".into()),
            ),
            (
                "Worker total polls",
                m.worker_poll_count
                    .map(|n| n.to_string())
                    .unwrap_or_else(|| "—".into()),
            ),
            (
                "Worker mean poll",
                m.worker_mean_poll_time_ns
                    .map(format_ns)
                    .unwrap_or_else(|| "—".into()),
            ),
            (
                "Budget forced yields",
                m.budget_forced_yield_count
                    .map(|n| n.to_string())
                    .unwrap_or_else(|| "—".into()),
            ),
        ];
        o.push_str("| Metric | Value |\n|---|---|\n");
        for (k, v) in &rows {
            o.push_str(&format!("| {k} | {v} |\n"));
        }
        // Highlight missing unstable fields
        let missing_unstable = [
            ("worker_poll_count", m.worker_poll_count.is_none()),
            (
                "worker_mean_poll_time_ns",
                m.worker_mean_poll_time_ns.is_none(),
            ),
            (
                "budget_forced_yield_count",
                m.budget_forced_yield_count.is_none(),
            ),
        ]
        .iter()
        .filter(|(_, m)| *m)
        .map(|(k, _)| *k)
        .collect::<Vec<_>>()
        .join(", ");
        if !missing_unstable.is_empty() {
            o.push_str(&format!(
                "\n_Note: {missing_unstable} require a `RUSTFLAGS=\"--cfg tokio_unstable\"` build. _\n",
            ));
        }
        o.push('\n');
    }

    // 10c. Tokio Handle::dump() (C — stuck-task backtraces)
    if let Some(ref d) = s.tokio_dump {
        o.push_str("## Tokio Handle::dump() (C — task backtraces)\n\n");
        if d.available {
            o.push_str(&format!("Dump file: `{}`\n\n", d.path));
            o.push_str("Use when tasks are stuck or contention is suspected. Open in any text editor or `less -R`.\n\n");
            // Show a brief preview (first ~30 lines) so the report is useful
            // even without the full file open.
            let preview: String = d.text.lines().take(30).collect::<Vec<_>>().join("\n");
            if !preview.is_empty() {
                o.push_str("<details><summary>Preview (first 30 lines)</summary>\n\n```\n");
                o.push_str(&preview);
                o.push_str("\n```\n\n</details>\n\n");
            }
        } else if let Some(ref hint) = d.hint {
            o.push_str("Endpoint returned 503 / not available. Build with:\n\n");
            o.push_str(&format!("> {hint}\n\n"));
        } else {
            o.push_str("Endpoint unreachable.\n\n");
        }
    }

    // 11. History (last 5 published runs)
    if let Some(ref h) = s.history {
        if !h.is_empty() {
            o.push_str("## History (last 5 published runs)\n\n");
            o.push_str("| Timestamp | SHA | Verdict | Top mover |\n|-----------|-----|---------|-----------|\n");
            for entry in h {
                let tm = match (entry.top_mover.as_ref(), entry.top_mover_pct) {
                    (Some(n), Some(p)) => format!("{} {:+.1}%", n.replace('_', " "), p),
                    (Some(n), None) => n.replace('_', " "),
                    _ => "—".into(),
                };
                o.push_str(&format!(
                    "| {} | `{}` | `{}` | {} |\n",
                    entry.timestamp, entry.sha, entry.verdict, tm,
                ));
            }
            o.push('\n');
        }
    }

    // 12. Rule-based recommendations
    if !s.regressions.is_empty() {
        o.push_str("## Recommendations\n\n");
        for r in &s.regressions {
            if let Some((path, rules)) = hot_path_for(&r.name) {
                o.push_str(&format!(
                    "- **{}** → inspect `{path}` ({rules})\n",
                    r.name.replace('_', " "),
                ));
            } else {
                o.push_str(&format!(
                    "- **{}** → no hot-path entry; run `make profile-flamegraph` and re-investigate\n",
                    r.name.replace('_', " "),
                ));
            }
        }
        o.push('\n');
    }

    // 13. Noise note
    if !s.inconclusive.is_empty() {
        o.push_str(&format!(
            "**Note:** {} bench{} showed raw change above threshold but the \
             95% CI crosses zero — this is statistical noise, not a real \
             regression. Do not act on inconclusive results; re-run with \
             more iterations if the signal persists.\n\n",
            s.inconclusive.len(),
            if s.inconclusive.len() == 1 { "" } else { "es" },
        ));
    }

    // 14. Next steps (branch on verdict)
    o.push_str("## Next Steps\n\n");
    match s.verdict.as_str() {
        "PASS" => {
            o.push_str("1. ✅ Safe to merge — no performance regressions\n");
            o.push_str(
                "2. Optional: run `make perf-tuning-default` or `full` for deeper profiling\n",
            );
            o.push_str("3. Publish: `make perf-publish` (commits evidence to `perf-history/`)\n");
        }
        "REGRESSIONS" => {
            if matches!(
                s.baseline_status.as_str(),
                "stale_sha" | "stale_age" | "stale_dirty"
            ) {
                o.push_str("1. **Refresh baseline:** `make bench-save` (verify clean tree)\n");
                o.push_str("2. **Re-run:** `make perf-tuning-quick`\n");
                o.push_str("3. If still REGRESSIONS → investigate code changes listed above\n");
            } else {
                o.push_str("1. **Investigate** the regression(s) listed above\n");
                o.push_str("2. Use hot-path map above as starting point\n");
                o.push_str(
                    "3. `make profile-flamegraph` or `make profile-dhat` for deeper diagnosis\n",
                );
            }
        }
        "NO_BASELINE" => {
            o.push_str(
                "1. **Create baseline:** `make bench-save` (clean tree, no uncommitted changes)\n",
            );
            o.push_str("2. **Re-run:** `make perf-tuning-quick`\n");
        }
        _ => {}
    }

    o
}

fn render_manifest(run_dir: &str, s: &Summary) -> String {
    let mut o = format!(
        "# MANIFEST — perf run {}\n\n\
         | File | Purpose | Who uses |\n|------|---------|----------|\n\
         | `summary.json` | Structured bench data + CI-aware verdicts | Agents (parse) |\n\
         | `report_criterion.json` | Improvements/regressions/inconclusive list | test_infra index |\n\
         | `SUMMARY.md` | Human scorecard | Humans (review) |\n\
         | `ANALYSIS.md` | Narrative analysis (what happened + next steps) | Humans + agents |\n\
         | `MANIFEST.md` | This file | Orientation |\n",
        s.timestamp,
    );
    let fg = Path::new(run_dir).join("flamegraph.svg");
    if fg.exists() {
        o.push_str("| `flamegraph.svg` | CPU flamegraph (inferno) | Humans (visual) |\n");
    }
    let fg_json = Path::new(run_dir).join("profile.json.gz");
    if fg_json.exists() {
        o.push_str(
            "| `profile.json.gz` | Firefox Profiler profile (samply) — open at https://profiler.firefox.com |\n",
        );
    }
    let metrics = Path::new(run_dir).join("metrics-prometheus.txt");
    if metrics.exists() {
        o.push_str(
            "| `metrics-prometheus.txt` | Prometheus scrape (after workload) | Humans/agents |\n",
        );
    }
    let metrics_before = Path::new(run_dir).join("metrics-before.txt");
    if metrics_before.exists() {
        o.push_str(
            "| `metrics-before.txt` | Prometheus scrape (before workload) — enables deltas |\n",
        );
    }
    if s.metrics.is_some() {
        o.push_str("| `summary.json:metrics` | Parsed headline counters | Agents (parse) |\n");
    }
    o.push_str("\n## Schema\n\n");
    o.push_str("`summary.json` schema v1 — see `src/bin/perf_summarize.rs`.\n");
    o.push_str("Verdict logic: regression iff `ci.lower_bound ≥ +threshold`;\n");
    o.push_str("improvement iff `ci.upper_bound ≤ −threshold`; else `inconclusive` or `ok`.\n");
    o
}

// ---------------------------------------------------------------------------
// Metrics parsing (best-effort)
// ---------------------------------------------------------------------------

/// Parse Prometheus text into a JSON object with sample counters.
/// Cheap heuristic: collect `# TYPE name counter` headers + numeric samples.
fn parse_prom_text(text: &str) -> serde_json::Value {
    let mut out = serde_json::Map::new();
    let mut current_type: Option<(String, String)> = None;
    for line in text.lines() {
        let line = line.trim();
        if line.is_empty() {
            continue;
        }
        if let Some(rest) = line.strip_prefix("# TYPE ") {
            let mut it = rest.split_whitespace();
            let name = it.next().unwrap_or("").to_string();
            let ty = it.next().unwrap_or("").to_string();
            current_type = Some((name, ty));
            continue;
        }
        if line.starts_with('#') {
            continue;
        }
        // Sample: metric_name{labels} value [timestamp]
        let (key, value_str) = match line.find(' ') {
            Some(idx) => (&line[..idx], &line[idx + 1..]),
            None => continue,
        };
        let value: f64 = match value_str.split_whitespace().next().unwrap_or("").parse() {
            Ok(v) => v,
            Err(_) => continue,
        };
        // Build entry
        let entry = out
            .entry(key.to_string())
            .or_insert_with(|| serde_json::Value::Object(serde_json::Map::new()));
        if let serde_json::Value::Object(map) = entry {
            let kind = current_type
                .as_ref()
                .filter(|(n, _)| n == key)
                .map(|(_, t)| t.clone());
            map.insert("value".into(), serde_json::json!(value));
            if let Some(k) = kind {
                map.insert("type".into(), serde_json::json!(k));
            }
        }
    }
    serde_json::Value::Object(out)
}

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

fn format_ns(ns: u64) -> String {
    if ns >= 1_000_000_000 {
        format!("{:.1}s", ns as f64 / 1e9)
    } else if ns >= 1_000_000 {
        format!("{:.1}ms", ns as f64 / 1e6)
    } else if ns >= 1_000 {
        format!("{:.1}µs", ns as f64 / 1e3)
    } else {
        format!("{ns}ns")
    }
}

fn git_sha() -> Option<String> {
    let out = Command::new("git")
        .args(["rev-parse", "--short", "HEAD"])
        .output()
        .ok()?;
    if out.status.success() {
        Some(String::from_utf8_lossy(&out.stdout).trim().to_string())
    } else {
        None
    }
}

fn rustc_version() -> Option<String> {
    let out = Command::new("rustc").arg("--version").output().ok()?;
    if out.status.success() {
        Some(String::from_utf8_lossy(&out.stdout).trim().to_string())
    } else {
        None
    }
}

fn chrono_timestamp() -> String {
    let out = Command::new("date").arg("+%Y%m%d-%H%M%S").output().ok();
    match out {
        Some(o) if o.status.success() => String::from_utf8_lossy(&o.stdout).trim().to_string(),
        _ => "unknown".into(),
    }
}

/// Parse ISO 8601 timestamp (2024-01-15T10:30:00Z) and return hours elapsed.
fn parse_iso8601_hours(ts: &str) -> Option<f64> {
    // Format: "2024-01-15T10:30:00Z"
    let now_secs = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .ok()?
        .as_secs();
    let s = ts.trim_end_matches('Z');
    let year: i64 = s[0..4].parse().ok()?;
    let month: i64 = s[5..7].parse().ok()?;
    let day: i64 = s[8..10].parse().ok()?;
    let hour: i64 = s[11..13].parse().ok()?;
    let min: i64 = s[14..16].parse().ok()?;
    let sec: i64 = s.get(17..19).and_then(|v| v.parse().ok()).unwrap_or(0);
    // Days from civil date to Unix epoch (Howard Hinnant's algorithm, public domain).
    let y = year - (month <= 2) as i64;
    let m = month + (if month <= 2 { 12 } else { 0 });
    let days = 365 * y + y / 4 - y / 100 + y / 400 + (153 * (m - 3) + 2) / 5 + day - 719468;
    let stamp_secs = (days * 86400 + hour * 3600 + min * 60 + sec) as u64;
    Some((now_secs.saturating_sub(stamp_secs)) as f64 / 3600.0)
}

// ---------------------------------------------------------------------------
// Main
// ---------------------------------------------------------------------------

fn run() -> Result<i32, Box<dyn std::error::Error>> {
    let args = parse_args().map_err(|e| -> Box<dyn std::error::Error> { e.into() })?;

    let results_path = args
        .results_dir
        .as_deref()
        .ok_or("--results-dir required")?;
    fs::create_dir_all(results_path)?;

    let crit_base = args.criterion_dir.as_deref().unwrap_or("target/criterion");
    let crit_path = Path::new(crit_base);

    // Read baseline metadata (written by `make bench-save`).
    let baseline_meta_path = crit_path.join(".baseline_meta.json");
    let baseline_meta: Option<BaselineMeta> = fs::read_to_string(&baseline_meta_path)
        .ok()
        .and_then(|s| serde_json::from_str(&s).ok());

    // Compute baseline status + age + commits behind.
    let (baseline_status, baseline_age_hours, baseline_commits_behind): (
        String,
        Option<f64>,
        Option<u32>,
    ) = if let Some(ref meta) = baseline_meta {
        let current_sha = git_sha().unwrap_or_default();
        let meta_sha_short: String = meta.git_sha.chars().take(8).collect();
        let age_hours = parse_iso8601_hours(&meta.saved_at);
        let commits_behind = if current_sha.starts_with(&meta_sha_short) {
            Some(0u32)
        } else {
            // Count commits between meta SHA and HEAD.
            Command::new("git")
                .args(["rev-list", "--count", &format!("{}..HEAD", meta.git_sha)])
                .output()
                .ok()
                .and_then(|o| {
                    String::from_utf8_lossy(&o.stdout)
                        .trim()
                        .parse::<u32>()
                        .ok()
                })
        };
        let status = if meta.git_dirty {
            "stale_dirty"
        } else if commits_behind.unwrap_or(0) > 0 {
            "stale_sha"
        } else if age_hours.unwrap_or(f64::INFINITY) > 24.0 {
            "stale_age"
        } else {
            "ok"
        };
        (status.into(), age_hours, commits_behind)
    } else {
        ("no_meta".into(), None, None)
    };

    let since = args
        .since
        .as_deref()
        .and_then(|p| fs::metadata(p).ok())
        .and_then(|m| m.modified().ok());

    let mut benches: Vec<BenchEntry> = Vec::new();
    let mut regressions: Vec<ChangeEntry> = Vec::new();
    let mut improvements: Vec<ChangeEntry> = Vec::new();
    let mut inconclusive: Vec<ChangeEntry> = Vec::new();

    if crit_path.is_dir() {
        for (name, new_path, change_path, main_path) in collect_benches(crit_path, since)? {
            let raw = match fs::read_to_string(&new_path) {
                Ok(s) => s,
                Err(e) => {
                    eprintln!("[perf_summarize] skip {name}: {e}");
                    continue;
                }
            };
            let new_est: Est = match serde_json::from_str(&raw) {
                Ok(v) => v,
                Err(e) => {
                    eprintln!("[perf_summarize] skip {name}: {e}");
                    continue;
                }
            };
            let mean_ns = new_est.mean.point_estimate;
            let group = bench_group(&name);

            let r = evaluate(
                name.clone(),
                group.clone(),
                mean_ns,
                new_est.mean.standard_error,
                change_path,
                main_path,
                args.threshold,
            )?;
            let entry = BenchEntry {
                name: r.name.clone(),
                group: r.group,
                mean_ns: r.mean_ns,
                change_pct: r.change_pct,
                ci: r.ci,
                has_main: r.has_main,
                status: r.status.clone(),
                verdict_reason: r.verdict_reason,
            };
            match r.status.as_str() {
                "regression" => regressions.push(ChangeEntry {
                    name: r.name,
                    change_pct: r.change_pct.unwrap_or(0.0),
                    ci: r.ci,
                }),
                "improvement" => improvements.push(ChangeEntry {
                    name: r.name,
                    change_pct: r.change_pct.unwrap_or(0.0),
                    ci: r.ci,
                }),
                "inconclusive" => inconclusive.push(ChangeEntry {
                    name: r.name,
                    change_pct: r.change_pct.unwrap_or(0.0),
                    ci: r.ci,
                }),
                _ => {}
            }
            benches.push(entry);
        }
    } else {
        eprintln!("No criterion output at {crit_base}");
    }

    benches.sort_by(|a, b| {
        let rank = |s: &str| match s {
            "regression" => 0,
            "inconclusive" => 1,
            "ok" => 2,
            "no_main" => 3,
            "improvement" => 4,
            _ => 5,
        };
        rank(&a.status).cmp(&rank(&b.status)).then_with(|| {
            b.change_pct
                .unwrap_or(0.0)
                .partial_cmp(&a.change_pct.unwrap_or(0.0))
                .unwrap_or(std::cmp::Ordering::Equal)
        })
    });
    regressions.sort_by(|a, b| {
        b.change_pct
            .partial_cmp(&a.change_pct)
            .unwrap_or(std::cmp::Ordering::Equal)
    });
    improvements.sort_by(|a, b| {
        a.change_pct
            .partial_cmp(&b.change_pct)
            .unwrap_or(std::cmp::Ordering::Equal)
    });

    let verdict = if !regressions.is_empty() {
        "REGRESSIONS"
    } else if benches.is_empty() || benches.iter().all(|b| !b.has_main && b.status == "no_main") {
        "NO_BASELINE"
    } else {
        "PASS"
    };

    let metrics = args.metrics_file.as_deref().and_then(|p| {
        let path = Path::new(p);
        if !path.is_file() {
            return None;
        }
        let text = fs::read_to_string(path).ok()?;
        let after = parse_prom_text(&text);
        // Compute deltas vs `--metrics-before-file` (counter-only). For each
        // metric present in both files with `type == "counter"`, emit a
        // `delta = after - before` entry. Gauges are left as after-only.
        let deltas = match args.metrics_before_file.as_deref() {
            Some(bp) => {
                let bp = Path::new(bp);
                if bp.is_file() {
                    let before_text = fs::read_to_string(bp).ok()?;
                    let before = parse_prom_text(&before_text);
                    let mut d = serde_json::Map::new();
                    if let (
                        serde_json::Value::Object(after_map),
                        serde_json::Value::Object(before_map),
                    ) = (&after, &before)
                    {
                        for (k, av) in after_map {
                            let is_counter =
                                av.get("type").and_then(|t| t.as_str()) == Some("counter");
                            if let Some(bv) = before_map.get(k) {
                                let a = av.get("value").and_then(|v| v.as_f64()).unwrap_or(0.0);
                                let b = bv.get("value").and_then(|v| v.as_f64()).unwrap_or(0.0);
                                if is_counter {
                                    d.insert(k.clone(), serde_json::json!({ "delta": a - b }));
                                }
                            }
                        }
                    }
                    Some(serde_json::Value::Object(d))
                } else {
                    None
                }
            }
            None => None,
        };
        let mut m = serde_json::Map::new();
        m.insert("after".into(), after);
        if let Some(d) = deltas {
            m.insert("deltas".into(), d);
        }
        Some(serde_json::Value::Object(m))
    });

    // ---- v2 wiring: load hitrate / history / tokio + compute trend/chronic ----
    let rd = Path::new(results_path);
    let hitrate = args
        .hitrate_summary
        .as_deref()
        .and_then(load_hitrate_summary);
    let tokio = args.tokio_snap.as_deref().and_then(load_tokio_snap);
    let tokio_metrics = args.tokio_metrics.as_deref().and_then(load_tokio_metrics);
    let tokio_dump = args.tokio_dump.as_deref().map(load_tokio_dump);
    let current_sha = git_sha().unwrap_or_else(|| "unknown".into());
    let history: Option<Vec<HistoryEntry>> = args.history_dir.as_ref().map(|d| {
        let mut h = load_history(d, &current_sha);
        h.truncate(5);
        h
    });
    let trend: Option<Vec<TrendPoint>> = if args.history_dir.is_some() {
        let mut t = compute_trend(
            &benches,
            &[],
            args.history_dir.as_deref().unwrap_or(""),
            &current_sha,
        );
        // Sort by |delta| descending so the report leads with the biggest moves.
        t.sort_by(|a, b| {
            b.delta_pct
                .abs()
                .partial_cmp(&a.delta_pct.abs())
                .unwrap_or(std::cmp::Ordering::Equal)
        });
        t.truncate(5);
        Some(t)
    } else {
        None
    };
    let chronic_regressions: Option<Vec<ChronicRegression>> = if args.history_dir.is_some() {
        let c = compute_chronic_regressions(
            &benches,
            args.history_dir.as_deref().unwrap_or(""),
            &current_sha,
        );
        if c.is_empty() {
            None
        } else {
            Some(c)
        }
    } else {
        None
    };
    let diagnostics = Diagnostics {
        flamegraph: rd.join("flamegraph.svg").is_file() || rd.join("profile.json.gz").is_file(),
        heaptrack: rd.join("heaptrack.txt").is_file(),
        tokio_console: tokio.is_some(),
        tokio_metrics: tokio_metrics.is_some(),
        tokio_dump: tokio_dump.as_ref().is_some_and(|d| d.available),
        hitrate: hitrate.is_some(),
    };

    let summary = Summary {
        schema_version: 2,
        timestamp: chrono_timestamp(),
        mode: args.mode.clone(),
        threshold_pct: args.threshold,
        verdict: verdict.into(),
        git_sha: current_sha.clone(),
        rustc_version: rustc_version().unwrap_or_else(|| "unknown".into()),
        benches,
        regressions: regressions.clone(),
        improvements: improvements.clone(),
        inconclusive: inconclusive.clone(),
        metrics,
        artifacts: Artifacts {
            summary_md: "SUMMARY.md".into(),
            report_criterion: "report_criterion.json".into(),
            analysis_md: "ANALYSIS.md".into(),
        },
        baseline_meta: baseline_meta.clone(),
        baseline_status: baseline_status.clone(),
        baseline_age_hours,
        baseline_commits_behind,
        hitrate,
        diagnostics,
        tokio,
        tokio_metrics,
        tokio_dump,
        trend,
        chronic_regressions,
        history,
    };

    write_outputs(results_path, &summary)?;

    // Print ANALYSIS.md to stdout so the agent (and Make) see it directly.
    let analysis = render_analysis(&summary);
    println!("\n{analysis}");

    // Always write env.json (machine-readable run context).
    let env = EnvJson {
        git_sha: git_sha().unwrap_or_else(|| "unknown".into()),
        rustc_version: rustc_version().unwrap_or_else(|| "unknown".into()),
        mode: args.mode.clone(),
        threshold_pct: args.threshold,
        timestamp: chrono_timestamp(),
        baseline_meta: summary.baseline_meta.clone(),
    };
    let env_path = Path::new(results_path).join("env.json");
    if let Ok(env_json) = serde_json::to_string_pretty(&env) {
        if let Err(e) = fs::write(&env_path, env_json) {
            eprintln!("[perf_summarize] env.json write failed: {e}");
        }
    }
    // Copy baseline_meta.json into run dir for drift audit.
    if baseline_meta_path.is_file() {
        let dst = Path::new(results_path).join("baseline_meta.json");
        if let Err(e) = fs::copy(&baseline_meta_path, &dst) {
            eprintln!("[perf_summarize] baseline_meta.json copy failed: {e}");
        }
    }

    // Copy flamegraph (or profile.json.gz) into run dir if fresh + present.
    if let Some(src) = args.flamegraph.as_deref() {
        let src_path = Path::new(src);
        if src_path.is_file() {
            let fname = src_path
                .file_name()
                .and_then(|n| n.to_str())
                .unwrap_or("flamegraph.svg");
            let dst = Path::new(results_path).join(fname);
            if let Err(e) = fs::copy(src_path, &dst) {
                eprintln!("[perf_summarize] flamegraph copy failed: {e}");
            }
        }
    } else {
        // Back-compat: if a root-level flamegraph.svg exists, copy it,
        // but only if it was modified within the last hour.
        let fg = Path::new("flamegraph.svg");
        if fg.is_file() {
            if let Ok(meta) = fg.metadata() {
                if let Ok(mtime) = meta.modified() {
                    if mtime.elapsed().map(|d| d.as_secs() < 3600).unwrap_or(false) {
                        let dst = Path::new(results_path).join("flamegraph.svg");
                        let _ = fs::copy(fg, dst);
                    }
                }
            }
        }
    }

    if args.fail_on_regression && verdict == "REGRESSIONS" {
        eprintln!("[perf_summarize] failing: REGRESSIONS detected");
        return Ok(2);
    }
    Ok(0)
}

fn main() -> ExitCode {
    match run() {
        Ok(0) => ExitCode::SUCCESS,
        Ok(code) => ExitCode::from(u8::try_from(code).unwrap_or(1)),
        Err(e) => {
            eprintln!("[perf_summarize] error: {e}");
            ExitCode::FAILURE
        }
    }
}