libdictenstein 0.1.0

High-performance dictionary data structures (trie, DAWG, double-array trie, suffix automaton, lock-free durable persistent ART) behind one trait API; pairs with liblevenshtein for fuzzy matching
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
//! Character-level Persistent Adaptive Radix Trie for proper Unicode support.
//!
//! This module provides a character-based variant of PersistentARTrie that operates
//! at the Unicode character level rather than byte level. This ensures correct edit
//! distance semantics for multi-byte UTF-8 sequences.
//!
//! ## Module Structure
//!
//! - `nodes`: ART node types adapted for u32/char keys (CharNode4, CharNode16, CharNode48, CharBucket)
//!
//! ## Differences from PersistentARTrie
//!
//! - Edge labels are `char` (4 bytes) instead of `u8` (1 byte)
//! - Distance calculations count characters, not bytes
//! - Correct semantics: "" → "¡" is distance 1, not 2
//! - No Node256: Would require 4GB array for u32 keys
//! - CharBucket handles >48 children using HashMap
//!
//! ## Performance Trade-offs
//!
//! - **Memory**: Uses char-indexed edges (larger fanout space)
//! - **Speed**: Slightly slower due to UTF-8 encoding/decoding
//! - **Correctness**: Proper Unicode semantics
//!
//! ## Use Cases
//!
//! Use `PersistentARTrieChar` when:
//! - Dictionary contains non-ASCII Unicode characters
//! - Edit distance must be measured in characters, not bytes
//! - Correctness is more important than maximum performance
//!
//! # ACID Guarantees
//!
//! This implementation provides the same ACID properties as [`super::persistent_artrie`]:
//!
//! ## Atomicity
//!
//! - **Single Operations**: Individual insert/remove operations are atomic
//! - **Document Transactions**: Multi-term transactions via [`CharDocumentTransaction`] provide
//!   all-or-nothing semantics
//! - **WAL Logging**: Operations are logged to WAL before application
//!
//! ## Consistency
//!
//! - **Trie Invariants**: Character-level ART structure invariants are maintained
//! - **CRC32 Checksums**: WAL records include CRC32 checksums
//! - **Recovery Validation**: Crash recovery validates and replays valid records
//!
//! ## Isolation
//!
//! | Isolation Level | Dirty Read | Non-Repeatable Read | Phantom Read |
//! |-----------------|------------|---------------------|--------------|
//! | RwLock (default)| No         | No                  | No           |
//! | MVCC-Lite       | No         | No                  | Possible*    |
//!
//! *Epoch-based snapshots allow concurrent reads/writes with potential phantoms.
//!
//! ## Durability
//!
//! Durability is controlled by [`DurabilityPolicy`]:
//!
//! | Policy      | fsync Behavior      | Guarantee    | Use Case                        |
//! |-------------|---------------------|--------------|----------------------------------|
//! | `Immediate` | Every CommitTx      | Full         | ACID compliance (default)       |
//! | `GroupCommit`| Batched by coordinator| Full      | High-throughput workloads       |
//! | `Periodic`  | At checkpoints only | Bounded loss | Performance-critical            |
//! | `None`      | Never               | None         | Testing only                    |

// Shared types for both in-memory and disk-backed modes
pub mod types;

// ART node types for char keys
pub mod nodes;

// Serialization for char nodes
pub mod serialization_char;

// Arena allocation for space-efficient disk storage
pub mod arena;

// Arena manager for managing multiple arenas
pub mod arena_manager;

// Compact variable-width encoding for space-efficient serialization
pub mod compact_encoding;

// Traversal context for block caching
pub mod traversal_context;

// Dirty tracking for incremental checkpoints
pub mod dirty_tracker;

// Hash-based deduplication for space efficiency
pub mod dedup;

// Relative offset encoding for space-efficient child pointers
pub mod relative_encoding;

// Crash recovery for corrupted files
pub mod recovery;

// Per-node logging for near-instant recovery (char-specific adaptation)
pub mod per_node_log_char;

// Page-aware prefix-iteration result types (Phase-6 split out of dict_impl_char).
pub mod prefix_term;

// EnhancedRecoveryMode + EnhancedRecoveryStats (Phase-6 split out of dict_impl_char).
pub mod recovery_stats;

// CharTrieFileHeader (Phase-6 split out of dict_impl_char).
pub mod file_header;

// CharDocumentTransaction (Phase-6 split out of dict_impl_char).
pub mod transactions;

// Char-side MVCC TrieRoot implementation (paired with generic core mvcc).
pub mod mvcc;

// Disk-backed implementation
pub mod dict_impl_char;

// IoUringDiskManager-specific constructors (Phase-6 split out of dict_impl_char).
#[cfg(feature = "io-uring-backend")]
pub mod io_uring_ctor;

// MmapDiskManager-specific constructors (Phase-6 split out of dict_impl_char).
pub mod mmap_ctor;

// Disk loading + child resolution helpers (Phase-6 split out of dict_impl_char).
pub mod disk_io;

// Lock-free CAS-based concurrent insert/contains/get/increment (Phase-6 split).
pub mod lockfree_cas;

// Public read-path API + optimistic concurrency variants (Phase-6 split).
pub mod query_api;

// Public prefix iter + remove API (Phase-6 split out of dict_impl_char).
pub mod overlay_read;
pub mod prefix_api;

// Char overlay fault-in handles (`OverlayFaulter` impls) that let the
// overlay-backed `DictionaryNode` resolve `Child::OnDisk` overlay children during a
// graph walk. F7 BLOCKER-1.
pub(crate) mod overlay_fault;

// Merge API (merge_from + variants) — Phase-6 split out of dict_impl_char.
pub mod merge_api;

// Document-transaction execution (begin/tx_insert/commit/abort) — Phase-6 split.
pub mod document_tx;

// Batch-insert API (insert_batch + 9 variants) — Phase-6 split.
pub mod batch_insert;

// Rayon-based parallel merge (feature-gated) — Phase-6 split.
#[cfg(feature = "parallel-merge")]
pub mod parallel_merge;

// Observability / durability / group-commit / memory-monitor / cache-stats API.
pub mod observability;

// Epoch-based checkpoint tracking (Phase-6 split out of dict_impl_char).
pub mod epoch_checkpointing;

// Atomic read-modify-write operations (Phase-6 split out of dict_impl_char).
pub mod atomic_ops;

// On-disk persistence (checkpoint + persist_to_disk + serialize_*) — Phase-6 split.
pub mod persist;

// Prefetch helpers (stats + bounded depth) — Phase-6 split out of dict_impl_char.
pub mod prefetch_api;

// WAL + durability helpers (append_to_wal, sync_wal, *durability_policy).
pub mod wal_helpers;

// Committed-LSN watermark for the lock-free Order-A durable write path
// (Migration Phase E; the executable refinement of LockFreeDurableCheckpoint.tla).
pub(crate) mod committed_watermark;

// Thin production-write-path router for the lock-free overlay (the SOLE
// representation since L3.3). `route_overlay()` lives here.
pub(crate) mod overlay_write_mode;

// Per-monomorph routing for the valued counter increment: routes to the overlay
// only for V = u64 (SAFE Any downcast); other V take the general overlay value-CAS
// path (the overlay is the sole representation for all V).
pub(crate) mod lockfree_value_route;

// Public mutation API (insert / insert_with_value / remove) — Phase-6 split.
pub mod mutation_api;

// F5 (Slice 3): the direct dense→overlay reopen loader — `load_root_immutable`
// (eager-load owned + iterative walk-converter owned→overlay) + the per-variant
// glue for the generic WAL-tail-into-overlay applier. Gated OFF by default
// (`LockFreeOverlay::USE_F5_REOPEN_LOADER`); see `docs/design/slice3-f5-loader-impl.md`.
pub(crate) mod f5_loader;

// In-crate white-box tests for the eviction-registry wiring (commit f10c43e):
// state oracle (slot swizzled -> on-disk) + the async eviction path, both of
// which need private node/coordinator internals.
#[cfg(test)]
mod eviction_registry_tests;

// OE1–OE4 correspondence tests for the reversible overlay-eviction driver
// (`evict_overlay_node_at_path` / `evict_overlay_nodes`). In-crate because they
// drive the private driver + `pub(crate)` `OverlayEvictOutcome` and the overlay
// internals. The Rust witness for `formal-verification/tla+/OverlayEvictionCas.tla`.
#[cfg(test)]
mod overlay_eviction_driver_correspondence;

// F7 BLOCKER-1: in-crate coverage that the overlay-backed `DictionaryNode` faults
// EVICTED (`Child::OnDisk`) overlay children back in during a graph walk (never
// dropping them). In-crate because it drives the `pub(crate)` overlay-eviction
// driver that is the only way to create OnDisk overlay children.
#[cfg(test)]
mod overlay_dictionary_node_faulting_tests;

// Re-export shared types (always available)
pub use types::{
    CharTrieFileHeader, CharTrieNodeInner, EnhancedRecoveryMode, EnhancedRecoveryStats,
    CHAR_FILE_HEADER_SIZE, CHAR_HEADER_VERSION_V1, CHAR_HEADER_VERSION_V2, CHAR_TRIE_MAGIC,
    DEFAULT_CHAR_BUFFER_POOL_SIZE,
};

// Re-export disk-backed types (feature-gated)
pub use types::{PrefixTermWithArena, PrefixTermWithValueAndArena};

// Re-export disk-backed implementation types
pub use dict_impl_char::{
    // Transaction types
    CharDocumentTransaction,
    DurabilityPolicy,
    TransactionState,
};

// Note: CharTrieNodeInner is already re-exported from types

// Re-export node types
pub use nodes::{
    AddChildError, CharArtNode, CharBucket, CharCompressedPrefix, CharNode, CharNode16, CharNode4,
    CharNode48, CharNodeHeader, CHAR_MAX_PREFIX_LEN,
};

// Re-export serialization
pub use serialization_char::{
    char_from_bytes, char_serialized_size, char_to_bytes, deserialize_char_node,
    serialize_char_node, SerializedCharNodeHeader, CHAR_FORMAT_VERSION, CHAR_NODE_MAGIC,
    CHAR_SERIALIZED_HEADER_SIZE,
};

// Re-export compact serialization (under feature flag)
pub use serialization_char::{
    char_compact_serialized_size, char_from_bytes_compact, char_to_bytes_compact,
};

// Re-export compact encoding utilities (under feature flag)
pub use compact_encoding::{
    determine_key_width, determine_ptr_width, CompactHeader, DecodedCompactNode,
    COMPACT_NODE_TYPE_BUCKET, COMPACT_NODE_TYPE_N16, COMPACT_NODE_TYPE_N4, COMPACT_NODE_TYPE_N48,
};

