Skip to content

The agent that grows with you

fix(desktop): drop pending stream rows whose reply the transcript already carries

李灵航cf63b7946785
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 e7a21b17cd5..4a18397200e 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
@@ -1086,6 +1086,64 @@ describe('preserveLocalPendingTurnMessages', () => {
expect(chatMessageText(preserved[1])).toBe('first answer')
expect(preserved.filter(message => message.role === 'assistant')).toHaveLength(2)
})
+
+ // A still-PENDING stream row whose committed twin the authoritative history
+ // already carries (ordinal shifted under compaction) used to fall through to
+ // `preserved.push` and render the same answer twice — the reported tail
+ // duplication (A B C D E C D). The #70209 guard only covers settled local
+ // rows (`pending !== true`); these cover the pending ones.
+ it('does not re-append a pending stream row the authoritative history already carries', () => {
+ const previous = [
+ msg('1-user', 'user', '查金价'),
+ msg('2-a', 'assistant', 'X'),
+ streamingMsg('assistant-stream-live', '面板内容')
+ ]
+
+ const next = [msg('1-user', 'user', '查金价'), msg('9-assistant', 'assistant', '面板内容')]
+
+ expect(preserveLocalPendingTurnMessages(next, previous)).toBe(next)
+ })
+
+ it('drops a pending stream row whose text the committed authoritative reply extends', () => {
+ const previous = [
+ msg('1-user', 'user', '查金价'),
+ msg('2-a', 'assistant', 'X'),
+ streamingMsg('assistant-stream-live', '面板')
+ ]
+
+ const next = [msg('1-user', 'user', '查金价'), msg('9-assistant', 'assistant', '面板内容完整版')]
+
+ expect(preserveLocalPendingTurnMessages(next, previous)).toBe(next)
+ })
+
+ it('replaces the committed row with a further-along pending copy instead of appending', () => {
+ const previous = [
+ msg('1-user', 'user', '查金价'),
+ msg('2-a', 'assistant', 'X'),
+ streamingMsg('assistant-stream-live', '面板内容完整版')
+ ]
+
+ const next = [msg('1-user', 'user', '查金价'), msg('9-assistant', 'assistant', '面板')]
+
+ const preserved = preserveLocalPendingTurnMessages(next, previous)
+
+ expect(preserved.map(message => message.id)).toEqual(['1-user', '9-assistant'])
+ expect(chatMessageText(preserved[1])).toBe('面板内容完整版')
+ })
+
+ // The authoritative history genuinely does not have this reply yet — the
+ // pending row is the only copy and must survive (same contract as the
+ // settled-row variant above).
+ it('still keeps a pending stream row when the authoritative history has no reply', () => {
+ const previous = [msg('1-user', 'user', '查金价'), streamingMsg('assistant-stream-live', '面板内容')]
+
+ const next = [msg('1-user', 'user', '查金价')]
+
+ expect(preserveLocalPendingTurnMessages(next, previous).map(message => message.id)).toEqual([
+ '1-user',
+ 'assistant-stream-live'
+ ])
+ })
})
describe('appendLiveSessionProjection', () => {
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 29e8d105dd9..6c031768acc 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
@@ -603,6 +603,64 @@ export function preserveLocalPendingTurnMessages(
}
}
+ // Ordinal pairing missed (the committed row shifted ordinal when history
+ // was compacted / the authoritative list is shorter), yet the
+ // authoritative transcript already carries this same reply under its
+ // committed id. The #70209 guard above only covers SETTLED local rows
+ // (`pending !== true`); a still-pending stream row that slips past
+ // pairing falls through to `preserved.push` and renders the answer
+ // twice — the reported A B C D E C D tail duplication.
+ //
+ // Three-way same-turn check against SETTLED authoritative rows only
+ // (a live projection shell must not swallow the richer local row, see
+ // the traces-only replacement test):
+ // 1. identical answer text -> authoritative already has it
+ // 2. authoritative extends local text -> authoritative is the settled
+ // final version of the still-streaming local copy
+ // 3. local extends authoritative text -> local is further along; replace
+ // the committed row with the richer body instead of appending
+ if (isPendingAssistant) {
+ const nextText = textWithoutReferenceLines(chatMessageText(message))
+
+ const committedMatch = nextMessages.find(
+ candidate =>
+ candidate.role === 'assistant' &&
+ !isLiveTailRow(candidate) &&
+ (textWithoutReferenceLines(chatMessageText(candidate)) === nextText ||
+ isStrictAnswerTextExtension(
+ textWithoutReferenceLines(chatMessageText(candidate)),
+ nextText
+ ))
+ )
+
+ if (committedMatch) {
+ continue
+ }
+
+ const committedPrefix = nextMessages.find(
+ candidate =>
+ candidate.role === 'assistant' &&
+ !isLiveTailRow(candidate) &&
+ isStrictAnswerTextExtension(
+ nextText,
+ textWithoutReferenceLines(chatMessageText(candidate))
+ )
+ )
+
+ if (committedPrefix) {
+ // Keep the COMMITTED id (not the local stream id): the turn is
+ // already in the authoritative transcript, so the merged row must
+ // stay addressable as that durable row — a stream id would read as a
+ // live row again next reconcile and re-enter this same path.
+ replacements.set(committedPrefix.id, {
+ ...withAuthoritativeTurnState(message, committedPrefix),
+ id: committedPrefix.id
+ })
+
+ continue
+ }
+ }
+
preserved.push(message)
}