From 060bd7aa56686b9d41c4887417cddd48d8c7da59 Mon Sep 17 00:00:00 2001 From: Jinhui Zheng Date: Sun, 22 Nov 2026 17:04:58 +0800 Subject: [PATCH 1/3] fix(web): preserve reply-only edits when a queued message leaves the queue --- .../components/chat/queuedMessageEdit.test.ts | 116 +++++++++++++----- .../src/components/chat/queuedMessageEdit.ts | 5 +- .../chat/useQueuedRunEdit.browser.test.tsx | 32 ++++- .../src/components/chat/useQueuedRunEdit.ts | 1 + 4 files changed, 120 insertions(+), 34 deletions(-) diff --git a/apps/web/src/components/chat/queuedMessageEdit.test.ts b/apps/web/src/components/chat/queuedMessageEdit.test.ts index f67d1843c38be..08841889fb7b7 100644 --- a/apps/web/src/components/chat/queuedMessageEdit.test.ts +++ b/apps/web/src/components/chat/queuedMessageEdit.test.ts @@ -15,6 +15,27 @@ import { prepareQueuedEditAttachments, recoverQueuedMessageEdit } from "./queued const environmentId = EnvironmentId.make("remote-environment"); const threadTarget = scopeThreadRef(environmentId, ThreadId.make("thread:edit")); const editTarget = DraftId.make("queued-edit:test"); +const originalMessage = { + editTarget, + threadTarget, + originalText: "Original message", + originalReplyTo: null, +}; +const reply = responseContextRecord({ + citation: { + version: 1, + environmentId, + threadId: ThreadId.make("thread:edit"), + messageId: MessageId.make("message:answer"), + text: "Done.", + start: 0, + end: 5, + prefix: "", + suffix: "", + }, + itemId: TurnItemId.make("item:answer"), + subagents: [], +}); const file: ComposerFileAttachment = { type: "file", id: "file:report", @@ -89,9 +110,7 @@ describe("queued message file edits", () => { uploadEnvironmentId: EnvironmentId.make("other-environment"), }; store.addFiles(editTarget, [file, elsewhere]); - expect( - recoverQueuedMessageEdit({ editTarget, threadTarget, originalText: "Original message" }), - ).toBe("kept"); + expect(recoverQueuedMessageEdit(originalMessage)).toBe("kept"); expect(store.getComposerDraft(threadTarget)?.prompt).toBe("Original message"); // An upload this environment cannot reach comes back as a file to reattach, not a dropped chip. const { uploadedAttachmentId: _id, uploadEnvironmentId: _env, ...reattach } = elsewhere; @@ -107,28 +126,11 @@ describe("queued message file edits", () => { "Other", ); const machine = environmentContextRecord(EnvironmentId.make("environment-vps"), "vps"); - const reply = responseContextRecord({ - citation: { - version: 1, - environmentId, - threadId: ThreadId.make("thread:edit"), - messageId: MessageId.make("message:answer"), - text: "Done.", - start: 0, - end: 5, - prefix: "", - suffix: "", - }, - itemId: TurnItemId.make("item:answer"), - subagents: [], - }); store.addThreadContexts(editTarget, [thread]); store.addEnvironmentContexts(editTarget, [machine]); store.setReplyTo(editTarget, reply); const editedPrompt = store.getComposerDraft(editTarget)!.prompt; - expect( - recoverQueuedMessageEdit({ editTarget, threadTarget, originalText: "Original message" }), - ).toBe("kept"); + expect(recoverQueuedMessageEdit(originalMessage)).toBe("kept"); expect(store.getComposerDraft(editTarget)).toBeNull(); expect(store.getComposerDraft(threadTarget)).toMatchObject({ prompt: editedPrompt, @@ -138,14 +140,73 @@ describe("queued message file edits", () => { }); }); + it.each([ + { + edit: "added reply", + originalReplyTo: null, + replyTo: reply, + originalText: "", + result: "kept", + }, + { + edit: "changed citation on the same response", + originalReplyTo: reply, + replyTo: responseContextRecord({ + citation: { ...reply.citation, text: "Updated.", end: 8 }, + itemId: reply.itemId, + subagents: [], + }), + result: "kept", + }, + { edit: "removed reply", originalReplyTo: reply, replyTo: null, result: "kept" }, + { + edit: "removed last content", + originalReplyTo: reply, + replyTo: null, + originalText: "", + result: "discarded", + }, + { + edit: "unchanged reply", + originalReplyTo: reply, + replyTo: structuredClone(reply), + result: "clean", + }, + { + edit: "changed text", + originalReplyTo: reply, + replyTo: reply, + prompt: "Changed", + result: "kept", + }, + ])( + "reports $result for a queued edit with $edit", + ({ + originalReplyTo, + replyTo, + originalText = "Original message", + prompt = originalText, + result, + }) => { + const store = useComposerDraftStore.getState(); + store.setPrompt(editTarget, prompt); + store.setReplyTo(editTarget, replyTo); + expect( + recoverQueuedMessageEdit({ editTarget, threadTarget, originalText, originalReplyTo }), + ).toBe(result); + expect(store.getComposerDraft(editTarget)).toBeNull(); + expect(store.getComposerDraft(threadTarget)).toEqual( + result === "kept" ? expect.objectContaining({ prompt, replyTo }) : null, + ); + }, + ); + it("reports a removed queued attachment as an edit when the run leaves", () => { const store = useComposerDraftStore.getState(); store.setPrompt(editTarget, "Original message"); expect( recoverQueuedMessageEdit({ - editTarget, - threadTarget, - originalText: "Original message", + ...originalMessage, removedExistingAttachment: true, }), ).toBe("kept"); @@ -154,8 +215,7 @@ describe("queued message file edits", () => { // An attachment-only message leaves nothing to put back in the composer. expect( recoverQueuedMessageEdit({ - editTarget, - threadTarget, + ...originalMessage, originalText: "", removedExistingAttachment: true, }), @@ -172,9 +232,7 @@ describe("queued message file edits", () => { [file.id]: { status: "ready", environmentId, attachmentId: uploadedFile.id }, }, }); - expect(recoverQueuedMessageEdit({ editTarget, threadTarget, originalText: "" })).toBe( - "discarded", - ); + expect(recoverQueuedMessageEdit({ ...originalMessage, originalText: "" })).toBe("discarded"); expect(store.getComposerDraft(threadTarget)?.prompt).toBe("Separate draft"); expect(store.getComposerDraft(editTarget)).toBeNull(); expect(readAttachmentUpload(file.id)).toBeUndefined(); diff --git a/apps/web/src/components/chat/queuedMessageEdit.ts b/apps/web/src/components/chat/queuedMessageEdit.ts index c3f14d6fe2247..e6b59e3a96b83 100644 --- a/apps/web/src/components/chat/queuedMessageEdit.ts +++ b/apps/web/src/components/chat/queuedMessageEdit.ts @@ -1,5 +1,6 @@ import { resolveQueuedEditExit } from "@t3tools/client-runtime/queued-run-edit"; -import type { ChatAttachment } from "@t3tools/contracts"; +import type { ChatAttachment, ResponseContextRecord } from "@t3tools/contracts"; +import * as Equal from "effect/Equal"; import { composerDraftHasUserContent, useComposerDraftStore, @@ -28,6 +29,7 @@ export function recoverQueuedMessageEdit(input: { readonly editTarget: ComposerThreadTarget; readonly threadTarget: ComposerThreadTarget; readonly originalText: string; + readonly originalReplyTo: ResponseContextRecord | null; readonly removedExistingAttachment?: boolean; }): "kept" | "discarded" | "clean" { const store = useComposerDraftStore.getState(); @@ -35,6 +37,7 @@ export function recoverQueuedMessageEdit(input: { const recovery = resolveQueuedEditExit({ editChanged: input.removedExistingAttachment === true || + !Equal.equals(draft?.replyTo ?? null, input.originalReplyTo) || (draft !== null && (draft.prompt !== input.originalText || draft.images.length > 0 || draft.files.length > 0)), // A cleared edit has nothing to put back, so it can only be discarded. diff --git a/apps/web/src/components/chat/useQueuedRunEdit.browser.test.tsx b/apps/web/src/components/chat/useQueuedRunEdit.browser.test.tsx index a88da51cce16e..7301a3163ed86 100644 --- a/apps/web/src/components/chat/useQueuedRunEdit.browser.test.tsx +++ b/apps/web/src/components/chat/useQueuedRunEdit.browser.test.tsx @@ -5,6 +5,7 @@ import { MessageId, RunId, ThreadId, + TurnItemId, type ScopedThreadRef, } from "@t3tools/contracts"; import { useImperativeHandle, type RefObject } from "react"; @@ -22,6 +23,8 @@ import { INLINE_TERMINAL_CONTEXT_PLACEHOLDER } from "../../lib/terminalContext"; import { environmentContextRecord, environmentContextReference, + responseContextRecord, + responseContextReference, threadContextReference, } from "../../lib/composerContextRecords"; import { deriveComposerSendState } from "../ChatView.logic"; @@ -434,16 +437,37 @@ describe("useQueuedRunEdit", () => { ).toBe("changed"); }); - it("moves a changed edit into the thread draft when its run starts while the user is away", async () => { + it("recovers a removed reply when the run starts while the user is away", async () => { const { hook } = setup(); - await using screen = await render(view(THREAD_A, "queued", hook)); - await beginChangedEdit(hook); + const reply = responseContextRecord({ + citation: { + version: 1, + ...THREAD_A, + messageId: MessageId.make("answer"), + text: "Done.", + start: 0, + end: 5, + prefix: "", + suffix: "", + }, + itemId: TurnItemId.make("item:answer"), + subagents: [], + }); + const messageId = MessageId.make("message-1"); + const text = `${formatInlineContextReference(responseContextReference(reply))} original`; + const messages = [{ id: messageId, text, context: { version: 1, records: [reply] } }]; + await using screen = await render(view(THREAD_A, "queued", hook, messages)); + await act(() => requireHook(hook).beginEdit({ runId: RUN, messageId, text, attachments: [] })); + const store = useComposerDraftStore.getState(); + const target = requireHook(hook).draftTarget; + expect(store.getComposerDraft(target)).toMatchObject({ prompt: "original", replyTo: reply }); + await act(() => store.setReplyTo(target, null)); await screen.rerender(view(THREAD_B, "queued", hook)); expect(promptOf(THREAD_A)).toBe(""); await act(() => setRuns(THREAD_A, "running")); - expect(promptOf(THREAD_A)).toBe("changed"); + expect(store.getComposerDraft(THREAD_A)).toMatchObject({ prompt: "original", replyTo: null }); }); it("does not copy a saved edit into the thread draft when the run starts mid-save", async () => { diff --git a/apps/web/src/components/chat/useQueuedRunEdit.ts b/apps/web/src/components/chat/useQueuedRunEdit.ts index 61907b93cfb22..77989f8aebc0d 100644 --- a/apps/web/src/components/chat/useQueuedRunEdit.ts +++ b/apps/web/src/components/chat/useQueuedRunEdit.ts @@ -312,6 +312,7 @@ export function useQueuedRunEdit(input: { editTarget: queuedRunEditDraftId(routeThreadRef, edit.runId), threadTarget: threadDraftTarget, originalText: edit.originalText, + originalReplyTo: reply ?? null, removedExistingAttachment: edit.removedExistingAttachment === true, }); if (recovery === "clean") return; From 4e7a1f7444e3602d305d4531dcb3a295da3e103a Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sun, 22 Nov 2026 09:32:37 +0000 Subject: [PATCH 2/3] test(web): keep queued edit reply coverage at the unit layer Restore the browser test for text-change recovery and drop the table row that repeated existing text-change behaviour. Co-Authored-By: Claude Opus 5.5 --- .../components/chat/queuedMessageEdit.test.ts | 19 ++--------- .../chat/useQueuedRunEdit.browser.test.tsx | 32 +++---------------- 2 files changed, 7 insertions(+), 44 deletions(-) diff --git a/apps/web/src/components/chat/queuedMessageEdit.test.ts b/apps/web/src/components/chat/queuedMessageEdit.test.ts index 08841889fb7b7..43112e529cf11 100644 --- a/apps/web/src/components/chat/queuedMessageEdit.test.ts +++ b/apps/web/src/components/chat/queuedMessageEdit.test.ts @@ -172,31 +172,18 @@ describe("queued message file edits", () => { replyTo: structuredClone(reply), result: "clean", }, - { - edit: "changed text", - originalReplyTo: reply, - replyTo: reply, - prompt: "Changed", - result: "kept", - }, ])( "reports $result for a queued edit with $edit", - ({ - originalReplyTo, - replyTo, - originalText = "Original message", - prompt = originalText, - result, - }) => { + ({ originalReplyTo, replyTo, originalText = "Original message", result }) => { const store = useComposerDraftStore.getState(); - store.setPrompt(editTarget, prompt); + store.setPrompt(editTarget, originalText); store.setReplyTo(editTarget, replyTo); expect( recoverQueuedMessageEdit({ editTarget, threadTarget, originalText, originalReplyTo }), ).toBe(result); expect(store.getComposerDraft(editTarget)).toBeNull(); expect(store.getComposerDraft(threadTarget)).toEqual( - result === "kept" ? expect.objectContaining({ prompt, replyTo }) : null, + result === "kept" ? expect.objectContaining({ prompt: originalText, replyTo }) : null, ); }, ); diff --git a/apps/web/src/components/chat/useQueuedRunEdit.browser.test.tsx b/apps/web/src/components/chat/useQueuedRunEdit.browser.test.tsx index 7301a3163ed86..a88da51cce16e 100644 --- a/apps/web/src/components/chat/useQueuedRunEdit.browser.test.tsx +++ b/apps/web/src/components/chat/useQueuedRunEdit.browser.test.tsx @@ -5,7 +5,6 @@ import { MessageId, RunId, ThreadId, - TurnItemId, type ScopedThreadRef, } from "@t3tools/contracts"; import { useImperativeHandle, type RefObject } from "react"; @@ -23,8 +22,6 @@ import { INLINE_TERMINAL_CONTEXT_PLACEHOLDER } from "../../lib/terminalContext"; import { environmentContextRecord, environmentContextReference, - responseContextRecord, - responseContextReference, threadContextReference, } from "../../lib/composerContextRecords"; import { deriveComposerSendState } from "../ChatView.logic"; @@ -437,37 +434,16 @@ describe("useQueuedRunEdit", () => { ).toBe("changed"); }); - it("recovers a removed reply when the run starts while the user is away", async () => { + it("moves a changed edit into the thread draft when its run starts while the user is away", async () => { const { hook } = setup(); - const reply = responseContextRecord({ - citation: { - version: 1, - ...THREAD_A, - messageId: MessageId.make("answer"), - text: "Done.", - start: 0, - end: 5, - prefix: "", - suffix: "", - }, - itemId: TurnItemId.make("item:answer"), - subagents: [], - }); - const messageId = MessageId.make("message-1"); - const text = `${formatInlineContextReference(responseContextReference(reply))} original`; - const messages = [{ id: messageId, text, context: { version: 1, records: [reply] } }]; - await using screen = await render(view(THREAD_A, "queued", hook, messages)); - await act(() => requireHook(hook).beginEdit({ runId: RUN, messageId, text, attachments: [] })); - const store = useComposerDraftStore.getState(); - const target = requireHook(hook).draftTarget; - expect(store.getComposerDraft(target)).toMatchObject({ prompt: "original", replyTo: reply }); - await act(() => store.setReplyTo(target, null)); + await using screen = await render(view(THREAD_A, "queued", hook)); + await beginChangedEdit(hook); await screen.rerender(view(THREAD_B, "queued", hook)); expect(promptOf(THREAD_A)).toBe(""); await act(() => setRuns(THREAD_A, "running")); - expect(store.getComposerDraft(THREAD_A)).toMatchObject({ prompt: "original", replyTo: null }); + expect(promptOf(THREAD_A)).toBe("changed"); }); it("does not copy a saved edit into the thread draft when the run starts mid-save", async () => { From 285b7a9ec52adc6fb2b869397fb024c2008102e1 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sun, 22 Nov 2026 11:34:54 +0000 Subject: [PATCH 3/3] test(web): trim queued reply edit cases to the ones that catch the bug Co-Authored-By: Claude Opus 5.5 --- .../components/chat/queuedMessageEdit.test.ts | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/apps/web/src/components/chat/queuedMessageEdit.test.ts b/apps/web/src/components/chat/queuedMessageEdit.test.ts index 43112e529cf11..49d22add3a239 100644 --- a/apps/web/src/components/chat/queuedMessageEdit.test.ts +++ b/apps/web/src/components/chat/queuedMessageEdit.test.ts @@ -148,24 +148,7 @@ describe("queued message file edits", () => { originalText: "", result: "kept", }, - { - edit: "changed citation on the same response", - originalReplyTo: reply, - replyTo: responseContextRecord({ - citation: { ...reply.citation, text: "Updated.", end: 8 }, - itemId: reply.itemId, - subagents: [], - }), - result: "kept", - }, { edit: "removed reply", originalReplyTo: reply, replyTo: null, result: "kept" }, - { - edit: "removed last content", - originalReplyTo: reply, - replyTo: null, - originalText: "", - result: "discarded", - }, { edit: "unchanged reply", originalReplyTo: reply,