Skip to content

A desktop coding agent with free access to frontier models.

TypeScript

fix: undo turn rule matches the engine

ogsamrat3aa8a7def9c1
diff --git a/apps/app/src/react-app/domains/session/commands/turn-messages.ts b/apps/app/src/react-app/domains/session/commands/turn-messages.ts
index c9b599b6..7f0bd60e 100644
--- a/apps/app/src/react-app/domains/session/commands/turn-messages.ts
+++ b/apps/app/src/react-app/domains/session/commands/turn-messages.ts
@@ -1,28 +1,50 @@
/**
* Which user messages start a turn someone sent. A compaction adds user
- * messages of its own (the compaction request, and the automatic "continue"
- * after a compaction mid-turn); undo, redo and the prompt put back in the
- * composer skip them, so one undo takes back the whole turn.
+ * messages of its own: the compaction request (its `compaction` part) and the
+ * automatic "continue" after a compaction mid-turn (its text part's
+ * `compaction_continue` marker). After compacting an overflowing context the
+ * engine may also send the person's prompt again (same text, right after the
+ * compaction): that replay counts as the original prompt. Undo, redo and the
+ * prompt put back in the composer go by these turns, so one undo takes back
+ * the whole turn. PARITY: the engine's TUI (packages/tui/src/util/undo-notice.ts
+ * isPersonPrompt / undoTarget).
*/
type MessageLike = { info: unknown; parts: ReadonlyArray<unknown> };
-type PartLike = { type?: unknown; synthetic?: unknown; providerMetadata?: { opencode?: { synthetic?: unknown } } };
+type PartLike = { type?: unknown; synthetic?: unknown; text?: unknown; metadata?: { compaction_continue?: unknown } | null };
-const isSynthetic = (part: PartLike) => part.synthetic === true || part.providerMetadata?.opencode?.synthetic === true;
+const partsOf = (message: MessageLike) => message.parts as ReadonlyArray<PartLike>;
+const isUser = (message: MessageLike) => (message.info as { role?: unknown } | null)?.role === "user";
+const isCompaction = (message: MessageLike) => partsOf(message).some((part) => part.type === "compaction" || part.type === "data-compaction");
-/** A user message the person sent (not a compaction's request, its summary or its automatic follow-up). */
+/** A user message the person sent (not a compaction's request or its automatic follow-up). */
export function isPersonUserMessage(message: MessageLike | null | undefined): boolean {
- if (!message) return false;
- const info = message.info as { role?: unknown; summary?: unknown; mode?: unknown; agent?: unknown } | null;
- if (info?.role !== "user") return false;
- if (info.summary === true || info.mode === "compaction" || info.agent === "compaction") return false;
- const parts = message.parts as ReadonlyArray<PartLike>;
- if (parts.some((part) => part.type === "compaction" || part.type === "data-compaction")) return false;
- const text = parts.filter((part) => part.type === "text");
- return !(text.length > 0 && text.length === parts.length && text.every(isSynthetic));
+ if (!message || !isUser(message)) return false;
+ if (isCompaction(message)) return false;
+ return !partsOf(message).some((part) => part.type === "text" && part.metadata?.compaction_continue === true);
}
-/** The user messages that start turns, oldest first. */
+/** The text the person typed (synthetic text left out), to recognise a replayed prompt. */
+function promptText(message: MessageLike): string {
+ return partsOf(message)
+ .filter((part) => part.type === "text" && !part.synthetic)
+ .map((part) => (typeof part.text === "string" ? part.text : ""))
+ .join("\n");
+}
+
+/**
+ * The user messages that start turns, oldest first: the person's prompts,
+ * with a prompt the engine replayed after a compaction counted as the
+ * original (left out here).
+ */
export function personUserMessages<T extends MessageLike>(messages: readonly T[]): T[] {
- return messages.filter((message) => isPersonUserMessage(message));
+ const users = messages.filter((message) => isUser(message));
+ return users.filter((message, index) => {
+ if (!isPersonUserMessage(message)) return false;
+ const compaction = users[index - 1];
+ const earlier = users[index - 2];
+ if (!compaction || !earlier || !isCompaction(compaction) || !isPersonUserMessage(earlier)) return true;
+ const text = promptText(message);
+ return !text || text !== promptText(earlier);
+ });
}
diff --git a/apps/app/src/react-app/domains/session/commands/undo-notice.ts b/apps/app/src/react-app/domains/session/commands/undo-notice.ts
index ede8c597..9bb353c9 100644
--- a/apps/app/src/react-app/domains/session/commands/undo-notice.ts
+++ b/apps/app/src/react-app/domains/session/commands/undo-notice.ts
@@ -44,7 +44,7 @@ export function undoNotice(revert: RevertOutcome | null | undefined, tracked: bo
`Left ${plural(kept.length, "file")} as ${kept.length === 1 ? "it is" : "they are"}: ${list(kept)} (edited after the turn, and the turn's change could not be taken out cleanly).`,
);
}
- if (outside.length) lines.push(`Not touched: ${list(outside)} (changed during the turn, but not by the agent).`);
+ if (outside.length) lines.push(`Not touched: ${list(outside)} (changed during the turn by something other than the agent's file edits).`);
if (!lines.length) lines.push("The last turn changed no files.");
lines.push("Redo puts it back.");
return { title: "Undid the last turn", message: lines.join(" "), variant: kept.length || outside.length ? "warning" : "success" };
diff --git a/apps/app/src/react-app/domains/session/commands/use-session-commands.ts b/apps/app/src/react-app/domains/session/commands/use-session-commands.ts
index bd1e3b58..78c60252 100644
--- a/apps/app/src/react-app/domains/session/commands/use-session-commands.ts
+++ b/apps/app/src/react-app/domains/session/commands/use-session-commands.ts
@@ -17,7 +17,7 @@ import {
import { getComposerDraft, useComposerStateStore } from "@/react-app/domains/session/surface/composer-state-store";
import { REDO_NOTICE, projectTracksFiles, undoNotice } from "./undo-notice";
import { commandTemplateLabel } from "./review-message";
-import { isPersonUserMessage } from "./turn-messages";
+import { personUserMessages } from "./turn-messages";
export type SessionCommandTarget = {
workspaceId: string;
@@ -58,7 +58,7 @@ export function visibleMessages(snapshot: OmniRushSessionSnapshot | null | undef
/** The user message an undo rewinds to: the newest one the person sent that the chat still shows (a compaction's own are skipped). */
export function undoTarget(snapshot: OmniRushSessionSnapshot | null | undefined): OmniRushSessionMessage | null {
- return visibleMessages(snapshot).findLast((message) => isPersonUserMessage(message)) ?? null;
+ return personUserMessages(visibleMessages(snapshot)).at(-1) ?? null;
}
/** The text a user message was sent with (what goes back into the composer after an undo). */
diff --git a/apps/app/src/react-app/domains/session/editor/editor-models.ts b/apps/app/src/react-app/domains/session/editor/editor-models.ts
index fe2f406d..4b456321 100644
--- a/apps/app/src/react-app/domains/session/editor/editor-models.ts
+++ b/apps/app/src/react-app/domains/session/editor/editor-models.ts
@@ -158,7 +158,7 @@ export class EditorDocuments {
file = await this.client.editorReadFile(this.workspaceId, doc.path);
} catch (error) {
// Deleted on disk: a clean document empties and says so; a dirty one keeps the user's text.
- if ((error as { status?: number }).status === 404 && !doc.missing) {
+ if ((error as { status?: number }).status === 404 && (!doc.missing || options.discard)) {
if (dirty(doc)) {
doc.conflict = true;
return;
diff --git a/apps/app/tests/session-commands.test.ts b/apps/app/tests/session-commands.test.ts
index aced23da..48904497 100644
--- a/apps/app/tests/session-commands.test.ts
+++ b/apps/app/tests/session-commands.test.ts
@@ -22,7 +22,7 @@ describe("undoNotice", () => {
expect(undoNotice({ kept: ["x", "y"] }, true).message).toBe("Left 2 files as they are: x, y (edited after the turn, and the turn's change could not be taken out cleanly). Redo puts it back.");
expect(undoNotice({ restored: ["a"], outside: ["log.txt"] }, true)).toEqual({
title: "Undid the last turn",
- message: "Restored 1 file: a. Not touched: log.txt (changed during the turn, but not by the agent). Redo puts it back.",
+ message: "Restored 1 file: a. Not touched: log.txt (changed during the turn by something other than the agent's file edits). Redo puts it back.",
variant: "warning",
});
});
diff --git a/apps/app/tests/undo-editor-sync.test.ts b/apps/app/tests/undo-editor-sync.test.ts
index 84502342..d67a41ed 100644
--- a/apps/app/tests/undo-editor-sync.test.ts
+++ b/apps/app/tests/undo-editor-sync.test.ts
@@ -22,7 +22,7 @@ const COMPACTED_TURN = [
msg("m04", "assistant", [{ type: "tool", tool: "edit" }]),
msg("m05", "user", [{ type: "compaction", auto: true }]),
msg("m06", "assistant", [text("summary")], { summary: true, mode: "compaction" }),
- msg("m07", "user", [text("Continue if you have next steps", { synthetic: true })]),
+ msg("m07", "user", [text("Continue if you have next steps", { synthetic: true, metadata: { compaction_continue: true } })]),
msg("m08", "assistant", [{ type: "tool", tool: "write" }, text("finished")]),
];
@@ -33,14 +33,34 @@ describe("turn user messages", () => {
// A prompt that is only an attachment (no typed text) is still the person's.
expect(isPersonUserMessage(msg("x", "user", [{ type: "file", url: "file:///a.png" }]))).toBe(true);
expect(isPersonUserMessage(msg("x", "user", [{ type: "data-compaction" }]))).toBe(false);
- expect(isPersonUserMessage(msg("x", "user", [text("go on", { providerMetadata: { opencode: { synthetic: true } } })]))).toBe(false);
- expect(isPersonUserMessage(msg("x", "user", [text("s")], { summary: true }))).toBe(false);
+ // Only synthetic text (an attachment whose read failed) is still the person's prompt.
+ expect(isPersonUserMessage(msg("x", "user", [text("Could not read a.png", { synthetic: true })]))).toBe(true);
// A user message's diff summary object is not a compaction mark.
expect(isPersonUserMessage(msg("x", "user", [text("hi")], { summary: { diffs: [] } }))).toBe(true);
expect(isPersonUserMessage(msg("x", "assistant", [text("hi")]))).toBe(false);
expect(isPersonUserMessage(null)).toBe(false);
});
+ test("a prompt the engine replays after an overflow compaction counts as the original", () => {
+ const replayed = [
+ msg("m01", "user", [text("first")]),
+ msg("m02", "assistant", [text("done")]),
+ msg("m03", "user", [text("big prompt")]),
+ msg("m04", "user", [{ type: "compaction", auto: true, overflow: true }]),
+ msg("m05", "assistant", [text("summary")], { summary: true }),
+ msg("m06", "user", [text("big prompt")]),
+ msg("m07", "user", [{ type: "compaction", auto: true, overflow: true }]),
+ msg("m08", "assistant", [text("summary")], { summary: true }),
+ msg("m09", "user", [text("big prompt")]),
+ msg("m10", "assistant", [text("answer")]),
+ ];
+ expect(personUserMessages(replayed).map((message) => message.info.id)).toEqual(["m01", "m03"]);
+ expect(undoTarget({ session: { id: "s", revert: null }, messages: replayed } as never)?.info.id).toBe("m03");
+ // A different prompt after a compaction is a turn of its own.
+ const next = [...replayed.slice(0, 5), msg("m06", "user", [text("something else")])];
+ expect(personUserMessages(next).map((message) => message.info.id)).toEqual(["m01", "m03", "m06"]);
+ });
+
test("undo picks the turn's own prompt in one press", () => {
const snapshot = { session: { id: "s", revert: null }, messages: COMPACTED_TURN } as never;
expect(undoTarget(snapshot)?.info.id).toBe("m03");
diff --git a/apps/desktop/scripts/engine-release.json b/apps/desktop/scripts/engine-release.json
index b1d9154a..e98fbcff 100644
--- a/apps/desktop/scripts/engine-release.json
+++ b/apps/desktop/scripts/engine-release.json
@@ -10,27 +10,27 @@
"assets": {
"aarch64-apple-darwin": {
"name": "omnirush-engine-darwin-arm64-1.18.32.tar.gz",
- "sha256": "032b9b8f53e0622f2e09526aad91099f0a34226fc5cd7d6fe32be48776923e94"
+ "sha256": "f2b3cf91a03a0890cb4643b83ec04bfcebfa08f69bfd71290b04d098c897df85"
},
"x86_64-apple-darwin": {
"name": "omnirush-engine-darwin-x64-1.18.32.tar.gz",
- "sha256": "2a82ae32754adec004c6ecef505be398aed178a3d53528f1100336fce9a497b6"
+ "sha256": "38fbdeba644f3b3ee5910e3641b5f6fc8319536148b8ae99b6cd1d6208c99cc7"
},
"x86_64-unknown-linux-gnu": {
"name": "omnirush-engine-linux-x64-1.18.32.tar.gz",
- "sha256": "d0ff0c6a48954863a40451511aad13fac9834d062ec43b6b125ad898d0d343d4"
+ "sha256": "cb6d1a18a4f31f5f81523eebdf7ae9ac428026aa0396e9c12e9bd15c5b376c65"
},
"aarch64-unknown-linux-gnu": {
"name": "omnirush-engine-linux-arm64-1.18.32.tar.gz",
- "sha256": "45fc20a65da22b2a13125ce72239c9e5cc12f902aa2cb9dfb918a12c7ac81860"
+ "sha256": "c78209d647b79242a2473e70b70cd73a262dd3f78c28baa645e00dd8f6a65eca"
},
"x86_64-pc-windows-msvc": {
"name": "omnirush-engine-win32-x64-1.18.32.tar.gz",
- "sha256": "86778a236e8fa3f0de7cbc2cf872ca4c9f5d9d5b15bc0d0f899a3ad4f664f2e7"
+ "sha256": "28de1d0ba4db55b54c78ca1e7f3bc409273e5de05211bcea136ba403c1d2d7fd"
},
"aarch64-pc-windows-msvc": {
"name": "omnirush-engine-win32-arm64-1.18.32.tar.gz",
- "sha256": "0aafe7ddb3e5ed409d887433759080c33e1208576172adeb978afdb4316a1cf4"
+ "sha256": "69a7174c0759b36402bbf697ecb0cc6afbbb16dfa1f102ab61ed1807eb7a1da4"
}
}
}
diff --git a/apps/server/src/server.ts b/apps/server/src/server.ts
index fa9adea3..470d55ff 100644
--- a/apps/server/src/server.ts
+++ b/apps/server/src/server.ts
@@ -2392,7 +2392,8 @@ export async function proxyOpencodeRequest(input: {
const undoSessionId = decodeEngineRouteParam(undoCall.sessionId) ?? undoCall.sessionId;
const kept = await revertBodyWithEditorSaves({
saves: editorSavesFor(input.config),
- root: workspace.path,
+ // The editor's folder, as the engine names paths (no Windows `\\?\` prefix): keep paths are absolute.
+ root: normalizeOpencodeDirectory(workspace.path),
body,
messageCreated: (messageId) => engineMessageCreated(baseUrl, auth, directory, undoSessionId, messageId),
});
@@ -4835,7 +4836,7 @@ function createRoutes(
requireApproval,
requireClientScope,
resolveWorkspace,
- onSaved: (workspace, path) => editorSavesFor(config).record(workspace.path, path),
+ onSaved: (workspace, path) => editorSavesFor(config).record(normalizeOpencodeDirectory(workspace.path), path),
onUserEdit: (edit) => {
const capture = captureServicesByServer.get(config);
if (!capture?.uploadEnabled || !capture.hasSession(edit.sessionId) || isUploadFileDenied(edit.path)) return;