Skip to main content

lean_ctx/core/
session.rs

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    /// Returns the effective working directory for shell commands.
346    /// Priority: explicit cwd arg > session shell_cwd > project_root > process cwd
347    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    /// Updates shell_cwd by detecting `cd` in the command.
365    /// Handles: `cd /abs/path`, `cd rel/path` (relative to current cwd),
366    /// `cd ..`, and chained commands like `cd foo && ...`.
367    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 &sections {
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 save(&mut self) -> Result<(), String> {
627        let dir = sessions_dir().ok_or("cannot determine home directory")?;
628        if !dir.exists() {
629            std::fs::create_dir_all(&dir).map_err(|e| e.to_string())?;
630        }
631
632        let path = dir.join(format!("{}.json", self.id));
633        let json = serde_json::to_string_pretty(self).map_err(|e| e.to_string())?;
634
635        let tmp = dir.join(format!(".{}.json.tmp", self.id));
636        std::fs::write(&tmp, &json).map_err(|e| e.to_string())?;
637        std::fs::rename(&tmp, &path).map_err(|e| e.to_string())?;
638
639        let pointer = LatestPointer {
640            id: self.id.clone(),
641        };
642        let pointer_json = serde_json::to_string(&pointer).map_err(|e| e.to_string())?;
643        let latest_path = dir.join("latest.json");
644        let latest_tmp = dir.join(".latest.json.tmp");
645        std::fs::write(&latest_tmp, &pointer_json).map_err(|e| e.to_string())?;
646        std::fs::rename(&latest_tmp, &latest_path).map_err(|e| e.to_string())?;
647
648        self.stats.unsaved_changes = 0;
649        Ok(())
650    }
651
652    pub fn load_latest() -> Option<Self> {
653        let dir = sessions_dir()?;
654        let latest_path = dir.join("latest.json");
655        let pointer_json = std::fs::read_to_string(&latest_path).ok()?;
656        let pointer: LatestPointer = serde_json::from_str(&pointer_json).ok()?;
657        Self::load_by_id(&pointer.id)
658    }
659
660    pub fn load_by_id(id: &str) -> Option<Self> {
661        let dir = sessions_dir()?;
662        let path = dir.join(format!("{id}.json"));
663        let json = std::fs::read_to_string(&path).ok()?;
664        let mut session: Self = serde_json::from_str(&json).ok()?;
665        // Legacy/malformed sessions may serialize empty strings instead of null.
666        if matches!(session.project_root.as_deref(), Some(r) if r.trim().is_empty()) {
667            session.project_root = None;
668        }
669        if matches!(session.shell_cwd.as_deref(), Some(c) if c.trim().is_empty()) {
670            session.shell_cwd = None;
671        }
672
673        // Heal stale project_root caused by agent/temp working directories.
674        // If project_root doesn't look like a real project root but shell_cwd does, prefer shell_cwd.
675        if let (Some(ref root), Some(ref cwd)) = (&session.project_root, &session.shell_cwd) {
676            fn has_marker(dir: &std::path::Path) -> bool {
677                const MARKERS: &[&str] = &[
678                    ".git",
679                    ".lean-ctx.toml",
680                    "Cargo.toml",
681                    "package.json",
682                    "go.mod",
683                    "pyproject.toml",
684                    ".planning",
685                ];
686                MARKERS.iter().any(|m| dir.join(m).exists())
687            }
688            fn is_agent_or_temp_dir(dir: &std::path::Path) -> bool {
689                let s = dir.to_string_lossy();
690                s.contains("/.claude")
691                    || s.contains("/.codex")
692                    || s.contains("/var/folders/")
693                    || s.contains("/tmp/")
694                    || s.contains("\\.claude")
695                    || s.contains("\\.codex")
696                    || s.contains("\\AppData\\Local\\Temp")
697                    || s.contains("\\Temp\\")
698            }
699
700            let root_p = std::path::Path::new(root);
701            let cwd_p = std::path::Path::new(cwd);
702            let root_looks_real = has_marker(root_p);
703            let cwd_looks_real = has_marker(cwd_p);
704
705            if !root_looks_real && cwd_looks_real && is_agent_or_temp_dir(root_p) {
706                session.project_root = Some(cwd.clone());
707            }
708        }
709        Some(session)
710    }
711
712    pub fn list_sessions() -> Vec<SessionSummary> {
713        let dir = match sessions_dir() {
714            Some(d) => d,
715            None => return Vec::new(),
716        };
717
718        let mut summaries = Vec::new();
719        if let Ok(entries) = std::fs::read_dir(&dir) {
720            for entry in entries.flatten() {
721                let path = entry.path();
722                if path.extension().and_then(|e| e.to_str()) != Some("json") {
723                    continue;
724                }
725                if path.file_name().and_then(|n| n.to_str()) == Some("latest.json") {
726                    continue;
727                }
728                if let Ok(json) = std::fs::read_to_string(&path) {
729                    if let Ok(session) = serde_json::from_str::<SessionState>(&json) {
730                        summaries.push(SessionSummary {
731                            id: session.id,
732                            started_at: session.started_at,
733                            updated_at: session.updated_at,
734                            version: session.version,
735                            task: session.task.as_ref().map(|t| t.description.clone()),
736                            tool_calls: session.stats.total_tool_calls,
737                            tokens_saved: session.stats.total_tokens_saved,
738                        });
739                    }
740                }
741            }
742        }
743
744        summaries.sort_by_key(|x| std::cmp::Reverse(x.updated_at));
745        summaries
746    }
747
748    pub fn cleanup_old_sessions(max_age_days: i64) -> u32 {
749        let dir = match sessions_dir() {
750            Some(d) => d,
751            None => return 0,
752        };
753
754        let cutoff = Utc::now() - chrono::Duration::days(max_age_days);
755        let latest = Self::load_latest().map(|s| s.id);
756        let mut removed = 0u32;
757
758        if let Ok(entries) = std::fs::read_dir(&dir) {
759            for entry in entries.flatten() {
760                let path = entry.path();
761                if path.extension().and_then(|e| e.to_str()) != Some("json") {
762                    continue;
763                }
764                let filename = path.file_stem().and_then(|n| n.to_str()).unwrap_or("");
765                if filename == "latest" || filename.starts_with('.') {
766                    continue;
767                }
768                if latest.as_deref() == Some(filename) {
769                    continue;
770                }
771                if let Ok(json) = std::fs::read_to_string(&path) {
772                    if let Ok(session) = serde_json::from_str::<SessionState>(&json) {
773                        if session.updated_at < cutoff && std::fs::remove_file(&path).is_ok() {
774                            removed += 1;
775                        }
776                    }
777                }
778            }
779        }
780
781        removed
782    }
783}
784
785#[derive(Debug, Clone)]
786pub struct SessionSummary {
787    pub id: String,
788    pub started_at: DateTime<Utc>,
789    pub updated_at: DateTime<Utc>,
790    pub version: u32,
791    pub task: Option<String>,
792    pub tool_calls: u32,
793    pub tokens_saved: u64,
794}
795
796fn sessions_dir() -> Option<PathBuf> {
797    crate::core::data_dir::lean_ctx_data_dir()
798        .ok()
799        .map(|d| d.join("sessions"))
800}
801
802fn generate_session_id() -> String {
803    let now = Utc::now();
804    let ts = now.format("%Y%m%d-%H%M%S").to_string();
805    let random: u32 = (std::time::SystemTime::now()
806        .duration_since(std::time::UNIX_EPOCH)
807        .unwrap_or_default()
808        .subsec_nanos())
809        % 10000;
810    format!("{ts}-{random:04}")
811}
812
813/// Extracts the `cd` target from a command string.
814/// Handles patterns like `cd /foo`, `cd foo && bar`, `cd ../dir; cmd`, etc.
815fn extract_cd_target(command: &str, base_cwd: &str) -> Option<String> {
816    let first_cmd = command
817        .split("&&")
818        .next()
819        .unwrap_or(command)
820        .split(';')
821        .next()
822        .unwrap_or(command)
823        .trim();
824
825    if !first_cmd.starts_with("cd ") && first_cmd != "cd" {
826        return None;
827    }
828
829    let target = first_cmd.strip_prefix("cd")?.trim();
830    if target.is_empty() || target == "~" {
831        return dirs::home_dir().map(|h| h.to_string_lossy().to_string());
832    }
833
834    let target = target.trim_matches('"').trim_matches('\'');
835    let path = std::path::Path::new(target);
836
837    if path.is_absolute() {
838        Some(target.to_string())
839    } else {
840        let base = std::path::Path::new(base_cwd);
841        let joined = base.join(target).to_string_lossy().to_string();
842        Some(joined.replace('\\', "/"))
843    }
844}
845
846fn shorten_path(path: &str) -> String {
847    let parts: Vec<&str> = path.split('/').collect();
848    if parts.len() <= 2 {
849        return path.to_string();
850    }
851    let last_two: Vec<&str> = parts.iter().rev().take(2).copied().collect();
852    format!("…/{}/{}", last_two[1], last_two[0])
853}
854
855#[cfg(test)]
856mod tests {
857    use super::*;
858
859    #[test]
860    fn extract_cd_absolute_path() {
861        let result = extract_cd_target("cd /usr/local/bin", "/home/user");
862        assert_eq!(result, Some("/usr/local/bin".to_string()));
863    }
864
865    #[test]
866    fn extract_cd_relative_path() {
867        let result = extract_cd_target("cd subdir", "/home/user");
868        assert_eq!(result, Some("/home/user/subdir".to_string()));
869    }
870
871    #[test]
872    fn extract_cd_with_chained_command() {
873        let result = extract_cd_target("cd /tmp && ls", "/home/user");
874        assert_eq!(result, Some("/tmp".to_string()));
875    }
876
877    #[test]
878    fn extract_cd_with_semicolon() {
879        let result = extract_cd_target("cd /tmp; ls", "/home/user");
880        assert_eq!(result, Some("/tmp".to_string()));
881    }
882
883    #[test]
884    fn extract_cd_parent_dir() {
885        let result = extract_cd_target("cd ..", "/home/user/project");
886        assert_eq!(result, Some("/home/user/project/..".to_string()));
887    }
888
889    #[test]
890    fn extract_cd_no_cd_returns_none() {
891        let result = extract_cd_target("ls -la", "/home/user");
892        assert!(result.is_none());
893    }
894
895    #[test]
896    fn extract_cd_bare_cd_goes_home() {
897        let result = extract_cd_target("cd", "/home/user");
898        assert!(result.is_some());
899    }
900
901    #[test]
902    fn effective_cwd_explicit_takes_priority() {
903        let mut session = SessionState::new();
904        session.project_root = Some("/project".to_string());
905        session.shell_cwd = Some("/project/src".to_string());
906        assert_eq!(session.effective_cwd(Some("/explicit")), "/explicit");
907    }
908
909    #[test]
910    fn effective_cwd_shell_cwd_second_priority() {
911        let mut session = SessionState::new();
912        session.project_root = Some("/project".to_string());
913        session.shell_cwd = Some("/project/src".to_string());
914        assert_eq!(session.effective_cwd(None), "/project/src");
915    }
916
917    #[test]
918    fn effective_cwd_project_root_third_priority() {
919        let mut session = SessionState::new();
920        session.project_root = Some("/project".to_string());
921        assert_eq!(session.effective_cwd(None), "/project");
922    }
923
924    #[test]
925    fn effective_cwd_dot_ignored() {
926        let mut session = SessionState::new();
927        session.project_root = Some("/project".to_string());
928        assert_eq!(session.effective_cwd(Some(".")), "/project");
929    }
930
931    #[test]
932    fn compaction_snapshot_includes_task() {
933        let mut session = SessionState::new();
934        session.set_task("fix auth bug", None);
935        let snapshot = session.build_compaction_snapshot();
936        assert!(snapshot.contains("<task>fix auth bug</task>"));
937        assert!(snapshot.contains("<session_snapshot>"));
938        assert!(snapshot.contains("</session_snapshot>"));
939    }
940
941    #[test]
942    fn compaction_snapshot_includes_files() {
943        let mut session = SessionState::new();
944        session.touch_file("src/auth.rs", None, "full", 500);
945        session.files_touched[0].modified = true;
946        session.touch_file("src/main.rs", None, "map", 100);
947        let snapshot = session.build_compaction_snapshot();
948        assert!(snapshot.contains("auth.rs"));
949        assert!(snapshot.contains("<files>"));
950    }
951
952    #[test]
953    fn compaction_snapshot_includes_decisions() {
954        let mut session = SessionState::new();
955        session.add_decision("Use JWT RS256", None);
956        let snapshot = session.build_compaction_snapshot();
957        assert!(snapshot.contains("JWT RS256"));
958        assert!(snapshot.contains("<decisions>"));
959    }
960
961    #[test]
962    fn compaction_snapshot_respects_size_limit() {
963        let mut session = SessionState::new();
964        session.set_task("a]task", None);
965        for i in 0..100 {
966            session.add_finding(
967                Some(&format!("file{i}.rs")),
968                Some(i),
969                &format!("Finding number {i} with some detail text here"),
970            );
971        }
972        let snapshot = session.build_compaction_snapshot();
973        assert!(snapshot.len() <= 2200);
974    }
975
976    #[test]
977    fn compaction_snapshot_includes_stats() {
978        let mut session = SessionState::new();
979        session.stats.total_tool_calls = 42;
980        session.stats.total_tokens_saved = 10000;
981        let snapshot = session.build_compaction_snapshot();
982        assert!(snapshot.contains("calls=42"));
983        assert!(snapshot.contains("saved=10000"));
984    }
985}