The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(local-models): llama-server never inherits credential-shaped environment variables
teknium163c16221fad6
diff --git a/hermes_cli/local_runtime/processes.py b/hermes_cli/local_runtime/processes.pyindex d2a3168cd0e..13cee8683b7 100644--- a/hermes_cli/local_runtime/processes.py+++ b/hermes_cli/local_runtime/processes.py@@ -2,8 +2,10 @@from __future__ import annotations+from collections.abc import Mappingimport ctypesfrom ctypes import wintypes+import osimport subprocessimport sysimport threading@@ -86,13 +88,33 @@ class _WindowsJob:self._handle = None+_CREDENTIAL_ENV_MARKERS = ("_API_KEY", "_TOKEN", "_SECRET", "PASSWORD", "_CREDENTIALS")+++def server_child_env(base_env: Mapping[str, str]) -> dict[str, str]:+ """Return the environment a native inference child (llama-server) gets.++ Provider and tool credentials never belong in a native child that talks to nobody but+ us — and on Windows they are not merely leaked: the bundled OpenMP runtime died with+ STATUS_HEAP_CORRUPTION during initialisation with one `*_API_KEY` present and loaded fine+ with only that variable removed (#116109, confirmed on a model-free libomp.dll probe).+ Everything else (PATH, CUDA_*, HSA_*, OMP_*, TEMP, …) passes through untouched.+ """+ return {+ key: value for key, value in base_env.items()+ if not any(marker in key.upper() for marker in _CREDENTIAL_ENV_MARKERS)+ }++def spawn_server(cmd, **kwargs) -> tuple[subprocess.Popen, _WindowsJob | None]:"""Start a router, returning its process and an owner-held containment handle.Keep the job until shutdown and call close() to terminate the entire tree.Windows closes it automatically if the owner dies. Other hosts retain Popen'sordinary behavior. Assignment happens before the child's first instruction.+ The child never inherits credential-shaped variables (see server_child_env)."""+ kwargs["env"] = server_child_env(kwargs.get("env") or os.environ)if sys.platform != "win32":return subprocess.Popen(cmd, **kwargs), Nonejob = _WindowsJob()diff --git a/tests/hermes_cli/test_local_runtime_child_env.py b/tests/hermes_cli/test_local_runtime_child_env.pynew file mode 100644index 00000000000..dd83648a415--- /dev/null+++ b/tests/hermes_cli/test_local_runtime_child_env.py@@ -0,0 +1,45 @@+"""A managed llama-server child never inherits credential-shaped environment variables.++Issue #116109: on Windows the bundled libomp.dll crashed with STATUS_HEAP_CORRUPTION during+initialisation whenever one `*_API_KEY` variable was present in the inherited Desktop environment+and loaded fine with only that variable removed. Credentials have no business in a native+inference child regardless of the crash, so spawn_server scrubs them at the one spawn boundary.+"""++import json+import os+import subprocess+import sys++from hermes_cli.local_runtime.processes import server_child_env, spawn_server+++def test_spawn_server_child_sees_no_credentials_but_keeps_runtime_env(tmp_path, monkeypatch):+ monkeypatch.setenv("FIRECRAWL_API_KEY", "fc-secret")+ monkeypatch.setenv("SOME_TOKEN", "t")+ monkeypatch.setenv("DB_PASSWORD", "p")+ monkeypatch.setenv("CUDA_VISIBLE_DEVICES", "0")+ monkeypatch.setenv("OMP_NUM_THREADS", "4")+ out = tmp_path / "env.json"+ proc, job = spawn_server(+ [sys.executable, "-c",+ "import json, os, sys; json.dump(dict(os.environ), open(sys.argv[1], 'w'))", str(out)],+ stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)+ try:+ assert proc.wait(timeout=60) == 0+ finally:+ if job is not None:+ job.close()+ child_env = json.loads(out.read_text(encoding="utf-8"))+ assert "FIRECRAWL_API_KEY" not in child_env+ assert "SOME_TOKEN" not in child_env+ assert "DB_PASSWORD" not in child_env+ assert child_env["CUDA_VISIBLE_DEVICES"] == "0"+ assert child_env["OMP_NUM_THREADS"] == "4"+ assert child_env["PATH"] == os.environ["PATH"]+++def test_server_child_env_is_case_insensitive_and_pure():+ base = {"openai_api_key": "k", "Path": "C:\\x", "GITHUB_TOKEN": "g", "HSA_OVERRIDE_GFX_VERSION": "11"}+ assert server_child_env(base) == {"Path": "C:\\x", "HSA_OVERRIDE_GFX_VERSION": "11"}+ assert base["openai_api_key"] == "k" # input untouched