// Re-export arena types (under feature flag)
pub use arena::{
    ArenaHeader, CharNodeArena, CharNodeArenaV2, SlotEntry, VarintSlotEntry, ARENA_MAGIC,
    ARENA_MAGIC_V2, ARENA_VERSION, ARENA_VERSION_V2, FLAG_VARINT_DIRECTORY, HEADER_SIZE,
    MIN_FREE_SPACE, SLOT_SIZE,
};

// Re-export arena manager types (under feature flag)
pub use arena_manager::{
    ArenaManager, ArenaSlot, ArenaStats, FlushConfig, FlushStats, ReservedSlots,
};

// Re-export per-node logging types (under feature flag)
pub use per_node_log_char::{
    CharInlineLog,
    CharInlineLogIter,
    CharLogIterExt,
    CharLogWriter,
    CharNodeLogEntry,
    // Re-export node-agnostic types from the base module
    DirtyNodeTracker,
    NodeId,
    PageId,
    PerNodeLogConfig,
    PerNodeLogStats,
    PerNodeLogStatsAtomic,
};

// Re-export traversal context types (under feature flag)
pub use traversal_context::{LightweightTraversalContext, TraversalContext, TraversalStats};

// Re-export dirty tracker types (under feature flag)
pub use dirty_tracker::{BatchDirtyTracker, DirtyTracker, DirtyTrackerStats};

// Re-export deduplication types (under feature flag)
pub use dedup::{
    BatchDeduplicator, DeduplicatingArenaManager, DeduplicatorStats, NodeDeduplicator,
};

// Re-export relative encoding types (under feature flag)
pub use relative_encoding::{
    decode_child_pointer, decode_children, decode_sequential_siblings, encode_child_pointer,
    encode_children, encode_sequential_siblings, encoded_size, is_same_arena,
    try_decode_child_pointer, try_decode_children, try_decode_full, try_decode_relative,
    try_decode_sequential_siblings, try_encode_child_pointer, try_encode_children,
    try_encode_sequential_siblings, try_encoded_size, RelativeEncodingError,
    RelativeEncodingResult, CROSS_ARENA_SIZE, FLAG_CROSS_ARENA, FLAG_RELATIVE_OFFSETS,
    FLAG_SEQUENTIAL_SIBLINGS,
};

// Re-export recovery types (under feature flag)
pub use recovery::{
    detect_corruption,
    find_wal_archive_segments,
    rebuild_from_wal_segments,
    CorruptionInfo,
    CorruptionType,
    // Re-exported from 1-byte implementation (node-agnostic)
    IncrementalRecovery,
    RecoveredOperation,
    RecoveredState,
    RecoveryError,
    RecoveryManager,
    RecoveryMode,
    RecoveryPolicy,
    RecoveryReport,
    RecoveryStats,
};

// Re-export eviction types from byte-level implementation (shared)
pub use crate::persistent_artrie::eviction::{
    AccessTracker, DiskLocationRegistry, EvictionConfig, EvictionCoordinator, EvictionStats,
    EvictionUrgency, LruRegistry,
};

use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering as AtomicOrdering};
use std::sync::Arc;

use parking_lot::RwLock;

use crate::persistent_artrie::wal::AsyncWalWriter;
use crate::persistent_artrie::wal_managed::WalManaged;
use crate::persistent_artrie_core::key_encoding::CharKey;
use crate::value::DictionaryValue;
use crate::zipper::{DictZipper, ValuedDictZipper};
use crate::{
    Dictionary, DictionaryNode, MappedDictionary, MappedDictionaryNode, MutableMappedDictionary,
};

/// Thread-safe handle for `PersistentARTrieChar`.
///
/// **F4 (the lock collapse):** now a bare `Arc<PersistentARTrieChar<V,S>>` — the
/// outer `RwLock` is DELETED. Overlay reads AND writes are fully lock-free; the
/// only operations needing mutual exclusion take dedicated inner locks
/// (`checkpoint_lock`, the wrapped `root` `RwLock`, the `eviction_coordinator`
/// `Mutex`, `merge_lock`) — never the handle. A backward-compatible
/// `.read()`/`.write()` API is preserved by
/// [`SharedTrieAccess`](crate::persistent_artrie_core::shared_access::SharedTrieAccess)
/// (both return a transparent guard that derefs to `&T`; no lock).
pub type SharedCharARTrie<V, S = crate::persistent_artrie::disk_manager::MmapDiskManager> =
    Arc<PersistentARTrieChar<V, S>>;

/// Deprecated alias for backward compatibility.
#[deprecated(since = "0.9.0", note = "Use SharedCharARTrie instead")]
pub type SharedCharTrie<V> = SharedCharARTrie<V>;

#[doc(inline)]
pub use crate::persistent_artrie_core::shared_access::SharedTrieAccess;

// F4: the concrete `.read()/.write()` shim impl on the collapsed char handle
// (CONCRETE, never a blanket `Arc<T>` — see the byte twin + the trait docs).
impl<V: DictionaryValue, S: crate::persistent_artrie::block_storage::BlockStorage>
    crate::persistent_artrie_core::shared_access::SharedTrieAccess
    for Arc<PersistentARTrieChar<V, S>>
{
    type Target = PersistentARTrieChar<V, S>;

    #[inline]
    fn read(
        &self,
    ) -> crate::persistent_artrie_core::shared_access::TrieAccessGuard<'_, PersistentARTrieChar<V, S>>
    {
        crate::persistent_artrie_core::shared_access::TrieAccessGuard::from_ref(self.as_ref())
    }

    #[inline]
    fn write(
        &self,
    ) -> crate::persistent_artrie_core::shared_access::TrieAccessGuard<'_, PersistentARTrieChar<V, S>>
    {
        crate::persistent_artrie_core::shared_access::TrieAccessGuard::from_ref(self.as_ref())
    }
}

/// Character-level Persistent Adaptive Radix Trie for Unicode support.
///
/// This dictionary provides proper Unicode character-level edit distance
/// calculations, ensuring that multi-byte UTF-8 characters are counted
/// as single edit operations.
///
/// # Thread Safety
///
/// This type provides interior mutability via RwLock. For concurrent access,
/// use `SharedCharTrie<V>` which is `Arc<RwLock<PersistentARTrieChar<V>>>`.
pub struct PersistentARTrieChar<V: DictionaryValue = (), S: crate::persistent_artrie::block_storage::BlockStorage = crate::persistent_artrie::disk_manager::MmapDiskManager> {
    /// Number of terms (atomic for lock-free access)
    pub(crate) len: AtomicUsize,
    /// Dirty flag (atomic for lock-free access)
    pub(crate) dirty: AtomicBool,

    // Storage infrastructure (optional - None for in-memory mode)
    pub(crate) buffer_manager: Option<Arc<RwLock<crate::persistent_artrie::buffer_manager::BufferManager<S>>>>,
    /// Async WAL writer for durability (handles synchronization internally)
    pub(crate) wal_writer: Option<Arc<crate::persistent_artrie::wal::AsyncWalWriter>>,
    /// WAL configuration (archive mode, segment limits, etc.)
    pub(crate) wal_config: crate::persistent_artrie::wal::WalConfig,
    pub(crate) next_lsn: std::sync::atomic::AtomicU64,
    /// Committed-LSN watermark for the lock-free Order-A durable write path
    /// (Migration Phase E). The largest contiguously-committed LSN; the only safe
    /// `checkpoint_lsn` under out-of-order lock-free commit. See
    /// [`committed_watermark::CommittedWatermark`].
    pub(crate) committed_watermark: committed_watermark::CommittedWatermark,
    /// **F3 / NF-3 — serializes concurrent checkpoints.** The overlay-arm
    /// non-blocking checkpoint holds only `self.read()`, so two concurrent
    /// `checkpoint()` calls on a `SharedCharARTrie` would otherwise interleave their
    /// block-0 descriptor / arena writes → a torn on-disk image. This lock is taken
    /// for the whole checkpoint body (cloned out of a brief read guard so the trie
    /// `RwLock` is NOT held); readers/writers never touch it → the overlay stays
    /// lock-free, only checkpoints serialize. `Arc<Mutex>` so it survives the F4
    /// `Arc<RwLock>`→`Arc` collapse unchanged. Formally verified:
    /// `formal-verification/tla+/ConcurrentCheckpointSerialization.tla`.
    pub(crate) checkpoint_lock: std::sync::Arc<parking_lot::Mutex<()>>,
    /// **F4 / V11.2 — serializes concurrent merge drivers** (mirrors
    /// `checkpoint_lock` EXACTLY). The per-key merge CAS-retry loop is
    /// obstruction-free; this lock kills merge‖merge livelock by serializing the
    /// whole-trie merge entry points. A dedicated lock in `CK > merge_lock > OR >
    /// EC`: the merge driver takes `merge_lock` then (owned path) OR, never the
    /// reverse; checkpoint (CK) snapshots the lock-free root concurrently and never
    /// holds `merge_lock`. Single acquisition site; public wrappers must NOT re-take
    /// it (parking_lot is non-reentrant).
    pub(crate) merge_lock: std::sync::Arc<parking_lot::Mutex<()>>,
    pub(crate) file_path: Option<std::path::PathBuf>,
    /// Arena manager for space-efficient node storage
    /// Packs multiple nodes into 256KB blocks instead of one node per block
    pub(crate) arena_manager: Option<Arc<RwLock<ArenaManager<S>>>>,

    // Concurrency infrastructure
    /// Version for optimistic concurrency control
    pub(crate) version: crate::persistent_artrie::concurrency::OptimisticVersion,
    /// Epoch manager for safe memory reclamation.
    ///
    /// `Arc` so the eviction coordinator can SHARE this exact manager (see
    /// `enable_eviction`) and a `DictionaryNode` walk can pin it for the walk's
    /// lifetime (see `CharWalkGuard`). A reader's `enter_read` and the
    /// coordinator's `wait_for_quiescence` must observe the same `active_readers`
    /// counter, or quiescence is vacuous and eviction frees nodes out from under a
    /// live walk.
    pub(crate) epoch_manager: Arc<crate::persistent_artrie::concurrency::EpochManager>,
    /// Monotonic counter bumped on every durable in-place structural mutation (via
    /// `invalidate_eviction_registry` at the `append_to_wal` chokepoint). A
    /// `DictionaryNode` walk snapshots it at `root()`; in debug builds each handle
    /// rechecks it before dereferencing its raw node pointer and panics on a
    /// mismatch — surfacing the contract violation "a handle was used across a
    /// concurrent structural mutation" (which would dangle the raw pointer) as a
    /// loud failure instead of silent UB. NOT bumped by eviction (EBR-safe) or by
    /// faulting (a read-path operation), so a walk concurrent with eviction/faulting
    /// does not trip it.
    pub(crate) structural_generation: std::sync::atomic::AtomicU64,
    /// Retry statistics for monitoring
    pub(crate) retry_stats: crate::persistent_artrie::concurrency::RetryStats,

