codelens-engine 1.13.34

Harness-native Rust MCP server for code intelligence — hybrid retrieval, mutation-gated workflows, and a token-lean response contract tuned for frontier agent models (Claude Fable-class)
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
use serde::Serialize;
use std::path::{Path, PathBuf};

#[derive(Debug, Clone, Serialize)]
pub struct LspRecipe {
    pub language: &'static str,
    pub extensions: &'static [&'static str],
    pub server_name: &'static str,
    pub install_command: &'static str,
    pub binary_name: &'static str,
    pub args: &'static [&'static str],
    pub package_manager: &'static str,
}

pub const LSP_RECIPES: &[LspRecipe] = &[
    LspRecipe {
        language: "python",
        extensions: &["py"],
        server_name: "pyright",
        install_command: "npm install -g pyright",
        binary_name: "pyright-langserver",
        args: &["--stdio"],
        package_manager: "npm",
    },
    LspRecipe {
        language: "typescript",
        extensions: &["ts", "tsx", "js", "jsx", "mjs", "cjs"],
        server_name: "typescript-language-server",
        install_command: "npm install -g typescript-language-server typescript",
        binary_name: "typescript-language-server",
        args: &["--stdio"],
        package_manager: "npm",
    },
    LspRecipe {
        language: "rust",
        extensions: &["rs"],
        server_name: "rust-analyzer",
        install_command: "rustup component add rust-analyzer",
        binary_name: "rust-analyzer",
        args: &[],
        package_manager: "rustup",
    },
    LspRecipe {
        language: "go",
        extensions: &["go"],
        server_name: "gopls",
        install_command: "go install golang.org/x/tools/gopls@latest",
        binary_name: "gopls",
        args: &["serve"],
        package_manager: "go",
    },
    LspRecipe {
        language: "java",
        extensions: &["java"],
        server_name: "jdtls",
        install_command: "brew install jdtls",
        binary_name: "jdtls",
        args: &[],
        package_manager: "brew",
    },
    LspRecipe {
        language: "kotlin",
        extensions: &["kt", "kts"],
        server_name: "kotlin-language-server",
        install_command: "brew install kotlin-language-server",
        binary_name: "kotlin-language-server",
        args: &[],
        package_manager: "brew",
    },
    LspRecipe {
        language: "c_cpp",
        extensions: &["c", "h", "cpp", "cc", "cxx", "hpp"],
        server_name: "clangd",
        install_command: "brew install llvm",
        binary_name: "clangd",
        args: &["--background-index"],
        package_manager: "brew",
    },
    LspRecipe {
        language: "ruby",
        extensions: &["rb"],
        server_name: "solargraph",
        install_command: "gem install solargraph",
        binary_name: "solargraph",
        args: &["stdio"],
        package_manager: "gem",
    },
    LspRecipe {
        language: "php",
        extensions: &["php"],
        server_name: "intelephense",
        install_command: "npm install -g intelephense",
        binary_name: "intelephense",
        args: &["--stdio"],
        package_manager: "npm",
    },
    LspRecipe {
        language: "scala",
        extensions: &["scala", "sc"],
        server_name: "metals",
        install_command: "cs install metals",
        binary_name: "metals",
        args: &[],
        package_manager: "coursier",
    },
    LspRecipe {
        language: "swift",
        extensions: &["swift"],
        server_name: "sourcekit-lsp",
        install_command: "xcode-select --install",
        binary_name: "sourcekit-lsp",
        args: &[],
        package_manager: "xcode",
    },
    LspRecipe {
        language: "csharp",
        extensions: &["cs"],
        server_name: "omnisharp",
        install_command: "dotnet tool install -g csharp-ls",
        binary_name: "csharp-ls",
        args: &[],
        package_manager: "dotnet",
    },
    LspRecipe {
        language: "dart",
        extensions: &["dart"],
        server_name: "dart-language-server",
        install_command: "dart pub global activate dart_language_server",
        binary_name: "dart",
        args: &["language-server", "--protocol=lsp"],
        package_manager: "dart",
    },
    // Phase 6a: new languages
    LspRecipe {
        language: "lua",
        extensions: &["lua"],
        server_name: "lua-language-server",
        install_command: "brew install lua-language-server",
        binary_name: "lua-language-server",
        args: &[],
        package_manager: "brew",
    },
    LspRecipe {
        language: "zig",
        extensions: &["zig"],
        server_name: "zls",
        install_command: "brew install zls",
        binary_name: "zls",
        args: &[],
        package_manager: "brew",
    },
    LspRecipe {
        language: "elixir",
        extensions: &["ex", "exs"],
        server_name: "next-ls",
        install_command: "mix escript.install hex next_ls",
        binary_name: "nextls",
        args: &["--stdio"],
        package_manager: "mix",
    },
    LspRecipe {
        language: "haskell",
        extensions: &["hs"],
        server_name: "haskell-language-server",
        install_command: "ghcup install hls",
        binary_name: "haskell-language-server-wrapper",
        args: &["--lsp"],
        package_manager: "ghcup",
    },
    LspRecipe {
        language: "ocaml",
        extensions: &["ml", "mli"],
        server_name: "ocamllsp",
        install_command: "opam install ocaml-lsp-server",
        binary_name: "ocamllsp",
        args: &[],
        package_manager: "opam",
    },
    LspRecipe {
        language: "erlang",
        extensions: &["erl", "hrl"],
        server_name: "erlang_ls",
        install_command: "brew install erlang_ls",
        binary_name: "erlang_ls",
        args: &[],
        package_manager: "brew",
    },
    LspRecipe {
        language: "r",
        extensions: &["r", "R"],
        server_name: "languageserver",
        install_command: "R -e 'install.packages(\"languageserver\")'",
        binary_name: "R",
        args: &["--slave", "-e", "languageserver::run()"],
        package_manager: "R",
    },
    LspRecipe {
        language: "shellscript",
        extensions: &["sh", "bash"],
        server_name: "bash-language-server",
        install_command: "npm install -g bash-language-server",
        binary_name: "bash-language-server",
        args: &["start"],
        package_manager: "npm",
    },
    LspRecipe {
        language: "julia",
        extensions: &["jl"],
        server_name: "julia-lsp",
        install_command: "julia -e 'using Pkg; Pkg.add(\"LanguageServer\")'",
        binary_name: "julia",
        args: &["--project=@.", "-e", "using LanguageServer; runserver()"],
        package_manager: "julia",
    },
    // Perl deferred until tree-sitter 0.26 upgrade
];

