Skip to content

The agent that grows with you

fix(desktop): remember the workspace you picked, not the one you looked at

Brooklyn Nicholson9050913e68ca
diff --git a/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event.ts b/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event.ts
index 2389b17ca1a..ee13f485219 100644
--- a/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event.ts
+++ b/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event.ts
@@ -51,7 +51,7 @@ import {
$sessions,
sessionMatchesStoredId,
setCurrentBranch,
- setCurrentCwd,
+ setCurrentCwdTransient,
setCurrentFastMode,
setCurrentPersonality,
setCurrentReasoningEffort,
@@ -446,7 +446,7 @@ export function useGatewayEventHandler(deps: GatewayEventDeps) {
const sameSession = !!sessionId && sessionId === lastCwdInfoSessionRef.current
lastCwdInfoSessionRef.current = sessionId
- setCurrentCwd(payload.cwd)
+ setCurrentCwdTransient(payload.cwd)
// The backend just confirmed the selected conversation's real
// workspace, so it owns the path we wrote. Without the claim the
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 bfa29501510..2ae36bd4c23 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
@@ -344,7 +344,9 @@ export function useSessionActions({
if (!hasWorkspaceTarget) {
// In a project → the repo's default-branch checkout; not in a project →
// detached. So cmd-n does not inherit an unrelated linked worktree.
- setCurrentCwd(resolveNewSessionCwd())
+ // Transient: a resolved default is not the user naming a workspace, and
+ // remembering it here would make the NEXT new chat inherit it.
+ setCurrentCwdTransient(resolveNewSessionCwd())
} else if (workspaceTarget === null) {
setCurrentCwdTransient('')
} else if (typeof workspaceTarget === 'string') {
@@ -533,7 +535,7 @@ export function useSessionActions({
patchSessionTile(stored, { runtimeId: created.session_id })
if (dir === 'center' && runtimeInfo?.cwd) {
- setCurrentCwd(runtimeInfo.cwd)
+ setCurrentCwdTransient(runtimeInfo.cwd)
setWorkspaceCwdOwner(stored)
}
@@ -716,7 +718,7 @@ export function useSessionActions({
setActiveSessionId(cachedRuntimeId)
activeSessionIdRef.current = cachedRuntimeId
syncSessionStateToView(cachedRuntimeId, cachedViewState)
- setCurrentCwd(cachedViewState.cwd)
+ setCurrentCwdTransient(cachedViewState.cwd)
// The warm cache IS this conversation's own workspace truth, so the
// switch is already re-homed here. This claim cannot wait for
// `session.activate`: its missing-RPC compat branch returns before
diff --git a/apps/desktop/src/app/session/hooks/use-session-actions/utils.ts b/apps/desktop/src/app/session/hooks/use-session-actions/utils.ts
index 4648ca5ff11..29e8d105dd9 100644
--- a/apps/desktop/src/app/session/hooks/use-session-actions/utils.ts
+++ b/apps/desktop/src/app/session/hooks/use-session-actions/utils.ts
@@ -13,7 +13,7 @@ import {
releaseWorkspaceCwdOwner,
sessionMatchesStoredId,
setCurrentBranch,
- setCurrentCwd,
+ setCurrentCwdTransient,
setCurrentFastMode,
setCurrentModel,
setCurrentPersonality,
@@ -968,9 +968,8 @@ function publishRuntimeToComposer(state: SessionRuntimeStatePatch): void {
commitWorkspaceCwdForSelectedSession(state.cwd)
} else {
// A detached session: the path on screen is provably still the previous
- // conversation's. Release rather than write `''` — `setCurrentCwd`
- // persists, so blanking here would also wipe the remembered workspace
- // that seeds `$currentCwd` on next boot.
+ // conversation's. Release rather than write `''` — clearing it collapses
+ // the workspace/review panes on every switch.
releaseWorkspaceCwdOwner()
}
}
@@ -1098,7 +1097,7 @@ export function applyStoredSessionPreviewRuntimeInfo(
const storedCwd = stored?.cwd?.trim() || ''
if (storedCwd) {
- setCurrentCwd(storedCwd)
+ setCurrentCwdTransient(storedCwd)
setWorkspaceCwdOwner(storedSessionId)
} else {
// Either a genuinely detached session, or a row outside the loaded sidebar
diff --git a/apps/desktop/src/store/session.test.ts b/apps/desktop/src/store/session.test.ts
index 891795821ca..5bc19f5f786 100644
--- a/apps/desktop/src/store/session.test.ts
+++ b/apps/desktop/src/store/session.test.ts
@@ -13,6 +13,7 @@ import {
$unreadFinishedSessionIds,
_resetLegacyDiscardForTests,
applyConfiguredDefaultProjectDir,
+ commitWorkspaceCwdForSelectedSession,
getRememberedRoute,
getRememberedSessionId,
mergeSessionPage,
@@ -21,6 +22,7 @@ import {
sessionBelongsToProfile,
sessionPinId,
setCurrentCwd,
+ setCurrentCwdTransient,
setRememberedRoute,
setRememberedSessionId,
setSelectedStoredSessionId,
@@ -371,6 +373,34 @@ describe('workspaceCwdForNewSession', () => {
$connection.set(null)
expect(workspaceCwdForNewSession()).toBe('')
})
+
+ 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.
+ $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')
+ })
+
+ it('settling a resumed session does not move where the next new chat starts', () => {
+ // 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
+ // that folder as the user's chosen workspace.
+ $connection.set({ baseUrl: 'http://backend-a', mode: 'remote' } as never)
+ setCurrentCwd('/backend/picked')
+
+ setSelectedStoredSessionId('sess-in-project')
+ commitWorkspaceCwdForSelectedSession('/backend/last-project')
+
+ expect(workspaceCwdForNewSession()).toBe('/backend/picked')
+ })
})
function makeState(over: Partial<ClientSessionState> = {}): ClientSessionState {
diff --git a/apps/desktop/src/store/session.ts b/apps/desktop/src/store/session.ts
index 953745f8112..cb84c2c9c6e 100644
--- a/apps/desktop/src/store/session.ts
+++ b/apps/desktop/src/store/session.ts
@@ -197,9 +197,12 @@ export async function ensureDefaultWorkspaceCwd(): Promise<void> {
await syncConfiguredDefaultProjectDir()
const configured = getConfiguredDefaultProjectDir()
+ // Transient: each source below is already remembered or comes from config, so
+ // persisting would only promote a configured default into the per-backend
+ // memory of what the user picked.
const seedLiveCwd = (cwd: string) => {
if (cwd && !$activeSessionId.get()) {
- setCurrentCwd(cwd)
+ setCurrentCwdTransient(cwd)
}
}
@@ -649,6 +652,14 @@ export const setCurrentFastMode = (next: Updater<boolean>) => {
export const setYoloActive = (next: Updater<boolean>) => updateAtom($yoloActive, next)
+/** Move the live workspace AND remember it as this backend's workspace.
+ *
+ * Only for a path the user chose — a folder pick, a project/worktree entry, an
+ * explicit workspace target. The remembered value is where a new chat starts on
+ * a remote backend, so writing it from a path the user merely *looked at* makes
+ * every new chat land in the last session's folder (#77496, #80213). To follow
+ * a conversation's cwd, use `setCurrentCwdTransient`.
+ */
export const setCurrentCwd = (next: Updater<string>) => {
updateAtom($currentCwd, next)
persistString(workspaceCwdKey(), $currentCwd.get().trim() || null)
@@ -656,6 +667,11 @@ export const setCurrentCwd = (next: Updater<string>) => {
export const setTerminalBackend = (next: Updater<string>) => updateAtom($terminalBackend, next)
+/** Move the live workspace without claiming it as the user's chosen one.
+ *
+ * For paths that come from a conversation rather than from the user: resume
+ * settling, a warm switch, the agent relocating mid-turn, detaching a draft.
+ */
export const setCurrentCwdTransient = (next: Updater<string>) => updateAtom($currentCwd, next)
// Released-ownership marker: the live path belongs to no conversation. `null`
@@ -688,12 +704,12 @@ export const releaseWorkspaceCwdOwner = () => updateAtom($workspaceCwdOwner, WOR
*
* The single primitive for "this path IS the selected conversation's" — a folder
* pick, a project entry, the agent relocating itself. Prefer it over a bare
- * `setCurrentCwd`, which moves the path while leaving ownership naming whatever
- * held it before; workspace-derived slices then stay hidden even though the
- * path is correct (#71254).
+ * `setCurrentCwdTransient`, which moves the path while leaving ownership naming
+ * whatever held it before; workspace-derived slices then stay hidden even though
+ * the path is correct (#71254).
*/
export const commitWorkspaceCwdForSelectedSession = (cwd: string) => {
- setCurrentCwd(cwd)
+ setCurrentCwdTransient(cwd)
setWorkspaceCwdOwner($selectedStoredSessionId.get())
}