The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): keep preserved error turns in place and drop stored copies
Austin Pickett865e94672e17
diff --git a/apps/desktop/src/lib/chat-messages/reconciliation.test.ts b/apps/desktop/src/lib/chat-messages/reconciliation.test.tsindex 6351d22eed2..ad3edfae2bc 100644--- a/apps/desktop/src/lib/chat-messages/reconciliation.test.ts+++ b/apps/desktop/src/lib/chat-messages/reconciliation.test.ts@@ -1,6 +1,6 @@import { expect, it } from 'vitest'-import { type ChatMessage, preserveLocalAssistantErrors, textPart, toChatMessages } from './index'+import { type ChatMessage, chatMessageText, preserveLocalAssistantErrors, textPart, toChatMessages } from './index'const row = (id: string, role: 'user' | 'assistant', text: string, extra: Partial<ChatMessage> = {}): ChatMessage => ({id,@@ -86,3 +86,50 @@ it('reconciles only the represented failed tail, retaining its structured errorexpect(merged.map(message => message.id)).toEqual(stored.map(message => message.id))expect(merged.at(-1)).toMatchObject({ error: failed.error, errorSurface: failed.errorSurface })})++const turnLabels = (messages: ChatMessage[]) => messages.map(message => `${message.role}:${chatMessageText(message)}`)++it('drops a local errored turn the refreshed transcript already stored under new ids', () => {+ const stored = [+ row('s1', 'user', 'old q', { rowId: 1 }),+ row('s2', 'assistant', 'old a', { rowId: 2 }),+ row('s3', 'user', 'new q', { rowId: 3 }),+ row('s4', 'assistant', 'new a', { rowId: 4 })+ ]++ const current = [+ row('user-1-x', 'user', 'old q'),+ row('assistant-stream-1-0', 'assistant', 'old a', { error: 'boom' }),+ row('s3', 'user', 'new q'),+ row('s4', 'assistant', 'new a')+ ]++ expect(turnLabels(preserveLocalAssistantErrors(stored, current))).toEqual(turnLabels(stored))+})++it('keeps an unstored errored turn at its original position instead of after newer turns', () => {+ const stored = [+ row('s1', 'user', 'first q'),+ row('s2', 'assistant', 'first a'),+ row('s3', 'user', 'later q'),+ row('s4', 'assistant', 'later a')+ ]++ const current = [+ row('s1', 'user', 'first q'),+ row('s2', 'assistant', 'first a'),+ row('user-9-x', 'user', 'failed q'),+ row('assistant-stream-9-0', 'assistant', '', { error: 'boom' }),+ row('s3', 'user', 'later q'),+ row('s4', 'assistant', 'later a')+ ]++ expect(turnLabels(preserveLocalAssistantErrors(stored, current))).toEqual([+ 'user:first q',+ 'assistant:first a',+ 'user:failed q',+ 'assistant:',+ 'user:later q',+ 'assistant:later a'+ ])+})diff --git a/apps/desktop/src/lib/chat-messages/reconciliation.ts b/apps/desktop/src/lib/chat-messages/reconciliation.tsindex 6e6f98310da..df773498fc3 100644--- a/apps/desktop/src/lib/chat-messages/reconciliation.ts+++ b/apps/desktop/src/lib/chat-messages/reconciliation.ts@@ -177,6 +177,12 @@ function reconcileLocalAssistantTimeline(nextMessages: ChatMessage[], currentMes})}+interface PreservedRun {+ after?: string+ before?: string+ rows: ChatMessage[]+}+export function preserveLocalAssistantErrors(nextMessages: ChatMessage[],currentMessages: ChatMessage[]@@ -256,11 +262,51 @@ export function preserveLocalAssistantErrors(return mergedNextMessages}- const preserved = currentMessages- .filter(message => preserveIds.has(message.id))- .map(message => ({ ...message, pending: false }))+ // Put each run of kept rows back after the refreshed row that preceded it+ // locally instead of below newer turns. When the refresh already fills that+ // gap with the same role/text sequence, the turn was stored under new ids.+ // A run with no refreshed successor stays trailing. #118002+ const label = (message: ChatMessage) => `${message.role}:${normalize(chatMessageText(message))}`+ const runs: PreservedRun[] = []+ let anchor: string | undefined++ for (const message of currentMessages) {+ const open = runs.at(-1)?.after === anchor ? runs.at(-1) : undefined++ if (existingIds.has(message.id)) {+ if (open) {+ open.before = message.id+ }++ anchor = message.id+ } else if (preserveIds.has(message.id)) {+ const kept = { ...message, pending: false }++ if (open) {+ open.rows.push(kept)+ } else {+ runs.push({ after: anchor, rows: [kept] })+ }+ }+ }++ const indexOf = (id?: string) => mergedNextMessages.findIndex(message => message.id === id)+ const keptAfter = new Map<string | undefined, ChatMessage[]>()++ for (const { after, before, rows } of runs) {+ const gap = before === undefined ? [] : mergedNextMessages.slice(indexOf(after) + 1, indexOf(before))++ if (gap.length && gap.map(label).join('\n') === rows.map(label).join('\n')) {+ continue+ }++ keptAfter.set(after, [...(keptAfter.get(after) ?? []), ...rows])+ }- return [...mergedNextMessages, ...preserved]+ return [+ ...mergedNextMessages.flatMap(message => [message, ...(keptAfter.get(message.id) ?? [])]),+ ...(keptAfter.get(undefined) ?? [])+ ]}export function branchGroupForUser(userMessage: ChatMessage): string {