heddle-object-model 0.15.0

Heddle's content-addressed object model and stable codecs.
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
// SPDX-License-Identifier: Apache-2.0
//! Canonical pack-extent claims — the physical half of the reshape's read path.
//!
//! A manifest node names only immutable logical facts. Where an object's bytes
//! actually live is a *mutable* control-plane fact, so it never enters the
//! content root; repacking must change a read envelope, not a manifest hash.
//! This module gives that envelope a canonical encoding of its own so it can be
//! signed, transported, and checked byte-for-byte.
//!
//! Two rules from the downstream consumer are load-bearing and reproduced here
//! exactly:
//!
//! * **Offset-canonical ordering.** Records are ordered by pack offset, not by
//!   object key. Packs are laid out for delta compression, so object order and
//!   physical order routinely disagree; ordering by object first makes valid
//!   coalesced ranges fail their contiguity check (weft #1070, the bug fixed
//!   after weft #1069 merged).
//! * **Gap-free coverage.** A coalesced range carries a sorted partition of its
//!   authorized records that covers `[start, end)` exactly — no gap, no
//!   overlap. A pack may hold objects of mixed audience, so one physical range
//!   read must never authorize an unselected byte gap between two authorized
//!   records. Coalescing joins *exactly adjacent* extents only.
//!
//! ```text
//! claim: "WPMX" | u8(version=1) | u16(pack_id_len) | pack_id
//!               | u16(etag_len) | etag | u64(start) | u64(end)
//!               | u32(record_count)
//!               | count * ( u8(kind) | [u8;32](object_hash)
//!                         | u64(decoded_size) | u64(offset) | u64(length)
//!                         | [u8;32](encoded_digest) )
//! ```

use crate::object::{
    ContentHash,
    manifest::node::{ManifestKey, ManifestObject, ManifestObjectKind},
};

/// Magic prefix on every canonical pack-range claim.
pub const PACK_CLAIM_MAGIC: [u8; 4] = *b"WPMX";
/// The only claim format version this binary reads or writes.
pub const PACK_CLAIM_VERSION: u8 = 1;

/// One object's physical slice of a pack.
///
/// `encoded_digest` is the BLAKE3 of the *encoded record bytes* — the bytes as
/// they sit in the pack, before decompression or delta resolution. It lets a
/// receiver validate every record independently instead of trusting the whole
/// range.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct PackRecord {
    pub object: ManifestObject,
    /// Byte offset of the encoded record within its pack.
    pub offset: u64,
    /// Encoded length in bytes. Zero-length records are not representable in a
    /// gap-free partition and are rejected by fsck.
    pub length: u64,
    /// `BLAKE3(encoded record bytes)`.
    pub encoded_digest: ContentHash,
}

impl PackRecord {
    pub fn new(
        object: ManifestObject,
        offset: u64,
        length: u64,
        encoded_digest: ContentHash,
    ) -> Self {
        Self {
            object,
            offset,
            length,
            encoded_digest,
        }
    }

    /// Exclusive end offset, or `None` on `u64` overflow.
    pub fn end(&self) -> Option<u64> {
        self.offset.checked_add(self.length)
    }

    pub fn key(&self) -> ManifestKey {
        self.object.key()
    }
}

/// One coalesced physical range read, plus the partition of authorized records
/// it covers.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PackRangeClaim {
    pub pack_id: String,
    /// Provider entity tag pinning the pack revision this claim was resolved
    /// against. A repack changes the ETag, invalidating the claim without
    /// touching any manifest.
    pub etag: String,
    pub start: u64,
    pub end: u64,
    records: Vec<PackRecord>,
}

impl PackRangeClaim {
    /// Build a claim over `records`, sorting them into offset-canonical order.
    ///
    /// Sorting here is deliberate and is the whole point of weft #1070: the
    /// canonical order is physical, so a pack whose object order differs from
    /// its offset order still produces a contiguous, checkable partition.
    pub fn new(
        pack_id: impl Into<String>,
        etag: impl Into<String>,
        start: u64,
        end: u64,
        mut records: Vec<PackRecord>,
    ) -> Self {
        records.sort_by_key(|record| (record.offset, record.length, record.key()));
        Self {
            pack_id: pack_id.into(),
            etag: etag.into(),
            start,
            end,
            records,
        }
    }

    /// Records in offset-canonical order.
    pub fn records(&self) -> &[PackRecord] {
        &self.records
    }

    /// Total bytes this claim authorizes, or `None` if the range is inverted.
    pub fn byte_len(&self) -> Option<u64> {
        self.end.checked_sub(self.start)
    }

