lean_ctx/core/session/
persistence.rs1use 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 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 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 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 pub fn load_latest() -> Option<Self> {
98 let cwd = std::env::current_dir().ok()?;
99 if crate::core::pathutil::is_broad_or_unsafe_root(&cwd) {
100 return None;
101 }
102 Self::load_latest_for_project_root(&cwd.to_string_lossy())
103 }
104
105 pub fn load_global_latest_pointer() -> Option<Self> {
110 let dir = sessions_dir()?;
111 let latest_path = dir.join("latest.json");
112 let pointer_json = std::fs::read_to_string(&latest_path).ok()?;
113 let pointer: LatestPointer = serde_json::from_str(&pointer_json).ok()?;
114 Self::load_by_id(&pointer.id)
115 }
116
117 pub fn load_latest_for_project_root(project_root: &str) -> Option<Self> {
119 let dir = sessions_dir()?;
120 let target_root =
121 crate::core::pathutil::safe_canonicalize_or_self(std::path::Path::new(project_root));
122 let mut latest_match: Option<Self> = None;
123
124 for entry in std::fs::read_dir(&dir).ok()?.flatten() {
125 let path = entry.path();
126 if path.extension().and_then(|e| e.to_str()) != Some("json") {
127 continue;
128 }
129 if path.file_name().and_then(|n| n.to_str()) == Some("latest.json") {
130 continue;
131 }
132
133 let Some(id) = path.file_stem().and_then(|n| n.to_str()) else {
134 continue;
135 };
136 let Some(session) = Self::load_by_id(id) else {
137 continue;
138 };
139
140 if !session_matches_project_root(&session, &target_root) {
141 continue;
142 }
143
144 if latest_match
145 .as_ref()
146 .is_none_or(|existing| session.updated_at > existing.updated_at)
147 {
148 latest_match = Some(session);
149 }
150 }
151
152 latest_match
153 }
154
155 pub fn load_by_id(id: &str) -> Option<Self> {
157 let dir = sessions_dir()?;
158 let path = dir.join(format!("{id}.json"));
159 let json = std::fs::read_to_string(&path).ok()?;
160 let session: Self = serde_json::from_str(&json).ok()?;
161 Some(normalize_loaded_session(session))
162 }
163
164 pub fn list_sessions() -> Vec<SessionSummary> {
166 let Some(dir) = sessions_dir() else {
167 return Vec::new();
168 };
169
170 let mut summaries = Vec::new();
171 if let Ok(entries) = std::fs::read_dir(&dir) {
172 for entry in entries.flatten() {
173 let path = entry.path();
174 if path.extension().and_then(|e| e.to_str()) != Some("json") {
175 continue;
176 }
177 if path.file_name().and_then(|n| n.to_str()) == Some("latest.json") {
178 continue;
179 }
180 if let Ok(json) = std::fs::read_to_string(&path) {
181 if let Ok(session) = serde_json::from_str::<SessionState>(&json) {
182 summaries.push(SessionSummary {
183 id: session.id,
184 started_at: session.started_at,
185 updated_at: session.updated_at,
186 version: session.version,
187 task: session.task.as_ref().map(|t| t.description.clone()),
188 tool_calls: session.stats.total_tool_calls,
189 tokens_saved: session.stats.total_tokens_saved,
190 });
191 }
192 }
193 }
194 }
195
196 summaries.sort_by_key(|x| std::cmp::Reverse(x.updated_at));
197 summaries
198 }
199
200 pub fn doctor_quarantine_unsafe_roots(apply: bool) -> (Vec<(String, String)>, usize) {
209 let mut found: Vec<(String, String)> = Vec::new();
210 let mut quarantined = 0usize;
211 let Some(dir) = sessions_dir() else {
212 return (found, quarantined);
213 };
214 let Ok(entries) = std::fs::read_dir(&dir) else {
215 return (found, quarantined);
216 };
217 for entry in entries.flatten() {
218 let path = entry.path();
219 if path.extension().and_then(|e| e.to_str()) != Some("json") {
220 continue;
221 }
222 let Some(id) = path.file_stem().and_then(|n| n.to_str()) else {
223 continue;
224 };
225 if id == "latest" || id.starts_with('.') {
226 continue;
227 }
228 let Some(session) = Self::load_by_id(id) else {
229 continue;
230 };
231 let Some(root) = session.project_root.as_deref() else {
232 continue;
233 };
234 let root_path = std::path::Path::new(root);
235 if crate::core::pathutil::is_broad_or_unsafe_root(root_path) {
236 found.push((id.to_string(), root.to_string()));
237 if apply {
238 let q_dir = dir.join("quarantine");
239 if std::fs::create_dir_all(&q_dir).is_ok()
240 && std::fs::rename(&path, q_dir.join(format!("{id}.json"))).is_ok()
241 {
242 quarantined += 1;
243 }
244 }
245 }
246 }
247 (found, quarantined)
248 }
249
250 pub fn cleanup_old_sessions(max_age_days: i64) -> u32 {
252 let Some(dir) = sessions_dir() else { return 0 };
253
254 let cutoff = Utc::now() - chrono::Duration::days(max_age_days);
255 let latest = Self::load_latest().map(|s| s.id);
256 let mut removed = 0u32;
257
258 if let Ok(entries) = std::fs::read_dir(&dir) {
259 for entry in entries.flatten() {
260 let path = entry.path();
261 if path.extension().and_then(|e| e.to_str()) != Some("json") {
262 continue;
263 }
264 let filename = path.file_stem().and_then(|n| n.to_str()).unwrap_or("");
265 if filename == "latest" || filename.starts_with('.') {
266 continue;
267 }
268 if latest.as_deref() == Some(filename) {
269 continue;
270 }
271 if let Ok(json) = std::fs::read_to_string(&path) {
272 if let Ok(session) = serde_json::from_str::<SessionState>(&json) {
273 if session.updated_at < cutoff && std::fs::remove_file(&path).is_ok() {
274 removed += 1;
275 }
276 }
277 }
278 }
279 }
280
281 removed
282 }
283}