fsqlite-btree 0.1.10

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

use std::collections::BTreeMap;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::{LazyLock, Mutex};

use fsqlite_types::PageNumber;
use serde::{Deserialize, Serialize};

/// Supported B-tree operation types for observability.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum BtreeOpType {
    /// Cursor seek operation (`table_move_to` / `index_move_to`).
    Seek,
    /// Mutation insert operation.
    Insert,
    /// Mutation delete operation.
    Delete,
}

impl BtreeOpType {
    /// Stable label used in logs and metrics dimensions.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Seek => "seek",
            Self::Insert => "insert",
            Self::Delete => "delete",
        }
    }
}

/// Snapshot of per-operation totals for `fsqlite_btree_operations_total`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct BtreeOperationTotals {
    /// Number of seek operations.
    pub seek: u64,
    /// Number of insert operations.
    pub insert: u64,
    /// Number of delete operations.
    pub delete: u64,
}

/// Snapshot of B-tree observability metrics.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct BtreeMetricsSnapshot {
    /// Counter by operation type.
    pub fsqlite_btree_operations_total: BtreeOperationTotals,
    /// Total number of split events observed.
    pub fsqlite_btree_page_splits_total: u64,
    /// Current B-tree depth gauge.
    pub fsqlite_btree_depth: u64,
    /// Total number of Swiss Table probes (lookups/inserts/removes).
    pub fsqlite_swiss_table_probes_total: u64,
    /// Current Swiss Table load factor (scaled by 1000, e.g. 875 = 0.875).
    pub fsqlite_swiss_table_load_factor: u64,
    /// Swizzle ratio gauge (0–1000, where 1000 = 100% swizzled).
    pub fsqlite_swizzle_ratio: u64,
    /// Total swizzle faults (CAS failures).
    pub fsqlite_swizzle_faults_total: u64,
    /// Total successful swizzle-in operations.
    pub fsqlite_swizzle_in_total: u64,
    /// Total successful unswizzle-out operations.
    pub fsqlite_swizzle_out_total: u64,
}

/// Snapshot of copy-heavy B-tree payload and cell-assembly kernels.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
pub struct BtreeCopyProfileSnapshot {
    /// Local payload copied into a caller-owned scratch buffer.
    pub local_payload_copy_calls: u64,
    pub local_payload_copy_bytes: u64,
    /// Fresh owned payload materializations (for example `payload()` or helper APIs).
    pub owned_payload_materialization_calls: u64,
    pub owned_payload_materialization_bytes: u64,
    /// Overflow payload reassembly activity.
    pub overflow_chain_reassembly_calls: u64,
    pub overflow_chain_local_bytes: u64,
    pub overflow_chain_overflow_bytes: u64,
    pub overflow_page_reads: u64,
    /// On-page cell assembly helpers.
    pub table_leaf_cell_assembly_calls: u64,
    pub table_leaf_cell_assembly_bytes: u64,
    pub index_leaf_cell_assembly_calls: u64,
    pub index_leaf_cell_assembly_bytes: u64,
    pub interior_cell_rebuild_calls: u64,
    pub interior_cell_rebuild_bytes: u64,
}

/// Snapshot of residual leaf-state reuse counters for W3-style insert paths.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
pub struct BtreeLeafReuseSnapshot {
    /// Successful no-split leaf inserts that reused the in-memory stack entry.
    pub no_split_reuse_hits: u64,
    /// Cases that fell back to the conservative balance/reload path.
    pub conservative_reload_fallbacks: u64,
    /// Full header + cell-pointer rebuilds performed via `reload_page_fresh`.
    pub page_header_rebuild_count: u64,
    /// No-overflow table-leaf append calls that wrote payload bytes directly.
    pub fast_table_leaf_payload_appends: u64,
    /// Time spent mutating the in-memory page image for the no-overflow append path.
    pub fast_table_leaf_payload_mutate_time_ns: u64,
    /// Time spent handing the no-overflow append image to the pager write-set.
    pub fast_table_leaf_payload_stage_time_ns: u64,
    /// Full-cell append calls used when the payload fast path was unavailable.
    pub fast_table_leaf_full_cell_appends: u64,
    /// Time spent mutating the in-memory page image for the full-cell append path.
    pub fast_table_leaf_full_cell_mutate_time_ns: u64,
    /// Time spent handing the full-cell append image to the pager write-set.
    pub fast_table_leaf_full_cell_stage_time_ns: u64,
    /// Attempts to use the right-edge quick-balance split path.
    pub quick_balance_attempts: u64,
    /// Successful right-edge quick-balance split path hits.
    pub quick_balance_hits: u64,
    /// Time spent in the right-edge quick-balance attempt path.
    pub quick_balance_time_ns: u64,
    /// Attempts to use the table-leaf local split path.
    pub local_split_attempts: u64,
    /// Successful table-leaf local split path hits.
    pub local_split_hits: u64,
    /// Time spent in the table-leaf local split attempt path.
    pub local_split_time_ns: u64,
    /// Calls into the generic nonroot rebalance path.
    pub nonroot_balance_calls: u64,
    /// Time spent in the generic nonroot rebalance path.
    pub nonroot_balance_time_ns: u64,
    /// Retained same-leaf DELETE run materializations.
    pub delete_leaf_run_materialize_calls: u64,
    /// Time spent materializing retained same-leaf DELETE runs into page images.
    pub delete_leaf_run_materialize_time_ns: u64,
    /// Retained same-leaf DELETE run page writes.
    pub delete_leaf_run_write_calls: u64,
    /// Time spent handing retained same-leaf DELETE page images to the pager.
    pub delete_leaf_run_write_time_ns: u64,
    /// Rowid searches inside retained same-leaf DELETE runs.
    pub delete_leaf_run_search_calls: u64,
    /// Time spent searching retained same-leaf DELETE run rowids.
    pub delete_leaf_run_search_time_ns: u64,
    /// Duplicate-index checks inside retained same-leaf DELETE runs.
    pub delete_leaf_run_duplicate_check_calls: u64,
    /// Time spent checking duplicate indexes in retained same-leaf DELETE runs.
    pub delete_leaf_run_duplicate_check_time_ns: u64,
    /// Compact-page-shape checks inside retained same-leaf DELETE runs.
    pub delete_leaf_run_compact_check_calls: u64,
    /// Time spent checking compact page shape in retained same-leaf DELETE runs.
    pub delete_leaf_run_compact_check_time_ns: u64,
    /// Cell parse/shape checks inside retained same-leaf DELETE runs.
    pub delete_leaf_run_cell_parse_calls: u64,
    /// Time spent parsing cells in retained same-leaf DELETE runs.
    pub delete_leaf_run_cell_parse_time_ns: u64,
    /// Bulk table INSERT page-run grouping calls.
    pub bulk_table_grouping_calls: u64,
    /// Time spent grouping bulk table INSERT records into page ranges.
    pub bulk_table_grouping_time_ns: u64,
    /// Bulk table INSERT leaf page builds.
    pub bulk_table_leaf_page_build_calls: u64,
    /// Time spent building bulk table INSERT leaf page images.
    pub bulk_table_leaf_page_build_time_ns: u64,
    /// Bulk table INSERT leaf page writes.
    pub bulk_table_leaf_page_write_calls: u64,
    /// Time spent handing bulk table INSERT leaf pages to the pager.
    pub bulk_table_leaf_page_write_time_ns: u64,
    /// Bulk table INSERT interior/root page builds.
    pub bulk_table_interior_page_build_calls: u64,
    /// Time spent building bulk table INSERT interior/root page images.
    pub bulk_table_interior_page_build_time_ns: u64,
    /// Bulk table INSERT interior/root page writes.
    pub bulk_table_interior_page_write_calls: u64,
    /// Time spent handing bulk table INSERT interior/root pages to the pager.
    pub bulk_table_interior_page_write_time_ns: u64,
}

/// Per-operation mutable stats while a `btree_op` span is active.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub(crate) struct BtreeOpRuntimeStats {
    pub(crate) pages_visited: u64,
    pub(crate) splits: u64,
    pub(crate) merges: u64,
}

impl BtreeOpRuntimeStats {
    pub(crate) fn record_page_visit(&mut self) {
        self.pages_visited = self.pages_visited.saturating_add(1);
    }

    pub(crate) fn record_split(&mut self) {
        self.splits = self.splits.saturating_add(1);
    }

    pub(crate) fn record_merge(&mut self) {
        self.merges = self.merges.saturating_add(1);
    }
}

// ── B-tree hot-path metrics gate (bd-perf cc_3 2026-04-25) ────────────────
//
// The per-cursor-op counters below (`record_operation`, `set_depth_gauge`,
// `record_split_event`, `record_no_split_reuse_hit`,
// `record_conservative_reload_fallback`, `record_page_header_rebuild`) are
// consumed only by `fsqlite-e2e` profiling captures and a handful of
// observability tests — production reads nothing here. Each unconditional
// `fetch_add` / `store` on a shared static atomic is a cross-core cache-line
// invalidation under MT load. Mirror the MVCC pattern (bc4fa6b5 / d2156302 /
// 03c49886): default the gate off so the hot path pays one shared-cache
// `AtomicBool::load(Relaxed)` instead of a `lock xadd` to a contended line.
//
// Callers flip the gate explicitly: `fsqlite-e2e/src/fsqlite_executor.rs`
// when `HotPathMetricsCapture` is enabled, and tests that assert on these
// counters before reading the snapshot.
static FSQLITE_BTREE_METRICS_ENABLED: AtomicBool = AtomicBool::new(false);

static BTREE_OP_SEEK_TOTAL: AtomicU64 = AtomicU64::new(0);
static BTREE_OP_INSERT_TOTAL: AtomicU64 = AtomicU64::new(0);
static BTREE_OP_DELETE_TOTAL: AtomicU64 = AtomicU64::new(0);
static BTREE_PAGE_SPLITS_TOTAL: AtomicU64 = AtomicU64::new(0);
static BTREE_DEPTH_GAUGE: AtomicU64 = AtomicU64::new(0);
static SWISS_TABLE_PROBES_TOTAL: AtomicU64 = AtomicU64::new(0);
static SWISS_TABLE_LOAD_FACTOR: AtomicU64 = AtomicU64::new(0);

// ── Conflict-topology allocator/split policy (bd-1dp9.6.7.13.2) ──────────

const CONFLICT_TOPOLOGY_POLICY_ID: &str = "btree.conflict_topology_split.v1";
const CONFLICT_TOPOLOGY_POLICY_ENV: &str = "FSQLITE_CONFLICT_TOPOLOGY_POLICY";
const CONFLICT_TOPOLOGY_HOT_HEAT_THRESHOLD: u64 = 2;
const CONFLICT_TOPOLOGY_HOT_OVERLAP_THRESHOLD: u32 = 2;
const CONFLICT_TOPOLOGY_TARGET_SHIFT_BPS: usize = 1_500;
const HOT_PAGE_DEFLECTION_POLICY_ID: &str = "btree.hot_page_deflection.v1";
const HOT_PAGE_DEFLECTION_HEAT_THRESHOLD: u64 = 64;
const HOT_PAGE_DEFLECTION_OVERLAP_THRESHOLD: u32 = 4;
const HOT_PAGE_DEFLECTION_TARGET_SHIFT_BPS: usize = 1_000;
const HOT_PAGE_DEFLECTION_BUDGET_PAGES: u8 = 2;
const HOT_PAGE_DEFLECTION_BUDGET_NS: u64 = 0;

// ── Adaptive fill-factor control (bd-1dp9.6.7.13.2) ──────────────────────
//
// The topology policy above applies a *flat* fill-factor shift the moment a
// page is judged "topology hot". Adaptive fill-factor control instead scales an
// additional, bounded shift in proportion to the accumulated conflict heat, so
// a page that keeps getting hotter cedes progressively more slack to the
// contended side — never exceeding explicit clamps. It is opt-in and
// default-disabled, so the baseline/topology split policy is byte-identical
// until an operator enables it (rollout discipline: observe -> advise ->
// enforce, with a reversible kill switch).
const ADAPTIVE_FILL_FACTOR_POLICY_ID: &str = "btree.adaptive_fill_factor.v1";
const ADAPTIVE_FILL_FACTOR_ENV: &str = "FSQLITE_ADAPTIVE_FILL_FACTOR";
// Heat at/below which the adaptive ramp adds zero extra shift, preserving the
// flat topology behavior at first contact. Anchored to the hot threshold.
const ADAPTIVE_FILL_FACTOR_KNEE_HEAT: u64 = CONFLICT_TOPOLOGY_HOT_HEAT_THRESHOLD;
// Heat at which the adaptive ramp saturates at its maximum extra shift. Anchored
// to the pathological-hotspot threshold so the ramp spans the topology-hot band.
const ADAPTIVE_FILL_FACTOR_SATURATION_HEAT: u64 = HOT_PAGE_DEFLECTION_HEAT_THRESHOLD;
// Maximum *additional* fill-factor shift the ramp may add on top of the flat
// topology shift, in basis points.
const ADAPTIVE_FILL_FACTOR_MAX_EXTRA_SHIFT_BPS: usize = 1_500;
// Explicit clamps for the adaptive path: wider than the flat topology clamps,
// but still bounded and operator-visible.
const ADAPTIVE_FILL_FACTOR_LEFT_FLOOR_BPS: usize = 1_500;
const ADAPTIVE_FILL_FACTOR_RIGHT_CEIL_BPS: usize = 9_000;

static CONFLICT_TOPOLOGY_POLICY_MODE: AtomicU64 = AtomicU64::new(2);
static CONFLICT_TOPOLOGY_POLICY_ENV_APPLIED: AtomicBool = AtomicBool::new(false);
static ADAPTIVE_FILL_FACTOR_ENABLED: AtomicBool = AtomicBool::new(false);
static ADAPTIVE_FILL_FACTOR_ENV_APPLIED: AtomicBool = AtomicBool::new(false);
static CONFLICT_TOPOLOGY_STATE: LazyLock<Mutex<ConflictTopologyState>> =
    LazyLock::new(|| Mutex::new(ConflictTopologyState::default()));

