lean-ctx 3.9.8

Context Runtime for AI Agents with CCP. 71 MCP tools, 10 read modes, 95+ 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
//! Call-site extraction from AST nodes.

#[cfg(feature = "tree-sitter")]
use tree_sitter::Node;

#[cfg(feature = "tree-sitter")]
use super::types::CallSite;
#[cfg(feature = "tree-sitter")]
use super::{find_child_by_kind, find_descendant_by_kind, node_text};

#[cfg(feature = "tree-sitter")]
pub(super) fn extract_calls(root: Node, src: &str, ext: &str) -> Vec<CallSite> {
    let mut calls = Vec::new();
    crate::core::ast_walk::for_each_descendant(root, |node| {
        if is_call_node(node.kind())
            && let Some(call) = parse_call(node, src, ext)
        {
            calls.push(call);
        }
        // Rust: calls written inside macro bodies (`println!("{}", greet(x))`,
        // `assert_eq!(foo(), 1)`) are token trees to tree-sitter, not
        // `call_expression`s — a file whose calls all sit inside macros used to
        // produce ZERO call edges (#658). Recover them at the token level.
        if ext == "rs"
            && let Some(call) = parse_macro_interior_call(node, src)
        {
            calls.push(call);
        }
    });
    calls
}

/// Detect a call-like pattern inside a Rust macro `token_tree`: an
/// `identifier` token immediately followed by a parenthesized `token_tree`
/// (`name(...)`). Path segments (`foo::bar(...)`) resolve to the last segment
/// because the earlier identifiers are followed by `::`, not by `(…)`. A `.`
/// before the identifier marks a method call. Nested macro names never match:
/// their identifier is followed by `!`.
#[cfg(feature = "tree-sitter")]
fn parse_macro_interior_call(node: Node, src: &str) -> Option<CallSite> {
    if node.kind() != "identifier" || node.parent()?.kind() != "token_tree" {
        return None;
    }
    let next = node.next_sibling()?;
    if next.kind() != "token_tree" || !node_text(next, src).starts_with('(') {
        return None;
    }
    let is_method = node
        .prev_sibling()
        .is_some_and(|p| node_text(p, src) == ".");
    Some(CallSite {
        callee: node_text(node, src).to_string(),
        line: node.start_position().row + 1,
        col: node.start_position().column,
        receiver: None,
        is_method,
    })
}

/// Call-like AST node kinds across the supported tree-sitter grammars.
///
/// Different grammars name the invocation node differently: most use
/// `call_expression`, Java methods use `method_invocation` and constructors
/// use `object_creation_expression`, while Python (and Elixir) use a bare
/// `call`. Missing `call` here is what previously made Python call sites —
/// including class instantiation `Foo(...)` — invisible to the call graph.
#[cfg(feature = "tree-sitter")]
fn is_call_node(kind: &str) -> bool {
    matches!(
        kind,
        "call_expression"
            | "method_invocation"
            | "call"
            | "object_creation_expression"
            // C# method calls are `invocation_expression`; `new T(...)` reuses
            // `object_creation_expression` (shared with Java) above.
            | "invocation_expression"
            // GDScript method calls and `X.new()` instantiation are `attribute_call`.
            | "attribute_call"
            // Lua / Luau calls (direct, `t.f(...)` and `t:m(...)`) are `function_call`.
            | "function_call"
    )
}

#[cfg(feature = "tree-sitter")]
fn parse_call(node: Node, src: &str, ext: &str) -> Option<CallSite> {
    match ext {
        "ts" | "tsx" | "js" | "jsx" => parse_call_ts(node, src),
        "rs" => parse_call_rust(node, src),
        "py" => parse_call_python(node, src),
        "go" => parse_call_go(node, src),
        "java" => parse_call_java(node, src),
        "kt" | "kts" => parse_call_kotlin(node, src),
        "gd" => parse_call_gd(node, src),
        "cs" => parse_call_csharp(node, src),
        "lua" | "luau" => parse_call_lua(node, src),
        _ => None,
    }
}

