Skip to content

The agent that grows with you

fix(desktop): keep client-local system notices across refreshes (#126422)

Brooklyn Nicholson0e78b0144fee
diff --git a/apps/desktop/src/app/contrib/hooks/use-background-sync.ts b/apps/desktop/src/app/contrib/hooks/use-background-sync.ts
index 109ff86c9df..661c51df353 100644
--- a/apps/desktop/src/app/contrib/hooks/use-background-sync.ts
+++ b/apps/desktop/src/app/contrib/hooks/use-background-sync.ts
@@ -8,7 +8,13 @@ import {
} from '@/app/chat/transcript-backfill'
import { preserveLocalPendingTurnMessages } from '@/app/session/hooks/use-session-actions/utils'
import { getLatestSessionMessages, type ProfileScope } from '@/hermes'
-import { type ChatMessage, preserveLocalAssistantErrors, sealOpenToolParts, toChatMessages } from '@/lib/chat-messages'
+import {
+ type ChatMessage,
+ preserveLocalAssistantErrors,
+ preserveLocalSystemNotices,
+ sealOpenToolParts,
+ toChatMessages
+} from '@/lib/chat-messages'
import { createClientSessionState } from '@/lib/chat-runtime'
import { sessionMessagesSignature } from '@/lib/session-signatures'
import { latestSessionTodos, latestSessionTodoSnapshot } from '@/lib/todos'
@@ -312,8 +318,13 @@ export async function reconcileTileTranscripts({
// background refresh that lands mid-send would drop it and the
// message would have to be retyped. Same composition order as
// reconcileAuthoritativeChatMessages (use-session-actions/index.ts).
- messages: preserveLocalAssistantErrors(
- preserveLocalPendingTurnMessages(graftRefreshedTailOntoBackfill(messages, state.messages), state.messages),
+ // Trailing client-local system notices (fallback switch, #126422)
+ // are re-grafted last: the stored page cannot carry them.
+ messages: preserveLocalSystemNotices(
+ preserveLocalAssistantErrors(
+ preserveLocalPendingTurnMessages(graftRefreshedTailOntoBackfill(messages, state.messages), state.messages),
+ state.messages
+ ),
state.messages
)
}),
@@ -394,9 +405,13 @@ export async function hydrateStoredSessionTranscript({
runtimeSessionId,
state => ({
...state,
- // Keep backfilled pages, un-acked optimistic input and local errors.
- messages: preserveLocalAssistantErrors(
- preserveLocalPendingTurnMessages(graftRefreshedTailOntoBackfill(messages, state.messages), state.messages),
+ // Keep backfilled pages, un-acked optimistic input, local errors, and
+ // trailing client-local system notices (#126422).
+ messages: preserveLocalSystemNotices(
+ preserveLocalAssistantErrors(
+ preserveLocalPendingTurnMessages(graftRefreshedTailOntoBackfill(messages, state.messages), state.messages),
+ state.messages
+ ),
state.messages
)
}),
@@ -544,9 +559,13 @@ export async function reconcileActiveTranscript({
...state,
// The refresh re-reads only the newest tail page; graft it onto any
// older pages "Show earlier" already backfilled instead of clobbering
- // them (see transcript-backfill).
- messages: preserveLocalAssistantErrors(
- preserveLocalPendingTurnMessages(graftRefreshedTailOntoBackfill(messages, state.messages), state.messages),
+ // them (see transcript-backfill). Trailing client-local system notices
+ // (fallback switch, #126422) are re-grafted last.
+ messages: preserveLocalSystemNotices(
+ preserveLocalAssistantErrors(
+ preserveLocalPendingTurnMessages(graftRefreshedTailOntoBackfill(messages, state.messages), state.messages),
+ state.messages
+ ),
state.messages
)
}),
diff --git a/apps/desktop/src/lib/chat-messages/index.ts b/apps/desktop/src/lib/chat-messages/index.ts
index 20a7989b2b9..97c10fd732f 100644
--- a/apps/desktop/src/lib/chat-messages/index.ts
+++ b/apps/desktop/src/lib/chat-messages/index.ts
@@ -15,7 +15,12 @@ export {
textPart
} from './parts'
export type { UnspokenTurnSpeech } from './parts'
-export { branchGroupForUser, preserveLocalAssistantErrors, spliceOlderPreservedRows } from './reconciliation'
+export {
+ branchGroupForUser,
+ preserveLocalAssistantErrors,
+ preserveLocalSystemNotices,
+ spliceOlderPreservedRows
+} from './reconciliation'
export {
restorePendingBlockingToolCall,
restorePendingClarifyToolCall,
diff --git a/apps/desktop/src/lib/chat-messages/reconciliation.test.ts b/apps/desktop/src/lib/chat-messages/reconciliation.test.ts
index 2c6c46e0c30..9afbe00c691 100644
--- a/apps/desktop/src/lib/chat-messages/reconciliation.test.ts
+++ b/apps/desktop/src/lib/chat-messages/reconciliation.test.ts
@@ -1,6 +1,13 @@
import { expect, it } from 'vitest'
-import { type ChatMessage, chatMessageText, preserveLocalAssistantErrors, textPart, toChatMessages } from './index'
+import {
+ type ChatMessage,
+ chatMessageText,
+ preserveLocalAssistantErrors,
+ preserveLocalSystemNotices,
+ textPart,
+ toChatMessages
+} from './index'
const row = (id: string, role: 'user' | 'assistant', text: string, extra: Partial<ChatMessage> = {}): ChatMessage => ({
id,
@@ -412,3 +419,37 @@ it('keeps a rowId-less preserved run trailing (#118002 behavior unchanged)', ()
expect(merged.map(message => message.id)).toEqual(['9-0-user', '9-1-assistant', 'user-no-row', 'assistant-stream-x'])
})
+
+// #126422: the fallback-switch notice is a client-local `system` row the
+// stored page cannot carry; the post-turn refresh rebuilds from stored rows
+// and must re-graft it instead of silently dropping it.
+it('preserveLocalSystemNotices re-grafts trailing client-local system notices', () => {
+ const notice: ChatMessage = {
+ id: 'fallback-switch-1234',
+ parts: [textPart('Model fallback: using xiaomi/mimo via nous.')],
+ role: 'system',
+ timestamp: 1234
+ }
+ const refreshed = [row('s1', 'user', 'prompt'), row('s2', 'assistant', 'reply')]
+
+ const preserved = preserveLocalSystemNotices(refreshed, [...refreshed, notice])
+
+ expect(preserved.at(-1)?.id).toBe('fallback-switch-1234')
+})
+
+it('preserveLocalSystemNotices does not duplicate a notice the page already carries', () => {
+ const notice: ChatMessage = {
+ id: 'fallback-switch-1234',
+ parts: [textPart('Model fallback: using xiaomi/mimo via nous.')],
+ role: 'system',
+ timestamp: 1234
+ }
+ const refreshed = [
+ row('s1', 'user', 'prompt'),
+ row('s2', 'assistant', 'reply'),
+ { ...notice, id: 'fallback-switch-5678' }
+ ]
+
+ const preserved = preserveLocalSystemNotices(refreshed, [...refreshed, { ...notice, id: 'other' }])
+ expect(preserved).toBe(refreshed)
+})
diff --git a/apps/desktop/src/lib/chat-messages/reconciliation.ts b/apps/desktop/src/lib/chat-messages/reconciliation.ts
index 33961a19674..6aec3a7e4ab 100644
--- a/apps/desktop/src/lib/chat-messages/reconciliation.ts
+++ b/apps/desktop/src/lib/chat-messages/reconciliation.ts
@@ -507,6 +507,43 @@ export function preserveLocalAssistantErrors(
return insertPreservedErrorRuns(merged, currentMessages, preserveIds)
}
+/**
+ * Re-graft trailing client-local `system` notices (the fallback-switch notice
+ * from status.update): refreshes rebuild from stored rows, which never carry
+ * them, so the notice vanished on the next refresh (#126422). Stored rows own
+ * a `rowId` and are left to the page. Idempotent by id and text.
+ */
+export function preserveLocalSystemNotices(
+ nextMessages: ChatMessage[],
+ currentMessages: ChatMessage[]
+): ChatMessage[] {
+ const trailing: ChatMessage[] = []
+
+ for (let index = currentMessages.length - 1; index >= 0; index -= 1) {
+ const message = currentMessages[index]
+
+ if (message.role !== 'system') {
+ break
+ }
+
+ if (message.rowId === undefined) {
+ trailing.unshift(message)
+ }
+ }
+
+ if (!trailing.length) {
+ return nextMessages
+ }
+
+ const nextIds = new Set(nextMessages.map(message => message.id))
+ const nextTexts = new Set(nextMessages.map(message => chatMessageText(message).trim()))
+ const unstored = trailing.filter(
+ message => !nextIds.has(message.id) && !nextTexts.has(chatMessageText(message).trim())
+ )
+
+ return unstored.length ? [...nextMessages, ...unstored] : nextMessages
+}
+
export function branchGroupForUser(userMessage: ChatMessage): string {
return `branch:${userMessage.id}`
}