    // Group commit infrastructure (optional - for high-throughput write batching)
    #[cfg(feature = "group-commit")]
    /// Group commit coordinator for WAL write batching.
    /// When enabled, WAL writes are batched for better throughput.
    ///
    /// **F4 (subsystem family, uniform):** `Mutex<Option<Arc<…>>>` so the now-`&self`
    /// `enable/disable_group_commit` toggle it; drop-before-join on disable + the
    /// take-old-then-drop re-arm on enable (V11.3).
    pub(crate) group_commit:
        std::sync::Mutex<Option<Arc<crate::persistent_artrie::group_commit::GroupCommitCoordinator>>>,

    // Performance infrastructure
    /// Memory pressure monitor for adaptive memory management.
    /// When enabled, automatically adjusts buffer pool size based on system memory pressure.
    ///
    /// **F4 (subsystem family, uniform):** `Mutex<Option<Arc<…>>>` (`&self`
    /// enable/disable; drop-before-join — its callback can re-enter the trie, so
    /// holding the field mutex across the join would deadlock, V11.3 GAP 2).
    pub(crate) memory_monitor:
        std::sync::Mutex<Option<Arc<crate::persistent_artrie::memory_monitor::MemoryPressureMonitor>>>,
    /// Cache statistics for monitoring buffer pool performance.
    pub(crate) cache_stats: crate::persistent_artrie::adaptive_pool::CacheStats,
    /// Epoch-based checkpoint manager for WAL/metadata tracking.
    ///
    /// When enabled, the checkpoint manager tracks operation counts and WAL size,
    /// advancing epoch metadata based on configurable thresholds. Explicit
    /// forced epoch checkpoints publish trie data before durable metadata.
    ///
    /// **F4 (subsystem family, uniform):** `Mutex<Option<Arc<…>>>` (`&self`
    /// enable/disable; drop-before-join on disable, V11.3).
    pub(crate) checkpoint_manager:
        std::sync::Mutex<Option<Arc<crate::persistent_artrie::epoch::CheckpointManager>>>,
    /// Durability policy for WAL synchronization.
    /// Controls when fsync is called after WAL writes.
    ///
    /// **F4:** an `AtomicEnumCell` (`&self` `set_durability_policy`; lock-free read
    /// on the write path).
    pub(crate) durability_policy:
        crate::persistent_artrie_core::shared_access::AtomicEnumCell<DurabilityPolicy>,

    // === Eviction Support ===
    /// Eviction coordinator for memory pressure-driven eviction.
    ///
    /// **F4 (EC leaf lock):** `Mutex<Option<Arc<…>>>` — the **EC** leaf in
    /// `CK > merge_lock > OR > EC`: never held across CK/merge_lock/OR, NEVER across
    /// a worker `.join()` (drop-before-join in `disable_eviction`/`close`/`Drop`).
    pub(crate) eviction_coordinator:
        std::sync::Mutex<Option<Arc<crate::persistent_artrie::eviction::EvictionCoordinator>>>,

    // === Prefetching Support ===
    /// Prefetcher for multi-level I/O optimization.
    ///
    /// When traversing disk-backed tries, the prefetcher initiates background I/O
    /// for children that may be visited soon, improving cold lookup performance
    /// by 15-30%.
    pub(crate) prefetcher: crate::persistent_artrie::prefetch::Prefetcher,

    /// Phantom for value type
    pub(crate) _phantom: std::marker::PhantomData<V>,

    // === Lock-Free Infrastructure (per plan Phase 4) ===
    /// Lock-free root using PersistentCharNode with im::Vector for CAS operations.
    /// When present, `insert_cas()` uses this for lock-free concurrent inserts.
    ///
    /// G1: the overlay node is generic over the trie's value type `V`, so a
    /// membership trie (`V=()`) carries `<()>` (unchanged behavior) and a counter
    /// trie (`V=u64`) carries the `u64` count in the overlay leaf's `Option<V>`.
    pub(crate) lockfree_root: Option<nodes::AtomicNodePtr<V>>,

    /// Lock-free cache for term lookups (DashMap for O(1) sharded access).
    pub(crate) lockfree_cache: Option<dashmap::DashMap<String, bool>>,

    /// Statistics: CAS retries for monitoring contention.
    pub(crate) cas_retries: std::sync::atomic::AtomicU64,

    /// DG0 (D2.8 D4): the durable global commit-sequence counter. Seeded on open
    /// from `max(header.commit_seq_floor, scan-max-of-CommitRank)`; a future
    /// `next_commit_seq()` is a claim-before-CAS `fetch_add` (the visibility-order
    /// replay key). Plumbed here (default 0); it becomes load-bearing only when
    /// DG-RECON stamps it into `CommitRank.generation` — until then it is inert.
    pub(crate) commit_seq: std::sync::atomic::AtomicU64,

    /// DG0 (D2.8 §4.2): index `data_lsn -> commit_seq` for the reclaimed-set floor
    /// (`floor = max{commit_seq : data_lsn <= checkpoint_lsn}`). A *cache* (the
    /// durable `CommitRank` records are truth); bounded with a scan-fallback so it
    /// cannot grow unbounded under a never-checkpoint overlay. Updated via `&self`
    /// in `append_commit_rank` (DG-RECON) ⇒ wrapped in a `Mutex`. The key is a WAL
    /// `Lsn` (a `u64` alias); kept as `u64` here to avoid an Lsn import.
    pub(crate) commit_seq_by_data_lsn: std::sync::Mutex<std::collections::BTreeMap<u64, u64>>,
}

// Manual Debug implementation to avoid requiring Debug on BufferManager and WalWriter
impl<V: DictionaryValue, S: crate::persistent_artrie::block_storage::BlockStorage> std::fmt::Debug
    for PersistentARTrieChar<V, S>
{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("PersistentARTrieChar")
            .field("len", &self.len)
            .field("dirty", &self.dirty)
            .finish_non_exhaustive()
    }
}

impl<V: DictionaryValue> Default for PersistentARTrieChar<V> {
    #[allow(deprecated)]
    fn default() -> Self {
        Self::new()
    }
}

impl<V: DictionaryValue, S: crate::persistent_artrie::block_storage::BlockStorage> Drop
    for PersistentARTrieChar<V, S>
{
    /// Stop + join the trie's background daemon threads before the rest of the
    /// trie (mmap buffers, WAL files, arena) is torn down. See
    /// [`PersistentARTrieChar::close`].
    fn drop(&mut self) {
        self.close();
    }
}

// === WalManaged Trait Implementation ===

impl<V: DictionaryValue, S: crate::persistent_artrie::block_storage::BlockStorage> WalManaged
    for PersistentARTrieChar<V, S>
{
    fn wal_writer(&self) -> Option<&Arc<AsyncWalWriter>> {
        self.wal_writer.as_ref()
    }
}

// Note: Most methods are implemented in dict_impl_char.rs on `impl super::PersistentARTrieChar<V>`
// These wrapper methods provide convenience APIs

impl<V: DictionaryValue, S: crate::persistent_artrie::block_storage::BlockStorage>
    PersistentARTrieChar<V, S>
{
    /// Stop and join all background daemon threads owned by this trie: the
    /// WAL-sync thread, the eviction thread, and the memory-pressure monitor.
    ///
    /// Idempotent and safe to call repeatedly — each underlying
    /// `stop()`/`shutdown()` takes its `JoinHandle` via `Option::take`, so a
    /// second call is a no-op. Called automatically by `Drop`, and exposed
    /// publicly so an owner can reclaim the threads *deterministically* (e.g.
    /// before swapping a freshly-rebuilt trie into a cache) instead of relying
    /// on `Arc`-refcount drop order.
    ///
    /// Historically each background thread captured a strong `Arc` to its
    /// manager, so the manager's `Drop` could never run and the OS thread
    /// leaked once per trie instance (≈3 threads per trie). The workers now
    /// hold only a `Weak`, so this teardown — and the managers' own `Drop`
    /// backstops — actually run.
    pub fn close(&self) {
        // **F4 drop-before-join (V11.3 sites 3+5):** take each background
        // coordinator OUT of its field `Mutex` into a statement-temporary so the
        // field guard DROPS before `shutdown()`/`Drop` joins the worker thread — the
        // eviction + memory-monitor callbacks re-enter the trie (OR/EC), so joining
        // while holding the field mutex would deadlock. Runs on EVERY teardown.
        let coordinator = self
            .eviction_coordinator
            .lock()
            .expect("eviction_coordinator mutex poisoned")
            .take();
        if let Some(coordinator) = coordinator {
            coordinator.shutdown();
        }
        let monitor = self
            .memory_monitor
            .lock()
            .expect("memory_monitor mutex poisoned")
            .take();
        if let Some(monitor) = monitor {
            monitor.shutdown();
        }
        if let Some(wal) = &self.wal_writer {
            wal.stop_sync();
        }
    }

    /// Check if trie has unsaved changes.
    ///
    /// Returns `true` if any modifications have been made since the last
    /// checkpoint (or creation).
    #[inline]
    pub fn is_dirty(&self) -> bool {
        self.dirty.load(AtomicOrdering::Acquire)
    }

    /// Get the number of terms in the dictionary.
    #[inline]
    pub fn len(&self) -> usize {
        // L3.3: the overlay is the sole representation; count its resident finals (the
        // owned `self.len` counter is moribund under the overlay regime).
        self.overlay_len()
    }

    /// Get the number of terms in the dictionary (alias for `len()`).
    #[inline]
    pub fn term_count(&self) -> usize {
        self.len()
    }

    /// Check if the dictionary is empty.
    #[inline]
    pub fn is_empty(&self) -> bool {
        // L3.3: cheap any-final early-out over the overlay (not `overlay_len() == 0`,
        // which would be O(N)).
        self.overlay_is_empty()
    }

    /// Get the root node for dictionary traversal.
    ///
    /// L3.3: the overlay is the sole representation, so this returns an OVERLAY-backed
    /// `DictionaryNode` that navigates the overlay lazily, so zipper / transducer / fuzzy
    /// traversal works (was: an EMPTY owned tree + a `log::warn!` deferral).
    ///
    /// The inherent `&self` path passes **no** overlay faulter: eviction (the only
    /// source of an `OnDisk` overlay child) is impossible on a non-`Shared` owned
    /// trie, so the overlay handed out here is fully `Child::InMem`. The
    /// eviction-capable `SharedCharARTrie::root` attaches a faulter.
    pub fn root(&self) -> PersistentARTrieCharNode<V> {
        use crate::persistent_artrie_core::overlay::flip::LockFreeOverlay;
        let root = <Self as LockFreeOverlay<CharKey, V, S>>::overlay_root_node(self)
            .unwrap_or_else(|| {
                Arc::new(crate::persistent_artrie_core::overlay::OverlayNode::<
                    CharKey,
                    V,
                >::new())
            });
        PersistentARTrieCharNode::from_overlay_root(root, None)
    }

    /// Iterate over all terms in the dictionary.
    ///
    /// Returns an iterator over all terms. For large tries, consider using
    /// `iter_prefix("")` with batching instead.
    pub fn iter(&self) -> impl Iterator<Item = String> + '_ {
        // Use iter_prefix with empty prefix to get all terms
        self.iter_prefix("")
            .ok()
            .flatten()
            .unwrap_or_default()
            .into_iter()
    }

    /// Iterate over all terms and their values.
    ///
    /// Returns an iterator over (term, value) pairs. For large tries, consider
    /// using `iter_prefix_with_values("")` with batching instead.
    pub fn iter_with_values(&self) -> impl Iterator<Item = (String, V)> + '_
    where
        V: Clone,
    {
        // Use iter_prefix_with_values with empty prefix to get all terms
        self.iter_prefix_with_values("")
            .ok()
            .flatten()
            .unwrap_or_default()
            .into_iter()
    }

    /// Iterate over all terms with the given prefix (convenience wrapper returning iterator).
    ///
    /// Returns `None` if the prefix doesn't exist in the trie.
    pub fn iter_prefix_vec(&self, prefix: &str) -> Option<impl Iterator<Item = String> + '_> {
        self.iter_prefix(prefix)
            .ok()
            .flatten()
            .map(|v| v.into_iter())
    }

    /// Iterate over all terms with the given prefix, including values (convenience wrapper).
    ///
    /// Returns `None` if the prefix doesn't exist in the trie.
    pub fn iter_prefix_with_values_vec(
        &self,
        prefix: &str,
    ) -> Option<impl Iterator<Item = (String, V)> + '_>
    where
        V: Clone,
    {
        self.iter_prefix_with_values(prefix)
            .ok()
            .flatten()
            .map(|v| v.into_iter())
    }
}

