Skip to content

The agent that grows with you

fix(desktop): restore the last-used profile when the primary local descriptor is profile-less

Konstantin Khlopkov825324fdc98b
diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.ts
index dbde42775ca..5588f5961a4 100644
--- a/apps/desktop/electron/main.ts
+++ b/apps/desktop/electron/main.ts
@@ -11364,10 +11364,13 @@ async function ensureBackend(profile, opts: { passive?: boolean; spawnPriority?:
// A shared backend still owes the caller its profile scope, so renderer-side
// WebSocket, filesystem, and cache routing target the selected profile.
// `sharedPrimary` marks this as the shared-primary route: pooled backends
- // also carry `profile`, so only this descriptor gets the flag.
+ // also carry `profile`, so only this descriptor gets the flag. The
+ // unshared primary carries its own key too: a profile-less descriptor
+ // reads as "default" downstream, which breaks per-source profile memory
+ // (the primary IS "default" only when it actually booted as default).
return route.descriptorProfile
? { ...connection, profile: route.descriptorProfile, sharedPrimary: true }
- : connection
+ : { ...connection, profile: key }
}
// A backend for this key may still be dying (idle reap, LRU eviction, a
@@ -13230,6 +13233,7 @@ async function runHermesStart() {
source: 'local',
authMode: 'token',
token: authToken,
+ profile,
wsUrl,
logs: hermesLog.slice(-80),
...getWindowState()
diff --git a/apps/desktop/src/store/connections.test.ts b/apps/desktop/src/store/connections.test.ts
index 737bb7db330..f9199b4f5d3 100644
--- a/apps/desktop/src/store/connections.test.ts
+++ b/apps/desktop/src/store/connections.test.ts
@@ -105,7 +105,13 @@ const list = vi.fn(async () => registry)
const api = vi.fn(async () => ({ profiles: [] }))
const setLastUsed = vi.fn(async (id: string) => ({ ok: true, registry: { ...registry, lastUsed: id } }))
+// What the (mocked) primary local descriptor reports as its profile. Null
+// mirrors startHermes()'s profile-less primary descriptor; a string mirrors
+// the fixed contract where the primary carries the profile it booted with.
+let primaryLocalDescriptorProfile: null | string = null
+
beforeEach(() => {
+ primaryLocalDescriptorProfile = null
localStorage.clear()
_resetConnectionsForTests()
$connectionsRegistry.set(null)
@@ -124,7 +130,12 @@ beforeEach(() => {
$connection.set({
connectionId: connectionId ?? undefined,
mode: connectionId === 'local' ? 'local' : 'remote',
- profile,
+ // The primary local descriptor from startHermes() historically carried
+ // no profile key at all; the registry route fills it in (see the
+ // switch-back regression test at the bottom of this file).
+ ...(connectionId === 'local' && primaryLocalDescriptorProfile === null
+ ? {}
+ : { profile }),
registryScoped: true
})
})
@@ -888,6 +899,33 @@ describe('selectConnection', () => {
expect($showAllProfiles.get()).toBe(false)
})
+ it('restores the last-used profile on switch-back even when the primary local descriptor is profile-less', async () => {
+ // The pair is remembered while the primary local descriptor is honest
+ // about its profile (boot publication).
+ setConnectionsRegistry(registry)
+ $connection.set({ connectionId: 'local', mode: 'local', profile: 'mac', registryScoped: true })
+ $activeGatewayProfile.set('mac')
+
+ expect(JSON.parse(localStorage.getItem('hermes.desktop.lastProfileByConnection') || '{}')).toEqual({
+ local: 'mac'
+ })
+
+ // A later resync republishes a profile-less primary descriptor (the
+ // startHermes shape). The remembered pair is the authority for "what was
+ // last used here" — switching away and back must still restore 'mac',
+ // and the commit must not die in targetIsActive() on the descriptor gap.
+ primaryLocalDescriptorProfile = null
+
+ await selectConnection('homelab')
+ expect(ensureGatewayAgent).toHaveBeenLastCalledWith('homelab', 'default', expect.anything())
+
+ await selectConnection('local')
+
+ expect(openGatewayAgent).toHaveBeenLastCalledWith('local', 'mac')
+ expect(ensureGatewayAgent).toHaveBeenLastCalledWith('local', 'mac', expect.anything())
+ expect($newChatProfile.get()).toBe('mac')
+ })
+
it('never re-homes a live connection the registry cannot name', async () => {
// A window connected through the legacy v1 route carries an unqualified
// descriptor, so resolvedConnectionId — and therefore $activeConnectionId
diff --git a/apps/desktop/src/store/connections.ts b/apps/desktop/src/store/connections.ts
index dcbbd04b9c4..cdd83e6e234 100644
--- a/apps/desktop/src/store/connections.ts
+++ b/apps/desktop/src/store/connections.ts
@@ -292,10 +292,21 @@ export async function selectConnection(connectionId: string, options: SelectConn
const targetKey = `${connectionId}::${targetProfile}`
+ // The primary local descriptor (startHermes) historically publishes without
+ // a profile of its own; a profile-less descriptor on the source we are
+ // landing must not strand the switch — the activation already published the
+ // route we asked for, so trust it for the same source instead of comparing
+ // against a "default" it never meant.
const targetIsActive = () => {
const active = $connection.get()
- return active?.connectionId === connectionId && normalizeProfileKey(active.profile) === targetProfile
+ if (active?.connectionId !== connectionId) {
+ return false
+ }
+
+ const activeProfile = active.profile === undefined ? null : normalizeProfileKey(active.profile)
+
+ return activeProfile === null || activeProfile === targetProfile
}
if (pendingTarget === targetKey) {