object-log 0.4.0

A buffered, multiplexing append log over pluggable object storage, with a sequencing seam (opaque payloads, durable-on-return writes, group-commit).
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
//! Native append-log facade. Queue policy remains in the consumer's planners.
use super::{
    codec::Scope,
    data::DataFrame,
    head::{
        AppendGroupLimits, AppendInput, FencedHead, NativeHead, OwnedPayload, ProofBudget,
        PublishOutcome, Resolution,
    },
    operation::{ExecutionConfig, Physical, Resources},
    page_store::{Charge, NativeStore, PageBinding, PageStore},
    provision::{NamespaceContext, PublishedNamespace, RetainedRead},
    submission::PublicationAttempt,
};
use crate::{LocalBlobStore, MetadataLimits, ObjectLogError};
use std::sync::Arc;
use tokio::sync::Mutex;
mod read_requirements;
mod semantic;
pub use read_requirements::NativeReadRequirements;
pub use semantic::{
    NativeEditLimits, NativeScanLimits, NativeScanStatus, NativeSemanticContinuation,
    NativeSemanticEdit, NativeSemanticRange, NativeSemanticRow, NativeSemanticScan,
    NativeSemanticValue, NativeSemanticWork,
};

/// Explicit store-wide execution and owned-byte limits.
#[derive(Clone, Copy, Debug)]
pub struct NativeLimits {
    /// Executor worker threads, from one through 64.
    pub workers: usize,
    /// Blocking worker threads, from one through 64.
    pub blocking_workers: usize,
    /// Concurrent physical operations.
    pub operations: usize,
    /// Shared retained and in-flight owned-byte admission.
    pub bytes: u64,
    /// Maximum opaque metadata version bytes.
    pub version_bytes: usize,
    /// Maximum complete literal namespace binding frame bytes.
    pub binding_bytes: usize,
    /// Maximum entries in one conditional append group.
    pub group_entries: usize,
    /// Maximum aggregate framed bytes in one append group.
    pub group_bytes: usize,
}
impl NativeLimits {
    fn validate(self) -> Result<(), ObjectLogError> {
        let bytes = usize::try_from(self.bytes)
            .map_err(|_| ObjectLogError::InvalidConfig("native store limits".into()))?;
        if self.workers == 0
            || self.workers > 64
            || self.blocking_workers == 0
            || self.blocking_workers > 64
            || self.operations == 0
            || self.operations > tokio::sync::Semaphore::MAX_PERMITS
            || self.bytes == 0
            || isize::try_from(self.bytes).is_err()
            || self.version_bytes == 0
            || self.binding_bytes <= 112
            || self.binding_bytes > bytes
            || self.group_entries == 0
            || self.group_bytes == 0
            || self.group_bytes > bytes
        {
            return Err(ObjectLogError::InvalidConfig("native store limits".into()));
        }
        Ok(())
    }
}
struct Owner {
    resources: Arc<Resources>,
    pages: Arc<PageStore>,
    prefix: Arc<str>,
    limits: NativeLimits,
    identity: Arc<()>,
}
impl Owner {
    fn context(&self, physical: Arc<Physical>) -> Result<Arc<NamespaceContext>, ObjectLogError> {
        let pages = self.pages.in_operation(physical)?;
        let _copy = pages.charge_output(self.prefix.len() + std::mem::size_of::<String>())?;
        NamespaceContext::new(pages, self.prefix.to_string(), self.limits.binding_bytes)
    }
}
/// One bounded executor and byte pool shared by all namespaces and clones.
#[derive(Clone)]
pub struct NativeLog {
    owner: Arc<Owner>,
    external_owner: Option<crate::AppendOwner>,
}
/// Validated namespace identity with shared monotonic authority knowledge.
#[derive(Clone)]
pub struct NativeNamespace {
    state: Arc<NamespaceState>,
    origin: Arc<()>,
}
struct NamespaceState {
    known: Mutex<PublishedNamespace>,
    _charge: Arc<Charge>,
}
/// Immutable authenticated read cut; payload reads do not refresh its head.
pub struct NativeReadCut {
    read: RetainedRead,
    origin: Arc<()>,
}
struct WriterBinding {
    writer: FencedHead,
    binding: PageBinding,
    head_key: String,
    _charge: Arc<Charge>,
}
/// Sealed writer family. Clones share knowledge and fencing authority.
#[derive(Clone)]
pub struct NativeWriter {
    inner: Arc<Mutex<WriterBinding>>,
    origin: Arc<()>,
}
/// Caller-retained original publication proof, including after reply loss.
#[derive(Clone)]
pub struct NativePublication {
    attempt: Arc<PublicationAttempt>,
    origin: Arc<()>,
}
/// Borrowed append input; backend-owned copies are admitted before allocation.
pub struct NativeAppend<'a> {
    /// Complete immutable logical identity.
    pub identity: &'a str,
    /// Opaque checked consumer record bytes.
    pub payload: &'a [u8],
    /// Logical records represented by this entry, from one through i32::MAX.
    pub records: u32,
}
struct OwnedAppend {
    identity: String,
    payload: Vec<u8>,
    records: u32,
}
struct LiteralInput {
    bytes: Vec<u8>,
    _charge: Arc<Charge>,
}
struct Inputs {
    entries: Vec<OwnedAppend>,
    _charge: Arc<Charge>,
}
// Drop implementations force whole-container capture under Rust's precise
// closure capture: input bytes cannot outlive their admission field.
impl Drop for LiteralInput {
    fn drop(&mut self) {}
}
impl Drop for Inputs {
    fn drop(&mut self) {}
}

