Skip to content

The agent that grows with you

fix(webapp): keep folder prefs with the profile that owns them

Adolaniumcd4f55a593f4
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 75a4e608afb..cad56caf91f 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
@@ -1004,12 +1004,8 @@ export function useSessionActions({
const listed = options?.listed ?? true
try {
- // A tile anchored at a project path belongs to the profile the project
- // tree is rendered under — the same owner the fresh-draft "+" pins
- // (#79005). The occupied-chat "+" and project-row drags reach this
- // path instead, where a stale $newChatProfile pin would otherwise win
- // and land the session in the wrong profile (#124265). All-profiles
- // view has no owner and keeps the ordinary fallback.
+ // Project-path tiles take the tree's profile (#79005 / #124265). A stale
+ // $newChatProfile pin must not win for the occupied-chat "+" or a project drag.
const projectOwnerProfile =
options?.profile === undefined && typeof options?.cwd === 'string'
? (projectProfile() ?? undefined)
@@ -1065,7 +1061,11 @@ export function useSessionActions({
}
const cwd =
- options?.cwd === null ? '' : typeof options?.cwd === 'string' ? options.cwd.trim() : resolveNewSessionCwd()
+ options?.cwd === null
+ ? ''
+ : typeof options?.cwd === 'string'
+ ? options.cwd.trim()
+ : resolveNewSessionCwd(requestedProfile)
// #52589 provenance for the tile path: an explicitly-passed cwd is a
// deliberate workspace pick; a resolved default is inherited.
diff --git a/apps/desktop/src/app/settings/sessions-settings.tsx b/apps/desktop/src/app/settings/sessions-settings.tsx
index 359c8353b27..bd28e610041 100644
--- a/apps/desktop/src/app/settings/sessions-settings.tsx
+++ b/apps/desktop/src/app/settings/sessions-settings.tsx
@@ -1,3 +1,4 @@
+import { useStore } from '@nanostores/react'
import { useCallback, useEffect, useState } from 'react'
import { restoreListedSession } from '@/app/session/hooks/use-session-actions/utils'
@@ -22,7 +23,7 @@ import { purgeInFlightTurnJournals } from '@/lib/inflight-turn-journal'
import { isBrowserHostedDesktop } from '@/lib/platform'
import { confirm } from '@/store/confirm'
import { notify, notifyError } from '@/store/notifications'
-import { applyConfiguredDefaultProjectDir, ensureDefaultWorkspaceCwd } from '@/store/session'
+import { $connection, applyConfiguredDefaultProjectDir, ensureDefaultWorkspaceCwd } from '@/store/session'
import { untombstoneSessions } from '@/store/session-removal'
import { forgetSessionUnread } from '@/store/session-unread'
import { listEveryArchivedSession } from '@/store/sidebar-archive'
@@ -329,6 +330,7 @@ function AutoArchiveSetting() {
function DefaultProjectDirSetting() {
const { t } = useI18n()
const s = t.settings.sessions
+ const profile = useStore($connection)?.profile ?? ''
const [dir, setDir] = useState<null | string>(null)
const [fallback, setFallback] = useState<string>('')
const [busy, setBusy] = useState(false)
@@ -347,6 +349,13 @@ function DefaultProjectDirSetting() {
let alive = true
+ // The folder on screen belongs to the profile this effect loaded. Drop it
+ // on a switch so the next paint and the next save both use the profile
+ // that is open now.
+ if (isBrowserHostedDesktop()) {
+ setDir(null)
+ }
+
void settings.getDefaultProjectDir().then(result => {
if (!alive) {
return
@@ -360,7 +369,7 @@ function DefaultProjectDirSetting() {
return () => {
alive = false
}
- }, [])
+ }, [profile])
const choose = useCallback(async () => {
const settings = window.hermesDesktop?.settings
diff --git a/apps/desktop/src/lib/browser-profile-prefs.test.ts b/apps/desktop/src/lib/browser-profile-prefs.test.ts
new file mode 100644
index 00000000000..de175a3833a
--- /dev/null
+++ b/apps/desktop/src/lib/browser-profile-prefs.test.ts
@@ -0,0 +1,221 @@
+import { afterEach, beforeEach, describe, expect, it } from 'vitest'
+
+import { NO_PROJECT_ID } from '@/app/chat/sidebar/projects/workspace-groups'
+import { $defaultProfileRoute } from '@/store/default-profile'
+import { $activeGatewayProfile, $newChatProfile } from '@/store/profile'
+import { $projectScope, ALL_PROJECTS } from '@/store/project-scope'
+import { resolveNewSessionCwd } from '@/store/projects'
+import { applyConfiguredDefaultProjectDir } from '@/store/session'
+import { dropTilesForProfile, migrateTilesForProfile } from '@/store/session-states'
+
+import { browserDefaultProjectDirKey } from './browser-settings'
+import { browserStartupProfileKey } from './browser-startup-profile'
+
+type BootstrapWindow = Window & { __HERMES_BASE_PATH__?: string }
+
+const win = window as BootstrapWindow
+
+function host(basePath = '/hermes') {
+ document.documentElement.dataset.hermesDesktopHost = 'browser'
+ win.__HERMES_BASE_PATH__ = basePath
+}
+
+function unhost() {
+ delete document.documentElement.dataset.hermesDesktopHost
+ delete win.__HERMES_BASE_PATH__
+}
+
+function putFolder(profile: string, dir: string, basePath = '/hermes') {
+ localStorage.setItem(browserDefaultProjectDirKey(basePath, profile), dir)
+}
+
+function putStartup(profile: string, basePath = '/hermes', connectionId: null | string = null) {
+ localStorage.setItem(browserStartupProfileKey(basePath), JSON.stringify({ connectionId, profile }))
+}
+
+beforeEach(() => {
+ localStorage.clear()
+ unhost()
+ $projectScope.set(ALL_PROJECTS)
+ $newChatProfile.set(null)
+ $activeGatewayProfile.set('default')
+ $defaultProfileRoute.set(null)
+ applyConfiguredDefaultProjectDir('/cache/alpha')
+})
+
+afterEach(() => {
+ localStorage.clear()
+ unhost()
+ $projectScope.set(ALL_PROJECTS)
+ $newChatProfile.set(null)
+ $activeGatewayProfile.set('default')
+ $defaultProfileRoute.set(null)
+ applyConfiguredDefaultProjectDir(null)
+})
+
+describe('browser profile folder for a new chat', () => {
+ it('starts a chat or tile in profile B in B folder, and stays detached when B never chose one', () => {
+ expect(browserDefaultProjectDirKey('/hermes', 'beta')).toBe('hermes:browser:default-project-dir:["/hermes","beta"]')
+ host()
+ putFolder('alpha', '/srv/alpha')
+ putFolder('beta', '/srv/beta')
+ $newChatProfile.set('alpha')
+
+ expect(resolveNewSessionCwd()).toBe('/srv/alpha')
+ expect(resolveNewSessionCwd('beta')).toBe('/srv/beta')
+
+ localStorage.removeItem(browserDefaultProjectDirKey('/hermes', 'beta'))
+
+ expect(resolveNewSessionCwd('beta')).toBe('')
+ $newChatProfile.set('beta')
+ expect(resolveNewSessionCwd()).toBe('')
+ })
+
+ it('uses the active profile folder when the new-chat pin is empty', () => {
+ host()
+ putFolder('beta', '/srv/beta')
+ $activeGatewayProfile.set('beta')
+
+ expect(resolveNewSessionCwd()).toBe('/srv/beta')
+ })
+
+ it('keeps Home detached when the profile has a default folder', () => {
+ host()
+ putFolder('beta', '/srv/beta')
+ $newChatProfile.set('beta')
+ $projectScope.set(NO_PROJECT_ID)
+
+ expect(resolveNewSessionCwd()).toBe('')
+ })
+
+ it('keeps the process cache for a desktop new chat, including one aimed at a profile', () => {
+ putFolder('beta', '/srv/beta')
+ $newChatProfile.set('beta')
+
+ expect(resolveNewSessionCwd()).toBe('/cache/alpha')
+ expect(resolveNewSessionCwd('beta')).toBe('/cache/alpha')
+ })
+})
+
+describe('browser profile prefs follow rename and delete', () => {
+ it('moves the folder and startup profile with a rename and leaves every other pref', () => {
+ host()
+ putFolder('alpha', '/srv/alpha')
+ putFolder('beta', '/srv/beta')
+ putFolder('alpha', '/srv/other-alpha', '/other')
+ putStartup('alpha', '/hermes', 'local')
+ putStartup('alpha', '/other', 'remote-box')
+ $defaultProfileRoute.set({ connectionId: 'local', profile: 'alpha' })
+
+ migrateTilesForProfile('alpha', 'renamed')
+
+ expect(localStorage.getItem(browserDefaultProjectDirKey('/hermes', 'alpha'))).toBeNull()
+ expect(localStorage.getItem(browserDefaultProjectDirKey('/hermes', 'renamed'))).toBe('/srv/alpha')
+ expect(localStorage.getItem(browserDefaultProjectDirKey('/hermes', 'beta'))).toBe('/srv/beta')
+ expect(localStorage.getItem(browserDefaultProjectDirKey('/other', 'alpha'))).toBe('/srv/other-alpha')
+ expect(JSON.parse(localStorage.getItem(browserStartupProfileKey('/hermes'))!)).toEqual({
+ connectionId: 'local',
+ profile: 'renamed'
+ })
+ expect(JSON.parse(localStorage.getItem(browserStartupProfileKey('/other'))!)).toEqual({
+ connectionId: 'remote-box',
+ profile: 'alpha'
+ })
+ expect($defaultProfileRoute.get()).toEqual({ connectionId: 'local', profile: 'renamed' })
+ expect(resolveNewSessionCwd('renamed')).toBe('/srv/alpha')
+ expect(resolveNewSessionCwd('alpha')).toBe('')
+ })
+
+ it('keeps a folder the destination profile already chose', () => {
+ host()
+ putFolder('alpha', '/srv/alpha')
+ putFolder('renamed', '/srv/kept')
+
+ migrateTilesForProfile('alpha', 'renamed')
+
+ expect(localStorage.getItem(browserDefaultProjectDirKey('/hermes', 'renamed'))).toBe('/srv/kept')
+ expect(localStorage.getItem(browserDefaultProjectDirKey('/hermes', 'alpha'))).toBeNull()
+ })
+
+ it('drops a blank folder instead of carrying it to the new name', () => {
+ host()
+ putFolder('alpha', ' ')
+
+ migrateTilesForProfile('alpha', 'renamed')
+
+ expect(localStorage.getItem(browserDefaultProjectDirKey('/hermes', 'alpha'))).toBeNull()
+ expect(localStorage.getItem(browserDefaultProjectDirKey('/hermes', 'renamed'))).toBeNull()
+ })
+
+ it('rewrites startup storage when this window is not currently mirroring it', () => {
+ host()
+ putStartup('alpha')
+
+ migrateTilesForProfile('alpha', 'renamed')
+
+ expect(JSON.parse(localStorage.getItem(browserStartupProfileKey('/hermes'))!).profile).toBe('renamed')
+ expect($defaultProfileRoute.get()).toBeNull()
+ })
+
+ it('leaves another profile startup mirror alone while the renamed profile folder moves', () => {
+ host()
+ putFolder('alpha', '/srv/alpha')
+ putStartup('alpha')
+ $defaultProfileRoute.set({ connectionId: null, profile: 'beta' })
+
+ migrateTilesForProfile('alpha', 'renamed')
+
+ expect(localStorage.getItem(browserDefaultProjectDirKey('/hermes', 'renamed'))).toBe('/srv/alpha')
+ expect(JSON.parse(localStorage.getItem(browserStartupProfileKey('/hermes'))!).profile).toBe('renamed')
+ expect($defaultProfileRoute.get()).toEqual({ connectionId: null, profile: 'beta' })
+ })
+
+ it('drops the deleted profile folder and startup profile and leaves the others', () => {
+ host()
+ putFolder('alpha', '/srv/alpha')
+ putFolder('beta', '/srv/beta')
+ putStartup('alpha', '/hermes', 'local')
+ putStartup('alpha', '/other', 'remote-box')
+ $defaultProfileRoute.set({ connectionId: 'local', profile: 'alpha' })
+
+ dropTilesForProfile('alpha')
+
+ expect(localStorage.getItem(browserDefaultProjectDirKey('/hermes', 'alpha'))).toBeNull()
+ expect(localStorage.getItem(browserDefaultProjectDirKey('/hermes', 'beta'))).toBe('/srv/beta')
+ expect(localStorage.getItem(browserStartupProfileKey('/hermes'))).toBeNull()
+ expect(JSON.parse(localStorage.getItem(browserStartupProfileKey('/other'))!).profile).toBe('alpha')
+ expect($defaultProfileRoute.get()).toBeNull()
+ expect(resolveNewSessionCwd('alpha')).toBe('')
+ })
+
+ it('leaves a startup profile that names someone else', () => {
+ host()
+ putFolder('alpha', '/srv/alpha')
+ putStartup('beta', '/hermes', 'local')
+ $defaultProfileRoute.set({ connectionId: 'local', profile: 'beta' })
+
+ dropTilesForProfile('alpha')
+
+ expect(localStorage.getItem(browserDefaultProjectDirKey('/hermes', 'alpha'))).toBeNull()
+ expect(JSON.parse(localStorage.getItem(browserStartupProfileKey('/hermes'))!)).toEqual({
+ connectionId: 'local',
+ profile: 'beta'
+ })
+ expect($defaultProfileRoute.get()).toEqual({ connectionId: 'local', profile: 'beta' })
+ })
+
+ it('keeps folder and startup prefs when this page is not the browser host', () => {
+ win.__HERMES_BASE_PATH__ = '/hermes'
+ putFolder('alpha', '/srv/alpha')
+ putStartup('alpha')
+ $defaultProfileRoute.set({ connectionId: null, profile: 'alpha' })
+
+ migrateTilesForProfile('alpha', 'renamed')
+ dropTilesForProfile('alpha')
+
+ expect(localStorage.getItem(browserDefaultProjectDirKey('/hermes', 'alpha'))).toBe('/srv/alpha')
+ expect(localStorage.getItem(browserDefaultProjectDirKey('/hermes', 'renamed'))).toBeNull()
+ expect(JSON.parse(localStorage.getItem(browserStartupProfileKey('/hermes'))!).profile).toBe('alpha')
+ expect($defaultProfileRoute.get()).toEqual({ connectionId: null, profile: 'alpha' })
+ })
+})
diff --git a/apps/desktop/src/lib/browser-profile-prefs.ts b/apps/desktop/src/lib/browser-profile-prefs.ts
new file mode 100644
index 00000000000..084ce879911
--- /dev/null
+++ b/apps/desktop/src/lib/browser-profile-prefs.ts
@@ -0,0 +1,58 @@
+import { $defaultProfileRoute, noteDefaultProfileRoute } from '@/store/default-profile'
+
+import { browserPageBasePath, dropBrowserFolderPref, migrateBrowserFolderPref } from './browser-settings'
+import {
+ dropBrowserStartupProfile,
+ migrateBrowserStartupProfile,
+ readBrowserStartupProfile
+} from './browser-startup-profile'
+import { isBrowserHostedDesktop } from './platform'
+
+function hostedBasePath(): string | null {
+ return isBrowserHostedDesktop() ? browserPageBasePath() : null
+}
+
+function namesProfile(profile: string | undefined, target: string): boolean {
+ return (profile ?? '').trim() === target.trim()
+}
+
+export function migrateBrowserProfilePrefs(from: string, to: string): void {
+ const basePath = hostedBasePath()
+ const fromName = from.trim()
+ const toName = to.trim()
+
+ if (basePath === null || !fromName || !toName || fromName === toName) {
+ return
+ }
+
+ migrateBrowserFolderPref(basePath, fromName, toName)
+
+ const movedStartup = migrateBrowserStartupProfile(basePath, fromName, toName)
+ const current = $defaultProfileRoute.get()
+
+ if (!current || !namesProfile(current.profile, fromName)) {
+ return
+ }
+
+ // Same-window storage writes do not fire a storage event, so the mirror
+ // has to follow the profile this window was actually applying.
+ const saved = movedStartup ? readBrowserStartupProfile(basePath) : null
+
+ noteDefaultProfileRoute(saved ?? { ...current, profile: toName })
+}
+
+export function dropBrowserProfilePrefs(profile: string): void {
+ const basePath = hostedBasePath()
+ const name = profile.trim()
+
+ if (basePath === null || !name) {
+ return
+ }
+
+ dropBrowserFolderPref(basePath, name)
+ dropBrowserStartupProfile(basePath, name)
+
+ if (namesProfile($defaultProfileRoute.get()?.profile, name)) {
+ noteDefaultProfileRoute(null)
+ }
+}
diff --git a/apps/desktop/src/lib/browser-profile.ts b/apps/desktop/src/lib/browser-profile.ts
index 101a407c043..0f94cda0e75 100644
--- a/apps/desktop/src/lib/browser-profile.ts
+++ b/apps/desktop/src/lib/browser-profile.ts
@@ -1,6 +1,7 @@
import type { DesktopProfileRoute } from '@/global'
import { requireBrowserConnection } from '@/lib/browser-connection'
+import { browserStartupProfileKey } from './browser-startup-profile'
import { readJson } from './storage'
interface BrowserProfileOptions {
@@ -25,7 +26,7 @@ export function createBrowserProfileBridge({
openWindow
}: BrowserProfileOptions): Pick<Window['hermesDesktop'], 'openWindow' | 'profile'> {
// localStorage supplies origin isolation; the base path separates colocated servers.
- const storageKey = `hermes:webapp:default-profile:${basePath || '/'}`
+ const storageKey = browserStartupProfileKey(basePath)
const listeners = new Set<(route: DesktopProfileRoute | null) => void>()
const requireRoute = (value: unknown): DesktopProfileRoute => {
diff --git a/apps/desktop/src/lib/browser-settings.ts b/apps/desktop/src/lib/browser-settings.ts
index 87a93e32949..af862ebfc7d 100644
--- a/apps/desktop/src/lib/browser-settings.ts
+++ b/apps/desktop/src/lib/browser-settings.ts
@@ -1,10 +1,56 @@
-import { readKey } from './storage'
+import { normalizeBasePath } from '@hermes/shared'
+
+import type { BrowserHostGlobals } from './platform'
+import { readKey, writeKey } from './storage'
interface BrowserSettingsOptions {
basePath: string
currentProfile: () => null | string
}
+/** This page's hosted gateway, the same scope `createBrowserSettings` keys on. */
+export function browserPageBasePath(): string {
+ return normalizeBasePath((window as Window & BrowserHostGlobals).__HERMES_BASE_PATH__)
+}
+
+export function browserDefaultProjectDirKey(basePath: string, profile: string): string {
+ return `hermes:browser:default-project-dir:${JSON.stringify([basePath || '/', profile.trim() || 'default'])}`
+}
+
+/** The profile's own default folder, or '' when it never chose one. */
+export function readBrowserDefaultProjectDir(basePath: string, profile: string): string {
+ return readKey(browserDefaultProjectDirKey(basePath, profile))?.trim() || ''
+}
+
+/** Rename keeps the folder with the profile. A destination that already chose
+ * one keeps it; the old key is dropped either way so the name can be reused. */
+export function migrateBrowserFolderPref(basePath: string, from: string, to: string): void {
+ const sourceKey = browserDefaultProjectDirKey(basePath, from)
+ const destKey = browserDefaultProjectDirKey(basePath, to)
+
+ if (sourceKey === destKey) {
+ return
+ }
+
+ const source = readKey(sourceKey)
+
+ if (source === null) {
+ return
+ }
+
+ const next = source.trim()
+
+ if (next && readKey(destKey) === null) {
+ writeKey(destKey, next)
+ }
+
+ writeKey(sourceKey, null)
+}
+
+export function dropBrowserFolderPref(basePath: string, profile: string): void {
+ writeKey(browserDefaultProjectDirKey(basePath, profile), null)
+}
+
/**
* The default project folder for new chats, kept in this origin's storage.
*
@@ -18,8 +64,7 @@ export function createBrowserSettings({
basePath,
currentProfile
}: BrowserSettingsOptions): Window['hermesDesktop']['settings'] {
- const key = () =>
- `hermes:browser:default-project-dir:${JSON.stringify([basePath || '/', currentProfile()?.trim() || 'default'])}`
+ const key = () => browserDefaultProjectDirKey(basePath, currentProfile()?.trim() || 'default')
return {
getDefaultProjectDir: async () => ({
diff --git a/apps/desktop/src/lib/browser-startup-profile.ts b/apps/desktop/src/lib/browser-startup-profile.ts
new file mode 100644
index 00000000000..048e65753f8
--- /dev/null
+++ b/apps/desktop/src/lib/browser-startup-profile.ts
@@ -0,0 +1,56 @@
+import type { DesktopProfileRoute } from '@/global'
+
+import { readJson, writeJson } from './storage'
+
+export function browserStartupProfileKey(basePath: string): string {
+ return `hermes:webapp:default-profile:${basePath || '/'}`
+}
+
+function startupRoute(basePath: string): DesktopProfileRoute | null {
+ const value = readJson<Record<string, unknown>>(browserStartupProfileKey(basePath))
+
+ if (!value || typeof value.profile !== 'string' || !value.profile.trim()) {
+ return null
+ }
+
+ if (value.connectionId !== null && typeof value.connectionId !== 'string') {
+ return null
+ }
+
+ return { connectionId: value.connectionId ?? null, profile: value.profile.trim() }
+}
+
+export function readBrowserStartupProfile(basePath: string): DesktopProfileRoute | null {
+ return startupRoute(basePath)
+}
+
+function sameProfile(left: string, right: string): boolean {
+ return left.trim() === right.trim()
+}
+
+/** The tab's startup profile follows a rename. Returns whether this page's
+ * route named the old profile. Another profile's route is left alone. */
+export function migrateBrowserStartupProfile(basePath: string, from: string, to: string): boolean {
+ const route = startupRoute(basePath)
+
+ if (!route || !sameProfile(route.profile, from) || sameProfile(from, to)) {
+ return false
+ }
+
+ writeJson(browserStartupProfileKey(basePath), { ...route, profile: to.trim() })
+
+ return true
+}
+
+/** A deleted profile stops being the tab's startup profile. */
+export function dropBrowserStartupProfile(basePath: string, profile: string): boolean {
+ const route = startupRoute(basePath)
+
+ if (!route || !sameProfile(route.profile, profile)) {
+ return false
+ }
+
+ writeJson(browserStartupProfileKey(basePath), null)
+
+ return true
+}
diff --git a/apps/desktop/src/store/default-profile.ts b/apps/desktop/src/store/default-profile.ts
index 686fbbf1107..02a3fc3bc9e 100644
--- a/apps/desktop/src/store/default-profile.ts
+++ b/apps/desktop/src/store/default-profile.ts
@@ -50,6 +50,12 @@ export async function setDefaultProfile(route: DesktopProfileRoute): Promise<Des
return saved
}
+/** This window rewrote the startup route (profile rename or delete). */
+export function noteDefaultProfileRoute(route: DesktopProfileRoute | null): void {
+ revision += 1
+ publish(route)
+}
+
export function subscribeDefaultProfile(): (() => void) | undefined {
return window.hermesDesktop?.profile?.onDefaultChanged?.(route => {
revision += 1
diff --git a/apps/desktop/src/store/projects.ts b/apps/desktop/src/store/projects.ts
index 917f47ca2d5..06806a328e5 100644
--- a/apps/desktop/src/store/projects.ts
+++ b/apps/desktop/src/store/projects.ts
@@ -29,6 +29,7 @@ import { $sidebarShowAllSessions, setSidebarAgentsGrouped } from '@/store/layout
import { notify } from '@/store/notifications'
import {
$activeGatewayProfile,
+ $newChatProfile,
$profiles,
$profileScope,
ALL_PROFILES,
@@ -161,7 +162,7 @@ export function goToProject(id: string, options?: { newSession?: boolean }): voi
// you were looking at: after a restart that's the just-resumed session, whose
// stored cwd is often a home-dir fallback, so every new chat landed there
// instead of the configured default (#71873, #80213, #77496).
-export function resolveNewSessionCwd(): string {
+export function resolveNewSessionCwd(profile?: string): string {
const scope = $projectScope.get()
// Inside Home, "no folder" is the point: a new chat must stay detached rather
@@ -178,7 +179,11 @@ export function resolveNewSessionCwd(): string {
}
}
- return workspaceCwdForNewSession()
+ // A tile names its profile before $newChatProfile catches up. The folder
+ // belongs to that profile, which on a browser host is its own stored default.
+ const owner = (profile?.trim() || $newChatProfile.get() || $activeGatewayProfile.get()).trim()
+
+ return workspaceCwdForNewSession(owner)
}
// Entering a project moves the live workspace only when main holds a fresh
diff --git a/apps/desktop/src/store/session-states.ts b/apps/desktop/src/store/session-states.ts
index c5b38a21bce..ae8d5a908a7 100644
--- a/apps/desktop/src/store/session-states.ts
+++ b/apps/desktop/src/store/session-states.ts
@@ -33,6 +33,7 @@ import {
} from '@/components/pane-shell/tree/store'
import { resolveRememberedActivePane, workspaceScopeKey } from '@/components/pane-shell/workspace-scope'
import type { WorkspaceMode } from '@/contrib/types'
+import { dropBrowserProfilePrefs, migrateBrowserProfilePrefs } from '@/lib/browser-profile-prefs'
import { finalizeInterruptedMessages, sealOpenToolParts } from '@/lib/chat-messages'
import { tileFocusStampOnFocusChange } from '@/lib/session-timer-since'
import { stableArray } from '@/lib/stable-array'
@@ -2832,9 +2833,8 @@ export function dropTilesForProfile(
}
persistTiles()
- // The rail is a profile-keyed family too: a deleted profile's tabs must not
- // outlive it, or a later profile of the same name inherits them.
dropPreviewTabsForProfile(name)
+ dropBrowserProfilePrefs(name)
}
/**
@@ -2898,9 +2898,8 @@ export function migrateTilesForProfile(oldProfile: string, newProfile: string):
migrateSessionOwnerHintsForProfile(from, to)
migratePreviewArtifactsForProfile(from, to)
migrateStatusDrawersForProfile(from, to)
- // Sibling family: the rail's profile-keyed buckets move with the rename, or
- // the renamed profile opens with an empty rail and the old name keeps them.
migratePreviewTabsForProfile(from, to)
+ migrateBrowserProfilePrefs(from, to)
}
/** ⌘⇧T — reopen the most recently closed tab where it was, then focus it.
diff --git a/apps/desktop/src/store/session.ts b/apps/desktop/src/store/session.ts
index 642b19836d0..b23672914f7 100644
--- a/apps/desktop/src/store/session.ts
+++ b/apps/desktop/src/store/session.ts
@@ -5,12 +5,14 @@ import { setApiRequestLocalMode } from '@/api/client'
import { lastVisibleMessageIsUser } from '@/app/chat/thread-loading'
import type { ContextSuggestion } from '@/app/types'
import type { HermesConnection } from '@/global'
+import { browserPageBasePath, readBrowserDefaultProjectDir } from '@/lib/browser-settings'
import type { ChatMessage } from '@/lib/chat-messages'
import {
activeConnectionScopeSuffix,
connectionScopeSuffix,
rescopeConnectionScopedStores
} from '@/lib/connection-scoped'
+import { isBrowserHostedDesktop } from '@/lib/platform'
import { isMessagingSource } from '@/lib/session-source'
import type { TileSessionFocusStamp } from '@/lib/session-timer-since'
import { persistBoolean, persistString, readJson, storedBoolean, storedString, writeJson } from '@/lib/storage'
@@ -1851,7 +1853,7 @@ export const $currentCwdExplicit = atom(false)
export const setCurrentCwdExplicit = (next: Updater<boolean>) => updateAtom($currentCwdExplicit, next)
-export const workspaceCwdForNewSession = (): string => {
+export const workspaceCwdForNewSession = (profile?: string): string => {
// 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.
@@ -1865,6 +1867,17 @@ export const workspaceCwdForNewSession = (): string => {
// the wrong workspace was the #57911 symptom. Resume/restore still reads
// the remembered cwd via ensureDefaultWorkspaceCwd (where it remains
// remote-keyed and intentionally sticky).
+ //
+ // A browser host stores that default folder per profile. The process cache
+ // was last filled for whichever profile the window synced, so a chat aimed
+ // at another profile reads that profile's key and stays detached when it
+ // never chose one.
+ const owner = profile?.trim()
+
+ if (owner && isBrowserHostedDesktop()) {
+ return readBrowserDefaultProjectDir(browserPageBasePath(), owner)
+ }
+
return getConfiguredDefaultProjectDir()
}