Skip to content

The agent that grows with you

fix(desktop): only drop a backfilled fold when every row it carries is held

kshitijk4poor274117bd4c5e
diff --git a/apps/desktop/src/app/chat/transcript-backfill.test.ts b/apps/desktop/src/app/chat/transcript-backfill.test.ts
index 9718b961308..21a5ce67285 100644
--- a/apps/desktop/src/app/chat/transcript-backfill.test.ts
+++ b/apps/desktop/src/app/chat/transcript-backfill.test.ts
@@ -166,43 +166,29 @@ 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
- }
+ // Hydration folds a turn into one bubble addressed by its FIRST source row,
+ // with the final reply (52203) riding inside as a text part. A drifting
+ // older-page fetch that overlaps the just-persisted turn must not paint that
+ // reply twice, but must not drop rows the store does not hold either.
+ const narration = { type: 'text', text: 'earlier tool narration', sourceRowId: 52201 } as const
+ const answer = { type: 'text', text: 'the answer', sourceRowId: 52203 } as const
+ const toolCall = { type: 'tool-call', toolCallId: 't1', toolName: 'read_file', args: {} } as unknown as ChatMessage['parts'][number]
+
+ it.each([
+ { name: 'drops a fold whose every row is already held live', liveParts: [narration, answer], foldParts: [narration, answer], kept: false },
+ { name: 'keeps a fold carrying a tool call and unheld narration', liveParts: [answer], foldParts: [toolCall, narration, answer], kept: true }
+ ])('$name (#123801)', ({ liveParts, foldParts, kept }) => {
+ const live: ChatMessage = { id: 'assistant-stream-1', role: 'assistant', parts: [...liveParts], rowId: 52203 }
const existing: ChatMessage[] = [chat('prompt', 52200), live]
+ const fold: ChatMessage = { id: '1770000000000-4-assistant', role: 'assistant', parts: [...foldParts], rowId: 52201 }
- // 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 merged = mergeOlderTranscriptPage(existing, [chat('prompt-refetch', 52200), fold])
- 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)
+ if (kept) {
+ expect(merged.flatMap(message => message.parts)).toEqual(expect.arrayContaining([toolCall, narration]))
+ } else {
+ expect(merged).toBe(existing)
+ }
})
})
diff --git a/apps/desktop/src/app/chat/transcript-backfill.ts b/apps/desktop/src/app/chat/transcript-backfill.ts
index a6c777d4bf5..16768ea8653 100644
--- a/apps/desktop/src/app/chat/transcript-backfill.ts
+++ b/apps/desktop/src/app/chat/transcript-backfill.ts
@@ -102,8 +102,6 @@ export function mergeOlderTranscriptPage(existing: ChatMessage[], olderPage: Cha
// 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) => {
@@ -128,20 +126,23 @@ 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 heldRowAnchor = (row: number) => existingRowIndices.get(row) ?? existingPartRowIndices.get(row)
- const anchor = existingRowIndices.get(part.sourceRowId) ?? existingPartRowIndices.get(part.sourceRowId)
+ // A fetched fold is only a duplicate when EVERY row it carries is already
+ // held; one unheld row (narration beside a tool call) means dropping it
+ // would lose that content, so it is appended as before.
+ const partRowAnchor = (message: ChatMessage): number | undefined => {
+ const rows = message.parts.flatMap(part =>
+ part.type === 'text' && part.sourceRowId !== undefined ? [part.sourceRowId] : []
+ )
- if (anchor !== undefined) {
- return anchor
- }
+ if (message.rowId !== undefined) {
+ rows.push(message.rowId)
}
- return undefined
+ const anchors = rows.map(heldRowAnchor)
+
+ return anchors.length > 0 && anchors.every(anchor => anchor !== undefined) ? anchors[anchors.length - 1] : undefined
}
for (const message of olderPage) {