Skip to content

The agent that grows with you

fix(desktop): fold the provider stop-row echo without folding authored repeats

kshitijk4poorc9bd3f7f2d7f
diff --git a/apps/desktop/src/lib/chat-messages/parts.ts b/apps/desktop/src/lib/chat-messages/parts.ts
index f38966073c9..2971907fee8 100644
--- a/apps/desktop/src/lib/chat-messages/parts.ts
+++ b/apps/desktop/src/lib/chat-messages/parts.ts
@@ -206,35 +206,59 @@ export function collectUnspokenTurnSpeech(
return { id, pending, text: parts.join('\n\n') }
}
-const normalizeWs = (value: string) => value.replace(/\s+/g, ' ').trim()
+export const normalizeWs = (value: string) => value.replace(/\s+/g, ' ').trim()
-/**
- * Collapse repeated representations of the same durable text occurrence.
- * Equal prose in different tool rounds (or without provenance) is not proof
- * of duplicate delivery and must survive hydration.
- */
-export function dedupeRepeatedTextInParts(parts: ChatMessagePart[]): ChatMessagePart[] {
+type TextPart = Extract<ChatMessagePart, { type: 'text' }>
+
+/** The same physical row delivered twice is one occurrence; keep its last copy. */
+function dedupeRepeatedRowText(parts: ChatMessagePart[]): ChatMessagePart[] {
+ const occurrence = (part: TextPart) => `${part.sourceRowId}:${normalizeWs(part.text)}`
const lastByOccurrence = new Map<string, number>()
parts.forEach((part, index) => {
if (part.type === 'text' && part.sourceRowId !== undefined) {
- const key = `${part.sourceRowId}:${normalizeWs(part.text)}`
-
- lastByOccurrence.set(key, index)
+ lastByOccurrence.set(occurrence(part), index)
}
})
- const dropped = parts.filter((part, index) => {
- if (part.type !== 'text' || part.sourceRowId === undefined) {
- return true
- }
+ const kept = parts.filter(
+ (part, index) => part.type !== 'text' || part.sourceRowId === undefined || lastByOccurrence.get(occurrence(part)) === index
+ )
- const key = `${part.sourceRowId}:${normalizeWs(part.text)}`
+ return kept.length === parts.length ? parts : kept
+}
- return lastByOccurrence.get(key) === index
- })
+/**
+ * Collapse duplicate deliveries of the same text without touching authored
+ * repeats. Providers that continue a turn after a tool call sometimes re-send
+ * the previous assistant text verbatim as the stop row (tool_calls row, then a
+ * stop row with identical prose) — the turn merge then holds the same
+ * paragraph twice and everything in it renders twice, most visibly ::preview
+ * frames. Only that shape folds across rows: the bubble's final text (no tool
+ * call after it) equal to the text directly before it across a tool call.
+ * Equal commentary in earlier tool rounds is authored twice and must hydrate
+ * in step with the live stream.
+ */
+export function dedupeRepeatedTextInParts(parts: ChatMessagePart[]): ChatMessagePart[] {
+ const rowDeduped = dedupeRepeatedRowText(parts)
+ const last = rowDeduped.findLastIndex(part => part.type === 'text')
+ const previous = rowDeduped.findLastIndex((part, index) => index < last && part.type === 'text')
+
+ if (previous < 0 || rowDeduped.slice(last + 1).some(part => part.type === 'tool-call')) {
+ return rowDeduped
+ }
+
+ const key = normalizeWs((rowDeduped[last] as TextPart).text)
+
+ if (
+ !key ||
+ key !== normalizeWs((rowDeduped[previous] as TextPart).text) ||
+ !rowDeduped.slice(previous + 1, last).some(part => part.type === 'tool-call')
+ ) {
+ return rowDeduped
+ }
- return dropped.length === parts.length ? parts : dropped
+ return rowDeduped.filter((_, index) => index !== previous)
}
/**
diff --git a/apps/desktop/src/lib/transcript-occurrences.test.ts b/apps/desktop/src/lib/transcript-occurrences.test.ts
index 4cab9b2867a..a02f865a1b9 100644
--- a/apps/desktop/src/lib/transcript-occurrences.test.ts
+++ b/apps/desktop/src/lib/transcript-occurrences.test.ts
@@ -36,14 +36,23 @@ afterEach(() => {
vi.useRealTimers()
})
-it('hydrates authored occurrences rather than deduplicating equal text across tool rounds', () => {
+// Equal commentary across tool rounds is authored twice and must hydrate in
+// step with the live stream. Only the provider echo folds: a final stop row
+// re-sending the previous tool round's prose verbatim (d690e0220e).
+it('hydrates repeated tool-round commentary but folds the stop-row echo', () => {
const first = round(2, 'Status unchanged.')
const second = round(4, 'Status unchanged.')
const final: SessionMessage = { id: 6, role: 'assistant', content: 'Status unchanged.', timestamp: 6 }
const messages = toChatMessages([prompt, ...first, ...second, final])
const textParts = messages.flatMap(row => row.parts.filter(part => part.type === 'text'))
- expect(textParts.map(part => part.text)).toEqual(['Inspect each phase', ...Array(3).fill('Status unchanged.')])
+ expect(textParts.map(part => part.text)).toEqual(['Inspect each phase', 'Status unchanged.', 'Status unchanged.'])
+ expect(textParts.at(-1)?.sourceRowId).toBe(6)
+ expect(
+ toChatMessages([prompt, ...first, ...second, { ...final, content: 'Done.' }])
+ .flatMap(row => row.parts.filter(part => part.type === 'text'))
+ .map(part => part.text)
+ ).toEqual(['Inspect each phase', 'Status unchanged.', 'Status unchanged.', 'Done.'])
expect(messages.at(-1)?.durableComplete).toBe(true)
expect(toChatMessages([prompt, ...first, ...second]).at(-1)?.durableComplete).toBe(false)
// The same physical row is not another authored occurrence.