hawdb-storage 0.36.0

An embedded Rust graph database for local data-plane workloads
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
// Copyright 2026 Nowledge
// SPDX-License-Identifier: Apache-2.0

//! Captured native directory authority; caller owns publication exclusion and
//! artifact registrations separately. No pathname is retained or consulted by
//! child operations. This primitive does not qualify GC, durability or memory.

use super::{File, FileBacking, NativeFile};
use crate::file_descriptors::{DescriptorKind, DescriptorPermit, FileOpenContext};
use std::ffi::OsStr;
use std::io;
use std::path::Path;

mod control_candidate;
pub use control_candidate::{CapturedControlCandidate, PendingControlPublication};
mod directory_candidate;
pub use directory_candidate::CapturedDirectoryCandidate;
mod directory_component;
pub use directory_component::CapturedDirectoryComponentCandidate;

#[cfg(unix)]
#[path = "source_directory/unix.rs"]
mod platform;
#[cfg(windows)]
#[path = "source_directory/windows.rs"]
mod platform;
#[cfg(not(any(unix, windows)))]
mod platform {
    use super::*;
    pub(super) fn create_directory_component(
        _root: &std::fs::File,
        _name: DirectoryComponent<'_>,
    ) -> io::Result<()> {
        Err(unsupported())
    }
    pub(super) fn open_directory_component(
        _root: &std::fs::File,
        _name: DirectoryComponent<'_>,
    ) -> io::Result<std::fs::File> {
        Err(unsupported())
    }
    pub(super) fn create_directory(_root: &std::fs::File, _name: ChildName<'_>) -> io::Result<()> {
        Err(unsupported())
    }
    pub(super) fn link_child_new(
        _source: &std::fs::File,
        _source_name: ChildName<'_>,
        _expected: &std::fs::File,
        _target: &std::fs::File,
        _target_name: ChildName<'_>,
        _context: &FileOpenContext,
    ) -> io::Result<()> {
        Err(unsupported())
    }
    pub(super) fn create_control_candidate(
        _root: &std::fs::File,
        _name: ChildName<'_>,
    ) -> io::Result<std::fs::File> {
        Err(unsupported())
    }
    pub(super) fn matches_child_file(
        _root: &std::fs::File,
        _name: ChildName<'_>,
        _file: &std::fs::File,
        _context: &FileOpenContext,
    ) -> io::Result<bool> {
        Err(unsupported())
    }
    pub(super) fn open_wal_append_child(
        _root: &std::fs::File,
        _name: ChildName<'_>,
    ) -> io::Result<std::fs::File> {
        Err(unsupported())
    }
    pub(super) fn open_mutable_child(
        _root: &std::fs::File,
        _name: ChildName<'_>,
    ) -> io::Result<std::fs::File> {
        Err(unsupported())
    }
    pub(super) fn replace_control(
        _root: &std::fs::File,
        _file: &std::fs::File,
        _source: ChildName<'_>,
        _target: ChildName<'_>,
    ) -> io::Result<()> {
        Err(unsupported())
    }
    pub(super) fn open_root(_path: &Path) -> io::Result<std::fs::File> {
        Err(unsupported())
    }
    pub(super) fn open_child(
        _root: &std::fs::File,
        _name: ChildName<'_>,
    ) -> io::Result<std::fs::File> {
        Err(unsupported())
    }
    pub(super) fn open_directory(
        _root: &std::fs::File,
        _name: ChildName<'_>,
    ) -> io::Result<std::fs::File> {
        Err(unsupported())
    }
    pub(super) fn remove_child(
        _root: &std::fs::File,
        _name: ChildName<'_>,
        _context: &FileOpenContext,
    ) -> io::Result<()> {
        Err(unsupported())
    }
    pub(super) fn remove_directory(
        _root: &std::fs::File,
        _name: ChildName<'_>,
        _context: &FileOpenContext,
    ) -> io::Result<()> {
        Err(unsupported())
    }
    pub(super) fn open_lock_file(
        _root: &std::fs::File,
        _name: ChildName<'_>,
        _mode: SourceDirectoryLockMode,
    ) -> io::Result<std::fs::File> {
        Err(unsupported())
    }
    #[cfg(any(test, feature = "test-support"))]
    pub(super) fn open_recorded_lock(
        _root: &std::fs::File,
        _name: ChildName<'_>,
        _create_new: bool,
    ) -> io::Result<std::fs::File> {
        Err(unsupported())
    }
    pub(super) fn visit_children(
        _root: &std::fs::File,
        _visitor: &mut dyn FnMut(&str) -> io::Result<()>,
    ) -> io::Result<()> {
        Err(unsupported())
    }
    pub(super) fn sync_directory(
        _root: &std::fs::File,
        _context: &FileOpenContext,
    ) -> io::Result<()> {
        Err(unsupported())
    }
    fn unsupported() -> io::Error {
        io::Error::new(
            io::ErrorKind::Unsupported,
            "native source directory requires Unix or Windows",
        )
    }
}

