From 64044628436bc404dbdf0e7a4c552a8ad468d1ca Mon Sep 17 00:00:00 2001 From: "paseo-bot[bot]" <266920839+paseo-bot[bot]@users.noreply.github.com> Date: Mon, 30 Nov 2026 14:28:30 +0000 Subject: [PATCH] fix(app): preserve existing drafts during rewind Restore the rewound prompt as one draft so its text or attachments cannot mix with a prompt already in the composer. --- .../browser/rewind-menu.ui-contract.spec.ts | 53 ++++++++++++++++++ .../rewind/composer-restore.test.ts | 54 +++++-------------- .../components/rewind/composer-restore.tsx | 54 +++++-------------- .../app/src/composer/draft/input-draft.ts | 12 +++++ packages/app/src/panels/agent-panel.tsx | 7 +-- packages/app/src/stores/draft-store/index.ts | 13 +++++ 6 files changed, 107 insertions(+), 86 deletions(-) diff --git a/packages/app/e2e/browser/rewind-menu.ui-contract.spec.ts b/packages/app/e2e/browser/rewind-menu.ui-contract.spec.ts index 5d2b2ffe54..a47a620e45 100644 --- a/packages/app/e2e/browser/rewind-menu.ui-contract.spec.ts +++ b/packages/app/e2e/browser/rewind-menu.ui-contract.spec.ts @@ -469,6 +469,59 @@ test.describe("Rewind sheet", () => { } }); + const existingDrafts = [ + { + name: "typed text", + text: "Keep this draft.", + fileCount: 0, + prepare: (page: Page) => fillComposerDraft(page, "Keep this draft."), + }, + { + name: "an uploaded file", + text: "", + fileCount: 1, + prepare: async (page: Page) => { + await attachFileFromMenu(page, { + name: "draft.txt", + mimeType: "text/plain", + buffer: Buffer.from("Keep this draft file"), + }); + await expectAttachmentPill(page, "composer-file-attachment-pill"); + }, + }, + ]; + + for (const draft of existingDrafts) { + test(`preserves a draft containing ${draft.name} when rewinding a sent image`, async ({ + page, + }) => { + const prompt = "Review this image before rewind."; + const session = await seedMockAgentWorkspace({ + repoPrefix: "rewind-keeps-draft-e2e-", + title: "Rewind with an existing draft", + }); + try { + await openAgentRoute(page, session); + await expectComposerVisible(page); + await attachComposerTestImage(page); + await expectAttachmentPill(page, "composer-image-attachment-pill"); + await submitMessageWithButton(page, prompt); + await session.client.waitForFinish(session.agentId); + await draft.prepare(page); + + await rewindMessageWithImage(page, prompt); + await expectUserMessageCount(page, 0); + await expectComposerDraft(page, draft.text); + await expect(page.getByTestId("composer-image-attachment-pill")).toHaveCount(0); + await expect(page.getByTestId("composer-file-attachment-pill")).toHaveCount( + draft.fileCount, + ); + } finally { + await session.cleanup(); + } + }); + } + test("surfaces rewind failures without crashing the page", async ({ page }) => { const firstPrompt = "emit 1 coalesced agent stream updates for failed rewind turn."; const rewindError = "No file checkpoint found for message rewind-failure-e2e."; diff --git a/packages/app/src/components/rewind/composer-restore.test.ts b/packages/app/src/components/rewind/composer-restore.test.ts index c2411e089e..50bc32f518 100644 --- a/packages/app/src/components/rewind/composer-restore.test.ts +++ b/packages/app/src/components/rewind/composer-restore.test.ts @@ -1,30 +1,10 @@ import { describe, expect, test } from "vitest"; -import type { AttachmentMetadata, UserComposerAttachment } from "@/attachments/types"; +import type { AttachmentMetadata } from "@/attachments/types"; import type { UploadedFileAttachment } from "@getpaseo/protocol/messages"; import { splitComposerAttachmentsForSubmit } from "@/composer/attachments/submit"; -import { restoreComposerAttachmentsIfEmpty, restoreComposerTextIfEmpty } from "./composer-restore"; +import { composeRewoundAttachments } from "./composer-restore"; import { shouldRestoreComposerForRewindMode } from "./rewind-mode"; -describe("restoreComposerTextIfEmpty", () => { - test("restores the rewound message when the composer is empty", () => { - expect( - restoreComposerTextIfEmpty({ - currentText: "", - rewoundText: "message before rewind", - }), - ).toBe("message before rewind"); - }); - - test("preserves an existing composer draft", () => { - expect( - restoreComposerTextIfEmpty({ - currentText: "keep this draft", - rewoundText: "message before rewind", - }), - ).toBe("keep this draft"); - }); -}); - describe("shouldRestoreComposerForRewindMode", () => { test("restores only conversation-mutating rewind modes", () => { expect(shouldRestoreComposerForRewindMode("conversation")).toBe(true); @@ -50,11 +30,12 @@ const file: UploadedFileAttachment = { path: "/uploads/notes.txt", }; -describe("restoreComposerAttachmentsIfEmpty", () => { +describe("composeRewoundAttachments", () => { test("restores images and uploaded files ready for resubmission", () => { - const restored = restoreComposerAttachmentsIfEmpty({ - currentAttachments: [], - rewoundMessage: { text: "Review these", images: [image], attachments: [file] }, + const restored = composeRewoundAttachments({ + text: "Review these", + images: [image], + attachments: [file], }); expect(restored).toEqual([ { kind: "image", metadata: image }, @@ -66,30 +47,19 @@ describe("restoreComposerAttachmentsIfEmpty", () => { }); }); - test("preserves attachments added to an existing draft", () => { - const draft: UserComposerAttachment[] = [{ kind: "file", attachment: file }]; - expect( - restoreComposerAttachmentsIfEmpty({ - currentAttachments: draft, - rewoundMessage: { text: "", images: [image] }, - }), - ).toBe(draft); - }); - test("restores an image-only message", () => { expect( - restoreComposerAttachmentsIfEmpty({ - currentAttachments: [], - rewoundMessage: { text: "", images: [image] }, + composeRewoundAttachments({ + text: "", + images: [image], }), ).toEqual([{ kind: "image", metadata: image }]); }); test("keeps text-only messages free of attachments", () => { expect( - restoreComposerAttachmentsIfEmpty({ - currentAttachments: [], - rewoundMessage: { text: "hello" }, + composeRewoundAttachments({ + text: "hello", }), ).toEqual([]); }); diff --git a/packages/app/src/components/rewind/composer-restore.tsx b/packages/app/src/components/rewind/composer-restore.tsx index 16b424b3df..6d1ba81009 100644 --- a/packages/app/src/components/rewind/composer-restore.tsx +++ b/packages/app/src/components/rewind/composer-restore.tsx @@ -1,7 +1,6 @@ import type { AttachmentMetadata, UserComposerAttachment } from "@/attachments/types"; import type { AgentInputDraft } from "@/composer/draft/input-draft"; import type { UserMessageItem } from "@/types/stream"; -import type { ComposerTextSource } from "@/composer/text-source"; import { createContext, useCallback, useContext, useMemo, type ReactNode } from "react"; type RewoundMessage = Pick & { @@ -13,36 +12,19 @@ interface RewindComposerRestoreContextValue { } interface RewindComposerRestoreProviderProps { - textSource: ComposerTextSource; - setText: (text: string) => void; - setAttachments: AgentInputDraft["setAttachments"]; + restoreIfEmpty: AgentInputDraft["restoreIfEmpty"]; onRewindComplete: (restoredComposerText: boolean) => void; children: ReactNode; } const RewindComposerRestoreContext = createContext(null); -export function restoreComposerTextIfEmpty(input: { - currentText: string; - rewoundText: string; -}): string { - if (input.currentText.length > 0) { - return input.currentText; - } - return input.rewoundText; -} - -export function restoreComposerAttachmentsIfEmpty(input: { - currentAttachments: UserComposerAttachment[]; - rewoundMessage: RewoundMessage; -}): UserComposerAttachment[] { - if (input.currentAttachments.length > 0) { - return input.currentAttachments; - } - const attachments: UserComposerAttachment[] = (input.rewoundMessage.images ?? []).map( - (metadata) => ({ kind: "image", metadata }), - ); - for (const attachment of input.rewoundMessage.attachments ?? []) { +export function composeRewoundAttachments(message: RewoundMessage): UserComposerAttachment[] { + const attachments: UserComposerAttachment[] = (message.images ?? []).map((metadata) => ({ + kind: "image", + metadata, + })); + for (const attachment of message.attachments ?? []) { if (attachment.type === "uploaded_file") { attachments.push({ kind: "file", attachment }); } @@ -51,29 +33,19 @@ export function restoreComposerAttachmentsIfEmpty(input: { } export function RewindComposerRestoreProvider({ - textSource, - setText, - setAttachments, + restoreIfEmpty, onRewindComplete, children, }: RewindComposerRestoreProviderProps) { const completeRewind = useCallback( (message: RewoundMessage) => { - const currentText = textSource.getSnapshot(); - const nextText = restoreComposerTextIfEmpty({ - currentText: currentText, - rewoundText: message.text, + const restored = restoreIfEmpty({ + text: message.text, + attachments: composeRewoundAttachments(message), }); - const restoredComposerText = nextText !== currentText; - if (restoredComposerText) { - setText(nextText); - } - setAttachments((currentAttachments) => - restoreComposerAttachmentsIfEmpty({ currentAttachments, rewoundMessage: message }), - ); - onRewindComplete(restoredComposerText); + onRewindComplete(restored && message.text.length > 0); }, - [onRewindComplete, setAttachments, setText, textSource], + [onRewindComplete, restoreIfEmpty], ); const value = useMemo(() => ({ completeRewind }), [completeRewind]); diff --git a/packages/app/src/composer/draft/input-draft.ts b/packages/app/src/composer/draft/input-draft.ts index 90e3c34235..885ad23878 100644 --- a/packages/app/src/composer/draft/input-draft.ts +++ b/packages/app/src/composer/draft/input-draft.ts @@ -66,6 +66,7 @@ export interface AgentInputDraft { textSource: ComposerTextSource; editText: (text: string) => void; replaceText: (text: string) => void; + restoreIfEmpty: (draft: DraftInput) => boolean; textReplacement: TextReplacement; attachments: UserComposerAttachment[]; setAttachments: (updater: AttachmentUpdater) => void; @@ -189,6 +190,16 @@ export function useAgentInputDraft(input: UseAgentInputDraftInput): AgentInputDr [draftKey, publishTextReplacement, textPublication], ); + const restoreIfEmpty = useCallback( + (draft: DraftInput) => { + textPublication.flush(); + if (!useDraftStore.getState().restoreDraftInputIfEmpty({ draftKey, draft })) return false; + if (draft.text.length > 0) publishTextReplacement("replace", draft.text); + return true; + }, + [draftKey, publishTextReplacement, textPublication], + ); + const setAttachments = useCallback( (updater: AttachmentUpdater) => { saveDraft((current) => ({ @@ -390,6 +401,7 @@ export function useAgentInputDraft(input: UseAgentInputDraftInput): AgentInputDr textSource, editText, replaceText, + restoreIfEmpty, textReplacement, attachments, setAttachments, diff --git a/packages/app/src/panels/agent-panel.tsx b/packages/app/src/panels/agent-panel.tsx index 891b0bde7d..12acf7b5e7 100644 --- a/packages/app/src/panels/agent-panel.tsx +++ b/packages/app/src/panels/agent-panel.tsx @@ -1189,6 +1189,7 @@ const ChatAgentReadyContent = memo(function ChatAgentReadyContent({ textSource, editText, replaceText, + restoreIfEmpty, textReplacement, attachments, setAttachments, @@ -1202,6 +1203,7 @@ const ChatAgentReadyContent = memo(function ChatAgentReadyContent({ textSource, editText, replaceText, + restoreIfEmpty, textReplacement, attachments, setAttachments, @@ -1214,6 +1216,7 @@ const ChatAgentReadyContent = memo(function ChatAgentReadyContent({ textSource, editText, replaceText, + restoreIfEmpty, textReplacement, attachments, setAttachments, @@ -1335,9 +1338,7 @@ const ChatAgentReadyContent = memo(function ChatAgentReadyContent({ const content = ( {hasAgentComposer ? ( diff --git a/packages/app/src/stores/draft-store/index.ts b/packages/app/src/stores/draft-store/index.ts index c0085bba82..176fd8a888 100644 --- a/packages/app/src/stores/draft-store/index.ts +++ b/packages/app/src/stores/draft-store/index.ts @@ -46,6 +46,7 @@ interface DraftStoreActions { getDraftInput: (draftKey: string) => DraftInput | undefined; hydrateDraftInput: (input: { draftKey: string }) => Promise; saveDraftInput: (input: { draftKey: string; draft: DraftInput }) => void; + restoreDraftInputIfEmpty: (input: { draftKey: string; draft: DraftInput }) => boolean; /** Saves what `update` makes of the current input; an emptied input abandons the draft. */ updateDraftInput: (input: { draftKey: string; @@ -343,6 +344,18 @@ export const useDraftStore = create()( scheduleAttachmentGc(); }, + restoreDraftInputIfEmpty: ({ draftKey, draft }) => { + const store = get(); + const record = store.drafts[draftKey]; + if (record?.lifecycle === "active") { + const current = toDraftInputIfReady(record); + if (!current || hasDraftContent(current)) return false; + } + if (!hasDraftContent(draft)) return false; + store.saveDraftInput({ draftKey, draft }); + return true; + }, + updateDraftInput: ({ draftKey, update }) => { const next = update(get().getDraftInput(draftKey) ?? { text: "", attachments: [] }); if (hasDraftContent(next)) get().saveDraftInput({ draftKey, draft: next });