Skip to content

The agent that grows with you

fix(gateway): resolve messaging cwd defaults at startup, not import

funky-xamarinaf1f5fce1682
diff --git a/gateway/run.py b/gateway/run.py
index a30a7b431d7..9a145be6348 100644
--- a/gateway/run.py
+++ b/gateway/run.py
@@ -2178,21 +2178,6 @@ os.environ["HERMES_QUIET"] = "1" # gateway runs quiet: no debug output, cwd use
# Terminal cwd: config.yaml terminal.cwd is canonical (bridged above); MESSAGING_CWD is legacy fallback.
from gateway.cwd_placeholder import CWD_PLACEHOLDERS, resolve_placeholder_terminal_cwd
-_configured_cwd = os.environ.get("TERMINAL_CWD", "")
-if not _configured_cwd or _configured_cwd in CWD_PLACEHOLDERS:
- _resolved_cwd = resolve_placeholder_terminal_cwd(
- configured_cwd=_configured_cwd,
- terminal_backend=os.environ.get("TERMINAL_ENV", ""),
- messaging_cwd=os.getenv("MESSAGING_CWD"),
- docker_mount_cwd_to_workspace=os.getenv(
- "TERMINAL_DOCKER_MOUNT_CWD_TO_WORKSPACE", "false").lower()
- in {"true", "1", "yes"},
- home_fallback=str(Path.home()))
- if _resolved_cwd is None:
- os.environ.pop("TERMINAL_CWD", None)
- else:
- os.environ["TERMINAL_CWD"] = _resolved_cwd
-
from gateway.config import (
ChannelOverride, Platform, GatewayConfig, PlatformConfig, _getenv, load_gateway_config)
from gateway.session import (
@@ -5851,6 +5836,22 @@ async def start_gateway(config: Optional[GatewayConfig] = None, replace: bool =
# Set here (not at import) so incidental gateway.run imports from CLI code don't poison it.
os.environ["HERMES_EXEC_ASK"] = "1"
+ # Messaging-only defaults belong to startup, not incidental imports by the TUI.
+ configured_cwd = os.environ.get("TERMINAL_CWD", "")
+ if not configured_cwd or configured_cwd in CWD_PLACEHOLDERS:
+ resolved_cwd = resolve_placeholder_terminal_cwd(
+ configured_cwd=configured_cwd,
+ terminal_backend=os.environ.get("TERMINAL_ENV", ""),
+ messaging_cwd=os.getenv("MESSAGING_CWD"),
+ docker_mount_cwd_to_workspace=os.getenv(
+ "TERMINAL_DOCKER_MOUNT_CWD_TO_WORKSPACE", "false").lower()
+ in {"true", "1", "yes"},
+ home_fallback=str(Path.home()))
+ if resolved_cwd is None:
+ os.environ.pop("TERMINAL_CWD", None)
+ else:
+ os.environ["TERMINAL_CWD"] = resolved_cwd
+
from hermes_cli.resource_limits import apply_nofile_soft_limit
apply_nofile_soft_limit()
diff --git a/tests/gateway/test_import_preserves_tui_cwd.py b/tests/gateway/test_import_preserves_tui_cwd.py
new file mode 100644
index 00000000000..91f628dc69f
--- /dev/null
+++ b/tests/gateway/test_import_preserves_tui_cwd.py
@@ -0,0 +1,78 @@
+"""Importing gateway helpers must not apply messaging-only cwd defaults."""
+import os
+from pathlib import Path
+import subprocess
+import sys
+
+import pytest
+
+
[email protected]("configured", ["", ".", "explicit"])
+def test_gateway_import_preserves_cwdless_tui_workspace(tmp_path, configured):
+ repo = Path(__file__).resolve().parents[2]
+ home = tmp_path / "home"
+ launch = tmp_path / "workspace"
+ home.mkdir()
+ launch.mkdir()
+ cwd = launch.as_posix() if configured == "explicit" else configured
+ (home / "config.yaml").write_text(f"terminal:\n cwd: '{cwd}'\n", encoding="utf-8")
+ env = {k: v for k, v in os.environ.items() if k.upper() in {
+ "PATH", "SYSTEMROOT", "WINDIR", "COMSPEC", "PATHEXT",
+ }}
+ for key in ("HOME", "USERPROFILE", "HERMES_HOME", "APPDATA", "LOCALAPPDATA", "TEMP", "TMP"):
+ env[key] = str(home)
+ env["PYTHONPATH"] = str(repo)
+ result = subprocess.run(
+ [sys.executable, "-c", """
+from pathlib import Path
+import tui_gateway.server as server
+launch = Path.cwd()
+assert Path(server._completion_cwd({})) == launch
+import gateway.run
+assert Path(server._completion_cwd({})) == launch, (
+ 'gateway import changed cwd-less TUI workspace', server._completion_cwd({}), str(launch)
+)
+"""],
+ cwd=launch, env=env, capture_output=True, text=True, timeout=60,
+ )
+ assert result.returncode == 0, result.stdout + result.stderr
+
+
+ "backend,mount,configured,legacy,expected",
+ [
+ ("local", "false", ".", None, "home"),
+ ("", "false", "", None, "home"),
+ ("docker", "false", ".", None, None),
+ ("docker", "true", ".", None, None),
+ ("docker", "true", ".", "legacy", "legacy"),
+ ("ssh", "false", ".", None, None),
+ ("local", "false", "explicit", None, "explicit"),
+ ("local", "false", ".", "legacy", "legacy"),
+ ],
+)
+def test_gateway_start_keeps_messaging_cwd_defaults(
+ monkeypatch, tmp_path, backend, mount, configured, legacy, expected,
+):
+ import asyncio
+ from gateway.run import start_gateway
+ from hermes_cli import resource_limits
+
+ class StopBeforeStartup(Exception):
+ pass
+
+ def stop():
+ raise StopBeforeStartup
+
+ monkeypatch.setattr(Path, "home", lambda: tmp_path)
+ monkeypatch.setenv("TERMINAL_ENV", backend)
+ monkeypatch.setenv("TERMINAL_DOCKER_MOUNT_CWD_TO_WORKSPACE", mount)
+ monkeypatch.setenv("TERMINAL_CWD", configured)
+ if legacy is None:
+ monkeypatch.delenv("MESSAGING_CWD", raising=False)
+ else:
+ monkeypatch.setenv("MESSAGING_CWD", legacy)
+ monkeypatch.setattr(resource_limits, "apply_nofile_soft_limit", stop)
+ with pytest.raises(StopBeforeStartup):
+ asyncio.run(start_gateway())
+ assert os.environ.get("TERMINAL_CWD") == (str(tmp_path) if expected == "home" else expected)