#[cfg(any(test, feature = "test-support"))]
mod power_loss_root;
#[cfg(any(test, feature = "test-support"))]
pub(crate) use power_loss_root::PowerLossRoot;

/// Physical identity copied while a counted directory handle is live. A
/// registry must retain that handle while using the key; this is not a logical
/// incarnation token and must not be reused after all capturing handles close.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct SourceDirectoryIdentity(crate::native_file_identity::NativeIdentity);

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SourceDirectoryLockMode {
    ReadExisting,
    ReadWriteCreate,
}

/// Captured native directory authority. Artifact names retain portable ASCII
/// validation; configured directory components use separate native validation.
/// Native handles and their descriptor receipts stay together.
///
/// Capture and every method require the caller's I/O admission. This type does
/// not take publication ownership, register reader pins, exclude concurrent
/// deletion, or authorize a complete source watermark. Recorder contexts bind
/// the held directory and returned files to logical inodes on Linux, Apple and
/// Windows; unsupported platforms and unbound recorder handles refuse.
#[derive(Debug)]
pub struct SourceDirectory {
    root: File,
    context: FileOpenContext,
    identity: SourceDirectoryIdentity,
    capture_id: u64,
    cached_children: std::sync::atomic::AtomicBool,
    #[cfg(any(test, feature = "test-support"))]
    trace: Option<Box<crate::power_loss::NativeDirectoryTrace>>,
}

impl SourceDirectory {
    /// Use the same registered (or standalone) descriptor domain as File::open.
    /// This does not create a new project or a new descriptor allowance.
    pub fn capture_for_path(path: &Path) -> io::Result<Self> {
        Self::capture(path, &crate::file_descriptors::context_for_path(path)?)
    }

    /// Borrow fresh operation capacity from the captured descriptor domain.
    pub fn operation_context(&self) -> FileOpenContext {
        self.context.for_operation()
    }

    /// Use an existing project context; no new registry/domain is created.
    pub fn capture(path: &Path, context: &FileOpenContext) -> io::Result<Self> {
        let native = native_scope(context)?;
        let permit = context.acquire(DescriptorKind::Transient)?;
        #[cfg(any(test, feature = "test-support"))]
        let (inner, trace) = match native._guard.upgrade() {
            Some(core) => {
                let (inner, trace) =
                    crate::power_loss::NativeDirectoryTrace::capture(core, path, context)?;
                (inner, Some(Box::new(trace)))
            }
            None => (
                platform::open_root(path).map_err(|error| context.map_open_error(error))?,
                None,
            ),
        };
        #[cfg(not(any(test, feature = "test-support")))]
        let inner = platform::open_root(path).map_err(|error| context.map_open_error(error))?;
        #[cfg(not(any(test, feature = "test-support")))]
        let _ = native;
        if !inner.metadata()?.is_dir() {
            return Err(io::Error::new(
                io::ErrorKind::NotADirectory,
                "source root is not a directory",
            ));
        }
        let identity = SourceDirectoryIdentity(crate::native_file_identity::of_file(&inner)?);
        Ok(Self {
            identity,
            capture_id: next_capture_id()?,
            cached_children: std::sync::atomic::AtomicBool::new(false),
            root: counted(inner, permit),
            // Keep the project domain, not a producer's reservation inventory.
            // Future operations supply their own fresh reserved capacity.
            context: context.without_inventory(),
            #[cfg(any(test, feature = "test-support"))]
            trace,
        })
    }

    /// No I/O, allocation or native handle acquisition is performed. Keep the
    /// captured directory alive while looking up its physical registry key.
    pub fn identity(&self) -> SourceDirectoryIdentity {
        self.identity
    }

    pub fn same_directory(&self, other: &Self) -> bool {
        self.identity.0 == other.identity.0
    }

    /// Duplicate this held directory with fresh capacity from the same project
    /// domain. This never reopens its pathname or duplicates a process lock.
    /// The caller owns metadata admission and any logical source registrations.
    pub fn duplicate(&self, context: &FileOpenContext) -> io::Result<Self> {
        self.require_context(context)?;
        let scope = native_scope(context)?;
        self.require_scope(&scope)?;
        let permit = context.acquire(DescriptorKind::Transient)?;
        let inner = self
            .root
            .with_native(std::fs::File::try_clone)
            .map_err(|error| context.map_open_error(error))?;
        Ok(Self {
            root: counted(inner, permit),
            context: context.without_inventory(),
            identity: self.identity,
            capture_id: next_capture_id()?,
            cached_children: std::sync::atomic::AtomicBool::new(false),
            #[cfg(any(test, feature = "test-support"))]
            trace: self.trace.clone(),
        })
    }

