Skip to content

The agent that grows with you

fix(desktop): keep un-acked optimistic messages across a resync

denis.dresvyanskiy5f97bb0fa70c
diff --git a/apps/desktop/src/app/contrib/hooks/use-background-sync.test.ts b/apps/desktop/src/app/contrib/hooks/use-background-sync.test.ts
index e7d2a7096e3..5ee7cefd5d1 100644
--- a/apps/desktop/src/app/contrib/hooks/use-background-sync.test.ts
+++ b/apps/desktop/src/app/contrib/hooks/use-background-sync.test.ts
@@ -640,6 +640,26 @@ describe('active transcript refresh', () => {
})
describe('reconcileActiveTranscript', () => {
+ // A drop mid-send on a flaky link leaves the optimistic `user-*` row as the
+ // only copy of the message: the server never acked it, so server truth does
+ // not contain it. A background refresh landing in that window replaced the
+ // transcript outright and the message vanished, forcing the user to retype.
+ it('keeps an un-acked optimistic user row when the refresh lands mid-send', async () => {
+ const fixture = makeRefresh()
+ const optimisticId = 'user-1758100000000-ab12cd'
+
+ fixture.state.messages = [
+ { id: optimisticId, parts: [{ text: 'the message I just sent', type: 'text' }], role: 'user' }
+ ]
+ vi.mocked(getLatestSessionMessages).mockResolvedValue(transcript('an older answer') as never)
+
+ await fixture.refresh()
+
+ const messages = fixture.states.get(ACTIVE_RUNTIME_ID)?.messages ?? []
+
+ expect(messages.map(message => message.id)).toContain(optimisticId)
+ })
+
it('keeps one failed assistant bubble when refresh rebuilds the same tail turn under a new id', async () => {
const fixture = makeRefresh()
fixture.state.messages = [
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 6ff53e0a34b..24463a803e8 100644
--- a/apps/desktop/src/app/contrib/hooks/use-background-sync.ts
+++ b/apps/desktop/src/app/contrib/hooks/use-background-sync.ts
@@ -2,6 +2,7 @@ import { useStore } from '@nanostores/react'
import { type MutableRefObject, useCallback, useEffect, useRef } from 'react'
import { graftRefreshedTailOntoBackfill } from '@/app/chat/transcript-backfill'
+import { preserveLocalPendingTurnMessages } from '@/app/session/hooks/use-session-actions/utils'
import { getLatestSessionMessages, type ProfileScope } from '@/hermes'
import { preserveLocalAssistantErrors, sealOpenToolParts, toChatMessages } from '@/lib/chat-messages'
import { createClientSessionState } from '@/lib/chat-runtime'
@@ -215,8 +216,15 @@ export async function reconcileTileTranscripts({
runtimeSessionId,
state => ({
...state,
+ // An un-acked optimistic `user-*` row exists only here, so a
+ // 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(
- graftRefreshedTailOntoBackfill(messages, state.messages),
+ preserveLocalPendingTurnMessages(
+ graftRefreshedTailOntoBackfill(messages, state.messages),
+ state.messages
+ ),
state.messages
)
}),
@@ -294,7 +302,13 @@ export async function reconcileActiveTranscript({
// 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(graftRefreshedTailOntoBackfill(messages, state.messages), state.messages)
+ messages: preserveLocalAssistantErrors(
+ preserveLocalPendingTurnMessages(
+ graftRefreshedTailOntoBackfill(messages, state.messages),
+ state.messages
+ ),
+ state.messages
+ )
}),
storedSessionId
)
diff --git a/apps/desktop/src/app/contrib/wiring.tsx b/apps/desktop/src/app/contrib/wiring.tsx
index 02777b7cf65..7d6671db749 100644
--- a/apps/desktop/src/app/contrib/wiring.tsx
+++ b/apps/desktop/src/app/contrib/wiring.tsx
@@ -14,6 +14,7 @@ import { type CSSProperties, lazy, type ReactNode, Suspense, useCallback, useEff
import { useLocation, useNavigate } from 'react-router'
import { graftRefreshedTailOntoBackfill } from '@/app/chat/transcript-backfill'
+import { preserveLocalPendingTurnMessages } from '@/app/session/hooks/use-session-actions/utils'
import { formatRefValue } from '@/components/assistant-ui/directive-text'
import { BootFailureOverlay } from '@/components/boot-failure-overlay'
import { ConfirmHost } from '@/components/confirm-host'
@@ -500,9 +501,16 @@ export function ContribWiring({ children }: { children: ReactNode }) {
state => ({
...state,
// Post-turn rehydrate reads only the newest tail page — graft it
- // onto any backfilled older pages instead of dropping them.
+ // onto any backfilled older pages instead of dropping them, and
+ // keep any un-acked optimistic `user-*` row, which lives nowhere
+ // else (a reconnect-triggered rehydrate would otherwise lose a
+ // message the user then has to retype). Same composition order
+ // as reconcileAuthoritativeChatMessages.
messages: preserveLocalAssistantErrors(
- graftRefreshedTailOntoBackfill(messages, state.messages),
+ preserveLocalPendingTurnMessages(
+ graftRefreshedTailOntoBackfill(messages, state.messages),
+ state.messages
+ ),
state.messages
)
}),