diff options
| author | Can Celik <ogulcancelik@gmail.com> | 2026-04-21 16:30:04 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-04-21 16:30:04 +0300 |
| commit | 3397e1ce2922c4ae9b814e8110e81de7d201de81 (patch) | |
| tree | c457a7cf0af271dea1a412474fad5a33c1b60be8 /src/persist/io.rs | |
| parent | 2818ff492d45d17fd4f9b15478d703c6c664a8c0 (diff) | |
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
Diffstat (limited to 'src/persist/io.rs')
| -rw-r--r-- | src/persist/io.rs | 130 |
1 files changed, 130 insertions, 0 deletions
diff --git a/src/persist/io.rs b/src/persist/io.rs new file mode 100644 index 00000000..26e6fd4e --- /dev/null +++ b/src/persist/io.rs @@ -0,0 +1,130 @@ +use std::path::{Path, PathBuf}; + +use tracing::{error, info, warn}; + +use super::snapshot::{parse_snapshot, snapshot_file_version, SessionSnapshot, SNAPSHOT_VERSION}; + +fn session_path() -> PathBuf { + crate::config::config_dir().join("session.json") +} + +pub(super) fn save_to_path(path: &Path, snapshot: &SessionSnapshot) -> std::io::Result<()> { + if let Some(parent) = path.parent() { + std::fs::create_dir_all(parent)?; + } + let json = serde_json::to_string_pretty(snapshot)?; + let tmp_path = path.with_extension("json.tmp"); + std::fs::write(&tmp_path, &json)?; + if let Err(err) = std::fs::rename(&tmp_path, path) { + let _ = std::fs::remove_file(&tmp_path); + return Err(err); + } + Ok(()) +} + +pub(super) fn clear_path(path: &Path) -> std::io::Result<()> { + match std::fs::remove_file(path) { + Ok(()) => Ok(()), + Err(err) if err.kind() == std::io::ErrorKind::NotFound => Ok(()), + Err(err) => Err(err), + } +} + +pub fn save(snapshot: &SessionSnapshot) { + let path = session_path(); + if let Err(err) = save_to_path(&path, snapshot) { + error!(err = %err, path = %path.display(), "failed to save session"); + return; + } + info!(workspaces = snapshot.workspaces.len(), "session saved"); +} + +pub fn clear() { + let path = session_path(); + if let Err(err) = clear_path(&path) { + error!(err = %err, path = %path.display(), "failed to clear session"); + return; + } + info!(path = %path.display(), "session cleared"); +} + +pub fn load() -> Option<SessionSnapshot> { + let path = session_path(); + if !path.exists() { + return None; + } + let content = match std::fs::read_to_string(&path) { + Ok(content) => content, + Err(err) => { + warn!(err = %err, "failed to read session file"); + return None; + } + }; + match parse_snapshot(&content) { + Ok(snapshot) => Some(snapshot), + Err(err) => { + if let Some(version) = snapshot_file_version(&content) { + if version > SNAPSHOT_VERSION { + warn!( + file_version = version, + supported = SNAPSHOT_VERSION, + "session file is from a newer herdr version, ignoring" + ); + return None; + } + } + warn!(err = %err, "failed to parse session file, ignoring"); + None + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::app::state::AgentPanelScope; + + fn temp_session_path(name: &str) -> PathBuf { + let unique = format!( + "herdr-session-tests-{}-{}-{}", + name, + std::process::id(), + std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .unwrap() + .as_nanos() + ); + std::env::temp_dir().join(unique).join("session.json") + } + + fn empty_snapshot() -> SessionSnapshot { + SessionSnapshot { + version: SNAPSHOT_VERSION, + workspaces: vec![], + active: None, + selected: 0, + agent_panel_scope: AgentPanelScope::CurrentWorkspace, + sidebar_width: Some(26), + sidebar_section_split: Some(0.5), + } + } + + #[test] + fn clear_path_removes_existing_session_file() { + let path = temp_session_path("clear-existing"); + save_to_path(&path, &empty_snapshot()).unwrap(); + + clear_path(&path).unwrap(); + + assert!(!path.exists()); + } + + #[test] + fn clear_path_ignores_missing_session_file() { + let path = temp_session_path("clear-missing"); + + clear_path(&path).unwrap(); + + assert!(!path.exists()); + } +} |