    /// Conservative owned metadata for a duplicate or generated child capture.
    /// This excludes shared project/model state and performs no I/O/allocation.
    pub fn metadata_bound(&self, child_bytes: usize) -> io::Result<usize> {
        let _ = child_bytes;
        let bytes = std::mem::size_of::<Self>();
        #[cfg(any(test, feature = "test-support"))]
        if let Some(trace) = &self.trace {
            return bytes
                .checked_add(trace.metadata_bound(child_bytes)?)
                .ok_or_else(|| io::Error::other("directory metadata bound overflow"));
        }
        Ok(bytes)
    }

    /// Exclusively create one regular read/write child. A private-directory
    /// owner must retain cleanup evidence before calling this primitive.
    pub fn create_child_new(&self, context: &FileOpenContext, name: &str) -> io::Result<File> {
        self.open_mutable_child(context, name, true)
    }

    /// Pure domain comparison; does not open a file or consume FD inventory.
    pub fn shares_project_context(&self, context: &FileOpenContext) -> bool {
        std::sync::Arc::ptr_eq(&self.context.state, &context.state)
    }

    /// Check an initial/legacy namespace label against this held directory.
    /// This is a binding fence, not logical incarnation or an ABA proof; child
    /// operations must still use the captured directory after the fence.
    #[doc(hidden)]
    pub fn matches_namespace(&self, path: &Path) -> io::Result<bool> {
        #[cfg(windows)]
        {
            // A directory requires BACKUP_SEMANTICS; File::open is for regular
            // files. The fresh capture uses the same counted descriptor domain.
            Ok(self.same_directory(&Self::capture(path, &self.operation_context())?))
        }
        #[cfg(unix)]
        {
            // Capture already pinned the identity with the original handle.
            // Only the namespace can change; retain fresh, counted path lookup.
            Ok(self.identity.0
                == crate::native_file_identity::of_metadata(
                    &self.operation_context().metadata(path)?,
                ))
        }
        #[cfg(not(any(unix, windows)))]
        self.root.matches_native_path(path)
    }

    /// Opens only a hidden `.lock` control file, never truncating an existing
    /// file. Lock acquisition/release and process-registry lifetime belong to
    /// the caller. ReadExisting supports a read-only root without creating data.
    pub fn open_lock_file(
        &self,
        context: &FileOpenContext,
        name: &str,
        mode: SourceDirectoryLockMode,
    ) -> io::Result<File> {
        self.require_context(context)?;
        let name = ChildName::new(name)?;
        if !name.0.starts_with('.') || !name.0.ends_with(".lock") || name.0.len() < 7 {
            return Err(io::Error::new(
                io::ErrorKind::InvalidInput,
                "invalid source ownership lock name",
            ));
        }
        self.open_named_lock(context, name, mode)
    }

    pub(crate) fn open_database_ownership_lock(
        &self,
        context: &FileOpenContext,
        mode: SourceDirectoryLockMode,
    ) -> io::Result<File> {
        self.open_named_lock(
            context,
            ChildName::new(crate::ownership::DATABASE_DIRECTORY_LOCK_FILE)?,
            mode,
        )
    }

    pub(crate) fn open_catalog_metadata_lock(&self, context: &FileOpenContext) -> io::Result<File> {
        self.open_named_lock(
            context,
            ChildName::new(crate::branch_catalog::METADATA_LOCK_FILE)?,
            SourceDirectoryLockMode::ReadWriteCreate,
        )
    }

