Skip to content

The agent that grows with you

fix(desktop): keep another tool's call and the answered copy in the overlay merge

Austin Pickett35a123970543
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
index 6df817fdfb9..b9b8fbf695d 100644
--- 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
@@ -8,7 +8,7 @@ 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', () => {
+ it('does not duplicate a tool call the live row already carries when merging the prefix', () => {
const authoritative = [
{
id: 'assistant-stream-activation',
@@ -39,7 +39,74 @@ describe('overlayConcurrentMessageChanges tool calls', () => {
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.
+ // The activation side still contributes what the live row lacks.
expect(overlaid[0].parts[0]).toEqual({ type: 'text', text: 'partial A' })
})
+
+ // The activation page's stream row and the live store's copy of the same turn.
+ const merge = (page: ChatMessage['parts'], live: ChatMessage['parts']) =>
+ overlayConcurrentMessageChanges(
+ [{ id: 'assistant-stream-activation', role: 'assistant', parts: page, pending: true }],
+ [],
+ [{ id: 'assistant-stream-runtime', role: 'assistant', parts: live, pending: true }]
+ )[0].parts
+
+ const calls = (parts: ChatMessage['parts']) =>
+ parts.flatMap(part => (part.type === 'tool-call' ? [`${part.toolName}:${part.toolCallId}`] : []))
+
+ it('keeps an answered clarify when a later call of another tool reuses its id', () => {
+ // llama.cpp sends one constant id for every call.
+ const parts = merge(
+ [
+ { type: 'text', text: 'partial A' },
+ { type: 'tool-call', toolCallId: 'call_0', toolName: 'clarify', result: { answer: 'staging' } }
+ ] as ChatMessage['parts'],
+ [
+ { type: 'tool-call', toolCallId: 'call_0', toolName: 'terminal' },
+ { type: 'text', text: ' + delta B' }
+ ] as ChatMessage['parts']
+ )
+
+ expect(calls(parts)).toEqual(['clarify:call_0', 'terminal:call_0'])
+ expect(parts.find(part => part.type === 'tool-call' && part.toolName === 'clarify')).toMatchObject({
+ result: { answer: 'staging' }
+ })
+ })
+
+ it('keeps the copy that carries a result', () => {
+ const parts = merge(
+ [
+ { type: 'text', text: 'partial A' },
+ { type: 'tool-call', toolCallId: 'call-clarify', toolName: 'clarify', result: { answer: 'staging' } }
+ ] as ChatMessage['parts'],
+ [
+ { type: 'tool-call', toolCallId: 'call-clarify', toolName: 'clarify' },
+ { type: 'text', text: ' + delta B' }
+ ] as ChatMessage['parts']
+ )
+
+ expect(calls(parts)).toEqual(['clarify:call-clarify'])
+ expect(parts[1]).toMatchObject({ result: { answer: 'staging' } })
+ })
+
+ it('keeps the call where the activation page had it', () => {
+ const parts = merge(
+ [
+ { type: 'text', text: 'partial A' },
+ { type: 'tool-call', toolCallId: 'call-clarify', toolName: 'clarify' },
+ { type: 'reasoning', text: 'after the call' }
+ ] as ChatMessage['parts'],
+ [
+ { type: 'tool-call', toolCallId: 'call-clarify', toolName: 'clarify' },
+ { type: 'text', text: ' + delta B' }
+ ] as ChatMessage['parts']
+ )
+
+ expect(parts.map(part => (part.type === 'tool-call' ? part.toolName : part.type))).toEqual([
+ 'text',
+ 'clarify',
+ 'reasoning',
+ 'text'
+ ])
+ })
})
diff --git a/apps/desktop/src/lib/chat-messages/tool-parts.ts b/apps/desktop/src/lib/chat-messages/tool-parts.ts
index 45f812249e6..3086562c1f8 100644
--- a/apps/desktop/src/lib/chat-messages/tool-parts.ts
+++ b/apps/desktop/src/lib/chat-messages/tool-parts.ts
@@ -970,17 +970,46 @@ 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.
+ * Merge an activation page's stream row (`earlier`) with the live store's copy
+ * of that turn (`later`) so each tool call appears once. A call keeps its place
+ * in `earlier`, so text and reasoning that followed it there still follow it,
+ * and the live copy's fields win unless only the page's copy carries a result.
+ * Calls match on id and tool name: ids repeat across calls (llama.cpp sends one
+ * constant id), and a later `terminal` call must not delete an answered clarify
+ * that shares its id. A re-delivered `open_requests` marks the cached row
+ * changed, and a raw concat rendered the same call twice.
*/
export function concatPartsByToolCall(earlier: ChatMessagePart[], later: ChatMessagePart[]): ChatMessagePart[] {
- const laterIds = new Set(later.flatMap(part => (part.type === 'tool-call' ? [part.toolCallId] : [])))
+ const callKey = (part: ChatMessagePart) =>
+ part.type === 'tool-call' && part.toolCallId ? `${part.toolName}\u0000${part.toolCallId}` : null
- return [...earlier.filter(part => !(part.type === 'tool-call' && laterIds.has(part.toolCallId))), ...later]
+ const hasResult = (part: ChatMessagePart) => part.type === 'tool-call' && part.result !== undefined
+ const laterByKey = new Map<string, ChatMessagePart>()
+
+ for (const part of later) {
+ const key = callKey(part)
+
+ if (key && !laterByKey.has(key)) {
+ laterByKey.set(key, part)
+ }
+ }
+
+ const placed = new Set<ChatMessagePart>()
+
+ const merged = earlier.map(part => {
+ const key = callKey(part)
+ const live = key ? laterByKey.get(key) : undefined
+
+ if (!live || placed.has(live)) {
+ return part
+ }
+
+ placed.add(live)
+
+ return hasResult(part) && !hasResult(live) ? part : live
+ })
+
+ return [...merged, ...later.filter(part => !placed.has(part))]
}
export function withUniqueToolCallIds(messages: ChatMessage[]): ChatMessage[] {