Skip to main content

rvf_types/
segment_type.rs

1//! Segment type discriminator for the RVF format.
2
3/// Identifies the kind of data stored in a segment.
4///
5/// Values `0x00` and `0xF0..=0xFF` are reserved.
6#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
7#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
8#[repr(u8)]
9pub enum SegmentType {
10    /// Not a valid segment (uninitialized / zeroed region).
11    Invalid = 0x00,
12    /// Raw vector payloads (the actual embeddings).
13    Vec = 0x01,
14    /// HNSW adjacency lists, entry points, routing tables.
15    Index = 0x02,
16    /// Graph overlay deltas, partition updates, min-cut witnesses.
17    Overlay = 0x03,
18    /// Metadata mutations (label changes, deletions, moves).
19    Journal = 0x04,
20    /// Segment directory, hotset pointers, epoch state.
21    Manifest = 0x05,
22    /// Quantization dictionaries and codebooks.
23    Quant = 0x06,
24    /// Arbitrary key-value metadata (tags, provenance, lineage).
25    Meta = 0x07,
26    /// Temperature-promoted hot data (vectors + neighbors).
27    Hot = 0x08,
28    /// Access counter sketches for temperature decisions.
29    Sketch = 0x09,
30    /// Capability manifests, proof of computation, audit trails.
31    Witness = 0x0A,
32    /// Domain profile declarations (RVDNA, RVText, etc.).
33    Profile = 0x0B,
34    /// Key material, signature chains, certificate anchors.
35    Crypto = 0x0C,
36    /// Metadata inverted indexes for filtered search.
37    MetaIdx = 0x0D,
38    /// Embedded kernel / unikernel image for self-booting.
39    Kernel = 0x0E,
40    /// Embedded eBPF program for kernel fast path.
41    Ebpf = 0x0F,
42    /// Embedded WASM bytecode for self-bootstrapping execution.
43    ///
44    /// A WASM_SEG contains either a WASM microkernel (the RVF query engine
45    /// compiled to wasm32) or a minimal WASM interpreter that can execute
46    /// the microkernel. When both are present the file becomes fully
47    /// self-bootstrapping: any host with raw execution capability can run
48    /// the embedded interpreter, which in turn runs the microkernel, which
49    /// processes the RVF data segments.
50    Wasm = 0x10,
51    /// COW cluster mapping.
52    CowMap = 0x20,
53    /// Cluster reference counts.
54    Refcount = 0x21,
55    /// Vector membership filter.
56    Membership = 0x22,
57    /// Sparse delta patches.
58    Delta = 0x23,
59    /// Serialized transfer prior (cross-domain posterior summaries + cost EMAs).
60    TransferPrior = 0x30,
61    /// Policy kernel configuration and performance history.
62    PolicyKernel = 0x31,
63    /// Cost curve convergence data for acceleration tracking.
64    CostCurve = 0x32,
65}
66
67impl TryFrom<u8> for SegmentType {
68    type Error = u8;
69
70    fn try_from(value: u8) -> Result<Self, Self::Error> {
71        match value {
72            0x00 => Ok(Self::Invalid),
73            0x01 => Ok(Self::Vec),
74            0x02 => Ok(Self::Index),
75            0x03 => Ok(Self::Overlay),
76            0x04 => Ok(Self::Journal),
77            0x05 => Ok(Self::Manifest),
78            0x06 => Ok(Self::Quant),
79            0x07 => Ok(Self::Meta),
80            0x08 => Ok(Self::Hot),
81            0x09 => Ok(Self::Sketch),
82            0x0A => Ok(Self::Witness),
83            0x0B => Ok(Self::Profile),
84            0x0C => Ok(Self::Crypto),
85            0x0D => Ok(Self::MetaIdx),
86            0x0E => Ok(Self::Kernel),
87            0x0F => Ok(Self::Ebpf),
88            0x10 => Ok(Self::Wasm),
89            0x20 => Ok(Self::CowMap),
90            0x21 => Ok(Self::Refcount),
91            0x22 => Ok(Self::Membership),
92            0x23 => Ok(Self::Delta),
93            0x30 => Ok(Self::TransferPrior),
94            0x31 => Ok(Self::PolicyKernel),
95            0x32 => Ok(Self::CostCurve),
96            other => Err(other),
97        }
98    }
99}
100
101#[cfg(test)]
102mod tests {
103    use super::*;
104
105    #[test]
106    fn round_trip_all_variants() {
107        let variants = [
108            SegmentType::Invalid,
109            SegmentType::Vec,
110            SegmentType::Index,
111            SegmentType::Overlay,
112            SegmentType::Journal,
113            SegmentType::Manifest,
114            SegmentType::Quant,
115            SegmentType::Meta,
116            SegmentType::Hot,
117            SegmentType::Sketch,
118            SegmentType::Witness,
119            SegmentType::Profile,
120            SegmentType::Crypto,
121            SegmentType::MetaIdx,
122            SegmentType::Kernel,
123            SegmentType::Ebpf,
124            SegmentType::Wasm,
125            SegmentType::CowMap,
126            SegmentType::Refcount,
127            SegmentType::Membership,
128            SegmentType::Delta,
129            SegmentType::TransferPrior,
130            SegmentType::PolicyKernel,
131            SegmentType::CostCurve,
132        ];
133        for v in variants {
134            let raw = v as u8;
135            assert_eq!(SegmentType::try_from(raw), Ok(v));
136        }
137    }
138
139    #[test]
140    fn invalid_value_returns_err() {
141        assert_eq!(SegmentType::try_from(0x24), Err(0x24));
142        assert_eq!(SegmentType::try_from(0x33), Err(0x33));
143        assert_eq!(SegmentType::try_from(0xF0), Err(0xF0));
144        assert_eq!(SegmentType::try_from(0xFF), Err(0xFF));
145    }
146
147    #[test]
148    fn domain_expansion_discriminants() {
149        assert_eq!(SegmentType::TransferPrior as u8, 0x30);
150        assert_eq!(SegmentType::PolicyKernel as u8, 0x31);
151        assert_eq!(SegmentType::CostCurve as u8, 0x32);
152    }
153
154    #[test]
155    fn kernel_ebpf_wasm_discriminants() {
156        assert_eq!(SegmentType::Kernel as u8, 0x0E);
157        assert_eq!(SegmentType::Ebpf as u8, 0x0F);
158        assert_eq!(SegmentType::Wasm as u8, 0x10);
159    }
160}