Skip to content

The agent that grows with you

fix(desktop): make workspaceCwdForNewSession behave identically in local and remote mode (#57911)

Ahmett1013420c9facb8a
diff --git a/apps/desktop/src/store/session.test.ts b/apps/desktop/src/store/session.test.ts
index 22253f6f205..3639261d680 100644
--- a/apps/desktop/src/store/session.test.ts
+++ b/apps/desktop/src/store/session.test.ts
@@ -37,6 +37,7 @@ import {
getConfiguredDefaultProjectDir,
getRememberedRoute,
getRememberedSessionId,
+ getRememberedWorkspaceCwd,
getSessionOwnerHint,
getSessionOwnerHints,
hydrateSessionOwnerHints,
@@ -814,16 +815,22 @@ describe('workspaceCwdForNewSession', () => {
$currentCwd.set('/live/session/path')
$connection.set({ baseUrl: 'http://backend-a', mode: 'remote' } as never)
+ // Bare new sessions are intentionally DETACHED across every mode
+ // (#57911): neither sticky local nor sticky remote cwd is an accept-
+ // able bare-default — only an explicit configured default pre-attaches.
+ // The per-backend memory itself stays isolated (asserted directly).
expect(workspaceCwdForNewSession()).toBe('')
setCurrentCwd('/backend/project-a')
- expect(workspaceCwdForNewSession()).toBe('/backend/project-a')
+ expect(getRememberedWorkspaceCwd()).toBe('/backend/project-a')
+ expect(workspaceCwdForNewSession()).toBe('')
$connection.set({ baseUrl: 'http://backend-b', mode: 'remote' } as never)
- expect(workspaceCwdForNewSession()).toBe('')
+ expect(getRememberedWorkspaceCwd()).toBe('')
setCurrentCwd('/backend/project-b')
- expect(workspaceCwdForNewSession()).toBe('/backend/project-b')
+ expect(getRememberedWorkspaceCwd()).toBe('/backend/project-b')
+ expect(workspaceCwdForNewSession()).toBe('')
// Back on local with no configured default: a bare new chat is detached and
// never reads the remote keys (nor inherits the sticky local workspace).
@@ -832,20 +839,23 @@ describe('workspaceCwdForNewSession', () => {
})
it('remembers only the workspace the user picked, not the one they looked at', () => {
- // The reported bug (#77496 / #80213): on a remote backend a new chat starts
- // in the remembered workspace, and every session resume used to write that
- // key — so opening a project chat silently made it the destination for the
- // next "New session". Following a conversation must leave the memory alone.
+ // The reported bug (#77496 / #80213): every session resume used to write
+ // the remembered-workspace key — so opening a project chat silently moved
+ // the memory. Following a conversation must leave the memory alone.
+ // (Since #57911 a bare new session is detached in remote mode too, so the
+ // memory is asserted directly — its remaining consumer is resume seeding
+ // via ensureDefaultWorkspaceCwd.)
$connection.set({ baseUrl: 'http://backend-a', mode: 'remote' } as never)
setCurrentCwd('/backend/picked')
setCurrentCwdTransient('/backend/some-other-project')
expect($currentCwd.get()).toBe('/backend/some-other-project')
- expect(workspaceCwdForNewSession()).toBe('/backend/picked')
+ expect(getRememberedWorkspaceCwd()).toBe('/backend/picked')
+ expect(workspaceCwdForNewSession()).toBe('')
})
- it('settling a resumed session does not move where the next new chat starts', () => {
+ it('settling a resumed session does not move the remembered workspace', () => {
// The reporter's exact sequence: work in a project, open a chat from it,
// then ask for a new session. Resume settling publishes the conversation's
// cwd through commitWorkspaceCwdForSelectedSession — which must not claim
@@ -856,7 +866,39 @@ describe('workspaceCwdForNewSession', () => {
setSelectedStoredSessionId('sess-in-project')
commitWorkspaceCwdForSelectedSession('/backend/last-project')
- expect(workspaceCwdForNewSession()).toBe('/backend/picked')
+ expect(getRememberedWorkspaceCwd()).toBe('/backend/picked')
+ expect(workspaceCwdForNewSession()).toBe('')
+ })
+
+ it('does not stick a previous remote workspace onto a bare new session (#57911)', () => {
+ // Repro: in remote mode the user attaches to project-A so the renderer
+ // persists /tradingview as the remembered cwd under the remote key. The
+ // user then presses Cmd+N *without* being scoped into any project. A bare
+ // new session must NOT inherit /tradingview — pre-fix this returned the
+ // sticky remembered cwd and the gateway mapped it back to the wrong
+ // project via project_tree.py.
+ window.localStorage.setItem(
+ 'hermes.desktop.workspace-cwd.remote.http%3A%2F%2Fbackend-a.default',
+ '/tradingview',
+ )
+ $connection.set({ baseUrl: 'http://backend-a', mode: 'remote' } as never)
+ applyConfiguredDefaultProjectDir(null)
+
+ expect(workspaceCwdForNewSession()).toBe('')
+ })
+
+ it('respects an explicit configured default in remote mode (#57911)', () => {
+ // Symmetric guard: removing the remote branch must NOT regress users who
+ // *did* set a configured default — the explicit default pre-attaches
+ // identically across local and remote mode.
+ $connection.set({ baseUrl: 'http://backend-a', mode: 'remote' } as never)
+ window.localStorage.setItem(
+ 'hermes.desktop.workspace-cwd.remote.http%3A%2F%2Fbackend-a.default',
+ '/tradingview',
+ )
+ applyConfiguredDefaultProjectDir('/home/user/configured')
+
+ expect(workspaceCwdForNewSession()).toBe('/home/user/configured')
})
})
diff --git a/apps/desktop/src/store/session.ts b/apps/desktop/src/store/session.ts
index b89d9e9a314..023d8d22051 100644
--- a/apps/desktop/src/store/session.ts
+++ b/apps/desktop/src/store/session.ts
@@ -1349,16 +1349,19 @@ export const setNewChatWorkspaceTarget = (next: NewChatWorkspaceTarget): number
}
export const workspaceCwdForNewSession = (): string => {
- if ($connection.get()?.mode === 'remote') {
- return getRememberedWorkspaceCwd()
- }
-
// 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
// in the gateway's default rather than silently in the last repo you touched.
// Only an explicit default-project-dir setting pre-attaches. Entering a
// project/worktree attaches its cwd directly (startSessionInWorkspace), so the
// "remember where I was when I'm in a project" case is unaffected.
+ //
+ // This must behave identically in local and remote mode: the remembered CWD
+ // under the remote-keyed workspaceCwdKey() can be from a *different* project
+ // than the one the user is currently scoped into, and bare-new-session in
+ // the wrong workspace was the #57911 symptom. Resume/restore still reads
+ // the remembered cwd via ensureDefaultWorkspaceCwd (where it remains
+ // remote-keyed and intentionally sticky).
return getConfiguredDefaultProjectDir()
}