Skip to content

The agent that grows with you

fix: profile terminal.cwd beats the desktop's inherited workspace cwd

Hermes Agenta9972dc3f915
diff --git a/apps/desktop/src/app/session/hooks/use-cwd-actions.ts b/apps/desktop/src/app/session/hooks/use-cwd-actions.ts
index 0943131d0d2..1474cf192d7 100644
--- a/apps/desktop/src/app/session/hooks/use-cwd-actions.ts
+++ b/apps/desktop/src/app/session/hooks/use-cwd-actions.ts
@@ -7,6 +7,7 @@ import {
$newChatWorkspaceTargetGeneration,
setCurrentBranch,
setCurrentCwd,
+ setCurrentCwdExplicit,
setNewChatWorkspaceTarget
} from '@/store/session'
import type { SessionRuntimeInfo } from '@/types/hermes'
@@ -61,6 +62,9 @@ export function useCwdActions({ activeSessionIdRef, onSessionRuntimeInfo, reques
if (!sessionId) {
setCurrentCwd(trimmed)
+ // A folder chosen here is a deliberate workspace pick (#52589) — the
+ // gateway must let it beat a named profile's configured terminal.cwd.
+ setCurrentCwdExplicit(true)
const workspaceGeneration = setNewChatWorkspaceTarget(trimmed)
try {
diff --git a/apps/desktop/src/app/session/hooks/use-session-actions/index.ts b/apps/desktop/src/app/session/hooks/use-session-actions/index.ts
index 416a81a27c2..7dc9df3538b 100644
--- a/apps/desktop/src/app/session/hooks/use-session-actions/index.ts
+++ b/apps/desktop/src/app/session/hooks/use-session-actions/index.ts
@@ -67,6 +67,7 @@ import {
$activeSessionStoredIdRotation,
$connection,
$currentCwd,
+ $currentCwdExplicit,
$currentFastMode,
$currentModel,
$currentProvider,
@@ -86,6 +87,7 @@ import {
setBusy,
setCurrentBranch,
setCurrentCwd,
+ setCurrentCwdExplicit,
setCurrentCwdTransient,
setCurrentServiceTier,
setCurrentUsage,
@@ -358,6 +360,9 @@ async function desktopSessionCreateParams(
cols: 96,
source: 'desktop',
...(cwd && { cwd }),
+ // #52589: explicit provenance for the shipped cwd — an inherited app-global
+ // workspace must not override the target profile's configured terminal.cwd.
+ ...(cwd && { cwd_explicit: $currentCwdExplicit.get() }),
...(profile ? { profile: capturedRoute?.targetProfile || profile } : {}),
...(includeComposerSelection
? {
@@ -596,6 +601,10 @@ export function useSessionActions({
setCurrentServiceTier('')
setYoloActive(false)
setNewChatWorkspaceTarget(hasWorkspaceTarget ? workspaceTarget : undefined)
+ // #52589 provenance: only a deliberate string workspace target is an explicit
+ // cwd choice. A plain new chat (or a detached `null`) is not — its inherited
+ // launch/project workspace must yield to a named profile's configured cwd.
+ setCurrentCwdExplicit(typeof workspaceTarget === 'string')
if (!hasWorkspaceTarget) {
// In a project → the repo's default-branch checkout; not in a project →
@@ -915,6 +924,10 @@ export function useSessionActions({
const cwd =
options?.cwd === null ? '' : typeof options?.cwd === 'string' ? options.cwd.trim() : resolveNewSessionCwd()
+ // #52589 provenance for the tile path: an explicitly-passed cwd is a
+ // deliberate workspace pick; a resolved default is inherited.
+ setCurrentCwdExplicit(typeof options?.cwd === 'string')
+
// Bot-workspace tabs target an agent profile without switching the
// window's ambient composer. Do not leak that unrelated session's
// composer selection (manual model/provider, reasoning effort, fast
diff --git a/apps/desktop/src/store/session.ts b/apps/desktop/src/store/session.ts
index 1cc5d10684f..e65d7321a3b 100644
--- a/apps/desktop/src/store/session.ts
+++ b/apps/desktop/src/store/session.ts
@@ -1721,6 +1721,15 @@ export const setNewChatWorkspaceTarget = (next: NewChatWorkspaceTarget): number
return generation
}
+// True only when the next new chat's cwd is a deliberate workspace choice (#52589).
+// The desktop otherwise seeds a chat's cwd from its app-global workspace (the launch
+// profile's configured directory / project scope); the gateway must treat that as an
+// inherited default — NOT an explicit pick — so a named profile's own terminal.cwd
+// wins. Path equality cannot distinguish the two, so the flag ships with the create.
+export const $currentCwdExplicit = atom(false)
+
+export const setCurrentCwdExplicit = (next: Updater<boolean>) => updateAtom($currentCwdExplicit, next)
+
export const workspaceCwdForNewSession = (): string => {
// A bare new chat starts DETACHED — no inherited cwd, so the composer's coding
// rail (which keys off $currentCwd) shows no branch and the first message runs
diff --git a/tests/tui_gateway/test_tui_gateway_server.py b/tests/tui_gateway/test_tui_gateway_server.py
index a64e5ca31f8..ee947f35acc 100644
--- a/tests/tui_gateway/test_tui_gateway_server.py
+++ b/tests/tui_gateway/test_tui_gateway_server.py
@@ -1022,10 +1022,60 @@ def test_completion_cwd_explicit_cwd_wins_over_profile(monkeypatch, tmp_path):
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"})
+ result = server._completion_cwd(
+ {"cwd": str(explicit), "cwd_explicit": True, "profile": "ef-design"}
+ )
assert result == str(explicit)
+def test_completion_cwd_profile_overrides_inherited_workspace(monkeypatch, tmp_path):
+ """Issue #52589: the desktop seeds a new chat's cwd from its app-global workspace
+ (the launch profile's configured directory) — that inherited default must NOT
+ override the target profile's own ``terminal.cwd``."""
+ launch_ws = tmp_path / "workspace"
+ launch_ws.mkdir()
+ profile_ws = tmp_path / "products"
+ profile_ws.mkdir()
+ home = _write_profile_cfg(tmp_path / "home-dev", str(profile_ws))
+
+ monkeypatch.setattr(server, "_profile_home", lambda name: home if name else None)
+ # No cwd_explicit: the client cwd is the inherited app-global workspace.
+ assert (
+ server._completion_cwd({"profile": "dev", "cwd": str(launch_ws)}) == str(profile_ws)
+ )
+
+
+def test_completion_cwd_explicit_pick_wins_over_profile(monkeypatch, tmp_path):
+ """Issue #52589 regression guard: a deliberate workspace pick (``cwd_explicit``)
+ still beats the profile's configured ``terminal.cwd``."""
+ explicit = tmp_path / "explicit-lane"
+ explicit.mkdir()
+ profile_ws = tmp_path / "products"
+ profile_ws.mkdir()
+ home = _write_profile_cfg(tmp_path / "home-dev", str(profile_ws))
+
+ monkeypatch.setattr(server, "_profile_home", lambda name: home if name else None)
+ assert (
+ server._completion_cwd(
+ {"profile": "dev", "cwd": str(explicit), "cwd_explicit": True}
+ )
+ == str(explicit)
+ )
+
+
+def test_completion_cwd_inherited_workspace_without_profile_cfg_kept(monkeypatch, tmp_path):
+ """An inherited workspace stays when the target profile has NO configured
+ terminal.cwd — the profile override only applies when one exists (#52589)."""
+ launch_ws = tmp_path / "workspace"
+ launch_ws.mkdir()
+ no_cfg_home = tmp_path / "home-plain"
+ no_cfg_home.mkdir()
+ (no_cfg_home / "config.yaml").write_text("{}", encoding="utf-8")
+
+ monkeypatch.setattr(server, "_profile_home", lambda name: no_cfg_home if name else None)
+ assert server._completion_cwd({"profile": "plain", "cwd": str(launch_ws)}) == str(launch_ws)
+
+
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"
diff --git a/tui_gateway/session_workdir.py b/tui_gateway/session_workdir.py
index c95a0d84892..2618bf8d391 100644
--- a/tui_gateway/session_workdir.py
+++ b/tui_gateway/session_workdir.py
@@ -22,10 +22,21 @@ def _normalize_completion_path(path_part: str) -> str:
def _completion_cwd(params: dict | None = None) -> str:
params = params or {}
+ # Provenance for the client-sent ``cwd`` (#52589): the desktop seeds a new chat's cwd
+ # from its app-global workspace (the launch profile's configured directory or the
+ # project scope) when the user did NOT pick one. That inherited default must NOT
+ # override a NAMED profile's own ``terminal.cwd`` — only a deliberate per-session
+ # workspace pick (``cwd_explicit``) wins over the profile config. Path equality
+ # cannot tell the two apart, so the desktop ships the flag alongside the path.
+ client_cwd = params.get("cwd")
+ if not params.get("cwd_explicit") and client_cwd:
+ profile_cwd = _profile_configured_cwd(_profile_home(params.get("profile")))
+ if profile_cwd:
+ return profile_cwd
# A session bound to another profile resolves its workspace from THAT profile's config before the launch profile's
# env var; the dashboard's in-memory gateway does NOT inherit the PTY child's bridged TERMINAL_CWD, so a configured
# terminal.cwd is read directly.
- raw = (params.get("cwd") or _sessions.get(params.get("session_id") or "", {}).get("cwd")
+ raw = (client_cwd or _sessions.get(params.get("session_id") or "", {}).get("cwd")
or _profile_configured_cwd(_profile_home(params.get("profile"))) or _launch_configured_cwd()
or os.environ.get("TERMINAL_CWD") or os.getcwd())
with contextlib.suppress(Exception):