/// Rollout mode for conflict-topology placement/split policy.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ConflictTopologyPolicyMode {
    /// Ignore conflict heat and keep the baseline B-tree split policy.
    Baseline,
    /// Compute and log what the topology-aware policy would do, but do not apply it.
    Advisory,
    /// Apply topology-aware target fill adjustments for hot pages.
    Enforced,
}

impl ConflictTopologyPolicyMode {
    /// Stable log label.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Baseline => "baseline",
            Self::Advisory => "advisory",
            Self::Enforced => "enforced",
        }
    }

    const fn to_raw(self) -> u64 {
        match self {
            Self::Baseline => 0,
            Self::Advisory => 1,
            Self::Enforced => 2,
        }
    }

    const fn from_raw(raw: u64) -> Self {
        match raw {
            0 => Self::Baseline,
            1 => Self::Advisory,
            _ => Self::Enforced,
        }
    }
}

/// Bounded hotspot-deflection state for a split decision.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
pub enum HotPageDeflectionStatus {
    /// No pathological hotspot signal selected the deflection path.
    Inactive,
    /// Advisory mode observed a hotspot but did not mutate split placement.
    AdvisoryOnly,
    /// Enforced mode consumed one bounded split-deflection credit.
    Applied,
    /// The page remains hot but its bounded deflection budget is exhausted.
    BudgetExhausted,
    /// Operator policy forced baseline placement.
    OperatorOverride,
}

impl HotPageDeflectionStatus {
    /// Stable log label.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Inactive => "inactive",
            Self::AdvisoryOnly => "advisory_only",
            Self::Applied => "applied",
            Self::BudgetExhausted => "budget_exhausted",
            Self::OperatorOverride => "operator_override",
        }
    }

    /// Whether this status represents an active pathological-hotspot signal.
    #[must_use]
    pub const fn is_active(self) -> bool {
        matches!(
            self,
            Self::AdvisoryOnly | Self::Applied | Self::BudgetExhausted
        )
    }

    /// Whether this status consumed a split-deflection credit.
    #[must_use]
    pub const fn is_applied(self) -> bool {
        matches!(self, Self::Applied)
    }
}

#[derive(Debug, Clone, Copy, Default)]
struct ConflictTopologyPageState {
    heat: u64,
    max_writer_overlap_estimate: u32,
    deflection_armed: bool,
    deflection_credits_remaining: u8,
}

#[derive(Debug, Default)]
struct ConflictTopologyState {
    pages: BTreeMap<PageNumber, ConflictTopologyPageState>,
}

/// Split-policy advice derived from MVCC conflict heat.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
pub struct ConflictTopologySplitAdvice {
    /// Stable policy identifier for logs and artifacts.
    pub policy_id: &'static str,
    /// Stable mitigation policy identifier for hotspot escape hatches.
    pub mitigation_policy_id: &'static str,
    /// Current rollout mode.
    pub policy_mode: ConflictTopologyPolicyMode,
    /// Page selected by the heat-map signal, or zero when no page is known.
    pub hot_page_id: u32,
    /// Baseline split target before topology adjustment.
    pub baseline_target_left_basis_points: usize,
    /// Advisory topology-aware target, even when mode is advisory.
    pub advised_target_left_basis_points: usize,
    /// Target actually applied to the split.
    pub effective_target_left_basis_points: usize,
    /// Conflict heat observed for the page.
    pub conflict_heat: u64,
    /// Maximum peer-writer overlap observed for the page.
    pub writer_overlap_estimate: u32,
    /// Whether the heat thresholds selected the topology-aware policy.
    pub topology_hot: bool,
    /// Whether the effective target differs from baseline.
    pub applied: bool,
    /// Bounded deflection outcome for this split decision.
    pub deflection_status: HotPageDeflectionStatus,
    /// Deflection credits available before this advice was produced.
    pub deflection_credits_before: u8,
    /// Deflection credits remaining after this advice was produced.
    pub deflection_credits_after: u8,
    /// Maximum synchronous budget consumed by this split-time mitigation.
    pub budget_ns: u64,
    /// Maximum page-level deflections allowed for this armed hotspot.
    pub budget_pages: u8,
    /// Snapshot generation published by a physical recluster path, if any.
    pub publication_generation: u64,
    /// Conflict heat before the mitigation estimate.
    pub heat_before: u64,
    /// Conflict heat after the mitigation estimate.
    pub heat_after: u64,
    /// Why the pathological-hotspot path did or did not trigger.
    pub trigger_reason: &'static str,
    /// Operator-facing outcome for the bounded mitigation path.
    pub migration_outcome: &'static str,
    /// Reversal or rollback reason, when the mitigation is not applied.
    pub rollback_reason: &'static str,
    /// Positive means the policy predicts fewer future hot-page overlaps.
    pub predicted_overlap_delta: i64,
    /// Whether an operator override forced baseline behavior.
    pub operator_override_active: bool,
}

impl ConflictTopologySplitAdvice {
    /// Log label for the chosen placement policy.
    #[must_use]
    pub const fn placement_policy(self) -> &'static str {
        if self.applied {
            if self.deflection_applied() {
                "hot_page_deflection_fill_factor"
            } else {
                "topology_aware_fill_factor"
            }
        } else {
            "baseline"
        }
    }

    /// Log label for why the policy did or did not adjust the split.
    #[must_use]
    pub const fn split_reason(self) -> &'static str {
        if self.operator_override_active {
            "operator_override_baseline"
        } else if self.deflection_applied() {
            "bounded_hot_page_deflection"
        } else if self.topology_hot {
            "mvcc_conflict_heat_hot_page"
        } else {
            "no_hot_page_signal"
        }
    }

    /// Whether this page crossed the stronger pathological-hotspot threshold.
    #[must_use]
    pub const fn deflection_active(self) -> bool {
        self.deflection_status.is_active()
    }

    /// Whether a bounded split-time deflection credit was consumed.
    #[must_use]
    pub const fn deflection_applied(self) -> bool {
        self.deflection_status.is_applied()
    }
}

/// Set the process-local rollout mode for conflict-topology split policy.
pub fn set_conflict_topology_policy_mode(mode: ConflictTopologyPolicyMode) {
    CONFLICT_TOPOLOGY_POLICY_ENV_APPLIED.store(true, Ordering::Release);
    CONFLICT_TOPOLOGY_POLICY_MODE.store(mode.to_raw(), Ordering::Relaxed);
}

/// Current rollout mode for conflict-topology split policy.
#[must_use]
pub fn conflict_topology_policy_mode() -> ConflictTopologyPolicyMode {
    apply_conflict_topology_policy_env_once();
    ConflictTopologyPolicyMode::from_raw(CONFLICT_TOPOLOGY_POLICY_MODE.load(Ordering::Relaxed))
}

/// Whether MVCC should feed conflict heat into the B-tree policy cache.
#[must_use]
pub fn conflict_topology_policy_enabled() -> bool {
    conflict_topology_policy_mode() != ConflictTopologyPolicyMode::Baseline
}

/// Stable policy identifier for adaptive fill-factor control.
#[must_use]
pub const fn adaptive_fill_factor_policy_id() -> &'static str {
    ADAPTIVE_FILL_FACTOR_POLICY_ID
}

fn apply_adaptive_fill_factor_env_once() {
    if ADAPTIVE_FILL_FACTOR_ENV_APPLIED
        .compare_exchange(false, true, Ordering::AcqRel, Ordering::Acquire)
        .is_err()
    {
        return;
    }
    let Ok(raw) = std::env::var(ADAPTIVE_FILL_FACTOR_ENV) else {
        return;
    };
    if let Some(enabled) = parse_adaptive_fill_factor_flag(raw.as_str()) {
        ADAPTIVE_FILL_FACTOR_ENABLED.store(enabled, Ordering::Relaxed);
    }
}

fn parse_adaptive_fill_factor_flag(raw: &str) -> Option<bool> {
    let raw = raw.trim();
    if raw.eq_ignore_ascii_case("on")
        || raw.eq_ignore_ascii_case("true")
        || raw.eq_ignore_ascii_case("enforced")
        || raw == "1"
    {
        Some(true)
    } else if raw.eq_ignore_ascii_case("off")
        || raw.eq_ignore_ascii_case("false")
        || raw.eq_ignore_ascii_case("baseline")
        || raw == "0"
    {
        Some(false)
    } else {
        None
    }
}

/// Whether adaptive fill-factor control is enabled (default: disabled).
///
/// Honors the `FSQLITE_ADAPTIVE_FILL_FACTOR` operator override once per process.
#[must_use]
pub fn adaptive_fill_factor_enabled() -> bool {
    apply_adaptive_fill_factor_env_once();
    ADAPTIVE_FILL_FACTOR_ENABLED.load(Ordering::Relaxed)
}

/// Set the process-local adaptive fill-factor enable flag (reversible kill switch).
pub fn set_adaptive_fill_factor_enabled(enabled: bool) {
    ADAPTIVE_FILL_FACTOR_ENV_APPLIED.store(true, Ordering::Release);
    ADAPTIVE_FILL_FACTOR_ENABLED.store(enabled, Ordering::Relaxed);
}

/// Additional fill-factor shift (basis points) the adaptive ramp adds for a
/// given accumulated conflict heat.
///
/// Zero at or below the knee heat (so first contact matches the flat topology
/// policy), increasing linearly to `ADAPTIVE_FILL_FACTOR_MAX_EXTRA_SHIFT_BPS` at
/// the saturation heat, and clamped flat above it. Pure and deterministic.
#[must_use]
fn adaptive_fill_factor_extra_shift_bps(conflict_heat: u64) -> usize {
    if conflict_heat <= ADAPTIVE_FILL_FACTOR_KNEE_HEAT {
        return 0;
    }
    let span = ADAPTIVE_FILL_FACTOR_SATURATION_HEAT.saturating_sub(ADAPTIVE_FILL_FACTOR_KNEE_HEAT);
    if span == 0 {
        return ADAPTIVE_FILL_FACTOR_MAX_EXTRA_SHIFT_BPS;
    }
    let pos = (conflict_heat - ADAPTIVE_FILL_FACTOR_KNEE_HEAT).min(span);
    let max = ADAPTIVE_FILL_FACTOR_MAX_EXTRA_SHIFT_BPS as u64;
    let extra = max * pos / span;
    usize::try_from(extra).unwrap_or(ADAPTIVE_FILL_FACTOR_MAX_EXTRA_SHIFT_BPS)
}

/// Refine a topology-aware split target with adaptive fill-factor control.
///
/// When adaptive control is disabled this returns `topology_target_left_basis_points`
/// unchanged, guaranteeing the baseline/topology split policy is byte-identical.
/// When enabled it biases the target further toward the contended side in
/// proportion to `conflict_heat`, clamped to explicit bounds. Interior splits
/// carry no directional bias and are returned unchanged.
#[must_use]
pub fn adaptive_fill_factor_target(
    predicted_hot_side: &str,
    topology_target_left_basis_points: usize,
    conflict_heat: u64,
) -> usize {
    if !adaptive_fill_factor_enabled() {
        return topology_target_left_basis_points;
    }
    let extra = adaptive_fill_factor_extra_shift_bps(conflict_heat);
    if extra == 0 {
        return topology_target_left_basis_points;
    }
    match predicted_hot_side {
        "left_edge" => topology_target_left_basis_points
            .saturating_sub(extra)
            .max(ADAPTIVE_FILL_FACTOR_LEFT_FLOOR_BPS),
        "right_edge" => topology_target_left_basis_points
            .saturating_add(extra)
            .min(ADAPTIVE_FILL_FACTOR_RIGHT_CEIL_BPS),
        _ => topology_target_left_basis_points,
    }
}

/// Clear accumulated conflict-topology heat.
pub fn reset_conflict_topology_policy_state() {
    *CONFLICT_TOPOLOGY_STATE
        .lock()
        .unwrap_or_else(std::sync::PoisonError::into_inner) = ConflictTopologyState::default();
}

/// Feed one MVCC conflict-heat observation into the B-tree split policy cache.
pub fn record_conflict_topology_heat(
    page: PageNumber,
    conflict_heat: u64,
    writer_overlap_estimate: u32,
) {
    let _updated = record_conflict_topology_heat_batch(
        std::iter::once(page),
        conflict_heat,
        writer_overlap_estimate,
    );
}

/// Feed several MVCC conflict-heat observations under one policy-cache lock.
///
/// Returns the number of pages updated.
#[must_use]
pub fn record_conflict_topology_heat_batch(
    pages: impl IntoIterator<Item = PageNumber>,
    conflict_heat: u64,
    writer_overlap_estimate: u32,
) -> usize {
    if !conflict_topology_policy_enabled() {
        return 0;
    }
    let mut state = CONFLICT_TOPOLOGY_STATE
        .lock()
        .unwrap_or_else(std::sync::PoisonError::into_inner);
    let conflict_heat = conflict_heat.max(1);
    let mut updated = 0usize;
    for page in pages {
        let page_state = state.pages.entry(page).or_default();
        page_state.heat = page_state.heat.saturating_add(conflict_heat);
        page_state.max_writer_overlap_estimate = page_state
            .max_writer_overlap_estimate
            .max(writer_overlap_estimate);
        if !page_state.deflection_armed
            && page_state.heat >= HOT_PAGE_DEFLECTION_HEAT_THRESHOLD
            && page_state.max_writer_overlap_estimate >= HOT_PAGE_DEFLECTION_OVERLAP_THRESHOLD
        {
            page_state.deflection_armed = true;
            page_state.deflection_credits_remaining = HOT_PAGE_DEFLECTION_BUDGET_PAGES;
        }
        updated += 1;
    }
    updated
}