    /// Encode to the single canonical byte string for this claim.
    pub fn encode(&self) -> Vec<u8> {
        let mut out = Vec::with_capacity(
            4 + 1
                + 2
                + self.pack_id.len()
                + 2
                + self.etag.len()
                + 8
                + 8
                + 4
                + self.records.len() * 89,
        );
        out.extend_from_slice(&PACK_CLAIM_MAGIC);
        out.push(PACK_CLAIM_VERSION);
        push_short_string(&mut out, &self.pack_id);
        push_short_string(&mut out, &self.etag);
        out.extend_from_slice(&self.start.to_be_bytes());
        out.extend_from_slice(&self.end.to_be_bytes());
        out.extend_from_slice(&(self.records.len() as u32).to_be_bytes());
        for record in &self.records {
            out.push(record.object.kind.to_byte());
            out.extend_from_slice(record.object.hash.as_bytes());
            out.extend_from_slice(&record.object.decoded_size.to_be_bytes());
            out.extend_from_slice(&record.offset.to_be_bytes());
            out.extend_from_slice(&record.length.to_be_bytes());
            out.extend_from_slice(record.encoded_digest.as_bytes());
        }
        out
    }

    /// The claim's content address — `BLAKE3` of its canonical bytes.
    pub fn address(&self) -> ContentHash {
        ContentHash::compute(&self.encode())
    }

    /// Decode strictly, rejecting truncation, trailing bytes, non-UTF-8 ids,
    /// out-of-order records, and any non-canonical spelling.
    pub fn decode(bytes: &[u8]) -> Result<Self, PackClaimDecodeError> {
        let claim = Self::decode_inner(bytes)?;
        if claim.encode() != bytes {
            return Err(PackClaimDecodeError::NonCanonicalEncoding);
        }
        Ok(claim)
    }

    fn decode_inner(bytes: &[u8]) -> Result<Self, PackClaimDecodeError> {
        let mut reader = ClaimReader { bytes, pos: 0 };

        if reader.take(4)? != PACK_CLAIM_MAGIC {
            return Err(PackClaimDecodeError::BadMagic);
        }
        let version = reader.u8()?;
        if version != PACK_CLAIM_VERSION {
            return Err(PackClaimDecodeError::UnsupportedVersion(version));
        }

        let pack_id = reader.short_string()?;
        let etag = reader.short_string()?;
        let start = reader.u64()?;
        let end = reader.u64()?;
        let count = reader.u32()?;

        let mut records = Vec::with_capacity((count as usize).min(4096));
        let mut previous: Option<(u64, u64, ManifestKey)> = None;
        for _ in 0..count {
            let kind_byte = reader.u8()?;
            let kind = ManifestObjectKind::from_byte(kind_byte)
                .ok_or(PackClaimDecodeError::UnknownObjectKind(kind_byte))?;
            let hash = ContentHash::from_bytes(reader.hash()?);
            let decoded_size = reader.u64()?;
            let offset = reader.u64()?;
            let length = reader.u64()?;
            let encoded_digest = ContentHash::from_bytes(reader.hash()?);
            let object = ManifestObject::new(kind, hash, decoded_size);
            let order = (offset, length, object.key());
            if let Some(prev) = previous
                && prev >= order
            {
                return Err(PackClaimDecodeError::RecordsOutOfOffsetOrder);
            }
            previous = Some(order);
            records.push(PackRecord::new(object, offset, length, encoded_digest));
        }
        if reader.pos != bytes.len() {
            return Err(PackClaimDecodeError::TrailingBytes);
        }

        Ok(Self {
            pack_id,
            etag,
            start,
            end,
            records,
        })
    }
}

struct ClaimReader<'a> {
    bytes: &'a [u8],
    pos: usize,
}

impl<'a> ClaimReader<'a> {
    fn take(&mut self, len: usize) -> Result<&'a [u8], PackClaimDecodeError> {
        let end = self
            .pos
            .checked_add(len)
            .ok_or(PackClaimDecodeError::Truncated)?;
        let slice = self
            .bytes
            .get(self.pos..end)
            .ok_or(PackClaimDecodeError::Truncated)?;
        self.pos = end;
        Ok(slice)
    }

    fn u8(&mut self) -> Result<u8, PackClaimDecodeError> {
        Ok(self.take(1)?[0])
    }

    fn u16(&mut self) -> Result<u16, PackClaimDecodeError> {
        let bytes = self.take(2)?;
        Ok(u16::from_be_bytes([bytes[0], bytes[1]]))
    }

    fn u32(&mut self) -> Result<u32, PackClaimDecodeError> {
        let bytes = self.take(4)?;
        Ok(u32::from_be_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]))
    }

    fn u64(&mut self) -> Result<u64, PackClaimDecodeError> {
        let bytes = self.take(8)?;
        let mut arr = [0u8; 8];
        arr.copy_from_slice(bytes);
        Ok(u64::from_be_bytes(arr))
    }

    fn hash(&mut self) -> Result<[u8; 32], PackClaimDecodeError> {
        let bytes = self.take(32)?;
        let mut arr = [0u8; 32];
        arr.copy_from_slice(bytes);
        Ok(arr)
    }

    fn short_string(&mut self) -> Result<String, PackClaimDecodeError> {
        let len = usize::from(self.u16()?);
        std::str::from_utf8(self.take(len)?)
            .map(str::to_string)
            .map_err(|_| PackClaimDecodeError::InvalidUtf8)
    }
}

