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 / tools / aien-test / src / sandbox.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 sandbox seam (contract C8 of the local verification pipeline). |
| 17 | //! |
| 18 | //! `--sandbox none` (the default) runs a gate's program directly, exactly as |
| 19 | //! before. `--sandbox user` runs it through the privileged exec helper |
| 20 | //! `/usr/local/sbin/aien-run-exec`, which drops to the unprivileged |
| 21 | //! `aien-run` user and applies the pool's profile: |
| 22 | //! |
| 23 | //! ```text |
| 24 | //! sudo -n /usr/local/sbin/aien-run-exec --cwd CWD --timeout SECS \ |
| 25 | //! --profile host|qemu|gb10 [--env K=V]... -- PROGRAM ARGS... |
| 26 | //! ``` |
| 27 | //! |
| 28 | //! Only the environment pairs the gate needs are passed: the manifest grammar |
| 29 | //! has no `env` key today, so that set is empty, plus `CARGO_TARGET_DIR` when |
| 30 | //! the runner itself was given one. The helper is INFRA's; this module only |
| 31 | //! builds the command line and never checks that the helper exists (a |
| 32 | //! missing helper fails the spawn, which the receipt records as rc_nonzero |
| 33 | //! with the error on stderr, never as PASS). |
| 34 | |
| 35 | use crate::process::RunSpec; |
| 36 | use std::path::{Path, PathBuf}; |
| 37 | |
| 38 | pub const EXEC_HELPER: &str = "/usr/local/sbin/aien-run-exec"; |
| 39 | pub const SUDO: &str = "sudo"; |
| 40 | /// The PATH the exec helper sets for the `aien-run` user (`RUN_PATH` in |
| 41 | /// `aien-run-exec`). Bare `run.exec` names resolve here under `--sandbox user`. |
| 42 | pub const TRUSTED_PATH: &str = "/var/lib/aien-run/cargo/bin:/usr/local/bin:/usr/bin:/bin"; |
| 43 | |
| 44 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] |
| 45 | pub enum Sandbox { |
| 46 | /// Run the program directly (every earlier behaviour unchanged). |
| 47 | #[default] |
| 48 | None, |
| 49 | /// Run it as the `aien-run` user through the exec helper. |
| 50 | User, |
| 51 | } |
| 52 | |
| 53 | impl Sandbox { |
| 54 | pub fn parse(s: &str) -> Result<Sandbox, String> { |
| 55 | match s { |
| 56 | "none" => Ok(Sandbox::None), |
| 57 | "user" => Ok(Sandbox::User), |
| 58 | other => Err(format!("--sandbox must be none or user, not {other:?}")), |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | pub fn as_str(self) -> &'static str { |
| 63 | match self { |
| 64 | Sandbox::None => "none", |
| 65 | Sandbox::User => "user", |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | /// Set to `1` in every program the user sandbox runs (`--env` to the helper). |
| 71 | /// Tests that start a sandbox themselves skip when they see it: aien-run |
| 72 | /// cannot call the helper or write `/var/lib/aien-run/wt`. |
| 73 | pub const NESTED_MARKER: &str = "AIEN_SANDBOX_NESTED"; |
| 74 | |
| 75 | /// The program and arguments to spawn for `program ARGS` under `sandbox`. |
| 76 | /// `profile` is the gate's pool name; `env` the `K=V` pairs to pass through. |
| 77 | pub fn command( |
| 78 | sandbox: Sandbox, |
| 79 | program: &Path, |
| 80 | args: &[String], |
| 81 | cwd: &Path, |
| 82 | timeout_secs: u64, |
| 83 | profile: &str, |
| 84 | env: &[(String, String)], |
| 85 | ) -> (PathBuf, Vec<String>) { |
| 86 | match sandbox { |
| 87 | Sandbox::None => (program.to_path_buf(), args.to_vec()), |
| 88 | Sandbox::User => { |
| 89 | let mut v: Vec<String> = vec![ |
| 90 | "-n".to_string(), |
| 91 | EXEC_HELPER.to_string(), |
| 92 | "--cwd".to_string(), |
| 93 | cwd.display().to_string(), |
| 94 | "--timeout".to_string(), |
| 95 | timeout_secs.to_string(), |
| 96 | "--profile".to_string(), |
| 97 | profile.to_string(), |
| 98 | ]; |
| 99 | // Always set: the program knows it runs inside the sandbox, so self-tests |
| 100 | // that would need to start a sandbox themselves can skip (they cannot nest). |
| 101 | v.push("--env".to_string()); |
| 102 | v.push(format!("{NESTED_MARKER}=1")); |
| 103 | for (k, val) in env { |
| 104 | v.push("--env".to_string()); |
| 105 | v.push(format!("{k}={val}")); |
| 106 | } |
| 107 | v.push("--".to_string()); |
| 108 | v.push(program.display().to_string()); |
| 109 | v.extend(args.iter().cloned()); |
| 110 | (PathBuf::from(SUDO), v) |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | /// Rewrite a direct `RunSpec` for the sandbox. `None` returns it unchanged, |
| 116 | /// byte for byte. The timeout policy (`enforce_timeout`) is not touched: a |
| 117 | /// gb10 job stays advisory and is never killed. |
| 118 | pub fn wrap(sandbox: Sandbox, spec: RunSpec, profile: &str, env: &[(String, String)]) -> RunSpec { |
| 119 | if sandbox == Sandbox::None { |
| 120 | return spec; |
| 121 | } |
| 122 | let secs = spec.timeout.as_secs().max(1); |
| 123 | // The runner's own pairs on the spec go through the helper too: sudo |
| 124 | // would drop them from the environment. |
| 125 | let mut pairs: Vec<(String, String)> = env.to_vec(); |
| 126 | pairs.extend(spec.env.iter().cloned()); |
| 127 | let (program, args) = command( |
| 128 | sandbox, |
| 129 | &spec.program, |
| 130 | &spec.args, |
| 131 | &spec.cwd, |
| 132 | secs, |
| 133 | profile, |
| 134 | &pairs, |
| 135 | ); |
| 136 | RunSpec { |
| 137 | program, |
| 138 | args, |
| 139 | env: Vec::new(), |
| 140 | ..spec |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | /// The pairs the runner passes through: only `CARGO_TARGET_DIR` when set. |
| 145 | /// The manifest grammar has no `env` key, so nothing comes from the gate. |
| 146 | pub fn env_pairs(cargo_target_dir: Option<&Path>) -> Vec<(String, String)> { |
| 147 | let mut v = Vec::new(); |
| 148 | if let Some(d) = cargo_target_dir { |
| 149 | v.push(("CARGO_TARGET_DIR".to_string(), d.display().to_string())); |
| 150 | } |
| 151 | v |
| 152 | } |
| 153 | |
| 154 | #[cfg(test)] |
| 155 | mod tests { |
| 156 | use super::*; |
| 157 | use std::time::Duration; |
| 158 | |
| 159 | fn spec() -> RunSpec { |
| 160 | RunSpec { |
| 161 | program: PathBuf::from("/repo/tools/run.sh"), |
| 162 | args: vec!["--fast".into(), "x y".into()], |
| 163 | cwd: PathBuf::from("/repo"), |
| 164 | timeout: Duration::from_millis(90_500), |
| 165 | kill_grace: Duration::from_secs(10), |
| 166 | enforce_timeout: false, |
| 167 | env: Vec::new(), |
| 168 | env_remove: Vec::new(), |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | #[test] |
| 173 | fn none_is_the_identity() { |
| 174 | let s = spec(); |
| 175 | let w = wrap( |
| 176 | Sandbox::None, |
| 177 | spec(), |
| 178 | "gb10", |
| 179 | &[("CARGO_TARGET_DIR".into(), "/t".into())], |
| 180 | ); |
| 181 | assert_eq!(w.program, s.program); |
| 182 | assert_eq!(w.args, s.args); |
| 183 | assert_eq!(w.cwd, s.cwd); |
| 184 | assert_eq!(w.timeout, s.timeout); |
| 185 | assert!(!w.enforce_timeout); |
| 186 | let (p, a) = command(Sandbox::None, &s.program, &s.args, &s.cwd, 5, "host", &[]); |
| 187 | assert_eq!(p, s.program); |
| 188 | assert_eq!(a, s.args); |
| 189 | } |
| 190 | |
| 191 | #[test] |
| 192 | fn user_forwards_the_spec_env_to_the_helper() { |
| 193 | // The runner's own pairs (the GPU lock marker) ride on the spec and reach |
| 194 | // the sandboxed program through `--env`, after the nested marker. |
| 195 | let mut s = spec(); |
| 196 | s.env = vec![("AIEN_GB10_LOCK_HELD".to_string(), "1".to_string())]; |
| 197 | let w = wrap(Sandbox::User, s, "gb10", &[]); |
| 198 | let i = w |
| 199 | .args |
| 200 | .iter() |
| 201 | .position(|a| a == "AIEN_GB10_LOCK_HELD=1") |
| 202 | .expect("marker is passed as --env"); |
| 203 | assert_eq!(w.args[i - 1], "--env"); |
| 204 | assert!( |
| 205 | w.env.is_empty(), |
| 206 | "sudo would drop it; the helper carries it" |
| 207 | ); |
| 208 | } |
| 209 | |
| 210 | #[test] |
| 211 | fn user_builds_the_c8_command_line() { |
| 212 | let env = vec![("CARGO_TARGET_DIR".to_string(), "/tmp/t".to_string())]; |
| 213 | let w = wrap(Sandbox::User, spec(), "qemu", &env); |
| 214 | assert_eq!(w.program, PathBuf::from("sudo")); |
| 215 | assert_eq!( |
| 216 | w.args, |
| 217 | [ |
| 218 | "-n", |
| 219 | EXEC_HELPER, |
| 220 | "--cwd", |
| 221 | "/repo", |
| 222 | "--timeout", |
| 223 | "90", |
| 224 | "--profile", |
| 225 | "qemu", |
| 226 | "--env", |
| 227 | "AIEN_SANDBOX_NESTED=1", |
| 228 | "--env", |
| 229 | "CARGO_TARGET_DIR=/tmp/t", |
| 230 | "--", |
| 231 | "/repo/tools/run.sh", |
| 232 | "--fast", |
| 233 | "x y", |
| 234 | ] |
| 235 | ); |
| 236 | // The timeout policy is untouched: gb10 stays advisory. |
| 237 | assert!(!w.enforce_timeout); |
| 238 | assert_eq!(w.timeout, Duration::from_millis(90_500)); |
| 239 | assert_eq!(w.cwd, PathBuf::from("/repo")); |
| 240 | } |
| 241 | |
| 242 | #[test] |
| 243 | fn no_env_means_only_the_nested_marker_and_a_tiny_timeout_rounds_up_to_one() { |
| 244 | let mut s = spec(); |
| 245 | s.timeout = Duration::from_millis(10); |
| 246 | let w = wrap(Sandbox::User, s, "host", &[]); |
| 247 | let envs: Vec<&String> = w |
| 248 | .args |
| 249 | .windows(2) |
| 250 | .filter(|p| p[0] == "--env") |
| 251 | .map(|p| &p[1]) |
| 252 | .collect(); |
| 253 | assert_eq!(envs, vec!["AIEN_SANDBOX_NESTED=1"]); |
| 254 | let pos = w.args.iter().position(|a| a == "--timeout").unwrap(); |
| 255 | assert_eq!(w.args[pos + 1], "1"); |
| 256 | assert_eq!(env_pairs(None), Vec::<(String, String)>::new()); |
| 257 | assert_eq!( |
| 258 | env_pairs(Some(Path::new("/x"))), |
| 259 | vec![("CARGO_TARGET_DIR".to_string(), "/x".to_string())] |
| 260 | ); |
| 261 | } |
| 262 | |
| 263 | #[test] |
| 264 | fn parse_and_names() { |
| 265 | assert_eq!(Sandbox::parse("none").unwrap(), Sandbox::None); |
| 266 | assert_eq!(Sandbox::parse("user").unwrap(), Sandbox::User); |
| 267 | assert!(Sandbox::parse("root").is_err()); |
| 268 | assert_eq!(Sandbox::default(), Sandbox::None); |
| 269 | assert_eq!(Sandbox::User.as_str(), "user"); |
| 270 | } |
| 271 | } |