mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-11-21 04:09:45 +03:00
refactor(environments-terminal): drop test-only aliases from product code
- ProcessRegistry._log_delta_command (staticmethod alias of log_delta_command): its only production use was the one internal call, which now calls log_delta_command. - _load_hermes_env_vars in environments/ssh.py and docker.py: module rebinding kept only for tests to patch; tests now patch load_hermes_env_vars on the same module. - env_probe.get_environment_probe_line(force_refresh=...): documented as test-only; no caller passes it (gateway/run.py, agent/system_prompt.py) and tests already reset via _reset_cache_for_tests(). - managed_modal: touch_activity_if_due is imported at module top. The lazy import and the try/except existed only for a test stub of tools.environments.base; touch_activity_if_due already swallows callback errors itself. The stub now provides the name. None of these names is in plugin_api.json or the frozen updater surface.
This commit is contained in:
committed by
Siddharth Balyan
Unverified
parent
0cbf579e63
commit
d90dc4daa6
@@ -128,7 +128,7 @@ def test_init_env_args_uses_hermes_dotenv_for_allowlisted_env(monkeypatch):
|
||||
env = _make_execute_only_env(["DATABASE_URL"])
|
||||
|
||||
monkeypatch.delenv("DATABASE_URL", raising=False)
|
||||
monkeypatch.setattr(docker_env, "_load_hermes_env_vars", lambda: {"DATABASE_URL": "value_from_dotenv"})
|
||||
monkeypatch.setattr(docker_env, "load_hermes_env_vars", lambda: {"DATABASE_URL": "value_from_dotenv"})
|
||||
|
||||
args = env._build_init_env_args()
|
||||
|
||||
@@ -144,7 +144,7 @@ def test_init_env_args_prefers_shell_env_over_hermes_dotenv(monkeypatch):
|
||||
env = _make_execute_only_env(["DATABASE_URL"])
|
||||
|
||||
monkeypatch.setenv("DATABASE_URL", "value_from_shell")
|
||||
monkeypatch.setattr(docker_env, "_load_hermes_env_vars", lambda: {"DATABASE_URL": "value_from_dotenv"})
|
||||
monkeypatch.setattr(docker_env, "load_hermes_env_vars", lambda: {"DATABASE_URL": "value_from_dotenv"})
|
||||
|
||||
args = env._build_init_env_args()
|
||||
|
||||
@@ -163,7 +163,7 @@ def test_init_env_args_uses_hermes_dotenv_for_empty_shell_env(monkeypatch):
|
||||
env = _make_execute_only_env(["MY_SECRET"])
|
||||
|
||||
monkeypatch.setenv("MY_SECRET", "")
|
||||
monkeypatch.setattr(docker_env, "_load_hermes_env_vars", lambda: {"MY_SECRET": "value_from_dotenv"})
|
||||
monkeypatch.setattr(docker_env, "load_hermes_env_vars", lambda: {"MY_SECRET": "value_from_dotenv"})
|
||||
|
||||
args = env._build_init_env_args()
|
||||
|
||||
@@ -179,7 +179,7 @@ def test_init_env_args_uses_active_profile_for_forwarded_env(monkeypatch):
|
||||
|
||||
env = _make_execute_only_env(forward_env=["SERVICE_TOKEN"])
|
||||
monkeypatch.setenv("SERVICE_TOKEN", "token-for-default")
|
||||
monkeypatch.setattr(docker_env, "_load_hermes_env_vars", dict)
|
||||
monkeypatch.setattr(docker_env, "load_hermes_env_vars", dict)
|
||||
ss.set_multiplex_active(True)
|
||||
token = ss.set_secret_scope({"SERVICE_TOKEN": "token-for-routed-profile"})
|
||||
try:
|
||||
@@ -199,7 +199,7 @@ def test_init_env_args_omits_missing_scoped_forwarded_env(monkeypatch):
|
||||
|
||||
env = _make_execute_only_env(forward_env=["SERVICE_TOKEN"])
|
||||
monkeypatch.setenv("SERVICE_TOKEN", "token-for-default")
|
||||
monkeypatch.setattr(docker_env, "_load_hermes_env_vars", dict)
|
||||
monkeypatch.setattr(docker_env, "load_hermes_env_vars", dict)
|
||||
ss.set_multiplex_active(True)
|
||||
token = ss.set_secret_scope({})
|
||||
try:
|
||||
@@ -219,7 +219,7 @@ def test_runtime_exec_tracks_scope_and_clears_missing_value(monkeypatch):
|
||||
|
||||
env = _make_execute_only_env(forward_env=["SERVICE_TOKEN"])
|
||||
monkeypatch.setenv("SERVICE_TOKEN", "token-for-default")
|
||||
monkeypatch.setattr(docker_env, "_load_hermes_env_vars", dict)
|
||||
monkeypatch.setattr(docker_env, "load_hermes_env_vars", dict)
|
||||
calls = []
|
||||
monkeypatch.setattr(
|
||||
docker_env,
|
||||
@@ -268,7 +268,7 @@ def test_wrapped_exec_scopes_explicit_forward_env_across_profiles(monkeypatch, t
|
||||
encoding="utf-8",
|
||||
)
|
||||
monkeypatch.setenv("EXPLICIT_TOKEN", "token-for-default")
|
||||
monkeypatch.setattr(docker_env, "_load_hermes_env_vars", dict)
|
||||
monkeypatch.setattr(docker_env, "load_hermes_env_vars", dict)
|
||||
|
||||
def _run_fake_docker_exec(cmd, stdin_data=None, **kwargs):
|
||||
"""Execute the generated docker exec command in a real local bash."""
|
||||
@@ -378,7 +378,7 @@ def test_forward_env_overrides_docker_env_in_init_args(monkeypatch):
|
||||
env._env = {"MY_KEY": "static_value"}
|
||||
|
||||
monkeypatch.setenv("MY_KEY", "dynamic_value")
|
||||
monkeypatch.setattr(docker_env, "_load_hermes_env_vars", dict)
|
||||
monkeypatch.setattr(docker_env, "load_hermes_env_vars", dict)
|
||||
|
||||
args = env._build_init_env_args()
|
||||
|
||||
@@ -1845,7 +1845,7 @@ def test_forwarded_secret_values_never_in_argv(monkeypatch):
|
||||
secret = "s3cr3t-gitlab-token-value"
|
||||
env = _make_execute_only_env(["GITLAB_TOKEN"])
|
||||
monkeypatch.setenv("GITLAB_TOKEN", secret)
|
||||
monkeypatch.setattr(docker_env, "_load_hermes_env_vars", dict)
|
||||
monkeypatch.setattr(docker_env, "load_hermes_env_vars", dict)
|
||||
|
||||
# init path
|
||||
init_args = env._build_init_env_args()
|
||||
|
||||
@@ -78,7 +78,8 @@ def _install_fake_tools_package(*, credential_mounts=None):
|
||||
def _prepare_command(self, command: str):
|
||||
return command, None
|
||||
|
||||
sys.modules["tools.environments.base"] = types.SimpleNamespace(BaseEnvironment=_DummyBaseEnvironment)
|
||||
sys.modules["tools.environments.base"] = types.SimpleNamespace(
|
||||
BaseEnvironment=_DummyBaseEnvironment, touch_activity_if_due=lambda _state, _label: None)
|
||||
sys.modules["tools.managed_tool_gateway"] = types.SimpleNamespace(
|
||||
resolve_managed_tool_gateway=lambda vendor: types.SimpleNamespace(
|
||||
vendor=vendor,
|
||||
|
||||
@@ -10,7 +10,7 @@ from unittest.mock import patch
|
||||
import pytest
|
||||
|
||||
from tools.process_registry import ProcessRegistry, ProcessSession
|
||||
from tools.process_registry_env_log import parse_log_delta_reply
|
||||
from tools.process_registry_env_log import log_delta_command, parse_log_delta_reply
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
@@ -71,7 +71,7 @@ class TestEnvPollerIncrementalRead:
|
||||
log, pid = tmp_path / "bg.log", tmp_path / "bg.pid"
|
||||
pid.write_text(f"{os.getpid()}\n")
|
||||
q = lambda p: shlex.quote(str(p))
|
||||
command = ProcessRegistry._log_delta_command(q(log), 0, q(pid), q(tmp_path / "bg.exit"))
|
||||
command = log_delta_command(q(log), 0, q(pid), q(tmp_path / "bg.exit"))
|
||||
for n in range(1, len(full) + 1):
|
||||
log.write_bytes(full[:n])
|
||||
out = subprocess.run(
|
||||
@@ -334,6 +334,6 @@ class TestEnvPollerIncrementalRead:
|
||||
(tmp_path / "bg.exit").write_text("0\n")
|
||||
(tmp_path / "bg.pid").write_text("") # no pid: reads as exited
|
||||
q = lambda p: shlex.quote(str(p))
|
||||
cmd = ProcessRegistry._log_delta_command(q(log), 0, q(tmp_path / "bg.pid"), q(tmp_path / "bg.exit"))
|
||||
cmd = log_delta_command(q(log), 0, q(tmp_path / "bg.pid"), q(tmp_path / "bg.exit"))
|
||||
out = subprocess.run(["sh", "-c", cmd], capture_output=True, timeout=30, check=False).stdout
|
||||
assert out == b"3 0 exit:0\nok\xc3"
|
||||
|
||||
@@ -108,7 +108,7 @@ class TestBuildSSHCommand:
|
||||
monkeypatch.setenv("OPENAI_API_KEY", "sk-must-not-forward")
|
||||
monkeypatch.setattr(env_passthrough, "get_all_passthrough",
|
||||
lambda: frozenset({"NEXTCLOUD_URL", "NEXTCLOUD_PASS", "OPENAI_API_KEY"}))
|
||||
monkeypatch.setattr(ssh_env, "_load_hermes_env_vars", lambda: {"NEXTCLOUD_PASS": "from-dotenv"})
|
||||
monkeypatch.setattr(ssh_env, "load_hermes_env_vars", lambda: {"NEXTCLOUD_PASS": "from-dotenv"})
|
||||
|
||||
captured = self._capture_run_bash(monkeypatch, env)
|
||||
|
||||
|
||||
+1
-4
@@ -167,18 +167,15 @@ def _build_probe_line() -> str:
|
||||
return "Python toolchain: " + ", ".join(bits) + "."
|
||||
|
||||
|
||||
def get_environment_probe_line(*, force_refresh: bool = False) -> str:
|
||||
def get_environment_probe_line() -> str:
|
||||
"""Return the cached probe line (building it on first call); "" when the
|
||||
environment is clean, so the prompt assembler drops the section. Waits at most
|
||||
``_PROBE_WAIT_TIMEOUT`` on the single worker, then fails open with "".
|
||||
``force_refresh`` is for tests.
|
||||
|
||||
A wedged probe subprocess (#67964) therefore can never block system-prompt construction — at worst the
|
||||
toolchain line is absent from prompts built while the probe is stuck.
|
||||
"""
|
||||
global _WAIT_ALREADY_TIMED_OUT
|
||||
if force_refresh:
|
||||
_reset_cache_for_tests()
|
||||
# Resolve the backend HERE, in the caller's context: under gateway multiplexing the
|
||||
# routed profile's backend lives in the per-turn terminal scope, which the worker
|
||||
# thread does not inherit. Remote backends answer "" without consulting the cache
|
||||
|
||||
@@ -93,10 +93,6 @@ def _normalize_env_dict(env: dict | None) -> dict[str, str]:
|
||||
return normalized
|
||||
|
||||
|
||||
# Module-level binding: tests patch ``docker._load_hermes_env_vars`` to fake the .env file.
|
||||
_load_hermes_env_vars = load_hermes_env_vars
|
||||
|
||||
|
||||
# Docker label values must match [a-zA-Z0-9_.-] and stay <=63 chars to round-trip
|
||||
# through `docker ps --filter label=key=value`.
|
||||
_LABEL_VALUE_OK_RE = re.compile(r"[^A-Za-z0-9_.-]")
|
||||
@@ -1086,7 +1082,7 @@ class DockerEnvironment(BaseEnvironment):
|
||||
|
||||
def _resolve_passthrough_env(self) -> tuple[dict[str, str], set[str]]:
|
||||
"""See ``remote_common.resolve_passthrough_env``; explicit docker_forward_env bypasses the blocklist."""
|
||||
return resolve_passthrough_env(self._forward_env, hermes_env_loader=_load_hermes_env_vars)
|
||||
return resolve_passthrough_env(self._forward_env, hermes_env_loader=load_hermes_env_vars)
|
||||
|
||||
def _build_runtime_env_args_with_unsets(self) -> tuple[list[str], tuple[str, ...], dict[str, str]]:
|
||||
"""Runtime name-only forwarding args, names absent from scope, and the values
|
||||
|
||||
@@ -15,7 +15,7 @@ import time
|
||||
import uuid
|
||||
from typing import Any, Dict, Optional
|
||||
|
||||
from tools.environments.base import BaseEnvironment
|
||||
from tools.environments.base import BaseEnvironment, touch_activity_if_due
|
||||
from tools.interrupt import is_interrupted
|
||||
from tools.managed_tool_gateway import resolve_managed_tool_gateway
|
||||
from tools.terminal_tool_sudo_askpass import fill_timeout_token
|
||||
@@ -122,13 +122,7 @@ class ManagedModalEnvironment(BaseEnvironment):
|
||||
if time.monotonic() >= deadline:
|
||||
self._cancel_exec(exec_id)
|
||||
return _result(f"Managed Modal exec timed out after {timeout}s", 124)
|
||||
# Periodic activity touch so the gateway knows we're alive (lazy import:
|
||||
# tests stub tools.environments.base with only BaseEnvironment)
|
||||
try:
|
||||
from tools.environments.base import touch_activity_if_due
|
||||
touch_activity_if_due(_activity_state, "modal command running")
|
||||
except Exception:
|
||||
pass
|
||||
touch_activity_if_due(_activity_state, "modal command running")
|
||||
time.sleep(0.25)
|
||||
|
||||
@staticmethod
|
||||
|
||||
@@ -28,9 +28,6 @@ logger = logging.getLogger(__name__)
|
||||
# Skip multiplexing there; each command pays a fresh connection but the backend works. See #73927.
|
||||
_SSH_MULTIPLEX = os.name != "nt"
|
||||
|
||||
# Module-level binding: tests patch ``ssh._load_hermes_env_vars`` to fake the .env file.
|
||||
_load_hermes_env_vars = load_hermes_env_vars
|
||||
|
||||
|
||||
def _ensure_ssh_available() -> None:
|
||||
"""Fail fast with a clear error when the SSH client is unavailable."""
|
||||
@@ -342,7 +339,7 @@ class SSHEnvironment(BaseEnvironment):
|
||||
client's env carries the values, so secrets never enter the remote ``bash -c`` argv. The
|
||||
remote sshd must ``AcceptEnv`` them (#14091). Profile-scoped names missing from the active
|
||||
scope are unset remotely so a shared host cannot serve another profile's value."""
|
||||
values, unset_names = resolve_passthrough_env(hermes_env_loader=_load_hermes_env_vars)
|
||||
values, unset_names = resolve_passthrough_env(hermes_env_loader=load_hermes_env_vars)
|
||||
cmd = self._build_ssh_command(send_env=values) + bash_argv(shlex.quote(prepend_unset(cmd_string, unset_names)), login)
|
||||
client_env = client_env_with(values)
|
||||
return _popen_bash(cmd, stdin_data, env=client_env) if client_env is not None else _popen_bash(cmd, stdin_data)
|
||||
|
||||
@@ -1413,7 +1413,6 @@ class ProcessRegistry(ProcessTerminationMixin, ProcessCheckpointMixin, ProcessOu
|
||||
logger.warning("%s wait failed; recording known exit status: %s", label, e)
|
||||
self._finish_exited(session, exit_code())
|
||||
|
||||
_log_delta_command = staticmethod(log_delta_command)
|
||||
|
||||
def _env_poller_loop(self, session: ProcessSession, env: Any, log_path: str, pid_path: str, exit_path: str):
|
||||
"""Background thread: poll a sandbox log file for non-local backends.
|
||||
@@ -1428,7 +1427,7 @@ class ProcessRegistry(ProcessTerminationMixin, ProcessCheckpointMixin, ProcessOu
|
||||
try:
|
||||
# Read only the bytes written since the last poll, plus the process status.
|
||||
raw = env.execute(
|
||||
self._log_delta_command(q(log_path), prev_output_bytes, q(pid_path), q(exit_path)),
|
||||
log_delta_command(q(log_path), prev_output_bytes, q(pid_path), q(exit_path)),
|
||||
timeout=10).get("output", "")
|
||||
reply = parse_log_delta_reply(raw)
|
||||
delta = reply.delta if reply else ""
|
||||
|
||||
Reference in New Issue
Block a user