// Note: Atomic operations (increment, upsert, compare_and_swap, fetch_add, get_or_insert)
// are implemented in dict_impl_char.rs on `impl super::PersistentARTrieChar<V>`

/// Build from an iterator of terms
impl<V: DictionaryValue + Default> FromIterator<String> for PersistentARTrieChar<V> {
    #[allow(deprecated)]
    fn from_iter<I: IntoIterator<Item = String>>(iter: I) -> Self {
        let trie = Self::new();
        for term in iter {
            trie.insert(&term);
        }
        trie
    }
}

impl<'a, V: DictionaryValue + Default> FromIterator<&'a str> for PersistentARTrieChar<V> {
    #[allow(deprecated)]
    fn from_iter<I: IntoIterator<Item = &'a str>>(iter: I) -> Self {
        let trie = Self::new();
        for term in iter {
            trie.insert(term);
        }
        trie
    }
}

/// Node in the character-level trie for the `DictionaryNode` trait.
///
/// **G5.1 (DRY unification).** This is now a thin alias of the shared, key-encoding-
/// generic [`OverlayDictionaryNode<CharKey, V>`](crate::persistent_artrie_core::overlay::OverlayDictionaryNode)
/// in `persistent_artrie_core`. The byte and char overlay-backed `DictionaryNode`
/// handles were token-for-token identical modulo the key encoding (`CharKey` vs
/// `ByteKey`) and the public unit presented (`char` vs `u8`); G5.1 collapses both
/// into the one generic handle. The public name `PersistentARTrieCharNode<V>` is
/// PRESERVED (downstream `liblevenshtein` / `libgrammstein` depend on it), and
/// `DictionaryNode::Unit = char` is preserved exactly (`K::Token = char` for
/// `CharKey`).
///
/// The handle holds an owned `Arc<OverlayNode>` snapshot (immutable + reference-
/// counted, so descent needs no pin and no `unsafe`; the `Arc` keeps the node + its
/// in-memory subtree alive) plus an optional SAFE [`OverlayFaulter`] for
/// `Child::OnDisk` overlay children. **The two former hand-written
/// `unsafe impl Send/Sync for PersistentARTrieCharNode` are GONE** — the shared
/// `OverlayDictionaryNode` AUTO-DERIVES `Send`/`Sync` (the `OverlayFaulter` trait
/// object carries a `Send + Sync` supertrait bound), a clean `−2` against the
/// unsafe-inventory gate with zero new `unsafe`. `value()` reads the overlay leaf's
/// `Option<V>` directly, unlocking liblevenshtein's value-aware transducer queries
/// (value-yielding + `query_filtered` / `query_by_value_set`) over the char trie.
pub type PersistentARTrieCharNode<V = ()> =
    crate::persistent_artrie_core::overlay::OverlayDictionaryNode<CharKey, V>;

impl<V: DictionaryValue, S: crate::persistent_artrie::block_storage::BlockStorage> Dictionary
    for PersistentARTrieChar<V, S>
{
    type Node = PersistentARTrieCharNode<V>;

    fn root(&self) -> Self::Node {
        PersistentARTrieChar::root(self)
    }

    fn contains(&self, term: &str) -> bool {
        PersistentARTrieChar::contains(self, term)
    }

    #[inline]
    fn len(&self) -> Option<usize> {
        // D2: delegate to the inherent `len()`, which routes to the overlay under the
        // flip (this trait body read `self.len` directly, bypassing the route).
        Some(self.len())
    }
}

impl<V: DictionaryValue + Clone, S: crate::persistent_artrie::block_storage::BlockStorage>
    MappedDictionary for PersistentARTrieChar<V, S>
{
    type Value = V;

    fn get_value(&self, term: &str) -> Option<V> {
        // D2/S3″: delegate to the inherent `get_value` (which value-routes to the
        // overlay), NOT `self.get(..).cloned()` — `get` returns `None` under overlay
        // routing. The inherent method shadows this trait method in `.get_value()` call
        // syntax.
        self.get_value(term)
    }
}

// Note: MutableMappedDictionary is NOT implemented for PersistentARTrieChar because
// its methods require &self (interior mutability) but PersistentARTrieChar::insert_with_value
// requires &mut self. Use SharedCharTrie for interior mutability.

// ============================================================================
// SharedCharARTrie trait implementations
// ============================================================================

impl<V: DictionaryValue> Dictionary for SharedCharARTrie<V> {
    type Node = PersistentARTrieCharNode<V>;

    fn root(&self) -> Self::Node {
        let guard = self.read();
        // L3.3 — the overlay is the sole representation. The held `Arc<OverlayNode>`
        // root snapshot (captured under the read guard for consistency) keeps its
        // whole in-memory subtree alive on its own (every `Child::InMem` is an owned
        // `Arc`), so no epoch pin is needed for memory safety: a concurrent overlay
        // eviction CAS-publishes a NEW root with the slot unswizzled, but THIS
        // snapshot still holds the pre-eviction in-memory Arcs (no UAF). The SAFE
        // `SharedOverlayFaulter` keeps the trie (and its buffer/arena managers) alive
        // for the walk and faults any `Child::OnDisk` slot in on demand — never
        // dropping it. No raw pointer, no `unsafe`.
        use crate::persistent_artrie_core::overlay::flip::LockFreeOverlay;
        let root =
            <PersistentARTrieChar<V> as LockFreeOverlay<CharKey, V, _>>::overlay_root_node(&guard)
                .unwrap_or_else(|| {
                    Arc::new(crate::persistent_artrie_core::overlay::OverlayNode::<
                        CharKey,
                        V,
                    >::new())
                });
        drop(guard);
        let faulter: Arc<dyn crate::persistent_artrie_core::overlay::OverlayFaulter<CharKey, V>> =
            Arc::new(overlay_fault::SharedOverlayFaulter::new(Arc::clone(self)));
        PersistentARTrieCharNode::from_overlay_root(root, Some(faulter))
    }

    fn contains(&self, term: &str) -> bool {
        let guard = self.read();
        guard.contains(term)
    }

    #[inline]
    fn len(&self) -> Option<usize> {
        // D3/S2: delegate to the inner inherent `len()` (overlay-routed under the
        // flip); this read `guard.len` directly, bypassing the route.
        let guard = self.read();
        Some(guard.len())
    }
}

impl<V: DictionaryValue + Clone> MappedDictionary for SharedCharARTrie<V> {
    type Value = V;

    fn get_value(&self, term: &str) -> Option<V> {
        // D3/S3: delegate to the inner inherent `get_value` (value-routed), NOT
        // `guard.get(..).cloned()` (which is `None` under overlay routing).
        let guard = self.read();
        guard.get_value(term)
    }
}

impl<V: DictionaryValue + Clone> MutableMappedDictionary for SharedCharARTrie<V> {
    fn insert_with_value(&self, term: &str, value: V) -> bool {
        let guard = self.write();
        guard.insert_with_value(term, value).unwrap_or(false)
    }

    fn union_with<F>(&self, other: &Self, merge_fn: F) -> usize
    where
        F: Fn(&Self::Value, &Self::Value) -> Self::Value,
        Self::Value: Clone,
    {
        // C2 deadlock fix (AB/BA, red-team R3-1/R4-1 BLOCKER): snapshot `other`'s
        // entries under its read lock and DROP that guard BEFORE taking `self`'s write
        // lock, then merge via the shared funnel — NEVER holding two `RwLock`s at once.
        // The old body held `other.read()` across `self.write()`, deadlocking
        // `A.union_with(&B)` ‖ `B.union_with(&A)`. Mirrors the vocab `union_with`
        // snapshot-then-release pattern (persistent_vocab_artrie/mod.rs:476).
        let entries: Vec<(String, V)> = {
            let other_guard = other.read();
            match other_guard.iter_prefix_with_values_and_arena("") {
                Ok(Some(terms)) => terms.into_iter().map(|i| (i.term, i.value)).collect(),
                _ => Vec::new(),
            }
        };
        // **F4 / V11.2 — merge_lock (merge‖merge serializer).** `union_with` is a
        // `Shared*`-reachable merge driver, so it takes `merge_lock` (a near-leaf in
        // `CK > merge_lock > OR > EC`: `merge_entries` takes OR / runs lock-free CAS
        // UNDER it, never the reverse). `other`'s guard is already dropped (snapshot
        // above), so this never holds two trie locks at once (the AB/BA fix).
        let merge_lock = self.merge_lock.clone();
        let _merge_guard = merge_lock.lock();
        self.merge_entries(entries, merge_fn).unwrap_or(0)
    }

