From 08ce6548e7623bbdcc08bda9c31a1b3b67964db3 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 19 Nov 2026 04:52:26 +0000 Subject: [PATCH 1/2] test(web): a pending send survives leaving and returning to its thread Fails on main: reconcileOptimisticUserMessages drops an unacknowledged optimistic message when the view switches threads, so returning to the thread shows nothing until the server accepts the send (#38903). Co-Authored-By: Claude Opus 5.5 --- apps/web/src/components/chat/optimisticUserMessages.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/web/src/components/chat/optimisticUserMessages.test.ts b/apps/web/src/components/chat/optimisticUserMessages.test.ts index 0f7ce644e10bd..a561f119b9525 100644 --- a/apps/web/src/components/chat/optimisticUserMessages.test.ts +++ b/apps/web/src/components/chat/optimisticUserMessages.test.ts @@ -61,13 +61,13 @@ describe("optimistic user messages", () => { expect(state.handoff).toEqual({}); }); - it("releases the previous thread's messages when the thread changes", () => { + it("a send the server has not acknowledged is still shown after leaving and returning to its thread", () => { let state = applyOptimisticUserMessagesAction(emptyOptimisticUserMessages(threadA), threadA, [ message("a1"), ]); state = reconcileOptimisticUserMessages(state, threadB, ids()); state = reconcileOptimisticUserMessages(state, threadA, ids()); - expect(state).toEqual(emptyOptimisticUserMessages(threadA)); + expect(state.messages.map((m) => m.id)).toEqual([MessageId.make("a1")]); }); it("drops a late update for a thread the view has left", () => { From 5cd2e6210eeebffbf1a80659920f6d8d2b0b8819 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 19 Nov 2026 05:10:17 +0000 Subject: [PATCH 2/2] fix(web): a pending send stays shown after leaving and returning to its thread The chat view's optimistic messages were dropped when it switched threads, and later send-path updates for the left thread were ignored, so a send the server had not accepted yet vanished until the server echoed it (#38903). Unacknowledged messages of a left thread now wait per thread and come back when the view returns; a late add or failure for that thread applies to them. Their blob previews are dropped, since leaving the thread revokes them. Co-Authored-By: Claude Opus 5.5 --- .../chat/optimisticUserMessages.test.ts | 18 ++++- .../components/chat/optimisticUserMessages.ts | 67 ++++++++++++++++--- 2 files changed, 71 insertions(+), 14 deletions(-) diff --git a/apps/web/src/components/chat/optimisticUserMessages.test.ts b/apps/web/src/components/chat/optimisticUserMessages.test.ts index a561f119b9525..563d605de1543 100644 --- a/apps/web/src/components/chat/optimisticUserMessages.test.ts +++ b/apps/web/src/components/chat/optimisticUserMessages.test.ts @@ -70,15 +70,27 @@ describe("optimistic user messages", () => { expect(state.messages.map((m) => m.id)).toEqual([MessageId.make("a1")]); }); - it("drops a late update for a thread the view has left", () => { - const state = reconcileOptimisticUserMessages( + it("a late update for a thread the view has left applies to that thread's pending sends", () => { + let state = reconcileOptimisticUserMessages( applyOptimisticUserMessagesAction(emptyOptimisticUserMessages(threadA), threadA, [ message("a1"), ]), threadB, ids(), ); - expect(applyOptimisticUserMessagesAction(state, threadA, [message("a2")])).toBe(state); + state = applyOptimisticUserMessagesAction(state, threadA, (existing) => [ + ...existing, + message("a2", "blob:a2"), + ]); + // A failed send leaves, even while its thread is not shown. + state = applyOptimisticUserMessagesAction(state, threadA, (existing) => + existing.filter((m) => m.id !== MessageId.make("a1")), + ); + expect(state.messages).toEqual([]); + state = reconcileOptimisticUserMessages(state, threadA, ids()); + expect(state.messages.map((m) => m.id)).toEqual([MessageId.make("a2")]); + // Leaving the thread revoked its blob previews. + expect(state.messages[0]?.attachments?.[0]).not.toHaveProperty("previewUrl"); }); it("revokes a thread's blob previews once, when their use ends or the thread is left", () => { diff --git a/apps/web/src/components/chat/optimisticUserMessages.ts b/apps/web/src/components/chat/optimisticUserMessages.ts index 34a62f2324b40..facbeea072204 100644 --- a/apps/web/src/components/chat/optimisticUserMessages.ts +++ b/apps/web/src/components/chat/optimisticUserMessages.ts @@ -4,34 +4,76 @@ import * as Effect from "effect/Effect"; import * as Schema from "effect/Schema"; import { AsyncResult, Atom } from "effect/reactivity"; import type { SetStateAction } from "react"; -import type { ChatMessage } from "../../types"; +import { type ChatMessage, isImageAttachment } from "../../types"; import { collectUserMessageBlobPreviewUrls } from "../ChatView.logic"; /** * Messages shown before the server echoes them. An acknowledged message leaves `messages` in the * render its ack arrives, so it never reappears even if the server later drops its row. Its blob * previews stay in `handoff` until the server's copies load (see useAttachmentPreviewHandoff). + * Sends still pending in threads the view has left wait in `pendingByThread` until it returns. */ export interface OptimisticUserMessages { readonly threadId: ThreadId; readonly messages: ChatMessage[]; readonly handoff: Readonly>>; + readonly pendingByThread: Readonly>; } export function emptyOptimisticUserMessages(threadId: ThreadId): OptimisticUserMessages { - return { threadId, messages: [], handoff: {} }; + return { threadId, messages: [], handoff: {}, pendingByThread: {} }; +} + +// Leaving a thread revokes its blob previews, so a message kept for later must not point at them. +function withoutBlobPreviews(message: ChatMessage): ChatMessage { + if (collectUserMessageBlobPreviewUrls(message).length === 0) return message; + return { + ...message, + attachments: message.attachments?.map((attachment) => { + if (!isImageAttachment(attachment) || !attachment.previewUrl?.startsWith("blob:")) { + return attachment; + } + const { previewUrl: _revoked, ...rest } = attachment; + return rest; + }), + }; +} + +function withPending( + pendingByThread: OptimisticUserMessages["pendingByThread"], + threadId: ThreadId, + messages: ChatMessage[], +): OptimisticUserMessages["pendingByThread"] { + const { [threadId]: _previous, ...rest } = pendingByThread; + return messages.length === 0 ? rest : { ...rest, [threadId]: messages.map(withoutBlobPreviews) }; } /** - * The view's state for `threadId`: the previous thread's messages are released when the thread - * changes, and acknowledged messages are moved out of `messages`. Returns `state` when nothing changes. + * The view's state for `threadId`: when the thread changes, the previous thread's unacknowledged + * messages are kept for its return and its handoff is released, and acknowledged messages are moved + * out of `messages`. Returns `state` when nothing changes. */ export function reconcileOptimisticUserMessages( state: OptimisticUserMessages, threadId: ThreadId, acknowledgedIds: ReadonlySet, ): OptimisticUserMessages { - if (state.threadId !== threadId) return emptyOptimisticUserMessages(threadId); + if (state.threadId !== threadId) { + return reconcileOptimisticUserMessages( + { + threadId, + messages: state.pendingByThread[threadId] ?? [], + handoff: {}, + pendingByThread: withPending( + withPending(state.pendingByThread, threadId, []), + state.threadId, + state.messages, + ), + }, + threadId, + acknowledgedIds, + ); + } if (!state.messages.some((message) => acknowledgedIds.has(message.id))) return state; const messages: ChatMessage[] = []; const handoff: Record> = { ...state.handoff }; @@ -46,18 +88,21 @@ export function reconcileOptimisticUserMessages( return { ...state, messages, handoff }; } -/** Applies a send-path update. An update for a thread the view has since left is dropped. */ +/** Applies a send-path update. An update for a thread the view has left applies to its pending sends. */ export function applyOptimisticUserMessagesAction( state: OptimisticUserMessages, threadId: ThreadId, action: SetStateAction, ): OptimisticUserMessages { - if (state.threadId !== threadId) return state; - const next = typeof action === "function" ? action(state.messages) : action; + const current = + state.threadId === threadId ? state.messages : (state.pendingByThread[threadId] ?? []); + const next = typeof action === "function" ? action(current) : action; const unchanged = - next.length === state.messages.length && - next.every((message, index) => message === state.messages[index]); - return unchanged ? state : { ...state, messages: next }; + next.length === current.length && next.every((message, index) => message === current[index]); + if (unchanged) return state; + return state.threadId === threadId + ? { ...state, messages: next } + : { ...state, pendingByThread: withPending(state.pendingByThread, threadId, next) }; } export function dropOptimisticHandoff(