    fn open_named_lock(
        &self,
        context: &FileOpenContext,
        name: ChildName<'_>,
        mode: SourceDirectoryLockMode,
    ) -> io::Result<File> {
        self.require_context(context)?;
        let _native = native_scope(context)?;
        self.require_scope(&_native)?;
        let permit = context.acquire(DescriptorKind::OwnershipLock)?;
        let open = || {
            self.root
                .with_native(|root| platform::open_lock_file(root, name, mode))
                .map_err(|error| context.map_open_error(error))
        };
        #[cfg(any(test, feature = "test-support"))]
        let (inner, trace) = match self.trace.as_deref() {
            Some(trace) => {
                let (inner, trace) = match mode {
                    SourceDirectoryLockMode::ReadExisting => {
                        trace.open_child(Path::new(name.0), open)?
                    }
                    SourceDirectoryLockMode::ReadWriteCreate => {
                        trace.open_lock(Path::new(name.0), |create_new| {
                            self.root
                                .with_native(|root| {
                                    platform::open_recorded_lock(root, name, create_new)
                                })
                                .map_err(|error| context.map_open_error(error))
                        })?
                    }
                };
                (inner, Some(Box::new(trace)))
            }
            None => (open()?, None),
        };
        #[cfg(not(any(test, feature = "test-support")))]
        let inner = open()?;
        if !inner.metadata()?.is_file() {
            return Err(io::Error::new(
                io::ErrorKind::InvalidData,
                "source lock is not a regular file",
            ));
        }
        #[cfg(any(test, feature = "test-support"))]
        {
            Ok(counted_traced(inner, permit, trace))
        }
        #[cfg(not(any(test, feature = "test-support")))]
        {
            Ok(counted(inner, permit))
        }
    }

    /// Opens one regular file without following a child symlink/reparse point.
    /// The returned file owns an admitted native handle; it is not an evictable
    /// path-based cache reference. The caller controls its retention lifetime.
    /// A fresh operation context supplies its reserved FD capacity; the
    /// directory capture context is never reused for child admission.
    pub fn open_child(&self, context: &FileOpenContext, name: &str) -> io::Result<File> {
        self.open_child_kind(context, name, DescriptorKind::Transient)
    }

    pub(crate) fn open_child_kind(
        &self,
        context: &FileOpenContext,
        name: &str,
        kind: DescriptorKind,
    ) -> io::Result<File> {
        self.open_existing_child(context, name, kind, false)
    }

    /// Compare a held file with one regular, non-symlink component through this
    /// original root. This is a binding fence, not publication/adoption authority.
    /// Unix uses fstatat without another open/close of a process lock inode;
    /// Windows charges its temporary validation handle to this original context.
    pub fn matches_child_file(
        &self,
        context: &FileOpenContext,
        name: &str,
        file: &File,
    ) -> io::Result<bool> {
        self.require_context(context)?;
        let name = ChildName::new(name)?;
        let scope = native_scope(context)?;
        self.require_scope(&scope)?;
        self.matches_child_file_scoped(context, name, file)
    }

    fn matches_child_file_scoped(
        &self,
        context: &FileOpenContext,
        name: ChildName<'_>,
        file: &File,
    ) -> io::Result<bool> {
        self.root.with_native(|root| {
            file.with_native(|file| platform::matches_child_file(root, name, file, context))
        })
    }

    /// Reuse one immutable regular file in another captured directory without
    /// copying its bytes or resolving either directory's diagnostic pathname.
    /// Both directories must belong to the same project/recorder; `expected`
    /// pins the original source file. The caller excludes source mutation and
    /// target-name reuse and owns the target's cleanup/publication custody.
    ///
    /// Creation is exclusive. A later identity check or lost response can fail
    /// after the link exists: retain original target custody rather than adopt
    /// or retry the name. This does not synchronize the target directory or
    /// grant integrity, source-watermark, publication or retirement authority.
    pub fn link_child_new(
        &self,
        context: &FileOpenContext,
        source_name: &str,
        expected: &File,
        target: &Self,
        target_name: &str,
    ) -> io::Result<()> {
        self.require_context(context)?;
        target.require_context(context)?;
        let FileBacking::Native(native_expected) = &expected.backing else {
            return Err(io::Error::new(
                io::ErrorKind::InvalidInput,
                "immutable link requires an already held native source",
            ));
        };
        self.require_context(&native_expected.permit.context)?;
        let source_name = ChildName::new(source_name)?;
        let target_name = ChildName::new(target_name)?;
        let scope = native_scope(context)?;
        self.require_scope(&scope)?;
        target.require_scope(&scope)?;
        if !self.matches_child_file_scoped(context, source_name, expected)? {
            return Err(io::Error::new(
                io::ErrorKind::InvalidData,
                "immutable link source binding changed",
            ));
        }
        let native = || {
            self.root
                .with_native(|source| {
                    target.root.with_native(|destination| {
                        // The directory recorder owns model exclusion around
                        // this native call. Re-entering the file trace would
                        // recursively lock that same model. Native custody was
                        // checked above; no cache lookup or fresh open occurs.
                        platform::link_child_new(
                            source,
                            source_name,
                            &native_expected.inner,
                            destination,
                            target_name,
                            context,
                        )
                    })
                })
                .map_err(|error| context.map_open_error(error))
        };
        #[cfg(any(test, feature = "test-support"))]
        match (self.trace.as_deref(), target.trace.as_deref()) {
            (Some(source), Some(target)) => source.link_child_new(
                Path::new(source_name.0),
                target,
                Path::new(target_name.0),
                native,
            )?,
            (None, None) => native()?,
            _ => {
                return Err(io::Error::new(
                    io::ErrorKind::Unsupported,
                    "immutable link has mismatched recorder custody",
                ))
            }
        }
        #[cfg(not(any(test, feature = "test-support")))]
        native()?;
        if !target.matches_child_file_scoped(context, target_name, expected)? {
            return Err(io::Error::new(
                io::ErrorKind::InvalidData,
                "immutable linked target binding changed",
            ));
        }
        Ok(())
    }

