valise 0.1.4

Single-file retrieval capsule for local-first AI: text and vector search over one portable file
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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

//! Generic append-only segment model, spec §9.

use crate::{
    error::{Error, Result},
    format::{Checksum, SegmentId, blake3_checksum},
};

pub const SEGMENT_MAGIC: [u8; 4] = *b"VLSG";
pub const SEGMENT_HEADER_SIZE: usize = 76;
pub const SEGMENT_VERSION: u16 = 1;

#[derive(
    Clone, Copy, Debug, Eq, PartialEq, Hash, serde::Deserialize, serde::Serialize, Ord, PartialOrd,
)]
#[repr(u16)]
pub enum SegmentType {
    Payload = 0x0001,
    FrameCatalog = 0x0002,
    CollectionCatalog = 0x0003,
    TextSpaceCatalog = 0x0004,
    AnalyzerCatalog = 0x0005,
    FieldSchemaCatalog = 0x0006,
    RetrievalProfileCatalog = 0x0007,
    EmbeddingSpaceCatalog = 0x0008,
    CodecCatalog = 0x0009,
    VectorCatalog = 0x000A,
    FusionProfileCatalog = 0x000C,
    TermDictionary = 0x0010,
    Postings = 0x0011,
    Positions = 0x0012,
    DocStats = 0x0013,
    TokenSet = 0x0014,
    MinHashSignature = 0x0015,
    ForwardIndex = 0x0016,
    RawText = 0x0017,
    RawTextDictionary = 0x0018,
    VectorData = 0x0020,
    CollectionFilter = 0x0022,
    CodecParams = 0x0023,
    /// Chronological `(frame_id, collection_id, created_at, updated_at)`
    /// index, columnar since v2.1. Backs `time_range_query`.
    ///
    /// Note the gaps either side: `0x000B` and `0x0021` were the
    /// `VoteProfileCatalog` and `VectorVoteIndex` segments, removed in
    /// v2.3 when vector search moved to the in-memory sign sketch. Those
    /// discriminants stay burned so an old file's segments can never be
    /// silently reinterpreted as something else.
    TimeIndex = 0x0030,
    SegmentRegistry = 0x00FE,
    Extension = 0x00FF,
    /// Batched per-frame metadata blobs (external identity key / URI,
    /// title, arbitrary metadata) referenced by `FrameDesc.uri_ref` /
    /// `title_ref` / `metadata_ref` via a `MetadataRef`. Written like
    /// `Payload`: one segment per flush batch, not one per frame.
    ///
    /// **Declared last on purpose.** `SegmentType` derives `serde` and is
    /// encoded by *declaration-order variant index* (e.g. inside the
    /// SegmentRegistry), so a new variant MUST be appended here — never
    /// inserted next to its `0x0019` discriminant — or every later
    /// variant's serde index shifts and existing files fail to decode.
    /// The explicit discriminant keeps the on-the-wire segment-header
    /// `u16` value at `0x0019` regardless of declaration position.
    Metadata = 0x0019,
}

impl TryFrom<u16> for SegmentType {
    type Error = Error;

    fn try_from(value: u16) -> Result<Self> {
        match value {
            0x0001 => Ok(Self::Payload),
            0x0002 => Ok(Self::FrameCatalog),
            0x0003 => Ok(Self::CollectionCatalog),
            0x0004 => Ok(Self::TextSpaceCatalog),
            0x0005 => Ok(Self::AnalyzerCatalog),
            0x0006 => Ok(Self::FieldSchemaCatalog),
            0x0007 => Ok(Self::RetrievalProfileCatalog),
            0x0008 => Ok(Self::EmbeddingSpaceCatalog),
            0x0009 => Ok(Self::CodecCatalog),
            0x000A => Ok(Self::VectorCatalog),
            0x000C => Ok(Self::FusionProfileCatalog),
            0x0010 => Ok(Self::TermDictionary),
            0x0011 => Ok(Self::Postings),
            0x0012 => Ok(Self::Positions),
            0x0013 => Ok(Self::DocStats),
            0x0014 => Ok(Self::TokenSet),
            0x0015 => Ok(Self::MinHashSignature),
            0x0016 => Ok(Self::ForwardIndex),
            0x0017 => Ok(Self::RawText),
            0x0018 => Ok(Self::RawTextDictionary),
            0x0019 => Ok(Self::Metadata),
            0x0020 => Ok(Self::VectorData),
            0x0022 => Ok(Self::CollectionFilter),
            0x0023 => Ok(Self::CodecParams),
            0x0030 => Ok(Self::TimeIndex),
            0x00FE => Ok(Self::SegmentRegistry),
            0x00FF => Ok(Self::Extension),
            _ => Err(Error::Format(format!("unknown segment type: {value:#06x}"))),
        }
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq, serde::Deserialize, serde::Serialize)]
#[repr(u16)]
pub enum Compression {
    None = 0,
    Zstd = 1,
    /// Reserved discriminant. **No encoder emits it and no decoder can
    /// read it** — see `Compression::ensure_decodable`. It exists so the
    /// wire value 2 stays claimed for a future LZ4 codec rather than being
    /// handed to something else.
    Lz4 = 2,
}

