Repository navigation
Description
Before submitting
- I searched existing issues and did not find a duplicate.
- I included enough detail to reproduce or investigate the problem.
Area
apps/server
Steps to reproduce
The server keeps history, a cleaned copy of terminal output for clients that reopen a terminal. A query is a control sequence that asks the terminal for something, and the terminal answers with a reply that goes to the program as input. The server strips queries from history, so that a replay sends no reply. On main, history keeps OSC 4 palette queries. The client's terminal answers each query again on every replay, and a client that forwards replies sends that reply to the shell as input.
Reproduction A runs on Linux with Node 26 and a checkout. It needs no device, browser, or install.
- Save
repro-osc4.mjsfrom the first fold outside the checkout. - Run
node repro-osc4.mjs /path/to/t3code. - Exit
1reproduces the bug,0is expected behavior, and2means setup or a control failed.
Reproduction B in the second fold adds a real server, a real PTY, and a real shell. It stands in for the Android view and is not a device. It reopens a terminal over the server's RPC, replays the history it gets in the Ghostty core with a reply writer attached, and sends what the core answers to the shell with terminal.write, which is the call the mobile app makes for input.
Reproduction C is what the code says happens on a phone, and I did not run it on a device.
- Run
printf '\033]4;1;?\007'in a terminal. - Leave the terminal, and open it again in the Android app.
- From reading the code, I expect
4;1;rgb:cccc/6666/6666to appear at the prompt as if typed.
Expected behavior
docs/internals/terminal-runtime.md at 6f94a06384 states:
Restoring scrollback must not send terminal replies to the current shell.
History should leave out an OSC 4 palette query, as it already leaves out the OSC 11 query, so a replay sends no reply. An OSC 4 that sets a colour should stay in history, because a replay needs it to show the same colours.
Actual behavior
I read shouldStripOscSequence in apps/server/src/terminal/Manager.ts, lines 1273 to 1275. It matches only OSC 10, 11, and 12. When I ran reproduction A on Linux, history kept ESC ]4;1;? BEL. The Ghostty core answered on replay, and the script exited 1:
OSC 4 palette query
output "A<ESC>]4;1;?<BEL>B"
history "A<ESC>]4;1;?<BEL>B"
live reply "<ESC>]4;1;rgb:cccc/6666/6666<BEL>"
replay reply "<ESC>]4;1;rgb:cccc/6666/6666<BEL>"
In the same run, the history filter stripped the script's control, an OSC 11 query, and its replay sent nothing.
When I ran reproduction B on Linux at 1ed4f6a96f, it failed with this assertion:
ASSERTION_FAILED: expected {"replies":"\u001b]4;1;rgb:cccc/6666/6666\u0007","shown":"\u00074;1;rgb:cccc/6666\r6/6666\u0007"} to equal {"replies":"","shown":""}
replies is what the replay made the Ghostty core send, and shown is what the shell showed after it received that as input. The \r inside shown is the shell's line editor redrawing its input line, and the text on screen is 4;1;rgb:cccc/6666/6666.
I read the clients and did not run them. Web drops replay replies because it detaches its reply writer in resetAndWrite, at line 429 of apps/web/src/terminal/ghostty/core.ts, before it writes history. Android forwards replay replies to the shell. iOS sets its write callback before it feeds saved output. iOS uses a Ghostty fork at cf8edc23f3a6a87a96e41a90013e89e987d34980. I did not read that fork, so I do not know whether iOS answers OSC 4.
Mobile forwarding paths, read and not run:
apps/mobile/modules/t3-terminal/android/src/main/cpp/t3_terminal_jni.cpp, line 124,OnWritePtyapps/mobile/modules/t3-terminal/android/src/main/java/expo/modules/t3terminal/T3TerminalView.kt, line 398createTerminal, line 431feedPendingBuffer, line 459feedOutput, line 473emitResponseapps/mobile/src/features/terminal/NativeTerminalSurface.tsx, line 268,handleNativeInputapps/mobile/modules/t3-terminal/ios/T3TerminalView.swift, line 695setupWriteCallback, line 698feedData, line 836 write callback toemitInput, lines 920 to 922emitInputtoonInput
I sent that reply to bash 5.2.15 as input through a PTY. The prompt showed 4;1;rgb:cccc/6666/6666. Pressing Enter made bash split it at ; and print bash: 4: command not found, bash: 1: command not found, and bash: rgb:cccc/6666/6666: No such file or directory.
I ran a 2,005-byte OSC 4 query for 500 colours against the Ghostty core. It replied with 12,500 bytes. A client that forwards replies sends those 12,500 bytes to the shell on every reopen.
Impact
Minor bug or occasional failure
This bug hits a user who reopens a terminal in the mobile app after a program in that terminal asked for a palette colour with OSC 4. The reply lands in the prompt or running program. Web is not affected, because it detaches its reply writer. No user reported it. CodeRabbit raised it in review of #39861, and I confirmed it with reproduction A.
Version or commit
main at 6f94a063845021634c35d8f2d97db558290fdb1d. Reproduction B ran on 1ed4f6a96f93b86839f998ede47c46261c08a4d0, 59 commits earlier, with identical terminal files.
Environment
- Debian 12 in Docker,
node:26.13.1-bookworm, arm64, kernel7.0.14-linuxkit, Node26.13.1. - The script gave the same output on macOS 27.2.1, Apple M5 Pro, Node
26.13.1. - The Ghostty core
3ff6d08fad56f2d6b81420a27bdc56ee707b453e, vendored wasm. - No Android or iOS device.
Related
- [Bug]: ESC Z identity query survives terminal history filtering and replies again on replay #36013 was the same kind of bug for
ESC Z, and fix(server): terminal history drops the ESC Z identity query #36024 fixed it. - [Bug]: Terminal history keeps the tertiary Device Attributes query (CSI = c), so the Android client types
!|00000000into the shell on reopen #35511 was the same kind of bug forCSI = c. The report said the Android client typed the reply into the shell on reopen, and fix(server): terminal history drops the tertiary Device Attributes query #35537 fixed it. - [Bug]: iOS terminal font and theme changes resend old query replies to the PTY #36000 reported that iOS replayed its kept output into a new surface on a font or theme change and sent old replies again. fix(mobile): iOS terminal font and theme changes no longer replay output #36115 stopped those two changes from rebuilding the surface.
- History misses other queries the Ghostty core answers on
main. This issue does not cover them:- OSC 21,
ESC ]21;foreground=? BEL. CSI 21 t, the title report.- A colour query behind a control character, such as
ESC ] LF 11;? BEL. - A colour query in a later slot, such as
ESC ]10;#ffffff;? BEL.
- OSC 21,
- The mobile clients have no guard of the kind web has. A guard there that drops replies during a replay would cover every query at once, and it is a separate change. I read
TerminalOutputBuffer.ktandTerminalOutputBuffer.swiftand did not run them. Android and iOS also keep their own copy of the output they received, live output included, and feed it to a new terminal with the reply writer attached when they rebuild the view. Live output never passes the server's history filter, so no server change reaches that replay. - fix(server): terminal history drops OSC 4 palette queries #40149 fixes this bug.
Script repro-osc4.mjs
Save outside the checkout and run node repro-osc4.mjs /path/to/t3code.
// Runs the checkout's terminal history filter and its vendored Ghostty core in Node.
// usage: node repro-osc4.mjs /path/to/t3code
// Exit 1: history kept an OSC 4 palette query and a replay answered it. Exit 0: it did not.
// Exit 2: setup or a control failed. Reads the checkout, writes nothing.
import fs from "node:fs";
import path from "node:path";
import { stripTypeScriptTypes } from "node:module";
const show = (text) => JSON.stringify(text).replaceAll("\\u001b", "<ESC>").replaceAll("\\u0007", "<BEL>");
let status = 2;
try {
if (!process.argv[2]) throw new Error("pass the checkout path as the only argument");
const root = path.resolve(process.argv[2]);
// The filter, cut out of Manager.ts as it is and loaded without the rest of the server.
const source = fs.readFileSync(path.join(root, "apps/server/src/terminal/Manager.ts"), "utf8");
const from = source.indexOf("function isCsiFinalByte(");
const to = source.indexOf("\nfunction ", source.indexOf("function sanitizeTerminalHistoryChunk(") + 10);
if (from < 0 || to < 0) throw new Error("sanitizeTerminalHistoryChunk not found in Manager.ts");
// Works whether or not the checkout exports the function, and supplies the one constant the
// filter may read from @t3tools/contracts.
const contracts = fs.readFileSync(path.join(root, "packages/contracts/src/terminal.ts"), "utf8");
const limit = /export (const DEFAULT_MAX_TERMINAL_BUFFER_BYTES = [^;]+;)/.exec(contracts)?.[1] ?? "";
const cut =
limit +
source
.slice(from, to)
.replace("export function sanitizeTerminalHistoryChunk", "function sanitizeTerminalHistoryChunk");
const filter = await import(
"data:text/javascript;base64," +
Buffer.from(stripTypeScriptTypes(cut + "\nexport { sanitizeTerminalHistoryChunk };")).toString("base64")
);
const historyOf = (output) => filter.sanitizeTerminalHistoryChunk("", output).visibleText;
// The Ghostty core the web client ships. Android builds the same revision.
const vendor = path.join(root, "apps/web/src/terminal/ghostty/vendor");
const core = await WebAssembly.instantiate(fs.readFileSync(path.join(vendor, "ghostty-vt.wasm")), {
env: { log: () => {} },
});
const ghostty = core.instance.exports;
const bytes = (pointer, length) => new Uint8Array(ghostty.memory.buffer, pointer, length);
const view = () => new DataView(ghostty.memory.buffer);
const decode = (pointer, length) => new TextDecoder().decode(bytes(pointer, length).slice());
let replies = "";
const writer = await WebAssembly.instantiate(
fs.readFileSync(path.join(vendor, "ghostty-write-pty.wasm")),
{ env: { t3_write_pty: (_terminal, _userdata, pointer, length) => (replies += decode(pointer, length)) } },
);
const writerIndex = ghostty.__indirect_function_table.length;
ghostty.__indirect_function_table.grow(1, writer.instance.exports.ghostty_write_pty);
// Feeds text to a new 80x24 terminal that has a reply writer, as a mobile client's has, and
// returns what the terminal sent back.
const repliesTo = (text) => {
const options = ghostty.ghostty_wasm_alloc_u8_array(8);
const slot = ghostty.ghostty_wasm_alloc_u8_array(4);
bytes(options, 8).fill(0);
view().setUint16(options, 80, true);
view().setUint16(options + 2, 24, true);
if (ghostty.ghostty_terminal_new(0, slot, options) !== 0) throw new Error("ghostty_terminal_new failed");
const terminal = view().getUint32(slot, true);
ghostty.ghostty_terminal_set(terminal, 0, 1);
ghostty.ghostty_terminal_set(terminal, 1, writerIndex);
// A background colour, so the OSC 11 control below has something to report.
const colour = ghostty.ghostty_wasm_alloc_u8_array(3);
bytes(colour, 3).fill(0);
ghostty.ghostty_terminal_set(terminal, 12, colour);
const data = Buffer.from(text);
const pointer = ghostty.ghostty_wasm_alloc_u8_array(data.length);
bytes(pointer, data.length).set(data);
replies = "";
ghostty.ghostty_terminal_vt_write(terminal, pointer, data.length);
ghostty.ghostty_terminal_free(terminal);
return replies;
};
let commit = "unknown commit";
try {
const { execFileSync } = await import("node:child_process");
commit = execFileSync("git", ["-C", root, "rev-parse", "HEAD"], { encoding: "utf8", stdio: ["ignore", "pipe", "ignore"] }).trim();
} catch {}
console.log(`T3 Code ${commit}, Ghostty ${fs.readFileSync(path.join(root, "native/libghostty-vt/VERSION"), "utf8").trim()}, Node ${process.version}, ${process.platform} ${process.arch}`);
const run = (name, output) => {
const history = historyOf(output);
const result = { live: repliesTo(output), replay: repliesTo(history), history };
console.log(`\n${name}\n output ${show(output)}\n history ${show(history)}\n live reply ${show(result.live)}\n replay reply ${show(result.replay)}`);
return result;
};
// Control: the filter already drops an OSC 11 background query, so its replay is silent.
const control = run("OSC 11 background query (control)", "A\u001b]11;?\u0007B");
if (control.live === "" || control.replay !== "" || control.history !== "AB") {
throw new Error("the OSC 11 control did not behave as expected");
}
// Control: an OSC 4 that sets a colour must stay in history.
const set = run("OSC 4 palette change (control)", "A\u001b]4;1;rgb:12/34/56\u0007B");
if (set.history !== "A\u001b]4;1;rgb:12/34/56\u0007B" || set.replay !== "") {
throw new Error("the OSC 4 palette change control did not behave as expected");
}
const query = run("OSC 4 palette query", "A\u001b]4;1;?\u0007B");
if (query.live === "") throw new Error("Ghostty did not answer the live OSC 4 query");
status = query.replay === "" ? 0 : 1;
} catch (error) {
console.log(`setup error: ${error.message}`);
}
console.log(
status === 1
? "\nRESULT: defect reproduced. History kept the OSC 4 query and the replay answered it."
: status === 0
? "\nRESULT: expected behavior. The replay of history sent no reply."
: "\nRESULT: inconclusive",
);
process.exit(status);Scenario terminal-reopen-sends-no-palette-reply.e2e.ts
Save under scripts/scenarios/ and run cd scripts && pnpm exec e2e run scenarios/terminal-reopen-sends-no-palette-reply.e2e.ts.
// Not for commit. A client that forwards terminal replies to the shell, as the Android view does,
// reopens a terminal whose output held an OSC 4 palette query. The reopen must send nothing.
import { ThreadId } from "@t3tools/contracts";
import { expect } from "e2e";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import { APP_DIR, T3_HOME } from "../e2e.config.ts";
import { rpcClient, runPromise, test, WEB } from "./t3.ts";
/** Feeds `text` to a new Ghostty terminal that has a reply writer and returns what it sent. */
async function ghosttyReplies(text: string): Promise<string> {
const read = (file: string) =>
runPromise(
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
return yield* fs.readFile(`${APP_DIR}/apps/web/src/terminal/ghostty/vendor/${file}`);
}),
);
const core = await WebAssembly.instantiate(await read("ghostty-vt.wasm"), {
env: { log: () => {} },
});
const ghostty = core.instance.exports as Record<string, (...args: number[]) => number> & {
memory: WebAssembly.Memory;
__indirect_function_table: WebAssembly.Table;
};
const bytes = (pointer: number, length: number) =>
new Uint8Array(ghostty.memory.buffer, pointer, length);
const view = () => new DataView(ghostty.memory.buffer);
let replies = "";
const writer = await WebAssembly.instantiate(await read("ghostty-write-pty.wasm"), {
env: {
t3_write_pty: (_terminal: number, _userdata: number, pointer: number, length: number) => {
replies += new TextDecoder().decode(bytes(pointer, length).slice());
},
},
});
const writerIndex = ghostty.__indirect_function_table.length;
ghostty.__indirect_function_table.grow(1, writer.instance.exports.ghostty_write_pty);
const options = ghostty.ghostty_wasm_alloc_u8_array!(8);
const slot = ghostty.ghostty_wasm_alloc_u8_array!(4);
bytes(options, 8).fill(0);
view().setUint16(options, 80, true);
view().setUint16(options + 2, 24, true);
if (ghostty.ghostty_terminal_new!(0, slot, options) !== 0) throw new Error("no terminal");
const terminal = view().getUint32(slot, true);
ghostty.ghostty_terminal_set!(terminal, 0, 1);
ghostty.ghostty_terminal_set!(terminal, 1, writerIndex);
const data = new TextEncoder().encode(text);
const pointer = ghostty.ghostty_wasm_alloc_u8_array!(data.length);
bytes(pointer, data.length).set(data);
ghostty.ghostty_terminal_vt_write!(terminal, pointer, data.length);
ghostty.ghostty_terminal_free!(terminal);
return replies;
}
test(
"Reopening a terminal after an OSC 4 palette query sends no reply to the shell",
WEB,
async ({ t3, app }) => {
const cwd = await t3.project("demo");
const thread = await t3.thread({ project: cwd, title: "Terminal", messages: ["hello"] });
const threadId = ThreadId.make(thread.threadId);
const terminalId = "shell";
const origin = app.baseUrl ?? "";
const open = () =>
runPromise(
Effect.gen(function* () {
const client = yield* rpcClient(T3_HOME, origin);
return yield* client["terminal.open"]({ threadId, terminalId, cwd, cols: 80, rows: 24 });
}).pipe(Effect.scoped),
);
const write = (data: string) =>
runPromise(
Effect.gen(function* () {
const client = yield* rpcClient(T3_HOME, origin);
yield* client["terminal.write"]({ threadId, terminalId, data });
}).pipe(Effect.scoped),
);
await open();
// Input sent before the shell runs is dropped, so retype until the marker shows.
await expect
.poll(
async () => {
await write("echo shell-is-$((40+2))\r");
return (await open()).history;
},
{ timeout: 30_000 },
)
.toContain("shell-is-42");
// A program asks for palette colour 1. No terminal is attached, so nothing answers it live.
await write(String.raw`printf 'A\033]4;1;?\007B-%s\n' done` + "\r");
await expect.poll(async () => (await open()).history, { timeout: 30_000 }).toContain("B-done");
// The client reopens the terminal: it replays the history it is given, and what its terminal
// answers goes to the shell as input.
const reopened = (await open()).history;
const replies = await ghosttyReplies(reopened);
if (replies !== "") {
await write(replies);
await expect
.poll(async () => (await open()).history.slice(reopened.length), { timeout: 30_000 })
.toContain("rgb:");
}
const shown = (await open()).history.slice(reopened.length);
await app.screenshot("after-reopen");
// What the replay made the terminal send, and what the shell showed once it got that as input.
expect({ replies, shown }).toEqual({ replies: "", shown: "" });
},
);Found by CodeRabbit in review of #39861. Reproduced by Claude Fable 5.2. Written by Gemini 4 Neon.
Activity