    /// Existing current-generation WAL only: no create, truncate or publication.
    /// Reading and appending share one native object and its original account.
    pub(crate) fn open_wal_append_child(
        &self,
        context: &FileOpenContext,
        name: &str,
    ) -> io::Result<File> {
        self.open_existing_child(context, name, DescriptorKind::MutableWal, true)
    }

    fn open_existing_child(
        &self,
        context: &FileOpenContext,
        name: &str,
        kind: DescriptorKind,
        append: bool,
    ) -> io::Result<File> {
        self.require_context(context)?;
        let name = ChildName::new(name)?;
        let _native = native_scope(context)?;
        self.require_scope(&_native)?;
        let permit = context.acquire(kind)?;
        let open = || {
            self.root
                .with_native(|root| {
                    if append {
                        platform::open_wal_append_child(root, name)
                    } else {
                        platform::open_child(root, name)
                    }
                })
                .map_err(|error| context.map_open_error(error))
        };
        #[cfg(any(test, feature = "test-support"))]
        let (inner, trace) = match self.trace.as_deref() {
            Some(trace) => {
                let (inner, mut trace) = trace.open_child(Path::new(name.0), open)?;
                if append {
                    trace.mark_append();
                }
                (inner, Some(Box::new(trace)))
            }
            None => (open()?, None),
        };
        #[cfg(not(any(test, feature = "test-support")))]
        let inner = open()?;
        if !inner.metadata()?.is_file() {
            return Err(io::Error::new(
                io::ErrorKind::InvalidData,
                "source child is not a regular file",
            ));
        }
        #[cfg(any(test, feature = "test-support"))]
        {
            Ok(counted_traced(inner, permit, trace))
        }
        #[cfg(not(any(test, feature = "test-support")))]
        {
            Ok(counted(inner, permit))
        }
    }

    /// Catalog writer primitive, not a publication or immutable-file capability.
    /// The catalog owns writer exclusion, sealing and recovery. Existing opens
    /// never create/truncate; new opens are exclusive. Both are captured-relative.
    pub(crate) fn open_mutable_child(
        &self,
        context: &FileOpenContext,
        name: &str,
        create_new: bool,
    ) -> io::Result<File> {
        self.require_context(context)?;
        let name = ChildName::new(name)?;
        let scope = native_scope(context)?;
        self.require_scope(&scope)?;
        let permit = context.acquire(DescriptorKind::Transient)?;
        let open = || {
            self.root
                .with_native(|root| {
                    if create_new {
                        platform::create_control_candidate(root, name)
                    } else {
                        platform::open_mutable_child(root, name)
                    }
                })
                .map_err(|error| context.map_open_error(error))
        };
        #[cfg(any(test, feature = "test-support"))]
        let (inner, trace) = match self.trace.as_deref() {
            Some(trace) => {
                let (inner, trace) = if create_new {
                    trace.create_file(Path::new(name.0), open)?
                } else {
                    trace.open_child(Path::new(name.0), open)?
                };
                (inner, Some(Box::new(trace)))
            }
            None => (open()?, None),
        };
        #[cfg(not(any(test, feature = "test-support")))]
        let inner = open()?;
        if !create_new && !inner.metadata()?.is_file() {
            return Err(io::Error::new(
                io::ErrorKind::InvalidData,
                "mutable catalog child is not a regular file",
            ));
        }
        #[cfg(any(test, feature = "test-support"))]
        {
            Ok(counted_traced(inner, permit, trace))
        }
        #[cfg(not(any(test, feature = "test-support")))]
        {
            Ok(counted(inner, permit))
        }
    }

