fix(web): restore reverted messages to composer #6044
Closedamitray007(GitHub profile, opens in a new tab)wants to merge 7 commits into
main fromrefs/pull/6044/headopened View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- apps/web/src/components/ChatView.tsx line 484Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -457,3 +459,18 @@457459 readonly connection: EnvironmentConnectionPresentation;458460 };459461462+interface PendingRevertedMessageRestore {463+ threadId: ThreadId;464+ messageId: MessageId;465+ turnCount: number;466+ knownFailureActivityIds: ReadonlySet<string>;467+ draft: RevertedMessageDraft;468+}469+470+interface RevertedMessageDraft {471+ prompt: string;472+ images: ComposerImageAttachment[];473+ unavailableImageNames: string[];474+}475+476+async function prepareRevertedMessageDraft(message: ChatMessage): Promise<RevertedMessageDraft> {apps/web/src/components/ChatView.tsx line 479OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -457,3 +459,21 @@457459 readonly connection: EnvironmentConnectionPresentation;458460 };459461462+interface PendingRevertedMessageRestore {463+ threadId: ThreadId;464+ messageId: MessageId;465+ turnCount: number;466+ knownFailureActivityIds: ReadonlySet<string>;467+ draft: RevertedMessageDraft;468+}469+470+interface RevertedMessageDraft {471+ prompt: string;472+ images: ComposerImageAttachment[];473+ unavailableImageNames: string[];474+}475+476+async function prepareRevertedMessageDraft(message: ChatMessage): Promise<RevertedMessageDraft> {477+ const prepared = await Promise.all(478+ (message.attachments ?? [])479+ .filter((attachment) => !attachment.name.startsWith("preview-annotation-"))
- apps/web/src/components/ChatView.tsx line 4266Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -3965,0 +4139,21 @@4139+ return;4140+ }4141+4142+ writeRevertedMessageToComposer();4143+ if (hasDraftToStash) {4144+ toastManager.add({4145+ type: "info",4146+ title: "Current draft stashed",4147+ description: "The reverted message is ready to edit in the composer.",4148+ data: { hideCopyButton: true },4149+ });4150+ }4151+ }, [4152+ addComposerDraftImages,4153+ composerDraftTarget,4154+ pendingRevertedMessageRestore,4155+ revertObservedActivities,4156+ revertObservedMessages,4157+ revertObservedThreadId,4158+ setComposerDraftPrompt,4159+ ]);apps/web/src/components/ChatView.tsx line 4234Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -3965,0 +4122,21 @@4122+ };4123+4124+ const currentDraft = useComposerDraftStore.getState().getComposerDraft(composerDraftTarget);4125+ const hasDraftToStash = Boolean(4126+ currentDraft &&4127+ (stripInlineTerminalContextPlaceholders(currentDraft.prompt).trim().length > 0 ||4128+ currentDraft.images.length > 0),4129+ );4130+ if (hasDraftToStash && composerRef.current?.stashCurrentPrompt() !== true) {4131+ writeRevertedMessageToComposer();4132+ toastManager.add({4133+ type: "warning",4134+ title: "Reverted message appended",4135+ description:4136+ "Your current draft could not be stashed, so the reverted message was added after it instead.",4137+ data: { hideCopyButton: true },4138+ });4139+ return;4140+ }4141+4142+ writeRevertedMessageToComposer();apps/web/src/components/ChatView.tsx line 5049OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -4808,7 +5035,15 @@48085035 });4809- if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {4810- const error = squashAtomCommandFailure(result);4811- setThreadError(4812- activeThread.id,4813- error instanceof Error ? error.message : "Failed to revert thread state.",4814- );5036+ if (result._tag === "Failure") {5037+ setPendingRevertedMessageRestore((current) => {5038+ if (current !== pendingRestore) return current;5039+ releaseRevertedMessageDraft(current.draft);5040+ pendingRevertedMessageRestoreRef.current = null;5041+ return null;5042+ });5043+ if (!isAtomCommandInterrupted(result)) {5044+ const error = squashAtomCommandFailure(result);5045+ setThreadError(5046+ threadId,5047+ error instanceof Error ? error.message : "Failed to revert thread state.",5048+ );5049+ }
- apps/web/src/components/ChatView.tsx line 5153Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -4800,2 +5044,19 @@4800- setIsRevertingCheckpoint(true);4801- setThreadError(activeThread.id, null);5044+ setRevertingCheckpointThreadKeys((current) => {5045+ const next = new Set(current);5046+ next.add(sourceThreadKey);5047+ return next;5048+ });5049+ setThreadError(threadId, null);5050+ const draft = await draftPromise;5051+ if (!chatViewMountedRef.current) {5052+ releaseRevertedMessageDraft(draft);5053+ return;5054+ }5055+ if (routeThreadKeyRef.current !== sourceThreadKey) {5056+ releaseRevertedMessageDraft(draft);5057+ setRevertingCheckpointThreadKeys((current) => {5058+ const next = new Set(current);5059+ next.delete(sourceThreadKey);5060+ return next;5061+ });5062+ return;
- apps/web/src/components/ChatView.tsx line 4117OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -3965,0 +4097,21 @@4097+ releaseRevertedMessageDraft(pending.draft);4098+ toastManager.add({4099+ type: "warning",4100+ title: "Message context was not restored",4101+ description:4102+ "The reverted message only contained attached context, which cannot be safely reused after a rewind.",4103+ data: { hideCopyButton: true },4104+ });4105+ return;4106+ }4107+4108+ const writeRevertedMessageToComposer = () => {4109+ const currentDraft = useComposerDraftStore.getState().getComposerDraft(composerDraftTarget);4110+ const existingPrompt = currentDraft?.prompt.trimEnd() ?? "";4111+ const existingText = stripInlineTerminalContextPlaceholders(existingPrompt).trim();4112+ const nextPrompt =4113+ existingPrompt.length > 0 && pending.draft.prompt.length > 04114+ ? `${existingPrompt}${existingText.length > 0 ? "\n\n" : ""}${pending.draft.prompt}`4115+ : existingPrompt || pending.draft.prompt;4116+ setComposerDraftPrompt(composerDraftTarget, nextPrompt);4117+ if (pending.draft.images.length > 0) {
- apps/web/src/components/ChatView.tsx line 5057OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -4803,12 +5044,14 @@5044+ setRevertingCheckpointThreadKeys((current) => {5045+ const next = new Set(current);5046+ next.add(sourceThreadKey);5047+ return next;5048+ });5049+ setThreadError(threadId, null);5050+ const resultPromise = revertThreadCheckpoint({48035051 environmentId,48045052 input: {4805- threadId: activeThread.id,5053+ threadId,48065054 turnCount,48075055 },48085056 });4809- if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {4810- const error = squashAtomCommandFailure(result);4811- setThreadError(4812- activeThread.id,4813- error instanceof Error ? error.message : "Failed to revert thread state.",4814- );5057+ const [draft, result] = await Promise.all([draftPromise, resultPromise]);
- apps/web/src/components/ChatView.tsx line 4082Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -3960,4 +4051,7 @@39604051 // activeThreadRef resets transitively with the active thread.39614052 }, [activeThread?.id]);396240534054+ const revertObservedThreadId = activeThread?.id ?? null;4055+ const revertObservedActivities = activeThread?.activities ?? null;4056+ const revertObservedMessages = activeThread?.messages ?? null;39634057 useEffect(() => {
- apps/web/src/components/ChatView.tsx line 2361Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -2255,4 +2328,9 @@22552328 activePendingUserInput: activePendingUserInput?.requestId ?? null,22562329 threadError,22572330 });2258- const isWorking = phase === "running" || isSendBusy || isConnecting || isRevertingCheckpoint;2331+ const isRevertingCheckpoint = revertingCheckpointThreadKeys.has(routeThreadKey);2332+ const isRestoringRevertedMessage =2333+ pendingRevertedMessageRestore !== null &&2334+ scopedThreadKey(pendingRevertedMessageRestore.threadRef) === routeThreadKey;2335+ const isRevertComposerBusy = isRevertingCheckpoint || isRestoringRevertedMessage;2336+ const isWorking = phase === "running" || isSendBusy || isConnecting || isRevertComposerBusy;
- apps/web/src/components/ChatView.tsx line 5153Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -4839,2 +5133,20 @@5133+ if (chatViewMountedRef.current) {5134+ finishRevert();5135+ if (!isAtomCommandInterrupted(result)) {5136+ const error = squashAtomCommandFailure(result);5137+ setThreadError(5138+ threadId,5139+ error instanceof Error ? error.message : "Failed to revert thread state.",5140+ );5141+ }5142+ }5143+ return;48395144 }4840- setIsRevertingCheckpoint(false);5145+ if (!chatViewMountedRef.current) {5146+ releaseRevertedMessageDraft(draft);5147+ return;5148+ }5149+ if (routeThreadKeyRef.current !== sourceThreadKey) {5150+ releaseRevertedMessageDraft(draft);5151+ finishRevert();5152+ return;
- apps/web/src/components/ChatView.tsx line 4227Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -3989,0 +4206,21 @@4206+ currentDraft.previewAnnotations.length > 0 ||4207+ currentDraft.reviewComments.length > 0),4208+ );4209+ const clearDraftContexts = () => {4210+ setComposerDraftTerminalContexts(composerDraftTarget, []);4211+ setComposerDraftElementContexts(composerDraftTarget, []);4212+ setComposerDraftPreviewAnnotations(composerDraftTarget, []);4213+ setComposerDraftReviewComments(composerDraftTarget, []);4214+ };4215+ if (hasDraftToStash && composerRef.current?.stashCurrentPrompt() !== true) {4216+ if (hasDraftContextToClear) clearDraftContexts();4217+ writeRevertedMessageToComposer();4218+ toastManager.add({4219+ type: "warning",4220+ title: "Reverted message appended",4221+ description: hasDraftContextToClear4222+ ? "Your current draft could not be stashed, so the reverted message was appended and attached context was cleared."4223+ : "Your current draft could not be stashed, so the reverted message was added after it instead.",4224+ data: { hideCopyButton: true },4225+ });4226+ return;
- apps/web/src/components/ChatView.tsx line 4114Resolvedby amitray007(GitHub profile, opens in a new tab)@@ -3989,0 +4094,21 @@4094+ const failed = revertObservedActivities.some((activity) => {4095+ if (4096+ activity.kind !== "checkpoint.revert.failed" ||4097+ pending.knownFailureActivityIds.has(activity.id) ||4098+ typeof activity.payload !== "object" ||4099+ activity.payload === null4100+ ) {4101+ return false;4102+ }4103+ return (activity.payload as { turnCount?: unknown }).turnCount === pending.turnCount;4104+ });4105+ if (failed) {4106+ releaseRevertedMessageDraft(pending.draft);4107+ pendingRevertedMessageRestoreRef.current = null;4108+ setPendingRevertedMessageRestore(null);4109+ return;4110+ }4111+4112+ if (revertObservedMessages.some((message) => message.id === pending.messageId)) {4113+ return;4114+ }
amitray007(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.


Important
Review skipped#
Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
.coderabbit.yamlfile in this repository. To trigger a single review, invoke the@coderabbitai reviewcommand.⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID:
ad10ca3c-9852-4304-9a76-f7b9f24cfd86You can disable this status message by setting the
reviews.review_statustofalsein the CodeRabbit configuration file.Use the checkbox below for a quick retry:
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
❤️ Share