    fn update_or_insert<F>(&self, term: &str, default_value: V, update_fn: F) -> bool
    where
        F: FnOnce(&mut V),
    {
        let guard = self.write();
        // MUST route the read to the overlay (`get_value`, NOT the legacy `get`): under
        // the overlay default `get` returned None for a present term, so this took the
        // insert branch and OVERWROTE the existing value with `default_value` (silent
        // data corruption). `get_value`/`upsert`/`insert_with_value` all overlay-route.
        if let Some(existing) = guard.get_value(term) {
            let mut value = existing;
            update_fn(&mut value);
            guard.upsert(term, value).unwrap_or(false);
            false // Term existed
        } else {
            guard
                .insert_with_value(term, default_value)
                .unwrap_or(false);
            true // New term
        }
    }
}

// ============================================================================
// Zipper Implementation
// ============================================================================

/// Zipper for navigating the character-level trie
#[derive(Debug, Clone)]
pub struct PersistentARTrieCharZipper<V: DictionaryValue = ()> {
    node: PersistentARTrieCharNode<V>,
    path_vec: Vec<char>,
}

impl<V: DictionaryValue> PersistentARTrieCharZipper<V> {
    /// Create a new zipper at the root
    pub fn new(dict: &PersistentARTrieChar<V>) -> Self {
        Self {
            node: dict.root(),
            path_vec: Vec::new(),
        }
    }

    /// Get the current path as a string
    pub fn path_string(&self) -> String {
        self.path_vec.iter().collect()
    }
}

impl<V: DictionaryValue> DictZipper for PersistentARTrieCharZipper<V> {
    type Unit = char;

    fn is_final(&self) -> bool {
        self.node.is_final()
    }

    fn descend(&self, label: char) -> Option<Self> {
        self.node.transition(label).map(|child| {
            let mut new_path = self.path_vec.clone();
            new_path.push(label);
            Self {
                node: child,
                path_vec: new_path,
            }
        })
    }

    fn children(&self) -> impl Iterator<Item = (char, Self)> {
        let path = self.path_vec.clone();
        self.node
            .edges()
            .map(move |(c, child)| {
                let mut new_path = path.clone();
                new_path.push(c);
                (
                    c,
                    Self {
                        node: child,
                        path_vec: new_path,
                    },
                )
            })
            .collect::<Vec<_>>()
            .into_iter()
    }

    fn path(&self) -> Vec<char> {
        self.path_vec.clone()
    }
}

impl<V: DictionaryValue + Clone> ValuedDictZipper for PersistentARTrieCharZipper<V> {
    type Value = V;

    fn value(&self) -> Option<V> {
        // L3.3 — overlay-routed: read the node handle's value (the overlay leaf's
        // `Option<V>`) only for a final node, preserving the prior owned semantics.
        if !self.node.is_final() {
            return None;
        }
        self.node.value()
    }
}

// ============================================================================
// ARTrie Trait Implementation (on SharedCharTrie for Clone requirement)
// ============================================================================

impl<V: DictionaryValue> crate::artrie_trait::ARTrie for SharedCharARTrie<V> {
    type Unit = char;
    type Value = V;

    fn create<P: AsRef<std::path::Path>>(path: P) -> crate::persistent_artrie::error::Result<Self> {
        PersistentARTrieChar::create(path).map(Arc::new)
    }

    fn create_with_slot_tracking<P: AsRef<std::path::Path>>(
        path: P,
    ) -> crate::persistent_artrie::error::Result<Self> {
        PersistentARTrieChar::create_with_slot_tracking(path).map(Arc::new)
    }

    fn open<P: AsRef<std::path::Path>>(path: P) -> crate::persistent_artrie::error::Result<Self> {
        PersistentARTrieChar::open(path).map(Arc::new)
    }

    fn open_with_slot_tracking<P: AsRef<std::path::Path>>(
        path: P,
    ) -> crate::persistent_artrie::error::Result<Self> {
        PersistentARTrieChar::open_with_slot_tracking(path).map(Arc::new)
    }

    fn open_with_recovery<P: AsRef<std::path::Path>>(
        path: P,
    ) -> crate::persistent_artrie::error::Result<(
        Self,
        crate::persistent_artrie::recovery::RecoveryReport,
    )> {
        PersistentARTrieChar::open_with_recovery(path).map(|(t, r)| (Arc::new(t), r))
    }

    fn open_with_recovery_and_slot_tracking<P: AsRef<std::path::Path>>(
        path: P,
    ) -> crate::persistent_artrie::error::Result<(
        Self,
        crate::persistent_artrie::recovery::RecoveryReport,
    )> {
        let (trie, report) = PersistentARTrieChar::open_with_recovery(path)?;
        if let Some(ref am) = trie.arena_manager {
            am.write().enable_slot_tracking();
        }
        Ok((Arc::new(trie), report))
    }

    fn enable_slot_tracking(&self) {
        let guard = self.read();
        if let Some(ref am) = guard.arena_manager {
            am.write().enable_slot_tracking();
        }
    }

    fn flush_sequential(&self) -> crate::persistent_artrie::error::Result<()> {
        let guard = self.read();
        if let Some(ref am) = guard.arena_manager {
            am.write().flush_sequential()?;
        }
        Ok(())
    }

    fn insert(&self, term: &str) -> bool
    where
        Self::Value: Default,
    {
        let guard = self.write();
        guard.insert(term).unwrap_or(false)
    }

    fn insert_with_value(&self, term: &str, value: Self::Value) -> bool {
        let guard = self.write();
        guard.insert_with_value(term, value).unwrap_or(false)
    }

    fn contains(&self, term: &str) -> bool {
        let guard = self.read();
        guard.contains(term)
    }

    fn get_value(&self, term: &str) -> Option<Self::Value>
    where
        V: Clone,
    {
        // D3/S3′: inner inherent `get_value` (value-routed to the overlay).
        let guard = self.read();
        guard.get_value(term)
    }

    fn remove(&self, term: &str) -> bool {
        let guard = self.write();
        guard.remove(term).unwrap_or(false)
    }

    #[inline]
    fn len(&self) -> usize {
        // D3/S2′: inner inherent `len()` (overlay-routed).
        let guard = self.read();
        guard.len()
    }

    fn checkpoint(&self) -> crate::persistent_artrie::error::Result<()> {
        // **F3 / NF-3 — serialize concurrent checkpoints.** The overlay arm below
        // captures under only `self.read()` (lock-free), so two concurrent
        // `checkpoint()` calls would otherwise interleave their block-0 descriptor /
        // arena writes → a torn on-disk image (lost/corrupt terms on reopen). Take
        // the `checkpoint_lock` for the WHOLE body: clone it out of a brief read
        // guard (so the trie `RwLock` is NOT held while we lock), then hold it.
        // Readers/writers never touch this mutex → the overlay stays lock-free; only
        // checkpoints serialize. Formally verified
        // (`formal-verification/tla+/ConcurrentCheckpointSerialization.tla`:
        // USE_LOCK=TRUE holds NoTornDescriptor; USE_LOCK=FALSE negative control fires).
        // **F4:** there is no outer trie lock. `checkpoint_lock` (CK) is the SOLE
        // concurrent-checkpoint serializer. `self.read()`/`self.write()` are the
        // no-lock shim (return `&T`); they exist only to keep the historical call
        // shape — the real exclusion is CK (here) + OR-read (inside the owned
        // capture). Lock order: CK > OR.
        let ckpt_lock = self.checkpoint_lock.clone();
        let _ckpt_guard = ckpt_lock.lock();
        // S5-9 route-split (RES-4, total-loss guard): under the overlay write mode the
        // checkpoint MUST capture the IMMUTABLE OVERLAY (the live data), not the empty
        // owned tree. `capture_snapshot_immutable` is lock-free (reads the atomic
        // overlay root) — no guard needed for memory safety; CK serializes the
        // descriptor/arena publish.
        // L3.3c: the overlay is the sole representation. Capture the IMMUTABLE OVERLAY
        // (lock-free — reads the atomic overlay root; CK serializes the descriptor/arena
        // publish) and publish it while RETAINING the WAL.
        let snapshot = self.capture_snapshot_immutable()?;
        if self
            .eviction_coordinator
            .lock()
            .expect("eviction_coordinator mutex poisoned")
            .is_some()
        {
            self.publish_immutable_snapshot_retaining_wal_with_eviction(snapshot)
        } else {
            self.publish_immutable_snapshot_retaining_wal(&snapshot)
        }
    }

    fn is_dirty(&self) -> bool {
        let guard = self.read();
        guard.is_dirty()
    }

    fn remove_prefix(&self, prefix: &str) -> usize {
        let guard = self.write();
        guard.remove_prefix(prefix).unwrap_or(0)
    }

    fn iter_prefix(&self, prefix: &str) -> Option<Box<dyn Iterator<Item = String> + '_>> {
        // Note: This returns owned data because we need to release the lock
        let guard = self.read();
        guard
            .iter_prefix(prefix)
            .ok()
            .flatten()
            .map(|v| Box::new(v.into_iter()) as Box<dyn Iterator<Item = String> + '_>)
    }

    fn sync(&self) -> crate::persistent_artrie::error::Result<()> {
        let guard = self.write();
        guard.sync()
    }

    fn current_lsn(&self) -> u64 {
        let guard = self.read();
        guard.current_lsn()
    }

    fn synced_lsn(&self) -> Option<u64> {
        let guard = self.read();
        guard.synced_lsn()
    }

    fn durability_policy(&self) -> crate::persistent_artrie::dict_impl::DurabilityPolicy {
        let guard = self.read();
        guard.durability_policy()
    }

    fn upsert(
        &self,
        term: &str,
        value: Self::Value,
    ) -> crate::persistent_artrie::error::Result<bool> {
        let guard = self.write();
        guard.upsert(term, value)
    }

    // C1: `increment` removed from the `ARTrie` trait (now an inherent `V: Counter`
    // method on PersistentARTrieChar). Delegation commented out (not deleted) per
    // convention; counter callers use the inner inherent method.
    // fn increment(&self, term: &str, delta: i64) -> crate::persistent_artrie::error::Result<i64> {
    //     let guard = self.write();
    //     guard.increment(term, delta)
    // }
}

// ============================================================================
// REVERSIBLE OVERLAY-EVICTION DRIVER (design g4-overlay-eviction-reclamation)
// ============================================================================
//
// `cfg(any(test, feature = "bench-internals"))` — NOT a production path. This is
// the reversible driver that makes the eviction-ON benchmark's TREATMENT arm do
// REAL in-memory reclamation of COLD overlay subtrees (vs the §E structural
// no-op): it path-copies
// the `lockfree_root` spine and swaps a COLD in-memory child for an `OnDisk`
// reference via a loser-safe root CAS. ZERO `unsafe` (reuses the proven Phase-D
// safe `Arc`/`arc-swap` primitive). The CAS-arbitration safety (loser-safe,
// cold-only, no-UAF) is TLC-verified in
// `formal-verification/tla+/OverlayEvictionCas.tla`.
//
// Rollback (design §4): delete this whole section + `bench_evict_overlay_cold_nodes`
// + the §F bench arm + the TLA spec + its 3 verify-script lines. The write path,
// recovery, production eviction, and `checkpoint()` are untouched.