    /// Exclusively create one child directory in this captured parent.
    /// Existing entries are never adopted, followed, replaced or removed.
    /// Admission precedes native creation; no descriptor remains open afterwards.
    ///
    /// This does not open the child or synchronize its parent entry. A later
    /// open, barrier or lost response can fail after creation; callers retain
    /// the actual parent and candidate name as cleanup/recovery evidence.
    pub fn create_directory_new(&self, context: &FileOpenContext, name: &str) -> io::Result<()> {
        self.require_context(context)?;
        let name = ChildName::new(name)?;
        let _native = native_scope(context)?;
        self.require_scope(&_native)?;
        let _permit = context.acquire(DescriptorKind::Transient)?;
        let create = || {
            self.root
                .with_native(|root| platform::create_directory(root, name))
        };
        #[cfg(any(test, feature = "test-support"))]
        let result = match self.trace.as_deref() {
            Some(trace) => trace.create_directory(Path::new(name.0), create),
            None => create(),
        };
        #[cfg(not(any(test, feature = "test-support")))]
        let result = create();
        result.map_err(|error| context.map_open_error(error))
    }

    /// Capture one child directory without resolving the parent pathname.
    /// Both authorities have independently counted handles; the child can
    /// outlive its parent. Symlinks and reparse points are not followed.
    /// The caller supplies I/O admission and namespace exclusion separately.
    pub fn open_directory(&self, context: &FileOpenContext, name: &str) -> io::Result<Self> {
        self.require_context(context)?;
        let name = ChildName::new(name)?;
        self.open_directory_with(context, Path::new(name.0), |root| {
            platform::open_directory(root, name)
        })
    }

    fn open_directory_with(
        &self,
        context: &FileOpenContext,
        name: &Path,
        open_native: impl FnOnce(&std::fs::File) -> io::Result<std::fs::File>,
    ) -> io::Result<Self> {
        self.require_context(context)?;
        let _native = native_scope(context)?;
        self.require_scope(&_native)?;
        let permit = context.acquire(DescriptorKind::Transient)?;
        let open = || {
            self.root
                .with_native(open_native)
                .map_err(|error| context.map_open_error(error))
        };
        #[cfg(any(test, feature = "test-support"))]
        let (inner, trace) = match self.trace.as_deref() {
            Some(trace) => {
                let (inner, trace) = trace.open_directory(name, open)?;
                (inner, Some(Box::new(trace)))
            }
            None => (open()?, None),
        };
        #[cfg(not(any(test, feature = "test-support")))]
        let inner = {
            let _ = name;
            open()?
        };
        Self::finish_directory_capture(
            context,
            inner,
            permit,
            #[cfg(any(test, feature = "test-support"))]
            trace,
        )
    }

    fn finish_directory_capture(
        context: &FileOpenContext,
        inner: std::fs::File,
        permit: DescriptorPermit,
        #[cfg(any(test, feature = "test-support"))] trace: Option<
            Box<crate::power_loss::NativeDirectoryTrace>,
        >,
    ) -> io::Result<Self> {
        if !inner.metadata()?.is_dir() {
            return Err(io::Error::new(
                io::ErrorKind::NotADirectory,
                "source child is not a directory",
            ));
        }
        let identity = SourceDirectoryIdentity(crate::native_file_identity::of_file(&inner)?);
        Ok(Self {
            root: counted(inner, permit),
            context: context.without_inventory(),
            identity,
            capture_id: next_capture_id()?,
            cached_children: std::sync::atomic::AtomicBool::new(false),
            #[cfg(any(test, feature = "test-support"))]
            trace,
        })
    }

    /// Unlinks one entry relative to the captured root. The caller must exclude
    /// registration/publication and establish that no live reader needs it.
    /// Namespace durability and replay are separate obligations. The operation
    /// context supplies any extra native FD capacity needed for deletion.
    pub fn remove_child(&self, context: &FileOpenContext, name: &str) -> io::Result<()> {
        self.require_context(context)?;
        let name = ChildName::new(name)?;
        let _native = native_scope(context)?;
        self.require_scope(&_native)?;
        let native = || {
            self.root
                .with_native(|root| platform::remove_child(root, name, context))
                .map_err(|error| context.map_open_error(error))
        };
        #[cfg(any(test, feature = "test-support"))]
        if let Some(trace) = self.trace.as_deref() {
            return trace.remove_child(Path::new(name.0), native);
        }
        native()
    }

    /// Consumer retirement already holds durable name ownership and excludes
    /// readers/publication. Recheck the held child before removing its empty
    /// directory; never follow a replacement or recursively remove evidence.
    pub(crate) fn remove_reclaimed_directory(
        &self,
        context: &FileOpenContext,
        name: &str,
        expected: &Self,
    ) -> io::Result<()> {
        self.require_context(context)?;
        let observed = self.open_directory(context, name)?;
        if !observed.same_directory(expected) {
            return Err(io::Error::other("reclaimed directory binding changed"));
        }
        drop(observed);
        let name = ChildName::new(name)?;
        let scope = native_scope(context)?;
        self.require_scope(&scope)?;
        let native = || {
            self.root
                .with_native(|root| platform::remove_directory(root, name, context))
                .map_err(|error| context.map_open_error(error))
        };
        #[cfg(any(test, feature = "test-support"))]
        if let Some(trace) = self.trace.as_deref() {
            return trace.remove_directory(Path::new(name.0), native);
        }
        native()
    }

