diff --git a/crates/deploy/src/facts.rs b/crates/deploy/src/facts.rs index da82955e..a4c262ae 100644 --- a/crates/deploy/src/facts.rs +++ b/crates/deploy/src/facts.rs @@ -329,8 +329,12 @@ mod windows { library_dirs.push(binary_dir.clone()); } // Where `scripts/windows/fetch-cublas.ps1` puts the two cuBLAS DLLs. - if let Some(local) = std::env::var_os("LOCALAPPDATA") { - library_dirs.push(PathBuf::from(local).join("leleka").join("cublas").join("bin")); + // LOCALAPPDATA is not set in every kind of terminal or service session: fall back to the profile folder. + let local = std::env::var_os("LOCALAPPDATA") + .map(PathBuf::from) + .or_else(|| std::env::var_os("USERPROFILE").map(|home| PathBuf::from(home).join("AppData").join("Local"))); + if let Some(local) = local { + library_dirs.push(local.join("leleka").join("cublas").join("bin")); } if let Some(paths) = std::env::var_os("PATH") { library_dirs.extend(std::env::split_paths(&paths).filter(|p| !p.as_os_str().is_empty())); diff --git a/crates/deploy/src/resources.rs b/crates/deploy/src/resources.rs index 844cd87d..747fbe94 100644 --- a/crates/deploy/src/resources.rs +++ b/crates/deploy/src/resources.rs @@ -32,6 +32,10 @@ const ENGINE_EXTRA_BYTES: u64 = 3 * GIB; /// (53.54 GiB, the fixed footprint the engine's own budget reported in the live restart check plat-reconf-live; while it runs the 121 GiB server has only /// 12-13 GiB available besides the 51.5 GiB sealed archive). It is what makes a higher free-RAM target unreachable on that host. const ENGINE_FIXED_RESIDENT_BYTES: u64 = 54 * GIB; +/// RAM a Windows desktop session keeps for itself (browser, IDE, Docker Desktop) when the engine shares the machine. +const WINDOWS_DESKTOP_RESERVE_BYTES: u64 = 12 * GIB; +/// Host memory of the file-backed engine besides its page-locked expert complement (pinned K/V, rings, pools, mapped working set; 11 GiB measured natively). +const MAPPED_ENGINE_FIXED_BYTES: u64 = 11 * GIB; /// The memory governor keeps this much beyond its soft floor (`memory.reserve_mib`, 2 GiB in the server profile). const DEFAULT_RESERVE_BYTES: u64 = 2 * GIB; /// Page-locked share of the routed experts in the mapped form never needs to exceed the bank itself. @@ -82,6 +86,9 @@ pub struct ResourcePlan { /// Host RAM the memory governor keeps available: (soft, hard) floors in MiB, `None` = engine defaults. pub ram_floors_mib: Option<(u64, u64)>, pub ram_expected_free_after_bytes: Option, + /// Windows file-backed form: GiB of experts the engine may page-lock, sized from the free RAM so that the desktop keeps its own working set + /// (without it the engine takes nearly all free RAM; live native run 2026-10-25: 31 GiB resident, 14 GiB of swap, a refused start). + pub resident_budget_gib: Option, /// Host RAM cannot hold the sealed archive and the experts next to the free target: serve from SSD/HDD instead /// (archive as a read-only file, experts memory-mapped, this many GiB of them page-locked). pub mapped_resident_gib: Option, @@ -125,6 +132,7 @@ pub fn compute(facts: &Facts, gpu: Option<&Gpu>, profile: &Profile, preset: Pres ram_floors_mib: None, ram_expected_free_after_bytes: None, mapped_resident_gib: None, + resident_budget_gib: None, problems: Vec::new(), warnings: Vec::new(), }; @@ -201,6 +209,21 @@ pub fn compute(facts: &Facts, gpu: Option<&Gpu>, profile: &Profile, preset: Pres } } } + if facts.os == "windows" + && profile.kar_source == KarSource::File + && profile.expert_source == ExpertSource::Mapped + && profile.memory.resident_budget_gib.is_none() + && ram.total > 0 + { + let room = ram.available.saturating_sub(WINDOWS_DESKTOP_RESERVE_BYTES + MAPPED_ENGINE_FIXED_BYTES) / GIB; + plan.resident_budget_gib = Some(room.min(24) as u32); + plan.warnings.push(format!( + "Windows: the engine may page-lock {room} GiB of experts ({} available, {} left to the desktop, {} for the engine's own pools); close heavy applications for a larger share", + gib_text(ram.available), + gib_text(WINDOWS_DESKTOP_RESERVE_BYTES), + gib_text(MAPPED_ENGINE_FIXED_BYTES) + )); + } plan } @@ -230,6 +253,9 @@ pub fn apply( profile.min_ram_total_bytes = profile.min_ram_total_bytes.min(16 * GIB); profile.min_ram_available_bytes = profile.min_ram_available_bytes.min(MAPPED_MIN_AVAILABLE_BYTES); } + if let Some(resident) = plan.resident_budget_gib { + profile.memory.resident_budget_gib.get_or_insert(resident); + } if let Some(workers) = plan.cpu_workers && !profile.engine_env.contains_key(CPU_WORKERS_ENV) { @@ -406,6 +432,27 @@ mod tests { assert!(ample.warnings.is_empty(), "{:?}", ample.warnings); } + #[test] + fn the_windows_file_backed_form_sizes_its_page_locked_experts_from_the_free_ram() { + let mut windows = host(16, 48, 34); + windows.os = "windows".into(); + let plan = compute(&windows, Some(&gpu(16384, 16175)), &laptop(), Preset::Dedicated); + assert_eq!(plan.resident_budget_gib, Some(11), "34 GiB free - 12 for the desktop - 11 for the engine's pools"); + let mut profile = laptop(); + let mut env = BTreeMap::new(); + apply(&plan, &mut profile, &mut env, false); + assert_eq!(profile.memory.resident_budget_gib, Some(11)); + // an explicit profile value wins, and Linux hosts are left alone + let mut explicit = laptop(); + explicit.memory.resident_budget_gib = Some(16); + assert_eq!(compute(&windows, None, &explicit, Preset::Dedicated).resident_budget_gib, None); + assert_eq!(compute(&host(16, 48, 34), None, &laptop(), Preset::Dedicated).resident_budget_gib, None); + // a squeezed machine gets zero, not an underflow + let mut squeezed = host(16, 48, 6); + squeezed.os = "windows".into(); + assert_eq!(compute(&squeezed, None, &laptop(), Preset::Dedicated).resident_budget_gib, Some(0)); + } + #[test] fn presets_parse_and_default_to_dedicated() { assert_eq!(Preset::default(), Preset::Dedicated); diff --git a/crates/leleka-runtime/src/backend_original_serve.rs b/crates/leleka-runtime/src/backend_original_serve.rs index f7c45ac6..be286a97 100644 --- a/crates/leleka-runtime/src/backend_original_serve.rs +++ b/crates/leleka-runtime/src/backend_original_serve.rs @@ -352,6 +352,9 @@ mod runner { pub(in super::super) options: &'a OriginalServeOptions, } + /// Host memory a reconfigure needs beside the soft floor: the reopened stack's pinned K/V (3.1 GiB at 262,144 cells), rings and pools. + const RECONFIGURE_HEADROOM_BYTES: u64 = 4 << 30; + /// The serve stack's configuration for these settings (everything except the weights, which stay loaded). fn stack_config(options: &OriginalServeOptions, context_tokens: u32) -> Result { let mut config = StackConfig::new(i64::from(context_tokens)); @@ -721,6 +724,23 @@ mod runner { return Ok(crate::serve_tuning::describe(&options, context_tokens, workers, None).to_string()); } use std::io::Write; + // The old stack is closed before the new one opens, so the new one's pinned host memory (K/V, rings, pools) must fit in + // what is free now. Refuse up front on a starved host instead of leaving the engine without a stack (live native run 2026-10-25). + if let Some(governor) = leleka_memory::Governor::global() { + let need = governor.policy().soft_floor.saturating_add(RECONFIGURE_HEADROOM_BYTES); + if let Some(available) = governor.probe().effective_available().filter(|available| *available < need) { + return Err(BackendError { + code: ErrorCode::ResourceExhausted, + message: format!( + "reconfigure refused: only {} MiB of host memory are available and reopening the serve stack needs {} MiB (the soft floor plus {} MiB); free memory first or restart with `leleka up`; nothing was changed", + available >> 20, + need >> 20, + RECONFIGURE_HEADROOM_BYTES >> 20 + ), + retryable: false, + }); + } + } let _ = writeln!(std::io::stderr().lock(), "leleka-serve reconfigure: {changed:?}; reopening the serve stack"); let started = Instant::now(); shared.ready.store(false, Ordering::Release); @@ -755,6 +775,13 @@ mod runner { } Err(_) => { shared.shutdown.store(true, Ordering::Release); + // No stack is left and none can be built here: end the process a moment after the reply goes out so that the + // supervisor starts a fresh engine instead of keeping a half-dead one. + let _ = writeln!(std::io::stderr().lock(), "leleka-serve: reconfigure and rollback both failed; exiting so that the supervisor restarts the engine"); + thread::spawn(|| { + thread::sleep(Duration::from_secs(2)); + std::process::exit(70); + }); return Err(unavailable("reconfigure failed and the previous settings could not be restored")); } }