Skip to content

The agent that grows with you

fix(webapp): the served profile keeps the folder an older build saved under default

unsupportedpastelsa3928dfca665
diff --git a/apps/desktop/src/lib/browser-profile-prefs.test.ts b/apps/desktop/src/lib/browser-profile-prefs.test.ts
index 4a295544799..c0713b9e0bd 100644
--- a/apps/desktop/src/lib/browser-profile-prefs.test.ts
+++ b/apps/desktop/src/lib/browser-profile-prefs.test.ts
@@ -9,10 +9,10 @@ import { applyConfiguredDefaultProjectDir } from '@/store/session'
import { dropTilesForProfile, migrateTilesForProfile } from '@/store/session-states'
import { createBrowserProfileBridge } from './browser-profile'
-import { browserDefaultProjectDirKey } from './browser-settings'
+import { browserDefaultProjectDirKey, createBrowserSettings } from './browser-settings'
import { browserStartupProfileKey } from './browser-startup-profile'
-type BootstrapWindow = Window & { __HERMES_BASE_PATH__?: string }
+type BootstrapWindow = Window & { __HERMES_BASE_PATH__?: string; __HERMES_DASHBOARD_PROFILE__?: string }
const win = window as BootstrapWindow
@@ -24,6 +24,7 @@ function host(basePath = '/hermes') {
function unhost() {
delete document.documentElement.dataset.hermesDesktopHost
delete win.__HERMES_BASE_PATH__
+ delete win.__HERMES_DASHBOARD_PROFILE__
}
function putFolder(profile: string, dir: string, basePath = '/hermes') {
@@ -255,3 +256,60 @@ describe('browser profile prefs follow rename and delete', () => {
expect($defaultProfileRoute.get()).toEqual({ connectionId: null, profile: 'alpha' })
})
})
+
+// An older build keyed the folder on the tab's named profile or 'default'. A tab
+// opened without `?profile=` names none, so on a gateway served as `alpha` the
+// folder alpha's tab chose sits under 'default'.
+describe('browser folder saved by an older build', () => {
+ const legacyKey = 'hermes:browser:default-project-dir:["/hermes","default"]'
+
+ it('moves the folder to the profile the gateway serves, once, so default does not inherit it', () => {
+ host()
+ win.__HERMES_DASHBOARD_PROFILE__ = 'alpha'
+ localStorage.setItem(legacyKey, '/srv/alpha')
+
+ expect(resolveNewSessionCwd('alpha')).toBe('/srv/alpha')
+ expect(resolveNewSessionCwd('default')).toBe('')
+ expect(localStorage.getItem(legacyKey)).toBeNull()
+
+ // Later choices are the new build's own: default's folder stays default's.
+ putFolder('default', '/srv/default')
+ localStorage.removeItem(browserDefaultProjectDirKey('/hermes', 'alpha'))
+
+ expect(resolveNewSessionCwd('alpha')).toBe('')
+ expect(resolveNewSessionCwd('default')).toBe('/srv/default')
+ })
+
+ it('shows the moved folder in Settings for a tab that names no profile', async () => {
+ host()
+ win.__HERMES_DASHBOARD_PROFILE__ = 'alpha'
+ localStorage.setItem(legacyKey, '/srv/alpha')
+
+ const settings = createBrowserSettings({ basePath: '/hermes', currentProfile: () => null })
+
+ await expect(settings.getDefaultProjectDir()).resolves.toMatchObject({ dir: '/srv/alpha' })
+ expect(localStorage.getItem(legacyKey)).toBeNull()
+ })
+
+ it('keeps a folder the served profile already chose and leaves the old one where it was', () => {
+ host()
+ win.__HERMES_DASHBOARD_PROFILE__ = 'alpha'
+ localStorage.setItem(legacyKey, '/srv/legacy')
+ putFolder('alpha', '/srv/alpha-own')
+
+ expect(resolveNewSessionCwd('alpha')).toBe('/srv/alpha-own')
+ expect(resolveNewSessionCwd('default')).toBe('/srv/legacy')
+ })
+
+ it('leaves a gateway served as default alone, and a later launch as alpha does not take its folder', () => {
+ host()
+ localStorage.setItem(legacyKey, '/srv/default')
+
+ expect(resolveNewSessionCwd('default')).toBe('/srv/default')
+
+ win.__HERMES_DASHBOARD_PROFILE__ = 'alpha'
+
+ expect(resolveNewSessionCwd('alpha')).toBe('')
+ expect(resolveNewSessionCwd('default')).toBe('/srv/default')
+ })
+})
diff --git a/apps/desktop/src/lib/browser-settings.ts b/apps/desktop/src/lib/browser-settings.ts
index baa04932c8e..d5b7be3d3e6 100644
--- a/apps/desktop/src/lib/browser-settings.ts
+++ b/apps/desktop/src/lib/browser-settings.ts
@@ -21,15 +21,54 @@ export function browserDefaultProjectDirKey(basePath: string, profile: string):
return `hermes:browser:default-project-dir:${JSON.stringify([basePath || '/', owner])}`
}
+/**
+ * An older build keyed the folder on the profile the tab named, else 'default'.
+ * A tab opened without `?profile=` names none, so on a gateway served as
+ * another profile the folder that profile's tab chose sits under 'default'.
+ * The first read on this build moves it to the served profile, unless that
+ * profile already has its own. The marker keeps a later launch as another
+ * profile from taking default's own folder.
+ */
+function adoptLegacyLaunchFolder(basePath: string): void {
+ const marker = `hermes:browser:default-project-dir-adopted:${JSON.stringify([basePath || '/'])}`
+
+ if (readKey(marker)) {
+ return
+ }
+
+ writeKey(marker, '1')
+
+ const launch = browserLaunchProfile()
+
+ if (!launch || launch === 'default') {
+ return
+ }
+
+ const legacyKey = browserDefaultProjectDirKey(basePath, 'default')
+ const ownKey = browserDefaultProjectDirKey(basePath, launch)
+ const legacy = readKey(legacyKey)?.trim()
+
+ if (legacy && !readKey(ownKey)?.trim()) {
+ writeKey(ownKey, legacy)
+ writeKey(legacyKey, null)
+ }
+}
+
+function folderKey(basePath: string, profile: string): string {
+ adoptLegacyLaunchFolder(basePath)
+
+ return browserDefaultProjectDirKey(basePath, profile)
+}
+
/** 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() || ''
+ return readKey(folderKey(basePath, profile))?.trim() || ''
}
/** Rename moves the folder with the profile, or the lack of one. */
export function migrateBrowserFolderPref(basePath: string, from: string, to: string): void {
- const sourceKey = browserDefaultProjectDirKey(basePath, from)
- const destKey = browserDefaultProjectDirKey(basePath, to)
+ const sourceKey = folderKey(basePath, from)
+ const destKey = folderKey(basePath, to)
if (sourceKey === destKey) {
return
@@ -40,7 +79,7 @@ export function migrateBrowserFolderPref(basePath: string, from: string, to: str
}
export function dropBrowserFolderPref(basePath: string, profile: string): void {
- writeKey(browserDefaultProjectDirKey(basePath, profile), null)
+ writeKey(folderKey(basePath, profile), null)
}
/**
@@ -56,7 +95,7 @@ export function createBrowserSettings({
basePath,
currentProfile
}: BrowserSettingsOptions): Window['hermesDesktop']['settings'] {
- const key = () => browserDefaultProjectDirKey(basePath, currentProfile() ?? '')
+ const key = () => folderKey(basePath, currentProfile() ?? '')
return {
getDefaultProjectDir: async () => ({