Skip to content

The agent that grows with you

fix(desktop): bind the optimistic prompt to the completion receipt's stored row

Tekniumd74c20ea6155
diff --git a/apps/desktop/src/app/session/hooks/use-message-stream/completion-boundaries.test.tsx b/apps/desktop/src/app/session/hooks/use-message-stream/completion-boundaries.test.tsx
index 59d8897cce4..ede73c9cd7d 100644
--- a/apps/desktop/src/app/session/hooks/use-message-stream/completion-boundaries.test.tsx
+++ b/apps/desktop/src/app/session/hooks/use-message-stream/completion-boundaries.test.tsx
@@ -4,6 +4,7 @@ import { act, cleanup, renderHook } from '@testing-library/react'
import { useRef } from 'react'
import { afterEach, beforeEach, expect, it, vi } from 'vitest'
+import { graftRefreshedTailOntoBackfill } from '@/app/chat/transcript-backfill'
import { type ChatMessage, chatMessageText, toChatMessages } from '@/lib/chat-messages'
import {
clearInFlightTurnJournal,
@@ -179,6 +180,34 @@ it.each([true, false, undefined])(
}
)
+// The session authority acknowledges a queued admission before the prompt row
+// exists, so the optimistic prompt has no row id until the completion receipt
+// names it. A stored page read after the turn must replace that bubble, not
+// paint the prompt a second time beside its stored copy.
+it('binds the optimistic prompt to the receipt row when the submit acknowledgement named none', async () => {
+ const h = mount()
+ await h.submit()
+ expect(h.state().messages[0].rowId).toBeUndefined()
+ await h.send('message.start')
+ await h.send('message.delta', { text: ANSWER })
+ await h.send('message.complete', {
+ text: ANSWER,
+ persisted_turn: { row_ids: [71, 72], user_row_id: 71, final_assistant_row_id: 72, complete: true }
+ })
+
+ const latestPage = toChatMessages([
+ { id: 71, role: 'user', content: 'Give the answer.' },
+ { id: 72, role: 'assistant', content: ANSWER }
+ ])
+
+ // The switch-back read grafts the stored page onto the window by row id.
+ expect(timeline(graftRefreshedTailOntoBackfill(latestPage, h.state().messages))).toEqual([
+ ['user', 'Give the answer.'],
+ ['assistant', ANSWER]
+ ])
+ h.dispose()
+})
+
const timeline = (messages: ChatMessage[]) => messages.map(message => [message.role, chatMessageText(message)])
const flush = () =>
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 37271cef716..68234a42b1b 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
@@ -51,6 +51,36 @@ import { extendInterruptedReply } from './interrupted-reply'
import { currentResponseParts, mergeCurrentResponseText } from './response-parts'
import { completionErrorText, delegateTaskPayloads, MAX_STREAM_FLUSH_GAP_MS, STREAM_DELTA_FLUSH_MS } from './utils'
+/**
+ * A complete receipt names the turn's one stored prompt row. A host whose
+ * submit acknowledgement cannot name it (the session authority queues the turn
+ * and writes the row when it runs) leaves the optimistic prompt without a row
+ * id; bind it here so a transcript read keyed on stored rows (switch-back,
+ * peer refresh) replaces that bubble instead of painting the prompt twice.
+ */
+function bindReceiptPrompt(
+ messages: ChatMessage[],
+ promptIndex: number,
+ persistedTurn: PersistedTurn | null | undefined
+): ChatMessage[] {
+ const rowId = persistedTurn?.complete === true ? persistedTurn.user_row_id : undefined
+ const prompt = messages[promptIndex]
+
+ if (
+ typeof rowId !== 'number' ||
+ !Number.isSafeInteger(rowId) ||
+ rowId <= 0 ||
+ prompt?.role !== 'user' ||
+ prompt.rowId !== undefined ||
+ !prompt.id.startsWith('user-') ||
+ prompt.id.startsWith('user-queued-')
+ ) {
+ return messages
+ }
+
+ return messages.map((message, index) => (index === promptIndex ? { ...message, rowId } : message))
+}
+
interface MessageStreamOptions {
activeGatewayProfile?: string
activeSessionIdRef: MutableRefObject<string | null>
@@ -1036,7 +1066,7 @@ export function useMessageStream({
// degraded websocket leaves its tool row spinning forever. The turn is
// provably done here — nothing can still be running — so seal any
// tool-call parts that never saw their completion event.
- nextMessages = sealOpenToolParts(nextMessages)
+ nextMessages = bindReceiptPrompt(sealOpenToolParts(nextMessages), lastUserIndex, persistedTurn)
const hasInlineError = nextMessages.some(
(m, index) => index > lastUserIndex && m.role === 'assistant' && m.error && !m.hidden