Skip to content

Commit 6a499df

Browse files
MaxFreedomPollardclaude
authored andcommitted
fix(tui): make the launch cwd win over an inherited HERMES_CWD
`_launch_tui` builds the child env from `os.environ.copy()` (via `build_subprocess_env`) and only repaired HERMES_CWD when it was missing or no longer a directory (`hermes_cli/main_tui_launch.py:684-686`). A HERMES_CWD exported by an outer `hermes --tui` — or by the user's own shell — names a real directory, so it survived into the Node TUI, and `ui-tui/src/gatewayClient.ts:478` starts the gateway process in whatever it names: the session read files and completions from last session's project. Assign HERMES_CWD from the live launch cwd right after the env is built, ahead of the `--worktree` block so an explicit worktree still wins. `_apply_tui_python_env` is deliberately untouched — it is shared with the dashboard PTY launch at `hermes_cli/web_server_chat.py:344`, which has no launch cwd of its own. Fixes #49637 Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
1 parent 23d9fd4 commit 6a499df

2 files changed

Lines changed: 61 additions & 0 deletions

File tree

‎hermes_cli/main_tui_launch.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,11 @@ def _launch_tui(
362362
# the single factory; keep secrets (the TUI/agent needs provider creds).
363363
from tools.environments.local import build_subprocess_env
364364
env = build_subprocess_env(scrub_secrets=False, inherit_profile_home=True)
365+
# The directory this launch was invoked from is the source of truth. An inherited
366+
# HERMES_CWD (exported by an outer `hermes --tui`, or by the user's own shell) merely
367+
# names *a* real directory, so the is_dir() repair in _apply_tui_python_env keeps it
368+
# and the gateway starts in last session's project. `--worktree` still wins below.
369+
env["HERMES_CWD"] = _safe_tui_cwd(env)
365370
from hermes_cli.shared_session_attach import configure_tui_attachment
366371
try:
367372
configure_tui_attachment(env, resume_session_id)

‎tests/hermes_cli/test_tui_resume_flow.py‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,3 +208,59 @@ def fake_call(argv, cwd=None, env=None):
208208
assert active_path_during_call == active_path
209209
assert not active_path.exists()
210210
assert env["NODE_ENV"] == "production"
211+
212+
213+
def test_launch_tui_prefers_launch_cwd_over_inherited_hermes_cwd(monkeypatch, main_mod, tmp_path):
214+
"""The directory `hermes --tui` was run from outranks an inherited HERMES_CWD.
215+
216+
A shell export - or an outer `hermes --tui` - leaves HERMES_CWD naming a real but stale
217+
directory, and ui-tui/src/gatewayClient.ts:478 starts the gateway in whatever it names,
218+
so the session reads files and completions from the wrong project (#49637).
219+
"""
220+
stale = tmp_path / "stale-project"
221+
launch = tmp_path / "launch-project"
222+
stale.mkdir()
223+
launch.mkdir()
224+
monkeypatch.setenv("HERMES_CWD", str(stale))
225+
monkeypatch.chdir(launch)
226+
227+
captured = {}
228+
monkeypatch.setattr(main_tui_launch, "_make_tui_argv",
229+
lambda tui_dir, tui_dev: (["node", "dist/entry.js"], Path(".")),
230+
)
231+
monkeypatch.setattr(main_mod.subprocess, "call",
232+
lambda argv, cwd=None, env=None: captured.update({"env": env}) or 1,
233+
)
234+
235+
with pytest.raises(SystemExit):
236+
main_mod._launch_tui()
237+
238+
handed_to_tui = Path(captured["env"]["HERMES_CWD"]).resolve()
239+
assert handed_to_tui == launch.resolve(), "the TUI gateway must start where the user launched"
240+
assert handed_to_tui != stale.resolve()
241+
242+
243+
def test_launch_tui_worktree_still_outranks_the_launch_cwd(monkeypatch, main_mod, tmp_path):
244+
"""`--worktree` names an explicit destination, so it keeps precedence over the launch cwd."""
245+
worktree = tmp_path / "worktree"
246+
launch = tmp_path / "launch-project"
247+
worktree.mkdir()
248+
launch.mkdir()
249+
monkeypatch.setenv("HERMES_CWD", str(tmp_path))
250+
monkeypatch.chdir(launch)
251+
252+
captured = {}
253+
monkeypatch.setattr(main_tui_launch, "_setup_tui_worktree", lambda: {"path": str(worktree)})
254+
monkeypatch.setattr(main_tui_launch, "_make_tui_argv",
255+
lambda tui_dir, tui_dev: (["node", "dist/entry.js"], Path(".")),
256+
)
257+
monkeypatch.setattr(main_mod.subprocess, "call",
258+
lambda argv, cwd=None, env=None: captured.update({"env": env}) or 1,
259+
)
260+
261+
with pytest.raises(SystemExit):
262+
main_mod._launch_tui(worktree=True)
263+
264+
assert captured["env"]["HERMES_CWD"] == str(worktree)
265+
assert captured["env"]["TERMINAL_CWD"] == str(worktree)
266+
assert Path(captured["env"]["HERMES_CWD"]).resolve() != launch.resolve()

0 commit comments

Comments
 (0)