mongreldb-core 0.64.14

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

use std::collections::BTreeMap;
use std::fs::{File, OpenOptions};
use std::io::{self, Read, Write};
use std::path::Path;

use crate::epoch::{Epoch, Snapshot};
use crate::manifest::RunRef;
use crate::rowid::RowId;
use crate::{Result, Table};
use crc::{Crc, CRC_32_ISCSI};
use mongreldb_types::hlc::HlcTimestamp;

/// On-disk filename of the single-file directory checkpoint. Production tables
/// use the sharded layout (see `directory_shard_path`).
pub const DIRECTORY_FILENAME: &str = "directory.bin";
/// Prefix for sharded directory files (`directory.shard-<start>.bin`).
pub const DIRECTORY_SHARD_PREFIX: &str = "directory.shard-";
pub const DIRECTORY_SHARD_SUFFIX: &str = ".bin";
/// Default max bytes per shard when publishing a large directory. Keeps the
/// in-memory buffer under 16 MiB so rebuilds/checkpoint publishes never need
/// the entire directory resident at once on a 256-run workload.
pub const DEFAULT_SHARD_BYTES: u64 = 16 * 1024 * 1024;

/// Magic bytes that prefix the on-disk checkpoint (`MLKP` = Mongrel Lookup).
const LOOKUP_MAGIC: [u8; 4] = *b"MLKP";
/// CRC32C (Castagnoli) over the body โ€” same algorithm as the WAL.
const CRC32C: Crc<u32> = Crc::<u32>::new(&CRC_32_ISCSI);
const FOOTER_LEN: usize = 4;
const HLC_BYTES: usize = 16;

/// What one run-level posting says about a `RowId`: enough metadata to
/// conservatively decide whether the run can contain a version visible to a
/// supplied snapshot, without opening the run.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct RunLocator {
    /// Stable identifier of the run, matched against the manifest's `RunRef`.
    pub run_id: u128,
    pub min_epoch: Epoch,
    pub max_epoch: Epoch,
    /// `Some` when the run carries HLC-stamped rows.
    pub min_hlc: Option<HlcTimestamp>,
    pub max_hlc: Option<HlcTimestamp>,
    /// `true` when the run also contains legacy unstamped (epoch-only) rows.
    pub contains_unstamped_versions: bool,
}

/// A complete, directly-consultable view of the directory for a single
/// `RowId`. Returned by [`RunLookupDirectory::locate`].
#[derive(Debug, Clone, Default)]
pub struct RunLocatorList {
    pub locators: Vec<RunLocator>,
}

impl RunLocatorList {
    pub fn is_empty(&self) -> bool {
        self.locators.is_empty()
    }

    pub fn len(&self) -> usize {
        self.locators.len()
    }
}

/// What a `Table::get` directory consultation concluded with.
///
/// `CompleteMiss` is the authoritative answer "this row has no postings in
/// the directory" โ€” the memtable and mutable-run tiers have already been
/// searched, so an empty complete lookup means no immutable run can host a
/// visible version. The caller opens zero readers.
///
/// `Candidates` means the directory returned at least one locator; the
/// caller walks them with the conservative snapshot filter and the
/// [`RunLocator::can_contain_version_newer_than`] early-stop proof.
///
/// `UnavailableOrStale` means the directory was missing, corrupt, or had a
/// stale fingerprint; the caller falls back to the existing range-scan
/// path. Distinct from `CompleteMiss` because the directory gave no
/// definitive answer and the row *could* still exist in an immutable run.
#[derive(Debug, Clone)]
pub(crate) enum DirectoryLookupDecision {
    CompleteMiss,
    Candidates(Vec<RunLocator>),
    UnavailableOrStale,
}

/// A compact snapshot of the (epoch, hlc) coordinates of a row version. Used
/// by the safe early-stop proof so the caller can ask "can this locator
/// still beat the current winner?" without exposing the full [`crate::memtable::Row`]
/// in the public surface.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct VersionStamp {
    pub epoch: Epoch,
    pub hlc: Option<HlcTimestamp>,
}

/// Per-`RowId` posting list. Backed by a `BTreeMap` for the in-memory
/// representation; the on-disk checkpoint uses delta-encoded `RowId` keys.
#[derive(Debug, Default, Clone)]
pub struct RunLookupDirectory {
    /// `(RowId -> newest-first RunLocator list)`.
    postings: BTreeMap<RowId, RunLocatorList>,
    /// Fingerprint of the active run-set + schema/index generation that
    /// produced this directory. `None` for an in-memory rebuild not yet
    /// checkpointed.
    fingerprint: Option<u64>,
}

impl RunLookupDirectory {
    /// Construct an empty directory with no fingerprint.
    pub fn empty() -> Self {
        Self::default()
    }

    /// Look up the candidate run locators for `row_id`. Returns an empty list
    /// when the row has no postings (caller may treat this as a miss).
    pub fn locate(&self, row_id: RowId) -> &RunLocatorList {
        static EMPTY: RunLocatorList = RunLocatorList {
            locators: Vec::new(),
        };
        self.postings.get(&row_id).unwrap_or(&EMPTY)
    }

    /// Decide how the read path should consult this directory for `row_id`.
    /// Distinguishes a complete directory miss (no postings โ†’ open zero
    /// immutable readers) from the absence of any usable directory
    /// (`UnavailableOrStale` โ†’ range-scan fallback).
    pub(crate) fn decide(&self, row_id: RowId) -> DirectoryLookupDecision {
        match self.postings.get(&row_id) {
            None => DirectoryLookupDecision::CompleteMiss,
            Some(list) if list.is_empty() => DirectoryLookupDecision::CompleteMiss,
            Some(list) => DirectoryLookupDecision::Candidates(list.locators.clone()),
        }
    }

    /// Insert or replace a `RunLocator` for `row_id`. Locators are kept
    /// newest-first; the most recent write wins for ties.
    pub fn insert(&mut self, row_id: RowId, locator: RunLocator) {
        debug_assert!(
            locator.validate_metadata().is_ok(),
            "RunLookupDirectory::insert with invalid locator: {locator:?}"
        );
        let entry = self.postings.entry(row_id).or_default();
        // Newest-first: locate the first locator whose `max_epoch`/`max_hlc`
        // is older than the new one and insert before it. Falls back to
        // append when the new locator is the oldest.
        let pos = entry
            .locators
            .iter()
            .position(|existing| locator_is_newer(locator, *existing));
        match pos {
            Some(idx) => entry.locators.insert(idx, locator),
            None => entry.locators.push(locator),
        }
    }

    /// Drop every locator whose `run_id` no longer exists in the active
    /// manifest. Returns the number of removed postings.
    pub fn retain_active_runs(&mut self, active_runs: &[u128]) -> usize {
        let mut removed = 0;
        for list in self.postings.values_mut() {
            let before = list.locators.len();
            list.locators.retain(|l| active_runs.contains(&l.run_id));
            removed += before - list.locators.len();
        }
        removed
    }

    /// Set the fingerprint of the active run-set + schema/index generation.
    pub fn set_fingerprint(&mut self, fp: u64) {
        self.fingerprint = Some(fp);
    }

    pub fn fingerprint(&self) -> Option<u64> {
        self.fingerprint
    }

    /// Number of `(RowId, posting)` entries. Used for memory/footprint metrics.
    pub fn total_locators(&self) -> usize {
        self.postings.values().map(|l| l.locators.len()).sum()
    }

    /// Number of distinct `RowId` keys.
    pub fn key_count(&self) -> usize {
        self.postings.len()
    }

    /// Maximum number of locators for any single `RowId`.
    pub fn max_locators_per_row(&self) -> usize {
        self.postings
            .values()
            .map(|l| l.locators.len())
            .max()
            .unwrap_or(0)
    }

    /// Average number of locators per `RowId` (0 when the directory is empty).
    pub fn avg_locators_per_row(&self) -> f64 {
        if self.postings.is_empty() {
            return 0.0;
        }
        self.total_locators() as f64 / self.postings.len() as f64
    }

