From 1662c3c7d63205a2492157ebe99cc93444016ad3 Mon Sep 17 00:00:00 2001 From: viktorce Date: Thu, 19 Nov 2026 23:10:18 +0000 Subject: [PATCH 1/4] fix(server): terminal output after an unfinished control sequence no longer slows the server The history sanitizer held everything that arrived while a control sequence was open, with no limit, and worked through the held text again on every chunk. A sequence that never ended made the cost grow with the square of the output. The held text now has a limit in UTF-16 code units: 524,288 for an OSC, DCS, PM or APC string, the byte size of what a client retains, and 4,096 for a CSI. Past the limit a string or CSI keeps only its introducer, and the rest of the sequence stays out of history. libghostty does the same with an oversized string: it drops the payload and stays in the string until the terminator. When a string or CSI is first dropped, history gets one ST. The dropped sequence's own ESC ended whatever history left open before it, and the ST does the same on replay. A held escape sequence keeps its ESC and four intermediates. libghostty ignores intermediates past the fourth, so the cut changes nothing on replay. --- apps/server/src/terminal/Manager.test.ts | 148 +++++++++++++++++++++++ apps/server/src/terminal/Manager.ts | 64 ++++++++-- docs/internals/terminal-runtime.md | 9 ++ 3 files changed, 212 insertions(+), 9 deletions(-) diff --git a/apps/server/src/terminal/Manager.test.ts b/apps/server/src/terminal/Manager.test.ts index 154dd4a445d34..ec90c64f37e4f 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 a294ba7f1afe2..44c3c68cc8a40 100644 --- a/apps/server/src/terminal/Manager.ts +++ b/apps/server/src/terminal/Manager.ts @@ -1379,7 +1379,25 @@ 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. Strip it before sending held text to a client, because a CAN +// cancels the open sequence there. +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 } { @@ -1387,9 +1405,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. @@ -1406,6 +1426,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)) { @@ -1422,7 +1443,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); @@ -1437,7 +1458,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); @@ -1463,7 +1484,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; } @@ -1480,13 +1511,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)) { @@ -1505,7 +1547,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 ca5f0d46ead6e..9cc79d5ff7d8e 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 From 6d435a7f3188be7563ed6b88617c028844223e16 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Fri, 20 Nov 2026 20:00:05 +0000 Subject: [PATCH 2/4] chore(server): drop a stale note about held control sequences reaching clients Co-Authored-By: Claude Opus 5.5 --- apps/server/src/terminal/Manager.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/apps/server/src/terminal/Manager.ts b/apps/server/src/terminal/Manager.ts index 863d6038a0df7..73a61498a9aca 100644 --- a/apps/server/src/terminal/Manager.ts +++ b/apps/server/src/terminal/Manager.ts @@ -1392,8 +1392,7 @@ const MAX_HELD_CSI_LENGTH = 4_096; 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. Strip it before sending held text to a client, because a CAN -// cancels the open sequence there. +// 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. From b65ceef0b8a28ff23e8615318ea261aedcbc988c Mon Sep 17 00:00:00 2001 From: viktorce Date: Fri, 20 Nov 2026 22:36:47 +0000 Subject: [PATCH 3/4] fix(server): terminal history drops OSC 4 palette queries History kept an OSC 4 palette query, so every restore made the client's terminal answer it again. A mobile client sends that answer to the shell as input. The sanitizer now writes ST in place of an OSC 4 that ends on its own terminator, BEL, ST, CAN or SUB, and whose every colour is "?". The ST ends a sequence that the query's ESC cut short, as the query did. A query that the next ESC cut short stays, and so does an OSC 4 that sets a colour. --- apps/server/src/terminal/Manager.test.ts | 8 ++++++-- apps/server/src/terminal/Manager.ts | 26 ++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/apps/server/src/terminal/Manager.test.ts b/apps/server/src/terminal/Manager.test.ts index 94c0e3d308082..18dbef61c2c00 100644 --- a/apps/server/src/terminal/Manager.test.ts +++ b/apps/server/src/terminal/Manager.test.ts @@ -1748,7 +1748,7 @@ it.layer( }), ); - it.effect("strips replayable CSI and DCS traffic while preserving setters", () => + it.effect("strips replayable CSI, DCS and OSC traffic while preserving setters", () => Effect.gen(function* () { const { manager, ptyAdapter } = yield* createManager(); yield* manager.open(openInput()); @@ -1764,6 +1764,9 @@ it.layer( // BEL is DCS payload, not a terminator: only ST ends the string. process.emitData("\u001bP$qm\u0007hidden\u001b\\\u001bP+q544e\u0007hidden\u001b\\"); process.emitData('\u001b[!p\u001b["p\u001b[4 q\u001b[u'); + // Ghostty sets a colour beside a query, skips an LF, an empty field and a last index with no + // colour in an OSC, and runs an OSC that CAN ends or that the next ESC cuts short. + process.emitData("\u001b]4;1;red;2;?\u0007\u001b]0;t\u001b]\n4;1;?;;2;?;3\u0018\u001b]4;3;?"); process.emitData("\u001b]0;x\u001b\u001b[?2026$p"); process.emitData("done\n"); @@ -1772,7 +1775,8 @@ it.layer( const reopened = yield* manager.open(openInput()); expect( reopened.history.startsWith( - 'prompt \u001b[!p\u001b["p\u001b[4 q\u001b[u\u001b]0;x\u001bdone\n', + 'prompt \u001b[!p\u001b["p\u001b[4 q\u001b[u' + + "\u001b]4;1;red;2;?\u0007\u001b]0;t\u001b\\\u001b]4;3;?\u001b]0;x\u001bdone\n", ), ).toBe(true); }), diff --git a/apps/server/src/terminal/Manager.ts b/apps/server/src/terminal/Manager.ts index 73a61498a9aca..e312b03f8c049 100644 --- a/apps/server/src/terminal/Manager.ts +++ b/apps/server/src/terminal/Manager.ts @@ -1274,6 +1274,19 @@ function shouldStripOscSequence(content: string): boolean { return /^(10|11|12);(?:\?|rgb:)/.test(content); } +// An OSC 4 whose every colour is "?" reports palette entries and sets none. Ghostty skips C0 +// controls inside an OSC, runs one that CAN or SUB ends, and reads the pairs past empty fields. +function isOscPaletteQuery(content: string): boolean { + if (!/^[\0-\x1f]*4/.test(content) || !content.includes("?")) return false; + const [code, ...pairs] = content + .replace(/[\0-\x1f]+/g, "") + .split(";") + .filter((field) => field !== ""); + return ( + code === "4" && pairs.length > 1 && pairs.every((field, at) => at % 2 === 0 || field === "?") + ); +} + function stripStringTerminator(value: string): string { if (value.endsWith("\u001b\\")) { return value.slice(0, -2); @@ -1528,6 +1541,19 @@ export function sanitizeTerminalHistoryChunk( const content = stripStringTerminator( prefix.slice(2) + input.slice(contentStart, c1?.index ?? end), ); + // ST ends what the query's ESC cut short and leaves ground state, as the query does when it + // has its own terminator. Without one, the query ends at the next ESC, which history may + // strip, so ST would not replay the same and the query stays. + if ( + !dropped && + nextCodePoint === 0x5d && + isOscPaletteQuery(content) && + /[\x07\x18\x1a]$|\x1b\\$/.test(input.slice(index, end)) + ) { + append("\u001b\\"); + index = end; + continue; + } const strip = dropped || (nextCodePoint === 0x5d && shouldStripOscSequence(content)) || From 01b286ed090a3a06eedf60362322a750603d73a1 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sat, 21 Nov 2026 07:05:47 +0000 Subject: [PATCH 4/4] fix(server): palette query fix no longer carries the held-sequence limits The branch inherited #39861's limit on held CSI text, which drops a valid setter split across PTY chunks once it passes 4,096 characters, so a reopened terminal could show the wrong colour. This takes the terminal sanitizer, its tests and the runtime doc back to main and keeps only the OSC 4 palette query change, without the parent's `dropped` guard. The existing history test now also checks that a long setter split across two chunks is kept whole. Co-Authored-By: Claude Opus 5.5 --- apps/server/src/terminal/Manager.test.ts | 154 +---------------------- apps/server/src/terminal/Manager.ts | 64 ++-------- docs/internals/terminal-runtime.md | 9 -- 3 files changed, 14 insertions(+), 213 deletions(-) diff --git a/apps/server/src/terminal/Manager.test.ts b/apps/server/src/terminal/Manager.test.ts index 0b188ddb9dd33..c6f45a60ab75e 100644 --- a/apps/server/src/terminal/Manager.test.ts +++ b/apps/server/src/terminal/Manager.test.ts @@ -3,7 +3,6 @@ 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, @@ -489,153 +488,6 @@ 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, @@ -1770,6 +1622,9 @@ it.layer( // colour in an OSC, and runs an OSC that CAN ends or that the next ESC cuts short. process.emitData("\u001b]4;1;red;2;?\u0007\u001b]0;t\u001b]\n4;1;?;;2;?;3\u0018\u001b]4;3;?"); process.emitData("\u001b]0;x\u001b\u001b[?2026$p"); + // A long setter split across chunks is kept whole. + process.emitData(`\u001b[${"0".repeat(5000)}`); + process.emitData("31mX"); process.emitData("done\n"); yield* manager.close({ threadId: "thread-1" }); @@ -1778,7 +1633,8 @@ it.layer( expect( reopened.history.startsWith( 'prompt \u001b[!p\u001b["p\u001b[4 q\u001b[u' + - "\u001b]4;1;red;2;?\u0007\u001b]0;t\u001b\\\u001b]4;3;?\u001b]0;x\u001bdone\n", + "\u001b]4;1;red;2;?\u0007\u001b]0;t\u001b\\\u001b]4;3;?\u001b]0;x\u001b" + + `\u001b[${"0".repeat(5000)}31mXdone\n`, ), ).toBe(true); }), diff --git a/apps/server/src/terminal/Manager.ts b/apps/server/src/terminal/Manager.ts index e312b03f8c049..86ccc1d3e0dd2 100644 --- a/apps/server/src/terminal/Manager.ts +++ b/apps/server/src/terminal/Manager.ts @@ -1394,24 +1394,7 @@ 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"); -// 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( +function sanitizeTerminalHistoryChunk( pendingControlSequence: string, data: string, ): { visibleText: string; pendingControlSequence: string } { @@ -1419,11 +1402,9 @@ export function sanitizeTerminalHistoryChunk( return { visibleText: data, pendingControlSequence: "" }; } - 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); + 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); let visibleText = ""; let index = 0; // Set when a C1 code point began the sequence at `index`: the ESC form that stands in for it. @@ -1440,7 +1421,6 @@ export 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)) { @@ -1457,7 +1437,7 @@ export function sanitizeTerminalHistoryChunk( const cursorCodePoint = input.charCodeAt(cursor); const cancelled = isControlSequenceCancel(cursorCodePoint); if (cancelled || isCsiFinalByte(cursorCodePoint)) { - if (!dropped && (cancelled || !shouldStripCsiSequence(body, input[cursor] ?? ""))) { + if (cancelled || !shouldStripCsiSequence(body, input[cursor] ?? "")) { append(prefix + input.slice(index, cursor + 1)); } else { append(executedControls); @@ -1472,7 +1452,7 @@ export 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 (!dropped && c1.next.length === 0 && !c1.query) { + if (c1.next.length === 0 && !c1.query) { append(prefix + input.slice(index, end)); } else { append(executedControls + c1.safe); @@ -1498,17 +1478,7 @@ export function sanitizeTerminalHistoryChunk( cursor += 1; } if (cursor >= input.length) { - 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 }; + return { visibleText, pendingControlSequence: prefix + input.slice(index) }; } continue; } @@ -1525,17 +1495,7 @@ export function sanitizeTerminalHistoryChunk( nextCodePoint === 0x5d, ); if (stringEnd === null) { - 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 }; + return { visibleText, pendingControlSequence: prefix + input.slice(index) }; } const { end, c1 } = stringEnd; const content = stripStringTerminator( @@ -1545,7 +1505,6 @@ export function sanitizeTerminalHistoryChunk( // has its own terminator. Without one, the query ends at the next ESC, which history may // strip, so ST would not replay the same and the query stays. if ( - !dropped && nextCodePoint === 0x5d && isOscPaletteQuery(content) && /[\x07\x18\x1a]$|\x1b\\$/.test(input.slice(index, end)) @@ -1555,7 +1514,6 @@ export function sanitizeTerminalHistoryChunk( continue; } const strip = - dropped || (nextCodePoint === 0x5d && shouldStripOscSequence(content)) || (nextCodePoint === 0x50 && shouldStripDcsSequence(content)); if (c1 === undefined || (!strip && c1.effect.next.length === 0 && !c1.effect.query)) { @@ -1574,11 +1532,7 @@ export function sanitizeTerminalHistoryChunk( const escapeSequenceEndIndex = findEscapeSequenceEndIndex(input, index + 1); if (escapeSequenceEndIndex === null) { - // Only intermediates follow this ESC. - return { - visibleText, - pendingControlSequence: input.slice(index, index + MAX_HELD_ESCAPE_LENGTH), - }; + return { visibleText, pendingControlSequence: input.slice(index) }; } // 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 9cc79d5ff7d8e..ca5f0d46ead6e 100644 --- a/docs/internals/terminal-runtime.md +++ b/docs/internals/terminal-runtime.md @@ -22,15 +22,6 @@ 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