@@ -1,12 +1,22 @@
1+use crate::instance::{VmLiveness, vm_liveness};
2+use crate::session_manager::INSTANCE_DIR_NAME;
3+use crate::{
4+ config::CONFIG_PATH_ENV,
5+ instance::{
6+ InstanceConfig, STATUS_VM_ERROR_PREFIX, build_ssh_login_actions, ensure_instance_dir,
7+ ensure_ssh_keypair, extract_ipv4, load_or_create_instance_config, write_instance_config,
8+ },
9+ session_manager::{GLOBAL_DIR_NAME, VM_MANAGER_PID_NAME, VM_MANAGER_SOCKET_NAME},
10+ vm::{self, DirectoryShare, LoginAction, PROJECT_GUEST_BASE, VmInput},
11+};
12+use anyhow::{Error, Result, anyhow, bail};
113use std::{
214 env, fs,
315 io::{Read, Write},
416 os::unix::{
5− fs::FileTypeExt,
617 fs::PermissionsExt,
718 io::AsRawFd,
819 net::{UnixListener, UnixStream},
9− process::CommandExt,
1020 },
1121 path::{Path, PathBuf},
1222 process::{Command, Stdio},
@@ -15,36 +25,44 @@ use std::{
1525 time::{Duration, Instant},
1626};
1727
18−use crate::{
19− config::CONFIG_PATH_ENV,
20− instance::SERIAL_LOG_NAME,
21− instance::{
22− InstanceConfig, build_ssh_login_actions, ensure_instance_dir, ensure_ssh_keypair,
23− extract_ipv4, load_or_create_instance_config, write_instance_config,
24− },
25− session_manager::{
26− GLOBAL_DIR_NAME, INSTANCE_FILENAME, VM_MANAGER_PID_NAME, VM_MANAGER_SOCKET_NAME,
27− },
28− vm::{self, DirectoryShare, LoginAction, VmInput},
29−};
30−
28+#[cfg_attr(feature = "mock-vm", allow(dead_code))]
29+const VM_ROOT_LOG_NAME: &str = "vm_root.log";
3130const VM_MANAGER_LOCK_NAME: &str = "vm.lock";
3231const VM_MANAGER_LOG_NAME: &str = "vm_manager.log";
32+const SHUTDOWN_RETRY_MS: u64 = 500;
33+#[cfg(test)]
34+const HARD_SHUTDOWN_TIMEOUT_MS: u64 = 1_000;
35+#[cfg(not(test))]
36+const HARD_SHUTDOWN_TIMEOUT_MS: u64 = 12_000;
37+const STATUS_PREFIX: &str = "status:";
38+
39+type ClientStreams = Arc<Mutex<Vec<UnixStream>>>;
40+type SharedStatus = Arc<Mutex<String>>;
41+
42+#[cfg(not(test))]
43+fn force_exit(_reason: &str) -> ! {
44+ std::process::exit(1);
45+}
46+
47+#[cfg(test)]
48+fn force_exit(reason: &str) -> ! {
49+ panic!("{reason}");
50+}
3351
3452pub fn ensure_manager(
3553 raw_args: &[std::ffi::OsString],
3654 auto_shutdown_ms: u64,
3755 config_path: Option<&Path>,
38−) -> Result<UnixStream, Box<dyn std::error::Error>> {
56+) -> Result<UnixStream> {
3957 let project_root = env::current_dir()?;
4058 tracing::debug!(root = %project_root.display(), "ensure vm manager");
4159 let instance_dir = ensure_instance_dir(&project_root)?;
42− cleanup_stale_manager(&instance_dir);
60+ cleanup_stale_manager(&project_root)?;
4361 let socket_path = instance_dir.join(VM_MANAGER_SOCKET_NAME);
4462
4563 if let Ok(stream) = UnixStream::connect(&socket_path) {
4664 send_client_pid(&stream);
47− tracing::info!(path = %socket_path.display(), "connected to existing vm manager");
65+ tracing::info!(path = %socket_path.display(), "connected to an existing vm manager");
4866 return Ok(stream);
4967 }
5068
@@ -82,12 +100,11 @@ pub fn ensure_manager(
82100 drop(lock_file.take());
83101 let _ = fs::remove_file(&lock_path);
84102 }
85− return Err(format!(
86− "Timed out waiting for vm manager socket: {} ({})",
103+ bail!(format!(
104+ "timed out waiting for vm manager socket: {} ({})",
87105 socket_path.display(),
88106 err
89− )
90− .into());
107+ ));
91108 }
92109 thread::sleep(Duration::from_millis(100));
93110 }
@@ -95,24 +112,49 @@ pub fn ensure_manager(
95112 }
96113}
97114
98−pub fn run_manager(
99− args: vm::VmArg,
100− auto_shutdown_ms: u64,
101−) -> Result<(), Box<dyn std::error::Error>> {
115+pub fn run_manager(args: vm::VmArg, auto_shutdown_ms: u64) -> Result<()> {
102116 let project_root = env::current_dir()?;
103117 tracing::info!(root = %project_root.display(), "vm manager starting");
118+ #[cfg(not(feature = "mock-vm"))]
119+ {
120+ unsafe {
121+ env::remove_var("VIBEBOX_SKIP_CODESIGN");
122+ }
123+ vm::ensure_signed();
124+ unsafe {
125+ env::set_var("VIBEBOX_SKIP_CODESIGN", "1");
126+ }
127+ }
104128 let _pid_guard = ensure_pid_file(&project_root)?;
105− run_manager_with(
106− &project_root,
107− args,
108− auto_shutdown_ms,
109− &RealVmExecutor,
110− ManagerOptions {
111− ensure_signed: true,
112− detach: true,
113− prepare_vm: true,
114− },
115− )
129+ #[cfg(feature = "mock-vm")]
130+ tracing::info!("vm manager using mock executor");
131+ let executor: &dyn VmExecutor = {
132+ #[cfg(feature = "mock-vm")]
133+ {
134+ &MockVmExecutor
135+ }
136+ #[cfg(not(feature = "mock-vm"))]
137+ {
138+ &RealVmExecutor
139+ }
140+ };
141+ let options = {
142+ #[cfg(feature = "mock-vm")]
143+ {
144+ ManagerOptions {
145+ detach: true,
146+ prepare_vm: false,
147+ }
148+ }
149+ #[cfg(not(feature = "mock-vm"))]
150+ {
151+ ManagerOptions {
152+ detach: true,
153+ prepare_vm: true,
154+ }
155+ }
156+ };
157+ run_manager_with(&project_root, args, auto_shutdown_ms, executor, options)
116158}
117159
118160fn spawn_manager_process(
@@ -120,30 +162,28 @@ fn spawn_manager_process(
120162 auto_shutdown_ms: u64,
121163 instance_dir: &Path,
122164 config_path: Option<&Path>,
123−) -> Result<(), Box<dyn std::error::Error>> {
165+) -> Result<()> {
124166 let exe = env::current_exe()?;
125167 let mut supervisor_exe = exe.clone();
126168 supervisor_exe.set_file_name("vibebox-supervisor");
127− let use_supervisor = supervisor_exe.exists();
128− let mut cmd = if use_supervisor {
129− Command::new(supervisor_exe)
130− } else {
131− let mut cmd = Command::new(exe);
132− cmd.arg0("vibebox-supervisor");
133− cmd
134− };
169+ // intentional
170+ if !supervisor_exe.exists() {
171+ bail!(format!(
172+ "vibebox-supervisor not found at {}",
173+ supervisor_exe.display()
174+ ));
175+ }
176+ let mut cmd = Command::new(supervisor_exe);
135177 if raw_args.len() > 1 {
136178 cmd.args(&raw_args[1..]);
137179 }
138− if !use_supervisor {
139− cmd.env("VIBEBOX_VM_MANAGER", "1");
140− }
180+ cmd.env("VIBEBOX_INTERNAL", "1");
141181 cmd.env("VIBEBOX_LOG_NO_COLOR", "1");
142182 cmd.env("VIBEBOX_AUTO_SHUTDOWN_MS", auto_shutdown_ms.to_string());
143183 if let Some(path) = config_path {
144184 cmd.env(CONFIG_PATH_ENV, path);
145185 }
146− tracing::info!(auto_shutdown_ms, "vm manager process spawn requested");
186+ tracing::debug!(auto_shutdown_ms, "vm manager process spawn requested");
147187 let log_path = instance_dir.join(VM_MANAGER_LOG_NAME);
148188 let log_file = fs::OpenOptions::new()
149189 .create(true)
@@ -165,46 +205,172 @@ fn spawn_manager_process(
165205 Ok(())
166206}
167207
168−fn ensure_pid_file(project_root: &Path) -> Result<PidFileGuard, Box<dyn std::error::Error>> {
208+fn ensure_pid_file(project_root: &Path) -> Result<PidFileGuard> {
169209 let instance_dir = ensure_instance_dir(project_root)?;
170210 let pid_path = instance_dir.join(VM_MANAGER_PID_NAME);
171211 let socket_path = instance_dir.join(VM_MANAGER_SOCKET_NAME);
172− if let Ok(content) = fs::read_to_string(&pid_path) {
173− if let Ok(pid) = content.trim().parse::<u32>() {
174− if pid_is_alive(pid) {
175− if is_socket_path(&socket_path) {
176− return Err(format!("vm manager already running (pid {pid})").into());
177− }
178− tracing::warn!(
179− pid,
180− path = %socket_path.display(),
181− "stale pid file detected with missing socket"
182− );
183− }
212+ match vm_liveness(project_root)? {
213+ VmLiveness::RunningWithSocket { pid } => {
214+ bail!("vm manager already running (pid {pid})");
184215 }
185− let _ = fs::remove_file(&pid_path);
216+ VmLiveness::RunningWithoutSocket { pid } => {
217+ tracing::warn!(
218+ pid,
219+ path = %socket_path.display(),
220+ "stale pid file detected with missing socket"
221+ );
222+ }
223+ VmLiveness::NotRunningOrMissing => {}
186224 }
225+ let _ = fs::remove_file(&pid_path);
187226 fs::write(&pid_path, format!("{}\n", std::process::id()))?;
188227 let _ = fs::set_permissions(&pid_path, fs::Permissions::from_mode(0o600));
189228 Ok(PidFileGuard { path: pid_path })
190229}
191230
192−fn cleanup_stale_manager(instance_dir: &Path) {
193− let pid_path = instance_dir.join(VM_MANAGER_PID_NAME);
194− if let Ok(content) = fs::read_to_string(&pid_path) {
195− if let Ok(pid) = content.trim().parse::<u32>() {
196− if pid_is_alive(pid) {
197− return;
198− }
199− }
231+fn cleanup_stale_manager(project_root: &Path) -> Result<()> {
232+ let pid_path = project_root
233+ .join(INSTANCE_DIR_NAME)
234+ .join(VM_MANAGER_PID_NAME);
235+ if matches!(
236+ vm_liveness(project_root)?,
237+ VmLiveness::RunningWithSocket { .. } | VmLiveness::RunningWithoutSocket { .. }
238+ ) {
239+ return Ok(());
200240 }
201241 let _ = fs::remove_file(&pid_path);
242+ Ok(())
202243}
203244
204−fn is_socket_path(path: &Path) -> bool {
205− fs::metadata(path)
206− .map(|meta| meta.file_type().is_socket())
207− .unwrap_or(false)
245+fn inject_project_mount(
246+ mounts: &mut Vec<String>,
247+ project_root: &Path,
248+ ssh_user: &str,
249+ project_name: &str,
250+) {
251+ let guest_tilde = format!("~/{project_name}");
252+ let guest_home = format!("/home/{ssh_user}/{project_name}");
253+ let guest_base = format!("{PROJECT_GUEST_BASE}/{project_name}");
254+ let already_mapped = mounts.iter().any(|spec| {
255+ let parts: Vec<&str> = spec.split(':').collect();
256+ if parts.len() < 2 {
257+ return false;
258+ }
259+ let guest = parts[1];
260+ guest == guest_tilde || guest == guest_home || guest == guest_base
261+ });
262+ if already_mapped {
263+ return;
264+ }
265+ let host = project_root.display();
266+ mounts.insert(0, format!("{host}:{guest_tilde}:read-write"));
267+}
268+
269+fn prepare_mounts_and_links(mut args: vm::VmArg, ssh_user: &str) -> (vm::VmArg, String) {
270+ let mut links = Vec::new();
271+ let mut mounts = Vec::with_capacity(args.mounts.len());
272+ for spec in args.mounts {
273+ let (rewritten, link) = rewrite_mount_spec(&spec, ssh_user);
274+ if let Some(link) = link {
275+ links.push(link);
276+ }
277+ mounts.push(rewritten);
278+ }
279+ args.mounts = mounts;
280+ let script = render_home_links_script(&links, ssh_user);
281+ (args, script)
282+}
283+
284+struct HomeLink {
285+ source: String,
286+ target: String,
287+}
288+
289+fn rewrite_mount_spec(spec: &str, ssh_user: &str) -> (String, Option<HomeLink>) {
290+ let parts: Vec<&str> = spec.split(':').collect();
291+ if parts.len() < 2 || parts.len() > 3 {
292+ return (spec.to_string(), None);
293+ }
294+ let host = parts[0];
295+ let guest = parts[1];
296+ let mode = parts.get(2).copied();
297+
298+ let home_prefix = format!("/home/{ssh_user}");
299+ let (rel, is_home) = if guest == "~" {
300+ (String::new(), true)
301+ } else if let Some(stripped) = guest.strip_prefix("~/") {
302+ (stripped.to_string(), true)
303+ } else if guest == home_prefix {
304+ (String::new(), true)
305+ } else if let Some(stripped) = guest.strip_prefix(&(home_prefix.clone() + "/")) {
306+ (stripped.to_string(), true)
307+ } else {
308+ (String::new(), false)
309+ };
310+
311+ if !is_home {
312+ return (spec.to_string(), None);
313+ }
314+
315+ let root_base = PROJECT_GUEST_BASE;
316+ let root_path = if rel.is_empty() {
317+ root_base.to_string()
318+ } else {
319+ format!("{root_base}/{rel}")
320+ };
321+ let target = if rel.is_empty() {
322+ home_prefix
323+ } else {
324+ format!("{home_prefix}/{rel}")
325+ };
326+
327+ let rewritten = match mode {
328+ Some(mode) => format!("{host}:{root_path}:{mode}"),
329+ None => format!("{host}:{root_path}"),
330+ };
331+
332+ (
333+ rewritten,
334+ Some(HomeLink {
335+ source: root_path,
336+ target,
337+ }),
338+ )
339+}
340+
341+fn render_home_links_script(links: &[HomeLink], ssh_user: &str) -> String {
342+ if links.is_empty() {
343+ return String::new();
344+ }
345+ let mut lines = Vec::new();
346+ lines.push("link_home() {".to_string());
347+ lines.push(" src=\"$1\"".to_string());
348+ lines.push(" dest=\"$2\"".to_string());
349+ lines.push(" if [ -L \"$dest\" ]; then".to_string());
350+ lines.push(" current=\"$(readlink \"$dest\" || true)\"".to_string());
351+ lines.push(" if [ \"$current\" != \"$src\" ]; then".to_string());
352+ lines.push(" rm -f \"$dest\"".to_string());
353+ lines.push(" fi".to_string());
354+ lines.push(" fi".to_string());
355+ lines.push(" if [ ! -e \"$dest\" ]; then".to_string());
356+ lines.push(" mkdir -p \"$(dirname \"$dest\")\"".to_string());
357+ lines.push(" ln -s \"$src\" \"$dest\"".to_string());
358+ lines.push(" fi".to_string());
359+ lines.push(format!(
360+ " chown -h \"{ssh_user}:{ssh_user}\" \"$dest\" 2>/dev/null || true"
361+ ));
362+ lines.push("}".to_string());
363+ for link in links {
364+ let src = shell_escape(&link.source);
365+ let dest = shell_escape(&link.target);
366+ lines.push(format!("link_home {src} {dest}"));
367+ }
368+ lines.join("\n")
369+}
370+
371+fn shell_escape(value: &str) -> String {
372+ let escaped = value.replace('\'', "'\"'\"'");
373+ format!("'{escaped}'")
208374}
209375
210376struct PidFileGuard {
@@ -243,6 +409,30 @@ fn wait_for_disconnect(mut stream: UnixStream) {
243409 }
244410}
245411
412+fn remove_client(streams: &ClientStreams, fd: std::os::fd::RawFd) {
413+ if let Ok(mut clients) = streams.lock() {
414+ clients.retain(|stream| stream.as_raw_fd() != fd);
415+ }
416+}
417+
418+fn send_status_line(stream: &mut UnixStream, status: &str) -> bool {
419+ let mut payload = String::with_capacity(STATUS_PREFIX.len() + status.len() + 1);
420+ payload.push_str(STATUS_PREFIX);
421+ payload.push_str(status);
422+ payload.push('\n');
423+ stream.write_all(payload.as_bytes()).is_ok()
424+}
425+
426+#[cfg_attr(feature = "mock-vm", allow(dead_code))]
427+fn broadcast_status(streams: &ClientStreams, latest_status: &SharedStatus, status: &str) {
428+ if let Ok(mut current) = latest_status.lock() {
429+ *current = status.to_string();
430+ }
431+ if let Ok(mut clients) = streams.lock() {
432+ clients.retain_mut(|stream| send_status_line(stream, status));
433+ }
434+}
435+
246436fn send_client_pid(stream: &UnixStream) {
247437 let pid = std::process::id();
248438 let payload = format!("pid={pid}\n");
@@ -252,7 +442,7 @@ fn send_client_pid(stream: &UnixStream) {
252442 }
253443}
254444
255−fn acquire_spawn_lock(lock_path: &Path) -> Result<Option<fs::File>, Box<dyn std::error::Error>> {
445+fn acquire_spawn_lock(lock_path: &Path) -> Result<Option<fs::File>> {
256446 match fs::OpenOptions::new()
257447 .write(true)
258448 .create_new(true)
@@ -286,6 +476,12 @@ fn is_lock_stale(lock_path: &Path) -> bool {
286476 }
287477}
288478
479+fn read_lock_pid(lock_path: &Path) -> Option<u32> {
480+ let content = fs::read_to_string(lock_path).ok()?;
481+ let line = content.lines().next()?;
482+ line.strip_prefix("pid=")?.trim().parse::<u32>().ok()
483+}
484+
289485fn pid_is_alive(pid: u32) -> bool {
290486 let pid = pid as libc::pid_t;
291487 let result = unsafe { libc::kill(pid, 0) };
@@ -299,12 +495,6 @@ fn pid_is_alive(pid: u32) -> bool {
299495 }
300496}
301497
302−fn read_lock_pid(lock_path: &Path) -> Option<u32> {
303− let content = fs::read_to_string(lock_path).ok()?;
304− let line = content.lines().next()?;
305− line.strip_prefix("pid=")?.trim().parse::<u32>().ok()
306−}
307−
308498fn read_client_pid(stream: &UnixStream) -> Option<u32> {
309499 let mut stream = stream.try_clone().ok()?;
310500 let _ = stream.set_read_timeout(Some(Duration::from_millis(200)));
@@ -315,7 +505,7 @@ fn read_client_pid(stream: &UnixStream) -> Option<u32> {
315505 Ok(0) => break,
316506 Ok(n) => {
317507 len += n;
318− if buf[..len].iter().any(|b| *b == b'\n') || len == buf.len() {
508+ if buf[..len].contains(&b'\n') || len == buf.len() {
319509 break;
320510 }
321511 }
@@ -337,14 +527,17 @@ fn read_client_pid(stream: &UnixStream) -> Option<u32> {
337527 }
338528}
339529
530+#[cfg_attr(feature = "mock-vm", allow(dead_code))]
340531fn spawn_manager_io(
341532 config: Arc<Mutex<InstanceConfig>>,
342− instance_dir: PathBuf,
533+ project_dir: PathBuf,
534+ clients: ClientStreams,
535+ latest_status: SharedStatus,
343536 output_monitor: Arc<vm::OutputMonitor>,
344537 vm_output_fd: std::os::unix::io::OwnedFd,
345538 vm_input_fd: std::os::unix::io::OwnedFd,
346539) -> vm::IoContext {
347− let log_path = instance_dir.join(SERIAL_LOG_NAME);
540+ let log_path = project_dir.join(INSTANCE_DIR_NAME).join(VM_ROOT_LOG_NAME);
348541 let log_file = fs::OpenOptions::new()
349542 .create(true)
350543 .write(true)
@@ -353,16 +546,15 @@ fn spawn_manager_io(
353546 .ok()
354547 .map(|file| Arc::new(Mutex::new(file)));
355548
356− let instance_path = instance_dir.join(INSTANCE_FILENAME);
357549 let config_for_output = config.clone();
358550 let log_for_output = log_file.clone();
359551 let mut line_buf = String::new();
360552
361553 let on_output = move |bytes: &[u8]| {
362− if let Some(log) = &log_for_output {
363− if let Ok(mut file) = log.lock() {
364− let _ = file.write_all(bytes);
365− }
554+ if let Some(log) = &log_for_output
555+ && let Ok(mut file) = log.lock()
556+ {
557+ let _ = file.write_all(bytes);
366558 }
367559
368560 let text = String::from_utf8_lossy(bytes);
@@ -375,17 +567,27 @@ fn spawn_manager_io(
375567 }
376568 line_buf.drain(..=pos);
377569
378− let cleaned = line.trim_start_matches(|c: char| c == '\r' || c == ' ');
570+ let cleaned = line.trim_start_matches(['\r', ' ']);
571+ if let Some(script_failure) = cleaned.strip_prefix("VIBEBOX_SCRIPT_ERROR:") {
572+ let failure = script_failure.trim();
573+ if !failure.is_empty() {
574+ tracing::error!(script_failure = %failure, "[vm] script reported failure");
575+ broadcast_status(
576+ &clients,
577+ &latest_status,
578+ &format!("{STATUS_VM_ERROR_PREFIX} {failure}"),
579+ );
580+ }
581+ }
379582 if let Some(pos) = cleaned.find("VIBEBOX_IPV4=") {
380583 let ip_raw = &cleaned[(pos + "VIBEBOX_IPV4=".len())..];
381584 let ip = extract_ipv4(ip_raw).unwrap_or_default();
382− if !ip.is_empty() {
383− if let Ok(mut cfg) = config_for_output.lock() {
384− if cfg.vm_ipv4.as_deref() != Some(ip.as_str()) {
385− cfg.vm_ipv4 = Some(ip.clone());
386− let _ = write_instance_config(&instance_path, &cfg);
387− }
388− }
585+ if !ip.is_empty()
586+ && let Ok(mut cfg) = config_for_output.lock()
587+ && cfg.vm_ipv4.as_deref() != Some(ip.as_str())
588+ {
589+ cfg.vm_ipv4 = Some(ip.clone());
590+ let _ = write_instance_config(&project_dir, &cfg);
389591 }
390592 }
391593 }
@@ -407,51 +609,13 @@ enum ManagerEvent {
407609 VmExited(Option<String>),
408610}
409611
410−#[cfg(test)]
411−mod tests {
412− use super::*;
413− use std::{sync::mpsc, time::Duration};
414−
415− #[test]
416− fn manager_powers_off_after_grace_when_no_refs() {
417− let _temp = tempfile::Builder::new()
418− .prefix("vb")
419− .tempdir_in("/tmp")
420− .expect("tempdir");
421−
422− let (event_tx, event_rx) = mpsc::channel::<ManagerEvent>();
423− let (vm_tx, vm_rx) = mpsc::channel::<VmInput>();
424− let vm_input_tx = Arc::new(Mutex::new(Some(vm_tx)));
425−
426− let manager_thread = thread::spawn(move || {
427− manager_event_loop(event_rx, vm_input_tx, 50).expect("event loop");
428− });
429−
430− event_tx.send(ManagerEvent::Inc(None)).unwrap();
431− assert!(vm_rx.recv_timeout(Duration::from_millis(100)).is_err());
432−
433− event_tx.send(ManagerEvent::Dec(None)).unwrap();
434− let msg = vm_rx
435− .recv_timeout(Duration::from_secs(2))
436− .expect("poweroff");
437− match msg {
438− VmInput::Bytes(data) => {
439− assert_eq!(data, b"systemctl poweroff\n");
440− }
441− _ => panic!("unexpected vm input"),
442− }
443− let _ = event_tx.send(ManagerEvent::VmExited(None));
444− let _ = manager_thread.join();
445− }
446−}
447−
448612struct ManagerOptions {
449− ensure_signed: bool,
450613 detach: bool,
451614 prepare_vm: bool,
452615}
453616
454617trait VmExecutor {
618+ #[allow(clippy::too_many_arguments)]
455619 fn run_vm(
456620 &self,
457621 args: vm::VmArg,
@@ -459,10 +623,13 @@ trait VmExecutor {
459623 extra_shares: Vec<DirectoryShare>,
460624 config: Arc<Mutex<InstanceConfig>>,
461625 instance_dir: PathBuf,
626+ clients: ClientStreams,
627+ latest_status: SharedStatus,
462628 vm_input_tx: Arc<Mutex<Option<mpsc::Sender<VmInput>>>>,
463− ) -> Result<(), Box<dyn std::error::Error>>;
629+ ) -> Result<()>;
464630}
465631
632+#[cfg_attr(feature = "mock-vm", allow(dead_code))]
466633struct RealVmExecutor;
467634
468635impl VmExecutor for RealVmExecutor {
@@ -472,15 +639,22 @@ impl VmExecutor for RealVmExecutor {
472639 extra_login_actions: Vec<LoginAction>,
473640 extra_shares: Vec<DirectoryShare>,
474641 config: Arc<Mutex<InstanceConfig>>,
475− instance_dir: PathBuf,
642+ project_dir: PathBuf,
643+ clients: ClientStreams,
644+ latest_status: SharedStatus,
476645 vm_input_tx: Arc<Mutex<Option<mpsc::Sender<VmInput>>>>,
477− ) -> Result<(), Box<dyn std::error::Error>> {
478− let result = vm::run_with_args_and_extras(
646+ ) -> Result<()> {
647+ let status_callback = |status: &str| {
648+ broadcast_status(&clients, &latest_status, status);
649+ };
650+ vm::run_with_args_and_extras(
479651 args,
480652 |output_monitor, vm_output_fd, vm_input_fd| {
481653 let io_ctx = spawn_manager_io(
482654 config.clone(),
483− instance_dir.clone(),
655+ project_dir.clone(),
656+ clients.clone(),
657+ latest_status.clone(),
484658 output_monitor,
485659 vm_output_fd,
486660 vm_input_fd,
@@ -490,35 +664,60 @@ impl VmExecutor for RealVmExecutor {
490664 },
491665 extra_login_actions,
492666 extra_shares,
493− );
494− result
667+ Some(&status_callback),
668+ )
669+ }
670+}
671+
672+#[cfg(feature = "mock-vm")]
673+struct MockVmExecutor;
674+
675+#[cfg(feature = "mock-vm")]
676+impl VmExecutor for MockVmExecutor {
677+ fn run_vm(
678+ &self,
679+ _args: vm::VmArg,
680+ _extra_login_actions: Vec<LoginAction>,
681+ _extra_shares: Vec<DirectoryShare>,
682+ _config: Arc<Mutex<InstanceConfig>>,
683+ _instance_dir: PathBuf,
684+ _clients: ClientStreams,
685+ _latest_status: SharedStatus,
686+ vm_input_tx: Arc<Mutex<Option<mpsc::Sender<VmInput>>>>,
687+ ) -> Result<()> {
688+ let (tx, rx) = mpsc::channel::<VmInput>();
689+ *vm_input_tx.lock().unwrap() = Some(tx);
690+ tracing::info!("mock vm executor running");
691+ while let Ok(input) = rx.recv() {
692+ match input {
693+ VmInput::Shutdown => break,
694+ VmInput::Bytes(bytes) => {
695+ let text = String::from_utf8_lossy(&bytes);
696+ if text.contains("systemctl poweroff") {
697+ break;
698+ }
699+ }
700+ }
701+ }
702+ tracing::info!("mock vm executor exiting");
703+ Ok(())
495704 }
496705}
497706
498707fn run_manager_with(
499708 project_root: &Path,
500− args: vm::VmArg,
709+ mut args: vm::VmArg,
501710 auto_shutdown_ms: u64,
502711 executor: &dyn VmExecutor,
503712 options: ManagerOptions,
504−) -> Result<(), Box<dyn std::error::Error>> {
505− if options.ensure_signed {
506− let _had_skip = env::var("VIBEBOX_SKIP_CODESIGN").ok();
507− unsafe {
508− env::remove_var("VIBEBOX_SKIP_CODESIGN");
509− }
510− vm::ensure_signed();
511− unsafe {
512− env::set_var("VIBEBOX_SKIP_CODESIGN", "1");
513− }
514− }
713+) -> Result<()> {
515714 if options.detach {
516715 detach_from_terminal();
517716 }
518717
519718 let project_name = project_root
520719 .file_name()
521− .ok_or("Project directory has no name")?
720+ .ok_or_else(|| anyhow!("Project directory has no name"))?
522721 .to_string_lossy()
523722 .into_owned();
524723 let instance_dir = ensure_instance_dir(project_root)?;
@@ -526,21 +725,35 @@ fn run_manager_with(
526725 let _ = ensure_ssh_keypair(&instance_dir)?;
527726 }
528727
529− let mut config = load_or_create_instance_config(&instance_dir)?;
728+ let mut config = load_or_create_instance_config(project_root)?;
530729 if config.vm_ipv4.is_some() {
531730 config.vm_ipv4 = None;
532− write_instance_config(&instance_dir.join(INSTANCE_FILENAME), &config)?;
731+ write_instance_config(project_root, &config)?;
533732 }
534733 let config = Arc::new(Mutex::new(config));
734+ let ssh_user = config
735+ .lock()
736+ .map(|cfg| cfg.ssh_user.clone())
737+ .map_err(|_| anyhow!("failed to acquire ssh user display"))?;
738+ if !args.no_default_mounts {
739+ inject_project_mount(&mut args.mounts, project_root, &ssh_user, &project_name);
740+ }
741+ let (args, home_links_script) = prepare_mounts_and_links(args, &ssh_user);
535742
743+ let project_guest_dir = format!("{PROJECT_GUEST_BASE}/{project_name}");
536744 let ssh_guest_dir = format!("/root/{}", GLOBAL_DIR_NAME);
537− let extra_shares = vec![DirectoryShare::new(
538− instance_dir.clone(),
539− ssh_guest_dir.clone().into(),
540− true,
541− )?];
542− let extra_login_actions =
543− build_ssh_login_actions(&config, &project_name, ssh_guest_dir.as_str(), "ssh_key");
745+ let extra_shares = vec![
746+ DirectoryShare::new(instance_dir.clone(), ssh_guest_dir.clone().into(), true)
747+ .map_err(|err| anyhow!(err.to_string()))?,
748+ ];
749+ let extra_login_actions = build_ssh_login_actions(
750+ &config,
751+ &project_name,
752+ &project_guest_dir,
753+ ssh_guest_dir.as_str(),
754+ "ssh_key",
755+ &home_links_script,
756+ );
544757
545758 let socket_path = instance_dir.join(VM_MANAGER_SOCKET_NAME);
546759 if let Ok(stream) = UnixStream::connect(&socket_path) {
@@ -555,17 +768,43 @@ fn run_manager_with(
555768 let _ = fs::set_permissions(&socket_path, fs::Permissions::from_mode(0o600));
556769 tracing::info!(path = %socket_path.display(), "vm manager socket bound");
557770
771+ let clients: ClientStreams = Arc::new(Mutex::new(Vec::new()));
772+ let latest_status: SharedStatus = Arc::new(Mutex::new(String::new()));
558773 let (event_tx, event_rx) = mpsc::channel::<ManagerEvent>();
559774 let event_tx_accept = event_tx.clone();
775+ let clients_accept = clients.clone();
776+ let latest_status_accept = latest_status.clone();
560777 thread::spawn(move || {
561778 for stream in listener.incoming() {
562779 match stream {
563780 Ok(stream) => {
781+ let mut client_fd: Option<std::os::fd::RawFd> = None;
782+ let latest_status_snapshot = latest_status_accept
783+ .lock()
784+ .ok()
785+ .map(|status| status.clone());
786+ if let Ok(writer) = stream.try_clone() {
787+ let writer_fd = writer.as_raw_fd();
788+ if let Ok(mut connected) = clients_accept.lock() {
789+ connected.push(writer);
790+ client_fd = Some(writer_fd);
791+ if let Some(last) = connected.last_mut()
792+ && let Some(status) = latest_status_snapshot.as_deref()
793+ && !status.is_empty()
794+ {
795+ let _ = send_status_line(last, status);
796+ }
797+ }
798+ }
564799 let event_tx_conn = event_tx_accept.clone();
800+ let clients_conn = clients_accept.clone();
565801 thread::spawn(move || {
566802 let pid = read_client_pid(&stream);
567803 let _ = event_tx_conn.send(ManagerEvent::Inc(pid));
568804 wait_for_disconnect(stream);
805+ if let Some(fd) = client_fd {
806+ remove_client(&clients_conn, fd);
807+ }
569808 let _ = event_tx_conn.send(ManagerEvent::Dec(pid));
570809 });
571810 }
@@ -585,43 +824,59 @@ fn run_manager_with(
585824 extra_login_actions,
586825 extra_shares,
587826 config.clone(),
588− instance_dir.clone(),
827+ project_root.to_path_buf(),
828+ clients.clone(),
829+ latest_status.clone(),
589830 vm_input_tx.clone(),
590831 );
591832 tracing::info!("vm manager vm run completed");
592833 let vm_err = vm_result.err().map(|e| e.to_string());
834+ if let Some(err) = &vm_err {
835+ broadcast_status(
836+ &clients,
837+ &latest_status,
838+ &format!("{STATUS_VM_ERROR_PREFIX} {err}"),
839+ );
840+ }
593841 let _ = event_tx.send(ManagerEvent::VmExited(vm_err.clone()));
594− let event_loop_result: Result<(), String> = event_loop_handle
842+ let event_loop_result = event_loop_handle
595843 .join()
596− .unwrap_or_else(|_| Err("vm manager event loop panicked".into()))
844+ .unwrap_or_else(|_| Err(Error::msg("vm manager event loop panicked")))
597845 .map_err(|err| err.to_string());
598846 let _ = fs::remove_file(&socket_path);
599847 if let Err(err) = &event_loop_result {
600848 tracing::error!(error = %err, "vm manager exiting due to event loop error");
601− return Err(err.to_string().into());
849+ bail!(err.to_string());
602850 }
603851 if let Some(err) = vm_err {
604852 tracing::error!(error = %err, "vm manager exiting due to vm error");
605− return Err(err.into());
853+ bail!(err);
606854 }
607855 tracing::info!("vm manager exiting");
608− Ok(event_loop_result?)
856+ event_loop_result.map_err(Error::msg)
609857}
610858
611859fn manager_event_loop(
612860 event_rx: mpsc::Receiver<ManagerEvent>,
613861 vm_input_tx: Arc<Mutex<Option<mpsc::Sender<VmInput>>>>,
614862 auto_shutdown_ms: u64,
615−) -> Result<(), String> {
863+) -> Result<()> {
616864 let mut ref_count: usize = 0;
617865 let mut shutdown_deadline: Option<Instant> = None;
618866 let mut shutdown_sent = false;
867+ let mut hard_deadline: Option<Instant> = None;
619868 let grace = Duration::from_millis(auto_shutdown_ms.max(1));
869+ let hard_timeout = Duration::from_millis(HARD_SHUTDOWN_TIMEOUT_MS);
620870
621871 loop {
622− let timeout = match shutdown_deadline {
623− Some(deadline) => deadline.saturating_duration_since(Instant::now()),
624− None => Duration::from_secs(1),
872+ let timeout = match (shutdown_deadline, hard_deadline) {
873+ (Some(shutdown), Some(hard)) => {
874+ let next = if shutdown <= hard { shutdown } else { hard };
875+ next.saturating_duration_since(Instant::now())
876+ }
877+ (Some(shutdown), None) => shutdown.saturating_duration_since(Instant::now()),
878+ (None, Some(hard)) => hard.saturating_duration_since(Instant::now()),
879+ (None, None) => Duration::from_secs(1),
625880 };
626881
627882 match event_rx.recv_timeout(timeout) {
@@ -635,11 +890,10 @@ fn manager_event_loop(
635890 );
636891 shutdown_deadline = None;
637892 shutdown_sent = false;
893+ hard_deadline = None;
638894 }
639895 Ok(ManagerEvent::Dec(pid)) => {
640− if ref_count > 0 {
641− ref_count -= 1;
642− }
896+ ref_count = ref_count.saturating_sub(1);
643897 tracing::info!(
644898 ref_count,
645899 pid = pid.unwrap_or(0),
@@ -658,15 +912,52 @@ fn manager_event_loop(
658912 break;
659913 }
660914 Err(mpsc::RecvTimeoutError::Timeout) => {
661− if let Some(deadline) = shutdown_deadline {
662− if Instant::now() >= deadline && !shutdown_sent {
663− if let Some(tx) = vm_input_tx.lock().unwrap().clone() {
664− let _ = tx.send(VmInput::Bytes(b"systemctl poweroff\n".to_vec()));
915+ if let Some(deadline) = shutdown_deadline
916+ && Instant::now() >= deadline
917+ && !shutdown_sent
918+ {
919+ if hard_deadline.is_none() {
920+ hard_deadline = Some(Instant::now() + hard_timeout);
921+ }
922+ let mut sent = false;
923+ if let Some(tx) = vm_input_tx.lock().unwrap().clone() {
924+ if tx
925+ .send(VmInput::Bytes(b"systemctl poweroff\n".to_vec()))
926+ .is_ok()
927+ {
928+ sent = true;
929+ } else {
930+ tracing::warn!("shutdown command failed to send");
665931 }
932+ } else {
933+ tracing::warn!("shutdown command deferred; vm input not ready");
934+ }
935+ if sent {
666936 tracing::info!("shutdown command sent");
667937 shutdown_sent = true;
668938 shutdown_deadline = None;
939+ hard_deadline = Some(Instant::now() + hard_timeout);
940+ } else {
941+ shutdown_deadline =
942+ Some(Instant::now() + Duration::from_millis(SHUTDOWN_RETRY_MS));
943+ }
944+ }
945+ if ref_count == 0
946+ && let Some(deadline) = hard_deadline
947+ && Instant::now() >= deadline
948+ {
949+ if shutdown_sent {
950+ tracing::warn!(
951+ timeout_ms = HARD_SHUTDOWN_TIMEOUT_MS,
952+ "force exiting: VM did not stop after shutdown timeout"
953+ );
954+ } else {
955+ tracing::warn!(
956+ timeout_ms = HARD_SHUTDOWN_TIMEOUT_MS,
957+ "force exiting: VM input not ready after shutdown timeout"
958+ );
669959 }
960+ force_exit("vm manager forced exit");
670961 }
671962 }
672963 Err(mpsc::RecvTimeoutError::Disconnected) => break,
@@ -675,3 +966,112 @@ fn manager_event_loop(
675966
676967 Ok(())
677968}
969+
970+#[cfg(test)]
971+mod tests {
972+ use super::*;
973+ use std::{fs, sync::mpsc, thread, time::Duration};
974+
975+ #[test]
976+ fn manager_powers_off_after_grace_when_no_refs() {
977+ let _temp = tempfile::Builder::new()
978+ .prefix("vb")
979+ .tempdir_in("/tmp")
980+ .expect("tempdir");
981+
982+ let (event_tx, event_rx) = mpsc::channel::<ManagerEvent>();
983+ let (vm_tx, vm_rx) = mpsc::channel::<VmInput>();
984+ let vm_input_tx = Arc::new(Mutex::new(Some(vm_tx)));
985+
986+ let manager_thread = thread::spawn(move || {
987+ manager_event_loop(event_rx, vm_input_tx, 50).expect("event loop");
988+ });
989+
990+ event_tx.send(ManagerEvent::Inc(None)).unwrap();
991+ assert!(vm_rx.recv_timeout(Duration::from_millis(100)).is_err());
992+
993+ event_tx.send(ManagerEvent::Dec(None)).unwrap();
994+ let msg = vm_rx
995+ .recv_timeout(Duration::from_secs(2))
996+ .expect("poweroff");
997+ match msg {
998+ VmInput::Bytes(data) => {
999+ assert_eq!(data, b"systemctl poweroff\n");
1000+ }
1001+ _ => panic!("unexpected vm input"),
1002+ }
1003+ let _ = event_tx.send(ManagerEvent::VmExited(None));
1004+ let _ = manager_thread.join();
1005+ }
1006+
1007+ #[test]
1008+ fn manager_force_exits_when_vm_input_never_ready() {
1009+ let (event_tx, event_rx) = mpsc::channel::<ManagerEvent>();
1010+ let vm_input_tx = Arc::new(Mutex::new(None));
1011+
1012+ let manager_thread = thread::spawn(move || {
1013+ let _ = manager_event_loop(event_rx, vm_input_tx, 10);
1014+ });
1015+
1016+ event_tx.send(ManagerEvent::Inc(None)).unwrap();
1017+ event_tx.send(ManagerEvent::Dec(None)).unwrap();
1018+
1019+ let join_result = manager_thread.join();
1020+ assert!(
1021+ join_result.is_err(),
1022+ "expected manager to force-exit when vm input never becomes ready"
1023+ );
1024+ }
1025+
1026+ #[test]
1027+ fn manager_sends_shutdown_after_vm_input_becomes_ready() {
1028+ let (event_tx, event_rx) = mpsc::channel::<ManagerEvent>();
1029+ let (vm_tx, vm_rx) = mpsc::channel::<VmInput>();
1030+ let vm_input_tx = Arc::new(Mutex::new(None));
1031+ let vm_input_for_thread = vm_input_tx.clone();
1032+
1033+ let manager_thread = thread::spawn(move || {
1034+ manager_event_loop(event_rx, vm_input_for_thread, 10).expect("event loop");
1035+ });
1036+
1037+ event_tx.send(ManagerEvent::Inc(None)).unwrap();
1038+ event_tx.send(ManagerEvent::Dec(None)).unwrap();
1039+
1040+ thread::sleep(Duration::from_millis(100));
1041+ *vm_input_tx.lock().unwrap() = Some(vm_tx);
1042+
1043+ let msg = vm_rx
1044+ .recv_timeout(Duration::from_secs(2))
1045+ .expect("poweroff");
1046+ match msg {
1047+ VmInput::Bytes(data) => {
1048+ assert_eq!(data, b"systemctl poweroff\n");
1049+ }
1050+ _ => panic!("unexpected vm input"),
1051+ }
1052+ let _ = event_tx.send(ManagerEvent::VmExited(None));
1053+ let _ = manager_thread.join();
1054+ }
1055+
1056+ #[test]
1057+ fn lock_is_not_stale_when_owner_pid_is_alive() {
1058+ let temp = tempfile::Builder::new()
1059+ .prefix("vb")
1060+ .tempdir_in("/tmp")
1061+ .expect("tempdir");
1062+ let lock_path = temp.path().join("vm.lock");
1063+ fs::write(&lock_path, format!("pid={}\n", std::process::id())).expect("write lock");
1064+ assert!(!is_lock_stale(&lock_path));
1065+ }
1066+
1067+ #[test]
1068+ fn lock_is_stale_when_owner_pid_is_missing() {
1069+ let temp = tempfile::Builder::new()
1070+ .prefix("vb")
1071+ .tempdir_in("/tmp")
1072+ .expect("tempdir");
1073+ let lock_path = temp.path().join("vm.lock");
1074+ fs::write(&lock_path, "pid=999999\n").expect("write lock");
1075+ assert!(is_lock_stale(&lock_path));
1076+ }
1077+}