A desktop coding agent with free access to frontier models.
TypeScript
fix: ask before a revert or edit takes back more than the last turn
ogsamrat238cbf1a5040
diff --git a/apps/app/src/i18n/locales/en.ts b/apps/app/src/i18n/locales/en.tsindex b960c8e6..02022e5b 100644--- a/apps/app/src/i18n/locales/en.ts+++ b/apps/app/src/i18n/locales/en.ts@@ -1349,7 +1349,7 @@ export default {"session.redo_aria_label": "Redo last reverted message","session.redo_label": "Redo","session.redo_title": "Redo last reverted message",- "session.reverted_messages_hidden": "{count} earlier messages are hidden",+ "session.reverted_messages_hidden": "Reverted: {count} later messages and their file changes are set aside. Restore brings them back; a new message removes them for good.","session.restore": "Restore","session.restoring": "Restoring...","session.restore_failed": "Could not restore the hidden messages. Try again.",diff --git a/apps/app/src/react-app/domains/session/surface/revert-scope.ts b/apps/app/src/react-app/domains/session/surface/revert-scope.tsnew file mode 100644index 00000000..0305df88--- /dev/null+++ b/apps/app/src/react-app/domains/session/surface/revert-scope.ts@@ -0,0 +1,39 @@+/**+ * How much of a chat a revert takes back. The engine rewinds to the person's+ * prompt at or before the chosen message (packages/opencode/src/session/+ * revert.ts): every turn from there on is hidden, the files go back to how+ * they were before it, and the next prompt deletes the hidden turns for good.+ * Reverting or editing the newest turn is a small step; anything further back+ * asks first, saying how many turns go.+ */+type ScopeMessage = { id: string; role: string };++/** Turns (the person's prompts) a revert at this message takes back; null when the message is not on screen. */+export function turnsFromRevertPoint(messages: readonly ScopeMessage[], messageId: string): number | null {+ const index = messages.findIndex((message) => message.id === messageId);+ if (index < 0) return null;+ let start = index;+ while (start > 0 && messages[start]?.role !== "user") start -= 1;+ let turns = 0;+ for (let at = start; at < messages.length; at += 1) {+ if (messages[at]?.role === "user") turns += 1;+ }+ return Math.max(turns, 1);+}++/** More than the newest turn, or a point not on screen (an older part of a long chat): ask first. */+export function revertNeedsConfirm(turns: number | null): boolean {+ return turns === null || turns > 1;+}++function turnsLabel(turns: number | null): string {+ return turns === null ? "every turn from there on" : `the last ${turns} turns`;+}++/** The question before a revert or an edit of an earlier message goes ahead. */+export function revertConfirmText(kind: "revert" | "edit", turns: number | null): string {+ if (kind === "edit") {+ return `Send this in place of the earlier message? It removes ${turnsLabel(turns)} of this chat and takes back the file changes they made. This cannot be undone.`;+ }+ return `Revert to this point? This hides ${turnsLabel(turns)} of this chat and takes back the file changes they made. Restore brings them back, until you send a new message.`;+}diff --git a/apps/app/src/react-app/domains/session/surface/session-surface.tsx b/apps/app/src/react-app/domains/session/surface/session-surface.tsxindex 428a506a..a48f7cb3 100644--- a/apps/app/src/react-app/domains/session/surface/session-surface.tsx+++ b/apps/app/src/react-app/domains/session/surface/session-surface.tsx@@ -132,6 +132,7 @@ import {type ComposerSessionState,useComposerStateStore,} from "./composer-state-store";+import { revertConfirmText, revertNeedsConfirm, turnsFromRevertPoint } from "./revert-scope";import { MessageList } from "@/components/chat/message-list";import { MessageListProvider, type DispatchAction } from "@/components/chat/message-list-provider";import type {@@ -1105,7 +1106,7 @@ function SessionErrorCard({ error, developerMode, onDismiss, onChangeModel, onOpfunction RevertedMessagesBanner(props: { hiddenCount: number; restoring: boolean; onRestore: () => void }) {return (<div- className="mb-3 flex items-center gap-3 rounded-2xl border border-amber-7/40 bg-amber-2/30 px-4 py-3 text-sm text-warning"+ className="mx-3 mb-2 flex items-center gap-3 rounded-xl border border-amber-7/40 bg-amber-2/30 px-3 py-2 text-xs text-warning"data-testid="reverted-messages-banner"role="status">@@ -1197,6 +1198,7 @@ export function SessionSurface(props: SessionSurfaceProps) {const replaceComposerDraft = useComposerStateStore((state) => state.replaceDraft);const hydrateComposerDraft = useComposerStateStore((state) => state.hydrateDraft);const clearComposerRevertTarget = useComposerStateStore((state) => state.clearRevertTarget);+ const composerEditTarget = useComposerStateStore((state) => getComposerRevertMessageId(state, props.sessionId));const setComposerAttachments = useComposerStateStore((state) => state.setAttachments);const setComposerMentions = useComposerStateStore((state) => state.setMentions);const setComposerPasteParts = useComposerStateStore((state) => state.setPasteParts);@@ -2283,6 +2285,11 @@ export function SessionSurface(props: SessionSurfaceProps) {...buildDraft(text, sourceAttachments, sourceComposer),messageId: createPromptMessageID(),};+ // An edited earlier message replaces every turn after it, and their file changes, for good: ask first.+ if (nextDraft.revertMessageId) {+ const turns = turnsFromRevertPoint(baseRenderedMessages, nextDraft.revertMessageId);+ if (revertNeedsConfirm(turns) && !window.confirm(revertConfirmText("edit", turns))) return;+ }// Immediate sends and queued sends share the same slot across all panes.dispatchQueuedDrain(props.sessionId, { type: "user_retry" });if (!claimQueuedSend(props.sessionId, nextDraft.messageId, true)) return;@@ -3193,6 +3200,9 @@ export function SessionSurface(props: SessionSurfaceProps) {}, [attachments, buildDraft, props.onDraftChange, props.sessionId, props.workspaceId, typeComposerText]);const handleRevertToUserMessage = useCallback((messageId: string) => {+ // One click under an older message used to take hours of turns and their file changes back unasked.+ const turns = turnsFromRevertPoint(renderedMessagesRef.current, messageId);+ if (revertNeedsConfirm(turns) && !window.confirm(revertConfirmText("revert", turns))) return;void props.onRevertToMessage?.(messageId, props.sessionId);}, [props.onRevertToMessage, props.sessionId]);@@ -3335,13 +3345,6 @@ export function SessionSurface(props: SessionSurfaceProps) {{/* Chat column: tighter than the composer (800px) so messageskeep a comfortable reading width and don't feel "too big". */}<div ref={contentRef} className="mx-auto w-full max-w-[720px]">- {revertMessageId ? (- <RevertedMessagesBanner- hiddenCount={revertedMessageCount}- restoring={restoringRevertedMessages}- onRestore={handleRestoreRevertedSession}- />- ) : null}{error && snapshot && snapshot.messages.length > 0 ? (<SessionErrorCarddeveloperMode={props.developerMode}@@ -3473,6 +3476,32 @@ export function SessionSurface(props: SessionSurfaceProps) {</div><div ref={composerShellRef} data-composer-shell className="shrink-0 px-0 pb-2 pt-2">+ {/* Next to the composer, not above the first message: in a long chat the top is far off screen. */}+ {revertMessageId ? (+ <RevertedMessagesBanner+ hiddenCount={revertedMessageCount}+ restoring={restoringRevertedMessages}+ onRestore={handleRestoreRevertedSession}+ />+ ) : null}+ {composerEditTarget && !revertMessageId ? (+ <div+ className="mx-3 mb-2 flex items-center gap-3 rounded-xl border border-amber-7/40 bg-amber-2/30 px-3 py-2 text-xs text-warning"+ data-testid="composer-edit-target"+ role="status"+ >+ <span className="min-w-0 flex-1">+ Editing an earlier message: sending replaces it and every turn after it, with their file changes.+ </span>+ <button+ type="button"+ className="shrink-0 font-medium hover:underline"+ onClick={() => clearComposerRevertTarget(props.sessionId)}+ >+ Send as a new message+ </button>+ </div>+ ) : null}{(props.providerConnectedCount ?? 0) === 0 ? (<buttontype="button"diff --git a/apps/app/tests/revert-scope.test.ts b/apps/app/tests/revert-scope.test.tsnew file mode 100644index 00000000..c74ad359--- /dev/null+++ b/apps/app/tests/revert-scope.test.ts@@ -0,0 +1,46 @@+import { describe, expect, test } from "bun:test";++import { revertConfirmText, revertNeedsConfirm, turnsFromRevertPoint } from "../src/react-app/domains/session/surface/revert-scope";++const chat = [+ { id: "u1", role: "user" },+ { id: "a1", role: "assistant" },+ { id: "u2", role: "user" },+ { id: "a2", role: "assistant" },+ { id: "a2b", role: "assistant" },+ { id: "u3", role: "user" },+ { id: "a3", role: "assistant" },+];++describe("turnsFromRevertPoint", () => {+ test("counts the person's prompts from the revert point on", () => {+ expect(turnsFromRevertPoint(chat, "u3")).toBe(1);+ expect(turnsFromRevertPoint(chat, "u2")).toBe(2);+ expect(turnsFromRevertPoint(chat, "u1")).toBe(3);+ });++ test("a reply rewinds to the prompt before it, as the engine does", () => {+ expect(turnsFromRevertPoint(chat, "a3")).toBe(1);+ expect(turnsFromRevertPoint(chat, "a2b")).toBe(2);+ expect(turnsFromRevertPoint(chat, "a1")).toBe(3);+ });++ test("a message not on screen is unknown", () => {+ expect(turnsFromRevertPoint(chat, "gone")).toBeNull();+ expect(turnsFromRevertPoint([], "u1")).toBeNull();+ });+});++describe("revertNeedsConfirm", () => {+ test("the newest turn goes at once; more, or an unknown point, asks", () => {+ expect(revertNeedsConfirm(1)).toBe(false);+ expect(revertNeedsConfirm(2)).toBe(true);+ expect(revertNeedsConfirm(null)).toBe(true);+ });++ test("the question names the turns and the file changes", () => {+ expect(revertConfirmText("revert", 5)).toBe("Revert to this point? This hides the last 5 turns of this chat and takes back the file changes they made. Restore brings them back, until you send a new message.");+ expect(revertConfirmText("edit", 3)).toBe("Send this in place of the earlier message? It removes the last 3 turns of this chat and takes back the file changes they made. This cannot be undone.");+ expect(revertConfirmText("edit", null)).toContain("every turn from there on");+ });+});