The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
feat(lsp): lsp.package_manager picks npm, pnpm or yarn for language-server installs
teknium1c484be01239d
diff --git a/agent/lsp/install.py b/agent/lsp/install.pyindex 8fb429837df..ecf069acf33 100644--- a/agent/lsp/install.py+++ b/agent/lsp/install.py@@ -198,26 +198,53 @@ def _link_into_bin(target: Path) -> str:return str(link if link.exists() else target)+# Node package manager → argv that installs into ``<staging>/node_modules`` (``lsp.package_manager``).+# Every manager keeps the staging-dir semantics: nothing touches the user's project or global tree.+_NODE_PM_ARGV: Dict[str, Callable[[str], list]] = {+ "npm": lambda staging: ["install", "--prefix", staging, "--silent", "--no-fund", "--no-audit"],+ "pnpm": lambda staging: ["add", "--dir", staging],+ "yarn": lambda staging: ["add", "--cwd", staging],+}+++def _node_package_manager() -> str:+ """``lsp.package_manager`` from config (npm default); unknown values warn once and fall back to npm."""+ try:+ from hermes_cli.config import load_config_readonly+ lsp_cfg = load_config_readonly().get("lsp") or {}+ except Exception: # noqa: BLE001 — installer must not die on a broken config; npm is the historical default+ return "npm"+ pm = str(lsp_cfg.get("package_manager") or "npm").strip().lower() if isinstance(lsp_cfg, dict) else "npm"+ if pm not in _NODE_PM_ARGV:+ logger.warning("[install] lsp.package_manager=%r is not one of %s; using npm", pm, sorted(_NODE_PM_ARGV))+ return "npm"+ return pm++def _install_npm(pkg: str, bin_name: str, extra_pkgs: Optional[list] = None) -> Optional[str]:- """``npm install --prefix <staging>`` then link ``node_modules/.bin/<bin_name>`` into ``lsp/bin/``."""- # Managed npm first: $HERMES_HOME/node isn't on an arbitrary process's+ """Install with the configured Node package manager into ``<staging>`` and link+ ``node_modules/.bin/<bin_name>`` into ``lsp/bin/``."""+ pm = _node_package_manager()+ # Managed Node first: $HERMES_HOME/node isn't on an arbitrary process's# PATH, so a bare which() would miss the Node that Hermes installed.- npm = find_node_executable("npm")- if npm is None:- logger.info("[install] cannot install %s: no usable npm found", pkg)+ pm_bin = find_node_executable(pm)+ if pm_bin is None:+ # Deliberately no silent fallback to npm: a pnpm/yarn choice is usually a supply-chain policy.+ logger.warning("[install] cannot install %s: lsp.package_manager is %r but no usable %s was found "+ "(install it, or set lsp.package_manager: npm)", pkg, pm, pm)return Nonestaging = hermes_lsp_bin_dir().parent # <HERMES_HOME>/lsp/install_targets = [pkg] + list(extra_pkgs or [])- logger.info("[install] npm install --prefix %s %s", staging, " ".join(install_targets))- cmd = [npm, "install", "--prefix", str(staging), "--silent", "--no-fund", "--no-audit", *install_targets]- if not _run_installer("npm", pkg, cmd, timeout=300):+ cmd = [pm_bin, *_NODE_PM_ARGV[pm](str(staging)), *install_targets]+ logger.info("[install] %s %s", pm, " ".join(cmd[1:]))+ if not _run_installer(pm, pkg, cmd, timeout=300):return Nonefound = _first_existing(staging / "node_modules" / ".bin" / bin_name)if found is not None:# npm's Windows wrappers resolve their payload via ``%~dp0\..\<pkg>``, so a copy or symlink# in ``lsp/bin/`` points at nothing; use them where npm put them (``_existing_binary`` probes there).return str(found) if _is_windows() and found.suffix.lower() in (".cmd", ".bat") else _link_into_bin(found)- logger.warning("[install] npm install for %s succeeded but bin %s not found", pkg, bin_name)+ logger.warning("[install] %s install for %s succeeded but bin %s not found", pm, pkg, bin_name)return Nonediff --git a/hermes_cli/config_defaults.py b/hermes_cli/config_defaults.pyindex 0e191cb6841..87ab39428b3 100644--- a/hermes_cli/config_defaults.py+++ b/hermes_cli/config_defaults.py@@ -2250,6 +2250,10 @@ DEFAULT_CONFIG = {# Missing server binaries: auto = install via npm/go/pip into <HERMES_HOME>/lsp/bin/ on# first use; manual = only binaries on PATH; off = alias for manual."install_strategy": "auto",+ # Node package manager for the npm-recipe servers: npm | pnpm | yarn. Installs still land in+ # <HERMES_HOME>/lsp/node_modules; a configured manager that is not installed skips the install+ # (no silent fallback to npm) so a pnpm/yarn supply-chain policy is never bypassed.+ "package_manager": "npm",# Idle seconds before a server is shut down (respawned on demand), so long- running# processes don't accumulate stale children (hundreds of MB + pipe FDs each) across# worktrees. 0 = keep servers for process lifetime.diff --git a/tests/agent/lsp/test_install_and_lint_fixes.py b/tests/agent/lsp/test_install_and_lint_fixes.pyindex b614cee50f3..e082b9b70e7 100644--- a/tests/agent/lsp/test_install_and_lint_fixes.py+++ b/tests/agent/lsp/test_install_and_lint_fixes.py@@ -186,3 +186,32 @@ def test_check_lint_returns_error_for_real_ts_type_errors(tmp_path):if __name__ == "__main__": # pragma: no coverpytest.main([__file__, "-v"])+++def test_lsp_package_manager_config_selects_installer_argv_and_never_falls_back_silently(tmp_path, monkeypatch):+ """``lsp.package_manager`` picks the Node installer (staging-dir semantics kept); a configured manager+ that is missing skips the install instead of quietly using npm; unknown values fall back to npm."""+ from unittest.mock import MagicMock++ from agent.lsp import install as install_mod++ monkeypatch.setenv("HERMES_HOME", str(tmp_path))+ staging = str(install_mod.hermes_lsp_bin_dir().parent)+ cfg = {"lsp": {}}+ monkeypatch.setattr("hermes_cli.config.load_config_readonly", lambda: cfg)+ runs = []+ monkeypatch.setattr(install_mod.subprocess, "run", lambda cmd, **kw: (runs.append(cmd), MagicMock(returncode=0, stderr=""))[1])+ present = {"npm": "/usr/bin/npm", "pnpm": "/usr/bin/pnpm"}+ monkeypatch.setattr(install_mod, "find_node_executable", lambda name: present.get(name))++ cfg["lsp"] = {"package_manager": "pnpm"}+ install_mod._install_npm("pyright", "pyright-langserver")+ assert runs[-1] == ["/usr/bin/pnpm", "add", "--dir", staging, "pyright"]++ cfg["lsp"] = {"package_manager": "bogus"} # unknown → npm, unchanged historical argv+ install_mod._install_npm("pyright", "pyright-langserver")+ assert runs[-1] == ["/usr/bin/npm", "install", "--prefix", staging, "--silent", "--no-fund", "--no-audit", "pyright"]++ cfg["lsp"] = {"package_manager": "yarn"} # configured but absent → no install, no npm run+ assert install_mod._install_npm("pyright", "pyright-langserver") is None+ assert len(runs) == 2diff --git a/website/docs/user-guide/features/lsp.md b/website/docs/user-guide/features/lsp.mdindex db3069b885b..ded3125b6cd 100644--- a/website/docs/user-guide/features/lsp.md+++ b/website/docs/user-guide/features/lsp.md@@ -193,6 +193,13 @@ lsp:# manual — only use binaries already on PATHinstall_strategy: auto+ # Node package manager for the npm-based servers: npm (default), pnpm+ # or yarn. Installs still land in <HERMES_HOME>/lsp/node_modules; a+ # manager that is configured but not installed skips the install with+ # a warning instead of silently using npm, so a pnpm/yarn supply-chain+ # policy (minimumReleaseAge, allowBuilds, …) is never bypassed.+ package_manager: npm+# How long an unused language-server client stays alive (seconds).# Idle servers are shut down automatically and respawned on the next# relevant file operation. Set to 0 to disable idle reaping and keep