lean-ctx-server 3.12.1

The LeanCTX MCP server and its tools on top of LeanCTX | OSS.
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
// SPDX-License-Identifier: Apache-2.0
//! Unit tests for the semantic-search stack (moved verbatim in the split).

#[cfg(test)]
mod filter_tests {
    #[allow(clippy::wildcard_imports)]
    use super::super::*;

    #[test]
    fn filter_language_rust() {
        let f = SearchFilter::new(Some(&["rust".into()]), None).unwrap();
        assert!(f.matches("src/main.rs"));
        assert!(!f.matches("src/main.ts"));
    }

    #[test]
    fn filter_path_glob() {
        let f = SearchFilter::new(None, Some("rust/src/**")).unwrap();
        assert!(f.matches("rust/src/core/mod.rs"));
        assert!(!f.matches("website/src/pages/index.astro"));
    }
}

#[cfg(test)]
mod dense_availability_tests {
    #[allow(clippy::wildcard_imports)]
    use super::super::*;

    /// #1259: a project that never built embeddings must be reported as
    /// degraded, so the default path can say so instead of calling BM25 hits
    /// "semantic".
    #[test]
    fn missing_embeddings_bin_is_reported_as_degraded() {
        let dir = std::env::temp_dir().join("lean-ctx-dense-missing-test");
        let _ = std::fs::create_dir_all(&dir);
        assert_eq!(dense_index_missing(&dir), cfg!(feature = "embeddings"));
    }
}

#[cfg(test)]
mod root_resolution_tests {
    #[allow(clippy::wildcard_imports)]
    use super::super::*;

    #[test]
    fn subdir_filter_scopes_results_to_subtree() {
        let f = SearchFilter::new(None, None)
            .unwrap()
            .with_subdir(Some("crate_a/src".to_string()));
        assert!(f.is_active());
        assert!(f.matches("crate_a/src/auth.rs"));
        assert!(f.matches("crate_a/src/nested/db.rs"));
        assert!(!f.matches("crate_b/src/auth.rs"));
        // Boundary: the prefix must be a whole path segment, not a substring.
        assert!(!f.matches("crate_a/src_extra/x.rs"));
        // Backslash input is normalized before matching.
        assert!(f.matches(r"crate_a\src\win.rs"));
    }

    #[test]
    fn subdir_filter_combines_with_extension_filter() {
        let f = SearchFilter::new(Some(&["rust".to_string()]), None)
            .unwrap()
            .with_subdir(Some("src".to_string()));
        assert!(f.matches("src/main.rs"));
        assert!(!f.matches("src/readme.md"), "wrong extension");
        assert!(!f.matches("docs/main.rs"), "outside subdir");
    }

    #[test]
    fn empty_subdir_is_no_scope() {
        let f = SearchFilter::new(None, None)
            .unwrap()
            .with_subdir(Some(String::new()));
        assert!(!f.is_active());
        assert!(f.matches("anything/here.rs"));
    }

    #[test]
    fn search_subdir_filter_derives_relative_prefix() {
        let tmp = tempfile::tempdir().unwrap();
        let root = tmp.path();
        let sub = root.join("a").join("b");
        std::fs::create_dir_all(&sub).unwrap();
        assert_eq!(search_subdir_filter(root, &sub).as_deref(), Some("a/b"));
        assert_eq!(search_subdir_filter(root, root), None);
        // A path that is not under root yields no scope.
        assert_eq!(search_subdir_filter(&sub, root), None);
    }

    #[test]
    fn resolve_search_root_promotes_subdir_to_project_root() {
        // #948: the index namespace is keyed on the project root; a subdir search
        // must resolve to that same root (and keep the subdir as a scope) instead
        // of hashing to a different, empty namespace.
        let _lock = lean_ctx::core::data_dir::test_env_lock();
        let tmp = tempfile::tempdir().unwrap();
        let root = lean_ctx::core::pathutil::safe_canonicalize_or_self(tmp.path());
        let sub = root.join("crate_a").join("src");
        std::fs::create_dir_all(&sub).unwrap();

        // Pin the project root so resolution is deterministic regardless of host
        // config; remove the env before asserting so a failure cannot leak it.
        lean_ctx::test_env::set_var("LEAN_CTX_PROJECT_ROOT", root.to_string_lossy().as_ref());
        let from_sub = resolve_search_root(&sub.to_string_lossy());
        let from_root = resolve_search_root(&root.to_string_lossy());
        lean_ctx::test_env::remove_var("LEAN_CTX_PROJECT_ROOT");

        let (resolved, subdir) = from_sub.unwrap();
        assert_eq!(resolved, root, "subdir must promote to the project root");
        assert_eq!(subdir.as_deref(), Some("crate_a/src"));

        let (resolved_root, subdir_root) = from_root.unwrap();
        assert_eq!(resolved_root, root);
        assert_eq!(subdir_root, None, "the root itself carries no subdir scope");
    }

    #[test]
    fn resolve_search_root_errors_on_missing_path() {
        assert!(resolve_search_root("/definitely/not/here/xyzzy-7f3a91").is_err());
    }

