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(terminal): leave transcript browsing when clicking the prompt (#356)

Ruben Lütke280d0c8b3ddc
diff --git a/jarvis/ui/web/frontend/src/components/agentic/AgenticTerminal.tsx b/jarvis/ui/web/frontend/src/components/agentic/AgenticTerminal.tsx
index e13a32008b..ad5a710608 100644
--- a/jarvis/ui/web/frontend/src/components/agentic/AgenticTerminal.tsx
+++ b/jarvis/ui/web/frontend/src/components/agentic/AgenticTerminal.tsx
@@ -145,6 +145,7 @@ import {
} from "./terminalScrollSurface";
import { installMouseSelection } from "./terminalMouseSelection";
import { installPromptSelectionBridge } from "./terminalPromptSelection";
+import { installBrowseExit } from "./terminalBrowseExit";
import {
openPaneSocket,
type PaneSocket,
@@ -1088,6 +1089,7 @@ export function AgenticTerminal({
// mouse tracking on, which every coding agent negotiates at start-up.
const disposeMouseSelection = installMouseSelection(container, term, isMac);
const disposePromptSelection = installPromptSelectionBridge(term, keys.add, isMac);
+ const disposeBrowseExit = installBrowseExit(container, term);
// 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.
@@ -2306,6 +2308,7 @@ export function AgenticTerminal({
disposeFontSync();
disposeMouseSelection();
disposePromptSelection();
+ disposeBrowseExit();
disposeCopyBridge();
disposePasteBridge();
disposeNewlineBridge();
diff --git a/jarvis/ui/web/frontend/src/components/agentic/terminalBrowseExit.test.ts b/jarvis/ui/web/frontend/src/components/agentic/terminalBrowseExit.test.ts
new file mode 100644
index 0000000000..98c5b5f6f5
--- /dev/null
+++ b/jarvis/ui/web/frontend/src/components/agentic/terminalBrowseExit.test.ts
@@ -0,0 +1,172 @@
+import { afterAll, afterEach, describe, expect, it, vi } from "vitest";
+import { Terminal } from "@xterm/xterm";
+import { installBrowseExit } from "./terminalBrowseExit";
+import { installMouseSelection } from "./terminalMouseSelection";
+
+const restoreCanvas = vi.hoisted(() => {
+ const original = HTMLCanvasElement.prototype.getContext;
+ HTMLCanvasElement.prototype.getContext = () => null;
+ return () => { HTMLCanvasElement.prototype.getContext = original; };
+});
+afterAll(restoreCanvas);
+const cleanups: (() => void)[] = [];
+afterEach(() => { cleanups.splice(0).reverse().forEach((cleanup) => cleanup()); });
+
+const FOOTER = " Browsing · ↑↓/jk ←→/hl · ↵ rewind · esc";
+const FRAME = ["Earlier output", "", "", "", "", "", "› Ask Codex to do anything",
+ "", " GPT-6-Astra xhigh", FOOTER].join("\r\n");
+
+async function setup(output = FRAME, cols = 80) {
+ // Real xterm parses the TUI screen. Only layout and transport are faked;
+ // these tests do not launch a browser or send a turn to a provider.
+ const terminal = new Terminal({ cols, rows: 10, allowProposedApi: true });
+ const write = (text: string) => new Promise<void>((resolve) => terminal.write(text, resolve));
+ await write(output);
+ const container = document.createElement("div");
+ const screen = document.createElement("div");
+ screen.className = "xterm-screen";
+ screen.getBoundingClientRect = () => ({
+ x: 10, y: 20, left: 10, top: 20, right: 810, bottom: 220,
+ width: 800, height: 200, toJSON: () => ({}),
+ });
+ container.appendChild(screen);
+ document.body.appendChild(container);
+ let onRender = () => {};
+ const sent: string[] = [];
+ let focused = 0;
+ let cleared = 0;
+ const disposeSelection = installMouseSelection(container, terminal, false);
+ const dispose = installBrowseExit(container, {
+ rows: terminal.rows, buffer: terminal.buffer,
+ input: (data) => sent.push(data), focus: () => { focused++; },
+ clearSelection: () => { cleared++; },
+ onRender: (listener) => {
+ onRender = () => listener({ start: 0, end: 9 });
+ return { dispose: () => { onRender = () => {}; } };
+ },
+ });
+ cleanups.push(() => { dispose(); disposeSelection(); terminal.dispose(); container.remove(); });
+ const pointer = (type: string, row = 6, init: MouseEventInit = {}, target: EventTarget = screen) => {
+ // jsdom has no PointerEvent constructor. Keep real DOM propagation and
+ // mouse coordinates, adding the two pointer fields the bridge consumes.
+ const event = new MouseEvent(type, {
+ bubbles: true, clientX: 100, clientY: 20 + row * 20 + 10, button: 0, ...init,
+ });
+ Object.defineProperties(event, { pointerId: { value: 1 }, isPrimary: { value: true } });
+ target.dispatchEvent(event);
+ };
+ const click = (row = 6, init: MouseEventInit = {}) => {
+ pointer("pointerdown", row, init);
+ screen.dispatchEvent(new MouseEvent("mousedown", { bubbles: true, button: 0, ...init }));
+ pointer("pointerup", row, init);
+ };
+ return { terminal, container, screen, write, pointer, click, sent, dispose,
+ render: () => onRender(), focused: () => focused, cleared: () => cleared };
+}
+
+describe("leaving transcript browsing from the prompt", () => {
+ it.each([51, 80])("restores input with one Escape in a %s-column pane", async (cols) => {
+ const pane = await setup(FRAME, cols);
+ pane.click();
+ expect(pane.sent).toEqual(["\x1b"]);
+ expect(pane.focused()).toBe(1);
+ expect(pane.cleared()).toBe(1);
+ });
+
+ it("still works while the CLI tracks the mouse and xterm forces text selection", async () => {
+ const pane = await setup();
+ await pane.write("\x1b[?1003h");
+ expect(pane.terminal.modes.mouseTrackingMode).toBe("any");
+ pane.click();
+ expect(pane.sent).toEqual(["\x1b"]);
+ });
+
+ it("does not send another Escape until browsing has visibly ended", async () => {
+ const pane = await setup();
+ pane.click(); pane.render(); pane.click();
+ expect(pane.sent).toEqual(["\x1b"]);
+ await pane.write("\x1b[10;1H\x1b[2K ? for shortcuts");
+ pane.render(); pane.click();
+ expect(pane.sent).toEqual(["\x1b"]);
+ await pane.write(`\x1b[10;1H\x1b[2K${FOOTER}`);
+ pane.render(); pane.click();
+ expect(pane.sent).toEqual(["\x1b", "\x1b"]);
+ });
+
+ it.each([0, 5, 7, 8, 9])("preserves output, padding and footer clicks on row %s", async (row) => {
+ const pane = await setup(); pane.click(row);
+ expect(pane.sent).toEqual([]);
+ });
+
+ it.each([{ shiftKey: true }, { altKey: true }, { ctrlKey: true }, { metaKey: true },
+ { button: 1 }, { button: 2 }])("preserves modified and non-primary clicks: %j", async (init) => {
+ const pane = await setup(); pane.click(6, init);
+ expect(pane.sent).toEqual([]);
+ });
+
+ it("preserves a selection drag even when it ends where it started", async () => {
+ const pane = await setup();
+ pane.pointer("pointerdown");
+ pane.pointer("pointermove", 6, { clientX: 160 });
+ pane.pointer("pointerup");
+ expect(pane.sent).toEqual([]);
+ expect(pane.cleared()).toBe(0);
+ });
+
+ it("ignores pointer motion when no prompt click is pending", async () => {
+ const pane = await setup();
+ document.body.dispatchEvent(new Event("pointermove", { bubbles: true }));
+ pane.pointer("pointermove", 0);
+ expect(pane.sent).toEqual([]);
+ });
+
+ it.each(["pointercancel", "blur"])("cancels a pending click on %s", async (type) => {
+ const pane = await setup(); pane.pointer("pointerdown");
+ if (type === "blur") window.dispatchEvent(new Event(type));
+ else pane.pointer(type);
+ pane.pointer("pointerup");
+ expect(pane.sent).toEqual([]);
+ });
+
+ it("does not act if the mode changes during the click", async () => {
+ const pane = await setup(); pane.pointer("pointerdown");
+ await pane.write("\x1b[10;1H\x1b[2K Working · esc to interrupt");
+ pane.pointer("pointerup");
+ expect(pane.sent).toEqual([]);
+ });
+
+ it.each(["Working · esc to interrupt", "? for shortcuts", "Browsing the repository",
+ "Browsing · ↑↓/jk ←→/hl · ↵ rewind", "Permission required · esc to cancel"])(
+ "never interrupts another mode: %s", async (footer) => {
+ const pane = await setup(FRAME.replace(FOOTER, footer)); pane.click();
+ expect(pane.sent).toEqual([]);
+ });
+
+ it("does not treat a quoted footer in output as an active browse mode", async () => {
+ const pane = await setup(FRAME.replace("Earlier output", FOOTER).replace(/Browsing[^\n]+esc$/, "? for shortcuts"));
+ pane.click(); expect(pane.sent).toEqual([]);
+ });
+
+ it("does not act on history scrolled above the live bottom", async () => {
+ const pane = await setup(`old\r\n`.repeat(20) + FRAME);
+ // Without a rendered viewport, xterm's scrollLines does not move it.
+ const buffer = pane.terminal.buffer.active;
+ Object.defineProperty(buffer, "viewportY", { get: () => buffer.baseY - 5 });
+ expect(pane.terminal.buffer.active.viewportY).toBeLessThan(buffer.baseY);
+ pane.click(); expect(pane.sent).toEqual([]);
+ });
+
+ it("does not interpret a press outside the screen as a prompt click", async () => {
+ const pane = await setup();
+ pane.pointer("pointerdown", 6, {}, pane.container);
+ pane.pointer("pointerup");
+ pane.click(6, { clientX: 900 });
+ expect(pane.sent).toEqual([]);
+ });
+
+ it("removes all listeners when the terminal is disposed", async () => {
+ const pane = await setup(); pane.pointer("pointerdown"); pane.dispose();
+ pane.pointer("pointerup"); pane.click(); pane.render();
+ expect(pane.sent).toEqual([]);
+ });
+});
diff --git a/jarvis/ui/web/frontend/src/components/agentic/terminalBrowseExit.ts b/jarvis/ui/web/frontend/src/components/agentic/terminalBrowseExit.ts
new file mode 100644
index 0000000000..edbaf88c0c
--- /dev/null
+++ b/jarvis/ui/web/frontend/src/components/agentic/terminalBrowseExit.ts
@@ -0,0 +1,91 @@
+import type { Terminal } from "@xterm/xterm";
+
+type BrowseTerminal = Pick<Terminal,
+ "rows" | "buffer" | "input" | "focus" | "clearSelection" | "onRender"
+>;
+
+// Match the live navigation footer, never just the word in transcript output.
+const BROWSE_FOOTER = /^\s*Browsing\s+·\s+↑↓\/jk\s+←→\/hl\s+·\s+↵ rewind\s+·\s+esc\s*$/;
+
+/** The dormant composer above a confirmed transcript-navigation footer. */
+function browsePromptRow(term: BrowseTerminal): number | null {
+ const buffer = term.buffer.active;
+ if (buffer.viewportY !== buffer.baseY) return null;
+ const line = (row: number) => {
+ const index = buffer.baseY + row;
+ return index >= 0 && index < buffer.length
+ ? buffer.getLine(index)?.translateToString(true) ?? "" : "";
+ };
+ let footer = term.rows - 1;
+ while (footer >= term.rows - 3 && footer >= 0 && !line(footer).trim()) footer--;
+ if (footer < 0 || !BROWSE_FOOTER.test(line(footer))) return null;
+ // The observed composer is three rows above the footer. Bound the search
+ // to its immediate neighborhood so transcript prompts cannot become input.
+ for (let row = footer - 1; row >= Math.max(0, footer - 5); row--) {
+ if (/^\s*› /u.test(line(row))) return row;
+ }
+ return null;
+}
+
+/**
+ * Clicking the dormant composer leaves transcript browsing, like Escape.
+ * A plain drag still selects text; output clicks and keyboard navigation keep
+ * their existing meaning. No provider identity, polling or synthetic submit.
+ */
+export function installBrowseExit(container: HTMLElement, term: BrowseTerminal): () => void {
+ let press: { id: number; x: number; y: number; row: number } | null = null;
+ let awaitingExit = false;
+ const plain = (event: PointerEvent) => event.button === 0 &&
+ !event.shiftKey && !event.altKey && !event.ctrlKey && !event.metaKey;
+ const rowAt = (event: PointerEvent): number | null => {
+ const screen = container.querySelector(".xterm-screen");
+ if (!screen || !(event.target instanceof Node) || !screen.contains(event.target)) return null;
+ const box = screen.getBoundingClientRect();
+ if (box.width <= 0 || box.height <= 0 || event.clientX < box.left ||
+ event.clientX >= box.right || event.clientY < box.top || event.clientY >= box.bottom) return null;
+ return Math.floor((event.clientY - box.top) * term.rows / box.height);
+ };
+ const onDown = (event: PointerEvent) => {
+ press = null;
+ if (awaitingExit || !plain(event) || !event.isPrimary) return;
+ const row = browsePromptRow(term);
+ if (row !== null && rowAt(event) === row) {
+ press = { id: event.pointerId, x: event.clientX, y: event.clientY, row };
+ }
+ };
+ const onMove = (event: PointerEvent) => {
+ if (press && press.id === event.pointerId &&
+ Math.hypot(event.clientX - press.x, event.clientY - press.y) > 4) press = null;
+ };
+ const onUp = (event: PointerEvent) => {
+ if (!press || press.id !== event.pointerId) return;
+ const start = press;
+ press = null;
+ if (!plain(event) || Math.hypot(event.clientX - start.x, event.clientY - start.y) > 4 ||
+ rowAt(event) !== start.row || browsePromptRow(term) !== start.row) return;
+ // A second click before the redraw must not send another Escape: outside
+ // browsing that could interrupt work or open the navigation mode again.
+ awaitingExit = true;
+ term.clearSelection();
+ term.focus();
+ term.input("\x1b");
+ };
+ const onCancel = () => { press = null; };
+ const render = term.onRender(() => {
+ if (awaitingExit && browsePromptRow(term) === null) awaitingExit = false;
+ });
+ const owner = container.ownerDocument;
+ container.addEventListener("pointerdown", onDown, true);
+ owner.addEventListener("pointermove", onMove, true);
+ owner.addEventListener("pointerup", onUp, true);
+ owner.addEventListener("pointercancel", onCancel, true);
+ owner.defaultView?.addEventListener("blur", onCancel);
+ return () => {
+ container.removeEventListener("pointerdown", onDown, true);
+ owner.removeEventListener("pointermove", onMove, true);
+ owner.removeEventListener("pointerup", onUp, true);
+ owner.removeEventListener("pointercancel", onCancel, true);
+ owner.defaultView?.removeEventListener("blur", onCancel);
+ render.dispose();
+ };
+}