GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — [Bug]: Terminal history keeps OSC 4 palette queries, so every replay answers them again · Issue #40148 · pingdotgg/t3code Closed DescriptionBefore submitting
Areaapps/server Steps to reproduceThe 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 Reproduction A runs on Linux with Node 26 and a checkout. It needs no device, browser, or install.
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 Reproduction C is what the code says happens on a phone, and I did not run it on a device.
Expected behavior
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 behaviorI read 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
I read the clients and did not run them. Web drops replay replies because it detaches its reply writer in Mobile forwarding paths, read and not run:
I sent that reply to bash 5.2.15 as input through a PTY. The prompt showed 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. ImpactMinor 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
Environment
Related
Script repro-osc4.mjsSave outside the checkout and run // 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.tsSave under // 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. ActivityMetadataMetadataAssigneesLabelsNo labels TypeProjectsMilestoneRelationshipsDevelopmentIssue actions |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |