Files
vetta-infer/crates/vetta-infer-abi/src/vtc1.rs
T
adm 7b50f1c07c [ABI][KN][LD][CC][RT][LAB][SP][NT][TM][GW][Proofs][Docs] M0.5: realign workspace to vetta-infer-* microkernel architecture
Dissolve the M0 crates (core/io/proto/telemetry/hip/cli) into the approved
service layout abi/kn/ld/cc/rt/lab/sp/nt/tm/gw (docs/ARCHITECTURE.md).

- kn: Lean-verified control bus + service registry (bus_lifecycle.lean, 14 thms)
  and SlabRing/UMA broker; rt: PrefixState + hybrid checkpoint pool.
- sp: faithful throughput-aware Adaptive-K port of clover_policy.h
  (cooldowns, probes, throughput guards) with scenario parity tests.
- gw: strata-ctl-compatible wire emitters + serve session FSM
  (serve_session.lean, 10 thms); abi::serve accepts image= (Vision delta closed).
- abi: pure contracts, checked JSON/VTC1/GEN parsers, zero-alloc sampler.
- ld/cc: checked safetensors reader + write-once VTC1 writer / O_RDONLY reader.
- tm: lock-free telemetry; nt: LyraRpc; lab shares the rt executor core.
- Docs: ARCHITECTURE.md decision-complete spec (MoE-only scope, .vbundle,
  .vplan->CompiledPlan, wire contract, migration map); AGENTS.md Lean rule
  + tandem/review workflow.

Verified live: cargo test --workspace -D warnings = 46/46 PASS;
lean proofs = 34 theorems (slab_ring_dma 10, bus_lifecycle 14, serve_session 10),
0 sorryAx, standard axioms only; vetta-infer info on Inkling = 1244 tensors,
18.18 GiB, checked bounds.
2026-11-30 00:54:00 +01:00

150 lines
4.8 KiB
Rust

//! `VTC1` write-once NVMe cache format — pure metadata contracts shared by
//! `ld` (atomic writer) and `cc` (`O_RDONLY` reader).
//!
//! Eliminates audit-05 F-05 (pre-reserve OOM on corrupt count) by construction:
//! the record count is validated against the blob size BEFORE any allocation.
pub const CACHE_MAGIC: [u8; 8] = *b"VTCACHE1";
pub const CACHE_VERSION: u32 = 1;
pub const CACHE_ALIGN: u64 = 4096;
pub const HEADER_SIZE: usize = 48;
pub const MIN_REC_BYTES: usize = 1 + 4 * 4 + 4 * 8 + 4; // 53 B minimum per serialized record
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CacheRecord {
pub kind: u8,
pub i0: i32,
pub i1: i32,
pub i2: i32,
pub flags: u32,
pub u0: u64,
pub u1: u64,
pub data_offset: u64,
pub data_bytes: u64,
pub name: String,
}
pub fn fnv1a_u64(bytes: &[u8], seed: u64) -> u64 {
let mut h = seed;
for &b in bytes {
h ^= b as u64;
h = h.wrapping_mul(0x0000_0100_0000_01B3);
}
h
}
pub fn align_up(v: u64, align: u64) -> Result<u64, String> {
let rem = v % align;
if rem == 0 {
Ok(v)
} else {
v.checked_add(align - rem).ok_or_else(|| "align_up overflow".into())
}
}
pub fn serialize_meta(recs: &[CacheRecord]) -> Vec<u8> {
let mut out = Vec::new();
for r in recs {
out.push(r.kind);
out.extend_from_slice(&r.i0.to_le_bytes());
out.extend_from_slice(&r.i1.to_le_bytes());
out.extend_from_slice(&r.i2.to_le_bytes());
out.extend_from_slice(&r.flags.to_le_bytes());
out.extend_from_slice(&r.u0.to_le_bytes());
out.extend_from_slice(&r.u1.to_le_bytes());
out.extend_from_slice(&r.data_offset.to_le_bytes());
out.extend_from_slice(&r.data_bytes.to_le_bytes());
let nlen = r.name.len() as u32;
out.extend_from_slice(&nlen.to_le_bytes());
out.extend_from_slice(r.name.as_bytes());
}
out
}
pub fn deserialize_meta(blob: &[u8], count: u32) -> Result<Vec<CacheRecord>, String> {
// Anti-OOM check BEFORE `Vec::with_capacity(count)` (audit-05 F-05)
if (count as usize) > blob.len() / MIN_REC_BYTES {
return Err(format!(
"corrupt cache metadata: count={count} exceeds blob capacity ({} B)",
blob.len()
));
}
let mut out = Vec::with_capacity(count as usize);
let mut pos = 0usize;
let take = |pos: &mut usize, n: usize| -> Result<&[u8], String> {
if n > blob.len().saturating_sub(*pos) {
return Err("truncated cache metadata".into());
}
let s = &blob[*pos..*pos + n];
*pos += n;
Ok(s)
};
for _ in 0..count {
let kind = take(&mut pos, 1)?[0];
let i0 = i32::from_le_bytes(take(&mut pos, 4)?.try_into().unwrap());
let i1 = i32::from_le_bytes(take(&mut pos, 4)?.try_into().unwrap());
let i2 = i32::from_le_bytes(take(&mut pos, 4)?.try_into().unwrap());
let flags = u32::from_le_bytes(take(&mut pos, 4)?.try_into().unwrap());
let u0 = u64::from_le_bytes(take(&mut pos, 8)?.try_into().unwrap());
let u1 = u64::from_le_bytes(take(&mut pos, 8)?.try_into().unwrap());
let data_offset = u64::from_le_bytes(take(&mut pos, 8)?.try_into().unwrap());
let data_bytes = u64::from_le_bytes(take(&mut pos, 8)?.try_into().unwrap());
let nlen = u32::from_le_bytes(take(&mut pos, 4)?.try_into().unwrap()) as usize;
if nlen > 4096 {
return Err("cache record name too long".into());
}
let nbytes = take(&mut pos, nlen)?;
let name = std::str::from_utf8(nbytes)
.map_err(|_| "non-UTF-8 cache record name")?
.to_string();
out.push(CacheRecord {
kind,
i0,
i1,
i2,
flags,
u0,
u1,
data_offset,
data_bytes,
name,
});
}
if pos != blob.len() {
return Err("trailing bytes in cache metadata".into());
}
Ok(out)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn anti_oom_before_reserve() {
// Absurd entry count rejected BEFORE Vec::with_capacity
assert!(deserialize_meta(&[0u8; 64], u32::MAX).is_err());
}
#[test]
fn meta_roundtrip_and_alignment() {
let rec = CacheRecord {
kind: 1,
i0: 128,
i1: 256,
i2: 0,
flags: 0,
u0: 4096,
u1: 0,
data_offset: 4096,
data_bytes: 8192,
name: "layer.0.q8".into(),
};
let blob = serialize_meta(&[rec.clone()]);
let back = deserialize_meta(&blob, 1).unwrap();
assert_eq!(back, vec![rec]);
assert_eq!(align_up(4097, CACHE_ALIGN).unwrap(), 8192);
assert_eq!(align_up(4096, CACHE_ALIGN).unwrap(), 4096);
assert!(align_up(u64::MAX, CACHE_ALIGN).is_err());
}
}