Skip to content

The agent that grows with you

fix(desktop): render a reply once when it completes during the switch-back hydrate

teknium15f875d0fcddf
diff --git a/apps/desktop/src/app/session/hooks/use-session-actions/utils.test.ts b/apps/desktop/src/app/session/hooks/use-session-actions/utils.test.ts
index e20d8e33093..fcf080be7e2 100644
--- a/apps/desktop/src/app/session/hooks/use-session-actions/utils.test.ts
+++ b/apps/desktop/src/app/session/hooks/use-session-actions/utils.test.ts
@@ -1749,6 +1749,51 @@ describe('overlayConcurrentMessageChanges', () => {
{ type: 'text', text: ' + delta B' }
])
})
+
+ // Switch back to a chat mid-reply: session.activate snapshots the first
+ // chunk, message.complete settles the live row, THEN the gated REST page
+ // resolves with the committed reply. Warm-activation composition order.
+ it('keeps one reply when message.complete lands while the switch-back hydrate is in flight', () => {
+ const snapshot = {
+ session_id: 'runtime-b',
+ inflight: { user: 'prompt b', assistant: 'A2 ', streaming: true }
+ }
+
+ const baseline = [
+ msg('user-optimistic', 'user', 'prompt b'),
+ msg('assistant-stream-1-2', 'assistant', 'A2 ', { pending: true })
+ ]
+
+ const current = [
+ baseline[0],
+ msg('assistant-stream-1-2', 'assistant', 'A2 finished while away', { pending: false })
+ ]
+
+ const persisted = [
+ msg('3-user', 'user', 'prompt b', { rowId: 3 }),
+ msg('4-assistant', 'assistant', 'A2 finished while away', { rowId: 4 })
+ ]
+
+ const hydrated = appendLiveSessionProjection(persisted, snapshot)
+ const overlaid = overlayConcurrentMessageChanges(hydrated, baseline, current)
+
+ expect(overlaid.map(message => [message.id, chatMessageText(message)])).toEqual([
+ ['3-user', 'prompt b'],
+ ['4-assistant', 'A2 finished while away']
+ ])
+
+ // Before the commit the same snapshot still projects the running reply.
+ const running = overlayConcurrentMessageChanges(
+ appendLiveSessionProjection(persisted.slice(0, 1), snapshot),
+ baseline,
+ [baseline[0], msg('assistant-stream-1-2', 'assistant', 'A2 finished', { pending: true })]
+ )
+
+ expect(running.map(message => [message.id, chatMessageText(message)])).toEqual([
+ ['3-user', 'prompt b'],
+ ['assistant-stream-1-2', 'A2 finished']
+ ])
+ })
})
describe('preserveEquivalentTranscript', () => {
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 b616e9812be..dd707dc483c 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
@@ -1034,11 +1034,24 @@ export function appendLiveSessionProjection(messages: ChatMessage[], projection:
isLiveTailRow(liveAssistantOfCurrentTurn)
)
+ // An activate snapshot taken before the turn committed goes stale once REST
+ // returns the committed reply after the persisted prompt: its partial
+ // `inflight.assistant` is a prefix of that reply, and projecting it paints
+ // the answer twice (the extra row frozen on its first chunk).
+ const turnAlreadyCommitted = Boolean(
+ inflightUserAlreadyPersisted &&
+ !inflightError &&
+ liveAssistantOfCurrentTurn &&
+ !isLiveTailRow(liveAssistantOfCurrentTurn) &&
+ !liveAssistantOfCurrentTurn.parts.some(part => part.type === 'tool-call') &&
+ isStrictAnswerTextExtension(chatMessageText(liveAssistantOfCurrentTurn), inflightAssistant)
+ )
+
const wantsAssistantRow = Boolean(
inflightAssistant || inflightStreaming || inflightError || (inflightUser && queuedUser)
)
- const projectAssistantDump = wantsAssistantRow && !(turnAlreadyStructured && !inflightError)
+ const projectAssistantDump = wantsAssistantRow && !turnAlreadyCommitted && !(turnAlreadyStructured && !inflightError)
const pushCorrection = (correction: string, index: number): void => {
if (persistedInLatestRun(correction)) {
@@ -1310,6 +1323,25 @@ export function overlayConcurrentMessageChanges(
continue
}
+ // message.complete settled this stream row while REST was in flight, and
+ // the page already carries the same reply under its committed id (#70209).
+ if (current.role === 'assistant' && current.pending !== true && isLiveTailReplyId(current.id)) {
+ const text = textWithoutReferenceLines(chatMessageText(current)).trim()
+ const lastUser = overlaid.findLastIndex(message => message.role === 'user')
+
+ const committed = overlaid.some(
+ (message, index) =>
+ index > lastUser &&
+ message.role === 'assistant' &&
+ !isLiveTailRow(message) &&
+ textWithoutReferenceLines(chatMessageText(message)).trim() === text
+ )
+
+ if (text && committed) {
+ continue
+ }
+ }
+
if (activationStreamIndex >= 0 && current.role === 'assistant' && current.id.startsWith('assistant-stream-')) {
const activationStream = overlaid[activationStreamIndex]
const activationText = chatMessageText(activationStream)