acyclic-fs 0.1.5

Rust SDK for versioned workspaces and embedded native mounts
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
//! Pluggable authority and immutable object storage interfaces.

use crate::foundation::{
    AuthorityId, Digest, DurableCommit, Epoch, Head, OperationId, ProposedCommit, Sequence,
};
use crate::performance::{MeasuredResult, OperationFailure, WorkBudget, WorkCounters};
use bytes::Bytes;
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use thiserror::Error;

/// Canonical bytes hashed in addition to one object's body.
pub const OBJECT_DIGEST_ENVELOPE_BYTES: u64 = b"acyclic-fs-object-v1\0".len() as u64 + 1 + 8;

/// Bounds one replay request before storage allocates or returns bytes.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct ReplayLimit {
    /// Maximum records returned.
    pub records: u32,
    /// Maximum cumulative payload bytes returned.
    pub payload_bytes: u64,
}

/// Idempotent authority creation result.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum CreateAuthorityOutcome {
    /// A new empty authority was durably created.
    Created(Head),
    /// The authority already existed and was not changed.
    Existing(Head),
}

/// Successful or rejected compare-and-append result.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum AppendOutcome {
    /// The proposed operation became durable at this commit.
    Committed(DurableCommit),
    /// The same operation and fingerprint was already durable.
    AlreadyCommitted(DurableCommit),
    /// The expected head was stale; no bytes were appended.
    Conflict {
        /// Linearizable current head.
        actual: Head,
    },
    /// The caller epoch was stale; no bytes were appended.
    Fenced {
        /// Active authority epoch.
        actual_epoch: Epoch,
    },
    /// The operation identity exists with a different fingerprint.
    IdempotencyConflict {
        /// Fingerprint already bound to the operation identity.
        committed_fingerprint: Digest,
    },
}

/// Optional authority-side admission condition for one publication.
///
/// Ordinary SDK callers use [`PublicationPermit::Unrestricted`]. Managed
/// operation windows issue lease permits whose gate is checked in the same
/// storage transaction as the generation append.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum PublicationPermit {
    /// No operation-window admission is required.
    Unrestricted,
    /// The exact durable tool lease must still be active.
    Lease {
        /// Authority derived from the admitted workspace identity.
        authority_id: [u8; 16],
        /// Workspace whose mount admitted the operation.
        workspace_id: [u8; 16],
        /// Unique durable lease identity.
        lease_id: [u8; 16],
        /// Provider-evaluated exclusive publication deadline.
        expires_at_millis: u64,
    },
    /// The exact durable multi-authority publication reservation is active.
    Reservation {
        /// Stable logical publication identity.
        operation_id: [u8; 16],
        /// Tail produced when the authority gate was reserved.
        gate_tail: u64,
        /// Exact authority head authenticated when the gate was acquired.
        expected: Head,
    },
}

/// Durable exclusive authority reservation used by multi-root publication.
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct PublicationReservation {
    /// Authority protected by the reservation.
    pub authority_id: AuthorityId,
    /// Stable logical publication identity.
    pub operation_id: OperationId,
    /// Exact authority head validated while acquiring the gate.
    pub expected: Head,
    /// Gate tail that authenticates the reservation holder.
    pub gate_tail: u64,
}

/// Result of atomically reserving an authority at an exact head.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ReservationOutcome {
    /// This operation owns the exclusive gate.
    Reserved(PublicationReservation),
    /// The identical reservation was already acquired.
    AlreadyReserved(PublicationReservation),
    /// The authority head or gate changed before acquisition.
    Conflict {
        /// Linearizable head observed while acquisition failed.
        actual: Head,
    },
}

/// Complete atomic authority append request, including any admission permit.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct GuardedAppend {
    /// Authority receiving the operation.
    pub authority_id: AuthorityId,
    /// Writer epoch expected by the caller.
    pub epoch: Epoch,
    /// Complete expected authority head.
    pub expected: Head,
    /// Canonical durable operation.
    pub commit: ProposedCommit,
    /// Optional operation-window admission requirement.
    pub permit: PublicationPermit,
}

