lean-ctx-engine 3.12.1

LeanCTX | OSS library: the open-source runtime for the Context Gateway for AI agents behind the lean-ctx CLI, MCP server and proxy integrations.
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
// SPDX-License-Identifier: Apache-2.0
//! Semantic escalation for call edges structure cannot settle.
//!
//! Only edges whose callee tree-sitter could not bind to the caller's scope
//! are escalated: an ambiguous name (several project definitions), a name that
//! is merely unique project-wide, or a path call (`db::save`). Scope-bound
//! edges (same file, unique import) and unknown names (no project definition)
//! are never queried.

use std::collections::{BTreeMap, BTreeSet, HashMap};
use std::time::{Duration, Instant};

use crate::core::call_graph::{
    CallEdge, CallGraphInputs, ScopeMatch, StructuralTarget, callee_segment,
};
use crate::core::config::SemanticMode;
use crate::core::property_graph::{CachedResolution, CodeGraph};
use crate::lsp::router::{LiveIdentity, StartPolicy};

use super::resolve::{Access, Resolution, ResolveError, live_backend_identity, resolve_definition};

const OP_DEFINITION: &str = "definition";

/// Limits for one escalation run. Cached answers are free; only live
/// backend queries count.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct EscalationBudget {
    pub max_live_queries: usize,
    /// Wall-clock budget for all live queries of the run.
    pub wall: Duration,
    /// Cap per request, so one slow answer cannot hold the backend long.
    pub per_request: Duration,
}

impl EscalationBudget {
    /// Background enrichment (after a graph build, or while a backend is in
    /// use). Measured ~65 ms per warm rust-analyzer lookup on a 2.4k-file
    /// crate, so one pass decides ~1000 file-pair groups in about a minute,
    /// off every interactive path.
    pub const BACKGROUND: Self = Self {
        max_live_queries: 1000,
        wall: Duration::from_mins(1),
        per_request: Duration::from_secs(5),
    };
    /// An interactive tool call (`ctx_callgraph`): must answer promptly.
    pub const INTERACTIVE: Self = Self {
        max_live_queries: 20,
        wall: Duration::from_secs(4),
        per_request: Duration::from_secs(2),
    };
}

/// What the semantic backend says about one call edge.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SemanticVerdict {
    /// Verified callee file (and backend identity).
    Verified { file: String, backend: String },
    /// The backend located the callee's definition outside the project: a
    /// structural name-match guess for this edge is wrong. ("No definition
    /// found" is *not* this — a server that is still indexing answers that.)
    NotInProject,
}

/// Counters for status reporting and tests.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct EscalationStats {
    pub candidates: usize,
    pub cache_hits: usize,
    pub live_queries: usize,
    pub verified: usize,
    pub not_in_project: usize,
    /// Candidates left unresolved (no backend, budget, ambiguity, errors).
    pub unresolved: usize,
}

/// Result of one escalation run, index-aligned with the input edges.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Escalation {
    /// `None` = no semantic information; keep the structural answer.
    pub verdicts: Vec<Option<SemanticVerdict>>,
    /// `true` = a candidate that got no *definitive* answer this run (backend
    /// unavailable or busy, budget exhausted, timeout, "no result"). Absence
    /// of an answer is not evidence: callers must not treat these sites'
    /// earlier semantic edges as disproved.
    pub unsettled: Vec<bool>,
    pub stats: EscalationStats,
}

fn needs_escalation(edge: &CallEdge, structural: &StructuralTarget) -> bool {
    let uncertain = matches!(
        structural,
        StructuralTarget::Ambiguous
            | StructuralTarget::Resolved {
                via: ScopeMatch::UniqueInProject,
                ..
            }
    );
    uncertain
        && edge.callee_pos.is_some()
        && std::path::Path::new(&edge.caller_file)
            .extension()
            .and_then(|e| e.to_str())
            .and_then(crate::lsp::config::language_for_extension)
            .is_some()
}