/// Lua / Luau call sites. The `function_call`'s `name` field is the callee:
/// - `f(...)`        -> callee `f`, no receiver
/// - `t.f(...)`      -> callee `f`, receiver `t` (dot access)
/// - `t:m(...)`      -> callee `m`, receiver `t`, method call
#[cfg(feature = "tree-sitter")]
fn parse_call_lua(node: Node, src: &str) -> Option<CallSite> {
    let line = node.start_position().row + 1;
    let col = node.start_position().column;
    let name = node.child_by_field_name("name")?;
    match name.kind() {
        "identifier" => Some(CallSite {
            callee: node_text(name, src).to_string(),
            line,
            col,
            receiver: None,
            is_method: false,
        }),
        "dot_index_expression" => {
            let field = name.child_by_field_name("field")?;
            let receiver = name
                .child_by_field_name("table")
                .map(|t| node_text(t, src).to_string());
            Some(CallSite {
                callee: node_text(field, src).to_string(),
                line,
                col,
                receiver,
                is_method: false,
            })
        }
        "method_index_expression" => {
            let method = name.child_by_field_name("method")?;
            let receiver = name
                .child_by_field_name("table")
                .map(|t| node_text(t, src).to_string());
            Some(CallSite {
                callee: node_text(method, src).to_string(),
                line,
                col,
                receiver,
                is_method: true,
            })
        }
        _ => None,
    }
}

/// C# call sites: `invocation_expression` for method/function calls and
/// `object_creation_expression` (`new T(...)`) for constructor calls.
///
/// - `foo(...)`            -> callee `foo`,  no receiver
/// - `obj.Method(...)`     -> callee `Method`, receiver `obj`, method call
/// - `A.B.Method(...)`     -> callee `Method`, receiver `A.B`,  method call
/// - `Method<T>(...)`      -> callee `Method` (generic name reduced to its identifier)
/// - `new Engine(...)`     -> callee `Engine` (the constructed type), no receiver
#[cfg(feature = "tree-sitter")]
fn parse_call_csharp(node: Node, src: &str) -> Option<CallSite> {
    let line = node.start_position().row + 1;
    let col = node.start_position().column;

    if node.kind() == "object_creation_expression" {
        // `new Foo(...)` / `new Foo<T>(...)` — the callee is the constructed type.
        let type_node = node.child_by_field_name("type")?;
        let callee = csharp_simple_name(type_node, src)?;
        return Some(CallSite {
            callee,
            line,
            col,
            receiver: None,
            is_method: false,
        });
    }

    // invocation_expression: the `function` field holds what is being called.
    let func = node.child_by_field_name("function")?;
    if func.kind() == "member_access_expression" {
        let name_node = func.child_by_field_name("name")?;
        let callee = csharp_simple_name(name_node, src)?;
        let receiver = func
            .child_by_field_name("expression")
            .map(|r| node_text(r, src).to_string());
        return Some(CallSite {
            callee,
            line,
            col,
            receiver,
            is_method: true,
        });
    }

    // Bare invocation: identifier / generic_name / qualified_name.
    let callee = csharp_simple_name(func, src)?;
    Some(CallSite {
        callee,
        line,
        col,
        receiver: None,
        is_method: false,
    })
}

/// Reduce a C# name node to its simple identifier:
/// `Foo` -> `Foo`, `Foo<T>` (generic_name) -> `Foo`, `A.B.Foo` (qualified_name) -> `Foo`.
/// Returns `None` for nameless constructs (e.g. `new int[]`) so they do not
/// pollute the call graph with junk callees.
#[cfg(feature = "tree-sitter")]
fn csharp_simple_name(node: Node, src: &str) -> Option<String> {
    match node.kind() {
        "identifier" => Some(node_text(node, src).to_string()),
        "generic_name" => {
            find_child_by_kind(node, "identifier").map(|id| node_text(id, src).to_string())
        }
        "qualified_name" | "alias_qualified_name" | "member_access_expression" => {
            let text = node_text(node, src);
            let last = text
                .rsplit(['.', ':'])
                .find(|seg| !seg.trim().is_empty())
                .unwrap_or(text)
                .trim();
            // Drop any generic argument suffix: `Foo<T>` -> `Foo`.
            let bare = last.split('<').next().unwrap_or(last).trim();
            if bare.is_empty() {
                None
            } else {
                Some(bare.to_string())
            }
        }
        // `object_creation_expression`'s `type` field wraps a concrete type node
        // (identifier / generic_name / qualified_name / predefined / array …).
        _ => find_child_by_kind(node, "identifier")
            .or_else(|| find_child_by_kind(node, "generic_name"))
            .or_else(|| find_child_by_kind(node, "qualified_name"))
            .and_then(|inner| csharp_simple_name(inner, src)),
    }
}

