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
//! `cas_walk` — the SHARED lock-free CAS-walk SKELETON (G5.3', design
//! `docs/design/slice3-g5-overlay-genericization-2026-06-09.md` §G5.3').
//!
//! # What this module shares (and what it deliberately does NOT)
//!
//! Before G5.3' the byte (`persistent_artrie/lockfree_cas.rs`) and char
//! (`persistent_artrie_char/lockfree_cas.rs`) overlays carried token-for-token-
//! identical CAS-walk DESCENT logic (find-leaf, create-spine, build-value-spine,
//! and the OnDisk write-path fault-in copied ~7×) that differed only in the key
//! unit `K::Unit` (`u8` vs `u32`). This module lifts the COMMON descent ONCE,
//! generic over `<K: KeyEncoding, V>`:
//!
//! * [`find_leaf_recursive`] / [`find_in_lockfree_trie`] — the non-faulting
//! in-memory point-read walks (membership `bool` + leaf `Arc`).
//! * [`create_spine`] — the bottom-up "build the remaining spine for a key
//! suffix" path builder, parameterized by a leaf-maker closure (so a
//! non-durable non-final leaf vs a durable `as_final` leaf vs a valued leaf
//! are all the SAME reverse-iteration loop). SAME build order as the prior
//! per-variant `create_lockfree_path` / `create_lockfree_path_final` — the
//! on-disk serializer consumes node-build order, so this is format-preserving.
//! * [`build_value_spine`] — the iterative valued path-copy (lifted from the
//! already-iterative per-variant `build_value_path_recursive`).
//! * [`resolve_or_fault`] — the single OnDisk-child resolution primitive (the
//! copy-pasted fault-in), returning a RICH [`ChildResolution`] so each
//! (variant × method) keeps its OWN error/null/absent → enum mapping.
//!
//! What STAYS per-variant (the design's "must stay specialized" list):
//! * the result/error enums (`LockfreeInsertResult` / `LockfreeRemoveResult` /
//! `BuildPathError` / `DurableBuildError` …) and the public
//! `insert_cas[_durable]` / `remove_cas_durable` entry points;
//! * the byte DUAL-method (non-durable two-phase `try_set_final` arbiter +
//! durable single-phase) vs char single `finalize`-flag method;
//! * the per-(variant × method) OnDisk/IO/null/missing mapping (see the table
//! in [`resolve_or_fault`]'s doc) — byte non-durable-insert's
//! `FaultFailed/Null → AlreadyExists` (TERMINAL, NOT a retry) is preserved;
//! * the recovery generation (the durable global `commit_seq`, claimed by the
//! CALLER's retry loop via [`OverlayCasWalk::claim_generation`] — NEVER the
//! walk's `root.version()`; see the §MANDATORY-FIX-1 note below).
//!
//! # MANDATORY FIX 1 (data-loss) — generation comes from `claim_generation`, NOT the walk
//!
//! The recovery generation that flows into `reconcile_lww` is the durable global
//! `commit_seq` (restart-seeded), NOT a node's `root.version()`. The skeleton's
//! retry loop (the per-variant CALLER) claims the generation via
//! [`OverlayCasWalk::claim_generation`] (default `self.claim_commit_seq()`,
//! identical in both variants) and passes the CALLER-CLAIMED
//! `committed_generation` to `commit_rank_and_mark`. The walk's `root.version()`
//! is DROPPED inside the skeleton exactly as both variants do today — a
//! `make_*(_, published_root_version)` hook that read the walk's version would
//! re-introduce the A.2 cross-restart resurrection bug (post-restart version
//! resets → wrong replay order → resurrected/dropped term).
//!
//! # MANDATORY FIX 2 (correctness) — the rich `ChildResolution`
//!
//! byte has FOUR distinct OnDisk/IO mappings; char is uniform (fault-in, only a
//! real I/O failure surfaces). [`resolve_or_fault`] returns the RICH
//! [`ChildResolution`] so the (variant × method) mapping stays per-variant — see
//! the table in its doc.
//!
//! # REC 3 — descent shared, the `try_set_final` arbiter NEVER inherited by durable
//!
//! Only the DESCENT is shared. byte's non-durable two-phase publish (CAS a
//! NON-final spine, THEN the CALLER-level `try_set_final` arbiter) and its durable
//! single-phase publish (CAS a final spine) are NOT merged into one driver — the
//! durable arm must NEVER inherit `try_set_final` (a second commit point breaks
//! single-LP). The leaf-shape choice is explicit per path: the non-durable builder
//! returns the SHARED node (so the caller's `try_set_final` arbitrates) while the
//! durable builder bakes `as_final()` into a fresh node published ONLY via the root
//! CAS (the sole LP).
use Arc;
use cratePersistentARTrieError;
use crateKeyEncoding;
use crateLockFreeOverlay;
use crate;
use crateDictionaryValue;
// ============================================================================
// ChildResolution — the RICH outcome of resolving one spine edge (FIX 2)
// ============================================================================
/// The outcome of resolving a single spine edge during a CAS-walk descent —
/// either an already-in-memory child, a freshly-faulted-in child, an I/O failure
/// faulting an evicted (`OnDisk`) child, a null filler slot, or a missing edge.
///
/// **Why a RICH enum (FIX 2).** byte has FOUR distinct OnDisk/IO mappings (see the
/// table in [`resolve_or_fault`]) and char is uniform; collapsing them to a single
/// `→ Conflict` would silently change byte's non-durable-insert behavior
/// (`FaultFailed/Null → AlreadyExists`, TERMINAL — a livelock if turned into a
/// retry) and lose char's `IoError`. So the resolution primitive returns this and
/// each (variant × method) maps the cells itself.
///
/// `InMem` and `Faulted` are distinguished so a caller could (today none do) tell a
/// resident child from a freshly-faulted one; both carry the descend target.
/// `FaultFailed` boxes the error so the common arms stay pointer-sized.
///
pub
/// Whether to fault an evicted (`OnDisk`) child back in during [`resolve_or_fault`].
///
/// The byte VALUE path (`build_value_path_recursive`) historically returned `None`
/// on an OnDisk child WITHOUT faulting (the `as_in_mem()?` short-circuit). The byte
/// value path now DOES fault (it was migrated; the in-mem-only contract is gone),
/// so both variants' value paths fault — but the mode is retained so a caller that
/// must NOT fault (e.g. a strictly non-faulting read) can opt out without routing
/// through a different primitive. Today every CAS-walk caller uses [`Self::Fault`].
///
pub
// ============================================================================
// Free COMMON descent functions — generic over <K: KeyEncoding, V>
// ============================================================================
/// Non-faulting recursive leaf find: descend `key[depth..]` through IN-MEMORY
/// children only, returning the final leaf `Arc` iff the full path exists AND the
/// leaf is final, else `None`. An `OnDisk` child short-circuits to `None` (the
/// lock-free overlay cannot traverse a disk ref without a faulter — the per-variant
/// faulting walk `find_leaf_faulting` handles eviction).
///
/// Token-for-token the prior per-variant `find_leaf_recursive` (byte
/// `lockfree_cas.rs:555`, char `:1293`), now generic over `K::Unit`.
///
pub
/// Non-faulting recursive membership check: `true` iff `key[depth..]` reaches a
/// final node through IN-MEMORY children only. Token-for-token the prior
/// per-variant `find_in_lockfree_trie` (byte `lockfree_cas.rs:511`, char `:1252`),
/// now generic over `K::Unit`.
///
pub
/// Build a NEW path for the remaining `suffix` units, bottom-up, with the terminal
/// leaf produced by `make_leaf`. Returns `(subtree_root, leaf)`:
/// * `subtree_root` — the top of the new path (the caller splices it as a child);
/// * `leaf` — the bottom node (the caller's `try_set_final` target on the
/// non-durable path; ignored on the durable path).
///
/// The leaf is `make_leaf()` (a non-final `OverlayNode::new()` for the non-durable
/// path, an `OverlayNode::new().as_final()` for the durable path, or an
/// `as_final().with_value(..)` valued leaf). The spine is then wrapped bottom-up via
/// `OverlayNode::new().with_child(unit, Child::InMem(child))` over `suffix.iter().rev()`
/// — the EXACT reverse-iteration order the prior per-variant `create_lockfree_path` /
/// `create_lockfree_path_final` used (so the on-disk serializer, which consumes
/// node-build order, sees an identical structure — format-preserving).
///
pub
/// The ITERATIVE valued path-copy: descend `key[depth..]` from `node` collecting the
/// `(parent, unit)` spine (faulting `OnDisk` children in per `fault`), then rebuild
/// it bottom-up with a fresh `as_final().with_value(value)` leaf. Returns the new
/// root `Arc`, or `None` if an `OnDisk` child blocked the copy (a fault failure, or
/// a null filler, or [`FaultMode::NoFaultIn`]) — the caller's increment/value seam
/// treats `None` as a transient conflict / a durable-but-deferred error.
///
/// Lifted from the already-iterative per-variant `build_value_path_recursive` (byte
/// `lockfree_cas.rs:1069`, char `:1348`) — SAME path-copy / absent-spine / valued-leaf
/// semantics and SAME bottom-up build order; only the recursion was already an
/// explicit `Vec`. ITERATIVE because the overlay spine is UN-path-compressed (one
/// node per unit), so a very long key would overflow a recursive stack.
///
/// `fault_in` is the per-variant loader (`load_overlay_node_from_disk`) threaded as a
/// closure so this free function names no `S`; it returns `Result<Arc, _>` and the
/// `.ok()?` collapses an I/O error to `None` (the EXACT prior value-path behavior:
/// both variants' value paths return `None` on a fault-in I/O error — no rich error).
///
pub
/// Resolve a single spine edge `node[unit]` into a RICH [`ChildResolution`] — the
/// single OnDisk-child resolution primitive (the fault-in copy-pasted ~7× before
/// G5.3'). The CALLER maps the resolution to its OWN per-variant enum.
///
/// `fault_in` is the per-variant loader (`load_overlay_node_from_disk`) as a closure
/// so this names no `S`; it returns `Result<Arc, PersistentARTrieError>` so a real
/// I/O failure is distinguishable from a missing/null edge (which is what
/// distinguishes byte's `FaultFailed → AlreadyExists` from char's `Io → IoError`).
///
/// # The (variant × method × resolution) mapping table (FIX 2 — assert each cell matches today)
///
/// Each cell is how that method maps the [`ChildResolution`] variant. `descend`
/// means recurse into the resolved child. Verified against source at the cited lines.
///
/// ```text
/// │ InMem │ Faulted │ FaultFailed │ Null │ Absent
/// ──────────────────────────────────────┼─────────┼─────────┼───────────────────┼───────────────────┼───────────────────
/// byte non-durable insert │ descend │ descend │ AlreadyExists │ AlreadyExists │ create_spine
/// build_path_recursive (byte:395-421) │ │ │ (TERMINAL, false)│ (TERMINAL, false)│ (non-final leaf)
/// byte durable insert │ descend │ descend │ Conflict (retry) │ Conflict (retry) │ create_spine
/// build_final_path_recursive │ │ │ │ │ (final leaf)
/// (byte:920-952) │ │ │ │ │
/// byte durable remove │ descend │ descend │ Conflict (retry) │ AlreadyExists │ AlreadyExists
/// build_remove_path_recursive │ │ │ │ (= absent) │ (= absent)
/// (byte:1030-1059) │ │ │ │ │
/// byte VALUE path │ descend │ descend │ None │ None │ create valued
/// build_value_path_recursive │ │ │ (no rich error) │ (no rich error) │ spine
/// (byte:1100-1132) │ │ │ │ │
/// char insert │ descend │ descend │ Io(e) → IoError │ AlreadyExists │ create_spine
/// build_path_recursive (char:1040-) │ │ │ │ │ (finalize flag)
/// char remove │ descend │ descend │ Io(e) → IoError │ AlreadyAbsent │ AlreadyAbsent
/// build_remove_path_recursive │ │ │ │ │
/// (char:911-954) │ │ │ │ │
/// char VALUE path │ descend │ descend │ None │ None │ create valued
/// build_value_path_recursive │ │ │ (no rich error) │ (no rich error) │ spine
/// (char:1379-1414) │ │ │ │ │
/// ```
///
/// NOTE the VALUE paths use [`build_value_spine`] (which already folds resolution +
/// the `None`-on-fault-failure mapping) rather than this primitive directly, so
/// their two cells are realized there; this primitive serves the membership/remove
/// builders (which need the RICH `FaultFailed` distinction).
///
pub
// ============================================================================
// RemoveAttempt — the UNIFORM core outcome of ONE durable-remove CAS attempt
// ============================================================================
/// The variant-agnostic outcome of a SINGLE durable membership-clear CAS attempt
/// (the [`OverlayCasWalk::try_remove_path_attempt`] hook). The per-variant
/// `LockfreeRemoveResult` enums (char's `Removed(u64)` / byte's `Removed`) collapse
/// to this UNIFORM core — and CRUCIALLY, FIX 1: the char variant's per-attempt
/// `root.version()` is DROPPED at the boundary (it is NOT carried in `Removed`), so
/// the skeleton's retry loop can ONLY rank with the CALLER-claimed
/// `committed_generation` ([`OverlayCasWalk::claim_generation`]). A
/// `Removed(published_root_version)` field would re-open the A.2 cross-restart
/// resurrection bug, so it deliberately carries nothing.
pub
// ============================================================================
// InsertAttempt — the UNIFORM core outcome of ONE durable-insert CAS attempt
// ============================================================================
/// The variant-agnostic outcome of a SINGLE durable membership-insert CAS attempt
/// (the [`OverlayCasWalk::try_insert_path_attempt`] hook) — the DURABLE
/// single-phase publish (a FRESH FINAL leaf inside the root CAS, the sole LP). The
/// per-variant `LockfreeInsertResult` (char `Inserted(node, version)`) /
/// `LockfreeDurableInsertResult` (byte `Inserted(version)`) collapse to this.
///
/// FIX 1: `Inserted` carries NEITHER the leaf NOR the per-attempt `root.version()`
/// — the DURABLE path does not hand a leaf to a caller-level `try_set_final` (the
/// root CAS fully arbitrates — REC 3, single-LP), and the generation is the
/// CALLER-claimed `commit_seq`, NEVER the walk's version.
///
/// This is the DURABLE-insert outcome ONLY. The NON-DURABLE `insert_cas`
/// two-phase publish (CAS a non-final spine, THEN the caller-level `try_set_final`)
/// is NOT routed through the skeleton (REC 3) and does not produce this.
pub
// ============================================================================
// P6 — the UNIFIED durable single-phase CAS outcome + cache direction.
//
// `drive_insert_cas` and `drive_remove_cas` (P3/P2) were 95%-identical retry
// loops differing only in (a) which attempt hook, (b) the cache direction
// (mark-present on insert vs invalidate on remove). P6 unifies their BODY into
// ONE `drive_cas` (REC 3: SAFE — both are DURABLE single-phase paths whose root
// CAS is the sole LP, NEITHER inherits the NON-durable `try_set_final` arbiter;
// the forbidden merge is byte's non-durable two-phase loop, which is NOT routed
// through the skeleton at all). The two public drivers stay as thin dispatchers
// so the insert-vs-remove distinction is explicit at the call boundary and the
// per-variant attempt enums (`InsertAttempt`/`RemoveAttempt`) stay separate.
// ============================================================================
/// The UNIFIED outcome of one durable single-phase CAS attempt, onto which both
/// [`InsertAttempt`] and [`RemoveAttempt`] map (FIX 1: NO generation field —
/// `drive_cas` ranks the CALLER-claimed `commit_seq`, never a walk version).
///
/// `pub(crate)` (not private) only because it appears in the signature of the
/// `pub(crate)` trait method `OverlayCasWalk::drive_cas` (which the two public
/// drivers call). It is never named outside this module's two dispatchers.
pub
/// Which way [`OverlayCasWalk::drive_cas`] touches the positive lookup cache on a
/// state-changing arm. Insert MARKS the term present (a later point read
/// short-circuits present); remove INVALIDATES it (§3.4 — a stale positive entry
/// would otherwise read present forever after a clear).
///
/// `pub(crate)` for the same reason as [`CasOutcome`] (it appears in `drive_cas`'s
/// `pub(crate)` signature); never named outside this module.
pub
// ============================================================================
// OverlayCasWalk — the per-variant specialization hook trait + default skeleton
// ============================================================================
/// The SHARED CAS-walk SKELETON surface (G5.3'). A subtrait of [`LockFreeOverlay`]
/// (so the skeleton has the overlay root + `claim_commit_seq` + `note_cas_retry` in
/// scope). The default method [`Self::claim_generation`] is the FIX-1 generation
/// source — the durable global `commit_seq`, NEVER the walk's `root.version()`.
///
/// P0 (this scaffold) defines only the generation hook + its default. The per-variant
/// descent helpers (`find_*`, `create_spine`, `build_value_spine`, `resolve_or_fault`)
/// are FREE functions above (no trait dispatch needed — they take `&Arc<OverlayNode>`
/// directly), so the variants delegate to them from their inherent `pub(crate)` shims
/// (P1) without an extra trait method. Subsequent phases (P2-P6) add the
/// remove/insert skeleton default methods + their hooks here as they are routed.
///
/// `Self: Sized` so the default methods take `&self` on the concrete monomorph (no
/// `dyn` — fully monomorphized, the design's "hooks monomorphized" requirement).
pub
// ============================================================================
// Send/Sync witness — the scaffold must not regress auto-Send/Sync
// ============================================================================
/// Compile-time witness that the shared CAS-walk types stay `Send + Sync` (the
/// overlay node auto-derives both; these free types must not introduce a non-`Send`
/// field). Zero `unsafe` — the assertion is a no-op generic fn, never called.