Skip to content

The agent that grows with you

fix(cli): a hermes chat started from an agent's terminal runs in its launch directory

Tekniumdc8e416b3421
diff --git a/hermes_cli/cli_config_load.py b/hermes_cli/cli_config_load.py
index feb79a0f67b..9fa18198e4a 100644
--- a/hermes_cli/cli_config_load.py
+++ b/hermes_cli/cli_config_load.py
@@ -9,6 +9,7 @@ from __future__ import annotations
import json
import logging
import os
+import sys
from pathlib import Path
from typing import List, Dict, Any
from utils import fast_safe_load
@@ -138,8 +139,10 @@ def _mirror_config_to_env(defaults, _file_has_terminal_config):
terminal_config.pop("cwd", None)
# TERMINAL_CWD is force-exported (beats stale .env) except inside a gateway process,
- # whose config bridge already set it.
- _is_gateway = os.environ.get("_HERMES_GATEWAY") == "1"
+ # whose config bridge already set it. Ask whether THIS process imported gateway.run:
+ # its _HERMES_GATEWAY env marker is inherited by every shell the gateway's terminal
+ # tool spawns, so a `hermes chat` launched there would keep the gateway's directory.
+ _is_gateway = "gateway.run" in sys.modules
for config_key, env_var in _TERMINAL_ENV_MAPPINGS.items():
if config_key not in terminal_config:
continue
diff --git a/tests/hermes_cli/test_cli_config_load_launch_cwd.py b/tests/hermes_cli/test_cli_config_load_launch_cwd.py
new file mode 100644
index 00000000000..82e37da2f4d
--- /dev/null
+++ b/tests/hermes_cli/test_cli_config_load_launch_cwd.py
@@ -0,0 +1,27 @@
+"""The local CLI runs in its launch directory even when started from a gateway agent's terminal."""
+
+from __future__ import annotations
+
+import os
+import sys
+import types
+
+from hermes_cli.cli_config_load import _mirror_config_to_env
+
+
+def _mirror(monkeypatch, tmp_path):
+ monkeypatch.chdir(tmp_path)
+ monkeypatch.setenv("_HERMES_GATEWAY", "1") # inherited from the gateway that spawned the shell
+ monkeypatch.setenv("TERMINAL_CWD", "/gateway/terminal/cwd")
+ _mirror_config_to_env({"terminal": {"backend": "local", "cwd": "/configured/cwd"}}, True)
+ return os.environ["TERMINAL_CWD"]
+
+
+def test_inherited_gateway_marker_does_not_keep_the_gateway_cwd(monkeypatch, tmp_path):
+ monkeypatch.delitem(sys.modules, "gateway.run", raising=False)
+ assert _mirror(monkeypatch, tmp_path) == str(tmp_path)
+
+
+def test_gateway_process_keeps_its_bridged_cwd(monkeypatch, tmp_path):
+ monkeypatch.setitem(sys.modules, "gateway.run", types.ModuleType("gateway.run"))
+ assert _mirror(monkeypatch, tmp_path) == "/gateway/terminal/cwd"