/// Result of the original conditional publication, after authority validation.
#[derive(Debug, PartialEq, Eq)]
pub enum NativePublishOutcome {
    /// The exact original conditional write succeeded.
    Stored(NativeHead),
    /// Storage rejected the original condition; current authority validated.
    Conflict,
}

/// Complete original content resolution; does not establish writer identity.
#[derive(Debug, PartialEq, Eq)]
pub enum NativeResolution {
    /// All original ordered certificates are durably covered.
    DurableContent,
    /// Complete competing coverage is incompatible with the original content.
    IncompatibleCoverage,
    /// Available authenticated coverage cannot classify the complete range.
    Unknown,
}
/// Sealed authenticated entry plan for exact consumer admission before hydration.
pub struct NativeReadPlan {
    entry: super::index::OwnedEntry,
    binding: PageBinding,
    origin: Arc<()>,
    _charge: Arc<Charge>,
}
impl NativeReadPlan {
    /// Return admission bounds from the authenticated entry without hydration.
    /// The returned requirements describe this sealed plan only; see the type's
    /// scope before treating these object-body bounds as transport or memory bounds.
    pub fn requirements(&self) -> Result<NativeReadRequirements, ObjectLogError> {
        NativeReadRequirements::from_entry(self.entry.entry())
    }
    /// Exact authenticated payload length; excludes immutable frame headers.
    pub fn payload_bytes(&self) -> usize {
        self.entry.entry().byte_len as usize
    }
    /// First logical offset represented by the entry.
    pub fn base_offset(&self) -> u64 {
        self.entry.entry().base
    }
    /// Logical records represented by the entry.
    pub fn record_count(&self) -> u32 {
        self.entry.entry().record_count
    }
}
impl Drop for NativeReadPlan {
    fn drop(&mut self) {}
}
/// A charged payload and its authenticated logical range.
pub struct NativeRecord {
    base: u64,
    records: u32,
    payload: OwnedPayload,
    _charge: Arc<Charge>,
}
impl NativeRecord {
    /// First logical offset represented by this immutable entry.
    pub fn base_offset(&self) -> u64 {
        self.base
    }
    /// Number of logical records in this entry.
    pub fn record_count(&self) -> u32 {
        self.records
    }
    /// Borrow payload bytes while their retained owner is alive.
    pub fn bytes(&self) -> &[u8] {
        self.payload.bytes()
    }
    /// Transfer payload without copying; every Bytes alias retains its admission.
    pub fn into_bytes(self) -> bytes::Bytes {
        bytes::Bytes::from_owner(self)
    }
}
impl AsRef<[u8]> for NativeRecord {
    fn as_ref(&self) -> &[u8] {
        self.bytes()
    }
}
impl NativeReadCut {
    fn retained(&self) -> RetainedRead {
        RetainedRead {
            captured: self.read.captured.clone(),
            binding: self.read.binding.clone(),
            knowledge: self.read.knowledge.clone(),
            charge: self.read.charge.clone(),
        }
    }

    /// Opaque complete captured authority, without a mutable root interface.
    pub fn head(&self) -> NativeHead {
        self.read.captured.value()
    }
}
impl NativeLog {
    /// Return an operation-scoped view retaining caller-owned admission.
    ///
    /// This view shares the exact identity, executor, limits and storage of this
    /// log. The original handle is unchanged. This replaces any prior owner on
    /// the returned view; existing views and in-flight operations are unaffected.
    /// Every physical operation started
    /// through the view retains a clone until its last provider child retires,
    /// including after caller cancellation, runtime destruction or task panic.
    /// Keeping this view also keeps the owner alive. Returned namespace, cut and
    /// publication carriers do not retain it; fund retained outputs separately.
    pub fn with_operation_owner(&self, owner: crate::AppendOwner) -> Self {
        Self {
            owner: self.owner.clone(),
            external_owner: Some(owner),
        }
    }

