1use chrono::{DateTime, Utc};
2use serde::{Deserialize, Serialize};
3use std::path::PathBuf;
4
5use crate::core::intent_protocol::{IntentRecord, IntentSource};
6
7const MAX_FINDINGS: usize = 20;
8const MAX_DECISIONS: usize = 10;
9const MAX_FILES: usize = 50;
10const MAX_EVIDENCE: usize = 500;
11const BATCH_SAVE_INTERVAL: u32 = 5;
12
13#[derive(Serialize, Deserialize, Clone, Debug)]
14pub struct SessionState {
15 pub id: String,
16 pub version: u32,
17 pub started_at: DateTime<Utc>,
18 pub updated_at: DateTime<Utc>,
19 pub project_root: Option<String>,
20 #[serde(default)]
21 pub shell_cwd: Option<String>,
22 pub task: Option<TaskInfo>,
23 pub findings: Vec<Finding>,
24 pub decisions: Vec<Decision>,
25 pub files_touched: Vec<FileTouched>,
26 pub test_results: Option<TestSnapshot>,
27 pub progress: Vec<ProgressEntry>,
28 pub next_steps: Vec<String>,
29 #[serde(default)]
30 pub evidence: Vec<EvidenceRecord>,
31 #[serde(default)]
32 pub intents: Vec<IntentRecord>,
33 #[serde(default)]
34 pub active_structured_intent: Option<crate::core::intent_engine::StructuredIntent>,
35 pub stats: SessionStats,
36}
37
38#[derive(Serialize, Deserialize, Clone, Debug)]
39pub struct TaskInfo {
40 pub description: String,
41 pub intent: Option<String>,
42 pub progress_pct: Option<u8>,
43}
44
45#[derive(Serialize, Deserialize, Clone, Debug)]
46pub struct Finding {
47 pub file: Option<String>,
48 pub line: Option<u32>,
49 pub summary: String,
50 pub timestamp: DateTime<Utc>,
51}
52
53#[derive(Serialize, Deserialize, Clone, Debug)]
54pub struct Decision {
55 pub summary: String,
56 pub rationale: Option<String>,
57 pub timestamp: DateTime<Utc>,
58}
59
60#[derive(Serialize, Deserialize, Clone, Debug)]
61pub struct FileTouched {
62 pub path: String,
63 pub file_ref: Option<String>,
64 pub read_count: u32,
65 pub modified: bool,
66 pub last_mode: String,
67 pub tokens: usize,
68}
69
70#[derive(Serialize, Deserialize, Clone, Debug)]
71pub struct TestSnapshot {
72 pub command: String,
73 pub passed: u32,
74 pub failed: u32,
75 pub total: u32,
76 pub timestamp: DateTime<Utc>,
77}
78
79#[derive(Serialize, Deserialize, Clone, Debug)]
80pub struct ProgressEntry {
81 pub action: String,
82 pub detail: Option<String>,
83 pub timestamp: DateTime<Utc>,
84}
85
86#[derive(Serialize, Deserialize, Clone, Debug)]
87#[serde(rename_all = "snake_case")]
88pub enum EvidenceKind {
89 ToolCall,
90 Manual,
91}
92
93#[derive(Serialize, Deserialize, Clone, Debug)]
94pub struct EvidenceRecord {
95 pub kind: EvidenceKind,
96 pub key: String,
97 pub value: Option<String>,
98 pub tool: Option<String>,
99 pub input_md5: Option<String>,
100 pub output_md5: Option<String>,
101 pub agent_id: Option<String>,
102 pub client_name: Option<String>,
103 pub timestamp: DateTime<Utc>,
104}
105
106#[derive(Serialize, Deserialize, Clone, Debug, Default)]
107#[serde(default)]
108pub struct SessionStats {
109 pub total_tool_calls: u32,
110 pub total_tokens_saved: u64,
111 pub total_tokens_input: u64,
112 pub cache_hits: u32,
113 pub files_read: u32,
114 pub commands_run: u32,
115 pub intents_inferred: u32,
116 pub intents_explicit: u32,
117 pub unsaved_changes: u32,
118}
119
120#[derive(Serialize, Deserialize, Clone, Debug)]
121struct LatestPointer {
122 id: String,
123}
124
125impl Default for SessionState {
126 fn default() -> Self {
127 Self::new()
128 }
129}
130
131impl SessionState {
132 pub fn new() -> Self {
133 let now = Utc::now();
134 Self {
135 id: generate_session_id(),
136 version: 0,
137 started_at: now,
138 updated_at: now,
139 project_root: None,
140 shell_cwd: None,
141 task: None,
142 findings: Vec::new(),
143 decisions: Vec::new(),
144 files_touched: Vec::new(),
145 test_results: None,
146 progress: Vec::new(),
147 next_steps: Vec::new(),
148 evidence: Vec::new(),
149 intents: Vec::new(),
150 active_structured_intent: None,
151 stats: SessionStats::default(),
152 }
153 }
154
155 pub fn increment(&mut self) {
156 self.version += 1;
157 self.updated_at = Utc::now();
158 self.stats.unsaved_changes += 1;
159 }
160
161 pub fn should_save(&self) -> bool {
162 self.stats.unsaved_changes >= BATCH_SAVE_INTERVAL
163 }
164
165 pub fn set_task(&mut self, description: &str, intent: Option<&str>) {
166 self.task = Some(TaskInfo {
167 description: description.to_string(),
168 intent: intent.map(|s| s.to_string()),
169 progress_pct: None,
170 });
171
172 let touched: Vec<String> = self.files_touched.iter().map(|f| f.path.clone()).collect();
173 let si = if touched.is_empty() {
174 crate::core::intent_engine::StructuredIntent::from_query(description)
175 } else {
176 crate::core::intent_engine::StructuredIntent::from_query_with_session(
177 description,
178 &touched,
179 )
180 };
181 if si.confidence >= 0.7 {
182 self.active_structured_intent = Some(si);
183 }
184
185 self.increment();
186 }
187
188 pub fn add_finding(&mut self, file: Option<&str>, line: Option<u32>, summary: &str) {
189 self.findings.push(Finding {
190 file: file.map(|s| s.to_string()),
191 line,
192 summary: summary.to_string(),
193 timestamp: Utc::now(),
194 });
195 while self.findings.len() > MAX_FINDINGS {
196 self.findings.remove(0);
197 }
198 self.increment();
199 }
200
201 pub fn add_decision(&mut self, summary: &str, rationale: Option<&str>) {
202 self.decisions.push(Decision {
203 summary: summary.to_string(),
204 rationale: rationale.map(|s| s.to_string()),
205 timestamp: Utc::now(),
206 });
207 while self.decisions.len() > MAX_DECISIONS {
208 self.decisions.remove(0);
209 }
210 self.increment();
211 }
212
213 pub fn touch_file(&mut self, path: &str, file_ref: Option<&str>, mode: &str, tokens: usize) {
214 if let Some(existing) = self.files_touched.iter_mut().find(|f| f.path == path) {
215 existing.read_count += 1;
216 existing.last_mode = mode.to_string();
217 existing.tokens = tokens;
218 if let Some(r) = file_ref {
219 existing.file_ref = Some(r.to_string());
220 }
221 } else {
222 self.files_touched.push(FileTouched {
223 path: path.to_string(),
224 file_ref: file_ref.map(|s| s.to_string()),
225 read_count: 1,
226 modified: false,
227 last_mode: mode.to_string(),
228 tokens,
229 });
230 while self.files_touched.len() > MAX_FILES {
231 self.files_touched.remove(0);
232 }
233 }
234 self.stats.files_read += 1;
235 self.increment();
236 }
237
238 pub fn mark_modified(&mut self, path: &str) {
239 if let Some(existing) = self.files_touched.iter_mut().find(|f| f.path == path) {
240 existing.modified = true;
241 }
242 self.increment();
243 }
244
245 pub fn record_tool_call(&mut self, tokens_saved: u64, tokens_input: u64) {
246 self.stats.total_tool_calls += 1;
247 self.stats.total_tokens_saved += tokens_saved;
248 self.stats.total_tokens_input += tokens_input;
249 }
250
251 pub fn record_intent(&mut self, mut intent: IntentRecord) {
252 if intent.occurrences == 0 {
253 intent.occurrences = 1;
254 }
255
256 if let Some(last) = self.intents.last_mut() {
257 if last.fingerprint() == intent.fingerprint() {
258 last.occurrences = last.occurrences.saturating_add(intent.occurrences);
259 last.timestamp = intent.timestamp;
260 match intent.source {
261 IntentSource::Inferred => self.stats.intents_inferred += 1,
262 IntentSource::Explicit => self.stats.intents_explicit += 1,
263 }
264 self.increment();
265 return;
266 }
267 }
268
269 match intent.source {
270 IntentSource::Inferred => self.stats.intents_inferred += 1,
271 IntentSource::Explicit => self.stats.intents_explicit += 1,
272 }
273
274 self.intents.push(intent);
275 while self.intents.len() > crate::core::budgets::INTENTS_PER_SESSION_LIMIT {
276 self.intents.remove(0);
277 }
278 self.increment();
279 }
280
281 pub fn record_tool_receipt(
282 &mut self,
283 tool: &str,
284 action: Option<&str>,
285 input_md5: &str,
286 output_md5: &str,
287 agent_id: Option<&str>,
288 client_name: Option<&str>,
289 ) {
290 let now = Utc::now();
291 let mut push = |key: String| {
292 self.evidence.push(EvidenceRecord {
293 kind: EvidenceKind::ToolCall,
294 key,
295 value: None,
296 tool: Some(tool.to_string()),
297 input_md5: Some(input_md5.to_string()),
298 output_md5: Some(output_md5.to_string()),
299 agent_id: agent_id.map(|s| s.to_string()),
300 client_name: client_name.map(|s| s.to_string()),
301 timestamp: now,
302 });
303 };
304
305 push(format!("tool:{tool}"));
306 if let Some(a) = action {
307 push(format!("tool:{tool}:{a}"));
308 }
309 while self.evidence.len() > MAX_EVIDENCE {
310 self.evidence.remove(0);
311 }
312 self.increment();
313 }
314
315 pub fn record_manual_evidence(&mut self, key: &str, value: Option<&str>) {
316 self.evidence.push(EvidenceRecord {
317 kind: EvidenceKind::Manual,
318 key: key.to_string(),
319 value: value.map(|s| s.to_string()),
320 tool: None,
321 input_md5: None,
322 output_md5: None,
323 agent_id: None,
324 client_name: None,
325 timestamp: Utc::now(),
326 });
327 while self.evidence.len() > MAX_EVIDENCE {
328 self.evidence.remove(0);
329 }
330 self.increment();
331 }
332
333 pub fn has_evidence_key(&self, key: &str) -> bool {
334 self.evidence.iter().any(|e| e.key == key)
335 }
336
337 pub fn record_cache_hit(&mut self) {
338 self.stats.cache_hits += 1;
339 }
340
341 pub fn record_command(&mut self) {
342 self.stats.commands_run += 1;
343 }
344
345 pub fn effective_cwd(&self, explicit_cwd: Option<&str>) -> String {
348 if let Some(cwd) = explicit_cwd {
349 if !cwd.is_empty() && cwd != "." {
350 return cwd.to_string();
351 }
352 }
353 if let Some(ref cwd) = self.shell_cwd {
354 return cwd.clone();
355 }
356 if let Some(ref root) = self.project_root {
357 return root.clone();
358 }
359 std::env::current_dir()
360 .map(|p| p.to_string_lossy().to_string())
361 .unwrap_or_else(|_| ".".to_string())
362 }
363
364 pub fn update_shell_cwd(&mut self, command: &str) {
368 let base = self.effective_cwd(None);
369 if let Some(new_cwd) = extract_cd_target(command, &base) {
370 let path = std::path::Path::new(&new_cwd);
371 if path.exists() && path.is_dir() {
372 self.shell_cwd = Some(
373 crate::core::pathutil::safe_canonicalize_or_self(path)
374 .to_string_lossy()
375 .to_string(),
376 );
377 }
378 }
379 }
380
381 pub fn format_compact(&self) -> String {
382 let duration = self.updated_at - self.started_at;
383 let hours = duration.num_hours();
384 let mins = duration.num_minutes() % 60;
385 let duration_str = if hours > 0 {
386 format!("{hours}h {mins}m")
387 } else {
388 format!("{mins}m")
389 };
390
391 let mut lines = Vec::new();
392 lines.push(format!(
393 "SESSION v{} | {} | {} calls | {} tok saved",
394 self.version, duration_str, self.stats.total_tool_calls, self.stats.total_tokens_saved
395 ));
396
397 if let Some(ref task) = self.task {
398 let pct = task
399 .progress_pct
400 .map_or(String::new(), |p| format!(" [{p}%]"));
401 lines.push(format!("Task: {}{pct}", task.description));
402 }
403
404 if let Some(ref root) = self.project_root {
405 lines.push(format!("Root: {}", shorten_path(root)));
406 }
407
408 if !self.findings.is_empty() {
409 let items: Vec<String> = self
410 .findings
411 .iter()
412 .rev()
413 .take(5)
414 .map(|f| {
415 let loc = match (&f.file, f.line) {
416 (Some(file), Some(line)) => format!("{}:{line}", shorten_path(file)),
417 (Some(file), None) => shorten_path(file),
418 _ => String::new(),
419 };
420 if loc.is_empty() {
421 f.summary.clone()
422 } else {
423 format!("{loc} \u{2014} {}", f.summary)
424 }
425 })
426 .collect();
427 lines.push(format!(
428 "Findings ({}): {}",
429 self.findings.len(),
430 items.join(" | ")
431 ));
432 }
433
434 if !self.decisions.is_empty() {
435 let items: Vec<&str> = self
436 .decisions
437 .iter()
438 .rev()
439 .take(3)
440 .map(|d| d.summary.as_str())
441 .collect();
442 lines.push(format!("Decisions: {}", items.join(" | ")));
443 }
444
445 if !self.files_touched.is_empty() {
446 let items: Vec<String> = self
447 .files_touched
448 .iter()
449 .rev()
450 .take(10)
451 .map(|f| {
452 let status = if f.modified { "mod" } else { &f.last_mode };
453 let r = f.file_ref.as_deref().unwrap_or("?");
454 format!("[{r} {} {status}]", shorten_path(&f.path))
455 })
456 .collect();
457 lines.push(format!(
458 "Files ({}): {}",
459 self.files_touched.len(),
460 items.join(" ")
461 ));
462 }
463
464 if let Some(ref tests) = self.test_results {
465 lines.push(format!(
466 "Tests: {}/{} pass ({})",
467 tests.passed, tests.total, tests.command
468 ));
469 }
470
471 if !self.next_steps.is_empty() {
472 lines.push(format!("Next: {}", self.next_steps.join(" | ")));
473 }
474
475 lines.join("\n")
476 }
477
478 pub fn build_compaction_snapshot(&self) -> String {
479 const MAX_SNAPSHOT_BYTES: usize = 2048;
480
481 let mut sections: Vec<(u8, String)> = Vec::new();
482
483 if let Some(ref task) = self.task {
484 let pct = task
485 .progress_pct
486 .map_or(String::new(), |p| format!(" [{p}%]"));
487 sections.push((1, format!("<task>{}{pct}</task>", task.description)));
488 }
489
490 if !self.files_touched.is_empty() {
491 let modified: Vec<&str> = self
492 .files_touched
493 .iter()
494 .filter(|f| f.modified)
495 .map(|f| f.path.as_str())
496 .collect();
497 let read_only: Vec<&str> = self
498 .files_touched
499 .iter()
500 .filter(|f| !f.modified)
501 .take(10)
502 .map(|f| f.path.as_str())
503 .collect();
504 let mut files_section = String::new();
505 if !modified.is_empty() {
506 files_section.push_str(&format!("Modified: {}", modified.join(", ")));
507 }
508 if !read_only.is_empty() {
509 if !files_section.is_empty() {
510 files_section.push_str(" | ");
511 }
512 files_section.push_str(&format!("Read: {}", read_only.join(", ")));
513 }
514 sections.push((1, format!("<files>{files_section}</files>")));
515 }
516
517 if !self.decisions.is_empty() {
518 let items: Vec<&str> = self.decisions.iter().map(|d| d.summary.as_str()).collect();
519 sections.push((2, format!("<decisions>{}</decisions>", items.join(" | "))));
520 }
521
522 if !self.findings.is_empty() {
523 let items: Vec<String> = self
524 .findings
525 .iter()
526 .rev()
527 .take(5)
528 .map(|f| f.summary.clone())
529 .collect();
530 sections.push((2, format!("<findings>{}</findings>", items.join(" | "))));
531 }
532
533 if !self.progress.is_empty() {
534 let items: Vec<String> = self
535 .progress
536 .iter()
537 .rev()
538 .take(5)
539 .map(|p| {
540 let detail = p.detail.as_deref().unwrap_or("");
541 if detail.is_empty() {
542 p.action.clone()
543 } else {
544 format!("{}: {detail}", p.action)
545 }
546 })
547 .collect();
548 sections.push((2, format!("<progress>{}</progress>", items.join(" | "))));
549 }
550
551 if let Some(ref tests) = self.test_results {
552 sections.push((
553 3,
554 format!(
555 "<tests>{}/{} pass ({})</tests>",
556 tests.passed, tests.total, tests.command
557 ),
558 ));
559 }
560
561 if !self.next_steps.is_empty() {
562 sections.push((
563 3,
564 format!("<next_steps>{}</next_steps>", self.next_steps.join(" | ")),
565 ));
566 }
567
568 sections.push((
569 4,
570 format!(
571 "<stats>calls={} saved={}tok</stats>",
572 self.stats.total_tool_calls, self.stats.total_tokens_saved
573 ),
574 ));
575
576 sections.sort_by_key(|(priority, _)| *priority);
577
578 let mut snapshot = String::from("<session_snapshot>\n");
579 for (_, section) in §ions {
580 if snapshot.len() + section.len() + 25 > MAX_SNAPSHOT_BYTES {
581 break;
582 }
583 snapshot.push_str(section);
584 snapshot.push('\n');
585 }
586 snapshot.push_str("</session_snapshot>");
587 snapshot
588 }
589
590 pub fn save_compaction_snapshot(&self) -> Result<String, String> {
591 let snapshot = self.build_compaction_snapshot();
592 let dir = sessions_dir().ok_or("cannot determine home directory")?;
593 if !dir.exists() {
594 std::fs::create_dir_all(&dir).map_err(|e| e.to_string())?;
595 }
596 let path = dir.join(format!("{}_snapshot.txt", self.id));
597 std::fs::write(&path, &snapshot).map_err(|e| e.to_string())?;
598 Ok(snapshot)
599 }
600
601 pub fn load_compaction_snapshot(session_id: &str) -> Option<String> {
602 let dir = sessions_dir()?;
603 let path = dir.join(format!("{session_id}_snapshot.txt"));
604 std::fs::read_to_string(&path).ok()
605 }
606
607 pub fn load_latest_snapshot() -> Option<String> {
608 let dir = sessions_dir()?;
609 let mut snapshots: Vec<(std::time::SystemTime, PathBuf)> = std::fs::read_dir(&dir)
610 .ok()?
611 .filter_map(|e| e.ok())
612 .filter(|e| e.path().to_string_lossy().ends_with("_snapshot.txt"))
613 .filter_map(|e| {
614 let meta = e.metadata().ok()?;
615 let modified = meta.modified().ok()?;
616 Some((modified, e.path()))
617 })
618 .collect();
619
620 snapshots.sort_by_key(|x| std::cmp::Reverse(x.0));
621 snapshots
622 .first()
623 .and_then(|(_, path)| std::fs::read_to_string(path).ok())
624 }
625
626 pub fn build_resume_block(&self) -> String {
629 let mut parts: Vec<String> = Vec::new();
630
631 if let Some(ref root) = self.project_root {
632 let short = root.rsplit('/').next().unwrap_or(root);
633 parts.push(format!("Project: {short}"));
634 }
635
636 if let Some(ref task) = self.task {
637 let pct = task
638 .progress_pct
639 .map_or(String::new(), |p| format!(" [{p}%]"));
640 parts.push(format!("Task: {}{pct}", task.description));
641 }
642
643 if !self.decisions.is_empty() {
644 let items: Vec<&str> = self
645 .decisions
646 .iter()
647 .rev()
648 .take(5)
649 .map(|d| d.summary.as_str())
650 .collect();
651 parts.push(format!("Decisions: {}", items.join("; ")));
652 }
653
654 if !self.files_touched.is_empty() {
655 let modified: Vec<&str> = self
656 .files_touched
657 .iter()
658 .filter(|f| f.modified)
659 .take(10)
660 .map(|f| f.path.as_str())
661 .collect();
662 if !modified.is_empty() {
663 parts.push(format!("Modified: {}", modified.join(", ")));
664 }
665 }
666
667 if !self.next_steps.is_empty() {
668 let steps: Vec<&str> = self.next_steps.iter().take(3).map(|s| s.as_str()).collect();
669 parts.push(format!("Next: {}", steps.join("; ")));
670 }
671
672 let archives = super::archive::list_entries(Some(&self.id));
673 if !archives.is_empty() {
674 let hints: Vec<String> = archives
675 .iter()
676 .take(5)
677 .map(|a| format!("{}({})", a.id, a.tool))
678 .collect();
679 parts.push(format!("Archives: {}", hints.join(", ")));
680 }
681
682 parts.push(format!(
683 "Stats: {} calls, {} tok saved",
684 self.stats.total_tool_calls, self.stats.total_tokens_saved
685 ));
686
687 format!(
688 "--- SESSION RESUME (post-compaction) ---\n{}\n---",
689 parts.join("\n")
690 )
691 }
692
693 pub fn save(&mut self) -> Result<(), String> {
694 let dir = sessions_dir().ok_or("cannot determine home directory")?;
695 if !dir.exists() {
696 std::fs::create_dir_all(&dir).map_err(|e| e.to_string())?;
697 }
698
699 let path = dir.join(format!("{}.json", self.id));
700 let json = serde_json::to_string_pretty(self).map_err(|e| e.to_string())?;
701
702 let tmp = dir.join(format!(".{}.json.tmp", self.id));
703 std::fs::write(&tmp, &json).map_err(|e| e.to_string())?;
704 std::fs::rename(&tmp, &path).map_err(|e| e.to_string())?;
705
706 let pointer = LatestPointer {
707 id: self.id.clone(),
708 };
709 let pointer_json = serde_json::to_string(&pointer).map_err(|e| e.to_string())?;
710 let latest_path = dir.join("latest.json");
711 let latest_tmp = dir.join(".latest.json.tmp");
712 std::fs::write(&latest_tmp, &pointer_json).map_err(|e| e.to_string())?;
713 std::fs::rename(&latest_tmp, &latest_path).map_err(|e| e.to_string())?;
714
715 self.stats.unsaved_changes = 0;
716 Ok(())
717 }
718
719 pub fn load_latest() -> Option<Self> {
720 let dir = sessions_dir()?;
721 let latest_path = dir.join("latest.json");
722 let pointer_json = std::fs::read_to_string(&latest_path).ok()?;
723 let pointer: LatestPointer = serde_json::from_str(&pointer_json).ok()?;
724 Self::load_by_id(&pointer.id)
725 }
726
727 pub fn load_latest_for_project_root(project_root: &str) -> Option<Self> {
728 let dir = sessions_dir()?;
729 let target_root =
730 crate::core::pathutil::safe_canonicalize_or_self(std::path::Path::new(project_root));
731 let mut latest_match: Option<Self> = None;
732
733 for entry in std::fs::read_dir(&dir).ok()?.flatten() {
734 let path = entry.path();
735 if path.extension().and_then(|e| e.to_str()) != Some("json") {
736 continue;
737 }
738 if path.file_name().and_then(|n| n.to_str()) == Some("latest.json") {
739 continue;
740 }
741
742 let Some(id) = path.file_stem().and_then(|n| n.to_str()) else {
743 continue;
744 };
745 let Some(session) = Self::load_by_id(id) else {
746 continue;
747 };
748
749 if !session_matches_project_root(&session, &target_root) {
750 continue;
751 }
752
753 if latest_match
754 .as_ref()
755 .is_none_or(|existing| session.updated_at > existing.updated_at)
756 {
757 latest_match = Some(session);
758 }
759 }
760
761 latest_match
762 }
763
764 pub fn load_by_id(id: &str) -> Option<Self> {
765 let dir = sessions_dir()?;
766 let path = dir.join(format!("{id}.json"));
767 let json = std::fs::read_to_string(&path).ok()?;
768 let session: Self = serde_json::from_str(&json).ok()?;
769 Some(normalize_loaded_session(session))
770 }
771
772 pub fn list_sessions() -> Vec<SessionSummary> {
773 let dir = match sessions_dir() {
774 Some(d) => d,
775 None => return Vec::new(),
776 };
777
778 let mut summaries = Vec::new();
779 if let Ok(entries) = std::fs::read_dir(&dir) {
780 for entry in entries.flatten() {
781 let path = entry.path();
782 if path.extension().and_then(|e| e.to_str()) != Some("json") {
783 continue;
784 }
785 if path.file_name().and_then(|n| n.to_str()) == Some("latest.json") {
786 continue;
787 }
788 if let Ok(json) = std::fs::read_to_string(&path) {
789 if let Ok(session) = serde_json::from_str::<SessionState>(&json) {
790 summaries.push(SessionSummary {
791 id: session.id,
792 started_at: session.started_at,
793 updated_at: session.updated_at,
794 version: session.version,
795 task: session.task.as_ref().map(|t| t.description.clone()),
796 tool_calls: session.stats.total_tool_calls,
797 tokens_saved: session.stats.total_tokens_saved,
798 });
799 }
800 }
801 }
802 }
803
804 summaries.sort_by_key(|x| std::cmp::Reverse(x.updated_at));
805 summaries
806 }
807
808 pub fn cleanup_old_sessions(max_age_days: i64) -> u32 {
809 let dir = match sessions_dir() {
810 Some(d) => d,
811 None => return 0,
812 };
813
814 let cutoff = Utc::now() - chrono::Duration::days(max_age_days);
815 let latest = Self::load_latest().map(|s| s.id);
816 let mut removed = 0u32;
817
818 if let Ok(entries) = std::fs::read_dir(&dir) {
819 for entry in entries.flatten() {
820 let path = entry.path();
821 if path.extension().and_then(|e| e.to_str()) != Some("json") {
822 continue;
823 }
824 let filename = path.file_stem().and_then(|n| n.to_str()).unwrap_or("");
825 if filename == "latest" || filename.starts_with('.') {
826 continue;
827 }
828 if latest.as_deref() == Some(filename) {
829 continue;
830 }
831 if let Ok(json) = std::fs::read_to_string(&path) {
832 if let Ok(session) = serde_json::from_str::<SessionState>(&json) {
833 if session.updated_at < cutoff && std::fs::remove_file(&path).is_ok() {
834 removed += 1;
835 }
836 }
837 }
838 }
839 }
840
841 removed
842 }
843}
844
845#[derive(Debug, Clone)]
846pub struct SessionSummary {
847 pub id: String,
848 pub started_at: DateTime<Utc>,
849 pub updated_at: DateTime<Utc>,
850 pub version: u32,
851 pub task: Option<String>,
852 pub tool_calls: u32,
853 pub tokens_saved: u64,
854}
855
856fn sessions_dir() -> Option<PathBuf> {
857 crate::core::data_dir::lean_ctx_data_dir()
858 .ok()
859 .map(|d| d.join("sessions"))
860}
861
862fn generate_session_id() -> String {
863 let now = Utc::now();
864 let ts = now.format("%Y%m%d-%H%M%S").to_string();
865 let random: u32 = (std::time::SystemTime::now()
866 .duration_since(std::time::UNIX_EPOCH)
867 .unwrap_or_default()
868 .subsec_nanos())
869 % 10000;
870 format!("{ts}-{random:04}")
871}
872
873fn extract_cd_target(command: &str, base_cwd: &str) -> Option<String> {
876 let first_cmd = command
877 .split("&&")
878 .next()
879 .unwrap_or(command)
880 .split(';')
881 .next()
882 .unwrap_or(command)
883 .trim();
884
885 if !first_cmd.starts_with("cd ") && first_cmd != "cd" {
886 return None;
887 }
888
889 let target = first_cmd.strip_prefix("cd")?.trim();
890 if target.is_empty() || target == "~" {
891 return dirs::home_dir().map(|h| h.to_string_lossy().to_string());
892 }
893
894 let target = target.trim_matches('"').trim_matches('\'');
895 let path = std::path::Path::new(target);
896
897 if path.is_absolute() {
898 Some(target.to_string())
899 } else {
900 let base = std::path::Path::new(base_cwd);
901 let joined = base.join(target).to_string_lossy().to_string();
902 Some(joined.replace('\\', "/"))
903 }
904}
905
906fn shorten_path(path: &str) -> String {
907 let parts: Vec<&str> = path.split('/').collect();
908 if parts.len() <= 2 {
909 return path.to_string();
910 }
911 let last_two: Vec<&str> = parts.iter().rev().take(2).copied().collect();
912 format!("…/{}/{}", last_two[1], last_two[0])
913}
914
915fn normalize_loaded_session(mut session: SessionState) -> SessionState {
916 if matches!(session.project_root.as_deref(), Some(r) if r.trim().is_empty()) {
917 session.project_root = None;
918 }
919 if matches!(session.shell_cwd.as_deref(), Some(c) if c.trim().is_empty()) {
920 session.shell_cwd = None;
921 }
922
923 if let (Some(ref root), Some(ref cwd)) = (&session.project_root, &session.shell_cwd) {
926 let root_p = std::path::Path::new(root);
927 let cwd_p = std::path::Path::new(cwd);
928 let root_looks_real = has_project_marker(root_p);
929 let cwd_looks_real = has_project_marker(cwd_p);
930
931 if !root_looks_real && cwd_looks_real && is_agent_or_temp_dir(root_p) {
932 session.project_root = Some(cwd.clone());
933 }
934 }
935
936 session
937}
938
939fn session_matches_project_root(session: &SessionState, target_root: &std::path::Path) -> bool {
940 if let Some(root) = session.project_root.as_deref() {
941 let root_path =
942 crate::core::pathutil::safe_canonicalize_or_self(std::path::Path::new(root));
943 if root_path == target_root {
944 return true;
945 }
946 if has_project_marker(&root_path) {
947 return false;
948 }
949 }
950
951 if let Some(cwd) = session.shell_cwd.as_deref() {
952 let cwd_path = crate::core::pathutil::safe_canonicalize_or_self(std::path::Path::new(cwd));
953 return cwd_path == target_root || cwd_path.starts_with(target_root);
954 }
955
956 false
957}
958
959fn has_project_marker(dir: &std::path::Path) -> bool {
960 const MARKERS: &[&str] = &[
961 ".git",
962 ".lean-ctx.toml",
963 "Cargo.toml",
964 "package.json",
965 "go.mod",
966 "pyproject.toml",
967 ".planning",
968 ];
969 MARKERS.iter().any(|m| dir.join(m).exists())
970}
971
972fn is_agent_or_temp_dir(dir: &std::path::Path) -> bool {
973 let s = dir.to_string_lossy();
974 s.contains("/.claude")
975 || s.contains("/.codex")
976 || s.contains("/var/folders/")
977 || s.contains("/tmp/")
978 || s.contains("\\.claude")
979 || s.contains("\\.codex")
980 || s.contains("\\AppData\\Local\\Temp")
981 || s.contains("\\Temp\\")
982}
983
984#[cfg(test)]
985mod tests {
986 use super::*;
987
988 #[test]
989 fn extract_cd_absolute_path() {
990 let result = extract_cd_target("cd /usr/local/bin", "/home/user");
991 assert_eq!(result, Some("/usr/local/bin".to_string()));
992 }
993
994 #[test]
995 fn extract_cd_relative_path() {
996 let result = extract_cd_target("cd subdir", "/home/user");
997 assert_eq!(result, Some("/home/user/subdir".to_string()));
998 }
999
1000 #[test]
1001 fn extract_cd_with_chained_command() {
1002 let result = extract_cd_target("cd /tmp && ls", "/home/user");
1003 assert_eq!(result, Some("/tmp".to_string()));
1004 }
1005
1006 #[test]
1007 fn extract_cd_with_semicolon() {
1008 let result = extract_cd_target("cd /tmp; ls", "/home/user");
1009 assert_eq!(result, Some("/tmp".to_string()));
1010 }
1011
1012 #[test]
1013 fn extract_cd_parent_dir() {
1014 let result = extract_cd_target("cd ..", "/home/user/project");
1015 assert_eq!(result, Some("/home/user/project/..".to_string()));
1016 }
1017
1018 #[test]
1019 fn extract_cd_no_cd_returns_none() {
1020 let result = extract_cd_target("ls -la", "/home/user");
1021 assert!(result.is_none());
1022 }
1023
1024 #[test]
1025 fn extract_cd_bare_cd_goes_home() {
1026 let result = extract_cd_target("cd", "/home/user");
1027 assert!(result.is_some());
1028 }
1029
1030 #[test]
1031 fn effective_cwd_explicit_takes_priority() {
1032 let mut session = SessionState::new();
1033 session.project_root = Some("/project".to_string());
1034 session.shell_cwd = Some("/project/src".to_string());
1035 assert_eq!(session.effective_cwd(Some("/explicit")), "/explicit");
1036 }
1037
1038 #[test]
1039 fn effective_cwd_shell_cwd_second_priority() {
1040 let mut session = SessionState::new();
1041 session.project_root = Some("/project".to_string());
1042 session.shell_cwd = Some("/project/src".to_string());
1043 assert_eq!(session.effective_cwd(None), "/project/src");
1044 }
1045
1046 #[test]
1047 fn effective_cwd_project_root_third_priority() {
1048 let mut session = SessionState::new();
1049 session.project_root = Some("/project".to_string());
1050 assert_eq!(session.effective_cwd(None), "/project");
1051 }
1052
1053 #[test]
1054 fn effective_cwd_dot_ignored() {
1055 let mut session = SessionState::new();
1056 session.project_root = Some("/project".to_string());
1057 assert_eq!(session.effective_cwd(Some(".")), "/project");
1058 }
1059
1060 #[test]
1061 fn compaction_snapshot_includes_task() {
1062 let mut session = SessionState::new();
1063 session.set_task("fix auth bug", None);
1064 let snapshot = session.build_compaction_snapshot();
1065 assert!(snapshot.contains("<task>fix auth bug</task>"));
1066 assert!(snapshot.contains("<session_snapshot>"));
1067 assert!(snapshot.contains("</session_snapshot>"));
1068 }
1069
1070 #[test]
1071 fn compaction_snapshot_includes_files() {
1072 let mut session = SessionState::new();
1073 session.touch_file("src/auth.rs", None, "full", 500);
1074 session.files_touched[0].modified = true;
1075 session.touch_file("src/main.rs", None, "map", 100);
1076 let snapshot = session.build_compaction_snapshot();
1077 assert!(snapshot.contains("auth.rs"));
1078 assert!(snapshot.contains("<files>"));
1079 }
1080
1081 #[test]
1082 fn compaction_snapshot_includes_decisions() {
1083 let mut session = SessionState::new();
1084 session.add_decision("Use JWT RS256", None);
1085 let snapshot = session.build_compaction_snapshot();
1086 assert!(snapshot.contains("JWT RS256"));
1087 assert!(snapshot.contains("<decisions>"));
1088 }
1089
1090 #[test]
1091 fn compaction_snapshot_respects_size_limit() {
1092 let mut session = SessionState::new();
1093 session.set_task("a]task", None);
1094 for i in 0..100 {
1095 session.add_finding(
1096 Some(&format!("file{i}.rs")),
1097 Some(i),
1098 &format!("Finding number {i} with some detail text here"),
1099 );
1100 }
1101 let snapshot = session.build_compaction_snapshot();
1102 assert!(snapshot.len() <= 2200);
1103 }
1104
1105 #[test]
1106 fn compaction_snapshot_includes_stats() {
1107 let mut session = SessionState::new();
1108 session.stats.total_tool_calls = 42;
1109 session.stats.total_tokens_saved = 10000;
1110 let snapshot = session.build_compaction_snapshot();
1111 assert!(snapshot.contains("calls=42"));
1112 assert!(snapshot.contains("saved=10000"));
1113 }
1114}