fix(tui_gateway): honor target profile's terminal.cwd on desktop profile switch (#40892)
* fix(tui_gateway): honor target profile's terminal.cwd on desktop profile switch The desktop's app-global remote mode serves every profile from one tui_gateway backend, so the process-global TERMINAL_CWD only reflects the launch profile. After switching profiles, a new session resolved its workspace from that stale env var and inherited the previous profile's directory. Add _profile_configured_cwd() to read a non-launch profile's own terminal.cwd from its config.yaml (skipping placeholder/empty/missing and non-existent paths so callers fall back cleanly), and wire it into _completion_cwd() with precedence: explicit client cwd -> existing session cwd -> bound profile's configured cwd -> TERMINAL_CWD -> os.getcwd(). Fixes #40334 * test(tui_gateway): cover per-profile cwd resolution (#40334) Pin the new contract: _profile_configured_cwd reads a profile's own terminal.cwd and rejects placeholders/missing paths, and _completion_cwd prefers a bound profile's cwd over a stale launch-profile TERMINAL_CWD while still letting an explicit client cwd win.
This commit is contained in:
parent
59ea2f98e6
commit
93340fa3c1
2 changed files with 98 additions and 2 deletions
|
|
@ -107,6 +107,64 @@ def test_session_context_explicit_cwd_for_ephemeral_task(monkeypatch, tmp_path):
|
|||
server._clear_session_context(tokens)
|
||||
|
||||
|
||||
def _write_profile_cfg(home: Path, cwd: str | None) -> Path:
|
||||
import yaml
|
||||
|
||||
home.mkdir(parents=True, exist_ok=True)
|
||||
cfg = {"terminal": {"cwd": cwd}} if cwd is not None else {}
|
||||
(home / "config.yaml").write_text(yaml.safe_dump(cfg), encoding="utf-8")
|
||||
return home
|
||||
|
||||
|
||||
def test_profile_configured_cwd_reads_target_profile(tmp_path):
|
||||
"""A profile's own terminal.cwd is read from its config.yaml."""
|
||||
project = tmp_path / "proj"
|
||||
project.mkdir()
|
||||
home = _write_profile_cfg(tmp_path / "home", str(project))
|
||||
assert server._profile_configured_cwd(home) == str(project)
|
||||
|
||||
|
||||
def test_profile_configured_cwd_skips_placeholders_and_missing(tmp_path):
|
||||
"""Placeholder values, missing config, and bad paths fall through to None."""
|
||||
assert server._profile_configured_cwd(None) is None
|
||||
assert server._profile_configured_cwd(tmp_path / "nope") is None
|
||||
for placeholder in (".", "auto", "cwd", ""):
|
||||
home = _write_profile_cfg(tmp_path / placeholder.strip("."), placeholder)
|
||||
assert server._profile_configured_cwd(home) is None
|
||||
home = _write_profile_cfg(tmp_path / "ghost", str(tmp_path / "does-not-exist"))
|
||||
assert server._profile_configured_cwd(home) is None
|
||||
|
||||
|
||||
def test_completion_cwd_prefers_profile_over_stale_env(monkeypatch, tmp_path):
|
||||
"""Issue #40334: a new session bound to another profile must use THAT
|
||||
profile's terminal.cwd, not the launch profile's stale TERMINAL_CWD."""
|
||||
profile_b = tmp_path / "ef-design"
|
||||
profile_b.mkdir()
|
||||
home = _write_profile_cfg(tmp_path / "home-b", str(profile_b))
|
||||
stale = tmp_path / "mahjong"
|
||||
stale.mkdir()
|
||||
|
||||
monkeypatch.setenv("TERMINAL_CWD", str(stale))
|
||||
monkeypatch.setattr(server, "_profile_home", lambda name: home if name else None)
|
||||
|
||||
assert server._completion_cwd({"profile": "ef-design"}) == str(profile_b)
|
||||
# No profile → unchanged fallback to the launch env var.
|
||||
assert server._completion_cwd({}) == str(stale)
|
||||
|
||||
|
||||
def test_completion_cwd_explicit_cwd_wins_over_profile(monkeypatch, tmp_path):
|
||||
"""An explicit client-provided cwd still beats the profile config."""
|
||||
explicit = tmp_path / "explicit"
|
||||
explicit.mkdir()
|
||||
profile_b = tmp_path / "configured"
|
||||
profile_b.mkdir()
|
||||
home = _write_profile_cfg(tmp_path / "home-c", str(profile_b))
|
||||
|
||||
monkeypatch.setattr(server, "_profile_home", lambda name: home if name else None)
|
||||
result = server._completion_cwd({"cwd": str(explicit), "profile": "ef-design"})
|
||||
assert result == str(explicit)
|
||||
|
||||
|
||||
def test_terminal_task_cwd_local_backend_uses_session_cwd(monkeypatch, tmp_path):
|
||||
"""A local terminal backend must keep host-validated session cwd behaviour."""
|
||||
project = tmp_path / "project"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue