Skip to content

The agent that grows with you

fix(desktop): a source's remembered profile falls back only when its backend no longer lists it

alt-glitch4fca4114d260
diff --git a/apps/desktop/src/store/connections.test.ts b/apps/desktop/src/store/connections.test.ts
index 0d1eddd0906..cabfa02024a 100644
--- a/apps/desktop/src/store/connections.test.ts
+++ b/apps/desktop/src/store/connections.test.ts
@@ -109,7 +109,7 @@ const registry: DesktopConnectionsRegistry = {
}
const list = vi.fn(async () => registry)
-const api = vi.fn(async () => ({ profiles: [] }))
+const api = vi.fn(async (): Promise<{ profiles: { name: string }[] }> => ({ profiles: [] }))
const setLastUsed = vi.fn(async (id: string) => ({ ok: true, registry: { ...registry, lastUsed: id } }))
beforeEach(() => {
@@ -381,12 +381,42 @@ describe('selectConnection', () => {
$connection.set({ connectionId: 'homelab', mode: 'remote', registryScoped: true })
$activeGatewayProfile.set('default')
$profilesByConnection.set(new Map([['local', [{ name: 'default' }, { name: 'work' }]]]))
+ api.mockResolvedValue({ profiles: [{ name: 'default' }, { name: 'work' }] })
await selectConnection('local')
expect(ensureGatewayAgent).toHaveBeenCalledWith('local', 'default', expect.anything())
})
+ it('keeps the remembered profile when only a stale cached list lacks it', async () => {
+ setConnectionsRegistry(registry)
+ $connection.set({ connectionId: 'local', mode: 'local', profile: 'research', registryScoped: true })
+ $activeGatewayProfile.set('research')
+ $connection.set({ connectionId: 'homelab', mode: 'remote', registryScoped: true })
+ $activeGatewayProfile.set('default')
+ // The list refresh after creating `research` failed, so the cache predates it.
+ $profilesByConnection.set(new Map([['local', [{ name: 'default' }]]]))
+ api.mockResolvedValue({ profiles: [{ name: 'default' }, { name: 'research' }] })
+
+ await selectConnection('local')
+
+ expect(ensureGatewayAgent).toHaveBeenCalledWith('local', 'research', expect.anything())
+ })
+
+ it('keeps the remembered profile when the backend cannot confirm it is gone', async () => {
+ setConnectionsRegistry(registry)
+ $connection.set({ connectionId: 'local', mode: 'local', profile: 'research', registryScoped: true })
+ $activeGatewayProfile.set('research')
+ $connection.set({ connectionId: 'homelab', mode: 'remote', registryScoped: true })
+ $activeGatewayProfile.set('default')
+ $profilesByConnection.set(new Map([['local', [{ name: 'default' }]]]))
+ api.mockRejectedValue(new Error('backend unreachable'))
+
+ await selectConnection('local')
+
+ expect(ensureGatewayAgent).toHaveBeenCalledWith('local', 'research', expect.anything())
+ })
+
it('does not remember a migrated v1 routing alias as a backend profile', async () => {
setConnectionsRegistry(registry)
$connection.set({ connectionId: 'homelab', mode: 'remote' })
diff --git a/apps/desktop/src/store/connections.ts b/apps/desktop/src/store/connections.ts
index 05b6ac40778..8aacc5a70a0 100644
--- a/apps/desktop/src/store/connections.ts
+++ b/apps/desktop/src/store/connections.ts
@@ -125,12 +125,23 @@ $activeConnectionProfile.subscribe(({ connectionId, descriptorProfile, profile,
$lastProfileByConnection.set({ ...$lastProfileByConnection.get(), [connectionId]: profile })
})
-// The profile last used on a source, unless that source's known list no longer has it (deleted elsewhere).
-function rememberedProfile(connectionId: string): string {
+const listsProfile = (profiles: readonly { name: string }[], key: string) =>
+ profiles.some(profile => normalizeProfileKey(profile.name) === key)
+
+// The profile last used on a source; default only once that source's backend no longer lists it (deleted
+// elsewhere). The cached list can predate the profile (a failed refresh after create), so it only triggers the check.
+function rememberedProfile(connectionId: string): Promise<string> | string {
const key = normalizeProfileKey($lastProfileByConnection.get()[connectionId])
- const listed = $profilesByConnection.get().get(connectionId)
+ const cached = $profilesByConnection.get().get(connectionId)
+
+ if (!cached || listsProfile(cached, key)) {
+ return key
+ }
- return !listed || listed.some(profile => normalizeProfileKey(profile.name) === key) ? key : 'default'
+ return getProfiles({ connectionId, profile: 'default' }).then(
+ ({ profiles }) => (listsProfile(profiles, key) ? key : 'default'),
+ () => key
+ )
}
/** @internal Reset module-owned preferences and switch coordination for tests. */
@@ -383,11 +394,12 @@ export async function selectConnection(connectionId: string, options: SelectConn
// browse-mode preference alone so it survives restart (#93197).
const restoreOnBoot = pendingTarget === null && $activeConnectionId.get() === null
+ const explicitProfile = String(options.profile ?? '').trim()
+ // Only a remembered profile the cache lacks waits on the backend; every other pick stays synchronous.
+ const remembered = explicitProfile || rememberedProfile(connectionId)
+ const targetProfile = normalizeProfileKey(typeof remembered === 'string' ? remembered : await remembered)
const currentConnectionId = $activeConnectionId.get()
const currentProfile = normalizeProfileKey($activeGatewayProfile.get())
- const explicitProfile = String(options.profile ?? '').trim()
-
- const targetProfile = normalizeProfileKey(explicitProfile || rememberedProfile(connectionId))
const targetKey = `${connectionId}::${targetProfile}`