/// Fingerprint of the files defining `name` *and their contents* — the part
/// of the project (besides the caller itself) a definition lookup depends on.
/// Adding, moving or removing a same-named definition, or any edit inside a
/// defining file (a new overload, a changed export), changes it.
fn definitions_fingerprint(
    defs: &BTreeMap<&str, BTreeSet<&str>>,
    name: &str,
    file_hashes: &HashMap<String, String>,
    dependency_revision: &str,
) -> String {
    let mut entries: Vec<String> = defs
        .get(name)
        .into_iter()
        .flatten()
        .map(|f| format!("{f}\t{}", file_hashes.get(*f).map_or("", String::as_str)))
        .collect();
    entries.push(dependency_revision.to_string());
    crate::core::hasher::hash_str(&entries.join("\n"))
}

/// Manifests and lockfiles whose change can re-route a call between the
/// project and a dependency (a glob import gaining a symbol) without any
/// indexed source file changing.
const DEPENDENCY_FILES: &[&str] = &[
    "Cargo.toml",
    "Cargo.lock",
    "package.json",
    "package-lock.json",
    "pnpm-lock.yaml",
    "yarn.lock",
    "go.mod",
    "go.sum",
    "pyproject.toml",
    "poetry.lock",
    "uv.lock",
    "requirements.txt",
];

/// Revision of the project's dependency manifests at its root. Nested
/// workspace manifests are covered through their root lockfile.
fn dependency_revision(project_root: &str) -> String {
    let entries: Vec<String> = DEPENDENCY_FILES
        .iter()
        .filter_map(|name| {
            let content = std::fs::read(std::path::Path::new(project_root).join(name)).ok()?;
            Some(format!("{name}\t{}", blake3::hash(&content).to_hex()))
        })
        .collect();
    crate::core::hasher::hash_str(&entries.join("\n"))
}

/// What a cached answer is worth now.
#[derive(Debug, PartialEq, Eq)]
enum CacheUse {
    /// Still valid; `None` = a definitive "ambiguous" with nothing to add.
    Reuse(Option<SemanticVerdict>),
    /// Must be re-resolved.
    Stale,
    /// Cannot be checked right now (the live backend is busy): neither reuse
    /// it nor query — the site stays unsettled for this run.
    Defer,
}

/// Validates a cached answer: same definition context, no *different* live
/// backend, and — for a resolved target — the recorded definition line still
/// lies inside a symbol of the callee's name.
fn verdict_from_cache(
    hit: &CachedResolution,
    inputs: &CallGraphInputs,
    callee: &str,
    context: &str,
    live: &LiveIdentity,
) -> CacheUse {
    if hit.context != context {
        return CacheUse::Stale;
    }
    match live {
        LiveIdentity::Known(id) if *id != hit.backend => return CacheUse::Stale,
        LiveIdentity::Busy => return CacheUse::Defer,
        LiveIdentity::Known(_) | LiveIdentity::NotRunning => {}
    }
    match hit.outcome.as_str() {
        "resolved" => {
            let (Some(file), Some(line)) = (hit.target_file.clone(), hit.target_line) else {
                return CacheUse::Stale;
            };
            let name = callee_segment(callee);
            let still_there = inputs.symbols.iter().any(|s| {
                s.file == file && s.name == name && s.start_line <= line && line <= s.end_line
            });
            if still_there {
                CacheUse::Reuse(Some(SemanticVerdict::Verified {
                    file,
                    backend: hit.backend.clone(),
                }))
            } else {
                CacheUse::Stale
            }
        }
        "external" => CacheUse::Reuse(Some(SemanticVerdict::NotInProject)),
        "ambiguous" => CacheUse::Reuse(None),
        _ => CacheUse::Stale,
    }
}

/// Cache record for a definitive answer. `NoResult` is not definitive — a
/// cold server answers it while indexing — so it is never cached (`None`).
fn to_cache(resolution: &Resolution, backend: &str, context: &str) -> Option<CachedResolution> {
    let (outcome, target_file, target_line, target_symbol) = match resolution {
        Resolution::Resolved { file, line, symbol } => {
            ("resolved", Some(file.clone()), Some(*line), symbol.clone())
        }
        Resolution::External => ("external", None, None, None),
        Resolution::Ambiguous => ("ambiguous", None, None, None),
        Resolution::NoResult => return None,
    };
    Some(CachedResolution {
        backend: backend.to_string(),
        outcome: outcome.to_string(),
        target_file,
        target_line,
        target_symbol,
        context: context.to_string(),
    })
}

