Skip to content

The agent that grows with you

fix(desktop): stop deleted profiles reconnecting

Gille976183f53610
diff --git a/apps/desktop/src/app/profiles/delete-profile-dialog.tsx b/apps/desktop/src/app/profiles/delete-profile-dialog.tsx
index 2d0e209f680..6f02d630b5a 100644
--- a/apps/desktop/src/app/profiles/delete-profile-dialog.tsx
+++ b/apps/desktop/src/app/profiles/delete-profile-dialog.tsx
@@ -1,6 +1,7 @@
import { ConfirmDialog } from '@/components/ui/confirm-dialog'
import { deleteProfile } from '@/hermes'
import { useI18n } from '@/i18n'
+import { retireLocalProfileGateways } from '@/store/gateway'
import { $activeGatewayProfile, normalizeProfileKey, selectProfile, setActiveProfile } from '@/store/profile'
// Thin wrapper over ConfirmDialog: owns the deleteProfile call, inherits
@@ -48,6 +49,7 @@ export function DeleteProfileDialog({
// onDeleted refresh so our reset is the last write — a refreshActiveProfile
// racing the (still-dying) backend can't clobber the pill back to it.
const wasActive = normalizeProfileKey(profile.name) === normalizeProfileKey($activeGatewayProfile.get())
+ retireLocalProfileGateways(profile.name)
await deleteProfile(profile.name)
await onDeleted?.()
diff --git a/apps/desktop/src/app/profiles/index.test.tsx b/apps/desktop/src/app/profiles/index.test.tsx
index 6050c935385..a87da7486b2 100644
--- a/apps/desktop/src/app/profiles/index.test.tsx
+++ b/apps/desktop/src/app/profiles/index.test.tsx
@@ -3,6 +3,7 @@ import type * as Nanostores from 'nanostores'
import { afterEach, describe, expect, it, vi } from 'vitest'
import { deleteProfile } from '@/hermes'
+import { retireLocalProfileGateways } from '@/store/gateway'
import { refreshProfiles, selectProfile, setActiveProfile } from '@/store/profile'
import type { ProfileInfo } from '@/types/hermes'
@@ -39,6 +40,10 @@ vi.mock('@/store/notifications', () => ({
notifyError: vi.fn()
}))
+vi.mock('@/store/gateway', () => ({
+ retireLocalProfileGateways: vi.fn()
+}))
+
const { $activeGatewayProfile: activeGateway, $profileColors } = vi.hoisted(() => {
const { atom } = require('nanostores') as typeof Nanostores
@@ -126,6 +131,11 @@ describe('ProfilesView', () => {
})
it('re-homes to default when the active profile is deleted', async () => {
+ const deleteProfileMock = vi.mocked(deleteProfile)
+ const retireLocalProfileGatewaysMock = vi.mocked(retireLocalProfileGateways)
+
+ deleteProfileMock.mockClear()
+ retireLocalProfileGatewaysMock.mockClear()
vi.mocked(refreshProfiles).mockResolvedValue([makeProfile('default', true), makeProfile(NAMED_PROFILE)])
activeGateway.set(NAMED_PROFILE)
@@ -133,6 +143,10 @@ describe('ProfilesView', () => {
await deleteTheNamedProfile()
await waitFor(() => expect(deleteProfile).toHaveBeenCalledWith(NAMED_PROFILE))
+ expect(retireLocalProfileGateways).toHaveBeenCalledWith(NAMED_PROFILE)
+ expect(retireLocalProfileGatewaysMock.mock.invocationCallOrder[0]).toBeLessThan(
+ deleteProfileMock.mock.invocationCallOrder[0]
+ )
await waitFor(() => expect(selectProfile).toHaveBeenCalledWith('default'))
expect(setActiveProfile).toHaveBeenCalledWith('default')
})
diff --git a/apps/desktop/src/sdk/index.ts b/apps/desktop/src/sdk/index.ts
index 31fb6e381d0..4ce9ea17828 100644
--- a/apps/desktop/src/sdk/index.ts
+++ b/apps/desktop/src/sdk/index.ts
@@ -32,7 +32,8 @@ import {
openGatewayForAgent,
openGatewayForProfile,
requestGatewayForAgent,
- requestGatewayForProfile
+ requestGatewayForProfile,
+ retireLocalProfileGateways
} from '@/store/gateway'
import { notify, notifyError } from '@/store/notifications'
import {
@@ -294,6 +295,10 @@ export const host = {
// backend can't clobber the pill back to the deleted profile).
const wasActive = normalizeProfileKey(name) === normalizeProfileKey($activeGatewayProfile.get())
+ // A hover-warmed Bot Mode row owns a retained renderer socket. Retire it
+ // before Electron stops the profile backend so the socket closure cannot
+ // schedule a reconnect that resurrects the deleted profile.
+ retireLocalProfileGateways(name)
await deleteProfile(name)
if (wasActive) {
diff --git a/apps/desktop/src/sdk/profile-routing.test.ts b/apps/desktop/src/sdk/profile-routing.test.ts
index bcf21feb026..812eb7846b0 100644
--- a/apps/desktop/src/sdk/profile-routing.test.ts
+++ b/apps/desktop/src/sdk/profile-routing.test.ts
@@ -87,12 +87,14 @@ vi.mock('@/store/gateway', async () => {
method,
params,
profile
- }))
+ })),
+ retireLocalProfileGateways: vi.fn()
}
})
const { host } = await import('./index')
-const { requestGatewayForAgent, requestGatewayForProfile } = await import('@/store/gateway')
+const { deleteProfile } = await import('@/hermes')
+const { requestGatewayForAgent, requestGatewayForProfile, retireLocalProfileGateways } = await import('@/store/gateway')
const { $profiles, refreshProfiles } = await import('@/store/profile')
const profile = (name: string): ProfileInfo => ({
@@ -112,6 +114,24 @@ afterEach(() => {
})
describe('connection-aware plugin host APIs', () => {
+ it('retires a profile gateway before deleting it', async () => {
+ const order: string[] = []
+
+ vi.mocked(retireLocalProfileGateways).mockImplementationOnce(() => {
+ order.push('retire')
+ })
+ vi.mocked(deleteProfile).mockImplementationOnce(async () => {
+ order.push('delete')
+
+ return { ok: true, path: '/profiles/worker' }
+ })
+
+ await host.deleteProfile('worker')
+
+ expect(order).toEqual(['retire', 'delete'])
+ expect(retireLocalProfileGateways).toHaveBeenCalledWith('worker')
+ })
+
it('refreshes the profile inventory before asking Electron for routes', async () => {
const getProfileRoutes = vi.fn(async () => [
{ connectionId: 'connection-local', mode: 'local', profile: 'desktop-primary', targetProfile: 'desktop-primary' },
diff --git a/apps/desktop/src/store/gateway-connection-lifecycle.test.ts b/apps/desktop/src/store/gateway-connection-lifecycle.test.ts
index 22b0f9a160a..53872b2d199 100644
--- a/apps/desktop/src/store/gateway-connection-lifecycle.test.ts
+++ b/apps/desktop/src/store/gateway-connection-lifecycle.test.ts
@@ -51,6 +51,9 @@ const {
disposeSecondariesForConnection,
ensureActiveGatewayOpen,
ensureGatewayForAgent,
+ openGatewayForProfile,
+ reconnectSecondaryGateways,
+ retireLocalProfileGateways,
setPrimaryGateway
} = await import('./gateway')
@@ -145,6 +148,52 @@ describe('disposeSecondariesForConnection', () => {
})
})
+describe('retireLocalProfileGateways', () => {
+ it('retires both local profile scopes without touching the same-named remote agent', async () => {
+ const getConnection = vi.fn(async (profile: string) => descriptorFor('legacy-local', profile))
+
+ const getConnectionFor = vi.fn(
+ async ({ connectionId, profile }: { connectionId: string; profile: string }) =>
+ descriptorFor(connectionId, profile)
+ )
+
+ installDesktop({ getConnection, getConnectionFor })
+
+ await openGatewayForProfile('selena')
+ await ensureGatewayForAgent('local', 'selena')
+ await ensureGatewayForAgent('homelab', 'selena')
+
+ expect(gatewayMocks.instances).toHaveLength(3)
+ const connectionCallsBeforeRetire = getConnection.mock.calls.length + getConnectionFor.mock.calls.length
+
+ retireLocalProfileGateways('selena')
+
+ expect(gatewayMocks.instances[0].close).toHaveBeenCalledOnce()
+ expect(gatewayMocks.instances[1].close).toHaveBeenCalledOnce()
+ expect(gatewayMocks.instances[2].close).not.toHaveBeenCalled()
+
+ // A wake/reconnect sweep cannot redial either retired local scope. The
+ // homelab entry remains open and therefore also needs no extra dial.
+ reconnectSecondaryGateways()
+ await Promise.resolve()
+ expect(getConnection.mock.calls.length + getConnectionFor.mock.calls.length).toBe(connectionCallsBeforeRetire)
+ })
+
+ it('allows an explicit later access to create a fresh profile secondary', async () => {
+ const getConnection = vi.fn(async (profile: string) => descriptorFor('legacy-local', profile))
+
+ installDesktop({ getConnection })
+
+ await openGatewayForProfile('selena')
+ retireLocalProfileGateways('selena')
+ await openGatewayForProfile('selena')
+
+ expect(gatewayMocks.instances).toHaveLength(2)
+ expect(gatewayMocks.instances[0].close).toHaveBeenCalledOnce()
+ expect(gatewayMocks.instances[1].close).not.toHaveBeenCalled()
+ })
+})
+
describe('reconnect fail-stop on a removed connection', () => {
it('evicts the entry instead of retrying when the registry no longer knows the id', async () => {
const getConnectionFor = vi
diff --git a/apps/desktop/src/store/gateway.ts b/apps/desktop/src/store/gateway.ts
index b9640203143..e753f7474e0 100644
--- a/apps/desktop/src/store/gateway.ts
+++ b/apps/desktop/src/store/gateway.ts
@@ -806,6 +806,43 @@ export function closeSecondaryGateways(): void {
restoreActiveToPrimaryIfEvicted()
}
+// A local profile can have two renderer-owned sockets: the legacy bare
+// profile scope and the explicit `local` registry scope. Profile deletion
+// stops their Electron backend processes, but a retained Secondary otherwise
+// sees that shutdown as a transient disconnect and starts its reconnect loop,
+// resurrecting the backend that was just deleted. Retire both local scopes
+// before the DELETE request while preserving same-named agents on remote,
+// cloud, or SSH connections.
+export function retireLocalProfileGateways(profile: string): void {
+ const name = String(profile || '').trim()
+
+ if (!name) {
+ return
+ }
+
+ const key = normKey(name)
+ const scopes = new Set([key, registryBackendScopeKey('local', key)])
+ let activeInvalidated = false
+
+ for (const scope of scopes) {
+ const entry = g.secondaries.get(scope)
+
+ if (!entry) {
+ continue
+ }
+
+ activeInvalidated ||= scope === g.activeKey
+ disposeSecondary(entry)
+ g.secondaries.delete(scope)
+ }
+
+ restoreActiveToPrimaryIfEvicted()
+
+ if (activeInvalidated) {
+ g.config?.onActiveConnectionInvalidated?.(g.primaryProfile, gatewayActivationEpoch())
+ }
+}
+
// Registry lifecycle: a connection was removed or materially edited. Dispose
// every secondary scoped to it (a removed remote/cloud source has no local
// process to die, so without this its WebSocket stays open streaming ghost