Skip to content

The agent that grows with you

fix(lsp): pre-write capture sees config-declared servers; e2e test goes through create_from_config

teknium1894ba961cf0a
diff --git a/agent/lsp/manager.py b/agent/lsp/manager.py
index fa0f46f65c8..fa41767072b 100644
--- a/agent/lsp/manager.py
+++ b/agent/lsp/manager.py
@@ -20,7 +20,7 @@ from typing import Any, Callable, Dict, List, Optional, Tuple
from agent.lsp import eventlog
from agent.lsp.client import DIAGNOSTICS_DOCUMENT_WAIT, LSPClient, _diagnostic_key as _diag_key
-from agent.lsp.servers import ServerContext, ServerDef, custom_servers, find_server_for_file, language_id_for
+from agent.lsp.servers import SERVERS, ServerContext, ServerDef, custom_servers, find_server_for_file, language_id_for
from agent.lsp.workspace import clear_cache, resolve_workspace_for_file
logger = logging.getLogger("agent.lsp.manager")
@@ -175,6 +175,10 @@ class LSPService:
extra = find_server_for_file(file_path, self._extra_servers) if self._extra_servers else None
return extra or find_server_for_file(file_path)
+ def handles_extension(self, ext: str) -> bool:
+ """True iff a config-declared or built-in server claims ``ext`` (pre-write capture decision)."""
+ return any(ext.lower() in s.extensions for s in (*self._extra_servers, *SERVERS))
+
# ---- public API ----
def is_active(self) -> bool:
diff --git a/tests/agent/lsp/test_custom_servers.py b/tests/agent/lsp/test_custom_servers.py
index ffdf453a823..895ceb89049 100644
--- a/tests/agent/lsp/test_custom_servers.py
+++ b/tests/agent/lsp/test_custom_servers.py
@@ -1,9 +1,11 @@
"""Config-declared LSP servers (``lsp.servers.<id>`` with ``extensions``) route files and spawn like built-ins."""
from __future__ import annotations
+import json
import os
import subprocess
import sys
+from unittest.mock import patch
import pytest
@@ -31,21 +33,38 @@ def test_custom_server_precedes_builtins_and_carries_language_id():
@pytest.mark.timeout(60)
-def test_service_gets_diagnostics_from_config_declared_server(tmp_path):
- subprocess.run(["git", "init", "-q", str(tmp_path)], check=True)
- target = tmp_path / "doc.pnch"
- target.write_text("hello\n")
- (tmp_path / "x.rs").write_text("fn main() {}\n")
- svc = LSPService(
- enabled=True, wait_mode="document", wait_timeout=5.0, install_strategy="manual",
- env_overrides={"panache": {"MOCK_LSP_SCRIPT": "errors"}}, idle_timeout=0,
- extra_servers=custom_servers({"panache": {"command": [sys.executable, _MOCK], "extensions": [".pnch"]}}),
- )
+def test_service_gets_diagnostics_from_config_declared_server(tmp_path, monkeypatch):
+ """End to end from config.yaml: the only production path from ``lsp.servers`` to a running server."""
+ from tools.environments.local import LocalEnvironment
+ from tools.file_operations import ShellFileOperations
+
+ home = tmp_path / "home"
+ home.mkdir()
+ (home / "config.yaml").write_text(json.dumps({"lsp": {
+ "install_strategy": "manual", "wait_timeout": 5.0, "idle_timeout": 0,
+ "servers": {"panache": {"command": [sys.executable, _MOCK], "extensions": [".pnch"],
+ "env": {"MOCK_LSP_SCRIPT": "errors"}}},
+ }}), encoding="utf-8")
+ monkeypatch.setenv("HERMES_HOME", str(home))
+ ws = tmp_path / "ws"
+ ws.mkdir()
+ subprocess.run(["git", "init", "-q", str(ws)], check=True)
+ target = ws / "doc.pnch"
+ target.write_text("hello\n", encoding="utf-8")
+ (ws / "x.rs").write_text("fn main() {}\n", encoding="utf-8")
+ svc = LSPService.create_from_config()
+ assert svc is not None
try:
assert svc.enabled_for(str(target))
diags = svc.get_diagnostics_sync(str(target))
assert diags and diags[0]["source"] == "mock-lsp"
# Control: a built-in extension still routes to the registry entry, not the custom server.
- assert svc._server_for(str(tmp_path / "x.rs")).server_id == "rust-analyzer"
+ assert svc._server_for(str(ws / "x.rs")).server_id == "rust-analyzer"
+ # The write path captures pre-write content for the custom extension only via the service.
+ fops = ShellFileOperations(LocalEnvironment())
+ with patch.object(fops, "_lsp_service", return_value=svc):
+ assert fops._lsp_handles_extension(".pnch")
+ with patch.object(fops, "_lsp_service", return_value=None):
+ assert not fops._lsp_handles_extension(".pnch") and fops._lsp_handles_extension(".rs")
finally:
svc.shutdown()
diff --git a/tools/file_operations_lint.py b/tools/file_operations_lint.py
index edc4e4e1f17..48bad2b8c19 100644
--- a/tools/file_operations_lint.py
+++ b/tools/file_operations_lint.py
@@ -216,12 +216,15 @@ class LintMixin:
return None
def _lsp_handles_extension(self, ext: str) -> bool:
- """True iff some registered LSP server claims ``ext`` (static registry
- only; safe on remote backends). Decides whether pre-write content is
- worth capturing for the line-shift map."""
+ """True iff the active service (config-declared servers included) or, without one,
+ the static registry claims ``ext``. Decides whether pre-write content is worth
+ capturing for the line-shift map."""
if not ext:
return False
try:
+ svc = self._lsp_service()
+ if svc is not None:
+ return svc.handles_extension(ext)
from agent.lsp.servers import SERVERS
except Exception: # noqa: BLE001
return False