The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): the installed unit is the fallback source for LD_LIBRARY_PATH, so a refresh cannot drop it
kshitijk4poor5cba54d4f531
diff --git a/hermes_cli/gateway.py b/hermes_cli/gateway.pyindex f668f47758b..c60cc878dc3 100644--- a/hermes_cli/gateway.py+++ b/hermes_cli/gateway.py@@ -2906,15 +2906,35 @@ def _remap_path_for_user(path: str, target_home_dir: str) -> str:def _systemd_env_line(name: str, value: str) -> str:- """One ``Environment="NAME=value"`` line; systemd's quoting needs ``\\`` and ``"`` escaped."""- escaped = value.replace("\\", "\\\\").replace('"', '\\"')+ """One ``Environment="NAME=value"`` line: ``\\`` and ``"`` escaped for systemd's quoting, ``%``+ doubled so specifier expansion leaves the value alone."""+ escaped = value.replace("\\", "\\\\").replace('"', '\\"').replace("%", "%%")return f'Environment="{name}={escaped}"\n'-def _ld_library_path_line(target_home_dir: str | None = None) -> str:+def _installed_unit_ld_library_path(system: bool) -> str:+ """``LD_LIBRARY_PATH`` baked into the installed unit, unescaped; ``""`` when absent."""+ import re+ unit_path = get_systemd_unit_path(system=system)+ try:+ text = unit_path.read_text(encoding="utf-8")+ except OSError:+ return ""+ match = re.search(r'^Environment="LD_LIBRARY_PATH=((?:[^"\\]|\\.)*)"$', text, flags=re.M)+ if not match:+ return ""+ return match.group(1).replace("%%", "%").replace('\\"', '"').replace("\\\\", "\\")+++def _ld_library_path_line(system: bool, 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]+ ~/.hermes/.env is too late for CUDA libs — #14613); system units remap caller-home components.++ The installed unit is the fallback source: the unit is regenerated and compared on every+ start/restart/status, and a shell without the export (ssh, cron, ``sudo`` strips ``LD_*``)+ must not be able to "repair" the line away."""+ raw = os.environ.get("LD_LIBRARY_PATH", "") or _installed_unit_ld_library_path(system)+ components = [p for p in raw.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 ""@@ -3068,7 +3088,7 @@ 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)}"+ f"{_ld_library_path_line(system=True, target_home_dir=home_dir)}")wanted_by = "multi-user.target"else:@@ -3076,7 +3096,7 @@ def generate_systemd_unit(system: bool = False, run_as_user: str | None = None)profile_arg = _profile_arg(hermes_home)user_home = Path.home()identity_lines = ordering_lines = ""- env_lines = _ld_library_path_line()+ env_lines = _ld_library_path_line(system=False)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 084a2a2b848..3a36050e4c3 100644--- a/tests/hermes_cli/test_gateway_service.py+++ b/tests/hermes_cli/test_gateway_service.py@@ -1439,6 +1439,20 @@ class TestSystemUnitHermesHome:assert "[email protected]" in unit_sectionassert "user@" not in user_unit+ def test_installed_unit_keeps_ld_library_path_when_the_shell_lacks_it(self, monkeypatch, tmp_path):+ """The unit is regenerated and compared on every start/restart/status; a later shell without+ the export (ssh, cron, sudo) must see the installed unit as current, not "repair" the line away."""+ unit_path = tmp_path / "hermes-gateway.service"+ monkeypatch.setattr(gateway_cli, "get_systemd_unit_path", lambda system=False: unit_path)+ monkeypatch.setenv("LD_LIBRARY_PATH", "/opt/cuda/lib64:/opt/pct%dir/lib")+ unit_path.write_text(gateway_cli.generate_systemd_unit(system=False), encoding="utf-8")+ assert 'Environment="LD_LIBRARY_PATH=/opt/cuda/lib64:/opt/pct%%dir/lib"' in unit_path.read_text()++ monkeypatch.delenv("LD_LIBRARY_PATH")++ assert gateway_cli.systemd_unit_is_current(system=False)+ assert 'LD_LIBRARY_PATH=/opt/cuda/lib64:/opt/pct%%dir/lib' in gateway_cli.generate_systemd_unit(system=False)+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 targetuser, so each colon-separated component is remapped like the PATH entries are."""