The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): a pick still checking its remembered profile yields to a later pick
alt-glitchd9ed3e28d658
diff --git a/apps/desktop/src/store/connections.test.ts b/apps/desktop/src/store/connections.test.tsindex cabfa02024a..400ed29306c 100644--- a/apps/desktop/src/store/connections.test.ts+++ b/apps/desktop/src/store/connections.test.ts@@ -417,6 +417,25 @@ describe('selectConnection', () => {expect(ensureGatewayAgent).toHaveBeenCalledWith('local', 'research', expect.anything())})+ it('a later pick wins over an earlier one still checking its remembered profile', 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' }]]]))+ const listing = deferred<{ profiles: { name: string }[] }>()+ api.mockReturnValueOnce(listing.promise)++ const earlier = selectConnection('local')+ await selectConnection('work-vps')+ listing.resolve({ profiles: [{ name: 'default' }, { name: 'research' }] })+ await earlier++ expect(ensureGatewayAgent).not.toHaveBeenCalledWith('local', expect.anything(), expect.anything())+ expect($activeConnectionId.get()).toBe('work-vps')+ })+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.tsindex 8aacc5a70a0..a2885d49499 100644--- a/apps/desktop/src/store/connections.ts+++ b/apps/desktop/src/store/connections.ts@@ -79,6 +79,9 @@ const $lastProfileByConnection = atom<Record<string, string>>(storedStringRecordlet pendingTarget: null | string = nulllet restoreAttempted = falselet switchRevision = 0+// Counts every pick, including ones that never start a switch, so a pick that waited on its source's backend+// can tell a later pick happened while it waited.+let selectRevision = 0export const $pendingConnectionId = atom<null | string>(null)@@ -128,20 +131,44 @@ $activeConnectionProfile.subscribe(({ connectionId, descriptorProfile, profile,const listsProfile = (profiles: readonly { name: string }[], key: string) =>profiles.some(profile => normalizeProfileKey(profile.name) === key)+// A pick either knows its profile now or waits on the backend; only a wait can be overtaken (null).+type ProfilePick = { kind: 'ready'; profile: string } | { kind: 'pending'; profile: Promise<null | string> }+// 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 {+function rememberedProfile(connectionId: string): ProfilePick {const key = normalizeProfileKey($lastProfileByConnection.get()[connectionId])const cached = $profilesByConnection.get().get(connectionId)if (!cached || listsProfile(cached, key)) {- return key+ return { kind: 'ready', profile: key }}- return getProfiles({ connectionId, profile: 'default' }).then(+ const listed = getProfiles({ connectionId, profile: 'default' }).then(({ profiles }) => (listsProfile(profiles, key) ? key : 'default'),() => key)++ return { kind: 'pending', profile: listed }+}++// The profile a pick lands on: the one asked for, else the one last used there. Only a remembered profile+// the cache lacks waits on the backend (every other pick stays synchronous); null when a later pick+// arrived during that wait.+function pickedProfile(connectionId: string, explicit: null | string | undefined): ProfilePick {+ const asked = String(explicit ?? '').trim()+ const remembered: ProfilePick = asked ? { kind: 'ready', profile: asked } : rememberedProfile(connectionId)+ const pick = ++selectRevision++ if (remembered.kind === 'ready') {+ return { kind: 'ready', profile: normalizeProfileKey(remembered.profile) }+ }++ const settled = remembered.profile.then(profile =>+ pick === selectRevision && profile !== null ? normalizeProfileKey(profile) : null+ )++ return { kind: 'pending', profile: settled }}/** @internal Reset module-owned preferences and switch coordination for tests. */@@ -150,6 +177,7 @@ export function _resetConnectionsForTests(): void {pendingTarget = nullrestoreAttempted = falseswitchRevision = 0+ selectRevision = 0$pendingConnectionId.set(null)}@@ -394,10 +422,13 @@ 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 picked = pickedProfile(connectionId, options.profile)+ const targetProfile = picked.kind === 'ready' ? picked.profile : await picked.profile++ if (targetProfile === null) {+ return+ }+const currentConnectionId = $activeConnectionId.get()const currentProfile = normalizeProfileKey($activeGatewayProfile.get())