Skip to content

The agent that grows with you

fix(desktop): keep two calls that share an id apart in the overlay merge

Austin Pickettee8335aed24b
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 b9b8fbf695d..f27c87a7b71 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
@@ -74,13 +74,15 @@ describe('overlayConcurrentMessageChanges tool calls', () => {
})
it('keeps the copy that carries a result', () => {
+ const args = { questions: [{ question: 'Which deployment target?' }] }
+
const parts = merge(
[
{ type: 'text', text: 'partial A' },
- { type: 'tool-call', toolCallId: 'call-clarify', toolName: 'clarify', result: { answer: 'staging' } }
+ { type: 'tool-call', toolCallId: 'call-clarify', toolName: 'clarify', args, result: { answer: 'staging' } }
] as ChatMessage['parts'],
[
- { type: 'tool-call', toolCallId: 'call-clarify', toolName: 'clarify' },
+ { type: 'tool-call', toolCallId: 'call-clarify', toolName: 'clarify', args },
{ type: 'text', text: ' + delta B' }
] as ChatMessage['parts']
)
@@ -89,6 +91,49 @@ describe('overlayConcurrentMessageChanges tool calls', () => {
expect(parts[1]).toMatchObject({ result: { answer: 'staging' } })
})
+ it('keeps a running call that shares a finished call of the same tool its id', () => {
+ const parts = merge(
+ [
+ { type: 'text', text: 'partial A' },
+ { type: 'tool-call', toolCallId: 'call_0', toolName: 'terminal', args: { command: 'ls' }, result: 'ok' }
+ ] as ChatMessage['parts'],
+ [
+ { type: 'tool-call', toolCallId: 'call_0', toolName: 'terminal', args: { command: 'pwd' } },
+ { type: 'text', text: ' + delta B' }
+ ] as ChatMessage['parts']
+ )
+
+ expect(calls(parts)).toEqual(['terminal:call_0', 'terminal:call_0'])
+ expect(parts.filter(part => part.type === 'tool-call' && part.result === undefined)).toHaveLength(1)
+ })
+
+ it('keeps an open clarify that shares an answered clarify its id', () => {
+ const parts = merge(
+ [
+ { type: 'text', text: 'partial A' },
+ {
+ type: 'tool-call',
+ toolCallId: 'call_0',
+ toolName: 'clarify',
+ args: { questions: [{ question: 'Which deployment target?' }] },
+ result: { answer: 'staging' }
+ }
+ ] as ChatMessage['parts'],
+ [
+ {
+ type: 'tool-call',
+ toolCallId: 'call_0',
+ toolName: 'clarify',
+ args: { questions: [{ question: 'Which region?' }] }
+ },
+ { type: 'text', text: ' + delta B' }
+ ] as ChatMessage['parts']
+ )
+
+ expect(calls(parts)).toEqual(['clarify:call_0', 'clarify:call_0'])
+ expect(parts.filter(part => part.type === 'tool-call' && part.result === undefined)).toHaveLength(1)
+ })
+
it('keeps the call where the activation page had it', () => {
const parts = merge(
[
diff --git a/apps/desktop/src/lib/chat-messages/tool-parts.ts b/apps/desktop/src/lib/chat-messages/tool-parts.ts
index 3086562c1f8..3a06b113fc9 100644
--- a/apps/desktop/src/lib/chat-messages/tool-parts.ts
+++ b/apps/desktop/src/lib/chat-messages/tool-parts.ts
@@ -974,33 +974,33 @@ export function storedToolMessagePart(toolMessage: SessionMessage, fallbackIndex
* 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.
+ * Two parts are one call when tool name and id match and either their args
+ * match or neither has finished: ids repeat across calls (llama.cpp sends one
+ * constant id), and a finished call must not replace a running call, or an
+ * answered clarify an open one, 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 callKey = (part: ChatMessagePart) =>
- part.type === 'tool-call' && part.toolCallId ? `${part.toolName}\u0000${part.toolCallId}` : null
-
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)
+ const argsOf = (part: ChatMessagePart) =>
+ part.type === 'tool-call' ? (part.argsText ?? JSON.stringify(part.args ?? {})) : ''
- if (key && !laterByKey.has(key)) {
- laterByKey.set(key, part)
- }
- }
+ const sameCall = (a: ChatMessagePart, b: ChatMessagePart) =>
+ a.type === 'tool-call' &&
+ b.type === 'tool-call' &&
+ Boolean(a.toolCallId) &&
+ a.toolCallId === b.toolCallId &&
+ a.toolName === b.toolName &&
+ (argsOf(a) === argsOf(b) || (!hasResult(a) && !hasResult(b)))
const placed = new Set<ChatMessagePart>()
const merged = earlier.map(part => {
- const key = callKey(part)
- const live = key ? laterByKey.get(key) : undefined
+ const live = later.find(candidate => !placed.has(candidate) && sameCall(part, candidate))
- if (!live || placed.has(live)) {
+ if (!live) {
return part
}