    /// Persist the directory to `path` as a single atomic file (write to temp
    /// sibling, fsync, rename). A `fingerprint` must have been set; without
    /// one the on-disk file is meaningless on reopen. Layout: magic, fingerprint,
    /// key count, delta-encoded `RowId` keys with newest-first per-key locator
    /// lists, then a CRC32C footer.
    pub fn write_checkpoint(&self, path: &Path) -> io::Result<()> {
        let fp = self.fingerprint.ok_or_else(|| {
            io::Error::new(
                io::ErrorKind::InvalidData,
                "run-lookup directory has no fingerprint; refusing to checkpoint",
            )
        })?;
        let mut body: Vec<u8> = Vec::new();
        body.extend_from_slice(&LOOKUP_MAGIC);
        body.extend_from_slice(&fp.to_le_bytes());
        let key_count = self.postings.len() as u32;
        body.extend_from_slice(&key_count.to_le_bytes());
        let mut prev: u64 = 0;
        for (row_id, list) in &self.postings {
            let cur = row_id.0;
            let delta = cur.checked_sub(prev).ok_or_else(|| {
                io::Error::new(
                    io::ErrorKind::InvalidData,
                    "run-lookup row_id sequence is not non-decreasing",
                )
            })?;
            encode_varint(delta, &mut body);
            let locator_count = list.locators.len() as u32;
            body.extend_from_slice(&locator_count.to_le_bytes());
            for loc in &list.locators {
                body.extend_from_slice(&loc.run_id.to_le_bytes());
                body.extend_from_slice(&loc.min_epoch.0.to_le_bytes());
                body.extend_from_slice(&loc.max_epoch.0.to_le_bytes());
                write_optional_hlc(loc.min_hlc, &mut body);
                write_optional_hlc(loc.max_hlc, &mut body);
                body.push(if loc.contains_unstamped_versions {
                    1
                } else {
                    0
                });
            }
            prev = cur;
        }
        let crc = CRC32C.checksum(&body);
        body.extend_from_slice(&crc.to_le_bytes());

        let parent = path.parent().unwrap_or_else(|| Path::new("."));
        let staging = parent.join(format!(
            ".{}.{}.{}.staging",
            path.file_name()
                .and_then(|name| name.to_str())
                .unwrap_or("checkpoint"),
            std::process::id(),
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .map(|d| d.as_nanos())
                .unwrap_or(0),
        ));
        {
            // Best-effort cleanup of any stale staging file left behind by a
            // crashed previous attempt at the same nanos tick. The window is
            // vanishingly small but the truncate avoids an `AlreadyExists`
            // failure that would mask a real I/O error.
            let _ = std::fs::remove_file(&staging);
            let mut file = OpenOptions::new()
                .create(true)
                .write(true)
                .truncate(true)
                .open(&staging)?;
            file.write_all(&body)?;
            file.sync_all()?;
        }
        match std::fs::rename(&staging, path) {
            Ok(()) => {}
            Err(error) => {
                let _ = std::fs::remove_file(&staging);
                return Err(error);
            }
        }
        if let Ok(dir) = OpenOptions::new().read(true).open(parent) {
            let _ = dir.sync_all();
        }
        Ok(())
    }

    /// Read a checkpoint file, validating magic, CRC32C, and that the stored
    /// fingerprint equals the supplied `fingerprint`. Any mismatch is returned
    /// as an `io::Error`; the caller may then fall back to `rebuild_from_runs`.
    pub fn read_checkpoint(path: &Path, fingerprint: u64) -> io::Result<Self> {
        let mut file = File::open(path)?;
        let len = file.metadata()?.len();
        const MAX_CHECKPOINT_BYTES: u64 = 64 * 1024 * 1024;
        if len < 4 + 8 + 4 + FOOTER_LEN as u64 || len > MAX_CHECKPOINT_BYTES {
            return Err(io::Error::new(
                io::ErrorKind::InvalidData,
                "run-lookup checkpoint length is out of range",
            ));
        }
        let mut bytes = Vec::with_capacity(len as usize);
        file.read_to_end(&mut bytes)?;
        let split = bytes.len() - FOOTER_LEN;
        let (body, footer) = bytes.split_at(split);
        let expected = u32::from_le_bytes(footer.try_into().unwrap());
        let actual = CRC32C.checksum(body);
        if expected != actual {
            return Err(io::Error::new(
                io::ErrorKind::InvalidData,
                "run-lookup checkpoint CRC mismatch",
            ));
        }
        if body.len() < 4 + 8 + 4 {
            return Err(io::Error::new(
                io::ErrorKind::InvalidData,
                "run-lookup checkpoint body too short",
            ));
        }
        if body[..4] != LOOKUP_MAGIC {
            return Err(io::Error::new(
                io::ErrorKind::InvalidData,
                "run-lookup checkpoint magic mismatch",
            ));
        }
        let stored_fp = u64::from_le_bytes(body[4..12].try_into().unwrap());
        if stored_fp != fingerprint {
            return Err(io::Error::new(
                io::ErrorKind::InvalidData,
                "run-lookup checkpoint fingerprint does not match active run set",
            ));
        }
        let key_count = u32::from_le_bytes(body[12..16].try_into().unwrap());
        let mut cursor = &body[16..];
        let mut postings: BTreeMap<RowId, RunLocatorList> = BTreeMap::new();
        let mut prev: u64 = 0;
        for _ in 0..key_count {
            let (delta, consumed) = decode_varint(cursor)?;
            cursor = &cursor[consumed..];
            let cur = prev.checked_add(delta).ok_or_else(|| {
                io::Error::new(
                    io::ErrorKind::InvalidData,
                    "run-lookup checkpoint varint overflows u64",
                )
            })?;
            if cur < prev {
                return Err(io::Error::new(
                    io::ErrorKind::InvalidData,
                    "run-lookup checkpoint row_id sequence is not monotonic",
                ));
            }
            if cursor.len() < 4 {
                return Err(io::Error::new(
                    io::ErrorKind::UnexpectedEof,
                    "run-lookup checkpoint truncates locator count",
                ));
            }
            let locator_count = u32::from_le_bytes(cursor[..4].try_into().unwrap()) as usize;
            cursor = &cursor[4..];
            let mut list = RunLocatorList {
                locators: Vec::with_capacity(locator_count),
            };
            for _ in 0..locator_count {
                // Per-locator fixed prefix: run_id(16) + min_epoch(8) +
                // max_epoch(8) + min_hlc_presence(1) + max_hlc_presence(1)
                // + contains_unstamped(1) = 35. The two 16-byte HLC bodies
                // are checked separately via read_optional_hlc.
                let need = 16 + 8 + 8 + 1 + 1 + 1;
                if cursor.len() < need {
                    return Err(io::Error::new(
                        io::ErrorKind::UnexpectedEof,
                        "run-lookup checkpoint truncates locator fields",
                    ));
                }
                let run_id_bytes: [u8; 16] = cursor[..16].try_into().unwrap();
                let run_id = u128::from_le_bytes(run_id_bytes);
                cursor = &cursor[16..];
                let min_epoch = u64::from_le_bytes(cursor[..8].try_into().unwrap());
                cursor = &cursor[8..];
                let max_epoch = u64::from_le_bytes(cursor[..8].try_into().unwrap());
                cursor = &cursor[8..];
                let (min_hlc, used) = read_optional_hlc(cursor)?;
                cursor = &cursor[used..];
                let (max_hlc, used) = read_optional_hlc(cursor)?;
                cursor = &cursor[used..];
                if cursor.is_empty() {
                    return Err(io::Error::new(
                        io::ErrorKind::UnexpectedEof,
                        "run-lookup checkpoint truncates unstamped flag",
                    ));
                }
                let contains_unstamped = cursor[0] != 0;
                cursor = &cursor[1..];
                let locator = RunLocator {
                    run_id,
                    min_epoch: Epoch(min_epoch),
                    max_epoch: Epoch(max_epoch),
                    min_hlc,
                    max_hlc,
                    contains_unstamped_versions: contains_unstamped,
                };
                if let Err(err) = locator.validate_metadata() {
                    return Err(io::Error::new(
                        io::ErrorKind::InvalidData,
                        format!("run-lookup locator metadata invalid: {err:?}"),
                    ));
                }
                list.locators.push(locator);
            }
            postings.insert(RowId(cur), list);
            prev = cur;
        }
        if !cursor.is_empty() {
            return Err(io::Error::new(
                io::ErrorKind::InvalidData,
                "run-lookup checkpoint has trailing bytes after declared key count",
            ));
        }
        Ok(Self {
            postings,
            fingerprint: Some(fingerprint),
        })
    }

    /// Rebuild the directory by scanning each run's system columns. One
    /// `RunLocator` is emitted per `(RowId, run_id)` pair, aggregating
    /// min/max epoch + HLC and the unstamped-row presence flag across every
    /// version that run carries for that row.
    ///
    /// The implementation is the system-column-only iterator
    /// ([`crate::sorted_run::RunReader::for_each_system`]); the full-row
    /// fallback is intentionally absent so directory rebuilds never pay to
    /// materialize user columns. See spec ยง8.4 step 4.
    pub fn rebuild_from_runs(runs: &[RunRef], table: &Table) -> Result<Self> {
        rebuild_from_runs_system_only(runs, table)
    }

    /// Persist the directory as a sequence of shard files in `base_dir`, one
    /// per `RowId` range, bounded by `max_shard_bytes` each (default
    /// [`DEFAULT_SHARD_BYTES`]). Any previous `directory.shard-*.bin` files
    /// in `base_dir` are unlinked first so a torn-tail publication cannot
    /// leave a stale shard from the previous run.
    pub fn write_checkpoint_sharded(
        &self,
        base_dir: &Path,
        max_shard_bytes: u64,
    ) -> io::Result<()> {
        let _ = self.fingerprint.ok_or_else(|| {
            io::Error::new(
                io::ErrorKind::InvalidData,
                "run-lookup directory has no fingerprint; refusing to checkpoint",
            )
        })?;
        // Best-effort: clear any prior shards before publishing new ones.
        clear_shards(base_dir)?;
        let max = max_shard_bytes.max(1024 * 1024); // sanity floor
        let mut iter = self.postings.iter().peekable();
        let mut shard_no: usize = 0;
        while let Some((first_row_id, _)) = iter.peek() {
            shard_no += 1;
            let shard_start = first_row_id.0;
            // The shard spans at least one entry. Determine a clean upper
            // boundary in row_id space by accumulating entries until the
            // projected body length would exceed `max`.
            let mut shard_entries: Vec<(RowId, RunLocatorList)> = Vec::new();
            let mut body_len: usize = 4 + 8 + 4; // magic + fp + key_count
            while let Some((_row_id, list)) = iter.peek() {
                let entry_cost = 1 + 8 // varint delta
                    + 4 // locator count
                    + list.locators.len()
                        * (16 + 8 + 8 + 1 + 1 + 16 + 16 + 1); // worst case per locator
                if !shard_entries.is_empty() && body_len + entry_cost > max as usize {
                    break;
                }
                body_len += entry_cost;
                let (rid, list) = iter.next().unwrap();
                shard_entries.push((*rid, list.clone()));
            }
            let last = shard_entries
                .last()
                .map(|(rid, _)| rid.0)
                .unwrap_or(shard_start);
            let path = if iter.peek().is_some() {
                directory_shard_path(base_dir, shard_start, Some(last))
            } else {
                directory_shard_path(base_dir, shard_start, None)
            };
            write_single_shard(&path, &shard_entries, self.fingerprint.unwrap())?;
        }
        if shard_no == 0 {
            // Empty directory: still emit one zero-row shard so the open path
            // can confirm the checkpoint was published atomically.
            let path = directory_shard_path(base_dir, 0, None);
            write_single_shard(&path, &[], self.fingerprint.unwrap())?;
        }
        // Final best-effort directory sync.
        if let Ok(dir) = OpenOptions::new().read(true).open(base_dir) {
            let _ = dir.sync_all();
        }
        Ok(())
    }

