Skip to main content

lean_ctx/core/session/
persistence.rs

1use chrono::Utc;
2
3use super::heuristics::{normalize_loaded_session, session_matches_project_root};
4use super::paths::sessions_dir;
5use super::state::BATCH_SAVE_INTERVAL;
6#[allow(clippy::wildcard_imports)]
7use super::types::*;
8
9#[cfg(unix)]
10fn restrict_file_permissions(path: &std::path::Path) {
11    use std::os::unix::fs::PermissionsExt;
12    let perms = std::fs::Permissions::from_mode(0o600);
13    let _ = std::fs::set_permissions(path, perms);
14}
15
16#[cfg(not(unix))]
17fn restrict_file_permissions(_path: &std::path::Path) {}
18
19impl PreparedSave {
20    /// Writes the pre-serialized session data, latest pointer, and compaction
21    /// snapshot to disk atomically.
22    pub fn write_to_disk(self) -> Result<(), String> {
23        if !self.dir.exists() {
24            std::fs::create_dir_all(&self.dir).map_err(|e| e.to_string())?;
25        }
26        let path = self.dir.join(format!("{}.json", self.id));
27        let tmp = self.dir.join(format!(".{}.json.tmp", self.id));
28        std::fs::write(&tmp, &self.json).map_err(|e| e.to_string())?;
29        restrict_file_permissions(&tmp);
30        std::fs::rename(&tmp, &path).map_err(|e| e.to_string())?;
31
32        let latest_path = self.dir.join("latest.json");
33        let latest_tmp = self.dir.join(".latest.json.tmp");
34        std::fs::write(&latest_tmp, &self.pointer_json).map_err(|e| e.to_string())?;
35        restrict_file_permissions(&latest_tmp);
36        std::fs::rename(&latest_tmp, &latest_path).map_err(|e| e.to_string())?;
37
38        if let Some(snapshot) = self.compaction_snapshot {
39            let snap_path = self.dir.join(format!("{}_snapshot.txt", self.id));
40            let _ = std::fs::write(&snap_path, &snapshot);
41            restrict_file_permissions(&snap_path);
42        }
43        Ok(())
44    }
45}
46
47impl SessionState {
48    /// Serializes and writes the session state to disk synchronously.
49    pub fn save(&mut self) -> Result<(), String> {
50        let prepared = self.prepare_save()?;
51        match prepared.write_to_disk() {
52            Ok(()) => Ok(()),
53            Err(e) => {
54                self.stats.unsaved_changes = BATCH_SAVE_INTERVAL;
55                Err(e)
56            }
57        }
58    }
59
60    /// Serialize session state while holding the lock (CPU-only), reset the
61    /// unsaved counter, and return a `PreparedSave` whose I/O can be deferred
62    /// to a background thread via `write_to_disk()`.
63    pub fn prepare_save(&mut self) -> Result<PreparedSave, String> {
64        let dir = sessions_dir().ok_or("cannot determine home directory")?;
65        let compaction_snapshot = if self.stats.total_tool_calls > 0 {
66            Some(self.build_compaction_snapshot())
67        } else {
68            None
69        };
70        let json = serde_json::to_string_pretty(self).map_err(|e| e.to_string())?;
71        let pointer_json = serde_json::to_string(&LatestPointer {
72            id: self.id.clone(),
73        })
74        .map_err(|e| e.to_string())?;
75        self.stats.unsaved_changes = 0;
76        Ok(PreparedSave {
77            dir,
78            id: self.id.clone(),
79            json,
80            pointer_json,
81            compaction_snapshot,
82        })
83    }
84
85    /// Loads the most recent session from disk.
86    ///
87    /// Prefers the session matching the current working directory's project root.
88    /// Falls back to the global `latest.json` pointer only if no project-scoped
89    /// match is found. This prevents cross-project session leakage.
90    pub fn load_latest() -> Option<Self> {
91        if let Some(project_root) = std::env::current_dir()
92            .ok()
93            .map(|p| p.to_string_lossy().to_string())
94        {
95            if let Some(session) = Self::load_latest_for_project_root(&project_root) {
96                return Some(session);
97            }
98        }
99        let dir = sessions_dir()?;
100        let latest_path = dir.join("latest.json");
101        let pointer_json = std::fs::read_to_string(&latest_path).ok()?;
102        let pointer: LatestPointer = serde_json::from_str(&pointer_json).ok()?;
103        Self::load_by_id(&pointer.id)
104    }
105
106    /// Loads the most recent session matching a specific project root.
107    pub fn load_latest_for_project_root(project_root: &str) -> Option<Self> {
108        let dir = sessions_dir()?;
109        let target_root =
110            crate::core::pathutil::safe_canonicalize_or_self(std::path::Path::new(project_root));
111        let mut latest_match: Option<Self> = None;
112
113        for entry in std::fs::read_dir(&dir).ok()?.flatten() {
114            let path = entry.path();
115            if path.extension().and_then(|e| e.to_str()) != Some("json") {
116                continue;
117            }
118            if path.file_name().and_then(|n| n.to_str()) == Some("latest.json") {
119                continue;
120            }
121
122            let Some(id) = path.file_stem().and_then(|n| n.to_str()) else {
123                continue;
124            };
125            let Some(session) = Self::load_by_id(id) else {
126                continue;
127            };
128
129            if !session_matches_project_root(&session, &target_root) {
130                continue;
131            }
132
133            if latest_match
134                .as_ref()
135                .is_none_or(|existing| session.updated_at > existing.updated_at)
136            {
137                latest_match = Some(session);
138            }
139        }
140
141        latest_match
142    }
143
144    /// Loads a specific session from disk by its unique ID.
145    pub fn load_by_id(id: &str) -> Option<Self> {
146        let dir = sessions_dir()?;
147        let path = dir.join(format!("{id}.json"));
148        let json = std::fs::read_to_string(&path).ok()?;
149        let session: Self = serde_json::from_str(&json).ok()?;
150        Some(normalize_loaded_session(session))
151    }
152
153    /// Lists all saved sessions as summaries, sorted by most recently updated.
154    pub fn list_sessions() -> Vec<SessionSummary> {
155        let Some(dir) = sessions_dir() else {
156            return Vec::new();
157        };
158
159        let mut summaries = Vec::new();
160        if let Ok(entries) = std::fs::read_dir(&dir) {
161            for entry in entries.flatten() {
162                let path = entry.path();
163                if path.extension().and_then(|e| e.to_str()) != Some("json") {
164                    continue;
165                }
166                if path.file_name().and_then(|n| n.to_str()) == Some("latest.json") {
167                    continue;
168                }
169                if let Ok(json) = std::fs::read_to_string(&path) {
170                    if let Ok(session) = serde_json::from_str::<SessionState>(&json) {
171                        summaries.push(SessionSummary {
172                            id: session.id,
173                            started_at: session.started_at,
174                            updated_at: session.updated_at,
175                            version: session.version,
176                            task: session.task.as_ref().map(|t| t.description.clone()),
177                            tool_calls: session.stats.total_tool_calls,
178                            tokens_saved: session.stats.total_tokens_saved,
179                        });
180                    }
181                }
182            }
183        }
184
185        summaries.sort_by_key(|x| std::cmp::Reverse(x.updated_at));
186        summaries
187    }
188
189    /// Deletes sessions older than `max_age_days`, preserving the latest. Returns count removed.
190    pub fn cleanup_old_sessions(max_age_days: i64) -> u32 {
191        let Some(dir) = sessions_dir() else { return 0 };
192
193        let cutoff = Utc::now() - chrono::Duration::days(max_age_days);
194        let latest = Self::load_latest().map(|s| s.id);
195        let mut removed = 0u32;
196
197        if let Ok(entries) = std::fs::read_dir(&dir) {
198            for entry in entries.flatten() {
199                let path = entry.path();
200                if path.extension().and_then(|e| e.to_str()) != Some("json") {
201                    continue;
202                }
203                let filename = path.file_stem().and_then(|n| n.to_str()).unwrap_or("");
204                if filename == "latest" || filename.starts_with('.') {
205                    continue;
206                }
207                if latest.as_deref() == Some(filename) {
208                    continue;
209                }
210                if let Ok(json) = std::fs::read_to_string(&path) {
211                    if let Ok(session) = serde_json::from_str::<SessionState>(&json) {
212                        if session.updated_at < cutoff && std::fs::remove_file(&path).is_ok() {
213                            removed += 1;
214                        }
215                    }
216                }
217            }
218        }
219
220        removed
221    }
222}