    async fn run<T, F, Fut>(&self, factory: F) -> Result<T, ObjectLogError>
    where
        T: Send + 'static,
        F: FnOnce(Arc<Physical>) -> Fut + Send + 'static,
        Fut: std::future::Future<Output = Result<T, ObjectLogError>> + Send + 'static,
    {
        self.owner
            .resources
            .run_with_owner(self.external_owner.clone(), factory)
            .await
    }

    /// Configure a local native log without contacting storage.
    pub fn local(
        store: LocalBlobStore,
        prefix: String,
        limits: NativeLimits,
    ) -> Result<Self, ObjectLogError> {
        Self::new(Arc::new(store), prefix, limits)
    }
    /// Configure an S3 native log over an already configured adapter.
    #[cfg(feature = "s3")]
    pub fn s3(
        store: crate::S3BlobStore,
        prefix: String,
        limits: NativeLimits,
    ) -> Result<Self, ObjectLogError> {
        Self::new(Arc::new(store), prefix, limits)
    }
    /// Pure S3 native configuration validation before client/cache construction.
    #[cfg(feature = "s3")]
    pub fn preflight_s3(
        prefix: &str,
        bucket: &str,
        limits: NativeLimits,
    ) -> Result<(), ObjectLogError> {
        limits.validate()?;
        super::provision::validate_prefix(prefix)?;
        let metadata = MetadataLimits {
            payload_bytes: limits.binding_bytes.max(limits.group_bytes).max(4096),
            version_bytes: limits.version_bytes,
        };
        let registry_len = prefix
            .len()
            .checked_add("/namespaces".len())
            .ok_or_else(|| invalid("registry length overflow"))?;
        let derived_len = super::provision::candidate_page_prefix_len(registry_len)?;
        let required =
            PageStore::planned_allocation_for_len(derived_len, metadata, |key_len, limits| {
                crate::S3BlobStore::planned_native_source_scratch(
                    key_len,
                    limits.version_bytes,
                    bucket.len(),
                )
            })?;
        if required > usize::try_from(limits.bytes).map_err(|_| invalid("native byte capacity"))? {
            return Err(invalid("native configuration exceeds byte admission"));
        }
        Ok(())
    }
    fn new(
        store: Arc<dyn NativeStore>,
        prefix: String,
        limits: NativeLimits,
    ) -> Result<Self, ObjectLogError> {
        limits.validate()?;
        super::provision::validate_prefix(&prefix)?;
        let metadata = MetadataLimits {
            payload_bytes: limits.binding_bytes.max(limits.group_bytes).max(4096),
            version_bytes: limits.version_bytes,
        };
        let registry_len = prefix
            .len()
            .checked_add("/namespaces".len())
            .ok_or_else(|| invalid("registry length overflow"))?;
        let derived_len = super::provision::candidate_page_prefix_len(registry_len)?;
        let required =
            PageStore::planned_allocation_for_len(derived_len, metadata, |key_len, limits| {
                store.page_source_scratch(key_len, limits)
            })?;
        if required > usize::try_from(limits.bytes).map_err(|_| invalid("native byte capacity"))? {
            return Err(invalid("native configuration exceeds byte admission"));
        }
        let page_prefix = format!("{prefix}/wire-pages");
        let registry_prefix = format!("{prefix}/namespaces");
        let resources = Resources::new(ExecutionConfig {
            workers: limits.workers,
            blocking_workers: limits.blocking_workers,
            operations: limits.operations,
            bytes: limits.bytes,
        })?;
        let pages = PageStore::new(
            store,
            page_prefix,
            Scope {
                namespace: [1; 32],
                incarnation: [1; 32],
            },
            resources.bytes.clone(),
            metadata,
        )?;
        Ok(Self {
            owner: Arc::new(Owner {
                resources,
                pages,
                prefix: registry_prefix.into(),
                limits,
                identity: Arc::new(()),
            }),
            external_owner: None,
        })
    }
    fn check(&self, origin: &Arc<()>) -> Result<(), ObjectLogError> {
        if !Arc::ptr_eq(origin, &self.owner.identity) {
            return Err(invalid("native handle belongs to another store"));
        }
        Ok(())
    }
    fn literal(&self, literal: &[u8]) -> Result<LiteralInput, ObjectLogError> {
        if literal.is_empty()
            || literal
                .len()
                .checked_add(112)
                .is_none_or(|n| n > self.owner.limits.binding_bytes)
        {
            return Err(invalid("literal namespace binding limit"));
        }
        let charge = self.owner.pages.charge_output(
            literal
                .len()
                .checked_add(std::mem::size_of::<Vec<u8>>())
                .ok_or_else(|| invalid("literal size"))?,
        )?;
        Ok(LiteralInput {
            bytes: literal.to_vec(),
            _charge: charge,
        })
    }
    /// Capture an existing namespace. Missing/corrupt selected authority fails closed.
    pub async fn open(&self, literal: &[u8]) -> Result<Option<NativeNamespace>, ObjectLogError> {
        let literal = self.literal(literal)?;
        let owner = self.owner.clone();
        self.run(move |physical| async move {
            let context = owner.context(physical)?;
            let Some(known) = context.open(&literal.bytes).await? else {
                return Ok(None);
            };
            let charge = owner.pages.charge_output(
                std::mem::size_of::<NamespaceState>() + 2 * std::mem::size_of::<usize>(),
            )?;
            Ok(Some(NativeNamespace {
                state: Arc::new(NamespaceState {
                    known: Mutex::new(known),
                    _charge: charge,
                }),
                origin: owner.identity.clone(),
            }))
        })
        .await
    }
    /// Explicitly publish a new empty namespace candidate; this does not provision a queue.
    pub async fn provision_new(&self, literal: &[u8]) -> Result<NativeNamespace, ObjectLogError> {
        let literal = self.literal(literal)?;
        let owner = self.owner.clone();
        self.run(move |physical| async move {
            let context = owner.context(physical)?;
            let charge = owner.pages.charge_output(
                std::mem::size_of::<NamespaceState>() + 2 * std::mem::size_of::<usize>(),
            )?;
            let mut attempt = context.attempt(&literal.bytes).await?;
            attempt.prepare_candidate().await?;
            let known = attempt.publish().await?;
            Ok(NativeNamespace {
                state: Arc::new(NamespaceState {
                    known: Mutex::new(known),
                    _charge: charge,
                }),
                origin: owner.identity.clone(),
            })
        })
        .await
    }
    /// Validate fresh authority and retain its knowledge before delivering a fixed cut.
    pub async fn capture(
        &self,
        namespace: &NativeNamespace,
    ) -> Result<NativeReadCut, ObjectLogError> {
        self.check(&namespace.origin)?;
        let namespace = namespace.clone();
        let owner = self.owner.clone();
        self.run(move |physical| async move {
            let context = owner.context(physical)?;
            let mut known = namespace.state.known.lock().await;
            let current = context.capture_known(known.duplicate(&context)?).await?;
            *known = current;
            Ok(NativeReadCut {
                read: known.retained_read()?,
                origin: owner.identity.clone(),
            })
        })
        .await
    }
    /// Fence the exact retained observation; no hidden fresh capture precedes CAS.
    pub async fn acquire(
        &self,
        namespace: &NativeNamespace,
    ) -> Result<NativeWriter, ObjectLogError> {
        self.check(&namespace.origin)?;
        let namespace = namespace.clone();
        let owner = self.owner.clone();
        self.run(move |physical| async move {
            let context = owner.context(physical)?;
            let known = namespace.state.known.lock().await;
            let selected = known.duplicate(&context)?;
            let charged_key = selected.charged_head_key(
                &context,
                std::mem::size_of::<Mutex<WriterBinding>>() + 2 * std::mem::size_of::<usize>(),
            )?;
            let (writer, binding) = selected.into_writer(&context).await?;
            Ok(NativeWriter {
                inner: Arc::new(Mutex::new(WriterBinding {
                    writer,
                    binding,
                    head_key: charged_key.key,
                    _charge: charged_key.charge,
                })),
                origin: owner.identity.clone(),
            })
        })
        .await
    }
    /// Explicitly refresh a writer in its acquired epoch, without another fence.
    /// The fresh observation is retained before the response is delivered.
    pub async fn refresh_writer(
        &self,
        writer: &NativeWriter,
    ) -> Result<NativeReadCut, ObjectLogError> {
        self.check(&writer.origin)?;
        let writer = writer.clone();
        let owner = self.owner.clone();
        self.run(move |physical| async move {
            let pages = owner.pages.in_operation(physical)?;
            let mut writer = writer.inner.lock().await;
            let selected = pages.for_binding(&writer.binding)?;
            let current = writer
                .writer
                .clone()
                .refresh(selected, &writer.head_key)
                .await?;
            writer.writer = current;
            Ok(NativeReadCut {
                read: RetainedRead {
                    captured: writer.writer.snapshot().clone(),
                    binding: writer.binding.clone(),
                    knowledge: writer.writer.knowledge(),
                    charge: writer._charge.clone(),
                },
                origin: owner.identity.clone(),
            })
        })
        .await
    }