/// Return split advice for a page about to split.
#[must_use]
pub fn conflict_topology_split_advice(
    page: PageNumber,
    predicted_hot_side: &str,
    baseline_target_left_basis_points: usize,
) -> ConflictTopologySplitAdvice {
    let mode = conflict_topology_policy_mode();
    let mut deflection_credits_before = 0;
    let mut deflection_credits_after = 0;
    let mut deflection_applied = false;

    let page_state = if mode == ConflictTopologyPolicyMode::Baseline {
        ConflictTopologyPageState::default()
    } else {
        let mut state = CONFLICT_TOPOLOGY_STATE
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        let Some(page_state) = state.pages.get_mut(&page) else {
            return baseline_conflict_topology_split_advice(
                page,
                mode,
                baseline_target_left_basis_points,
            );
        };
        let deflection_active = page_state.deflection_armed
            && page_state.heat >= HOT_PAGE_DEFLECTION_HEAT_THRESHOLD
            && page_state.max_writer_overlap_estimate >= HOT_PAGE_DEFLECTION_OVERLAP_THRESHOLD;
        deflection_credits_before = page_state.deflection_credits_remaining;
        if mode == ConflictTopologyPolicyMode::Enforced
            && deflection_active
            && page_state.deflection_credits_remaining > 0
        {
            page_state.deflection_credits_remaining =
                page_state.deflection_credits_remaining.saturating_sub(1);
            deflection_applied = true;
        }
        deflection_credits_after = page_state.deflection_credits_remaining;
        *page_state
    };
    let topology_hot = page_state.heat >= CONFLICT_TOPOLOGY_HOT_HEAT_THRESHOLD
        && page_state.max_writer_overlap_estimate >= CONFLICT_TOPOLOGY_HOT_OVERLAP_THRESHOLD;
    let topology_target_left_basis_points = if topology_hot {
        topology_adjusted_target(predicted_hot_side, baseline_target_left_basis_points)
    } else {
        baseline_target_left_basis_points
    };
    let deflection_active = page_state.deflection_armed
        && page_state.heat >= HOT_PAGE_DEFLECTION_HEAT_THRESHOLD
        && page_state.max_writer_overlap_estimate >= HOT_PAGE_DEFLECTION_OVERLAP_THRESHOLD;
    let advised_target_left_basis_points = if deflection_active {
        hot_page_deflection_adjusted_target(predicted_hot_side, topology_target_left_basis_points)
    } else {
        topology_target_left_basis_points
    };
    let effective_target_left_basis_points =
        if mode == ConflictTopologyPolicyMode::Enforced && deflection_applied {
            advised_target_left_basis_points
        } else if mode == ConflictTopologyPolicyMode::Enforced && topology_hot {
            topology_target_left_basis_points
        } else {
            baseline_target_left_basis_points
        };
    let applied = effective_target_left_basis_points != baseline_target_left_basis_points;
    let overlap_estimate = i64::from(page_state.max_writer_overlap_estimate.max(1));
    let predicted_overlap_delta = if deflection_applied {
        overlap_estimate * 2
    } else if topology_hot {
        overlap_estimate
    } else {
        0
    };
    let heat_after = if deflection_applied {
        page_state
            .heat
            .saturating_sub(u64::from(page_state.max_writer_overlap_estimate.max(1)))
    } else {
        page_state.heat
    };

    ConflictTopologySplitAdvice {
        policy_id: CONFLICT_TOPOLOGY_POLICY_ID,
        mitigation_policy_id: HOT_PAGE_DEFLECTION_POLICY_ID,
        policy_mode: mode,
        hot_page_id: page.get(),
        baseline_target_left_basis_points,
        advised_target_left_basis_points,
        effective_target_left_basis_points,
        conflict_heat: page_state.heat,
        writer_overlap_estimate: page_state.max_writer_overlap_estimate,
        topology_hot,
        applied,
        deflection_status: hot_page_deflection_status(
            mode,
            deflection_active,
            deflection_applied,
            deflection_credits_before,
        ),
        deflection_credits_before,
        deflection_credits_after,
        budget_ns: HOT_PAGE_DEFLECTION_BUDGET_NS,
        budget_pages: HOT_PAGE_DEFLECTION_BUDGET_PAGES,
        publication_generation: 0,
        heat_before: page_state.heat,
        heat_after,
        trigger_reason: hot_page_deflection_trigger_reason(
            mode,
            topology_hot,
            deflection_active,
            deflection_credits_before,
        ),
        migration_outcome: hot_page_deflection_migration_outcome(
            mode,
            deflection_active,
            deflection_applied,
            deflection_credits_before,
        ),
        rollback_reason: hot_page_deflection_rollback_reason(
            mode,
            deflection_active,
            deflection_applied,
            deflection_credits_before,
        ),
        predicted_overlap_delta,
        operator_override_active: mode == ConflictTopologyPolicyMode::Baseline,
    }
}

fn baseline_conflict_topology_split_advice(
    page: PageNumber,
    mode: ConflictTopologyPolicyMode,
    baseline_target_left_basis_points: usize,
) -> ConflictTopologySplitAdvice {
    ConflictTopologySplitAdvice {
        policy_id: CONFLICT_TOPOLOGY_POLICY_ID,
        mitigation_policy_id: HOT_PAGE_DEFLECTION_POLICY_ID,
        policy_mode: mode,
        hot_page_id: page.get(),
        baseline_target_left_basis_points,
        advised_target_left_basis_points: baseline_target_left_basis_points,
        effective_target_left_basis_points: baseline_target_left_basis_points,
        conflict_heat: 0,
        writer_overlap_estimate: 0,
        topology_hot: false,
        applied: false,
        deflection_status: hot_page_deflection_status(mode, false, false, 0),
        deflection_credits_before: 0,
        deflection_credits_after: 0,
        budget_ns: HOT_PAGE_DEFLECTION_BUDGET_NS,
        budget_pages: HOT_PAGE_DEFLECTION_BUDGET_PAGES,
        publication_generation: 0,
        heat_before: 0,
        heat_after: 0,
        trigger_reason: hot_page_deflection_trigger_reason(mode, false, false, 0),
        migration_outcome: hot_page_deflection_migration_outcome(mode, false, false, 0),
        rollback_reason: hot_page_deflection_rollback_reason(mode, false, false, 0),
        predicted_overlap_delta: 0,
        operator_override_active: mode == ConflictTopologyPolicyMode::Baseline,
    }
}

fn topology_adjusted_target(predicted_hot_side: &str, baseline: usize) -> usize {
    match predicted_hot_side {
        "left_edge" => baseline
            .saturating_sub(CONFLICT_TOPOLOGY_TARGET_SHIFT_BPS)
            .max(2_500),
        "right_edge" => baseline
            .saturating_add(CONFLICT_TOPOLOGY_TARGET_SHIFT_BPS)
            .min(8_000),
        _ => 5_000,
    }
}

fn hot_page_deflection_adjusted_target(predicted_hot_side: &str, baseline: usize) -> usize {
    match predicted_hot_side {
        "left_edge" => baseline
            .saturating_sub(HOT_PAGE_DEFLECTION_TARGET_SHIFT_BPS)
            .max(1_500),
        "right_edge" => baseline
            .saturating_add(HOT_PAGE_DEFLECTION_TARGET_SHIFT_BPS)
            .min(9_000),
        _ => baseline,
    }
}

fn hot_page_deflection_trigger_reason(
    mode: ConflictTopologyPolicyMode,
    topology_hot: bool,
    deflection_active: bool,
    deflection_credits_before: u8,
) -> &'static str {
    if mode == ConflictTopologyPolicyMode::Baseline {
        "operator_override_baseline"
    } else if deflection_active && deflection_credits_before > 0 {
        "pathological_hot_page"
    } else if deflection_active {
        "pathological_hot_page_budget_exhausted"
    } else if topology_hot {
        "below_deflection_threshold"
    } else {
        "no_hot_page_signal"
    }
}

fn hot_page_deflection_status(
    mode: ConflictTopologyPolicyMode,
    deflection_active: bool,
    deflection_applied: bool,
    deflection_credits_before: u8,
) -> HotPageDeflectionStatus {
    if mode == ConflictTopologyPolicyMode::Baseline {
        HotPageDeflectionStatus::OperatorOverride
    } else if deflection_applied {
        HotPageDeflectionStatus::Applied
    } else if deflection_active && deflection_credits_before == 0 {
        HotPageDeflectionStatus::BudgetExhausted
    } else if deflection_active && mode == ConflictTopologyPolicyMode::Advisory {
        HotPageDeflectionStatus::AdvisoryOnly
    } else {
        HotPageDeflectionStatus::Inactive
    }
}

fn hot_page_deflection_migration_outcome(
    mode: ConflictTopologyPolicyMode,
    deflection_active: bool,
    deflection_applied: bool,
    deflection_credits_before: u8,
) -> &'static str {
    if mode == ConflictTopologyPolicyMode::Baseline {
        "operator_override_baseline"
    } else if deflection_applied {
        "split_deflected"
    } else if deflection_active && deflection_credits_before == 0 {
        "budget_exhausted"
    } else if deflection_active && mode == ConflictTopologyPolicyMode::Advisory {
        "advisory_only"
    } else {
        "not_triggered"
    }
}

fn hot_page_deflection_rollback_reason(
    mode: ConflictTopologyPolicyMode,
    deflection_active: bool,
    deflection_applied: bool,
    deflection_credits_before: u8,
) -> &'static str {
    if mode == ConflictTopologyPolicyMode::Baseline {
        "operator_override_baseline"
    } else if deflection_applied {
        "none"
    } else if deflection_active && deflection_credits_before == 0 {
        "budget_exhausted"
    } else if deflection_active && mode == ConflictTopologyPolicyMode::Advisory {
        "advisory_only"
    } else {
        "no_hotspot"
    }
}

fn apply_conflict_topology_policy_env_once() {
    if CONFLICT_TOPOLOGY_POLICY_ENV_APPLIED
        .compare_exchange(false, true, Ordering::AcqRel, Ordering::Acquire)
        .is_err()
    {
        return;
    }
    let Ok(raw) = std::env::var(CONFLICT_TOPOLOGY_POLICY_ENV) else {
        return;
    };
    if let Some(mode) = parse_conflict_topology_policy_mode(raw.as_str()) {
        CONFLICT_TOPOLOGY_POLICY_MODE.store(mode.to_raw(), Ordering::Relaxed);
    }
}

fn parse_conflict_topology_policy_mode(raw: &str) -> Option<ConflictTopologyPolicyMode> {
    let raw = raw.trim();
    if raw.eq_ignore_ascii_case("baseline")
        || raw.eq_ignore_ascii_case("off")
        || raw.eq_ignore_ascii_case("false")
        || raw == "0"
    {
        Some(ConflictTopologyPolicyMode::Baseline)
    } else if raw.eq_ignore_ascii_case("advisory")
        || raw.eq_ignore_ascii_case("shadow")
        || raw == "1"
    {
        Some(ConflictTopologyPolicyMode::Advisory)
    } else if raw.eq_ignore_ascii_case("enforced")
        || raw.eq_ignore_ascii_case("on")
        || raw.eq_ignore_ascii_case("true")
        || raw == "2"
    {
        Some(ConflictTopologyPolicyMode::Enforced)
    } else {
        None
    }
}

// ── Swizzle metrics (bd-3ta.3) ──────────────────────────────────────────────

/// Swizzle ratio gauge: (swizzled_count / total_tracked) * 1000.
static SWIZZLE_RATIO_GAUGE: AtomicU64 = AtomicU64::new(0);
/// Total swizzle faults (CAS failures + retry attempts).
static SWIZZLE_FAULTS_TOTAL: AtomicU64 = AtomicU64::new(0);
/// Total successful swizzle operations.
static SWIZZLE_IN_TOTAL: AtomicU64 = AtomicU64::new(0);
/// Total successful unswizzle operations.
static SWIZZLE_OUT_TOTAL: AtomicU64 = AtomicU64::new(0);

// ── Copy-heavy payload/cell kernel metrics (bd-db300.4.4.1) ─────────────────

