lean-ctx 3.6.5

Context Runtime for AI Agents with CCP. 51 MCP tools, 10 read modes, 60+ compression patterns, cross-session memory (CCP), persistent AI knowledge with temporal facts + contradiction detection, multi-agent context sharing, LITM-aware positioning, AAAK compact format, adaptive compression with Thompson Sampling bandits. Supports 24+ AI tools. Reduces LLM token consumption by up to 99%.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
//! Code smell detection engine.
//!
//! Runs structural rules against the Property Graph (SQLite) and tree-sitter
//! data to identify dead code, high complexity, god files, fan-out skew, etc.
//! Each rule is a pure function: `&Connection -> Vec<SmellFinding>`.

use rusqlite::Connection;
use serde::Serialize;

#[derive(Debug, Clone, Serialize)]
pub struct SmellFinding {
    pub rule: &'static str,
    pub severity: Severity,
    pub file_path: String,
    pub symbol: Option<String>,
    pub line: Option<usize>,
    pub message: String,
    pub metric: Option<f64>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum Severity {
    Info,
    Warning,
    Error,
}

#[derive(Debug, Clone, Serialize)]
pub struct SmellSummary {
    pub rule: &'static str,
    pub description: &'static str,
    pub findings: usize,
}

pub struct SmellConfig {
    pub long_function_lines: usize,
    pub long_file_lines: usize,
    pub god_file_symbols: usize,
    pub fan_out_threshold: usize,
}

impl Default for SmellConfig {
    fn default() -> Self {
        Self {
            long_function_lines: 100,
            long_file_lines: 500,
            god_file_symbols: 30,
            fan_out_threshold: 15,
        }
    }
}

pub static RULES: &[(&str, &str)] = &[
    ("dead_code", "Symbols defined but never referenced"),
    ("long_function", "Functions exceeding line threshold"),
    ("long_file", "Files exceeding line threshold"),
    ("god_file", "Files with excessive symbol count"),
    ("fan_out_skew", "Functions calling too many other symbols"),
    (
        "duplicate_definitions",
        "Same symbol name defined in multiple files",
    ),
    (
        "untested_function",
        "Exported symbols without test coverage",
    ),
    (
        "cyclomatic_complexity",
        "Functions with high branching complexity",
    ),
];

pub fn scan_all(conn: &Connection, cfg: &SmellConfig) -> Vec<SmellFinding> {
    let mut all = Vec::new();
    for &(rule, _) in RULES {
        all.extend(scan_rule(conn, rule, cfg));
    }
    all
}

pub fn scan_rule(conn: &Connection, rule: &str, cfg: &SmellConfig) -> Vec<SmellFinding> {
    match rule {
        "dead_code" => detect_dead_code(conn),
        "long_function" => detect_long_functions(conn, cfg.long_function_lines),
        "long_file" => detect_long_files(conn, cfg.long_file_lines),
        "god_file" => detect_god_files(conn, cfg.god_file_symbols),
        "fan_out_skew" => detect_fan_out(conn, cfg.fan_out_threshold),
        "duplicate_definitions" => detect_duplicate_definitions(conn),
        "untested_function" => detect_untested(conn),
        "cyclomatic_complexity" => detect_cyclomatic_complexity(conn),
        _ => Vec::new(),
    }
}

pub fn summarize(findings: &[SmellFinding]) -> Vec<SmellSummary> {
    RULES
        .iter()
        .map(|&(rule, desc)| SmellSummary {
            rule,
            description: desc,
            findings: findings.iter().filter(|f| f.rule == rule).count(),
        })
        .collect()
}

fn detect_dead_code(conn: &Connection) -> Vec<SmellFinding> {
    let sql = "
        SELECT n.name, n.file_path, n.line_start
        FROM nodes n
        WHERE n.kind = 'symbol'
          AND n.file_path NOT LIKE '%test%'
          AND n.file_path NOT LIKE '%spec%'
          AND n.name NOT IN ('main', 'new', 'default', 'fmt', 'drop')
          AND n.id NOT IN (
              SELECT DISTINCT e.target_id FROM edges e
              WHERE e.kind IN ('calls', 'type_ref', 'imports')
          )
        ORDER BY n.file_path, n.line_start
        LIMIT 200
    ";
    query_findings(
        conn,
        sql,
        "dead_code",
        Severity::Warning,
        |name, path, _line| format!("'{name}' defined in {path} but never referenced"),
    )
}

fn detect_long_functions(conn: &Connection, threshold: usize) -> Vec<SmellFinding> {
    let sql = format!(
        "SELECT n.name, n.file_path, n.line_start,
                (n.line_end - n.line_start) AS span
         FROM nodes n
         WHERE n.kind = 'symbol'
           AND n.line_start IS NOT NULL
           AND n.line_end IS NOT NULL
           AND (n.line_end - n.line_start) > {threshold}
         ORDER BY span DESC
         LIMIT 100"
    );
    query_findings_with_metric(
        conn,
        &sql,
        "long_function",
        Severity::Warning,
        |name, _path, _line, metric| {
            format!("'{name}' is {metric:.0} lines (threshold: {threshold})")
        },
    )
}

fn detect_long_files(conn: &Connection, threshold: usize) -> Vec<SmellFinding> {
    let sql = format!(
        "SELECT n.name, n.file_path, NULL,
                CAST(n.metadata AS INTEGER) AS line_count
         FROM nodes n
         WHERE n.kind = 'file'
           AND n.metadata IS NOT NULL
           AND CAST(n.metadata AS INTEGER) > {threshold}
         ORDER BY line_count DESC
         LIMIT 100"
    );
    query_findings_with_metric(
        conn,
        &sql,
        "long_file",
        Severity::Info,
        |_name, path, _line, metric| {
            format!("{path} has {metric:.0} lines (threshold: {threshold})")
        },
    )
}

fn detect_god_files(conn: &Connection, threshold: usize) -> Vec<SmellFinding> {
    let sql = format!(
        "SELECT COUNT(*) AS sym_count, n.file_path
         FROM nodes n
         WHERE n.kind = 'symbol'
         GROUP BY n.file_path
         HAVING sym_count > {threshold}
         ORDER BY sym_count DESC
         LIMIT 50"
    );
    let mut findings = Vec::new();
    let Ok(mut stmt) = conn.prepare(&sql) else {
        return findings;
    };
    let Ok(rows) = stmt.query_map([], |row| {
        Ok((row.get::<_, i64>(0)?, row.get::<_, String>(1)?))
    }) else {
        return findings;
    };
    for row in rows.flatten() {
        let (count, path) = row;
        findings.push(SmellFinding {
            rule: "god_file",
            severity: Severity::Warning,
            file_path: path.clone(),
            symbol: None,
            line: None,
            message: format!("{path} has {count} symbols (threshold: {threshold})"),
            metric: Some(count as f64),
        });
    }
    findings
}

fn detect_fan_out(conn: &Connection, threshold: usize) -> Vec<SmellFinding> {
    let sql = format!(
        "SELECT n.name, n.file_path, n.line_start, COUNT(e.id) AS call_count
         FROM nodes n
         JOIN edges e ON e.source_id = n.id AND e.kind = 'calls'
         WHERE n.kind = 'symbol'
         GROUP BY n.id
         HAVING call_count > {threshold}
         ORDER BY call_count DESC
         LIMIT 100"
    );
    query_findings_with_metric(
        conn,
        &sql,
        "fan_out_skew",
        Severity::Warning,
        |name, _path, _line, metric| {
            format!("'{name}' calls {metric:.0} symbols (threshold: {threshold})")
        },
    )
}

fn detect_duplicate_definitions(conn: &Connection) -> Vec<SmellFinding> {
    let sql = "
        SELECT n.name, GROUP_CONCAT(n.file_path, ', ') AS files, COUNT(*) AS cnt
        FROM nodes n
        WHERE n.kind = 'symbol'
          AND n.name NOT IN ('new', 'default', 'fmt', 'from', 'into', 'drop', 'clone', 'eq')
        GROUP BY n.name
        HAVING cnt > 1
        ORDER BY cnt DESC
        LIMIT 50
    ";
    let mut findings = Vec::new();
    let Ok(mut stmt) = conn.prepare(sql) else {
        return findings;
    };
    let Ok(rows) = stmt.query_map([], |row| {
        Ok((
            row.get::<_, String>(0)?,
            row.get::<_, String>(1)?,
            row.get::<_, i64>(2)?,
        ))
    }) else {
        return findings;
    };
    for row in rows.flatten() {
        let (name, files, count) = row;
        findings.push(SmellFinding {
            rule: "duplicate_definitions",
            severity: Severity::Info,
            file_path: files.clone(),
            symbol: Some(name.clone()),
            line: None,
            message: format!("'{name}' defined in {count} files: {files}"),
            metric: Some(count as f64),
        });
    }
    findings
}

fn detect_untested(conn: &Connection) -> Vec<SmellFinding> {
    let sql = "
        SELECT n.name, n.file_path, n.line_start
        FROM nodes n
        WHERE n.kind = 'symbol'
          AND n.file_path NOT LIKE '%test%'
          AND n.file_path NOT LIKE '%spec%'
          AND n.metadata LIKE '%export%'
          AND n.id NOT IN (
              SELECT DISTINCT e.source_id FROM edges e WHERE e.kind = 'tested_by'
          )
          AND n.id NOT IN (
              SELECT DISTINCT e.target_id FROM edges e WHERE e.kind = 'tested_by'
          )
        ORDER BY n.file_path, n.line_start
        LIMIT 100
    ";
    query_findings(
        conn,
        sql,
        "untested_function",
        Severity::Info,
        |name, path, _line| format!("'{name}' in {path} has no test coverage"),
    )
}

fn detect_cyclomatic_complexity(conn: &Connection) -> Vec<SmellFinding> {
    #[cfg(feature = "tree-sitter")]
    {
        detect_cyclomatic_tree_sitter(conn)
    }
    #[cfg(not(feature = "tree-sitter"))]
    {
        detect_cyclomatic_heuristic(conn)
    }
}

/// Span × calls proxy when the `tree-sitter` feature is off (no AST available).
#[cfg(not(feature = "tree-sitter"))]
fn detect_cyclomatic_heuristic(conn: &Connection) -> Vec<SmellFinding> {
    let sql = "
        SELECT n.name, n.file_path, n.line_start,
               (n.line_end - n.line_start) AS span,
               (SELECT COUNT(*) FROM edges e WHERE e.source_id = n.id AND e.kind = 'calls') AS calls
        FROM nodes n
        WHERE n.kind = 'symbol'
          AND n.line_start IS NOT NULL
          AND n.line_end IS NOT NULL
          AND (n.line_end - n.line_start) > 20
        ORDER BY (span * 0.3 + calls * 0.7) DESC
        LIMIT 100
    ";
    let mut findings = Vec::new();
    let Ok(mut stmt) = conn.prepare(sql) else {
        return findings;
    };
    let Ok(rows) = stmt.query_map([], |row| {
        Ok((
            row.get::<_, String>(0)?,
            row.get::<_, String>(1)?,
            row.get::<_, Option<i64>>(2)?,
            row.get::<_, i64>(3)?,
            row.get::<_, i64>(4)?,
        ))
    }) else {
        return findings;
    };
    for row in rows.flatten() {
        let (name, path, line, span, calls) = row;
        let complexity_proxy = (span as f64) * 0.3 + (calls as f64) * 0.7;
        if complexity_proxy < 10.0 {
            continue;
        }
        let severity = if complexity_proxy > 30.0 {
            Severity::Error
        } else if complexity_proxy > 20.0 {
            Severity::Warning
        } else {
            Severity::Info
        };
        findings.push(SmellFinding {
            rule: "cyclomatic_complexity",
            severity,
            file_path: path,
            symbol: Some(name.clone()),
            line: line.map(|l| l as usize),
            message: format!(
                "'{name}' complexity proxy {complexity_proxy:.1} (span={span}, calls={calls})"
            ),
            metric: Some(complexity_proxy),
        });
    }
    findings
}

#[cfg(feature = "tree-sitter")]
fn detect_cyclomatic_tree_sitter(conn: &Connection) -> Vec<SmellFinding> {
    use std::collections::HashMap;
    use std::path::Path;

    const WARN_CC: u32 = 11;
    const ERR_CC: u32 = 21;

    let sql = "
        SELECT DISTINCT n.file_path
        FROM nodes n
        WHERE n.kind = 'symbol'
          AND n.file_path IS NOT NULL
          AND length(trim(n.file_path)) > 0
        LIMIT 400
    ";
    let mut paths = Vec::new();
    let Ok(mut stmt) = conn.prepare(sql) else {
        return Vec::new();
    };
    let Ok(rows) = stmt.query_map([], |row| row.get::<_, String>(0)) else {
        return Vec::new();
    };
    for row in rows.flatten() {
        paths.push(row);
    }

    let mut per_file: HashMap<String, Vec<crate::core::cyclomatic::FunctionComplexity>> =
        HashMap::new();

    for path in paths {
        if per_file.contains_key(&path) {
            continue;
        }
        let Ok(content) = std::fs::read_to_string(&path) else {
            continue;
        };
        let Some(ext) = Path::new(&path).extension().and_then(|e| e.to_str()) else {
            continue;
        };
        let Some(metrics) = crate::core::cyclomatic::cyclomatic_per_function(&content, ext) else {
            continue;
        };
        per_file.insert(path, metrics);
    }

    let mut findings = Vec::new();
    for (path, metrics) in per_file {
        for m in metrics {
            if m.cyclomatic < WARN_CC {
                continue;
            }
            let severity = if m.cyclomatic >= ERR_CC {
                Severity::Error
            } else {
                Severity::Warning
            };
            findings.push(SmellFinding {
                rule: "cyclomatic_complexity",
                severity,
                file_path: path.clone(),
                symbol: Some(m.name.clone()),
                line: Some(m.line),
                message: format!(
                    "'{}' cyclomatic complexity {} (thresholds: warning {WARN_CC}, error {ERR_CC})",
                    m.name, m.cyclomatic
                ),
                metric: Some(f64::from(m.cyclomatic)),
            });
        }
    }

    findings.sort_by(|a, b| {
        b.metric
            .unwrap_or(0.0)
            .partial_cmp(&a.metric.unwrap_or(0.0))
            .unwrap_or(std::cmp::Ordering::Equal)
    });
    findings.truncate(100);
    findings
}

fn query_findings(
    conn: &Connection,
    sql: &str,
    rule: &'static str,
    severity: Severity,
    msg_fn: impl Fn(&str, &str, Option<usize>) -> String,
) -> Vec<SmellFinding> {
    let mut findings = Vec::new();
    let Ok(mut stmt) = conn.prepare(sql) else {
        return findings;
    };
    let Ok(rows) = stmt.query_map([], |row| {
        Ok((
            row.get::<_, String>(0)?,
            row.get::<_, String>(1)?,
            row.get::<_, Option<i64>>(2)?,
        ))
    }) else {
        return findings;
    };
    for row in rows.flatten() {
        let (name, path, line) = row;
        let line_usize = line.map(|l| l as usize);
        findings.push(SmellFinding {
            rule,
            severity,
            file_path: path.clone(),
            symbol: Some(name.clone()),
            line: line_usize,
            message: msg_fn(&name, &path, line_usize),
            metric: None,
        });
    }
    findings
}

fn query_findings_with_metric(
    conn: &Connection,
    sql: &str,
    rule: &'static str,
    severity: Severity,
    msg_fn: impl Fn(&str, &str, Option<usize>, f64) -> String,
) -> Vec<SmellFinding> {
    let mut findings = Vec::new();
    let Ok(mut stmt) = conn.prepare(sql) else {
        return findings;
    };
    let Ok(rows) = stmt.query_map([], |row| {
        Ok((
            row.get::<_, String>(0)?,
            row.get::<_, String>(1)?,
            row.get::<_, Option<i64>>(2)?,
            row.get::<_, f64>(3)?,
        ))
    }) else {
        return findings;
    };
    for row in rows.flatten() {
        let (name, path, line, metric) = row;
        let line_usize = line.map(|l| l as usize);
        findings.push(SmellFinding {
            rule,
            severity,
            file_path: path.clone(),
            symbol: Some(name.clone()),
            line: line_usize,
            message: msg_fn(&name, &path, line_usize, metric),
            metric: Some(metric),
        });
    }
    findings
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::core::property_graph::{CodeGraph, Edge, EdgeKind, Node, NodeKind};

    fn setup_graph() -> CodeGraph {
        let g = CodeGraph::open_in_memory().unwrap();

        let file_a = g.upsert_node(&Node::file("src/main.rs")).unwrap();
        let file_b = g.upsert_node(&Node::file("src/lib.rs")).unwrap();
        let file_c = g
            .upsert_node(&Node::file("src/utils.rs").with_metadata("600"))
            .unwrap();

        let sym_used = g
            .upsert_node(
                &Node::symbol("process", "src/lib.rs", NodeKind::Symbol).with_lines(10, 50),
            )
            .unwrap();
        let sym_dead = g
            .upsert_node(
                &Node::symbol("unused_helper", "src/lib.rs", NodeKind::Symbol).with_lines(60, 80),
            )
            .unwrap();
        let sym_long = g
            .upsert_node(
                &Node::symbol("mega_function", "src/utils.rs", NodeKind::Symbol).with_lines(1, 200),
            )
            .unwrap();

        g.upsert_edge(&Edge::new(file_a, file_b, EdgeKind::Imports))
            .unwrap();
        g.upsert_edge(&Edge::new(file_a, sym_used, EdgeKind::Calls))
            .unwrap();

        // sym_dead has no incoming edges -> dead code
        let _ = sym_dead;
        let _ = sym_long;
        let _ = file_c;

        g
    }

    #[test]
    fn dead_code_detection() {
        let g = setup_graph();
        let findings = detect_dead_code(g.connection());
        let dead: Vec<_> = findings
            .iter()
            .filter(|f| f.symbol.as_deref() == Some("unused_helper"))
            .collect();
        assert!(!dead.is_empty(), "Should detect unused_helper as dead code");
    }

    #[test]
    fn long_function_detection() {
        let g = setup_graph();
        let findings = detect_long_functions(g.connection(), 100);
        let long: Vec<_> = findings
            .iter()
            .filter(|f| f.symbol.as_deref() == Some("mega_function"))
            .collect();
        assert!(!long.is_empty(), "Should detect mega_function as too long");
    }

    #[test]
    fn long_file_detection() {
        let g = setup_graph();
        let findings = detect_long_files(g.connection(), 500);
        let long: Vec<_> = findings
            .iter()
            .filter(|f| f.file_path == "src/utils.rs")
            .collect();
        assert!(
            !long.is_empty(),
            "Should detect src/utils.rs as long file (600 lines)"
        );
    }

    #[test]
    fn scan_all_returns_findings() {
        let g = setup_graph();
        let cfg = SmellConfig::default();
        let all = scan_all(g.connection(), &cfg);
        assert!(!all.is_empty(), "Should find at least one smell");
    }

    #[test]
    fn summarize_groups_by_rule() {
        let g = setup_graph();
        let cfg = SmellConfig::default();
        let all = scan_all(g.connection(), &cfg);
        let summary = summarize(&all);
        assert_eq!(summary.len(), RULES.len());
        for s in &summary {
            assert!(!s.description.is_empty());
        }
    }
}