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