Skip to content

The agent that grows with you

refactor(desktop): move the overlay's tool-call merge into tool-parts

Austin Pickettb0cdaa1ea31a
diff --git a/apps/desktop/src/app/session/hooks/use-session-actions/utils-overlay-tool-calls.test.ts b/apps/desktop/src/app/session/hooks/use-session-actions/utils-overlay-tool-calls.test.ts
new file mode 100644
index 00000000000..6df817fdfb9
--- /dev/null
+++ b/apps/desktop/src/app/session/hooks/use-session-actions/utils-overlay-tool-calls.test.ts
@@ -0,0 +1,45 @@
+import { describe, expect, it } from 'vitest'
+
+import type { ChatMessage } from '@/lib/chat-messages'
+
+import { overlayConcurrentMessageChanges } from './utils'
+
+describe('overlayConcurrentMessageChanges tool calls', () => {
+ // A re-delivered open_requests marks the cached activation row "changed";
+ // the raw concat used to carry the same clarify call twice, interleaved
+ // with reasoning that ran between the copies.
+ it('does not duplicate a tool call the re-fetched row already carries when merging the prefix', () => {
+ const authoritative = [
+ {
+ id: 'assistant-stream-activation',
+ role: 'assistant',
+ parts: [
+ { type: 'text', text: 'partial A' },
+ { type: 'tool-call', toolCallId: 'call-clarify', toolName: 'clarify' }
+ ],
+ pending: true
+ }
+ ] as ChatMessage[]
+
+ const current = [
+ {
+ id: 'assistant-stream-runtime',
+ role: 'assistant',
+ parts: [
+ { type: 'tool-call', toolCallId: 'call-clarify', toolName: 'clarify' },
+ { type: 'reasoning', text: 'mid reasoning' },
+ { type: 'text', text: ' + delta B' }
+ ],
+ pending: true
+ }
+ ] as ChatMessage[]
+
+ const overlaid = overlayConcurrentMessageChanges(authoritative, [], current)
+
+ expect(overlaid.map(message => message.id)).toEqual(['assistant-stream-runtime'])
+ const toolCallIds = overlaid[0].parts.flatMap(part => (part.type === 'tool-call' ? [part.toolCallId] : []))
+ expect(toolCallIds).toEqual(['call-clarify'])
+ // The activation side still contributes what the fresh row lacks.
+ expect(overlaid[0].parts[0]).toEqual({ type: 'text', text: 'partial A' })
+ })
+})
diff --git a/apps/desktop/src/app/session/hooks/use-session-actions/utils.test.ts b/apps/desktop/src/app/session/hooks/use-session-actions/utils.test.ts
index d9c07241184..eff319eb6a4 100644
--- a/apps/desktop/src/app/session/hooks/use-session-actions/utils.test.ts
+++ b/apps/desktop/src/app/session/hooks/use-session-actions/utils.test.ts
@@ -2106,44 +2106,6 @@ describe('overlayConcurrentMessageChanges', () => {
])
})
- // A re-delivered open_requests marks the cached activation row "changed";
- // the raw concat used to carry the same clarify call twice, interleaved
- // with reasoning that ran between the copies.
- it('does not duplicate a tool call the re-fetched row already carries when merging the prefix', () => {
- const authoritative = [
- {
- id: 'assistant-stream-activation',
- role: 'assistant',
- parts: [
- { type: 'text', text: 'partial A' },
- { type: 'tool-call', toolCallId: 'call-clarify', toolName: 'clarify' }
- ],
- pending: true
- }
- ] as ChatMessage[]
-
- const current = [
- {
- id: 'assistant-stream-runtime',
- role: 'assistant',
- parts: [
- { type: 'tool-call', toolCallId: 'call-clarify', toolName: 'clarify' },
- { type: 'reasoning', text: 'mid reasoning' },
- { type: 'text', text: ' + delta B' }
- ],
- pending: true
- }
- ] as ChatMessage[]
-
- const overlaid = overlayConcurrentMessageChanges(authoritative, [], current)
-
- expect(overlaid.map(message => message.id)).toEqual(['assistant-stream-runtime'])
- const toolCallIds = overlaid[0].parts.flatMap(part => (part.type === 'tool-call' ? [part.toolCallId] : []))
- expect(toolCallIds).toEqual(['call-clarify'])
- // The activation side still contributes what the fresh row lacks.
- expect(overlaid[0].parts[0]).toEqual({ type: 'text', text: 'partial A' })
- })
-
// Switch back to a chat mid-reply: session.activate snapshots the first
// chunk, message.complete settles the live row, THEN the gated REST page
// resolves with the committed reply. Warm-activation composition order.
diff --git a/apps/desktop/src/app/session/hooks/use-session-actions/utils.ts b/apps/desktop/src/app/session/hooks/use-session-actions/utils.ts
index d3d49317c9a..87288fd254f 100644
--- a/apps/desktop/src/app/session/hooks/use-session-actions/utils.ts
+++ b/apps/desktop/src/app/session/hooks/use-session-actions/utils.ts
@@ -3,7 +3,7 @@ import { mergeUsage } from '@hermes/shared'
import { resolveSessionRpcOwner } from '@/app/contrib/wiring-routing'
import { textWithoutReferenceLines } from '@/components/assistant-ui/reference-kinds'
import { getSession } from '@/hermes'
-import { sameAttachmentTurn, spliceOlderPreservedRows } from '@/lib/chat-messages'
+import { concatPartsByToolCall, sameAttachmentTurn, spliceOlderPreservedRows } from '@/lib/chat-messages'
import {
assistantTextPart,
type ChatMessage,
@@ -1595,25 +1595,6 @@ export function removeRepresentedLocalLiveProjection(
)
}
-/**
- * Concatenate an activation row's parts with the re-fetched row's parts, but
- * let the fresher side win per tool call: a re-delivered `open_requests` marks
- * the cached row changed, and a raw concat would render the same call twice —
- * interleaved with reasoning from between the copies. Non-tool parts concat
- * unchanged.
- */
-function mergeActivationStreamParts(activationParts: ChatMessage['parts'], currentParts: ChatMessage['parts']) {
- const deliveredIds = new Set(
- currentParts.flatMap(part => (part.type === 'tool-call' ? [part.toolCallId] : []))
- )
-
- const activationTail = activationParts.filter(
- part => !(part.type === 'tool-call' && deliveredIds.has(part.toolCallId))
- )
-
- return [...activationTail, ...currentParts]
-}
-
/**
* Overlay messages that changed while activation waited on REST. Existing ids
* replace the older activation row; only rows added or changed since the warm
@@ -1747,7 +1728,7 @@ export function overlayConcurrentMessageChanges(
const replacement =
activationText && !currentText.startsWith(activationText)
- ? { ...current, parts: mergeActivationStreamParts(activationStream.parts, current.parts) }
+ ? { ...current, parts: concatPartsByToolCall(activationStream.parts, current.parts) }
: current
nextIndexById.delete(activationStream.id)
diff --git a/apps/desktop/src/lib/chat-messages/index.ts b/apps/desktop/src/lib/chat-messages/index.ts
index d0b90e69989..ca2f2b650f1 100644
--- a/apps/desktop/src/lib/chat-messages/index.ts
+++ b/apps/desktop/src/lib/chat-messages/index.ts
@@ -27,6 +27,7 @@ export {
spliceOlderPreservedRows
} from './reconciliation'
export {
+ concatPartsByToolCall,
hasUnresolvedToolCall,
QUESTION_CARD_TOOLS,
restorePendingBlockingToolCall,
diff --git a/apps/desktop/src/lib/chat-messages/tool-parts.ts b/apps/desktop/src/lib/chat-messages/tool-parts.ts
index ebb7651f9e4..4c7cfcf562b 100644
--- a/apps/desktop/src/lib/chat-messages/tool-parts.ts
+++ b/apps/desktop/src/lib/chat-messages/tool-parts.ts
@@ -921,6 +921,20 @@ export function storedToolMessagePart(toolMessage: SessionMessage, fallbackIndex
}
}
+/**
+ * Concatenate two part lists so each tool call appears once: an earlier
+ * tool-call part is dropped when the later list carries the same id, so the
+ * fresher copy wins. Other parts concatenate unchanged. Activation uses this
+ * when it merges a cached stream row into the re-fetched one: a re-delivered
+ * `open_requests` marks the cached row changed, and a raw concat rendered the
+ * same call twice, with reasoning from between the copies.
+ */
+export function concatPartsByToolCall(earlier: ChatMessagePart[], later: ChatMessagePart[]): ChatMessagePart[] {
+ const laterIds = new Set(later.flatMap(part => (part.type === 'tool-call' ? [part.toolCallId] : [])))
+
+ return [...earlier.filter(part => !(part.type === 'tool-call' && laterIds.has(part.toolCallId))), ...later]
+}
+
export function withUniqueToolCallIds(messages: ChatMessage[]): ChatMessage[] {
return messages.map(message => {
const seen = new Set<string>()