Skip to content

The agent that grows with you

fix(desktop): stop a finished reply rendering twice after history catches up

Brooklyn Nicholson34f0427f7fdf
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 81a00da36f8..849aeffa720 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
@@ -945,6 +945,32 @@ describe('preserveLocalPendingTurnMessages', () => {
expect(chatMessageText(preserved[1])).toBe('streamed body')
})
+ // #70209: history committed the reply under its own id, so the settled local
+ // stream row sits at a later ordinal, pairs with nothing, and gets appended —
+ // the same answer twice.
+ it('does not re-append a settled stream row the authoritative history already carries', () => {
+ const next = [msg('1-user-stored', 'user', 'question'), msg('2-assistant-stored', 'assistant', 'answer')]
+ const settledLocalStream = msg('assistant-stream-runtime-1', 'assistant', 'answer', { pending: false })
+
+ expect(preserveLocalPendingTurnMessages(next, [...next, settledLocalStream])).toBe(next)
+ })
+
+ // The reply finished locally but the gateway had not committed it when the
+ // session was reopened — the local row is the only copy and must survive.
+ it('keeps a settled stream row the authoritative history has not committed', () => {
+ const previous = [
+ msg('1-user', 'user', 'question'),
+ msg('assistant-stream-sess', 'assistant', 'the finished reply', { pending: false })
+ ]
+
+ const next = [msg('1-user', 'user', 'question')]
+
+ expect(preserveLocalPendingTurnMessages(next, previous).map(message => message.id)).toEqual([
+ '1-user',
+ 'assistant-stream-sess'
+ ])
+ })
+
// The whole point of replacing rather than appending: one reply on screen,
// and the committed history around the live turn untouched.
it('does not duplicate or rewrite committed history around the live turn', () => {
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 51b0323f22f..3b73ae2a7d8 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
@@ -507,6 +507,24 @@ export function preserveLocalPendingTurnMessages(
const authoritative = nextByRoleOrdinal.get(`${message.role}:${ordinal}`)
+ // A settled stream row (`pending: false` after message.complete) whose reply
+ // the authoritative transcript already carries under its committed id is
+ // stale: ordinal pairing can't see it, because the commit shifted the row
+ // one ordinal earlier, and re-appending it renders the same answer twice
+ // (#70209). Only text-identical rows are dropped — a settled row the backend
+ // has NOT committed yet is the only copy of that reply and must survive.
+ if (
+ isPendingAssistant &&
+ message.pending !== true &&
+ nextMessages.some(
+ candidate =>
+ candidate.role === 'assistant' &&
+ textWithoutReferenceLines(chatMessageText(candidate)) === textWithoutReferenceLines(chatMessageText(message))
+ )
+ ) {
+ continue
+ }
+
if (authoritative) {
if (isPendingAssistant) {
// Keep the local pending row when it is the same reply further along
@@ -522,7 +540,8 @@ export function preserveLocalPendingTurnMessages(
}
if (
- textWithoutReferenceLines(chatMessageText(authoritative)) === textWithoutReferenceLines(chatMessageText(message))
+ textWithoutReferenceLines(chatMessageText(authoritative)) ===
+ textWithoutReferenceLines(chatMessageText(message))
) {
continue
}