#[cfg(feature = "tree-sitter")]
fn parse_call_gd(node: Node, src: &str) -> Option<CallSite> {
    let line = node.start_position().row + 1;
    let col = node.start_position().column;
    match node.kind() {
        // Direct call: `func(...)` / `preload(...)`.
        "call" => {
            let func = node.child(0)?;
            if func.kind() == "identifier" {
                Some(CallSite {
                    callee: node_text(func, src).to_string(),
                    line,
                    col,
                    receiver: None,
                    is_method: false,
                })
            } else {
                find_descendant_by_kind(func, "identifier").map(|id| CallSite {
                    callee: node_text(id, src).to_string(),
                    line,
                    col,
                    receiver: None,
                    is_method: false,
                })
            }
        }
        // Method call / instantiation: `receiver.method(...)` under an `attribute`.
        "attribute_call" => {
            let method_node = find_child_by_kind(node, "identifier")?;
            let method = node_text(method_node, src).to_string();
            let receiver = node
                .parent()
                .filter(|p| p.kind() == "attribute")
                .and_then(|p| p.child(0))
                .map(|r| node_text(r, src).to_string());
            // `X.new()` instantiates class `X`; attribute the reference to the
            // class so it registers in the call graph / dead-code analysis (#365).
            if method == "new"
                && let Some(class) = receiver
            {
                return Some(CallSite {
                    callee: class,
                    line,
                    col,
                    receiver: None,
                    is_method: false,
                });
            }
            Some(CallSite {
                callee: method,
                line,
                col,
                receiver,
                is_method: true,
            })
        }
        _ => None,
    }
}

#[cfg(feature = "tree-sitter")]
fn parse_call_ts(node: Node, src: &str) -> Option<CallSite> {
    let func = find_child_by_kind(node, "member_expression")
        .or_else(|| find_child_by_kind(node, "identifier"))
        .or_else(|| find_child_by_kind(node, "subscript_expression"))?;

    if func.kind() == "member_expression" {
        let obj =
            find_child_by_kind(func, "identifier").or_else(|| find_child_by_kind(func, "this"))?;
        let prop = find_child_by_kind(func, "property_identifier")?;
        Some(CallSite {
            callee: node_text(prop, src).to_string(),
            line: node.start_position().row + 1,
            col: node.start_position().column,
            receiver: Some(node_text(obj, src).to_string()),
            is_method: true,
        })
    } else {
        Some(CallSite {
            callee: node_text(func, src).to_string(),
            line: node.start_position().row + 1,
            col: node.start_position().column,
            receiver: None,
            is_method: false,
        })
    }
}

#[cfg(feature = "tree-sitter")]
fn parse_call_rust(node: Node, src: &str) -> Option<CallSite> {
    let func = node.child(0)?;
    match func.kind() {
        "field_expression" => {
            let field = find_child_by_kind(func, "field_identifier")?;
            let receiver = func.child(0).map(|r| node_text(r, src).to_string());
            Some(CallSite {
                callee: node_text(field, src).to_string(),
                line: node.start_position().row + 1,
                col: node.start_position().column,
                receiver,
                is_method: true,
            })
        }
        "scoped_identifier" | "identifier" => Some(CallSite {
            callee: node_text(func, src).to_string(),
            line: node.start_position().row + 1,
            col: node.start_position().column,
            receiver: None,
            is_method: false,
        }),
        _ => None,
    }
}

#[cfg(feature = "tree-sitter")]
fn parse_call_python(node: Node, src: &str) -> Option<CallSite> {
    let func = node.child(0)?;
    match func.kind() {
        "attribute" => {
            // `obj.method(...)` — the callee is the trailing `attribute` field,
            // not the leading `object`. Fall back to text parsing only if the
            // grammar fields are unavailable.
            let attr = func
                .child_by_field_name("attribute")
                .or_else(|| func.child_by_field_name("attr"));
            let obj = func
                .child_by_field_name("object")
                .or_else(|| func.child(0))
                .map(|r| node_text(r, src).to_string());
            let name = attr
                .map(|a| node_text(a, src).to_string())
                .or_else(|| {
                    let text = node_text(func, src);
                    text.rsplit('.')
                        .next()
                        .map(std::string::ToString::to_string)
                })
                .unwrap_or_default();
            Some(CallSite {
                callee: name,
                line: node.start_position().row + 1,
                col: node.start_position().column,
                receiver: obj,
                is_method: true,
            })
        }
        "identifier" => Some(CallSite {
            callee: node_text(func, src).to_string(),
            line: node.start_position().row + 1,
            col: node.start_position().column,
            receiver: None,
            is_method: false,
        }),
        _ => None,
    }
}