// Phase 4 (DRY K-generic lift): the per-node evict outcome + the per-attempt evict
// primitive (`evict_overlay_node_at_path`) + the read-path fault-in walk
// (`find_leaf_faulting`) now live ONCE, K-generic, in
// `persistent_artrie_core::overlay::evict` as default methods of the
// `OverlayEvictable<K, V, S>` subtrait of `OverlayFaulter`. Char re-exports the
// shared `OverlayEvictOutcome` (so `evict_overlay_nodes` + the OE tests name a
// single type) and IMPLEMENTS the trait below (the three variant-specific
// accessors + the `cas_retries` fault-counter hook). The lifted primitives are
// behavior-identical to the prior char-only inherent methods — OE1–OE8 + every
// eviction test pass unchanged. Phase 7.4 (GO-LIVE): the `OverlayEvictOutcome`
// re-export + the `evict_overlay_nodes` batch driver are now UN-GATED to production —
// the checkpoint-tail resident-budget eviction (Phase 7.5) is their production caller.
pub(crate) use crate::persistent_artrie_core::overlay::evict::OverlayEvictOutcome;

/// Char impl of the SHARED GENERIC [`OverlayEvictable`] (the per-attempt overlay
/// evict + read-fault primitives, K-generic over `OverlayNode<CharKey, V>`). Supplies
/// the three variant-specific accessors (`lockfree_root` / `epoch_manager` /
/// `eviction_coordinator`) + the `cas_retries` fault-counter hook; the primitives
/// themselves are the trait defaults. The `OverlayFaulter<CharKey, V>` super-trait
/// requirement is satisfied by char's existing impl (the `load_overlay_node_from_disk`
/// loader). NOT `#[cfg]`-gated: the trait default `find_leaf_faulting` is called on
/// char's UN-GATED production read/remove/valued-insert/increment paths (Flip F0), so
/// the impl must exist in non-test builds; only the per-node evict primitive's
/// production caller + the batch `evict_overlay_nodes` driver stay gated.
impl<V: DictionaryValue, S: crate::persistent_artrie::block_storage::BlockStorage>
    crate::persistent_artrie_core::overlay::evict::OverlayEvictable<
        crate::persistent_artrie_core::key_encoding::CharKey,
        V,
        S,
    > for PersistentARTrieChar<V, S>
{
    #[inline]
    fn overlay_root_slot(
        &self,
    ) -> Option<
        &crate::persistent_artrie_core::overlay::AtomicNodePtr<
            crate::persistent_artrie_core::key_encoding::CharKey,
            V,
        >,
    > {
        self.lockfree_root.as_ref()
    }

    #[inline]
    fn overlay_epoch_manager(&self) -> &crate::persistent_artrie_core::concurrency::EpochManager {
        &self.epoch_manager
    }

    #[inline]
    fn overlay_eviction_coordinator(
        &self,
    ) -> Option<Arc<crate::persistent_artrie::eviction::EvictionCoordinator>> {
        self.eviction_coordinator
            .lock()
            .expect("eviction_coordinator mutex poisoned")
            .as_ref()
            .map(Arc::clone)
    }

    #[inline]
    fn note_faultin_cas(&self) {
        // Char's pre-lift `find_leaf_faulting` bumped `cas_retries` on both the win
        // and the loss arm of the fault-in install CAS; preserve that observable
        // behavior (the contention monitor `cas_retry_count()`).
        self.cas_retries
            .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
    }
}

/// Reclaim a batch of COLD OVERLAY nodes (the overlay evictor). Evicts
/// LEAF-FIRST (descending depth) so a node is
/// evicted before any ancestor — keeping each victim's parent spine in memory at
/// eviction time (a later shallower candidate whose spine now passes through an
/// already-on-disk slot is reported `NotEvictable` and skipped). Each victim gets
/// up to `max_rebase_retries` root-CAS attempts: a `RootCasLost` (a concurrent
/// writer won) rebases and retries; on exhaustion the victim is SKIPPED (a missed
/// eviction is liveness-only — loser-safe).
///
/// Returns `(evicted, bytes_freed)` where `bytes_freed` is the registry
/// `size_bytes` sum of the successfully-evicted nodes (nominal; the peak-RSS pass
/// is the physical witness). Takes NO lock and uses NO `unsafe`.
///
/// Phase 7.4: UN-GATED to production (the checkpoint-tail resident-budget eviction
/// calls it). The `bench_*` enablers stay gated; this driver does not.
pub(crate) fn evict_overlay_nodes<
    V: DictionaryValue,
    S: crate::persistent_artrie::block_storage::BlockStorage,
>(
    trie: &PersistentARTrieChar<V, S>,
    mut nodes: Vec<(
        u64,
        Vec<char>,
        crate::persistent_artrie::swizzled_ptr::SwizzledPtr,
    )>,
    max_rebase_retries: usize,
) -> (usize, usize) {
    // Phase 4: the per-attempt evict primitive is the K-generic trait default; bring
    // `OverlayEvictable` (+ its `evict_overlay_node_at_path`) into scope. The batch
    // driver itself (LEAF-FIRST ordering, `Vec<char>` registry-path conversion, LRU
    // remove_hash) stays char-specific — only the primitive is shared.
    use crate::persistent_artrie_core::overlay::evict::OverlayEvictable;

    // LEAF-FIRST: sort by DESCENDING path length (depth). Deeper nodes evict
    // first, so an ancestor's spine is still fully in memory when we reach it.
    nodes.sort_by(|a, b| b.1.len().cmp(&a.1.len()));

    let mut evicted = 0usize;
    let mut bytes_freed = 0usize;
    for (_path_hash, path, disk_ptr) in nodes {
        // The registry stores the path as `Vec<char>`; the overlay keys on u32
        // code points. Convert once (preallocated).
        let mut char_path: Vec<u32> = Vec::with_capacity(path.len());
        char_path.extend(path.iter().map(|&c| c as u32));

        // Bounded loser-safe retry: rebase on a lost root CAS; stop on the first
        // terminal outcome (Evicted or NotEvictable) or on retry exhaustion.
        let mut attempt = 0;
        loop {
            match trie.evict_overlay_node_at_path(&char_path, disk_ptr.clone()) {
                OverlayEvictOutcome::Evicted => {
                    evicted += 1;
                    // Nominal byte estimate per evicted overlay node (parity with
                    // `evict_char_nodes`' ~256 B/node estimate; the RSS pass is the
                    // physical witness).
                    bytes_freed += 256;
                    // Drop the LRU entry so a later (re)insert of this cold path
                    // starts fresh (parity with `evict_char_nodes`).
                    if let Some(coordinator) = trie.overlay_eviction_coordinator() {
                        use crate::persistent_artrie::eviction::lru_tracker::hash_char_path;
                        coordinator
                            .lru_registry()
                            .remove_hash(hash_char_path(&path));
                    }
                    break;
                }
                OverlayEvictOutcome::RootCasLost => {
                    attempt += 1;
                    if attempt > max_rebase_retries {
                        break; // exhausted → SKIP (liveness-only miss)
                    }
                    // else: rebase (re-load the root) on the next iteration.
                }
                OverlayEvictOutcome::NotEvictable => break, // skip; never retried
            }
        }
    }
    (evicted, bytes_freed)
}

// ============================================================================
// EvictableARTrie Trait Implementation (on SharedCharARTrie)
// ============================================================================

impl<V: DictionaryValue> crate::artrie_trait::EvictableARTrie for SharedCharARTrie<V> {
    fn enable_eviction(
        &self,
        config: crate::persistent_artrie::eviction::EvictionConfig,
    ) -> crate::persistent_artrie::error::Result<()> {
        use crate::persistent_artrie::error::PersistentARTrieError;

        config
            .validate()
            .map_err(|e| PersistentARTrieError::internal(&e))?;

        // F4 (EC leaf): check + install under a BRIEF EC lock; build/start the
        // coordinator OUTSIDE the lock. Already-enabled ⇒ error (no old to join).
        if self
            .eviction_coordinator
            .lock()
            .expect("eviction_coordinator mutex poisoned")
            .is_some()
        {
            return Err(PersistentARTrieError::internal("Eviction already enabled"));
        }

        // Share the trie's OWN epoch manager with the coordinator (the field is a
        // bare `Arc`, already interior-mutable — no wrap). A walk pins this same
        // manager via `CharWalkGuard`, so the coordinator genuinely waits for active
        // walks to drain before reclaiming.
        let epoch_manager = Arc::clone(&self.epoch_manager);

        // Create the eviction coordinator
        let coordinator = crate::persistent_artrie::eviction::EvictionCoordinator::new(
            config.clone(),
            epoch_manager,
        );

        // Create a weak reference to self for the eviction callback
        let self_weak = Arc::downgrade(self);

        // Start the eviction coordinator with the eviction callback for char nodes
        coordinator
            .start_char(move |nodes_to_evict| {
                // Try to upgrade the weak reference
                let Some(trie) = self_weak.upgrade() else {
                    return (0, 0);
                };
                // L0.1/L3.3: always reclaim the overlay (the owned tree is gone).
                // `evict_overlay_nodes` locks EC for its LRU remove; safe here
                // (this callback holds no EC).
                evict_overlay_nodes(&trie, nodes_to_evict, 4)
            })
            .map_err(|e| PersistentARTrieError::internal(&e))?;

        // Start memory pressure monitor if configured
        coordinator
            .start_memory_monitor()
            .map_err(|e| PersistentARTrieError::internal(&e))?;

        // Install under a brief EC lock; re-check (first writer wins; a loser shuts
        // its own coordinator down OUTSIDE the lock — drop-before-join).
        let mut slot = self
            .eviction_coordinator
            .lock()
            .expect("eviction_coordinator mutex poisoned");
        if slot.is_some() {
            drop(slot);
            coordinator.shutdown();
            return Err(PersistentARTrieError::internal("Eviction already enabled"));
        }
        *slot = Some(coordinator);
        Ok(())
    }

    fn disable_eviction(&self) -> crate::persistent_artrie::error::Result<()> {
        // **F4 drop-before-join (V11.3 site 2):** take the coordinator into a
        // statement-temporary so the EC guard DROPS before `shutdown()` joins the
        // eviction thread — the callback takes OR (and briefly EC), so joining while
        // holding EC would deadlock.
        let coordinator = self
            .eviction_coordinator
            .lock()
            .expect("eviction_coordinator mutex poisoned")
            .take();
        if let Some(coordinator) = coordinator {
            coordinator.shutdown();
        }
        Ok(())
    }