/// Successful or rejected compare-and-fence result.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum FenceOutcome {
    /// The exact expected head was atomically advanced to a fresh writer epoch.
    Advanced(Head),
    /// The authority changed before ownership could be acquired.
    Conflict {
        /// Linearizable current head; no fence was advanced.
        actual: Head,
    },
}

/// Storage-only authority interface.
///
/// Implementations own ordering, fencing, idempotency, durable acknowledgement,
/// and bounded replay. They do not interpret commit payloads.
pub trait AuthorityStore: Send + Sync {
    /// Creates one authority at the supplied non-zero genesis epoch.
    ///
    /// # Errors
    ///
    /// Returns a typed backend error if creation cannot be made durable.
    fn create_authority(
        &self,
        authority_id: AuthorityId,
        genesis_epoch: Epoch,
        budget: WorkBudget,
    ) -> AuthorityResult<CreateAuthorityOutcome>;

    /// Returns the linearizable current head.
    ///
    /// # Errors
    ///
    /// Returns a typed backend error when the authority is missing, corrupt,
    /// unavailable, or cannot be read durably.
    fn head(&self, authority_id: AuthorityId, budget: WorkBudget) -> AuthorityResult<Head>;

    /// Atomically checks epoch/head/idempotency and durably appends one operation.
    ///
    /// # Errors
    ///
    /// Returns a typed backend error when durable evaluation cannot complete.
    /// Semantic conflicts are returned as [`AppendOutcome`] variants.
    fn compare_and_append(
        &self,
        authority_id: AuthorityId,
        epoch: Epoch,
        expected: Head,
        commit: ProposedCommit,
        budget: WorkBudget,
    ) -> AuthorityResult<AppendOutcome>;

    /// Replays a contiguous bounded page after `sequence`.
    ///
    /// # Errors
    ///
    /// Returns a typed backend error for invalid limits, missing/corrupt
    /// authority state, or storage failure.
    fn replay(
        &self,
        authority_id: AuthorityId,
        after: Sequence,
        limit: ReplayLimit,
        budget: WorkBudget,
    ) -> AuthorityResult<Vec<DurableCommit>>;

    /// Atomically compares the complete head, advances its epoch, and
    /// permanently fences prior writers.
    ///
    /// # Errors
    ///
    /// A stale expected head is a semantic [`FenceOutcome::Conflict`], while a
    /// backend failure means the fence could not be durably resolved.
    fn fence(
        &self,
        authority_id: AuthorityId,
        expected: Head,
        budget: WorkBudget,
    ) -> AuthorityResult<FenceOutcome>;

    /// Looks up a durable operation for exact retry resolution.
    ///
    /// # Errors
    ///
    /// Returns a typed backend error when the authority cannot be read safely.
    fn find_operation(
        &self,
        authority_id: AuthorityId,
        operation_id: OperationId,
        budget: WorkBudget,
    ) -> AuthorityResult<Option<DurableCommit>>;
}

impl<T: AuthorityStore + ?Sized> AuthorityStore for Arc<T> {
    fn create_authority(
        &self,
        authority_id: AuthorityId,
        genesis_epoch: Epoch,
        budget: WorkBudget,
    ) -> AuthorityResult<CreateAuthorityOutcome> {
        (**self).create_authority(authority_id, genesis_epoch, budget)
    }

    fn head(&self, authority_id: AuthorityId, budget: WorkBudget) -> AuthorityResult<Head> {
        (**self).head(authority_id, budget)
    }

    fn compare_and_append(
        &self,
        authority_id: AuthorityId,
        epoch: Epoch,
        expected: Head,
        commit: ProposedCommit,
        budget: WorkBudget,
    ) -> AuthorityResult<AppendOutcome> {
        (**self).compare_and_append(authority_id, epoch, expected, commit, budget)
    }

    fn replay(
        &self,
        authority_id: AuthorityId,
        after: Sequence,
        limit: ReplayLimit,
        budget: WorkBudget,
    ) -> AuthorityResult<Vec<DurableCommit>> {
        (**self).replay(authority_id, after, limit, budget)
    }

    fn fence(
        &self,
        authority_id: AuthorityId,
        expected: Head,
        budget: WorkBudget,
    ) -> AuthorityResult<FenceOutcome> {
        (**self).fence(authority_id, expected, budget)
    }

