interplane.Get Omega
Read in your language

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

branch main Branches Tags History

aien-sovereign-core-mv / crates / aien-omega-compose / tests / callbacks.rs

181 lines · 7581 bytes Raw Blame History
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//! Callback safety across the rxc_host boundary, against the real librx_compose.a:
17//! a failing or panicking Skill or verifier is contained and reported as a failed alternative
18//! (never unwinds into C), repeated open/close neither leaks handle slots nor keeps callback
19//! state alive, and several homes driven from several threads at once keep their own
20//! callbacks apart (the C side routes ctx-less trampolines through a shared slot table).
21//!
22//! Ignored unless the library is linked. Note: the workspace release profile sets
23//! `panic = "abort"`, so in a release binary a panicking callback aborts the process instead
24//! (also never an unwind across C); these tests run in the unwinding test profile, where the
25//! `catch_unwind` in the trampolines is what keeps the panic out of C.
26
27use aien_omega_compose::*;
28use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
29use std::sync::{Arc, Mutex, MutexGuard};
30
31const ROOT: &[u8] = b"abi-qual-callback-test-machine";
32
33/// librx_compose.a has RXC_HOST_MAX_HANDLES (4) handle slots per process, and the test harness
34/// runs these tests on parallel threads in one process: together the five open up to seven
35/// handles at once (3, 1, 1, 1, 1) and ran out of slots (E_FULL at `Compose::open`) under a plain `cargo test`,
36/// though each passes alone. Every test holds this guard for as long as it holds handles, so
37/// the process never needs more slots than one test does (the most is 3, the concurrent test).
38static HANDLE_SLOTS: Mutex<()> = Mutex::new(());
39
40fn handle_slots() -> MutexGuard<'static, ()> {
41 // A test that panicked while holding the guard poisons it; its handles are closed by then.
42 HANDLE_SLOTS.lock().unwrap_or_else(|e| e.into_inner())
43}
44
45fn propose(t: u64) -> u64 {
46 t.wrapping_mul(7).wrapping_add(3)
47}
48
49#[test]
50#[cfg_attr(
51 not(has_omega_compose),
52 ignore = "needs librx_compose.a (AIEN_OMEGA_COMPOSE_DIR)"
53)]
54fn panicking_skill_is_contained_and_reported_as_failure() {
55 let _slots = handle_slots();
56 let tmp = tempfile::tempdir().unwrap();
57 let (mut c, _) = Compose::open(&tmp.path().join("h"), RootKind::Provisioned, ROOT, 1).unwrap();
58 c.register_skill("abi.panics", None, 1, |_| panic!("skill panic"))
59 .unwrap();
60 let r = c.run(0xA1, 1_000_000).unwrap();
61 assert_eq!(r.committed, 0, "a panicking Skill must not commit: {r:?}");
62 assert_ne!(r.outcome, OUT_COMMITTED);
63 assert_eq!(r.result, 0);
64 // The handle is still usable after the contained panic.
65 let r2 = c.run(0xA2, 2_000_000).unwrap();
66 assert_eq!(r2.committed, 0);
67}
68
69#[test]
70#[cfg_attr(
71 not(has_omega_compose),
72 ignore = "needs librx_compose.a (AIEN_OMEGA_COMPOSE_DIR)"
73)]
74fn failing_and_zero_results_do_not_commit() {
75 let _slots = handle_slots();
76 let tmp = tempfile::tempdir().unwrap();
77 let (mut c, _) = Compose::open(&tmp.path().join("h"), RootKind::Provisioned, ROOT, 2).unwrap();
78 c.register_skill("abi.none", None, 1, |_| None).unwrap();
79 c.register_skill("abi.zero", None, 2, |_| Some(0)).unwrap();
80 let r = c.run(0xB1, 1_000_000).unwrap();
81 assert_eq!(r.committed, 0, "{r:?}");
82 assert_eq!(r.aegis_pass_mask, 0, "{r:?}");
83}
84
85#[test]
86#[cfg_attr(
87 not(has_omega_compose),
88 ignore = "needs librx_compose.a (AIEN_OMEGA_COMPOSE_DIR)"
89)]
90fn panicking_verifier_fails_the_contract() {
91 let _slots = handle_slots();
92 let tmp = tempfile::tempdir().unwrap();
93 let (mut c, _) = Compose::open(&tmp.path().join("h"), RootKind::Provisioned, ROOT, 3).unwrap();
94 c.register_skill("abi.ok", None, 1, |t| Some(propose(t)))
95 .unwrap();
96 c.set_verify(|_, _| panic!("verifier panic")).unwrap();
97 let r = c.run(0xC1, 1_000_000).unwrap();
98 assert_eq!(r.committed, 0, "{r:?}");
99 assert_eq!(r.aegis_pass_mask, 0, "{r:?}");
100}
101
102#[test]
103#[cfg_attr(
104 not(has_omega_compose),
105 ignore = "needs librx_compose.a (AIEN_OMEGA_COMPOSE_DIR)"
106)]
107fn repeated_open_close_releases_slots_and_callback_state() {
108 let _slots = handle_slots();
109 let tmp = tempfile::tempdir().unwrap();
110 let home = tmp.path().join("h");
111 let state = Arc::new(AtomicU64::new(0));
112 // More cycles than RXC_HOST_MAX_HANDLES (4): a leaked slot would refuse with E_FULL.
113 for i in 0..12u64 {
114 let s = state.clone();
115 let (mut c, info) = Compose::open(&home, RootKind::Provisioned, ROOT, 10 + i).unwrap();
116 assert_eq!(info.abi_version, ffi::RXC_HOST_ABI_VERSION);
117 c.register_skill("abi.count", None, 1, move |t| {
118 s.fetch_add(1, Ordering::SeqCst);
119 Some(propose(t))
120 })
121 .unwrap();
122 let r = c.run(0xD000 + i, 1_000_000 * (i + 1)).unwrap();
123 assert_eq!(r.committed, 1, "cycle {i}: {r:?}");
124 assert_eq!(r.result, propose(0xD000 + i));
125 drop(c);
126 // The closure (and its clone of `state`) is freed with the handle.
127 assert_eq!(
128 Arc::strong_count(&state),
129 1,
130 "cycle {i}: callback state outlived close"
131 );
132 }
133 assert_eq!(state.load(Ordering::SeqCst), 12);
134}
135
136#[test]
137#[cfg_attr(
138 not(has_omega_compose),
139 ignore = "needs librx_compose.a (AIEN_OMEGA_COMPOSE_DIR)"
140)]
141fn concurrent_homes_keep_their_callbacks_apart() {
142 let _slots = handle_slots();
143 let tmp = Arc::new(tempfile::tempdir().unwrap());
144 let calls = Arc::new(AtomicUsize::new(0));
145 let threads: Vec<_> = (0..3u64)
146 .map(|k| {
147 let tmp = tmp.clone();
148 let calls = calls.clone();
149 std::thread::spawn(move || {
150 let home = tmp.path().join(format!("h{k}"));
151 let (mut c, _) =
152 Compose::open(&home, RootKind::Provisioned, ROOT, 100 + k).unwrap();
153 let tag = k << 56;
154 let seen = Arc::new(AtomicU64::new(0));
155 let s2 = seen.clone();
156 let calls2 = calls.clone();
157 c.register_skill("abi.tagged", None, 1, move |t| {
158 calls2.fetch_add(1, Ordering::SeqCst);
159 s2.store(t, Ordering::SeqCst);
160 Some(propose(t) | 1)
161 })
162 .unwrap();
163 // A verifier that only accepts this home's own tag: a callback routed to the
164 // wrong handle would fail the contract.
165 c.set_verify(move |t, r| t >> 56 == k && r == (propose(t) | 1))
166 .unwrap();
167 for i in 1..=5u64 {
168 let task = tag | i;
169 let r = c.run(task, i * 1_000_000).unwrap();
170 assert_eq!(r.committed, 1, "home {k} goal {i}: {r:?}");
171 assert_eq!(r.result, propose(task) | 1);
172 assert_eq!(seen.load(Ordering::SeqCst), task);
173 }
174 })
175 })
176 .collect();
177 for t in threads {
178 t.join().expect("worker thread");
179 }
180 assert_eq!(calls.load(Ordering::SeqCst), 15);
181}