The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): resolve default Kanban board before event subscription
funky-xamarin23d95743a022
diff --git a/apps/desktop/src/plugins/kanban/api.default-notify.test.ts b/apps/desktop/src/plugins/kanban/api.default-notify.test.tsnew file mode 100644index 00000000000..041feeb9913--- /dev/null+++ b/apps/desktop/src/plugins/kanban/api.default-notify.test.ts@@ -0,0 +1,132 @@+import { afterEach, describe, expect, it, vi } from 'vitest'++const doors = vi.hoisted(() => ({+ notify: vi.fn(),+ navigate: vi.fn(),+ setConnection: vi.fn<(id: string | null) => void>()+}))++vi.mock('@hermes/plugin-sdk', async () => {+ const { atom } = await import('nanostores')+ const { QueryClient } = await import('@tanstack/react-query')+ const connectionId = atom<string | null>(null)+ doors.setConnection.mockImplementation(id => connectionId.set(id))++ return {+ atom,+ queryClient: new QueryClient({ defaultOptions: { queries: { retry: false } } }),+ host: { ...doors, state: { connectionId }, activeConnectionId: () => connectionId.get() },+ useValue: vi.fn(),+ usePluginI18n: vi.fn(),+ captureGatewayFileDownload: vi.fn()+ }+})++const { queryClient } = await import('@hermes/plugin-sdk')+const { bindApi, $boardSlug } = await import('./api')+let dispose: (() => void) | undefined++afterEach(() => {+ dispose?.()+ dispose = undefined+ doors.setConnection(null)+ queryClient.clear()+ vi.clearAllMocks()+})++describe('default board event notifications', () => {+ it.each(['empty', 'rejected'])('preserves the live alias socket when board resolution is %s', async mode => {+ const rest = vi.fn(async (path: string) => {+ if (path === '/boards') {+ if (mode === 'rejected') throw new Error('offline')+ return { current: '' }+ }+ return { latest_event_id: 20 }+ })+ const socket = vi.fn(() => vi.fn())+ dispose = bindApi(rest as never, { get: (_key, fallback) => fallback, set: vi.fn(), remove: vi.fn() }, socket)+ await vi.waitFor(() => expect(socket).toHaveBeenCalledWith('/events?since=20', expect.any(Function)))+ expect(doors.notify).not.toHaveBeenCalled()+ })++ it.each(['selection', 'dispose'])('ignores alias resolution after %s', async change => {+ let resolveBoards!: (value: unknown) => void+ const boards = new Promise(resolve => {+ resolveBoards = resolve+ })+ const rest = vi.fn(async (path: string) => (path === '/boards' ? boards : { latest_event_id: 30 }))+ const socket = vi.fn(() => vi.fn())+ dispose = bindApi(rest as never, { get: (_key, fallback) => fallback, set: vi.fn(), remove: vi.fn() }, socket)+ if (change === 'selection') {+ $boardSlug.set('chosen')+ await vi.waitFor(() => expect(socket).toHaveBeenCalledWith('/events?board=chosen&since=30', expect.any(Function)))+ } else {+ dispose()+ dispose = undefined+ }+ resolveBoards({ current: 'late' })+ await new Promise(resolve => setTimeout(resolve, 0))+ expect(socket).toHaveBeenCalledTimes(change === 'selection' ? 1 : 0)+ expect(rest).not.toHaveBeenCalledWith('/board?board=late')+ })++ it('does not resolve or override a caller-selected board', async () => {+ const rest = vi.fn(async () => ({ latest_event_id: 30 }))+ const socket = vi.fn(() => vi.fn())+ dispose = bindApi(+ rest as never,+ {+ get: <T>(key: string, fallback: T) => (key === 'boardSlug' ? ('chosen' as T) : fallback),+ set: vi.fn(),+ remove: vi.fn()+ },+ socket+ )+ await vi.waitFor(() => expect(socket).toHaveBeenCalledWith('/events?board=chosen&since=30', expect.any(Function)))+ expect(rest).not.toHaveBeenCalledWith('/boards')+ })++ it('pins the socket to the resolved board and notifies a post-baseline blocked event once', async () => {+ const callbacks: Array<(data: unknown) => void> = []+ const socket = vi.fn((_path: string, cb: (data: unknown) => void) => {+ callbacks.push(cb)+ return vi.fn()+ })+ let latest = 100+ const rest = vi.fn(async (path: string) => {+ if (path === '/boards') return { current: 'default', boards: [{ slug: 'default' }] }+ if (path === '/board?board=default' || path === '/board') return { latest_event_id: latest }+ throw new Error(`Unexpected REST call: ${path}`)+ })+ const storage = { get: <T>(_key: string, fallback: T) => fallback, set: vi.fn(), remove: vi.fn() }+ dispose = bindApi(rest as never, storage, socket)+ await vi.waitFor(() => expect(callbacks).toHaveLength(1))+ expect(socket).toHaveBeenCalledWith('/events?board=default&since=100', expect.any(Function))+ expect($boardSlug.get()).toBe('')+ // Establish the existing notifier's baseline from a nonterminal frame;+ // first-live-frame baseline seeding is separately owned by PR #116236.+ latest = 101+ callbacks[0]({ cursor: 101, events: [{ id: 101, kind: 'created' }] })+ await vi.waitFor(() => expect(rest.mock.calls.filter(([path]) => path === '/board?board=default')).toHaveLength(2))+ await new Promise(resolve => setTimeout(resolve, 0))+ expect(doors.notify).not.toHaveBeenCalled()+ latest = 102+ callbacks[0]({+ cursor: 102,+ events: [+ { id: 99, kind: 'completed' },+ { id: 102, kind: 'blocked', task_id: 'task', payload: { reason: 'Need approval' } }+ ]+ })+ await vi.waitFor(() => expect(doors.notify).toHaveBeenCalledTimes(1))+ expect(doors.notify).toHaveBeenCalledWith(expect.objectContaining({ message: 'Need approval' }))+ callbacks[0]({ cursor: 102, events: [{ id: 102, kind: 'blocked' }] })+ await new Promise(resolve => setTimeout(resolve, 0))+ expect(doors.notify).toHaveBeenCalledTimes(1)+ dispose()+ dispose = undefined+ callbacks[0]({ cursor: 103, events: [{ id: 103, kind: 'blocked' }] })+ await new Promise(resolve => setTimeout(resolve, 0))+ expect(doors.notify).toHaveBeenCalledTimes(1)+ })+})diff --git a/apps/desktop/src/plugins/kanban/api.ts b/apps/desktop/src/plugins/kanban/api.tsindex f9c2f49668a..845d0c62fec 100644--- a/apps/desktop/src/plugins/kanban/api.ts+++ b/apps/desktop/src/plugins/kanban/api.ts@@ -227,49 +227,72 @@ export function bindApi(let close: (() => void) | null = nulllet socketGeneration = 0- const dial = (scope: string, slug: string, since: number | undefined) =>- socket(eventsUrl(slug, since), data => onEventsFrame(scope, slug, data))+ const dial = (scope: string, slug: string, since: number | undefined) => {+ const generation = socketGeneration- const open = (slug: string) => {+ return socket(eventsUrl(slug, since), data => {+ if (generation === socketGeneration) onEventsFrame(scope, slug, data)+ })+ }++ const open = (selectedSlug: string) => {const generation = ++socketGenerationconst scope = kanbanConnectionScope()close?.()close = null- const since = eventsSince(scope, slug)+ const openResolved = (slug: string) => {+ if (generation !== socketGeneration) {+ return+ }++ const since = eventsSince(scope, slug)- if (since !== undefined) {- close = dial(scope, slug, since)+ if (since !== undefined) {+ close = dial(scope, slug, since)++ return+ }++ // No cached tail yet. Wait for the snapshot and open at its+ // latest_event_id. A board switch or unload bumps the generation so a+ // late snapshot cannot open a stale socket. A failed fetch still opens+ // with no since — the server starts at the tail rather than replaying.+ void queryClient+ .fetchQuery({+ queryFn: () => r<KanbanBoard>(boardSnapshotPath(slug)),+ queryKey: boardKey(scope, slug, false)+ })+ .then(board => {+ if (generation !== socketGeneration) {+ return+ }++ const tail = typeof board?.latest_event_id === 'number' ? board.latest_event_id : undefined++ close = dial(scope, slug, tail)+ })+ .catch(() => {+ if (generation !== socketGeneration) {+ return+ }++ close = dial(scope, slug, undefined)+ })+ }+ if (selectedSlug) {+ openResolved(selectedSlug)return}- // No cached tail yet. Wait for the snapshot and open at its- // latest_event_id. A board switch or unload bumps the generation so a- // late snapshot cannot open a stale socket. A failed fetch still opens- // with no since — the server starts at the tail rather than replaying.- void queryClient- .fetchQuery({- queryFn: () => r<KanbanBoard>(boardSnapshotPath(slug)),- queryKey: boardKey(scope, slug, false)- })- .then(board => {- if (generation !== socketGeneration) {- return- }-- const tail = typeof board?.latest_event_id === 'number' ? board.latest_event_id : undefined-- close = dial(scope, slug, tail)- })- .catch(() => {- if (generation !== socketGeneration) {- return- }-- close = dial(scope, slug, undefined)- })+ // Resolve the alias BEFORE the handshake: /events is pinned to its board+ // when opened. Resolving on each frame could classify an old socket's+ // events against a different server-current board's cursor.+ void r<BoardsResponse>('/boards')+ .then(boards => openResolved(typeof boards.current === 'string' ? boards.current : ''))+ .catch(() => openResolved('')) // Keep live cache invalidation; notifications fail closed.}// The local connection keeps the BARE key (the bare-local rule of