From 3397e1ce2922c4ae9b814e8110e81de7d201de81 Mon Sep 17 00:00:00 2001 From: Can Celik Date: Tue, 21 Apr 2026 16:30:04 +0300 Subject: feat: add persistent server-client sessions Make herdr persistent by default. Launching herdr now starts or reattaches to a background session server. Clients can detach and reattach while panes and agent processes keep running. Session mode now supports multi-client attach, auto-detect startup, and a thin-client/headless-server split. This also refactors the large app, ui, input, pane, config, workspace, and persistence modules into smaller focused submodules, while preserving behavior and colocating tests with the code they exercise. Upgrade notes: - persistence mode is now the default - in-app quit detaches the current client instead of stopping the server - use `herdr server stop` to stop the background session - use `--no-session` for the old single-process behavior - default socket paths now live under the config directory --- src/api/mod.rs | 101 +++++++++++++++++++++++++++++---------------------------- 1 file changed, 52 insertions(+), 49 deletions(-) (limited to 'src/api/mod.rs') diff --git a/src/api/mod.rs b/src/api/mod.rs index 1b81e09c..437a616e 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -2,7 +2,6 @@ pub mod schema; use std::fs; use std::io::{BufRead, BufReader, Read, Write}; -use std::os::unix::fs::PermissionsExt; use std::os::unix::net::{UnixListener, UnixStream}; use std::path::{Path, PathBuf}; use std::sync::atomic::{AtomicBool, Ordering}; @@ -28,6 +27,25 @@ const APP_RESPONSE_TIMEOUT: Duration = Duration::from_secs(5); const INITIAL_REQUEST_TIMEOUT: Duration = Duration::from_secs(5); const STREAM_WRITE_TIMEOUT: Duration = Duration::from_secs(5); +pub(crate) fn request_changes_ui(request: &Request) -> bool { + matches!( + &request.method, + Method::WorkspaceCreate(_) + | Method::WorkspaceFocus(_) + | Method::WorkspaceRename(_) + | Method::WorkspaceClose(_) + | Method::TabCreate(_) + | Method::TabFocus(_) + | Method::TabRename(_) + | Method::TabClose(_) + | Method::PaneSplit(_) + | Method::PaneReportAgent(_) + | Method::PaneClearAgentAuthority(_) + | Method::PaneReleaseAgent(_) + | Method::PaneClose(_) + ) +} + pub struct ApiRequestMessage { pub request: Request, pub respond_to: std::sync::mpsc::Sender, @@ -80,16 +98,6 @@ pub fn socket_path() -> PathBuf { return PathBuf::from(path); } - let socket_name = if crate::config::app_dir_name() == "herdr" { - "herdr.sock" - } else { - "herdr-dev.sock" - }; - - if let Ok(dir) = std::env::var("XDG_RUNTIME_DIR") { - return PathBuf::from(dir).join(socket_name); - } - crate::config::config_dir().join("herdr.sock") } @@ -156,47 +164,16 @@ pub fn start_server( } fn prepare_socket_path(path: &Path) -> std::io::Result<()> { - if let Some(parent) = path.parent() { - fs::create_dir_all(parent)?; - } - - if !path.exists() { - return Ok(()); - } - - match UnixStream::connect(path) { - Ok(_) => { - return Err(std::io::Error::new( - std::io::ErrorKind::AddrInUse, - format!( - "herdr is already running (socket busy at {})", - path.display() - ), - )); - } - Err(err) - if matches!( - err.kind(), - std::io::ErrorKind::ConnectionRefused - | std::io::ErrorKind::NotFound - | std::io::ErrorKind::TimedOut - ) => {} - Err(err) => return Err(err), - } - - if let Err(err) = fs::remove_file(path) { - if err.kind() != std::io::ErrorKind::NotFound { - return Err(err); - } - } - - Ok(()) + crate::ipc::prepare_socket_path(path, |path| { + format!( + "herdr is already running (socket busy at {})", + path.display() + ) + }) } fn restrict_socket_permissions(path: &Path) -> std::io::Result<()> { - let mut permissions = fs::metadata(path)?.permissions(); - permissions.set_mode(SOCKET_PERMISSION_MODE); - fs::set_permissions(path, permissions) + crate::ipc::restrict_socket_permissions(path, SOCKET_PERMISSION_MODE) } fn handle_connection( @@ -947,6 +924,13 @@ fn error_response_json(id: String, code: &str, message: String) -> String { #[cfg(test)] mod tests { use super::*; + use std::os::unix::fs::PermissionsExt; + use std::sync::{Mutex, OnceLock}; + + fn env_lock() -> &'static Mutex<()> { + static LOCK: OnceLock> = OnceLock::new(); + LOCK.get_or_init(|| Mutex::new(())) + } fn unique_test_path(name: &str) -> PathBuf { let nanos = std::time::SystemTime::now() @@ -965,12 +949,31 @@ mod tests { #[test] fn socket_path_prefers_explicit_env_override() { + let _guard = env_lock().lock().unwrap(); let unique = format!("/tmp/herdr-test-{}.sock", std::process::id()); std::env::set_var(SOCKET_PATH_ENV_VAR, &unique); assert_eq!(socket_path(), PathBuf::from(&unique)); std::env::remove_var(SOCKET_PATH_ENV_VAR); } + #[test] + fn socket_path_defaults_to_config_dir_even_when_xdg_runtime_dir_is_set() { + let _guard = env_lock().lock().unwrap(); + let config_home = unique_test_path("socket-default-config-home"); + let runtime_dir = unique_test_path("socket-default-runtime"); + std::env::remove_var(SOCKET_PATH_ENV_VAR); + std::env::set_var("XDG_CONFIG_HOME", &config_home); + std::env::set_var("XDG_RUNTIME_DIR", &runtime_dir); + + let expected = config_home + .join(crate::config::app_dir_name()) + .join("herdr.sock"); + assert_eq!(socket_path(), expected); + + std::env::remove_var("XDG_CONFIG_HOME"); + std::env::remove_var("XDG_RUNTIME_DIR"); + } + #[test] fn restrict_socket_permissions_sets_user_only_mode() { let dir = unique_test_path("socket-perms"); -- cgit v1.2.3