impl Compression {
    /// Reject a segment this build cannot actually decompress.
    ///
    /// The read paths are written as `if compression == Zstd { decompress }
    /// else { use the bytes as-is }`. Without this guard an `Lz4` segment
    /// would take the else branch and compressed bytes would be returned as
    /// though they were plaintext — silent corruption rather than an error.
    pub(crate) fn ensure_decodable(self) -> Result<()> {
        match self {
            Self::None | Self::Zstd => Ok(()),
            Self::Lz4 => Err(Error::Unsupported(
                "segment is LZ4-compressed; this build only decodes zstd (wire code 2 is reserved but unimplemented)"
                    .into(),
            )),
        }
    }
}

impl TryFrom<u16> for Compression {
    type Error = Error;

    fn try_from(value: u16) -> Result<Self> {
        match value {
            0 => Ok(Self::None),
            1 => Ok(Self::Zstd),
            2 => Ok(Self::Lz4),
            _ => Err(Error::Format(format!("unknown compression code: {value}"))),
        }
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq, serde::Deserialize, serde::Serialize)]
pub struct SegmentRef {
    pub segment_id: SegmentId,
    pub offset: u64,
    pub length: u64,
    pub checksum: Checksum,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SegmentHeader {
    pub segment_version: u16,
    pub segment_type: SegmentType,
    pub segment_id: SegmentId,
    pub flags: u32,
    pub item_count: u32,
    pub compression: Compression,
    pub uncompressed_length: u64,
    pub payload_length: u64,
    pub payload_checksum: Checksum,
}

impl SegmentHeader {
    #[must_use]
    pub fn new(
        segment_type: SegmentType,
        segment_id: SegmentId,
        item_count: u32,
        compression: Compression,
        uncompressed_length: u64,
        payload: &[u8],
    ) -> Self {
        Self {
            segment_version: SEGMENT_VERSION,
            segment_type,
            segment_id,
            flags: 0,
            item_count,
            compression,
            uncompressed_length,
            payload_length: payload.len() as u64,
            payload_checksum: blake3_checksum(payload),
        }
    }

    pub fn validate_payload(&self, payload: &[u8]) -> Result<()> {
        self.validate_header()?;
        if self.payload_length != payload.len() as u64 {
            return Err(Error::Integrity(format!(
                "segment payload length mismatch: expected {}, got {}",
                self.payload_length,
                payload.len()
            )));
        }
        let actual = blake3_checksum(payload);
        if self.payload_checksum != actual {
            return Err(Error::Integrity("segment payload checksum mismatch".into()));
        }
        Ok(())
    }

    pub fn validate_header(&self) -> Result<()> {
        if self.compression == Compression::None && self.uncompressed_length != self.payload_length
        {
            return Err(Error::Format(format!(
                "uncompressed length must equal payload length when compression is None: {} != {}",
                self.uncompressed_length, self.payload_length
            )));
        }
        Ok(())
    }
}

pub struct SegmentHeaderCodec;

impl SegmentHeaderCodec {
    pub fn encode(header: &SegmentHeader) -> [u8; SEGMENT_HEADER_SIZE] {
        let mut buf = [0u8; SEGMENT_HEADER_SIZE];
        buf[0..4].copy_from_slice(&SEGMENT_MAGIC);
        put_u16(&mut buf, 4, header.segment_version);
        put_u16(&mut buf, 6, header.segment_type as u16);
        put_u64(&mut buf, 8, header.segment_id.0);
        put_u32(&mut buf, 16, header.flags);
        put_u32(&mut buf, 20, header.item_count);
        put_u16(&mut buf, 24, header.compression as u16);
        put_u16(&mut buf, 26, 0);
        put_u64(&mut buf, 28, header.uncompressed_length);
        put_u64(&mut buf, 36, header.payload_length);
        buf[44..76].copy_from_slice(&header.payload_checksum);
        buf
    }

