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-omega-compose / tests / compose.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 | //! The omega C test (tests/runtime/rxc_host_abi_test.c) repeated from Rust: |
| 17 | //! run one goal, restart + recall, identity refusal, torn tail refused then |
| 18 | //! recovered, host notes recalled after restart. Ignored |
| 19 | //! unless librx_compose.a is linked (AIEN_OMEGA_COMPOSE_DIR or |
| 20 | //! AIEN_OMEGA_COMPOSE_LIB at build time; see build.rs / README.md). |
| 21 | use aien_omega_compose::*; |
| 22 | use std::sync::atomic::{AtomicU64, Ordering}; |
| 23 | use std::sync::Arc; |
| 24 | |
| 25 | const ROOT: &[u8] = b"np1-host-abi-test-machine"; |
| 26 | fn propose(t: u64) -> u64 { |
| 27 | t.wrapping_mul(3).wrapping_add(1) |
| 28 | } |
| 29 | |
| 30 | fn open(dir: &std::path::Path, seen: Arc<AtomicU64>) -> Result<(Compose, Info), ComposeError> { |
| 31 | let (mut c, info) = Compose::open(dir, RootKind::Provisioned, ROOT, 0x5E55)?; |
| 32 | c.register_skill("np1.echo-proposal", None, 10, move |task| { |
| 33 | seen.store(task, Ordering::SeqCst); |
| 34 | Some(propose(task)) |
| 35 | })?; |
| 36 | c.set_verify(|task, result| result == propose(task))?; |
| 37 | Ok((c, info)) |
| 38 | } |
| 39 | |
| 40 | #[test] |
| 41 | #[cfg_attr( |
| 42 | not(has_omega_compose), |
| 43 | ignore = "needs librx_compose.a (AIEN_OMEGA_COMPOSE_DIR)" |
| 44 | )] |
| 45 | fn run_restart_recall_torn_tail() { |
| 46 | let tmp = tempfile::tempdir().unwrap(); |
| 47 | let home = tmp.path().join("home"); |
| 48 | let seen = Arc::new(AtomicU64::new(0)); |
| 49 | |
| 50 | // T1: one goal |
| 51 | let (mut c, i1) = open(&home, seen.clone()).unwrap(); |
| 52 | assert_eq!(i1.abi_version, 2); |
| 53 | assert_eq!(i1.machine_id_was_stored, 0); |
| 54 | let task = 0x4E50_3100_0000_0001u64; |
| 55 | let r = c.run(task, 1_000_000).unwrap(); |
| 56 | assert_eq!(r.outcome, OUT_COMMITTED); |
| 57 | assert_eq!(r.committed, 1); |
| 58 | assert_eq!(seen.load(Ordering::SeqCst), task); |
| 59 | assert_eq!(r.result, propose(task)); |
| 60 | assert_eq!((r.n_branches, r.winner, r.winner_skill), (1, 0, 0)); |
| 61 | assert_eq!(r.aegis_pass_mask & 1, 1); |
| 62 | let cited = [r.cx_goal, r.cx_candidate[0], r.cx_evidence, r.cx_promotion]; |
| 63 | assert!(cited.iter().all(|&id| id != 0), "{cited:?}"); |
| 64 | let cited_digests: Vec<[u8; 32]> = cited |
| 65 | .iter() |
| 66 | .map(|&id| c.record(id).unwrap().digest) |
| 67 | .collect(); |
| 68 | let (before, total) = c.recall(SUBJECT_STATE, 64).unwrap(); |
| 69 | assert!(!before.is_empty() && before.len() as u64 == total); |
| 70 | let mid = c.info().unwrap().machine_id; |
| 71 | drop(c); |
| 72 | |
| 73 | // T2: restart + recall (pre-restart records are an unchanged prefix) |
| 74 | let (mut c, i2) = open(&home, seen.clone()).unwrap(); |
| 75 | assert_eq!(i2.machine_id_was_stored, 1); |
| 76 | assert_eq!(i2.machine_id, mid); |
| 77 | assert_eq!(i2.tail_torn, 0); |
| 78 | let (after, _) = c.recall(SUBJECT_STATE, 64).unwrap(); |
| 79 | assert!(after.len() >= before.len()); |
| 80 | for (a, b) in after.iter().zip(before.iter()) { |
| 81 | assert_eq!((a.id, a.digest, a.verified), (b.id, b.digest, 1)); |
| 82 | } |
| 83 | for (id, d) in cited.iter().zip(cited_digests.iter()) { |
| 84 | assert_eq!(&c.record(*id).unwrap().digest, d); |
| 85 | } |
| 86 | drop(c); |
| 87 | |
| 88 | // T3: another machine is refused on this home |
| 89 | let other = Compose::open(&home, RootKind::Provisioned, b"np1-some-other-machine", 1); |
| 90 | assert!(matches!( |
| 91 | other.err(), |
| 92 | Some(ComposeError::Code { code: -2, .. }) |
| 93 | )); |
| 94 | |
| 95 | // T5: a run's own tail torn: open refuses (E_TORN) without writing, stably; |
| 96 | // recover cuts the torn record, records the cut, re-anchors; open succeeds |
| 97 | // with the old records an unchanged prefix; a goal commits. |
| 98 | let (mut c, _) = open(&home, seen.clone()).unwrap(); |
| 99 | let r = c.run(0x4E50_3100_0000_0003, 3_000_000).unwrap(); |
| 100 | assert_eq!(r.committed, 1); |
| 101 | let n0 = c.info().unwrap().records; |
| 102 | let all: Vec<Record> = (1..=n0).map(|id| c.record(id).unwrap()).collect(); |
| 103 | drop(c); |
| 104 | let cx = home.join("cortex.cx"); |
| 105 | let len = std::fs::metadata(&cx).unwrap().len(); |
| 106 | let f = std::fs::OpenOptions::new().write(true).open(&cx).unwrap(); |
| 107 | f.set_len(len - 5).unwrap(); |
| 108 | drop(f); |
| 109 | for _ in 0..2 { |
| 110 | let refused = open(&home, seen.clone()); |
| 111 | assert!(matches!( |
| 112 | refused.err(), |
| 113 | Some(ComposeError::Code { code: -3, .. }) |
| 114 | )); |
| 115 | assert_eq!( |
| 116 | std::fs::metadata(&cx).unwrap().len(), |
| 117 | len - 5, |
| 118 | "refusal wrote" |
| 119 | ); |
| 120 | } |
| 121 | let rep = Compose::recover(&home, RootKind::Provisioned, ROOT).unwrap(); |
| 122 | assert_eq!( |
| 123 | (rep.repaired, rep.tail_torn, rep.cause, rep.opens), |
| 124 | (1, 1, -8, 1) |
| 125 | ); |
| 126 | assert_eq!(rep.cut_hi, len - 5); |
| 127 | assert_eq!(rep.cut_bytes_kept, rep.cut_hi - rep.cut_lo); |
| 128 | assert!(rep.dropped_records >= 1); |
| 129 | assert_eq!(rep.event_id, rep.records_kept + 1); |
| 130 | let again = Compose::recover(&home, RootKind::Provisioned, ROOT).unwrap(); |
| 131 | assert_eq!(again.repaired, 0, "second recover is a no-op"); |
| 132 | let (mut c, ir) = open(&home, seen.clone()).unwrap(); |
| 133 | assert_eq!(ir.tail_torn, 0); |
| 134 | c.info().unwrap(); |
| 135 | for rec in &all[..rep.records_kept as usize] { |
| 136 | assert_eq!(c.record(rec.id).unwrap().digest, rec.digest); |
| 137 | } |
| 138 | let ev = c.record(rep.event_id).unwrap(); |
| 139 | assert_eq!( |
| 140 | (ev.subject, ev.tag), |
| 141 | (SUBJECT_HOST, ffi::RXC_HOST_TAG_REPAIR_TAIL) |
| 142 | ); |
| 143 | let p = c.payload(rep.event_id).unwrap(); |
| 144 | assert_eq!(p[ffi::RXC_HOST_RP_CUT_LO], rep.cut_lo); |
| 145 | assert_eq!(p[ffi::RXC_HOST_RP_DROPPED], rep.dropped_records); |
| 146 | let r = c.run(0x4E50_3100_0000_0004, 4_000_000).unwrap(); |
| 147 | assert_eq!(r.committed, 1); |
| 148 | |
| 149 | // T7: host notes through the composition writer, recalled after restart |
| 150 | let text = b"constraint: only touch files under the workspace"; |
| 151 | let cid = c.note(NoteKind::Constraint, [0; 4], text).unwrap(); |
| 152 | let eid = c |
| 153 | .note( |
| 154 | NoteKind::Effect, |
| 155 | [r.cx_promotion, cid, 0, 0], |
| 156 | b"effect: wrote a.txt", |
| 157 | ) |
| 158 | .unwrap(); |
| 159 | assert!(eid > cid); |
| 160 | assert!(c.note(NoteKind::Effect, [1 << 40, 0, 0, 0], b"x").is_err()); |
| 161 | let (before, _) = c.recall(SUBJECT_HOST, 64).unwrap(); |
| 162 | drop(c); |
| 163 | let (mut c, i7) = open(&home, seen.clone()).unwrap(); |
| 164 | assert_eq!(i7.machine_id, mid); |
| 165 | let (after, _) = c.recall(SUBJECT_HOST, 64).unwrap(); |
| 166 | assert_eq!(after.len(), before.len()); |
| 167 | for (a, b) in after.iter().zip(before.iter()) { |
| 168 | assert_eq!((a.id, a.digest, a.verified), (b.id, b.digest, 1)); |
| 169 | } |
| 170 | let got = note_bytes(&c.payload(cid).unwrap()).unwrap(); |
| 171 | assert_eq!(got, text); |
| 172 | assert_eq!(c.record(eid).unwrap().links[..2], [r.cx_promotion, cid]); |
| 173 | } |