    /// Read the directory from the sharded layout in `base_dir`. Validates
    /// every shard's magic, CRC, and fingerprint independently. Returns the
    /// merged directory; a missing or unreadable shard is propagated as
    /// `io::Error` so the caller can fall back to rebuild-from-runs.
    pub fn read_checkpoint_sharded(base_dir: &Path, fingerprint: u64) -> io::Result<Self> {
        let mut paths: Vec<PathBuf> = Vec::new();
        for entry in std::fs::read_dir(base_dir)? {
            let entry = entry?;
            let name = entry.file_name();
            let Some(name) = name.to_str() else {
                continue;
            };
            // Shards use the `directory.shard-` prefix; the last shard is
            // published as `directory.shard-<start>-open` (no `.bin` suffix)
            // so the open boundary is unambiguous. Both layouts are matched
            // here.
            if name.starts_with(DIRECTORY_SHARD_PREFIX)
                && (name.ends_with(DIRECTORY_SHARD_SUFFIX) || name.ends_with("-open"))
            {
                paths.push(entry.path());
            }
        }
        paths.sort();
        if paths.is_empty() {
            return Err(io::Error::new(
                io::ErrorKind::NotFound,
                "no run-lookup directory shards present",
            ));
        }
        let mut merged: BTreeMap<RowId, RunLocatorList> = BTreeMap::new();
        for path in paths {
            let shard = read_single_shard(&path, fingerprint)?;
            for (rid, list) in shard {
                merged.insert(rid, list);
            }
        }
        Ok(Self {
            postings: merged,
            fingerprint: Some(fingerprint),
        })
    }
}

fn clear_shards(base_dir: &Path) -> io::Result<()> {
    if !base_dir.exists() {
        return Ok(());
    }
    for entry in std::fs::read_dir(base_dir)? {
        let entry = entry?;
        let name = entry.file_name();
        let Some(name) = name.to_str() else {
            continue;
        };
        if name.starts_with(DIRECTORY_SHARD_PREFIX)
            && (name.ends_with(DIRECTORY_SHARD_SUFFIX) || name.ends_with("-open"))
        {
            let _ = std::fs::remove_file(entry.path());
        }
    }
    Ok(())
}

fn write_single_shard(
    path: &Path,
    entries: &[(RowId, RunLocatorList)],
    fingerprint: u64,
) -> io::Result<()> {
    let mut body: Vec<u8> = Vec::new();
    body.extend_from_slice(&LOOKUP_MAGIC);
    body.extend_from_slice(&fingerprint.to_le_bytes());
    let key_count = entries.len() as u32;
    body.extend_from_slice(&key_count.to_le_bytes());
    let mut prev: u64 = 0;
    for (row_id, list) in entries {
        let cur = row_id.0;
        let delta = cur.checked_sub(prev).ok_or_else(|| {
            io::Error::new(
                io::ErrorKind::InvalidData,
                "run-lookup row_id sequence is not non-decreasing",
            )
        })?;
        encode_varint(delta, &mut body);
        let locator_count = list.locators.len() as u32;
        body.extend_from_slice(&locator_count.to_le_bytes());
        for loc in &list.locators {
            body.extend_from_slice(&loc.run_id.to_le_bytes());
            body.extend_from_slice(&loc.min_epoch.0.to_le_bytes());
            body.extend_from_slice(&loc.max_epoch.0.to_le_bytes());
            write_optional_hlc(loc.min_hlc, &mut body);
            write_optional_hlc(loc.max_hlc, &mut body);
            body.push(if loc.contains_unstamped_versions {
                1
            } else {
                0
            });
        }
        prev = cur;
    }
    let crc = CRC32C.checksum(&body);
    body.extend_from_slice(&crc.to_le_bytes());

    let parent = path.parent().unwrap_or_else(|| Path::new("."));
    let staging = parent.join(format!(
        ".{}.{}.{}.staging",
        path.file_name()
            .and_then(|name| name.to_str())
            .unwrap_or("shard"),
        std::process::id(),
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map(|d| d.as_nanos())
            .unwrap_or(0),
    ));
    {
        let _ = std::fs::remove_file(&staging);
        let mut file = OpenOptions::new()
            .create(true)
            .write(true)
            .truncate(true)
            .open(&staging)?;
        file.write_all(&body)?;
        file.sync_all()?;
    }
    match std::fs::rename(&staging, path) {
        Ok(()) => {}
        Err(error) => {
            let _ = std::fs::remove_file(&staging);
            return Err(error);
        }
    }
    Ok(())
}

fn read_single_shard(path: &Path, fingerprint: u64) -> io::Result<BTreeMap<RowId, RunLocatorList>> {
    let mut file = File::open(path)?;
    let len = file.metadata()?.len();
    // Shards are bounded by `max_shard_bytes`. A pathologically large shard
    // is treated as corruption so the caller can fall back to rebuild.
    if len < 4 + 8 + 4 + FOOTER_LEN as u64 || len > MAX_SHARD_BYTES * 4 {
        return Err(io::Error::new(
            io::ErrorKind::InvalidData,
            "run-lookup shard length is out of range",
        ));
    }
    let mut bytes = Vec::with_capacity(len as usize);
    file.read_to_end(&mut bytes)?;
    let split = bytes.len() - FOOTER_LEN;
    let (body, footer) = bytes.split_at(split);
    let expected = u32::from_le_bytes(footer.try_into().unwrap());
    let actual = CRC32C.checksum(body);
    if expected != actual {
        return Err(io::Error::new(
            io::ErrorKind::InvalidData,
            "run-lookup shard CRC mismatch",
        ));
    }
    if body.len() < 4 + 8 + 4 {
        return Err(io::Error::new(
            io::ErrorKind::InvalidData,
            "run-lookup shard body too short",
        ));
    }
    if body[..4] != LOOKUP_MAGIC {
        return Err(io::Error::new(
            io::ErrorKind::InvalidData,
            "run-lookup shard magic mismatch",
        ));
    }
    let stored_fp = u64::from_le_bytes(body[4..12].try_into().unwrap());
    if stored_fp != fingerprint {
        return Err(io::Error::new(
            io::ErrorKind::InvalidData,
            "run-lookup shard fingerprint does not match active run set",
        ));
    }
    let key_count = u32::from_le_bytes(body[12..16].try_into().unwrap());
    let mut cursor = &body[16..];
    let mut postings: BTreeMap<RowId, RunLocatorList> = BTreeMap::new();
    let mut prev: u64 = 0;
    for _ in 0..key_count {
        let (delta, consumed) = decode_varint(cursor)?;
        cursor = &cursor[consumed..];
        let cur = prev.checked_add(delta).ok_or_else(|| {
            io::Error::new(
                io::ErrorKind::InvalidData,
                "run-lookup shard varint overflows u64",
            )
        })?;
        if cur < prev {
            return Err(io::Error::new(
                io::ErrorKind::InvalidData,
                "run-lookup shard row_id sequence is not monotonic",
            ));
        }
        if cursor.len() < 4 {
            return Err(io::Error::new(
                io::ErrorKind::UnexpectedEof,
                "run-lookup shard truncates locator count",
            ));
        }
        let locator_count = u32::from_le_bytes(cursor[..4].try_into().unwrap()) as usize;
        cursor = &cursor[4..];
        let mut list = RunLocatorList {
            locators: Vec::with_capacity(locator_count),
        };
        for _ in 0..locator_count {
            let need = 16 + 8 + 8 + 1 + 1 + 1;
            if cursor.len() < need {
                return Err(io::Error::new(
                    io::ErrorKind::UnexpectedEof,
                    "run-lookup shard truncates locator fields",
                ));
            }
            let run_id_bytes: [u8; 16] = cursor[..16].try_into().unwrap();
            let run_id = u128::from_le_bytes(run_id_bytes);
            cursor = &cursor[16..];
            let min_epoch = u64::from_le_bytes(cursor[..8].try_into().unwrap());
            cursor = &cursor[8..];
            let max_epoch = u64::from_le_bytes(cursor[..8].try_into().unwrap());
            cursor = &cursor[8..];
            let (min_hlc, used) = read_optional_hlc(cursor)?;
            cursor = &cursor[used..];
            let (max_hlc, used) = read_optional_hlc(cursor)?;
            cursor = &cursor[used..];
            if cursor.is_empty() {
                return Err(io::Error::new(
                    io::ErrorKind::UnexpectedEof,
                    "run-lookup shard truncates unstamped flag",
                ));
            }
            let contains_unstamped = cursor[0] != 0;
            cursor = &cursor[1..];
            let locator = RunLocator {
                run_id,
                min_epoch: Epoch(min_epoch),
                max_epoch: Epoch(max_epoch),
                min_hlc,
                max_hlc,
                contains_unstamped_versions: contains_unstamped,
            };
            if let Err(err) = locator.validate_metadata() {
                return Err(io::Error::new(
                    io::ErrorKind::InvalidData,
                    format!("run-lookup locator metadata invalid: {err:?}"),
                ));
            }
            list.locators.push(locator);
        }
        postings.insert(RowId(cur), list);
        prev = cur;
    }
    if !cursor.is_empty() {
        return Err(io::Error::new(
            io::ErrorKind::InvalidData,
            "run-lookup shard has trailing bytes after declared key count",
        ));
    }
    Ok(postings)
}