    /// Persist this captured directory's direct entries. Windows admits a
    /// separate write-capable handle to the same native directory; failures
    /// propagate and cannot substitute for a successful namespace barrier.
    pub fn sync(&self, context: &FileOpenContext) -> io::Result<()> {
        self.require_context(context)?;
        let scope = native_scope(context)?;
        self.require_scope(&scope)?;
        let native = || {
            self.root
                .with_native(|root| platform::sync_directory(root, context))
                .map_err(|error| context.map_open_error(error))
        };
        #[cfg(any(test, feature = "test-support"))]
        if let Some(trace) = self.trace.as_deref() {
            return trace.sync(native);
        }
        native()
    }

    /// Visit generated artifact names through a fresh native directory cursor.
    /// The caller supplies I/O admission and namespace exclusion: enumeration
    /// alone is not a snapshot. Exceeding the row budget or encountering an
    /// unsupported component fails, even after earlier callbacks succeeded;
    /// callers must not publish partial results. Callback errors stop scanning.
    /// Each invocation admits one extra descriptor before opening its cursor.
    pub fn visit_children(
        &self,
        context: &FileOpenContext,
        maximum_entries: usize,
        mut visitor: impl FnMut(&str) -> io::Result<()>,
    ) -> io::Result<()> {
        self.require_context(context)?;
        // This live counted root prevents attaching a new recorder (attach
        // requires zero open/reserved descriptors). Release registration
        // exclusion before invoking user callbacks, which may perform I/O.
        let _permit = {
            let _native = native_scope(context)?;
            self.require_scope(&_native)?;
            context.acquire(DescriptorKind::Transient)?
        };
        let mut remaining = maximum_entries;
        self.root
            .with_native(|root| {
                platform::visit_children(root, &mut |name| {
                    ChildName::new(name)?;
                    remaining = remaining.checked_sub(1).ok_or_else(|| {
                        io::Error::new(
                            io::ErrorKind::InvalidData,
                            "source directory entry budget exceeded",
                        )
                    })?;
                    visitor(name)
                })
            })
            .map_err(|error| context.map_open_error(error))
    }

    pub(super) fn capture_id(&self) -> u64 {
        self.capture_id
    }

    pub(super) fn mark_cached_child(&self) {
        self.cached_children
            .store(true, std::sync::atomic::Ordering::Relaxed);
    }

    /// Close idle captured-source cache leases before releasing cooperative
    /// process custody. Does not revoke any live logical file or read.
    #[doc(hidden)]
    pub fn retire_cached_children(&self) {
        if self
            .cached_children
            .load(std::sync::atomic::Ordering::Relaxed)
            && let Some(handles) = self.context.state.existing_immutable_handles()
        {
            handles.retire_source(self.capture_id);
        }
    }

    fn require_context(&self, context: &FileOpenContext) -> io::Result<()> {
        if self.shares_project_context(context) {
            Ok(())
        } else {
            Err(io::Error::new(
                io::ErrorKind::InvalidInput,
                "source directory operation requires its project context",
            ))
        }
    }

    fn require_scope(&self, scope: &NativeScope<'_>) -> io::Result<()> {
        #[cfg(any(test, feature = "test-support"))]
        match (scope._guard.upgrade(), self.trace.as_deref()) {
            (None, None) => {}
            (Some(core), Some(trace)) if trace.belongs_to(&core) => {}
            _ => {
                return Err(io::Error::new(
                    io::ErrorKind::Unsupported,
                    "source directory is not bound to this native recorder",
                ))
            }
        }
        #[cfg(not(any(test, feature = "test-support")))]
        let _ = scope;
        Ok(())
    }
}

impl Drop for SourceDirectory {
    fn drop(&mut self) {
        self.retire_cached_children();
    }
}

fn next_capture_id() -> io::Result<u64> {
    static NEXT: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1);
    NEXT.try_update(
        std::sync::atomic::Ordering::Relaxed,
        std::sync::atomic::Ordering::Relaxed,
        |value| value.checked_add(1),
    )
    .map_err(|_| io::Error::other("source capture identity exhausted"))
}

pub(super) fn validate_child_name(name: &str) -> io::Result<()> {
    ChildName::new(name).map(|_| ())
}

