Skip to content

The agent that grows with you

fix(desktop): match preserved error turns by durable rowId, not renderer id (#119326)

brooklyn!4e950948e485
diff --git a/apps/desktop/src/lib/chat-messages/reconciliation.test.ts b/apps/desktop/src/lib/chat-messages/reconciliation.test.ts
index 40026c8a583..8cf4e8d93fb 100644
--- a/apps/desktop/src/lib/chat-messages/reconciliation.test.ts
+++ b/apps/desktop/src/lib/chat-messages/reconciliation.test.ts
@@ -168,3 +168,50 @@ it('keeps a failed tail at the end while its re-submitted prompt is still local-
expect(merged.map(message => message.id)).toEqual(['u1', 'a1', 'u0', 'local-failure'])
})
+
+it('does not re-append a failed turn whose prompt hydration carries under a new id (#119326)', () => {
+ // The backend rewrote the stored prompt (attachment suffix), so only its
+ // durable rowId still ties it to the local optimistic row.
+ const merged = preserveLocalAssistantErrors(
+ [
+ row('9-0-user', 'user', 'hi', { rowId: 1 }),
+ row('9-1-assistant', 'assistant', 'hello', { rowId: 2 }),
+ row('9-2-user', 'user', 'look\n\n[image attached]', { rowId: 3 }),
+ row('9-3-user', 'user', 'newer', { rowId: 5 }),
+ row('9-4-assistant', 'assistant', 'reply', { rowId: 6 })
+ ],
+ [
+ row('1-0-user', 'user', 'hi', { rowId: 1 }),
+ row('1-1-assistant', 'assistant', 'hello', { rowId: 2 }),
+ row('user-look', 'user', 'look', { rowId: 3 }),
+ row('local-failure', 'assistant', '', { error: 'upstream timeout' }),
+ row('user-newer', 'user', 'newer', { rowId: 5 }),
+ row('assistant-stream-reply', 'assistant', 'reply', { rowId: 6 })
+ ]
+ )
+
+ expect(merged.map(message => message.id)).toEqual([
+ '9-0-user',
+ '9-1-assistant',
+ '9-2-user',
+ 'local-failure',
+ '9-3-user',
+ '9-4-assistant'
+ ])
+})
+
+it('moves a local error onto the durable row it already represents (#119326)', () => {
+ const merged = preserveLocalAssistantErrors(
+ [
+ row('9-0-user', 'user', 'look\n\n[image attached]', { rowId: 3 }),
+ row('9-1-assistant', 'assistant', 'partial', { rowId: 4 })
+ ],
+ [
+ row('user-look', 'user', 'look', { rowId: 3 }),
+ row('assistant-stream-x', 'assistant', 'partial', { error: 'upstream timeout', rowId: 4 })
+ ]
+ )
+
+ expect(merged.map(message => message.id)).toEqual(['9-0-user', '9-1-assistant'])
+ expect(merged[1]).toMatchObject({ error: 'upstream timeout', pending: false })
+})
diff --git a/apps/desktop/src/lib/chat-messages/reconciliation.ts b/apps/desktop/src/lib/chat-messages/reconciliation.ts
index df773498fc3..f320c5ff778 100644
--- a/apps/desktop/src/lib/chat-messages/reconciliation.ts
+++ b/apps/desktop/src/lib/chat-messages/reconciliation.ts
@@ -210,6 +210,20 @@ export function preserveLocalAssistantErrors(
})
const existingIds = new Set(mergedNextMessages.map(message => message.id))
+
+ // Renderer ids are positional, so a hydrated page can carry a local row under
+ // a new id; its durable rowId still names the same row (#119326).
+ const hydratedIdByRowId = new Map(
+ mergedNextMessages.flatMap(message => (message.rowId === undefined ? [] : [[message.rowId, message.id] as const]))
+ )
+
+ const hydratedIdFor = (message: ChatMessage): string | undefined =>
+ existingIds.has(message.id)
+ ? message.id
+ : message.rowId === undefined
+ ? undefined
+ : hydratedIdByRowId.get(message.rowId)
+
const preserveIds = new Set<string>()
const normalize = (value: string) => value.replace(/\s+/g, ' ').trim()
const tailUserInNext = [...mergedNextMessages].reverse().find(message => message.role === 'user' && !message.hidden)
@@ -228,7 +242,12 @@ export function preserveLocalAssistantErrors(
continue
}
- const hydratedAssistantIndex = tailTurnAssistantMatchIndex(mergedNextMessages, currentMessages, index)
+ const hydratedId = hydratedIdFor(message)
+
+ const hydratedAssistantIndex =
+ hydratedId === undefined
+ ? tailTurnAssistantMatchIndex(mergedNextMessages, currentMessages, index)
+ : mergedNextMessages.findIndex(candidate => candidate.id === hydratedId && candidate.role === 'assistant')
if (hydratedAssistantIndex !== -1) {
mergedNextMessages[hydratedAssistantIndex] = {
@@ -250,7 +269,7 @@ export function preserveLocalAssistantErrors(
continue
}
- if (candidate.role === 'user' && !existingIds.has(candidate.id) && !matchesTailUserInNext(candidate)) {
+ if (candidate.role === 'user' && hydratedIdFor(candidate) === undefined && !matchesTailUserInNext(candidate)) {
preserveIds.add(candidate.id)
}
@@ -272,13 +291,14 @@ export function preserveLocalAssistantErrors(
for (const message of currentMessages) {
const open = runs.at(-1)?.after === anchor ? runs.at(-1) : undefined
+ const hydratedId = preserveIds.has(message.id) ? undefined : hydratedIdFor(message)
- if (existingIds.has(message.id)) {
+ if (hydratedId !== undefined) {
if (open) {
- open.before = message.id
+ open.before = hydratedId
}
- anchor = message.id
+ anchor = hydratedId
} else if (preserveIds.has(message.id)) {
const kept = { ...message, pending: false }