Skip to content

The agent that grows with you

fix: scrub credentials at the llama-server spawn only, not the generic probe spawner (review follow-up)

teknium1c6c595508d6f
diff --git a/hermes_cli/local_runtime/processes.py b/hermes_cli/local_runtime/processes.py
index 13cee8683b7..4e3196e22c7 100644
--- a/hermes_cli/local_runtime/processes.py
+++ b/hermes_cli/local_runtime/processes.py
@@ -5,7 +5,6 @@ from __future__ import annotations
from collections.abc import Mapping
import ctypes
from ctypes import wintypes
-import os
import subprocess
import sys
import threading
@@ -98,7 +97,9 @@ def server_child_env(base_env: Mapping[str, str]) -> dict[str, str]:
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.
+ Everything else (PATH, CUDA_*, HSA_*, OMP_*, TEMP, …) passes through untouched. Applied by
+ the llama-server supervisor only: spawn_server is also the generic bounded-probe spawner
+ (git / PowerShell / update probes), whose children legitimately need GH_TOKEN, HF_TOKEN, …
"""
return {
key: value for key, value in base_env.items()
@@ -112,9 +113,7 @@ def spawn_server(cmd, **kwargs) -> tuple[subprocess.Popen, _WindowsJob | None]:
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's
ordinary 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), None
job = _WindowsJob()
diff --git a/hermes_cli/local_runtime/supervisor.py b/hermes_cli/local_runtime/supervisor.py
index 0cc45ce512d..1cb90f7742e 100644
--- a/hermes_cli/local_runtime/supervisor.py
+++ b/hermes_cli/local_runtime/supervisor.py
@@ -13,6 +13,7 @@ from contextlib import suppress
from functools import lru_cache
import json
import logging
+import os
import secrets
import socket
import subprocess
@@ -24,7 +25,7 @@ import urllib.request
from pathlib import Path
from hermes_cli.local_runtime.binaries import server_binary, runtimes_root
-from hermes_cli.local_runtime.processes import spawn_server
+from hermes_cli.local_runtime.processes import server_child_env, spawn_server
logger = logging.getLogger(__name__)
@@ -197,8 +198,8 @@ class LlamaServerSupervisor:
self._log_handle.write(f"\n# spawn: {cmd}\n")
self._log_handle.flush()
# list-args, never a shell: spaced paths (user homes) must survive.
- self.proc, self._job = spawn_server(cmd, stdout=self._log_handle,
- stderr=subprocess.STDOUT, cwd=str(exe.parent))
+ self.proc, self._job = spawn_server(cmd, stdout=self._log_handle, stderr=subprocess.STDOUT,
+ cwd=str(exe.parent), env=server_child_env(os.environ))
logger.info("llama-server router spawned pid=%s port=%s", self.proc.pid, self.port)
# State goes down at SPAWN, not after health: endpoint resolution treats a
# live-pid-but-not-yet-healthy server as "starting" rather than "unconfigured", so a
diff --git a/tests/hermes_cli/test_local_runtime_child_env.py b/tests/hermes_cli/test_local_runtime_child_env.py
index dd83648a415..3e52488fc33 100644
--- a/tests/hermes_cli/test_local_runtime_child_env.py
+++ b/tests/hermes_cli/test_local_runtime_child_env.py
@@ -3,34 +3,39 @@
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.
+inference child regardless of the crash, so the supervisor scrubs them at its spawn. The scrub
+is the supervisor's, not spawn_server's: the same spawner backs bounded_probe_run (git,
+PowerShell, update probes) whose children legitimately need GH_TOKEN / HF_TOKEN.
"""
-import json
import os
import subprocess
-import sys
+from types import SimpleNamespace
+from hermes_cli.local_runtime import supervisor
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):
+def test_supervisor_spawns_llama_server_without_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)
+ monkeypatch.setattr(supervisor, "runtimes_root", lambda: tmp_path / "runtime")
+ monkeypatch.setattr(supervisor, "server_binary", lambda _: tmp_path / "engine" / "llama-server")
+ monkeypatch.setattr(supervisor, "_direct_io_args", lambda exe: [])
+ spawns = []
+ monkeypatch.setattr(supervisor, "spawn_server", lambda argv, **kwargs: (
+ spawns.append(kwargs) or SimpleNamespace(pid=123, poll=lambda: 0), None,
+ ))
+ sup = supervisor.LlamaServerSupervisor(tmp_path / "engine", tmp_path / "models", port=19002)
+ monkeypatch.setattr(sup, "_write_state", lambda: None)
try:
- assert proc.wait(timeout=60) == 0
+ sup._spawn()
finally:
- if job is not None:
- job.close()
- child_env = json.loads(out.read_text(encoding="utf-8"))
+ sup.stop()
+ child_env = spawns[0]["env"]
assert "FIRECRAWL_API_KEY" not in child_env
assert "SOME_TOKEN" not in child_env
assert "DB_PASSWORD" not in child_env
@@ -39,6 +44,14 @@ def test_spawn_server_child_sees_no_credentials_but_keeps_runtime_env(tmp_path,
assert child_env["PATH"] == os.environ["PATH"]
+def test_spawn_server_keeps_the_callers_environment(monkeypatch):
+ """The generic spawner (bounded_probe_run's git/PowerShell probes) must pass an explicit
+ ``env=`` through untouched: a gh-backed credential helper needs GITHUB_TOKEN."""
+ monkeypatch.setattr(subprocess, "Popen", lambda cmd, **kwargs: SimpleNamespace(kwargs=kwargs))
+ proc, _job = spawn_server(["git", "fetch"], env={"GITHUB_TOKEN": "g", "PATH": "/bin"})
+ assert proc.kwargs["env"] == {"GITHUB_TOKEN": "g", "PATH": "/bin"}
+
+
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"}