Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aien-sovereign-core-mv/patches/19e8c43cfbf81ea5 # 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 Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers ## GitHub aien-dev/aien-sovereign-core-mv#256 (code only) closed derived `19e8c43cfbf81ea5` #256 Imported from GitHub aien-dev/aien-sovereign-core-mv#256 (code only) GitHub's metadata for this pull request is unavailable: its state is DERIVED from the commits (merged: the head is reachable from this project's main; open: GitHub kept a merge ref; else closed). Base `27b2e165316a3db44d27934bf0866758577933d1` · Head `8de45dbad16a0626f310c220dd0bf063678101ad` ### Apply it ``` git checkout -b patch-19e8c43cfbf81ea5 27b2e165316a3db44d27934bf0866758577933d1 curl -fsSL https://interplane.io/drakestapleton/aien-sovereign-core-mv/patches/19e8c43cfbf81ea5.patch | git am ``` Raw patch (mbox) · Diff ### Provenance This proposal is imported from GitHub; it is not signed by its author. Its import statement and source record: ``` github-import 1 aien-dev/aien-sovereign-core-mv 256 8de45dbad16a0626f310c220dd0bf063678101ad ``` ``` {"kind":"github-import","owner":"aien-dev","repo":"aien-sovereign-core-mv","number":256,"url":"https://github.com/aien-dev/aien-sovereign-core-mv/pull/256","export_sha256":"967c79e26a893657b5cdaa45efd442be5dbd2ecfe68e25f9a8c1b5da4d60d452","imported_utc":"2026-11-18T16:36:32Z","metadata":"unavailable","state_derived":true} ``` ### Changes ``` diff --git a/.crumb b/.crumb index c58a5a29..673aa8e5 100644 --- a/.crumb +++ b/.crumb @@ -77,14 +77,14 @@ } ], "children_root": "sha256:009593670dc7accc2b442a6a8c6f3b848ab73de4d6e61df57401bf4bf00b294a", - "compiled_at": "2026-10-07T00:27:30Z", + "compiled_at": "2026-10-07T01:20:29Z", "compiler": "crumb-compile/1", - "digest": "sha256:a5f75b49a58c12a8c16c1be0988184ec747c241692d263b54a8b58e8228b29d9", + "digest": "sha256:71dff059f116e36be75c6abaa600ed11efdd46aff66899d493fb1e9b0bffa676", "evidence_files": 90, "evidence_root": "sha256:913405b594470a9dfda6998ea0386eba372f438bce4e9d05c062369a9f7b1e22", "files": 971, - "generated_at_commit": "80024ad93cecfdd7495f9bfbc634fdb313e7e6f7", - "source_tree": "sha256:3dcf81761d327caf4322388e5088f49692506236adb4a10690285d9660443293" + "generated_at_commit": "27b2e165316a3db44d27934bf0866758577933d1", + "source_tree": "sha256:5f2d2d80e1f97f643af4d3038c7904388ece361f948aacbae75fe1992e1d4890" } }, "history": [], diff --git a/crates/aien-cli/.crumb b/crates/aien-cli/.crumb index b69187b4..a394907c 100644 --- a/crates/aien-cli/.crumb +++ b/crates/aien-cli/.crumb @@ -34,21 +34,21 @@ "generated": { "children": [ { - "digest": "a2a6a6d1d40496ec8caadef540d3ddfa3783b1a8c86bce80ddc18b4205331d9f", - "last_commit": "9a8298c", + "digest": "58d4b12884ad334297e89d1f6f8b54c819d2e8052f817c663efc2a18f9f7ca40", + "last_commit": "2e8cef9", "name": "src", "purpose": "" } ], - "children_root": "sha256:236d1ee979d722f3fca5ff35301e27e05835c7b9caa177cbea0d5f77e4f0a19c", - "compiled_at": "2026-10-07T00:19:21Z", + "children_root": "sha256:7e0badc2664cee1f72c3d411750f2a790da389780abfced7d864c8d24bee7b84", + "compiled_at": "2026-10-07T01:20:29Z", "compiler": "crumb-compile/1", - "digest": "sha256:79d6c058a6cae2ef1e180918b84499fae8dc51508c175e4ae149955123c1b89f", + "digest": "sha256:a4177f17aba850e2784044f3173943420b001467135ba3dcbddea4e80362e161", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 28, - "generated_at_commit": "de17cfae614885230fa033c86f6e17ed4e4e34dd", - "source_tree": "sha256:7c492cb18b9d18212a5e8c8b0665e533098624c80ffb4ce04a2de098d7117746" + "generated_at_commit": "27b2e165316a3db44d27934bf0866758577933d1", + "source_tree": "sha256:dcbe92fd5d69cc80b76ffd7a05e43950545e801027e5dabc1a9d6400d69ae609" } }, "history": [ diff --git a/crates/aien-cli/src/.crumb b/crates/aien-cli/src/.crumb index 7c5cfa18..edfe2f80 100644 --- a/crates/aien-cli/src/.crumb +++ b/crates/aien-cli/src/.crumb @@ -64,14 +64,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-07T00:19:21Z", + "compiled_at": "2026-10-07T01:20:29Z", "compiler": "crumb-compile/1", - "digest": "sha256:a2a6a6d1d40496ec8caadef540d3ddfa3783b1a8c86bce80ddc18b4205331d9f", + "digest": "sha256:58d4b12884ad334297e89d1f6f8b54c819d2e8052f817c663efc2a18f9f7ca40", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 25, - "generated_at_commit": "de17cfae614885230fa033c86f6e17ed4e4e34dd", - "source_tree": "sha256:0aa9afadef9bbdb28be1b22b893d53a45ce8d0d0544228cb698121b76da20365" + "generated_at_commit": "27b2e165316a3db44d27934bf0866758577933d1", + "source_tree": "sha256:da17730a7f9658b0f8009858396d5633ee85738b95ad24ba88c95ddf89591230" } }, "history": [ diff --git a/crates/aien-cli/src/commands.rs b/crates/aien-cli/src/commands.rs index f3639fd2..c1351ac6 100644 --- a/crates/aien-cli/src/commands.rs +++ b/crates/aien-cli/src/commands.rs @@ -1452,7 +1452,8 @@ pub async fn run_daemon_server() { tensor_backend, sched_cfg, 4096, - &aien_runtime::shared_kv::read_mem_available, + &aien_runtime::shared_kv::read_mem_available_checked, + aien_runtime::shared_kv::allow_unchecked_memory_from_env(), ) { Ok(parts) => parts, Err(fatal) => { @@ -1467,6 +1468,7 @@ pub async fn run_daemon_server() { if let Err(fatal) = aien_inference_abi::open_gpu_session_with_retry( aien_inference_abi::GPU_SESSION_OPEN_ATTEMPTS, aien_inference_abi::GPU_SESSION_RETRY_DELAY, + aien_inference_abi::GPU_SESSION_OPEN_DEADLINE, &aien_runtime::shared_kv::read_mem_available, ) { eprintln!("Fatal: {}", fatal.red().bold()); diff --git a/crates/aien-inference-abi/src/lib.rs b/crates/aien-inference-abi/src/lib.rs index c5d643a4..2f3c75ae 100644 --- a/crates/aien-inference-abi/src/lib.rs +++ b/crates/aien-inference-abi/src/lib.rs @@ -33,8 +33,9 @@ pub use model_dir::{load_model_config, model_config_from_hf_json, model_id_for_d pub use moe_plan::*; pub use mojo_backend::*; pub use omega_backend::{ - omega_backend_selected, open_gpu_session_with_retry, retry_bounded, OmegaGb10Backend, - SessionAttemptError, GPU_BACKEND_ENV, GPU_SESSION_OPEN_ATTEMPTS, GPU_SESSION_RETRY_DELAY, + omega_backend_selected, open_gpu_session_with_retry, retry_bounded, retry_bounded_within, + OmegaGb10Backend, SessionAttemptError, GPU_BACKEND_ENV, GPU_SESSION_OPEN_ATTEMPTS, + GPU_SESSION_OPEN_DEADLINE, GPU_SESSION_RETRY_DELAY, }; pub use qwen3_coder::*; pub use qwen3_moe::*; diff --git a/crates/aien-inference-abi/src/omega_backend.rs b/crates/aien-inference-abi/src/omega_backend.rs index 10da47ac..b743f9bd 100644 --- a/crates/aien-inference-abi/src/omega_backend.rs +++ b/crates/aien-inference-abi/src/omega_backend.rs @@ -765,6 +765,14 @@ pub const GPU_SESSION_OPEN_ATTEMPTS: u32 = 3; /// Pause between two session-open attempts. pub const GPU_SESSION_RETRY_DELAY: std::time::Duration = std::time::Duration::from_millis(500); +/// Wall-clock budget for the whole GPU session open (probe plus retries). It is checked +/// only BETWEEN attempts: no new attempt starts once it has passed. An attempt already +/// running is never interrupted, aborted or killed (standing rule: a GPU job is not +/// killed mid-launch), so a single probe that hangs forever still blocks startup; the +/// bound guarantees only that a slow-failing sequence of attempts cannot go on past +/// the deadline plus one attempt. +pub const GPU_SESSION_OPEN_DEADLINE: std::time::Duration = std::time::Duration::from_secs(30); + /// Why one session-open attempt failed. #[derive(Debug, Clone, PartialEq, Eq)] pub struct SessionAttemptError { @@ -781,6 +789,31 @@ pub struct SessionAttemptError { /// latched failure stops at once. Never loops past `attempts`. pub fn retry_bounded( attempts: u32, + attempt: impl FnMut(u32) -> Result, + on_failure: impl FnMut(u32, u32, &SessionAttemptError), + pause: impl FnMut(u32), +) -> Result<(T, u32), String> { + retry_bounded_within( + attempts, + None, + || std::time::Duration::ZERO, + attempt, + on_failure, + pause, + ) +} + +/// [`retry_bounded`] with a wall-clock bound. `elapsed()` is the time since the whole +/// startup began (the caller's injected clock). `deadline` is checked only BETWEEN +/// attempts, after the pause: once `elapsed() >= deadline` no further attempt starts +/// and the error names the attempts made and the elapsed time. Attempt 1 always runs. +/// An attempt already in flight is never interrupted: `attempt` is called to +/// completion, because a GPU launch must not be cancelled or killed mid-way. So the +/// deadline is a bound on starting attempts, not a timeout on a single hung attempt. +pub fn retry_bounded_within( + attempts: u32, + deadline: Option, + mut elapsed: impl FnMut() -> std::time::Duration, mut attempt: impl FnMut(u32) -> Result, mut on_failure: impl FnMut(u32, u32, &SessionAttemptError), mut pause: impl FnMut(u32), @@ -788,6 +821,19 @@ pub fn retry_bounded( let attempts = attempts.max(1); let mut last = String::new(); for n in 1..=attempts { + if n > 1 { + if let Some(limit) = deadline { + let now = elapsed(); + if now >= limit { + return Err(format!( + "GPU session open timed out: deadline {} ms passed after {} of {attempts} attempts, elapsed {} ms (an attempt in flight is never interrupted; the deadline applies between attempts); last error: {last}", + limit.as_millis(), + n - 1, + now.as_millis() + )); + } + } + } match attempt(n) { Ok(v) => return Ok((v, n)), Err(e) => { @@ -827,10 +873,13 @@ fn fmt_mem(bytes: Option) -> String { /// `TensorBackend` accounting, so a failure here is a clear start-up refusal /// instead of the strict-fallback panic at warm-up (#236). Every attempt logs its /// number, the omega status and stage, and `MemAvailable` from `mem_available`. +/// `deadline` bounds the whole open between attempts only; see [`retry_bounded_within`] +/// (an in-flight probe is never interrupted). /// Returns the attempt number that opened the session. pub fn open_gpu_session_with_retry( attempts: u32, delay: std::time::Duration, + deadline: std::time::Duration, mem_available: &dyn Fn() -> Option, ) -> Result { if !aien_omega_gpu::is_native() { @@ -840,8 +889,11 @@ pub fn open_gpu_session_with_retry( } const PROBE_DIM: usize = 128; // omega rmsnorm needs dim % 128 == 0 let ones = [1.0f32; PROBE_DIM]; - let (_, n) = retry_bounded( + let started = std::time::Instant::now(); + let (_, n) = retry_bounded_within( attempts, + Some(deadline), + || started.elapsed(), |_| { let mut out = [0.0f32; PROBE_DIM]; aien_omega_gpu::rmsnorm_f32(1, PROBE_DIM, &ones, &ones, 1e-5, &mut out) @@ -1148,12 +1200,81 @@ mod tests { assert_eq!(zero_calls, 1); } + #[test] + fn deadline_passed_between_attempts_stops_with_a_timeout_error() { + // Injected clock: each attempt "takes" 400 ms; deadline 1000 ms. + let clock = std::cell::Cell::new(0u64); + let mut calls = 0; + let err = retry_bounded_within::<()>( + 10, + Some(std::time::Duration::from_millis(1000)), + || std::time::Duration::from_millis(clock.get()), + |n| { + calls += 1; + clock.set(clock.get() + 400); + Err(fail_msg(n, false)) + }, + |_, _, _| {}, + |_| {}, + ) + .unwrap_err(); + // Attempts at t=0, 400, 800 start; the check at t=1200 stops the fourth. + assert_eq!(calls, 3, "{err}"); + assert!(err.contains("timed out"), "{err}"); + assert!(err.contains("after 3 of 10 attempts"), "{err}"); + assert!(err.contains("elapsed 1200 ms"), "{err}"); + assert!(err.contains("never interrupted"), "{err}"); + } + + #[test] + fn attempts_stay_bounded_when_the_deadline_never_passes() { + let mut calls = 0; + let err = retry_bounded_within::<()>( + 3, + Some(std::time::Duration::from_secs(3600)), + || std::time::Duration::ZERO, + |n| { + calls += 1; + Err(fail_msg(n, false)) + }, + |_, _, _| {}, + |_| {}, + ) + .unwrap_err(); + assert_eq!(calls, 3); + assert!(err.contains("did not open after 3 attempts"), "{err}"); + assert!(!err.contains("timed out"), "{err}"); + } + + #[test] + fn first_attempt_runs_even_if_the_deadline_is_already_zero() { + let mut calls = 0; + let _ = retry_bounded_within::<()>( + 3, + Some(std::time::Duration::ZERO), + || std::time::Duration::from_secs(1), + |n| { + calls += 1; + Err(fail_msg(n, false)) + }, + |_, _, _| {}, + |_| {}, + ); + assert_eq!(calls, 1); + } + #[test] fn session_open_refuses_cleanly_in_a_stub_build() { if aien_omega_gpu::is_native() { return; // the native path is verified on the chip, not here } - let err = open_gpu_session_with_retry(3, std::time::Duration::ZERO, &|| None).unwrap_err(); + let err = open_gpu_session_with_retry( + 3, + std::time::Duration::ZERO, + GPU_SESSION_OPEN_DEADLINE, + &|| None, + ) + .unwrap_err(); assert!(err.contains("not linked"), "{err}"); } } diff --git a/crates/aien-runtime/src/shared_kv.rs b/crates/aien-runtime/src/shared_kv.rs index de5fa39d..f0fbbaae 100644 --- a/crates/aien-runtime/src/shared_kv.rs +++ b/crates/aien-runtime/src/shared_kv.rs @@ -216,41 +216,82 @@ pub fn read_mem_available() -> Option { .and_then(parse_mem_available) } +/// Why `MemAvailable` could not be established, as a plain message. +pub fn parse_mem_available_checked(meminfo: &str) -> Result { + parse_mem_available(meminfo) + .ok_or_else(|| "/proc/meminfo has no parsable `MemAvailable: kB` line".to_string()) +} + +/// `MemAvailable` of this host in bytes, or the reason it cannot be established +/// (`/proc/meminfo` unreadable, or no parsable `MemAvailable` line). +pub fn read_mem_available_checked() -> Result { + let text = std::fs::read_to_string("/proc/meminfo") + .map_err(|e| format!("/proc/meminfo cannot be read: {e}"))?; + parse_mem_available_checked(&text) +} + +/// Operator override (documented): set to `1` to let the daemon allocate the KV pool +/// although `MemAvailable` cannot be established. Without it the daemon refuses to +/// start (fail closed). Every use is logged loudly on stderr. +pub const ALLOW_UNCHECKED_MEMORY_ENV: &str = "AIEN_ALLOW_UNCHECKED_KV_MEMORY"; + +/// True only when [`ALLOW_UNCHECKED_MEMORY_ENV`] is exactly `1`. +pub fn allow_unchecked_memory_from_env() -> bool { + std::env::var(ALLOW_UNCHECKED_MEMORY_ENV).is_ok_and(|v| v == "1") +} + /// Refuses the pool when the host reports less available memory than it needs. -/// `available == None` (no `/proc/meminfo`) cannot be checked and passes; the -/// caller logs that the check was skipped. -pub fn check_kv_memory(requested: u64, available: Option) -> Result<(), String> { +/// `available` is `Err(reason)` when memory could not be established: that refuses +/// too (fail closed, #254) unless `allow_unchecked` is set, in which case the check +/// is bypassed with a loud stderr line. +pub fn check_kv_memory( + requested: u64, + available: Result, + allow_unchecked: bool, +) -> Result<(), String> { match available { - Some(avail) if requested > avail => Err(format!( + Ok(avail) if requested > avail => Err(format!( "KV pool needs {requested} bytes ({}) but MemAvailable is {avail} bytes ({}); refusing to allocate", fmt_gib(requested), fmt_gib(avail), )), - _ => Ok(()), + Ok(_) => Ok(()), + Err(reason) if allow_unchecked => { + eprintln!( + "WARNING: KV POOL MEMORY CHECK BYPASSED by {ALLOW_UNCHECKED_MEMORY_ENV}=1: {reason}; allocating {requested} bytes ({}) WITHOUT knowing MemAvailable", + fmt_gib(requested) + ); + Ok(()) + } + Err(reason) => Err(format!( + "KV pool memory cannot be checked: {reason}; refusing to allocate {requested} bytes ({}). Set {ALLOW_UNCHECKED_MEMORY_ENV}=1 to override (not recommended)", + fmt_gib(requested) + )), } } /// Daemon planning step (#239): plans the pool for `cfg`, logs the plan, and /// checks `MemAvailable` (read through `mem_available`) before anything is -/// allocated. Allocates nothing itself. +/// allocated. Allocates nothing itself. Fails closed when memory cannot be +/// established unless `allow_unchecked` (#254). pub fn plan_checked_model_kv( cfg: &aien_inference_abi::ModelConfig, - mem_available: &dyn Fn() -> Option, + mem_available: &dyn Fn() -> Result, + allow_unchecked: bool, ) -> Result { let plan = KvPoolPlan::for_model(cfg)?; println!(" {}", plan.log_line()); let available = mem_available(); - match available { - Some(avail) => println!( + if let Ok(avail) = &available { + println!( " KV pool memory check: needs {} bytes ({}), MemAvailable {} bytes ({})", plan.total_bytes, fmt_gib(plan.total_bytes as u64), avail, - fmt_gib(avail) - ), - None => println!(" KV pool memory check: skipped (no MemAvailable in /proc/meminfo)"), + fmt_gib(*avail) + ); } - check_kv_memory(plan.total_bytes as u64, available)?; + check_kv_memory(plan.total_bytes as u64, available, allow_unchecked)?; Ok(plan) } @@ -261,9 +302,10 @@ pub fn build_shared_kv_runtime_for_model( tensor_backend: Arc, scheduler_config: SchedulerConfig, arena_capacity: usize, - mem_available: &dyn Fn() -> Option, + mem_available: &dyn Fn() -> Result, + allow_unchecked: bool, ) -> Result<(AienRuntimeSpine, NativeTransformerBackend, KvPoolPlan), String> { - let plan = plan_checked_model_kv(&weights.config, mem_available)?; + let plan = plan_checked_model_kv(&weights.config, mem_available, allow_unchecked)?; let (spine, backend) = build_shared_kv_runtime( weights, tensor_backend, @@ -411,12 +453,11 @@ mod tests { #[test] fn memory_check_refuses_with_requested_and_available_bytes() { - let err = check_kv_memory(3_221_225_472, Some(1_073_741_824)).unwrap_err(); + let err = check_kv_memory(3_221_225_472, Ok(1_073_741_824), false).unwrap_err(); assert!(err.contains("3221225472 bytes (3.00 GiB)"), "{err}"); assert!(err.contains("1073741824 bytes (1.00 GiB)"), "{err}"); assert!(err.contains("refusing"), "{err}"); - assert!(check_kv_memory(10, Some(10)).is_ok()); - assert!(check_kv_memory(u64::MAX, None).is_ok()); + assert!(check_kv_memory(10, Ok(10), false).is_ok()); } fn tiny_config() -> aien_inference_abi::ModelConfig { @@ -439,16 +480,57 @@ mod tests { } } + // ---- #254: fail closed when memory cannot be established ---- + + #[test] + fn unreadable_meminfo_refuses_the_pool() { + let cfg = tiny_config(); + let err = plan_checked_model_kv( + &cfg, + &|| Err("/proc/meminfo cannot be read: No such file".to_string()), + false, + ) + .unwrap_err(); + assert!(err.contains("cannot be checked"), "{err}"); + assert!(err.contains("refusing to allocate"), "{err}"); + assert!(err.contains(ALLOW_UNCHECKED_MEMORY_ENV), "{err}"); + } + + #[test] + fn unreadable_meminfo_with_override_proceeds() { + // The override path also prints a WARNING line on stderr (see check_kv_memory). + let cfg = tiny_config(); + let plan = plan_checked_model_kv(&cfg, &|| Err("unreadable".to_string()), true) + .expect("override set: proceeds"); + assert_eq!(plan.total_blocks, 3); + // The override never bypasses a known shortage. + assert!(check_kv_memory(10, Ok(1), true).is_err()); + } + + #[test] + fn unparsable_meminfo_refuses_the_pool() { + for text in [ + "MemTotal: 1 kB\n", + "MemAvailable: x kB\n", + "MemAvailable: 5\n", + "", + ] { + let cfg = tiny_config(); + let err = plan_checked_model_kv(&cfg, &|| parse_mem_available_checked(text), false) + .unwrap_err(); + assert!(err.contains("no parsable"), "{text:?}: {err}"); + } + } #[test] fn daemon_plan_refuses_before_allocating_and_sizes_the_manager() { // Exercises the planning, memory check and pooled manager without building a // spine: a spine creates a RuntimeController, which reads the process-wide // AIEN_RUNTIME_STATE_DIR that other tests in this crate point at temp dirs. let cfg = tiny_config(); - let err = plan_checked_model_kv(&cfg, &|| Some(1)).unwrap_err(); + let err = plan_checked_model_kv(&cfg, &|| Ok(1), false).unwrap_err(); assert!(err.contains("refusing"), "{err}"); - let plan = plan_checked_model_kv(&cfg, &|| None).expect("no meminfo: check skipped"); + let plan = plan_checked_model_kv(&cfg, &|| Ok(1 << 30), false).expect("enough memory"); // ceil(40 / 16) = 3 blocks of 16 tokens * (2 * 2 * 2 * 16 * 4) bytes per token. assert_eq!(plan.total_blocks, 3); assert_eq!(plan.total_bytes, 3 * 16 * 512); ```