static BTREE_COPY_PROFILE_ENABLED: AtomicBool = AtomicBool::new(false);
static BTREE_LOCAL_PAYLOAD_COPY_CALLS: AtomicU64 = AtomicU64::new(0);
static BTREE_LOCAL_PAYLOAD_COPY_BYTES: AtomicU64 = AtomicU64::new(0);
static BTREE_OWNED_PAYLOAD_MATERIALIZATION_CALLS: AtomicU64 = AtomicU64::new(0);
static BTREE_OWNED_PAYLOAD_MATERIALIZATION_BYTES: AtomicU64 = AtomicU64::new(0);
static BTREE_OVERFLOW_REASSEMBLY_CALLS: AtomicU64 = AtomicU64::new(0);
static BTREE_OVERFLOW_LOCAL_BYTES: AtomicU64 = AtomicU64::new(0);
static BTREE_OVERFLOW_BYTES: AtomicU64 = AtomicU64::new(0);
static BTREE_OVERFLOW_PAGE_READS: AtomicU64 = AtomicU64::new(0);
static BTREE_TABLE_LEAF_CELL_ASSEMBLY_CALLS: AtomicU64 = AtomicU64::new(0);
static BTREE_TABLE_LEAF_CELL_ASSEMBLY_BYTES: AtomicU64 = AtomicU64::new(0);
static BTREE_INDEX_LEAF_CELL_ASSEMBLY_CALLS: AtomicU64 = AtomicU64::new(0);
static BTREE_INDEX_LEAF_CELL_ASSEMBLY_BYTES: AtomicU64 = AtomicU64::new(0);
static BTREE_INTERIOR_CELL_REBUILD_CALLS: AtomicU64 = AtomicU64::new(0);
static BTREE_INTERIOR_CELL_REBUILD_BYTES: AtomicU64 = AtomicU64::new(0);
static BTREE_NO_SPLIT_REUSE_HITS: AtomicU64 = AtomicU64::new(0);
static BTREE_CONSERVATIVE_RELOAD_FALLBACKS: AtomicU64 = AtomicU64::new(0);
static BTREE_PAGE_HEADER_REBUILD_COUNT: AtomicU64 = AtomicU64::new(0);
static BTREE_FAST_TABLE_LEAF_PAYLOAD_APPEND_CALLS: AtomicU64 = AtomicU64::new(0);
static BTREE_FAST_TABLE_LEAF_PAYLOAD_MUTATE_TIME_NS: AtomicU64 = AtomicU64::new(0);
static BTREE_FAST_TABLE_LEAF_PAYLOAD_STAGE_TIME_NS: AtomicU64 = AtomicU64::new(0);
static BTREE_FAST_TABLE_LEAF_FULL_CELL_APPEND_CALLS: AtomicU64 = AtomicU64::new(0);
static BTREE_FAST_TABLE_LEAF_FULL_CELL_MUTATE_TIME_NS: AtomicU64 = AtomicU64::new(0);
static BTREE_FAST_TABLE_LEAF_FULL_CELL_STAGE_TIME_NS: AtomicU64 = AtomicU64::new(0);
static BTREE_QUICK_BALANCE_ATTEMPTS: AtomicU64 = AtomicU64::new(0);
static BTREE_QUICK_BALANCE_HITS: AtomicU64 = AtomicU64::new(0);
static BTREE_QUICK_BALANCE_TIME_NS: AtomicU64 = AtomicU64::new(0);
static BTREE_LOCAL_SPLIT_ATTEMPTS: AtomicU64 = AtomicU64::new(0);
static BTREE_LOCAL_SPLIT_HITS: AtomicU64 = AtomicU64::new(0);
static BTREE_LOCAL_SPLIT_TIME_NS: AtomicU64 = AtomicU64::new(0);
static BTREE_NONROOT_BALANCE_CALLS: AtomicU64 = AtomicU64::new(0);
static BTREE_NONROOT_BALANCE_TIME_NS: AtomicU64 = AtomicU64::new(0);
static BTREE_DELETE_LEAF_RUN_MATERIALIZE_CALLS: AtomicU64 = AtomicU64::new(0);
static BTREE_DELETE_LEAF_RUN_MATERIALIZE_TIME_NS: AtomicU64 = AtomicU64::new(0);
static BTREE_DELETE_LEAF_RUN_WRITE_CALLS: AtomicU64 = AtomicU64::new(0);
static BTREE_DELETE_LEAF_RUN_WRITE_TIME_NS: AtomicU64 = AtomicU64::new(0);
static BTREE_DELETE_LEAF_RUN_SEARCH_CALLS: AtomicU64 = AtomicU64::new(0);
static BTREE_DELETE_LEAF_RUN_SEARCH_TIME_NS: AtomicU64 = AtomicU64::new(0);
static BTREE_DELETE_LEAF_RUN_DUPLICATE_CHECK_CALLS: AtomicU64 = AtomicU64::new(0);
static BTREE_DELETE_LEAF_RUN_DUPLICATE_CHECK_TIME_NS: AtomicU64 = AtomicU64::new(0);
static BTREE_DELETE_LEAF_RUN_COMPACT_CHECK_CALLS: AtomicU64 = AtomicU64::new(0);
static BTREE_DELETE_LEAF_RUN_COMPACT_CHECK_TIME_NS: AtomicU64 = AtomicU64::new(0);
static BTREE_DELETE_LEAF_RUN_CELL_PARSE_CALLS: AtomicU64 = AtomicU64::new(0);
static BTREE_DELETE_LEAF_RUN_CELL_PARSE_TIME_NS: AtomicU64 = AtomicU64::new(0);
static BTREE_BULK_TABLE_GROUPING_CALLS: AtomicU64 = AtomicU64::new(0);
static BTREE_BULK_TABLE_GROUPING_TIME_NS: AtomicU64 = AtomicU64::new(0);
static BTREE_BULK_TABLE_LEAF_PAGE_BUILD_CALLS: AtomicU64 = AtomicU64::new(0);
static BTREE_BULK_TABLE_LEAF_PAGE_BUILD_TIME_NS: AtomicU64 = AtomicU64::new(0);
static BTREE_BULK_TABLE_LEAF_PAGE_WRITE_CALLS: AtomicU64 = AtomicU64::new(0);
static BTREE_BULK_TABLE_LEAF_PAGE_WRITE_TIME_NS: AtomicU64 = AtomicU64::new(0);
static BTREE_BULK_TABLE_INTERIOR_PAGE_BUILD_CALLS: AtomicU64 = AtomicU64::new(0);
static BTREE_BULK_TABLE_INTERIOR_PAGE_BUILD_TIME_NS: AtomicU64 = AtomicU64::new(0);
static BTREE_BULK_TABLE_INTERIOR_PAGE_WRITE_CALLS: AtomicU64 = AtomicU64::new(0);
static BTREE_BULK_TABLE_INTERIOR_PAGE_WRITE_TIME_NS: AtomicU64 = AtomicU64::new(0);

#[inline]
pub(crate) fn copy_profile_enabled() -> bool {
    BTREE_COPY_PROFILE_ENABLED.load(Ordering::Relaxed)
}

/// Start a profiling timer for a hot-path segment.
///
/// Returns `Some(Instant::now())` only when the copy profile gate is enabled;
/// otherwise returns `None`. Call sites capture this once at segment start,
/// run their work unconditionally, then pass the sentinel to the paired
/// `record_*` recorder — which is a no-op when the sentinel is `None`.
///
/// Purpose: avoid paying `clock_gettime` on every invocation when profiling
/// is off. Previously the pattern `let t = Instant::now(); /* work */;
/// record_x(t.elapsed())` captured the clock unconditionally and the gate
/// was checked only inside the recorder — wasting two `clock_gettime`
/// syscalls per hot-path op.
#[inline]
pub(crate) fn profile_start() -> Option<std::time::Instant> {
    copy_profile_enabled().then(std::time::Instant::now)
}

#[inline]
fn profile_elapsed_ns(start: Option<std::time::Instant>) -> Option<u64> {
    let s = start?;
    Some(u64::try_from(s.elapsed().as_nanos()).unwrap_or(u64::MAX))
}

#[inline]
fn saturating_add_bytes(counter: &AtomicU64, bytes: usize) {
    counter.fetch_add(u64::try_from(bytes).unwrap_or(u64::MAX), Ordering::Relaxed);
}

#[inline]
pub(crate) fn record_operation(op_type: BtreeOpType) {
    if !btree_metrics_enabled() {
        return;
    }
    record_operation_cold(op_type);
}

#[cold]
#[inline(never)]
fn record_operation_cold(op_type: BtreeOpType) {
    let counter = match op_type {
        BtreeOpType::Seek => &BTREE_OP_SEEK_TOTAL,
        BtreeOpType::Insert => &BTREE_OP_INSERT_TOTAL,
        BtreeOpType::Delete => &BTREE_OP_DELETE_TOTAL,
    };
    counter.fetch_add(1, Ordering::Relaxed);
}

pub fn set_btree_copy_profile_enabled(enabled: bool) {
    BTREE_COPY_PROFILE_ENABLED.store(enabled, Ordering::Relaxed);
}

/// Toggle the B-tree hot-path metrics gate.
///
/// When `false` (the default), `record_operation`, `set_depth_gauge`,
/// `record_split_event`, `record_no_split_reuse_hit`,
/// `record_conservative_reload_fallback`, and `record_page_header_rebuild`
/// each early-exit on an `AtomicBool` load instead of paying a `fetch_add` /
/// `store` on a contended global atomic. Diagnostic snapshots (`E2E
/// `HotPathMetricsCapture`, observability tests) flip this on.
pub fn set_btree_metrics_enabled(enabled: bool) {
    FSQLITE_BTREE_METRICS_ENABLED.store(enabled, Ordering::Relaxed);
}

/// Read the B-tree hot-path metrics gate.
#[inline]
#[must_use]
pub fn btree_metrics_enabled() -> bool {
    FSQLITE_BTREE_METRICS_ENABLED.load(Ordering::Relaxed)
}

pub(crate) fn record_local_payload_copy(bytes: usize) {
    if !copy_profile_enabled() {
        return;
    }
    BTREE_LOCAL_PAYLOAD_COPY_CALLS.fetch_add(1, Ordering::Relaxed);
    saturating_add_bytes(&BTREE_LOCAL_PAYLOAD_COPY_BYTES, bytes);
}

pub(crate) fn record_owned_payload_materialization(bytes: usize) {
    if !copy_profile_enabled() {
        return;
    }
    BTREE_OWNED_PAYLOAD_MATERIALIZATION_CALLS.fetch_add(1, Ordering::Relaxed);
    saturating_add_bytes(&BTREE_OWNED_PAYLOAD_MATERIALIZATION_BYTES, bytes);
}

pub(crate) fn record_overflow_chain_reassembly(
    local_bytes: usize,
    overflow_bytes: usize,
    overflow_page_reads: usize,
) {
    if !copy_profile_enabled() {
        return;
    }
    BTREE_OVERFLOW_REASSEMBLY_CALLS.fetch_add(1, Ordering::Relaxed);
    saturating_add_bytes(&BTREE_OVERFLOW_LOCAL_BYTES, local_bytes);
    saturating_add_bytes(&BTREE_OVERFLOW_BYTES, overflow_bytes);
    saturating_add_bytes(&BTREE_OVERFLOW_PAGE_READS, overflow_page_reads);
}

pub(crate) fn record_table_leaf_cell_assembly(bytes: usize) {
    if !copy_profile_enabled() {
        return;
    }
    BTREE_TABLE_LEAF_CELL_ASSEMBLY_CALLS.fetch_add(1, Ordering::Relaxed);
    saturating_add_bytes(&BTREE_TABLE_LEAF_CELL_ASSEMBLY_BYTES, bytes);
}

pub(crate) fn record_index_leaf_cell_assembly(bytes: usize) {
    if !copy_profile_enabled() {
        return;
    }
    BTREE_INDEX_LEAF_CELL_ASSEMBLY_CALLS.fetch_add(1, Ordering::Relaxed);
    saturating_add_bytes(&BTREE_INDEX_LEAF_CELL_ASSEMBLY_BYTES, bytes);
}

pub(crate) fn record_interior_cell_rebuild(bytes: usize) {
    if !copy_profile_enabled() {
        return;
    }
    BTREE_INTERIOR_CELL_REBUILD_CALLS.fetch_add(1, Ordering::Relaxed);
    saturating_add_bytes(&BTREE_INTERIOR_CELL_REBUILD_BYTES, bytes);
}

#[inline]
pub(crate) fn record_no_split_reuse_hit() {
    if !btree_metrics_enabled() {
        return;
    }
    BTREE_NO_SPLIT_REUSE_HITS.fetch_add(1, Ordering::Relaxed);
}

#[inline]
pub(crate) fn record_conservative_reload_fallback() {
    if !btree_metrics_enabled() {
        return;
    }
    BTREE_CONSERVATIVE_RELOAD_FALLBACKS.fetch_add(1, Ordering::Relaxed);
}

#[inline]
pub(crate) fn record_page_header_rebuild() {
    if !btree_metrics_enabled() {
        return;
    }
    BTREE_PAGE_HEADER_REBUILD_COUNT.fetch_add(1, Ordering::Relaxed);
}

pub(crate) fn record_fast_table_leaf_payload_append_mutate(start: Option<std::time::Instant>) {
    let Some(duration_ns) = profile_elapsed_ns(start) else {
        return;
    };
    BTREE_FAST_TABLE_LEAF_PAYLOAD_APPEND_CALLS.fetch_add(1, Ordering::Relaxed);
    BTREE_FAST_TABLE_LEAF_PAYLOAD_MUTATE_TIME_NS.fetch_add(duration_ns, Ordering::Relaxed);
}

pub(crate) fn record_fast_table_leaf_payload_append_stage(start: Option<std::time::Instant>) {
    let Some(duration_ns) = profile_elapsed_ns(start) else {
        return;
    };
    BTREE_FAST_TABLE_LEAF_PAYLOAD_STAGE_TIME_NS.fetch_add(duration_ns, Ordering::Relaxed);
}

pub(crate) fn record_fast_table_leaf_full_cell_append_mutate(start: Option<std::time::Instant>) {
    let Some(duration_ns) = profile_elapsed_ns(start) else {
        return;
    };
    BTREE_FAST_TABLE_LEAF_FULL_CELL_APPEND_CALLS.fetch_add(1, Ordering::Relaxed);
    BTREE_FAST_TABLE_LEAF_FULL_CELL_MUTATE_TIME_NS.fetch_add(duration_ns, Ordering::Relaxed);
}

pub(crate) fn record_fast_table_leaf_full_cell_append_stage(start: Option<std::time::Instant>) {
    let Some(duration_ns) = profile_elapsed_ns(start) else {
        return;
    };
    BTREE_FAST_TABLE_LEAF_FULL_CELL_STAGE_TIME_NS.fetch_add(duration_ns, Ordering::Relaxed);
}

pub(crate) fn record_quick_balance_attempt(start: Option<std::time::Instant>, hit: bool) {
    let Some(duration_ns) = profile_elapsed_ns(start) else {
        return;
    };
    BTREE_QUICK_BALANCE_ATTEMPTS.fetch_add(1, Ordering::Relaxed);
    if hit {
        BTREE_QUICK_BALANCE_HITS.fetch_add(1, Ordering::Relaxed);
    }
    BTREE_QUICK_BALANCE_TIME_NS.fetch_add(duration_ns, Ordering::Relaxed);
}

pub(crate) fn record_local_split_attempt(start: Option<std::time::Instant>, hit: bool) {
    let Some(duration_ns) = profile_elapsed_ns(start) else {
        return;
    };
    BTREE_LOCAL_SPLIT_ATTEMPTS.fetch_add(1, Ordering::Relaxed);
    if hit {
        BTREE_LOCAL_SPLIT_HITS.fetch_add(1, Ordering::Relaxed);
    }
    BTREE_LOCAL_SPLIT_TIME_NS.fetch_add(duration_ns, Ordering::Relaxed);
}