fn ext_of(file: &str) -> String {
    std::path::Path::new(file)
        .extension()
        .and_then(|e| e.to_str())
        .unwrap_or("")
        .to_string()
}

/// Order in which uncertain sites are asked: a budget-limited run must decide
/// as many file-level edges as possible, so it takes one site of every
/// `(caller file, structural target)` group before a second site of any —
/// guessed edges (a name match that may be false) before ambiguous calls
/// (an edge that is missing). Deterministic: ties break by file and position.
fn escalation_order(
    edges: &[CallEdge],
    structural: &[StructuralTarget],
    candidates: Vec<usize>,
) -> Vec<usize> {
    let group = |i: usize| match &structural[i] {
        StructuralTarget::Resolved { file, .. } => (0u8, file.as_str()),
        _ => (1u8, edges[i].callee_name.as_str()),
    };
    let mut by_site = candidates;
    by_site.sort_by(|&a, &b| {
        (&edges[a].caller_file, edges[a].callee_pos)
            .cmp(&(&edges[b].caller_file, edges[b].callee_pos))
    });
    let mut seen: HashMap<(&str, (u8, &str)), usize> = HashMap::new();
    let mut ranked: Vec<(usize, u8, usize)> = by_site
        .iter()
        .map(|&i| {
            let (class, key) = group(i);
            let rank = seen
                .entry((edges[i].caller_file.as_str(), (class, key)))
                .or_insert(0);
            *rank += 1;
            (*rank, class, i)
        })
        .collect();
    // Stable: equal (rank, class) keep the file/position order.
    ranked.sort_by_key(|&(rank, class, _)| (rank, class));
    ranked.into_iter().map(|(_, _, i)| i).collect()
}

