A desktop coding agent with free access to frontier models.
TypeScript
fix: revert and edit ask first, hidden turns go only when confirmed, load earlier messages
ogsamrat9ba023f84bf9
diff --git a/apps/app/src/i18n/locales/en.ts b/apps/app/src/i18n/locales/en.tsindex 02022e5b..85045021 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": "Reverted: {count} later messages and their file changes are set aside. Restore brings them back; a new message removes them for good.",+ "session.reverted_messages_hidden": "{count} later messages are hidden and their file changes undone. Restore brings them back.","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.tsindex 0305df88..3120563c 100644--- a/apps/app/src/react-app/domains/session/surface/revert-scope.ts+++ b/apps/app/src/react-app/domains/session/surface/revert-scope.ts@@ -3,17 +3,30 @@* 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. */+function revertPointIndex(messages: readonly ScopeMessage[], messageId: string): number {+ let index = messages.findIndex((message) => message.id === messageId);+ while (index > 0 && messages[index]?.role !== "user") index -= 1;+ return index;+}++/**+ * The person's prompt a revert at this message rewinds to (itself for a prompt, the prompt that started the+ * turn for a reply). Reverting at the prompt also takes back the file changes of the turn's earlier steps,+ * which a revert at a reply's last step leaves on disk. Null when the message is not on screen.+ */+export function revertPointMessageId(messages: readonly ScopeMessage[], messageId: string): string | null {+ const index = revertPointIndex(messages, messageId);+ const message = index < 0 ? null : messages[index];+ return message?.role === "user" ? message.id : null;+}++/** Turns (the person's prompts) a revert at this message takes back, its own included; null when 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;+ const start = revertPointIndex(messages, messageId);+ if (start < 0) return null;let turns = 0;for (let at = start; at < messages.length; at += 1) {if (messages[at]?.role === "user") turns += 1;@@ -21,19 +34,36 @@ export function turnsFromRevertPoint(messages: readonly ScopeMessage[], messageIreturn Math.max(turns, 1);}-/** More than the newest turn, or a point not on screen (an older part of a long chat): ask first. */+/** More than the newest turn, or a point not on screen (an older part of a long chat). */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`;+function laterTurns(turns: number | null): string {+ if (turns === null) return "every later turn";+ const later = turns - 1;+ return `the ${later} later turn${later === 1 ? "" : "s"}`;}-/** 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.`;+/** The question before a revert from a message's Revert button. */+export function revertConfirmText(turns: number | null, running: boolean): string {+ const scope = turns === 1 ? "Hide this turn" : `Hide this and ${laterTurns(turns)}`;+ const stop = running ? " The running turn stops first." : "";+ return `${scope}, and undo their file changes?${stop} Restore brings them back until you send a new message.`;+}++/** The bar above the composer while it holds an edit of an earlier message. */+export function editTargetText(turns: number | null): string {+ if (turns === 1) return "Editing your last message: sending replaces it and undoes its file changes.";+ return `Editing an earlier message: sending will remove ${laterTurns(turns)} and undo their file changes.`;+}++/** The question before an edit of an earlier message goes out. */+export function editConfirmText(turns: number | null): string {+ return `Send this in place of the earlier message? It removes ${laterTurns(turns)} for good and undoes their file changes.`;+}++/** The question before a send that would delete hidden (reverted) messages for good. */+export function hiddenDeleteConfirmText(hidden: number): string {+ return `Sending removes the ${hidden} hidden message${hidden === 1 ? "" : "s"} for good; their file changes stay undone. Send and remove them? (Cancel keeps them: Restore brings them back.)`;}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 a48f7cb3..40af85a7 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,7 +132,7 @@ import {type ComposerSessionState,useComposerStateStore,} from "./composer-state-store";-import { revertConfirmText, revertNeedsConfirm, turnsFromRevertPoint } from "./revert-scope";+import { editConfirmText, editTargetText, hiddenDeleteConfirmText, revertConfirmText, revertNeedsConfirm, revertPointMessageId, turnsFromRevertPoint } from "./revert-scope";import { MessageList } from "@/components/chat/message-list";import { MessageListProvider, type DispatchAction } from "@/components/chat/message-list-provider";import type {@@ -1167,6 +1167,9 @@ function composerSessionHasContent(state: ComposerSessionState | undefined) {));}+/** Messages a chat's snapshot asks the engine for, newest first; "Load earlier messages" adds this many more. */+const SNAPSHOT_PAGE = 140;+function hiddenMessageCount(snapshot: OmniRushSessionSnapshot, revertMessageId: string): number {const index = snapshot.messages.findIndex((message) => message.info.id === revertMessageId);return index < 0 ? snapshot.messages.length : snapshot.messages.length - index;@@ -1405,6 +1408,11 @@ export function SessionSurface(props: SessionSurfaceProps) {[props.opencodeBaseUrl, props.omnirushToken, props.workspaceRoot],);+ // The engine sends a chat's newest messages only; "Load earlier messages" asks for more of them.+ const [snapshotLimit, setSnapshotLimit] = useState(SNAPSHOT_PAGE);+ const snapshotLimitRef = useRef(SNAPSHOT_PAGE);+ snapshotLimitRef.current = snapshotLimit;+ useEffect(() => setSnapshotLimit(SNAPSHOT_PAGE), [props.sessionId]);const snapshotQueryKey = useMemo(() => reactSnapshotKey(props.workspaceId, props.sessionId),[props.workspaceId, props.sessionId],@@ -1430,12 +1438,12 @@ export function SessionSurface(props: SessionSurfaceProps) {? await opencodeSessionNative.composeNativeSessionSnapshotWithRetry(sessionOwner,() => snapshotTargetRef.current,- { limit: 140, signal },+ { limit: snapshotLimitRef.current, signal },): await opencodeSessionNative.composeNativeSessionSnapshot({ opencodeBaseUrl: props.opencodeBaseUrl, token: props.omnirushToken },props.sessionId,- { limit: 140, signal },+ { limit: snapshotLimitRef.current, signal },);markSessionSnapshotFetchStart(item, startedAt);return item;@@ -2288,7 +2296,10 @@ export function SessionSurface(props: SessionSurfaceProps) {// 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;+ if (revertNeedsConfirm(turns) && !window.confirm(editConfirmText(turns))) return;+ } else if (revertMessageId && !window.confirm(hiddenDeleteConfirmText(revertedMessageCount))) {+ // The engine deletes hidden (reverted) turns on the next prompt: never as a side effect of a send.+ return;}// Immediate sends and queued sends share the same slot across all panes.dispatchQueuedDrain(props.sessionId, { type: "user_retry" });@@ -2385,7 +2396,7 @@ export function SessionSurface(props: SessionSurfaceProps) {} finally {setAttachmentsUploading(false);}- }, [attachments, baseRenderedMessages, buildDraft, clearComposer, draft, liveStatus.type, persistedDraftKey, props.onDraftChange, props.sessionId, sendDraft, sessionOwner]);+ }, [attachments, baseRenderedMessages, buildDraft, clearComposer, draft, liveStatus.type, persistedDraftKey, props.onDraftChange, props.sessionId, revertMessageId, revertedMessageCount, sendDraft, sessionOwner]);// A chat command from a button ("/review", "/fix 1, 3"): sent like a typed one, the composer's draft left alone.const runChatCommand = useCallback(async (text: string) => {@@ -3201,10 +3212,13 @@ export function SessionSurface(props: SessionSurfaceProps) {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]);+ const messages = renderedMessagesRef.current;+ const turns = turnsFromRevertPoint(messages, messageId);+ if (!window.confirm(revertConfirmText(turns, liveStatus.type !== "idle"))) return;+ // From a reply: rewind at the prompt that started its turn, so the turn's earlier steps' file changes go too+ // (the engine reverting at the reply's own message keeps them on disk while the transcript hides them).+ void props.onRevertToMessage?.(revertPointMessageId(messages, messageId) ?? messageId, props.sessionId);+ }, [liveStatus.type, props.onRevertToMessage, props.sessionId]);const handleForkAtMessage = useCallback((messageId: string) => {// OpenCode's fork copies messages strictly before the given id, so pass@@ -3338,13 +3352,34 @@ export function SessionSurface(props: SessionSurfaceProps) {sessionScroll.markScrollGesture(event.currentTarget);}}onScroll={sessionScroll.handleScroll}+ // Focusable by a click, never by Tab: Cmd/Ctrl+Z undoes a chat turn only with focus here (undo-key.ts).+ data-chat-transcript+ tabIndex={-1}// Extra top padding while the find bar is open so it never covers// the first message (short transcripts cannot scroll it clear).- className={`absolute inset-0 overflow-x-hidden overflow-y-auto overscroll-y-contain touch-pan-y px-3 pb-4 sm:px-5 ${findOwned ? "pt-16" : "pt-4"}`}+ className={`absolute inset-0 outline-none overflow-x-hidden overflow-y-auto overscroll-y-contain touch-pan-y px-3 pb-4 sm:px-5 ${findOwned ? "pt-16" : "pt-4"}`}>{/* 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]">+ {snapshot && snapshot.messages.length >= snapshotLimit ? (+ <div className="mb-3 flex justify-center">+ <button+ type="button"+ data-testid="load-earlier-messages"+ disabled={snapshotQuery.isFetching}+ className="rounded-full border border-dls-border bg-dls-surface px-3 py-1.5 text-xs font-medium text-dls-secondary transition-colors hover:bg-dls-hover disabled:opacity-50"+ onClick={() => {+ // Nothing was deleted: an older part of a long chat is only not loaded yet.+ snapshotLimitRef.current = snapshotLimit + SNAPSHOT_PAGE;+ setSnapshotLimit(snapshotLimitRef.current);+ void snapshotQuery.refetch();+ }}+ >+ {snapshotQuery.isFetching ? "Loading…" : "Load earlier messages"}+ </button>+ </div>+ ) : null}{error && snapshot && snapshot.messages.length > 0 ? (<SessionErrorCarddeveloperMode={props.developerMode}@@ -3491,14 +3526,22 @@ export function SessionSurface(props: SessionSurfaceProps) {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.+ {editTargetText(turnsFromRevertPoint(baseRenderedMessages, composerEditTarget))}</span><buttontype="button"className="shrink-0 font-medium hover:underline"onClick={() => clearComposerRevertTarget(props.sessionId)}>- Send as a new message+ Send as new+ </button>+ <button+ type="button"+ className="shrink-0 font-medium hover:underline"+ data-testid="composer-edit-cancel"+ onClick={() => replaceComposerDraft(props.sessionId, "")}+ >+ Cancel</button></div>) : null}diff --git a/apps/app/tests/revert-scope.test.ts b/apps/app/tests/revert-scope.test.tsindex c74ad359..e060e478 100644--- a/apps/app/tests/revert-scope.test.ts+++ b/apps/app/tests/revert-scope.test.ts@@ -1,6 +1,6 @@import { describe, expect, test } from "bun:test";-import { revertConfirmText, revertNeedsConfirm, turnsFromRevertPoint } from "../src/react-app/domains/session/surface/revert-scope";+import { editConfirmText, editTargetText, hiddenDeleteConfirmText, revertConfirmText, revertNeedsConfirm, revertPointMessageId, turnsFromRevertPoint } from "../src/react-app/domains/session/surface/revert-scope";const chat = [{ id: "u1", role: "user" },@@ -38,9 +38,24 @@ describe("revertNeedsConfirm", () => {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");+ test("the questions name the later turns and the file changes", () => {+ expect(revertConfirmText(5, false)).toBe("Hide this and the 4 later turns, and undo their file changes? Restore brings them back until you send a new message.");+ expect(revertConfirmText(2, true)).toBe("Hide this and the 1 later turn, and undo their file changes? The running turn stops first. Restore brings them back until you send a new message.");+ expect(revertConfirmText(1, false)).toBe("Hide this turn, and undo their file changes? Restore brings them back until you send a new message.");+ expect(revertConfirmText(null, false)).toContain("every later turn");+ expect(editTargetText(3)).toBe("Editing an earlier message: sending will remove the 2 later turns and undo their file changes.");+ expect(editTargetText(1)).toBe("Editing your last message: sending replaces it and undoes its file changes.");+ expect(editConfirmText(4)).toBe("Send this in place of the earlier message? It removes the 3 later turns for good and undoes their file changes.");+ expect(hiddenDeleteConfirmText(6)).toBe("Sending removes the 6 hidden messages for good; their file changes stay undone. Send and remove them? (Cancel keeps them: Restore brings them back.)");+ });+});++describe("revertPointMessageId", () => {+ test("a reply rewinds at the prompt that started its turn, so the turn's earlier steps go too", () => {+ expect(revertPointMessageId(chat, "a2b")).toBe("u2");+ expect(revertPointMessageId(chat, "a3")).toBe("u3");+ expect(revertPointMessageId(chat, "u1")).toBe("u1");+ expect(revertPointMessageId(chat, "gone")).toBeNull();+ expect(revertPointMessageId([{ id: "a0", role: "assistant" }], "a0")).toBeNull();});});