fn command_candidates(command: &str) -> Vec<String> {
    #[cfg(windows)]
    let mut candidates = vec![command.to_owned()];
    #[cfg(not(windows))]
    let candidates = vec![command.to_owned()];
    #[cfg(windows)]
    if Path::new(command).extension().is_none() {
        let pathext = std::env::var_os("PATHEXT")
            .and_then(|value| value.into_string().ok())
            .unwrap_or_else(|| ".COM;.EXE;.BAT;.CMD".to_owned());
        for ext in pathext.split(';').filter(|ext| !ext.is_empty()) {
            let normalized = if ext.starts_with('.') {
                ext.to_owned()
            } else {
                format!(".{ext}")
            };
            let candidate = format!("{command}{normalized}");
            if !candidates
                .iter()
                .any(|existing| existing.eq_ignore_ascii_case(&candidate))
            {
                candidates.push(candidate);
            }
        }
    }
    candidates
}

fn resolve_in_dir(dir: &Path, command: &str) -> Option<PathBuf> {
    command_candidates(command)
        .into_iter()
        .map(|candidate| dir.join(candidate))
        .find(|candidate| candidate.is_file())
}

fn fallback_search_dirs() -> Vec<PathBuf> {
    let mut dirs = Vec::new();
    #[cfg(not(windows))]
    {
        let home = std::env::var("HOME").unwrap_or_default();
        for dir in [
            "/opt/homebrew/bin".to_owned(),
            "/usr/local/bin".to_owned(),
            format!("{home}/.cargo/bin"),
            format!("{home}/.fnm/aliases/default/bin"),
            format!("{home}/.nvm/versions/node/current/bin"),
        ] {
            if !dir.is_empty() {
                dirs.push(PathBuf::from(dir));
            }
        }
    }
    #[cfg(windows)]
    {
        let user_profile = std::env::var("USERPROFILE").unwrap_or_default();
        let app_data = std::env::var("APPDATA").unwrap_or_default();
        for dir in [
            format!("{user_profile}\\.cargo\\bin"),
            format!("{app_data}\\npm"),
        ] {
            if !dir.is_empty() {
                dirs.push(PathBuf::from(dir));
            }
        }
    }
    dirs
}

/// Maximum number of parent directories to traverse when looking for a
/// `node_modules/.bin/<command>` shim. The Next.js / pnpm monorepo layout
/// rarely nests workspaces deeper than a few levels, so 8 is a generous
/// upper bound that still avoids walking out of the project tree.
const NODE_MODULES_TRAVERSE_DEPTH: usize = 8;

