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 / src / lib.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 | //! Safe binding to omega COMPOSITION-2 through the rxc_host ABI |
| 17 | //! (`src/runtime/rxc_host_abi.h`, linked from `librx_compose.a`). |
| 18 | //! |
| 19 | //! One goal = one `Compose::run`: the registered Skills compete on staged |
| 20 | //! J-Space branches, the AEGIS verifier checks each result against the |
| 21 | //! contract (the `set_verify` callback), the winner is committed to the World |
| 22 | //! and the whole composition is recorded in the Cortex journal |
| 23 | //! `<dir>/cortex.cx`. Records and their digests survive a restart; the |
| 24 | //! directory is bound to one AienMachineId. |
| 25 | //! |
| 26 | //! Without the library (stub build, see build.rs) every constructor returns |
| 27 | //! [`ComposeError::Unavailable`] and [`LINKED`] is false. |
| 28 | pub mod ffi; |
| 29 | |
| 30 | use std::fmt; |
| 31 | use std::path::Path; |
| 32 | |
| 33 | /// The wait (ms) most recently accepted by `Compose::set_wait_ms`, 0 before any. A test |
| 34 | /// observation point: the value omega was actually told, not a recomputation. |
| 35 | pub static LAST_WAIT_MS: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0); |
| 36 | |
| 37 | /// True when `librx_compose.a` is linked (not the stub). |
| 38 | pub const LINKED: bool = cfg!(has_omega_compose); |
| 39 | /// The omega commit this build expected (omega.lock or `AIEN_OMEGA_COMPOSE_SHA`). |
| 40 | pub const EXPECTED_OMEGA_SHA: &str = env!("AIEN_OMEGA_COMPOSE_EXPECTED_SHA"); |
| 41 | |
| 42 | /// What this binary linked for the omega composition library (stub, pinned source, developer override or |
| 43 | /// prebuilt), the C ABI check result and the archive digest, as `build.rs` established them. |
| 44 | /// Only `qualifies()` identities count as release-grade native qualification. |
| 45 | pub fn native_identity() -> aien_abi_conformance::NativeIdentity { |
| 46 | aien_abi_conformance::NativeIdentity { |
| 47 | component: "librx_compose.a", |
| 48 | link: env!("AIEN_OMEGA_COMPOSE_LINK"), |
| 49 | abi: env!("AIEN_OMEGA_COMPOSE_ABI"), |
| 50 | digest: env!("AIEN_OMEGA_COMPOSE_DIGEST"), |
| 51 | archive_sha256: env!("AIEN_OMEGA_COMPOSE_ARCHIVE_SHA256"), |
| 52 | source_sha: env!("AIEN_OMEGA_COMPOSE_SOURCE_SHA"), |
| 53 | aienos_lock: env!("AIEN_OMEGA_COMPOSE_AIENOS_LOCK"), |
| 54 | physics_lock: env!("AIEN_OMEGA_COMPOSE_PHYSICS_LOCK"), |
| 55 | target: env!("AIEN_OMEGA_COMPOSE_TARGET"), |
| 56 | cc: env!("AIEN_OMEGA_COMPOSE_CC"), |
| 57 | } |
| 58 | } |
| 59 | pub use ffi::{RXC_HOST_SUBJECT_GOAL as SUBJECT_GOAL, RXC_HOST_SUBJECT_STATE as SUBJECT_STATE}; |
| 60 | /// No winner / no Skill. |
| 61 | pub const NONE: u32 = ffi::RXC_HOST_NONE; |
| 62 | |
| 63 | /// Composition outcomes (`RXC_HOST_OUT_*`). |
| 64 | pub const OUT_COMMITTED: i32 = 1; |
| 65 | pub const OUT_NO_WINNER: i32 = 2; |
| 66 | pub const OUT_NOT_COMMITTED: i32 = 3; |
| 67 | pub const OUT_NOT_DURABLE: i32 = 4; |
| 68 | pub const OUT_RECORD_FAILED: i32 = 5; |
| 69 | |
| 70 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 71 | pub enum RootKind { |
| 72 | /// 1..4096 opaque bytes generated once at provisioning. |
| 73 | Provisioned = 1, |
| 74 | /// The 32-byte digest of the owner-enrolled key. |
| 75 | Hardware = 2, |
| 76 | } |
| 77 | |
| 78 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 79 | pub enum ComposeError { |
| 80 | /// The library is not linked (stub build). |
| 81 | Unavailable, |
| 82 | /// Rust and C struct layouts differ (wrong omega commit). |
| 83 | Layout { c: [u32; 4], rust: [u32; 4] }, |
| 84 | /// An argument Rust refuses before calling C (interior NUL, empty name). |
| 85 | Arg(String), |
| 86 | /// `RXC_HOST_E_*` from the C side, with the underlying rx code when known. |
| 87 | Code { code: i32, detail: i32 }, |
| 88 | } |
| 89 | |
| 90 | impl ComposeError { |
| 91 | pub fn name(code: i32) -> &'static str { |
| 92 | match code { |
| 93 | ffi::RXC_HOST_E_ARG => "E_ARG", |
| 94 | ffi::RXC_HOST_E_IDENTITY => "E_IDENTITY", |
| 95 | ffi::RXC_HOST_E_TORN => "E_TORN", |
| 96 | ffi::RXC_HOST_E_OPEN => "E_OPEN", |
| 97 | ffi::RXC_HOST_E_FULL => "E_FULL", |
| 98 | ffi::RXC_HOST_E_STATE => "E_STATE", |
| 99 | ffi::RXC_HOST_E_RUN => "E_RUN", |
| 100 | ffi::RXC_HOST_E_NOMEM => "E_NOMEM", |
| 101 | ffi::RXC_HOST_E_NOT_FOUND => "E_NOT_FOUND", |
| 102 | ffi::RXC_HOST_E_DIGEST => "E_DIGEST", |
| 103 | ffi::RXC_HOST_E_REPLAY => "E_REPLAY", |
| 104 | _ => "E_UNKNOWN", |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | impl fmt::Display for ComposeError { |
| 110 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 111 | match self { |
| 112 | ComposeError::Unavailable => write!(f, "librx_compose.a is not linked (stub build)"), |
| 113 | ComposeError::Layout { c, rust } => { |
| 114 | write!(f, "rxc_host ABI layout mismatch: C {c:?} vs Rust {rust:?}") |
| 115 | } |
| 116 | ComposeError::Arg(s) => write!(f, "invalid argument: {s}"), |
| 117 | ComposeError::Code { code, detail } => { |
| 118 | write!( |
| 119 | f, |
| 120 | "rxc_host {} ({code}), detail {detail}", |
| 121 | Self::name(*code) |
| 122 | ) |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | impl std::error::Error for ComposeError {} |
| 129 | |
| 130 | pub type Info = ffi::RxcHostInfo; |
| 131 | pub type RunResult = ffi::RxcHostResult; |
| 132 | pub type Record = ffi::RxcHostRecord; |
| 133 | pub type Repair = ffi::RxcHostRepair; |
| 134 | pub use ffi::RXC_HOST_SUBJECT_HOST as SUBJECT_HOST; |
| 135 | |
| 136 | /// Host record kinds (`RXC_HOST_NOTE_*`), all on [`SUBJECT_HOST`]. The record tag |
| 137 | /// is the kind. |
| 138 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 139 | pub enum NoteKind { |
| 140 | /// CLAIM / CX_K_CLAIM: an operator constraint. |
| 141 | Constraint = 2, |
| 142 | /// EVIDENCE / CX_K_ADMISSION: an approval for one effect. |
| 143 | Authorization = 3, |
| 144 | /// EVIDENCE / CX_K_EVIDENCE_REF: an effect receipt. |
| 145 | Effect = 4, |
| 146 | } |
| 147 | |
| 148 | impl NoteKind { |
| 149 | pub fn from_tag(tag: u64) -> Option<Self> { |
| 150 | match tag { |
| 151 | 2 => Some(Self::Constraint), |
| 152 | 3 => Some(Self::Authorization), |
| 153 | 4 => Some(Self::Effect), |
| 154 | _ => None, |
| 155 | } |
| 156 | } |
| 157 | pub fn name(self) -> &'static str { |
| 158 | match self { |
| 159 | Self::Constraint => "constraint", |
| 160 | Self::Authorization => "authorization", |
| 161 | Self::Effect => "effect", |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | /// The bytes of a host note payload (`[len, sha256 x4, bytes...]`), or None |
| 167 | /// when the payload is short or its sha256 does not match the bytes. |
| 168 | pub fn note_bytes(payload: &[u64]) -> Option<Vec<u8>> { |
| 169 | use sha2::{Digest, Sha256}; |
| 170 | let len = *payload.get(ffi::RXC_HOST_NP_LEN)? as usize; |
| 171 | let words = payload.get(ffi::RXC_HOST_NP_BYTES..)?; |
| 172 | if len > words.len() * 8 { |
| 173 | return None; |
| 174 | } |
| 175 | let bytes: Vec<u8> = words |
| 176 | .iter() |
| 177 | .flat_map(|w| w.to_le_bytes()) |
| 178 | .take(len) |
| 179 | .collect(); |
| 180 | let sha: Vec<u8> = payload |
| 181 | .get(ffi::RXC_HOST_NP_SHA..ffi::RXC_HOST_NP_SHA + 4)? |
| 182 | .iter() |
| 183 | .flat_map(|w| w.to_le_bytes()) |
| 184 | .collect(); |
| 185 | (Sha256::digest(&bytes).as_slice() == sha.as_slice()).then_some(bytes) |
| 186 | } |
| 187 | |
| 188 | /// Lower-case hex of a byte slice. |
| 189 | pub fn hex(b: &[u8]) -> String { |
| 190 | b.iter().map(|x| format!("{x:02x}")).collect() |
| 191 | } |
| 192 | |
| 193 | type SkillFn = dyn Fn(u64) -> Option<u64> + Send + Sync; |
| 194 | type VerifyFn = dyn Fn(u64, u64) -> bool + Send + Sync; |
| 195 | |
| 196 | /// An open composition home. Calls take `&mut self`: one caller at a time. |
| 197 | /// Skill and verify callbacks run on omega World worker threads (two Skills |
| 198 | /// may run at once), hence `Send + Sync`. |
| 199 | pub struct Compose { |
| 200 | #[cfg_attr(not(has_omega_compose), allow(dead_code))] |
| 201 | h: *mut ffi::RxcHost, |
| 202 | // Boxed so the ctx pointers handed to C stay put; dropped after close. |
| 203 | #[allow(clippy::vec_box, dead_code)] |
| 204 | skills: Vec<Box<Box<SkillFn>>>, |
| 205 | #[allow(dead_code)] |
| 206 | verify: Option<Box<Box<VerifyFn>>>, |
| 207 | } |
| 208 | |
| 209 | // SAFETY: the handle is only used through &mut self (one thread at a time); |
| 210 | // the C side's own worker threads only call the Send + Sync callbacks. |
| 211 | unsafe impl Send for Compose {} |
| 212 | |
| 213 | #[cfg(has_omega_compose)] |
| 214 | mod linked { |
| 215 | use super::*; |
| 216 | use std::ffi::CString; |
| 217 | use std::os::raw::{c_int, c_void}; |
| 218 | use std::panic::{catch_unwind, AssertUnwindSafe}; |
| 219 | |
| 220 | fn check(rc: c_int, detail: i32) -> Result<(), ComposeError> { |
| 221 | if rc == ffi::RXC_HOST_OK { |
| 222 | Ok(()) |
| 223 | } else { |
| 224 | Err(ComposeError::Code { code: rc, detail }) |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | unsafe extern "C" fn skill_tramp(ctx: *mut c_void, task: u64, result: *mut u64) -> c_int { |
| 229 | if ctx.is_null() || result.is_null() { |
| 230 | return 1; |
| 231 | } |
| 232 | let f = &*(ctx as *const Box<SkillFn>); |
| 233 | match catch_unwind(AssertUnwindSafe(|| f(task))) { |
| 234 | Ok(Some(r)) if r != 0 => { |
| 235 | *result = r; |
| 236 | 0 |
| 237 | } |
| 238 | _ => 1, |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | unsafe extern "C" fn verify_tramp(ctx: *mut c_void, task: u64, result: u64) -> c_int { |
| 243 | if ctx.is_null() { |
| 244 | return 0; |
| 245 | } |
| 246 | let f = &*(ctx as *const Box<VerifyFn>); |
| 247 | matches!(catch_unwind(AssertUnwindSafe(|| f(task, result))), Ok(true)) as c_int |
| 248 | } |
| 249 | |
| 250 | fn layout() -> Result<(), ComposeError> { |
| 251 | let mut c = [0u32; 4]; |
| 252 | // SAFETY: writes exactly four u32. |
| 253 | let v = unsafe { ffi::rxc_host_abi_layout(c.as_mut_ptr()) }; |
| 254 | let rust = [ |
| 255 | std::mem::size_of::<ffi::RxcHostInfo>() as u32, |
| 256 | std::mem::size_of::<ffi::RxcHostResult>() as u32, |
| 257 | std::mem::size_of::<ffi::RxcHostRecord>() as u32, |
| 258 | std::mem::size_of::<ffi::RxcHostRepair>() as u32, |
| 259 | ]; |
| 260 | if v != ffi::RXC_HOST_ABI_VERSION || c != rust { |
| 261 | return Err(ComposeError::Layout { c, rust }); |
| 262 | } |
| 263 | Ok(()) |
| 264 | } |
| 265 | |
| 266 | impl Compose { |
| 267 | pub fn open( |
| 268 | dir: &Path, |
| 269 | root_kind: RootKind, |
| 270 | root: &[u8], |
| 271 | session: u64, |
| 272 | ) -> Result<(Self, Info), ComposeError> { |
| 273 | layout()?; |
| 274 | use std::os::unix::ffi::OsStrExt; |
| 275 | let d = CString::new(dir.as_os_str().as_bytes()) |
| 276 | .map_err(|_| ComposeError::Arg("dir holds a NUL byte".into()))?; |
| 277 | let mut h = std::ptr::null_mut(); |
| 278 | let mut info = Info::default(); |
| 279 | // SAFETY: valid C string, root slice, out pointers. |
| 280 | let rc = unsafe { |
| 281 | ffi::rxc_host_open( |
| 282 | d.as_ptr(), |
| 283 | root_kind as u32, |
| 284 | root.as_ptr(), |
| 285 | root.len(), |
| 286 | session, |
| 287 | 0, |
| 288 | &mut h, |
| 289 | &mut info, |
| 290 | ) |
| 291 | }; |
| 292 | check(rc, info.open_rc)?; |
| 293 | Ok(( |
| 294 | Compose { |
| 295 | h, |
| 296 | skills: Vec::new(), |
| 297 | verify: None, |
| 298 | }, |
| 299 | info, |
| 300 | )) |
| 301 | } |
| 302 | |
| 303 | /// Set how long omega waits for a run to settle (default 30 000 ms, 1..=600 000). |
| 304 | /// Needs an omega that exposes `rxc_host_set_wait_ms`; against an older omega only |
| 305 | /// the unchanged default 30 000 ms is accepted and anything else is refused. |
| 306 | pub fn set_wait_ms(&mut self, wait_ms: u32) -> Result<(), ComposeError> { |
| 307 | #[cfg(has_omega_wait_ms)] |
| 308 | { |
| 309 | // SAFETY: valid open handle. |
| 310 | let rc = unsafe { ffi::rxc_host_set_wait_ms(self.h, wait_ms) }; |
| 311 | check(rc, 0)?; |
| 312 | LAST_WAIT_MS.store(wait_ms, std::sync::atomic::Ordering::SeqCst); |
| 313 | Ok(()) |
| 314 | } |
| 315 | #[cfg(not(has_omega_wait_ms))] |
| 316 | { |
| 317 | if wait_ms == ffi::WAIT_MS_DEFAULT { |
| 318 | LAST_WAIT_MS.store(wait_ms, std::sync::atomic::Ordering::SeqCst); |
| 319 | Ok(()) |
| 320 | } else { |
| 321 | Err(ComposeError::Arg(format!( |
| 322 | "wait {wait_ms} ms needs omega with rxc_host_set_wait_ms; this omega waits a fixed {} ms", |
| 323 | ffi::WAIT_MS_DEFAULT |
| 324 | ))) |
| 325 | } |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | pub fn register_skill<F>( |
| 330 | &mut self, |
| 331 | name: &str, |
| 332 | digest: Option<[u8; 32]>, |
| 333 | cost: u64, |
| 334 | f: F, |
| 335 | ) -> Result<u32, ComposeError> |
| 336 | where |
| 337 | F: Fn(u64) -> Option<u64> + Send + Sync + 'static, |
| 338 | { |
| 339 | let n = CString::new(name) |
| 340 | .map_err(|_| ComposeError::Arg("name holds a NUL byte".into()))?; |
| 341 | let b: Box<Box<SkillFn>> = Box::new(Box::new(f)); |
| 342 | let ctx = &*b as *const Box<SkillFn> as *mut c_void; |
| 343 | let dp = digest.as_ref().map_or(std::ptr::null(), |d| d.as_ptr()); |
| 344 | // SAFETY: ctx outlives the handle (dropped after close in Drop). |
| 345 | let rc = unsafe { |
| 346 | ffi::rxc_host_register_skill(self.h, n.as_ptr(), dp, cost, skill_tramp, ctx) |
| 347 | }; |
| 348 | if rc < 0 { |
| 349 | return Err(ComposeError::Code { |
| 350 | code: rc, |
| 351 | detail: 0, |
| 352 | }); |
| 353 | } |
| 354 | self.skills.push(b); |
| 355 | Ok(rc as u32) |
| 356 | } |
| 357 | |
| 358 | pub fn set_verify<F>(&mut self, f: F) -> Result<(), ComposeError> |
| 359 | where |
| 360 | F: Fn(u64, u64) -> bool + Send + Sync + 'static, |
| 361 | { |
| 362 | let b: Box<Box<VerifyFn>> = Box::new(Box::new(f)); |
| 363 | let ctx = &*b as *const Box<VerifyFn> as *mut c_void; |
| 364 | // SAFETY: as register_skill. |
| 365 | let rc = unsafe { ffi::rxc_host_set_verify(self.h, Some(verify_tramp), ctx) }; |
| 366 | check(rc, 0)?; |
| 367 | self.verify = Some(b); |
| 368 | Ok(()) |
| 369 | } |
| 370 | |
| 371 | pub fn run(&mut self, task: u64, now_us: u64) -> Result<RunResult, ComposeError> { |
| 372 | let mut r = RunResult::default(); |
| 373 | // SAFETY: valid handle and out pointer. |
| 374 | let rc = unsafe { ffi::rxc_host_run(self.h, task, now_us, &mut r) }; |
| 375 | check(rc, r.run_rc)?; |
| 376 | Ok(r) |
| 377 | } |
| 378 | |
| 379 | /// Records of `subject`, oldest first (at most `max`), and how many exist. |
| 380 | pub fn recall( |
| 381 | &mut self, |
| 382 | subject: u64, |
| 383 | max: u32, |
| 384 | ) -> Result<(Vec<Record>, u64), ComposeError> { |
| 385 | let mut v = vec![Record::default(); max as usize]; |
| 386 | let (mut n, mut total) = (0u32, 0u64); |
| 387 | // SAFETY: v holds max records. |
| 388 | let rc = unsafe { |
| 389 | ffi::rxc_host_recall(self.h, subject, v.as_mut_ptr(), max, &mut n, &mut total) |
| 390 | }; |
| 391 | check(rc, 0)?; |
| 392 | v.truncate(n as usize); |
| 393 | Ok((v, total)) |
| 394 | } |
| 395 | |
| 396 | pub fn record(&mut self, id: u64) -> Result<Record, ComposeError> { |
| 397 | let mut r = Record::default(); |
| 398 | // SAFETY: valid handle and out pointer. |
| 399 | let rc = unsafe { ffi::rxc_host_record(self.h, id, &mut r) }; |
| 400 | check(rc, 0)?; |
| 401 | Ok(r) |
| 402 | } |
| 403 | |
| 404 | pub fn payload(&mut self, id: u64) -> Result<Vec<u64>, ComposeError> { |
| 405 | // SAFETY: max 0 only asks for the count. |
| 406 | let n = unsafe { ffi::rxc_host_payload(self.h, id, std::ptr::null_mut(), 0) }; |
| 407 | if n < 0 { |
| 408 | return Err(ComposeError::Code { code: n, detail: 0 }); |
| 409 | } |
| 410 | let mut v = vec![0u64; n as usize]; |
| 411 | // SAFETY: v holds n words. |
| 412 | let m = unsafe { ffi::rxc_host_payload(self.h, id, v.as_mut_ptr(), n as u32) }; |
| 413 | if m < 0 { |
| 414 | return Err(ComposeError::Code { code: m, detail: 0 }); |
| 415 | } |
| 416 | Ok(v) |
| 417 | } |
| 418 | |
| 419 | pub fn info(&mut self) -> Result<Info, ComposeError> { |
| 420 | let mut i = Info::default(); |
| 421 | // SAFETY: valid handle and out pointer. |
| 422 | let rc = unsafe { ffi::rxc_host_info(self.h, &mut i) }; |
| 423 | check(rc, i.open_rc)?; |
| 424 | Ok(i) |
| 425 | } |
| 426 | |
| 427 | /// Append one host record through the composition's Cortex writer |
| 428 | /// (between runs). `links` name related Cortex ids (0 = none); each |
| 429 | /// must exist. Returns the new record id. |
| 430 | pub fn note( |
| 431 | &mut self, |
| 432 | kind: NoteKind, |
| 433 | links: [u64; 4], |
| 434 | bytes: &[u8], |
| 435 | ) -> Result<u64, ComposeError> { |
| 436 | if bytes.is_empty() || bytes.len() > ffi::RXC_HOST_NOTE_MAX { |
| 437 | return Err(ComposeError::Arg(format!( |
| 438 | "note of {} bytes (1..={})", |
| 439 | bytes.len(), |
| 440 | ffi::RXC_HOST_NOTE_MAX |
| 441 | ))); |
| 442 | } |
| 443 | let mut id = 0u64; |
| 444 | // SAFETY: valid handle, 4 links, byte slice, out pointer. |
| 445 | let rc = unsafe { |
| 446 | ffi::rxc_host_note( |
| 447 | self.h, |
| 448 | kind as u32, |
| 449 | links.as_ptr(), |
| 450 | bytes.as_ptr(), |
| 451 | bytes.len(), |
| 452 | &mut id, |
| 453 | ) |
| 454 | }; |
| 455 | check(rc, 0)?; |
| 456 | Ok(id) |
| 457 | } |
| 458 | |
| 459 | /// Operator repair of a refused home (no handle may be open on it). |
| 460 | /// `Ok` when nothing needed repair or the repair left a home that |
| 461 | /// opens (`opens == 1`), and also when the cut was recorded but the |
| 462 | /// composition still refuses (`repaired == 1, opens == 0`: the caller |
| 463 | /// must report it). `Err` when nothing was repaired. |
| 464 | pub fn recover( |
| 465 | dir: &Path, |
| 466 | root_kind: RootKind, |
| 467 | root: &[u8], |
| 468 | ) -> Result<Repair, ComposeError> { |
| 469 | layout()?; |
| 470 | use std::os::unix::ffi::OsStrExt; |
| 471 | let d = CString::new(dir.as_os_str().as_bytes()) |
| 472 | .map_err(|_| ComposeError::Arg("dir holds a NUL byte".into()))?; |
| 473 | let mut r = Repair::default(); |
| 474 | // SAFETY: valid C string, root slice, out pointer. |
| 475 | let rc = unsafe { |
| 476 | ffi::rxc_host_recover( |
| 477 | d.as_ptr(), |
| 478 | root_kind as u32, |
| 479 | root.as_ptr(), |
| 480 | root.len(), |
| 481 | &mut r, |
| 482 | ) |
| 483 | }; |
| 484 | if rc == ffi::RXC_HOST_OK || (rc == ffi::RXC_HOST_E_REPLAY && r.repaired == 1) { |
| 485 | return Ok(r); |
| 486 | } |
| 487 | Err(ComposeError::Code { |
| 488 | code: rc, |
| 489 | detail: r.open_rc, |
| 490 | }) |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | impl Drop for Compose { |
| 495 | fn drop(&mut self) { |
| 496 | // SAFETY: close stops every World worker before returning, so no |
| 497 | // callback runs after this; the boxes drop afterwards. |
| 498 | unsafe { ffi::rxc_host_close(self.h) }; |
| 499 | self.h = std::ptr::null_mut(); |
| 500 | } |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | #[cfg(not(has_omega_compose))] |
| 505 | impl Compose { |
| 506 | pub fn open(_: &Path, _: RootKind, _: &[u8], _: u64) -> Result<(Self, Info), ComposeError> { |
| 507 | Err(ComposeError::Unavailable) |
| 508 | } |
| 509 | pub fn register_skill<F>( |
| 510 | &mut self, |
| 511 | _: &str, |
| 512 | _: Option<[u8; 32]>, |
| 513 | _: u64, |
| 514 | _: F, |
| 515 | ) -> Result<u32, ComposeError> |
| 516 | where |
| 517 | F: Fn(u64) -> Option<u64> + Send + Sync + 'static, |
| 518 | { |
| 519 | Err(ComposeError::Unavailable) |
| 520 | } |
| 521 | pub fn set_verify<F>(&mut self, _: F) -> Result<(), ComposeError> |
| 522 | where |
| 523 | F: Fn(u64, u64) -> bool + Send + Sync + 'static, |
| 524 | { |
| 525 | Err(ComposeError::Unavailable) |
| 526 | } |
| 527 | pub fn set_wait_ms(&mut self, _: u32) -> Result<(), ComposeError> { |
| 528 | Err(ComposeError::Unavailable) |
| 529 | } |
| 530 | pub fn run(&mut self, _: u64, _: u64) -> Result<RunResult, ComposeError> { |
| 531 | Err(ComposeError::Unavailable) |
| 532 | } |
| 533 | pub fn recall(&mut self, _: u64, _: u32) -> Result<(Vec<Record>, u64), ComposeError> { |
| 534 | Err(ComposeError::Unavailable) |
| 535 | } |
| 536 | pub fn record(&mut self, _: u64) -> Result<Record, ComposeError> { |
| 537 | Err(ComposeError::Unavailable) |
| 538 | } |
| 539 | pub fn payload(&mut self, _: u64) -> Result<Vec<u64>, ComposeError> { |
| 540 | Err(ComposeError::Unavailable) |
| 541 | } |
| 542 | pub fn info(&mut self) -> Result<Info, ComposeError> { |
| 543 | Err(ComposeError::Unavailable) |
| 544 | } |
| 545 | pub fn note(&mut self, _: NoteKind, _: [u64; 4], _: &[u8]) -> Result<u64, ComposeError> { |
| 546 | Err(ComposeError::Unavailable) |
| 547 | } |
| 548 | pub fn recover(_: &Path, _: RootKind, _: &[u8]) -> Result<Repair, ComposeError> { |
| 549 | Err(ComposeError::Unavailable) |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | #[cfg(test)] |
| 554 | mod tests { |
| 555 | use super::*; |
| 556 | |
| 557 | #[test] |
| 558 | fn stub_or_linked_is_reported() { |
| 559 | let dir = |
| 560 | std::env::temp_dir().join(format!("aien-omega-compose-unit-{}", std::process::id())); |
| 561 | let r = Compose::open(&dir, RootKind::Provisioned, b"unit", 1); |
| 562 | match &r { |
| 563 | Ok(_) => { |
| 564 | if !LINKED { |
| 565 | panic!("stub build opened a home"); |
| 566 | } |
| 567 | } |
| 568 | Err(e) => { |
| 569 | if LINKED { |
| 570 | panic!("linked build failed to open: {e}"); |
| 571 | } |
| 572 | assert_eq!(*e, ComposeError::Unavailable); |
| 573 | } |
| 574 | } |
| 575 | drop(r); |
| 576 | let _ = std::fs::remove_dir_all(&dir); |
| 577 | } |
| 578 | } |