Skip to content
TypeScript

fix(server): terminal history keeps text with C1 characters (#29649)

Alex Southwell663cd0822d7d
diff --git a/apps/server/src/terminal/Manager.test.ts b/apps/server/src/terminal/Manager.test.ts
index c210e4a519d..7accdb95837 100644
--- a/apps/server/src/terminal/Manager.test.ts
+++ b/apps/server/src/terminal/Manager.test.ts
@@ -1115,7 +1115,8 @@ it.layer(
const { manager, ptyAdapter } = yield* createManager();
yield* manager.open(openInput());
const process = ptyAdapter.processes[0]!;
- const before = "café\t界🙂\u0007\u009c\u0085";
+ // Mojibake like "â€\u009d" (for ”) carries C1 code points that libghostty prints as text.
+ const before = "café\t界🙂\u0007\u009c\u0085 â€\u009d Ð\u0090 \u009e\u009f\u009b6n";
const after = "e\u0301\r\n";
process.emitData(before);
@@ -1139,8 +1140,6 @@ it.layer(
process.emitData("\u001b[?2026$p\u001b[?2026;2$y\u001b[>q\u001b[?u\u001b[?31u");
process.emitData("\u001bP$q m\u001b\\\u001bP1$r0m\u001b\\");
process.emitData("\u001bP+q544e\u001b\\\u001bP1+r544e=1b\u001b\\");
- process.emitData("\u0090$q m\u009c\u00901$r0m\u009c");
- process.emitData("\u0090+q544e\u009c\u00901+r544e=1b\u009c");
process.emitData('\u001b[!p\u001b["p\u001b[4 q\u001b[u');
process.emitData("\u001b]0;x\u001b\u001b[?2026$p");
process.emitData("done\n");
@@ -1162,11 +1161,6 @@ it.layer(
{ name: "PM", sequence: "\u001b^unfinished" },
{ name: "APC", sequence: "\u001b_unfinished" },
{ name: "CSI", sequence: "\u001b[5" },
- { name: "C1 OSC", sequence: "\u009d0;unfinished" },
- { name: "C1 DCS", sequence: "\u0090unfinished" },
- { name: "C1 PM", sequence: "\u009eunfinished" },
- { name: "C1 APC", sequence: "\u009funfinished" },
- { name: "C1 CSI", sequence: "\u009b5" },
])("keeps output after a cancelled $name sequence in history", ({ sequence }) =>
Effect.gen(function* () {
const { manager, ptyAdapter } = yield* createManager(10);
@@ -1200,10 +1194,10 @@ it.layer(
process.emitData("\u001bP$q ");
process.emitData("m\u001b");
process.emitData("\\after ");
- process.emitData("\u009b?3");
+ process.emitData("\u001b[?3");
process.emitData("1uafter ");
- process.emitData("\u0090+q544e");
- process.emitData("\u009cafter\n");
+ process.emitData("\u001bP+q544e");
+ process.emitData("\u001b\\after\n");
yield* manager.close({ threadId: "thread-1" });
@@ -1226,10 +1220,10 @@ it.layer(
yield* Effect.addFinalizer(() => Effect.sync(unsubscribe));
const sequences = [
{ prefix: `\u001b]52;c;${"YWJj".repeat(16_384)}\u001b`, terminator: "\\" },
- { prefix: `\u0090q${"?".repeat(65_536)}`, terminator: "\u009c" },
+ { prefix: `\u001bPq${"?".repeat(65_536)}`, terminator: "\u001b\\" },
];
- const stripped = "\u001b]11;?\u0007\u0090$q m\u009c";
- const preserved = "\u001b[31m\u009d52;c;YWJj\u009c";
+ const stripped = "\u001b]11;?\u0007\u001bP$q m\u001b\\";
+ const preserved = "\u001b[31m\u001b]52;c;YWJj\u0007";
const expected =
sequences.map(({ prefix, terminator }) => prefix + terminator + preserved).join("") +
"done\n";
diff --git a/apps/server/src/terminal/Manager.ts b/apps/server/src/terminal/Manager.ts
index 09a881d15ea..6a661db4e09 100644
--- a/apps/server/src/terminal/Manager.ts
+++ b/apps/server/src/terminal/Manager.ts
@@ -1174,7 +1174,8 @@ function findEscapeSequenceEndIndex(input: string, start: number): number | null
return isEscapeFinalByte(input.charCodeAt(cursor)) ? cursor + 1 : start + 1;
}
-const HISTORY_SEQUENCE_START = /[\u001b\u0090\u009b\u009d\u009e\u009f]/;
+// libghostty prints UTF-8 C1 code points (U+0080-U+009F) as text, so only ESC starts a sequence.
+const HISTORY_SEQUENCE_START = /\u001b/;
const NEXT_HISTORY_SEQUENCE_START = new RegExp(HISTORY_SEQUENCE_START.source, "g");
function sanitizeTerminalHistoryChunk(
@@ -1259,47 +1260,6 @@ function sanitizeTerminalHistoryChunk(
continue;
}
- if (codePoint === 0x9b) {
- let cursor = index + 1;
- while (cursor < input.length) {
- const cancelled = isControlSequenceCancel(input.charCodeAt(cursor));
- if (cancelled || isCsiFinalByte(input.charCodeAt(cursor))) {
- const sequence = input.slice(index, cursor + 1);
- const body = input.slice(index + 1, cursor);
- if (cancelled || !shouldStripCsiSequence(body, input[cursor] ?? "")) {
- append(sequence);
- }
- index = cursor + 1;
- break;
- }
- cursor += 1;
- }
- if (cursor >= input.length) {
- return { visibleText, pendingControlSequence: input.slice(index) };
- }
- continue;
- }
-
- if (codePoint === 0x9d || codePoint === 0x90 || codePoint === 0x9e || codePoint === 0x9f) {
- const terminatorIndex = findStringTerminatorIndex(
- input,
- Math.max(index + 1, pendingScanStart),
- );
- if (terminatorIndex === null) {
- return { visibleText, pendingControlSequence: input.slice(index) };
- }
- const sequence = input.slice(index, terminatorIndex);
- const content = stripStringTerminator(input.slice(index + 1, terminatorIndex));
- const strip =
- (codePoint === 0x9d && shouldStripOscSequence(content)) ||
- (codePoint === 0x90 && shouldStripDcsSequence(content));
- if (!strip) {
- append(sequence);
- }
- index = terminatorIndex;
- continue;
- }
-
// One slice per printable run: every chunk of coloured output reaches this loop.
NEXT_HISTORY_SEQUENCE_START.lastIndex = index + 1;
const nextSequenceStart = NEXT_HISTORY_SEQUENCE_START.exec(input)?.index ?? input.length;