// PathBuf is referenced by `read_checkpoint_sharded`; import for clarity.
use std::path::PathBuf;

fn encode_varint(mut value: u64, out: &mut Vec<u8>) {
    loop {
        let mut byte = (value & 0x7F) as u8;
        value >>= 7;
        if value != 0 {
            byte |= 0x80;
        }
        out.push(byte);
        if value == 0 {
            break;
        }
    }
}

fn decode_varint(input: &[u8]) -> io::Result<(u64, usize)> {
    let mut value: u64 = 0;
    let mut shift: u32 = 0;
    let mut consumed = 0;
    for byte in input.iter().take(10) {
        consumed += 1;
        let low = (byte & 0x7F) as u64;
        value |= low.checked_shl(shift).ok_or_else(|| {
            io::Error::new(
                io::ErrorKind::InvalidData,
                "run-lookup varint overflows u64",
            )
        })?;
        shift += 7;
        if byte & 0x80 == 0 {
            return Ok((value, consumed));
        }
    }
    Err(io::Error::new(
        io::ErrorKind::InvalidData,
        "run-lookup varint is truncated or too long",
    ))
}

fn write_optional_hlc(ts: Option<HlcTimestamp>, out: &mut Vec<u8>) {
    match ts {
        None => out.push(0),
        Some(ts) => {
            out.push(1);
            out.extend_from_slice(&ts.physical_micros.to_le_bytes());
            out.extend_from_slice(&ts.logical.to_le_bytes());
            out.extend_from_slice(&ts.node_tiebreaker.to_le_bytes());
        }
    }
}

fn read_optional_hlc(input: &[u8]) -> io::Result<(Option<HlcTimestamp>, usize)> {
    if input.is_empty() {
        return Err(io::Error::new(
            io::ErrorKind::UnexpectedEof,
            "run-lookup checkpoint truncates hlc presence flag",
        ));
    }
    let present = input[0] != 0;
    if !present {
        return Ok((None, 1));
    }
    if input.len() < 1 + HLC_BYTES {
        return Err(io::Error::new(
            io::ErrorKind::UnexpectedEof,
            "run-lookup checkpoint truncates hlc body",
        ));
    }
    let physical_micros = u64::from_le_bytes(input[1..9].try_into().unwrap());
    let logical = u32::from_le_bytes(input[9..13].try_into().unwrap());
    let node_tiebreaker = u32::from_le_bytes(input[13..17].try_into().unwrap());
    Ok((
        Some(HlcTimestamp {
            physical_micros,
            logical,
            node_tiebreaker,
        }),
        1 + HLC_BYTES,
    ))
}

/// `true` when `newer` is strictly newer than `older` by either epoch or
/// HLC. Ties compare as `false` (preserve existing order).
fn locator_is_newer(newer: RunLocator, older: RunLocator) -> bool {
    if newer.max_epoch > older.max_epoch {
        return true;
    }
    if newer.max_epoch < older.max_epoch {
        return false;
    }
    match (newer.max_hlc, older.max_hlc) {
        (Some(a), Some(b)) => a > b,
        (Some(_), None) => true,
        (None, Some(_)) => false,
        (None, None) => false,
    }
}

/// Why a [`RunLocator`]'s metadata is contradictory or incomplete (B468-08).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RunLocatorMetadataError {
    MinEpochAfterMaxEpoch,
    PartialHlcEnvelope,
    MinHlcAfterMaxHlc,
    EmptyVersionEnvelope,
}

impl RunLocator {
    /// Validate locator metadata invariants (B468-08).
    ///
    /// - `min_epoch <= max_epoch`
    /// - HLC bounds are both present or both absent
    /// - when both present: `min_hlc <= max_hlc`
    /// - when both absent: `contains_unstamped_versions` must be true
    pub fn validate_metadata(&self) -> std::result::Result<(), RunLocatorMetadataError> {
        if self.min_epoch > self.max_epoch {
            return Err(RunLocatorMetadataError::MinEpochAfterMaxEpoch);
        }
        match (self.min_hlc, self.max_hlc) {
            (Some(min), Some(max)) => {
                if min > max {
                    return Err(RunLocatorMetadataError::MinHlcAfterMaxHlc);
                }
            }
            (None, None) => {
                if !self.contains_unstamped_versions {
                    return Err(RunLocatorMetadataError::EmptyVersionEnvelope);
                }
            }
            _ => return Err(RunLocatorMetadataError::PartialHlcEnvelope),
        }
        Ok(())
    }

    /// `true` when this locator *cannot* contain a version visible to
    /// `snapshot`. Conservative: when in doubt, return `false` so the caller
    /// opens the run. The proof is split into the stamped and unstamped
    /// possibilities and never uses positional tuple fields (REM-A):
    ///
    /// - a stamped version may be visible when the locator's **minimum**
    ///   HLC is at or below the snapshot (`min_hlc`, not `max_hlc` โ€” a run
    ///   can span the snapshot and still host an older visible version);
    /// - an unstamped version may be visible when the locator's minimum
    ///   epoch is at or below the snapshot epoch.
    ///
    /// Contradictory/malformed metadata never proves impossibility (B468-08).
    pub fn is_impossible_for(&self, snapshot: Snapshot) -> bool {
        if self.validate_metadata().is_err() {
            return false;
        }
        let stamped_possible = self.may_have_visible_stamped_version(snapshot);
        let unstamped_possible = self.may_have_visible_unstamped_version(snapshot);

        !stamped_possible && !unstamped_possible
    }

    /// `true` when a stamped version inside this locator may be visible to
    /// `snapshot`. HLC-authoritative snapshots compare HLC bounds; epoch-only
    /// snapshots compare epoch bounds (stamped rows remain epoch-visible
    /// under the dual-model rule). The locator's `min_epoch` is aggregated
    /// across stamped and unstamped rows, so a false positive (opening a run
    /// that could have been skipped) is possible โ€” that is safe; a false
    /// negative (skipping a run that contains a visible row) is not.
    fn may_have_visible_stamped_version(&self, snapshot: Snapshot) -> bool {
        let Some(min_hlc) = self.min_hlc else {
            return false;
        };

        if snapshot.uses_hlc_authority() {
            min_hlc <= snapshot.commit_ts
        } else {
            self.min_epoch <= snapshot.epoch
        }
    }

    /// `true` when an unstamped version inside this locator may be visible to
    /// `snapshot`. Unstamped visibility is epoch-based under both snapshot
    /// modes.
    fn may_have_visible_unstamped_version(&self, snapshot: Snapshot) -> bool {
        self.contains_unstamped_versions && self.min_epoch <= snapshot.epoch
    }

    /// Safe early-stop proof for `Table::get`. Returns `true` when this
    /// locator **might** still contain a version strictly newer than the
    /// current winner `best` and visible to `snapshot`. Returns `false`
    /// only when the locator provably cannot โ€” the caller may then skip
    /// opening this run and increment the early-stop counter.
    ///
    /// The proof asks the stamped and unstamped questions independently
    /// (REM-B): a locator is skippable only when neither a stamped nor an
    /// unstamped version inside it can beat `best`. The recency rule of
    /// [`Snapshot::version_is_newer`] is honored exactly โ€” HLC recency
    /// applies whenever both candidates are stamped, regardless of whether
    /// the snapshot itself is HLC-authoritative.
    ///
    /// Conservative: when the metadata is too sparse to prove impossibility,
    /// returns `true` so the caller opens the run (correctness wins over the
    /// optimization).
    pub(crate) fn can_contain_version_newer_than(
        &self,
        best: VersionStamp,
        snapshot: Snapshot,
    ) -> bool {
        // Unknown metadata: the locator records no usable HLC envelope and
        // claims no unstamped versions, so neither proof below can establish
        // anything. Contradictory-by-construction metadata must stay
        // conservative โ€” open the run.
        if (self.min_hlc.is_none() || self.max_hlc.is_none()) && !self.contains_unstamped_versions {
            return true;
        }
        self.stamped_version_may_beat(best, snapshot)
            || self.unstamped_version_may_beat(best, snapshot)
    }

