feat(desktop): ⌘N/⌘T keep the focused session's project

resolveNewSessionCwd now inherits the focused chat's workspace when you
aren't drilled into a sidebar project, so a new tab or draft stays in
the same repo as the chat you were looking at.
This commit is contained in:
Brooklyn Nicholson
2026-08-01 02:32:28 -05:00
parent 3707741d9f
commit 883076ccd7
3 changed files with 137 additions and 9 deletions
@@ -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<SessionCreateResponse>('session.create', params)
const stored = created.stored_session_id
+65 -1
View File
@@ -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', () => {
+71 -7
View File
@@ -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}/`)