Skip to content

The agent that grows with you

fix(desktop): kanban keeps the bare slug key for local and dials once per switch

kshitijk4poore4a1969fd61d
diff --git a/apps/desktop/src/plugins/kanban/api.connection-scope.test.ts b/apps/desktop/src/plugins/kanban/api.connection-scope.test.ts
new file mode 100644
index 00000000000..0b38b177114
--- /dev/null
+++ b/apps/desktop/src/plugins/kanban/api.connection-scope.test.ts
@@ -0,0 +1,80 @@
+import { act, renderHook } from '@testing-library/react'
+import { afterEach, describe, expect, it, vi } from 'vitest'
+
+// A board lives on ONE gateway. The kanban data layer follows the active
+// connection: keys built during render change with it (so an observer is a
+// clean cache miss on a switch), the selected slug is remembered per
+// connection (the local pool keeps the bare key — the lib/connection-scoped.ts
+// contract, and the slug picked before per-connection keys existed survives),
+// and the events socket dials the new backend exactly once per switch.
+
+vi.mock('@/hermes', () => ({ setApiRequestProfile: vi.fn() }))
+
+const { $boardSlug, bindApi, boardsKey, useKanbanScope } = await import('./api')
+const { setConnection } = await import('@/store/session')
+
+afterEach(() => {
+ setConnection(null)
+})
+
+describe('kanban connection scope', () => {
+ it('render-time keys follow the active connection', () => {
+ const { result } = renderHook(() => useKanbanScope())
+
+ expect(boardsKey(result.current)).toEqual(['kanban', 'boards', 'local'])
+
+ // No rerender(): the subscription itself must re-render the component.
+ act(() => setConnection({ connectionId: 'spark', mode: 'remote' } as never))
+
+ expect(boardsKey(result.current)).toEqual(['kanban', 'boards', 'spark'])
+ })
+
+ it('remembers the slug per connection and dials the socket once per switch', () => {
+ const stored = new Map<string, unknown>([
+ ['boardSlug', 'ops'],
+ ['boardSlug.spark', 'research']
+ ])
+
+ const storage = {
+ get: <T>(key: string, fallback: T) => (stored.has(key) ? (stored.get(key) as T) : fallback),
+ remove: vi.fn(),
+ set: (key: string, value: unknown) => void stored.set(key, value)
+ }
+
+ const dials: string[] = []
+
+ const socket = vi.fn((path: string) => {
+ dials.push(path)
+
+ return vi.fn()
+ })
+
+ const dispose = bindApi(async () => ({}) as never, storage, socket)
+
+ expect($boardSlug.get()).toBe('ops')
+ expect(dials).toEqual(['/events?board=ops'])
+
+ // Boot publishes the local descriptor after plugins bound: same scope, no dial.
+ setConnection({ mode: 'local' } as never)
+ expect(dials).toEqual(['/events?board=ops'])
+
+ // Different slug on the next gateway: exactly one dial, not one per listener.
+ setConnection({ connectionId: 'spark', mode: 'remote' } as never)
+ expect($boardSlug.get()).toBe('research')
+ expect(dials).toEqual(['/events?board=ops', '/events?board=research'])
+
+ // Same slug on the way back to a gateway with an equal selection still
+ // dials once — the backend behind the slug changed.
+ stored.set('boardSlug', 'research')
+ setConnection({ mode: 'local' } as never)
+ expect($boardSlug.get()).toBe('research')
+ expect(dials).toEqual(['/events?board=ops', '/events?board=research', '/events?board=research'])
+
+ // Writes land under the scope current at write time.
+ $boardSlug.set('triage')
+ expect(stored.get('boardSlug')).toBe('triage')
+ expect(stored.get('boardSlug.spark')).toBe('research')
+
+ dispose()
+ })
+})
diff --git a/apps/desktop/src/plugins/kanban/api.ts b/apps/desktop/src/plugins/kanban/api.ts
index 8135c0fde3c..3de2173b296 100644
--- a/apps/desktop/src/plugins/kanban/api.ts
+++ b/apps/desktop/src/plugins/kanban/api.ts
@@ -152,28 +152,41 @@ export function bindApi(
close = socket(slug ? `/events?board=${encodeURIComponent(slug)}` : '/events', data => onEventsFrame(slug, data))
}
- // Board selection is per-connection: hydrate under the connection's storage
- // key, and re-hydrate + reopen the socket when the connection changes (the
- // outgoing gateway's slug may 404 on the next one).
- const slugStorageKey = () => `${BOARD_SLUG_KEY}.${kanbanConnectionScope()}`
-
- const hydrateSlug = () => {
- $boardSlug.set(storage.get(slugStorageKey(), ''))
+ // The local connection keeps the BARE key (same rule as lib/connection-scoped
+ // for single-backend users: byte-identical storage, and the slug they picked
+ // before per-connection keys existed survives the upgrade). Remotes are
+ // suffixed by registry id.
+ const slugStorageKey = () => {
+ const scope = kanbanConnectionScope()
+
+ return scope === 'local' ? BOARD_SLUG_KEY : `${BOARD_SLUG_KEY}.${scope}`
}
- hydrateSlug()
+ $boardSlug.set(storage.get(slugStorageKey(), ''))
unsubs.push($boardSlug.listen(slug => storage.set(slugStorageKey(), slug)))
+ open($boardSlug.get())
+ unsubs.push($boardSlug.listen(open))
+ let scope = kanbanConnectionScope()
unsubs.push(
host.state.connectionId.listen(() => {
- // Query keys embed the connection scope (kanbanConnectionScope), so the
- // new connection is already a clean React Query cache miss — no
- // invalidation needed here. Only the LIVE bindings (socket, slug) follow.
- hydrateSlug()
- open($boardSlug.get())
+ // Query keys embed the scope, so the new connection is already a cache
+ // miss; only the LIVE bindings (socket, slug) follow it. The boot-time
+ // null → 'local' publish is the same scope, not a switch. A changed slug
+ // reopens the socket through the $boardSlug listener above; an unchanged
+ // slug still needs a dial because the backend behind it changed.
+ if (kanbanConnectionScope() === scope) {
+ return
+ }
+
+ scope = kanbanConnectionScope()
+ const previous = $boardSlug.get()
+ $boardSlug.set(storage.get(slugStorageKey(), ''))
+
+ if ($boardSlug.get() === previous) {
+ open(previous)
+ }
})
)
- open($boardSlug.get())
- unsubs.push($boardSlug.listen(open))
return () => {
unsubs.forEach(unsub => unsub())