Improve mobile terminal input, selection, and resizing (#1607)
* Improve mobile terminal typing and selection * Address mobile terminal review feedback * Remove dead native terminal input branch * Replay bracketed paste in terminal preamble * Stop per-key terminal resize claims * Stabilize terminal resize browser test * Use tracked terminal mode for native paste * Restore terminal toolbar focus handling * Adapt native terminal to active visibility state * Stabilize native mobile terminal interactions * Add native terminal fallback boundary * test(app): expect explicit terminal resize reclaim * fix(app): stabilize terminal buffer coordinates * fix(app): restrict terminal resize ownership * fix(terminal): gate restored input modes * fix(app): recover stalled terminal WebViews * chore(app): remove obsolete terminal harnesses * fix(app): make terminal resize claims explicit * test(app): stabilize native terminal device flows * test(app): track absolute scrollback coordinates * fix(app): isolate terminal WebView streams * fix(terminal): arbitrate size ownership across clients The daemon now distinguishes explicit ownership claims from passive geometry updates. This keeps the active client authoritative while allowing its split, viewport, and keyboard changes to resize the PTY without idle clients stealing it. * test(server): make worker resize assertion portable Query the worker's authoritative terminal snapshot instead of invoking a Unix-only shell command, so the resize regression exercises the same boundary on Windows and Unix.
This commit is contained in:
@@ -0,0 +1,123 @@
|
||||
import { describe, expect, test } from "vitest";
|
||||
|
||||
import {
|
||||
TerminalStreamOpcode,
|
||||
encodeTerminalResizePayload,
|
||||
type TerminalStreamFrame,
|
||||
} from "@getpaseo/protocol/binary-frames/index";
|
||||
import type { SessionOutboundMessage, TerminalState } from "@getpaseo/protocol/messages";
|
||||
import type pino from "pino";
|
||||
|
||||
import { TerminalSessionController } from "./terminal-session-controller.js";
|
||||
import type { TerminalManager } from "./terminal-manager.js";
|
||||
import type { TerminalSession } from "./terminal.js";
|
||||
|
||||
function createFixture(): {
|
||||
terminal: TerminalSession;
|
||||
terminalManager: TerminalManager;
|
||||
appliedSizes: string[];
|
||||
} {
|
||||
const appliedSizes: string[] = [];
|
||||
let size = { rows: 24, cols: 80 };
|
||||
const state = (): TerminalState => ({
|
||||
rows: size.rows,
|
||||
cols: size.cols,
|
||||
grid: [],
|
||||
scrollback: [],
|
||||
cursor: { row: 0, col: 0 },
|
||||
});
|
||||
const terminal = {
|
||||
id: "terminal-1",
|
||||
name: "Terminal",
|
||||
cwd: "/tmp",
|
||||
workspaceId: "workspace-1",
|
||||
getSize: () => size,
|
||||
send: (message: { type: string; rows?: number; cols?: number }) => {
|
||||
if (message.type !== "resize" || message.rows === undefined || message.cols === undefined) {
|
||||
return;
|
||||
}
|
||||
size = { rows: message.rows, cols: message.cols };
|
||||
appliedSizes.push(`${message.cols}x${message.rows}`);
|
||||
},
|
||||
subscribe: () => () => {},
|
||||
onExit: () => () => {},
|
||||
} as unknown as TerminalSession;
|
||||
const terminalManager = {
|
||||
getTerminal: () => terminal,
|
||||
getTerminalState: async () => ({ state: state(), revision: 0 }),
|
||||
} as unknown as TerminalManager;
|
||||
return { terminal, terminalManager, appliedSizes };
|
||||
}
|
||||
|
||||
function createController(
|
||||
terminalManager: TerminalManager,
|
||||
outbound: SessionOutboundMessage[] = [],
|
||||
): TerminalSessionController {
|
||||
return new TerminalSessionController({
|
||||
terminalManager,
|
||||
emit: (message) => outbound.push(message),
|
||||
emitBinary: () => {},
|
||||
hasBinaryChannel: () => true,
|
||||
isPathWithinRoot: () => false,
|
||||
sessionLogger: { warn: () => {}, error: () => {} } as unknown as pino.Logger,
|
||||
});
|
||||
}
|
||||
|
||||
function resizeFrame(input: {
|
||||
rows: number;
|
||||
cols: number;
|
||||
intent?: "claim" | "update";
|
||||
}): TerminalStreamFrame {
|
||||
return {
|
||||
opcode: TerminalStreamOpcode.Resize,
|
||||
slot: 0,
|
||||
payload: encodeTerminalResizePayload(input),
|
||||
};
|
||||
}
|
||||
|
||||
describe("terminal session controller size ownership", () => {
|
||||
test("routes JSON, binary, and attach sizes through one connection-owned arbiter", async () => {
|
||||
const { terminalManager, appliedSizes } = createFixture();
|
||||
const controllerA = createController(terminalManager);
|
||||
const controllerB = createController(terminalManager);
|
||||
|
||||
controllerA.dispatch({
|
||||
type: "terminal_input",
|
||||
terminalId: "terminal-1",
|
||||
message: { type: "resize", rows: 30, cols: 100, intent: "claim" },
|
||||
});
|
||||
controllerA.dispatch({
|
||||
type: "terminal_input",
|
||||
terminalId: "terminal-1",
|
||||
message: { type: "resize", rows: 31, cols: 101, intent: "update" },
|
||||
});
|
||||
controllerB.dispatch({
|
||||
type: "terminal_input",
|
||||
terminalId: "terminal-1",
|
||||
message: { type: "resize", rows: 32, cols: 102, intent: "update" },
|
||||
});
|
||||
|
||||
await controllerB.dispatch({
|
||||
type: "subscribe_terminal_request",
|
||||
terminalId: "terminal-1",
|
||||
requestId: "subscribe-b",
|
||||
restore: { mode: "live" },
|
||||
});
|
||||
controllerB.handleBinaryFrame(resizeFrame({ rows: 33, cols: 103, intent: "claim" }));
|
||||
controllerA.dispatch({
|
||||
type: "terminal_input",
|
||||
terminalId: "terminal-1",
|
||||
message: { type: "resize", rows: 34, cols: 104, intent: "update" },
|
||||
});
|
||||
|
||||
await controllerA.dispatch({
|
||||
type: "subscribe_terminal_request",
|
||||
terminalId: "terminal-1",
|
||||
requestId: "subscribe-a",
|
||||
restore: { mode: "live", size: { rows: 35, cols: 105 } },
|
||||
});
|
||||
controllerB.handleBinaryFrame(resizeFrame({ rows: 36, cols: 106, intent: "update" }));
|
||||
|
||||
expect(appliedSizes).toEqual(["100x30", "101x31", "103x33", "105x35"]);
|
||||
});
|
||||
});
|
||||
@@ -74,7 +74,7 @@ describe("terminal-session-controller restore", () => {
|
||||
getSize: () => ({ rows: 1, cols: 80 }),
|
||||
getState: () => terminalState("restore-before"),
|
||||
getStateSnapshot: () => ({ state: terminalState("restore-before"), revision: 1 }),
|
||||
getReplayPreamble: () => "",
|
||||
getReplayPreamble: () => "\x1b[?1h\x1b[?2004h",
|
||||
getTitle: () => undefined,
|
||||
getActivity: () => null,
|
||||
setActivity: vi.fn(),
|
||||
@@ -146,7 +146,9 @@ describe("terminal-session-controller restore", () => {
|
||||
TerminalStreamOpcode.Output,
|
||||
]);
|
||||
expect(new TextDecoder().decode(binaryFrames[0]?.payload)).toContain("restore-before");
|
||||
expect(new TextDecoder().decode(binaryFrames[1]?.payload)).toBe("restore-after\n");
|
||||
expect(new TextDecoder().decode(binaryFrames[1]?.payload)).toBe(
|
||||
"\x1b[?1h\x1b[?2004hrestore-after\n",
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -33,6 +33,7 @@ import {
|
||||
} from "./terminal-restore.js";
|
||||
import type { TerminalSession } from "./terminal.js";
|
||||
import type { TerminalManager, TerminalsChangedEvent } from "./terminal-manager.js";
|
||||
import { applyTerminalSize } from "./terminal-size-ownership.js";
|
||||
import type { TerminalActivity } from "@getpaseo/protocol/terminal-activity";
|
||||
import { terminalSubscriptionKey } from "@getpaseo/protocol/terminal-subscription-key";
|
||||
|
||||
@@ -130,6 +131,7 @@ export class TerminalSessionController {
|
||||
private readonly listTerminalWorkspaceRoots: () => Promise<readonly string[]>;
|
||||
private readonly clientSupportsWrapReflow: () => boolean;
|
||||
private readonly getClientBufferedAmount: () => number | null;
|
||||
private readonly terminalSizeOwner = {};
|
||||
|
||||
// A subscription is scoped to a (cwd, workspaceId) pair, keyed by
|
||||
// terminalSubscriptionKey: two workspaces sharing a cwd subscribe and unsub
|
||||
@@ -239,7 +241,7 @@ export class TerminalSessionController {
|
||||
if (!resize) {
|
||||
return;
|
||||
}
|
||||
terminal.send({ type: "resize", rows: resize.rows, cols: resize.cols });
|
||||
applyTerminalSize(terminal, this.terminalSizeOwner, resize);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -660,17 +662,10 @@ export class TerminalSessionController {
|
||||
this.ensureExitSubscription(session);
|
||||
|
||||
if (msg.restore?.size) {
|
||||
const currentSize = session.getSize();
|
||||
if (
|
||||
currentSize.rows !== msg.restore.size.rows ||
|
||||
currentSize.cols !== msg.restore.size.cols
|
||||
) {
|
||||
session.send({
|
||||
type: "resize",
|
||||
rows: msg.restore.size.rows,
|
||||
cols: msg.restore.size.cols,
|
||||
});
|
||||
}
|
||||
applyTerminalSize(session, this.terminalSizeOwner, {
|
||||
...msg.restore.size,
|
||||
intent: "claim",
|
||||
});
|
||||
}
|
||||
|
||||
const slot = this.bindActiveStream(session, { restore: msg.restore });
|
||||
@@ -725,10 +720,8 @@ export class TerminalSessionController {
|
||||
this.ensureExitSubscription(session);
|
||||
|
||||
if (msg.message.type === "resize") {
|
||||
const currentSize = session.getSize();
|
||||
if (currentSize.rows === msg.message.rows && currentSize.cols === msg.message.cols) {
|
||||
return;
|
||||
}
|
||||
applyTerminalSize(session, this.terminalSizeOwner, msg.message);
|
||||
return;
|
||||
}
|
||||
|
||||
session.send(msg.message);
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
import { describe, expect, test } from "vitest";
|
||||
|
||||
import type { TerminalSession } from "./terminal.js";
|
||||
import { applyTerminalSize } from "./terminal-size-ownership.js";
|
||||
|
||||
function createTerminal(): { terminal: TerminalSession; appliedSizes: string[] } {
|
||||
const appliedSizes: string[] = [];
|
||||
let size = { rows: 24, cols: 80 };
|
||||
const terminal = {
|
||||
getSize: () => size,
|
||||
send: (message: { type: "resize"; rows: number; cols: number }) => {
|
||||
size = { rows: message.rows, cols: message.cols };
|
||||
appliedSizes.push(`${message.cols}x${message.rows}`);
|
||||
},
|
||||
} as TerminalSession;
|
||||
return { terminal, appliedSizes };
|
||||
}
|
||||
|
||||
describe("terminal size ownership", () => {
|
||||
test("only the latest claimant can update a terminal size", () => {
|
||||
const { terminal, appliedSizes } = createTerminal();
|
||||
const ownerA = {};
|
||||
const ownerB = {};
|
||||
|
||||
expect(applyTerminalSize(terminal, ownerA, { rows: 30, cols: 100, intent: "claim" })).toBe(
|
||||
true,
|
||||
);
|
||||
expect(applyTerminalSize(terminal, ownerA, { rows: 31, cols: 101, intent: "update" })).toBe(
|
||||
true,
|
||||
);
|
||||
expect(applyTerminalSize(terminal, ownerB, { rows: 32, cols: 102, intent: "update" })).toBe(
|
||||
false,
|
||||
);
|
||||
expect(applyTerminalSize(terminal, ownerB, { rows: 33, cols: 103, intent: "claim" })).toBe(
|
||||
true,
|
||||
);
|
||||
expect(applyTerminalSize(terminal, ownerA, { rows: 34, cols: 104, intent: "update" })).toBe(
|
||||
false,
|
||||
);
|
||||
expect(applyTerminalSize(terminal, ownerA, { rows: 35, cols: 105 })).toBe(true);
|
||||
expect(applyTerminalSize(terminal, ownerB, { rows: 36, cols: 106, intent: "update" })).toBe(
|
||||
false,
|
||||
);
|
||||
|
||||
expect(appliedSizes).toEqual(["100x30", "101x31", "103x33", "105x35"]);
|
||||
});
|
||||
|
||||
test("transfers ownership when a new claimant reports the current size", () => {
|
||||
const { terminal, appliedSizes } = createTerminal();
|
||||
const ownerA = {};
|
||||
const ownerB = {};
|
||||
|
||||
applyTerminalSize(terminal, ownerA, { rows: 30, cols: 100, intent: "claim" });
|
||||
applyTerminalSize(terminal, ownerB, { rows: 30, cols: 100, intent: "claim" });
|
||||
applyTerminalSize(terminal, ownerA, { rows: 31, cols: 101, intent: "update" });
|
||||
applyTerminalSize(terminal, ownerB, { rows: 32, cols: 102, intent: "update" });
|
||||
|
||||
expect(appliedSizes).toEqual(["100x30", "102x32"]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,38 @@
|
||||
import type { TerminalSession } from "./terminal.js";
|
||||
|
||||
interface TerminalSizeRequest {
|
||||
rows: number;
|
||||
cols: number;
|
||||
intent?: "claim" | "update";
|
||||
}
|
||||
|
||||
const terminalSizeOwners = new WeakMap<TerminalSession, object>();
|
||||
|
||||
export function applyTerminalSize(
|
||||
terminal: TerminalSession,
|
||||
owner: object,
|
||||
request: TerminalSizeRequest,
|
||||
): boolean {
|
||||
const intent = resolveTerminalSizeIntent(request.intent);
|
||||
if (intent === "update" && terminalSizeOwners.get(terminal) !== owner) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (intent === "claim") {
|
||||
terminalSizeOwners.set(terminal, owner);
|
||||
}
|
||||
|
||||
const currentSize = terminal.getSize();
|
||||
if (currentSize.rows !== request.rows || currentSize.cols !== request.cols) {
|
||||
terminal.send({ type: "resize", rows: request.rows, cols: request.cols });
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
function resolveTerminalSizeIntent(intent: TerminalSizeRequest["intent"]): "claim" | "update" {
|
||||
if (intent) {
|
||||
return intent;
|
||||
}
|
||||
// COMPAT(terminalSizeOwnership): added in v0.2.6, remove after 2027-02-02 once the client floor sends resize intent.
|
||||
return "claim";
|
||||
}
|
||||
@@ -130,21 +130,27 @@ function watchTerminal(session: TerminalSession): void {
|
||||
});
|
||||
outputCoalescerByTerminalId.set(session.id, outputCoalescer);
|
||||
|
||||
const unsubscribeMessage = session.subscribe((message) => {
|
||||
if (message.type === "output") {
|
||||
pendingOutputRevision = message.revision;
|
||||
outputCoalescer.handle(message.data);
|
||||
return;
|
||||
}
|
||||
// Non-output messages (snapshot/snapshotReady/titleChange) must not jump
|
||||
// ahead of buffered output: flush the coalescer first, then forward.
|
||||
outputCoalescer.flush();
|
||||
sendToParent({
|
||||
type: "terminalMessage",
|
||||
terminalId: session.id,
|
||||
message,
|
||||
});
|
||||
});
|
||||
const unsubscribeMessage = session.subscribe(
|
||||
(message) => {
|
||||
if (message.type === "output") {
|
||||
pendingOutputRevision = message.revision;
|
||||
outputCoalescer.handle(message.data);
|
||||
return;
|
||||
}
|
||||
// Non-output messages (snapshot/snapshotReady/titleChange) must not jump
|
||||
// ahead of buffered output: flush the coalescer first, then forward.
|
||||
outputCoalescer.flush();
|
||||
sendToParent({
|
||||
type: "terminalMessage",
|
||||
terminalId: session.id,
|
||||
message,
|
||||
});
|
||||
},
|
||||
// Creation already sends an authoritative state in terminalCreated. A second
|
||||
// asynchronous state snapshot can arrive after the create response and
|
||||
// overwrite a resize issued immediately by the caller.
|
||||
{ initialSnapshot: "ready" },
|
||||
);
|
||||
const unsubscribeExit = session.onExit((info) => {
|
||||
outputCoalescer.flush();
|
||||
clearTerminalSubscriptions(session.id);
|
||||
|
||||
@@ -352,12 +352,10 @@ it("refreshes cached terminal size after worker resize", async () => {
|
||||
const session = trackTerminal(await manager.createTerminal({ cwd, workspaceId: "ws-test" }));
|
||||
|
||||
session.send({ type: "resize", rows: 10, cols: 40 });
|
||||
const snapshot = await manager.getTerminalState(session.id);
|
||||
|
||||
await waitForCondition(() => {
|
||||
const size = session.getSize();
|
||||
return size.rows === 10 && size.cols === 40;
|
||||
}, 10000);
|
||||
|
||||
expect(snapshot?.state.rows).toBe(10);
|
||||
expect(snapshot?.state.cols).toBe(40);
|
||||
expect(session.getState().rows).toBe(10);
|
||||
expect(session.getState().cols).toBe(40);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user