pub(crate) fn record_nonroot_balance(start: Option<std::time::Instant>) {
    let Some(duration_ns) = profile_elapsed_ns(start) else {
        return;
    };
    BTREE_NONROOT_BALANCE_CALLS.fetch_add(1, Ordering::Relaxed);
    BTREE_NONROOT_BALANCE_TIME_NS.fetch_add(duration_ns, Ordering::Relaxed);
}

pub(crate) fn record_delete_leaf_run_materialize(start: Option<std::time::Instant>) {
    let Some(duration_ns) = profile_elapsed_ns(start) else {
        return;
    };
    BTREE_DELETE_LEAF_RUN_MATERIALIZE_CALLS.fetch_add(1, Ordering::Relaxed);
    BTREE_DELETE_LEAF_RUN_MATERIALIZE_TIME_NS.fetch_add(duration_ns, Ordering::Relaxed);
}

pub(crate) fn record_delete_leaf_run_write(start: Option<std::time::Instant>) {
    let Some(duration_ns) = profile_elapsed_ns(start) else {
        return;
    };
    BTREE_DELETE_LEAF_RUN_WRITE_CALLS.fetch_add(1, Ordering::Relaxed);
    BTREE_DELETE_LEAF_RUN_WRITE_TIME_NS.fetch_add(duration_ns, Ordering::Relaxed);
}

pub(crate) fn record_delete_leaf_run_search(start: Option<std::time::Instant>) {
    let Some(duration_ns) = profile_elapsed_ns(start) else {
        return;
    };
    BTREE_DELETE_LEAF_RUN_SEARCH_CALLS.fetch_add(1, Ordering::Relaxed);
    BTREE_DELETE_LEAF_RUN_SEARCH_TIME_NS.fetch_add(duration_ns, Ordering::Relaxed);
}

pub(crate) fn record_delete_leaf_run_duplicate_check(start: Option<std::time::Instant>) {
    let Some(duration_ns) = profile_elapsed_ns(start) else {
        return;
    };
    BTREE_DELETE_LEAF_RUN_DUPLICATE_CHECK_CALLS.fetch_add(1, Ordering::Relaxed);
    BTREE_DELETE_LEAF_RUN_DUPLICATE_CHECK_TIME_NS.fetch_add(duration_ns, Ordering::Relaxed);
}

pub(crate) fn record_delete_leaf_run_compact_check(start: Option<std::time::Instant>) {
    let Some(duration_ns) = profile_elapsed_ns(start) else {
        return;
    };
    BTREE_DELETE_LEAF_RUN_COMPACT_CHECK_CALLS.fetch_add(1, Ordering::Relaxed);
    BTREE_DELETE_LEAF_RUN_COMPACT_CHECK_TIME_NS.fetch_add(duration_ns, Ordering::Relaxed);
}

pub(crate) fn record_delete_leaf_run_cell_parse(start: Option<std::time::Instant>) {
    let Some(duration_ns) = profile_elapsed_ns(start) else {
        return;
    };
    BTREE_DELETE_LEAF_RUN_CELL_PARSE_CALLS.fetch_add(1, Ordering::Relaxed);
    BTREE_DELETE_LEAF_RUN_CELL_PARSE_TIME_NS.fetch_add(duration_ns, Ordering::Relaxed);
}

pub(crate) fn record_bulk_table_grouping(start: Option<std::time::Instant>) {
    let Some(duration_ns) = profile_elapsed_ns(start) else {
        return;
    };
    BTREE_BULK_TABLE_GROUPING_CALLS.fetch_add(1, Ordering::Relaxed);
    BTREE_BULK_TABLE_GROUPING_TIME_NS.fetch_add(duration_ns, Ordering::Relaxed);
}

pub(crate) fn record_bulk_table_leaf_page_build(start: Option<std::time::Instant>) {
    let Some(duration_ns) = profile_elapsed_ns(start) else {
        return;
    };
    BTREE_BULK_TABLE_LEAF_PAGE_BUILD_CALLS.fetch_add(1, Ordering::Relaxed);
    BTREE_BULK_TABLE_LEAF_PAGE_BUILD_TIME_NS.fetch_add(duration_ns, Ordering::Relaxed);
}

pub(crate) fn record_bulk_table_leaf_page_write(start: Option<std::time::Instant>) {
    let Some(duration_ns) = profile_elapsed_ns(start) else {
        return;
    };
    BTREE_BULK_TABLE_LEAF_PAGE_WRITE_CALLS.fetch_add(1, Ordering::Relaxed);
    BTREE_BULK_TABLE_LEAF_PAGE_WRITE_TIME_NS.fetch_add(duration_ns, Ordering::Relaxed);
}

pub(crate) fn record_bulk_table_interior_page_build(start: Option<std::time::Instant>) {
    let Some(duration_ns) = profile_elapsed_ns(start) else {
        return;
    };
    BTREE_BULK_TABLE_INTERIOR_PAGE_BUILD_CALLS.fetch_add(1, Ordering::Relaxed);
    BTREE_BULK_TABLE_INTERIOR_PAGE_BUILD_TIME_NS.fetch_add(duration_ns, Ordering::Relaxed);
}

pub(crate) fn record_bulk_table_interior_page_write(start: Option<std::time::Instant>) {
    let Some(duration_ns) = profile_elapsed_ns(start) else {
        return;
    };
    BTREE_BULK_TABLE_INTERIOR_PAGE_WRITE_CALLS.fetch_add(1, Ordering::Relaxed);
    BTREE_BULK_TABLE_INTERIOR_PAGE_WRITE_TIME_NS.fetch_add(duration_ns, Ordering::Relaxed);
}

#[inline]
pub(crate) fn record_split_event() {
    if !btree_metrics_enabled() {
        return;
    }
    BTREE_PAGE_SPLITS_TOTAL.fetch_add(1, Ordering::Relaxed);
}

#[inline]
pub(crate) fn set_depth_gauge(depth: usize) {
    if !btree_metrics_enabled() {
        return;
    }
    let depth_u64 = u64::try_from(depth).unwrap_or(u64::MAX);
    BTREE_DEPTH_GAUGE.store(depth_u64, Ordering::Relaxed);
}

/// Record a Swiss Table probe (lookup/insert/remove).
pub fn record_swiss_probe() {
    SWISS_TABLE_PROBES_TOTAL.fetch_add(1, Ordering::Relaxed);
}

/// Set Swiss Table load factor (scaled by 1000).
pub fn set_swiss_load_factor(load_factor_milli: u64) {
    SWISS_TABLE_LOAD_FACTOR.store(load_factor_milli, Ordering::Relaxed);
}

/// Record a successful swizzle-in event and emit a tracing span.
pub fn record_swizzle_in(page_id: u64) {
    SWIZZLE_IN_TOTAL.fetch_add(1, Ordering::Relaxed);
    let _span = tracing::trace_span!(
        "swizzle",
        page_id,
        swizzled_in = true,
        unswizzled_out = false,
    )
    .entered();
}

/// Record a successful unswizzle-out event and emit a tracing span.
pub fn record_swizzle_out(page_id: u64) {
    SWIZZLE_OUT_TOTAL.fetch_add(1, Ordering::Relaxed);
    let _span = tracing::trace_span!(
        "swizzle",
        page_id,
        swizzled_in = false,
        unswizzled_out = true,
    )
    .entered();
}

/// Record a swizzle fault (CAS failure or retry).
pub fn record_swizzle_fault() {
    SWIZZLE_FAULTS_TOTAL.fetch_add(1, Ordering::Relaxed);
}

/// Update the swizzle ratio gauge (0–1000, where 1000 = 100% swizzled).
pub fn set_swizzle_ratio(ratio_milli: u64) {
    SWIZZLE_RATIO_GAUGE.store(ratio_milli, Ordering::Relaxed);
}

/// Return a snapshot of B-tree observability counters.
#[must_use]
pub fn btree_metrics_snapshot() -> BtreeMetricsSnapshot {
    BtreeMetricsSnapshot {
        fsqlite_btree_operations_total: BtreeOperationTotals {
            seek: BTREE_OP_SEEK_TOTAL.load(Ordering::Relaxed),
            insert: BTREE_OP_INSERT_TOTAL.load(Ordering::Relaxed),
            delete: BTREE_OP_DELETE_TOTAL.load(Ordering::Relaxed),
        },
        fsqlite_btree_page_splits_total: BTREE_PAGE_SPLITS_TOTAL.load(Ordering::Relaxed),
        fsqlite_btree_depth: BTREE_DEPTH_GAUGE.load(Ordering::Relaxed),
        fsqlite_swiss_table_probes_total: SWISS_TABLE_PROBES_TOTAL.load(Ordering::Relaxed),
        fsqlite_swiss_table_load_factor: SWISS_TABLE_LOAD_FACTOR.load(Ordering::Relaxed),
        fsqlite_swizzle_ratio: SWIZZLE_RATIO_GAUGE.load(Ordering::Relaxed),
        fsqlite_swizzle_faults_total: SWIZZLE_FAULTS_TOTAL.load(Ordering::Relaxed),
        fsqlite_swizzle_in_total: SWIZZLE_IN_TOTAL.load(Ordering::Relaxed),
        fsqlite_swizzle_out_total: SWIZZLE_OUT_TOTAL.load(Ordering::Relaxed),
    }
}

#[must_use]
pub fn btree_copy_profile_snapshot() -> BtreeCopyProfileSnapshot {
    BtreeCopyProfileSnapshot {
        local_payload_copy_calls: BTREE_LOCAL_PAYLOAD_COPY_CALLS.load(Ordering::Relaxed),
        local_payload_copy_bytes: BTREE_LOCAL_PAYLOAD_COPY_BYTES.load(Ordering::Relaxed),
        owned_payload_materialization_calls: BTREE_OWNED_PAYLOAD_MATERIALIZATION_CALLS
            .load(Ordering::Relaxed),
        owned_payload_materialization_bytes: BTREE_OWNED_PAYLOAD_MATERIALIZATION_BYTES
            .load(Ordering::Relaxed),
        overflow_chain_reassembly_calls: BTREE_OVERFLOW_REASSEMBLY_CALLS.load(Ordering::Relaxed),
        overflow_chain_local_bytes: BTREE_OVERFLOW_LOCAL_BYTES.load(Ordering::Relaxed),
        overflow_chain_overflow_bytes: BTREE_OVERFLOW_BYTES.load(Ordering::Relaxed),
        overflow_page_reads: BTREE_OVERFLOW_PAGE_READS.load(Ordering::Relaxed),
        table_leaf_cell_assembly_calls: BTREE_TABLE_LEAF_CELL_ASSEMBLY_CALLS
            .load(Ordering::Relaxed),
        table_leaf_cell_assembly_bytes: BTREE_TABLE_LEAF_CELL_ASSEMBLY_BYTES
            .load(Ordering::Relaxed),
        index_leaf_cell_assembly_calls: BTREE_INDEX_LEAF_CELL_ASSEMBLY_CALLS
            .load(Ordering::Relaxed),
        index_leaf_cell_assembly_bytes: BTREE_INDEX_LEAF_CELL_ASSEMBLY_BYTES
            .load(Ordering::Relaxed),
        interior_cell_rebuild_calls: BTREE_INTERIOR_CELL_REBUILD_CALLS.load(Ordering::Relaxed),
        interior_cell_rebuild_bytes: BTREE_INTERIOR_CELL_REBUILD_BYTES.load(Ordering::Relaxed),
    }
}