    /// `true` when a **stamped** version inside this locator may be visible
    /// to `snapshot` and strictly newer than `best`.
    ///
    /// - Stamped candidate versus stamped best compares HLC โ€” under an
    ///   HLC-authoritative snapshot visibility caps the locator's max HLC at
    ///   `snapshot.commit_ts`; under an epoch-only snapshot visibility is
    ///   epoch-based but recency is still HLC-based, so the proof only
    ///   requires some stamped row to be epoch-visible while the locator's
    ///   max HLC exceeds the best (the locator does not preserve the
    ///   per-version epochโ†”HLC correlation, so this is conservative).
    /// - Stamped candidate versus unstamped best compares epoch. Under an
    ///   HLC-authoritative snapshot the candidate is HLC-visible regardless
    ///   of its local epoch, so `max_epoch` is **not** capped by
    ///   `snapshot.epoch`; under an epoch-only snapshot both visibility and
    ///   recency are epoch-based and the cap applies.
    fn stamped_version_may_beat(&self, best: VersionStamp, snapshot: Snapshot) -> bool {
        let (Some(min_hlc), Some(max_hlc)) = (self.min_hlc, self.max_hlc) else {
            return false;
        };

        match best.hlc {
            Some(best_hlc) => {
                if snapshot.uses_hlc_authority() {
                    max_hlc.min(snapshot.commit_ts) > best_hlc
                } else {
                    self.min_epoch <= snapshot.epoch && max_hlc > best_hlc
                }
            }
            None => {
                if snapshot.uses_hlc_authority() {
                    min_hlc <= snapshot.commit_ts && self.max_epoch > best.epoch
                } else {
                    self.max_epoch.min(snapshot.epoch) > best.epoch
                }
            }
        }
    }

    /// `true` when an **unstamped** version inside this locator may be
    /// visible to `snapshot` and strictly newer than `best`. One side of the
    /// comparison is unstamped, so recency uses epoch; unstamped visibility
    /// is also epoch-based under both snapshot modes. `max_epoch` includes
    /// stamped versions too, so this may be a false positive โ€” it cannot
    /// produce a false negative.
    fn unstamped_version_may_beat(&self, best: VersionStamp, snapshot: Snapshot) -> bool {
        self.contains_unstamped_versions && self.max_epoch.min(snapshot.epoch) > best.epoch
    }
}

/// Deterministic fingerprint of the run-set + schema/index generations that
/// produced this directory. `format_version` is the directory's own wire
/// version so a future format bump invalidates older checkpoints even if the
/// run set is unchanged. Uses xxh3 (already a workspace dependency) with a
/// fixed seed, so the value is reproducible across processes/restarts.
pub fn compute_fingerprint(
    run_ids_ordered: &[u128],
    schema_id: u64,
    index_generation: u64,
    run_generation: u64,
    format_version: u32,
) -> u64 {
    let mut buf: Vec<u8> = Vec::with_capacity(8 + 8 + 8 + 4 + run_ids_ordered.len() * (16 + 2));
    buf.extend_from_slice(&schema_id.to_le_bytes());
    buf.extend_from_slice(&index_generation.to_le_bytes());
    buf.extend_from_slice(&run_generation.to_le_bytes());
    buf.extend_from_slice(&format_version.to_le_bytes());
    // Include the run level alongside the run id so a compacted-down topology
    // (level 0 โ†’ level 1 promotion) doesn't accidentally share a fingerprint
    // with the pre-compaction set.
    for run in run_ids_ordered {
        let run_id_bytes = run.to_le_bytes();
        buf.extend_from_slice(&run_id_bytes);
    }
    // Stable, fixed seed (NOT process-local / time-based).
    xxhash_rust::xxh3::xxh3_64_with_seed(&buf, 0x9E37_79B1_854A_0001)
}

/// Build the file path of a single sharded directory checkpoint for the
/// `RowId` range `[start, end_inclusive]`. `end_inclusive == None` means
/// the trailing shard (last open-ended range).
pub fn directory_shard_path(
    base_dir: &Path,
    start: u64,
    end_inclusive: Option<u64>,
) -> std::path::PathBuf {
    match end_inclusive {
        Some(end) => base_dir.join(format!(
            "{DIRECTORY_SHARD_PREFIX}{start:020}-{end:020}{DIRECTORY_SHARD_SUFFIX}"
        )),
        None => base_dir.join(format!("{DIRECTORY_SHARD_PREFIX}{start:020}-open")),
    }
}

/// Build a `RunLookupDirectory` for a single sorted run without materializing
/// any user columns. Uses [`crate::sorted_run::RunReader::for_each_system`]
/// (a system-column-only iterator) and folds the result into one
/// [`RunLocator`] per `RowId` that the run carries. The caller is expected
/// to call this from inside an `Arc<Table>` or equivalent โ€” `table` is
/// borrowed for `open_reader`.
pub fn rebuild_from_runs_system_only(runs: &[RunRef], table: &Table) -> Result<RunLookupDirectory> {
    let mut dir = RunLookupDirectory::empty();
    for run_ref in runs {
        let mut reader = table.open_reader(run_ref.run_id)?;
        // Per-row agg: min epoch, max epoch, min hlc, max hlc,
        // contains_unstamped.
        type RunRowAgg = (
            Epoch,
            Epoch,
            Option<HlcTimestamp>,
            Option<HlcTimestamp>,
            bool,
        );
        let mut per_row: BTreeMap<RowId, RunRowAgg> = BTreeMap::new();
        reader.for_each_system(|row_id, committed_epoch, commit_ts, deleted| {
            let entry = per_row.entry(row_id).or_insert((
                committed_epoch,
                committed_epoch,
                commit_ts,
                commit_ts,
                commit_ts.is_none(),
            ));
            if committed_epoch < entry.0 {
                entry.0 = committed_epoch;
            }
            if committed_epoch > entry.1 {
                entry.1 = committed_epoch;
            }
            match (commit_ts, entry.2) {
                (Some(ts), Some(prev)) if ts < prev => entry.2 = Some(ts),
                (Some(_), None) => entry.2 = commit_ts,
                _ => {}
            }
            match (commit_ts, entry.3) {
                (Some(ts), Some(prev)) if ts > prev => entry.3 = Some(ts),
                (Some(_), None) => entry.3 = commit_ts,
                _ => {}
            }
            if commit_ts.is_none() {
                entry.4 = true;
            }
            // Tombstones do not change the locator's min/max envelope (they
            // share the row's commit epoch/HLC with the live version that
            // preceded them), so the deleted flag is intentionally ignored
            // for envelope construction. The HOT/manifest already removes
            // tombstones that are superseded by a newer live version.
            let _ = deleted;
            Ok(())
        })?;
        for (row_id, (min_epoch, max_epoch, min_hlc, max_hlc, contains_unstamped)) in per_row {
            let locator = RunLocator {
                run_id: run_ref.run_id,
                min_epoch,
                max_epoch,
                min_hlc,
                max_hlc,
                contains_unstamped_versions: contains_unstamped,
            };
            // B468-08 ยง12.5: builder must not emit contradictory metadata.
            debug_assert!(
                locator.validate_metadata().is_ok(),
                "rebuild_from_runs_system_only produced invalid locator: {locator:?}"
            );
            if let Err(err) = locator.validate_metadata() {
                return Err(crate::error::MongrelError::InvalidArgument(format!(
                    "run-lookup builder emitted invalid locator metadata: {err:?}"
                )));
            }
            dir.insert(row_id, locator);
        }
    }
    Ok(dir)
}

