Skip to content

The agent that grows with you

fix(desktop): keep a streamed reply over an empty committed tool shell

Austin Pickett835501f92f00
diff --git a/apps/desktop/src/app/session/hooks/use-session-actions/streamed-reply-survives-sync.test.ts b/apps/desktop/src/app/session/hooks/use-session-actions/streamed-reply-survives-sync.test.ts
new file mode 100644
index 00000000000..db066af9b05
--- /dev/null
+++ b/apps/desktop/src/app/session/hooks/use-session-actions/streamed-reply-survives-sync.test.ts
@@ -0,0 +1,102 @@
+import { describe, expect, it } from 'vitest'
+
+import { type ChatMessage, type ChatMessagePart, chatMessageText, textPart, toChatMessages } from '@/lib/chat-messages'
+
+import { preserveLocalPendingTurnMessages } from './utils'
+
+const tool = (id: string): ChatMessagePart =>
+ ({ type: 'tool-call', toolCallId: id, toolName: 'terminal', result: 'done' }) as ChatMessagePart
+
+const message = (
+ id: string,
+ role: ChatMessage['role'],
+ parts: ChatMessagePart[],
+ extra: Partial<ChatMessage> = {}
+): ChatMessage => ({ id, role, parts, ...extra }) as ChatMessage
+
+const liveUser = message('user-live', 'user', [textPart('check the model')])
+
+const settledStream = (parts: ChatMessagePart[], extra: Partial<ChatMessage> = {}) =>
+ message('assistant-stream-live', 'assistant', parts, { interim: false, pending: false, ...extra })
+
+const storedCall = { id: 'call-1', type: 'function', function: { name: 'terminal', arguments: '{}' } }
+
+// #123047: the store committed the turn as a tool round with content = '' and
+// no prose row after it, while the renderer streamed and settled the answer.
+describe('preserveLocalPendingTurnMessages — settled reply over an empty tool shell (#123047)', () => {
+ it('keeps the streamed final reply when the stored turn hydrates as a text-less tool shell', () => {
+ const next = toChatMessages([
+ { id: 196700, role: 'user', content: 'check the model', timestamp: 1 },
+ { id: 196701, role: 'assistant', content: '', tool_calls: [storedCall], timestamp: 2 },
+ { id: 196702, role: 'tool', content: 'done', tool_call_id: 'call-1', timestamp: 3 }
+ ] as never)
+
+ expect(next.map(row => chatMessageText(row))).toEqual(['check the model', ''])
+
+ const previous = [liveUser, settledStream([textPart('I checked it.'), tool('call-1'), textPart('Model is up.')])]
+ const merged = preserveLocalPendingTurnMessages(next, previous)
+
+ expect(merged).toHaveLength(2)
+ expect(merged[1]).toMatchObject({ id: 'assistant-stream-live', pending: false })
+ expect(chatMessageText(merged[1])).toContain('Model is up.')
+ })
+
+ it('carries the shell row identity onto the kept reply', () => {
+ const next = [
+ message('user-stored', 'user', [textPart('check the model')], { rowId: 196700 }),
+ message('assistant-shell', 'assistant', [tool('call-1')], { pending: false, rowId: 196701 })
+ ]
+
+ const merged = preserveLocalPendingTurnMessages(next, [liveUser, settledStream([textPart('Model is up.')])])
+
+ expect(merged).toHaveLength(2)
+ expect(merged[1]).toMatchObject({ id: 'assistant-stream-live', pending: false, rowId: 196701 })
+ })
+
+ it('yields to the committed answer once a prose row folds into the tool round (no duplicate)', () => {
+ const next = toChatMessages([
+ { id: 196700, role: 'user', content: 'check the model', timestamp: 1 },
+ { id: 196701, role: 'assistant', content: '', tool_calls: [storedCall], timestamp: 2 },
+ { id: 196702, role: 'tool', content: 'done', tool_call_id: 'call-1', timestamp: 3 },
+ { id: 196703, role: 'assistant', content: 'Model is up and serving.', timestamp: 4 }
+ ] as never)
+
+ const previous = [liveUser, settledStream([textPart('I checked it.'), tool('call-1'), textPart('Model is up.')])]
+ const merged = preserveLocalPendingTurnMessages(next, previous)
+
+ expect(merged).toEqual(next)
+ })
+
+ it('does not let settled interim narration replace the shell', () => {
+ const next = [
+ message('user-stored', 'user', [textPart('check the model')], { rowId: 196700 }),
+ message('assistant-shell', 'assistant', [tool('call-1')], { pending: false, rowId: 196701 })
+ ]
+
+ const previous = [liveUser, settledStream([textPart('Checking the logs…')], { interim: true })]
+
+ expect(preserveLocalPendingTurnMessages(next, previous)).toEqual(next)
+ })
+
+ it('does not let a structural-only local row replace the shell', () => {
+ const next = [
+ message('user-stored', 'user', [textPart('check the model')], { rowId: 196700 }),
+ message('assistant-shell', 'assistant', [tool('call-stored')], { pending: false, rowId: 196701 })
+ ]
+
+ expect(preserveLocalPendingTurnMessages(next, [liveUser, settledStream([tool('call-local')])])).toEqual(next)
+ })
+
+ it('never repaints a retained error row from the local partial', () => {
+ const next = [
+ message('user-stored', 'user', [textPart('check the model')], { rowId: 196700 }),
+ message('assistant-error', 'assistant', [tool('call-1')], {
+ error: 'provider failed',
+ pending: false,
+ rowId: 196701
+ } as Partial<ChatMessage>)
+ ]
+
+ expect(preserveLocalPendingTurnMessages(next, [liveUser, settledStream([textPart('Model is up.')])])).toEqual(next)
+ })
+})
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 c51d5fc137a..5f97a2b42d3 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
@@ -584,6 +584,28 @@ const localPendingSupersedes = (local: ChatMessage, authoritative: ChatMessage):
return localText.length > authoritativeText.length && isStrictAnswerTextExtension(localText, authoritativeText)
}
+const answerText = (message: ChatMessage) => textWithoutReferenceLines(chatMessageText(message)).trim()
+
+/**
+ * A committed tool round with no prose (`content = ''`, `tool_calls` set)
+ * hydrates as a text-less shell. When no later prose row folds into it, the
+ * settled local final bubble is the only copy of the answer the user watched
+ * stream, and dropping it for the shell deletes the reply on sync (#123047).
+ * Interim narration and structural-only local rows never claim the slot, and
+ * a retained error row is never a shell. Kept apart from
+ * `localPendingSupersedes`, whose stricter live-only rule also gates resume.
+ */
+const settledReplyOverEmptyShell = (local: ChatMessage, authoritative: ChatMessage): boolean =>
+ local.role === 'assistant' &&
+ local.pending !== true &&
+ local.interim !== true &&
+ !local.error &&
+ authoritative.role === 'assistant' &&
+ !isLiveTailRow(authoritative) &&
+ !authoritative.error &&
+ answerText(authoritative).length === 0 &&
+ answerText(local).length > 0
+
/**
* Take the cached row's content, but never its liveness. The renderer holds the
* only copy of the streamed parts; the gateway remains the authority on whether
@@ -931,8 +953,9 @@ export function preserveLocalPendingTurnMessages(
if (isPendingAssistant) {
// Keep the local pending row when it is the same reply further along
// and the authoritative row is an empty projection shell or a prefix.
- // #75825
- if (!localPendingSupersedes(message, authoritative)) {
+ // #75825. A settled final over a committed text-less tool shell is
+ // the only copy of the answer (#123047).
+ if (!localPendingSupersedes(message, authoritative) && !settledReplyOverEmptyShell(message, authoritative)) {
continue
}