    /// Prepare immutable group objects without publishing the authoritative head.
    pub async fn prepare(
        &self,
        writer: &NativeWriter,
        inputs: &[NativeAppend<'_>],
    ) -> Result<NativePublication, ObjectLogError> {
        self.prepare_inner(writer, inputs, None).await
    }
    async fn prepare_inner(
        &self,
        writer: &NativeWriter,
        inputs: &[NativeAppend<'_>],
        semantic: Option<(&NativeReadCut, &[NativeSemanticEdit<'_>], NativeEditLimits)>,
    ) -> Result<NativePublication, ObjectLogError> {
        self.check(&writer.origin)?;
        if inputs.is_empty() || inputs.len() > self.owner.limits.group_entries {
            return Err(invalid("native group entry limit"));
        }
        let mut framed = 0usize;
        let mut allocation = std::mem::size_of::<Inputs>();
        for (i, input) in inputs.iter().enumerate() {
            if input.records == 0
                || input.records > i32::MAX as u32
                || input.identity.is_empty()
                || input.identity.contains('\0')
                || inputs[..i]
                    .iter()
                    .any(|prior| prior.identity == input.identity)
            {
                return Err(invalid("native append identity or count"));
            }
            let length = if semantic.is_some() {
                DataFrame::coupled_encoded_len(input.identity, input.payload.len())?
            } else {
                DataFrame::encoded_len(input.identity, input.payload.len())?
            };
            framed = framed
                .checked_add(length)
                .ok_or_else(|| invalid("native group bytes"))?;
            allocation = allocation
                .checked_add(input.identity.len())
                .and_then(|n| n.checked_add(input.payload.len()))
                .and_then(|n| {
                    n.checked_add(
                        std::mem::size_of::<OwnedAppend>() + std::mem::size_of::<AppendInput<'_>>(),
                    )
                })
                .ok_or_else(|| invalid("native input allocation"))?;
        }
        if framed > self.owner.limits.group_bytes {
            return Err(invalid("native group framed limit"));
        }
        let semantic = semantic
            .map(|(cut, edits, limits)| {
                self.check(&cut.origin)?;
                Ok::<_, ObjectLogError>((
                    cut.retained(),
                    semantic::SemanticInputs::copy(&self.owner.pages, edits, limits)?,
                ))
            })
            .transpose()?;
        let charge = self.owner.pages.charge_output(allocation)?;
        let inputs = Inputs {
            entries: inputs
                .iter()
                .map(|i| OwnedAppend {
                    identity: i.identity.into(),
                    payload: i.payload.into(),
                    records: i.records,
                })
                .collect(),
            _charge: charge,
        };
        let writer = writer.clone();
        let owner = self.owner.clone();
        self.run(move |physical| async move {
            let scoped = owner.pages.in_operation(physical)?;
            let mut writer = writer.inner.lock().await;
            let pages = scoped.for_binding(&writer.binding)?;
            if let Some((expected, _)) = &semantic
                && !writer.writer.snapshot().same_cut(&expected.captured)
            {
                return Err(invalid(
                    "semantic edit cut differs from writer original authority",
                ));
            }
            let references: Vec<_> = inputs
                .entries
                .iter()
                .map(|i| AppendInput {
                    object_id: &i.identity,
                    payload: &i.payload,
                    records: i.records,
                })
                .collect();
            let limits = AppendGroupLimits {
                entries: owner.limits.group_entries,
                framed_bytes: owner.limits.group_bytes,
            };
            let prepared = if let Some((_, semantic)) = &semantic {
                let edits = semantic.references();
                let mut work = semantic.limits.work.budget();
                writer
                    .writer
                    .take_for_prepare()
                    .prepare_semantic_group(
                        pages.clone(),
                        &references,
                        limits,
                        super::head::SemanticEdits {
                            edits: &edits,
                            max_edits: semantic.limits.edits,
                            max_input_bytes: semantic.limits.input_bytes,
                            work: &mut work,
                        },
                    )
                    .await?
            } else {
                writer
                    .writer
                    .take_for_prepare()
                    .prepare_group(pages.clone(), &references, limits)
                    .await?
            };
            let attempt = PublicationAttempt::new(
                &pages,
                prepared,
                writer.binding.clone(),
                &writer.head_key,
            )?;
            Ok(NativePublication {
                attempt,
                origin: owner.identity.clone(),
            })
        })
        .await
    }
    /// Adopt the head that `publication` stored as the writer's captured head,
    /// making no storage request, so the next `prepare` needs no refresh.
    ///
    /// Fails unless `publication` was stored by this writer family and directly
    /// extends the writer's current captured head in its acquired epoch. After an
    /// error the writer is unchanged; use `resolve` and `refresh_writer`.
    pub async fn advance_writer(
        &self,
        writer: &NativeWriter,
        publication: &NativePublication,
    ) -> Result<NativeHead, ObjectLogError> {
        self.check(&writer.origin)?;
        self.check(&publication.origin)?;
        let mut writer = writer.inner.lock().await;
        let WriterBinding {
            writer: fenced,
            binding,
            head_key,
            ..
        } = &mut *writer;
        publication.attempt.adopt(fenced, binding, head_key).await
    }
    /// Submit the retained original transition once; errors require original resolution.
    pub async fn submit(
        &self,
        publication: &NativePublication,
    ) -> Result<NativePublishOutcome, ObjectLogError> {
        self.check(&publication.origin)?;
        Ok(
            match publication
                .attempt
                .submit_outcome(
                    &self.owner.resources,
                    self.owner.pages.clone(),
                    self.external_owner.clone(),
                )
                .await?
            {
                PublishOutcome::Stored(head) => NativePublishOutcome::Stored(head),
                PublishOutcome::Conflict => NativePublishOutcome::Conflict,
            },
        )
    }
    /// Resolve the complete original group using bounded actual competing entries.
    pub async fn resolve(
        &self,
        publication: &NativePublication,
        entries: usize,
    ) -> Result<NativeResolution, ObjectLogError> {
        self.check(&publication.origin)?;
        Ok(
            match publication
                .attempt
                .resolve(
                    &self.owner.resources,
                    self.owner.pages.clone(),
                    ProofBudget { entries },
                    self.external_owner.clone(),
                )
                .await?
            {
                Resolution::DurableContent => NativeResolution::DurableContent,
                Resolution::Rejected => NativeResolution::IncompatibleCoverage,
                Resolution::Unknown => NativeResolution::Unknown,
            },
        )
    }
    /// Authenticate a fixed-cut entry without hydrating identity or payload bytes.
    pub async fn plan_read(
        &self,
        cut: &NativeReadCut,
        offset: u64,
    ) -> Result<Option<NativeReadPlan>, ObjectLogError> {
        self.check(&cut.origin)?;
        let read = RetainedRead {
            captured: cut.read.captured.clone(),
            binding: cut.read.binding.clone(),
            knowledge: cut.read.knowledge.clone(),
            charge: cut.read.charge.clone(),
        };
        let owner = self.owner.clone();
        self.run(move |physical| async move {
            let pages = owner
                .pages
                .in_operation(physical)?
                .for_binding(&read.binding)?;
            let charge = pages.charge_output(std::mem::size_of::<NativeReadPlan>())?;
            let Some(entry) = read.captured.lookup(pages, offset).await? else {
                return Ok(None);
            };
            Ok(Some(NativeReadPlan {
                entry,
                binding: read.binding.clone(),
                origin: owner.identity.clone(),
                _charge: charge,
            }))
        })
        .await
    }
    /// Authenticate consecutive entries of a fixed cut with one root-to-leaf walk.
    ///
    /// Returns plans for the entries from the one covering `offset` through the
    /// end of that leaf, at most `max_entries`; call again from the next entry's
    /// offset to continue. Empty at or past the cut's end. Each plan is hydrated
    /// with `read_planned` exactly as one from `plan_read`, so payloads can be
    /// fetched concurrently. `max_entries` must be nonzero.
    pub async fn plan_range(
        &self,
        cut: &NativeReadCut,
        offset: u64,
        max_entries: usize,
    ) -> Result<Vec<NativeReadPlan>, ObjectLogError> {
        self.check(&cut.origin)?;
        if max_entries == 0 {
            return Err(ObjectLogError::InvalidConfig(
                "native plan range needs at least one entry".into(),
            ));
        }
        let read = cut.retained();
        let owner = self.owner.clone();
        self.run(move |physical| async move {
            let pages = owner
                .pages
                .in_operation(physical)?
                .for_binding(&read.binding)?;
            let entries = read
                .captured
                .lookup_run(pages.clone(), offset, max_entries)
                .await?;
            let mut plans = Vec::with_capacity(entries.len());
            for entry in entries {
                plans.push(NativeReadPlan {
                    entry,
                    binding: read.binding.clone(),
                    origin: owner.identity.clone(),
                    _charge: pages.charge_output(std::mem::size_of::<NativeReadPlan>())?,
                });
            }
            Ok(plans)
        })
        .await
    }
    /// Hydrate the exact sealed entry after the consumer admits its actual length.
    pub async fn read_planned(
        &self,
        plan: NativeReadPlan,
        max_payload_bytes: usize,
    ) -> Result<NativeRecord, ObjectLogError> {
        self.check(&plan.origin)?;
        let owner = self.owner.clone();
        self.run(move |physical| async move {
            let pages = owner
                .pages
                .in_operation(physical)?
                .for_binding(&plan.binding)?;
            let charge = pages.charge_output(std::mem::size_of::<NativeRecord>())?;
            let (base, records, payload) =
                super::head::CapturedHead::hydrate_record(pages, &plan.entry, max_payload_bytes)
                    .await?;
            Ok(NativeRecord {
                base,
                records,
                payload,
                _charge: charge,
            })
        })
        .await
    }
    /// Read only the original authenticated cut; validate touched pages and frame bytes.
    pub async fn read(
        &self,
        cut: &NativeReadCut,
        offset: u64,
    ) -> Result<Option<NativeRecord>, ObjectLogError> {
        self.read_bounded(cut, offset, self.owner.limits.group_bytes)
            .await
    }
    /// Reject an oversized authenticated payload before issuing its data read.
    pub async fn read_bounded(
        &self,
        cut: &NativeReadCut,
        offset: u64,
        max_payload_bytes: usize,
    ) -> Result<Option<NativeRecord>, ObjectLogError> {
        self.check(&cut.origin)?;
        let read = RetainedRead {
            captured: cut.read.captured.clone(),
            binding: cut.read.binding.clone(),
            knowledge: cut.read.knowledge.clone(),
            charge: cut.read.charge.clone(),
        };
        let owner = self.owner.clone();
        self.run(move |physical| async move {
            let pages = owner
                .pages
                .in_operation(physical)?
                .for_binding(&read.binding)?;
            let charge = pages.charge_output(std::mem::size_of::<NativeRecord>())?;
            let Some((base, records, payload)) = read
                .captured
                .read_record_bounded(pages, offset, max_payload_bytes)
                .await?
            else {
                return Ok(None);
            };
            Ok(Some(NativeRecord {
                base,
                records,
                payload,
                _charge: charge,
            }))
        })
        .await
    }
    /// Close admission and drain actual owned work; retained byte-only handles may survive.
    pub async fn shutdown(&self) -> Result<(), ObjectLogError> {
        self.owner.resources.close_and_drain().await
    }
}
fn invalid(message: &str) -> ObjectLogError {
    ObjectLogError::InvalidBatch(message.into())
}

#[cfg(test)]
mod advance_tests;

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

    #[cfg(feature = "s3")]
    #[tokio::test]
    async fn pure_s3_preflight_matches_constructed_adapter_allocation() {
        assert!(super::super::provision::candidate_page_prefix_len(usize::MAX).is_err());
        let bucket = "preflight-β-bucket";
        let prefix = "preflight/λ";
        let metadata = MetadataLimits {
            payload_bytes: 4096,
            version_bytes: 4096,
        };
        let (_, derived) = super::super::provision::candidate_keys(
            &format!("{prefix}/namespaces"),
            Scope {
                namespace: [1; 32],
                incarnation: [1; 32],
            },
        );
        let required = PageStore::planned_allocation_with(&derived, metadata, |key_len, limits| {
            crate::S3BlobStore::planned_native_source_scratch(
                key_len,
                limits.version_bytes,
                bucket.len(),
            )
        })
        .unwrap();
        let mut limits = NativeLimits {
            workers: 1,
            blocking_workers: 1,
            operations: 1,
            bytes: (required - 1).try_into().unwrap(),
            version_bytes: 4096,
            binding_bytes: 256,
            group_entries: 1,
            group_bytes: 4096,
        };
        assert!(NativeLog::preflight_s3(prefix, bucket, limits).is_err());
        limits.bytes = required.try_into().unwrap();
        NativeLog::preflight_s3(prefix, bucket, limits).unwrap();
        let store = crate::S3BlobStore::new(
            "http://127.0.0.1:1",
            "us-east-1",
            bucket,
            "preflight-key",
            "preflight-secret",
        );
        assert_eq!(
            PageStore::planned_allocation(&store, &derived, metadata).unwrap(),
            required
        );
        let log = NativeLog::s3(store, prefix.into(), limits).unwrap();
        log.shutdown().await.unwrap();
        limits.operations = 0;
        assert!(NativeLog::preflight_s3(prefix, bucket, limits).is_err());
        limits.operations = 1;
        assert!(NativeLog::preflight_s3("invalid/../prefix", bucket, limits).is_err());
    }

    #[tokio::test]
    async fn sealed_read_plan_retains_credit_after_drain() {
        let directory = tempfile::tempdir().unwrap();
        let log = NativeLog::local(
            crate::LocalBlobStore::new(directory.path()),
            "plan-owner".into(),
            NativeLimits {
                workers: 1,
                blocking_workers: 1,
                operations: 2,
                bytes: 4 << 20,
                version_bytes: 4096,
                binding_bytes: 4096,
                group_entries: 1,
                group_bytes: 1 << 20,
            },
        )
        .unwrap();
        let before = log.owner.resources.bytes.available();
        let namespace = log.provision_new(b"queue").await.unwrap();
        let writer = log.acquire(&namespace).await.unwrap();
        let publication = log
            .prepare(
                &writer,
                &[NativeAppend {
                    identity: "one",
                    payload: b"planned",
                    records: 1,
                }],
            )
            .await
            .unwrap();
        log.submit(&publication).await.unwrap();
        let cut = log.capture(&namespace).await.unwrap();
        let plan = log.plan_read(&cut, 0).await.unwrap().unwrap();
        log.shutdown().await.unwrap();
        drop((namespace, writer, publication, cut));
        assert_eq!(plan.payload_bytes(), 7);
        assert!(log.owner.resources.bytes.available() < before);
        drop(plan);
        assert_eq!(log.owner.resources.bytes.available(), before);
    }

    #[tokio::test]
    async fn payload_byte_aliases_retain_credit_after_drain() {
        let directory = tempfile::tempdir().unwrap();
        let log = NativeLog::local(
            crate::LocalBlobStore::new(directory.path()),
            "byte-owner".into(),
            NativeLimits {
                workers: 1,
                blocking_workers: 1,
                operations: 2,
                bytes: 4 << 20,
                version_bytes: 4096,
                binding_bytes: 4096,
                group_entries: 1,
                group_bytes: 1 << 20,
            },
        )
        .unwrap();
        let before = log.owner.resources.bytes.available();
        let namespace = log.provision_new(b"queue").await.unwrap();
        let writer = log.acquire(&namespace).await.unwrap();
        let publication = log
            .prepare(
                &writer,
                &[NativeAppend {
                    identity: "one",
                    payload: b"retained",
                    records: 1,
                }],
            )
            .await
            .unwrap();
        log.submit(&publication).await.unwrap();
        let cut = log.capture(&namespace).await.unwrap();
        let bytes = log.read(&cut, 0).await.unwrap().unwrap().into_bytes();
        let alias = bytes.clone();
        drop(bytes);
        log.shutdown().await.unwrap();
        drop((namespace, writer, publication, cut));
        assert!(log.owner.resources.bytes.available() < before);
        assert_eq!(alias.as_ref(), b"retained");
        drop(alias);
        assert_eq!(log.owner.resources.bytes.available(), before);
    }

    #[tokio::test]
    async fn constructor_rejects_derived_page_allocation_minus_one_before_io() {
        let directory = tempfile::tempdir().unwrap();
        let store = crate::LocalBlobStore::new(directory.path());
        let prefix = "derived-allocation";
        let metadata = MetadataLimits {
            payload_bytes: 4096,
            version_bytes: 4096,
        };
        let (_, derived) = super::super::provision::candidate_keys(
            &format!("{prefix}/namespaces"),
            Scope {
                namespace: [1; 32],
                incarnation: [1; 32],
            },
        );
        let required = PageStore::planned_allocation(&store, &derived, metadata).unwrap();
        let base = PageStore::planned_allocation(&store, &format!("{prefix}/wire-pages"), metadata)
            .unwrap();
        assert!(required > base);
        let mut limits = NativeLimits {
            workers: 1,
            blocking_workers: 1,
            operations: 1,
            bytes: (required - 1).try_into().unwrap(),
            version_bytes: 4096,
            binding_bytes: 256,
            group_entries: 1,
            group_bytes: 4096,
        };
        assert!(NativeLog::local(store.clone(), prefix.into(), limits).is_err());
        assert!(crate::BlobStore::list(&store, "").await.unwrap().is_empty());
        limits.bytes = required.try_into().unwrap();
        let log = NativeLog::local(store.clone(), prefix.into(), limits).unwrap();
        assert!(crate::BlobStore::list(&store, "").await.unwrap().is_empty());
        log.shutdown().await.unwrap();
    }

    #[tokio::test]
    async fn admitted_namespace_state_retains_credit_after_caller_abort() {
        let directory = tempfile::tempdir().unwrap();
        let log = NativeLog::local(
            crate::LocalBlobStore::new(directory.path()),
            "cancelled-namespace".into(),
            NativeLimits {
                workers: 2,
                blocking_workers: 2,
                operations: 4,
                bytes: 4 << 20,
                version_bytes: 4096,
                binding_bytes: 4096,
                group_entries: 16,
                group_bytes: 1 << 20,
            },
        )
        .unwrap();
        let before = log.owner.resources.bytes.available();
        let namespace = log.provision_new(b"queue").await.unwrap();
        let weak = Arc::downgrade(&namespace.state);
        let admitted_credit = log.owner.resources.bytes.available();
        assert!(admitted_credit < before);
        let resources = log.owner.resources.clone();
        let (started, admitted) = tokio::sync::oneshot::channel();
        let (release, resume) = tokio::sync::oneshot::channel();
        let task = tokio::spawn(async move {
            resources
                .run(move |_physical| async move {
                    // Deliberately use only the state field: this reproduces precise
                    // closure capture in capture/acquire without timing-dependent IO.
                    let guard = namespace.state.known.lock().await;
                    started.send(()).unwrap();
                    resume.await.unwrap();
                    drop(guard);
                    Ok(())
                })
                .await
        });
        admitted.await.unwrap();
        task.abort();
        assert!(task.await.unwrap_err().is_cancelled());
        assert!(weak.upgrade().is_some());
        assert_eq!(log.owner.resources.bytes.available(), admitted_credit);
        release.send(()).unwrap();
        log.shutdown().await.unwrap();
        assert!(weak.upgrade().is_none());
        assert_eq!(log.owner.resources.bytes.available(), before);
    }
}