#[must_use]
pub fn btree_leaf_reuse_snapshot() -> BtreeLeafReuseSnapshot {
    BtreeLeafReuseSnapshot {
        no_split_reuse_hits: BTREE_NO_SPLIT_REUSE_HITS.load(Ordering::Relaxed),
        conservative_reload_fallbacks: BTREE_CONSERVATIVE_RELOAD_FALLBACKS.load(Ordering::Relaxed),
        page_header_rebuild_count: BTREE_PAGE_HEADER_REBUILD_COUNT.load(Ordering::Relaxed),
        fast_table_leaf_payload_appends: BTREE_FAST_TABLE_LEAF_PAYLOAD_APPEND_CALLS
            .load(Ordering::Relaxed),
        fast_table_leaf_payload_mutate_time_ns: BTREE_FAST_TABLE_LEAF_PAYLOAD_MUTATE_TIME_NS
            .load(Ordering::Relaxed),
        fast_table_leaf_payload_stage_time_ns: BTREE_FAST_TABLE_LEAF_PAYLOAD_STAGE_TIME_NS
            .load(Ordering::Relaxed),
        fast_table_leaf_full_cell_appends: BTREE_FAST_TABLE_LEAF_FULL_CELL_APPEND_CALLS
            .load(Ordering::Relaxed),
        fast_table_leaf_full_cell_mutate_time_ns: BTREE_FAST_TABLE_LEAF_FULL_CELL_MUTATE_TIME_NS
            .load(Ordering::Relaxed),
        fast_table_leaf_full_cell_stage_time_ns: BTREE_FAST_TABLE_LEAF_FULL_CELL_STAGE_TIME_NS
            .load(Ordering::Relaxed),
        quick_balance_attempts: BTREE_QUICK_BALANCE_ATTEMPTS.load(Ordering::Relaxed),
        quick_balance_hits: BTREE_QUICK_BALANCE_HITS.load(Ordering::Relaxed),
        quick_balance_time_ns: BTREE_QUICK_BALANCE_TIME_NS.load(Ordering::Relaxed),
        local_split_attempts: BTREE_LOCAL_SPLIT_ATTEMPTS.load(Ordering::Relaxed),
        local_split_hits: BTREE_LOCAL_SPLIT_HITS.load(Ordering::Relaxed),
        local_split_time_ns: BTREE_LOCAL_SPLIT_TIME_NS.load(Ordering::Relaxed),
        nonroot_balance_calls: BTREE_NONROOT_BALANCE_CALLS.load(Ordering::Relaxed),
        nonroot_balance_time_ns: BTREE_NONROOT_BALANCE_TIME_NS.load(Ordering::Relaxed),
        delete_leaf_run_materialize_calls: BTREE_DELETE_LEAF_RUN_MATERIALIZE_CALLS
            .load(Ordering::Relaxed),
        delete_leaf_run_materialize_time_ns: BTREE_DELETE_LEAF_RUN_MATERIALIZE_TIME_NS
            .load(Ordering::Relaxed),
        delete_leaf_run_write_calls: BTREE_DELETE_LEAF_RUN_WRITE_CALLS.load(Ordering::Relaxed),
        delete_leaf_run_write_time_ns: BTREE_DELETE_LEAF_RUN_WRITE_TIME_NS.load(Ordering::Relaxed),
        delete_leaf_run_search_calls: BTREE_DELETE_LEAF_RUN_SEARCH_CALLS.load(Ordering::Relaxed),
        delete_leaf_run_search_time_ns: BTREE_DELETE_LEAF_RUN_SEARCH_TIME_NS
            .load(Ordering::Relaxed),
        delete_leaf_run_duplicate_check_calls: BTREE_DELETE_LEAF_RUN_DUPLICATE_CHECK_CALLS
            .load(Ordering::Relaxed),
        delete_leaf_run_duplicate_check_time_ns: BTREE_DELETE_LEAF_RUN_DUPLICATE_CHECK_TIME_NS
            .load(Ordering::Relaxed),
        delete_leaf_run_compact_check_calls: BTREE_DELETE_LEAF_RUN_COMPACT_CHECK_CALLS
            .load(Ordering::Relaxed),
        delete_leaf_run_compact_check_time_ns: BTREE_DELETE_LEAF_RUN_COMPACT_CHECK_TIME_NS
            .load(Ordering::Relaxed),
        delete_leaf_run_cell_parse_calls: BTREE_DELETE_LEAF_RUN_CELL_PARSE_CALLS
            .load(Ordering::Relaxed),
        delete_leaf_run_cell_parse_time_ns: BTREE_DELETE_LEAF_RUN_CELL_PARSE_TIME_NS
            .load(Ordering::Relaxed),
        bulk_table_grouping_calls: BTREE_BULK_TABLE_GROUPING_CALLS.load(Ordering::Relaxed),
        bulk_table_grouping_time_ns: BTREE_BULK_TABLE_GROUPING_TIME_NS.load(Ordering::Relaxed),
        bulk_table_leaf_page_build_calls: BTREE_BULK_TABLE_LEAF_PAGE_BUILD_CALLS
            .load(Ordering::Relaxed),
        bulk_table_leaf_page_build_time_ns: BTREE_BULK_TABLE_LEAF_PAGE_BUILD_TIME_NS
            .load(Ordering::Relaxed),
        bulk_table_leaf_page_write_calls: BTREE_BULK_TABLE_LEAF_PAGE_WRITE_CALLS
            .load(Ordering::Relaxed),
        bulk_table_leaf_page_write_time_ns: BTREE_BULK_TABLE_LEAF_PAGE_WRITE_TIME_NS
            .load(Ordering::Relaxed),
        bulk_table_interior_page_build_calls: BTREE_BULK_TABLE_INTERIOR_PAGE_BUILD_CALLS
            .load(Ordering::Relaxed),
        bulk_table_interior_page_build_time_ns: BTREE_BULK_TABLE_INTERIOR_PAGE_BUILD_TIME_NS
            .load(Ordering::Relaxed),
        bulk_table_interior_page_write_calls: BTREE_BULK_TABLE_INTERIOR_PAGE_WRITE_CALLS
            .load(Ordering::Relaxed),
        bulk_table_interior_page_write_time_ns: BTREE_BULK_TABLE_INTERIOR_PAGE_WRITE_TIME_NS
            .load(Ordering::Relaxed),
    }
}

/// Reset all B-tree observability counters.
pub fn reset_btree_metrics() {
    BTREE_OP_SEEK_TOTAL.store(0, Ordering::Relaxed);
    BTREE_OP_INSERT_TOTAL.store(0, Ordering::Relaxed);
    BTREE_OP_DELETE_TOTAL.store(0, Ordering::Relaxed);
    BTREE_PAGE_SPLITS_TOTAL.store(0, Ordering::Relaxed);
    BTREE_DEPTH_GAUGE.store(0, Ordering::Relaxed);
    SWISS_TABLE_PROBES_TOTAL.store(0, Ordering::Relaxed);
    SWISS_TABLE_LOAD_FACTOR.store(0, Ordering::Relaxed);
    SWIZZLE_RATIO_GAUGE.store(0, Ordering::Relaxed);
    SWIZZLE_FAULTS_TOTAL.store(0, Ordering::Relaxed);
    SWIZZLE_IN_TOTAL.store(0, Ordering::Relaxed);
    SWIZZLE_OUT_TOTAL.store(0, Ordering::Relaxed);
}

pub fn reset_btree_copy_profile() {
    BTREE_LOCAL_PAYLOAD_COPY_CALLS.store(0, Ordering::Relaxed);
    BTREE_LOCAL_PAYLOAD_COPY_BYTES.store(0, Ordering::Relaxed);
    BTREE_OWNED_PAYLOAD_MATERIALIZATION_CALLS.store(0, Ordering::Relaxed);
    BTREE_OWNED_PAYLOAD_MATERIALIZATION_BYTES.store(0, Ordering::Relaxed);
    BTREE_OVERFLOW_REASSEMBLY_CALLS.store(0, Ordering::Relaxed);
    BTREE_OVERFLOW_LOCAL_BYTES.store(0, Ordering::Relaxed);
    BTREE_OVERFLOW_BYTES.store(0, Ordering::Relaxed);
    BTREE_OVERFLOW_PAGE_READS.store(0, Ordering::Relaxed);
    BTREE_TABLE_LEAF_CELL_ASSEMBLY_CALLS.store(0, Ordering::Relaxed);
    BTREE_TABLE_LEAF_CELL_ASSEMBLY_BYTES.store(0, Ordering::Relaxed);
    BTREE_INDEX_LEAF_CELL_ASSEMBLY_CALLS.store(0, Ordering::Relaxed);
    BTREE_INDEX_LEAF_CELL_ASSEMBLY_BYTES.store(0, Ordering::Relaxed);
    BTREE_INTERIOR_CELL_REBUILD_CALLS.store(0, Ordering::Relaxed);
    BTREE_INTERIOR_CELL_REBUILD_BYTES.store(0, Ordering::Relaxed);
}

pub fn reset_btree_leaf_reuse_profile() {
    BTREE_NO_SPLIT_REUSE_HITS.store(0, Ordering::Relaxed);
    BTREE_CONSERVATIVE_RELOAD_FALLBACKS.store(0, Ordering::Relaxed);
    BTREE_PAGE_HEADER_REBUILD_COUNT.store(0, Ordering::Relaxed);
    BTREE_FAST_TABLE_LEAF_PAYLOAD_APPEND_CALLS.store(0, Ordering::Relaxed);
    BTREE_FAST_TABLE_LEAF_PAYLOAD_MUTATE_TIME_NS.store(0, Ordering::Relaxed);
    BTREE_FAST_TABLE_LEAF_PAYLOAD_STAGE_TIME_NS.store(0, Ordering::Relaxed);
    BTREE_FAST_TABLE_LEAF_FULL_CELL_APPEND_CALLS.store(0, Ordering::Relaxed);
    BTREE_FAST_TABLE_LEAF_FULL_CELL_MUTATE_TIME_NS.store(0, Ordering::Relaxed);
    BTREE_FAST_TABLE_LEAF_FULL_CELL_STAGE_TIME_NS.store(0, Ordering::Relaxed);
    BTREE_QUICK_BALANCE_ATTEMPTS.store(0, Ordering::Relaxed);
    BTREE_QUICK_BALANCE_HITS.store(0, Ordering::Relaxed);
    BTREE_QUICK_BALANCE_TIME_NS.store(0, Ordering::Relaxed);
    BTREE_LOCAL_SPLIT_ATTEMPTS.store(0, Ordering::Relaxed);
    BTREE_LOCAL_SPLIT_HITS.store(0, Ordering::Relaxed);
    BTREE_LOCAL_SPLIT_TIME_NS.store(0, Ordering::Relaxed);
    BTREE_NONROOT_BALANCE_CALLS.store(0, Ordering::Relaxed);
    BTREE_NONROOT_BALANCE_TIME_NS.store(0, Ordering::Relaxed);
    BTREE_DELETE_LEAF_RUN_MATERIALIZE_CALLS.store(0, Ordering::Relaxed);
    BTREE_DELETE_LEAF_RUN_MATERIALIZE_TIME_NS.store(0, Ordering::Relaxed);
    BTREE_DELETE_LEAF_RUN_WRITE_CALLS.store(0, Ordering::Relaxed);
    BTREE_DELETE_LEAF_RUN_WRITE_TIME_NS.store(0, Ordering::Relaxed);
    BTREE_DELETE_LEAF_RUN_SEARCH_CALLS.store(0, Ordering::Relaxed);
    BTREE_DELETE_LEAF_RUN_SEARCH_TIME_NS.store(0, Ordering::Relaxed);
    BTREE_DELETE_LEAF_RUN_DUPLICATE_CHECK_CALLS.store(0, Ordering::Relaxed);
    BTREE_DELETE_LEAF_RUN_DUPLICATE_CHECK_TIME_NS.store(0, Ordering::Relaxed);
    BTREE_DELETE_LEAF_RUN_COMPACT_CHECK_CALLS.store(0, Ordering::Relaxed);
    BTREE_DELETE_LEAF_RUN_COMPACT_CHECK_TIME_NS.store(0, Ordering::Relaxed);
    BTREE_DELETE_LEAF_RUN_CELL_PARSE_CALLS.store(0, Ordering::Relaxed);
    BTREE_DELETE_LEAF_RUN_CELL_PARSE_TIME_NS.store(0, Ordering::Relaxed);
    BTREE_BULK_TABLE_GROUPING_CALLS.store(0, Ordering::Relaxed);
    BTREE_BULK_TABLE_GROUPING_TIME_NS.store(0, Ordering::Relaxed);
    BTREE_BULK_TABLE_LEAF_PAGE_BUILD_CALLS.store(0, Ordering::Relaxed);
    BTREE_BULK_TABLE_LEAF_PAGE_BUILD_TIME_NS.store(0, Ordering::Relaxed);
    BTREE_BULK_TABLE_LEAF_PAGE_WRITE_CALLS.store(0, Ordering::Relaxed);
    BTREE_BULK_TABLE_LEAF_PAGE_WRITE_TIME_NS.store(0, Ordering::Relaxed);
    BTREE_BULK_TABLE_INTERIOR_PAGE_BUILD_CALLS.store(0, Ordering::Relaxed);
    BTREE_BULK_TABLE_INTERIOR_PAGE_BUILD_TIME_NS.store(0, Ordering::Relaxed);
    BTREE_BULK_TABLE_INTERIOR_PAGE_WRITE_CALLS.store(0, Ordering::Relaxed);
    BTREE_BULK_TABLE_INTERIOR_PAGE_WRITE_TIME_NS.store(0, Ordering::Relaxed);
}

#[cfg(test)]
pub(crate) static LEAF_REUSE_TEST_LOCK: std::sync::LazyLock<std::sync::Mutex<()>> =
    std::sync::LazyLock::new(|| std::sync::Mutex::new(()));

/// Serializes any test that flips `FSQLITE_BTREE_METRICS_ENABLED`.
///
/// The gate is process-global, so two parallel tests that flip it would race
/// — one disabling the gate mid-run of another, causing the latter's expected
/// counter advances to be lost. All tests that depend on a specific
/// gate-on/gate-off state must hold this mutex for the full enable→work→read
/// span.
#[cfg(test)]
pub(crate) static BTREE_METRICS_TEST_LOCK: std::sync::LazyLock<std::sync::Mutex<()>> =
    std::sync::LazyLock::new(|| std::sync::Mutex::new(()));

#[cfg(test)]
pub(crate) static CONFLICT_TOPOLOGY_POLICY_TEST_LOCK: std::sync::LazyLock<std::sync::Mutex<()>> =
    std::sync::LazyLock::new(|| std::sync::Mutex::new(()));

#[cfg(test)]
mod tests {
    use super::{
        BtreeOpType, ConflictTopologyPolicyMode, adaptive_fill_factor_enabled,
        adaptive_fill_factor_policy_id, adaptive_fill_factor_target, btree_copy_profile_snapshot,
        btree_leaf_reuse_snapshot, btree_metrics_snapshot, conflict_topology_split_advice,
        record_conflict_topology_heat, record_conservative_reload_fallback,
        record_no_split_reuse_hit, record_operation, record_page_header_rebuild,
        reset_btree_copy_profile, reset_btree_metrics, reset_conflict_topology_policy_state,
        set_adaptive_fill_factor_enabled, set_btree_copy_profile_enabled,
        set_btree_metrics_enabled, set_conflict_topology_policy_mode,
    };
    use crate::{BtCursor, BtreeCursorOps, MemPageStore};
    use fsqlite_types::PageNumber;
    use fsqlite_types::cx::Cx;
    use std::collections::BTreeSet;
    use std::sync::{LazyLock, Mutex};

    const TEST_USABLE: u32 = 4096;
    static COPY_PROFILE_TEST_LOCK: LazyLock<Mutex<()>> = LazyLock::new(|| Mutex::new(()));

    #[test]
    fn adaptive_fill_factor_disabled_is_a_noop() {
        let _guard = super::CONFLICT_TOPOLOGY_POLICY_TEST_LOCK
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        set_adaptive_fill_factor_enabled(false);

        // Disabled: every side/heat returns the topology target verbatim, so the
        // baseline/topology split policy is byte-identical to pre-adaptive code.
        for heat in [0_u64, 2, 3, 32, 64, 4096] {
            assert_eq!(
                adaptive_fill_factor_target("right_edge", 6_500, heat),
                6_500
            );
            assert_eq!(adaptive_fill_factor_target("left_edge", 4_500, heat), 4_500);
            assert_eq!(adaptive_fill_factor_target("interior", 5_000, heat), 5_000);
        }

        set_adaptive_fill_factor_enabled(false);
    }