/// Walk `start` and up to `depth` parents, returning the path of the first
/// `node_modules/.bin/<command>` shim that exists. Used by the LSP resolver
/// so a Next.js / TS project that installs `typescript-language-server`
/// only as a devDependency does not have to globally install it.
fn find_in_node_modules_bin(start: &Path, command: &str, depth: usize) -> Option<PathBuf> {
    let mut current = Some(start);
    let mut steps = 0;
    while let Some(dir) = current {
        if steps > depth {
            break;
        }
        if let Some(path) = resolve_in_dir(&dir.join("node_modules").join(".bin"), command) {
            return Some(path);
        }
        current = dir.parent();
        steps += 1;
    }
    None
}

pub(crate) fn resolve_lsp_binary(command: &str) -> Option<PathBuf> {
    resolve_lsp_binary_with_hint(command, None)
}

/// Resolve an LSP binary with an optional `hint_dir`. When `hint_dir` is
/// `Some`, the resolver also walks up from that directory looking for a
/// `node_modules/.bin/<command>` shim before reporting the binary as
/// missing. This unblocks Node / TS projects that install LSP servers
/// as devDependencies (the Next.js standard pattern), where the global
/// PATH does not see the binary but the per-project shim does.
pub fn resolve_lsp_binary_with_hint(command: &str, hint_dir: Option<&Path>) -> Option<PathBuf> {
    let command_path = Path::new(command);
    if command_path.components().count() > 1 {
        return if command_path.is_file() {
            Some(command_path.to_path_buf())
        } else if let Some(parent) = command_path.parent() {
            resolve_in_dir(parent, command_path.file_name()?.to_str()?)
        } else {
            None
        };
    }

    if let Some(path_dirs) = std::env::var_os("PATH") {
        for dir in std::env::split_paths(&path_dirs) {
            if let Some(path) = resolve_in_dir(&dir, command) {
                return Some(path);
            }
        }
    }

    for dir in fallback_search_dirs() {
        if let Some(path) = resolve_in_dir(&dir, command) {
            return Some(path);
        }
    }

    if let Some(extra) = std::env::var_os("CODELENS_LSP_PATH_EXTRA") {
        for dir in std::env::split_paths(&extra) {
            if let Some(path) = resolve_in_dir(&dir, command) {
                return Some(path);
            }
        }
    }

    if let Some(start) = hint_dir
        && let Some(path) = find_in_node_modules_bin(start, command, NODE_MODULES_TRAVERSE_DEPTH)
    {
        return Some(path);
    }

    None
}

/// Return `true` when the given LSP binary is resolvable either via the
/// current `PATH` or via a conservative allow-list of common install
/// locations. This keeps runtime capability reporting and `check_lsp_status`
/// aligned even when the daemon inherits a minimal launchd/systemd PATH.
pub fn lsp_binary_exists(command: &str) -> bool {
    resolve_lsp_binary(command).is_some()
}

/// Like [`lsp_binary_exists`] but also walks up from `hint_dir` looking
/// for `node_modules/.bin/<command>` shims. Callers that have a concrete
/// project root or file path should prefer this — it lets capability
/// reporting return `installed = true` for Node / TS projects that ship
/// the LSP only as a devDependency.
pub fn lsp_binary_exists_with_hint(command: &str, hint_dir: Option<&Path>) -> bool {
    resolve_lsp_binary_with_hint(command, hint_dir).is_some()
}

/// Check which LSP servers are installed and which are missing.
pub fn check_lsp_status() -> Vec<LspStatus> {
    LSP_RECIPES
        .iter()
        .map(|recipe| LspStatus {
            language: recipe.language,
            server_name: recipe.server_name,
            installed: lsp_binary_exists(recipe.binary_name),
            install_command: recipe.install_command,
        })
        .collect()
}

#[derive(Debug, Clone, Serialize)]
pub struct LspStatus {
    pub language: &'static str,
    pub server_name: &'static str,
    pub installed: bool,
    pub install_command: &'static str,
}

/// Get the recipe for a file extension.
pub fn get_lsp_recipe(extension: &str) -> Option<&'static LspRecipe> {
    let ext = extension.trim_start_matches('.').to_ascii_lowercase();
    LSP_RECIPES
        .iter()
        .find(|r| r.extensions.contains(&ext.as_str()))
}

pub fn default_lsp_command_for_extension(extension: &str) -> Option<&'static str> {
    get_lsp_recipe(extension).map(|recipe| recipe.binary_name)
}

