From ec1bd327373cf362a0b84c5189d5970b8a1ef355 Mon Sep 17 00:00:00 2001 From: gemini-4-neon Date: Sun, 22 Nov 2026 08:34:27 -0600 Subject: [PATCH 01/14] fix(web): editing a queued message keeps its attachment chips resolved Co-Authored-By: stefan --- apps/web/src/components/chat/ChatComposer.tsx | 80 ++++++++++++++----- .../components/chat/queuedMessageEdit.test.ts | 31 +++++++ .../src/components/chat/queuedMessageEdit.ts | 52 +++++++++++- .../chat/useQueuedRunEdit.browser.test.tsx | 42 ++++++++++ .../src/components/chat/useQueuedRunEdit.ts | 23 ++++++ .../composerContextPresentation.test.ts | 71 +++++++++++++++- .../composerContextPresentation.tsx | 64 +++++++++++++-- apps/web/src/lib/composerContextRecords.ts | 28 +++++-- 8 files changed, 358 insertions(+), 33 deletions(-) diff --git a/apps/web/src/components/chat/ChatComposer.tsx b/apps/web/src/components/chat/ChatComposer.tsx index af4871b45193..ffe7fd8d51c2 100644 --- a/apps/web/src/components/chat/ChatComposer.tsx +++ b/apps/web/src/components/chat/ChatComposer.tsx @@ -23,6 +23,7 @@ import { import type { KeybindingCommand, AssistantCitation, + ComposerContextId, ResponseContextRecord, ChatAttachment as ContractChatAttachment, ChatFileAttachment, @@ -287,6 +288,7 @@ import { useComposerImageGroups } from "./useComposerImageGroups"; import { useComposerStash } from "./useComposerStash"; import { useComposerContextImport } from "./useComposerContextImport"; import { useComposerAttachmentIntake } from "./useComposerAttachmentIntake"; +import { isQueuedFileAttachment, isQueuedImageAttachment } from "./queuedMessageEdit"; import { cancelHeldComposerSend, countPreparingComposerImages, @@ -457,6 +459,7 @@ export interface ChatComposerProps { editingQueuedAttachments: ReadonlyArray<{ readonly attachment: ContractChatAttachment; readonly url: string | null; + readonly contextIds?: ReadonlyArray | undefined; }> | null; onRemoveEditingQueuedAttachment: (attachmentId: string) => void; @@ -757,33 +760,55 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) cwd: gitCwd ?? undefined, projectId: activeProjectId ?? undefined, expandImage: (imageId: string) => { - const preview = buildExpandedImagePreview( - composerImages, - imageId, - copyComposerSnapshotStable, - ); + const preview = + buildExpandedImagePreview(composerImages, imageId, copyComposerSnapshotStable) ?? + buildExpandedImagePreview( + (editingQueuedAttachments ?? []).flatMap(({ attachment, url }) => + isQueuedImageAttachment(attachment) && url ? [{ ...attachment, previewUrl: url }] : [], + ), + imageId, + ); if (preview) onExpandImage(preview); }, - openFile: setPreviewFileId, + openFile: (fileId: string) => { + if (composerFiles.some((file) => file.id === fileId)) { + setPreviewFileId(fileId); + return; + } + const queuedFile = editingQueuedAttachments?.find( + ({ attachment }) => isQueuedFileAttachment(attachment) && attachment.id === fileId, + )?.attachment; + if (queuedFile && isQueuedFileAttachment(queuedFile)) onFileOpen(queuedFile); + }, openMention: (path: string) => useRightPanelStore.getState().openFile(routeThreadRef, path), expandVideo: (fileId: string) => { const file = composerFiles.find((candidate) => candidate.id === fileId); - if (!file || !isVideoAttachment(file)) return; - const localPreview = buildExpandedImagePreview([file], file.id); - if (localPreview) { - onExpandImage(localPreview); + if (file && isVideoAttachment(file)) { + const localPreview = buildExpandedImagePreview([file], file.id); + if (localPreview) { + onExpandImage(localPreview); + return; + } + if (file.uploadedAttachmentId === undefined || file.uploadEnvironmentId !== environmentId) { + return; + } + const persistedPreview = buildAttachmentVideoPreview(environmentId, { + type: "file", + id: file.uploadedAttachmentId, + name: file.name, + mimeType: file.mimeType, + sizeBytes: file.sizeBytes, + }); + if (persistedPreview) onExpandImage(persistedPreview); return; } - if (file.uploadedAttachmentId === undefined || file.uploadEnvironmentId !== environmentId) { + const queuedFile = editingQueuedAttachments?.find( + ({ attachment }) => isQueuedFileAttachment(attachment) && attachment.id === fileId, + )?.attachment; + if (!queuedFile || !isQueuedFileAttachment(queuedFile) || !isVideoAttachment(queuedFile)) { return; } - const persistedPreview = buildAttachmentVideoPreview(environmentId, { - type: "file", - id: file.uploadedAttachmentId, - name: file.name, - mimeType: file.mimeType, - sizeBytes: file.sizeBytes, - }); + const persistedPreview = buildAttachmentVideoPreview(environmentId, queuedFile); if (persistedPreview) onExpandImage(persistedPreview); }, openPullRequest: (event: React.MouseEvent, url: string) => { @@ -798,6 +823,8 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) environmentContexts: composerEnvironmentContexts, images: composerImages, files: composerFiles, + existingAttachments: editingQueuedAttachments ?? undefined, + environmentId, uploadsByImageId, }); const needsReattachFileCount = composerFiles.filter(composerFileNeedsReattach).length; @@ -1399,6 +1426,23 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) : record, ]; }), + ...(editingQueuedAttachments ?? []).flatMap(({ attachment, contextIds }) => { + if (!isQueuedImageAttachment(attachment) && !isQueuedFileAttachment(attachment)) return []; + return (contextIds ?? []) + .filter((contextId) => wanted.has(contextId)) + .map((contextId) => { + const record = attachmentContextRecord({ + attachment, + attachmentId: attachment.id, + contextId, + }); + return record.kind === "image" && + isQueuedImageAttachment(attachment) && + attachment.source + ? { ...record, source: attachment.source } + : record; + }); + }), ]; if (records.length === 0) return null; const fragment = encodeComposerContextFragment({ diff --git a/apps/web/src/components/chat/queuedMessageEdit.test.ts b/apps/web/src/components/chat/queuedMessageEdit.test.ts index 49d22add3a23..5b8573f3ac6c 100644 --- a/apps/web/src/components/chat/queuedMessageEdit.test.ts +++ b/apps/web/src/components/chat/queuedMessageEdit.test.ts @@ -10,6 +10,7 @@ import { } from "../../composerDraftStore"; import { readAttachmentUpload, useAttachmentUploadStore } from "../../lib/attachmentUploadQueue"; import { environmentContextRecord, responseContextRecord } from "../../lib/composerContextRecords"; +import { formatInlineContextReference } from "../../lib/composerContextReferences"; import { prepareQueuedEditAttachments, recoverQueuedMessageEdit } from "./queuedMessageEdit"; const environmentId = EnvironmentId.make("remote-environment"); @@ -193,6 +194,36 @@ describe("queued message file edits", () => { expect(store.getComposerDraft(threadTarget)).toBeNull(); }); + it("strips existing queued attachment references when recovering an edit into the thread draft", () => { + const store = useComposerDraftStore.getState(); + const chip = formatInlineContextReference({ + kind: "image", + contextId: "image_queued-1", + label: "image.png", + }); + store.setPrompt(editTarget, `Check ${chip} closely`); + expect( + recoverQueuedMessageEdit({ + ...originalMessage, + originalText: `Check ${chip}`, + existingAttachmentContextIds: ["image_queued-1"], + }), + ).toBe("kept"); + expect(store.getComposerDraft(threadTarget)?.prompt).toBe("Check closely"); + expect(store.getComposerDraft(editTarget)).toBeNull(); + + store.clearComposerContent(threadTarget); + store.setPrompt(editTarget, chip); + expect( + recoverQueuedMessageEdit({ + ...originalMessage, + originalText: `${chip} note`, + existingAttachmentContextIds: ["image_queued-1"], + }), + ).toBe("discarded"); + expect(store.getComposerDraft(threadTarget)).toBeNull(); + }); + it("does not overwrite a separate draft when the queued run leaves", () => { const store = useComposerDraftStore.getState(); store.setPrompt(threadTarget, "Separate draft"); diff --git a/apps/web/src/components/chat/queuedMessageEdit.ts b/apps/web/src/components/chat/queuedMessageEdit.ts index e6b59e3a96b8..63424bfca620 100644 --- a/apps/web/src/components/chat/queuedMessageEdit.ts +++ b/apps/web/src/components/chat/queuedMessageEdit.ts @@ -1,5 +1,12 @@ import { resolveQueuedEditExit } from "@t3tools/client-runtime/queued-run-edit"; -import type { ChatAttachment, ResponseContextRecord } from "@t3tools/contracts"; +import type { + ChatAttachment, + ChatFileAttachment, + ChatImageAttachment, + ComposerContextId, + OrchestrationMessageContext, + ResponseContextRecord, +} from "@t3tools/contracts"; import * as Equal from "effect/Equal"; import { composerDraftHasUserContent, @@ -11,6 +18,32 @@ import { import { msg } from "gt-react"; import { m } from "../../i18n/messages"; import { releaseDraftAttachments } from "../../lib/attachmentUploadQueue"; +import { removeInlineContextReference } from "../../lib/composerContextReferences"; + +export function isQueuedImageAttachment( + attachment: ChatAttachment, +): attachment is ChatImageAttachment { + return attachment.type === "image"; +} + +export function isQueuedFileAttachment( + attachment: ChatAttachment, +): attachment is ChatFileAttachment { + return attachment.type === "file"; +} + +export function attachmentContextIdsForRecord( + context: OrchestrationMessageContext | undefined, + attachment: Pick, +): ComposerContextId[] { + return (context?.records ?? []).flatMap((record) => + record.kind === attachment.type && + "attachmentId" in record && + record.attachmentId === attachment.id + ? [record.contextId] + : [], + ); +} /** Staged attachments upload as soon as they are added, so a dropped edit must release them. */ export function discardQueuedEditDraft(target: ComposerThreadTarget): void { @@ -31,9 +64,21 @@ export function recoverQueuedMessageEdit(input: { readonly originalText: string; readonly originalReplyTo: ResponseContextRecord | null; readonly removedExistingAttachment?: boolean; + readonly existingAttachmentContextIds?: ReadonlyArray; }): "kept" | "discarded" | "clean" { const store = useComposerDraftStore.getState(); const draft = store.getComposerDraft(input.editTarget); + const strippedPrompt = + draft && (input.existingAttachmentContextIds?.length ?? 0) > 0 + ? input.existingAttachmentContextIds!.reduce( + (prompt, contextId) => removeInlineContextReference(prompt, contextId).prompt, + draft.prompt, + ) + : draft?.prompt; + const recoveredDraft = + draft && strippedPrompt !== undefined && strippedPrompt !== draft.prompt + ? { ...draft, prompt: strippedPrompt } + : draft; const recovery = resolveQueuedEditExit({ editChanged: input.removedExistingAttachment === true || @@ -42,10 +87,13 @@ export function recoverQueuedMessageEdit(input: { (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. threadDraftEmpty: - composerDraftHasUserContent(draft) && + composerDraftHasUserContent(recoveredDraft) && !composerDraftHasUserContent(store.getComposerDraft(input.threadTarget)), }); if (recovery === "kept") { + if (draft && strippedPrompt !== undefined && strippedPrompt !== draft.prompt) { + store.setPrompt(input.editTarget, strippedPrompt); + } store.moveComposerContent(input.editTarget, input.threadTarget); store.clearComposerContent(input.editTarget); } else { diff --git a/apps/web/src/components/chat/useQueuedRunEdit.browser.test.tsx b/apps/web/src/components/chat/useQueuedRunEdit.browser.test.tsx index a88da51cce16..d110ac33c7c2 100644 --- a/apps/web/src/components/chat/useQueuedRunEdit.browser.test.tsx +++ b/apps/web/src/components/chat/useQueuedRunEdit.browser.test.tsx @@ -2,6 +2,7 @@ import type { ThreadStructure } from "@t3tools/client-runtime/state/shell"; import { EnvironmentId, ChatAttachmentId, + ComposerContextId, MessageId, RunId, ThreadId, @@ -343,6 +344,47 @@ describe("useQueuedRunEdit", () => { ); }); + it("binds existing attachment context ids for inline chips and strips them on removal", async () => { + const { hook } = setup(); + const messageId = MessageId.make("message-1"); + const attachmentId = ChatAttachmentId.make("server-image-1"); + const contextId = ComposerContextId.make("image_local-image-1"); + const attachment = { + type: "image" as const, + id: attachmentId, + name: "image.png", + mimeType: "image/png", + sizeBytes: 67, + }; + const chip = formatInlineContextReference({ kind: "image", contextId, label: "image.png" }); + const text = `Check ${chip} map`; + const imageRecord = { + version: 1 as const, + kind: "image" as const, + contextId, + label: "image.png", + attachmentId, + name: "image.png", + mimeType: "image/png", + sizeBytes: 67, + }; + const messages = [{ id: messageId, text, context: { version: 1, records: [imageRecord] } }]; + await using _screen = await render(view(THREAD_A, "queued", hook, messages)); + + await act(() => + requireHook(hook).beginEdit({ runId: RUN, messageId, text, attachments: [attachment] }), + ); + expect(requireHook(hook).editingAttachments).toEqual([ + expect.objectContaining({ attachment, contextIds: [contextId] }), + ]); + + await act(() => requireHook(hook).removeEditingAttachment(attachmentId)); + expect(requireHook(hook).editingAttachments).toEqual([]); + expect( + useComposerDraftStore.getState().getComposerDraft(requireHook(hook).draftTarget)?.prompt, + ).toBe("Check map"); + }); + it("opens an edit with only the thread and machine records its text still mentions", async () => { const { hook } = setup(); const thread = threadContextRecord(THREAD_B, "Support Any File Uploads"); diff --git a/apps/web/src/components/chat/useQueuedRunEdit.ts b/apps/web/src/components/chat/useQueuedRunEdit.ts index 77989f8aebc0..0d9f5f984884 100644 --- a/apps/web/src/components/chat/useQueuedRunEdit.ts +++ b/apps/web/src/components/chat/useQueuedRunEdit.ts @@ -69,6 +69,7 @@ import type { ChatComposerHandle } from "./ChatComposer"; import { fileAttachmentCapabilityBlockReason } from "./composerAttachmentFiles"; import { composerDraftContentChanged } from "./composerSend.logic"; import { + attachmentContextIdsForRecord, discardQueuedEditDraft, prepareQueuedEditAttachments, recoverQueuedMessageEdit, @@ -244,6 +245,7 @@ export function useQueuedRunEdit(input: { : editing.existingAttachments.map((attachment) => ({ attachment, url: urlByAttachmentId.get(attachment.id) ?? null, + contextIds: attachmentContextIdsForRecord(editing.context, attachment), })); const focusComposerSoon = () => { @@ -314,6 +316,11 @@ export function useQueuedRunEdit(input: { originalText: edit.originalText, originalReplyTo: reply ?? null, removedExistingAttachment: edit.removedExistingAttachment === true, + existingAttachmentContextIds: (edit.context?.records ?? []).flatMap((record) => + (record.kind === "image" || record.kind === "file") && "attachmentId" in record + ? [record.contextId] + : [], + ), }); if (recovery === "clean") return; const title = appAtomRegistry.get( @@ -345,6 +352,22 @@ export function useQueuedRunEdit(input: { focusComposerSoon(); }); const removeEditingAttachment = useStableCallback((attachmentId: string) => { + if (editing !== null) { + const removed = editing.existingAttachments.find( + (attachment) => attachment.id === attachmentId, + ); + const contextIds = removed ? attachmentContextIdsForRecord(editing.context, removed) : []; + if (contextIds.length > 0) { + const target = queuedRunEditDraftId(routeThreadRef, editing.runId); + const store = useComposerDraftStore.getState(); + const currentPrompt = store.getComposerDraft(target)?.prompt ?? ""; + const nextPrompt = contextIds.reduce( + (prompt, contextId) => removeInlineContextReference(prompt, contextId).prompt, + currentPrompt, + ); + if (nextPrompt !== currentPrompt) store.setPrompt(target, nextPrompt); + } + } queuedRunEdits.update(editKey, (current) => ({ ...current, removedExistingAttachment: true, diff --git a/apps/web/src/components/composerContextPresentation.test.ts b/apps/web/src/components/composerContextPresentation.test.ts index 77b0a5fb7815..248212c51484 100644 --- a/apps/web/src/components/composerContextPresentation.test.ts +++ b/apps/web/src/components/composerContextPresentation.test.ts @@ -1,8 +1,12 @@ -import { ThreadId } from "@t3tools/contracts"; +import { ChatAttachmentId, ComposerContextId, EnvironmentId, ThreadId } from "@t3tools/contracts"; import { describe, expect, it } from "vite-plus/test"; +import { composerFileNeedsReattach } from "~/composerDraftStore"; import { buildMessageContext } from "~/lib/composerContextRecords"; -import { composerContextRecordsFromDraft } from "./composerContextPresentation"; +import { + composerContextRecordsFromDraft, + uploadedContextRecordFromDraft, +} from "./composerContextPresentation"; describe("composerContextRecordsFromDraft", () => { it("resolves each wire reference to its own backing draft even when producer ids collide", () => { @@ -53,4 +57,67 @@ describe("composerContextRecordsFromDraft", () => { expect(draftRecords.get(record.contextId)?.kind).toBe(record.kind); } }); + + it("resolves existing queued attachments under their original wire context ids", () => { + const environmentId = EnvironmentId.make("env-1"); + const imageContextId = ComposerContextId.make("image_local-shot"); + const fileContextId = ComposerContextId.make("file_local-notes"); + const imageId = ChatAttachmentId.make("server-shot"); + const fileId = ChatAttachmentId.make("server-notes"); + const draftRecords = composerContextRecordsFromDraft({ + terminalContexts: [], + environmentId, + existingAttachments: [ + { + attachment: { + type: "image", + id: imageId, + name: "image.png", + mimeType: "image/png", + sizeBytes: 67, + }, + url: "http://127.0.0.1/assets/server-shot", + contextIds: [imageContextId], + }, + { + attachment: { + type: "file", + id: fileId, + name: "notes.txt", + mimeType: "text/plain", + sizeBytes: 12, + }, + url: null, + contextIds: [fileContextId], + }, + ], + }); + + const imageEntry = draftRecords.get(imageContextId); + expect(imageEntry).toMatchObject({ + kind: "image", + record: { + id: imageId, + name: "image.png", + previewUrl: "http://127.0.0.1/assets/server-shot", + }, + }); + expect(uploadedContextRecordFromDraft(imageEntry!)).toMatchObject({ + kind: "image", + contextId: imageContextId, + attachmentId: imageId, + }); + + const fileEntry = draftRecords.get(fileContextId); + expect(fileEntry?.kind).toBe("file"); + if (fileEntry?.kind === "file") { + expect(composerFileNeedsReattach(fileEntry.record)).toBe(false); + expect(fileEntry.record.uploadEnvironmentId).toBe(environmentId); + } + expect(uploadedContextRecordFromDraft(fileEntry!)).toMatchObject({ + kind: "file", + contextId: fileContextId, + attachmentId: fileId, + }); + }); }); diff --git a/apps/web/src/components/composerContextPresentation.tsx b/apps/web/src/components/composerContextPresentation.tsx index ab7ce9b40186..0fb3f31e5413 100644 --- a/apps/web/src/components/composerContextPresentation.tsx +++ b/apps/web/src/components/composerContextPresentation.tsx @@ -2,7 +2,10 @@ import ChatMarkdown from "./ChatMarkdown"; import { isSvgAttachment } from "@t3tools/shared/image"; import { ReadOnlySourcePreview } from "./files/AttachmentFilePreview"; import type { + ChatAttachment, + ComposerContextId, EnvironmentContextRecord, + EnvironmentId, PreviewAnnotationPayload, ThreadContextRecord, } from "@t3tools/contracts"; @@ -40,6 +43,7 @@ import { } from "~/lib/composerContextRecords"; import type { TerminalContextDraft } from "~/lib/terminalContext"; import type { ReviewCommentContext } from "~/reviewCommentContext"; +import { isQueuedFileAttachment, isQueuedImageAttachment } from "./chat/queuedMessageEdit"; import { ComposerPendingTerminalContextChip } from "./chat/ComposerPendingTerminalContexts"; import { EnvironmentContextChip } from "./EnvironmentContextChip"; import { ThreadContextChip } from "./ThreadContextChip"; @@ -60,6 +64,11 @@ import { UnresolvedChip, } from "./contextChipParts"; +export interface ComposerImageContextAttachment extends Omit { + readonly file: File | null; + readonly uploadedAttachmentId?: string | undefined; +} + export type ComposerDraftContextRecord = | { kind: "terminal"; record: TerminalContextDraft } | { kind: "review-comment"; record: ReviewCommentContext } @@ -68,8 +77,18 @@ export type ComposerDraftContextRecord = record: PreviewAnnotationPayload; screenshot?: ComposerImageAttachment | undefined; } - | { kind: "image"; record: ComposerImageAttachment; upload?: AttachmentUploadState | undefined } - | { kind: "file"; record: ComposerFileAttachment; upload?: AttachmentUploadState | undefined } + | { + kind: "image"; + record: ComposerImageContextAttachment; + upload?: AttachmentUploadState | undefined; + contextId?: ComposerContextId | undefined; + } + | { + kind: "file"; + record: ComposerFileAttachment; + upload?: AttachmentUploadState | undefined; + contextId?: ComposerContextId | undefined; + } | { kind: "thread"; record: ThreadContextRecord } | { kind: "environment"; record: EnvironmentContextRecord }; @@ -84,7 +103,7 @@ export const EMPTY_COMPOSER_CONTEXT_RECORDS: ComposerDraftContextRecords = new M export function uploadedContextRecordFromDraft(entry: ComposerDraftContextRecord) { if (entry.kind !== "image" && entry.kind !== "file") return null; - return uploadedAttachmentContextRecord(entry.record, entry.upload); + return uploadedAttachmentContextRecord(entry.record, entry.upload, entry.contextId); } export const ComposerContextRecordsContext = createContext( @@ -99,9 +118,44 @@ export function composerContextRecordsFromDraft(input: { environmentContexts?: ReadonlyArray; images?: ReadonlyArray; files?: ReadonlyArray; + existingAttachments?: + | ReadonlyArray<{ + readonly attachment: ChatAttachment; + readonly url: string | null; + readonly contextIds?: ReadonlyArray | undefined; + }> + | undefined; + environmentId?: EnvironmentId | undefined; uploadsByImageId?: Readonly>; }): ComposerDraftContextRecords { const records = new Map(); + for (const entry of input.existingAttachments ?? []) { + for (const contextId of entry.contextIds ?? []) { + if (isQueuedImageAttachment(entry.attachment)) { + records.set(contextId, { + kind: "image", + contextId, + record: { + ...entry.attachment, + previewUrl: entry.url ?? "", + file: null, + uploadedAttachmentId: entry.attachment.id, + }, + }); + } else if (isQueuedFileAttachment(entry.attachment)) { + records.set(contextId, { + kind: "file", + contextId, + record: { + ...entry.attachment, + file: null, + uploadedAttachmentId: entry.attachment.id, + ...(input.environmentId ? { uploadEnvironmentId: input.environmentId } : {}), + }, + }); + } + } + } for (const record of input.images ?? []) { records.set(imageContextReference(record).contextId, { kind: "image", @@ -183,7 +237,7 @@ function useUploadStatusSuffix( } function attachmentTooltip( - attachment: ComposerImageAttachment | ComposerFileAttachment, + attachment: Pick, upload: AttachmentUploadState | undefined, resolve: MessageResolver, ): string { @@ -193,7 +247,7 @@ function attachmentTooltip( } function ImageContextChip(props: { - record: ComposerImageAttachment; + record: ComposerImageContextAttachment; upload: AttachmentUploadState | undefined; }) { const actions = use(ComposerContextActionsContext); diff --git a/apps/web/src/lib/composerContextRecords.ts b/apps/web/src/lib/composerContextRecords.ts index 31b7239719f1..a3adcc975442 100644 --- a/apps/web/src/lib/composerContextRecords.ts +++ b/apps/web/src/lib/composerContextRecords.ts @@ -9,6 +9,8 @@ import { COMPOSER_CONTEXT_TERMINAL_TEXT_MAX_CHARS, } from "@t3tools/contracts"; import type { + ChatFileAttachment, + ChatImageAttachment, ComposerContextId, ComposerContextRecord, EnvironmentContextRecord, @@ -488,32 +490,46 @@ export function fileContextReference(file: ComposerFileAttachment): ComposerCont }; } +export type ComposerContextAttachment = + | ComposerImageAttachment + | ComposerFileAttachment + | (Omit & { + readonly file: File | null; + readonly uploadedAttachmentId?: string | undefined; + }) + | ChatImageAttachment + | ChatFileAttachment; + export interface BoundComposerAttachment { - attachment: ComposerImageAttachment | ComposerFileAttachment; + attachment: ComposerContextAttachment; attachmentId: string; + contextId?: ComposerContextId | undefined; } /** Returns null until the attachment has a server id. */ export function uploadedAttachmentContextRecord( - attachment: ComposerImageAttachment | ComposerFileAttachment, + attachment: ComposerContextAttachment, upload: AttachmentUploadState | undefined, + contextId?: ComposerContextId, ): ImageContextRecord | FileContextRecord | null { const attachmentId = - attachment.type === "file" && attachment.uploadedAttachmentId !== undefined + "uploadedAttachmentId" in attachment && attachment.uploadedAttachmentId !== undefined ? attachment.uploadedAttachmentId : upload?.status === "ready" ? upload.attachmentId : undefined; - return attachmentId === undefined ? null : attachmentContextRecord({ attachment, attachmentId }); + return attachmentId === undefined + ? null + : attachmentContextRecord({ attachment, attachmentId, contextId }); } export function attachmentContextRecord( bound: BoundComposerAttachment, ): ImageContextRecord | FileContextRecord { - const { attachment, attachmentId } = bound; + const { attachment, attachmentId, contextId } = bound; const base = { version: 1 as const, - contextId: toKindScopedComposerContextId(attachment.type, attachment.id), + contextId: contextId ?? toKindScopedComposerContextId(attachment.type, attachment.id), label: sanitizeComposerContextLabel(attachment.name, attachment.type), attachmentId, name: attachment.name, From 448597f92f5a73ebc1113bf8b5bb86b34718543f Mon Sep 17 00:00:00 2001 From: gemini-4-neon Date: Sun, 22 Nov 2026 08:54:11 -0600 Subject: [PATCH 02/14] fix(web): reconcile deleted queued file chips and preserve snapshot source Co-Authored-By: stefan --- apps/web/src/components/ChatView.tsx | 3 + apps/web/src/components/chat/ChatComposer.tsx | 32 +++-- .../chat/composerContextUndo.test.ts | 53 +++++++- .../components/chat/composerContextUndo.ts | 47 ++++++- .../chat/useQueuedRunEdit.browser.test.tsx | 122 +++++++++++++++--- .../src/components/chat/useQueuedRunEdit.ts | 71 +++++++--- .../composerContextPresentation.test.ts | 43 ++++-- apps/web/src/lib/composerContextRecords.ts | 8 +- 8 files changed, 315 insertions(+), 64 deletions(-) diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsx index d17af4f62075..0adfb02719b5 100644 --- a/apps/web/src/components/ChatView.tsx +++ b/apps/web/src/components/ChatView.tsx @@ -3943,6 +3943,9 @@ export default function ChatView(props: ChatViewProps) { onRemoveEditingQueuedAttachment={ queuedRunEdit.removeEditingAttachment } + onRestoreEditingQueuedAttachment={ + queuedRunEdit.restoreEditingAttachment + } /> )} diff --git a/apps/web/src/components/chat/ChatComposer.tsx b/apps/web/src/components/chat/ChatComposer.tsx index ffe7fd8d51c2..cd3baffb3cd3 100644 --- a/apps/web/src/components/chat/ChatComposer.tsx +++ b/apps/web/src/components/chat/ChatComposer.tsx @@ -462,6 +462,7 @@ export interface ChatComposerProps { readonly contextIds?: ReadonlyArray | undefined; }> | null; onRemoveEditingQueuedAttachment: (attachmentId: string) => void; + onRestoreEditingQueuedAttachment?: ((attachment: ContractChatAttachment) => void) | undefined; onCompactContext: () => void; onSend: ( @@ -603,6 +604,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) onFileOpen, editingQueuedAttachments, onRemoveEditingQueuedAttachment, + onRestoreEditingQueuedAttachment, } = props; const { activePendingProgress: incomingPendingProgress, @@ -1420,28 +1422,19 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) : null); if (!record) return []; if (attachment.file) copiedFiles.set(record.attachmentId, attachment.file); - return [ - record.kind === "image" && attachment.type === "image" && attachment.source - ? { ...record, source: attachment.source } - : record, - ]; + return [record]; }), ...(editingQueuedAttachments ?? []).flatMap(({ attachment, contextIds }) => { if (!isQueuedImageAttachment(attachment) && !isQueuedFileAttachment(attachment)) return []; return (contextIds ?? []) .filter((contextId) => wanted.has(contextId)) - .map((contextId) => { - const record = attachmentContextRecord({ + .map((contextId) => + attachmentContextRecord({ attachment, attachmentId: attachment.id, contextId, - }); - return record.kind === "image" && - isQueuedImageAttachment(attachment) && - attachment.source - ? { ...record, source: attachment.source } - : record; - }); + }), + ); }), ]; if (records.length === 0) return null; @@ -1806,6 +1799,11 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) const attachmentChanges = reconcileAttachmentContextReferences({ referencedContextIds: referenced, files: composerFiles, + queuedFiles: (editingQueuedAttachments ?? []).flatMap(({ attachment, contextIds }) => + isQueuedFileAttachment(attachment) && contextIds && contextIds.length > 0 + ? [{ attachment, contextIds }] + : [], + ), images: composerImages, previewAnnotations: composerPreviewAnnotations, retained: retainedContextPayloads, @@ -1825,6 +1823,12 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) if (attachmentChanges.filesToRestore.length > 0) { addComposerDraftFiles(attachmentDraftTarget, attachmentChanges.filesToRestore); } + for (const fileId of attachmentChanges.queuedFileIdsToRemove) { + onRemoveEditingQueuedAttachment(fileId); + } + for (const attachment of attachmentChanges.queuedFilesToRestore) { + onRestoreEditingQueuedAttachment?.(attachment); + } setComposerCursor(nextCursor); updateTrigger(trigger); }; diff --git a/apps/web/src/components/chat/composerContextUndo.test.ts b/apps/web/src/components/chat/composerContextUndo.test.ts index a763b47a0d6d..4589903e5ca7 100644 --- a/apps/web/src/components/chat/composerContextUndo.test.ts +++ b/apps/web/src/components/chat/composerContextUndo.test.ts @@ -1,4 +1,9 @@ -import type { PreviewAnnotationPayload } from "@t3tools/contracts"; +import { + ChatAttachmentId, + ComposerContextId, + type ChatFileAttachment, + type PreviewAnnotationPayload, +} from "@t3tools/contracts"; import { describe, expect, it } from "vite-plus/test"; import type { ComposerImageAttachment } from "../../composerDraftStore"; @@ -37,7 +42,7 @@ const annotation = { } satisfies PreviewAnnotationPayload; function retention(): RetainedAttachmentContextPayloads { - return { files: new Map(), previewAnnotations: new Map() }; + return { files: new Map(), queuedFiles: new Map(), previewAnnotations: new Map() }; } describe("reconcileAttachmentContextReferences", () => { @@ -88,4 +93,48 @@ describe("reconcileAttachmentContextReferences", () => { }), ]); }); + + it("removes and restores chip-backed queued files on delete and undo while preserving files with no chip", () => { + const retained = retention(); + const chipFile: ChatFileAttachment = { + type: "file", + id: ChatAttachmentId.make("server-file-1"), + name: "notes.txt", + mimeType: "text/plain", + sizeBytes: 12, + }; + const plainFile: ChatFileAttachment = { + type: "file", + id: ChatAttachmentId.make("server-file-2"), + name: "legacy.pdf", + mimeType: "application/pdf", + sizeBytes: 40, + }; + const chipContextId = ComposerContextId.make("file_local-1"); + + const removed = reconcileAttachmentContextReferences({ + referencedContextIds: new Set(), + files: [], + queuedFiles: [ + { attachment: chipFile, contextIds: [chipContextId] }, + { attachment: plainFile, contextIds: [] }, + ], + images: [], + previewAnnotations: [], + retained, + }); + expect(removed.queuedFileIdsToRemove).toEqual([chipFile.id]); + expect(removed.queuedFilesToRestore).toEqual([]); + + const restored = reconcileAttachmentContextReferences({ + referencedContextIds: new Set([chipContextId]), + files: [], + queuedFiles: [{ attachment: plainFile, contextIds: [] }], + images: [], + previewAnnotations: [], + retained, + }); + expect(restored.queuedFileIdsToRemove).toEqual([]); + expect(restored.queuedFilesToRestore).toEqual([chipFile]); + }); }); diff --git a/apps/web/src/components/chat/composerContextUndo.ts b/apps/web/src/components/chat/composerContextUndo.ts index fd2a0cde5934..bb77eaccd222 100644 --- a/apps/web/src/components/chat/composerContextUndo.ts +++ b/apps/web/src/components/chat/composerContextUndo.ts @@ -1,4 +1,6 @@ import type { + ChatFileAttachment, + ComposerContextId, EnvironmentContextRecord, PreviewAnnotationPayload, ThreadContextRecord, @@ -15,8 +17,14 @@ interface RetainedPreviewAnnotation { image: ComposerImageAttachment | undefined; } +export interface RetainedQueuedFileAttachment { + readonly attachment: ChatFileAttachment; + readonly contextIds: ReadonlyArray; +} + export interface RetainedAttachmentContextPayloads { files: Map; + queuedFiles: Map; previewAnnotations: Map; } @@ -34,6 +42,7 @@ function createRetainedContextPayloads(): RetainedContextPayloads { threads: new Map(), environments: new Map(), files: new Map(), + queuedFiles: new Map(), previewAnnotations: new Map(), }; } @@ -61,12 +70,15 @@ export function useRetainedContextPayloads(draftTargetKey: string): RetainedCont export function reconcileAttachmentContextReferences(input: { referencedContextIds: ReadonlySet; files: ReadonlyArray; + queuedFiles?: ReadonlyArray; images: ReadonlyArray; previewAnnotations: ReadonlyArray; retained: RetainedAttachmentContextPayloads; }): { filesToRemove: string[]; filesToRestore: ComposerFileAttachment[]; + queuedFileIdsToRemove: string[]; + queuedFilesToRestore: ChatFileAttachment[]; annotationIdsToRemove: string[]; annotationsToRestore: RetainedPreviewAnnotation[]; } { @@ -86,6 +98,32 @@ export function reconcileAttachmentContextReferences(input: { return file ? [file] : []; }); + const liveQueuedFiles = input.queuedFiles ?? []; + const liveQueuedFileIds = new Set(liveQueuedFiles.map((entry) => entry.attachment.id)); + const queuedFileIdsToRemove: string[] = []; + for (const entry of liveQueuedFiles) { + if (entry.contextIds.length === 0) continue; + if (entry.contextIds.some((contextId) => input.referencedContextIds.has(contextId))) continue; + for (const contextId of entry.contextIds) { + input.retained.queuedFiles.set(contextId, entry); + } + queuedFileIdsToRemove.push(entry.attachment.id); + } + const restoredQueuedFileIds = new Set(); + const queuedFilesToRestore: ChatFileAttachment[] = []; + for (const contextId of input.referencedContextIds) { + const entry = input.retained.queuedFiles.get(contextId); + if (!entry) continue; + if ( + liveQueuedFileIds.has(entry.attachment.id) || + restoredQueuedFileIds.has(entry.attachment.id) + ) { + continue; + } + restoredQueuedFileIds.add(entry.attachment.id); + queuedFilesToRestore.push(entry.attachment); + } + const imagesById = new Map(input.images.map((image) => [image.id, image])); const liveAnnotationContextIds = new Set( input.previewAnnotations.map((annotation) => previewAnnotationContextId(annotation.id)), @@ -106,5 +144,12 @@ export function reconcileAttachmentContextReferences(input: { return retained ? [retained] : []; }); - return { filesToRemove, filesToRestore, annotationIdsToRemove, annotationsToRestore }; + return { + filesToRemove, + filesToRestore, + queuedFileIdsToRemove, + queuedFilesToRestore, + annotationIdsToRemove, + annotationsToRestore, + }; } diff --git a/apps/web/src/components/chat/useQueuedRunEdit.browser.test.tsx b/apps/web/src/components/chat/useQueuedRunEdit.browser.test.tsx index d110ac33c7c2..b82820c759e0 100644 --- a/apps/web/src/components/chat/useQueuedRunEdit.browser.test.tsx +++ b/apps/web/src/components/chat/useQueuedRunEdit.browser.test.tsx @@ -344,45 +344,135 @@ describe("useQueuedRunEdit", () => { ); }); - it("binds existing attachment context ids for inline chips and strips them on removal", async () => { + it("binds existing attachment context ids, restores removed files on undo, and saves without removed chips or records", async () => { const { hook } = setup(); + mocks.editQueuedRun.mockResolvedValue({ _tag: "Success" }); const messageId = MessageId.make("message-1"); - const attachmentId = ChatAttachmentId.make("server-image-1"); - const contextId = ComposerContextId.make("image_local-image-1"); - const attachment = { + const imageId = ChatAttachmentId.make("server-image-1"); + const fileId = ChatAttachmentId.make("server-file-1"); + const unreferencedFileId = ChatAttachmentId.make("server-file-2"); + const imageContextId = ComposerContextId.make("image_local-image-1"); + const fileContextId = ComposerContextId.make("file_local-file-1"); + const staleContextId = ComposerContextId.make("file_stale-file-2"); + const imageAttachment = { type: "image" as const, - id: attachmentId, + id: imageId, name: "image.png", mimeType: "image/png", sizeBytes: 67, }; - const chip = formatInlineContextReference({ kind: "image", contextId, label: "image.png" }); - const text = `Check ${chip} map`; + const fileAttachment = { + type: "file" as const, + id: fileId, + name: "notes.txt", + mimeType: "text/plain", + sizeBytes: 12, + }; + const unreferencedFile = { + type: "file" as const, + id: unreferencedFileId, + name: "legacy.pdf", + mimeType: "application/pdf", + sizeBytes: 40, + }; + const imageChip = formatInlineContextReference({ + kind: "image", + contextId: imageContextId, + label: "image.png", + }); + const fileChip = formatInlineContextReference({ + kind: "file", + contextId: fileContextId, + label: "notes.txt", + }); + const text = `Check ${imageChip} and ${fileChip} map`; const imageRecord = { version: 1 as const, kind: "image" as const, - contextId, + contextId: imageContextId, label: "image.png", - attachmentId, + attachmentId: imageId, name: "image.png", mimeType: "image/png", sizeBytes: 67, }; - const messages = [{ id: messageId, text, context: { version: 1, records: [imageRecord] } }]; - await using _screen = await render(view(THREAD_A, "queued", hook, messages)); + const fileRecord = { + version: 1 as const, + kind: "file" as const, + contextId: fileContextId, + label: "notes.txt", + attachmentId: fileId, + name: "notes.txt", + mimeType: "text/plain", + sizeBytes: 12, + }; + const staleRecord = { + version: 1 as const, + kind: "file" as const, + contextId: staleContextId, + label: "legacy.pdf", + attachmentId: unreferencedFileId, + name: "legacy.pdf", + mimeType: "application/pdf", + sizeBytes: 40, + }; + const messages = [ + { + id: messageId, + text, + context: { version: 1, records: [imageRecord, fileRecord, staleRecord] }, + }, + ]; + await using screen = await render(view(THREAD_A, "queued", hook, messages)); await act(() => - requireHook(hook).beginEdit({ runId: RUN, messageId, text, attachments: [attachment] }), + requireHook(hook).beginEdit({ + runId: RUN, + messageId, + text, + attachments: [imageAttachment, fileAttachment, unreferencedFile], + }), ); expect(requireHook(hook).editingAttachments).toEqual([ - expect.objectContaining({ attachment, contextIds: [contextId] }), + expect.objectContaining({ attachment: imageAttachment, contextIds: [imageContextId] }), + expect.objectContaining({ attachment: fileAttachment, contextIds: [fileContextId] }), + expect.objectContaining({ attachment: unreferencedFile, contextIds: [] }), + ]); + + await act(() => requireHook(hook).removeEditingAttachment(fileId)); + expect(requireHook(hook).editingAttachments?.map(({ attachment }) => attachment.id)).toEqual([ + imageId, + unreferencedFileId, + ]); + await act(() => requireHook(hook).restoreEditingAttachment(fileAttachment)); + expect(requireHook(hook).editingAttachments?.map(({ attachment }) => attachment.id)).toEqual([ + imageId, + fileId, + unreferencedFileId, ]); - await act(() => requireHook(hook).removeEditingAttachment(attachmentId)); - expect(requireHook(hook).editingAttachments).toEqual([]); + await act(() => requireHook(hook).removeEditingAttachment(imageId)); + await act(() => requireHook(hook).removeEditingAttachment(fileId)); + expect(requireHook(hook).editingAttachments?.map(({ attachment }) => attachment.id)).toEqual([ + unreferencedFileId, + ]); expect( useComposerDraftStore.getState().getComposerDraft(requireHook(hook).draftTarget)?.prompt, - ).toBe("Check map"); + ).toBe("Check and map"); + + await screen.getByRole("button", { name: "Update queued message" }).click(); + await vi.waitFor(() => expect(mocks.editQueuedRun).toHaveBeenCalledOnce()); + expect(mocks.editQueuedRun).toHaveBeenCalledWith( + expect.objectContaining({ + input: expect.objectContaining({ + text: "Check and map", + edit: expect.objectContaining({ + attachments: [unreferencedFile], + context: { version: 1, records: [staleRecord] }, + }), + }), + }), + ); }); it("opens an edit with only the thread and machine records its text still mentions", async () => { diff --git a/apps/web/src/components/chat/useQueuedRunEdit.ts b/apps/web/src/components/chat/useQueuedRunEdit.ts index 0d9f5f984884..e26bf8fe4f32 100644 --- a/apps/web/src/components/chat/useQueuedRunEdit.ts +++ b/apps/web/src/components/chat/useQueuedRunEdit.ts @@ -81,10 +81,9 @@ interface EditingQueuedRun { readonly runId: RunId; readonly messageId: MessageId; readonly originalText: string; + readonly originalAttachments: ReadonlyArray; readonly existingAttachments: ReadonlyArray; readonly context?: OrchestrationMessageContext | undefined; - /** Removing a server attachment changes the message even when the draft looks untouched. */ - readonly removedExistingAttachment?: true; } // Keyed by the route thread, so an edit survives navigating away and back. @@ -239,13 +238,17 @@ export function useQueuedRunEdit(input: { const urlByAttachmentId = new Map( imageResources.map((resource, index) => [resource.attachmentId, imageUrls[index] ?? null]), ); + const originalReferencedContextIds = + editing === null ? null : new Set(collectInlineContextIds(editing.originalText)); const editingAttachments = editing === null ? null : editing.existingAttachments.map((attachment) => ({ attachment, url: urlByAttachmentId.get(attachment.id) ?? null, - contextIds: attachmentContextIdsForRecord(editing.context, attachment), + contextIds: attachmentContextIdsForRecord(editing.context, attachment).filter( + (contextId) => originalReferencedContextIds!.has(contextId), + ), })); const focusComposerSoon = () => { @@ -292,6 +295,7 @@ export function useQueuedRunEdit(input: { runId: request.runId, messageId: request.messageId, originalText: text, + originalAttachments: request.attachments, existingAttachments: request.attachments, context, }, @@ -315,7 +319,8 @@ export function useQueuedRunEdit(input: { threadTarget: threadDraftTarget, originalText: edit.originalText, originalReplyTo: reply ?? null, - removedExistingAttachment: edit.removedExistingAttachment === true, + removedExistingAttachment: + edit.existingAttachments.length < edit.originalAttachments.length, existingAttachmentContextIds: (edit.context?.records ?? []).flatMap((record) => (record.kind === "image" || record.kind === "file") && "attachmentId" in record ? [record.contextId] @@ -352,29 +357,52 @@ export function useQueuedRunEdit(input: { focusComposerSoon(); }); const removeEditingAttachment = useStableCallback((attachmentId: string) => { - if (editing !== null) { - const removed = editing.existingAttachments.find( - (attachment) => attachment.id === attachmentId, - ); - const contextIds = removed ? attachmentContextIdsForRecord(editing.context, removed) : []; - if (contextIds.length > 0) { - const target = queuedRunEditDraftId(routeThreadRef, editing.runId); - const store = useComposerDraftStore.getState(); - const currentPrompt = store.getComposerDraft(target)?.prompt ?? ""; - const nextPrompt = contextIds.reduce( - (prompt, contextId) => removeInlineContextReference(prompt, contextId).prompt, - currentPrompt, - ); - if (nextPrompt !== currentPrompt) store.setPrompt(target, nextPrompt); - } - } + const contextIds = + editing === null + ? [] + : (() => { + const removed = editing.existingAttachments.find( + (attachment) => attachment.id === attachmentId, + ); + return removed ? attachmentContextIdsForRecord(editing.context, removed) : []; + })(); queuedRunEdits.update(editKey, (current) => ({ ...current, - removedExistingAttachment: true, existingAttachments: current.existingAttachments.filter( (attachment) => attachment.id !== attachmentId, ), })); + if (editing !== null && contextIds.length > 0) { + const target = queuedRunEditDraftId(routeThreadRef, editing.runId); + const store = useComposerDraftStore.getState(); + const currentPrompt = store.getComposerDraft(target)?.prompt ?? ""; + const nextPrompt = contextIds.reduce( + (prompt, contextId) => removeInlineContextReference(prompt, contextId).prompt, + currentPrompt, + ); + if (nextPrompt !== currentPrompt) store.setPrompt(target, nextPrompt); + } + }); + const restoreEditingAttachment = useStableCallback((attachment: ChatAttachment) => { + queuedRunEdits.update(editKey, (current) => { + if (current.existingAttachments.some((entry) => entry.id === attachment.id)) { + return current; + } + const restoredIds = new Set([ + ...current.existingAttachments.map((entry) => entry.id), + attachment.id, + ]); + const existingAttachments = current.originalAttachments.filter((entry) => + restoredIds.has(entry.id), + ); + return { + ...current, + existingAttachments: + existingAttachments.length > current.existingAttachments.length + ? existingAttachments + : [...current.existingAttachments, attachment], + }; + }); }); // The same move as the queued row's "Send now" while a turn runs. @@ -504,6 +532,7 @@ export function useQueuedRunEdit(input: { beginEdit, cancelEdit, removeEditingAttachment, + restoreEditingAttachment, saveEdit, }; } diff --git a/apps/web/src/components/composerContextPresentation.test.ts b/apps/web/src/components/composerContextPresentation.test.ts index 248212c51484..641e190ef86a 100644 --- a/apps/web/src/components/composerContextPresentation.test.ts +++ b/apps/web/src/components/composerContextPresentation.test.ts @@ -2,7 +2,11 @@ import { ChatAttachmentId, ComposerContextId, EnvironmentId, ThreadId } from "@t import { describe, expect, it } from "vite-plus/test"; import { composerFileNeedsReattach } from "~/composerDraftStore"; -import { buildMessageContext } from "~/lib/composerContextRecords"; +import { + attachmentContextRecord, + buildMessageContext, + isSameComposerContextPayload, +} from "~/lib/composerContextRecords"; import { composerContextRecordsFromDraft, uploadedContextRecordFromDraft, @@ -64,18 +68,26 @@ describe("composerContextRecordsFromDraft", () => { const fileContextId = ComposerContextId.make("file_local-notes"); const imageId = ChatAttachmentId.make("server-shot"); const fileId = ChatAttachmentId.make("server-notes"); + const source = { + kind: "snap-shot" as const, + capturedAt: "2026-11-22T14:00:00.000Z", + appName: "Safari", + windowTitle: "Example", + }; + const imageAttachment = { + type: "image" as const, + id: imageId, + name: "image.png", + mimeType: "image/png", + sizeBytes: 67, + source, + }; const draftRecords = composerContextRecordsFromDraft({ terminalContexts: [], environmentId, existingAttachments: [ { - attachment: { - type: "image", - id: imageId, - name: "image.png", - mimeType: "image/png", - sizeBytes: 67, - }, + attachment: imageAttachment, url: "http://127.0.0.1/assets/server-shot", contextIds: [imageContextId], }, @@ -100,13 +112,26 @@ describe("composerContextRecordsFromDraft", () => { id: imageId, name: "image.png", previewUrl: "http://127.0.0.1/assets/server-shot", + source, }, }); - expect(uploadedContextRecordFromDraft(imageEntry!)).toMatchObject({ + const uploadedImageRecord = uploadedContextRecordFromDraft(imageEntry!); + expect(uploadedImageRecord).toMatchObject({ kind: "image", contextId: imageContextId, attachmentId: imageId, + source, }); + expect( + isSameComposerContextPayload( + uploadedImageRecord!, + attachmentContextRecord({ + attachment: imageAttachment, + attachmentId: imageId, + contextId: imageContextId, + }), + ), + ).toBe(true); const fileEntry = draftRecords.get(fileContextId); expect(fileEntry?.kind).toBe("file"); diff --git a/apps/web/src/lib/composerContextRecords.ts b/apps/web/src/lib/composerContextRecords.ts index a3adcc975442..6f1c9d3bb822 100644 --- a/apps/web/src/lib/composerContextRecords.ts +++ b/apps/web/src/lib/composerContextRecords.ts @@ -536,7 +536,13 @@ export function attachmentContextRecord( mimeType: attachment.mimeType, sizeBytes: attachment.sizeBytes, }; - return attachment.type === "image" ? { ...base, kind: "image" } : { ...base, kind: "file" }; + return attachment.type === "image" + ? { + ...base, + kind: "image", + ...(attachment.source !== undefined ? { source: attachment.source } : {}), + } + : { ...base, kind: "file" }; } export function buildMessageContext(input: { From 30401dd383723e382556a40575aeb026a01f7ebe Mon Sep 17 00:00:00 2001 From: gemini-4-neon Date: Sun, 22 Nov 2026 09:03:29 -0600 Subject: [PATCH 03/14] fix(web): restore removed queued attachments on undo and same-edit paste Co-Authored-By: stefan --- apps/web/src/components/ChatView.tsx | 3 + apps/web/src/components/chat/ChatComposer.tsx | 17 +++-- .../chat/composerContextUndo.test.ts | 32 +++++++-- .../components/chat/composerContextUndo.ts | 47 +++++++------ .../useComposerContextImport.browser.test.tsx | 69 +++++++++++++++++++ .../chat/useComposerContextImport.ts | 33 +++++++++ .../chat/useQueuedRunEdit.browser.test.tsx | 4 ++ .../src/components/chat/useQueuedRunEdit.ts | 23 ++++++- 8 files changed, 188 insertions(+), 40 deletions(-) diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsx index 0adfb02719b5..be1360704063 100644 --- a/apps/web/src/components/ChatView.tsx +++ b/apps/web/src/components/ChatView.tsx @@ -3940,6 +3940,9 @@ export default function ChatView(props: ChatViewProps) { onExpandImage={onExpandTimelineImageStable} onFileOpen={openFileAttachmentStable} editingQueuedAttachments={queuedRunEdit.editingAttachments} + removedEditingQueuedAttachments={ + queuedRunEdit.removedEditingAttachments + } onRemoveEditingQueuedAttachment={ queuedRunEdit.removeEditingAttachment } diff --git a/apps/web/src/components/chat/ChatComposer.tsx b/apps/web/src/components/chat/ChatComposer.tsx index cd3baffb3cd3..095e1ca0075b 100644 --- a/apps/web/src/components/chat/ChatComposer.tsx +++ b/apps/web/src/components/chat/ChatComposer.tsx @@ -121,6 +121,7 @@ import { useComposerTriggerState } from "./useComposerTriggerState"; import { useComposerMenuItems } from "./useComposerMenuItems"; import { useComposerMultilinePrompt } from "./useComposerMultilinePrompt"; import { + type QueuedAttachmentContextBinding, reconcileAttachmentContextReferences, useRetainedContextPayloads, } from "./composerContextUndo"; @@ -461,6 +462,7 @@ export interface ChatComposerProps { readonly url: string | null; readonly contextIds?: ReadonlyArray | undefined; }> | null; + removedEditingQueuedAttachments?: ReadonlyArray | undefined; onRemoveEditingQueuedAttachment: (attachmentId: string) => void; onRestoreEditingQueuedAttachment?: ((attachment: ContractChatAttachment) => void) | undefined; @@ -603,6 +605,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) onExpandImage, onFileOpen, editingQueuedAttachments, + removedEditingQueuedAttachments, onRemoveEditingQueuedAttachment, onRestoreEditingQueuedAttachment, } = props; @@ -1484,6 +1487,8 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) promptRef, pendingThreadImportsRef, addComposerFilesToDraft, + removedQueuedAttachments: removedEditingQueuedAttachments, + onRestoreQueuedAttachment: onRestoreEditingQueuedAttachment, }); // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- keeps the shared prompt ref in step with the committed draft prompt for imperative handlers @@ -1799,11 +1804,11 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) const attachmentChanges = reconcileAttachmentContextReferences({ referencedContextIds: referenced, files: composerFiles, - queuedFiles: (editingQueuedAttachments ?? []).flatMap(({ attachment, contextIds }) => - isQueuedFileAttachment(attachment) && contextIds && contextIds.length > 0 - ? [{ attachment, contextIds }] - : [], - ), + queuedAttachments: (editingQueuedAttachments ?? []).map(({ attachment, contextIds }) => ({ + attachment, + contextIds: contextIds ?? [], + })), + removedQueuedAttachments: removedEditingQueuedAttachments ?? [], images: composerImages, previewAnnotations: composerPreviewAnnotations, retained: retainedContextPayloads, @@ -1826,7 +1831,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) for (const fileId of attachmentChanges.queuedFileIdsToRemove) { onRemoveEditingQueuedAttachment(fileId); } - for (const attachment of attachmentChanges.queuedFilesToRestore) { + for (const attachment of attachmentChanges.queuedAttachmentsToRestore) { onRestoreEditingQueuedAttachment?.(attachment); } setComposerCursor(nextCursor); diff --git a/apps/web/src/components/chat/composerContextUndo.test.ts b/apps/web/src/components/chat/composerContextUndo.test.ts index 4589903e5ca7..07becf1f81ec 100644 --- a/apps/web/src/components/chat/composerContextUndo.test.ts +++ b/apps/web/src/components/chat/composerContextUndo.test.ts @@ -2,6 +2,7 @@ import { ChatAttachmentId, ComposerContextId, type ChatFileAttachment, + type ChatImageAttachment, type PreviewAnnotationPayload, } from "@t3tools/contracts"; import { describe, expect, it } from "vite-plus/test"; @@ -42,7 +43,7 @@ const annotation = { } satisfies PreviewAnnotationPayload; function retention(): RetainedAttachmentContextPayloads { - return { files: new Map(), queuedFiles: new Map(), previewAnnotations: new Map() }; + return { files: new Map(), previewAnnotations: new Map() }; } describe("reconcileAttachmentContextReferences", () => { @@ -94,7 +95,7 @@ describe("reconcileAttachmentContextReferences", () => { ]); }); - it("removes and restores chip-backed queued files on delete and undo while preserving files with no chip", () => { + it("reconciles chip-backed queued files on delete and restores removed queued images and files on undo", () => { const retained = retention(); const chipFile: ChatFileAttachment = { type: "file", @@ -110,31 +111,48 @@ describe("reconcileAttachmentContextReferences", () => { mimeType: "application/pdf", sizeBytes: 40, }; + const rowRemovedImage: ChatImageAttachment = { + type: "image", + id: ChatAttachmentId.make("server-image-1"), + name: "shot.png", + mimeType: "image/png", + sizeBytes: 67, + }; const chipContextId = ComposerContextId.make("file_local-1"); + const imageContextId = ComposerContextId.make("image_local-1"); + // Deleting a chip-backed file removes it; deleting an image chip or editing around an + // unreferenced file preserves both. const removed = reconcileAttachmentContextReferences({ referencedContextIds: new Set(), files: [], - queuedFiles: [ + queuedAttachments: [ { attachment: chipFile, contextIds: [chipContextId] }, { attachment: plainFile, contextIds: [] }, + { attachment: rowRemovedImage, contextIds: [imageContextId] }, ], + removedQueuedAttachments: [], images: [], previewAnnotations: [], retained, }); expect(removed.queuedFileIdsToRemove).toEqual([chipFile.id]); - expect(removed.queuedFilesToRestore).toEqual([]); + expect(removed.queuedAttachmentsToRestore).toEqual([]); + // Undoing restores both a keyboard-deleted file and a row-removed image when their chips return. const restored = reconcileAttachmentContextReferences({ - referencedContextIds: new Set([chipContextId]), + referencedContextIds: new Set([chipContextId, imageContextId]), files: [], - queuedFiles: [{ attachment: plainFile, contextIds: [] }], + queuedAttachments: [{ attachment: plainFile, contextIds: [] }], + removedQueuedAttachments: [ + { attachment: chipFile, contextIds: [chipContextId] }, + { attachment: rowRemovedImage, contextIds: [imageContextId] }, + ], images: [], previewAnnotations: [], retained, }); expect(restored.queuedFileIdsToRemove).toEqual([]); - expect(restored.queuedFilesToRestore).toEqual([chipFile]); + expect(restored.queuedAttachmentsToRestore).toEqual([chipFile, rowRemovedImage]); }); }); diff --git a/apps/web/src/components/chat/composerContextUndo.ts b/apps/web/src/components/chat/composerContextUndo.ts index bb77eaccd222..40fbd490ceb5 100644 --- a/apps/web/src/components/chat/composerContextUndo.ts +++ b/apps/web/src/components/chat/composerContextUndo.ts @@ -1,5 +1,5 @@ import type { - ChatFileAttachment, + ChatAttachment, ComposerContextId, EnvironmentContextRecord, PreviewAnnotationPayload, @@ -11,20 +11,20 @@ import type { ComposerFileAttachment, ComposerImageAttachment } from "../../comp import { fileContextReference, previewAnnotationContextId } from "../../lib/composerContextRecords"; import type { TerminalContextDraft } from "../../lib/terminalContext"; import type { ReviewCommentContext } from "../../reviewCommentContext"; +import { isQueuedFileAttachment } from "./queuedMessageEdit"; interface RetainedPreviewAnnotation { annotation: PreviewAnnotationPayload; image: ComposerImageAttachment | undefined; } -export interface RetainedQueuedFileAttachment { - readonly attachment: ChatFileAttachment; +export interface QueuedAttachmentContextBinding { + readonly attachment: ChatAttachment; readonly contextIds: ReadonlyArray; } export interface RetainedAttachmentContextPayloads { files: Map; - queuedFiles: Map; previewAnnotations: Map; } @@ -42,7 +42,6 @@ function createRetainedContextPayloads(): RetainedContextPayloads { threads: new Map(), environments: new Map(), files: new Map(), - queuedFiles: new Map(), previewAnnotations: new Map(), }; } @@ -70,7 +69,8 @@ export function useRetainedContextPayloads(draftTargetKey: string): RetainedCont export function reconcileAttachmentContextReferences(input: { referencedContextIds: ReadonlySet; files: ReadonlyArray; - queuedFiles?: ReadonlyArray; + queuedAttachments?: ReadonlyArray; + removedQueuedAttachments?: ReadonlyArray; images: ReadonlyArray; previewAnnotations: ReadonlyArray; retained: RetainedAttachmentContextPayloads; @@ -78,7 +78,7 @@ export function reconcileAttachmentContextReferences(input: { filesToRemove: string[]; filesToRestore: ComposerFileAttachment[]; queuedFileIdsToRemove: string[]; - queuedFilesToRestore: ChatFileAttachment[]; + queuedAttachmentsToRestore: ChatAttachment[]; annotationIdsToRemove: string[]; annotationsToRestore: RetainedPreviewAnnotation[]; } { @@ -98,30 +98,29 @@ export function reconcileAttachmentContextReferences(input: { return file ? [file] : []; }); - const liveQueuedFiles = input.queuedFiles ?? []; - const liveQueuedFileIds = new Set(liveQueuedFiles.map((entry) => entry.attachment.id)); + const liveQueuedAttachments = input.queuedAttachments ?? []; + const liveQueuedAttachmentIds = new Set( + liveQueuedAttachments.map((entry) => entry.attachment.id), + ); const queuedFileIdsToRemove: string[] = []; - for (const entry of liveQueuedFiles) { - if (entry.contextIds.length === 0) continue; + for (const entry of liveQueuedAttachments) { + if (!isQueuedFileAttachment(entry.attachment) || entry.contextIds.length === 0) continue; if (entry.contextIds.some((contextId) => input.referencedContextIds.has(contextId))) continue; - for (const contextId of entry.contextIds) { - input.retained.queuedFiles.set(contextId, entry); - } queuedFileIdsToRemove.push(entry.attachment.id); } - const restoredQueuedFileIds = new Set(); - const queuedFilesToRestore: ChatFileAttachment[] = []; - for (const contextId of input.referencedContextIds) { - const entry = input.retained.queuedFiles.get(contextId); - if (!entry) continue; + const restoredQueuedAttachmentIds = new Set(); + const queuedAttachmentsToRestore: ChatAttachment[] = []; + for (const entry of input.removedQueuedAttachments ?? []) { if ( - liveQueuedFileIds.has(entry.attachment.id) || - restoredQueuedFileIds.has(entry.attachment.id) + entry.contextIds.length === 0 || + liveQueuedAttachmentIds.has(entry.attachment.id) || + restoredQueuedAttachmentIds.has(entry.attachment.id) ) { continue; } - restoredQueuedFileIds.add(entry.attachment.id); - queuedFilesToRestore.push(entry.attachment); + if (!entry.contextIds.some((contextId) => input.referencedContextIds.has(contextId))) continue; + restoredQueuedAttachmentIds.add(entry.attachment.id); + queuedAttachmentsToRestore.push(entry.attachment); } const imagesById = new Map(input.images.map((image) => [image.id, image])); @@ -148,7 +147,7 @@ export function reconcileAttachmentContextReferences(input: { filesToRemove, filesToRestore, queuedFileIdsToRemove, - queuedFilesToRestore, + queuedAttachmentsToRestore, annotationIdsToRemove, annotationsToRestore, }; diff --git a/apps/web/src/components/chat/useComposerContextImport.browser.test.tsx b/apps/web/src/components/chat/useComposerContextImport.browser.test.tsx index d48e965895dd..a42b00f23a40 100644 --- a/apps/web/src/components/chat/useComposerContextImport.browser.test.tsx +++ b/apps/web/src/components/chat/useComposerContextImport.browser.test.tsx @@ -1,6 +1,7 @@ import { threadContextRecord } from "@t3tools/shared/composerContextReferences"; import { useEffect } from "react"; import { + ChatAttachmentId, type ComposerContextRecord, ComposerContextId, EnvironmentId, @@ -274,3 +275,71 @@ it.each(["same", "other-environment"] as const)( expect(store.getComposerDraft(target)!.threadContexts).toEqual(records); }, ); + +it("restores a cut queued file chip in the same edit without downloading or reminting its id", async () => { + const target = DraftId.make("queued-cut-paste"); + const store = useComposerDraftStore.getState(); + using _draft = { [Symbol.dispose]: () => store.clearComposerContent(target) }; + const environmentId = EnvironmentId.make("env-same"); + const attachmentId = ChatAttachmentId.make("server-file-1"); + const contextId = ComposerContextId.make("file_local-1"); + const removedAttachment = { + type: "file" as const, + id: attachmentId, + name: "notes.txt", + mimeType: "text/plain", + sizeBytes: 12, + }; + const onRestoreQueuedAttachment = vi.fn(); + const fetchSpy = vi.fn(); + using _fetch = stubGlobal("fetch", fetchSpy); + let importer!: Importer; + await using _screen = await render( + [], + removedQueuedAttachments: [{ attachment: removedAttachment, contextIds: [contextId] }], + onRestoreQueuedAttachment, + }} + ready={(value) => { + importer = value; + }} + />, + ); + const record = { + version: 1 as const, + kind: "file" as const, + contextId, + label: "notes.txt", + attachmentId, + name: "notes.txt", + mimeType: "text/plain", + sizeBytes: 12, + } satisfies ComposerContextRecord; + + let remapped!: ReadonlyMap; + await act(() => { + remapped = importer.importContextRecords([record], environmentId); + }); + + expect(remapped.get(contextId)).toBe(contextId); + expect(onRestoreQueuedAttachment).toHaveBeenCalledExactlyOnceWith(removedAttachment); + expect(fetchSpy).not.toHaveBeenCalled(); + expect(pendingDraftWork.has(target)).toBe(false); + expect(store.getComposerDraft(target)?.files ?? []).toEqual([]); +}); diff --git a/apps/web/src/components/chat/useComposerContextImport.ts b/apps/web/src/components/chat/useComposerContextImport.ts index 61d0abd7319a..dbf7ce270fa2 100644 --- a/apps/web/src/components/chat/useComposerContextImport.ts +++ b/apps/web/src/components/chat/useComposerContextImport.ts @@ -10,6 +10,7 @@ import * as Schema from "effect/Schema"; import type { RefObject } from "react"; import type { ChatComposerProps } from "./ChatComposer"; import { + type ChatAttachment, type ComposerContextClipboardFragment, type ComposerContextRecord, type EnvironmentId, @@ -26,6 +27,7 @@ import { import { type PendingUserInput } from "../../session-logic"; import { asKnownContextRecord, + attachmentContextRecord, composerContextImportLookupIds, isSameComposerContextPayload, previewAnnotationContextId, @@ -59,6 +61,8 @@ import { useAtomCommand } from "../../state/use-atom-command"; import { useAtomQueryRunner } from "~/state/use-atom-query-runner"; import { useStableCallback } from "../../hooks/useStableCallback"; import { countPreparingComposerImages } from "./preparingComposerImages"; +import type { QueuedAttachmentContextBinding } from "./composerContextUndo"; +import { isQueuedFileAttachment, isQueuedImageAttachment } from "./queuedMessageEdit"; const isUnreachable = Schema.is( Schema.Union([ConnectionTransientError, ConnectionBlockedError, EnvironmentRpcUnavailableError]), @@ -94,6 +98,8 @@ export function useComposerContextImport(input: { readonly promptRef: RefObject; readonly pendingThreadImportsRef: RefObject>; readonly addComposerFilesToDraft: (files: ComposerFileAttachment[]) => string[]; + readonly removedQueuedAttachments?: ReadonlyArray | undefined; + readonly onRestoreQueuedAttachment?: ((attachment: ChatAttachment) => void) | undefined; }) { const { environmentId, @@ -112,6 +118,8 @@ export function useComposerContextImport(input: { promptRef, pendingThreadImportsRef, addComposerFilesToDraft, + removedQueuedAttachments, + onRestoreQueuedAttachment, } = input; const getComposerDraft = useComposerDraftStore((store) => store.getComposerDraft); const addComposerDraftImages = useComposerDraftStore((store) => store.addImages); @@ -433,6 +441,31 @@ export function useComposerContextImport(input: { case "image": case "file": { if (skippedDependentAttachmentIds.has(record.contextId)) break; + if (sourceEnvironmentId === environmentId && !conflicts) { + const restorable = (removedQueuedAttachments ?? []).find( + ({ attachment, contextIds }) => + (isQueuedImageAttachment(attachment) || isQueuedFileAttachment(attachment)) && + attachment.type === record.kind && + contextIds.some((contextId) => + isSameComposerContextPayload( + attachmentContextRecord({ + attachment, + attachmentId: attachment.id, + contextId, + }), + record, + ), + ), + ); + if (restorable) { + const restoredContextId = + restorable.contextIds.find((contextId) => contextId === record.contextId) ?? + restorable.contextIds[0]!; + onRestoreQueuedAttachment?.(restorable.attachment); + rewritten.set(record.contextId, restoredContextId); + break; + } + } if (sourceEnvironmentId === null && !conflicts) { rewritten.set(record.contextId, record.contextId); break; diff --git a/apps/web/src/components/chat/useQueuedRunEdit.browser.test.tsx b/apps/web/src/components/chat/useQueuedRunEdit.browser.test.tsx index b82820c759e0..696dbde2c7dc 100644 --- a/apps/web/src/components/chat/useQueuedRunEdit.browser.test.tsx +++ b/apps/web/src/components/chat/useQueuedRunEdit.browser.test.tsx @@ -444,12 +444,16 @@ describe("useQueuedRunEdit", () => { imageId, unreferencedFileId, ]); + expect(requireHook(hook).removedEditingAttachments).toEqual([ + { attachment: fileAttachment, contextIds: [fileContextId] }, + ]); await act(() => requireHook(hook).restoreEditingAttachment(fileAttachment)); expect(requireHook(hook).editingAttachments?.map(({ attachment }) => attachment.id)).toEqual([ imageId, fileId, unreferencedFileId, ]); + expect(requireHook(hook).removedEditingAttachments).toEqual([]); await act(() => requireHook(hook).removeEditingAttachment(imageId)); await act(() => requireHook(hook).removeEditingAttachment(fileId)); diff --git a/apps/web/src/components/chat/useQueuedRunEdit.ts b/apps/web/src/components/chat/useQueuedRunEdit.ts index e26bf8fe4f32..89f2be2acfbf 100644 --- a/apps/web/src/components/chat/useQueuedRunEdit.ts +++ b/apps/web/src/components/chat/useQueuedRunEdit.ts @@ -240,16 +240,32 @@ export function useQueuedRunEdit(input: { ); const originalReferencedContextIds = editing === null ? null : new Set(collectInlineContextIds(editing.originalText)); + const attachmentContextIds = (attachment: ChatAttachment) => + editing === null + ? [] + : attachmentContextIdsForRecord(editing.context, attachment).filter((contextId) => + originalReferencedContextIds!.has(contextId), + ); const editingAttachments = editing === null ? null : editing.existingAttachments.map((attachment) => ({ attachment, url: urlByAttachmentId.get(attachment.id) ?? null, - contextIds: attachmentContextIdsForRecord(editing.context, attachment).filter( - (contextId) => originalReferencedContextIds!.has(contextId), - ), + contextIds: attachmentContextIds(attachment), })); + const existingAttachmentIds = new Set( + (editing?.existingAttachments ?? []).map((attachment) => attachment.id), + ); + const removedEditingAttachments = + editing === null + ? [] + : editing.originalAttachments + .filter((attachment) => !existingAttachmentIds.has(attachment.id)) + .map((attachment) => ({ + attachment, + contextIds: attachmentContextIds(attachment), + })); const focusComposerSoon = () => { window.requestAnimationFrame(() => composerRef.current?.focusAtEnd()); @@ -529,6 +545,7 @@ export function useQueuedRunEdit(input: { isSaving, draftTarget, editingAttachments, + removedEditingAttachments, beginEdit, cancelEdit, removeEditingAttachment, From b712a45bf52f1e8bf44c8d696b1e11dca625aaf1 Mon Sep 17 00:00:00 2001 From: gemini-4-neon Date: Sun, 22 Nov 2026 09:12:12 -0600 Subject: [PATCH 04/14] fix(web): remove restored queued images on redo and unit-test paste matching Co-Authored-By: stefan --- apps/web/src/components/chat/ChatComposer.tsx | 4 +- .../chat/composerContextUndo.test.ts | 32 ++++++++-- .../components/chat/composerContextUndo.ts | 25 ++++++-- .../components/chat/queuedMessageEdit.test.ts | 64 ++++++++++++++++++- .../src/components/chat/queuedMessageEdit.ts | 38 +++++++++++ .../useComposerContextImport.browser.test.tsx | 7 +- .../chat/useComposerContextImport.ts | 35 ++++------ 7 files changed, 165 insertions(+), 40 deletions(-) diff --git a/apps/web/src/components/chat/ChatComposer.tsx b/apps/web/src/components/chat/ChatComposer.tsx index 095e1ca0075b..83eedb10ddb0 100644 --- a/apps/web/src/components/chat/ChatComposer.tsx +++ b/apps/web/src/components/chat/ChatComposer.tsx @@ -1828,8 +1828,8 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) if (attachmentChanges.filesToRestore.length > 0) { addComposerDraftFiles(attachmentDraftTarget, attachmentChanges.filesToRestore); } - for (const fileId of attachmentChanges.queuedFileIdsToRemove) { - onRemoveEditingQueuedAttachment(fileId); + for (const attachmentId of attachmentChanges.queuedAttachmentIdsToRemove) { + onRemoveEditingQueuedAttachment(attachmentId); } for (const attachment of attachmentChanges.queuedAttachmentsToRestore) { onRestoreEditingQueuedAttachment?.(attachment); diff --git a/apps/web/src/components/chat/composerContextUndo.test.ts b/apps/web/src/components/chat/composerContextUndo.test.ts index 07becf1f81ec..6a023707a771 100644 --- a/apps/web/src/components/chat/composerContextUndo.test.ts +++ b/apps/web/src/components/chat/composerContextUndo.test.ts @@ -43,7 +43,11 @@ const annotation = { } satisfies PreviewAnnotationPayload; function retention(): RetainedAttachmentContextPayloads { - return { files: new Map(), previewAnnotations: new Map() }; + return { + files: new Map(), + previewAnnotations: new Map(), + restoredQueuedImageIds: new Set(), + }; } describe("reconcileAttachmentContextReferences", () => { @@ -95,7 +99,7 @@ describe("reconcileAttachmentContextReferences", () => { ]); }); - it("reconciles chip-backed queued files on delete and restores removed queued images and files on undo", () => { + it("reconciles chip-backed queued files on delete, restores removed queued images and files on undo, and re-removes restored images on redo", () => { const retained = retention(); const chipFile: ChatFileAttachment = { type: "file", @@ -121,7 +125,7 @@ describe("reconcileAttachmentContextReferences", () => { const chipContextId = ComposerContextId.make("file_local-1"); const imageContextId = ComposerContextId.make("image_local-1"); - // Deleting a chip-backed file removes it; deleting an image chip or editing around an + // Deleting a chip-backed file removes it; deleting an image chip directly or editing around an // unreferenced file preserves both. const removed = reconcileAttachmentContextReferences({ referencedContextIds: new Set(), @@ -136,7 +140,7 @@ describe("reconcileAttachmentContextReferences", () => { previewAnnotations: [], retained, }); - expect(removed.queuedFileIdsToRemove).toEqual([chipFile.id]); + expect(removed.queuedAttachmentIdsToRemove).toEqual([chipFile.id]); expect(removed.queuedAttachmentsToRestore).toEqual([]); // Undoing restores both a keyboard-deleted file and a row-removed image when their chips return. @@ -152,7 +156,25 @@ describe("reconcileAttachmentContextReferences", () => { previewAnnotations: [], retained, }); - expect(restored.queuedFileIdsToRemove).toEqual([]); + expect(restored.queuedAttachmentIdsToRemove).toEqual([]); expect(restored.queuedAttachmentsToRestore).toEqual([chipFile, rowRemovedImage]); + + // Redoing removes both the file and the row-removed image that undo brought back, + // while keeping an unreferenced file intact. + const redone = reconcileAttachmentContextReferences({ + referencedContextIds: new Set(), + files: [], + queuedAttachments: [ + { attachment: chipFile, contextIds: [chipContextId] }, + { attachment: plainFile, contextIds: [] }, + { attachment: rowRemovedImage, contextIds: [imageContextId] }, + ], + removedQueuedAttachments: [], + images: [], + previewAnnotations: [], + retained, + }); + expect(redone.queuedAttachmentIdsToRemove).toEqual([chipFile.id, rowRemovedImage.id]); + expect(redone.queuedAttachmentsToRestore).toEqual([]); }); }); diff --git a/apps/web/src/components/chat/composerContextUndo.ts b/apps/web/src/components/chat/composerContextUndo.ts index 40fbd490ceb5..f58d99e954c1 100644 --- a/apps/web/src/components/chat/composerContextUndo.ts +++ b/apps/web/src/components/chat/composerContextUndo.ts @@ -11,7 +11,7 @@ import type { ComposerFileAttachment, ComposerImageAttachment } from "../../comp import { fileContextReference, previewAnnotationContextId } from "../../lib/composerContextRecords"; import type { TerminalContextDraft } from "../../lib/terminalContext"; import type { ReviewCommentContext } from "../../reviewCommentContext"; -import { isQueuedFileAttachment } from "./queuedMessageEdit"; +import { isQueuedFileAttachment, isQueuedImageAttachment } from "./queuedMessageEdit"; interface RetainedPreviewAnnotation { annotation: PreviewAnnotationPayload; @@ -26,6 +26,7 @@ export interface QueuedAttachmentContextBinding { export interface RetainedAttachmentContextPayloads { files: Map; previewAnnotations: Map; + restoredQueuedImageIds: Set; } export interface RetainedContextPayloads extends RetainedAttachmentContextPayloads { @@ -43,6 +44,7 @@ function createRetainedContextPayloads(): RetainedContextPayloads { environments: new Map(), files: new Map(), previewAnnotations: new Map(), + restoredQueuedImageIds: new Set(), }; } @@ -77,7 +79,7 @@ export function reconcileAttachmentContextReferences(input: { }): { filesToRemove: string[]; filesToRestore: ComposerFileAttachment[]; - queuedFileIdsToRemove: string[]; + queuedAttachmentIdsToRemove: string[]; queuedAttachmentsToRestore: ChatAttachment[]; annotationIdsToRemove: string[]; annotationsToRestore: RetainedPreviewAnnotation[]; @@ -102,11 +104,19 @@ export function reconcileAttachmentContextReferences(input: { const liveQueuedAttachmentIds = new Set( liveQueuedAttachments.map((entry) => entry.attachment.id), ); - const queuedFileIdsToRemove: string[] = []; + const queuedAttachmentIdsToRemove: string[] = []; for (const entry of liveQueuedAttachments) { - if (!isQueuedFileAttachment(entry.attachment) || entry.contextIds.length === 0) continue; + if (entry.contextIds.length === 0) continue; if (entry.contextIds.some((contextId) => input.referencedContextIds.has(contextId))) continue; - queuedFileIdsToRemove.push(entry.attachment.id); + if (isQueuedFileAttachment(entry.attachment)) { + queuedAttachmentIdsToRemove.push(entry.attachment.id); + } else if ( + isQueuedImageAttachment(entry.attachment) && + input.retained.restoredQueuedImageIds.has(entry.attachment.id) + ) { + input.retained.restoredQueuedImageIds.delete(entry.attachment.id); + queuedAttachmentIdsToRemove.push(entry.attachment.id); + } } const restoredQueuedAttachmentIds = new Set(); const queuedAttachmentsToRestore: ChatAttachment[] = []; @@ -120,6 +130,9 @@ export function reconcileAttachmentContextReferences(input: { } if (!entry.contextIds.some((contextId) => input.referencedContextIds.has(contextId))) continue; restoredQueuedAttachmentIds.add(entry.attachment.id); + if (isQueuedImageAttachment(entry.attachment)) { + input.retained.restoredQueuedImageIds.add(entry.attachment.id); + } queuedAttachmentsToRestore.push(entry.attachment); } @@ -146,7 +159,7 @@ export function reconcileAttachmentContextReferences(input: { return { filesToRemove, filesToRestore, - queuedFileIdsToRemove, + queuedAttachmentIdsToRemove, queuedAttachmentsToRestore, annotationIdsToRemove, annotationsToRestore, diff --git a/apps/web/src/components/chat/queuedMessageEdit.test.ts b/apps/web/src/components/chat/queuedMessageEdit.test.ts index 5b8573f3ac6c..0ebfdacdbb60 100644 --- a/apps/web/src/components/chat/queuedMessageEdit.test.ts +++ b/apps/web/src/components/chat/queuedMessageEdit.test.ts @@ -1,4 +1,11 @@ -import { EnvironmentId, MessageId, ThreadId, TurnItemId } from "@t3tools/contracts"; +import { + ChatAttachmentId, + ComposerContextId, + EnvironmentId, + MessageId, + ThreadId, + TurnItemId, +} from "@t3tools/contracts"; import { scopeThreadRef } from "@t3tools/client-runtime/environment"; import { threadContextRecord } from "@t3tools/shared/composerContextReferences"; import { beforeEach, describe, expect, it } from "vite-plus/test"; @@ -11,7 +18,11 @@ import { import { readAttachmentUpload, useAttachmentUploadStore } from "../../lib/attachmentUploadQueue"; import { environmentContextRecord, responseContextRecord } from "../../lib/composerContextRecords"; import { formatInlineContextReference } from "../../lib/composerContextReferences"; -import { prepareQueuedEditAttachments, recoverQueuedMessageEdit } from "./queuedMessageEdit"; +import { + matchRestorableQueuedAttachment, + prepareQueuedEditAttachments, + recoverQueuedMessageEdit, +} from "./queuedMessageEdit"; const environmentId = EnvironmentId.make("remote-environment"); const threadTarget = scopeThreadRef(environmentId, ThreadId.make("thread:edit")); @@ -224,6 +235,55 @@ describe("queued message file edits", () => { expect(store.getComposerDraft(threadTarget)).toBeNull(); }); + it("matches a removed queued attachment for same-environment paste while rejecting other environments or payloads", () => { + const fileId = ChatAttachmentId.make("server-file-1"); + const contextId = ComposerContextId.make("file_local-1"); + const removedAttachment = { + type: "file" as const, + id: fileId, + name: "notes.txt", + mimeType: "text/plain", + sizeBytes: 12, + }; + const record = { + version: 1 as const, + kind: "file" as const, + contextId, + label: "notes.txt", + attachmentId: fileId, + name: "notes.txt", + mimeType: "text/plain", + sizeBytes: 12, + }; + + expect( + matchRestorableQueuedAttachment({ + environmentId, + sourceEnvironmentId: environmentId, + record, + removedQueuedAttachments: [{ attachment: removedAttachment, contextIds: [contextId] }], + }), + ).toEqual({ attachment: removedAttachment, contextId }); + + expect( + matchRestorableQueuedAttachment({ + environmentId, + sourceEnvironmentId: EnvironmentId.make("other-environment"), + record, + removedQueuedAttachments: [{ attachment: removedAttachment, contextIds: [contextId] }], + }), + ).toBeNull(); + + expect( + matchRestorableQueuedAttachment({ + environmentId, + sourceEnvironmentId: environmentId, + record: { ...record, attachmentId: ChatAttachmentId.make("different-file") }, + removedQueuedAttachments: [{ attachment: removedAttachment, contextIds: [contextId] }], + }), + ).toBeNull(); + }); + it("does not overwrite a separate draft when the queued run leaves", () => { const store = useComposerDraftStore.getState(); store.setPrompt(threadTarget, "Separate draft"); diff --git a/apps/web/src/components/chat/queuedMessageEdit.ts b/apps/web/src/components/chat/queuedMessageEdit.ts index 63424bfca620..ca58777dcb44 100644 --- a/apps/web/src/components/chat/queuedMessageEdit.ts +++ b/apps/web/src/components/chat/queuedMessageEdit.ts @@ -4,6 +4,8 @@ import type { ChatFileAttachment, ChatImageAttachment, ComposerContextId, + ComposerContextRecord, + EnvironmentId, OrchestrationMessageContext, ResponseContextRecord, } from "@t3tools/contracts"; @@ -18,6 +20,10 @@ import { import { msg } from "gt-react"; import { m } from "../../i18n/messages"; import { releaseDraftAttachments } from "../../lib/attachmentUploadQueue"; +import { + attachmentContextRecord, + isSameComposerContextPayload, +} from "../../lib/composerContextRecords"; import { removeInlineContextReference } from "../../lib/composerContextReferences"; export function isQueuedImageAttachment( @@ -45,6 +51,38 @@ export function attachmentContextIdsForRecord( ); } +export function matchRestorableQueuedAttachment(input: { + readonly environmentId: EnvironmentId; + readonly sourceEnvironmentId: EnvironmentId | null; + readonly record: Extract; + readonly removedQueuedAttachments: ReadonlyArray<{ + readonly attachment: ChatAttachment; + readonly contextIds: ReadonlyArray; + }>; +}): { readonly attachment: ChatAttachment; readonly contextId: ComposerContextId } | null { + if (input.sourceEnvironmentId !== input.environmentId) return null; + const match = input.removedQueuedAttachments.find( + ({ attachment, contextIds }) => + (isQueuedImageAttachment(attachment) || isQueuedFileAttachment(attachment)) && + attachment.type === input.record.kind && + contextIds.some((contextId) => + isSameComposerContextPayload( + attachmentContextRecord({ + attachment, + attachmentId: attachment.id, + contextId, + }), + input.record, + ), + ), + ); + if (!match || match.contextIds.length === 0) return null; + const contextId = + match.contextIds.find((candidate) => candidate === input.record.contextId) ?? + match.contextIds[0]!; + return { attachment: match.attachment, contextId }; +} + /** Staged attachments upload as soon as they are added, so a dropped edit must release them. */ export function discardQueuedEditDraft(target: ComposerThreadTarget): void { const store = useComposerDraftStore.getState(); diff --git a/apps/web/src/components/chat/useComposerContextImport.browser.test.tsx b/apps/web/src/components/chat/useComposerContextImport.browser.test.tsx index a42b00f23a40..df30568df045 100644 --- a/apps/web/src/components/chat/useComposerContextImport.browser.test.tsx +++ b/apps/web/src/components/chat/useComposerContextImport.browser.test.tsx @@ -291,7 +291,9 @@ it("restores a cut queued file chip in the same edit without downloading or remi sizeBytes: 12, }; const onRestoreQueuedAttachment = vi.fn(); + const beginWorkSpy = vi.spyOn(pendingDraftWork, "begin"); const fetchSpy = vi.fn(); + using _begin = { [Symbol.dispose]: () => beginWorkSpy.mockRestore() }; using _fetch = stubGlobal("fetch", fetchSpy); let importer!: Importer; await using _screen = await render( @@ -333,12 +335,15 @@ it("restores a cut queued file chip in the same edit without downloading or remi } satisfies ComposerContextRecord; let remapped!: ReadonlyMap; - await act(() => { + await act(async () => { remapped = importer.importContextRecords([record], environmentId); + await Promise.resolve(); + await Promise.resolve(); }); expect(remapped.get(contextId)).toBe(contextId); expect(onRestoreQueuedAttachment).toHaveBeenCalledExactlyOnceWith(removedAttachment); + expect(beginWorkSpy).not.toHaveBeenCalled(); expect(fetchSpy).not.toHaveBeenCalled(); expect(pendingDraftWork.has(target)).toBe(false); expect(store.getComposerDraft(target)?.files ?? []).toEqual([]); diff --git a/apps/web/src/components/chat/useComposerContextImport.ts b/apps/web/src/components/chat/useComposerContextImport.ts index dbf7ce270fa2..ed1d8ef4988d 100644 --- a/apps/web/src/components/chat/useComposerContextImport.ts +++ b/apps/web/src/components/chat/useComposerContextImport.ts @@ -27,7 +27,6 @@ import { import { type PendingUserInput } from "../../session-logic"; import { asKnownContextRecord, - attachmentContextRecord, composerContextImportLookupIds, isSameComposerContextPayload, previewAnnotationContextId, @@ -62,7 +61,7 @@ import { useAtomQueryRunner } from "~/state/use-atom-query-runner"; import { useStableCallback } from "../../hooks/useStableCallback"; import { countPreparingComposerImages } from "./preparingComposerImages"; import type { QueuedAttachmentContextBinding } from "./composerContextUndo"; -import { isQueuedFileAttachment, isQueuedImageAttachment } from "./queuedMessageEdit"; +import { matchRestorableQueuedAttachment } from "./queuedMessageEdit"; const isUnreachable = Schema.is( Schema.Union([ConnectionTransientError, ConnectionBlockedError, EnvironmentRpcUnavailableError]), @@ -441,28 +440,16 @@ export function useComposerContextImport(input: { case "image": case "file": { if (skippedDependentAttachmentIds.has(record.contextId)) break; - if (sourceEnvironmentId === environmentId && !conflicts) { - const restorable = (removedQueuedAttachments ?? []).find( - ({ attachment, contextIds }) => - (isQueuedImageAttachment(attachment) || isQueuedFileAttachment(attachment)) && - attachment.type === record.kind && - contextIds.some((contextId) => - isSameComposerContextPayload( - attachmentContextRecord({ - attachment, - attachmentId: attachment.id, - contextId, - }), - record, - ), - ), - ); - if (restorable) { - const restoredContextId = - restorable.contextIds.find((contextId) => contextId === record.contextId) ?? - restorable.contextIds[0]!; - onRestoreQueuedAttachment?.(restorable.attachment); - rewritten.set(record.contextId, restoredContextId); + if (!conflicts) { + const restored = matchRestorableQueuedAttachment({ + environmentId, + sourceEnvironmentId, + record, + removedQueuedAttachments: removedQueuedAttachments ?? [], + }); + if (restored) { + onRestoreQueuedAttachment?.(restored.attachment); + rewritten.set(record.contextId, restored.contextId); break; } } From eb93331be918866a7abe1844de113432720bc062 Mon Sep 17 00:00:00 2001 From: gemini-4-neon Date: Sun, 22 Nov 2026 09:19:55 -0600 Subject: [PATCH 05/14] fix(web): gate queued image redo removal on editor history transactions Co-Authored-By: stefan --- .../components/ComposerPromptEditorTiptap.tsx | 3 + apps/web/src/components/chat/ChatComposer.tsx | 3 + .../chat/composerContextUndo.test.ts | 34 +++++++++ .../components/chat/composerContextUndo.ts | 6 ++ .../useComposerContextImport.browser.test.tsx | 74 ------------------- 5 files changed, 46 insertions(+), 74 deletions(-) diff --git a/apps/web/src/components/ComposerPromptEditorTiptap.tsx b/apps/web/src/components/ComposerPromptEditorTiptap.tsx index 402156b00128..ef7d7357ec25 100644 --- a/apps/web/src/components/ComposerPromptEditorTiptap.tsx +++ b/apps/web/src/components/ComposerPromptEditorTiptap.tsx @@ -203,6 +203,8 @@ export interface ComposerPromptEditorProps { contextIds: string[], /** False when only the caret or selection moved. */ textChanged: boolean, + /** True when ProseMirror history (undo or redo) produced this change. */ + isHistory?: boolean, ) => void; onVisibleSelectionChange?: () => void; onCommandKeyDown?: (key: string, event: KeyboardEvent, isTaskItem?: boolean) => boolean; @@ -1127,6 +1129,7 @@ function ComposerPromptEditorTiptapInner( suppressTrigger, contextIds, previousSnapshot.value !== nextValue, + isHistoryTransaction(transaction), ); }; diff --git a/apps/web/src/components/chat/ChatComposer.tsx b/apps/web/src/components/chat/ChatComposer.tsx index 83eedb10ddb0..3de15bb4e0d8 100644 --- a/apps/web/src/components/chat/ChatComposer.tsx +++ b/apps/web/src/components/chat/ChatComposer.tsx @@ -1694,6 +1694,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) cursorAdjacentToMention: boolean, contextIds: string[], textChanged: boolean, + isHistory = false, ) => { refreshWorkspaceSnapshotOnTyping(); const trigger = cursorAdjacentToMention @@ -1809,6 +1810,8 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) contextIds: contextIds ?? [], })), removedQueuedAttachments: removedEditingQueuedAttachments ?? [], + isHistory, + textChanged, images: composerImages, previewAnnotations: composerPreviewAnnotations, retained: retainedContextPayloads, diff --git a/apps/web/src/components/chat/composerContextUndo.test.ts b/apps/web/src/components/chat/composerContextUndo.test.ts index 6a023707a771..95179e5b4373 100644 --- a/apps/web/src/components/chat/composerContextUndo.test.ts +++ b/apps/web/src/components/chat/composerContextUndo.test.ts @@ -152,6 +152,8 @@ describe("reconcileAttachmentContextReferences", () => { { attachment: chipFile, contextIds: [chipContextId] }, { attachment: rowRemovedImage, contextIds: [imageContextId] }, ], + isHistory: true, + textChanged: true, images: [], previewAnnotations: [], retained, @@ -170,11 +172,43 @@ describe("reconcileAttachmentContextReferences", () => { { attachment: rowRemovedImage, contextIds: [imageContextId] }, ], removedQueuedAttachments: [], + isHistory: true, + textChanged: true, images: [], previewAnnotations: [], retained, }); expect(redone.queuedAttachmentIdsToRemove).toEqual([chipFile.id, rowRemovedImage.id]); expect(redone.queuedAttachmentsToRestore).toEqual([]); + + // Undoing again and then deleting the image chip directly via Backspace (non-history edit) + // keeps the image on the thumbnail shelf. + reconcileAttachmentContextReferences({ + referencedContextIds: new Set([imageContextId]), + files: [], + queuedAttachments: [{ attachment: plainFile, contextIds: [] }], + removedQueuedAttachments: [{ attachment: rowRemovedImage, contextIds: [imageContextId] }], + isHistory: true, + textChanged: true, + images: [], + previewAnnotations: [], + retained, + }); + const backspaced = reconcileAttachmentContextReferences({ + referencedContextIds: new Set(), + files: [], + queuedAttachments: [ + { attachment: plainFile, contextIds: [] }, + { attachment: rowRemovedImage, contextIds: [imageContextId] }, + ], + removedQueuedAttachments: [], + isHistory: false, + textChanged: true, + images: [], + previewAnnotations: [], + retained, + }); + expect(backspaced.queuedAttachmentIdsToRemove).toEqual([]); + expect(backspaced.queuedAttachmentsToRestore).toEqual([]); }); }); diff --git a/apps/web/src/components/chat/composerContextUndo.ts b/apps/web/src/components/chat/composerContextUndo.ts index f58d99e954c1..a73be5d32967 100644 --- a/apps/web/src/components/chat/composerContextUndo.ts +++ b/apps/web/src/components/chat/composerContextUndo.ts @@ -73,6 +73,8 @@ export function reconcileAttachmentContextReferences(input: { files: ReadonlyArray; queuedAttachments?: ReadonlyArray; removedQueuedAttachments?: ReadonlyArray; + isHistory?: boolean; + textChanged?: boolean; images: ReadonlyArray; previewAnnotations: ReadonlyArray; retained: RetainedAttachmentContextPayloads; @@ -100,6 +102,9 @@ export function reconcileAttachmentContextReferences(input: { return file ? [file] : []; }); + if (input.textChanged && !input.isHistory) { + input.retained.restoredQueuedImageIds.clear(); + } const liveQueuedAttachments = input.queuedAttachments ?? []; const liveQueuedAttachmentIds = new Set( liveQueuedAttachments.map((entry) => entry.attachment.id), @@ -111,6 +116,7 @@ export function reconcileAttachmentContextReferences(input: { if (isQueuedFileAttachment(entry.attachment)) { queuedAttachmentIdsToRemove.push(entry.attachment.id); } else if ( + input.isHistory === true && isQueuedImageAttachment(entry.attachment) && input.retained.restoredQueuedImageIds.has(entry.attachment.id) ) { diff --git a/apps/web/src/components/chat/useComposerContextImport.browser.test.tsx b/apps/web/src/components/chat/useComposerContextImport.browser.test.tsx index df30568df045..d48e965895dd 100644 --- a/apps/web/src/components/chat/useComposerContextImport.browser.test.tsx +++ b/apps/web/src/components/chat/useComposerContextImport.browser.test.tsx @@ -1,7 +1,6 @@ import { threadContextRecord } from "@t3tools/shared/composerContextReferences"; import { useEffect } from "react"; import { - ChatAttachmentId, type ComposerContextRecord, ComposerContextId, EnvironmentId, @@ -275,76 +274,3 @@ it.each(["same", "other-environment"] as const)( expect(store.getComposerDraft(target)!.threadContexts).toEqual(records); }, ); - -it("restores a cut queued file chip in the same edit without downloading or reminting its id", async () => { - const target = DraftId.make("queued-cut-paste"); - const store = useComposerDraftStore.getState(); - using _draft = { [Symbol.dispose]: () => store.clearComposerContent(target) }; - const environmentId = EnvironmentId.make("env-same"); - const attachmentId = ChatAttachmentId.make("server-file-1"); - const contextId = ComposerContextId.make("file_local-1"); - const removedAttachment = { - type: "file" as const, - id: attachmentId, - name: "notes.txt", - mimeType: "text/plain", - sizeBytes: 12, - }; - const onRestoreQueuedAttachment = vi.fn(); - const beginWorkSpy = vi.spyOn(pendingDraftWork, "begin"); - const fetchSpy = vi.fn(); - using _begin = { [Symbol.dispose]: () => beginWorkSpy.mockRestore() }; - using _fetch = stubGlobal("fetch", fetchSpy); - let importer!: Importer; - await using _screen = await render( - [], - removedQueuedAttachments: [{ attachment: removedAttachment, contextIds: [contextId] }], - onRestoreQueuedAttachment, - }} - ready={(value) => { - importer = value; - }} - />, - ); - const record = { - version: 1 as const, - kind: "file" as const, - contextId, - label: "notes.txt", - attachmentId, - name: "notes.txt", - mimeType: "text/plain", - sizeBytes: 12, - } satisfies ComposerContextRecord; - - let remapped!: ReadonlyMap; - await act(async () => { - remapped = importer.importContextRecords([record], environmentId); - await Promise.resolve(); - await Promise.resolve(); - }); - - expect(remapped.get(contextId)).toBe(contextId); - expect(onRestoreQueuedAttachment).toHaveBeenCalledExactlyOnceWith(removedAttachment); - expect(beginWorkSpy).not.toHaveBeenCalled(); - expect(fetchSpy).not.toHaveBeenCalled(); - expect(pendingDraftWork.has(target)).toBe(false); - expect(store.getComposerDraft(target)?.files ?? []).toEqual([]); -}); From e34e3120f4d33ad20ff8e3409c60ee6e181e0cc8 Mon Sep 17 00:00:00 2001 From: gemini-4-neon Date: Sun, 22 Nov 2026 09:27:46 -0600 Subject: [PATCH 06/14] fix(web): retain restored queued image history tracking across typing Co-Authored-By: stefan --- apps/web/src/components/chat/ChatComposer.tsx | 8 ++++-- .../chat/composerContextUndo.test.ts | 27 ++++++++++++++----- .../components/chat/composerContextUndo.ts | 9 +++---- .../chat/useQueuedRunEdit.browser.test.tsx | 2 +- 4 files changed, 31 insertions(+), 15 deletions(-) diff --git a/apps/web/src/components/chat/ChatComposer.tsx b/apps/web/src/components/chat/ChatComposer.tsx index 3de15bb4e0d8..2961fa0b8403 100644 --- a/apps/web/src/components/chat/ChatComposer.tsx +++ b/apps/web/src/components/chat/ChatComposer.tsx @@ -1488,7 +1488,12 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) pendingThreadImportsRef, addComposerFilesToDraft, removedQueuedAttachments: removedEditingQueuedAttachments, - onRestoreQueuedAttachment: onRestoreEditingQueuedAttachment, + onRestoreQueuedAttachment: (attachment) => { + if (isQueuedImageAttachment(attachment)) { + retainedContextPayloads.restoredQueuedImageIds.add(attachment.id); + } + onRestoreEditingQueuedAttachment?.(attachment); + }, }); // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- keeps the shared prompt ref in step with the committed draft prompt for imperative handlers @@ -1811,7 +1816,6 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) })), removedQueuedAttachments: removedEditingQueuedAttachments ?? [], isHistory, - textChanged, images: composerImages, previewAnnotations: composerPreviewAnnotations, retained: retainedContextPayloads, diff --git a/apps/web/src/components/chat/composerContextUndo.test.ts b/apps/web/src/components/chat/composerContextUndo.test.ts index 95179e5b4373..d5c5b649285a 100644 --- a/apps/web/src/components/chat/composerContextUndo.test.ts +++ b/apps/web/src/components/chat/composerContextUndo.test.ts @@ -153,7 +153,6 @@ describe("reconcileAttachmentContextReferences", () => { { attachment: rowRemovedImage, contextIds: [imageContextId] }, ], isHistory: true, - textChanged: true, images: [], previewAnnotations: [], retained, @@ -161,7 +160,25 @@ describe("reconcileAttachmentContextReferences", () => { expect(restored.queuedAttachmentIdsToRemove).toEqual([]); expect(restored.queuedAttachmentsToRestore).toEqual([chipFile, rowRemovedImage]); - // Redoing removes both the file and the row-removed image that undo brought back, + // Typing around a restored image chip (non-history change while chip stays referenced) + // does not lose history-undo tracking when a subsequent Undo removes the pasted/restored chip. + const typed = reconcileAttachmentContextReferences({ + referencedContextIds: new Set([chipContextId, imageContextId]), + files: [], + queuedAttachments: [ + { attachment: chipFile, contextIds: [chipContextId] }, + { attachment: plainFile, contextIds: [] }, + { attachment: rowRemovedImage, contextIds: [imageContextId] }, + ], + removedQueuedAttachments: [], + isHistory: false, + images: [], + previewAnnotations: [], + retained, + }); + expect(typed.queuedAttachmentIdsToRemove).toEqual([]); + + // Redoing or undoing past the restoration removes both the file and the restored image, // while keeping an unreferenced file intact. const redone = reconcileAttachmentContextReferences({ referencedContextIds: new Set(), @@ -173,7 +190,6 @@ describe("reconcileAttachmentContextReferences", () => { ], removedQueuedAttachments: [], isHistory: true, - textChanged: true, images: [], previewAnnotations: [], retained, @@ -182,14 +198,13 @@ describe("reconcileAttachmentContextReferences", () => { expect(redone.queuedAttachmentsToRestore).toEqual([]); // Undoing again and then deleting the image chip directly via Backspace (non-history edit) - // keeps the image on the thumbnail shelf. + // keeps the image on the thumbnail shelf and clears its history-removal flag. reconcileAttachmentContextReferences({ referencedContextIds: new Set([imageContextId]), files: [], queuedAttachments: [{ attachment: plainFile, contextIds: [] }], removedQueuedAttachments: [{ attachment: rowRemovedImage, contextIds: [imageContextId] }], isHistory: true, - textChanged: true, images: [], previewAnnotations: [], retained, @@ -203,12 +218,12 @@ describe("reconcileAttachmentContextReferences", () => { ], removedQueuedAttachments: [], isHistory: false, - textChanged: true, images: [], previewAnnotations: [], retained, }); expect(backspaced.queuedAttachmentIdsToRemove).toEqual([]); expect(backspaced.queuedAttachmentsToRestore).toEqual([]); + expect(retained.restoredQueuedImageIds.size).toBe(0); }); }); diff --git a/apps/web/src/components/chat/composerContextUndo.ts b/apps/web/src/components/chat/composerContextUndo.ts index a73be5d32967..5b2380494e4b 100644 --- a/apps/web/src/components/chat/composerContextUndo.ts +++ b/apps/web/src/components/chat/composerContextUndo.ts @@ -74,7 +74,6 @@ export function reconcileAttachmentContextReferences(input: { queuedAttachments?: ReadonlyArray; removedQueuedAttachments?: ReadonlyArray; isHistory?: boolean; - textChanged?: boolean; images: ReadonlyArray; previewAnnotations: ReadonlyArray; retained: RetainedAttachmentContextPayloads; @@ -102,9 +101,6 @@ export function reconcileAttachmentContextReferences(input: { return file ? [file] : []; }); - if (input.textChanged && !input.isHistory) { - input.retained.restoredQueuedImageIds.clear(); - } const liveQueuedAttachments = input.queuedAttachments ?? []; const liveQueuedAttachmentIds = new Set( liveQueuedAttachments.map((entry) => entry.attachment.id), @@ -116,12 +112,13 @@ export function reconcileAttachmentContextReferences(input: { if (isQueuedFileAttachment(entry.attachment)) { queuedAttachmentIdsToRemove.push(entry.attachment.id); } else if ( - input.isHistory === true && isQueuedImageAttachment(entry.attachment) && input.retained.restoredQueuedImageIds.has(entry.attachment.id) ) { input.retained.restoredQueuedImageIds.delete(entry.attachment.id); - queuedAttachmentIdsToRemove.push(entry.attachment.id); + if (input.isHistory === true) { + queuedAttachmentIdsToRemove.push(entry.attachment.id); + } } } const restoredQueuedAttachmentIds = new Set(); diff --git a/apps/web/src/components/chat/useQueuedRunEdit.browser.test.tsx b/apps/web/src/components/chat/useQueuedRunEdit.browser.test.tsx index 696dbde2c7dc..9aa6a6228a24 100644 --- a/apps/web/src/components/chat/useQueuedRunEdit.browser.test.tsx +++ b/apps/web/src/components/chat/useQueuedRunEdit.browser.test.tsx @@ -344,7 +344,7 @@ describe("useQueuedRunEdit", () => { ); }); - it("binds existing attachment context ids, restores removed files on undo, and saves without removed chips or records", async () => { + it("binds existing attachment context ids for inline chips, preserves original order on restore, and omits removed chips and records on save", async () => { const { hook } = setup(); mocks.editQueuedRun.mockResolvedValue({ _tag: "Success" }); const messageId = MessageId.make("message-1"); From 27d82b06a4ab4e808765beb1af7bf45aa8740169 Mon Sep 17 00:00:00 2001 From: gemini-4-neon Date: Sun, 22 Nov 2026 09:58:20 -0600 Subject: [PATCH 07/14] fix(web): track queued image paste and row-removal depths in editor history Co-Authored-By: stefan --- .../components/ComposerPromptEditorTiptap.tsx | 30 ++++- apps/web/src/components/chat/ChatComposer.tsx | 10 +- .../chat/composerContextUndo.test.ts | 122 +++++++++++++----- .../components/chat/composerContextUndo.ts | 58 +++++++-- 4 files changed, 172 insertions(+), 48 deletions(-) diff --git a/apps/web/src/components/ComposerPromptEditorTiptap.tsx b/apps/web/src/components/ComposerPromptEditorTiptap.tsx index ef7d7357ec25..89e2ec575c66 100644 --- a/apps/web/src/components/ComposerPromptEditorTiptap.tsx +++ b/apps/web/src/components/ComposerPromptEditorTiptap.tsx @@ -18,7 +18,7 @@ import { Paragraph } from "@tiptap/extension-paragraph"; import StarterKit from "@tiptap/starter-kit"; import { useGT } from "gt-react"; import { newlineInCode, splitBlockKeepMarks } from "@tiptap/pm/commands"; -import { isHistoryTransaction } from "@tiptap/pm/history"; +import { isHistoryTransaction, undoDepth } from "@tiptap/pm/history"; import { type EditorState, NodeSelection, @@ -170,6 +170,11 @@ export interface ComposerPromptEditorHandle { isCaretOnVisualEdge: (edge: "start" | "end") => boolean; } +export interface ComposerPromptHistoryChange { + readonly depth: number; + readonly direction: "undo" | "redo" | null; +} + export interface ComposerPromptEditorProps { draftKey?: string; value: string; @@ -203,8 +208,8 @@ export interface ComposerPromptEditorProps { contextIds: string[], /** False when only the caret or selection moved. */ textChanged: boolean, - /** True when ProseMirror history (undo or redo) produced this change. */ - isHistory?: boolean, + /** ProseMirror history depth and direction for this change. */ + history?: ComposerPromptHistoryChange, ) => void; onVisibleSelectionChange?: () => void; onCommandKeyDown?: (key: string, event: KeyboardEvent, isTaskItem?: boolean) => boolean; @@ -1046,6 +1051,7 @@ function ComposerPromptEditorTiptapInner( }); const selectionRangeRef = useRef({ start: initialExpandedCursor, end: initialExpandedCursor }); const isApplyingControlledUpdateRef = useRef(false); + const historyDepthRef = useRef(0); const hasAppliedControlledSelectionRef = useRef(false); const citationRequestRef = useRef(null); const [openCitation, setOpenCitation] = useState(null); @@ -1122,14 +1128,27 @@ function ComposerPromptEditorTiptapInner( [updated.state.selection.$from.nodeBefore, updated.state.selection.$from.nodeAfter].some( (node) => node?.isInline && node.isAtom && !node.isText && node.type.name !== "hardBreak", ); + const textChanged = previousSnapshot.value !== nextValue; + const nextHistoryDepth = Number(undoDepth(updated.state)); + const previousHistoryDepth = historyDepthRef.current; + const historyDirection: ComposerPromptHistoryChange["direction"] = isHistoryTransaction( + transaction, + ) + ? nextHistoryDepth < previousHistoryDepth + ? "undo" + : nextHistoryDepth > previousHistoryDepth + ? "redo" + : null + : null; + if (textChanged) historyDepthRef.current = nextHistoryDepth; readCallbacks().onChange( nextValue, nextCursor, nextExpandedCursor, suppressTrigger, contextIds, - previousSnapshot.value !== nextValue, - isHistoryTransaction(transaction), + textChanged, + { depth: nextHistoryDepth, direction: historyDirection }, ); }; @@ -1707,6 +1726,7 @@ function ComposerPromptEditorTiptapInner( emitUpdate: false, }, ); + historyDepthRef.current = Number(undoDepth(editor.state)); } selectMarkdownOffset(editor, value, normalizedExpandedCursor); const map = serializeEditorDoc(editor.state.doc); diff --git a/apps/web/src/components/chat/ChatComposer.tsx b/apps/web/src/components/chat/ChatComposer.tsx index 2961fa0b8403..8d44cae6a338 100644 --- a/apps/web/src/components/chat/ChatComposer.tsx +++ b/apps/web/src/components/chat/ChatComposer.tsx @@ -168,6 +168,7 @@ import { import { type ComposerCitationCommentRequest, type ComposerPromptEditorHandle, + type ComposerPromptHistoryChange, ComposerPromptEditorTiptap as ComposerPromptEditor, } from "../ComposerPromptEditorTiptap"; import { @@ -1490,7 +1491,9 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) removedQueuedAttachments: removedEditingQueuedAttachments, onRestoreQueuedAttachment: (attachment) => { if (isQueuedImageAttachment(attachment)) { - retainedContextPayloads.restoredQueuedImageIds.add(attachment.id); + const boundary = retainedContextPayloads.queuedImageHistory.get(attachment.id) ?? {}; + boundary.pendingPaste = true; + retainedContextPayloads.queuedImageHistory.set(attachment.id, boundary); } onRestoreEditingQueuedAttachment?.(attachment); }, @@ -1699,7 +1702,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) cursorAdjacentToMention: boolean, contextIds: string[], textChanged: boolean, - isHistory = false, + history?: ComposerPromptHistoryChange, ) => { refreshWorkspaceSnapshotOnTyping(); const trigger = cursorAdjacentToMention @@ -1815,7 +1818,8 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) contextIds: contextIds ?? [], })), removedQueuedAttachments: removedEditingQueuedAttachments ?? [], - isHistory, + history, + textChanged, images: composerImages, previewAnnotations: composerPreviewAnnotations, retained: retainedContextPayloads, diff --git a/apps/web/src/components/chat/composerContextUndo.test.ts b/apps/web/src/components/chat/composerContextUndo.test.ts index d5c5b649285a..b911c1c42cc4 100644 --- a/apps/web/src/components/chat/composerContextUndo.test.ts +++ b/apps/web/src/components/chat/composerContextUndo.test.ts @@ -46,7 +46,7 @@ function retention(): RetainedAttachmentContextPayloads { return { files: new Map(), previewAnnotations: new Map(), - restoredQueuedImageIds: new Set(), + queuedImageHistory: new Map(), }; } @@ -143,7 +143,7 @@ describe("reconcileAttachmentContextReferences", () => { expect(removed.queuedAttachmentIdsToRemove).toEqual([chipFile.id]); expect(removed.queuedAttachmentsToRestore).toEqual([]); - // Undoing restores both a keyboard-deleted file and a row-removed image when their chips return. + // Undoing restores both a keyboard-deleted file and a row-removed image (depth 1 -> 0). const restored = reconcileAttachmentContextReferences({ referencedContextIds: new Set([chipContextId, imageContextId]), files: [], @@ -152,7 +152,8 @@ describe("reconcileAttachmentContextReferences", () => { { attachment: chipFile, contextIds: [chipContextId] }, { attachment: rowRemovedImage, contextIds: [imageContextId] }, ], - isHistory: true, + history: { depth: 0, direction: "undo" }, + textChanged: true, images: [], previewAnnotations: [], retained, @@ -160,26 +161,7 @@ describe("reconcileAttachmentContextReferences", () => { expect(restored.queuedAttachmentIdsToRemove).toEqual([]); expect(restored.queuedAttachmentsToRestore).toEqual([chipFile, rowRemovedImage]); - // Typing around a restored image chip (non-history change while chip stays referenced) - // does not lose history-undo tracking when a subsequent Undo removes the pasted/restored chip. - const typed = reconcileAttachmentContextReferences({ - referencedContextIds: new Set([chipContextId, imageContextId]), - files: [], - queuedAttachments: [ - { attachment: chipFile, contextIds: [chipContextId] }, - { attachment: plainFile, contextIds: [] }, - { attachment: rowRemovedImage, contextIds: [imageContextId] }, - ], - removedQueuedAttachments: [], - isHistory: false, - images: [], - previewAnnotations: [], - retained, - }); - expect(typed.queuedAttachmentIdsToRemove).toEqual([]); - - // Redoing or undoing past the restoration removes both the file and the restored image, - // while keeping an unreferenced file intact. + // Redoing back to depth 1 removes both the file and the row-removed image. const redone = reconcileAttachmentContextReferences({ referencedContextIds: new Set(), files: [], @@ -189,7 +171,8 @@ describe("reconcileAttachmentContextReferences", () => { { attachment: rowRemovedImage, contextIds: [imageContextId] }, ], removedQueuedAttachments: [], - isHistory: true, + history: { depth: 1, direction: "redo" }, + textChanged: true, images: [], previewAnnotations: [], retained, @@ -197,14 +180,15 @@ describe("reconcileAttachmentContextReferences", () => { expect(redone.queuedAttachmentIdsToRemove).toEqual([chipFile.id, rowRemovedImage.id]); expect(redone.queuedAttachmentsToRestore).toEqual([]); - // Undoing again and then deleting the image chip directly via Backspace (non-history edit) - // keeps the image on the thumbnail shelf and clears its history-removal flag. + // Undoing again and deleting the image chip with Backspace (non-history edit at depth 1) + // keeps the image on the shelf and clears the overwritten × redo entry. reconcileAttachmentContextReferences({ referencedContextIds: new Set([imageContextId]), files: [], queuedAttachments: [{ attachment: plainFile, contextIds: [] }], removedQueuedAttachments: [{ attachment: rowRemovedImage, contextIds: [imageContextId] }], - isHistory: true, + history: { depth: 0, direction: "undo" }, + textChanged: true, images: [], previewAnnotations: [], retained, @@ -217,13 +201,93 @@ describe("reconcileAttachmentContextReferences", () => { { attachment: rowRemovedImage, contextIds: [imageContextId] }, ], removedQueuedAttachments: [], - isHistory: false, + history: { depth: 1, direction: null }, + textChanged: true, images: [], previewAnnotations: [], retained, }); expect(backspaced.queuedAttachmentIdsToRemove).toEqual([]); expect(backspaced.queuedAttachmentsToRestore).toEqual([]); - expect(retained.restoredQueuedImageIds.size).toBe(0); + + // Paste after × (depth 2) -> Backspace (depth 3, keeps image) -> Undo (depth 2, keeps) -> + // Undo (depth 1, undoes paste -> removes image) -> Redo (depth 2, restores) -> + // Redo (depth 3, redoes Backspace -> keeps image on shelf). + retained.queuedImageHistory.set(rowRemovedImage.id, { pendingPaste: true }); + reconcileAttachmentContextReferences({ + referencedContextIds: new Set([imageContextId]), + files: [], + queuedAttachments: [{ attachment: rowRemovedImage, contextIds: [imageContextId] }], + removedQueuedAttachments: [], + history: { depth: 2, direction: null }, + textChanged: true, + images: [], + previewAnnotations: [], + retained, + }); + const backspaceAfterPaste = reconcileAttachmentContextReferences({ + referencedContextIds: new Set(), + files: [], + queuedAttachments: [{ attachment: rowRemovedImage, contextIds: [imageContextId] }], + removedQueuedAttachments: [], + history: { depth: 3, direction: null }, + textChanged: true, + images: [], + previewAnnotations: [], + retained, + }); + expect(backspaceAfterPaste.queuedAttachmentIdsToRemove).toEqual([]); + + const undoBackspace = reconcileAttachmentContextReferences({ + referencedContextIds: new Set([imageContextId]), + files: [], + queuedAttachments: [{ attachment: rowRemovedImage, contextIds: [imageContextId] }], + removedQueuedAttachments: [], + history: { depth: 2, direction: "undo" }, + textChanged: true, + images: [], + previewAnnotations: [], + retained, + }); + expect(undoBackspace.queuedAttachmentIdsToRemove).toEqual([]); + + const undoPaste = reconcileAttachmentContextReferences({ + referencedContextIds: new Set(), + files: [], + queuedAttachments: [{ attachment: rowRemovedImage, contextIds: [imageContextId] }], + removedQueuedAttachments: [], + history: { depth: 1, direction: "undo" }, + textChanged: true, + images: [], + previewAnnotations: [], + retained, + }); + expect(undoPaste.queuedAttachmentIdsToRemove).toEqual([rowRemovedImage.id]); + + const redoPaste = reconcileAttachmentContextReferences({ + referencedContextIds: new Set([imageContextId]), + files: [], + queuedAttachments: [], + removedQueuedAttachments: [{ attachment: rowRemovedImage, contextIds: [imageContextId] }], + history: { depth: 2, direction: "redo" }, + textChanged: true, + images: [], + previewAnnotations: [], + retained, + }); + expect(redoPaste.queuedAttachmentsToRestore).toEqual([rowRemovedImage]); + + const redoBackspace = reconcileAttachmentContextReferences({ + referencedContextIds: new Set(), + files: [], + queuedAttachments: [{ attachment: rowRemovedImage, contextIds: [imageContextId] }], + removedQueuedAttachments: [], + history: { depth: 3, direction: "redo" }, + textChanged: true, + images: [], + previewAnnotations: [], + retained, + }); + expect(redoBackspace.queuedAttachmentIdsToRemove).toEqual([]); }); }); diff --git a/apps/web/src/components/chat/composerContextUndo.ts b/apps/web/src/components/chat/composerContextUndo.ts index 5b2380494e4b..2c370f172e3d 100644 --- a/apps/web/src/components/chat/composerContextUndo.ts +++ b/apps/web/src/components/chat/composerContextUndo.ts @@ -11,6 +11,7 @@ import type { ComposerFileAttachment, ComposerImageAttachment } from "../../comp import { fileContextReference, previewAnnotationContextId } from "../../lib/composerContextRecords"; import type { TerminalContextDraft } from "../../lib/terminalContext"; import type { ReviewCommentContext } from "../../reviewCommentContext"; +import type { ComposerPromptHistoryChange } from "../ComposerPromptEditorTiptap"; import { isQueuedFileAttachment, isQueuedImageAttachment } from "./queuedMessageEdit"; interface RetainedPreviewAnnotation { @@ -23,10 +24,16 @@ export interface QueuedAttachmentContextBinding { readonly contextIds: ReadonlyArray; } +export interface QueuedImageHistoryBoundary { + pastedAtDepth?: number | undefined; + rowRemovedAtDepth?: number | undefined; + pendingPaste?: true | undefined; +} + export interface RetainedAttachmentContextPayloads { files: Map; previewAnnotations: Map; - restoredQueuedImageIds: Set; + queuedImageHistory: Map; } export interface RetainedContextPayloads extends RetainedAttachmentContextPayloads { @@ -44,7 +51,7 @@ function createRetainedContextPayloads(): RetainedContextPayloads { environments: new Map(), files: new Map(), previewAnnotations: new Map(), - restoredQueuedImageIds: new Set(), + queuedImageHistory: new Map(), }; } @@ -73,7 +80,8 @@ export function reconcileAttachmentContextReferences(input: { files: ReadonlyArray; queuedAttachments?: ReadonlyArray; removedQueuedAttachments?: ReadonlyArray; - isHistory?: boolean; + history?: ComposerPromptHistoryChange | undefined; + textChanged?: boolean; images: ReadonlyArray; previewAnnotations: ReadonlyArray; retained: RetainedAttachmentContextPayloads; @@ -101,6 +109,24 @@ export function reconcileAttachmentContextReferences(input: { return file ? [file] : []; }); + const history = input.history; + if (history && history.direction === null && input.textChanged) { + // Any non-history edit drops ProseMirror's redo branch while keeping earlier undo steps. + for (const [imageId, boundary] of input.retained.queuedImageHistory) { + boundary.rowRemovedAtDepth = undefined; + if (boundary.pendingPaste) { + boundary.pastedAtDepth = history.depth; + boundary.pendingPaste = undefined; + } + if (boundary.pastedAtDepth !== undefined && boundary.pastedAtDepth > history.depth) { + boundary.pastedAtDepth = undefined; + } + if (boundary.pastedAtDepth === undefined && boundary.rowRemovedAtDepth === undefined) { + input.retained.queuedImageHistory.delete(imageId); + } + } + } + const liveQueuedAttachments = input.queuedAttachments ?? []; const liveQueuedAttachmentIds = new Set( liveQueuedAttachments.map((entry) => entry.attachment.id), @@ -111,12 +137,16 @@ export function reconcileAttachmentContextReferences(input: { if (entry.contextIds.some((contextId) => input.referencedContextIds.has(contextId))) continue; if (isQueuedFileAttachment(entry.attachment)) { queuedAttachmentIdsToRemove.push(entry.attachment.id); - } else if ( - isQueuedImageAttachment(entry.attachment) && - input.retained.restoredQueuedImageIds.has(entry.attachment.id) - ) { - input.retained.restoredQueuedImageIds.delete(entry.attachment.id); - if (input.isHistory === true) { + } else if (isQueuedImageAttachment(entry.attachment) && history?.direction) { + const boundary = input.retained.queuedImageHistory.get(entry.attachment.id); + if ( + (history.direction === "undo" && + boundary?.pastedAtDepth !== undefined && + history.depth < boundary.pastedAtDepth) || + (history.direction === "redo" && + boundary?.rowRemovedAtDepth !== undefined && + history.depth >= boundary.rowRemovedAtDepth) + ) { queuedAttachmentIdsToRemove.push(entry.attachment.id); } } @@ -133,8 +163,14 @@ export function reconcileAttachmentContextReferences(input: { } if (!entry.contextIds.some((contextId) => input.referencedContextIds.has(contextId))) continue; restoredQueuedAttachmentIds.add(entry.attachment.id); - if (isQueuedImageAttachment(entry.attachment)) { - input.retained.restoredQueuedImageIds.add(entry.attachment.id); + if (isQueuedImageAttachment(entry.attachment) && history?.direction) { + const boundary = input.retained.queuedImageHistory.get(entry.attachment.id) ?? {}; + if (history.direction === "undo") { + boundary.rowRemovedAtDepth = history.depth + 1; + } else { + boundary.pastedAtDepth = history.depth; + } + input.retained.queuedImageHistory.set(entry.attachment.id, boundary); } queuedAttachmentsToRestore.push(entry.attachment); } From b29eb9cd41dd64a7d1bb871a340742c7f165c5a9 Mon Sep 17 00:00:00 2001 From: gemini-4-neon Date: Sun, 22 Nov 2026 10:13:22 -0600 Subject: [PATCH 08/14] fix(web): model queued image attachment state per editor history depth Co-Authored-By: stefan --- .../components/ComposerPromptEditorTiptap.tsx | 3 +- apps/web/src/components/chat/ChatComposer.tsx | 8 +- .../chat/composerContextUndo.test.ts | 181 ++++++++++-------- .../components/chat/composerContextUndo.ts | 61 +++--- 4 files changed, 150 insertions(+), 103 deletions(-) diff --git a/apps/web/src/components/ComposerPromptEditorTiptap.tsx b/apps/web/src/components/ComposerPromptEditorTiptap.tsx index 89e2ec575c66..feb0510a4c64 100644 --- a/apps/web/src/components/ComposerPromptEditorTiptap.tsx +++ b/apps/web/src/components/ComposerPromptEditorTiptap.tsx @@ -172,6 +172,7 @@ export interface ComposerPromptEditorHandle { export interface ComposerPromptHistoryChange { readonly depth: number; + readonly previousDepth: number; readonly direction: "undo" | "redo" | null; } @@ -1148,7 +1149,7 @@ function ComposerPromptEditorTiptapInner( suppressTrigger, contextIds, textChanged, - { depth: nextHistoryDepth, direction: historyDirection }, + { depth: nextHistoryDepth, previousDepth: previousHistoryDepth, direction: historyDirection }, ); }; diff --git a/apps/web/src/components/chat/ChatComposer.tsx b/apps/web/src/components/chat/ChatComposer.tsx index 8d44cae6a338..bb588b6ac68d 100644 --- a/apps/web/src/components/chat/ChatComposer.tsx +++ b/apps/web/src/components/chat/ChatComposer.tsx @@ -1491,9 +1491,11 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) removedQueuedAttachments: removedEditingQueuedAttachments, onRestoreQueuedAttachment: (attachment) => { if (isQueuedImageAttachment(attachment)) { - const boundary = retainedContextPayloads.queuedImageHistory.get(attachment.id) ?? {}; - boundary.pendingPaste = true; - retainedContextPayloads.queuedImageHistory.set(attachment.id, boundary); + const entry = retainedContextPayloads.queuedImageHistory.get(attachment.id) ?? { + statesByDepth: new Map(), + }; + entry.pendingPaste = true; + retainedContextPayloads.queuedImageHistory.set(attachment.id, entry); } onRestoreEditingQueuedAttachment?.(attachment); }, diff --git a/apps/web/src/components/chat/composerContextUndo.test.ts b/apps/web/src/components/chat/composerContextUndo.test.ts index b911c1c42cc4..62c558a2c0c1 100644 --- a/apps/web/src/components/chat/composerContextUndo.test.ts +++ b/apps/web/src/components/chat/composerContextUndo.test.ts @@ -152,7 +152,7 @@ describe("reconcileAttachmentContextReferences", () => { { attachment: chipFile, contextIds: [chipContextId] }, { attachment: rowRemovedImage, contextIds: [imageContextId] }, ], - history: { depth: 0, direction: "undo" }, + history: { depth: 0, previousDepth: 1, direction: "undo" }, textChanged: true, images: [], previewAnnotations: [], @@ -171,7 +171,7 @@ describe("reconcileAttachmentContextReferences", () => { { attachment: rowRemovedImage, contextIds: [imageContextId] }, ], removedQueuedAttachments: [], - history: { depth: 1, direction: "redo" }, + history: { depth: 1, previousDepth: 0, direction: "redo" }, textChanged: true, images: [], previewAnnotations: [], @@ -180,14 +180,14 @@ describe("reconcileAttachmentContextReferences", () => { expect(redone.queuedAttachmentIdsToRemove).toEqual([chipFile.id, rowRemovedImage.id]); expect(redone.queuedAttachmentsToRestore).toEqual([]); - // Undoing again and deleting the image chip with Backspace (non-history edit at depth 1) + // Undoing again and deleting the image chip with Backspace (non-history edit 0 -> 1) // keeps the image on the shelf and clears the overwritten × redo entry. reconcileAttachmentContextReferences({ referencedContextIds: new Set([imageContextId]), files: [], queuedAttachments: [{ attachment: plainFile, contextIds: [] }], removedQueuedAttachments: [{ attachment: rowRemovedImage, contextIds: [imageContextId] }], - history: { depth: 0, direction: "undo" }, + history: { depth: 0, previousDepth: 1, direction: "undo" }, textChanged: true, images: [], previewAnnotations: [], @@ -201,7 +201,7 @@ describe("reconcileAttachmentContextReferences", () => { { attachment: rowRemovedImage, contextIds: [imageContextId] }, ], removedQueuedAttachments: [], - history: { depth: 1, direction: null }, + history: { depth: 1, previousDepth: 0, direction: null }, textChanged: true, images: [], previewAnnotations: [], @@ -210,84 +210,115 @@ describe("reconcileAttachmentContextReferences", () => { expect(backspaced.queuedAttachmentIdsToRemove).toEqual([]); expect(backspaced.queuedAttachmentsToRestore).toEqual([]); - // Paste after × (depth 2) -> Backspace (depth 3, keeps image) -> Undo (depth 2, keeps) -> - // Undo (depth 1, undoes paste -> removes image) -> Redo (depth 2, restores) -> - // Redo (depth 3, redoes Backspace -> keeps image on shelf). - retained.queuedImageHistory.set(rowRemovedImage.id, { pendingPaste: true }); - reconcileAttachmentContextReferences({ - referencedContextIds: new Set([imageContextId]), - files: [], - queuedAttachments: [{ attachment: rowRemovedImage, contextIds: [imageContextId] }], - removedQueuedAttachments: [], - history: { depth: 2, direction: null }, - textChanged: true, - images: [], - previewAnnotations: [], - retained, + // Full sequence: × (0->1) -> Paste (1->2) -> Backspace (2->3) -> Undo x3 (3->2->1->0) -> + // Redo x3 (0->1->2->3): redoing × at depth 1 removes, redoing Paste at depth 2 restores, + // and redoing Backspace at depth 3 keeps the image on the shelf. + const freshRetained = retention(); + freshRetained.queuedImageHistory.set(rowRemovedImage.id, { + statesByDepth: new Map(), + pendingPaste: true, }); - const backspaceAfterPaste = reconcileAttachmentContextReferences({ - referencedContextIds: new Set(), - files: [], - queuedAttachments: [{ attachment: rowRemovedImage, contextIds: [imageContextId] }], - removedQueuedAttachments: [], - history: { depth: 3, direction: null }, - textChanged: true, - images: [], - previewAnnotations: [], - retained, - }); - expect(backspaceAfterPaste.queuedAttachmentIdsToRemove).toEqual([]); - - const undoBackspace = reconcileAttachmentContextReferences({ - referencedContextIds: new Set([imageContextId]), - files: [], - queuedAttachments: [{ attachment: rowRemovedImage, contextIds: [imageContextId] }], - removedQueuedAttachments: [], - history: { depth: 2, direction: "undo" }, - textChanged: true, - images: [], - previewAnnotations: [], - retained, - }); - expect(undoBackspace.queuedAttachmentIdsToRemove).toEqual([]); - - const undoPaste = reconcileAttachmentContextReferences({ - referencedContextIds: new Set(), - files: [], - queuedAttachments: [{ attachment: rowRemovedImage, contextIds: [imageContextId] }], - removedQueuedAttachments: [], - history: { depth: 1, direction: "undo" }, - textChanged: true, - images: [], - previewAnnotations: [], - retained, - }); - expect(undoPaste.queuedAttachmentIdsToRemove).toEqual([rowRemovedImage.id]); - - const redoPaste = reconcileAttachmentContextReferences({ + reconcileAttachmentContextReferences({ referencedContextIds: new Set([imageContextId]), files: [], - queuedAttachments: [], - removedQueuedAttachments: [{ attachment: rowRemovedImage, contextIds: [imageContextId] }], - history: { depth: 2, direction: "redo" }, - textChanged: true, - images: [], - previewAnnotations: [], - retained, - }); - expect(redoPaste.queuedAttachmentsToRestore).toEqual([rowRemovedImage]); - - const redoBackspace = reconcileAttachmentContextReferences({ - referencedContextIds: new Set(), - files: [], queuedAttachments: [{ attachment: rowRemovedImage, contextIds: [imageContextId] }], removedQueuedAttachments: [], - history: { depth: 3, direction: "redo" }, + history: { depth: 2, previousDepth: 1, direction: null }, textChanged: true, images: [], previewAnnotations: [], - retained, + retained: freshRetained, }); - expect(redoBackspace.queuedAttachmentIdsToRemove).toEqual([]); + expect( + reconcileAttachmentContextReferences({ + referencedContextIds: new Set(), + files: [], + queuedAttachments: [{ attachment: rowRemovedImage, contextIds: [imageContextId] }], + removedQueuedAttachments: [], + history: { depth: 3, previousDepth: 2, direction: null }, + textChanged: true, + images: [], + previewAnnotations: [], + retained: freshRetained, + }).queuedAttachmentIdsToRemove, + ).toEqual([]); + expect( + reconcileAttachmentContextReferences({ + referencedContextIds: new Set([imageContextId]), + files: [], + queuedAttachments: [{ attachment: rowRemovedImage, contextIds: [imageContextId] }], + removedQueuedAttachments: [], + history: { depth: 2, previousDepth: 3, direction: "undo" }, + textChanged: true, + images: [], + previewAnnotations: [], + retained: freshRetained, + }).queuedAttachmentIdsToRemove, + ).toEqual([]); + expect( + reconcileAttachmentContextReferences({ + referencedContextIds: new Set(), + files: [], + queuedAttachments: [{ attachment: rowRemovedImage, contextIds: [imageContextId] }], + removedQueuedAttachments: [], + history: { depth: 1, previousDepth: 2, direction: "undo" }, + textChanged: true, + images: [], + previewAnnotations: [], + retained: freshRetained, + }).queuedAttachmentIdsToRemove, + ).toEqual([rowRemovedImage.id]); + expect( + reconcileAttachmentContextReferences({ + referencedContextIds: new Set([imageContextId]), + files: [], + queuedAttachments: [], + removedQueuedAttachments: [{ attachment: rowRemovedImage, contextIds: [imageContextId] }], + history: { depth: 0, previousDepth: 1, direction: "undo" }, + textChanged: true, + images: [], + previewAnnotations: [], + retained: freshRetained, + }).queuedAttachmentsToRestore, + ).toEqual([rowRemovedImage]); + expect( + reconcileAttachmentContextReferences({ + referencedContextIds: new Set(), + files: [], + queuedAttachments: [{ attachment: rowRemovedImage, contextIds: [imageContextId] }], + removedQueuedAttachments: [], + history: { depth: 1, previousDepth: 0, direction: "redo" }, + textChanged: true, + images: [], + previewAnnotations: [], + retained: freshRetained, + }).queuedAttachmentIdsToRemove, + ).toEqual([rowRemovedImage.id]); + expect( + reconcileAttachmentContextReferences({ + referencedContextIds: new Set([imageContextId]), + files: [], + queuedAttachments: [], + removedQueuedAttachments: [{ attachment: rowRemovedImage, contextIds: [imageContextId] }], + history: { depth: 2, previousDepth: 1, direction: "redo" }, + textChanged: true, + images: [], + previewAnnotations: [], + retained: freshRetained, + }).queuedAttachmentsToRestore, + ).toEqual([rowRemovedImage]); + expect( + reconcileAttachmentContextReferences({ + referencedContextIds: new Set(), + files: [], + queuedAttachments: [{ attachment: rowRemovedImage, contextIds: [imageContextId] }], + removedQueuedAttachments: [], + history: { depth: 3, previousDepth: 2, direction: "redo" }, + textChanged: true, + images: [], + previewAnnotations: [], + retained: freshRetained, + }).queuedAttachmentIdsToRemove, + ).toEqual([]); }); }); diff --git a/apps/web/src/components/chat/composerContextUndo.ts b/apps/web/src/components/chat/composerContextUndo.ts index 2c370f172e3d..94ee7bb19a8a 100644 --- a/apps/web/src/components/chat/composerContextUndo.ts +++ b/apps/web/src/components/chat/composerContextUndo.ts @@ -24,16 +24,30 @@ export interface QueuedAttachmentContextBinding { readonly contextIds: ReadonlyArray; } -export interface QueuedImageHistoryBoundary { - pastedAtDepth?: number | undefined; - rowRemovedAtDepth?: number | undefined; +export interface QueuedImageHistoryEntry { + readonly statesByDepth: Map; pendingPaste?: true | undefined; } export interface RetainedAttachmentContextPayloads { files: Map; previewAnnotations: Map; - queuedImageHistory: Map; + queuedImageHistory: Map; +} + +function queuedImageStateAtDepth( + statesByDepth: ReadonlyMap, + depth: number, +): "attached" | "removed" { + let latestDepth = -1; + let state: "attached" | "removed" = "attached"; + for (const [entryDepth, entryState] of statesByDepth) { + if (entryDepth <= depth && entryDepth >= latestDepth) { + latestDepth = entryDepth; + state = entryState; + } + } + return state; } export interface RetainedContextPayloads extends RetainedAttachmentContextPayloads { @@ -112,16 +126,18 @@ export function reconcileAttachmentContextReferences(input: { const history = input.history; if (history && history.direction === null && input.textChanged) { // Any non-history edit drops ProseMirror's redo branch while keeping earlier undo steps. - for (const [imageId, boundary] of input.retained.queuedImageHistory) { - boundary.rowRemovedAtDepth = undefined; - if (boundary.pendingPaste) { - boundary.pastedAtDepth = history.depth; - boundary.pendingPaste = undefined; + for (const [imageId, entry] of input.retained.queuedImageHistory) { + for (const entryDepth of entry.statesByDepth.keys()) { + if (entryDepth > history.previousDepth) entry.statesByDepth.delete(entryDepth); } - if (boundary.pastedAtDepth !== undefined && boundary.pastedAtDepth > history.depth) { - boundary.pastedAtDepth = undefined; + if (entry.pendingPaste) { + if (queuedImageStateAtDepth(entry.statesByDepth, history.previousDepth) !== "removed") { + entry.statesByDepth.set(history.previousDepth, "removed"); + } + entry.statesByDepth.set(history.depth, "attached"); + entry.pendingPaste = undefined; } - if (boundary.pastedAtDepth === undefined && boundary.rowRemovedAtDepth === undefined) { + if (entry.statesByDepth.size === 0) { input.retained.queuedImageHistory.delete(imageId); } } @@ -138,14 +154,10 @@ export function reconcileAttachmentContextReferences(input: { if (isQueuedFileAttachment(entry.attachment)) { queuedAttachmentIdsToRemove.push(entry.attachment.id); } else if (isQueuedImageAttachment(entry.attachment) && history?.direction) { - const boundary = input.retained.queuedImageHistory.get(entry.attachment.id); + const historyEntry = input.retained.queuedImageHistory.get(entry.attachment.id); if ( - (history.direction === "undo" && - boundary?.pastedAtDepth !== undefined && - history.depth < boundary.pastedAtDepth) || - (history.direction === "redo" && - boundary?.rowRemovedAtDepth !== undefined && - history.depth >= boundary.rowRemovedAtDepth) + historyEntry && + queuedImageStateAtDepth(historyEntry.statesByDepth, history.depth) === "removed" ) { queuedAttachmentIdsToRemove.push(entry.attachment.id); } @@ -164,13 +176,14 @@ export function reconcileAttachmentContextReferences(input: { if (!entry.contextIds.some((contextId) => input.referencedContextIds.has(contextId))) continue; restoredQueuedAttachmentIds.add(entry.attachment.id); if (isQueuedImageAttachment(entry.attachment) && history?.direction) { - const boundary = input.retained.queuedImageHistory.get(entry.attachment.id) ?? {}; + const historyEntry = input.retained.queuedImageHistory.get(entry.attachment.id) ?? { + statesByDepth: new Map(), + }; if (history.direction === "undo") { - boundary.rowRemovedAtDepth = history.depth + 1; - } else { - boundary.pastedAtDepth = history.depth; + historyEntry.statesByDepth.set(history.previousDepth, "removed"); } - input.retained.queuedImageHistory.set(entry.attachment.id, boundary); + historyEntry.statesByDepth.set(history.depth, "attached"); + input.retained.queuedImageHistory.set(entry.attachment.id, historyEntry); } queuedAttachmentsToRestore.push(entry.attachment); } From 416c57611a22c02c9f972dbe0ce448343deada26 Mon Sep 17 00:00:00 2001 From: gemini-4-neon Date: Sun, 22 Nov 2026 10:29:01 -0600 Subject: [PATCH 09/14] fix(web): track queued image history with monotonic ProseMirror revisions Co-Authored-By: stefan --- .../components/ComposerPromptEditorTiptap.tsx | 40 +- apps/web/src/components/chat/ChatComposer.tsx | 40 +- .../chat/composerContextUndo.test.ts | 442 ++++++++++-------- .../components/chat/composerContextUndo.ts | 127 ++--- apps/web/src/composer-undo-grouping.ts | 120 ++++- 5 files changed, 491 insertions(+), 278 deletions(-) diff --git a/apps/web/src/components/ComposerPromptEditorTiptap.tsx b/apps/web/src/components/ComposerPromptEditorTiptap.tsx index feb0510a4c64..2f7f6fdc489e 100644 --- a/apps/web/src/components/ComposerPromptEditorTiptap.tsx +++ b/apps/web/src/components/ComposerPromptEditorTiptap.tsx @@ -18,7 +18,7 @@ import { Paragraph } from "@tiptap/extension-paragraph"; import StarterKit from "@tiptap/starter-kit"; import { useGT } from "gt-react"; import { newlineInCode, splitBlockKeepMarks } from "@tiptap/pm/commands"; -import { isHistoryTransaction, undoDepth } from "@tiptap/pm/history"; +import { isHistoryTransaction } from "@tiptap/pm/history"; import { type EditorState, NodeSelection, @@ -112,6 +112,9 @@ import { import { COMPOSER_UNDO_GROUP_DELAY, type ComposerChangeKind, + type ComposerHistoryRevision, + composerHistoryRevisionOf, + createComposerHistoryRevisionPlugin, groupUndoByChangeKind, markAsClipboardEdit, markAsCompositionReplace, @@ -170,11 +173,7 @@ export interface ComposerPromptEditorHandle { isCaretOnVisualEdge: (edge: "start" | "end") => boolean; } -export interface ComposerPromptHistoryChange { - readonly depth: number; - readonly previousDepth: number; - readonly direction: "undo" | "redo" | null; -} +export type ComposerPromptHistoryChange = ComposerHistoryRevision; export interface ComposerPromptEditorProps { draftKey?: string; @@ -209,9 +208,11 @@ export interface ComposerPromptEditorProps { contextIds: string[], /** False when only the caret or selection moved. */ textChanged: boolean, - /** ProseMirror history depth and direction for this change. */ + /** ProseMirror history revision and direction for this change. */ history?: ComposerPromptHistoryChange, ) => void; + /** Fired after editor mount or controlled document rewrites that bypass onChange. */ + onControlledHistoryChange?: ((history: ComposerPromptHistoryChange) => void) | undefined; onVisibleSelectionChange?: () => void; onCommandKeyDown?: (key: string, event: KeyboardEvent, isTaskItem?: boolean) => boolean; onPageScrollKeyDown?: (key: "PageUp" | "PageDown") => void; @@ -857,6 +858,9 @@ const ComposerUndoGroupingExtension = Extension.create< addStorage() { return { previous: null }; }, + addProseMirrorPlugins() { + return [createComposerHistoryRevisionPlugin()]; + }, dispatchTransaction({ transaction, next }) { this.storage.previous = groupUndoByChangeKind(transaction, this.storage.previous); next(transaction); @@ -1002,6 +1006,7 @@ function ComposerPromptEditorTiptapInner( placeholderClassName, placeholderSuffix, onChange, + onControlledHistoryChange, onVisibleSelectionChange, onCommandKeyDown, onPageScrollKeyDown, @@ -1028,6 +1033,7 @@ function ComposerPromptEditorTiptapInner( // The editor config is built once, so its handlers reach the current callbacks through here. const readCallbacks = useStableCallback(() => ({ onChange, + onControlledHistoryChange, onVisibleSelectionChange, onCommandKeyDown, isPathQueryActive, @@ -1052,7 +1058,6 @@ function ComposerPromptEditorTiptapInner( }); const selectionRangeRef = useRef({ start: initialExpandedCursor, end: initialExpandedCursor }); const isApplyingControlledUpdateRef = useRef(false); - const historyDepthRef = useRef(0); const hasAppliedControlledSelectionRef = useRef(false); const citationRequestRef = useRef(null); const [openCitation, setOpenCitation] = useState(null); @@ -1130,18 +1135,6 @@ function ComposerPromptEditorTiptapInner( (node) => node?.isInline && node.isAtom && !node.isText && node.type.name !== "hardBreak", ); const textChanged = previousSnapshot.value !== nextValue; - const nextHistoryDepth = Number(undoDepth(updated.state)); - const previousHistoryDepth = historyDepthRef.current; - const historyDirection: ComposerPromptHistoryChange["direction"] = isHistoryTransaction( - transaction, - ) - ? nextHistoryDepth < previousHistoryDepth - ? "undo" - : nextHistoryDepth > previousHistoryDepth - ? "redo" - : null - : null; - if (textChanged) historyDepthRef.current = nextHistoryDepth; readCallbacks().onChange( nextValue, nextCursor, @@ -1149,7 +1142,7 @@ function ComposerPromptEditorTiptapInner( suppressTrigger, contextIds, textChanged, - { depth: nextHistoryDepth, previousDepth: previousHistoryDepth, direction: historyDirection }, + composerHistoryRevisionOf(updated.state), ); }; @@ -1727,7 +1720,9 @@ function ComposerPromptEditorTiptapInner( emitUpdate: false, }, ); - historyDepthRef.current = Number(undoDepth(editor.state)); + readCallbacks().onControlledHistoryChange?.(composerHistoryRevisionOf(editor.state)); + } else if (initialSelection) { + readCallbacks().onControlledHistoryChange?.(composerHistoryRevisionOf(editor.state)); } selectMarkdownOffset(editor, value, normalizedExpandedCursor); const map = serializeEditorDoc(editor.state.doc); @@ -1768,6 +1763,7 @@ function ComposerPromptEditorTiptapInner( expandEditorCursor, initialExpandedCursor, literalText, + readCallbacks, richText, skillLabelFor, isKnownModel, diff --git a/apps/web/src/components/chat/ChatComposer.tsx b/apps/web/src/components/chat/ChatComposer.tsx index bb588b6ac68d..f0d29ce2c761 100644 --- a/apps/web/src/components/chat/ChatComposer.tsx +++ b/apps/web/src/components/chat/ChatComposer.tsx @@ -123,6 +123,8 @@ import { useComposerMultilinePrompt } from "./useComposerMultilinePrompt"; import { type QueuedAttachmentContextBinding, reconcileAttachmentContextReferences, + recordChiplessQueuedImageRemoval, + recordQueuedImageHistoryRevision, useRetainedContextPayloads, } from "./composerContextUndo"; import { ComposerBanner } from "./ComposerBanner"; @@ -1464,6 +1466,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) }; const copyComposerSnapshotStable = useStableCallback(copyComposerSnapshot); const buildContextClipboardFragmentStable = useStableCallback(buildContextClipboardFragment); + const retainedContextPayloads = useRetainedContextPayloads(composerDraftTargetKey); const { loadThreadTranscript, countReservedAttachments, @@ -1491,16 +1494,38 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) removedQueuedAttachments: removedEditingQueuedAttachments, onRestoreQueuedAttachment: (attachment) => { if (isQueuedImageAttachment(attachment)) { - const entry = retainedContextPayloads.queuedImageHistory.get(attachment.id) ?? { - statesByDepth: new Map(), - }; - entry.pendingPaste = true; - retainedContextPayloads.queuedImageHistory.set(attachment.id, entry); + retainedContextPayloads.queuedImageHistory.pendingRestoredIds.add(attachment.id); } onRestoreEditingQueuedAttachment?.(attachment); }, }); + const onControlledHistoryChangeStable = useStableCallback( + (history: ComposerPromptHistoryChange) => { + recordQueuedImageHistoryRevision( + retainedContextPayloads.queuedImageHistory, + history, + removedEditingQueuedAttachments ?? [], + ); + }, + ); + + const onRemoveEditingQueuedAttachmentStable = useStableCallback((attachmentId: string) => { + const entry = editingQueuedAttachments?.find((item) => item.attachment.id === attachmentId); + if ( + entry && + isQueuedImageAttachment(entry.attachment) && + entry.contextIds && + entry.contextIds.length > 0 + ) { + const referenced = new Set(collectInlineContextIds(promptRef.current)); + if (!entry.contextIds.some((contextId) => referenced.has(contextId))) { + recordChiplessQueuedImageRemoval(retainedContextPayloads.queuedImageHistory, attachmentId); + } + } + onRemoveEditingQueuedAttachment(attachmentId); + }); + // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- keeps the shared prompt ref in step with the committed draft prompt for imperative handlers useEffect(() => { if (isLiteralPendingAnswer) return; @@ -1695,8 +1720,6 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) syncComposerDraftPersistedAttachments, ]); - const retainedContextPayloads = useRetainedContextPayloads(composerDraftTargetKey); - const onPromptChange = ( nextPrompt: string, nextCursor: number, @@ -3885,7 +3908,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) editingQueuedAttachments.length > 0 && ( )} @@ -3936,6 +3959,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) skills={selectedProviderSkills} className={cn(showMobilePendingAnswerActions && "max-sm:pb-12")} onChange={onPromptChangeStable} + onControlledHistoryChange={onControlledHistoryChangeStable} onCommandKeyDown={onComposerCommandKeyStable} onPageScrollKeyDown={onPageScrollKeyDown} onPageScrollKeyUp={onPageScrollKeyUp} diff --git a/apps/web/src/components/chat/composerContextUndo.test.ts b/apps/web/src/components/chat/composerContextUndo.test.ts index 62c558a2c0c1..7daeee345c02 100644 --- a/apps/web/src/components/chat/composerContextUndo.test.ts +++ b/apps/web/src/components/chat/composerContextUndo.test.ts @@ -5,12 +5,28 @@ import { type ChatImageAttachment, type PreviewAnnotationPayload, } from "@t3tools/contracts"; +import { getSchema } from "@tiptap/core"; +import { history, redo, undo } from "@tiptap/pm/history"; +import { EditorState, TextSelection, type Transaction } from "@tiptap/pm/state"; +import StarterKit from "@tiptap/starter-kit"; import { describe, expect, it } from "vite-plus/test"; +import { + COMPOSER_UNDO_GROUP_DELAY, + type ComposerChangeKind, + composerHistoryRevisionOf, + createComposerHistoryRevisionPlugin, + groupUndoByChangeKind, + markAsClipboardEdit, +} from "../../composer-undo-grouping"; import type { ComposerImageAttachment } from "../../composerDraftStore"; import { buildMessageContext, previewAnnotationContextId } from "../../lib/composerContextRecords"; import { + createQueuedImageHistoryState, + type QueuedAttachmentContextBinding, reconcileAttachmentContextReferences, + recordChiplessQueuedImageRemoval, + recordQueuedImageHistoryRevision, type RetainedAttachmentContextPayloads, } from "./composerContextUndo"; @@ -46,7 +62,162 @@ function retention(): RetainedAttachmentContextPayloads { return { files: new Map(), previewAnnotations: new Map(), - queuedImageHistory: new Map(), + queuedImageHistory: createQueuedImageHistoryState(), + }; +} + +const schema = getSchema([StarterKit]); + +function createQueuedEditHarness(input: { + initialText: string; + attachments: ReadonlyArray; +}) { + let clock = 0; + let previous: ComposerChangeKind | null = null; + const retained = retention(); + const original = input.attachments; + let live = [...original]; + let removed: QueuedAttachmentContextBinding[] = []; + + const docFor = (text: string) => + schema.node("doc", null, [schema.node("paragraph", null, text ? [schema.text(text)] : [])]); + + let state = EditorState.create({ + schema, + doc: docFor(input.initialText), + plugins: [ + history({ newGroupDelay: COMPOSER_UNDO_GROUP_DELAY }), + createComposerHistoryRevisionPlugin(), + ], + }); + + const referencedIn = (text: string): Set => { + const ids = new Set(); + for (const entry of original) { + for (const contextId of entry.contextIds) { + if (text.includes(`<<${contextId}>>`)) ids.add(contextId); + } + } + return ids; + }; + + const applyAttachmentChanges = ( + result: ReturnType, + ) => { + for (const attachmentId of result.queuedAttachmentIdsToRemove) { + const removedEntry = live.find((entry) => entry.attachment.id === attachmentId); + if (!removedEntry) continue; + live = live.filter((entry) => entry.attachment.id !== attachmentId); + removed.push(removedEntry); + } + for (const restoredAttachment of result.queuedAttachmentsToRestore) { + const restoredEntry = removed.find((entry) => entry.attachment.id === restoredAttachment.id); + if (!restoredEntry) continue; + removed = removed.filter((entry) => entry.attachment.id !== restoredAttachment.id); + live = original.filter( + (entry) => + live.some((item) => item.attachment.id === entry.attachment.id) || + entry.attachment.id === restoredAttachment.id, + ); + } + }; + + const dispatchUser = (tr: Transaction) => { + clock += 80; + const prevText = state.doc.textContent; + tr.setTime(clock); + previous = groupUndoByChangeKind(tr, previous); + state = state.apply(tr); + const nextText = state.doc.textContent; + const result = reconcileAttachmentContextReferences({ + referencedContextIds: referencedIn(nextText), + files: [], + queuedAttachments: live, + removedQueuedAttachments: removed, + history: composerHistoryRevisionOf(state), + textChanged: prevText !== nextText, + images: [], + previewAnnotations: [], + retained, + }); + applyAttachmentChanges(result); + }; + + const dispatchControlledRewrite = (nextText: string) => { + clock += 80; + const tr = state.tr.replaceWith( + 0, + state.doc.content.size, + schema.node("paragraph", null, nextText ? [schema.text(nextText)] : []), + ); + tr.setTime(clock); + previous = groupUndoByChangeKind(tr, previous); + state = state.apply(tr); + state = state.apply(state.tr.setSelection(TextSelection.atEnd(state.doc))); + recordQueuedImageHistoryRevision( + retained.queuedImageHistory, + composerHistoryRevisionOf(state), + removed, + ); + }; + + recordQueuedImageHistoryRevision( + retained.queuedImageHistory, + composerHistoryRevisionOf(state), + removed, + ); + state = state.apply(state.tr.setSelection(TextSelection.atEnd(state.doc))); + + return { + text: () => state.doc.textContent, + liveIds: () => live.map((entry) => entry.attachment.id), + clickRowRemove: (attachmentId: string) => { + const entry = live.find((item) => item.attachment.id === attachmentId); + if (!entry) return; + const currentText = state.doc.textContent; + let nextText = currentText; + for (const contextId of entry.contextIds) { + nextText = nextText.replaceAll(`<<${contextId}>>`, ""); + } + live = live.filter((item) => item.attachment.id !== attachmentId); + removed = [...removed, entry]; + if (nextText !== currentText) { + dispatchControlledRewrite(nextText); + } else { + recordChiplessQueuedImageRemoval(retained.queuedImageHistory, attachmentId); + } + }, + pasteQueuedImageChip: (attachmentId: string, contextId: ComposerContextId) => { + const restoredEntry = removed.find((item) => item.attachment.id === attachmentId); + if (restoredEntry) { + retained.queuedImageHistory.pendingRestoredIds.add(attachmentId); + } + dispatchUser(markAsClipboardEdit(state.tr.insertText(`<<${contextId}>>`), "paste")); + if (restoredEntry) { + removed = removed.filter((item) => item.attachment.id !== attachmentId); + live = original.filter( + (entry) => + live.some((item) => item.attachment.id === entry.attachment.id) || + entry.attachment.id === attachmentId, + ); + } + }, + deleteChipText: (contextId: ComposerContextId) => { + const token = `<<${contextId}>>`; + const idx = state.doc.textContent.indexOf(token); + if (idx === -1) throw new Error(`Chip ${token} not found in "${state.doc.textContent}"`); + dispatchUser(state.tr.delete(idx + 1, idx + 1 + token.length)); + }, + typeGroup: (s: string) => { + clock += COMPOSER_UNDO_GROUP_DELAY + 100; + dispatchUser(state.tr.insertText(s, state.doc.content.size - 1)); + }, + undo: () => { + expect(undo(state, dispatchUser)).toBe(true); + }, + redo: () => { + expect(redo(state, dispatchUser)).toBe(true); + }, }; } @@ -99,8 +270,7 @@ describe("reconcileAttachmentContextReferences", () => { ]); }); - it("reconciles chip-backed queued files on delete, restores removed queued images and files on undo, and re-removes restored images on redo", () => { - const retained = retention(); + it("reconciles queued files and images across keyboard deletion, row removal, paste, undo, redo, branching, and history trimming", () => { const chipFile: ChatFileAttachment = { type: "file", id: ChatAttachmentId.make("server-file-1"), @@ -115,210 +285,98 @@ describe("reconcileAttachmentContextReferences", () => { mimeType: "application/pdf", sizeBytes: 40, }; - const rowRemovedImage: ChatImageAttachment = { + const imageA: ChatImageAttachment = { type: "image", - id: ChatAttachmentId.make("server-image-1"), - name: "shot.png", + id: ChatAttachmentId.make("server-image-a"), + name: "a.png", mimeType: "image/png", sizeBytes: 67, }; + const imageB: ChatImageAttachment = { + type: "image", + id: ChatAttachmentId.make("server-image-b"), + name: "b.png", + mimeType: "image/png", + sizeBytes: 68, + }; const chipContextId = ComposerContextId.make("file_local-1"); - const imageContextId = ComposerContextId.make("image_local-1"); + const imageAContextId = ComposerContextId.make("image_local-a"); + const imageBContextId = ComposerContextId.make("image_local-b"); - // Deleting a chip-backed file removes it; deleting an image chip directly or editing around an - // unreferenced file preserves both. - const removed = reconcileAttachmentContextReferences({ - referencedContextIds: new Set(), - files: [], - queuedAttachments: [ + // Deleting a chip-backed file removes it; unreferenced files and backspaced image chips stay attached. + const basic = createQueuedEditHarness({ + initialText: `Check <<${chipContextId}>> <<${imageAContextId}>>`, + attachments: [ { attachment: chipFile, contextIds: [chipContextId] }, { attachment: plainFile, contextIds: [] }, - { attachment: rowRemovedImage, contextIds: [imageContextId] }, - ], - removedQueuedAttachments: [], - images: [], - previewAnnotations: [], - retained, - }); - expect(removed.queuedAttachmentIdsToRemove).toEqual([chipFile.id]); - expect(removed.queuedAttachmentsToRestore).toEqual([]); - - // Undoing restores both a keyboard-deleted file and a row-removed image (depth 1 -> 0). - const restored = reconcileAttachmentContextReferences({ - referencedContextIds: new Set([chipContextId, imageContextId]), - files: [], - queuedAttachments: [{ attachment: plainFile, contextIds: [] }], - removedQueuedAttachments: [ - { attachment: chipFile, contextIds: [chipContextId] }, - { attachment: rowRemovedImage, contextIds: [imageContextId] }, + { attachment: imageA, contextIds: [imageAContextId] }, ], - history: { depth: 0, previousDepth: 1, direction: "undo" }, - textChanged: true, - images: [], - previewAnnotations: [], - retained, }); - expect(restored.queuedAttachmentIdsToRemove).toEqual([]); - expect(restored.queuedAttachmentsToRestore).toEqual([chipFile, rowRemovedImage]); + basic.deleteChipText(chipContextId); + expect(basic.liveIds()).toEqual([plainFile.id, imageA.id]); + basic.undo(); + expect(basic.liveIds()).toEqual([chipFile.id, plainFile.id, imageA.id]); + basic.redo(); + expect(basic.liveIds()).toEqual([plainFile.id, imageA.id]); - // Redoing back to depth 1 removes both the file and the row-removed image. - const redone = reconcileAttachmentContextReferences({ - referencedContextIds: new Set(), - files: [], - queuedAttachments: [ - { attachment: chipFile, contextIds: [chipContextId] }, - { attachment: plainFile, contextIds: [] }, - { attachment: rowRemovedImage, contextIds: [imageContextId] }, - ], - removedQueuedAttachments: [], - history: { depth: 1, previousDepth: 0, direction: "redo" }, - textChanged: true, - images: [], - previewAnnotations: [], - retained, - }); - expect(redone.queuedAttachmentIdsToRemove).toEqual([chipFile.id, rowRemovedImage.id]); - expect(redone.queuedAttachmentsToRestore).toEqual([]); + // × -> Paste -> Backspace -> Undo x3 -> Redo x3 keeps image A attached after the final redo. + basic.clickRowRemove(imageA.id); + expect(basic.liveIds()).toEqual([plainFile.id]); + basic.pasteQueuedImageChip(imageA.id, imageAContextId); + expect(basic.liveIds()).toEqual([plainFile.id, imageA.id]); + basic.deleteChipText(imageAContextId); + expect(basic.liveIds()).toEqual([plainFile.id, imageA.id]); + basic.undo(); + expect(basic.liveIds()).toEqual([plainFile.id, imageA.id]); + basic.undo(); + expect(basic.liveIds()).toEqual([plainFile.id]); + basic.undo(); + expect(basic.liveIds()).toEqual([plainFile.id, imageA.id]); + basic.redo(); + expect(basic.liveIds()).toEqual([plainFile.id]); + basic.redo(); + expect(basic.liveIds()).toEqual([plainFile.id, imageA.id]); + basic.redo(); + expect(basic.liveIds()).toEqual([plainFile.id, imageA.id]); - // Undoing again and deleting the image chip with Backspace (non-history edit 0 -> 1) - // keeps the image on the shelf and clears the overwritten × redo entry. - reconcileAttachmentContextReferences({ - referencedContextIds: new Set([imageContextId]), - files: [], - queuedAttachments: [{ attachment: plainFile, contextIds: [] }], - removedQueuedAttachments: [{ attachment: rowRemovedImage, contextIds: [imageContextId] }], - history: { depth: 0, previousDepth: 1, direction: "undo" }, - textChanged: true, - images: [], - previewAnnotations: [], - retained, - }); - const backspaced = reconcileAttachmentContextReferences({ - referencedContextIds: new Set(), - files: [], - queuedAttachments: [ - { attachment: plainFile, contextIds: [] }, - { attachment: rowRemovedImage, contextIds: [imageContextId] }, + // Branching via a controlled row removal: × on A -> Undo -> × on B -> Backspace A -> Undo -> Redo + // must keep A attached and B removed. + const branched = createQueuedEditHarness({ + initialText: `Compare <<${imageAContextId}>> <<${imageBContextId}>>`, + attachments: [ + { attachment: imageA, contextIds: [imageAContextId] }, + { attachment: imageB, contextIds: [imageBContextId] }, ], - removedQueuedAttachments: [], - history: { depth: 1, previousDepth: 0, direction: null }, - textChanged: true, - images: [], - previewAnnotations: [], - retained, }); - expect(backspaced.queuedAttachmentIdsToRemove).toEqual([]); - expect(backspaced.queuedAttachmentsToRestore).toEqual([]); + branched.clickRowRemove(imageA.id); + expect(branched.liveIds()).toEqual([imageB.id]); + branched.undo(); + expect(branched.liveIds()).toEqual([imageA.id, imageB.id]); + branched.clickRowRemove(imageB.id); + expect(branched.liveIds()).toEqual([imageA.id]); + branched.deleteChipText(imageAContextId); + expect(branched.liveIds()).toEqual([imageA.id]); + branched.undo(); + expect(branched.liveIds()).toEqual([imageA.id]); + branched.redo(); + expect(branched.liveIds()).toEqual([imageA.id]); - // Full sequence: × (0->1) -> Paste (1->2) -> Backspace (2->3) -> Undo x3 (3->2->1->0) -> - // Redo x3 (0->1->2->3): redoing × at depth 1 removes, redoing Paste at depth 2 restores, - // and redoing Backspace at depth 3 keeps the image on the shelf. - const freshRetained = retention(); - freshRetained.queuedImageHistory.set(rowRemovedImage.id, { - statesByDepth: new Map(), - pendingPaste: true, + // Overflow trimming: after 119 undo groups, × (depth 120) -> Paste (trims 121 -> 100) -> Undo + // must still remove image A. + const trimmed = createQueuedEditHarness({ + initialText: `Start <<${imageAContextId}>>`, + attachments: [{ attachment: imageA, contextIds: [imageAContextId] }], }); - reconcileAttachmentContextReferences({ - referencedContextIds: new Set([imageContextId]), - files: [], - queuedAttachments: [{ attachment: rowRemovedImage, contextIds: [imageContextId] }], - removedQueuedAttachments: [], - history: { depth: 2, previousDepth: 1, direction: null }, - textChanged: true, - images: [], - previewAnnotations: [], - retained: freshRetained, - }); - expect( - reconcileAttachmentContextReferences({ - referencedContextIds: new Set(), - files: [], - queuedAttachments: [{ attachment: rowRemovedImage, contextIds: [imageContextId] }], - removedQueuedAttachments: [], - history: { depth: 3, previousDepth: 2, direction: null }, - textChanged: true, - images: [], - previewAnnotations: [], - retained: freshRetained, - }).queuedAttachmentIdsToRemove, - ).toEqual([]); - expect( - reconcileAttachmentContextReferences({ - referencedContextIds: new Set([imageContextId]), - files: [], - queuedAttachments: [{ attachment: rowRemovedImage, contextIds: [imageContextId] }], - removedQueuedAttachments: [], - history: { depth: 2, previousDepth: 3, direction: "undo" }, - textChanged: true, - images: [], - previewAnnotations: [], - retained: freshRetained, - }).queuedAttachmentIdsToRemove, - ).toEqual([]); - expect( - reconcileAttachmentContextReferences({ - referencedContextIds: new Set(), - files: [], - queuedAttachments: [{ attachment: rowRemovedImage, contextIds: [imageContextId] }], - removedQueuedAttachments: [], - history: { depth: 1, previousDepth: 2, direction: "undo" }, - textChanged: true, - images: [], - previewAnnotations: [], - retained: freshRetained, - }).queuedAttachmentIdsToRemove, - ).toEqual([rowRemovedImage.id]); - expect( - reconcileAttachmentContextReferences({ - referencedContextIds: new Set([imageContextId]), - files: [], - queuedAttachments: [], - removedQueuedAttachments: [{ attachment: rowRemovedImage, contextIds: [imageContextId] }], - history: { depth: 0, previousDepth: 1, direction: "undo" }, - textChanged: true, - images: [], - previewAnnotations: [], - retained: freshRetained, - }).queuedAttachmentsToRestore, - ).toEqual([rowRemovedImage]); - expect( - reconcileAttachmentContextReferences({ - referencedContextIds: new Set(), - files: [], - queuedAttachments: [{ attachment: rowRemovedImage, contextIds: [imageContextId] }], - removedQueuedAttachments: [], - history: { depth: 1, previousDepth: 0, direction: "redo" }, - textChanged: true, - images: [], - previewAnnotations: [], - retained: freshRetained, - }).queuedAttachmentIdsToRemove, - ).toEqual([rowRemovedImage.id]); - expect( - reconcileAttachmentContextReferences({ - referencedContextIds: new Set([imageContextId]), - files: [], - queuedAttachments: [], - removedQueuedAttachments: [{ attachment: rowRemovedImage, contextIds: [imageContextId] }], - history: { depth: 2, previousDepth: 1, direction: "redo" }, - textChanged: true, - images: [], - previewAnnotations: [], - retained: freshRetained, - }).queuedAttachmentsToRestore, - ).toEqual([rowRemovedImage]); - expect( - reconcileAttachmentContextReferences({ - referencedContextIds: new Set(), - files: [], - queuedAttachments: [{ attachment: rowRemovedImage, contextIds: [imageContextId] }], - removedQueuedAttachments: [], - history: { depth: 3, previousDepth: 2, direction: "redo" }, - textChanged: true, - images: [], - previewAnnotations: [], - retained: freshRetained, - }).queuedAttachmentIdsToRemove, - ).toEqual([]); + for (let i = 0; i < 119; i += 1) { + trimmed.typeGroup("x"); + } + trimmed.clickRowRemove(imageA.id); + expect(trimmed.liveIds()).toEqual([]); + trimmed.pasteQueuedImageChip(imageA.id, imageAContextId); + expect(trimmed.liveIds()).toEqual([imageA.id]); + trimmed.undo(); + expect(trimmed.liveIds()).toEqual([]); + trimmed.redo(); + expect(trimmed.liveIds()).toEqual([imageA.id]); }); }); diff --git a/apps/web/src/components/chat/composerContextUndo.ts b/apps/web/src/components/chat/composerContextUndo.ts index 94ee7bb19a8a..cdd99556e302 100644 --- a/apps/web/src/components/chat/composerContextUndo.ts +++ b/apps/web/src/components/chat/composerContextUndo.ts @@ -7,11 +7,11 @@ import type { } from "@t3tools/contracts"; import { useEffect, useState } from "react"; +import type { ComposerHistoryRevision } from "../../composer-undo-grouping"; import type { ComposerFileAttachment, ComposerImageAttachment } from "../../composerDraftStore"; import { fileContextReference, previewAnnotationContextId } from "../../lib/composerContextRecords"; import type { TerminalContextDraft } from "../../lib/terminalContext"; import type { ReviewCommentContext } from "../../reviewCommentContext"; -import type { ComposerPromptHistoryChange } from "../ComposerPromptEditorTiptap"; import { isQueuedFileAttachment, isQueuedImageAttachment } from "./queuedMessageEdit"; interface RetainedPreviewAnnotation { @@ -24,30 +24,56 @@ export interface QueuedAttachmentContextBinding { readonly contextIds: ReadonlyArray; } -export interface QueuedImageHistoryEntry { - readonly statesByDepth: Map; - pendingPaste?: true | undefined; +export interface QueuedImageHistoryState { + currentRevision: number; + readonly removedIdsByRevision: Map>; + readonly pendingRestoredIds: Set; } export interface RetainedAttachmentContextPayloads { files: Map; previewAnnotations: Map; - queuedImageHistory: Map; + queuedImageHistory: QueuedImageHistoryState; } -function queuedImageStateAtDepth( - statesByDepth: ReadonlyMap, - depth: number, -): "attached" | "removed" { - let latestDepth = -1; - let state: "attached" | "removed" = "attached"; - for (const [entryDepth, entryState] of statesByDepth) { - if (entryDepth <= depth && entryDepth >= latestDepth) { - latestDepth = entryDepth; - state = entryState; +export function createQueuedImageHistoryState(): QueuedImageHistoryState { + return { + currentRevision: 0, + removedIdsByRevision: new Map(), + pendingRestoredIds: new Set(), + }; +} + +export function recordQueuedImageHistoryRevision( + state: QueuedImageHistoryState, + history: ComposerHistoryRevision, + removedQueuedAttachments: ReadonlyArray, +): void { + const active = new Set(history.activeRevisions); + for (const revision of state.removedIdsByRevision.keys()) { + if (!active.has(revision)) state.removedIdsByRevision.delete(revision); + } + const removedImageIds = new Set(); + for (const entry of removedQueuedAttachments) { + if (isQueuedImageAttachment(entry.attachment)) { + removedImageIds.add(entry.attachment.id); } } - return state; + for (const restoredId of state.pendingRestoredIds) { + removedImageIds.delete(restoredId); + } + state.pendingRestoredIds.clear(); + state.currentRevision = history.revision; + state.removedIdsByRevision.set(history.revision, removedImageIds); +} + +export function recordChiplessQueuedImageRemoval( + state: QueuedImageHistoryState, + attachmentId: string, +): void { + const next = new Set(state.removedIdsByRevision.get(state.currentRevision)); + next.add(attachmentId); + state.removedIdsByRevision.set(state.currentRevision, next); } export interface RetainedContextPayloads extends RetainedAttachmentContextPayloads { @@ -65,7 +91,7 @@ function createRetainedContextPayloads(): RetainedContextPayloads { environments: new Map(), files: new Map(), previewAnnotations: new Map(), - queuedImageHistory: new Map(), + queuedImageHistory: createQueuedImageHistoryState(), }; } @@ -82,7 +108,14 @@ export function useRetainedContextPayloads(draftTargetKey: string): RetainedCont // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- clears the retained payload maps when the draft target changes or the composer unmounts useEffect( () => () => { - for (const payloads of Object.values(retained)) payloads.clear(); + retained.terminals.clear(); + retained.reviewComments.clear(); + retained.threads.clear(); + retained.environments.clear(); + retained.files.clear(); + retained.previewAnnotations.clear(); + retained.queuedImageHistory.removedIdsByRevision.clear(); + retained.queuedImageHistory.pendingRestoredIds.clear(); }, [retained], ); @@ -94,7 +127,7 @@ export function reconcileAttachmentContextReferences(input: { files: ReadonlyArray; queuedAttachments?: ReadonlyArray; removedQueuedAttachments?: ReadonlyArray; - history?: ComposerPromptHistoryChange | undefined; + history?: ComposerHistoryRevision | undefined; textChanged?: boolean; images: ReadonlyArray; previewAnnotations: ReadonlyArray; @@ -124,25 +157,24 @@ export function reconcileAttachmentContextReferences(input: { }); const history = input.history; - if (history && history.direction === null && input.textChanged) { - // Any non-history edit drops ProseMirror's redo branch while keeping earlier undo steps. - for (const [imageId, entry] of input.retained.queuedImageHistory) { - for (const entryDepth of entry.statesByDepth.keys()) { - if (entryDepth > history.previousDepth) entry.statesByDepth.delete(entryDepth); - } - if (entry.pendingPaste) { - if (queuedImageStateAtDepth(entry.statesByDepth, history.previousDepth) !== "removed") { - entry.statesByDepth.set(history.previousDepth, "removed"); - } - entry.statesByDepth.set(history.depth, "attached"); - entry.pendingPaste = undefined; - } - if (entry.statesByDepth.size === 0) { - input.retained.queuedImageHistory.delete(imageId); - } + const removedQueuedAttachments = input.removedQueuedAttachments ?? []; + if (history) { + if (history.direction === null && input.textChanged) { + recordQueuedImageHistoryRevision( + input.retained.queuedImageHistory, + history, + removedQueuedAttachments, + ); + } else if (history.direction !== null) { + input.retained.queuedImageHistory.currentRevision = history.revision; } } + const removedAtRevision = + history?.direction != null + ? input.retained.queuedImageHistory.removedIdsByRevision.get(history.revision) + : undefined; + const liveQueuedAttachments = input.queuedAttachments ?? []; const liveQueuedAttachmentIds = new Set( liveQueuedAttachments.map((entry) => entry.attachment.id), @@ -153,19 +185,16 @@ export function reconcileAttachmentContextReferences(input: { if (entry.contextIds.some((contextId) => input.referencedContextIds.has(contextId))) continue; if (isQueuedFileAttachment(entry.attachment)) { queuedAttachmentIdsToRemove.push(entry.attachment.id); - } else if (isQueuedImageAttachment(entry.attachment) && history?.direction) { - const historyEntry = input.retained.queuedImageHistory.get(entry.attachment.id); - if ( - historyEntry && - queuedImageStateAtDepth(historyEntry.statesByDepth, history.depth) === "removed" - ) { - queuedAttachmentIdsToRemove.push(entry.attachment.id); - } + } else if ( + isQueuedImageAttachment(entry.attachment) && + removedAtRevision?.has(entry.attachment.id) + ) { + queuedAttachmentIdsToRemove.push(entry.attachment.id); } } const restoredQueuedAttachmentIds = new Set(); const queuedAttachmentsToRestore: ChatAttachment[] = []; - for (const entry of input.removedQueuedAttachments ?? []) { + for (const entry of removedQueuedAttachments) { if ( entry.contextIds.length === 0 || liveQueuedAttachmentIds.has(entry.attachment.id) || @@ -175,16 +204,6 @@ export function reconcileAttachmentContextReferences(input: { } if (!entry.contextIds.some((contextId) => input.referencedContextIds.has(contextId))) continue; restoredQueuedAttachmentIds.add(entry.attachment.id); - if (isQueuedImageAttachment(entry.attachment) && history?.direction) { - const historyEntry = input.retained.queuedImageHistory.get(entry.attachment.id) ?? { - statesByDepth: new Map(), - }; - if (history.direction === "undo") { - historyEntry.statesByDepth.set(history.previousDepth, "removed"); - } - historyEntry.statesByDepth.set(history.depth, "attached"); - input.retained.queuedImageHistory.set(entry.attachment.id, historyEntry); - } queuedAttachmentsToRestore.push(entry.attachment); } diff --git a/apps/web/src/composer-undo-grouping.ts b/apps/web/src/composer-undo-grouping.ts index 708b4f41fe52..ba5aaa4cb7e8 100644 --- a/apps/web/src/composer-undo-grouping.ts +++ b/apps/web/src/composer-undo-grouping.ts @@ -1,5 +1,5 @@ -import { closeHistory, isHistoryTransaction } from "@tiptap/pm/history"; -import type { Transaction } from "@tiptap/pm/state"; +import { closeHistory, isHistoryTransaction, redoDepth, undoDepth } from "@tiptap/pm/history"; +import { type EditorState, Plugin, PluginKey, type Transaction } from "@tiptap/pm/state"; import { ReplaceStep } from "@tiptap/pm/transform"; // Whole-draft rewrites make all edits adjacent; explicit boundaries keep change kinds separate. @@ -7,6 +7,122 @@ export const COMPOSER_UNDO_GROUP_DELAY = 1000; export type ComposerChangeKind = "insert" | "delete" | "other"; +export interface ComposerHistoryRevision { + readonly revision: number; + readonly activeRevisions: ReadonlyArray; + readonly direction: "undo" | "redo" | null; +} + +interface ComposerHistoryRevisionState { + readonly current: number; + readonly nextId: number; + readonly undo: ReadonlyArray; + readonly redo: ReadonlyArray; + readonly direction: "undo" | "redo" | null; +} + +const composerHistoryRevisionPluginKey = new PluginKey( + "composerHistoryRevision", +); + +/** + * Mirrors ProseMirror's undo/redo event stacks with monotonic revision IDs so + * external state stays accurate across controlled rewrites and history overflow trimming. + * Register after the ProseMirror history plugin. + */ +export function createComposerHistoryRevisionPlugin(): Plugin { + return new Plugin({ + key: composerHistoryRevisionPluginKey, + state: { + init(): ComposerHistoryRevisionState { + return { current: 0, nextId: 1, undo: [], redo: [], direction: null }; + }, + apply(tr, prev, oldState, newState): ComposerHistoryRevisionState { + const prevUndoDepth = Number(undoDepth(oldState)); + const prevRedoDepth = Number(redoDepth(oldState)); + const nextUndoDepth = Number(undoDepth(newState)); + const nextRedoDepth = Number(redoDepth(newState)); + + if (isHistoryTransaction(tr)) { + if (nextUndoDepth < prevUndoDepth) { + const landed = prev.undo.at(-1) ?? prev.current; + const nextRedo = [...prev.redo, prev.current]; + return { + current: landed, + nextId: prev.nextId, + undo: prev.undo.slice(0, -1), + redo: + nextRedo.length > nextRedoDepth + ? nextRedo.slice(nextRedo.length - nextRedoDepth) + : nextRedo, + direction: "undo", + }; + } + if (nextRedoDepth < prevRedoDepth) { + const landed = prev.redo.at(-1) ?? prev.current; + const nextUndo = [...prev.undo, prev.current]; + return { + current: landed, + nextId: prev.nextId, + undo: + nextUndo.length > nextUndoDepth + ? nextUndo.slice(nextUndo.length - nextUndoDepth) + : nextUndo, + redo: prev.redo.slice(0, -1), + direction: "redo", + }; + } + return { ...prev, direction: null }; + } + + const appended = tr.getMeta("appendedTransaction") as Transaction | undefined; + if ( + tr.steps.length === 0 || + tr.getMeta("addToHistory") === false || + appended?.getMeta("addToHistory") === false || + (appended && isHistoryTransaction(appended)) + ) { + return { ...prev, direction: null }; + } + + if (nextUndoDepth !== prevUndoDepth) { + const nextUndo = [...prev.undo, prev.current]; + return { + current: prev.nextId, + nextId: prev.nextId + 1, + undo: + nextUndo.length > nextUndoDepth + ? nextUndo.slice(nextUndo.length - nextUndoDepth) + : nextUndo, + redo: [], + direction: null, + }; + } + + return { + current: prev.current, + nextId: prev.nextId, + undo: prev.undo, + redo: [], + direction: null, + }; + }, + }, + }); +} + +export function composerHistoryRevisionOf(state: EditorState): ComposerHistoryRevision { + const revisionState = composerHistoryRevisionPluginKey.getState(state); + if (!revisionState) { + return { revision: 0, activeRevisions: [0], direction: null }; + } + return { + revision: revisionState.current, + activeRevisions: [...revisionState.undo, revisionState.current, ...revisionState.redo], + direction: revisionState.direction, + }; +} + const CLIPBOARD_EDIT_META = "composerClipboardEdit"; const COMPOSITION_REPLACE_META = "composerCompositionReplace"; From 5f8773df3b424b717d812b59c8858d26ece8f62f Mon Sep 17 00:00:00 2001 From: gemini-4-neon Date: Sun, 22 Nov 2026 10:31:14 -0600 Subject: [PATCH 10/14] fix(web): preserve history direction across stale skill chip replacement Co-Authored-By: stefan --- apps/web/src/components/ComposerPromptEditorTiptap.tsx | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/apps/web/src/components/ComposerPromptEditorTiptap.tsx b/apps/web/src/components/ComposerPromptEditorTiptap.tsx index 2f7f6fdc489e..da12fad6709f 100644 --- a/apps/web/src/components/ComposerPromptEditorTiptap.tsx +++ b/apps/web/src/components/ComposerPromptEditorTiptap.tsx @@ -1058,6 +1058,7 @@ function ComposerPromptEditorTiptapInner( }); const selectionRangeRef = useRef({ start: initialExpandedCursor, end: initialExpandedCursor }); const isApplyingControlledUpdateRef = useRef(false); + const pendingHistoryDirectionRef = useRef(null); const hasAppliedControlledSelectionRef = useRef(false); const citationRequestRef = useRef(null); const [openCitation, setOpenCitation] = useState(null); @@ -1135,6 +1136,12 @@ function ComposerPromptEditorTiptapInner( (node) => node?.isInline && node.isAtom && !node.isText && node.type.name !== "hardBreak", ); const textChanged = previousSnapshot.value !== nextValue; + const rawHistory = composerHistoryRevisionOf(updated.state); + const history = + pendingHistoryDirectionRef.current !== null && rawHistory.direction === null + ? { ...rawHistory, direction: pendingHistoryDirectionRef.current } + : rawHistory; + pendingHistoryDirectionRef.current = null; readCallbacks().onChange( nextValue, nextCursor, @@ -1142,7 +1149,7 @@ function ComposerPromptEditorTiptapInner( suppressTrigger, contextIds, textChanged, - composerHistoryRevisionOf(updated.state), + history, ); }; @@ -1572,6 +1579,7 @@ function ComposerPromptEditorTiptapInner( isKnownModel, ).filter(({ node }) => node.isText); if (stale.length > 0) { + pendingHistoryDirectionRef.current = composerHistoryRevisionOf(updated.state).direction; replaceSkillChips(updated, stale); return; } From 145f1b372bf5ac00b0350868e26779a9d787ee31 Mon Sep 17 00:00:00 2001 From: gemini-4-neon Date: Sun, 22 Nov 2026 10:50:05 -0600 Subject: [PATCH 11/14] fix(web): scope queued edit restoration and stale skill history direction Co-Authored-By: stefan --- .../components/ComposerPromptEditorTiptap.tsx | 18 +- apps/web/src/components/chat/ChatComposer.tsx | 45 +++-- .../components/chat/queuedMessageEdit.test.ts | 59 ++++++ .../chat/useComposerContextImport.ts | 2 +- .../chat/useQueuedRunEdit.browser.test.tsx | 190 ++++++------------ 5 files changed, 160 insertions(+), 154 deletions(-) diff --git a/apps/web/src/components/ComposerPromptEditorTiptap.tsx b/apps/web/src/components/ComposerPromptEditorTiptap.tsx index da12fad6709f..485df8083a21 100644 --- a/apps/web/src/components/ComposerPromptEditorTiptap.tsx +++ b/apps/web/src/components/ComposerPromptEditorTiptap.tsx @@ -806,11 +806,17 @@ type TiptapEditor = NonNullable>; const SILENT_SELECTION_META = "composerSilentSelection"; +const CARRIED_HISTORY_DIRECTION_META = "composerCarriedHistoryDirection"; + function replaceSkillChips( editor: TiptapEditor, replacements: ReturnType, + carriedHistoryDirection?: ComposerPromptHistoryChange["direction"], ): void { const transaction = editor.state.tr.setMeta("addToHistory", false); + if (carriedHistoryDirection) { + transaction.setMeta(CARRIED_HISTORY_DIRECTION_META, carriedHistoryDirection); + } for (const { from, to, node } of replacements) transaction.replaceWith(from, to, node); editor.view.dispatch(transaction); } @@ -1058,7 +1064,6 @@ function ComposerPromptEditorTiptapInner( }); const selectionRangeRef = useRef({ start: initialExpandedCursor, end: initialExpandedCursor }); const isApplyingControlledUpdateRef = useRef(false); - const pendingHistoryDirectionRef = useRef(null); const hasAppliedControlledSelectionRef = useRef(false); const citationRequestRef = useRef(null); const [openCitation, setOpenCitation] = useState(null); @@ -1137,11 +1142,13 @@ function ComposerPromptEditorTiptapInner( ); const textChanged = previousSnapshot.value !== nextValue; const rawHistory = composerHistoryRevisionOf(updated.state); + const carriedDirection = transaction.getMeta(CARRIED_HISTORY_DIRECTION_META) as + | ComposerPromptHistoryChange["direction"] + | undefined; const history = - pendingHistoryDirectionRef.current !== null && rawHistory.direction === null - ? { ...rawHistory, direction: pendingHistoryDirectionRef.current } + carriedDirection && rawHistory.direction === null + ? { ...rawHistory, direction: carriedDirection } : rawHistory; - pendingHistoryDirectionRef.current = null; readCallbacks().onChange( nextValue, nextCursor, @@ -1579,8 +1586,7 @@ function ComposerPromptEditorTiptapInner( isKnownModel, ).filter(({ node }) => node.isText); if (stale.length > 0) { - pendingHistoryDirectionRef.current = composerHistoryRevisionOf(updated.state).direction; - replaceSkillChips(updated, stale); + replaceSkillChips(updated, stale, composerHistoryRevisionOf(updated.state).direction); return; } } diff --git a/apps/web/src/components/chat/ChatComposer.tsx b/apps/web/src/components/chat/ChatComposer.tsx index f0d29ce2c761..f37e94f76956 100644 --- a/apps/web/src/components/chat/ChatComposer.tsx +++ b/apps/web/src/components/chat/ChatComposer.tsx @@ -668,6 +668,12 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) activePendingProgress.activeQuestion.id, ) : null; + const queuedEditOwnsComposer = + questionAttachmentTarget === null && pendingUserInputs.length === 0; + const activeEditingQueuedAttachments = queuedEditOwnsComposer ? editingQueuedAttachments : null; + const activeRemovedEditingQueuedAttachments = queuedEditOwnsComposer + ? removedEditingQueuedAttachments + : undefined; const attachmentDraftTarget = questionAttachmentTarget ?? composerDraftTarget; const attachmentDraft = useComposerThreadDraft(attachmentDraftTarget); const questionTakesSnapShots = @@ -771,7 +777,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) const preview = buildExpandedImagePreview(composerImages, imageId, copyComposerSnapshotStable) ?? buildExpandedImagePreview( - (editingQueuedAttachments ?? []).flatMap(({ attachment, url }) => + (activeEditingQueuedAttachments ?? []).flatMap(({ attachment, url }) => isQueuedImageAttachment(attachment) && url ? [{ ...attachment, previewUrl: url }] : [], ), imageId, @@ -783,7 +789,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) setPreviewFileId(fileId); return; } - const queuedFile = editingQueuedAttachments?.find( + const queuedFile = activeEditingQueuedAttachments?.find( ({ attachment }) => isQueuedFileAttachment(attachment) && attachment.id === fileId, )?.attachment; if (queuedFile && isQueuedFileAttachment(queuedFile)) onFileOpen(queuedFile); @@ -810,7 +816,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) if (persistedPreview) onExpandImage(persistedPreview); return; } - const queuedFile = editingQueuedAttachments?.find( + const queuedFile = activeEditingQueuedAttachments?.find( ({ attachment }) => isQueuedFileAttachment(attachment) && attachment.id === fileId, )?.attachment; if (!queuedFile || !isQueuedFileAttachment(queuedFile) || !isVideoAttachment(queuedFile)) { @@ -831,7 +837,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) environmentContexts: composerEnvironmentContexts, images: composerImages, files: composerFiles, - existingAttachments: editingQueuedAttachments ?? undefined, + existingAttachments: activeEditingQueuedAttachments ?? undefined, environmentId, uploadsByImageId, }); @@ -1430,7 +1436,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) if (attachment.file) copiedFiles.set(record.attachmentId, attachment.file); return [record]; }), - ...(editingQueuedAttachments ?? []).flatMap(({ attachment, contextIds }) => { + ...(activeEditingQueuedAttachments ?? []).flatMap(({ attachment, contextIds }) => { if (!isQueuedImageAttachment(attachment) && !isQueuedFileAttachment(attachment)) return []; return (contextIds ?? []) .filter((contextId) => wanted.has(contextId)) @@ -1491,8 +1497,9 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) promptRef, pendingThreadImportsRef, addComposerFilesToDraft, - removedQueuedAttachments: removedEditingQueuedAttachments, + removedQueuedAttachments: activeRemovedEditingQueuedAttachments, onRestoreQueuedAttachment: (attachment) => { + if (!queuedEditOwnsComposer) return; if (isQueuedImageAttachment(attachment)) { retainedContextPayloads.queuedImageHistory.pendingRestoredIds.add(attachment.id); } @@ -1502,16 +1509,19 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) const onControlledHistoryChangeStable = useStableCallback( (history: ComposerPromptHistoryChange) => { + if (!queuedEditOwnsComposer) return; recordQueuedImageHistoryRevision( retainedContextPayloads.queuedImageHistory, history, - removedEditingQueuedAttachments ?? [], + activeRemovedEditingQueuedAttachments ?? [], ); }, ); const onRemoveEditingQueuedAttachmentStable = useStableCallback((attachmentId: string) => { - const entry = editingQueuedAttachments?.find((item) => item.attachment.id === attachmentId); + const entry = activeEditingQueuedAttachments?.find( + (item) => item.attachment.id === attachmentId, + ); if ( entry && isQueuedImageAttachment(entry.attachment) && @@ -1838,11 +1848,13 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) const attachmentChanges = reconcileAttachmentContextReferences({ referencedContextIds: referenced, files: composerFiles, - queuedAttachments: (editingQueuedAttachments ?? []).map(({ attachment, contextIds }) => ({ - attachment, - contextIds: contextIds ?? [], - })), - removedQueuedAttachments: removedEditingQueuedAttachments ?? [], + queuedAttachments: (activeEditingQueuedAttachments ?? []).map( + ({ attachment, contextIds }) => ({ + attachment, + contextIds: contextIds ?? [], + }), + ), + removedQueuedAttachments: activeRemovedEditingQueuedAttachments ?? [], history, textChanged, images: composerImages, @@ -3903,11 +3915,10 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) )} {!isComposerCollapsedMobile && - pendingUserInputs.length === 0 && - editingQueuedAttachments !== null && - editingQueuedAttachments.length > 0 && ( + activeEditingQueuedAttachments !== null && + activeEditingQueuedAttachments.length > 0 && ( )} diff --git a/apps/web/src/components/chat/queuedMessageEdit.test.ts b/apps/web/src/components/chat/queuedMessageEdit.test.ts index 0ebfdacdbb60..29a36aa7911e 100644 --- a/apps/web/src/components/chat/queuedMessageEdit.test.ts +++ b/apps/web/src/components/chat/queuedMessageEdit.test.ts @@ -19,6 +19,7 @@ import { readAttachmentUpload, useAttachmentUploadStore } from "../../lib/attach import { environmentContextRecord, responseContextRecord } from "../../lib/composerContextRecords"; import { formatInlineContextReference } from "../../lib/composerContextReferences"; import { + attachmentContextIdsForRecord, matchRestorableQueuedAttachment, prepareQueuedEditAttachments, recoverQueuedMessageEdit, @@ -235,6 +236,64 @@ describe("queued message file edits", () => { expect(store.getComposerDraft(threadTarget)).toBeNull(); }); + it("maps existing queued attachments to matching wire context ids", () => { + const imageId = ChatAttachmentId.make("server-image-1"); + const fileId = ChatAttachmentId.make("server-file-1"); + const otherFileId = ChatAttachmentId.make("server-file-2"); + const imageContextId = ComposerContextId.make("image_local-1"); + const fileContextId = ComposerContextId.make("file_local-1"); + const imageAttachment = { + type: "image" as const, + id: imageId, + name: "image.png", + mimeType: "image/png", + sizeBytes: 67, + }; + const fileAttachment = { + type: "file" as const, + id: fileId, + name: "notes.txt", + mimeType: "text/plain", + sizeBytes: 12, + }; + const unreferencedFile = { + type: "file" as const, + id: otherFileId, + name: "legacy.pdf", + mimeType: "application/pdf", + sizeBytes: 40, + }; + const context = { + version: 1 as const, + records: [ + { + version: 1 as const, + kind: "image" as const, + contextId: imageContextId, + label: "image.png", + attachmentId: imageId, + name: "image.png", + mimeType: "image/png", + sizeBytes: 67, + }, + { + version: 1 as const, + kind: "file" as const, + contextId: fileContextId, + label: "notes.txt", + attachmentId: fileId, + name: "notes.txt", + mimeType: "text/plain", + sizeBytes: 12, + }, + ], + }; + + expect(attachmentContextIdsForRecord(context, imageAttachment)).toEqual([imageContextId]); + expect(attachmentContextIdsForRecord(context, fileAttachment)).toEqual([fileContextId]); + expect(attachmentContextIdsForRecord(context, unreferencedFile)).toEqual([]); + }); + it("matches a removed queued attachment for same-environment paste while rejecting other environments or payloads", () => { const fileId = ChatAttachmentId.make("server-file-1"); const contextId = ComposerContextId.make("file_local-1"); diff --git a/apps/web/src/components/chat/useComposerContextImport.ts b/apps/web/src/components/chat/useComposerContextImport.ts index ed1d8ef4988d..458f39dc9863 100644 --- a/apps/web/src/components/chat/useComposerContextImport.ts +++ b/apps/web/src/components/chat/useComposerContextImport.ts @@ -440,7 +440,7 @@ export function useComposerContextImport(input: { case "image": case "file": { if (skippedDependentAttachmentIds.has(record.contextId)) break; - if (!conflicts) { + if (questionAttachmentTarget === null && !conflicts) { const restored = matchRestorableQueuedAttachment({ environmentId, sourceEnvironmentId, diff --git a/apps/web/src/components/chat/useQueuedRunEdit.browser.test.tsx b/apps/web/src/components/chat/useQueuedRunEdit.browser.test.tsx index 9aa6a6228a24..5ad406da5ef9 100644 --- a/apps/web/src/components/chat/useQueuedRunEdit.browser.test.tsx +++ b/apps/web/src/components/chat/useQueuedRunEdit.browser.test.tsx @@ -344,177 +344,103 @@ describe("useQueuedRunEdit", () => { ); }); - it("binds existing attachment context ids for inline chips, preserves original order on restore, and omits removed chips and records on save", async () => { + it("opens an edit with only the thread and machine records its text still mentions", async () => { const { hook } = setup(); - mocks.editQueuedRun.mockResolvedValue({ _tag: "Success" }); + const thread = threadContextRecord(THREAD_B, "Support Any File Uploads"); + const machine = environmentContextRecord(ENV, "Build box"); + const unreferenced = threadContextRecord(THREAD_A, "Removed earlier"); const messageId = MessageId.make("message-1"); - const imageId = ChatAttachmentId.make("server-image-1"); - const fileId = ChatAttachmentId.make("server-file-1"); - const unreferencedFileId = ChatAttachmentId.make("server-file-2"); - const imageContextId = ComposerContextId.make("image_local-image-1"); - const fileContextId = ComposerContextId.make("file_local-file-1"); - const staleContextId = ComposerContextId.make("file_stale-file-2"); - const imageAttachment = { + const text = `wait for ${formatInlineContextReference(threadContextReference(thread))} on ${formatInlineContextReference(environmentContextReference(machine))}`; + const messages = [ + { id: messageId, text, context: { version: 1, records: [thread, machine, unreferenced] } }, + ]; + await using _screen = await render(view(THREAD_A, "queued", hook, messages)); + + await act(() => requireHook(hook).beginEdit({ runId: RUN, messageId, text, attachments: [] })); + + const draft = useComposerDraftStore.getState().getComposerDraft(requireHook(hook).draftTarget); + expect(draft?.prompt).toBe(text); + expect(draft?.threadContexts).toEqual([thread]); + expect(draft?.environmentContexts).toEqual([machine]); + }); + + it("saves only the thread mentions and attachments still in the edited message", async () => { + const { hook } = setup(); + mocks.editQueuedRun.mockResolvedValue({ _tag: "Success" }); + const kept = threadContextRecord(THREAD_B, "Kept"); + const removed = threadContextRecord(THREAD_A, "Removed"); + const removedImageId = ChatAttachmentId.make("server-image-removed"); + const keptFileId = ChatAttachmentId.make("server-file-kept"); + const removedImageContextId = ComposerContextId.make("image_local-removed"); + const keptFileContextId = ComposerContextId.make("file_local-kept"); + const removedImage = { type: "image" as const, - id: imageId, + id: removedImageId, name: "image.png", mimeType: "image/png", sizeBytes: 67, }; - const fileAttachment = { - type: "file" as const, - id: fileId, - name: "notes.txt", - mimeType: "text/plain", - sizeBytes: 12, - }; - const unreferencedFile = { + const keptFile = { type: "file" as const, - id: unreferencedFileId, + id: keptFileId, name: "legacy.pdf", mimeType: "application/pdf", sizeBytes: 40, }; - const imageChip = formatInlineContextReference({ - kind: "image", - contextId: imageContextId, - label: "image.png", - }); - const fileChip = formatInlineContextReference({ - kind: "file", - contextId: fileContextId, - label: "notes.txt", - }); - const text = `Check ${imageChip} and ${fileChip} map`; - const imageRecord = { + const removedImageRecord = { version: 1 as const, kind: "image" as const, - contextId: imageContextId, + contextId: removedImageContextId, label: "image.png", - attachmentId: imageId, + attachmentId: removedImageId, name: "image.png", mimeType: "image/png", sizeBytes: 67, }; - const fileRecord = { - version: 1 as const, - kind: "file" as const, - contextId: fileContextId, - label: "notes.txt", - attachmentId: fileId, - name: "notes.txt", - mimeType: "text/plain", - sizeBytes: 12, - }; - const staleRecord = { + const keptFileRecord = { version: 1 as const, kind: "file" as const, - contextId: staleContextId, + contextId: keptFileContextId, label: "legacy.pdf", - attachmentId: unreferencedFileId, + attachmentId: keptFileId, name: "legacy.pdf", mimeType: "application/pdf", sizeBytes: 40, }; + const messageId = MessageId.make("message-1"); + const text = [ + formatInlineContextReference(threadContextReference(kept)), + formatInlineContextReference(threadContextReference(removed)), + formatInlineContextReference({ + kind: "image", + contextId: removedImageContextId, + label: "image.png", + }), + ].join(" and "); const messages = [ { id: messageId, text, - context: { version: 1, records: [imageRecord, fileRecord, staleRecord] }, + context: { version: 1, records: [kept, removed, removedImageRecord, keptFileRecord] }, }, ]; - await using screen = await render(view(THREAD_A, "queued", hook, messages)); - + await using _screen = await render(view(THREAD_A, "queued", hook, messages)); await act(() => requireHook(hook).beginEdit({ runId: RUN, messageId, text, - attachments: [imageAttachment, fileAttachment, unreferencedFile], - }), - ); - expect(requireHook(hook).editingAttachments).toEqual([ - expect.objectContaining({ attachment: imageAttachment, contextIds: [imageContextId] }), - expect.objectContaining({ attachment: fileAttachment, contextIds: [fileContextId] }), - expect.objectContaining({ attachment: unreferencedFile, contextIds: [] }), - ]); - - await act(() => requireHook(hook).removeEditingAttachment(fileId)); - expect(requireHook(hook).editingAttachments?.map(({ attachment }) => attachment.id)).toEqual([ - imageId, - unreferencedFileId, - ]); - expect(requireHook(hook).removedEditingAttachments).toEqual([ - { attachment: fileAttachment, contextIds: [fileContextId] }, - ]); - await act(() => requireHook(hook).restoreEditingAttachment(fileAttachment)); - expect(requireHook(hook).editingAttachments?.map(({ attachment }) => attachment.id)).toEqual([ - imageId, - fileId, - unreferencedFileId, - ]); - expect(requireHook(hook).removedEditingAttachments).toEqual([]); - - await act(() => requireHook(hook).removeEditingAttachment(imageId)); - await act(() => requireHook(hook).removeEditingAttachment(fileId)); - expect(requireHook(hook).editingAttachments?.map(({ attachment }) => attachment.id)).toEqual([ - unreferencedFileId, - ]); - expect( - useComposerDraftStore.getState().getComposerDraft(requireHook(hook).draftTarget)?.prompt, - ).toBe("Check and map"); - - await screen.getByRole("button", { name: "Update queued message" }).click(); - await vi.waitFor(() => expect(mocks.editQueuedRun).toHaveBeenCalledOnce()); - expect(mocks.editQueuedRun).toHaveBeenCalledWith( - expect.objectContaining({ - input: expect.objectContaining({ - text: "Check and map", - edit: expect.objectContaining({ - attachments: [unreferencedFile], - context: { version: 1, records: [staleRecord] }, - }), - }), + attachments: [removedImage, keptFile], }), ); - }); - - it("opens an edit with only the thread and machine records its text still mentions", async () => { - const { hook } = setup(); - const thread = threadContextRecord(THREAD_B, "Support Any File Uploads"); - const machine = environmentContextRecord(ENV, "Build box"); - const unreferenced = threadContextRecord(THREAD_A, "Removed earlier"); - const messageId = MessageId.make("message-1"); - const text = `wait for ${formatInlineContextReference(threadContextReference(thread))} on ${formatInlineContextReference(environmentContextReference(machine))}`; - const messages = [ - { id: messageId, text, context: { version: 1, records: [thread, machine, unreferenced] } }, - ]; - await using _screen = await render(view(THREAD_A, "queued", hook, messages)); - - await act(() => requireHook(hook).beginEdit({ runId: RUN, messageId, text, attachments: [] })); - - const draft = useComposerDraftStore.getState().getComposerDraft(requireHook(hook).draftTarget); - expect(draft?.prompt).toBe(text); - expect(draft?.threadContexts).toEqual([thread]); - expect(draft?.environmentContexts).toEqual([machine]); - }); - - it("saves only the thread mentions still in the edited message", async () => { - const { hook } = setup(); - mocks.editQueuedRun.mockResolvedValue({ _tag: "Success" }); - const kept = threadContextRecord(THREAD_B, "Kept"); - const removed = threadContextRecord(THREAD_A, "Removed"); - const messageId = MessageId.make("message-1"); - const text = [kept, removed] - .map((record) => formatInlineContextReference(threadContextReference(record))) - .join(" and "); - const messages = [{ id: messageId, text, context: { version: 1, records: [kept, removed] } }]; - await using _screen = await render(view(THREAD_A, "queued", hook, messages)); - await act(() => requireHook(hook).beginEdit({ runId: RUN, messageId, text, attachments: [] })); const target = requireHook(hook).draftTarget; - await act(() => useComposerDraftStore.getState().setThreadContexts(target, [kept])); + await act(() => { + useComposerDraftStore.getState().setThreadContexts(target, [kept]); + requireHook(hook).removeEditingAttachment(removedImageId); + }); const draft = useComposerDraftStore.getState().getComposerDraft(target); + expect(draft?.prompt).not.toContain(removedImageContextId); await requireHook(hook).saveEdit( { ...savedContent(draft?.prompt ?? ""), threadContexts: draft?.threadContexts ?? [] }, () => {}, @@ -523,7 +449,11 @@ describe("useQueuedRunEdit", () => { expect(mocks.editQueuedRun).toHaveBeenCalledWith( expect.objectContaining({ input: expect.objectContaining({ - edit: expect.objectContaining({ context: { version: 1, records: [kept] } }), + text: draft?.prompt, + edit: expect.objectContaining({ + attachments: [keptFile], + context: { version: 1, records: [keptFileRecord, kept] }, + }), }), }), ); From 252f4d8a786d814be994084af09ce0826307be34 Mon Sep 17 00:00:00 2001 From: gemini-4-neon Date: Sun, 22 Nov 2026 10:59:54 -0600 Subject: [PATCH 12/14] fix(web): isolate question answer editor undo history from queued edits Co-Authored-By: stefan --- apps/web/src/components/chat/ChatComposer.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/web/src/components/chat/ChatComposer.tsx b/apps/web/src/components/chat/ChatComposer.tsx index f37e94f76956..64c6af44120c 100644 --- a/apps/web/src/components/chat/ChatComposer.tsx +++ b/apps/web/src/components/chat/ChatComposer.tsx @@ -3942,7 +3942,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) ) : null} Date: Sun, 22 Nov 2026 17:24:49 +0000 Subject: [PATCH 13/14] refactor(web): drop the editor history mirror from queued attachment edits Queued attachments now follow the same rules as draft attachments: a file leaves with its chip, an image stays on the shelf when only its chip is deleted, and a removed attachment returns when its chip comes back (undo or paste). That removes the ProseMirror revision plugin, the carried history direction, and the per-revision bookkeeping, which existed only so Redo could remove a queued image thumbnail again. The editor keeps its draftKey; the queued-edit ownership gate already isolates agent questions. Co-Authored-By: Claude Opus 5.5 --- .../components/ComposerPromptEditorTiptap.tsx | 38 +-- apps/web/src/components/chat/ChatComposer.tsx | 51 +-- .../chat/composerContextUndo.test.ts | 311 ++---------------- .../components/chat/composerContextUndo.ts | 122 +------ .../components/chat/queuedMessageEdit.test.ts | 59 ---- .../src/components/chat/useQueuedRunEdit.ts | 27 +- apps/web/src/composer-undo-grouping.ts | 120 +------ 7 files changed, 60 insertions(+), 668 deletions(-) diff --git a/apps/web/src/components/ComposerPromptEditorTiptap.tsx b/apps/web/src/components/ComposerPromptEditorTiptap.tsx index 485df8083a21..402156b00128 100644 --- a/apps/web/src/components/ComposerPromptEditorTiptap.tsx +++ b/apps/web/src/components/ComposerPromptEditorTiptap.tsx @@ -112,9 +112,6 @@ import { import { COMPOSER_UNDO_GROUP_DELAY, type ComposerChangeKind, - type ComposerHistoryRevision, - composerHistoryRevisionOf, - createComposerHistoryRevisionPlugin, groupUndoByChangeKind, markAsClipboardEdit, markAsCompositionReplace, @@ -173,8 +170,6 @@ export interface ComposerPromptEditorHandle { isCaretOnVisualEdge: (edge: "start" | "end") => boolean; } -export type ComposerPromptHistoryChange = ComposerHistoryRevision; - export interface ComposerPromptEditorProps { draftKey?: string; value: string; @@ -208,11 +203,7 @@ export interface ComposerPromptEditorProps { contextIds: string[], /** False when only the caret or selection moved. */ textChanged: boolean, - /** ProseMirror history revision and direction for this change. */ - history?: ComposerPromptHistoryChange, ) => void; - /** Fired after editor mount or controlled document rewrites that bypass onChange. */ - onControlledHistoryChange?: ((history: ComposerPromptHistoryChange) => void) | undefined; onVisibleSelectionChange?: () => void; onCommandKeyDown?: (key: string, event: KeyboardEvent, isTaskItem?: boolean) => boolean; onPageScrollKeyDown?: (key: "PageUp" | "PageDown") => void; @@ -806,17 +797,11 @@ type TiptapEditor = NonNullable>; const SILENT_SELECTION_META = "composerSilentSelection"; -const CARRIED_HISTORY_DIRECTION_META = "composerCarriedHistoryDirection"; - function replaceSkillChips( editor: TiptapEditor, replacements: ReturnType, - carriedHistoryDirection?: ComposerPromptHistoryChange["direction"], ): void { const transaction = editor.state.tr.setMeta("addToHistory", false); - if (carriedHistoryDirection) { - transaction.setMeta(CARRIED_HISTORY_DIRECTION_META, carriedHistoryDirection); - } for (const { from, to, node } of replacements) transaction.replaceWith(from, to, node); editor.view.dispatch(transaction); } @@ -864,9 +849,6 @@ const ComposerUndoGroupingExtension = Extension.create< addStorage() { return { previous: null }; }, - addProseMirrorPlugins() { - return [createComposerHistoryRevisionPlugin()]; - }, dispatchTransaction({ transaction, next }) { this.storage.previous = groupUndoByChangeKind(transaction, this.storage.previous); next(transaction); @@ -1012,7 +994,6 @@ function ComposerPromptEditorTiptapInner( placeholderClassName, placeholderSuffix, onChange, - onControlledHistoryChange, onVisibleSelectionChange, onCommandKeyDown, onPageScrollKeyDown, @@ -1039,7 +1020,6 @@ function ComposerPromptEditorTiptapInner( // The editor config is built once, so its handlers reach the current callbacks through here. const readCallbacks = useStableCallback(() => ({ onChange, - onControlledHistoryChange, onVisibleSelectionChange, onCommandKeyDown, isPathQueryActive, @@ -1140,23 +1120,13 @@ function ComposerPromptEditorTiptapInner( [updated.state.selection.$from.nodeBefore, updated.state.selection.$from.nodeAfter].some( (node) => node?.isInline && node.isAtom && !node.isText && node.type.name !== "hardBreak", ); - const textChanged = previousSnapshot.value !== nextValue; - const rawHistory = composerHistoryRevisionOf(updated.state); - const carriedDirection = transaction.getMeta(CARRIED_HISTORY_DIRECTION_META) as - | ComposerPromptHistoryChange["direction"] - | undefined; - const history = - carriedDirection && rawHistory.direction === null - ? { ...rawHistory, direction: carriedDirection } - : rawHistory; readCallbacks().onChange( nextValue, nextCursor, nextExpandedCursor, suppressTrigger, contextIds, - textChanged, - history, + previousSnapshot.value !== nextValue, ); }; @@ -1586,7 +1556,7 @@ function ComposerPromptEditorTiptapInner( isKnownModel, ).filter(({ node }) => node.isText); if (stale.length > 0) { - replaceSkillChips(updated, stale, composerHistoryRevisionOf(updated.state).direction); + replaceSkillChips(updated, stale); return; } } @@ -1734,9 +1704,6 @@ function ComposerPromptEditorTiptapInner( emitUpdate: false, }, ); - readCallbacks().onControlledHistoryChange?.(composerHistoryRevisionOf(editor.state)); - } else if (initialSelection) { - readCallbacks().onControlledHistoryChange?.(composerHistoryRevisionOf(editor.state)); } selectMarkdownOffset(editor, value, normalizedExpandedCursor); const map = serializeEditorDoc(editor.state.doc); @@ -1777,7 +1744,6 @@ function ComposerPromptEditorTiptapInner( expandEditorCursor, initialExpandedCursor, literalText, - readCallbacks, richText, skillLabelFor, isKnownModel, diff --git a/apps/web/src/components/chat/ChatComposer.tsx b/apps/web/src/components/chat/ChatComposer.tsx index 64c6af44120c..58f95b2d3f65 100644 --- a/apps/web/src/components/chat/ChatComposer.tsx +++ b/apps/web/src/components/chat/ChatComposer.tsx @@ -123,8 +123,6 @@ import { useComposerMultilinePrompt } from "./useComposerMultilinePrompt"; import { type QueuedAttachmentContextBinding, reconcileAttachmentContextReferences, - recordChiplessQueuedImageRemoval, - recordQueuedImageHistoryRevision, useRetainedContextPayloads, } from "./composerContextUndo"; import { ComposerBanner } from "./ComposerBanner"; @@ -170,7 +168,6 @@ import { import { type ComposerCitationCommentRequest, type ComposerPromptEditorHandle, - type ComposerPromptHistoryChange, ComposerPromptEditorTiptap as ComposerPromptEditor, } from "../ComposerPromptEditorTiptap"; import { @@ -1472,7 +1469,6 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) }; const copyComposerSnapshotStable = useStableCallback(copyComposerSnapshot); const buildContextClipboardFragmentStable = useStableCallback(buildContextClipboardFragment); - const retainedContextPayloads = useRetainedContextPayloads(composerDraftTargetKey); const { loadThreadTranscript, countReservedAttachments, @@ -1498,42 +1494,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) pendingThreadImportsRef, addComposerFilesToDraft, removedQueuedAttachments: activeRemovedEditingQueuedAttachments, - onRestoreQueuedAttachment: (attachment) => { - if (!queuedEditOwnsComposer) return; - if (isQueuedImageAttachment(attachment)) { - retainedContextPayloads.queuedImageHistory.pendingRestoredIds.add(attachment.id); - } - onRestoreEditingQueuedAttachment?.(attachment); - }, - }); - - const onControlledHistoryChangeStable = useStableCallback( - (history: ComposerPromptHistoryChange) => { - if (!queuedEditOwnsComposer) return; - recordQueuedImageHistoryRevision( - retainedContextPayloads.queuedImageHistory, - history, - activeRemovedEditingQueuedAttachments ?? [], - ); - }, - ); - - const onRemoveEditingQueuedAttachmentStable = useStableCallback((attachmentId: string) => { - const entry = activeEditingQueuedAttachments?.find( - (item) => item.attachment.id === attachmentId, - ); - if ( - entry && - isQueuedImageAttachment(entry.attachment) && - entry.contextIds && - entry.contextIds.length > 0 - ) { - const referenced = new Set(collectInlineContextIds(promptRef.current)); - if (!entry.contextIds.some((contextId) => referenced.has(contextId))) { - recordChiplessQueuedImageRemoval(retainedContextPayloads.queuedImageHistory, attachmentId); - } - } - onRemoveEditingQueuedAttachment(attachmentId); + onRestoreQueuedAttachment: onRestoreEditingQueuedAttachment, }); // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- keeps the shared prompt ref in step with the committed draft prompt for imperative handlers @@ -1730,6 +1691,8 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) syncComposerDraftPersistedAttachments, ]); + const retainedContextPayloads = useRetainedContextPayloads(composerDraftTargetKey); + const onPromptChange = ( nextPrompt: string, nextCursor: number, @@ -1737,7 +1700,6 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) cursorAdjacentToMention: boolean, contextIds: string[], textChanged: boolean, - history?: ComposerPromptHistoryChange, ) => { refreshWorkspaceSnapshotOnTyping(); const trigger = cursorAdjacentToMention @@ -1855,8 +1817,6 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) }), ), removedQueuedAttachments: activeRemovedEditingQueuedAttachments ?? [], - history, - textChanged, images: composerImages, previewAnnotations: composerPreviewAnnotations, retained: retainedContextPayloads, @@ -3919,7 +3879,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) activeEditingQueuedAttachments.length > 0 && ( )} @@ -3942,7 +3902,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) ) : null} ; -}) { - let clock = 0; - let previous: ComposerChangeKind | null = null; - const retained = retention(); - const original = input.attachments; - let live = [...original]; - let removed: QueuedAttachmentContextBinding[] = []; - - const docFor = (text: string) => - schema.node("doc", null, [schema.node("paragraph", null, text ? [schema.text(text)] : [])]); - - let state = EditorState.create({ - schema, - doc: docFor(input.initialText), - plugins: [ - history({ newGroupDelay: COMPOSER_UNDO_GROUP_DELAY }), - createComposerHistoryRevisionPlugin(), - ], - }); - - const referencedIn = (text: string): Set => { - const ids = new Set(); - for (const entry of original) { - for (const contextId of entry.contextIds) { - if (text.includes(`<<${contextId}>>`)) ids.add(contextId); - } - } - return ids; - }; - - const applyAttachmentChanges = ( - result: ReturnType, - ) => { - for (const attachmentId of result.queuedAttachmentIdsToRemove) { - const removedEntry = live.find((entry) => entry.attachment.id === attachmentId); - if (!removedEntry) continue; - live = live.filter((entry) => entry.attachment.id !== attachmentId); - removed.push(removedEntry); - } - for (const restoredAttachment of result.queuedAttachmentsToRestore) { - const restoredEntry = removed.find((entry) => entry.attachment.id === restoredAttachment.id); - if (!restoredEntry) continue; - removed = removed.filter((entry) => entry.attachment.id !== restoredAttachment.id); - live = original.filter( - (entry) => - live.some((item) => item.attachment.id === entry.attachment.id) || - entry.attachment.id === restoredAttachment.id, - ); - } - }; - - const dispatchUser = (tr: Transaction) => { - clock += 80; - const prevText = state.doc.textContent; - tr.setTime(clock); - previous = groupUndoByChangeKind(tr, previous); - state = state.apply(tr); - const nextText = state.doc.textContent; - const result = reconcileAttachmentContextReferences({ - referencedContextIds: referencedIn(nextText), - files: [], - queuedAttachments: live, - removedQueuedAttachments: removed, - history: composerHistoryRevisionOf(state), - textChanged: prevText !== nextText, - images: [], - previewAnnotations: [], - retained, - }); - applyAttachmentChanges(result); - }; - - const dispatchControlledRewrite = (nextText: string) => { - clock += 80; - const tr = state.tr.replaceWith( - 0, - state.doc.content.size, - schema.node("paragraph", null, nextText ? [schema.text(nextText)] : []), - ); - tr.setTime(clock); - previous = groupUndoByChangeKind(tr, previous); - state = state.apply(tr); - state = state.apply(state.tr.setSelection(TextSelection.atEnd(state.doc))); - recordQueuedImageHistoryRevision( - retained.queuedImageHistory, - composerHistoryRevisionOf(state), - removed, - ); - }; - - recordQueuedImageHistoryRevision( - retained.queuedImageHistory, - composerHistoryRevisionOf(state), - removed, - ); - state = state.apply(state.tr.setSelection(TextSelection.atEnd(state.doc))); - - return { - text: () => state.doc.textContent, - liveIds: () => live.map((entry) => entry.attachment.id), - clickRowRemove: (attachmentId: string) => { - const entry = live.find((item) => item.attachment.id === attachmentId); - if (!entry) return; - const currentText = state.doc.textContent; - let nextText = currentText; - for (const contextId of entry.contextIds) { - nextText = nextText.replaceAll(`<<${contextId}>>`, ""); - } - live = live.filter((item) => item.attachment.id !== attachmentId); - removed = [...removed, entry]; - if (nextText !== currentText) { - dispatchControlledRewrite(nextText); - } else { - recordChiplessQueuedImageRemoval(retained.queuedImageHistory, attachmentId); - } - }, - pasteQueuedImageChip: (attachmentId: string, contextId: ComposerContextId) => { - const restoredEntry = removed.find((item) => item.attachment.id === attachmentId); - if (restoredEntry) { - retained.queuedImageHistory.pendingRestoredIds.add(attachmentId); - } - dispatchUser(markAsClipboardEdit(state.tr.insertText(`<<${contextId}>>`), "paste")); - if (restoredEntry) { - removed = removed.filter((item) => item.attachment.id !== attachmentId); - live = original.filter( - (entry) => - live.some((item) => item.attachment.id === entry.attachment.id) || - entry.attachment.id === attachmentId, - ); - } - }, - deleteChipText: (contextId: ComposerContextId) => { - const token = `<<${contextId}>>`; - const idx = state.doc.textContent.indexOf(token); - if (idx === -1) throw new Error(`Chip ${token} not found in "${state.doc.textContent}"`); - dispatchUser(state.tr.delete(idx + 1, idx + 1 + token.length)); - }, - typeGroup: (s: string) => { - clock += COMPOSER_UNDO_GROUP_DELAY + 100; - dispatchUser(state.tr.insertText(s, state.doc.content.size - 1)); - }, - undo: () => { - expect(undo(state, dispatchUser)).toBe(true); - }, - redo: () => { - expect(redo(state, dispatchUser)).toBe(true); - }, - }; + return { files: new Map(), previewAnnotations: new Map() }; } describe("reconcileAttachmentContextReferences", () => { @@ -270,113 +93,41 @@ describe("reconcileAttachmentContextReferences", () => { ]); }); - it("reconciles queued files and images across keyboard deletion, row removal, paste, undo, redo, branching, and history trimming", () => { - const chipFile: ChatFileAttachment = { - type: "file", - id: ChatAttachmentId.make("server-file-1"), - name: "notes.txt", - mimeType: "text/plain", - sizeBytes: 12, + it("drops a queued file with its chip, keeps a queued image, and restores either on undo", () => { + const file = { + attachment: { + type: "file" as const, + id: ChatAttachmentId.make("server-file"), + name: "notes.txt", + mimeType: "text/plain", + sizeBytes: 12, + }, + contextIds: [ComposerContextId.make("file_local")], }; - const plainFile: ChatFileAttachment = { - type: "file", - id: ChatAttachmentId.make("server-file-2"), - name: "legacy.pdf", - mimeType: "application/pdf", - sizeBytes: 40, + const queuedImage = { + attachment: { + type: "image" as const, + id: ChatAttachmentId.make("server-image"), + name: "image.png", + mimeType: "image/png", + sizeBytes: 67, + }, + contextIds: [ComposerContextId.make("image_local")], }; - const imageA: ChatImageAttachment = { - type: "image", - id: ChatAttachmentId.make("server-image-a"), - name: "a.png", - mimeType: "image/png", - sizeBytes: 67, - }; - const imageB: ChatImageAttachment = { - type: "image", - id: ChatAttachmentId.make("server-image-b"), - name: "b.png", - mimeType: "image/png", - sizeBytes: 68, - }; - const chipContextId = ComposerContextId.make("file_local-1"); - const imageAContextId = ComposerContextId.make("image_local-a"); - const imageBContextId = ComposerContextId.make("image_local-b"); - - // Deleting a chip-backed file removes it; unreferenced files and backspaced image chips stay attached. - const basic = createQueuedEditHarness({ - initialText: `Check <<${chipContextId}>> <<${imageAContextId}>>`, - attachments: [ - { attachment: chipFile, contextIds: [chipContextId] }, - { attachment: plainFile, contextIds: [] }, - { attachment: imageA, contextIds: [imageAContextId] }, - ], - }); - basic.deleteChipText(chipContextId); - expect(basic.liveIds()).toEqual([plainFile.id, imageA.id]); - basic.undo(); - expect(basic.liveIds()).toEqual([chipFile.id, plainFile.id, imageA.id]); - basic.redo(); - expect(basic.liveIds()).toEqual([plainFile.id, imageA.id]); - - // × -> Paste -> Backspace -> Undo x3 -> Redo x3 keeps image A attached after the final redo. - basic.clickRowRemove(imageA.id); - expect(basic.liveIds()).toEqual([plainFile.id]); - basic.pasteQueuedImageChip(imageA.id, imageAContextId); - expect(basic.liveIds()).toEqual([plainFile.id, imageA.id]); - basic.deleteChipText(imageAContextId); - expect(basic.liveIds()).toEqual([plainFile.id, imageA.id]); - basic.undo(); - expect(basic.liveIds()).toEqual([plainFile.id, imageA.id]); - basic.undo(); - expect(basic.liveIds()).toEqual([plainFile.id]); - basic.undo(); - expect(basic.liveIds()).toEqual([plainFile.id, imageA.id]); - basic.redo(); - expect(basic.liveIds()).toEqual([plainFile.id]); - basic.redo(); - expect(basic.liveIds()).toEqual([plainFile.id, imageA.id]); - basic.redo(); - expect(basic.liveIds()).toEqual([plainFile.id, imageA.id]); + const base = { files: [], images: [], previewAnnotations: [], retained: retention() }; - // Branching via a controlled row removal: × on A -> Undo -> × on B -> Backspace A -> Undo -> Redo - // must keep A attached and B removed. - const branched = createQueuedEditHarness({ - initialText: `Compare <<${imageAContextId}>> <<${imageBContextId}>>`, - attachments: [ - { attachment: imageA, contextIds: [imageAContextId] }, - { attachment: imageB, contextIds: [imageBContextId] }, - ], + const deleted = reconcileAttachmentContextReferences({ + ...base, + referencedContextIds: new Set(), + queuedAttachments: [file, queuedImage], }); - branched.clickRowRemove(imageA.id); - expect(branched.liveIds()).toEqual([imageB.id]); - branched.undo(); - expect(branched.liveIds()).toEqual([imageA.id, imageB.id]); - branched.clickRowRemove(imageB.id); - expect(branched.liveIds()).toEqual([imageA.id]); - branched.deleteChipText(imageAContextId); - expect(branched.liveIds()).toEqual([imageA.id]); - branched.undo(); - expect(branched.liveIds()).toEqual([imageA.id]); - branched.redo(); - expect(branched.liveIds()).toEqual([imageA.id]); + expect(deleted.queuedAttachmentIdsToRemove).toEqual([file.attachment.id]); - // Overflow trimming: after 119 undo groups, × (depth 120) -> Paste (trims 121 -> 100) -> Undo - // must still remove image A. - const trimmed = createQueuedEditHarness({ - initialText: `Start <<${imageAContextId}>>`, - attachments: [{ attachment: imageA, contextIds: [imageAContextId] }], + const undone = reconcileAttachmentContextReferences({ + ...base, + referencedContextIds: new Set(["file_local", "image_local"]), + removedQueuedAttachments: [file, queuedImage], }); - for (let i = 0; i < 119; i += 1) { - trimmed.typeGroup("x"); - } - trimmed.clickRowRemove(imageA.id); - expect(trimmed.liveIds()).toEqual([]); - trimmed.pasteQueuedImageChip(imageA.id, imageAContextId); - expect(trimmed.liveIds()).toEqual([imageA.id]); - trimmed.undo(); - expect(trimmed.liveIds()).toEqual([]); - trimmed.redo(); - expect(trimmed.liveIds()).toEqual([imageA.id]); + expect(undone.queuedAttachmentsToRestore).toEqual([file.attachment, queuedImage.attachment]); }); }); diff --git a/apps/web/src/components/chat/composerContextUndo.ts b/apps/web/src/components/chat/composerContextUndo.ts index cdd99556e302..6656fa73c3dc 100644 --- a/apps/web/src/components/chat/composerContextUndo.ts +++ b/apps/web/src/components/chat/composerContextUndo.ts @@ -7,12 +7,11 @@ import type { } from "@t3tools/contracts"; import { useEffect, useState } from "react"; -import type { ComposerHistoryRevision } from "../../composer-undo-grouping"; import type { ComposerFileAttachment, ComposerImageAttachment } from "../../composerDraftStore"; import { fileContextReference, previewAnnotationContextId } from "../../lib/composerContextRecords"; import type { TerminalContextDraft } from "../../lib/terminalContext"; import type { ReviewCommentContext } from "../../reviewCommentContext"; -import { isQueuedFileAttachment, isQueuedImageAttachment } from "./queuedMessageEdit"; +import { isQueuedFileAttachment } from "./queuedMessageEdit"; interface RetainedPreviewAnnotation { annotation: PreviewAnnotationPayload; @@ -24,56 +23,9 @@ export interface QueuedAttachmentContextBinding { readonly contextIds: ReadonlyArray; } -export interface QueuedImageHistoryState { - currentRevision: number; - readonly removedIdsByRevision: Map>; - readonly pendingRestoredIds: Set; -} - export interface RetainedAttachmentContextPayloads { files: Map; previewAnnotations: Map; - queuedImageHistory: QueuedImageHistoryState; -} - -export function createQueuedImageHistoryState(): QueuedImageHistoryState { - return { - currentRevision: 0, - removedIdsByRevision: new Map(), - pendingRestoredIds: new Set(), - }; -} - -export function recordQueuedImageHistoryRevision( - state: QueuedImageHistoryState, - history: ComposerHistoryRevision, - removedQueuedAttachments: ReadonlyArray, -): void { - const active = new Set(history.activeRevisions); - for (const revision of state.removedIdsByRevision.keys()) { - if (!active.has(revision)) state.removedIdsByRevision.delete(revision); - } - const removedImageIds = new Set(); - for (const entry of removedQueuedAttachments) { - if (isQueuedImageAttachment(entry.attachment)) { - removedImageIds.add(entry.attachment.id); - } - } - for (const restoredId of state.pendingRestoredIds) { - removedImageIds.delete(restoredId); - } - state.pendingRestoredIds.clear(); - state.currentRevision = history.revision; - state.removedIdsByRevision.set(history.revision, removedImageIds); -} - -export function recordChiplessQueuedImageRemoval( - state: QueuedImageHistoryState, - attachmentId: string, -): void { - const next = new Set(state.removedIdsByRevision.get(state.currentRevision)); - next.add(attachmentId); - state.removedIdsByRevision.set(state.currentRevision, next); } export interface RetainedContextPayloads extends RetainedAttachmentContextPayloads { @@ -91,7 +43,6 @@ function createRetainedContextPayloads(): RetainedContextPayloads { environments: new Map(), files: new Map(), previewAnnotations: new Map(), - queuedImageHistory: createQueuedImageHistoryState(), }; } @@ -108,14 +59,7 @@ export function useRetainedContextPayloads(draftTargetKey: string): RetainedCont // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- clears the retained payload maps when the draft target changes or the composer unmounts useEffect( () => () => { - retained.terminals.clear(); - retained.reviewComments.clear(); - retained.threads.clear(); - retained.environments.clear(); - retained.files.clear(); - retained.previewAnnotations.clear(); - retained.queuedImageHistory.removedIdsByRevision.clear(); - retained.queuedImageHistory.pendingRestoredIds.clear(); + for (const payloads of Object.values(retained)) payloads.clear(); }, [retained], ); @@ -125,10 +69,9 @@ export function useRetainedContextPayloads(draftTargetKey: string): RetainedCont export function reconcileAttachmentContextReferences(input: { referencedContextIds: ReadonlySet; files: ReadonlyArray; + /** Attachments of a queued message being edited, and those removed during the edit. */ queuedAttachments?: ReadonlyArray; removedQueuedAttachments?: ReadonlyArray; - history?: ComposerHistoryRevision | undefined; - textChanged?: boolean; images: ReadonlyArray; previewAnnotations: ReadonlyArray; retained: RetainedAttachmentContextPayloads; @@ -156,56 +99,17 @@ export function reconcileAttachmentContextReferences(input: { return file ? [file] : []; }); - const history = input.history; - const removedQueuedAttachments = input.removedQueuedAttachments ?? []; - if (history) { - if (history.direction === null && input.textChanged) { - recordQueuedImageHistoryRevision( - input.retained.queuedImageHistory, - history, - removedQueuedAttachments, - ); - } else if (history.direction !== null) { - input.retained.queuedImageHistory.currentRevision = history.revision; - } - } - - const removedAtRevision = - history?.direction != null - ? input.retained.queuedImageHistory.removedIdsByRevision.get(history.revision) - : undefined; - - const liveQueuedAttachments = input.queuedAttachments ?? []; - const liveQueuedAttachmentIds = new Set( - liveQueuedAttachments.map((entry) => entry.attachment.id), + const isReferenced = (entry: QueuedAttachmentContextBinding) => + entry.contextIds.some((contextId) => input.referencedContextIds.has(contextId)); + // Like draft images, a queued image stays attached when only its chip is deleted. + const queuedAttachmentIdsToRemove = (input.queuedAttachments ?? []).flatMap((entry) => + isQueuedFileAttachment(entry.attachment) && entry.contextIds.length > 0 && !isReferenced(entry) + ? [entry.attachment.id] + : [], + ); + const queuedAttachmentsToRestore = (input.removedQueuedAttachments ?? []).flatMap((entry) => + isReferenced(entry) ? [entry.attachment] : [], ); - const queuedAttachmentIdsToRemove: string[] = []; - for (const entry of liveQueuedAttachments) { - if (entry.contextIds.length === 0) continue; - if (entry.contextIds.some((contextId) => input.referencedContextIds.has(contextId))) continue; - if (isQueuedFileAttachment(entry.attachment)) { - queuedAttachmentIdsToRemove.push(entry.attachment.id); - } else if ( - isQueuedImageAttachment(entry.attachment) && - removedAtRevision?.has(entry.attachment.id) - ) { - queuedAttachmentIdsToRemove.push(entry.attachment.id); - } - } - const restoredQueuedAttachmentIds = new Set(); - const queuedAttachmentsToRestore: ChatAttachment[] = []; - for (const entry of removedQueuedAttachments) { - if ( - entry.contextIds.length === 0 || - liveQueuedAttachmentIds.has(entry.attachment.id) || - restoredQueuedAttachmentIds.has(entry.attachment.id) - ) { - continue; - } - if (!entry.contextIds.some((contextId) => input.referencedContextIds.has(contextId))) continue; - restoredQueuedAttachmentIds.add(entry.attachment.id); - queuedAttachmentsToRestore.push(entry.attachment); - } const imagesById = new Map(input.images.map((image) => [image.id, image])); const liveAnnotationContextIds = new Set( diff --git a/apps/web/src/components/chat/queuedMessageEdit.test.ts b/apps/web/src/components/chat/queuedMessageEdit.test.ts index 29a36aa7911e..0ebfdacdbb60 100644 --- a/apps/web/src/components/chat/queuedMessageEdit.test.ts +++ b/apps/web/src/components/chat/queuedMessageEdit.test.ts @@ -19,7 +19,6 @@ import { readAttachmentUpload, useAttachmentUploadStore } from "../../lib/attach import { environmentContextRecord, responseContextRecord } from "../../lib/composerContextRecords"; import { formatInlineContextReference } from "../../lib/composerContextReferences"; import { - attachmentContextIdsForRecord, matchRestorableQueuedAttachment, prepareQueuedEditAttachments, recoverQueuedMessageEdit, @@ -236,64 +235,6 @@ describe("queued message file edits", () => { expect(store.getComposerDraft(threadTarget)).toBeNull(); }); - it("maps existing queued attachments to matching wire context ids", () => { - const imageId = ChatAttachmentId.make("server-image-1"); - const fileId = ChatAttachmentId.make("server-file-1"); - const otherFileId = ChatAttachmentId.make("server-file-2"); - const imageContextId = ComposerContextId.make("image_local-1"); - const fileContextId = ComposerContextId.make("file_local-1"); - const imageAttachment = { - type: "image" as const, - id: imageId, - name: "image.png", - mimeType: "image/png", - sizeBytes: 67, - }; - const fileAttachment = { - type: "file" as const, - id: fileId, - name: "notes.txt", - mimeType: "text/plain", - sizeBytes: 12, - }; - const unreferencedFile = { - type: "file" as const, - id: otherFileId, - name: "legacy.pdf", - mimeType: "application/pdf", - sizeBytes: 40, - }; - const context = { - version: 1 as const, - records: [ - { - version: 1 as const, - kind: "image" as const, - contextId: imageContextId, - label: "image.png", - attachmentId: imageId, - name: "image.png", - mimeType: "image/png", - sizeBytes: 67, - }, - { - version: 1 as const, - kind: "file" as const, - contextId: fileContextId, - label: "notes.txt", - attachmentId: fileId, - name: "notes.txt", - mimeType: "text/plain", - sizeBytes: 12, - }, - ], - }; - - expect(attachmentContextIdsForRecord(context, imageAttachment)).toEqual([imageContextId]); - expect(attachmentContextIdsForRecord(context, fileAttachment)).toEqual([fileContextId]); - expect(attachmentContextIdsForRecord(context, unreferencedFile)).toEqual([]); - }); - it("matches a removed queued attachment for same-environment paste while rejecting other environments or payloads", () => { const fileId = ChatAttachmentId.make("server-file-1"); const contextId = ComposerContextId.make("file_local-1"); diff --git a/apps/web/src/components/chat/useQueuedRunEdit.ts b/apps/web/src/components/chat/useQueuedRunEdit.ts index 89f2be2acfbf..a33fd9177635 100644 --- a/apps/web/src/components/chat/useQueuedRunEdit.ts +++ b/apps/web/src/components/chat/useQueuedRunEdit.ts @@ -373,15 +373,10 @@ export function useQueuedRunEdit(input: { focusComposerSoon(); }); const removeEditingAttachment = useStableCallback((attachmentId: string) => { - const contextIds = - editing === null - ? [] - : (() => { - const removed = editing.existingAttachments.find( - (attachment) => attachment.id === attachmentId, - ); - return removed ? attachmentContextIdsForRecord(editing.context, removed) : []; - })(); + const removed = editing?.existingAttachments.find( + (attachment) => attachment.id === attachmentId, + ); + const contextIds = removed ? attachmentContextIdsForRecord(editing?.context, removed) : []; queuedRunEdits.update(editKey, (current) => ({ ...current, existingAttachments: current.existingAttachments.filter( @@ -401,22 +396,14 @@ export function useQueuedRunEdit(input: { }); const restoreEditingAttachment = useStableCallback((attachment: ChatAttachment) => { queuedRunEdits.update(editKey, (current) => { - if (current.existingAttachments.some((entry) => entry.id === attachment.id)) { - return current; - } - const restoredIds = new Set([ + const keptIds = new Set([ ...current.existingAttachments.map((entry) => entry.id), attachment.id, ]); - const existingAttachments = current.originalAttachments.filter((entry) => - restoredIds.has(entry.id), - ); + // Filtering the originals keeps the restored attachment in its original position. return { ...current, - existingAttachments: - existingAttachments.length > current.existingAttachments.length - ? existingAttachments - : [...current.existingAttachments, attachment], + existingAttachments: current.originalAttachments.filter((entry) => keptIds.has(entry.id)), }; }); }); diff --git a/apps/web/src/composer-undo-grouping.ts b/apps/web/src/composer-undo-grouping.ts index ba5aaa4cb7e8..708b4f41fe52 100644 --- a/apps/web/src/composer-undo-grouping.ts +++ b/apps/web/src/composer-undo-grouping.ts @@ -1,5 +1,5 @@ -import { closeHistory, isHistoryTransaction, redoDepth, undoDepth } from "@tiptap/pm/history"; -import { type EditorState, Plugin, PluginKey, type Transaction } from "@tiptap/pm/state"; +import { closeHistory, isHistoryTransaction } from "@tiptap/pm/history"; +import type { Transaction } from "@tiptap/pm/state"; import { ReplaceStep } from "@tiptap/pm/transform"; // Whole-draft rewrites make all edits adjacent; explicit boundaries keep change kinds separate. @@ -7,122 +7,6 @@ export const COMPOSER_UNDO_GROUP_DELAY = 1000; export type ComposerChangeKind = "insert" | "delete" | "other"; -export interface ComposerHistoryRevision { - readonly revision: number; - readonly activeRevisions: ReadonlyArray; - readonly direction: "undo" | "redo" | null; -} - -interface ComposerHistoryRevisionState { - readonly current: number; - readonly nextId: number; - readonly undo: ReadonlyArray; - readonly redo: ReadonlyArray; - readonly direction: "undo" | "redo" | null; -} - -const composerHistoryRevisionPluginKey = new PluginKey( - "composerHistoryRevision", -); - -/** - * Mirrors ProseMirror's undo/redo event stacks with monotonic revision IDs so - * external state stays accurate across controlled rewrites and history overflow trimming. - * Register after the ProseMirror history plugin. - */ -export function createComposerHistoryRevisionPlugin(): Plugin { - return new Plugin({ - key: composerHistoryRevisionPluginKey, - state: { - init(): ComposerHistoryRevisionState { - return { current: 0, nextId: 1, undo: [], redo: [], direction: null }; - }, - apply(tr, prev, oldState, newState): ComposerHistoryRevisionState { - const prevUndoDepth = Number(undoDepth(oldState)); - const prevRedoDepth = Number(redoDepth(oldState)); - const nextUndoDepth = Number(undoDepth(newState)); - const nextRedoDepth = Number(redoDepth(newState)); - - if (isHistoryTransaction(tr)) { - if (nextUndoDepth < prevUndoDepth) { - const landed = prev.undo.at(-1) ?? prev.current; - const nextRedo = [...prev.redo, prev.current]; - return { - current: landed, - nextId: prev.nextId, - undo: prev.undo.slice(0, -1), - redo: - nextRedo.length > nextRedoDepth - ? nextRedo.slice(nextRedo.length - nextRedoDepth) - : nextRedo, - direction: "undo", - }; - } - if (nextRedoDepth < prevRedoDepth) { - const landed = prev.redo.at(-1) ?? prev.current; - const nextUndo = [...prev.undo, prev.current]; - return { - current: landed, - nextId: prev.nextId, - undo: - nextUndo.length > nextUndoDepth - ? nextUndo.slice(nextUndo.length - nextUndoDepth) - : nextUndo, - redo: prev.redo.slice(0, -1), - direction: "redo", - }; - } - return { ...prev, direction: null }; - } - - const appended = tr.getMeta("appendedTransaction") as Transaction | undefined; - if ( - tr.steps.length === 0 || - tr.getMeta("addToHistory") === false || - appended?.getMeta("addToHistory") === false || - (appended && isHistoryTransaction(appended)) - ) { - return { ...prev, direction: null }; - } - - if (nextUndoDepth !== prevUndoDepth) { - const nextUndo = [...prev.undo, prev.current]; - return { - current: prev.nextId, - nextId: prev.nextId + 1, - undo: - nextUndo.length > nextUndoDepth - ? nextUndo.slice(nextUndo.length - nextUndoDepth) - : nextUndo, - redo: [], - direction: null, - }; - } - - return { - current: prev.current, - nextId: prev.nextId, - undo: prev.undo, - redo: [], - direction: null, - }; - }, - }, - }); -} - -export function composerHistoryRevisionOf(state: EditorState): ComposerHistoryRevision { - const revisionState = composerHistoryRevisionPluginKey.getState(state); - if (!revisionState) { - return { revision: 0, activeRevisions: [0], direction: null }; - } - return { - revision: revisionState.current, - activeRevisions: [...revisionState.undo, revisionState.current, ...revisionState.redo], - direction: revisionState.direction, - }; -} - const CLIPBOARD_EDIT_META = "composerClipboardEdit"; const COMPOSITION_REPLACE_META = "composerCompositionReplace"; From bd0f1995b3c92b912ef2692c9fcb446dd167cd64 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sun, 22 Nov 2026 18:16:22 +0000 Subject: [PATCH 14/14] fix(web): redo of a queued image removal takes the image off the shelf again Undo of a queued image's removal brings the image back with its chip. Redo now removes it again: the editor reports undo/redo changes, and an image that returned with its chip leaves when undo or redo takes the chip away. Deleting that chip by hand still keeps the image attached. Co-Authored-By: Claude Opus 5.5 --- .../components/ComposerPromptEditorTiptap.tsx | 3 ++ apps/web/src/components/chat/ChatComposer.tsx | 2 ++ .../chat/composerContextUndo.test.ts | 27 +++++++++++++++-- .../components/chat/composerContextUndo.ts | 30 ++++++++++++------- 4 files changed, 50 insertions(+), 12 deletions(-) diff --git a/apps/web/src/components/ComposerPromptEditorTiptap.tsx b/apps/web/src/components/ComposerPromptEditorTiptap.tsx index 402156b00128..634b7a9389e9 100644 --- a/apps/web/src/components/ComposerPromptEditorTiptap.tsx +++ b/apps/web/src/components/ComposerPromptEditorTiptap.tsx @@ -203,6 +203,8 @@ export interface ComposerPromptEditorProps { contextIds: string[], /** False when only the caret or selection moved. */ textChanged: boolean, + /** True when the change came from undo or redo. */ + fromHistory?: boolean, ) => void; onVisibleSelectionChange?: () => void; onCommandKeyDown?: (key: string, event: KeyboardEvent, isTaskItem?: boolean) => boolean; @@ -1127,6 +1129,7 @@ function ComposerPromptEditorTiptapInner( suppressTrigger, contextIds, previousSnapshot.value !== nextValue, + isHistoryTransaction(transaction), ); }; diff --git a/apps/web/src/components/chat/ChatComposer.tsx b/apps/web/src/components/chat/ChatComposer.tsx index 58f95b2d3f65..85a7f6173bf9 100644 --- a/apps/web/src/components/chat/ChatComposer.tsx +++ b/apps/web/src/components/chat/ChatComposer.tsx @@ -1700,6 +1700,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) cursorAdjacentToMention: boolean, contextIds: string[], textChanged: boolean, + fromHistory = false, ) => { refreshWorkspaceSnapshotOnTyping(); const trigger = cursorAdjacentToMention @@ -1817,6 +1818,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) }), ), removedQueuedAttachments: activeRemovedEditingQueuedAttachments ?? [], + fromHistory, images: composerImages, previewAnnotations: composerPreviewAnnotations, retained: retainedContextPayloads, diff --git a/apps/web/src/components/chat/composerContextUndo.test.ts b/apps/web/src/components/chat/composerContextUndo.test.ts index be938f599c9a..c493aa2dfc8f 100644 --- a/apps/web/src/components/chat/composerContextUndo.test.ts +++ b/apps/web/src/components/chat/composerContextUndo.test.ts @@ -41,7 +41,7 @@ const annotation = { } satisfies PreviewAnnotationPayload; function retention(): RetainedAttachmentContextPayloads { - return { files: new Map(), previewAnnotations: new Map() }; + return { files: new Map(), previewAnnotations: new Map(), restoredQueuedImageIds: new Set() }; } describe("reconcileAttachmentContextReferences", () => { @@ -93,7 +93,7 @@ describe("reconcileAttachmentContextReferences", () => { ]); }); - it("drops a queued file with its chip, keeps a queued image, and restores either on undo", () => { + it("drops a queued file with its chip, keeps a queued image, and lets undo and redo restore and remove either", () => { const file = { attachment: { type: "file" as const, @@ -129,5 +129,28 @@ describe("reconcileAttachmentContextReferences", () => { removedQueuedAttachments: [file, queuedImage], }); expect(undone.queuedAttachmentsToRestore).toEqual([file.attachment, queuedImage.attachment]); + + const redone = reconcileAttachmentContextReferences({ + ...base, + referencedContextIds: new Set(), + queuedAttachments: [file, queuedImage], + fromHistory: true, + }); + expect(redone.queuedAttachmentIdsToRemove).toEqual([ + file.attachment.id, + queuedImage.attachment.id, + ]); + + reconcileAttachmentContextReferences({ + ...base, + referencedContextIds: new Set(["image_local"]), + removedQueuedAttachments: [queuedImage], + }); + const backspaced = reconcileAttachmentContextReferences({ + ...base, + referencedContextIds: new Set(), + queuedAttachments: [queuedImage], + }); + expect(backspaced.queuedAttachmentIdsToRemove).toEqual([]); }); }); diff --git a/apps/web/src/components/chat/composerContextUndo.ts b/apps/web/src/components/chat/composerContextUndo.ts index 6656fa73c3dc..4c96ec12b70f 100644 --- a/apps/web/src/components/chat/composerContextUndo.ts +++ b/apps/web/src/components/chat/composerContextUndo.ts @@ -11,7 +11,7 @@ import type { ComposerFileAttachment, ComposerImageAttachment } from "../../comp import { fileContextReference, previewAnnotationContextId } from "../../lib/composerContextRecords"; import type { TerminalContextDraft } from "../../lib/terminalContext"; import type { ReviewCommentContext } from "../../reviewCommentContext"; -import { isQueuedFileAttachment } from "./queuedMessageEdit"; +import { isQueuedFileAttachment, isQueuedImageAttachment } from "./queuedMessageEdit"; interface RetainedPreviewAnnotation { annotation: PreviewAnnotationPayload; @@ -26,6 +26,8 @@ export interface QueuedAttachmentContextBinding { export interface RetainedAttachmentContextPayloads { files: Map; previewAnnotations: Map; + /** Queued images brought back because their chip reappeared. */ + restoredQueuedImageIds: Set; } export interface RetainedContextPayloads extends RetainedAttachmentContextPayloads { @@ -43,6 +45,7 @@ function createRetainedContextPayloads(): RetainedContextPayloads { environments: new Map(), files: new Map(), previewAnnotations: new Map(), + restoredQueuedImageIds: new Set(), }; } @@ -72,6 +75,8 @@ export function reconcileAttachmentContextReferences(input: { /** Attachments of a queued message being edited, and those removed during the edit. */ queuedAttachments?: ReadonlyArray; removedQueuedAttachments?: ReadonlyArray; + /** The change came from undo or redo. */ + fromHistory?: boolean; images: ReadonlyArray; previewAnnotations: ReadonlyArray; retained: RetainedAttachmentContextPayloads; @@ -101,15 +106,20 @@ export function reconcileAttachmentContextReferences(input: { const isReferenced = (entry: QueuedAttachmentContextBinding) => entry.contextIds.some((contextId) => input.referencedContextIds.has(contextId)); - // Like draft images, a queued image stays attached when only its chip is deleted. - const queuedAttachmentIdsToRemove = (input.queuedAttachments ?? []).flatMap((entry) => - isQueuedFileAttachment(entry.attachment) && entry.contextIds.length > 0 && !isReferenced(entry) - ? [entry.attachment.id] - : [], - ); - const queuedAttachmentsToRestore = (input.removedQueuedAttachments ?? []).flatMap((entry) => - isReferenced(entry) ? [entry.attachment] : [], - ); + const restoredImageIds = input.retained.restoredQueuedImageIds; + const queuedAttachmentIdsToRemove = (input.queuedAttachments ?? []).flatMap((entry) => { + if (entry.contextIds.length === 0 || isReferenced(entry)) return []; + if (isQueuedFileAttachment(entry.attachment)) return [entry.attachment.id]; + // Like draft images, a queued image outlives its deleted chip, unless undo or redo + // takes away a chip whose return had brought the image back. + const wasRestored = restoredImageIds.delete(entry.attachment.id); + return wasRestored && input.fromHistory ? [entry.attachment.id] : []; + }); + const queuedAttachmentsToRestore = (input.removedQueuedAttachments ?? []).flatMap((entry) => { + if (!isReferenced(entry)) return []; + if (isQueuedImageAttachment(entry.attachment)) restoredImageIds.add(entry.attachment.id); + return [entry.attachment]; + }); const imagesById = new Map(input.images.map((image) => [image.id, image])); const liveAnnotationContextIds = new Set(