Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(desktop-app): catch connectMcpServer rejections in the connect buttons (R4-L1)
Diego Zacarias1c272bb8b42b
diff --git a/desktop-app/ui/src/components/agents/ChatThread.tsx b/desktop-app/ui/src/components/agents/ChatThread.tsxindex d28a6cf00..7ead4bad6 100644--- a/desktop-app/ui/src/components/agents/ChatThread.tsx+++ b/desktop-app/ui/src/components/agents/ChatThread.tsx@@ -489,7 +489,20 @@ export function ChatThread({ showAgentLabel = false, onScrollPositionChange }: C// The deep-link completion resumes the task via the same approval RPC.si?.reason === 'connect_required' && si.mcpServerName && selectedAgent? () => {- void window.clerum.rpc.connectMcpServer(si.mcpServerName!, selectedAgent)+ // R4-L1: a failed mint (403 membership, rpc-proxy down) must not+ // escape as an unhandled rejection. The ProgressStepper's 5s timer+ // re-enables the Connect button so the user can retry; log the+ // failure so it is observable. (Follow-up: a user-facing error+ // indicator on the button — needs pushToast wiring / an+ // onConnect→Promise contract in ProgressStepper.)+ window.clerum.rpc+ .connectMcpServer(si.mcpServerName!, selectedAgent)+ .catch(err => {+ console.error('[connect] connectMcpServer failed', {+ mcpServerName: si.mcpServerName,+ error: err instanceof Error ? err.message : String(err),+ })+ })}: undefined}diff --git a/desktop-app/ui/src/components/agents/InFlightAssistantPlaceholder.tsx b/desktop-app/ui/src/components/agents/InFlightAssistantPlaceholder.tsxindex e8f1c79d1..8f77df0dd 100644--- a/desktop-app/ui/src/components/agents/InFlightAssistantPlaceholder.tsx+++ b/desktop-app/ui/src/components/agents/InFlightAssistantPlaceholder.tsx@@ -113,7 +113,17 @@ export function InFlightAssistantPlaceholder({// host-bound to this conversation's agent (hostRef ≡ agentRef).si?.reason === 'connect_required' && si.mcpServerName? () => {- void window.clerum.rpc.connectMcpServer(si.mcpServerName!, agentRef)+ // R4-L1: a failed mint (403 membership, rpc-proxy down) must not+ // escape as an unhandled rejection. The ProgressStepper's 5s timer+ // re-enables the Connect button so the user can retry; log the+ // failure so it is observable. (Follow-up: a user-facing error+ // indicator on the button.)+ window.clerum.rpc.connectMcpServer(si.mcpServerName!, agentRef).catch(err => {+ console.error('[connect] connectMcpServer failed', {+ mcpServerName: si.mcpServerName,+ error: err instanceof Error ? err.message : String(err),+ })+ })}: undefined}diff --git a/desktop-app/ui/src/components/agents/__tests__/chatThread.connectFailure.test.tsx b/desktop-app/ui/src/components/agents/__tests__/chatThread.connectFailure.test.tsxnew file mode 100644index 000000000..c86386072--- /dev/null+++ b/desktop-app/ui/src/components/agents/__tests__/chatThread.connectFailure.test.tsx@@ -0,0 +1,148 @@+// @vitest-environment jsdom+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'+import { TaskTracker, makeTaskKey } from '@contexts/AgentTaskTrackerContext'+import { act, cleanup, fireEvent, render } from '@testing-library/react'+import { trackerStateToTaskProgress } from '@hooks/domain/trackerToProgress'+import { buildLoadedChatSemanticModels } from '../../../lib/chatMessageSemantics'+import type { AgentChatMessage, TaskProgress } from '../../../uiTypes'+import { ChatThread } from '../ChatThread'++;(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true++// The ChatThread renders its suspended `ProgressStepper` (the Connect button) only+// for a `role: 'user'` group whose message id has an entry in `progressByMessageId`.+// The context values are mutated per-test; the mock factories read them lazily.+const navigationValue = { selectedAgent: 'agent-x', handleSelectChatAgent: vi.fn() }+const notificationsValue = { decideApproval: vi.fn() }+const chatListValue = { chatList: [], chatListLoading: false, sessionStateByChatId: {} }+const actionsValue = {+ chatEndRef: { current: null },+ handleSelectChat: vi.fn(),+ handleRenameChat: vi.fn(),+ handleDeleteChat: vi.fn(),+}+const runtimeValue = { cancelTask: vi.fn() }++let userMessage: AgentChatMessage+let progressByMessageId: Record<string, TaskProgress>++vi.mock('@contexts/NavigationContext', () => ({ useNavigationContext: () => navigationValue }))+vi.mock('@contexts/NotificationsContext', () => ({+ useNotificationsContext: () => notificationsValue,+}))+vi.mock('@contexts/ChatListContext', () => ({ useChatListContext: () => chatListValue }))+vi.mock('@contexts/AgentChatActionsContext', () => ({+ useAgentChatActionsContext: () => actionsValue,+}))+vi.mock('@contexts/McpRuntimeContext', () => ({ useMcpRuntimeContext: () => runtimeValue }))+vi.mock('@contexts/ChatThreadStateContext', () => ({+ useChatThreadStateContext: () => ({+ activeMessages: [userMessage],+ groupedMessages: [{ role: 'user', items: [userMessage] }],+ chatMessagesLoading: false,+ hasOlderMessages: false,+ olderMessagesLoading: false,+ handleLoadOlderMessages: vi.fn(),+ activeChatId: 'chat-1',+ activityByMessageId: {},+ progressByMessageId,+ localSearchQuery: '',+ localSearchCurrentMatch: null,+ semanticModelsByMessageId: new Map(+ buildLoadedChatSemanticModels([userMessage]).map(model => [model.messageId, model])+ ),+ }),+}))+vi.mock('../InFlightAssistantPlaceholder', () => ({ InFlightAssistantPlaceholder: () => null }))+vi.mock('../NudgeArea', () => ({ NudgeArea: () => null }))+vi.mock('../ChatStateBadge', () => ({ ChatStateBadge: () => null }))+vi.mock('@components/MessageArtifactActions', () => ({ MessageArtifactActions: () => null }))++/**+ * T1: the `connect_required` progress is DERIVED from the real producer — a live+ * `TaskTracker` fed the exact `suspended` stream event, then mapped through the+ * production `trackerStateToTaskProgress` — NOT a hand-built literal. So the+ * `progress.suspendedInfo` the ChatThread renders has the real shape.+ */+type ProgressHandler = (e: unknown) => void | Promise<void>++async function deriveConnectRequiredProgress(): Promise<TaskProgress> {+ let handler: ProgressHandler | null = null+ Object.defineProperty(window, 'clerum', {+ configurable: true,+ writable: true,+ value: {+ rpc: {+ subscribeTaskProgress: vi.fn(async (_h: string, _t: string, onEvent: ProgressHandler) => {+ handler = onEvent+ return async () => undefined+ }),+ getTaskResult: vi.fn(async () => ({ response: 'ok' })),+ cancelTask: vi.fn(async () => undefined),+ connectMcpServer,+ },+ },+ })+ const tracker = new TaskTracker()+ const key = makeTaskKey('agent-x', 'chat-1')+ tracker.start(key, 'task-1', 'msg-1')+ await Promise.resolve() // let subscribeTaskProgress register the handler+ if (!handler) throw new Error('no progress handler registered')+ // `handler` is reassigned inside a closure, so TS won't narrow the null away+ // after the guard above — the runtime throw already made it non-null.+ const fire = handler as ProgressHandler+ await fire({+ type: 'suspended',+ data: {+ taskId: 'task-1',+ requestId: 'req-1',+ displayName: 'monday tool',+ reason: 'connect_required',+ mcpServerName: 'monday',+ },+ })+ const state = tracker.get(key)+ if (!state) throw new Error('tracker produced no state')+ return trackerStateToTaskProgress(state)+}++let connectMcpServer: ReturnType<typeof vi.fn>++beforeEach(() => {+ connectMcpServer = vi.fn(() => Promise.reject(new Error('403 not a member')))+ userMessage = { id: 'msg-1', role: 'user', content: 'hi', timestamp: 1 }+ progressByMessageId = {}+})++afterEach(() => {+ cleanup()+ vi.clearAllMocks()+ delete (window as { clerum?: unknown }).clerum+})++describe('ChatThread — connect failure (R4-L1)', () => {+ it('catches a rejecting connectMcpServer from the in-chat Connect button (no unhandled rejection)', async () => {+ progressByMessageId = { 'msg-1': await deriveConnectRequiredProgress() }+ const errSpy = vi.spyOn(console, 'error').mockImplementation(() => undefined)++ const { getByTestId } = render(<ChatThread />)++ const btn = getByTestId('connect-mcp-btn')+ await act(async () => {+ fireEvent.click(btn)+ // Let the rejected connect promise settle so the call-site `.catch` runs.+ await Promise.resolve()+ await Promise.resolve()+ })++ expect(connectMcpServer).toHaveBeenCalledWith('monday', 'agent-x')+ // The rejection is handled by the ChatThread call-site `.catch` — it does NOT+ // float as an unhandled rejection. Its observable is the error log; revert the+ // `.catch` to `void connectMcpServer(...)` and this assertion goes red.+ expect(errSpy).toHaveBeenCalledWith(+ '[connect] connectMcpServer failed',+ expect.objectContaining({ mcpServerName: 'monday' })+ )+ errSpy.mockRestore()+ })+})diff --git a/desktop-app/ui/src/components/agents/__tests__/inFlightAssistantPlaceholder.test.tsx b/desktop-app/ui/src/components/agents/__tests__/inFlightAssistantPlaceholder.test.tsxindex 2f57d1d01..5857668d2 100644--- a/desktop-app/ui/src/components/agents/__tests__/inFlightAssistantPlaceholder.test.tsx+++ b/desktop-app/ui/src/components/agents/__tests__/inFlightAssistantPlaceholder.test.tsx@@ -5,7 +5,7 @@ import {TaskTracker,makeTaskKey,} from '@contexts/AgentTaskTrackerContext'-import { cleanup, render } from '@testing-library/react'+import { act, cleanup, fireEvent, render } from '@testing-library/react'import { InFlightAssistantPlaceholder } from '../InFlightAssistantPlaceholder';(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true@@ -89,4 +89,84 @@ describe('InFlightAssistantPlaceholder (D.5)', () => {rerender(element('c2'))expect(container.querySelector('[data-task-id]')).toBeNull()})++ it('R4-L1: a rejecting connectMcpServer is caught (no unhandled rejection), not floated as `void`', async () => {+ // T1: the suspended-connect state is DERIVED from the real TaskTracker by+ // feeding the exact `suspended` stream event the live tracker consumes —+ // NOT hand-built — so `progress.suspendedInfo` has the real shape and the+ // Connect button renders as it does in production.+ let handler: ((e: unknown) => void | Promise<void>) | null = null+ const connectMcpServer = vi.fn(() => Promise.reject(new Error('403 not a member')))+ Object.defineProperty(window, 'clerum', {+ configurable: true,+ writable: true,+ value: {+ rpc: {+ subscribeTaskProgress: vi.fn(async (_h: string, _t: string, onEvent: typeof handler) => {+ handler = onEvent+ return async () => undefined+ }),+ getTaskResult: vi.fn(async () => ({ response: 'ok' })),+ cancelTask: vi.fn(async () => undefined),+ connectMcpServer,+ },+ },+ })+ const errSpy = vi.spyOn(console, 'error').mockImplementation(() => undefined)++ const tracker = new TaskTracker()+ const key = makeTaskKey('agent-x', 'c1')+ tracker.start(key, 'task-1', 'um-1')+ const emit = async (event: unknown) => {+ if (!handler) throw new Error('no progress handler registered')+ await handler(event)+ }++ const { getByTestId } = render(+ <AgentTaskTrackerContext.Provider value={tracker}>+ <InFlightAssistantPlaceholder+ agentRef="agent-x"+ chatId="c1"+ localMessageIds={new Set()}+ onCancelTask={vi.fn()}+ decideApproval={vi.fn(async () => undefined)}+ />+ </AgentTaskTrackerContext.Provider>+ )++ // Drive the task into a `connect_required` suspension through the real reducer.+ await act(async () => {+ await emit({+ type: 'suspended',+ data: {+ taskId: 'task-1',+ requestId: 'req-1',+ displayName: 'monday tool',+ reason: 'connect_required',+ mcpServerName: 'monday',+ },+ })+ })++ const btn = getByTestId('connect-mcp-btn')+ await act(async () => {+ fireEvent.click(btn)+ // Let the rejected connect promise settle so the call-site `.catch` runs.+ await Promise.resolve()+ await Promise.resolve()+ })++ expect(connectMcpServer).toHaveBeenCalledWith('monday', 'agent-x')+ // The rejection is handled by the call-site `.catch` — it does NOT float as an+ // unhandled rejection. Its observable is the error log; delete the `.catch`+ // (revert to `void connectMcpServer(...)`) and this assertion goes red.+ expect(errSpy).toHaveBeenCalledWith(+ '[connect] connectMcpServer failed',+ expect.objectContaining({ mcpServerName: 'monday' })+ )+ // The button is still mounted (the failure did not tear the stepper down) so+ // the ProgressStepper's re-enable timer can return it to an actionable state.+ expect(getByTestId('connect-mcp-btn')).not.toBeNull()+ errSpy.mockRestore()+ })})