From f3e0cf098b974fb603257a38d276f7dfafef822d Mon Sep 17 00:00:00 2001 From: xxxigm Date: Mon, 24 Aug 2026 19:19:45 +0700 Subject: [PATCH] fix(desktop): keep Home new sessions detached from the last project Home's "+" passes path/cwd null on purpose, but null was falsy and fell through into resolveNewSessionCwd(), so "New session in Home" (especially the openTab path while main chat is occupied) still created under the previous project folder and showed its branch. --- .../hooks/use-session-actions/index.ts | 23 +++++++++++++++---- .../app/session/workspace-session-target.ts | 20 ++++++++++++---- 2 files changed, 33 insertions(+), 10 deletions(-) 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 cf913713f0..727242428e 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 @@ -2,6 +2,7 @@ import { useStore } from '@nanostores/react' import { type MutableRefObject, useCallback, useEffect, useRef } from 'react' import type { NavigateFunction } from 'react-router' +import { NO_PROJECT_ID } from '@/app/chat/sidebar/projects/workspace-groups' import { graftRefreshedTailOntoBackfill } from '@/app/chat/transcript-backfill' import { revealTreePane } from '@/components/pane-shell/tree/store' import { setWorkspaceScope } from '@/components/pane-shell/workspace-scope' @@ -30,6 +31,7 @@ import { normalizeProfileKey } from '@/store/profile' import { + $projectScope, beginSessionMutation, endSessionMutation, resolveNewSessionCwd, @@ -470,10 +472,13 @@ export function useSessionActions({ // An explicit one-shot workspace target (null → detached, string → that // folder) wins; otherwise the live cwd, then the project-aware default // (resolveNewSessionCwd — a project's new session keeps its repo cwd). + // Home is an explicit detached scope: do not let a stale live cwd from + // the previously selected project leak into this new session (#84220). const workspaceTarget = $newChatWorkspaceTarget.get() + const homeScope = $projectScope.get() === NO_PROJECT_ID const cwd = - workspaceTarget === null + workspaceTarget === null || (workspaceTarget === undefined && homeScope) ? '' : typeof workspaceTarget === 'string' ? workspaceTarget.trim() @@ -612,16 +617,24 @@ export function useSessionActions({ try { // Fresh tile → the caller's workspace when one was named (the sidebar - // "+" on a project/worktree lane), else the resolved new-session cwd - // (project scope → configured default). + // "+" on a project/worktree lane), explicit null means Home/detached, + // else the resolved new-session cwd (project scope → configured default). + // `options?.cwd || resolve…` is wrong for Home: null is falsy and used + // to fall through into the last project folder while main chat was + // occupied (openTab path for "New session in Home"). const capturedRoute = options?.route === undefined ? $newChatRoute.get() : options.route const workspaceScope = options?.workspaceScope ?? { workspaceMode: 'sessions' } + const cwd = + options?.cwd === null + ? '' + : typeof options?.cwd === 'string' + ? options.cwd.trim() + : resolveNewSessionCwd() const params = { - ...(await desktopSessionCreateParams((options?.cwd || resolveNewSessionCwd()).trim(), capturedRoute)), + ...(await desktopSessionCreateParams(cwd, capturedRoute)), ...(workspaceScope.workspaceMode === 'bots' ? { hidden: true } : {}) } - const created = capturedRoute ? await requestGatewayForAgent( capturedRoute.connectionId, diff --git a/apps/desktop/src/app/session/workspace-session-target.ts b/apps/desktop/src/app/session/workspace-session-target.ts index da5028502a..d41aad375f 100644 --- a/apps/desktop/src/app/session/workspace-session-target.ts +++ b/apps/desktop/src/app/session/workspace-session-target.ts @@ -3,6 +3,7 @@ import type { MutableRefObject } from 'react' import { followActiveSessionCwd, resolveNewSessionCwd } from '@/store/projects' import { $newChatWorkspaceTargetGeneration, + type NewChatWorkspaceTarget, setCurrentBranch, setCurrentCwd, setNewChatWorkspaceTarget @@ -14,7 +15,7 @@ interface WorkspaceSessionOptions { onExplicitWorkspace?: (cwd: string) => void path: null | string requestGateway: (method: string, params?: Record) => Promise - startFreshSessionDraft: (options?: { workspaceTarget: string }) => void + startFreshSessionDraft: (options?: { workspaceTarget: NewChatWorkspaceTarget }) => void } export function startWorkspaceSession({ @@ -25,13 +26,22 @@ export function startWorkspaceSession({ requestGateway, startFreshSessionDraft }: WorkspaceSessionOptions): void { - // A worktree lane carries its own path; a project trunk can be path-less, so - // fall back to the active project's root for that existing controller path. - const explicitTarget = path?.trim() + // Home's "+" passes path=null on purpose ("no folder"). That must stay + // detached — do NOT fall through to resolveNewSessionCwd(), which can still + // return a default/remembered project folder and re-attach the last repo + // (digitwo: New session in Home still shows `main`). + if (path === null) { + startFreshSessionDraft({ workspaceTarget: null }) + + return + } + + // A worktree lane carries its own path. Empty string (legacy/path-less trunk) + // can fall back to the active project's root, but null was handled above. + const explicitTarget = path.trim() const target = explicitTarget || resolveNewSessionCwd() startFreshSessionDraft(target ? { workspaceTarget: target } : undefined) - if (!target) { return }