Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -586,14 +586,18 @@ def load_cli_config() -> Dict[str, Any]:

# CWD resolution for CLI/TUI. The gateway has its own config bridge in
# gateway/run.py but may lazily import cli.py (triggering this code).
# Local backend: always os.getcwd(). Use `cd /dir && hermes` to control it.
# Local backend: honor an explicit launcher-provided workspace first
# (Desktop / embedded TUI set HERMES_CWD), otherwise fall back to the
# current process cwd. Use `cd /dir && hermes` to control it when no
# launcher workspace is provided.
# Non-local with placeholder: pop so terminal_tool uses its per-backend default.
# Non-local with explicit path: keep as-is.
_CWD_PLACEHOLDERS = (".", "auto", "cwd")
effective_backend = terminal_config.get("env_type", "local")

if effective_backend == "local":
terminal_config["cwd"] = os.getcwd()
requested_cwd = os.environ.get("HERMES_CWD") or ""
terminal_config["cwd"] = requested_cwd or os.getcwd()
defaults["terminal"]["cwd"] = terminal_config["cwd"]
elif terminal_config.get("cwd") in _CWD_PLACEHOLDERS:
terminal_config.pop("cwd", None)
Expand Down
24 changes: 24 additions & 0 deletions tests/cli/test_cli_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,30 @@ def test_normalize_root_model_keys_moves_to_model(self):
assert result["model"]["provider"] == "opencode-go"
assert result["model"]["base_url"] == "https://example.com/v1"


class TestLoadCliConfigCwd:
"""load_cli_config() must preserve launcher-provided local workspaces."""

def test_local_backend_prefers_explicit_hermes_cwd(self, tmp_path, monkeypatch):
import cli

hermes_home = tmp_path / ".hermes"
hermes_home.mkdir()
workspace = tmp_path / "workspace"
workspace.mkdir()
elsewhere = tmp_path / "elsewhere"
elsewhere.mkdir()

monkeypatch.setenv("HERMES_HOME", str(hermes_home))
monkeypatch.setenv("HERMES_CWD", str(workspace))
monkeypatch.chdir(elsewhere)
monkeypatch.setattr(cli, "_hermes_home", hermes_home)

cfg = cli.load_cli_config()

assert cfg["terminal"]["cwd"] == str(workspace)
assert os.environ["TERMINAL_CWD"] == str(workspace)

def test_normalize_root_model_keys_does_not_override_existing(self):
"""Existing model.provider is never overridden by root-level key."""
from hermes_cli.config import _normalize_root_model_keys
Expand Down
12 changes: 9 additions & 3 deletions tests/cli/test_cwd_env_respect.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Tests for CLI/TUI CWD resolution in load_cli_config().

Rules:
- Local backend CLI/TUI: always os.getcwd(), ignoring config and inherited env.
- Local backend CLI/TUI: prefer explicit HERMES_CWD, else os.getcwd().
- Non-local with placeholder: pop cwd for backend default.
- Non-local with explicit path: keep as-is.
"""
Expand All @@ -15,7 +15,7 @@ def _resolve_cwd(terminal_config: dict, defaults: dict, env: dict):
effective_backend = terminal_config.get("env_type", "local")

if effective_backend == "local":
terminal_config["cwd"] = "/fake/getcwd"
terminal_config["cwd"] = env.get("HERMES_CWD") or "/fake/getcwd"
defaults["terminal"]["cwd"] = terminal_config["cwd"]
elif terminal_config.get("cwd") in _CWD_PLACEHOLDERS:
terminal_config.pop("cwd", None)
Expand All @@ -32,7 +32,13 @@ def _resolve_cwd(terminal_config: dict, defaults: dict, env: dict):


class TestLocalBackendCli:
"""Local backend always uses os.getcwd()."""
"""Local backend uses explicit workspace cwd when provided."""

def test_explicit_hermes_cwd_wins(self):
env = {"HERMES_CWD": "/workspace/project"}
tc = {"cwd": ".", "env_type": "local"}
d = {"terminal": {"cwd": "."}}
assert _resolve_cwd(tc, d, env) == "/workspace/project"

def test_explicit_config_ignored(self):
env = {}
Expand Down
Loading