fn push_short_string(out: &mut Vec<u8>, value: &str) {
    out.extend_from_slice(&(value.len() as u16).to_be_bytes());
    out.extend_from_slice(value.as_bytes());
}

#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
pub enum PackClaimDecodeError {
    #[error("claim does not start with the WPMX magic")]
    BadMagic,
    #[error("unsupported pack claim version {0}")]
    UnsupportedVersion(u8),
    #[error("unknown manifest object kind {0}")]
    UnknownObjectKind(u8),
    #[error("claim bytes are truncated")]
    Truncated,
    #[error("claim has trailing bytes after its declared content")]
    TrailingBytes,
    #[error("pack id or etag is not valid UTF-8")]
    InvalidUtf8,
    #[error("records are not strictly ascending by pack offset")]
    RecordsOutOfOffsetOrder,
    #[error("claim bytes are a non-canonical spelling of their own content")]
    NonCanonicalEncoding,
}

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

    fn record(offset: u64, length: u64, seed: u8) -> PackRecord {
        PackRecord::new(
            ManifestObject::new(
                ManifestObjectKind::Blob,
                ContentHash::from_bytes([seed; 32]),
                length * 2,
            ),
            offset,
            length,
            ContentHash::from_bytes([seed.wrapping_add(100); 32]),
        )
    }

    #[test]
    fn records_canonicalize_by_offset_not_by_object_key() {
        // Object key order (0x01 < 0x02 < 0x03) is the exact reverse of the
        // physical offset order here — the weft #1070 shape.
        let claim = PackRangeClaim::new(
            "pack-a",
            "etag-1",
            0,
            30,
            vec![record(20, 10, 1), record(0, 10, 3), record(10, 10, 2)],
        );
        let offsets: Vec<u64> = claim.records().iter().map(|r| r.offset).collect();
        assert_eq!(offsets, vec![0, 10, 20]);
        let seeds: Vec<u8> = claim
            .records()
            .iter()
            .map(|r| r.object.hash.as_bytes()[0])
            .collect();
        assert_eq!(
            seeds,
            vec![3, 2, 1],
            "object order must not drive the layout"
        );
    }

    #[test]
    fn claim_round_trips_and_is_hash_stable() {
        let claim = PackRangeClaim::new(
            "pack-a",
            "\"etag-xyz\"",
            100,
            140,
            vec![record(120, 20, 2), record(100, 20, 1)],
        );
        let encoded = claim.encode();
        let decoded = PackRangeClaim::decode(&encoded).unwrap();
        assert_eq!(decoded, claim);
        assert_eq!(decoded.encode(), encoded);
        assert_eq!(decoded.address(), claim.address());
    }

    #[test]
    fn decode_rejects_trailing_and_truncated_bytes() {
        let claim = PackRangeClaim::new("p", "e", 0, 10, vec![record(0, 10, 1)]);
        let encoded = claim.encode();

        let mut trailing = encoded.clone();
        trailing.push(0);
        assert_eq!(
            PackRangeClaim::decode(&trailing).unwrap_err(),
            PackClaimDecodeError::TrailingBytes
        );
        assert_eq!(
            PackRangeClaim::decode(&encoded[..encoded.len() - 1]).unwrap_err(),
            PackClaimDecodeError::Truncated
        );
    }

    #[test]
    fn decode_rejects_records_written_out_of_offset_order() {
        let claim = PackRangeClaim::new("p", "e", 0, 20, vec![record(0, 10, 1), record(10, 10, 2)]);
        let encoded = claim.encode();
        // Swap the two fixed-width record bodies.
        let header = encoded.len() - 2 * 89;
        let mut swapped = encoded.clone();
        swapped[header..header + 89].copy_from_slice(&encoded[header + 89..]);
        swapped[header + 89..].copy_from_slice(&encoded[header..header + 89]);
        assert_eq!(
            PackRangeClaim::decode(&swapped).unwrap_err(),
            PackClaimDecodeError::RecordsOutOfOffsetOrder
        );
    }

    #[test]
    fn decode_rejects_a_bad_magic_and_version() {
        let encoded = PackRangeClaim::new("p", "e", 0, 10, vec![record(0, 10, 1)]).encode();

        let mut bad_magic = encoded.clone();
        bad_magic[0] = b'Z';
        assert_eq!(
            PackRangeClaim::decode(&bad_magic).unwrap_err(),
            PackClaimDecodeError::BadMagic
        );

        let mut bad_version = encoded;
        bad_version[4] = 9;
        assert_eq!(
            PackRangeClaim::decode(&bad_version).unwrap_err(),
            PackClaimDecodeError::UnsupportedVersion(9)
        );
    }
}