/// Format version of the on-disk directory. Bumped when the wire format
/// changes in a way that older readers cannot safely interpret.
pub const DIRECTORY_FORMAT_VERSION: u32 = 1;
/// Hard upper bound for a single shard, raised from the 64 MiB legacy limit so
/// very large tables (e.g. 256-run benchmarks) can checkpoint without
/// truncation. The reader enforces only a soft sanity check.
pub const MAX_SHARD_BYTES: u64 = 16 * 1024 * 1024;

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

    fn hlc_from_phys(physical_micros: u64) -> HlcTimestamp {
        HlcTimestamp {
            physical_micros,
            logical: 0,
            node_tiebreaker: 0,
        }
    }

    fn loc(run_id: u128, epoch: u64, hlc: Option<u64>) -> RunLocator {
        RunLocator {
            run_id,
            min_epoch: Epoch(epoch),
            max_epoch: Epoch(epoch),
            min_hlc: hlc.map(hlc_from_phys),
            max_hlc: hlc.map(hlc_from_phys),
            contains_unstamped_versions: hlc.is_none(),
        }
    }

    #[test]
    fn empty_directory_returns_empty_list() {
        let dir = RunLookupDirectory::empty();
        assert!(dir.locate(RowId(42)).is_empty());
        assert_eq!(dir.key_count(), 0);
    }

    #[test]
    fn insert_orders_locators_newest_first() {
        let mut dir = RunLookupDirectory::empty();
        dir.insert(RowId(1), loc(0xA, 1, None));
        dir.insert(RowId(1), loc(0xB, 3, None));
        dir.insert(RowId(1), loc(0xC, 2, None));
        let list = dir.locate(RowId(1));
        let ids: Vec<u128> = list.locators.iter().map(|l| l.run_id).collect();
        assert_eq!(ids, vec![0xB, 0xC, 0xA]);
    }

    #[test]
    fn retain_active_runs_drops_stale_locators() {
        let mut dir = RunLookupDirectory::empty();
        dir.insert(RowId(1), loc(10, 1, None));
        dir.insert(RowId(1), loc(20, 2, None));
        dir.insert(RowId(2), loc(20, 1, None));
        let removed = dir.retain_active_runs(&[10]);
        assert_eq!(removed, 2);
        assert!(dir.locate(RowId(1)).locators.iter().all(|l| l.run_id == 10));
        assert!(dir.locate(RowId(2)).is_empty());
    }

    #[test]
    fn hlc_tie_break_resolves_above_epoch_only() {
        let mut dir = RunLookupDirectory::empty();
        dir.insert(RowId(1), loc(0xA, 5, None));
        dir.insert(RowId(1), loc(0xB, 5, Some(100)));
        let list = dir.locate(RowId(1));
        assert_eq!(list.locators[0].run_id, 0xB, "HLC newer wins on tie");
    }

    #[test]
    fn memory_metrics_report_postings() {
        let mut dir = RunLookupDirectory::empty();
        dir.insert(RowId(1), loc(1, 1, None));
        dir.insert(RowId(1), loc(2, 2, None));
        dir.insert(RowId(2), loc(1, 1, None));
        assert_eq!(dir.total_locators(), 3);
        assert_eq!(dir.key_count(), 2);
        assert_eq!(dir.max_locators_per_row(), 2);
        assert!((dir.avg_locators_per_row() - 1.5).abs() < 1e-9);
    }

    #[test]
    fn checkpoint_roundtrip_preserves_all_locators() {
        let dir = tempdir().unwrap();
        let mut src = RunLookupDirectory::empty();
        src.set_fingerprint(0x0102_0304_0506_0708);
        src.insert(RowId(7), loc(0x11, 1, None));
        src.insert(RowId(7), loc(0x22, 5, Some(100)));
        src.insert(RowId(8), loc(0x33, 3, Some(50)));
        src.insert(RowId(1_000_000), loc(0x44, 9, None));
        let path = dir.path().join("lookup.ckpt");
        src.write_checkpoint(&path).unwrap();

        let restored = RunLookupDirectory::read_checkpoint(&path, 0x0102_0304_0506_0708).unwrap();
        assert_eq!(restored.fingerprint(), Some(0x0102_0304_0506_0708));
        assert_eq!(restored.key_count(), 3);
        assert_eq!(restored.total_locators(), 4);
        for (row_id, expected) in [
            (RowId(7), vec![0x22, 0x11]),
            (RowId(8), vec![0x33]),
            (RowId(1_000_000), vec![0x44]),
        ] {
            let actual: Vec<u128> = restored
                .locate(row_id)
                .locators
                .iter()
                .map(|l| l.run_id)
                .collect();
            assert_eq!(actual, expected, "row_id {row_id} locator order");
        }
        let list7 = restored.locate(RowId(7));
        assert_eq!(
            list7.locators[0].min_hlc.map(|t| t.physical_micros),
            Some(100)
        );
        assert!(list7.locators[1].contains_unstamped_versions);
    }

    #[test]
    fn read_checkpoint_rejects_mismatched_fingerprint() {
        let dir = tempdir().unwrap();
        let mut src = RunLookupDirectory::empty();
        src.set_fingerprint(42);
        src.insert(RowId(1), loc(7, 1, None));
        let path = dir.path().join("lookup.ckpt");
        src.write_checkpoint(&path).unwrap();
        let err = RunLookupDirectory::read_checkpoint(&path, 43).unwrap_err();
        assert_eq!(err.kind(), std::io::ErrorKind::InvalidData);
        assert!(err.to_string().contains("fingerprint"));
    }

    fn stamped_loc(run_id: u128, epoch: u64, hlc: u64) -> RunLocator {
        RunLocator {
            run_id,
            min_epoch: Epoch(epoch),
            max_epoch: Epoch(epoch),
            min_hlc: Some(hlc_from_phys(hlc)),
            max_hlc: Some(hlc_from_phys(hlc)),
            contains_unstamped_versions: false,
        }
    }

    fn mixed_loc(run_id: u128, epoch: u64, hlc: u64) -> RunLocator {
        RunLocator {
            run_id,
            min_epoch: Epoch(epoch),
            max_epoch: Epoch(epoch),
            min_hlc: Some(hlc_from_phys(hlc)),
            max_hlc: Some(hlc_from_phys(hlc)),
            contains_unstamped_versions: true,
        }
    }

    #[test]
    fn decide_distinguishes_complete_miss_from_candidates() {
        let mut dir = RunLookupDirectory::empty();
        // No insert for RowId(99): a complete miss.
        match dir.decide(RowId(99)) {
            DirectoryLookupDecision::CompleteMiss => {}
            other => panic!("expected CompleteMiss, got {other:?}"),
        }
        // Empty posting list is also a complete miss (defensive โ€” should
        // not happen in production, but the contract still says miss).
        dir.insert(RowId(7), stamped_loc(1, 1, 100));
        dir.insert(RowId(7), stamped_loc(2, 2, 200));
        match dir.decide(RowId(7)) {
            DirectoryLookupDecision::Candidates(list) => assert_eq!(list.len(), 2),
            other => panic!("expected Candidates, got {other:?}"),
        }
    }

    #[test]
    fn early_stop_epoch_only_proves_no_beat() {
        // Locator max_epoch = 5; best = epoch 10; cannot beat.
        let l = loc(1, 5, None);
        let snap = Snapshot::at(Epoch(20));
        let best = VersionStamp {
            epoch: Epoch(10),
            hlc: None,
        };
        assert!(
            !l.can_contain_version_newer_than(best, snap),
            "max_epoch <= best.epoch must early-stop"
        );
    }

    #[test]
    fn early_stop_epoch_only_proves_beat() {
        // Locator max_epoch = 15; best = epoch 10; visible cap 20.
        let l = loc(1, 15, None);
        let snap = Snapshot::at(Epoch(20));
        let best = VersionStamp {
            epoch: Epoch(10),
            hlc: None,
        };
        assert!(
            l.can_contain_version_newer_than(best, snap),
            "max_epoch > best.epoch must not early-stop"
        );
    }

    #[test]
    fn early_stop_pure_hlc_proves_no_beat() {
        // Locator max HLC = 100; best HLC = 200; snapshot HLC = 1000.
        // Visible cap = min(100, 1000) = 100 <= best.hlc=200 โ†’ no beat.
        let l = stamped_loc(1, 5, 100);
        let snap = Snapshot::at_hlc(Epoch(20), hlc_from_phys(1000));
        let best = VersionStamp {
            epoch: Epoch(5),
            hlc: Some(hlc_from_phys(200)),
        };
        assert!(
            !l.can_contain_version_newer_than(best, snap),
            "pure HLC: max visible HLC <= best HLC must early-stop"
        );
    }

    #[test]
    fn early_stop_pure_hlc_proves_beat() {
        // Locator max HLC = 500; best HLC = 200; snapshot HLC = 1000.
        // Visible cap = min(500, 1000) = 500 > best.hlc=200 โ†’ beat possible.
        let l = stamped_loc(1, 5, 500);
        let snap = Snapshot::at_hlc(Epoch(20), hlc_from_phys(1000));
        let best = VersionStamp {
            epoch: Epoch(5),
            hlc: Some(hlc_from_phys(200)),
        };
        assert!(
            l.can_contain_version_newer_than(best, snap),
            "pure HLC: max visible HLC > best HLC must not early-stop"
        );
    }

    #[test]
    fn early_stop_pure_hlc_respects_snapshot_cap() {
        // Locator max HLC = 5000; best HLC = 200; snapshot HLC = 100.
        // Visibility caps at snapshot, so locator cannot contain anything
        // strictly newer than best AND visible. Note: this is the case
        // where `is_impossible_for` would skip the run entirely; the early-
        // stop proof must agree when best is already pinned.
        let l = stamped_loc(1, 5, 5000);
        let snap = Snapshot::at_hlc(Epoch(20), hlc_from_phys(100));
        let best = VersionStamp {
            epoch: Epoch(5),
            hlc: Some(hlc_from_phys(200)),
        };
        assert!(
            !l.can_contain_version_newer_than(best, snap),
            "snapshot HLC cap below locator max must early-stop when best > cap"
        );
    }

    #[test]
    fn early_stop_mixed_with_unstamped_falls_back_to_epoch() {
        // Locator has stamped HLC=100 AND unstamped; best has high epoch
        // 200 but no HLC. Mixed comparison falls back to epoch. Even
        // though the locator's max HLC is below any sensible best, the
        // unstamped rows can beat on epoch alone.
        let l = mixed_loc(1, 250, 100);
        let snap = Snapshot::at_hlc(Epoch(300), hlc_from_phys(1000));
        let best = VersionStamp {
            epoch: Epoch(200),
            hlc: None,
        };
        assert!(
            l.can_contain_version_newer_than(best, snap),
            "mixed: unstamped epoch beat path must not early-stop"
        );
        // Inverse: best's epoch exceeds the locator's max.
        let l = mixed_loc(1, 100, 5000);
        let best = VersionStamp {
            epoch: Epoch(200),
            hlc: None,
        };
        assert!(
            !l.can_contain_version_newer_than(best, snap),
            "mixed: max epoch <= best.epoch must early-stop"
        );
    }

    #[test]
    fn early_stop_hlc_snap_with_unstamped_best_uses_epoch_path() {
        // best has no HLC; even under HLC-authoritative snap, comparison
        // falls back to epoch (mixed comparison rule).
        let l = stamped_loc(1, 250, 999);
        let snap = Snapshot::at_hlc(Epoch(300), hlc_from_phys(1000));
        let best = VersionStamp {
            epoch: Epoch(200),
            hlc: None,
        };
        assert!(
            l.can_contain_version_newer_than(best, snap),
            "unstamped best: epoch path wins even under HLC-authority snap"
        );
    }

    #[test]
    fn early_stop_unknown_metadata_stays_conservative() {
        // Spec ยง36.4: when locator metadata cannot prove a remaining run
        // is unable to beat the winner, open the run. Construct a locator
        // whose epoch bound is *equal* to the best โ€” strict inequality is
        // required to prove the locator cannot beat, so a tie must stay
        // conservative.
        let l = RunLocator {
            run_id: 1,
            min_epoch: Epoch(10),
            max_epoch: Epoch(10),
            min_hlc: Some(hlc_from_phys(50)),
            max_hlc: Some(hlc_from_phys(50)),
            contains_unstamped_versions: false,
        };
        let snap = Snapshot::at_hlc(Epoch(20), hlc_from_phys(100));
        let best = VersionStamp {
            epoch: Epoch(10),
            hlc: Some(hlc_from_phys(50)),
        };
        // Strict equality is not a beat โ€” but the proof is conservative
        // and never asserts a tie is a win.
        assert!(
            !l.can_contain_version_newer_than(best, snap),
            "strict tie (equal epoch+HLC) is not a beat; safe to early-stop"
        );

        // Truly unknown: locator with no HLC bounds AND no
        // contains_unstamped flag โ€” metadata is contradictory and we
        // cannot prove anything in either direction. The proof must stay
        // conservative and keep the run open.
        let l_unknown = RunLocator {
            run_id: 2,
            min_epoch: Epoch(10),
            max_epoch: Epoch(10),
            min_hlc: None,
            max_hlc: None,
            contains_unstamped_versions: false,
        };
        let best_lower = VersionStamp {
            epoch: Epoch(5),
            hlc: Some(hlc_from_phys(50)),
        };
        assert!(
            l_unknown.can_contain_version_newer_than(best_lower, snap),
            "unknown metadata with higher possible epoch must stay conservative"
        );
    }

    // ------------------------------------------------------------------
    // REM-A (spec ยง5.6): the HLC impossibility proof must use `min_hlc`.
    // ------------------------------------------------------------------

    #[test]
    fn locator_spanning_hlc_snapshot_is_not_impossible() {
        // One run carries an old visible version (HLC 100) and a newer
        // invisible version (HLC 1,000); the snapshot sits between them.
        let locator = RunLocator {
            run_id: 1,
            min_epoch: Epoch(10),
            max_epoch: Epoch(20),
            min_hlc: Some(hlc_from_phys(100)),
            max_hlc: Some(hlc_from_phys(1_000)),
            contains_unstamped_versions: false,
        };
        let snapshot = Snapshot::at_hlc(Epoch(15), hlc_from_phys(500));
        assert!(
            !locator.is_impossible_for(snapshot),
            "min_hlc 100 <= snapshot 500: the run spans the snapshot and must be opened"
        );
    }

    #[test]
    fn locator_entirely_after_hlc_snapshot_is_impossible() {
        let locator = RunLocator {
            run_id: 1,
            min_epoch: Epoch(10),
            max_epoch: Epoch(20),
            min_hlc: Some(hlc_from_phys(600)),
            max_hlc: Some(hlc_from_phys(1_000)),
            contains_unstamped_versions: false,
        };
        let snapshot = Snapshot::at_hlc(Epoch(15), hlc_from_phys(500));
        assert!(
            locator.is_impossible_for(snapshot),
            "min_hlc 600 > snapshot 500: every stamped version is too new"
        );
    }

    #[test]
    fn locator_entirely_before_hlc_snapshot_is_possible() {
        let locator = RunLocator {
            run_id: 1,
            min_epoch: Epoch(10),
            max_epoch: Epoch(20),
            min_hlc: Some(hlc_from_phys(100)),
            max_hlc: Some(hlc_from_phys(200)),
            contains_unstamped_versions: false,
        };
        let snapshot = Snapshot::at_hlc(Epoch(15), hlc_from_phys(500));
        assert!(
            !locator.is_impossible_for(snapshot),
            "locator entirely below the snapshot is obviously possible"
        );
    }

    #[test]
    fn mixed_locator_is_impossible_only_when_both_paths_are_excluded() {
        // Stamped path open (min_hlc visible) even though the epoch bound
        // exceeds the snapshot epoch.
        let stamped_side_open = RunLocator {
            run_id: 1,
            min_epoch: Epoch(60),
            max_epoch: Epoch(70),
            min_hlc: Some(hlc_from_phys(100)),
            max_hlc: Some(hlc_from_phys(900)),
            contains_unstamped_versions: true,
        };
        let snap = Snapshot::at_hlc(Epoch(50), hlc_from_phys(500));
        assert!(
            !stamped_side_open.is_impossible_for(snap),
            "stamped min_hlc 100 <= 500 keeps the mixed locator possible"
        );
        // Unstamped path open (min_epoch visible) even though every HLC is
        // above the snapshot.
        let unstamped_side_open = RunLocator {
            run_id: 2,
            min_epoch: Epoch(40),
            max_epoch: Epoch(70),
            min_hlc: Some(hlc_from_phys(600)),
            max_hlc: Some(hlc_from_phys(900)),
            contains_unstamped_versions: true,
        };
        assert!(
            !unstamped_side_open.is_impossible_for(snap),
            "unstamped min_epoch 40 <= 50 keeps the mixed locator possible"
        );
        // Both paths excluded: min_hlc above the snapshot AND min_epoch
        // above the snapshot epoch.
        let fully_after = RunLocator {
            run_id: 3,
            min_epoch: Epoch(60),
            max_epoch: Epoch(70),
            min_hlc: Some(hlc_from_phys(600)),
            max_hlc: Some(hlc_from_phys(900)),
            contains_unstamped_versions: true,
        };
        assert!(
            fully_after.is_impossible_for(snap),
            "every stamped version is HLC-invisible and every unstamped version is epoch-invisible"
        );
    }

    // ------------------------------------------------------------------
    // REM-B (spec ยง6.12): authority-matrix tests for the split
    // stamped/unstamped early-stop proof.
    // ------------------------------------------------------------------

    /// Spec ยง6.5: a mixed locator (unstamped epoch 50 + stamped epoch 40 /
    /// HLC 300) must not be epoch-pruned against a stamped best (epoch 100 /
    /// HLC 200) โ€” the stamped candidate wins by HLC.
    #[test]
    fn mixed_locator_stamped_candidate_can_beat_stamped_best_by_hlc() {
        let locator = RunLocator {
            run_id: 1,
            min_epoch: Epoch(40),
            max_epoch: Epoch(50),
            min_hlc: Some(hlc_from_phys(300)),
            max_hlc: Some(hlc_from_phys(300)),
            contains_unstamped_versions: true,
        };
        let snapshot = Snapshot::at_hlc(Epoch(200), hlc_from_phys(400));
        let best = VersionStamp {
            epoch: Epoch(100),
            hlc: Some(hlc_from_phys(200)),
        };
        assert!(
            locator.can_contain_version_newer_than(best, snapshot),
            "stamped candidate HLC 300 > best HLC 200; the locator must be opened"
        );
    }

    /// Spec ยง6.6: under an epoch-only snapshot, two stamped candidates are
    /// still ordered by HLC โ€” the epoch-only snapshot must not disable HLC
    /// recency.
    #[test]
    fn epoch_snapshot_stamped_candidate_can_beat_stamped_best_by_hlc() {
        let locator = stamped_loc(1, 50, 300);
        let snapshot = Snapshot::at(Epoch(200));
        let best = VersionStamp {
            epoch: Epoch(100),
            hlc: Some(hlc_from_phys(200)),
        };
        assert!(
            locator.can_contain_version_newer_than(best, snapshot),
            "stamped vs stamped recency is HLC-based even under an epoch-only snapshot"
        );
        // Negative cell: the locator's max HLC does not exceed the best.
        let older = stamped_loc(2, 50, 150);
        assert!(
            !older.can_contain_version_newer_than(best, snapshot),
            "max_hlc 150 <= best HLC 200 and no unstamped rows: provably cannot beat"
        );
        // Negative cell: no stamped row can be epoch-visible.
        let future = RunLocator {
            run_id: 3,
            min_epoch: Epoch(300),
            max_epoch: Epoch(400),
            min_hlc: Some(hlc_from_phys(300)),
            max_hlc: Some(hlc_from_phys(300)),
            contains_unstamped_versions: false,
        };
        assert!(
            !future.can_contain_version_newer_than(best, snapshot),
            "min_epoch 300 > snapshot epoch 200: no stamped row is epoch-visible"
        );
    }

    /// Spec ยง6.7: a stamped candidate under HLC visibility is visible
    /// regardless of its local epoch, so its epoch must NOT be capped by
    /// `snapshot.epoch` when the best is unstamped.
    #[test]
    fn hlc_snapshot_stamped_candidate_can_beat_unstamped_best_by_epoch() {
        let locator = stamped_loc(1, 500, 200);
        let snapshot = Snapshot::at_hlc(Epoch(300), hlc_from_phys(300));
        let best = VersionStamp {
            epoch: Epoch(400),
            hlc: None,
        };
        assert!(
            locator.can_contain_version_newer_than(best, snapshot),
            "candidate HLC 200 <= snapshot 300 and epoch 500 > best epoch 400: must open"
        );
        // Negative cell: the stamped candidate is HLC-invisible.
        let invisible = stamped_loc(2, 500, 400);
        assert!(
            !invisible.can_contain_version_newer_than(best, snapshot),
            "min_hlc 400 > snapshot 300: no stamped version is visible"
        );
        // Negative cell: visible, but its epoch cannot beat the best.
        let older = stamped_loc(3, 350, 200);
        assert!(
            !older.can_contain_version_newer_than(best, snapshot),
            "max_epoch 350 <= best epoch 400: provably cannot beat"
        );
    }

    /// Unstamped candidates use epoch visibility and epoch recency under
    /// both snapshot modes.
    #[test]
    fn unstamped_candidate_uses_epoch_visibility_under_hlc_snapshot() {
        let locator = loc(1, 250, None); // unstamped-only, epoch 250
        let snapshot = Snapshot::at_hlc(Epoch(300), hlc_from_phys(1_000));
        let best = VersionStamp {
            epoch: Epoch(200),
            hlc: Some(hlc_from_phys(50)),
        };
        assert!(
            locator.can_contain_version_newer_than(best, snapshot),
            "unstamped epoch 250 > best epoch 200 and visible at snapshot epoch 300"
        );
        // Visibility cap: the snapshot epoch hides the whole locator.
        let tight_snap = Snapshot::at_hlc(Epoch(240), hlc_from_phys(1_000));
        let tight_best = VersionStamp {
            epoch: Epoch(240),
            hlc: Some(hlc_from_phys(50)),
        };
        assert!(
            !locator.can_contain_version_newer_than(tight_best, tight_snap),
            "min(250, 240) = 240 <= best epoch 240: no visible unstamped version can beat"
        );
        // Epoch-only snapshot behaves identically for unstamped candidates.
        let epoch_snap = Snapshot::at(Epoch(300));
        assert!(
            locator.can_contain_version_newer_than(best, epoch_snap),
            "unstamped epoch comparison does not depend on the snapshot mode"
        );
    }

    /// Metadata that cannot prove anything in either direction must keep the
    /// run open.
    #[test]
    fn unknown_metadata_is_conservative() {
        let best = VersionStamp {
            epoch: Epoch(5),
            hlc: Some(hlc_from_phys(50)),
        };
        let hlc_snap = Snapshot::at_hlc(Epoch(20), hlc_from_phys(100));
        let epoch_snap = Snapshot::at(Epoch(20));
        // Contradictory: claims fully stamped but records no HLC envelope.
        let no_envelope = RunLocator {
            run_id: 1,
            min_epoch: Epoch(10),
            max_epoch: Epoch(10),
            min_hlc: None,
            max_hlc: None,
            contains_unstamped_versions: false,
        };
        for snap in [hlc_snap, epoch_snap] {
            assert!(
                no_envelope.can_contain_version_newer_than(best, snap),
                "missing HLC envelope without the unstamped flag is unknown: stay conservative"
            );
        }
        // Partial envelope (min without max) is equally unprovable.
        let partial = RunLocator {
            min_hlc: Some(hlc_from_phys(10)),
            ..no_envelope
        };
        assert!(
            partial.can_contain_version_newer_than(best, hlc_snap),
            "a partial HLC envelope cannot prove impossibility: stay conservative"
        );
    }

    /// Strict inequality is required everywhere: equal HLC or equal epoch
    /// never counts as newer.
    #[test]
    fn equal_authority_does_not_count_as_strictly_newer() {
        let hlc_snap = Snapshot::at_hlc(Epoch(20), hlc_from_phys(100));
        // Stamped vs stamped, equal HLC.
        let stamped = stamped_loc(1, 10, 50);
        let stamped_best = VersionStamp {
            epoch: Epoch(10),
            hlc: Some(hlc_from_phys(50)),
        };
        assert!(
            !stamped.can_contain_version_newer_than(stamped_best, hlc_snap),
            "equal HLC is not strictly newer"
        );
        // Unstamped vs unstamped, equal epoch.
        let unstamped = loc(2, 10, None);
        let unstamped_best = VersionStamp {
            epoch: Epoch(10),
            hlc: None,
        };
        assert!(
            !unstamped.can_contain_version_newer_than(unstamped_best, hlc_snap),
            "equal epoch is not strictly newer"
        );
        // Stamped vs unstamped best under an epoch-only snapshot, equal
        // epoch.
        let epoch_snap = Snapshot::at(Epoch(20));
        assert!(
            !stamped.can_contain_version_newer_than(unstamped_best, epoch_snap),
            "equal epoch is not strictly newer on the stamped-vs-unstamped path"
        );
        // Unstamped candidate vs stamped best, equal epoch.
        assert!(
            !unstamped.can_contain_version_newer_than(stamped_best, epoch_snap),
            "equal epoch is not strictly newer on the unstamped path"
        );
    }

    #[test]
    fn missing_hlc_and_not_unstamped_is_conservative_for_is_impossible() {
        let snap = Snapshot::at(Epoch(20));
        let bad = RunLocator {
            run_id: 1,
            min_epoch: Epoch(1),
            max_epoch: Epoch(10),
            min_hlc: None,
            max_hlc: None,
            contains_unstamped_versions: false,
        };
        assert!(bad.validate_metadata().is_err());
        assert!(
            !bad.is_impossible_for(snap),
            "contradictory empty envelope must open the run"
        );
    }

    #[test]
    fn only_min_hlc_present_is_rejected() {
        let loc = RunLocator {
            run_id: 1,
            min_epoch: Epoch(1),
            max_epoch: Epoch(2),
            min_hlc: Some(hlc_from_phys(10)),
            max_hlc: None,
            contains_unstamped_versions: false,
        };
        assert_eq!(
            loc.validate_metadata(),
            Err(RunLocatorMetadataError::PartialHlcEnvelope)
        );
        assert!(!loc.is_impossible_for(Snapshot::at(Epoch(5))));
    }

    #[test]
    fn only_max_hlc_present_is_rejected() {
        let loc = RunLocator {
            run_id: 1,
            min_epoch: Epoch(1),
            max_epoch: Epoch(2),
            min_hlc: None,
            max_hlc: Some(hlc_from_phys(10)),
            contains_unstamped_versions: true,
        };
        assert_eq!(
            loc.validate_metadata(),
            Err(RunLocatorMetadataError::PartialHlcEnvelope)
        );
    }

    #[test]
    fn min_hlc_greater_than_max_hlc_is_rejected() {
        let loc = RunLocator {
            run_id: 1,
            min_epoch: Epoch(1),
            max_epoch: Epoch(2),
            min_hlc: Some(hlc_from_phys(50)),
            max_hlc: Some(hlc_from_phys(10)),
            contains_unstamped_versions: false,
        };
        assert_eq!(
            loc.validate_metadata(),
            Err(RunLocatorMetadataError::MinHlcAfterMaxHlc)
        );
        assert!(!loc.is_impossible_for(Snapshot::at_hlc(Epoch(5), hlc_from_phys(20))));
    }

    #[test]
    fn min_epoch_greater_than_max_epoch_is_rejected() {
        let loc = RunLocator {
            run_id: 1,
            min_epoch: Epoch(9),
            max_epoch: Epoch(2),
            min_hlc: None,
            max_hlc: None,
            contains_unstamped_versions: true,
        };
        assert_eq!(
            loc.validate_metadata(),
            Err(RunLocatorMetadataError::MinEpochAfterMaxEpoch)
        );
    }

    #[test]
    fn valid_unstamped_only_locator_is_accepted() {
        let loc = RunLocator {
            run_id: 1,
            min_epoch: Epoch(1),
            max_epoch: Epoch(2),
            min_hlc: None,
            max_hlc: None,
            contains_unstamped_versions: true,
        };
        assert!(loc.validate_metadata().is_ok());
    }

    #[test]
    fn checkpoint_with_invalid_locator_is_rejected() {
        // B468-08 ยง12.3/12.6: loader must reject contradictory locator metadata.
        let dir = tempfile::tempdir().unwrap();
        let mut src = RunLookupDirectory::empty();
        src.set_fingerprint(42);
        // Insert a valid unstamped-only locator, then mutate in-memory to the
        // contradictory empty envelope before writing the checkpoint.
        src.insert(
            RowId(1),
            RunLocator {
                run_id: 7,
                min_epoch: Epoch(1),
                max_epoch: Epoch(2),
                min_hlc: None,
                max_hlc: None,
                contains_unstamped_versions: true,
            },
        );
        src.postings.get_mut(&RowId(1)).unwrap().locators[0].contains_unstamped_versions = false;
        let path = dir.path().join("lookup-bad.ckpt");
        src.write_checkpoint(&path).unwrap();
        let err = RunLookupDirectory::read_checkpoint(&path, 42).unwrap_err();
        assert_eq!(err.kind(), std::io::ErrorKind::InvalidData);
        assert!(
            err.to_string().contains("locator metadata invalid")
                || err.to_string().contains("EmptyVersionEnvelope"),
            "unexpected error: {err}"
        );
    }
}