Skip to content

The agent that grows with you

refactor(desktop): connection-scope invalidation is a listen on $activeConnectionId

kshitijk4poor49f47ef64cd5
diff --git a/apps/desktop/src/store/connections.ts b/apps/desktop/src/store/connections.ts
index 1cda5c1d348..455844d3011 100644
--- a/apps/desktop/src/store/connections.ts
+++ b/apps/desktop/src/store/connections.ts
@@ -77,37 +77,19 @@ const $activeConnectionProfile = computed(
// The CONNECTION twin of profile.ts's $activeGatewayProfile subscription.
// The switch commit point (beginGatewaySwitch → invalidateProfileScopedQueries)
-// runs inside beforeActivate — BEFORE the activation publishes the new request
-// scope (applyActive → setApiRequestConnection) — so that invalidation's
-// immediate refetches ride the OUTGOING backend and repaint its data under the
-// incoming connection's route. When the connection id later moves, no kanban
-// query key changes (the pane's keys are unscoped), so nothing re-invalidates:
-// the pane keeps painting the previous gateway's boards. Invalidate here, on
-// the actual (connectionId) change, so the refetch lands on the backend the
-// tags now name.
-//
-// The profile twin suppresses its FIRST fire via `_lastRoutedProfile !==
-// null`, which relies on the profile atom being non-null at startup. The
-// connection id is legitimately null on the primary local route, so null
-// cannot double as "uninitialized" here — an explicit sentinel does.
-let _lastRoutedConnectionId: null | string | undefined
+// runs inside beforeActivate — BEFORE applyActive publishes the new request
+// scope (setApiRequestConnection) — so that invalidation's refetches ride the
+// OUTGOING backend. Re-invalidate on the actual connection-id change, which
+// 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())
/** 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
* remains the authority for the connection registry and all secrets.
*/
$activeConnectionProfile.subscribe(({ connectionId, descriptorProfile, profile, registryScoped }) => {
- // The connection twin (above) rides this existing subscription — the one
- // listener already holding the computed open — instead of a second
- // subscribe, so the store's task graph is unchanged.
- const id = connectionId ?? null
-
- if (_lastRoutedConnectionId !== undefined && _lastRoutedConnectionId !== id) {
- invalidateProfileScopedQueries()
- }
-
- _lastRoutedConnectionId = id
-
// A migrated v1 per-profile remote may expose a client-side alias such as
// "work" while the registered source's actual profile is "default". Only
// remember a source/profile pair after Electron confirms that exact v2
diff --git a/apps/desktop/src/store/kanban-connection-scope.test.ts b/apps/desktop/src/store/kanban-connection-scope.test.ts
index aaaab9b5922..f7871cc8be8 100644
--- a/apps/desktop/src/store/kanban-connection-scope.test.ts
+++ b/apps/desktop/src/store/kanban-connection-scope.test.ts
@@ -45,6 +45,7 @@ const { queryClient } = await import('@/lib/query-client')
const { closeSecondaryGateways, configureGatewayRegistry, setPrimaryGateway } = await import('@/store/gateway')
const { $activeGatewayProfile } = await import('@/store/profile')
const { selectConnection, setConnectionsRegistry, _resetConnectionsForTests } = await import('@/store/connections')
+const { setConnection } = await import('@/store/session')
const conn = (over: Partial<HermesConnection> = {}): HermesConnection =>
({
@@ -116,9 +117,8 @@ describe('connection-switch query invalidation', () => {
})
it('refetches connection-scoped queries against the NEW gateway after a switch', async () => {
- // One active profile-scoped query, exactly like the kanban pane's boards
- // list: the connection tag is read at queryFn time, which is what
- // pluginRest → hermesApi does.
+ // One active profile-scoped query whose connection tag is read at queryFn
+ // time, which is what pluginRest → hermesApi does.
observer = new QueryObserver(queryClient, {
queryKey: ['kanban', 'boards'],
queryFn: async () => {
@@ -161,11 +161,14 @@ describe('connection-switch query invalidation', () => {
})
const unsubscribe = observer.subscribe(() => undefined)
- await vi.waitFor(() => expect(fetches).toBe(1))
+ // Settle, not just start: an invalidation racing an in-flight fetch is
+ // absorbed by the fetch and would hide a listener that fires too often.
+ await vi.waitFor(() => expect(observer!.getCurrentResult().status).toBe('success'))
+ expect(fetches).toBe(1)
- // Re-publishing the same registry must not re-invalidate (first-fire
- // suppression + change guard, same contract as the profile twin).
- setConnectionsRegistry(registry)
+ // A fresh descriptor object naming the SAME connection id (a resync, not a
+ // switch) must not re-invalidate.
+ setConnection(connection => ({ ...connection! }))
await new Promise(resolve => setTimeout(resolve, 200))
expect(fetches).toBe(1)