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    /// Build a compact resume block for post-compaction injection.
627    /// Max ~500 tokens. Includes task, decisions, files, and archive references.
628    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    static COUNTER: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0);
864    let now = Utc::now();
865    let ts = now.format("%Y%m%d-%H%M%S").to_string();
866    let nanos = now.timestamp_subsec_micros();
867    let seq = COUNTER.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
868    format!("{ts}-{nanos:06}s{seq}")
869}
870
871/// Extracts the `cd` target from a command string.
872/// Handles patterns like `cd /foo`, `cd foo && bar`, `cd ../dir; cmd`, etc.
873fn extract_cd_target(command: &str, base_cwd: &str) -> Option<String> {
874    let first_cmd = command
875        .split("&&")
876        .next()
877        .unwrap_or(command)
878        .split(';')
879        .next()
880        .unwrap_or(command)
881        .trim();
882
883    if !first_cmd.starts_with("cd ") && first_cmd != "cd" {
884        return None;
885    }
886
887    let target = first_cmd.strip_prefix("cd")?.trim();
888    if target.is_empty() || target == "~" {
889        return dirs::home_dir().map(|h| h.to_string_lossy().to_string());
890    }
891
892    let target = target.trim_matches('"').trim_matches('\'');
893    let path = std::path::Path::new(target);
894
895    if path.is_absolute() {
896        Some(target.to_string())
897    } else {
898        let base = std::path::Path::new(base_cwd);
899        let joined = base.join(target).to_string_lossy().to_string();
900        Some(joined.replace('\\', "/"))
901    }
902}
903
904fn shorten_path(path: &str) -> String {
905    let parts: Vec<&str> = path.split('/').collect();
906    if parts.len() <= 2 {
907        return path.to_string();
908    }
909    let last_two: Vec<&str> = parts.iter().rev().take(2).copied().collect();
910    format!("…/{}/{}", last_two[1], last_two[0])
911}
912
913fn normalize_loaded_session(mut session: SessionState) -> SessionState {
914    if matches!(session.project_root.as_deref(), Some(r) if r.trim().is_empty()) {
915        session.project_root = None;
916    }
917    if matches!(session.shell_cwd.as_deref(), Some(c) if c.trim().is_empty()) {
918        session.shell_cwd = None;
919    }
920
921    // Heal stale project_root caused by agent/temp working directories.
922    // If project_root doesn't look like a real project root but shell_cwd does, prefer shell_cwd.
923    if let (Some(ref root), Some(ref cwd)) = (&session.project_root, &session.shell_cwd) {
924        let root_p = std::path::Path::new(root);
925        let cwd_p = std::path::Path::new(cwd);
926        let root_looks_real = has_project_marker(root_p);
927        let cwd_looks_real = has_project_marker(cwd_p);
928
929        if !root_looks_real && cwd_looks_real && is_agent_or_temp_dir(root_p) {
930            session.project_root = Some(cwd.clone());
931        }
932    }
933
934    session
935}
936
937fn session_matches_project_root(session: &SessionState, target_root: &std::path::Path) -> bool {
938    if let Some(root) = session.project_root.as_deref() {
939        let root_path =
940            crate::core::pathutil::safe_canonicalize_or_self(std::path::Path::new(root));
941        if root_path == target_root {
942            return true;
943        }
944        if has_project_marker(&root_path) {
945            return false;
946        }
947    }
948
949    if let Some(cwd) = session.shell_cwd.as_deref() {
950        let cwd_path = crate::core::pathutil::safe_canonicalize_or_self(std::path::Path::new(cwd));
951        return cwd_path == target_root || cwd_path.starts_with(target_root);
952    }
953
954    false
955}
956
957fn has_project_marker(dir: &std::path::Path) -> bool {
958    const MARKERS: &[&str] = &[
959        ".git",
960        ".lean-ctx.toml",
961        "Cargo.toml",
962        "package.json",
963        "go.mod",
964        "pyproject.toml",
965        ".planning",
966    ];
967    MARKERS.iter().any(|m| dir.join(m).exists())
968}
969
970fn is_agent_or_temp_dir(dir: &std::path::Path) -> bool {
971    let s = dir.to_string_lossy();
972    s.contains("/.claude")
973        || s.contains("/.codex")
974        || s.contains("/var/folders/")
975        || s.contains("/tmp/")
976        || s.contains("\\.claude")
977        || s.contains("\\.codex")
978        || s.contains("\\AppData\\Local\\Temp")
979        || s.contains("\\Temp\\")
980}
981
982#[cfg(test)]
983mod tests {
984    use super::*;
985
986    #[test]
987    fn extract_cd_absolute_path() {
988        let result = extract_cd_target("cd /usr/local/bin", "/home/user");
989        assert_eq!(result, Some("/usr/local/bin".to_string()));
990    }
991
992    #[test]
993    fn extract_cd_relative_path() {
994        let result = extract_cd_target("cd subdir", "/home/user");
995        assert_eq!(result, Some("/home/user/subdir".to_string()));
996    }
997
998    #[test]
999    fn extract_cd_with_chained_command() {
1000        let result = extract_cd_target("cd /tmp && ls", "/home/user");
1001        assert_eq!(result, Some("/tmp".to_string()));
1002    }
1003
1004    #[test]
1005    fn extract_cd_with_semicolon() {
1006        let result = extract_cd_target("cd /tmp; ls", "/home/user");
1007        assert_eq!(result, Some("/tmp".to_string()));
1008    }
1009
1010    #[test]
1011    fn extract_cd_parent_dir() {
1012        let result = extract_cd_target("cd ..", "/home/user/project");
1013        assert_eq!(result, Some("/home/user/project/..".to_string()));
1014    }
1015
1016    #[test]
1017    fn extract_cd_no_cd_returns_none() {
1018        let result = extract_cd_target("ls -la", "/home/user");
1019        assert!(result.is_none());
1020    }
1021
1022    #[test]
1023    fn extract_cd_bare_cd_goes_home() {
1024        let result = extract_cd_target("cd", "/home/user");
1025        assert!(result.is_some());
1026    }
1027
1028    #[test]
1029    fn effective_cwd_explicit_takes_priority() {
1030        let mut session = SessionState::new();
1031        session.project_root = Some("/project".to_string());
1032        session.shell_cwd = Some("/project/src".to_string());
1033        assert_eq!(session.effective_cwd(Some("/explicit")), "/explicit");
1034    }
1035
1036    #[test]
1037    fn effective_cwd_shell_cwd_second_priority() {
1038        let mut session = SessionState::new();
1039        session.project_root = Some("/project".to_string());
1040        session.shell_cwd = Some("/project/src".to_string());
1041        assert_eq!(session.effective_cwd(None), "/project/src");
1042    }
1043
1044    #[test]
1045    fn effective_cwd_project_root_third_priority() {
1046        let mut session = SessionState::new();
1047        session.project_root = Some("/project".to_string());
1048        assert_eq!(session.effective_cwd(None), "/project");
1049    }
1050
1051    #[test]
1052    fn effective_cwd_dot_ignored() {
1053        let mut session = SessionState::new();
1054        session.project_root = Some("/project".to_string());
1055        assert_eq!(session.effective_cwd(Some(".")), "/project");
1056    }
1057
1058    #[test]
1059    fn compaction_snapshot_includes_task() {
1060        let mut session = SessionState::new();
1061        session.set_task("fix auth bug", None);
1062        let snapshot = session.build_compaction_snapshot();
1063        assert!(snapshot.contains("<task>fix auth bug</task>"));
1064        assert!(snapshot.contains("<session_snapshot>"));
1065        assert!(snapshot.contains("</session_snapshot>"));
1066    }
1067
1068    #[test]
1069    fn compaction_snapshot_includes_files() {
1070        let mut session = SessionState::new();
1071        session.touch_file("src/auth.rs", None, "full", 500);
1072        session.files_touched[0].modified = true;
1073        session.touch_file("src/main.rs", None, "map", 100);
1074        let snapshot = session.build_compaction_snapshot();
1075        assert!(snapshot.contains("auth.rs"));
1076        assert!(snapshot.contains("<files>"));
1077    }
1078
1079    #[test]
1080    fn compaction_snapshot_includes_decisions() {
1081        let mut session = SessionState::new();
1082        session.add_decision("Use JWT RS256", None);
1083        let snapshot = session.build_compaction_snapshot();
1084        assert!(snapshot.contains("JWT RS256"));
1085        assert!(snapshot.contains("<decisions>"));
1086    }
1087
1088    #[test]
1089    fn compaction_snapshot_respects_size_limit() {
1090        let mut session = SessionState::new();
1091        session.set_task("a]task", None);
1092        for i in 0..100 {
1093            session.add_finding(
1094                Some(&format!("file{i}.rs")),
1095                Some(i),
1096                &format!("Finding number {i} with some detail text here"),
1097            );
1098        }
1099        let snapshot = session.build_compaction_snapshot();
1100        assert!(snapshot.len() <= 2200);
1101    }
1102
1103    #[test]
1104    fn compaction_snapshot_includes_stats() {
1105        let mut session = SessionState::new();
1106        session.stats.total_tool_calls = 42;
1107        session.stats.total_tokens_saved = 10000;
1108        let snapshot = session.build_compaction_snapshot();
1109        assert!(snapshot.contains("calls=42"));
1110        assert!(snapshot.contains("saved=10000"));
1111    }
1112}