    fn find_operation(
        &self,
        authority_id: AuthorityId,
        operation_id: OperationId,
        budget: WorkBudget,
    ) -> AuthorityResult<Option<DurableCommit>> {
        (**self).find_operation(authority_id, operation_id, budget)
    }
}

/// One successful authority operation and its exact backend work.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct AuthorityReceipt<T> {
    /// Operation value.
    pub value: T,
    /// Exact backend work.
    pub work: WorkCounters,
}

/// One failed authority operation retaining exact spent work.
pub type AuthorityFailure = OperationFailure<AuthorityStoreError>;

/// Receipt-bearing authority result.
pub type AuthorityResult<T> = MeasuredResult<AuthorityReceipt<T>, AuthorityStoreError>;

/// Typed authority storage failures.
#[derive(Debug, Error)]
pub enum AuthorityStoreError {
    /// Operation was cancelled before durable authority work began.
    #[error("authority operation was cancelled")]
    Cancelled,
    /// The authority does not exist.
    #[error("authority does not exist")]
    Missing,
    /// A configured replay bound is invalid.
    #[error("replay limit must have non-zero record and payload bounds")]
    InvalidReplayLimit,
    /// One authority record cannot fit into the requested replay page.
    #[error("next authority record has {observed} payload bytes; replay maximum is {maximum}")]
    ReplayRecordTooLarge {
        /// Next record payload bytes.
        observed: u64,
        /// Requested page payload bound.
        maximum: u64,
    },
    /// One submitted authority payload exceeds the backend admission bound.
    #[error("authority payload has {observed} bytes; maximum is {maximum}")]
    PayloadTooLarge {
        /// Submitted payload bytes.
        observed: u64,
        /// Configured maximum.
        maximum: u64,
    },
    /// Epoch arithmetic cannot represent another fence.
    #[error("authority epoch exhausted")]
    EpochExhausted,
    /// Sequence arithmetic cannot represent another authority fact.
    #[error("authority sequence exhausted")]
    SequenceExhausted,
    /// Persistent state is corrupt and cannot be safely interpreted.
    #[error("authority storage is corrupt: {0}")]
    Corrupt(String),
    /// Persistent I/O failed.
    #[error("authority storage I/O failed: {0}")]
    Io(#[from] std::io::Error),
    /// Durable state may have changed, so the caller must reopen and resolve by identity.
    #[error("authority {operation} outcome is indeterminate: {source}")]
    Indeterminate {
        /// Durable operation whose acknowledgement was lost.
        operation: &'static str,
        /// Underlying storage failure after mutation may have begun.
        #[source]
        source: std::io::Error,
    },
    /// Backend rejected an operation for a stable implementation-specific reason.
    #[error("authority backend rejected operation: {0}")]
    Rejected(String),
    /// Backend work could not be admitted within the caller's hard budget.
    #[error(transparent)]
    Work(#[from] crate::performance::WorkError),
}

/// Immutable object class. Hashes are scoped by both kind and canonical bytes.
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum ObjectKind {
    /// Authenticated blob-index page.
    Blob,
    /// Fixed-size immutable content chunk addressed by its complete bytes.
    BlobChunk,
    /// Authenticated namespace page.
    TreePage,
    /// Authenticated file-extent page.
    ExtentPage,
    /// Authenticated file-table page.
    FileTablePage,
    /// Composite immutable generation root.
    GenerationRoot,
    /// Canonical metadata object.
    Metadata,
    /// Authenticated named extended-attribute/stream page.
    AttributePage,
}

impl ObjectKind {
    /// Returns the stable canonical object-class tag.
    #[must_use]
    pub const fn canonical_tag(self) -> u8 {
        match self {
            Self::Blob => 1,
            Self::BlobChunk => 2,
            Self::TreePage => 3,
            Self::ExtentPage => 4,
            Self::FileTablePage => 5,
            Self::GenerationRoot => 6,
            Self::Metadata => 7,
            Self::AttributePage => 8,
        }
    }

    /// Parses one stable canonical object-class tag.
    ///
    /// # Errors
    ///
    /// Rejects unknown tags instead of guessing a future object class.
    pub const fn from_canonical_tag(tag: u8) -> Result<Self, ObjectKindError> {
        match tag {
            1 => Ok(Self::Blob),
            2 => Ok(Self::BlobChunk),
            3 => Ok(Self::TreePage),
            4 => Ok(Self::ExtentPage),
            5 => Ok(Self::FileTablePage),
            6 => Ok(Self::GenerationRoot),
            7 => Ok(Self::Metadata),
            8 => Ok(Self::AttributePage),
            value => Err(ObjectKindError::Unknown(value)),
        }
    }
}

/// Canonical object-kind parsing failures.
#[derive(Clone, Copy, Debug, Eq, Error, PartialEq)]
pub enum ObjectKindError {
    /// A newer or corrupt object used an unknown kind tag.
    #[error("unknown object kind tag {0}")]
    Unknown(u8),
}

/// Typed object identity.
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct ObjectId {
    /// Object class.
    pub kind: ObjectKind,
    /// Digest over object class and canonical bytes.
    pub digest: Digest,
}

/// Computes the canonical digest for one typed immutable object.
#[must_use]
pub fn object_digest(kind: ObjectKind, bytes: &[u8]) -> Digest {
    let mut hasher = blake3::Hasher::new();
    hasher.update(b"acyclic-fs-object-v1\0");
    hasher.update(&[kind.canonical_tag()]);
    hasher.update(&u64::try_from(bytes.len()).unwrap_or(u64::MAX).to_le_bytes());
    hasher.update(bytes);
    Digest::from_bytes(*hasher.finalize().as_bytes())
}

/// Exact bounded logical byte range.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct ByteRange {
    /// Inclusive start offset.
    pub offset: u64,
    /// Exact requested byte count.
    pub length: u64,
}

/// One successful immutable-object operation and its exact physical work.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ObjectReceipt<T> {
    /// Operation value.
    pub value: T,
    /// Exact backend work, including zero-copy versus copied-byte behavior.
    pub work: WorkCounters,
}