    fn eviction_enabled(&self) -> bool {
        self.eviction_coordinator
            .lock()
            .expect("eviction_coordinator mutex poisoned")
            .is_some()
    }

    fn eviction_stats(&self) -> crate::persistent_artrie::eviction::EvictionStats {
        self.eviction_coordinator
            .lock()
            .expect("eviction_coordinator mutex poisoned")
            .as_ref()
            .map(|c| c.stats())
            .unwrap_or_default()
    }

    fn force_eviction(
        &self,
        target_bytes: usize,
    ) -> crate::persistent_artrie::error::Result<(usize, usize)> {
        // Clone the coordinator Arc out under a BRIEF EC lock, then release EC before
        // reclaiming: the reclaim callback takes OR (order OR > EC; parking_lot is
        // non-reentrant — no lock held across `force_eviction_char`).
        let coordinator = {
            match self
                .eviction_coordinator
                .lock()
                .expect("eviction_coordinator mutex poisoned")
                .as_ref()
            {
                Some(c) => Arc::clone(c),
                None => return Ok((0, 0)),
            }
        };

        // Route to the char-aware path: the byte `force_eviction_bytes` reads the byte
        // `locations` map and would always return (0, 0) for a char trie, whose nodes are
        // registered in `char_locations`. `force_eviction_char` selects from
        // `char_locations` and reclaims inline via the overlay evictor.
        let trie = Arc::clone(self);
        Ok(coordinator.force_eviction_char(target_bytes, move |nodes| {
            // L0.1: owned-eviction arm DELETED — always reclaim the overlay.
            evict_overlay_nodes(&trie, nodes, 4)
        }))
    }

    fn touch_node(&self, path: &[Self::Unit]) {
        if let Some(coordinator) = self
            .eviction_coordinator
            .lock()
            .expect("eviction_coordinator mutex poisoned")
            .as_ref()
        {
            use crate::persistent_artrie::eviction::lru_tracker::hash_char_path;
            coordinator.lru_registry().touch_hash(hash_char_path(path));
        }
    }
}