    #[test]
    fn adaptive_fill_factor_ramps_monotonically_within_bounds() {
        let _guard = super::CONFLICT_TOPOLOGY_POLICY_TEST_LOCK
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        set_adaptive_fill_factor_enabled(true);

        // At/below the knee heat the ramp adds nothing (matches the flat topology shift).
        assert_eq!(adaptive_fill_factor_target("right_edge", 8_000, 0), 8_000);
        assert_eq!(adaptive_fill_factor_target("right_edge", 8_000, 2), 8_000);

        // Right-edge: extra shift grows with heat, monotonically, clamped to the ceiling.
        let mut prev = 8_000;
        for heat in [3_u64, 16, 33, 50, 64] {
            let target = adaptive_fill_factor_target("right_edge", 8_000, heat);
            assert!(
                target >= prev,
                "right-edge target must be monotonic in heat"
            );
            assert!(
                target <= 9_000,
                "right-edge target must respect the ceiling"
            );
            prev = target;
        }
        // Saturation heat yields the full extra shift, then clamps flat above it.
        assert_eq!(adaptive_fill_factor_target("right_edge", 8_000, 64), 9_000);
        assert_eq!(
            adaptive_fill_factor_target("right_edge", 8_000, 10_000),
            9_000
        );

        // Left-edge: target descends with heat, clamped to the floor.
        let mut prev = 4_500;
        for heat in [3_u64, 16, 33, 50, 64] {
            let target = adaptive_fill_factor_target("left_edge", 4_500, heat);
            assert!(target <= prev, "left-edge target must be monotonic in heat");
            assert!(target >= 1_500, "left-edge target must respect the floor");
            prev = target;
        }
        assert_eq!(adaptive_fill_factor_target("left_edge", 4_500, 64), 3_000);

        // Interior carries no directional bias regardless of heat.
        assert_eq!(adaptive_fill_factor_target("interior", 5_000, 64), 5_000);

        set_adaptive_fill_factor_enabled(false);
    }

    #[test]
    fn adaptive_fill_factor_extra_shift_is_bounded_and_proportional() {
        // Pure ramp function: zero at/below the knee, full at saturation, clamped above.
        assert_eq!(super::adaptive_fill_factor_extra_shift_bps(0), 0);
        assert_eq!(super::adaptive_fill_factor_extra_shift_bps(2), 0);
        assert!(super::adaptive_fill_factor_extra_shift_bps(3) > 0);
        assert_eq!(super::adaptive_fill_factor_extra_shift_bps(64), 1_500);
        assert_eq!(super::adaptive_fill_factor_extra_shift_bps(u64::MAX), 1_500);
        // Midpoint heat lands near half the maximum extra shift.
        let mid = super::adaptive_fill_factor_extra_shift_bps(33);
        assert!((720..=780).contains(&mid), "midpoint extra shift was {mid}");
    }

    #[test]
    fn adaptive_fill_factor_kill_switch_reverts_to_baseline() {
        let _guard = super::CONFLICT_TOPOLOGY_POLICY_TEST_LOCK
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);

        set_adaptive_fill_factor_enabled(true);
        assert!(adaptive_fill_factor_enabled());
        let hot = adaptive_fill_factor_target("right_edge", 8_000, 64);
        assert_eq!(hot, 9_000);

        // Operator override flips it off: behavior reverts to the topology target.
        set_adaptive_fill_factor_enabled(false);
        assert!(!adaptive_fill_factor_enabled());
        assert_eq!(adaptive_fill_factor_target("right_edge", 8_000, 64), 8_000);