/// How the bytes returned by one immutable-object read remain resident.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ObjectReadRetention {
    /// The returned view shares backend-owned immutable storage and adds no
    /// operation-owned byte buffer.
    Shared,
    /// The backend allocated this exact logical byte capacity and transfers
    /// ownership to the caller until the value is dropped.
    Owned {
        /// Exact requested capacity retained by the returned value.
        logical_bytes: u64,
    },
}

/// Immutable bytes plus explicit retention evidence for zero-copy and peak
/// allocation accounting.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ObjectRead {
    /// Complete canonical object bytes.
    pub bytes: Bytes,
    /// Whether those bytes are a shared view or an owned returned buffer.
    pub retention: ObjectReadRetention,
}

/// One complete immutable object requested as part of an ordered batch.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct ObjectReadRequest {
    /// Exact typed immutable identity.
    pub object_id: ObjectId,
    /// Maximum canonical bytes admitted for this object.
    pub maximum_bytes: u64,
}

/// One canonical immutable object submitted as part of an ordered write batch.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ObjectWrite {
    /// Exact typed immutable identity.
    pub object_id: ObjectId,
    /// Canonical bytes authenticated by `object_id`.
    pub bytes: Bytes,
}

impl std::ops::Deref for ObjectRead {
    type Target = [u8];

    fn deref(&self) -> &Self::Target {
        &self.bytes
    }
}

/// One failed immutable-object operation and all work completed before failure.
pub type ObjectFailure = OperationFailure<ObjectStoreError>;

/// Receipt-bearing immutable-object result. Failure never erases spent work.
pub type ObjectResult<T> = MeasuredResult<ObjectReceipt<T>, ObjectStoreError>;

/// Immutable digest-addressed object storage interface.
///
/// Objects are deliberately read whole. Pages and blob chunks have hard size
/// limits; logical range reads are built over authenticated chunk indexes. This
/// prevents a backend from hiding a whole-object scan behind a tiny range read.
pub trait ObjectStore: Send + Sync {
    /// Admits canonical bytes under their verified identity.
    ///
    /// # Errors
    ///
    /// Returns a typed error for digest mismatch or storage failure.
    fn put(&self, object_id: ObjectId, bytes: Bytes, budget: WorkBudget) -> ObjectResult<()>;

