The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): preserve ld library path in systemd units
LeonSGP43efd7bb55fd94
diff --git a/hermes_cli/gateway.py b/hermes_cli/gateway.pyindex f5abc7d4032..f668f47758b 100644--- a/hermes_cli/gateway.py+++ b/hermes_cli/gateway.py@@ -2905,6 +2905,21 @@ def _remap_path_for_user(path: str, target_home_dir: str) -> str:return str(p)+def _systemd_env_line(name: str, value: str) -> str:+ """One ``Environment="NAME=value"`` line; systemd's quoting needs ``\\`` and ``"`` escaped."""+ escaped = value.replace("\\", "\\\\").replace('"', '\\"')+ return f'Environment="{name}={escaped}"\n'+++def _ld_library_path_line(target_home_dir: str | None = None) -> str:+ """Carry the installer's LD_LIBRARY_PATH into the unit (glibc reads it only at process start, so+ ~/.hermes/.env is too late for CUDA libs — #14613); system units remap caller-home components."""+ components = [p for p in os.environ.get("LD_LIBRARY_PATH", "").split(":") if p]+ if target_home_dir is not None:+ components = [_remap_path_for_user(p, target_home_dir) for p in components]+ return _systemd_env_line("LD_LIBRARY_PATH", ":".join(components)) if components else ""++def _hermes_home_for_target_user(target_home_dir: str) -> str:"""Remap the current HERMES_HOME (root's, under sudo) to the target user's equivalent:``/root/.hermes[/profiles/x]`` → ``/home/alice/.hermes[/profiles/x]``; custom paths kept as-is."""@@ -3053,13 +3068,15 @@ def generate_systemd_unit(system: bool = False, run_as_user: str | None = None)f'Environment="HOME={home_dir}"\n'f'Environment="USER={username}"\n'f'Environment="LOGNAME={username}"\n'+ f"{_ld_library_path_line(home_dir)}")wanted_by = "multi-user.target"else:hermes_home = str(get_hermes_home().resolve())profile_arg = _profile_arg(hermes_home)user_home = Path.home()- identity_lines = env_lines = ordering_lines = ""+ identity_lines = ordering_lines = ""+ env_lines = _ld_library_path_line()wanted_by = "default.target"watchdog_seconds = _systemd_watchdog_seconds(hermes_home)diff --git a/tests/hermes_cli/test_gateway_service.py b/tests/hermes_cli/test_gateway_service.pyindex 319c3789b7f..084a2a2b848 100644--- a/tests/hermes_cli/test_gateway_service.py+++ b/tests/hermes_cli/test_gateway_service.py@@ -332,6 +332,19 @@ class TestGeneratedSystemdUnits:assert f"RestartForceExitStatus={GATEWAY_SERVICE_RESTART_EXIT_CODE}" in unitassert f"RestartPreventExitStatus={GATEWAY_FATAL_CONFIG_EXIT_CODE}" in unit+ def test_user_unit_carries_ld_library_path_escaped_for_systemd(self, monkeypatch):+ """#14613: glibc reads LD_LIBRARY_PATH only at process start, so the unit file is the+ only place it can reach CUDA-backed tools; quotes/backslashes must survive systemd quoting."""+ monkeypatch.setenv("LD_LIBRARY_PATH", '/opt/cu"da/lib64:/opt/back\\slash/lib')++ unit = gateway_cli.generate_systemd_unit(system=False)+ assert 'Environment="LD_LIBRARY_PATH=/opt/cu\\"da/lib64:/opt/back\\\\slash/lib"' in unit++ monkeypatch.setenv("LD_LIBRARY_PATH", "")+ assert "LD_LIBRARY_PATH" not in gateway_cli.generate_systemd_unit(system=False)+ monkeypatch.delenv("LD_LIBRARY_PATH")+ assert "LD_LIBRARY_PATH" not in gateway_cli.generate_systemd_unit(system=False)+def test_unit_stop_budget_beats_drain_only_formula_with_real_loaders(self, monkeypatch):@@ -1426,6 +1439,22 @@ class TestSystemUnitHermesHome:assert "[email protected]" in unit_sectionassert "user@" not in user_unit+ def test_system_unit_remaps_caller_home_ld_library_path_components(self, monkeypatch):+ """#14613: under sudo the caller's /root/... library dirs are unreadable to the target+ user, so each colon-separated component is remapped like the PATH entries are."""+ monkeypatch.setattr(Path, "home", staticmethod(lambda: Path("/root")))+ monkeypatch.delenv("HERMES_HOME", raising=False)+ monkeypatch.setattr(+ gateway_cli, "_system_service_identity",+ lambda run_as_user=None: ("alice", "alice", "/home/alice", 1001),+ )+ monkeypatch.setattr(gateway_cli, "_build_service_path_dirs", lambda: [])+ monkeypatch.setenv("LD_LIBRARY_PATH", "/root/cuda/lib:/opt/cuda/lib64")++ unit = gateway_cli.generate_systemd_unit(system=True, run_as_user="alice")++ assert 'Environment="LD_LIBRARY_PATH=/home/alice/cuda/lib:/opt/cuda/lib64"' in unit+def test_system_unit_uses_target_user_home_not_calling_user(self, monkeypatch):# Simulate sudo: Path.home() returns /root, target user is alicemonkeypatch.setattr(Path, "home", staticmethod(lambda: Path("/root")))