#[cfg(any(test, feature = "test-support"))]
fn counted_traced(
    inner: std::fs::File,
    permit: DescriptorPermit,
    trace: Option<Box<crate::power_loss::NativeTrace>>,
) -> File {
    File {
        backing: FileBacking::Native(NativeFile {
            inner,
            permit,
            trace,
        }),
    }
}

fn counted(inner: std::fs::File, permit: DescriptorPermit) -> File {
    File {
        backing: FileBacking::Native(NativeFile {
            inner,
            permit,
            #[cfg(any(test, feature = "test-support"))]
            trace: None,
        }),
    }
}

/// Bounded native input storage, not a filesystem filename-equivalence rule.
/// Apple permits existing canonical-equivalent spellings beyond NAME_MAX raw
/// bytes/UTF16 units. Its kernel decides create/lookup eligibility separately.
pub(crate) fn native_component_within_input_bound(name: &OsStr) -> bool {
    #[cfg(windows)]
    {
        use std::os::windows::ffi::OsStrExt;
        name.encode_wide().take(256).count() <= 255
    }
    #[cfg(all(unix, target_vendor = "apple"))]
    {
        name.as_encoded_bytes().len() < libc::PATH_MAX as usize
    }
    #[cfg(not(any(windows, all(unix, target_vendor = "apple"))))]
    {
        name.as_encoded_bytes().len() <= 255
    }
}

#[derive(Clone, Copy)]
struct DirectoryComponent<'a>(&'a OsStr);

impl<'a> DirectoryComponent<'a> {
    fn new(name: &'a OsStr) -> io::Result<Self> {
        if !native_component_within_input_bound(name) {
            return Err(invalid_directory_component());
        }
        let mut components = Path::new(name).components();
        if !matches!(components.next(), Some(std::path::Component::Normal(value)) if value == name)
            || components.next().is_some()
        {
            return Err(invalid_directory_component());
        }
        #[cfg(unix)]
        {
            use std::os::unix::ffi::OsStrExt;
            if name.as_bytes().iter().any(|byte| matches!(byte, 0 | b'/')) {
                return Err(invalid_directory_component());
            }
        }
        #[cfg(windows)]
        {
            use std::os::windows::ffi::OsStrExt;
            if name
                .encode_wide()
                .any(|unit| matches!(unit, 0..=31 | 34 | 42 | 47 | 58 | 60 | 62 | 63 | 92 | 124))
            {
                return Err(invalid_directory_component());
            }
        }
        #[cfg(not(any(unix, windows)))]
        return Err(io::Error::new(
            io::ErrorKind::Unsupported,
            "native directory components require Unix or Windows",
        ));
        #[cfg(any(unix, windows))]
        Ok(Self(name))
    }
}

fn invalid_directory_component() -> io::Error {
    io::Error::new(
        io::ErrorKind::InvalidInput,
        "invalid native directory component",
    )
}

#[derive(Clone, Copy)]
struct ChildName<'a>(&'a str);
impl<'a> ChildName<'a> {
    fn new(name: &'a str) -> io::Result<Self> {
        let name = Self(name);
        // Engine artifact names fit one portable component and fixed native
        // scratch. Reject traversal, streams, separators and NUL before I/O.
        if name.0.is_empty()
            || name.0.len() > 255
            || matches!(name.0, "." | "..")
            || !name
                .0
                .bytes()
                .all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'.' | b'_' | b'-'))
        {
            return Err(io::Error::new(
                io::ErrorKind::InvalidInput,
                "invalid source artifact name",
            ));
        }
        Ok(name)
    }
}

struct NativeScope<'a> {
    #[cfg(any(test, feature = "test-support"))]
    _guard: std::sync::MutexGuard<'a, std::sync::Weak<crate::power_loss::ModelCore>>,
    #[cfg(not(any(test, feature = "test-support")))]
    _borrow: std::marker::PhantomData<&'a FileOpenContext>,
}
fn native_scope(context: &FileOpenContext) -> io::Result<NativeScope<'_>> {
    #[cfg(any(test, feature = "test-support"))]
    {
        // Hold registration exclusion for the operation, so enabling a harness
        // cannot race an unrecorded native namespace mutation.
        let guard = context
            .state
            .power_loss
            .lock()
            .map_err(|_| io::Error::other("power-loss context poisoned"))?;
        Ok(NativeScope { _guard: guard })
    }
    #[cfg(not(any(test, feature = "test-support")))]
    {
        let _ = context;
        Ok(NativeScope {
            _borrow: std::marker::PhantomData,
        })
    }
}

#[cfg(all(test, any(unix, windows)))]
mod tests;

#[cfg(all(test, any(target_os = "linux", target_vendor = "apple", windows)))]
mod recording_tests;