Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

fix(ide): delete selected prompt text across terminal repaints (#278)

Ruben Lütke9d054284dc37
diff --git a/jarvis/ui/web/frontend/src/components/agentic/AgenticTerminal.offscreen.test.tsx b/jarvis/ui/web/frontend/src/components/agentic/AgenticTerminal.offscreen.test.tsx
index 8ed8e939cb..94beb1ab4e 100644
--- a/jarvis/ui/web/frontend/src/components/agentic/AgenticTerminal.offscreen.test.tsx
+++ b/jarvis/ui/web/frontend/src/components/agentic/AgenticTerminal.offscreen.test.tsx
@@ -39,7 +39,7 @@ vi.mock("@xterm/xterm", () => ({
options: Record<string, unknown> = {};
unicode = { activeVersion: "" };
buffer = {
- active: { type: "normal", baseY: 0, viewportY: 0 },
+ active: { type: "normal", baseY: 0, viewportY: 0, cursorX: 0, cursorY: 0, getLine: () => undefined },
};
// A pane takes over the terminal's protocol replies on mount, because the
// backend answers those instead (see ./terminalQueries). A stand-in without
@@ -63,6 +63,9 @@ vi.mock("@xterm/xterm", () => ({
getSelection() {
return "";
}
+ getSelectionPosition() { return undefined; }
+ onSelectionChange() { return { dispose() {} }; }
+ onRender() { return { dispose() {} }; }
onData() {
return { dispose() {} };
}
diff --git a/jarvis/ui/web/frontend/src/components/agentic/AgenticTerminal.test.tsx b/jarvis/ui/web/frontend/src/components/agentic/AgenticTerminal.test.tsx
index e7d52e51f4..20014e66b1 100644
--- a/jarvis/ui/web/frontend/src/components/agentic/AgenticTerminal.test.tsx
+++ b/jarvis/ui/web/frontend/src/components/agentic/AgenticTerminal.test.tsx
@@ -70,7 +70,7 @@ vi.mock("@xterm/xterm", () => ({
}
get buffer() {
return {
- active: { type: terminalHarness.bufferType, ...terminalHarness.viewport },
+ active: { type: terminalHarness.bufferType, ...terminalHarness.viewport, cursorX: 0, cursorY: 0, getLine: () => undefined },
};
}
scrollLines(amount: number) {
@@ -125,6 +125,9 @@ vi.mock("@xterm/xterm", () => ({
getSelection() {
return "";
}
+ getSelectionPosition() { return undefined; }
+ onSelectionChange() { return { dispose() {} }; }
+ onRender() { return { dispose() {} }; }
onData() {
return { dispose() {} };
}
@@ -2687,7 +2690,7 @@ describe("terminal text size across a rebuild", () => {
});
// GrokNight paints #141414 on every empty cell. That RGB is the canvas
- // fill; xterm must see the default background so the wallpaper shows.
+ // fill; xterm must see the default background so the pane shell shows.
// The rewrite is not keyed on the pane's product name.
expect(terminalHarness.write).toHaveBeenCalledWith("\x1b[49m \x1b[0m");
});
diff --git a/jarvis/ui/web/frontend/src/components/agentic/AgenticTerminal.tsx b/jarvis/ui/web/frontend/src/components/agentic/AgenticTerminal.tsx
index 6dcb052b7b..b216d6e138 100644
--- a/jarvis/ui/web/frontend/src/components/agentic/AgenticTerminal.tsx
+++ b/jarvis/ui/web/frontend/src/components/agentic/AgenticTerminal.tsx
@@ -141,6 +141,7 @@ import {
captureWheelForTerminalHistory,
} from "./terminalScrollSurface";
import { installMouseSelection } from "./terminalMouseSelection";
+import { installPromptSelectionBridge } from "./terminalPromptSelection";
import {
openPaneSocket,
type PaneSocket,
@@ -1070,6 +1071,7 @@ export function AgenticTerminal({
// A plain left-button drag selects text even while the agent's CLI has
// mouse tracking on, which every coding agent negotiates at start-up.
const disposeMouseSelection = installMouseSelection(container, term, isMac);
+ const disposePromptSelection = installPromptSelectionBridge(term, keys.add, isMac);
// The desktop IDE reserves its platform copy chord for copying. In
// particular, an unselected Ctrl+C on Windows/Linux must not reach Codex
// as `^C`, where it cancels the current turn or exits the pane.
@@ -2281,6 +2283,7 @@ export function AgenticTerminal({
io?.disconnect();
disposeFontSync();
disposeMouseSelection();
+ disposePromptSelection();
disposeCopyBridge();
disposePasteBridge();
disposeNewlineBridge();
diff --git a/jarvis/ui/web/frontend/src/components/agentic/terminalPromptSelection.test.ts b/jarvis/ui/web/frontend/src/components/agentic/terminalPromptSelection.test.ts
new file mode 100644
index 0000000000..3813e89e2b
--- /dev/null
+++ b/jarvis/ui/web/frontend/src/components/agentic/terminalPromptSelection.test.ts
@@ -0,0 +1,232 @@
+import { afterAll, afterEach, describe, expect, it, vi } from "vitest";
+import { Terminal } from "@xterm/xterm";
+import type { IBufferRange } from "@xterm/xterm";
+import { installPromptSelectionBridge } from "./terminalPromptSelection";
+
+// These tests parse cells without rendering. xterm's colour fallback supports
+// a missing canvas context; jsdom otherwise logs its unimplemented method.
+const restoreCanvas = vi.hoisted(() => {
+ const original = HTMLCanvasElement.prototype.getContext;
+ HTMLCanvasElement.prototype.getContext = () => null;
+ return () => { HTMLCanvasElement.prototype.getContext = original; };
+});
+afterAll(restoreCanvas);
+
+const terminals: Terminal[] = [];
+afterEach(() => { terminals.splice(0).forEach((term) => term.dispose()); });
+
+/** Parse real terminal cells, with a fake selection/input transport. No browser. */
+async function setup(output = "› hello world", isMac = false, cols = 40) {
+ const terminal = new Terminal({ cols, rows: 10, allowProposedApi: true });
+ terminals.push(terminal);
+ const write = (text: string) => new Promise<void>((resolve) => terminal.write(text, resolve));
+ await write(output);
+ let selected: IBufferRange | undefined;
+ let onSelection = () => {};
+ let onRender = () => {};
+ let keyHandler = (_event: KeyboardEvent) => true;
+ const sent: string[] = [];
+ let listenerDisposed = false;
+ let handlerDisposed = false;
+ const cleanup = installPromptSelectionBridge({
+ cols,
+ buffer: terminal.buffer,
+ getSelectionPosition: () => selected,
+ select: (x, y, length) => {
+ const end = y * cols + x + length;
+ selected = { start: { x, y }, end: { x: end % cols, y: Math.floor(end / cols) } };
+ onSelection();
+ },
+ clearSelection: () => { selected = undefined; onSelection(); },
+ input: (data) => sent.push(data),
+ onSelectionChange: (listener) => {
+ onSelection = listener;
+ return { dispose: () => { listenerDisposed = true; onSelection = () => {}; } };
+ },
+ onRender: (listener) => {
+ onRender = listener;
+ return { dispose: () => { onRender = () => {}; } };
+ },
+ }, (handler) => {
+ keyHandler = handler;
+ return () => { handlerDisposed = true; keyHandler = () => true; };
+ }, isMac);
+ return {
+ write, sent, cleanup, renderFrame: () => onRender(),
+ selection: () => selected,
+ disposed: () => listenerDisposed && handlerDisposed,
+ select: (start: number, end: number, row = 0, endRow = row) => {
+ selected = { start: { x: start, y: row }, end: { x: end, y: endRow } };
+ onSelection();
+ },
+ press: (key: string, mods: KeyboardEventInit = {}, type = "keydown") => {
+ const event = new KeyboardEvent(type, { key, cancelable: true, ...mods });
+ return { passthrough: keyHandler(event), prevented: event.defaultPrevented };
+ },
+ };
+}
+
+describe("terminal prompt selection", () => {
+ it.each(["Backspace", "Delete"])("deletes the whole selected draft with one %s press", async (key) => {
+ const pane = await setup();
+ pane.select(2, 13);
+ expect(pane.press(key)).toEqual({ passthrough: false, prevented: true });
+ expect(pane.sent).toEqual(["\x7f".repeat(11)]);
+ expect(pane.selection()).toBeUndefined();
+ pane.press(key, {}, "keyup");
+ expect(pane.sent).toHaveLength(1);
+ });
+
+ it("deletes a middle selection while leaving the suffix in place", async () => {
+ const pane = await setup();
+ pane.select(2, 7);
+ pane.press("Delete");
+ expect(pane.sent).toEqual(["\x1b[D".repeat(6) + "\x7f".repeat(5)]);
+ });
+
+ it("moves right when the caret precedes the selected text", async () => {
+ const pane = await setup("❯ hello world\x1b[11D");
+ pane.select(8, 13);
+ pane.press("Backspace");
+ expect(pane.sent).toEqual(["\x1b[C".repeat(11) + "\x7f".repeat(5)]);
+ });
+
+ it.each([false, true])("selects only input using the platform select-all chord (mac=%s)", async (isMac) => {
+ const pane = await setup("old output\r\n› hello world", isMac);
+ expect(pane.press("a", isMac ? { metaKey: true } : { ctrlKey: true }).passthrough).toBe(false);
+ expect(pane.selection()).toEqual({ start: { x: 2, y: 1 }, end: { x: 13, y: 1 } });
+ pane.press("Backspace");
+ expect(pane.sent).toEqual(["\x7f".repeat(11)]);
+ });
+
+ it("excludes the prompt marker and terminal padding from a full-row drag", async () => {
+ const pane = await setup(" > hello \x1b[5D");
+ pane.select(0, 39);
+ pane.press("Delete");
+ expect(pane.sent).toEqual(["\x7f".repeat(5)]);
+ });
+
+ it("handles an input line wrapped by the terminal", async () => {
+ const pane = await setup("› hello world", false, 10);
+ pane.select(2, 3, 0, 1);
+ pane.press("Backspace");
+ expect(pane.sent).toEqual(["\x7f".repeat(11)]);
+ });
+
+ it("handles select-all ending at the last terminal column", async () => {
+ const pane = await setup("› abcdefgh", false, 10);
+ pane.press("a", { ctrlKey: true });
+ pane.press("Delete");
+ expect(pane.sent).toEqual(["\x7f".repeat(8)]);
+ });
+
+ it("edits a prompt drawn in the alternate screen too", async () => {
+ const pane = await setup("\x1b[?1049h› hello");
+ pane.select(2, 7);
+ pane.press("Backspace");
+ expect(pane.sent).toEqual(["\x7f".repeat(5)]);
+ });
+
+ it("counts wide and combining characters as editing units, not cells or bytes", async () => {
+ const pane = await setup("› 界e\u0301z");
+ pane.select(2, 5);
+ pane.press("Delete");
+ expect(pane.sent).toEqual(["\x1b[D" + "\x7f\x7f"]);
+ });
+
+ it("does not split a wide character", async () => {
+ const pane = await setup("› 界z");
+ pane.select(3, 4);
+ expect(pane.press("Delete").passthrough).toBe(false);
+ expect(pane.sent).toEqual([]);
+ });
+
+ it("never edits the draft when output is selected", async () => {
+ const pane = await setup("earlier output\r\n› hello");
+ pane.select(0, 7);
+ expect(pane.press("Backspace").passthrough).toBe(false);
+ expect(pane.sent).toEqual([]);
+ });
+
+ it("rejects a selection made before a CLI redraw changed the draft", async () => {
+ const pane = await setup();
+ pane.select(2, 13);
+ await pane.write("\r\x1b[2K› new draft");
+ pane.renderFrame();
+ pane.press("Delete");
+ expect(pane.sent).toEqual([]);
+ });
+
+ it("keeps a mouse selection made while the CLI is painting another row", async () => {
+ const pane = await setup("status\r\n› dsadas");
+ // A synchronized TUI frame leaves the old prompt visible while the parser
+ // cursor is temporarily on a status row. Mouseup can happen in that gap.
+ await pane.write("\x1b[1;1HWorking");
+ pane.select(2, 8, 1);
+ await pane.write("\x1b[2;9H");
+ pane.renderFrame();
+ pane.press("Delete");
+ expect(pane.sent).toEqual(["\x7f".repeat(6)]);
+ });
+
+ it("defers Delete until an in-flight repaint restores the editor cursor", async () => {
+ const pane = await setup("status\r\n› dsadas");
+ pane.select(2, 8, 1);
+ await pane.write("\x1b[1;1HWorking");
+ expect(pane.press("Delete").passthrough).toBe(false);
+ expect(pane.sent).toEqual([]);
+ await pane.write("\x1b[2;9H");
+ pane.renderFrame();
+ expect(pane.sent).toEqual(["\x7f".repeat(6)]);
+ pane.cleanup();
+ });
+
+ it("does not apply deferred deletion to a changed draft", async () => {
+ const pane = await setup("status\r\n› dsadas");
+ pane.select(2, 8, 1);
+ await pane.write("\x1b[1;1HWorking");
+ pane.press("Delete");
+ await pane.write("\x1b[2;1H\x1b[2K› new text");
+ pane.renderFrame();
+ expect(pane.sent).toEqual([]);
+ pane.cleanup();
+ });
+
+ it.each(["selection", "key", "dispose"])("cancels deferred deletion after a later %s action", async (action) => {
+ const pane = await setup("status\r\n› dsadas");
+ pane.select(2, 8, 1);
+ await pane.write("\x1b[1;1HWorking");
+ pane.press("Delete");
+ if (action === "selection") pane.select(2, 4, 1);
+ if (action === "key") pane.press("ArrowLeft");
+ if (action === "dispose") pane.cleanup();
+ await pane.write("\x1b[2;9H");
+ pane.renderFrame();
+ expect(pane.sent).toEqual([]);
+ pane.cleanup();
+ });
+
+ it.each(["shell output", "› [Pasted text 20 lines]", "› [Image #1]"])(
+ "leaves an unsupported editor alone: %s", async (output) => {
+ const pane = await setup(output);
+ expect(pane.press("a", { ctrlKey: true }).passthrough).toBe(true);
+ pane.select(2, 7);
+ pane.press("Delete");
+ expect(pane.sent).toEqual([]);
+ },
+ );
+
+ it("leaves ordinary editing, modified keys and IME composition to the CLI", async () => {
+ const pane = await setup();
+ expect(pane.press("Backspace").passthrough).toBe(true);
+ pane.select(2, 7);
+ for (const mods of [{ ctrlKey: true }, { altKey: true }, { metaKey: true }, { isComposing: true }]) {
+ expect(pane.press("Backspace", mods).passthrough).toBe(true);
+ }
+ expect(pane.press("Enter").passthrough).toBe(true);
+ expect(pane.sent).toEqual([]);
+ pane.cleanup();
+ expect(pane.disposed()).toBe(true);
+ expect(pane.press("Delete").passthrough).toBe(true);
+ });
+});
diff --git a/jarvis/ui/web/frontend/src/components/agentic/terminalPromptSelection.ts b/jarvis/ui/web/frontend/src/components/agentic/terminalPromptSelection.ts
new file mode 100644
index 0000000000..aaebe50fe8
--- /dev/null
+++ b/jarvis/ui/web/frontend/src/components/agentic/terminalPromptSelection.ts
@@ -0,0 +1,178 @@
+import type { IBuffer, IBufferCellPosition, IBufferRange } from "@xterm/xterm";
+
+/** The public xterm surface needed to edit a selected, visible prompt. */
+export interface PromptSelectionTerminal {
+ cols: number;
+ buffer: { active: IBuffer };
+ getSelectionPosition(): IBufferRange | undefined;
+ select(column: number, row: number, length: number): void;
+ clearSelection(): void;
+ input(data: string): void;
+ onSelectionChange(listener: () => void): { dispose(): void };
+ onRender(listener: () => void): { dispose(): void };
+}
+
+interface PromptLine {
+ text: string;
+ start: IBufferCellPosition;
+ end: IBufferCellPosition;
+ cursor: number;
+ offsets: Map<number, number>;
+}
+
+const segmenter = typeof Intl.Segmenter === "function"
+ ? new Intl.Segmenter(undefined, { granularity: "grapheme" }) : null;
+const graphemes = (text: string) => [...segmenter!.segment(text)].length;
+
+/**
+ * Only the logical input line containing the live cursor is editable. A
+ * terminal selection otherwise belongs to output, not to the CLI's editor.
+ * Recognise the same input markers as agentic_ide/session.py, without using
+ * provider names. Wrapped buffer rows are one line; separate TUI rows are not
+ * guessed to be input (they can be menus, permission questions or output).
+ */
+function readPrompt(term: PromptSelectionTerminal): PromptLine | null {
+ const buffer = term.buffer.active;
+ if (!segmenter) return null;
+ const cursorRow = buffer.baseY + buffer.cursorY;
+ let first = cursorRow;
+ while (first > buffer.baseY && buffer.getLine(first)?.isWrapped) first--;
+ const head = buffer.getLine(first)?.translateToString(false, 0, term.cols);
+ const prefix = head?.match(/^ *[❯›>] /)?.[0];
+ if (!prefix) return null;
+ let last = cursorRow;
+ while (buffer.getLine(last + 1)?.isWrapped) last++;
+
+ const offsets = new Map<number, number>();
+ let text = "";
+ const start = { x: prefix.length, y: first };
+ let end = start;
+ for (let row = first; row <= last; row++) {
+ const line = buffer.getLine(row);
+ if (!line) return null;
+ const left = row === first ? start.x : 0;
+ // Keep typed spaces through the caret, but omit empty cells after input.
+ let right = term.cols;
+ if (row === last) {
+ right = left;
+ for (let col = left; col < term.cols; col++) {
+ const cell = line.getCell(col);
+ if (cell?.getChars().trim()) right = col + cell.getWidth();
+ }
+ if (row === cursorRow) right = Math.max(right, buffer.cursorX);
+ }
+ for (let col = left; col < right; col++) {
+ const cell = line.getCell(col);
+ if (!cell || cell.getWidth() === 0) continue;
+ offsets.set(row * term.cols + col, text.length);
+ text += cell.getChars() || " ";
+ }
+ offsets.set(row * term.cols + right, text.length);
+ end = { x: right, y: row };
+ }
+ const cursor = offsets.get(cursorRow * term.cols + buffer.cursorX);
+ // Collapsed paste/attachment tokens have editor-specific cursor widths.
+ // Sending one Backspace per displayed letter could erase adjacent input.
+ if (cursor === undefined || /\[(?:Pasted|Image|Attachment)\b/i.test(text)) return null;
+ return { text, start, end, cursor, offsets };
+}
+
+const samePrompt = (a: PromptLine | null, b: PromptLine | null) =>
+ a !== null && b !== null && a.text === b.text && a.cursor === b.cursor &&
+ a.start.x === b.start.x && a.start.y === b.start.y &&
+ a.end.x === b.end.x && a.end.y === b.end.y;
+
+/**
+ * Turn mouse selection + Backspace/Delete into editor cursor moves and deletes.
+ * Never erase the rendered buffer: the CLI owns the draft and must redraw it.
+ */
+export function installPromptSelectionBridge(
+ term: PromptSelectionTerminal,
+ addKeyHandler: (handler: (event: KeyboardEvent) => boolean) => () => void,
+ isMac: boolean,
+): () => void {
+ let selectedPrompt: PromptLine | null = null;
+ // A TUI moves the parser cursor while repainting. Selection belongs to the
+ // last presented frame, not to that temporary drawing cursor.
+ let paintedPrompt = readPrompt(term);
+ let pending: { prompt: PromptLine; selection: IBufferRange } | null = null;
+ let pendingTimer: ReturnType<typeof setTimeout> | undefined;
+ const cancelPending = () => {
+ pending = null;
+ if (pendingTimer !== undefined) clearTimeout(pendingTimer);
+ pendingTimer = undefined;
+ };
+ const selectionListener = term.onSelectionChange(() => {
+ cancelPending();
+ selectedPrompt = term.getSelectionPosition() ? paintedPrompt : null;
+ });
+ const erase = (prompt: PromptLine, selection: IBufferRange) => {
+ const key = (pos: IBufferCellPosition) => pos.y * term.cols + pos.x;
+ if (selection.start.y < prompt.start.y ||
+ key(selection.end) > (prompt.end.y + 1) * term.cols) return false;
+ const start = prompt.offsets.get(Math.max(key(selection.start), key(prompt.start)));
+ const end = prompt.offsets.get(Math.min(key(selection.end), key(prompt.end)));
+ if (start === undefined || end === undefined || start >= end) return false;
+ // A cell boundary can be inside an emoji composed of several code points.
+ const boundaries = new Set([0, ...[...segmenter!.segment(prompt.text)].map((s) => s.index + s.segment.length)]);
+ if (![start, end, prompt.cursor].every((offset) => boundaries.has(offset))) return false;
+ const move = end < prompt.cursor
+ ? "\x1b[D".repeat(graphemes(prompt.text.slice(end, prompt.cursor)))
+ : "\x1b[C".repeat(graphemes(prompt.text.slice(prompt.cursor, end)));
+ const backspaces = "\x7f".repeat(graphemes(prompt.text.slice(start, end)));
+ term.clearSelection();
+ selectedPrompt = null;
+ // One input frame, no Enter, no Ctrl+C and no clipboard round trip.
+ term.input(move + backspaces);
+ return false;
+ };
+ const renderListener = term.onRender(() => {
+ paintedPrompt = readPrompt(term);
+ if (!pending || !paintedPrompt) return;
+ const request = pending;
+ const selection = term.getSelectionPosition();
+ cancelPending();
+ if (samePrompt(request.prompt, paintedPrompt) && selection &&
+ selection.start.x === request.selection.start.x && selection.start.y === request.selection.start.y &&
+ selection.end.x === request.selection.end.x && selection.end.y === request.selection.end.y) {
+ erase(paintedPrompt, selection);
+ }
+ });
+ const removeKeyHandler = addKeyHandler((event) => {
+ if (event.type !== "keydown") return true;
+ cancelPending();
+ if (event.isComposing || event.altKey) return true;
+ const selectAll = event.key.toLowerCase() === "a" && !event.shiftKey &&
+ (isMac ? event.metaKey && !event.ctrlKey : event.ctrlKey && !event.metaKey);
+ if (selectAll) {
+ const prompt = paintedPrompt;
+ if (!prompt) return true;
+ event.preventDefault();
+ const { start, end } = prompt;
+ term.select(start.x, start.y, (end.y - start.y) * term.cols + end.x - start.x);
+ selectedPrompt = prompt;
+ return false;
+ }
+ if (event.ctrlKey || event.metaKey || !["Backspace", "Delete"].includes(event.key)) return true;
+ const selection = term.getSelectionPosition();
+ if (!selection) return true;
+ // A selected output line must never delete unrelated prompt text.
+ event.preventDefault();
+ if (!selectedPrompt) return false;
+ const prompt = readPrompt(term);
+ if (prompt && samePrompt(selectedPrompt, prompt)) return erase(prompt, selection);
+ // Wait for one completed frame instead of silently discarding Delete in
+ // the middle of a synchronized repaint. Never retry a changed draft.
+ if (samePrompt(selectedPrompt, paintedPrompt)) {
+ pending = { prompt: selectedPrompt, selection };
+ pendingTimer = setTimeout(cancelPending, 1_500);
+ }
+ return false;
+ });
+ return () => {
+ cancelPending();
+ selectionListener.dispose();
+ renderListener.dispose();
+ removeKeyHandler();
+ };
+}