    #[test]
    fn a_pinned_root_does_not_claim_another_projects_path() {
        // #1875: with project A pinned, a search rooted in project B must hit
        // B's corpus — the pin used to win for every path.
        let _lock = lean_ctx::core::data_dir::test_env_lock();
        let tmp = tempfile::tempdir().unwrap();
        let base = lean_ctx::core::pathutil::safe_canonicalize_or_self(tmp.path());
        let (a, b) = (base.join("project_a"), base.join("project_b"));
        for p in [&a, &b] {
            std::fs::create_dir_all(p.join(".git")).unwrap();
        }
        std::fs::create_dir_all(b.join("src")).unwrap();

        lean_ctx::test_env::set_var("LEAN_CTX_PROJECT_ROOT", a.to_string_lossy().as_ref());
        let from_b = resolve_search_root(&b.to_string_lossy());
        let from_b_sub = resolve_search_root(&b.join("src").to_string_lossy());
        let from_a = resolve_search_root(&a.to_string_lossy());
        lean_ctx::test_env::remove_var("LEAN_CTX_PROJECT_ROOT");

        assert_eq!(from_b.unwrap(), (b.clone(), None));
        assert_eq!(from_b_sub.unwrap(), (b, Some("src".to_string())));
        assert_eq!(from_a.unwrap(), (a, None), "paths inside the pin keep it");
    }

    #[test]
    fn request_authority_still_rejects_another_projects_path() {
        let tmp = tempfile::tempdir().unwrap();
        let a = tmp.path().join("authorized");
        let b = tmp.path().join("foreign");
        std::fs::create_dir_all(a.join("src")).unwrap();
        std::fs::create_dir_all(&b).unwrap();
        lean_ctx::core::policy::runtime::REQUEST_PROJECT.sync_scope(
            std::cell::RefCell::new(Some(a.clone())),
            || {
                assert_eq!(
                    resolve_search_root(&a.join("src").to_string_lossy()).unwrap(),
                    (a.canonicalize().unwrap(), Some("src".into()))
                );
                assert_eq!(
                    resolve_search_root(&b.to_string_lossy()).unwrap_err(),
                    "source path is outside the request authority"
                );
            },
        );
    }

    /// I07/S03: the search query is remembered in the caller's own session.
    /// It was saved into the project's latest session — another agent's when
    /// that agent had saved last.
    #[test]
    fn the_search_query_lands_in_the_callers_session_not_the_latest() {
        use lean_ctx::core::session::SessionState;
        let _data = lean_ctx::core::data_dir::isolated_data_dir();
        let _community = lean_ctx::core::policy::runtime::TestPolicyOverride::set(None);
        let tmp = tempfile::tempdir().unwrap();
        let root = lean_ctx::core::pathutil::safe_canonicalize_or_self(tmp.path());
        std::fs::create_dir_all(root.join(".git")).unwrap();
        std::fs::write(
            root.join("auth.rs"),
            "fn validate_token(token: &str) -> bool { true }\n",
        )
        .unwrap();
        let root_s = root.to_string_lossy().into_owned();

        let mut a1 = SessionState::new();
        a1.project_root = Some(root_s.clone());
        a1.save().unwrap();
        let mut a2 = SessionState::new();
        a2.project_root = Some(root_s.clone());
        let a2 = std::sync::Arc::new(tokio::sync::RwLock::new(a2));

        let search = || {
            handle_for_tool(
                "ctx_search",
                "validate token",
                &root_s,
                5,
                CrpMode::Off,
                None,
                None,
                Some("bm25"),
                None,
                None,
            )
        };
        lean_ctx::core::session::caller::scope(Some(a2.clone()), search);
        // Without a caller (CLI) nothing is written anywhere.
        search();

        let a1_stored = SessionState::load_by_id_for_project_root(&a1.id, &root_s)
            .unwrap()
            .unwrap();
        assert_eq!(a1_stored.last_semantic_query, None);
        assert_eq!(
            a2.try_read().unwrap().last_semantic_query.as_deref(),
            Some("validate token")
        );
    }
}

#[cfg(all(test, feature = "embeddings"))]
mod cold_start_guard_tests {
    #[allow(clippy::wildcard_imports)]
    use super::super::*;

    #[test]
    fn budget_zero_disables_guard() {
        // 0 = "always embed inline" (pre-#512 behavior), regardless of size.
        assert!(!exceeds_inline_embed_budget(1_000_000, 0));
    }

    #[test]
    fn budget_is_inclusive_and_triggers_above_threshold() {
        assert!(!exceeds_inline_embed_budget(0, 2000), "warm index: inline");
        assert!(
            !exceeds_inline_embed_budget(2000, 2000),
            "at the budget: still inline"
        );
        assert!(
            exceeds_inline_embed_budget(2001, 2000),
            "over the budget: degrade"
        );
    }

