The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(tui-gateway): isolate session cwd by profile
fangliquana613d4f961fe
diff --git a/tests/tui_gateway/test_config_profile_scope.py b/tests/tui_gateway/test_config_profile_scope.pyindex f10e2d61cc0..4bf40b33ae8 100644--- a/tests/tui_gateway/test_config_profile_scope.py+++ b/tests/tui_gateway/test_config_profile_scope.py@@ -13,6 +13,7 @@ from pathlib import Pathimport yamlimport tui_gateway.server as server+from hermes_constants import reset_hermes_home_override, set_hermes_home_overrideLAUNCH_CWD = "/workspace/default"@@ -111,3 +112,49 @@ def test_config_set_without_profile_still_writes_launch_home(tmp_path, monkeypatassert resp["result"]["value"] == "interrupt"assert _read_yaml(launch)["display"]["busy_input_mode"] == "interrupt"assert _read_yaml(worker)["display"]["busy_input_mode"] == "queue"+++def test_launch_cwd_reads_launch_config_during_foreign_profile_scope(tmp_path, monkeypatch):+ launch, worker = _homes(tmp_path)+ launch_cwd = tmp_path / "launch-workspace"+ worker_cwd = tmp_path / "worker-workspace"+ launch_cwd.mkdir()+ worker_cwd.mkdir()+ _write_cfg(launch, str(launch_cwd), LAUNCH_ROOTS)+ _write_cfg(worker, str(worker_cwd), WORKER_ROOTS)+ _bind_homes(monkeypatch, launch, worker)++ token = set_hermes_home_override(str(worker))+ try:+ assert server._launch_configured_cwd() == str(launch_cwd)+ finally:+ reset_hermes_home_override(token)+++def test_profile_cwd_write_does_not_retarget_launch_session(tmp_path, monkeypatch):+ launch, worker = _homes(tmp_path)+ launch_cwd = tmp_path / "launch-workspace"+ worker_cwd = tmp_path / "worker-workspace"+ launch_cwd.mkdir()+ worker_cwd.mkdir()+ _write_cfg(launch, ".", LAUNCH_ROOTS)+ _write_cfg(worker, str(worker_cwd), WORKER_ROOTS)+ _bind_homes(monkeypatch, launch, worker)+ monkeypatch.setenv("TERMINAL_CWD", str(launch_cwd))+ monkeypatch.setattr(server, "_schedule_agent_build", lambda _sid: None)+ monkeypatch.setattr(server, "_schedule_session_cap_enforcement", lambda: None)++ response = _set(+ {"key": "terminal.cwd", "value": str(worker_cwd), "profile": "code"}+ )+ assert response["result"]["cwd"] == str(worker_cwd)+ assert _read_yaml(worker)["terminal"]["cwd"] == str(worker_cwd)+ assert server.os.environ["TERMINAL_CWD"] == str(launch_cwd)++ created = server._methods["session.create"]("rid-create", {"cols": 80})+ sid = created["result"]["session_id"]+ try:+ assert created["result"]["info"]["cwd"] == str(launch_cwd)+ assert server._sessions[sid]["profile_home"] is None+ finally:+ server._sessions.pop(sid, None)diff --git a/tui_gateway/methods_config_set.py b/tui_gateway/methods_config_set.pyindex eeb83c24e87..8034a869df6 100644--- a/tui_gateway/methods_config_set.py+++ b/tui_gateway/methods_config_set.py@@ -414,9 +414,9 @@ def _set_cwd(rid, params, key, value, session):if not os.path.isdir(cwd):return _err(rid, 4002, f"working directory does not exist: {raw}")_write_config_key("terminal.cwd", cwd)- # ``TERMINAL_CWD`` is the LAUNCH process's; a profile-bound session persists its cwd through its- # own config.yaml (written above under the session scope) and must not leak it into other profiles.- if not (isinstance(session, dict) and session.get("profile_home")):+ # ``TERMINAL_CWD`` belongs to the launch process. Keep launch-profile updates live, but never+ # publish an explicit or session-bound secondary profile's cwd into that process-wide carrier.+ if Path(get_hermes_home()).resolve() == Path(_hermes_home).resolve():os.environ["TERMINAL_CWD"] = cwdreturn _kv(rid, "terminal.cwd", cwd, cwd=cwd, branch=git_probe.branch(cwd))diff --git a/tui_gateway/server.py b/tui_gateway/server.pyindex e6f0242f061..69a3ccf5d1a 100644--- a/tui_gateway/server.py+++ b/tui_gateway/server.py@@ -589,9 +589,9 @@ def _configured_cwd_from_cfg(cfg: dict | None) -> str | None:def _profile_configured_cwd(profile_home: Path | None) -> str | None:- """A non-launch profile's ``terminal.cwd`` from ITS config.yaml (fail-open → None): the process-global+ """A profile's ``terminal.cwd`` from ITS config.yaml (fail-open → None): the process-global``TERMINAL_CWD`` belongs to the *launch* profile, and load_config() resolves the ACTIVE profile, so- read that file through the same effective-config pipeline as ``_load_cfg``.+ read the requested file through the same effective-config pipeline as ``_load_cfg``.A new session bound to another profile must take its workspace from THAT profile's config, not the staleenv var (issue #40334). Returns an absolute, existing directory, or None for placeholders / missing /@@ -609,9 +609,9 @@ def _profile_configured_cwd(profile_home: Path | None) -> str | None:def _launch_configured_cwd() -> str | None:"""Launch profile's ``terminal.cwd`` from config.yaml: the dashboard's in-memory gateway gets no bridged``TERMINAL_CWD`` env (only the Node PTY child does), so a fresh /chat would otherwise start in ``os.getcwd()``."""- with contextlib.suppress(Exception):- return _configured_cwd_from_cfg(_load_cfg())- return None+ # Read the launch file by path. ``_load_cfg`` follows the active HERMES_HOME+ # override, which may belong to a different profile-scoped RPC.+ return _profile_configured_cwd(Path(_hermes_home))def _default_session_cwd() -> str: