Skip to main content

lean_ctx/tools/
ctx_overview.rs

1use crate::core::cache::SessionCache;
2use crate::core::task_relevance::{compute_relevance, parse_task_hints};
3use crate::core::tokens::count_tokens;
4use crate::tools::CrpMode;
5
6/// Multi-resolution context overview.
7///
8/// Provides a compact map of the entire project, organized by task relevance.
9/// Files are shown at different detail levels based on their relevance score:
10/// - Level 0 (full): directly task-relevant files → full content (use ctx_read)
11/// - Level 1 (signatures): graph neighbors → key signatures
12/// - Level 2 (reference): distant files → name + line count only
13///
14/// This implements lazy evaluation for context: start with the overview,
15/// then zoom into specific files as needed.
16pub fn handle(
17    _cache: &SessionCache,
18    task: Option<&str>,
19    path: Option<&str>,
20    _crp_mode: CrpMode,
21) -> String {
22    let project_root = path.map_or_else(|| ".".to_string(), std::string::ToString::to_string);
23
24    let auto_loaded = crate::core::context_package::auto_load_packages(&project_root);
25
26    let Some(index) = crate::core::index_orchestrator::try_load_graph_index(&project_root) else {
27        crate::core::index_orchestrator::ensure_all_background(&project_root);
28        return format!(
29            "INDEXING IN PROGRESS\n\n\
30            The knowledge graph for this project is being built in the background.\n\
31            Project: {project_root}\n\n\
32            Because this is a large project, the initial scan may take a moment.\n\
33            Please try this command again in 1-2 minutes."
34        );
35    };
36
37    let (task_files, task_keywords) = if let Some(task_desc) = task {
38        parse_task_hints(task_desc)
39    } else {
40        (vec![], vec![])
41    };
42
43    let has_task = !task_files.is_empty() || !task_keywords.is_empty();
44
45    let mut output = Vec::new();
46
47    if has_task {
48        let relevance = compute_relevance(&index, &task_files, &task_keywords);
49
50        // Static project-level header first (prefix-cache-friendly)
51        output.push(format!(
52            "PROJECT OVERVIEW  {} files  task-filtered",
53            index.files.len()
54        ));
55        output.push(String::new());
56
57        let high: Vec<&_> = relevance.iter().filter(|r| r.score >= 0.8).collect();
58        let medium: Vec<&_> = relevance
59            .iter()
60            .filter(|r| r.score >= 0.3 && r.score < 0.8)
61            .collect();
62        let low: Vec<&_> = relevance.iter().filter(|r| r.score < 0.3).collect();
63
64        if !high.is_empty() {
65            use crate::core::context_field::{ContextItemId, ContextKind, ViewCosts};
66            use crate::core::context_handles::HandleRegistry;
67
68            let mut handle_reg = HandleRegistry::new();
69            output.push("▸ DIRECTLY RELEVANT (use ctx_read or ctx_expand @ref):".to_string());
70            for r in &high {
71                let line_count = file_line_count(&r.path);
72                let item_id = ContextItemId::from_file(&r.path);
73                let view_costs = ViewCosts::from_full_tokens(line_count * 5);
74                let handle = handle_reg.register(
75                    item_id,
76                    ContextKind::File,
77                    &r.path,
78                    &format!(
79                        "{} {}L score={:.1}",
80                        short_path(&r.path),
81                        line_count,
82                        r.score
83                    ),
84                    &view_costs,
85                    r.score,
86                    false,
87                );
88                output.push(format!(
89                    "  @{} {} {}L  phi={:.2}  mode={}",
90                    handle.ref_label,
91                    short_path(&r.path),
92                    line_count,
93                    r.score,
94                    r.recommended_mode
95                ));
96            }
97            output.push(String::new());
98        }
99
100        if !medium.is_empty() {
101            output.push("▸ CONTEXT (use ctx_read signatures/map):".to_string());
102            for r in medium.iter().take(20) {
103                let line_count = file_line_count(&r.path);
104                output.push(format!(
105                    "  {} {line_count}L  mode={}",
106                    short_path(&r.path),
107                    r.recommended_mode
108                ));
109            }
110            if medium.len() > 20 {
111                output.push(format!("  ... +{} more", medium.len() - 20));
112            }
113            output.push(String::new());
114        }
115
116        if !low.is_empty() {
117            output.push(format!(
118                "▸ DISTANT ({} files, not loaded unless needed)",
119                low.len()
120            ));
121            for r in low.iter().take(10) {
122                output.push(format!("  {}", short_path(&r.path)));
123            }
124            if low.len() > 10 {
125                output.push(format!("  ... +{} more", low.len() - 10));
126            }
127        }
128
129        // Dynamic task-specific briefing last (prefix-cache-friendly)
130        if let Some(task_desc) = task {
131            let file_context: Vec<(String, usize)> = relevance
132                .iter()
133                .filter(|r| r.score >= 0.3)
134                .take(8)
135                .filter_map(|r| {
136                    std::fs::read_to_string(&r.path)
137                        .ok()
138                        .map(|c| (r.path.clone(), c.lines().count()))
139                })
140                .collect();
141            let briefing = crate::core::task_briefing::build_briefing(task_desc, &file_context);
142            output.push(String::new());
143            output.push(crate::core::task_briefing::format_briefing(&briefing));
144        }
145    } else {
146        // No task context: show project structure overview
147        let scan_age = chrono::NaiveDateTime::parse_from_str(&index.last_scan, "%Y-%m-%d %H:%M:%S")
148            .ok()
149            .map(|t| {
150                let elapsed = chrono::Local::now().naive_local().signed_duration_since(t);
151                if elapsed.num_hours() < 1 {
152                    format!("{}m ago", elapsed.num_minutes())
153                } else if elapsed.num_hours() < 24 {
154                    format!("{}h ago", elapsed.num_hours())
155                } else {
156                    format!("{}d ago", elapsed.num_days())
157                }
158            })
159            .unwrap_or_default();
160        let scan_info = if scan_age.is_empty() {
161            String::new()
162        } else {
163            format!("  scanned {scan_age}")
164        };
165        output.push(format!(
166            "PROJECT OVERVIEW  {} files  {} edges{scan_info}",
167            index.files.len(),
168            index.edges.len()
169        ));
170        output.push(String::new());
171
172        // Group by directory
173        let mut by_dir: std::collections::BTreeMap<String, Vec<String>> =
174            std::collections::BTreeMap::new();
175
176        for file_entry in index.files.values() {
177            let dir = std::path::Path::new(&file_entry.path)
178                .parent()
179                .map_or_else(|| ".".to_string(), |p| p.to_string_lossy().to_string());
180            by_dir
181                .entry(dir)
182                .or_default()
183                .push(short_path(&file_entry.path));
184        }
185
186        for (dir, files) in &by_dir {
187            let dir_display = if dir.len() > 50 {
188                let start = truncate_start_char_boundary(dir, 47);
189                format!("...{}", &dir[start..])
190            } else {
191                dir.clone()
192            };
193
194            if files.len() <= 5 {
195                output.push(format!("{dir_display}/  {}", files.join(" ")));
196            } else {
197                output.push(format!(
198                    "{dir_display}/  {} +{} more",
199                    files[..3].join(" "),
200                    files.len() - 3
201                ));
202            }
203        }
204    }
205
206    if let Some(task_desc) = task {
207        append_knowledge_task_section(&mut output, &index.project_root, task_desc);
208    }
209    append_graph_hotspots_section(&mut output, &index.project_root, &index);
210
211    let cfg = crate::core::config::Config::load();
212    if cfg.enable_wakeup_ctx {
213        let wakeup = build_wakeup_briefing(&project_root, task);
214        if !wakeup.is_empty() {
215            output.push(String::new());
216            output.push(wakeup);
217        }
218    }
219
220    if !auto_loaded.is_empty() {
221        output.push(String::new());
222        output.push(format!(
223            "CONTEXT PACKAGES AUTO-LOADED: {}",
224            auto_loaded.join(", ")
225        ));
226    }
227
228    let original = count_tokens(&format!("{} files", index.files.len())) * index.files.len();
229    let compressed = count_tokens(&output.join("\n"));
230    output.push(String::new());
231    output.push(crate::core::protocol::format_savings(original, compressed));
232
233    output.join("\n")
234}
235
236fn append_knowledge_task_section(output: &mut Vec<String>, project_root: &str, task: &str) {
237    let Some(knowledge) = crate::core::knowledge::ProjectKnowledge::load(project_root) else {
238        return;
239    };
240    let hits: Vec<_> = knowledge.recall(task).into_iter().take(5).collect();
241    if hits.is_empty() {
242        return;
243    }
244    let n = hits.len();
245    output.push(String::new());
246    output.push(format!("[knowledge: {n} relevant facts]"));
247    for f in hits {
248        let text = compact_fact_phrase(f);
249        output.push(format!("  \"{text}\" (confidence: {:.1})", f.confidence));
250    }
251}
252
253fn compact_fact_phrase(f: &crate::core::knowledge::KnowledgeFact) -> String {
254    let v = f.value.trim();
255    let k = f.key.trim();
256    let raw = if !v.is_empty() && (k.is_empty() || v.contains(' ') || v.len() >= k.len()) {
257        v.to_string()
258    } else if !k.is_empty() && !v.is_empty() {
259        format!("{k}: {v}")
260    } else {
261        k.to_string()
262    };
263    let neutral = crate::core::sanitize::neutralize_metadata(&raw);
264    const MAX: usize = 100;
265    if neutral.chars().count() > MAX {
266        let trimmed: String = neutral.chars().take(MAX.saturating_sub(1)).collect();
267        format!("{trimmed}…")
268    } else {
269        neutral
270    }
271}
272
273fn append_graph_hotspots_section(
274    output: &mut Vec<String>,
275    project_root: &str,
276    index: &crate::core::graph_index::ProjectIndex,
277) {
278    let rows = graph_hotspot_rows(project_root, index);
279    if rows.is_empty() {
280        return;
281    }
282    let n = rows.len();
283    output.push(String::new());
284    output.push(format!("[graph: {n} architectural hotspots]"));
285    for (path, imp, cal) in rows {
286        let p = short_path(&path);
287        if cal > 0 {
288            output.push(format!("  {p} ({imp} imports, {cal} calls)"));
289        } else {
290            output.push(format!("  {p} ({imp} imports)"));
291        }
292    }
293}
294
295/// Import/call edge touches per file from SQLite graph when available; otherwise
296/// import-edge degree from the JSON graph index (calls omitted).
297fn graph_hotspot_rows(
298    project_root: &str,
299    index: &crate::core::graph_index::ProjectIndex,
300) -> Vec<(String, usize, usize)> {
301    if let Ok(graph) = crate::core::property_graph::CodeGraph::open(project_root) {
302        let sql = "
303            WITH edge_files AS (
304              SELECT e.kind AS kind, ns.file_path AS fp
305              FROM edges e
306              JOIN nodes ns ON e.source_id = ns.id
307              WHERE e.kind IN ('imports', 'calls')
308              UNION ALL
309              SELECT e.kind, nt.file_path
310              FROM edges e
311              JOIN nodes nt ON e.target_id = nt.id
312              WHERE e.kind IN ('imports', 'calls')
313            )
314            SELECT fp,
315                   SUM(CASE WHEN kind = 'imports' THEN 1 ELSE 0 END) AS imp,
316                   SUM(CASE WHEN kind = 'calls' THEN 1 ELSE 0 END) AS cal
317            FROM edge_files
318            GROUP BY fp
319            ORDER BY (imp + cal) DESC
320            LIMIT 5
321        ";
322        let conn = graph.connection();
323        if let Ok(mut stmt) = conn.prepare(sql) {
324            let mapped = stmt.query_map([], |row| {
325                Ok((
326                    row.get::<_, String>(0)?,
327                    row.get::<_, i64>(1)? as usize,
328                    row.get::<_, i64>(2)? as usize,
329                ))
330            });
331            if let Ok(iter) = mapped {
332                let collected: Vec<_> = iter.filter_map(std::result::Result::ok).collect();
333                if !collected.is_empty() {
334                    return collected;
335                }
336            }
337        }
338    }
339    index_import_hotspots(index, 5)
340}
341
342fn index_import_hotspots(
343    index: &crate::core::graph_index::ProjectIndex,
344    limit: usize,
345) -> Vec<(String, usize, usize)> {
346    use std::collections::HashMap;
347
348    let mut imp: HashMap<String, usize> = HashMap::new();
349    for e in &index.edges {
350        if e.kind != "import" {
351            continue;
352        }
353        *imp.entry(e.from.clone()).or_insert(0) += 1;
354        *imp.entry(e.to.clone()).or_insert(0) += 1;
355    }
356    let mut v: Vec<(String, usize, usize)> =
357        imp.into_iter().map(|(p, c)| (p, c, 0_usize)).collect();
358    v.sort_by_key(|x| std::cmp::Reverse(x.1 + x.2));
359    v.truncate(limit);
360    v
361}
362
363fn build_wakeup_briefing(project_root: &str, task: Option<&str>) -> String {
364    let mut parts = Vec::new();
365
366    if let Some(knowledge) = crate::core::knowledge::ProjectKnowledge::load(project_root) {
367        let facts_line = knowledge.format_wakeup();
368        if !facts_line.is_empty() {
369            parts.push(facts_line);
370        }
371    }
372
373    if let Some(session) = crate::core::session::SessionState::load_latest() {
374        if let Some(ref task) = session.task {
375            parts.push(format!("LAST_TASK:{}", task.description));
376        }
377        if !session.decisions.is_empty() {
378            let recent: Vec<String> = session
379                .decisions
380                .iter()
381                .rev()
382                .take(3)
383                .map(|d| d.summary.clone())
384                .collect();
385            parts.push(format!("RECENT_DECISIONS:{}", recent.join("|")));
386        }
387    }
388
389    if let Some(t) = task {
390        for r in crate::core::prospective_memory::reminders_for_task(project_root, t) {
391            parts.push(r);
392        }
393    }
394
395    let registry = crate::core::agents::AgentRegistry::load_or_create();
396    let active_agents: Vec<&crate::core::agents::AgentEntry> = registry
397        .agents
398        .iter()
399        .filter(|a| a.status != crate::core::agents::AgentStatus::Finished)
400        .collect();
401    if !active_agents.is_empty() {
402        let agents: Vec<String> = active_agents
403            .iter()
404            .map(|a| format!("{}({})", a.agent_id, a.role.as_deref().unwrap_or("-")))
405            .collect();
406        parts.push(format!("AGENTS:{}", agents.join(",")));
407    }
408
409    if parts.is_empty() {
410        return String::new();
411    }
412
413    format!("WAKE-UP BRIEFING:\n{}", parts.join("\n"))
414}
415
416fn short_path(path: &str) -> String {
417    let parts: Vec<&str> = path.split('/').collect();
418    if parts.len() <= 2 {
419        return path.to_string();
420    }
421    parts[parts.len() - 2..].join("/")
422}
423
424/// Find a byte offset at most `max_tail_bytes` from the end of `s`
425/// that falls on a valid UTF-8 char boundary.
426fn truncate_start_char_boundary(s: &str, max_tail_bytes: usize) -> usize {
427    if max_tail_bytes >= s.len() {
428        return 0;
429    }
430    let mut start = s.len() - max_tail_bytes;
431    while start < s.len() && !s.is_char_boundary(start) {
432        start += 1;
433    }
434    start
435}
436
437fn file_line_count(path: &str) -> usize {
438    std::fs::read_to_string(path).map_or(0, |c| c.lines().count())
439}
440
441#[cfg(test)]
442mod tests {
443    use super::*;
444
445    #[test]
446    fn truncate_start_ascii() {
447        let s = "abcdefghij"; // 10 bytes
448        assert_eq!(truncate_start_char_boundary(s, 5), 5);
449        assert_eq!(&s[5..], "fghij");
450    }
451
452    #[test]
453    fn truncate_start_multibyte_chinese() {
454        // "文档/examples/extensions/custom-provider-anthropic" = multi-byte prefix
455        let s = "文档/examples/extensions/custom-provider-anthropic";
456        let start = truncate_start_char_boundary(s, 47);
457        assert!(s.is_char_boundary(start));
458        let tail = &s[start..];
459        assert!(tail.len() <= 47);
460    }
461
462    #[test]
463    fn truncate_start_all_multibyte() {
464        let s = "这是一个很长的中文目录路径用于测试字符边界处理";
465        let start = truncate_start_char_boundary(s, 20);
466        assert!(s.is_char_boundary(start));
467    }
468
469    #[test]
470    fn truncate_start_larger_than_string() {
471        let s = "short";
472        assert_eq!(truncate_start_char_boundary(s, 100), 0);
473    }
474
475    #[test]
476    fn truncate_start_emoji() {
477        let s = "/home/user/🎉🎉🎉/src/components/deeply/nested";
478        let start = truncate_start_char_boundary(s, 30);
479        assert!(s.is_char_boundary(start));
480    }
481}