    #[test]
    fn default_threshold_positive_when_env_unset() {
        // With the env override unset the default must be a real, positive guard.
        if std::env::var_os("LEAN_CTX_HYBRID_INLINE_EMBED_MAX").is_none() {
            assert!(inline_embed_max_chunks() >= 1);
        }
    }

    #[test]
    fn dense_build_hint_always_points_at_the_cli_build() {
        let full = dense_build_hint(22_741, false);
        assert!(full.contains("lean-ctx index build-semantic"));
        assert!(full.contains("22741"));
        let compact = dense_build_hint(22_741, true);
        assert!(compact.contains("lean-ctx index build-semantic"));
        assert!(compact.contains("22741"));
    }
}

#[cfg(test)]
mod determinism_tests {
    #[allow(clippy::wildcard_imports)]
    use super::super::*;

    #[test]
    fn rrf_merge_hybrid_is_deterministic_on_ties() {
        let a = HybridResult {
            file_path: "a.rs".to_string(),
            symbol_name: "foo".to_string(),
            kind: lean_ctx::core::bm25_index::ChunkKind::Function,
            start_line: 1,
            end_line: 1,
            snippet: "a".to_string(),
            rrf_score: 0.0,
            bm25_score: None,
            dense_score: None,
            bm25_rank: None,
            dense_rank: None,
        };
        let b = HybridResult {
            file_path: "b.rs".to_string(),
            symbol_name: "foo".to_string(),
            kind: lean_ctx::core::bm25_index::ChunkKind::Function,
            start_line: 1,
            end_line: 1,
            snippet: "b".to_string(),
            rrf_score: 0.0,
            bm25_score: None,
            dense_score: None,
            bm25_rank: None,
            dense_rank: None,
        };

        // Two lists with swapped ranks yield identical RRF sums for a and b.
        let fused = rrf_merge_hybrid(
            vec![
                ("root".to_string(), vec![a.clone(), b.clone()]),
                ("root".to_string(), vec![b.clone(), a.clone()]),
            ],
            10,
        );

        assert_eq!(fused.len(), 2);
        assert_eq!(fused[0].file_path, "a.rs");
        assert_eq!(fused[1].file_path, "b.rs");
    }
}

#[cfg(test)]
mod dense_config_tests {
    use lean_ctx::core::hybrid_search::HybridConfig;

    /// #686: dense stays on by default — the flip is opt-in, no behavior change.
    #[test]
    fn dense_enabled_defaults_true() {
        assert!(HybridConfig::default().dense_enabled);
    }

    /// #686: `[search].dense_enabled = false` parses and leaves siblings at default.
    #[test]
    fn dense_enabled_deserializes_false() {
        let cfg: HybridConfig = toml::from_str("dense_enabled = false").unwrap();
        assert!(!cfg.dense_enabled);
        assert_eq!(cfg.bm25_candidates, 75);
        assert_eq!(cfg.splade_weight, 0.5);
    }
}

#[cfg(all(test, feature = "embeddings"))]
mod dense_toggle_tests {
    #[allow(clippy::wildcard_imports)]
    use super::super::*;
    use lean_ctx::core::bm25_index::{BM25Index, ChunkKind, CodeChunk, tokenize};
    use lean_ctx::core::hybrid_search::HybridConfig;

    fn small_index() -> BM25Index {
        BM25Index::from_chunks_for_test(vec![
            CodeChunk {
                file_path: "auth.rs".into(),
                symbol_name: "validate_token".into(),
                kind: ChunkKind::Function,
                start_line: 1,
                end_line: 10,
                content: "fn validate_token(token: &str) -> bool { check_jwt_expiry(token) }"
                    .into(),
                tokens: tokenize("fn validate_token token str bool check_jwt_expiry token"),
                token_count: 0,
            },
            CodeChunk {
                file_path: "db.rs".into(),
                symbol_name: "connect_database".into(),
                kind: ChunkKind::Function,
                start_line: 1,
                end_line: 5,
                content: "fn connect_database(url: &str) -> Pool { create_pool(url) }".into(),
                tokens: tokenize("fn connect_database url str Pool create_pool url"),
                token_count: 0,
            },
        ])
    }

    /// #686: the dense-disabled body ranks via BM25 (+ graph + rerank + SPLADE),
    /// emits a BM25 header, finds the lexical match, and crucially never loads the
    /// embedding engine or writes `embeddings.json` — the on-disk vector footprint
    /// and embed latency disappear.
    #[test]
    fn bm25_graph_search_ranks_without_embeddings() {
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();
        let index = small_index();
        let cfg = HybridConfig {
            dense_enabled: false,
            ..Default::default()
        };
        let filter = SearchFilter::new(None, None).unwrap();

        let out = bm25_graph_search(
            "jwt token validation",
            root,
            &index,
            5,
            false,
            &filter,
            &cfg,
        );

        assert!(
            out.contains("Semantic search (BM25"),
            "expected BM25 header, got: {out}"
        );
        assert!(
            out.contains("validate_token"),
            "expected lexical match, got: {out}"
        );
        assert!(
            !root.join("embeddings.json").exists(),
            "dense-disabled path must not persist embeddings.json"
        );
    }
}