Skip to content

The agent that grows with you

fix(tui): make the launch cwd win over an inherited HERMES_CWD

Max Freedom Pollard6a499df86c47
diff --git a/hermes_cli/main_tui_launch.py b/hermes_cli/main_tui_launch.py
index 9771d39af5a..79b07876104 100644
--- a/hermes_cli/main_tui_launch.py
+++ b/hermes_cli/main_tui_launch.py
@@ -362,6 +362,11 @@ def _launch_tui(
# the single factory; keep secrets (the TUI/agent needs provider creds).
from tools.environments.local import build_subprocess_env
env = build_subprocess_env(scrub_secrets=False, inherit_profile_home=True)
+ # The directory this launch was invoked from is the source of truth. An inherited
+ # HERMES_CWD (exported by an outer `hermes --tui`, or by the user's own shell) merely
+ # names *a* real directory, so the is_dir() repair in _apply_tui_python_env keeps it
+ # and the gateway starts in last session's project. `--worktree` still wins below.
+ env["HERMES_CWD"] = _safe_tui_cwd(env)
from hermes_cli.shared_session_attach import configure_tui_attachment
try:
configure_tui_attachment(env, resume_session_id)
diff --git a/tests/hermes_cli/test_tui_resume_flow.py b/tests/hermes_cli/test_tui_resume_flow.py
index 78abab09f7e..c4ef95f9604 100644
--- a/tests/hermes_cli/test_tui_resume_flow.py
+++ b/tests/hermes_cli/test_tui_resume_flow.py
@@ -208,3 +208,59 @@ def test_launch_tui_exports_model_provider_and_toolsets(monkeypatch, main_mod):
assert active_path_during_call == active_path
assert not active_path.exists()
assert env["NODE_ENV"] == "production"
+
+
+def test_launch_tui_prefers_launch_cwd_over_inherited_hermes_cwd(monkeypatch, main_mod, tmp_path):
+ """The directory `hermes --tui` was run from outranks an inherited HERMES_CWD.
+
+ A shell export - or an outer `hermes --tui` - leaves HERMES_CWD naming a real but stale
+ directory, and ui-tui/src/gatewayClient.ts:478 starts the gateway in whatever it names,
+ so the session reads files and completions from the wrong project (#49637).
+ """
+ stale = tmp_path / "stale-project"
+ launch = tmp_path / "launch-project"
+ stale.mkdir()
+ launch.mkdir()
+ monkeypatch.setenv("HERMES_CWD", str(stale))
+ monkeypatch.chdir(launch)
+
+ captured = {}
+ monkeypatch.setattr(main_tui_launch, "_make_tui_argv",
+ lambda tui_dir, tui_dev: (["node", "dist/entry.js"], Path(".")),
+ )
+ monkeypatch.setattr(main_mod.subprocess, "call",
+ lambda argv, cwd=None, env=None: captured.update({"env": env}) or 1,
+ )
+
+ with pytest.raises(SystemExit):
+ main_mod._launch_tui()
+
+ handed_to_tui = Path(captured["env"]["HERMES_CWD"]).resolve()
+ assert handed_to_tui == launch.resolve(), "the TUI gateway must start where the user launched"
+ assert handed_to_tui != stale.resolve()
+
+
+def test_launch_tui_worktree_still_outranks_the_launch_cwd(monkeypatch, main_mod, tmp_path):
+ """`--worktree` names an explicit destination, so it keeps precedence over the launch cwd."""
+ worktree = tmp_path / "worktree"
+ launch = tmp_path / "launch-project"
+ worktree.mkdir()
+ launch.mkdir()
+ monkeypatch.setenv("HERMES_CWD", str(tmp_path))
+ monkeypatch.chdir(launch)
+
+ captured = {}
+ monkeypatch.setattr(main_tui_launch, "_setup_tui_worktree", lambda: {"path": str(worktree)})
+ monkeypatch.setattr(main_tui_launch, "_make_tui_argv",
+ lambda tui_dir, tui_dev: (["node", "dist/entry.js"], Path(".")),
+ )
+ monkeypatch.setattr(main_mod.subprocess, "call",
+ lambda argv, cwd=None, env=None: captured.update({"env": env}) or 1,
+ )
+
+ with pytest.raises(SystemExit):
+ main_mod._launch_tui(worktree=True)
+
+ assert captured["env"]["HERMES_CWD"] == str(worktree)
+ assert captured["env"]["TERMINAL_CWD"] == str(worktree)
+ assert Path(captured["env"]["HERMES_CWD"]).resolve() != launch.resolve()