aien-sovereign-core-mv
AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.
Brought by drakestapleton
aien-sovereign-core-mv / crates / aien-kv-store / tests / record_tests.rs
| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | mod common; |
| 17 | |
| 18 | use aien_kv_store::{ |
| 19 | payload_root, segment_count, KvError, RecordHeader, HEADER_LEN, HEADER_REGION_LEN, MAGIC, |
| 20 | MAX_SEGMENTS, PAYLOAD_OFFSET, |
| 21 | }; |
| 22 | use common::*; |
| 23 | use sha2::{Digest, Sha256}; |
| 24 | |
| 25 | fn record() -> RecordHeader { |
| 26 | RecordHeader { |
| 27 | chunk: sample_chunk_header(), |
| 28 | payload_sha256: [0x5A; 32], |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | fn u32_at(b: &[u8], o: usize) -> u32 { |
| 33 | u32::from_le_bytes(b[o..o + 4].try_into().unwrap()) |
| 34 | } |
| 35 | |
| 36 | fn u64_at(b: &[u8], o: usize) -> u64 { |
| 37 | u64::from_le_bytes(b[o..o + 8].try_into().unwrap()) |
| 38 | } |
| 39 | |
| 40 | #[test] |
| 41 | fn header_roundtrip_256_bytes() { |
| 42 | let r = record(); |
| 43 | let bytes = r.encode(); |
| 44 | assert_eq!(bytes.len(), 256); |
| 45 | assert_eq!(HEADER_LEN, 256); |
| 46 | let back = RecordHeader::decode_struct(&bytes).unwrap(); |
| 47 | assert_eq!(back, r); |
| 48 | // Re-encoding is byte for byte identical. |
| 49 | assert_eq!(back.encode(), bytes); |
| 50 | // Extra trailing bytes do not matter to the struct decode. |
| 51 | let mut with_tail = bytes.to_vec(); |
| 52 | with_tail.extend_from_slice(&[1, 2, 3]); |
| 53 | assert_eq!(RecordHeader::decode_struct(&with_tail).unwrap(), r); |
| 54 | } |
| 55 | |
| 56 | /// Every field sits at the offset ADR 0003 section 2.1 gives. |
| 57 | #[test] |
| 58 | fn header_fields_sit_at_adr_offsets() { |
| 59 | let r = record(); |
| 60 | let c = r.chunk; |
| 61 | let b = r.encode(); |
| 62 | assert_eq!(&b[0..8], &MAGIC); |
| 63 | assert_eq!(&b[0..8], b"AIENKVC1"); |
| 64 | assert_eq!(u16::from_le_bytes([b[8], b[9]]), 1); // format_version |
| 65 | assert_eq!(u16::from_le_bytes([b[10], b[11]]), 1); // record_kind |
| 66 | assert_eq!(u32_at(&b, 12), 256); // header_len |
| 67 | assert_eq!(&b[16..48], &c.namespace_id.0); |
| 68 | assert_eq!(&b[48..80], c.chunk_key.as_bytes()); |
| 69 | assert_eq!(&b[80..112], &c.parent_key); |
| 70 | assert_eq!(&b[112..144], &c.model_sha256); |
| 71 | assert_eq!(u32_at(&b, 144), c.first_position); |
| 72 | assert_eq!(u32_at(&b, 148), c.token_count); |
| 73 | assert_eq!(u32_at(&b, 152), c.chunk_tokens); |
| 74 | assert_eq!(u32_at(&b, 156), c.block_size); |
| 75 | assert_eq!(u32_at(&b, 160), c.num_layers); |
| 76 | assert_eq!(u32_at(&b, 164), c.num_kv_heads); |
| 77 | assert_eq!(u32_at(&b, 168), c.head_dim); |
| 78 | assert_eq!(b[172], c.dtype); |
| 79 | assert_eq!(b[173], c.backend_format); |
| 80 | assert_eq!(u16::from_le_bytes([b[174], b[175]]), c.flags); |
| 81 | assert_eq!(u64_at(&b, 176), c.payload_len); |
| 82 | assert_eq!(&b[184..216], &r.payload_sha256); |
| 83 | assert_eq!(u64_at(&b, 216), c.written_unix_ns); |
| 84 | // header_sha256 covers bytes 0..224. |
| 85 | let digest = Sha256::digest(&b[0..224]); |
| 86 | assert_eq!(&b[224..256], digest.as_slice()); |
| 87 | } |
| 88 | |
| 89 | #[test] |
| 90 | fn decode_refuses_short_input_and_structural_damage() { |
| 91 | let (rec, digests) = sample_record(); |
| 92 | let region = rec.encode_region(&digests).unwrap(); |
| 93 | // The region decode needs the whole 4096 bytes; the struct decode needs 256. |
| 94 | assert_eq!( |
| 95 | RecordHeader::decode(®ion[..4095]), |
| 96 | Err(KvError::Truncated) |
| 97 | ); |
| 98 | assert_eq!( |
| 99 | RecordHeader::decode(®ion[..256]), |
| 100 | Err(KvError::Truncated) |
| 101 | ); |
| 102 | assert_eq!(RecordHeader::decode(&[]), Err(KvError::Truncated)); |
| 103 | assert_eq!( |
| 104 | RecordHeader::decode_struct(®ion[..255]), |
| 105 | Err(KvError::Truncated) |
| 106 | ); |
| 107 | let mut bad = region; |
| 108 | bad[3] ^= 0xFF; |
| 109 | assert_eq!(RecordHeader::decode(&bad), Err(KvError::BadMagic)); |
| 110 | let mut bad = region; |
| 111 | bad[100] ^= 1; // inside the hashed prefix, magic untouched |
| 112 | assert_eq!(RecordHeader::decode(&bad), Err(KvError::HeaderChecksum)); |
| 113 | let mut bad = region; |
| 114 | bad[240] ^= 1; // inside the stored hash itself |
| 115 | assert_eq!(RecordHeader::decode(&bad), Err(KvError::HeaderChecksum)); |
| 116 | } |
| 117 | |
| 118 | #[test] |
| 119 | fn header_region_roundtrip_4096() { |
| 120 | let (rec, digests) = sample_record(); |
| 121 | assert_eq!(digests.len(), 16); |
| 122 | assert_eq!(HEADER_REGION_LEN, 4096); |
| 123 | assert_eq!(PAYLOAD_OFFSET, 4096); |
| 124 | assert_eq!(MAX_SEGMENTS, 119); |
| 125 | let region = rec.encode_region(&digests).unwrap(); |
| 126 | assert_eq!(region.len(), 4096); |
| 127 | // Struct first, then the digest table, then zeros to the end. |
| 128 | assert_eq!(®ion[..256], &rec.encode()); |
| 129 | for (i, d) in digests.iter().enumerate() { |
| 130 | assert_eq!(®ion[256 + 32 * i..256 + 32 * (i + 1)], d, "digest {i}"); |
| 131 | } |
| 132 | assert!(region[256 + 32 * 16..].iter().all(|b| *b == 0)); |
| 133 | // Decode reads the struct back; the root in the struct is the root of the table. |
| 134 | let back = RecordHeader::decode(®ion).unwrap(); |
| 135 | assert_eq!(back, rec); |
| 136 | assert_eq!(back.payload_sha256, payload_root(&digests)); |
| 137 | // Re-encoding is byte for byte identical. |
| 138 | assert_eq!(back.encode_region(&digests).unwrap(), region); |
| 139 | |
| 140 | // The table can fill the region up to the seal slot: 119 digests, no |
| 141 | // padding left, the 32-byte slot zero until sealed (ADR 0003 section 20). |
| 142 | let full = seg_digests("full", 119); |
| 143 | let region = rec.encode_region(&full).unwrap(); |
| 144 | assert_eq!(®ion[256 + 32 * 118..4064], full[118].as_slice()); |
| 145 | assert!(region[4064..].iter().all(|b| *b == 0)); |
| 146 | // 120 digests do not fit. |
| 147 | let over = seg_digests("over", 120); |
| 148 | assert_eq!(rec.encode_region(&over), Err(KvError::TooManySegments)); |
| 149 | } |
| 150 | |
| 151 | #[test] |
| 152 | fn payload_root_is_domain_separated_and_order_dependent() { |
| 153 | let d = seg_digests("root", 3); |
| 154 | let mut h = Sha256::new(); |
| 155 | h.update(b"AIEN-PKV-PAYLOAD-V1\0"); |
| 156 | for x in &d { |
| 157 | h.update(x); |
| 158 | } |
| 159 | let want: [u8; 32] = h.finalize().into(); |
| 160 | assert_eq!(payload_root(&d), want); |
| 161 | let mut swapped = d.clone(); |
| 162 | swapped.swap(0, 1); |
| 163 | assert_ne!(payload_root(&swapped), want); |
| 164 | assert_ne!(payload_root(&d[..2]), want); |
| 165 | // Not the bare hash of the concatenation. |
| 166 | let mut bare = Sha256::new(); |
| 167 | for x in &d { |
| 168 | bare.update(x); |
| 169 | } |
| 170 | let bare: [u8; 32] = bare.finalize().into(); |
| 171 | assert_ne!(bare, want); |
| 172 | } |
| 173 | |
| 174 | #[test] |
| 175 | fn segment_count_is_ceil_and_bounded() { |
| 176 | assert_eq!(segment_count(524_288, 524_288), Ok(1)); |
| 177 | assert_eq!(segment_count(524_289, 524_288), Ok(2)); |
| 178 | assert_eq!(segment_count(119 * 4096, 4096), Ok(119)); |
| 179 | assert_eq!( |
| 180 | segment_count(119 * 4096 + 1, 4096), |
| 181 | Err(KvError::TooManySegments) |
| 182 | ); |
| 183 | assert_eq!(segment_count(4096, 0), Err(KvError::InvalidGeometry)); |
| 184 | assert_eq!(segment_count(0, 4096), Err(KvError::InvalidGeometry)); |
| 185 | } |