/// Escalates every uncertain call edge, in `escalation_order` so a
/// budget-limited run always covers the same, most decisive subset.
pub fn escalate_calls(
    graph: &CodeGraph,
    project_root: &str,
    inputs: &CallGraphInputs,
    edges: &[CallEdge],
    structural: &[StructuralTarget],
    file_hashes: &HashMap<String, String>,
    mode: SemanticMode,
    budget: EscalationBudget,
) -> Escalation {
    let mut out = Escalation {
        verdicts: vec![None; edges.len()],
        unsettled: vec![false; edges.len()],
        stats: EscalationStats::default(),
    };
    let candidates: Vec<usize> = (0..edges.len())
        .filter(|&i| needs_escalation(&edges[i], &structural[i]))
        .collect();
    out.stats.candidates = candidates.len();
    if mode == SemanticMode::Off {
        // Off is a deliberate choice, not missing evidence: nothing pending.
        return out;
    }
    let order = escalation_order(edges, structural, candidates);

    let access = Access {
        policy: if mode == SemanticMode::Eager {
            StartPolicy::Lazy
        } else {
            StartPolicy::ReuseOnly
        },
        wait: false,
        timeout: Some(budget.per_request),
        deadline: None,
    };
    let mut defs: BTreeMap<&str, BTreeSet<&str>> = BTreeMap::new();
    for s in &inputs.symbols {
        defs.entry(s.name.as_str())
            .or_default()
            .insert(s.file.as_str());
    }

    // Stale cache rows are pruned by the background pass, never here: an
    // interactive call must not queue for the write lock, and lookups are
    // keyed by the caller's content hash anyway.
    let deps = dependency_revision(project_root);
    // One storage-admission snapshot for every answer this run keeps.
    let admission = crate::core::context_admission::stores::StoreAdmission::current();
    let started = Instant::now();
    // Per language: identity of the live backend (a cheap registry peek,
    // remembered unless the backend was busy) and whether the backend is
    // unavailable for the rest of this run.
    let mut live_identity: HashMap<String, LiveIdentity> = HashMap::new();
    let mut unavailable: BTreeSet<String> = BTreeSet::new();

    for i in order {
        let edge = &edges[i];
        out.unsettled[i] = true;
        let (Some((line, col)), Some(hash)) = (edge.callee_pos, file_hashes.get(&edge.caller_file))
        else {
            continue;
        };
        let site = (edge.caller_file.as_str(), line, col, OP_DEFINITION);
        let context =
            definitions_fingerprint(&defs, callee_segment(&edge.callee_name), file_hashes, &deps);
        let ext = ext_of(&edge.caller_file);

        if let Ok(Some(hit)) = graph.semantic_lookup(site, hash) {
            if live_identity
                .get(&ext)
                .is_none_or(|l| *l == LiveIdentity::Busy)
            {
                live_identity.insert(
                    ext.clone(),
                    live_backend_identity(project_root, &edge.caller_file),
                );
            }
            let live = live_identity.get(&ext).unwrap_or(&LiveIdentity::NotRunning);
            match verdict_from_cache(&hit, inputs, &edge.callee_name, &context, live) {
                CacheUse::Reuse(v) => {
                    out.stats.cache_hits += 1;
                    out.verdicts[i] = v;
                    out.unsettled[i] = false;
                    continue;
                }
                CacheUse::Defer => continue,
                CacheUse::Stale => {}
            }
        }

        let remaining = budget.wall.saturating_sub(started.elapsed());
        if unavailable.contains(&ext)
            || out.stats.live_queries >= budget.max_live_queries
            || remaining.is_zero()
        {
            continue;
        }

        out.stats.live_queries += 1;
        let access = Access {
            deadline: Some(started + budget.wall),
            ..access
        };
        match resolve_definition(project_root, &edge.caller_file, (line, col), access, inputs) {
            Ok(answer) => {
                if let Some(record) = to_cache(&answer.resolution, &answer.backend, &context) {
                    let _ = graph.semantic_store(site, hash, &record, &admission);
                }
                live_identity.insert(ext, LiveIdentity::Known(answer.backend.clone()));
                (out.verdicts[i], out.unsettled[i]) = match answer.resolution {
                    Resolution::Resolved { file, .. } => (
                        Some(SemanticVerdict::Verified {
                            file,
                            backend: answer.backend,
                        }),
                        false,
                    ),
                    Resolution::External => (Some(SemanticVerdict::NotInProject), false),
                    Resolution::Ambiguous => (None, false),
                    Resolution::NoResult => (None, true),
                };
            }
            // Never cached: retried next run. An unavailable or busy backend
            // is the same for every remaining site of this language.
            Err(ResolveError::Unavailable(_)) => {
                unavailable.insert(ext);
            }
            Err(ResolveError::Site(_)) => {}
        }
    }

    for v in out.verdicts.iter().flatten() {
        match v {
            SemanticVerdict::Verified { .. } => out.stats.verified += 1,
            SemanticVerdict::NotInProject => out.stats.not_in_project += 1,
        }
    }
    out.stats.unresolved = out.stats.candidates - out.stats.verified - out.stats.not_in_project;
    tracing::debug!(
        target: "lean_ctx::semantic",
        "escalate: {} candidates, {} cached, {} live, {} verified, {} external in {} ms",
        out.stats.candidates,
        out.stats.cache_hits,
        out.stats.live_queries,
        out.stats.verified,
        out.stats.not_in_project,
        started.elapsed().as_millis()
    );
    out
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::core::call_graph::SymbolSpan;

    fn edge(file: &str, callee: &str, pos: Option<(usize, usize)>) -> CallEdge {
        CallEdge {
            caller_file: file.into(),
            caller_symbol: "f".into(),
            caller_line: 1,
            callee_name: callee.into(),
            callee_pos: pos,
            ..Default::default()
        }
    }

    #[test]
    fn only_scope_unbound_edges_with_a_position_are_escalated() {
        let resolved = |via| StructuralTarget::Resolved {
            file: "b.rs".into(),
            via,
        };
        let e = edge("a.rs", "save", Some((1, 4)));
        assert!(needs_escalation(&e, &StructuralTarget::Ambiguous));
        assert!(needs_escalation(&e, &resolved(ScopeMatch::UniqueInProject)));
        assert!(!needs_escalation(&e, &resolved(ScopeMatch::SameFile)));
        assert!(!needs_escalation(&e, &resolved(ScopeMatch::UniqueImport)));
        assert!(!needs_escalation(&e, &StructuralTarget::Unknown));
        assert!(!needs_escalation(
            &edge("a.rs", "save", None),
            &StructuralTarget::Ambiguous
        ));
        assert!(
            !needs_escalation(
                &edge("a.md", "save", Some((1, 0))),
                &StructuralTarget::Ambiguous
            ),
            "no semantic backend for the language"
        );
    }

    #[test]
    fn cached_answers_are_reused_only_while_their_context_holds() {
        let inputs = CallGraphInputs {
            symbols: vec![SymbolSpan {
                file: "repo.rs".into(),
                name: "save".into(),
                start_line: 5,
                end_line: 9,
                ..Default::default()
            }],
            ..Default::default()
        };
        let hit = |file: &str, line: usize| CachedResolution {
            backend: "lsp:ra@1".into(),
            outcome: "resolved".into(),
            target_file: Some(file.into()),
            target_line: Some(line),
            target_symbol: None,
            context: "ctx".into(),
        };
        let verified = CacheUse::Reuse(Some(SemanticVerdict::Verified {
            file: "repo.rs".into(),
            backend: "lsp:ra@1".into(),
        }));
        let none = LiveIdentity::NotRunning;
        let check = |h: &CachedResolution, ctx: &str, live: &LiveIdentity| {
            verdict_from_cache(h, &inputs, "save", ctx, live)
        };

        assert_eq!(check(&hit("repo.rs", 6), "ctx", &none), verified);
        assert_eq!(
            check(
                &hit("repo.rs", 6),
                "ctx",
                &LiveIdentity::Known("lsp:ra@1".into())
            ),
            verified
        );
        assert_eq!(
            check(
                &hit("repo.rs", 6),
                "ctx",
                &LiveIdentity::Known("lsp:ra@2".into())
            ),
            CacheUse::Stale,
            "another server version answers anew"
        );
        assert_eq!(
            check(&hit("repo.rs", 6), "ctx", &LiveIdentity::Busy),
            CacheUse::Defer,
            "a busy backend cannot vouch for the old answer"
        );
        assert_eq!(
            check(&hit("repo.rs", 6), "other-ctx", &none),
            CacheUse::Stale,
            "the `save` definitions (set or content) changed"
        );
        assert_eq!(
            check(&hit("repo.rs", 2), "ctx", &none),
            CacheUse::Stale,
            "the recorded definition line no longer holds a `save`"
        );
        assert_eq!(check(&hit("moved.rs", 6), "ctx", &none), CacheUse::Stale);
        let ambiguous = CachedResolution {
            outcome: "ambiguous".into(),
            target_file: None,
            ..hit("repo.rs", 6)
        };
        assert_eq!(
            check(&ambiguous, "ctx", &none),
            CacheUse::Reuse(None),
            "a definitive ambiguous answer is reused, not re-asked"
        );
    }

    #[test]
    fn unanswered_candidates_are_reported_unsettled_but_off_mode_is_settled() {
        let g = CodeGraph::open_in_memory().unwrap();
        let edges = [edge("a.rs", "save", Some((1, 4)))];
        let hashes = HashMap::from([("a.rs".to_string(), "h".to_string())]);
        let run = |mode| {
            escalate_calls(
                &g,
                "/nonexistent/leanctx-escalation",
                &CallGraphInputs::default(),
                &edges,
                &[StructuralTarget::Ambiguous],
                &hashes,
                mode,
                EscalationBudget::INTERACTIVE,
            )
        };

        let off = run(SemanticMode::Off);
        assert_eq!((off.verdicts, off.unsettled), (vec![None], vec![false]));

        // No backend can serve this root: no verdict, and the site stays
        // unsettled so earlier semantic edges are not pruned on its account.
        let auto = run(SemanticMode::Auto);
        assert_eq!((auto.verdicts, auto.unsettled), (vec![None], vec![true]));
    }
}