    pub fn decode(bytes: &[u8; SEGMENT_HEADER_SIZE]) -> Result<SegmentHeader> {
        if bytes[0..4] != SEGMENT_MAGIC {
            return Err(Error::Format("segment magic mismatch".into()));
        }
        let reserved = get_u16(bytes, 26)?;
        if reserved != 0 {
            return Err(Error::Format("segment reserved field must be zero".into()));
        }
        let version = get_u16(bytes, 4)?;
        if version != SEGMENT_VERSION {
            return Err(Error::Format(format!(
                "unsupported segment version: {version}"
            )));
        }
        let payload_checksum: Checksum = bytes[44..76]
            .try_into()
            .map_err(|_| Error::Format("segment checksum truncated".into()))?;

        let header = SegmentHeader {
            segment_version: version,
            segment_type: SegmentType::try_from(get_u16(bytes, 6)?)?,
            segment_id: SegmentId(get_u64(bytes, 8)?),
            flags: get_u32(bytes, 16)?,
            item_count: get_u32(bytes, 20)?,
            compression: Compression::try_from(get_u16(bytes, 24)?)?,
            uncompressed_length: get_u64(bytes, 28)?,
            payload_length: get_u64(bytes, 36)?,
            payload_checksum,
        };
        header.validate_header()?;
        Ok(header)
    }
}

fn put_u16(buf: &mut [u8], offset: usize, value: u16) {
    buf[offset..offset + 2].copy_from_slice(&value.to_le_bytes());
}

fn put_u32(buf: &mut [u8], offset: usize, value: u32) {
    buf[offset..offset + 4].copy_from_slice(&value.to_le_bytes());
}

fn put_u64(buf: &mut [u8], offset: usize, value: u64) {
    buf[offset..offset + 8].copy_from_slice(&value.to_le_bytes());
}

fn get_u16(bytes: &[u8], offset: usize) -> Result<u16> {
    Ok(u16::from_le_bytes(
        bytes[offset..offset + 2]
            .try_into()
            .map_err(|_| Error::Format("segment u16 field truncated".into()))?,
    ))
}

fn get_u32(bytes: &[u8], offset: usize) -> Result<u32> {
    Ok(u32::from_le_bytes(
        bytes[offset..offset + 4]
            .try_into()
            .map_err(|_| Error::Format("segment u32 field truncated".into()))?,
    ))
}

fn get_u64(bytes: &[u8], offset: usize) -> Result<u64> {
    Ok(u64::from_le_bytes(
        bytes[offset..offset + 8]
            .try_into()
            .map_err(|_| Error::Format("segment u64 field truncated".into()))?,
    ))
}

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

    #[test]
    fn segment_header_roundtrips() {
        let payload = b"payload bytes";
        let header = SegmentHeader::new(
            SegmentType::Payload,
            SegmentId(42),
            1,
            Compression::None,
            payload.len() as u64,
            payload,
        );

        let encoded = SegmentHeaderCodec::encode(&header);
        let decoded = SegmentHeaderCodec::decode(&encoded).expect("decode segment header");

        assert_eq!(decoded, header);
        decoded
            .validate_payload(payload)
            .expect("checksum should validate");
    }

    #[test]
    fn rejects_bad_payload_checksum() {
        let payload = b"payload bytes";
        let header = SegmentHeader::new(
            SegmentType::Payload,
            SegmentId(42),
            1,
            Compression::None,
            payload.len() as u64,
            payload,
        );

        let err = header
            .validate_payload(b"other bytes")
            .expect_err("checksum should fail");
        assert!(err.to_string().contains("length") || err.to_string().contains("checksum"));
    }

    #[test]
    fn rejects_unknown_segment_type() {
        let payload = b"payload bytes";
        let header = SegmentHeader::new(
            SegmentType::Payload,
            SegmentId(42),
            1,
            Compression::None,
            payload.len() as u64,
            payload,
        );
        let mut encoded = SegmentHeaderCodec::encode(&header);
        encoded[6..8].copy_from_slice(&0xBEEFu16.to_le_bytes());

        let err = SegmentHeaderCodec::decode(&encoded).expect_err("unknown type should fail");
        assert!(err.to_string().contains("unknown segment type"));
    }

