The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(webapp): read the startup profile one way and clear leftovers on create
Adolanium6f2ec72cf76b
diff --git a/apps/desktop/src/app/profiles/create-profile-dialog.test.tsx b/apps/desktop/src/app/profiles/create-profile-dialog.test.tsxindex 1587e91e573..ad99ecc82b7 100644--- a/apps/desktop/src/app/profiles/create-profile-dialog.test.tsx+++ b/apps/desktop/src/app/profiles/create-profile-dialog.test.tsx@@ -2,6 +2,9 @@import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react'import { afterEach, expect, it, vi } from 'vitest'+import { browserDefaultProjectDirKey } from '@/lib/browser-settings'+import { browserStartupProfileKey } from '@/lib/browser-startup-profile'+import { CreateProfileDialog } from './create-profile-dialog'const api = vi.hoisted(() => ({ createProfile: vi.fn(async () => ({})), updateProfileSoul: vi.fn(async () => ({})) }))@@ -34,3 +37,30 @@ it.each([true, false])(}})++it('does not let a new browser profile inherit the prefs a deleted one left under its name', async () => {+ const win = window as Window & { __HERMES_BASE_PATH__?: string }+ const folderKey = browserDefaultProjectDirKey('/hermes', 'work')+ const startupKey = browserStartupProfileKey('/hermes')+ window.document.documentElement.dataset.hermesDesktopHost = 'browser'+ win.__HERMES_BASE_PATH__ = '/hermes'+ localStorage.setItem(folderKey, '/srv/deleted-work')+ localStorage.setItem(startupKey, JSON.stringify({ connectionId: null, profile: 'work' }))+ localStorage.setItem(browserDefaultProjectDirKey('/hermes', 'alpha'), '/srv/alpha')++ try {+ const created = vi.fn()+ render(<CreateProfileDialog onClose={vi.fn()} onCreated={created} open />)+ fireEvent.change(screen.getByPlaceholderText('my-profile'), { target: { value: 'work' } })+ fireEvent.click(screen.getByRole('button', { name: 'Create profile' }))+ await waitFor(() => expect(created).toHaveBeenCalledWith('work'))++ expect(localStorage.getItem(folderKey)).toBeNull()+ expect(localStorage.getItem(startupKey)).toBeNull()+ expect(localStorage.getItem(browserDefaultProjectDirKey('/hermes', 'alpha'))).toBe('/srv/alpha')+ } finally {+ localStorage.clear()+ delete window.document.documentElement.dataset.hermesDesktopHost+ delete win.__HERMES_BASE_PATH__+ }+})diff --git a/apps/desktop/src/app/profiles/create-profile-dialog.tsx b/apps/desktop/src/app/profiles/create-profile-dialog.tsxindex b2197aa2199..a56cd6d6fe9 100644--- a/apps/desktop/src/app/profiles/create-profile-dialog.tsx+++ b/apps/desktop/src/app/profiles/create-profile-dialog.tsx@@ -16,6 +16,7 @@ import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@import { Textarea } from '@/components/ui/textarea'import { createProfile, updateProfileSoul } from '@/hermes'import { useI18n } from '@/i18n'+import { dropBrowserProfilePrefs } from '@/lib/browser-profile-prefs'import { AlertTriangle } from '@/lib/icons'import { slug } from '@/lib/sanitize'import type { ProfileInfo } from '@/types/hermes'@@ -78,6 +79,9 @@ export function CreateProfileDialog({try {const result = await createProfile({ name: trimmed, clone_from: cloneFrom, ...(soul.trim() ? { soul } : {}) })+ // A create never lands on a live profile's name, so a folder or startup profile stored under+ // it was left by a profile deleted before delete dropped them.+ dropBrowserProfilePrefs(trimmed)// Older remote backends do not accept an initial persona in the create request.if (soul.trim() && !result.soul_set) {diff --git a/apps/desktop/src/lib/browser-profile-prefs.test.ts b/apps/desktop/src/lib/browser-profile-prefs.test.tsindex 4903f03c477..4a295544799 100644--- a/apps/desktop/src/lib/browser-profile-prefs.test.ts+++ b/apps/desktop/src/lib/browser-profile-prefs.test.ts@@ -8,6 +8,7 @@ import { resolveNewSessionCwd } from '@/store/projects'import { applyConfiguredDefaultProjectDir } from '@/store/session'import { dropTilesForProfile, migrateTilesForProfile } from '@/store/session-states'+import { createBrowserProfileBridge } from './browser-profile'import { browserDefaultProjectDirKey } from './browser-settings'import { browserStartupProfileKey } from './browser-startup-profile'@@ -203,6 +204,26 @@ describe('browser profile prefs follow rename and delete', () => {expect(resolveNewSessionCwd('alpha')).toBe('')})+ it('leaves a stored startup route the profile bridge does not read as one', async () => {+ host()+ const bridge = createBrowserProfileBridge({ basePath: '/hermes', currentProfile: () => null, openWindow: async () => ({ ok: true }) })++ for (const stored of [+ { connectionId: 'remote-1', profile: 'work' },+ { connectionId: null, profile: ' work ' }+ ]) {+ const raw = JSON.stringify(stored)+ localStorage.setItem(browserStartupProfileKey('/hermes'), raw)+ await expect(bridge.profile.getDefault()).resolves.toBeNull()++ migrateTilesForProfile('work', 'renamed')+ expect(localStorage.getItem(browserStartupProfileKey('/hermes'))).toBe(raw)++ dropTilesForProfile('work')+ expect(localStorage.getItem(browserStartupProfileKey('/hermes'))).toBe(raw)+ }+ })+it('leaves a startup profile that names someone else', () => {host()putFolder('alpha', '/srv/alpha')diff --git a/apps/desktop/src/lib/browser-profile.ts b/apps/desktop/src/lib/browser-profile.tsindex 0f94cda0e75..7f516c1f848 100644--- a/apps/desktop/src/lib/browser-profile.ts+++ b/apps/desktop/src/lib/browser-profile.ts@@ -1,8 +1,6 @@import type { DesktopProfileRoute } from '@/global'-import { requireBrowserConnection } from '@/lib/browser-connection'-import { browserStartupProfileKey } from './browser-startup-profile'-import { readJson } from './storage'+import { browserStartupProfileKey, readBrowserStartupProfile, requireProfileRoute } from './browser-startup-profile'interface BrowserProfileOptions {basePath: string@@ -29,33 +27,7 @@ export function createBrowserProfileBridge({const storageKey = browserStartupProfileKey(basePath)const listeners = new Set<(route: DesktopProfileRoute | null) => void>()- const requireRoute = (value: unknown): DesktopProfileRoute => {- if (!value || typeof value !== 'object') {- throw new Error('A profile route is required.')- }-- const { connectionId, profile } = value as Record<string, unknown>-- if (typeof profile !== 'string' || !/^[a-z0-9][a-z0-9_-]{0,63}$/.test(profile)) {- throw new Error('Invalid profile name.')- }-- if (connectionId !== null && typeof connectionId !== 'string') {- throw new Error('Invalid connection id.')- }-- requireBrowserConnection(connectionId)-- return { connectionId, profile }- }-- const readDefault = (): DesktopProfileRoute | null => {- try {- return requireRoute(readJson(storageKey))- } catch {- return null- }- }+ const readDefault = (): DesktopProfileRoute | null => readBrowserStartupProfile(basePath)const remember = async (profile: string | null) => {const selected = profile?.trim() || null@@ -70,7 +42,7 @@ export function createBrowserProfileBridge({return {openWindow: async options => {const url = new URL(window.location.href)- setUrlProfile(url, options === undefined ? currentProfile() : requireRoute(options).profile)+ setUrlProfile(url, options === undefined ? currentProfile() : requireProfileRoute(options).profile)url.searchParams.delete('win')url.searchParams.delete('watch')@@ -82,7 +54,7 @@ export function createBrowserProfileBridge({get: async () => ({ profile: currentProfile() }),getDefault: async () => readDefault(),setDefault: async value => {- const route = requireRoute(value)+ const route = requireProfileRoute(value)window.localStorage.setItem(storageKey, JSON.stringify(route))listeners.forEach(listener => listener(route))diff --git a/apps/desktop/src/lib/browser-startup-profile.ts b/apps/desktop/src/lib/browser-startup-profile.tsindex 048e65753f8..051c3cbff43 100644--- a/apps/desktop/src/lib/browser-startup-profile.ts+++ b/apps/desktop/src/lib/browser-startup-profile.ts@@ -1,23 +1,41 @@import type { DesktopProfileRoute } from '@/global'+import { requireBrowserConnection } from './browser-connection'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))+/** A profile route this tab can apply: a valid profile name on its one connection. The profile+ * bridge and the rename/delete migrations read the stored startup route through this alone, so+ * they agree on which stored value is a startup profile. */+export function requireProfileRoute(value: unknown): DesktopProfileRoute {+ if (!value || typeof value !== 'object') {+ throw new Error('A profile route is required.')+ }- if (!value || typeof value.profile !== 'string' || !value.profile.trim()) {- return null+ const { connectionId, profile } = value as Record<string, unknown>++ if (typeof profile !== 'string' || !/^[a-z0-9][a-z0-9_-]{0,63}$/.test(profile)) {+ throw new Error('Invalid profile name.')}- if (value.connectionId !== null && typeof value.connectionId !== 'string') {- return null+ if (connectionId !== null && typeof connectionId !== 'string') {+ throw new Error('Invalid connection id.')}- return { connectionId: value.connectionId ?? null, profile: value.profile.trim() }+ requireBrowserConnection(connectionId)++ return { connectionId, profile }+}++function startupRoute(basePath: string): DesktopProfileRoute | null {+ try {+ return requireProfileRoute(readJson(browserStartupProfileKey(basePath)))+ } catch {+ return null+ }}export function readBrowserStartupProfile(basePath: string): DesktopProfileRoute | null {