#[cfg(feature = "tree-sitter")]
fn parse_call_go(node: Node, src: &str) -> Option<CallSite> {
    let func = node.child(0)?;
    match func.kind() {
        "selector_expression" => {
            let field = find_child_by_kind(func, "field_identifier")?;
            let obj = func.child(0).map(|r| node_text(r, src).to_string());
            Some(CallSite {
                callee: node_text(field, src).to_string(),
                line: node.start_position().row + 1,
                col: node.start_position().column,
                receiver: obj,
                is_method: true,
            })
        }
        "identifier" => Some(CallSite {
            callee: node_text(func, src).to_string(),
            line: node.start_position().row + 1,
            col: node.start_position().column,
            receiver: None,
            is_method: false,
        }),
        _ => None,
    }
}

#[cfg(feature = "tree-sitter")]
fn parse_call_java(node: Node, src: &str) -> Option<CallSite> {
    if node.kind() == "object_creation_expression" {
        // `new Foo(...)` / `new Foo<T>(...)` — the callee is the constructed type.
        let type_node = node
            .child_by_field_name("type")
            .or_else(|| find_child_by_kind(node, "type_identifier"))
            .or_else(|| find_child_by_kind(node, "scoped_type_identifier"))
            .or_else(|| find_child_by_kind(node, "generic_type"))?;
        let name = find_descendant_by_kind(type_node, "type_identifier").unwrap_or(type_node);
        return Some(CallSite {
            callee: node_text(name, src).to_string(),
            line: node.start_position().row + 1,
            col: node.start_position().column,
            receiver: None,
            is_method: false,
        });
    }

    if node.kind() == "method_invocation" {
        // `obj.method(...)` — the callee is the `name` field, not the leading
        // `object` identifier (which would otherwise be picked up first).
        let name = node
            .child_by_field_name("name")
            .or_else(|| find_child_by_kind(node, "identifier"))?;
        let obj = node
            .child_by_field_name("object")
            .map(|o| node_text(o, src).to_string());
        return Some(CallSite {
            callee: node_text(name, src).to_string(),
            line: node.start_position().row + 1,
            col: node.start_position().column,
            receiver: obj,
            is_method: true,
        });
    }

    let func = node.child(0)?;
    Some(CallSite {
        callee: node_text(func, src).to_string(),
        line: node.start_position().row + 1,
        col: node.start_position().column,
        receiver: None,
        is_method: false,
    })
}

#[cfg(feature = "tree-sitter")]
fn parse_call_kotlin(node: Node, src: &str) -> Option<CallSite> {
    let callee = node.child(0)?;

    match callee.kind() {
        "identifier" => Some(CallSite {
            callee: node_text(callee, src).to_string(),
            line: node.start_position().row + 1,
            col: node.start_position().column,
            receiver: None,
            is_method: false,
        }),
        "navigation_expression" => {
            let mut cursor = callee.walk();
            let children: Vec<Node> = callee.children(&mut cursor).collect();
            let callee_name = children
                .iter()
                .rev()
                .find(|child| child.kind() == "identifier")
                .map(|child| node_text(*child, src).to_string())?;
            let receiver = children
                .iter()
                .find(|child| {
                    matches!(
                        child.kind(),
                        "expression"
                            | "primary_expression"
                            | "identifier"
                            | "navigation_expression"
                            | "this_expression"
                            | "super_expression"
                    )
                })
                .map(|child| node_text(*child, src).to_string())
                .filter(|text| text != &callee_name);

            Some(CallSite {
                callee: callee_name,
                line: node.start_position().row + 1,
                col: node.start_position().column,
                receiver,
                is_method: true,
            })
        }
        _ => find_descendant_by_kind(callee, "identifier").map(|name| CallSite {
            callee: node_text(name, src).to_string(),
            line: node.start_position().row + 1,
            col: node.start_position().column,
            receiver: None,
            is_method: false,
        }),
    }
}