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 ce148f2970..6a7e568cba 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 @@ -482,7 +482,7 @@ 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/default) — never the primary composer's live cwd. + // (focused session's project → project scope → default). const params = await desktopSessionCreateParams((options?.cwd || resolveNewSessionCwd()).trim()) const created = await requestGateway('session.create', params) const stored = created.stored_session_id diff --git a/apps/desktop/src/store/projects.test.ts b/apps/desktop/src/store/projects.test.ts index 190c3d8348..18fab07eae 100644 --- a/apps/desktop/src/store/projects.test.ts +++ b/apps/desktop/src/store/projects.test.ts @@ -4,7 +4,12 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { NO_PROJECT_ID, type SidebarProjectTree } from '@/app/chat/sidebar/projects/workspace-groups' import { $sidebarAgentsGrouped } from '@/store/layout' import { $activeGatewayProfile } from '@/store/profile' -import { applyConfiguredDefaultProjectDir } from '@/store/session' +import { + $currentCwd, + $selectedStoredSessionId, + $sessions, + applyConfiguredDefaultProjectDir +} from '@/store/session' import { $activeProjectId, @@ -116,11 +121,20 @@ describe('resolveNewSessionCwd', () => { beforeEach(() => { $projectScope.set(ALL_PROJECTS) applyConfiguredDefaultProjectDir('/home/user/configured') + $currentCwd.set('') + $selectedStoredSessionId.set(null) + $sessions.set([]) + // Reset focused-session projections by clearing the inputs they read. + // $focusedStoredSessionId falls back to $selectedStoredSessionId. + // $focusedSessionState needs a runtime — leave it empty via no session states. }) afterEach(() => { applyConfiguredDefaultProjectDir(null) $projectScope.set(ALL_PROJECTS) + $currentCwd.set('') + $selectedStoredSessionId.set(null) + $sessions.set([]) }) it('starts a chat detached inside Home, ignoring the configured default dir', () => { @@ -134,6 +148,56 @@ describe('resolveNewSessionCwd', () => { it('still falls back to the configured default outside Home', () => { expect(resolveNewSessionCwd()).toBe('/home/user/configured') }) + + it('inherits the focused session workspace when not drilled into a project', () => { + // Simulate a primary session whose stored row carries a project cwd — + // the common case: you're in a chat that has a pwd, hit ⌘N/⌘T, and + // expect the new draft to stay in that project without sidebar drill-in. + $selectedStoredSessionId.set('sess-a') + $sessions.set([ + { + archived: false, + cwd: '/Users/me/www/hermes-agent', + ended_at: null, + id: 'sess-a', + input_tokens: 0, + is_active: true, + last_active: 0, + message_count: 1, + model: null, + output_tokens: 0, + started_at: 0, + title: 'work' + } as never + ]) + + expect(resolveNewSessionCwd()).toBe('/Users/me/www/hermes-agent') + }) + + it('does not re-attach a remembered cwd when the focused session is detached', () => { + $currentCwd.set('/Users/me/stale-remembered') + $selectedStoredSessionId.set('sess-detached') + $sessions.set([ + { + archived: false, + cwd: null, + ended_at: null, + id: 'sess-detached', + input_tokens: 0, + is_active: true, + last_active: 0, + message_count: 1, + model: null, + output_tokens: 0, + started_at: 0, + title: 'loose' + } as never + ]) + + // Focused session has no workspace → fall through to configured default, + // not the stale $currentCwd from an earlier chat. + expect(resolveNewSessionCwd()).toBe('/home/user/configured') + }) }) describe('projectNameForCwd', () => { diff --git a/apps/desktop/src/store/projects.ts b/apps/desktop/src/store/projects.ts index ca4511e618..2ba0d7e59b 100644 --- a/apps/desktop/src/store/projects.ts +++ b/apps/desktop/src/store/projects.ts @@ -16,7 +16,15 @@ import { $gateway, activeGateway, ensureActiveGatewayOpen } from '@/store/gatewa import { setSidebarAgentsGrouped } from '@/store/layout' import { notify } from '@/store/notifications' import { $activeGatewayProfile, requestFreshSession } from '@/store/profile' -import { $selectedStoredSessionId, $sessions, sessionMatchesStoredId, workspaceCwdForNewSession } from '@/store/session' +import { + $currentCwd, + $selectedStoredSessionId, + $sessions, + idsShareLineage, + sessionMatchesStoredId, + workspaceCwdForNewSession +} from '@/store/session' +import { $focusedSessionState, $focusedStoredSessionId } from '@/store/session-states' import type { ProjectInfo, ProjectsPayload } from '@/types/hermes' // First-class, per-profile Projects (named, multi-folder workspaces). State is @@ -191,12 +199,19 @@ export function goToProject(id: string, options?: { newSession?: boolean }): voi } } -// The cwd a NEW chat should start in. The "active project" is just an atom -// ($projectScope) — so when you're inside a project, a new session (cmd-n, the -// trunk "+") starts at that project's root (its primary repo = the default-branch -// checkout) instead of inheriting whatever unrelated worktree the live cwd -// drifted into. Outside a project it falls back to the plain default (detached), -// so a bare new chat shows no branch. +// The cwd a NEW chat should start in. +// +// Priority (first hit wins): +// 1. Explicit sidebar project scope (drilled into a project / Home bucket) +// 2. The FOCUSED session's workspace — so ⌘N / ⌘T from a chat that's already +// in a project/worktree stay there without requiring a sidebar drill-in +// 3. Configured default project dir / remote remembered cwd (detached otherwise) +// +// The "active project" is just an atom ($projectScope) — so when you're inside +// a project, a new session (cmd-n, the trunk "+") starts at that project's root +// (its primary repo = the default-branch checkout). Outside a project it used +// to fall straight to the plain default (detached), which dropped the workspace +// of the chat you were looking at — that's the case (2) covers. export function resolveNewSessionCwd(): string { const scope = $projectScope.get() @@ -214,9 +229,58 @@ export function resolveNewSessionCwd(): string { } } + // Inherit the focused chat's workspace. ⌘N/⌘T from a session that already + // has a project/pwd should stay there — drilling into the sidebar project + // is the uncommon path, not the requirement. + const focusedCwd = focusedSessionWorkspaceCwd() + + if (focusedCwd) { + return focusedCwd + } + return workspaceCwdForNewSession() } +/** Live workspace of the session the user is looking at (tile or primary). */ +function focusedSessionWorkspaceCwd(): string { + const focusedStoredId = $focusedStoredSessionId.get() + const state = $focusedSessionState.get() + + // Prefer the live runtime slice when it belongs to the focused chat + // (agent can relocate mid-turn). Cold tabs / mid-switch lag fall through + // to the stored session row. + const stateCwd = state?.cwd?.trim() || '' + const stateStoredId = state?.storedSessionId?.trim() || '' + + if ( + stateCwd && + (!focusedStoredId || + !stateStoredId || + stateStoredId === focusedStoredId || + idsShareLineage(focusedStoredId, stateStoredId, $sessions.get())) + ) { + return stateCwd + } + + if (focusedStoredId) { + const row = $sessions.get().find(s => sessionMatchesStoredId(s, focusedStoredId)) + const rowCwd = row?.cwd?.trim() || '' + + if (rowCwd) { + return rowCwd + } + + // Focused a real session with no workspace → stay detached. Do NOT fall + // through to `$currentCwd` (it may still hold a remembered path from an + // earlier chat and would re-attach a project the user left). + return '' + } + + // No focused stored session: primary draft. The composer atom is the draft's + // workspace target (set by startFreshSession / startSessionInWorkspace). + return $currentCwd.get().trim() +} + const underPath = (parent: string, child: string): boolean => child === parent || child.startsWith(parent.endsWith('/') ? parent : `${parent}/`)