    #[test]
    fn segment_type_discriminants_match_spec() {
        assert_eq!(SegmentType::Payload as u16, 0x0001);
        assert_eq!(SegmentType::FrameCatalog as u16, 0x0002);
        assert_eq!(SegmentType::CollectionCatalog as u16, 0x0003);
        assert_eq!(SegmentType::TextSpaceCatalog as u16, 0x0004);
        assert_eq!(SegmentType::AnalyzerCatalog as u16, 0x0005);
        assert_eq!(SegmentType::FieldSchemaCatalog as u16, 0x0006);
        assert_eq!(SegmentType::RetrievalProfileCatalog as u16, 0x0007);
        assert_eq!(SegmentType::EmbeddingSpaceCatalog as u16, 0x0008);
        assert_eq!(SegmentType::CodecCatalog as u16, 0x0009);
        assert_eq!(SegmentType::VectorCatalog as u16, 0x000A);
        assert_eq!(SegmentType::TermDictionary as u16, 0x0010);
        assert_eq!(SegmentType::Postings as u16, 0x0011);
        assert_eq!(SegmentType::Positions as u16, 0x0012);
        assert_eq!(SegmentType::DocStats as u16, 0x0013);
        assert_eq!(SegmentType::TokenSet as u16, 0x0014);
        assert_eq!(SegmentType::MinHashSignature as u16, 0x0015);
        assert_eq!(SegmentType::ForwardIndex as u16, 0x0016);
        assert_eq!(SegmentType::RawText as u16, 0x0017);
        assert_eq!(SegmentType::RawTextDictionary as u16, 0x0018);
        assert_eq!(SegmentType::Metadata as u16, 0x0019);
        assert_eq!(SegmentType::VectorData as u16, 0x0020);
        assert_eq!(SegmentType::CollectionFilter as u16, 0x0022);
        assert_eq!(SegmentType::CodecParams as u16, 0x0023);
        assert_eq!(SegmentType::TimeIndex as u16, 0x0030);
        assert_eq!(SegmentType::SegmentRegistry as u16, 0x00FE);
        assert_eq!(SegmentType::Extension as u16, 0x00FF);
    }

    #[test]
    fn all_segment_types_roundtrip() {
        let all = [
            SegmentType::Payload,
            SegmentType::FrameCatalog,
            SegmentType::CollectionCatalog,
            SegmentType::TextSpaceCatalog,
            SegmentType::AnalyzerCatalog,
            SegmentType::FieldSchemaCatalog,
            SegmentType::RetrievalProfileCatalog,
            SegmentType::EmbeddingSpaceCatalog,
            SegmentType::CodecCatalog,
            SegmentType::VectorCatalog,
            SegmentType::TermDictionary,
            SegmentType::Postings,
            SegmentType::Positions,
            SegmentType::DocStats,
            SegmentType::TokenSet,
            SegmentType::MinHashSignature,
            SegmentType::ForwardIndex,
            SegmentType::RawText,
            SegmentType::RawTextDictionary,
            SegmentType::Metadata,
            SegmentType::VectorData,
            SegmentType::CollectionFilter,
            SegmentType::CodecParams,
            SegmentType::TimeIndex,
            SegmentType::SegmentRegistry,
            SegmentType::Extension,
        ];
        let payload = b"p";
        for ty in all {
            let header = SegmentHeader::new(
                ty,
                SegmentId(7),
                0,
                Compression::None,
                payload.len() as u64,
                payload,
            );
            let encoded = SegmentHeaderCodec::encode(&header);
            let decoded = SegmentHeaderCodec::decode(&encoded).expect("decode");
            assert_eq!(decoded.segment_type, ty);
        }
    }

    #[test]
    fn rejects_invalid_none_compression_lengths() {
        let payload = b"payload bytes";
        let header = SegmentHeader {
            uncompressed_length: payload.len() as u64 + 1,
            ..SegmentHeader::new(
                SegmentType::Payload,
                SegmentId(42),
                1,
                Compression::None,
                payload.len() as u64,
                payload,
            )
        };
        let encoded = SegmentHeaderCodec::encode(&header);

        let err = SegmentHeaderCodec::decode(&encoded).expect_err("invalid length should fail");
        assert!(err.to_string().contains("uncompressed length"));
    }
}