Skip to content

The agent that grows with you

fix(desktop): drop persisted tiles of deleted profiles so bots cannot resurrect

Finn763ae6d3880ae22
diff --git a/apps/desktop/src/app/profiles/delete-profile-dialog.tsx b/apps/desktop/src/app/profiles/delete-profile-dialog.tsx
index dd6f43640df..765adad7b2e 100644
--- a/apps/desktop/src/app/profiles/delete-profile-dialog.tsx
+++ b/apps/desktop/src/app/profiles/delete-profile-dialog.tsx
@@ -4,6 +4,7 @@ import { deleteProfile } from '@/hermes'
import { useI18n } from '@/i18n'
import { retireLocalProfileGateways } from '@/store/gateway'
import { $activeGatewayProfile, normalizeProfileKey, selectProfile, setActiveProfile } from '@/store/profile'
+import { dropTilesForProfile } from '@/store/session-states'
// Thin wrapper over ConfirmDialog: owns the deleteProfile call, inherits
// Enter-to-confirm + busy/done/error from the shared dialog. The single choke
@@ -69,6 +70,11 @@ export function DeleteProfileDialog({
// Legacy arity when unscoped: callers and tests pin the one-arg call.
await (remote ? deleteProfile(profile.name, scope) : deleteProfile(profile.name))
+ // The profile is gone. Drop its persisted tiles now — a leftover
+ // session/Bot tile restores on relaunch and dials the deleted
+ // profile's backend, whose ensure_hermes_home() re-creates the
+ // directory the delete just removed (hermes-agent#94235).
+ dropTilesForProfile(profile.name)
await onDeleted?.()
if (wasActive) {
diff --git a/apps/desktop/src/sdk/index.ts b/apps/desktop/src/sdk/index.ts
index c552934e52a..89700a06251 100644
--- a/apps/desktop/src/sdk/index.ts
+++ b/apps/desktop/src/sdk/index.ts
@@ -91,6 +91,7 @@ import {
$focusedStoredSessionId,
$sessionStates,
$sessionTiles,
+ dropTilesForProfile,
focusWorkspaceOwnerSessionTile,
sessionTileDelegate
} from '@/store/session-states'
@@ -720,6 +721,17 @@ export const host = {
: undefined
)
+ // The profile is gone. Drop its persisted tiles NOW: a leftover Bot Mode
+ // tile restores on relaunch and dials the deleted profile's backend, whose
+ // ensure_hermes_home() re-creates the profile directory the delete just
+ // removed (hermes-agent#94235).
+ dropTilesForProfile(
+ route ? route.profile : name,
+ route
+ ? { connectionId: route.connectionId, profile: route.profile, targetProfile: route.targetProfile }
+ : undefined
+ )
+
// The profile rail paints from the shared $profiles cache; without a
// refresh the deleted profile's badge survives and clicking it starts a
// doomed spawn-retry loop against Electron's deletion guard (#88769).
diff --git a/apps/desktop/src/sdk/profile-routing.test.ts b/apps/desktop/src/sdk/profile-routing.test.ts
index fd9227abf4c..8b1a8f95c3d 100644
--- a/apps/desktop/src/sdk/profile-routing.test.ts
+++ b/apps/desktop/src/sdk/profile-routing.test.ts
@@ -45,7 +45,8 @@ vi.mock('@/store/session-states', async () => {
$focusedStoredSessionId: atom(null),
$sessionTiles: atom([]),
$sessionStates: atom({}),
- sessionTileDelegate: vi.fn(() => null)
+ sessionTileDelegate: vi.fn(() => null),
+ dropTilesForProfile: vi.fn()
}
})
vi.mock('@/store/profile', async () => {
@@ -137,6 +138,7 @@ const {
$sessionTiles,
sessionTileDelegate
} = await import('@/store/session-states')
+const { dropTilesForProfile } = await import('@/store/session-states')
const { setWorkspaceScope } = await import('@/components/pane-shell/workspace-scope')
@@ -201,6 +203,9 @@ describe('connection-aware plugin host APIs', () => {
// The rail paints from $profiles; skipping the refresh leaves a stale
// badge whose click hot-loops against the deletion guard (#88769).
expect(refreshProfiles).toHaveBeenCalled()
+ // A leftover Bot Mode tile would restore on relaunch and dial the deleted
+ // profile's backend, re-creating its HERMES_HOME (#94235).
+ expect(dropTilesForProfile).toHaveBeenCalledWith('worker', undefined)
})
it('pins an ambient SSH profile delete to the active connection and target profile', async () => {
@@ -355,6 +360,11 @@ describe('connection-aware plugin host APIs', () => {
profile: 'worker'
})
expect(retireLocalProfileGateways).not.toHaveBeenCalled()
+ expect(dropTilesForProfile).toHaveBeenCalledWith('worker', {
+ connectionId: 'source-a',
+ profile: 'worker',
+ targetProfile: 'backend-worker'
+ })
})
it('rejects a remote deletion route without a connection id', async () => {
@@ -401,6 +411,11 @@ describe('connection-aware plugin host APIs', () => {
connectionId: 'source-local',
profile: 'worker'
})
+ expect(dropTilesForProfile).toHaveBeenCalledWith('worker', {
+ connectionId: 'source-local',
+ profile: 'worker',
+ targetProfile: 'backend-worker'
+ })
})
it('keeps the profile-only request overload as a legacy fallback', async () => {
diff --git a/apps/desktop/src/store/session-states.test.ts b/apps/desktop/src/store/session-states.test.ts
index bd08bff2d70..c469254f13d 100644
--- a/apps/desktop/src/store/session-states.test.ts
+++ b/apps/desktop/src/store/session-states.test.ts
@@ -20,6 +20,7 @@ import {
blankDraftTile,
clearAllSessionStates,
closeAllOpenSessionTiles,
+ dropTilesForProfile,
focusedSessionNeedsRoute,
focusOpenSession,
focusWorkspaceOwnerSessionTile,
@@ -401,7 +402,6 @@ describe('closeAllOpenSessionTiles persists Bot Mode Close All (#94137)', () =>
$layoutTree.set(null)
$sessionTiles.set([])
})
-
it('drops persisted bot tiles so a roster/profile rehydrate cannot restore them', () => {
openSessionTile('chat-a', 'center', 'workspace', undefined, {
workspaceMode: 'bots',
@@ -412,11 +412,8 @@ describe('closeAllOpenSessionTiles persists Bot Mode Close All (#94137)', () =>
workspaceOwnerKey: 'bot:b'
})
$layoutTree.set(group(['workspace', tilePane('chat-a'), tilePane('chat-b')], { active: 'workspace', id: 'main' }))
-
closeAllOpenSessionTiles('workspace')
-
expect($sessionTiles.get()).toEqual([])
-
// Profile swap re-reads the shared Bot bucket. Close All must have
// emptied it, not only dismissed the tree panes.
$activeGatewayProfile.set('other-profile')
@@ -424,7 +421,6 @@ describe('closeAllOpenSessionTiles persists Bot Mode Close All (#94137)', () =>
$activeGatewayProfile.set('default')
expect($sessionTiles.get()).toEqual([])
})
-
it('leaves session tiles stacked in a different zone open', () => {
openSessionTile('keep', 'right', 'workspace')
openSessionTile('close-me', 'center', 'workspace')
@@ -434,10 +430,105 @@ describe('closeAllOpenSessionTiles persists Bot Mode Close All (#94137)', () =>
group([tilePane('keep')], { active: tilePane('keep'), id: 'right' })
])
)
-
closeAllOpenSessionTiles('workspace')
-
expect($sessionTiles.get().map(tile => tile.storedSessionId)).toEqual(['keep'])
+
+describe('dropTilesForProfile', () => {
+ const TILES_KEY = 'hermes.desktop.sessionTiles.v2'
+ const BOTS_BUCKET = '__bots_workspace__'
+ const storedTiles = (): Record<string, unknown> => {
+ const raw = window.localStorage.getItem(TILES_KEY)
+ return raw ? (JSON.parse(raw) as Record<string, unknown>) : {}
+ }
+ // The tile buckets are module-private and other suites leave persisted
+ // entries behind, so each test re-imports a FRESH session-states module
+ // (empty tilesByProfile + empty storage) instead of fighting the residue —
+ // the same isolation the browser gets between app launches.
+ type SessionStates = typeof import('@/store/session-states')
+ let mod: SessionStates
+ let activeGatewayProfile: { set: (name: string) => void }
+ beforeEach(async () => {
+ window.localStorage.clear()
+ vi.resetModules()
+ // Re-import the module graph so the private tile buckets start empty; the
+ // profile atom session-states subscribes to is the freshly loaded one too.
+ mod = await import('@/store/session-states')
+ const profile = await import('@/store/profile')
+ activeGatewayProfile = profile.$activeGatewayProfile
+ activeGatewayProfile.set('default')
+ mod.$sessionTiles.set([])
+ })
+ afterEach(() => {
+ window.localStorage.clear()
+ $activeGatewayProfile.set('default')
+ $layoutTree.set(null)
+ $selectedStoredSessionId.set(null)
+ $sessionTiles.set([])
+ })
+ it("drops the deleted profile's persisted session tiles from memory and storage", () => {
+ activeGatewayProfile.set('worker')
+ mod.openSessionTile('worker-session-1')
+ mod.openSessionTile('worker-session-2', 'left')
+ expect(mod.$sessionTiles.get().map(tile => tile.storedSessionId)).toEqual(['worker-session-1', 'worker-session-2'])
+ expect(storedTiles()).toHaveProperty('worker')
+ mod.dropTilesForProfile('worker')
+ expect(mod.$sessionTiles.get()).toEqual([])
+ expect(storedTiles()).not.toHaveProperty('worker')
+ // Session tiles of another profile survive the drop.
+ activeGatewayProfile.set('writer')
+ mod.openSessionTile('writer-session-1')
+ expect(mod.$sessionTiles.get().map(tile => tile.storedSessionId)).toEqual(['writer-session-1'])
+ expect(storedTiles()).toHaveProperty('writer')
+ })
+ it("drops Bot Mode tiles owned by a locally-deleted profile and keeps the other bots' tiles", () => {
+ mod.openSessionTile('bot-chat-1', 'right', undefined, undefined, {
+ ownerRoute: { connectionId: 'local', mode: 'local' as const, profile: 'researcher-1' },
+ workspaceMode: 'bots' as const,
+ workspaceOwnerKey: 'researcher-1'
+ })
+ mod.openSessionTile('bot-chat-2', 'right', undefined, undefined, {
+ ownerRoute: { connectionId: 'local', mode: 'local' as const, profile: 'writer-1' },
+ workspaceMode: 'bots' as const,
+ workspaceOwnerKey: 'writer-1'
+ })
+ mod.dropTilesForProfile('researcher-1')
+ expect(mod.$sessionTiles.get().map(tile => tile.storedSessionId)).toEqual(['bot-chat-2'])
+ expect(
+ (storedTiles()[BOTS_BUCKET] as Array<{ storedSessionId: string }>).map(tile => tile.storedSessionId)
+ ).toEqual(['bot-chat-2'])
+ })
+ it('drops a source-scoped bot tile only for the exact route and keeps same-name agents elsewhere', () => {
+ const route = {
+ connectionId: 'source-a',
+ mode: 'remote' as const,
+ profile: 'worker',
+ targetProfile: 'backend-worker'
+ }
+ mod.openSessionTile('bot-a', 'right', undefined, undefined, {
+ ownerRoute: route,
+ workspaceMode: 'bots' as const,
+ workspaceOwnerKey: 'source-a::worker'
+ })
+ mod.openSessionTile('bot-b', 'right', undefined, undefined, {
+ ownerRoute: {
+ connectionId: 'source-b',
+ mode: 'remote' as const,
+ profile: 'worker',
+ targetProfile: 'backend-worker'
+ },
+ workspaceMode: 'bots' as const,
+ workspaceOwnerKey: 'source-b::worker'
+ })
+ mod.openSessionTile('bot-local', 'right', undefined, undefined, {
+ ownerRoute: { connectionId: 'local', mode: 'local' as const, profile: 'worker' },
+ workspaceMode: 'bots' as const,
+ workspaceOwnerKey: 'local::worker'
+ })
+ mod.dropTilesForProfile('worker', route)
+ expect(mod.$sessionTiles.get().map(tile => tile.storedSessionId)).toEqual(['bot-b', 'bot-local'])
+ expect(
+ (storedTiles()[BOTS_BUCKET] as Array<{ storedSessionId: string }>).map(tile => tile.storedSessionId)
+ ).toEqual(['bot-b', 'bot-local'])
})
})
diff --git a/apps/desktop/src/store/session-states.ts b/apps/desktop/src/store/session-states.ts
index b14a07b18ac..aaa07df97a1 100644
--- a/apps/desktop/src/store/session-states.ts
+++ b/apps/desktop/src/store/session-states.ts
@@ -1583,6 +1583,84 @@ export function discardSessionTile(storedSessionId: string) {
saveTiles($sessionTiles.get().filter(t => t.storedSessionId !== storedSessionId))
}
+/**
+ * Drop every persisted tile owned by a profile that is being deleted — the
+ * profile's own session-tile bucket and any Bot Mode tile whose ownerRoute
+ * points at it (matched by desktop profile name, or by exact connection /
+ * backend target profile when a source-scoped route is given).
+ *
+ * A leftover tile RESURRECTS the deleted profile on the next launch: Bot tab
+ * restore re-dials the profile's backend, whose ensure_hermes_home() re-creates
+ * the profile directory the delete just removed (hermes-agent#94235). Same
+ * discard (no ⌘⇧T) semantics as discardSessionTile — undoing the delete of the
+ * owning profile would resolve to a 404 again.
+ */
+export function dropTilesForProfile(
+ profile: string,
+ route?: { connectionId?: string; profile?: string; targetProfile?: string }
+): void {
+ const name = normalizeProfileKey(profile)
+ const routeProfile = String(route?.profile ?? '').trim()
+ const routeTarget = String(route?.targetProfile ?? '').trim()
+ const routeConnection = String(route?.connectionId ?? '').trim()
+
+ const ownerMatches = (owner: SessionProfileRoute | undefined): boolean => {
+ if (!owner) {
+ return false
+ }
+
+ const ownerProfile = String(owner.profile ?? '').trim()
+ const ownerTarget = String(owner.targetProfile ?? '').trim()
+ const ownerConnection = String(owner.connectionId ?? '').trim()
+
+ if (routeProfile) {
+ // Source-scoped delete: the route's desktop profile name, backend target,
+ // and connection must all agree with the tile's owner route.
+ if (ownerProfile !== routeProfile) {
+ return false
+ }
+
+ if (routeTarget && ownerTarget !== routeTarget) {
+ return false
+ }
+
+ return !routeConnection || ownerConnection === routeConnection
+ }
+
+ return ownerProfile === name || ownerTarget === name
+ }
+
+ // The profile's own sessions bucket (Bot tiles live in the shared bucket
+ // and are keyed by ownerRoute, not by bucket).
+ delete tilesByProfile[name]
+
+ const botTiles = tilesByProfile[BOTS_TILE_BUCKET]
+
+ if (botTiles) {
+ const remaining = botTiles.filter(tile => !ownerMatches(tile.ownerRoute))
+
+ if (remaining.length > 0) {
+ tilesByProfile[BOTS_TILE_BUCKET] = remaining
+ } else {
+ delete tilesByProfile[BOTS_TILE_BUCKET]
+ }
+ }
+
+ // Live atom: drop the deleted profile's Bot tiles, and — when the deleted
+ // profile IS the live gateway's profile — the session tiles in view (they
+ // belong to that bucket; the caller re-homes afterwards).
+ const live = $sessionTiles.get()
+ const next = live.filter(tile =>
+ tile.workspaceMode === 'bots' ? !ownerMatches(tile.ownerRoute) : profileKey() !== name
+ )
+
+ if (next.length !== live.length) {
+ $sessionTiles.set(next)
+ }
+
+ persistTiles()
+}
+
/** ⌘⇧T — reopen the most recently closed tab where it was, then focus it.
* Adoption alone is silent (won't steal the active tab), so restore has to
* front the pane explicitly. Skips ids that are live again (reopened / now