Skip to content

The agent that grows with you

fix(desktop): a gateway pick rescans the star map and leaves the old profile's project

Teknium32a39600c414
diff --git a/apps/desktop/src/store/connections-route-scope.test.ts b/apps/desktop/src/store/connections-route-scope.test.ts
new file mode 100644
index 00000000000..43a9f6105b6
--- /dev/null
+++ b/apps/desktop/src/store/connections-route-scope.test.ts
@@ -0,0 +1,132 @@
+import { afterEach, beforeEach, expect, it, vi } from 'vitest'
+
+import type { DesktopConnectionsRegistry, HermesConnection } from '@/global'
+import type * as Hermes from '@/hermes'
+
+vi.mock('@/app/contrib/hooks/use-background-sync', () => ({ resetLiveRuntimeTracking: vi.fn() }))
+vi.mock('@/hermes', async importOriginal => {
+ const actual = await importOriginal<typeof Hermes>()
+
+ return {
+ ...actual,
+ getProfiles: vi.fn(async () => ({ profiles: [{ name: 'default' }] })),
+ getStarmapGraph: vi.fn(async () => ({ nodes: [], edges: [] })),
+ hermesApi: vi.fn(async () => ({ current: 'default', profiles: [{ name: 'default' }] })),
+ HermesGateway: class {
+ connectionState = 'closed'
+ wsUrl = ''
+ connect = async (url: string) => {
+ this.wsUrl = url
+ this.connectionState = 'open'
+ }
+ close = () => {
+ this.connectionState = 'closed'
+ }
+ onEvent = () => () => undefined
+ onState = () => () => undefined
+ request = async () => ({})
+ }
+ }
+})
+
+import {
+ _resetConnectionsForTests,
+ selectConnection,
+ setConnectionsRegistry
+} from '@/store/connections'
+import {
+ closeSecondaryGateways,
+ configureGatewayRegistry,
+ setPrimaryGateway,
+ setPrimaryGatewayConnection
+} from '@/store/gateway'
+import {
+ $activeGatewayProfile,
+ $newChatRoute,
+ $showAllProfiles,
+ ensureGatewayAgent,
+ newSessionInAgent
+} from '@/store/profile'
+import { $projectScope, ALL_PROJECTS } from '@/store/project-scope'
+import { $projectTree, resolveNewSessionCwd } from '@/store/projects'
+import { setConnection } from '@/store/session'
+import { $starmapGraph, loadStarmapGraph } from '@/store/starmap'
+
+const registry: DesktopConnectionsRegistry = {
+ version: 2,
+ primary: 'homelab',
+ secureTokenStorage: true,
+ connections: [
+ { id: 'homelab', kind: 'remote', label: 'Home', tokenSet: true, tokenPreview: null },
+ { id: 'local', kind: 'local', label: 'This device', tokenSet: false, tokenPreview: null }
+ ]
+}
+
+function descriptor(connectionId: string, profile = 'default'): HermesConnection {
+ return {
+ connectionId,
+ profile,
+ registryScoped: true,
+ isFullscreen: false,
+ nativeOverlayWidth: 0,
+ logs: [],
+ windowButtonPosition: null,
+ mode: connectionId === 'local' ? 'local' : 'remote',
+ authMode: 'token',
+ token: 'test',
+ wsUrl: `ws://${connectionId}.invalid/ws`,
+ baseUrl: `http://${connectionId}.invalid`
+ }
+}
+
+const getConnectionFor = vi.fn(async ({ connectionId, profile }: { connectionId: string; profile: string }) =>
+ descriptor(connectionId, profile)
+)
+
+beforeEach(async () => {
+ _resetConnectionsForTests()
+ getConnectionFor.mockReset()
+ getConnectionFor.mockImplementation(async ({ connectionId, profile }) => descriptor(connectionId, profile))
+ Object.defineProperty(window, 'hermesDesktop', {
+ configurable: true,
+ value: {
+ getConnectionFor,
+ getGatewayWsUrlFor: vi.fn(async ({ connectionId }) => ({ ok: true, wsUrl: `ws://${connectionId}.invalid/ws` })),
+ api: vi.fn(async () => ({ current: 'default', profiles: [{ name: 'default' }] }))
+ }
+ })
+ setConnectionsRegistry(registry)
+ setPrimaryGateway({ connectionState: 'open', request: vi.fn(async () => ({})) } as never, 'default')
+ setPrimaryGatewayConnection({ connectionId: 'homelab', mode: 'remote' })
+ setConnection(descriptor('homelab'))
+ configureGatewayRegistry({ onEvent: vi.fn(), onActiveRouteChanged: profile => $activeGatewayProfile.set(profile) })
+ $showAllProfiles.set(false)
+ newSessionInAgent({ connectionId: 'homelab', profile: 'default' })
+ // Drain the real activation mutex used by the fire-and-forget agent action.
+ await ensureGatewayAgent('homelab', 'default')
+})
+
+afterEach(() => {
+ closeSecondaryGateways()
+ $newChatRoute.set(null)
+})
+
+it("picking another profile on the same gateway leaves the old profile's project before drafting", async () => {
+ $projectScope.set('old-profile-project')
+ $projectTree.set([{ id: 'old-profile-project', path: '/profile-a/repo', name: 'old', repos: [] }] as never)
+
+ await selectConnection('homelab', { profile: 'research' })
+
+ expect($projectScope.get()).toBe(ALL_PROJECTS)
+ expect(resolveNewSessionCwd()).not.toBe('/profile-a/repo')
+})
+
+it('picking another gateway with the same profile name rescans the star map', async () => {
+ const oldGraph = { nodes: [{ id: 'skill:old-host-only' }], edges: [] }
+ $starmapGraph.set(oldGraph as never)
+
+ await selectConnection('local')
+ await loadStarmapGraph() // the lazy read a mounting Star Map view makes
+
+ expect($starmapGraph.get()).not.toBe(oldGraph)
+})
diff --git a/apps/desktop/src/store/connections.ts b/apps/desktop/src/store/connections.ts
index c3f024a246a..1d85d998399 100644
--- a/apps/desktop/src/store/connections.ts
+++ b/apps/desktop/src/store/connections.ts
@@ -36,6 +36,7 @@ import {
setActiveSessionId,
setSelectedStoredSessionId
} from '@/store/session'
+import { reloadOpenStarmapGraph, resetStarmapGraph } from '@/store/starmap'
import { isPeerInstanceWindow, windowProfileOverride } from '@/store/windows'
const LAST_PROFILE_STORAGE_KEY = 'hermes.desktop.lastProfileByConnection'
@@ -97,7 +98,13 @@ const $activeConnectionProfile = computed(
// applyActive publishes only after the tag moved, so the refetch lands on the
// backend the tags now name. `listen` (not `subscribe`) skips the mount-time
// fire, and the computed dedupes equal ids, so this only runs on a real switch.
-$activeConnectionId.listen(() => invalidateProfileScopedQueries())
+$activeConnectionId.listen(() => {
+ invalidateProfileScopedQueries()
+ // The star map is a scan of one backend's skills and memory; same-named
+ // profiles on two gateways are different graphs.
+ resetStarmapGraph()
+ reloadOpenStarmapGraph()
+})
// Remember one profile per source, so switching machines is a re-home rather
// than a reset to `default`. The map is local UI preference only; Electron
@@ -463,9 +470,9 @@ export async function selectConnection(connectionId: string, options: SelectConn
$showAllProfiles.set(false)
}
- // Project ids name rows in one backend's projects.db; leave before the
- // fresh draft copies the project's folder into its cwd.
- if (currentConnectionId !== connectionId) {
+ // Projects belong to one profile on one backend; leave before the fresh
+ // draft copies the project's folder into its cwd.
+ if (currentConnectionId !== connectionId || currentProfile !== targetProfile) {
exitProjectScope()
}