@devries-cameron20 / t3code Mirror · read-only / commits / 45dbb4436c
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 11:00 UTC

fix(web): legacy mouse reports reach the PTY byte for byte (#37223)

Wout Stiens committed Nov 15, 2026 at 15:21 UTC 45dbb4436c07001e3bac81c912e63c54c4d9b0af
15 files changed +126 -58
apps/server/src/terminal/Manager.test.ts
+9 -3
@@ -64,7 +64,7 @@ vi.mock("effect/SynchronizedRef", async (importOriginal) => {
64 const encodeUnknownJson = Schema.encodeUnknownSync(Schema.fromJsonString(Schema.Unknown));
65
66 class FakePtyProcess implements PtyAdapter.PtyProcess {
67 - readonly writes: string[] = [];
67 + readonly writes: Array<string | Buffer> = [];
68 readonly resizeCalls: Array<{ cols: number; rows: number }> = [];
69 readonly killSignals: Array<string | undefined> = [];
70 readonly pid: number;
@@ -80,7 +80,7 @@ class FakePtyProcess implements PtyAdapter.PtyProcess {
80 this.pid = pid;
81 }
82
83 - write(data: string): void {
83 + write(data: string | Buffer): void {
84 if (this.writeFailure !== undefined) {
85 throw this.writeFailure;
86 }
@@ -891,9 +891,15 @@ it.layer(
891
892 yield* manager.write({ threadId: "thread-1", terminalId: "default", data: "pwd\n" });
893 yield* manager.write({ threadId: "thread-1", terminalId: "term-2", data: "ls\n" });
894 + yield* manager.write({
895 + threadId: "thread-1",
896 + terminalId: "term-2",
897 + data: "\x1b[M \x80(",
898 + binary: true,
899 + });
900
901 expect(first.writes).toEqual(["pwd\n"]);
896 - expect(second.writes).toEqual(["ls\n"]);
902 + expect(second.writes).toEqual(["ls\n", Buffer.from([0x1b, 0x5b, 0x4d, 0x20, 0x80, 0x28])]);
903 expect(ptyAdapter.spawnInputs).toHaveLength(2);
904 }),
905 );
apps/server/src/terminal/Manager.ts
+1 -1
@@ -3699,7 +3699,7 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func
3699 }
3700 session.inputCount += 1;
3701 yield* Effect.try({
3702 - try: () => process.write(input.data),
3702 + try: () => process.write(PtyAdapter.ptyInput(input.data, input.binary)),
3703 catch: (cause) =>
3704 new TerminalWriteError({
3705 threadId: input.threadId,
apps/server/src/terminal/NodePtyAdapter.ts
+1 -1
@@ -190,7 +190,7 @@ class NodePtyProcess implements PtyAdapter.PtyProcess {
190 return this.process.pid;
191 }
192
193 - write(data: string): void {
193 + write(data: string | Buffer): void {
194 this.process.write(data);
195 }
196
apps/web/src/components/ThreadTerminalDrawer.tsx
+5 -4
@@ -723,6 +723,7 @@ function TerminalSessionViewport({
723 theme: terminalThemeFromApp(mount),
724 font: terminalFontOptions(setupFont.family, setupFont.size),
725 onData: (data) => handleData(data),
726 + onBinary: (data) => void sendTerminalInput(data, "Terminal write failed", true),
727 onResize: (cols, rows) => void resizeTerminal(cols, rows),
728 onSelectionChange: () => {},
729 beforeKey: (event) => handleBeforeKey(event),
@@ -929,12 +930,12 @@ function TerminalSessionViewport({
930
931 const readInputSession = () => appAtomRegistry.get(terminalSessionAtom);
932 const canWriteInput = () => readEnvironmentScope(environmentId, AuthTerminalOperateScope);
932 - const writeInputChunk = (data: string) =>
933 + const writeInputChunk = (data: string, binary?: true) =>
934 retainedTerminalWrite.current({
935 environmentId,
935 - input: { threadId, terminalId, data },
936 + input: binary ? { threadId, terminalId, data, binary } : { threadId, terminalId, data },
937 });
937 - const sendTerminalInput = (data: string, fallbackError: string) => {
938 + const sendTerminalInput = (data: string, fallbackError: string, binary?: true) => {
939 // The server rejects input until the shell runs, and each rejection would print an error.
940 // Input belongs to the shell and surface it was typed into; a restart the client has seen drops what is left.
941 const session = readTerminalSession();
@@ -962,7 +963,7 @@ function TerminalSessionViewport({
963 ) {
964 return "not-dispatched";
965 }
965 - const result = await writeInputChunk(chunk);
966 + const result = await writeInputChunk(chunk, binary);
967 if (result._tag === "Success") return true;
968 if (terminalRef.current === terminal && !isAtomCommandInterrupted(result)) {
969 const error = squashAtomCommandFailure(result);
apps/web/src/components/settings/ProviderAuthTerminal.tsx
+1
@@ -35,6 +35,7 @@ export default function ProviderAuthTerminal({
35 theme: terminalThemeFromApp(element),
36 font: { size: 13 },
37 onData: (data) => respond({ type: "terminal", data }),
38 + onBinary: (data) => respond({ type: "terminal", data, binary: true }),
39 onResize: (cols, rows) => respond({ type: "terminal", data: "", size: { cols, rows } }),
40 onSelectionChange: () => {},
41 beforeKey: (event) => event.key !== "Tab",
apps/web/src/components/settings/SettingsFontPreviews.tsx
+1
@@ -214,6 +214,7 @@ export function TerminalFontPreview({ family, size }: { family: string; size: nu
214 theme: terminalThemeFromApp(mount),
215 font: initialFont,
216 onData: echo,
217 + onBinary: noop,
218 onResize: noop,
219 onSelectionChange: noop,
220 beforeKey: (event) => event.key !== "Tab",
apps/web/src/terminal/ghostty/core.test.ts
+8 -1
@@ -578,7 +578,7 @@ describe("GhosttyTerminalCore mouse encoding", () => {
578 },
579 () => {},
580 );
581 - const mouse = (
581 + const report = (
582 x: number,
583 y: number,
584 paddingTop: number,
@@ -601,6 +601,7 @@ describe("GhosttyTerminalCore mouse encoding", () => {
601 paddingBottom: 279 - paddingTop - 15 * cellHeight,
602 anyButtonPressed: true,
603 });
604 + const mouse = (...args: Parameters<typeof report>) => report(...args).data;
605 try {
606 core.write("\x1b[?1002h\x1b[?1006h");
607 expect(mouse(218.5, 140, 4)).toBe("\x1b[<0;28;8M");
@@ -610,6 +611,12 @@ describe("GhosttyTerminalCore mouse encoding", () => {
611 expect(mouse(500, 140, 4, 305, "release")).toBe("\x1b[<0;37;8m");
612 core.write("\x1b[?1016h");
613 expect(mouse(218.5, 22, 4.5)).toBe("\x1b[<0;215;17M");
614 + core.resize(160, 15, cellWidth, cellHeight);
615 + core.write("\x1b[?1002;1006;1016l\x1b[?1000h");
616 + expect(report(4 + 99.5 * cellWidth, 140, 4, 1261)).toEqual({
617 + data: "\x1b[M \x84(",
618 + binary: true,
619 + });
620 } finally {
621 core.dispose();
622 }
apps/web/src/terminal/ghostty/core.ts
+37 -16
@@ -166,10 +166,25 @@ export interface GhosttyMouseInput {
166 // advertises xterm-256color, whose kf15/kf27/... are CSI 1;m R. Terminfo keys only apply in
167 // keypad-transmit mode (smkx), so that mode marks a terminfo reader; raw parsers never set it.
168 const MODIFIED_F3 = /^\x1b\[13;(\d+)~$/;
169 +/** Mouse report input: UTF-8 text, or one UTF-16 code unit per byte when `binary`. */
170 +interface GhosttyMouseReport {
171 + readonly data: string;
172 + readonly binary: boolean;
173 +}
174
175 const decoder = new TextDecoder("utf-8", { ignoreBOM: true });
176 +const strictDecoder = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true });
177 const encoder = new TextEncoder();
178
179 +// Legacy X10 reports can contain bytes above 0x7f that are not valid UTF-8.
180 +function mouseReport(bytes: Uint8Array): GhosttyMouseReport {
181 + try {
182 + return { data: strictDecoder.decode(bytes), binary: false };
183 + } catch {
184 + return { data: String.fromCharCode(...bytes), binary: true };
185 + }
186 +}
187 +
188 // The encoder only takes whole-pixel cells, so cell reports rescale the pointer onto that grid.
189 function encoderAxis(
190 position: number,
@@ -595,14 +610,16 @@ export class GhosttyTerminalCore {
610 this.runtime.call("ghostty_key_event_set_utf8", this.keyEvent, textPointer, textBytes.length);
611
612 const written = this.runtime.call("ghostty_wasm_alloc_usize");
598 - const encoded = this.encodeOutput(written, (output, outputSize) =>
599 - this.runtime.call(
600 - "ghostty_key_encoder_encode",
601 - this.keyEncoder,
602 - this.keyEvent,
603 - output,
604 - outputSize,
605 - written,
613 + const encoded = decoder.decode(
614 + this.encodeOutput(written, (output, outputSize) =>
615 + this.runtime.call(
616 + "ghostty_key_encoder_encode",
617 + this.keyEncoder,
618 + this.keyEvent,
619 + output,
620 + outputSize,
621 + written,
622 + ),
623 ),
624 );
625 this.runtime.call("ghostty_wasm_free_usize", written);
@@ -657,14 +674,16 @@ export class GhosttyTerminalCore {
674 this.ensureActive();
675 if (!this.modeEnabled(1004)) return "";
676 const written = this.runtime.call("ghostty_wasm_alloc_usize");
660 - const encoded = this.encodeOutput(written, (output, outputSize) =>
661 - this.runtime.call("ghostty_focus_encode", focused ? 0 : 1, output, outputSize, written),
677 + const encoded = decoder.decode(
678 + this.encodeOutput(written, (output, outputSize) =>
679 + this.runtime.call("ghostty_focus_encode", focused ? 0 : 1, output, outputSize, written),
680 + ),
681 );
682 this.runtime.call("ghostty_wasm_free_usize", written);
683 return encoded;
684 }
685
667 - encodeMouse(input: GhosttyMouseInput): string {
686 + encodeMouse(input: GhosttyMouseInput): GhosttyMouseReport {
687 this.ensureActive();
688 this.runtime.call(
689 "ghostty_mouse_encoder_setopt_from_terminal",
@@ -745,7 +764,7 @@ export class GhosttyTerminalCore {
764 ),
765 );
766 this.runtime.call("ghostty_wasm_free_usize", written);
748 - return encoded;
767 + return mouseReport(encoded);
768 }
769
770 setSelection(anchor: GhosttyPointInput, end: GhosttyPointInput): void {
@@ -1065,17 +1084,19 @@ export class GhosttyTerminalCore {
1084 private encodeOutput(
1085 written: number,
1086 encode: (output: number, outputSize: number) => number,
1068 - ): string {
1087 + ): Uint8Array {
1088 const sizeResult = encode(0, 0);
1089 const outputSize = this.runtime.view(written, 4).getUint32(0, true);
1071 - if (sizeResult === GHOSTTY_SUCCESS && outputSize === 0) return "";
1072 - if (sizeResult !== GHOSTTY_OUT_OF_SPACE || outputSize === 0) return "";
1090 + if (sizeResult === GHOSTTY_SUCCESS && outputSize === 0) return new Uint8Array();
1091 + if (sizeResult !== GHOSTTY_OUT_OF_SPACE || outputSize === 0) return new Uint8Array();
1092
1093 const output = this.runtime.alloc(outputSize);
1094 const result = encode(output, outputSize);
1095 const outputLength = this.runtime.view(written, 4).getUint32(0, true);
1096 const encoded =
1078 - result === GHOSTTY_SUCCESS ? decoder.decode(this.runtime.bytes(output, outputLength)) : "";
1097 + result === GHOSTTY_SUCCESS
1098 + ? this.runtime.bytes(output, outputLength).slice()
1099 + : new Uint8Array();
1100 this.runtime.free(output, outputSize);
1101 return encoded;
1102 }
apps/web/src/terminal/ghostty/surface.test.ts
+19 -13
@@ -249,6 +249,7 @@ describe("GhosttyTerminalSurface visibility", () => {
249 cursor: { r: 255, g: 255, b: 255 },
250 },
251 onData,
252 + onBinary() {},
253 onResize() {},
254 onSelectionChange() {},
255 beforeKey: () => false,
@@ -1056,19 +1057,24 @@ describe("isTerminalCompositionKey", () => {
1057
1058 describe("application mouse reporting", () => {
1059 it("drops repeated motion reports until another mouse action resets the cell", () => {
1059 - const first = resolveTerminalMouseData("motion", "\u001b[<35;8;4M", "");
1060 - expect(first).toEqual({ send: true, nextMotionData: "\u001b[<35;8;4M" });
1061 -
1062 - const duplicate = resolveTerminalMouseData("motion", "\u001b[<35;8;4M", first.nextMotionData);
1063 - expect(duplicate).toEqual({ send: false, nextMotionData: "\u001b[<35;8;4M" });
1064 -
1065 - const press = resolveTerminalMouseData("press", "\u001b[<0;8;4M", duplicate.nextMotionData);
1066 - expect(press).toEqual({ send: true, nextMotionData: "" });
1067 -
1068 - expect(resolveTerminalMouseData("motion", "\u001b[<35;8;4M", press.nextMotionData)).toEqual({
1069 - send: true,
1070 - nextMotionData: "\u001b[<35;8;4M",
1071 - });
1060 + const data = "\u001b[<35;8;4M";
1061 + const report = { data, binary: false };
1062 + const first = resolveTerminalMouseData("motion", report, null);
1063 + expect(first).toEqual({ send: true, nextMotionData: report });
1064 +
1065 + const duplicate = resolveTerminalMouseData("motion", report, first.nextMotionData);
1066 + expect(duplicate).toEqual({ send: false, nextMotionData: report });
1067 +
1068 + const press = resolveTerminalMouseData("press", report, duplicate.nextMotionData);
1069 + expect(press).toEqual({ send: true, nextMotionData: null });
1070 + expect(resolveTerminalMouseData("motion", report, press.nextMotionData).send).toBe(true);
1071 +
1072 + const legacy = { data: "\x1b[M@\x80$", binary: true };
1073 + const utf8 = { ...legacy, binary: false };
1074 + const legacyMotion = resolveTerminalMouseData("motion", legacy, null);
1075 + const utf8Motion = resolveTerminalMouseData("motion", utf8, legacyMotion.nextMotionData);
1076 + expect(utf8Motion.send).toBe(true);
1077 + expect(resolveTerminalMouseData("motion", legacy, utf8Motion.nextMotionData).send).toBe(true);
1078 });
1079 });
1080
apps/web/src/terminal/ghostty/surface.ts
+24 -15
@@ -400,15 +400,20 @@ function shouldReportTerminalMouse(
400 }
401
402 type TerminalMouseAction = "press" | "release" | "motion";
403 +type TerminalMouseReport = ReturnType<GhosttyTerminalCore["encodeMouse"]>;
404
405 export function resolveTerminalMouseData(
406 action: TerminalMouseAction,
406 - data: string,
407 - previousMotionData: string,
408 -): { readonly send: boolean; readonly nextMotionData: string } {
409 - const nextMotionData = action === "motion" ? data : "";
407 + report: TerminalMouseReport,
408 + previousMotionData: TerminalMouseReport | null,
409 +): { readonly send: boolean; readonly nextMotionData: TerminalMouseReport | null } {
410 + const nextMotionData = action === "motion" ? report : null;
411 return {
411 - send: data.length > 0 && (action !== "motion" || data !== previousMotionData),
412 + send:
413 + report.data.length > 0 &&
414 + (action !== "motion" ||
415 + report.data !== previousMotionData?.data ||
416 + report.binary !== previousMotionData?.binary),
417 nextMotionData,
418 };
419 }
@@ -416,11 +421,11 @@ export function resolveTerminalMouseData(
421 function resolveTerminalMouseTrackingState(
422 previousTracking: boolean,
423 tracking: boolean,
419 - motionData: string,
420 -): { readonly tracking: boolean; readonly motionData: string } {
424 + motionData: TerminalMouseReport | null,
425 +): { readonly tracking: boolean; readonly motionData: TerminalMouseReport | null } {
426 return {
427 tracking,
423 - motionData: previousTracking === tracking ? motionData : "",
428 + motionData: previousTracking === tracking ? motionData : null,
429 };
430 }
431
@@ -523,6 +528,8 @@ export interface GhosttyTerminalSurfaceOptions {
528 /** Read after font and WASM loading. */
529 readonly visible?: boolean;
530 readonly onData: (data: string) => void;
531 + /** Input bytes that are not UTF-8, one UTF-16 code unit per byte. */
532 + readonly onBinary: (data: string) => void;
533 readonly onResize: (cols: number, rows: number) => void;
534 readonly onSelectionChange: () => void;
535 readonly beforeKey: (event: KeyboardEvent) => boolean;
@@ -637,7 +644,7 @@ export class GhosttyTerminalSurface {
644 remainder: number;
645 moved: boolean;
646 } | null = null;
640 - private lastMouseMotionData = "";
647 + private lastMouseMotionData: TerminalMouseReport | null = null;
648 private mouseAnyEventTracking = false;
649 private dprMedia: MediaQueryList | null = null;
650 // Watch reduced motion changes to restart blink cycles that have no remaining timer.
@@ -783,7 +790,7 @@ export class GhosttyTerminalSurface {
790 resetAndWrite(data: string): void {
791 if (this.disposed) return;
792 this.cancelSynchronizedRenderTimer();
786 - this.lastMouseMotionData = "";
793 + this.lastMouseMotionData = null;
794 this.core.resetAndWrite(data);
795 this.synchronizeMouseTrackingState();
796 this.cursorOn = true;
@@ -1508,7 +1515,7 @@ export class GhosttyTerminalSurface {
1515 this.sendMouse("motion", this.buttonFromButtons(event.buttons), event);
1516 return;
1517 }
1511 - this.lastMouseMotionData = "";
1518 + this.lastMouseMotionData = null;
1519 if (!this.selectionAnchorScreen || !this.canvas.hasPointerCapture(event.pointerId)) {
1520 this.updateHoverCursor(event);
1521 return;
@@ -1586,7 +1593,7 @@ export class GhosttyTerminalSurface {
1593 }
1594
1595 private readonly onPointerLeave = () => {
1589 - this.lastMouseMotionData = "";
1596 + this.lastMouseMotionData = null;
1597 this.clearHoveredLink();
1598 };
1599
@@ -2251,7 +2258,7 @@ export class GhosttyTerminalSurface {
2258
2259 private sendMouse(action: TerminalMouseAction, button: number | null, event: MouseEvent): void {
2260 const bounds = this.canvas.getBoundingClientRect();
2254 - const data = this.core.encodeMouse({
2261 + const report = this.core.encodeMouse({
2262 action,
2263 button,
2264 mods:
@@ -2271,9 +2278,11 @@ export class GhosttyTerminalSurface {
2278 paddingBottom: Math.max(0, bounds.height - this.originY - this.rows * this.metrics.height),
2279 anyButtonPressed: event.buttons !== 0,
2280 });
2274 - const resolution = resolveTerminalMouseData(action, data, this.lastMouseMotionData);
2281 + const resolution = resolveTerminalMouseData(action, report, this.lastMouseMotionData);
2282 this.lastMouseMotionData = resolution.nextMotionData;
2276 - if (resolution.send) this.options.onData(data);
2283 + if (!resolution.send) return;
2284 + if (report.binary) this.options.onBinary(report.data);
2285 + else this.options.onData(report.data);
2286 }
2287
2288 private synchronizeMouseTrackingState(): boolean {
packages/contracts/src/providerSetup.ts
+2
@@ -73,6 +73,8 @@ export const ProviderAuthResponse = Schema.Union([
73 Schema.Struct({
74 type: Schema.Literal("terminal"),
75 data: Schema.String.check(Schema.isMaxLength(4_096)),
76 + // `data` holds one byte per UTF-16 code unit, as in `TerminalWriteInput`.
77 + binary: Schema.optionalKey(Schema.Boolean),
78 size: Schema.optionalKey(
79 Schema.Struct({
80 cols: Schema.Int.check(Schema.isBetween({ minimum: 1, maximum: 500 })),
packages/contracts/src/terminal.ts
+2
@@ -198,6 +198,8 @@ export const TerminalWriteInput = Schema.Struct({
198 data: Schema.String.check(Schema.isNonEmpty()).check(
199 Schema.isMaxLength(TERMINAL_WRITE_MAX_LENGTH),
200 ),
201 + // `data` holds one byte per UTF-16 code unit; older servers write it as UTF-8 text.
202 + binary: Schema.optionalKey(Schema.Boolean),
203 });
204 export type TerminalWriteInput = Schema.Codec.Encoded<typeof TerminalWriteInput>;
205
packages/provider-acp-registry/src/server/auth.test.ts
+8 -2
@@ -77,7 +77,7 @@ const makeHarness = (
77 let exit: ((event: PtyAdapter.PtyExitEvent) => void) | undefined;
78 let killed = false;
79 let closedBeforeTerminal = false;
80 - const written: string[] = [];
80 + const written: Array<string | Buffer> = [];
81 const sizes: number[][] = [];
82 const controller = yield* makeAcpRegistryAuth({
83 instanceId: providerInstanceId,
@@ -305,7 +305,13 @@ it.effect(
305 interactionId: "terminal",
306 response: { type: "terminal", data: "123\r", size: { cols: 100, rows: 30 } },
307 });
308 - assert.deepEqual(h.written, ["123\r"]);
308 + yield* h.controller.respond!("owner", {
309 + instanceId,
310 + flowId: flow.flowId!,
311 + interactionId: "terminal",
312 + response: { type: "terminal", data: "\x1b[M \x80(", binary: true },
313 + });
314 + assert.deepEqual(h.written, ["123\r", Buffer.from([0x1b, 0x5b, 0x4d, 0x20, 0x80, 0x28])]);
315 assert.deepEqual(h.sizes, [[100, 30]]);
316 h.exit(0);
317 yield* h.phase("verifying");
packages/provider-acp-registry/src/server/auth.ts
+2 -1
@@ -231,7 +231,8 @@ export const makeAcpRegistryAuth = Effect.fn("makeAcpRegistryAuth")(function* (o
231 ? Effect.try({
232 try: () => {
233 if (response.size) process.resize(response.size.cols, response.size.rows);
234 - if (response.data) process.write(response.data);
234 + if (response.data)
235 + process.write(PtyAdapter.ptyInput(response.data, response.binary));
236 },
237 catch: () =>
238 failure("respond", "The provider sign-in terminal is no longer available."),
packages/shared/src/PtyAdapter.ts
+6 -1
@@ -23,9 +23,14 @@ export interface PtyExitEvent {
23 signal: number | null;
24 }
25
26 +/** What a PTY receives for client terminal input, whose `binary` data is one byte per code unit. */
27 +export function ptyInput(data: string, binary: boolean | undefined): string | Buffer {
28 + return binary ? Buffer.from(data, "latin1") : data;
29 +}
30 +
31 export interface PtyProcess {
32 readonly pid: number;
28 - write(data: string): void;
33 + write(data: string | Buffer): void;
34 resize(cols: number, rows: number): void;
35 kill(signal?: string): void;
36 onData(callback: (data: string) => void): () => void;