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 / src / error.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 | //! Error vocabulary for identity, codec and validation. Every variant is a |
| 17 | //! plain value (no allocation to build or report one). |
| 18 | |
| 19 | use aien_platform::PlatformError; |
| 20 | |
| 21 | /// Why a key, header or record was refused. Variants are listed in the order |
| 22 | /// `validate_record` checks them (ADR 0003 section 1.4). |
| 23 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 24 | pub enum KvError { |
| 25 | /// Fewer than 4096 bytes were supplied for the header region (256 for |
| 26 | /// `RecordHeader::decode_struct`). |
| 27 | Truncated, |
| 28 | BadMagic, |
| 29 | UnsupportedVersion, |
| 30 | BadRecordKind, |
| 31 | BadHeaderLen, |
| 32 | /// `header_sha256` does not match bytes 0..224. |
| 33 | HeaderChecksum, |
| 34 | /// A reserved flag bit is set, or `partial_tail` disagrees with `token_count`. |
| 35 | BadFlags, |
| 36 | /// A nonzero byte in the header region outside the 256-byte struct and the |
| 37 | /// segment digest table (ADR 0003 section 2.1, H7). |
| 38 | BadPadding, |
| 39 | /// The root recomputed from the segment digest table differs from |
| 40 | /// `payload_sha256` (offset 184). |
| 41 | SegmentRootMismatch, |
| 42 | /// More than `MAX_SEGMENTS` (119) segments: the table would not fit the region |
| 43 | /// before the seal slot (ADR 0003 section 20). |
| 44 | TooManySegments, |
| 45 | NamespaceMismatch, |
| 46 | KeyMismatch, |
| 47 | ParentMismatch, |
| 48 | PositionMismatch, |
| 49 | /// `token_count` is 0 or larger than `chunk_tokens`. |
| 50 | TokenCountOutOfRange, |
| 51 | TokenCountMismatch, |
| 52 | /// Model digest, chunk size, block size, layers, heads, head dim, dtype or backend differ. |
| 53 | GeometryMismatch, |
| 54 | PayloadLenMismatch, |
| 55 | /// File size is not `4096 + payload_len`. |
| 56 | FileSizeMismatch, |
| 57 | // Construction errors (identity), not validation outcomes. |
| 58 | /// A length-prefixed identity field is longer than its prefix can express or its cap. |
| 59 | FieldTooLong(&'static str), |
| 60 | /// A required identity field is empty (ADR 1.2.2: an empty engine build id or |
| 61 | /// rounding class would let every engine alias). |
| 62 | FieldEmpty(&'static str), |
| 63 | /// Zero sizes, or `chunk_tokens` not a multiple of `block_size`. |
| 64 | InvalidGeometry, |
| 65 | /// A count, position or length does not fit its on-disk integer width. |
| 66 | Overflow, |
| 67 | } |
| 68 | |
| 69 | impl core::fmt::Display for KvError { |
| 70 | fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 71 | match self { |
| 72 | Self::FieldTooLong(name) => write!(f, "kv identity field {name} is too long"), |
| 73 | Self::FieldEmpty(name) => write!(f, "kv identity field {name} is empty"), |
| 74 | other => write!(f, "kv record refused: {other:?}"), |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | #[cfg(feature = "std")] |
| 80 | impl std::error::Error for KvError {} |
| 81 | |
| 82 | impl From<KvError> for PlatformError { |
| 83 | /// Every validation failure is an `InvalidLayout` at the platform boundary |
| 84 | /// (ADR 0003 section 3.1). |
| 85 | fn from(_: KvError) -> Self { |
| 86 | PlatformError::InvalidLayout |
| 87 | } |
| 88 | } |