// Helper methods for eviction on PersistentARTrieChar
impl<V: DictionaryValue, S: crate::persistent_artrie::block_storage::BlockStorage>
    PersistentARTrieChar<V, S>
{
    /// Invalidate any published eviction [`DiskLocationRegistry`] because the
    /// in-memory trie is about to diverge from the last checkpoint's on-disk
    /// image.
    ///
    /// After [`checkpoint`](Self::checkpoint) publishes a registry, it maps each
    /// node's char-path to a *durable, verified* on-disk location. A subsequent
    /// mutation rewrites in-memory nodes (new value, grown node type, added/
    /// removed children) while those on-disk images stay frozen at the last
    /// checkpoint. If eviction then reclaimed such a node it would unswizzle the
    /// *newer* in-memory box onto the *stale* on-disk pointer and drop the box,
    /// so the next read would reload the old value — a lost update. Marking the
    /// registry invalid makes the coordinator refuse to select any node for
    /// eviction (`select_char_for_eviction` / `perform_eviction_char` both gate
    /// on `is_valid()`) until the next checkpoint rebuilds and republishes a
    /// fresh, durable registry.
    ///
    /// This is the formal `Mutate` action of
    /// `formal-verification/tla+/EvictionRegistryPublication.tla`, which clears
    /// the registry on any write so the `RegistryEntriesAreDurable` invariant is
    /// preserved across mutations. It is a no-op when eviction is disabled.
    ///
    /// Called from the single mutation chokepoint
    /// [`append_to_wal`](Self::append_to_wal): every durable public mutation —
    /// `insert`/`insert_with_value`/`remove`/`upsert`/the `insert_batch*` family/
    /// `insert_cas`/document transactions, and the `merge_from`/`remove_prefix`
    /// helpers that delegate to them — logs through it, while `checkpoint` (which
    /// writes its WAL record directly) and WAL recovery replay do not.
    pub(crate) fn invalidate_eviction_registry(&self) {
        // Bump the structural generation on every durable in-place mutation (this is
        // the `append_to_wal` chokepoint). A concurrent `DictionaryNode` walk's
        // debug detector compares its `root()` snapshot against this and panics on a
        // mismatch, surfacing the "handle used across a structural mutation" contract
        // violation (which would dangle the handle's raw pointer) as a loud failure.
        self.structural_generation
            .fetch_add(1, std::sync::atomic::Ordering::Release);
        if let Some(coordinator) = self
            .eviction_coordinator
            .lock()
            .expect("eviction_coordinator mutex poisoned")
            .as_ref()
        {
            coordinator.invalidate_registry();
        }
    }

    /// Number of char nodes registered as evictable in the disk-location
    /// registry published at the last [`checkpoint`](Self::checkpoint).
    ///
    /// Returns `None` when eviction is disabled. Returns `Some(0)` before the
    /// first checkpoint. After a checkpoint with eviction enabled it reflects how
    /// many on-disk node locations the coordinator may reclaim in-memory boxes
    /// for. Note that a post-checkpoint mutation *invalidates* the registry
    /// (eviction then selects nothing) without immediately changing this count —
    /// observe invalidation via [`force_eviction`](crate::artrie_trait::EvictableARTrie::force_eviction)
    /// returning `(0, 0)`; the count is refreshed by the next checkpoint.
    pub fn evictable_node_count(&self) -> Option<usize> {
        self.eviction_coordinator
            .lock()
            .expect("eviction_coordinator mutex poisoned")
            .as_ref()
            .map(|c| c.disk_registry_char_len())
    }

    /// **REVERSIBLE BENCH ENABLER — EVICTION-ON** (gated entirely behind the
    /// existing `bench-internals` feature). Constructs and installs an
    /// [`EvictionCoordinator`] directly on this bare `PersistentARTrieChar` so the
    /// `lockfree_flip_benchmark` `--eviction` TREATMENT arm — which holds the trie
    /// as a bare `Arc<PersistentARTrieChar>` (the lock-free path needs only `&self`),
    /// NOT a `SharedCharARTrie<_>` (`Arc<RwLock<…>>`) — can run eviction-ON
    /// checkpoints (`bench_immutable_checkpoint_with_eviction`). The
    /// `bench_immutable_checkpoint*` methods are `PersistentARTrieChar` methods, so
    /// the TREATMENT trie cannot be a `SharedCharARTrie`; this enabler is the
    /// bare-trie analogue of [`SharedCharARTrie::enable_eviction`].
    ///
    /// Mirrors that construction: validate the config, share THIS trie's own
    /// `epoch_manager` with the coordinator (so a walk and the coordinator pin the
    /// same epoch), `start_char` the background loop, and `start_memory_monitor`
    /// (a no-op under `EvictionConfig::without_memory_monitor`).
    ///
    /// THE RECLAIM CALLBACK IS A NO-OP `(0, 0)`. This is faithful, not a cut: over
    /// a lock-free OVERLAY trie the owned `self.root` is `Empty` (the data lives in
    /// `lockfree_root`), so `evict_node_at_path` — which the production
    /// `SharedCharARTrie` callback (`evict_char_nodes`) walks — would unswizzle
    /// nothing and return `(0, 0)` ANYWAY (proven by the in-crate T1 correspondence
    /// test `immutable_eviction_checkpoint_reopens_losing_nothing`). Wiring the
    /// overlay into the owned eviction walk is the owner-gated Phase-E flip, out of
    /// scope. The benchmark measures the CHECKPOINT path (registry build +
    /// `update_disk_registry` publication — the eviction-ON cost being studied),
    /// which this enabler activates; the reclaim callback is never on the timed
    /// writer path.
    ///
    /// **Maintenance coupling (design §8 risk 5):** the coordinator construction
    /// duplicates `SharedCharARTrie::enable_eviction`'s shape (same crate; flagged).
    /// Deleting this method + `bench_immutable_checkpoint_with_eviction` + the
    /// `bench-internals` cfg disjunct on the publisher fully reverts the eviction-ON
    /// bench surface.
    ///
    /// `cfg(any(test, feature = "bench-internals"))`: widened from `bench-internals`-
    /// only so the in-crate OE1–OE4 overlay-eviction correspondence tests can
    /// install the coordinator (and thus publish a real overlay eviction registry)
    /// under the DEFAULT `cargo test`. The `bench-internals` benchmark path is
    /// unchanged.
    #[cfg(any(test, feature = "bench-internals"))]
    pub fn bench_enable_eviction(
        &mut self,
        config: crate::persistent_artrie::eviction::EvictionConfig,
    ) -> crate::persistent_artrie::error::Result<()> {
        use crate::persistent_artrie::error::PersistentARTrieError;

        config
            .validate()
            .map_err(|e| PersistentARTrieError::internal(&e))?;

        if self
            .eviction_coordinator
            .lock()
            .expect("eviction_coordinator mutex poisoned")
            .is_some()
        {
            return Err(PersistentARTrieError::internal("Eviction already enabled"));
        }

        // Share THIS trie's epoch manager with the coordinator (parity with
        // SharedCharARTrie::enable_eviction).
        let epoch_manager = Arc::clone(&self.epoch_manager);
        let coordinator = crate::persistent_artrie::eviction::EvictionCoordinator::new(
            config.clone(),
            epoch_manager,
        );

        // No-op reclaim callback: see the method doc — overlay eviction is a
        // structural no-op (owned self.root is Empty), so the production
        // `evict_char_nodes` callback would reclaim nothing here regardless. The
        // bench only measures the registry-publication CHECKPOINT path.
        coordinator
            .start_char(|_nodes_to_evict| (0usize, 0usize))
            .map_err(|e| PersistentARTrieError::internal(&e))?;
        coordinator
            .start_memory_monitor()
            .map_err(|e| PersistentARTrieError::internal(&e))?;

        *self
            .eviction_coordinator
            .lock()
            .expect("eviction_coordinator mutex poisoned") = Some(coordinator);
        Ok(())
    }

    /// **REVERSIBLE BENCH ACCESSOR — overlay COLD reclamation** (gated entirely
    /// behind `bench-internals`). Synchronously reclaim COLD overlay subtrees from
    /// the calling thread, returning the number of overlay nodes evicted.
    ///
    /// This is what makes the eviction-ON benchmark's TREATMENT arm perform REAL
    /// in-memory reclamation (vs the §E structural no-op). It reuses the
    /// coordinator's eviction SELECTION (coldest-first LRU, `min_eviction_depth`,
    /// `batch_size`, registry-validity gate — `force_eviction_char` refuses an
    /// invalidated registry, yielding 0 = liveness-not-safety), then filters the
    /// selected candidates to COLD paths (`cold_filter`, e.g.
    /// `|p| p.first() == Some(&'c')`) and reclaims them via the driver
    /// [`evict_overlay_nodes`]. ONLY COLD nodes are ever evicted (SF5(ii)
    /// `faultin_count == 0`): fault-in is absent, so a re-touchable LIVE node must
    /// never be evicted.
    ///
    /// Needs only `&self` (the overlay path is all `&self`), so the benchmark's
    /// checkpointer thread can call it synchronously after each checkpoint
    /// publishes the registry — deterministic, off the writer path. Returns 0 if
    /// eviction was not enabled (`bench_enable_eviction` not called).
    ///
    /// **Rollback (design §4):** delete this method (one edit); the driver +
    /// `OverlayEvictOutcome` + the §F bench arm + the TLA spec then revert
    /// independently. The write path, recovery, production eviction, and
    /// `checkpoint()` are untouched.
    #[cfg(feature = "bench-internals")]
    pub fn bench_evict_overlay_cold_nodes<F>(&self, budget_bytes: usize, cold_filter: F) -> usize
    where
        F: Fn(&[char]) -> bool,
    {
        // F4 (EC leaf): clone the coordinator Arc out under a brief lock; release
        // EC before `force_eviction_char` (its callback takes OR — order OR > EC).
        let coordinator = match self
            .eviction_coordinator
            .lock()
            .expect("eviction_coordinator mutex poisoned")
            .as_ref()
        {
            Some(c) => Arc::clone(c),
            None => return 0,
        };
        coordinator
            .force_eviction_char(budget_bytes, |cands| {
                // COLD-only: drop any selected candidate whose path is not cold, so
                // the evictor never touches a LIVE (re-touchable) subtree.
                let filtered: Vec<_> = cands
                    .into_iter()
                    .filter(|(_, p, _)| cold_filter(p))
                    .collect();
                evict_overlay_nodes(self, filtered, 4)
            })
            .0
    }
}

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

    /// Regression: after a trie is reopened from disk, the root's children are
    /// swizzled (on-disk) `SwizzledPtr`s. The `DictionaryNode` traversal that
    /// external transducers (e.g. liblevenshtein) drive MUST fault those
    /// children in. Before the swizzle-aware fix, `transition`/`edges` used the
    /// non-faulting `get_child`/`iter_children`, which drop swizzled children via
    /// `as_ptr`, so the walk saw an empty subtree and every fuzzy query returned
    /// zero hits after a daemon restart. (liblevenshtein is not a dev-dependency
    /// here, so this drives the `DictionaryNode` API the transducer relies on
    /// directly; the end-to-end transducer test lives in pgmcp.)
    #[test]
    fn dictionary_node_traversal_descends_after_reopen() {
        use crate::{DictionaryNode, MappedDictionaryNode};
        let dir = tempfile::tempdir().expect("tempdir");
        let path = dir.path().join("reopen_traversal.artc");

        // Build + checkpoint + DROP so the in-memory node boxes are released and
        // only the on-disk image remains.
        {
            let trie = PersistentARTrieChar::<i32>::create(&path).expect("create");
            // L3.2/L3.3: the `DictionaryNode` faulting walk (`root().edges()`/`transition`)
            // reads the lock-free overlay (the owned tree is gone). Reopen rebuilds the
            // overlay from the dense on-disk image, so the walk descends it directly.
            trie.insert_with_value("receive", 1).expect("insert");
            trie.insert_with_value("recipe", 2).expect("insert");
            trie.insert_with_value("decide", 3).expect("insert");
            trie.checkpoint().expect("checkpoint");
        }

        // Reopen: root resident, children swizzled (on-disk, `eager_depth=None`).
        let trie = PersistentARTrieChar::<i32>::open(&path).expect("open");
        assert_eq!(trie.len(), 3);

        // Before the fix this count was 0 (swizzled children dropped by `as_ptr`).
        assert!(
            trie.root().edges().count() > 0,
            "root edges empty after reopen — swizzle-fault regression"
        );

        // Descend the full path of an inserted term; every step must fault the
        // next on-disk child in, ending on a final node that carries its value.
        let mut node = trie.root();
        for ch in "receive".chars() {
            node = node
                .transition(ch)
                .unwrap_or_else(|| panic!("transition '{ch}' lost after reopen"));
        }
        assert!(node.is_final(), "terminal node not final after reopen");
        assert_eq!(node.value(), Some(1), "value lost after reopen");

        // An absent first character still yields no transition (no false edge).
        assert!(
            trie.root().transition('x').is_none(),
            "spurious transition for absent edge"
        );
    }

    /// Regression variant: forced eviction swizzles resident node boxes back to
    /// disk. The `DictionaryNode` traversal must re-fault them on demand. The
    /// background eviction thread is stopped before traversal so the assertions
    /// are deterministic and race-free (the production tool instances likewise
    /// never run eviction concurrently with a query).
    #[test]
    fn dictionary_node_traversal_descends_after_forced_eviction() {
        use crate::{Dictionary, DictionaryNode, EvictableARTrie};

        use std::sync::Arc;

        let dir = tempfile::tempdir().expect("tempdir");
        let path = dir.path().join("evict_traversal.artc");
        {
            let trie = PersistentARTrieChar::<i32>::create(&path).expect("create");
            // L3.2/L3.3: forced-eviction re-fault of the `DictionaryNode` walk over the
            // lock-free overlay (the owned tree is gone). Eviction unswizzles overlay nodes
            // to disk; the walk re-faults them on descent.
            for (t, v) in [
                ("receive", 1),
                ("recipe", 2),
                ("recital", 3),
                ("reception", 4),
                ("decide", 5),
            ] {
                trie.insert_with_value(t, v).expect("insert");
            }
            trie.checkpoint().expect("checkpoint");
        }

        let shared: SharedCharARTrie<i32> =
            Arc::new(PersistentARTrieChar::<i32>::open(&path).expect("open"));

        // Enable eviction + checkpoint to populate the disk-location registry,
        // force a one-shot reclaim, then stop the background thread BEFORE
        // traversing. `force_eviction` may legitimately reclaim zero nodes; the
        // assertions require only query correctness, so the test holds either way.
        let _ = shared.enable_eviction(EvictionConfig::default());
        shared.write().checkpoint().expect("checkpoint");
        let _ = shared.force_eviction(usize::MAX);
        let _ = shared.disable_eviction();

        assert!(
            shared.root().edges().count() > 0,
            "root edges empty after eviction — re-fault regression"
        );
        let mut node = shared.root();
        for ch in "reception".chars() {
            node = node
                .transition(ch)
                .unwrap_or_else(|| panic!("transition '{ch}' lost after eviction"));
        }
        assert!(node.is_final(), "terminal node not final after eviction");
    }

    #[test]
    #[allow(deprecated)]
    fn test_new_empty() {
        let trie: PersistentARTrieChar<()> = PersistentARTrieChar::new();
        assert!(trie.is_empty());
        assert_eq!(trie.len(), 0);
    }

    #[test]
    #[allow(deprecated)]
    fn test_insert_ascii() {
        let trie: PersistentARTrieChar<()> = PersistentARTrieChar::new();
        assert!(trie.insert("hello").expect("insert failed"));
        assert!(trie.insert("world").expect("insert failed"));
        assert!(!trie.insert("hello").expect("insert failed")); // Duplicate
        assert_eq!(trie.len(), 2);
    }

    #[test]
    #[allow(deprecated)]
    fn test_insert_unicode() {
        let trie: PersistentARTrieChar<()> = PersistentARTrieChar::new();
        assert!(trie.insert("héllo").expect("insert failed")); // é is one character
        assert!(trie.insert("日本語").expect("insert failed")); // Japanese characters
        assert!(trie.insert("emoji😀").expect("insert failed")); // Emoji
        assert_eq!(trie.len(), 3);
    }

    #[test]
    #[allow(deprecated)]
    fn test_contains() {
        let trie: PersistentARTrieChar<()> = PersistentARTrieChar::new();
        let _ = trie.insert("hello");
        let _ = trie.insert("héllo");

        assert!(trie.contains("hello"));
        assert!(trie.contains("héllo"));
        assert!(!trie.contains("helo"));
        assert!(!trie.contains("hello ")); // Trailing space
    }

    #[test]
    #[allow(deprecated)]
    fn test_value_storage() {
        let trie: PersistentARTrieChar<i32> = PersistentARTrieChar::new();
        let _ = trie.insert_with_value("one", 1);
        let _ = trie.insert_with_value("two", 2);
        let _ = trie.insert_with_value("three", 3);

        assert_eq!(trie.get("one"), Some(1));
        assert_eq!(trie.get("two"), Some(2));
        assert_eq!(trie.get("three"), Some(3));
        assert_eq!(trie.get("four"), None);
    }

    #[test]
    #[allow(deprecated)]
    fn test_unicode_correctness() {
        // This test verifies that multi-byte characters are treated as single units
        let trie: PersistentARTrieChar<()> = PersistentARTrieChar::new();
        let _ = trie.insert("¡");

        let root = trie.root();
        // Should have exactly one edge (for '¡'), not two edges (for the bytes)
        let edges: Vec<_> = root.edges().collect();
        assert_eq!(edges.len(), 1);
        assert_eq!(edges[0].0, '¡');
    }

    #[test]
    #[allow(deprecated)]
    fn test_from_iter() {
        let terms = vec!["alpha", "beta", "gamma"];
        let trie: PersistentARTrieChar<()> = terms.into_iter().collect();
        assert_eq!(trie.len(), 3);
        assert!(trie.contains("alpha"));
        assert!(trie.contains("beta"));
        assert!(trie.contains("gamma"));
    }

    #[test]
    #[allow(deprecated)]
    fn test_zipper() {
        let trie: PersistentARTrieChar<()> = PersistentARTrieChar::new();
        let _ = trie.insert("hello");
        let _ = trie.insert("help");

        let zipper = PersistentARTrieCharZipper::new(&trie);
        let zipper = zipper.descend('h').expect("should have 'h'");
        let zipper = zipper.descend('e').expect("should have 'e'");
        let zipper = zipper.descend('l').expect("should have 'l'");

        let edges: Vec<_> = zipper.children().map(|(c, _)| c).collect();
        assert_eq!(edges.len(), 2); // 'l' and 'p'
    }

    #[test]
    #[allow(deprecated)]
    fn test_iter() {
        let trie: PersistentARTrieChar<()> = PersistentARTrieChar::new();
        let _ = trie.insert("apple");
        let _ = trie.insert("banana");
        let _ = trie.insert("cherry");

        let terms: Vec<_> = trie.iter().collect();
        assert_eq!(terms.len(), 3);
        assert!(terms.contains(&"apple".to_string()));
        assert!(terms.contains(&"banana".to_string()));
        assert!(terms.contains(&"cherry".to_string()));
    }
}