        assert_eq!(
            adaptive_fill_factor_policy_id(),
            "btree.adaptive_fill_factor.v1"
        );
    }

    #[test]
    fn adaptive_fill_factor_env_flag_parses_operator_labels() {
        // Affirmative operator labels enable adaptive control (case- and
        // whitespace-insensitive).
        for raw in ["on", "ON", "true", "True", "1", "enforced", "  enforced  "] {
            assert_eq!(
                super::parse_adaptive_fill_factor_flag(raw),
                Some(true),
                "{raw:?} should enable"
            );
        }
        // Kill-switch / negative labels disable it.
        for raw in [
            "off",
            "OFF",
            "false",
            "False",
            "0",
            "baseline",
            "  baseline  ",
        ] {
            assert_eq!(
                super::parse_adaptive_fill_factor_flag(raw),
                Some(false),
                "{raw:?} should disable"
            );
        }
        // Unrecognized values are ignored (None => leave the current setting untouched),
        // including the topology mode's "2"/"advisory"/"shadow" which are not flags here.
        for raw in ["", "yes", "2", "advisory", "shadow", "maybe", "enable"] {
            assert_eq!(
                super::parse_adaptive_fill_factor_flag(raw),
                None,
                "{raw:?} should be ignored"
            );
        }
    }

    #[test]
    fn hot_page_deflection_status_labels_and_predicates() {
        // as_str / is_active / is_applied are exercised via the split-advice
        // wrapper, but the enum's own per-variant contract was never directly
        // asserted.
        use crate::HotPageDeflectionStatus::{
            AdvisoryOnly, Applied, BudgetExhausted, Inactive, OperatorOverride,
        };

        assert_eq!(Inactive.as_str(), "inactive");
        assert_eq!(AdvisoryOnly.as_str(), "advisory_only");
        assert_eq!(Applied.as_str(), "applied");
        assert_eq!(BudgetExhausted.as_str(), "budget_exhausted");
        assert_eq!(OperatorOverride.as_str(), "operator_override");

        // is_active: the three pathological-hotspot states are active; Inactive
        // and OperatorOverride (forced baseline) are not.
        assert!(AdvisoryOnly.is_active() && Applied.is_active() && BudgetExhausted.is_active());
        assert!(!Inactive.is_active() && !OperatorOverride.is_active());

        // is_applied: only Applied consumed a bounded deflection credit.
        assert!(Applied.is_applied());
        for s in [Inactive, AdvisoryOnly, BudgetExhausted, OperatorOverride] {
            assert!(!s.is_applied());
        }
    }

    #[test]
    fn btree_op_type_as_str_labels() {
        // Metric-dimension labels for fsqlite_btree_operations_total; the enum's
        // as_str was never directly asserted.
        use crate::BtreeOpType::{Delete, Insert, Seek};
        assert_eq!(Seek.as_str(), "seek");
        assert_eq!(Insert.as_str(), "insert");
        assert_eq!(Delete.as_str(), "delete");
    }

    #[test]
    fn conflict_topology_policy_mode_labels_and_raw_round_trip() {
        // as_str labels (pub) plus the to_raw/from_raw codec that stashes the
        // mode in a u64 atomic. as_str was never directly asserted, and
        // from_raw's out-of-range catch-all (-> Enforced) guards a corrupt
        // stored value.
        use crate::ConflictTopologyPolicyMode::{Advisory, Baseline, Enforced};

        assert_eq!(Baseline.as_str(), "baseline");
        assert_eq!(Advisory.as_str(), "advisory");
        assert_eq!(Enforced.as_str(), "enforced");

        // Exact raw codes and a clean round-trip for every variant.
        assert_eq!(Baseline.to_raw(), 0);
        assert_eq!(Advisory.to_raw(), 1);
        assert_eq!(Enforced.to_raw(), 2);
        for mode in [Baseline, Advisory, Enforced] {
            assert_eq!(ConflictTopologyPolicyMode::from_raw(mode.to_raw()), mode);
        }

        // from_raw maps any out-of-range value to Enforced (defensive default).
        assert_eq!(ConflictTopologyPolicyMode::from_raw(99), Enforced);
    }

    #[test]
    fn conflict_topology_advice_applies_hot_right_edge_fill_shift() {
        let _guard = super::CONFLICT_TOPOLOGY_POLICY_TEST_LOCK
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        let page = PageNumber::new(42).expect("page");
        set_conflict_topology_policy_mode(ConflictTopologyPolicyMode::Enforced);
        reset_conflict_topology_policy_state();

        record_conflict_topology_heat(page, 2, 3);
        let advice = conflict_topology_split_advice(page, "right_edge", 6_500);

        assert!(advice.topology_hot);
        assert!(advice.applied);
        assert_eq!(advice.policy_mode, ConflictTopologyPolicyMode::Enforced);
        assert_eq!(advice.effective_target_left_basis_points, 8_000);
        assert_eq!(advice.placement_policy(), "topology_aware_fill_factor");
        assert_eq!(advice.predicted_overlap_delta, 3);

        reset_conflict_topology_policy_state();
        set_conflict_topology_policy_mode(ConflictTopologyPolicyMode::Enforced);
    }

    #[test]
    fn conflict_topology_baseline_mode_is_kill_switch() {
        let _guard = super::CONFLICT_TOPOLOGY_POLICY_TEST_LOCK
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        let page = PageNumber::new(43).expect("page");
        set_conflict_topology_policy_mode(ConflictTopologyPolicyMode::Enforced);
        reset_conflict_topology_policy_state();
        record_conflict_topology_heat(page, 10, 8);

        set_conflict_topology_policy_mode(ConflictTopologyPolicyMode::Baseline);
        let advice = conflict_topology_split_advice(page, "right_edge", 6_500);

        assert_eq!(advice.policy_mode, ConflictTopologyPolicyMode::Baseline);
        assert!(!advice.applied);
        assert_eq!(advice.effective_target_left_basis_points, 6_500);
        assert!(advice.operator_override_active);

        reset_conflict_topology_policy_state();
        set_conflict_topology_policy_mode(ConflictTopologyPolicyMode::Enforced);
    }

    #[test]
    fn hot_page_deflection_synthetic_pathological_hotspot_reduces_projected_overlap() {
        let _guard = super::CONFLICT_TOPOLOGY_POLICY_TEST_LOCK
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        let page = PageNumber::new(45).expect("page");
        set_conflict_topology_policy_mode(ConflictTopologyPolicyMode::Enforced);
        reset_conflict_topology_policy_state();

        for _ in 0..64 {
            record_conflict_topology_heat(page, 1, 4);
        }

        let first = conflict_topology_split_advice(page, "right_edge", 6_500);
        assert!(first.topology_hot);
        assert!(first.deflection_active());
        assert!(first.deflection_applied());
        assert_eq!(first.deflection_credits_before, 2);
        assert_eq!(first.deflection_credits_after, 1);
        assert_eq!(first.effective_target_left_basis_points, 9_000);
        assert_eq!(first.trigger_reason, "pathological_hot_page");
        assert_eq!(first.migration_outcome, "split_deflected");
        assert_eq!(first.rollback_reason, "none");
        assert_eq!(first.budget_pages, 2);
        assert_eq!(first.budget_ns, 0);
        assert!(first.heat_after < first.heat_before);
        assert_eq!(first.predicted_overlap_delta, 8);

        let second = conflict_topology_split_advice(page, "right_edge", 6_500);
        assert!(second.deflection_applied());
        assert_eq!(second.deflection_credits_before, 1);
        assert_eq!(second.deflection_credits_after, 0);
        assert_eq!(second.effective_target_left_basis_points, 9_000);

        let exhausted = conflict_topology_split_advice(page, "right_edge", 6_500);
        assert!(exhausted.deflection_active());
        assert!(!exhausted.deflection_applied());
        assert_eq!(exhausted.deflection_credits_before, 0);
        assert_eq!(exhausted.deflection_credits_after, 0);
        assert_eq!(exhausted.effective_target_left_basis_points, 8_000);
        assert_eq!(
            exhausted.trigger_reason,
            "pathological_hot_page_budget_exhausted"
        );
        assert_eq!(exhausted.migration_outcome, "budget_exhausted");
        assert_eq!(exhausted.rollback_reason, "budget_exhausted");

        reset_conflict_topology_policy_state();
        set_conflict_topology_policy_mode(ConflictTopologyPolicyMode::Enforced);
    }

    #[test]
    fn hot_page_deflection_baseline_mode_is_reversible_kill_switch() {
        let _guard = super::CONFLICT_TOPOLOGY_POLICY_TEST_LOCK
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        let page = PageNumber::new(46).expect("page");
        set_conflict_topology_policy_mode(ConflictTopologyPolicyMode::Enforced);
        reset_conflict_topology_policy_state();

        for _ in 0..64 {
            record_conflict_topology_heat(page, 1, 4);
        }

        set_conflict_topology_policy_mode(ConflictTopologyPolicyMode::Baseline);
        let advice = conflict_topology_split_advice(page, "right_edge", 6_500);

        assert!(!advice.applied);
        assert!(!advice.deflection_active());
        assert_eq!(advice.effective_target_left_basis_points, 6_500);
        assert!(advice.operator_override_active);
        assert_eq!(advice.trigger_reason, "operator_override_baseline");
        assert_eq!(advice.migration_outcome, "operator_override_baseline");
        assert_eq!(advice.rollback_reason, "operator_override_baseline");

        reset_conflict_topology_policy_state();
        set_conflict_topology_policy_mode(ConflictTopologyPolicyMode::Enforced);
    }

    fn certification_log_fields(
        scenario_id: &'static str,
        conflict_topology_class: &'static str,
        advice: super::ConflictTopologySplitAdvice,
        throughput_rows_per_s: u64,
        latency_p95_ns: u64,
    ) -> [(&'static str, String); 12] {
        [
            ("trace_id", "bd-1dp9.6.7.13.4-cert".to_owned()),
            ("run_id", "bd-1dp9.6.7.13.4-static-replay".to_owned()),
            ("scenario_id", scenario_id.to_owned()),
            (
                "conflict_topology_class",
                conflict_topology_class.to_owned(),
            ),
            ("policy_mode", advice.policy_mode.as_str().to_owned()),
            ("backend_identity", "file_backed_mvcc".to_owned()),
            ("abort_rate", "0".to_owned()),
            ("latency_p95_ns", latency_p95_ns.to_string()),
            ("throughput_rows_per_s", throughput_rows_per_s.to_string()),
            ("semantic_diff_status", "no_divergence".to_owned()),
            ("artifact_hash", "0".repeat(64)),
            ("first_failure_diag", "none".to_owned()),
        ]
    }

    fn assert_certification_log_fields_complete(fields: &[(&str, String)]) {
        let names = fields
            .iter()
            .map(|(field_name, _value)| *field_name)
            .collect::<BTreeSet<_>>();
        for required in [
            "trace_id",
            "run_id",
            "scenario_id",
            "conflict_topology_class",
            "policy_mode",
            "backend_identity",
            "abort_rate",
            "latency_p95_ns",
            "throughput_rows_per_s",
            "semantic_diff_status",
            "artifact_hash",
            "first_failure_diag",
        ] {
            assert!(names.contains(required), "missing field {required}");
        }

        let artifact_hash = fields
            .iter()
            .find_map(|(field_name, value)| (*field_name == "artifact_hash").then_some(value))
            .expect("artifact hash field");
        assert_eq!(artifact_hash.len(), 64);
        assert!(
            artifact_hash.chars().all(|ch| ch.is_ascii_hexdigit()),
            "artifact hash must be hex"
        );
    }

    #[test]
    fn conflict_topology_certification_matrix_covers_rollout_and_hotspot_states() {
        let _guard = super::CONFLICT_TOPOLOGY_POLICY_TEST_LOCK
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        let page = PageNumber::new(47).expect("page");

        set_conflict_topology_policy_mode(ConflictTopologyPolicyMode::Enforced);
        reset_conflict_topology_policy_state();
        let cold = conflict_topology_split_advice(page, "right_edge", 6_500);
        assert!(!cold.topology_hot);
        assert!(!cold.applied);
        assert_eq!(cold.effective_target_left_basis_points, 6_500);
        assert_eq!(
            cold.deflection_status,
            super::HotPageDeflectionStatus::Inactive
        );

        record_conflict_topology_heat(page, 2, 2);
        let topology_hot = conflict_topology_split_advice(page, "right_edge", 6_500);
        assert!(topology_hot.topology_hot);
        assert!(topology_hot.applied);
        assert_eq!(topology_hot.effective_target_left_basis_points, 8_000);
        assert_eq!(
            topology_hot.deflection_status,
            super::HotPageDeflectionStatus::Inactive
        );

        reset_conflict_topology_policy_state();
        for _ in 0..64 {
            record_conflict_topology_heat(page, 1, 4);
        }
        set_conflict_topology_policy_mode(ConflictTopologyPolicyMode::Advisory);
        let advisory = conflict_topology_split_advice(page, "right_edge", 6_500);
        assert!(advisory.deflection_active());
        assert!(!advisory.applied);
        assert_eq!(advisory.effective_target_left_basis_points, 6_500);
        assert_eq!(advisory.advised_target_left_basis_points, 9_000);
        assert_eq!(
            advisory.deflection_status,
            super::HotPageDeflectionStatus::AdvisoryOnly
        );

        set_conflict_topology_policy_mode(ConflictTopologyPolicyMode::Baseline);
        let baseline = conflict_topology_split_advice(page, "right_edge", 6_500);
        assert!(baseline.operator_override_active);
        assert_eq!(baseline.effective_target_left_basis_points, 6_500);
        assert_eq!(
            baseline.deflection_status,
            super::HotPageDeflectionStatus::OperatorOverride
        );

        reset_conflict_topology_policy_state();
        set_conflict_topology_policy_mode(ConflictTopologyPolicyMode::Enforced);
        for _ in 0..64 {
            record_conflict_topology_heat(page, 1, 4);
        }
        let first = conflict_topology_split_advice(page, "right_edge", 6_500);
        let second = conflict_topology_split_advice(page, "right_edge", 6_500);
        let exhausted = conflict_topology_split_advice(page, "right_edge", 6_500);
        assert!(first.deflection_applied());
        assert_eq!(first.effective_target_left_basis_points, 9_000);
        assert!(second.deflection_applied());
        assert_eq!(second.deflection_credits_after, 0);
        assert_eq!(
            exhausted.deflection_status,
            super::HotPageDeflectionStatus::BudgetExhausted
        );
        assert_eq!(exhausted.effective_target_left_basis_points, 8_000);

        for fields in [
            certification_log_fields("cold-shared-table", "cold_page", cold, 120_000, 9_000_000),
            certification_log_fields(
                "topology-hot-shared-table",
                "topology_hot_page",
                topology_hot,
                140_000,
                8_000_000,
            ),
            certification_log_fields(
                "advisory-pathological-hot-page",
                "pathological_hot_page",
                advisory,
                140_000,
                8_000_000,
            ),
            certification_log_fields(
                "baseline-operator-override",
                "operator_override",
                baseline,
                120_000,
                9_000_000,
            ),
            certification_log_fields(
                "enforced-bounded-deflection",
                "pathological_hot_page",
                first,
                150_000,
                7_000_000,
            ),
            certification_log_fields(
                "budget-exhausted-fallback",
                "budget_exhausted",
                exhausted,
                130_000,
                8_000_000,
            ),
        ] {
            assert_certification_log_fields_complete(&fields);
        }

        reset_conflict_topology_policy_state();
        set_conflict_topology_policy_mode(ConflictTopologyPolicyMode::Enforced);
    }

    #[test]
    fn conflict_topology_policy_parse_accepts_operator_labels() {
        assert_eq!(
            super::parse_conflict_topology_policy_mode("baseline"),
            Some(ConflictTopologyPolicyMode::Baseline)
        );
        assert_eq!(
            super::parse_conflict_topology_policy_mode("shadow"),
            Some(ConflictTopologyPolicyMode::Advisory)
        );
        assert_eq!(
            super::parse_conflict_topology_policy_mode("true"),
            Some(ConflictTopologyPolicyMode::Enforced)
        );
        assert_eq!(super::parse_conflict_topology_policy_mode("bogus"), None);
    }

    #[test]
    fn metrics_snapshot_tracks_operation_buckets() {
        let _gate_guard = super::BTREE_METRICS_TEST_LOCK
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        set_btree_metrics_enabled(true);
        let before = btree_metrics_snapshot();
        record_operation(BtreeOpType::Seek);
        record_operation(BtreeOpType::Seek);
        record_operation(BtreeOpType::Insert);

        let after = btree_metrics_snapshot();
        set_btree_metrics_enabled(false);
        assert!(
            after.fsqlite_btree_operations_total.seek
                >= before.fsqlite_btree_operations_total.seek.saturating_add(2)
        );
        assert!(
            after.fsqlite_btree_operations_total.insert
                >= before
                    .fsqlite_btree_operations_total
                    .insert
                    .saturating_add(1)
        );
        assert!(
            after.fsqlite_btree_operations_total.delete
                >= before.fsqlite_btree_operations_total.delete
        );
    }

    #[test]
    fn copy_profile_tracks_owned_materialization_and_cell_assembly() {
        let _guard = COPY_PROFILE_TEST_LOCK
            .lock()
            .expect("copy-profile test lock");
        reset_btree_metrics();
        reset_btree_copy_profile();
        set_btree_copy_profile_enabled(true);

        let before = btree_copy_profile_snapshot();
        let cx = Cx::new();
        let root = PageNumber::new(2).expect("root page");
        let store = MemPageStore::with_empty_table(root, TEST_USABLE);
        let mut cursor = BtCursor::new(store, root, TEST_USABLE, true);

        cursor
            .table_insert(&cx, 1, b"copy-kernel-row")
            .expect("insert should succeed");
        assert!(
            cursor
                .table_move_to(&cx, 1)
                .expect("seek should succeed")
                .is_found()
        );
        let payload = cursor.payload(&cx).expect("payload should decode");
        assert_eq!(payload, b"copy-kernel-row");

        let after = btree_copy_profile_snapshot();
        set_btree_copy_profile_enabled(false);

        assert!(
            after.table_leaf_cell_assembly_calls
                >= before.table_leaf_cell_assembly_calls.saturating_add(1)
        );
        assert!(
            after.table_leaf_cell_assembly_bytes
                >= before
                    .table_leaf_cell_assembly_bytes
                    .saturating_add(payload.len() as u64)
        );
        assert!(
            after.owned_payload_materialization_calls
                >= before.owned_payload_materialization_calls.saturating_add(1)
        );
        assert!(
            after.owned_payload_materialization_bytes
                >= before
                    .owned_payload_materialization_bytes
                    .saturating_add(payload.len() as u64)
        );
    }

    #[test]
    fn copy_profile_tracks_overflow_reassembly() {
        let _guard = COPY_PROFILE_TEST_LOCK
            .lock()
            .expect("copy-profile test lock");
        reset_btree_copy_profile();
        set_btree_copy_profile_enabled(true);

        let before = btree_copy_profile_snapshot();
        let cx = Cx::new();
        let root = PageNumber::new(2).expect("root page");
        let store = MemPageStore::with_empty_table(root, TEST_USABLE);
        let mut cursor = BtCursor::new(store, root, TEST_USABLE, true);
        let payload = vec![b'X'; 8_000];

        cursor
            .table_insert(&cx, 1, &payload)
            .expect("overflow insert should succeed");
        assert!(
            cursor
                .table_move_to(&cx, 1)
                .expect("seek should succeed")
                .is_found()
        );
        let mut scratch = Vec::new();
        cursor
            .payload_into(&cx, &mut scratch)
            .expect("payload_into should decode overflow");
        set_btree_copy_profile_enabled(false);

        let after = btree_copy_profile_snapshot();
        assert_eq!(scratch, payload);
        assert!(
            after.overflow_chain_reassembly_calls
                >= before.overflow_chain_reassembly_calls.saturating_add(1)
        );
        assert!(after.overflow_chain_overflow_bytes > before.overflow_chain_overflow_bytes);
        assert!(after.overflow_page_reads > before.overflow_page_reads);
    }

    #[test]
    fn leaf_reuse_profile_tracks_reuse_fallback_and_rebuilds() {
        let _guard = super::LEAF_REUSE_TEST_LOCK
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        let _gate_guard = super::BTREE_METRICS_TEST_LOCK
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        set_btree_metrics_enabled(true);
        let before = btree_leaf_reuse_snapshot();

        record_no_split_reuse_hit();
        record_conservative_reload_fallback();
        record_page_header_rebuild();

        let after = btree_leaf_reuse_snapshot();
        set_btree_metrics_enabled(false);
        assert!(
            after.no_split_reuse_hits >= before.no_split_reuse_hits.saturating_add(1),
            "no-split reuse counter should advance"
        );
        assert!(
            after.conservative_reload_fallbacks
                >= before.conservative_reload_fallbacks.saturating_add(1),
            "fallback counter should advance"
        );
        assert!(
            after.page_header_rebuild_count >= before.page_header_rebuild_count.saturating_add(1),
            "page-header rebuild counter should advance"
        );
    }

    /// Microbench: gate-off vs gate-on cost of `record_operation` and
    /// `set_depth_gauge` — the two recorders that fire on every cursor op.
    ///
    /// Cross-core contention isn't exercised here (single thread), but the
    /// per-call atomic fetch_add / store still costs ~5–10 ns even when the
    /// counter isn't contended. With the gate off we expect a single
    /// `AtomicBool::load(Relaxed)` per call (~1 ns).
    ///
    /// Run via:
    /// ```text
    /// cargo test -p fsqlite-btree --lib --release -- --ignored --nocapture \
    ///   bench_btree_metrics_gate_per_op_cost
    /// ```
    /// Wrapper that prevents LLVM from hoisting the gate-load out of the
    /// bench loop or folding away the body. `record_operation` is `#[inline]`
    /// — without an inline boundary here the compiler sees a constant-folded
    /// loop body and elides the work entirely.
    #[inline(never)]
    fn bench_record_op_pair(op: BtreeOpType, depth: usize) {
        record_operation(op);
        super::set_depth_gauge(depth);
    }

    #[test]
    #[ignore = "microbench — run with --ignored --nocapture"]
    fn bench_btree_metrics_gate_per_op_cost() {
        use std::hint::black_box;
        use std::time::Instant;

        let _gate_guard = super::BTREE_METRICS_TEST_LOCK
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        const ITERATIONS: u64 = 50_000_000;

        // Gate-off path (default).
        set_btree_metrics_enabled(false);
        for _ in 0..100_000 {
            bench_record_op_pair(black_box(BtreeOpType::Seek), black_box(3));
        }
        let off_start = Instant::now();
        for _ in 0..ITERATIONS {
            bench_record_op_pair(black_box(BtreeOpType::Seek), black_box(3));
        }
        let off_elapsed = off_start.elapsed();

        // Gate-on path.
        set_btree_metrics_enabled(true);
        for _ in 0..100_000 {
            bench_record_op_pair(black_box(BtreeOpType::Seek), black_box(3));
        }
        let on_start = Instant::now();
        for _ in 0..ITERATIONS {
            bench_record_op_pair(black_box(BtreeOpType::Seek), black_box(3));
        }
        let on_elapsed = on_start.elapsed();
        set_btree_metrics_enabled(false);

        let off_ns = off_elapsed.as_nanos() as f64 / ITERATIONS as f64;
        let on_ns = on_elapsed.as_nanos() as f64 / ITERATIONS as f64;
        println!(
            "btree-metrics gate per-op (record_operation + set_depth_gauge): \
             gate-off={off_ns:.2} ns/pair  gate-on={on_ns:.2} ns/pair  iterations={ITERATIONS}"
        );
        // Gate-off must be no slower than gate-on. An `AtomicBool::load(Relaxed)`
        // is ~1 ns while the gated `fetch_add` + `store` cost ~5–10 ns even
        // uncontended (and far more under multi-thread cache-line ping-pong).
        assert!(
            off_ns <= on_ns,
            "gate-off ({off_ns:.2} ns) should not exceed gate-on ({on_ns:.2} ns)"
        );
    }
}