    /// Admits an ordered bounded group through one backend batch operation.
    ///
    /// # Errors
    ///
    /// Returns the first typed failure and all work completed before it.
    fn put_many(&self, writes: &[ObjectWrite], budget: WorkBudget) -> ObjectResult<()>;

    /// Reads exactly the requested range or returns a typed error.
    ///
    /// # Errors
    ///
    /// Returns a typed error when the object is absent, corrupt, unavailable,
    /// or the range is invalid.
    fn read(
        &self,
        object_id: ObjectId,
        maximum_bytes: u64,
        budget: WorkBudget,
    ) -> ObjectResult<ObjectRead>;

    /// Reads an ordered batch of complete bounded objects.
    ///
    /// The result position always matches the request position. Implementations
    /// should coalesce physical transactions or I/O submission where their
    /// backend supports it. The default preserves exact semantics and work
    /// accounting without claiming fewer backend operations.
    ///
    /// # Errors
    ///
    /// Returns the first typed object failure and all work completed before it.
    fn read_many(
        &self,
        requests: &[ObjectReadRequest],
        budget: WorkBudget,
    ) -> ObjectResult<Vec<ObjectRead>>;

    /// Returns whether the exact typed object is present and verified.
    ///
    /// # Errors
    ///
    /// Returns a typed error when presence cannot be checked safely.
    fn contains(&self, object_id: ObjectId, budget: WorkBudget) -> ObjectResult<bool>;
}

impl<T: ObjectStore + ?Sized> ObjectStore for Arc<T> {
    fn put(&self, object_id: ObjectId, bytes: Bytes, budget: WorkBudget) -> ObjectResult<()> {
        (**self).put(object_id, bytes, budget)
    }

    fn put_many(&self, writes: &[ObjectWrite], budget: WorkBudget) -> ObjectResult<()> {
        (**self).put_many(writes, budget)
    }

    fn read(
        &self,
        object_id: ObjectId,
        maximum_bytes: u64,
        budget: WorkBudget,
    ) -> ObjectResult<ObjectRead> {
        (**self).read(object_id, maximum_bytes, budget)
    }

    fn read_many(
        &self,
        requests: &[ObjectReadRequest],
        budget: WorkBudget,
    ) -> ObjectResult<Vec<ObjectRead>> {
        (**self).read_many(requests, budget)
    }

    fn contains(&self, object_id: ObjectId, budget: WorkBudget) -> ObjectResult<bool> {
        (**self).contains(object_id, budget)
    }
}

/// Typed immutable-object failures.
#[derive(Debug, Error)]
pub enum ObjectStoreError {
    /// Operation was cancelled before immutable-object work began.
    #[error("object operation was cancelled")]
    Cancelled,
    /// Exact typed object is absent.
    #[error("object is missing")]
    Missing,
    /// Supplied bytes do not match the typed object digest.
    #[error("object digest does not match canonical bytes")]
    DigestMismatch,
    /// Object exceeds the backend's declared bound.
    #[error("object is {observed} bytes; maximum is {maximum}")]
    TooLarge {
        /// Observed canonical byte length.
        observed: u64,
        /// Configured maximum canonical byte length.
        maximum: u64,
    },
    /// Requested range is invalid or outside the object.
    #[error("object range is outside the admitted object")]
    InvalidRange,
    /// Stored bytes fail verification.
    #[error("stored object is corrupt")]
    Corrupt,
    /// Corrupt bytes could not be isolated from the live object namespace.
    #[error("corrupt object quarantine failed: {0}")]
    QuarantineFailed(#[source] std::io::Error),
    /// Storage I/O failed.
    #[error("object storage I/O failed: {0}")]
    Io(#[from] std::io::Error),
    /// Backend rejected an operation for a stable implementation-specific reason.
    #[error("object backend rejected operation: {0}")]
    Rejected(String),
    /// Backend work could not be admitted without exceeding the caller budget.
    #[error(transparent)]
    Work(#[from] crate::performance::WorkError),
}

#[cfg(test)]
#[path = "tests/storage.rs"]
mod tests;