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-allen / tests / support / mod.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 | //! TEST-ONLY subject fixture builder. A second, independent encoder of the |
| 17 | //! kind-24 layout (aienos ADR 0018 s2.2). It lives under tests/ so the |
| 18 | //! production crate gains no provisioning path: real subjects are created |
| 19 | //! only by Aienux `cs_provision`. Anything built here is a fixture. |
| 20 | #![allow(dead_code)] |
| 21 | use aien_allen::subject_v0::object_id; |
| 22 | use sha2::{Digest, Sha256}; |
| 23 | use std::path::{Path, PathBuf}; |
| 24 | |
| 25 | pub fn h32(tag: &str) -> [u8; 32] { |
| 26 | Sha256::digest(tag.as_bytes()).into() |
| 27 | } |
| 28 | |
| 29 | pub struct Fx { |
| 30 | pub root: [u8; 32], |
| 31 | pub agent: [u8; 32], |
| 32 | pub cortex: [u8; 32], |
| 33 | pub provenance: [u8; 32], |
| 34 | } |
| 35 | |
| 36 | pub fn fx(name: &str) -> Fx { |
| 37 | Fx { |
| 38 | root: h32(&format!("root:{name}")), |
| 39 | agent: h32(&format!("agent:{name}")), |
| 40 | cortex: h32(&format!("cortex:{name}")), |
| 41 | provenance: h32(&format!("prov:{name}")), |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | fn intent_id(agent: &[u8; 32], kind: u32, since: u64, p: [u64; 2]) -> [u8; 32] { |
| 46 | let mut h = Sha256::new(); |
| 47 | h.update(b"AIENOS_INTENT_v0:"); |
| 48 | h.update(agent); |
| 49 | h.update(kind.to_le_bytes()); |
| 50 | h.update(since.to_le_bytes()); |
| 51 | h.update(p[0].to_le_bytes()); |
| 52 | h.update(p[1].to_le_bytes()); |
| 53 | h.finalize().into() |
| 54 | } |
| 55 | |
| 56 | /// One object: sequence `seq`, `previous` (zero for 1), one ACTIVE GOAL_LATENCY |
| 57 | /// intent created at sequence 1 with payload (regime, target ns). |
| 58 | pub fn encode(f: &Fx, seq: u64, previous: [u8; 32], payload: [u64; 2]) -> Vec<u8> { |
| 59 | let id = intent_id(&f.agent, 1, 1, payload); |
| 60 | let mut b = Vec::new(); |
| 61 | b.extend_from_slice(b"AIENSUBJ"); |
| 62 | b.extend_from_slice(&0u16.to_le_bytes()); |
| 63 | b.extend_from_slice(&0u16.to_le_bytes()); |
| 64 | b.extend_from_slice(&[0; 4]); // body length, patched below |
| 65 | b.extend_from_slice(&f.root); |
| 66 | b.extend_from_slice(&f.agent); |
| 67 | b.extend_from_slice(&previous); |
| 68 | b.extend_from_slice(&seq.to_le_bytes()); |
| 69 | b.extend_from_slice(&f.cortex); |
| 70 | b.extend_from_slice(&f.provenance); |
| 71 | b.push(1); // origin operator |
| 72 | b.extend_from_slice(&[0; 7]); |
| 73 | b.extend_from_slice(&1u16.to_le_bytes()); // n_intents |
| 74 | b.extend_from_slice(&0u16.to_le_bytes()); // n_knowledge |
| 75 | b.extend_from_slice(&[0; 4]); |
| 76 | b.extend_from_slice(&id); |
| 77 | b.extend_from_slice(&[0; 32]); |
| 78 | b.extend_from_slice(&1u32.to_le_bytes()); |
| 79 | b.push(1); // ACTIVE |
| 80 | b.extend_from_slice(&[0; 3]); |
| 81 | b.extend_from_slice(&1u64.to_le_bytes()); |
| 82 | b.extend_from_slice(&payload[0].to_le_bytes()); |
| 83 | b.extend_from_slice(&payload[1].to_le_bytes()); |
| 84 | let n = (b.len() - 16) as u32; |
| 85 | b[12..16].copy_from_slice(&n.to_le_bytes()); |
| 86 | b |
| 87 | } |
| 88 | |
| 89 | /// A chain of `n` objects; returns the encoded objects in order. |
| 90 | pub fn chain(f: &Fx, n: u64) -> Vec<Vec<u8>> { |
| 91 | let mut out: Vec<Vec<u8>> = Vec::new(); |
| 92 | let mut prev = [0u8; 32]; |
| 93 | for s in 1..=n { |
| 94 | let o = encode(f, s, prev, [7, 1000]); |
| 95 | prev = object_id(&o); |
| 96 | out.push(o); |
| 97 | } |
| 98 | out |
| 99 | } |
| 100 | |
| 101 | /// Write objects into `dir` as `<id-hex>.bin`. |
| 102 | pub fn write_dir(dir: &Path, objs: &[Vec<u8>]) { |
| 103 | std::fs::create_dir_all(dir).unwrap(); |
| 104 | for o in objs { |
| 105 | let name = aien_allen::hex(&object_id(o)); |
| 106 | std::fs::write(dir.join(format!("{name}.bin")), o).unwrap(); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | pub fn write_head(path: &Path, o: &[u8]) -> PathBuf { |
| 111 | std::fs::write(path, o).unwrap(); |
| 112 | path.to_path_buf() |
| 113 | } |