diff --git a/apps/server/src/terminal/Manager.test.ts b/apps/server/src/terminal/Manager.test.ts index dea7d3f31a3f..94c0e3d30808 100644 --- a/apps/server/src/terminal/Manager.test.ts +++ b/apps/server/src/terminal/Manager.test.ts @@ -3,6 +3,7 @@ import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; import * as NodeServices from "@effect/platform-node/NodeServices"; import { assert, it } from "@effect/vitest"; import { + DEFAULT_MAX_TERMINAL_BUFFER_BYTES, DEFAULT_TERMINAL_ID, ProjectId, ThreadId, @@ -487,6 +488,153 @@ it("restores bounded history across append compaction, clears and split Unicode" expect(persisted).toBe("z".repeat(maxBytes)); }); +it("stops holding a control sequence that outgrows its limit", () => { + const stringLimit = DEFAULT_MAX_TERMINAL_BUFFER_BYTES; + const csiLimit = 4_096; + // No final byte for a CSI and no terminator for a string. + const digits = "0123456789 123\r\n".repeat(64); + // History gets one ST where a sequence is dropped. + const st = "\u001b\\"; + // The next ESC cuts this title short, so it goes to history with no terminator. + const leftOpen = "\u001b]0;one"; + // The terminal runs the carriage returns and line feeds inside a CSI while it waits for the + // final byte. + const lineBreaks = (fed: string) => fed.replaceAll(/[^\r\n]/g, ""); + // The last UTF-8 byte of U+20805 is 0x85, NEL, which ends a CSI or a string other than OSC. + // History replays it as ESC E. + const splitCodePoint = ["\ud842", "\udc05"]; + for (const { name, open, limit, filler = digits, end, history } of [ + { + name: "DCS, split code point", + open: `${leftOpen}\u001bPstuck`, + limit: stringLimit, + end: splitCodePoint, + history: () => `${leftOpen}${st}\u001bE`, + }, + // Each piece's U+0090 ends the sequence before it and opens a DCS that the piece drops. + { + name: "DCS opened by U+0090", + open: "\u001b[", + limit: stringLimit, + filler: `\u0090${"y".repeat(stringLimit)}`, + end: ["\u001b", "\\"], + history: () => st.repeat(3), + }, + { name: "OSC, BEL", open: "\u001b]0;", limit: stringLimit, end: ["\u0007"], history: () => st }, + { + name: "OSC, ESC", + open: "\u001b]0;", + limit: stringLimit, + end: ["\u001b[31m"], + history: () => `${st}\u001b[31m`, + }, + { + name: "CSI, final byte", + open: `${leftOpen}\u001b[`, + limit: csiLimit, + end: ["\nm"], + history: (fed: string) => `${leftOpen}${st}${lineBreaks(fed)}\n`, + }, + { + name: "CSI, CAN", + open: "\u001b[", + limit: csiLimit, + end: ["\u0018"], + history: (fed: string) => `${st}${lineBreaks(fed)}`, + }, + { + name: "CSI, U+0090", + open: "\u001b[", + limit: csiLimit, + end: ["\n\u0090kept\u001b\\"], + history: (fed: string) => `${st}${lineBreaks(fed)}\n\u001bPkept\u001b\\`, + }, + // An ESC abandons the CSI and starts a sequence that is kept. + { + name: "CSI, ESC", + open: "\u001b[", + limit: csiLimit, + end: ["\n\u001b[31m"], + history: (fed: string) => `${st}${lineBreaks(fed)}\n\u001b[31m`, + }, + { + name: "CSI, split code point", + open: "\u001b[", + limit: csiLimit, + end: ["\n", ...splitCodePoint], + history: (fed: string) => `${st}${lineBreaks(fed)}\n\u001bE`, + }, + ]) { + let held = ""; + let visible = ""; + const feed = (data: string) => { + const sanitized = TerminalManager.sanitizeTerminalHistoryChunk(held, data); + held = sanitized.pendingControlSequence; + visible += sanitized.visibleText; + }; + const fed = filler.repeat(Math.ceil(limit / filler.length) + 2); + + feed("before\n"); + feed(open); + for (let offset = 0; offset < fed.length; offset += filler.length) { + feed(fed.slice(offset, offset + filler.length)); + } + expect(held.length, name).toBeLessThanOrEqual(4); + + for (const data of end) feed(data); + feed("after\n"); + expect(held, name).toBe(""); + expect(visible, name).toBe(`before\n${history(fed)}after\n`); + } + + // Held text of exactly the limit stays held, and one more code unit passes the limit. Output + // before it in the same chunk is kept, and so is a last code unit that may begin the end. A C1 + // introducer counts as its ESC form. + const afterOutput = (data: string) => + TerminalManager.sanitizeTerminalHistoryChunk("", `before\n${data}`); + const string = "\u001b]".padEnd(stringLimit, "0"); + const csi = "\u001b[".padEnd(csiLimit, "0"); + for (const atLimit of [string, csi]) { + expect(afterOutput(atLimit), atLimit.slice(0, 2)).toEqual({ + visibleText: "before\n", + pendingControlSequence: atLimit, + }); + } + // A sequence that ends in the chunk where it passes the limit is kept whole. + for (const ended of [`${string}0\u0007`, `${csi}0m`]) { + expect(afterOutput(ended), `${ended.slice(0, 2)} ends`).toEqual({ + visibleText: `before\n${ended}`, + pendingControlSequence: "", + }); + } + expect(afterOutput(`${string}\u001b`), "string past the limit").toEqual({ + visibleText: `before\n${st}`, + pendingControlSequence: "\u0018\u001b]\u001b", + }); + expect(afterOutput(`\u001b[\u009b${csi.slice(2)}\ud842`), "CSI past the limit").toEqual({ + visibleText: `before\n${st}`, + pendingControlSequence: "\u0018\u001b[\ud842", + }); + expect(afterOutput(`${string}\ud842`), "string past the limit, high surrogate").toEqual({ + visibleText: `before\n${st}`, + pendingControlSequence: "\u0018\u001b]\ud842", + }); + // libghostty ignores intermediates past the fourth, so an escape sequence keeps the first four. + const escape = '\u001b!"#$'; + expect(afterOutput(`${escape}%`), "escape sequence past the limit").toEqual({ + visibleText: "before\n", + pendingControlSequence: escape, + }); + expect(TerminalManager.sanitizeTerminalHistoryChunk(escape, "&'"), "escape stays cut").toEqual({ + visibleText: "", + pendingControlSequence: escape, + }); + expect(TerminalManager.sanitizeTerminalHistoryChunk(escape, "&'B"), "escape ends").toEqual({ + visibleText: `${escape}&'B`, + pendingControlSequence: "", + }); +}); + it.layer( Layer.mergeAll( NodeServices.layer, diff --git a/apps/server/src/terminal/Manager.ts b/apps/server/src/terminal/Manager.ts index dd578d2c27fb..73a61498a9ac 100644 --- a/apps/server/src/terminal/Manager.ts +++ b/apps/server/src/terminal/Manager.ts @@ -1381,7 +1381,24 @@ function findEscapeSequenceEndIndex(input: string, start: number): number | null const HISTORY_SEQUENCE_START = /\u001b/; const NEXT_HISTORY_SEQUENCE_START = new RegExp(HISTORY_SEQUENCE_START.source, "g"); -function sanitizeTerminalHistoryChunk( +// Clients keep this much output and drop a sequence their cut goes through, so none of them +// could replay a longer string whole. +const MAX_HELD_STRING_LENGTH = DEFAULT_MAX_TERMINAL_BUFFER_BYTES; +// A real CSI is short, because it carries no payload and libghostty runs one only when it has +// at most 24 parameters. Each chunk rescans the held text, so this also bounds that work. +const MAX_HELD_CSI_LENGTH = 4_096; +// The ESC and four intermediates. libghostty ignores every intermediate past the fourth, so a +// held escape sequence cut to this replays the same. +const MAX_HELD_ESCAPE_LENGTH = 5; +// Leads the held text of a string or CSI that outgrew its limit, whose rest stays out of history. +// Only its introducer is still held, with a final ESC or high surrogate that may begin its end. +// Other held text starts with ESC, so this marker never collides with it. +const DROPPED_SEQUENCE = "\u0018"; +// The first drop writes ST to history. The dropped sequence's own ESC ended whatever history left +// open before it, and ST does the same on replay. In ground state ST does nothing. +const DROPPED_SEQUENCE_ST = "\u001b\\"; + +export function sanitizeTerminalHistoryChunk( pendingControlSequence: string, data: string, ): { visibleText: string; pendingControlSequence: string } { @@ -1389,9 +1406,11 @@ function sanitizeTerminalHistoryChunk( return { visibleText: data, pendingControlSequence: "" }; } - const input = `${pendingControlSequence}${data}`; - // Only the pending prefix's final ESC can begin a terminator split across chunks. - const pendingScanStart = Math.max(0, pendingControlSequence.length - 1); + const resumesDropped = pendingControlSequence.startsWith(DROPPED_SEQUENCE); + const input = `${pendingControlSequence.slice(resumesDropped ? 1 : 0)}${data}`; + // Only the pending prefix's final code unit, an ESC or a high surrogate, can begin an end split + // across chunks. + const pendingScanStart = Math.max(0, input.length - data.length - 1); let visibleText = ""; let index = 0; // Set when a C1 code point began the sequence at `index`: the ESC form that stands in for it. @@ -1408,6 +1427,7 @@ function sanitizeTerminalHistoryChunk( // Empty for a sequence the input introduces itself, which starts with ESC at `index`. const prefix = c1Introducer; c1Introducer = ""; + const dropped = resumesDropped && index === 0; const contentStart = prefix.length > 0 ? index : index + 2; const nextCodePoint = (prefix || input).charCodeAt(prefix.length > 0 ? 1 : index + 1); if (Number.isNaN(nextCodePoint)) { @@ -1424,7 +1444,7 @@ function sanitizeTerminalHistoryChunk( const cursorCodePoint = input.charCodeAt(cursor); const cancelled = isControlSequenceCancel(cursorCodePoint); if (cancelled || isCsiFinalByte(cursorCodePoint)) { - if (cancelled || !shouldStripCsiSequence(body, input[cursor] ?? "")) { + if (!dropped && (cancelled || !shouldStripCsiSequence(body, input[cursor] ?? ""))) { append(prefix + input.slice(index, cursor + 1)); } else { append(executedControls); @@ -1439,7 +1459,7 @@ function sanitizeTerminalHistoryChunk( if (c1) { // The CSI ends here undispatched, so it is kept only when the terminal is back in // ground with nothing to answer. - if (c1.next.length === 0 && !c1.query) { + if (!dropped && c1.next.length === 0 && !c1.query) { append(prefix + input.slice(index, end)); } else { append(executedControls + c1.safe); @@ -1465,7 +1485,17 @@ function sanitizeTerminalHistoryChunk( cursor += 1; } if (cursor >= input.length) { - return { visibleText, pendingControlSequence: prefix + input.slice(index) }; + const held = prefix + input.slice(index); + if (dropped || held.length > MAX_HELD_CSI_LENGTH) { + // The terminal has run these already, so history takes them as they come. A final + // high surrogate may begin a code point whose bytes end the CSI. + const surrogate = /[\ud800-\udbff]$/.exec(held)?.[0] ?? ""; + return { + visibleText: visibleText + (dropped ? "" : DROPPED_SEQUENCE_ST) + executedControls, + pendingControlSequence: `${DROPPED_SEQUENCE}\u001b[${surrogate}`, + }; + } + return { visibleText, pendingControlSequence: held }; } continue; } @@ -1482,13 +1512,24 @@ function sanitizeTerminalHistoryChunk( nextCodePoint === 0x5d, ); if (stringEnd === null) { - return { visibleText, pendingControlSequence: prefix + input.slice(index) }; + const held = prefix + input.slice(index); + if (dropped || held.length > MAX_HELD_STRING_LENGTH) { + // A final ESC may begin the terminator, and a final high surrogate a code point + // whose bytes end the string. + const terminatorStart = /[\u001b\ud800-\udbff]$/.exec(held)?.[0] ?? ""; + return { + visibleText: visibleText + (dropped ? "" : DROPPED_SEQUENCE_ST), + pendingControlSequence: DROPPED_SEQUENCE + held.slice(0, 2) + terminatorStart, + }; + } + return { visibleText, pendingControlSequence: held }; } const { end, c1 } = stringEnd; const content = stripStringTerminator( prefix.slice(2) + input.slice(contentStart, c1?.index ?? end), ); const strip = + dropped || (nextCodePoint === 0x5d && shouldStripOscSequence(content)) || (nextCodePoint === 0x50 && shouldStripDcsSequence(content)); if (c1 === undefined || (!strip && c1.effect.next.length === 0 && !c1.effect.query)) { @@ -1507,7 +1548,11 @@ function sanitizeTerminalHistoryChunk( const escapeSequenceEndIndex = findEscapeSequenceEndIndex(input, index + 1); if (escapeSequenceEndIndex === null) { - return { visibleText, pendingControlSequence: input.slice(index) }; + // Only intermediates follow this ESC. + return { + visibleText, + pendingControlSequence: input.slice(index, index + MAX_HELD_ESCAPE_LENGTH), + }; } // ESC Z (DECID) is the legacy primary device attributes query. if (nextCodePoint !== 0x5a) { diff --git a/docs/internals/terminal-runtime.md b/docs/internals/terminal-runtime.md index ca5f0d46ead6..9cc79d5ff7d8 100644 --- a/docs/internals/terminal-runtime.md +++ b/docs/internals/terminal-runtime.md @@ -22,6 +22,15 @@ discarded chunk references immediately, even if array compaction happens later. Client buffers have a separate 512 KiB cap. Measure throughput with full scrollback when changing this path. +The server holds the text of an unfinished control sequence until it ends, and limits +how much it holds. The limit must not end a string. libghostty stays in an oversized +string until its terminator and prints none of it as text, so history that resumed +early would replay output the live screen never showed. A CSI longer than the limit can +still run in libghostty at its final byte. History leaves it out, because it may be a +query. Below the limit each chunk copies the held string. The string limit in code +units is the client buffer's limit in bytes, so raising that buffer raises the cost +with its square. + Restoration must read only the bounded tail of current or legacy history files, skip any incomplete UTF-8 prefix, and apply the line limit. Close the read handle before rewriting the capped file. Reading whole old logs would defeat the memory