The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): dedupe duplicate toolCallId parts at the runtime boundary (#87857)
PRATHAMESH759f8dca34dc2a
diff --git a/apps/desktop/src/app/chat/runtime-repository.ts b/apps/desktop/src/app/chat/runtime-repository.tsindex 39b4d0c1d18..f15329afa1e 100644--- a/apps/desktop/src/app/chat/runtime-repository.ts+++ b/apps/desktop/src/app/chat/runtime-repository.ts@@ -3,6 +3,7 @@ import type { ExportedMessageRepository, ThreadMessage } from '@assistant-ui/reaimport { useMemo, useRef } from 'react'import type { ChatMessage } from '@/lib/chat-messages'+import { withUniqueToolCallIdsWithinMessage } from '@/lib/chat-messages'import { coalesceToolOnlyAssistants, createToolMergeCache, toRuntimeMessage } from '@/lib/chat-runtime'// The exact fallback status ExportedMessageRepository.fromBranchableArray uses.@@ -56,10 +57,17 @@ export function useRuntimeMessageRepository(messages: ChatMessage[]): ExportedMeparentId = branchParentByGroup.get(message.branchGroupId) ?? null}+ // Guard against two `tool-call` parts of one message sharing a+ // `toolCallId`: assistant-ui's `useResources` throws on the duplicate key+ // and crash-loops the renderer (#87857). Same class of defensive dedup as+ // the repeated-`message.id` skip above, one level down at the parts. Keeps+ // identity when clean, so the cache below is unaffected in the common case.+ const deduped = withUniqueToolCallIdsWithinMessage(message)+const cachedMessage = cacheRef.current.get(message)const runtimeMessage =- cachedMessage ?? fromThreadMessageLike(toRuntimeMessage(message), message.id, FALLBACK_STATUS)+ cachedMessage ?? fromThreadMessageLike(toRuntimeMessage(deduped), message.id, FALLBACK_STATUS)if (!cachedMessage) {cacheRef.current.set(message, runtimeMessage)diff --git a/apps/desktop/src/lib/chat-messages.test.ts b/apps/desktop/src/lib/chat-messages.test.tsindex 38bc2aca889..d57dab42298 100644--- a/apps/desktop/src/lib/chat-messages.test.ts+++ b/apps/desktop/src/lib/chat-messages.test.ts@@ -15,9 +15,45 @@ import {renderMediaTags,sealOpenToolParts,toChatMessages,- upsertToolPart+ upsertToolPart,+ withUniqueToolCallIdsWithinMessage} from './chat-messages'+const toolCallPart = (toolCallId: string): ChatMessagePart =>+ ({ type: 'tool-call' as const, toolCallId, toolName: 'read_file', args: {} as never, argsText: '{}' }) as ChatMessagePart++const assistantWith = (parts: ChatMessagePart[]): ChatMessage =>+ ({ id: 'm1', role: 'assistant', parts, timestamp: 0 }) as unknown as ChatMessage++describe('withUniqueToolCallIdsWithinMessage', () => {+ it('renames a duplicate toolCallId within one message (#87857)', () => {+ const message = assistantWith([toolCallPart('call_x'), toolCallPart('call_x')])++ const result = withUniqueToolCallIdsWithinMessage(message)++ const ids = result.parts+ .filter((part): part is Extract<ChatMessagePart, { type: 'tool-call' }> => part.type === 'tool-call')+ .map(part => part.toolCallId)++ expect(new Set(ids).size).toBe(ids.length)+ expect(ids[0]).toBe('call_x')+ expect(ids[1]).not.toBe('call_x')+ })++ it('returns the same reference when there is no duplicate', () => {+ const message = assistantWith([toolCallPart('call_a'), toolCallPart('call_b')])++ expect(withUniqueToolCallIdsWithinMessage(message)).toBe(message)+ })++ it('does not touch parts without a toolCallId', () => {+ const textPart = { type: 'text' as const, text: 'hi' } as ChatMessagePart+ const message = assistantWith([textPart, toolCallPart('call_a')])++ expect(withUniqueToolCallIdsWithinMessage(message)).toBe(message)+ })+})+describe('toChatMessages', () => {it('rebuilds the full command from a gateway tool row carrying args', () => {// Gateway watch-window hydration projects tool rows asdiff --git a/apps/desktop/src/lib/chat-messages/index.ts b/apps/desktop/src/lib/chat-messages/index.tsindex 259a607a046..d69c676a7d5 100644--- a/apps/desktop/src/lib/chat-messages/index.ts+++ b/apps/desktop/src/lib/chat-messages/index.ts@@ -14,5 +14,5 @@ export {} from './parts'export type { UnspokenTurnSpeech } from './parts'export { branchGroupForUser, preserveLocalAssistantErrors } from './reconciliation'-export { sealOpenToolParts, upsertToolPart } from './tool-parts'+export { sealOpenToolParts, upsertToolPart, withUniqueToolCallIdsWithinMessage } from './tool-parts'export type { ChatMessage, ChatMessagePart, GatewayEventPayload, TimelinePartMetadata } from './types'diff --git a/apps/desktop/src/lib/chat-messages/tool-parts.ts b/apps/desktop/src/lib/chat-messages/tool-parts.tsindex 6399e09caba..835a0eda7e9 100644--- a/apps/desktop/src/lib/chat-messages/tool-parts.ts+++ b/apps/desktop/src/lib/chat-messages/tool-parts.ts@@ -577,3 +577,53 @@ export function withUniqueToolCallIds(messages: ChatMessage[]): ChatMessage[] {return changed ? { ...message, parts } : message})}++/**+ * Ensure no two `tool-call` parts of a SINGLE message share a `toolCallId`.+ *+ * assistant-ui's `useResources` derives one resource key per content part+ * (`toolCallId-<id>`) and throws `Duplicate key <key> in useResources` on a+ * collision, which the desktop error boundary turns into a renderer crash loop+ * that blanks the window (#87857). Two paths can produce a message whose parts+ * carry the same id: the streaming reducer appends the same tool-call part+ * twice under a specific optimistic-update ordering, and coalescing tool-only+ * assistant turns can fold two parts with the same id into one message. Neither+ * passes through {@link withUniqueToolCallIds} — that runs only on the static+ * `toChatMessages` output, not the live runtime boundary — so the dedup is+ * applied again at the point ChatMessages are converted for the runtime.+ *+ * The scope is deliberately per-message (a fresh seen-set each call): the+ * assistant-ui key space is per-message, so a `toolCallId` shared across+ * different messages is not a collision and must not be renamed. Only the+ * later duplicate within one message is renamed, mirroring the list-level+ * helper. Returns the same reference when nothing changes, so the runtime+ * repository's identity cache is preserved for the common no-duplicate case.+ */+export function withUniqueToolCallIdsWithinMessage(message: ChatMessage): ChatMessage {+ let seen: null | Set<string> = null+ let changed = false++ const parts = message.parts.map((part, index) => {+ if (part.type !== 'tool-call' || !part.toolCallId) {+ return part+ }++ if (seen === null) {+ seen = new Set<string>()+ }++ if (!seen.has(part.toolCallId)) {+ seen.add(part.toolCallId)++ return part+ }++ changed = true+ const uniqueId = `${part.toolCallId}-dup-${index}`+ seen.add(uniqueId)++ return { ...part, toolCallId: uniqueId } as ChatMessagePart+ })++ return changed ? { ...message, parts } : message+}