Skip to content

The agent that grows with you

fix(desktop): dedupe backfilled folds by final-reply part row

finn76311d5eb81a3aa
diff --git a/apps/desktop/src/app/chat/transcript-backfill.test.ts b/apps/desktop/src/app/chat/transcript-backfill.test.ts
index c6677ba5925..9718b961308 100644
--- a/apps/desktop/src/app/chat/transcript-backfill.test.ts
+++ b/apps/desktop/src/app/chat/transcript-backfill.test.ts
@@ -165,6 +165,45 @@ describe('mergeOlderTranscriptPage', () => {
expect(mergeOlderTranscriptPage(existing, [chat('a', 1)])).toBe(existing)
})
+
+ it('drops a backfilled fold whose final-reply part row is already held live (#123801)', () => {
+ // In-memory store after completion: the user prompt plus the settled
+ // live bubble for gateway row 52203 (one stored row, one completion).
+ const live: ChatMessage = {
+ id: 'assistant-stream-1',
+ role: 'assistant',
+ parts: [{ type: 'text', text: 'the answer', sourceRowId: 52203 }],
+ rowId: 52203
+ }
+ const existing: ChatMessage[] = [chat('prompt', 52200), live]
+
+ // A drifting older-page fetch overlaps the just-persisted turn. Hydration
+ // folded the turn into one bubble addressed by its FIRST source row
+ // (52201); the final reply (52203) rides inside as a text part. Matching
+ // only message-level row ids appends the fold next to the settled live
+ // bubble, so the same reply renders twice.
+ const fold: ChatMessage = {
+ id: '1770000000000-4-assistant',
+ role: 'assistant',
+ parts: [
+ { type: 'text', text: 'earlier tool narration', sourceRowId: 52201 },
+ { type: 'text', text: 'the answer', sourceRowId: 52203 }
+ ],
+ rowId: 52201
+ }
+ const older: ChatMessage[] = [chat('prompt-refetch', 52200), fold]
+
+ const merged = mergeOlderTranscriptPage(existing, older)
+
+ const finalRowOwners = merged.filter(
+ message =>
+ message.rowId === 52203 ||
+ message.parts.some(part => part.type === 'text' && part.sourceRowId === 52203)
+ )
+
+ expect(finalRowOwners).toHaveLength(1)
+ expect(merged).toBe(existing)
+ })
})
describe('graftRefreshedTailOntoBackfill', () => {
diff --git a/apps/desktop/src/app/chat/transcript-backfill.ts b/apps/desktop/src/app/chat/transcript-backfill.ts
index b25d2908dd8..a6c777d4bf5 100644
--- a/apps/desktop/src/app/chat/transcript-backfill.ts
+++ b/apps/desktop/src/app/chat/transcript-backfill.ts
@@ -96,12 +96,27 @@ export function mergeOlderTranscriptPage(existing: ChatMessage[], olderPage: Cha
const existingRowIndices = new Map<number, number>()
const existingIdIndices = new Map<string, number>()
+ // Part-level durable identity. Hydration folds a turn's tool rows and its
+ // final reply into one bubble addressed by its FIRST source row
+ // (message.rowId), while the final reply rides inside as a text part with
+ // its own sourceRowId. Message-level matching alone misses that shared
+ // final row and appends the fold next to the settled live bubble, so the
+ // same reply renders twice (#123801).
+ // ponytail: fold-level dedup — a fold carrying unheld rows alongside the
+ // shared one is dropped whole; merge at part granularity if that loss matters.
+ const existingPartRowIndices = new Map<number, number>()
existing.forEach((message, index) => {
if (message.rowId !== undefined) {
existingRowIndices.set(message.rowId, index)
}
+ for (const part of message.parts) {
+ if (part.type === 'text' && part.sourceRowId !== undefined) {
+ existingPartRowIndices.set(part.sourceRowId, index)
+ }
+ }
+
existingIdIndices.set(message.id, index)
})
@@ -113,10 +128,31 @@ export function mergeOlderTranscriptPage(existing: ChatMessage[], olderPage: Cha
let pending: ChatMessage[] = []
let lastAnchor = -1
+ const partRowAnchor = (message: ChatMessage): number | undefined => {
+ for (const part of message.parts) {
+ if (part.type !== 'text' || part.sourceRowId === undefined) {
+ continue
+ }
+
+ const anchor = existingRowIndices.get(part.sourceRowId) ?? existingPartRowIndices.get(part.sourceRowId)
+
+ if (anchor !== undefined) {
+ return anchor
+ }
+ }
+
+ return undefined
+ }
+
for (const message of olderPage) {
const anchor =
(message.rowId !== undefined ? existingRowIndices.get(message.rowId) : undefined) ??
- existingIdIndices.get(message.id)
+ existingIdIndices.get(message.id) ??
+ // Either side of the match may be a folded part row: a fetched single
+ // row already held inside a live fold, or a fetched fold carrying a
+ // row the store already holds.
+ (message.rowId !== undefined ? existingPartRowIndices.get(message.rowId) : undefined) ??
+ partRowAnchor(message)
if (anchor === undefined) {
pending.push(message)