pub fn default_lsp_command_for_path(file_path: &str) -> Option<&'static str> {
    Path::new(file_path)
        .extension()
        .and_then(|ext| ext.to_str())
        .and_then(default_lsp_command_for_extension)
}

pub fn default_lsp_args_for_command(command: &str) -> Option<&'static [&'static str]> {
    LSP_RECIPES
        .iter()
        .find(|recipe| recipe.binary_name == command)
        .map(|recipe| recipe.args)
}

#[cfg(test)]
mod tests {
    use super::*;

    /// Issue #215: a Next.js / TS project that installs
    /// `typescript-language-server` only as a devDependency must still
    /// be reachable via `node_modules/.bin/<command>`. The hint
    /// directory points the resolver at the project root so the shim
    /// is found even when the daemon's PATH does not include it.
    #[test]
    fn resolves_lsp_via_node_modules_bin_when_hint_dir_provided() {
        let tempdir = tempfile::tempdir().expect("tempdir");
        let bin_dir = tempdir.path().join("node_modules").join(".bin");
        std::fs::create_dir_all(&bin_dir).expect("mkdir node_modules/.bin");
        let unique = format!(
            "phantom-lsp-{}",
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .map(|d| d.as_nanos())
                .unwrap_or(0)
        );
        let shim = bin_dir.join(&unique);
        std::fs::write(&shim, b"#!/bin/sh\nexit 0\n").expect("write shim");

        // Without the hint, resolution fails — the binary is not on PATH.
        assert!(
            resolve_lsp_binary(&unique).is_none(),
            "binary must be invisible without the hint dir"
        );

        // With the hint, the project-local shim is discovered.
        let resolved = resolve_lsp_binary_with_hint(&unique, Some(tempdir.path()))
            .expect("hint dir resolves the project-local shim");
        assert_eq!(resolved, shim);
        assert!(lsp_binary_exists_with_hint(&unique, Some(tempdir.path())));
    }

    /// The resolver walks up to `NODE_MODULES_TRAVERSE_DEPTH` parents,
    /// so a hint pointing at `<repo>/apps/web/src/lib/...` still finds
    /// `<repo>/node_modules/.bin/<command>` (npm/pnpm hoisting layout).
    #[test]
    fn resolves_lsp_via_node_modules_bin_in_parent_directory() {
        let tempdir = tempfile::tempdir().expect("tempdir");
        let nested = tempdir.path().join("apps/web/src/lib");
        std::fs::create_dir_all(&nested).expect("mkdir nested");
        let bin_dir = tempdir.path().join("node_modules").join(".bin");
        std::fs::create_dir_all(&bin_dir).expect("mkdir node_modules/.bin");
        let unique = format!(
            "phantom-lsp-parent-{}",
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .map(|d| d.as_nanos())
                .unwrap_or(0)
        );
        let shim = bin_dir.join(&unique);
        std::fs::write(&shim, b"#!/bin/sh\nexit 0\n").expect("write shim");

        let resolved = resolve_lsp_binary_with_hint(&unique, Some(&nested))
            .expect("parent traversal must surface hoisted shim");
        assert_eq!(resolved, shim);
    }

    /// `resolve_lsp_binary_with_hint(_, None)` must behave exactly like
    /// `resolve_lsp_binary` — passing `None` does not enable any new
    /// fallback path (and so does not regress callers that have no
    /// project context).
    #[test]
    fn hint_none_does_not_enable_node_modules_fallback() {
        let tempdir = tempfile::tempdir().expect("tempdir");
        let bin_dir = tempdir.path().join("node_modules").join(".bin");
        std::fs::create_dir_all(&bin_dir).expect("mkdir node_modules/.bin");
        let unique = format!(
            "phantom-lsp-no-hint-{}",
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .map(|d| d.as_nanos())
                .unwrap_or(0)
        );
        std::fs::write(bin_dir.join(&unique), b"#!/bin/sh\nexit 0\n").expect("write shim");

        // No hint → resolver only consults PATH + standard fallback dirs.
        // Since `tempdir` is not on PATH and is not a fallback dir, the
        // binary remains undiscovered.
        assert!(resolve_lsp_binary_with_hint(&unique, None).is_none());
    }

    #[test]
    fn recipe_accepts_leading_dot_for_extensions() {
        let recipe = get_lsp_recipe(".tsx").expect("tsx recipe");
        assert_eq!(recipe.language, "typescript");
        assert_eq!(recipe.binary_name, "typescript-language-server");
    }
}