Skip to content

The agent that grows with you

fix(desktop): preserve current response ownership on completion

BearHuddlestonbf4f92b68863
diff --git a/apps/desktop/src/app/session/hooks/use-message-stream/completion-projections.test.tsx b/apps/desktop/src/app/session/hooks/use-message-stream/completion-projections.test.tsx
new file mode 100644
index 00000000000..e4fc73ce11d
--- /dev/null
+++ b/apps/desktop/src/app/session/hooks/use-message-stream/completion-projections.test.tsx
@@ -0,0 +1,114 @@
+import type { GatewayEventName } from '@hermes/shared'
+import { act, cleanup } from '@testing-library/react'
+import { afterEach, beforeEach, expect, it, vi } from 'vitest'
+
+import { chatMessageText, textPart } from '@/lib/chat-messages'
+import { createClientSessionState } from '@/lib/chat-runtime'
+
+import { appendLiveSessionProjection } from '../use-session-actions/utils'
+
+import { renderMessageStream } from './test-harness'
+
+const SID = 'completion-projections'
+const STORED = 'completion-projections-stored'
+
+beforeEach(() => vi.useFakeTimers())
+afterEach(() => {
+ cleanup()
+ vi.clearAllTimers()
+ vi.useRealTimers()
+ vi.restoreAllMocks()
+})
+
+function mount() {
+ const initial = createClientSessionState(STORED, [
+ { id: 'user-current', role: 'user', parts: [textPart('Inspect the file')] }
+ ])
+
+ const stream = renderMessageStream(SID, { states: new Map([[SID, initial]]) })
+
+ const send = (type: GatewayEventName, payload: Record<string, unknown> = {}) =>
+ act(() => stream.handleEvent({ type, payload, session_id: SID }))
+
+ return { stream, send }
+}
+
+it.each([false, true])('settles before a queued next-turn prompt (fallback=%s)', async fallback => {
+ const { stream, send } = mount()
+ await send('message.start')
+ await send('message.delta', { text: 'Current partial' })
+ await act(async () => {
+ await vi.advanceTimersByTimeAsync(100)
+ })
+ const current = stream.state()
+ const streamId = current.streamId
+ expect(streamId).not.toBeNull()
+
+ // Use the same queue projection as session.resume; the pending response
+ // still belongs to the current turn, not to this next-turn user row.
+ const projected = appendLiveSessionProjection(current.messages, {
+ session_id: SID,
+ queued: { user: 'Run this next' }
+ })
+
+ stream.states.set(SID, { ...current, messages: projected, streamId: fallback ? null : streamId })
+ const timeline = () => stream.state().messages.map(message => [message.role, chatMessageText(message)])
+
+ const expected = [
+ ['user', 'Inspect the file'],
+ ['assistant', 'Current complete answer'],
+ ['user', 'Run this next']
+ ]
+
+ await send('message.complete', { text: 'Current complete answer' })
+ expect(timeline()).toEqual(expected)
+ expect(stream.state().messages.find(message => message.id === streamId)?.pending).toBe(false)
+ const ids = stream.state().messages.map(message => message.id)
+ await send('message.complete', { text: 'Current complete answer' })
+ expect(timeline()).toEqual(expected)
+ expect(stream.state().messages.map(message => message.id)).toEqual(ids)
+
+ // A real next-turn start with no delta must not reuse the previous answer,
+ // even if the backend hasn't replaced the projected queued-user row yet.
+ await send('message.start')
+ await send('message.complete', { text: 'Current complete answer' })
+ expect(timeline()).toEqual([...expected, ['assistant', 'Current complete answer']])
+})
+
+it.each([
+ { partial: 'Partial answer', final: 'Partial answer' },
+ { partial: 'Partial ans', final: 'Partial answer from the retained buffer' },
+ { partial: 'Checking the file.', final: 'Checking the file.' }
+])('merges the cumulative partial-error buffer once ($partial → $final)', async fixture => {
+ const { stream, send } = mount()
+ await send('message.start')
+ // display.interim_assistant_messages=false keeps these responses in one
+ // bubble; the gateway's terminal error carries the whole inflight buffer.
+ await send('message.delta', { text: 'Checking the file.' })
+ await send('tool.start', { name: 'read_file', tool_id: 'read-1', args: {} })
+ await send('tool.complete', { name: 'read_file', tool_id: 'read-1', result: 'fixture' })
+ await send('message.delta', { text: `\n\n${fixture.partial}` })
+
+ const finalText = `Checking the file.\n\n${fixture.final}`
+
+ const completion = {
+ status: 'error',
+ error: 'dispatcher failure',
+ partial: true,
+ recoverable: true,
+ text: finalText
+ }
+
+ await send('message.complete', completion)
+ const assistants = () => stream.state().messages.filter(message => message.role === 'assistant')
+ expect(assistants().map(chatMessageText).join('')).toBe(finalText)
+ expect(assistants()).toHaveLength(1)
+ expect(assistants()[0]).toMatchObject({ error: 'dispatcher failure', pending: false })
+ expect(assistants()[0].parts.filter(part => part.type === 'tool-call')).toMatchObject([
+ { toolCallId: 'read-1', completedAt: expect.any(Number), result: 'fixture' }
+ ])
+ const ids = stream.state().messages.map(message => message.id)
+ await send('message.complete', completion)
+ expect(assistants().map(chatMessageText).join('')).toBe(finalText)
+ expect(stream.state().messages.map(message => message.id)).toEqual(ids)
+})
diff --git a/apps/desktop/src/app/session/hooks/use-message-stream/index.ts b/apps/desktop/src/app/session/hooks/use-message-stream/index.ts
index 19b94851bd4..4d2cbfbddf8 100644
--- a/apps/desktop/src/app/session/hooks/use-message-stream/index.ts
+++ b/apps/desktop/src/app/session/hooks/use-message-stream/index.ts
@@ -546,8 +546,8 @@ export function useMessageStream({
// A later response can share this bubble after a suppressed interim.
// A seal arriving after its tools, without any newer text, still
// confirms the pre-tool response (legacy/delayed seal ordering).
- const hasNewResponse = currentResponseParts(parts).some(part =>
- (part.type === 'text' || part.type === 'reasoning') && part.text.trim()
+ const hasNewResponse = currentResponseParts(parts).some(
+ part => (part.type === 'text' || part.type === 'reasoning') && part.text.trim()
)
return hasNewResponse
@@ -648,7 +648,9 @@ export function useMessageStream({
const replaceTextPart = (parts: ChatMessagePart[], interim: boolean) => {
const visibleFinalText = stripGeneratedImageEchoes(finalText, generatedImageEchoSources(parts)).trim()
- return interim
+ // Partial terminal errors carry the whole retained assistant buffer,
+ // not just the response after the last tool (unlike healthy finals).
+ return interim || keepFailedPartialText
? mergeFinalAssistantText(parts, visibleFinalText, occurredAt)
: mergeCurrentResponseText(parts, visibleFinalText, occurredAt)
}
@@ -698,7 +700,14 @@ export function useMessageStream({
// A new prompt or correction starts another occurrence, even when its
// text (or answer) repeats. Hidden user rows are boundaries too.
- const lastUserIndex = prev.findLastIndex(message => message.role === 'user')
+ // A projected queued prompt is for the NEXT turn while this response
+ // exists. message.start clears payload/interim ownership so a queued
+ // turn completing without deltas still starts its own occurrence.
+ const hasCurrentResponse = Boolean(streamId || state.sawAssistantPayload || interimBoundaryPending)
+
+ const lastUserIndex = prev.findLastIndex(
+ message => message.role === 'user' && !(hasCurrentResponse && message.id === `user-queued-${sessionId}`)
+ )
const streamIndex = streamId
? prev.findIndex((message, index) => index > lastUserIndex && message.id === streamId)
@@ -727,7 +736,7 @@ export function useMessageStream({
const existingText = chatMessageText({
...existing,
- parts: existing.interim ? existing.parts : currentResponseParts(existing.parts)
+ parts: existing.interim || keepFailedPartialText ? existing.parts : currentResponseParts(existing.parts)
}).trim()
// The last assistant row is a sealed interim (a tool-call turn or a