Skip to content

The agent that grows with you

refactor(desktop): gate identical-interim reuse on failure in one place

kshitijk4poor9d1d52f24bb7
diff --git a/apps/desktop/src/app/chat/transcript-backfill.test.ts b/apps/desktop/src/app/chat/transcript-backfill.test.ts
index 2693b68b06b..707609fd3a2 100644
--- a/apps/desktop/src/app/chat/transcript-backfill.test.ts
+++ b/apps/desktop/src/app/chat/transcript-backfill.test.ts
@@ -172,7 +172,13 @@ describe('mergeOlderTranscriptPage', () => {
// 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]
+
+ const toolCall = {
+ type: 'tool-call',
+ toolCallId: 't1',
+ toolName: 'read_file',
+ args: {}
+ } as unknown as ChatMessage['parts'][number]
it.each([
{
@@ -194,10 +200,21 @@ describe('mergeOlderTranscriptPage', () => {
expected: ['prompt', '1770000000000-4-assistant', 'assistant-stream-1']
}
])('$name (#123801)', ({ live, foldParts, expected }) => {
- const existing: ChatMessage[] = [chat('prompt', 52200), { id: 'assistant-stream-1', role: 'assistant', ...live, parts: [...live.parts] }]
- const fold: ChatMessage = { id: '1770000000000-4-assistant', role: 'assistant', parts: [...foldParts], rowId: 52201 }
+ const existing: ChatMessage[] = [
+ chat('prompt', 52200),
+ { id: 'assistant-stream-1', role: 'assistant', ...live, parts: [...live.parts] }
+ ]
+
+ const fold: ChatMessage = {
+ id: '1770000000000-4-assistant',
+ role: 'assistant',
+ parts: [...foldParts],
+ rowId: 52201
+ }
- expect(mergeOlderTranscriptPage(existing, [chat('prompt-refetch', 52200), fold]).map(message => message.id)).toEqual(expected)
+ expect(
+ mergeOlderTranscriptPage(existing, [chat('prompt-refetch', 52200), fold]).map(message => message.id)
+ ).toEqual(expected)
})
})
diff --git a/apps/desktop/src/app/chat/transcript-backfill.ts b/apps/desktop/src/app/chat/transcript-backfill.ts
index bf613bd8fc4..44d5563ddea 100644
--- a/apps/desktop/src/app/chat/transcript-backfill.ts
+++ b/apps/desktop/src/app/chat/transcript-backfill.ts
@@ -132,17 +132,14 @@ export function mergeOlderTranscriptPage(existing: ChatMessage[], olderPage: Cha
// 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] : []
- )
+ const anchors = transcriptRowIds(message).map(heldRowAnchor)
- if (message.rowId !== undefined) {
- rows.push(message.rowId)
+ if (!anchors.length || anchors.includes(undefined)) {
+ return undefined
}
- const anchors = rows.map(heldRowAnchor)
-
- return anchors.length > 0 && anchors.every(anchor => anchor !== undefined) ? anchors[anchors.length - 1] : undefined
+ // Anchor on the message's own row when it has one, else its last part row.
+ return message.rowId !== undefined ? anchors[0] : anchors[anchors.length - 1]
}
for (const message of olderPage) {
diff --git a/apps/desktop/src/app/session/hooks/use-message-stream/collapse-duplicate-final.ts b/apps/desktop/src/app/session/hooks/use-message-stream/collapse-duplicate-final.ts
index 592faef344e..338da65ebfe 100644
--- a/apps/desktop/src/app/session/hooks/use-message-stream/collapse-duplicate-final.ts
+++ b/apps/desktop/src/app/session/hooks/use-message-stream/collapse-duplicate-final.ts
@@ -43,16 +43,27 @@ export function collapseDuplicateFinalAfterToolInterim(
return null
}
+ return mergeLiveIntoKept(messages, streamIndex, priorIndex, options.completeMessage)
+}
+
+/** Fold the live bubble's non-text parts into the kept bubble, then drop the live one. */
+function mergeLiveIntoKept(
+ messages: ChatMessage[],
+ streamIndex: number,
+ keptIndex: number,
+ completeMessage: (message: ChatMessage) => ChatMessage
+): DuplicateFinalCollapse {
+ const kept = messages[keptIndex]
const next = messages.slice()
- next[priorIndex] = options.completeMessage(
+ next[keptIndex] = completeMessage(
withUniqueToolCallIdsWithinMessage({
- ...prior,
- parts: [...prior.parts, ...live.parts.filter(part => part.type !== 'text')]
+ ...kept,
+ parts: [...kept.parts, ...messages[streamIndex].parts.filter(part => part.type !== 'text')]
})
)
next.splice(streamIndex, 1)
- return { keptId: prior.id, messages: next }
+ return { keptId: kept.id, messages: next }
}
/**
@@ -88,9 +99,9 @@ export function identicalInterimSiblingIndex(
messages: ChatMessage[],
boundaryIndex: number,
finalText: string,
- options: { interimBoundaryPending: boolean; excludeIndex?: number }
+ options: { interimBoundaryPending: boolean; excludeIndex?: number; hasFailure?: boolean }
): number {
- if (!finalText || !options.interimBoundaryPending) {
+ if (!finalText || !options.interimBoundaryPending || options.hasFailure) {
return -1
}
@@ -125,28 +136,12 @@ export function collapseDuplicateFinalOntoIdenticalInterim(
options: {
completeMessage: (message: ChatMessage) => ChatMessage
finalText: string
- hasFailure: boolean
}
): DuplicateFinalCollapse | null {
- if (streamIndex < 0 || interimIndex < 0 || options.hasFailure || !options.finalText) {
- return null
- }
-
- const live = messages[streamIndex]
- const interim = messages[interimIndex]
-
- if (!live || !interim || chatMessageText(live).trim() !== options.finalText) {
+ // A non-negative interimIndex already implies a non-empty finalText and no failure.
+ if (streamIndex < 0 || interimIndex < 0 || chatMessageText(messages[streamIndex]).trim() !== options.finalText) {
return null
}
- const next = messages.slice()
- next[interimIndex] = options.completeMessage(
- withUniqueToolCallIdsWithinMessage({
- ...interim,
- parts: [...interim.parts, ...live.parts.filter(part => part.type !== 'text')]
- })
- )
- next.splice(streamIndex, 1)
-
- return { keptId: interim.id, messages: next }
+ return mergeLiveIntoKept(messages, streamIndex, interimIndex, options.completeMessage)
}
diff --git a/apps/desktop/src/app/session/hooks/use-message-stream/index.ts b/apps/desktop/src/app/session/hooks/use-message-stream/index.ts
index 04fdcc5b10e..5c9911053fd 100644
--- a/apps/desktop/src/app/session/hooks/use-message-stream/index.ts
+++ b/apps/desktop/src/app/session/hooks/use-message-stream/index.ts
@@ -869,15 +869,11 @@ export function useMessageStream({
const hasFailure = Boolean(failure) || Boolean(completionError)
- // A sealed text-only interim carrying exactly this reply IS the reply
- // (#123801) — see identicalInterimSiblingIndex. Settling it keeps one
- // bubble for the one stored row instead of painting the text twice.
- // Gated on the occurrence boundary: an interim sealed before a
- // `message.start` belongs to a previous turn and must not be reached
- // (the new turn's bubble would be the one deleted).
+ // #123801 — see identicalInterimSiblingIndex.
const identicalInterimIndex = identicalInterimSiblingIndex(prev, lastUserIndex, finalText, {
- interimBoundaryPending,
- excludeIndex: streamIndex
+ excludeIndex: streamIndex,
+ hasFailure,
+ interimBoundaryPending
})
if (streamIndex >= 0) {
@@ -890,8 +886,7 @@ export function useMessageStream({
}) ??
collapseDuplicateFinalOntoIdenticalInterim(prev, streamIndex, identicalInterimIndex, {
completeMessage,
- finalText,
- hasFailure
+ finalText
})
nextMessages = collapsed?.messages ?? settleAt(streamIndex)
} else {
@@ -1016,8 +1011,6 @@ export function useMessageStream({
if (sealed?.interim === true && chatMessageText(sealed).trim() === finalText) {
nextMessages = settleAt(sealedIndex)
- } else if (identicalInterimIndex >= 0) {
- nextMessages = settleAt(identicalInterimIndex)
} else if (finalText) {
nextMessages = [...prev, newAssistantFromCompletion()]
}