Skip to content

Commit e934c6b

Browse files
saphidclaude
authored andcommitted
fix(server): terminal history keeps text with C1 characters
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 30084f0 commit e934c6b

2 files changed

Lines changed: 10 additions & 56 deletions

File tree

‎apps/server/src/terminal/Manager.test.ts‎

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,7 +1110,8 @@ it.layer(
11101110
const { manager, ptyAdapter } = yield* createManager();
11111111
yield* manager.open(openInput());
11121112
const process = ptyAdapter.processes[0]!;
1113-
const before = "café\t界🙂\u0007\u009c\u0085";
1113+
// Mojibake like "â€\u009d" (for ”) carries C1 code points that libghostty prints as text.
1114+
const before = "café\t界🙂\u0007\u009c\u0085 â€\u009d Ð\u0090 \u009e\u009f\u009b6n";
11141115
const after = "e\u0301\r\n";
11151116

11161117
process.emitData(before);
@@ -1134,8 +1135,6 @@ it.layer(
11341135
process.emitData("\u001b[?2026$p\u001b[?2026;2$y\u001b[>q\u001b[?u\u001b[?31u");
11351136
process.emitData("\u001bP$q m\u001b\\\u001bP1$r0m\u001b\\");
11361137
process.emitData("\u001bP+q544e\u001b\\\u001bP1+r544e=1b\u001b\\");
1137-
process.emitData("\u0090$q m\u009c\u00901$r0m\u009c");
1138-
process.emitData("\u0090+q544e\u009c\u00901+r544e=1b\u009c");
11391138
process.emitData('\u001b[!p\u001b["p\u001b[4 q\u001b[u');
11401139
process.emitData("done\n");
11411140

@@ -1154,11 +1153,6 @@ it.layer(
11541153
{ name: "PM", sequence: "\u001b^unfinished" },
11551154
{ name: "APC", sequence: "\u001b_unfinished" },
11561155
{ name: "CSI", sequence: "\u001b[5" },
1157-
{ name: "C1 OSC", sequence: "\u009d0;unfinished" },
1158-
{ name: "C1 DCS", sequence: "\u0090unfinished" },
1159-
{ name: "C1 PM", sequence: "\u009eunfinished" },
1160-
{ name: "C1 APC", sequence: "\u009funfinished" },
1161-
{ name: "C1 CSI", sequence: "\u009b5" },
11621156
])("keeps output after a cancelled $name sequence in history", ({ sequence }) =>
11631157
Effect.gen(function* () {
11641158
const { manager, ptyAdapter } = yield* createManager();
@@ -1191,10 +1185,10 @@ it.layer(
11911185
process.emitData("\u001bP$q ");
11921186
process.emitData("m\u001b");
11931187
process.emitData("\\after ");
1194-
process.emitData("\u009b?3");
1188+
process.emitData("\u001b[?3");
11951189
process.emitData("1uafter ");
1196-
process.emitData("\u0090+q544e");
1197-
process.emitData("\u009cafter\n");
1190+
process.emitData("\u001bP+q544e");
1191+
process.emitData("\u001b\\after\n");
11981192

11991193
yield* manager.close({ threadId: "thread-1" });
12001194

@@ -1217,10 +1211,10 @@ it.layer(
12171211
yield* Effect.addFinalizer(() => Effect.sync(unsubscribe));
12181212
const sequences = [
12191213
{ prefix: `\u001b]52;c;${"YWJj".repeat(16_384)}\u001b`, terminator: "\\" },
1220-
{ prefix: `\u0090q${"?".repeat(65_536)}`, terminator: "\u009c" },
1214+
{ prefix: `\u001bPq${"?".repeat(65_536)}`, terminator: "\u001b\\" },
12211215
];
1222-
const stripped = "\u001b]11;?\u0007\u0090$q m\u009c";
1223-
const preserved = "\u001b[31m\u009d52;c;YWJj\u009c";
1216+
const stripped = "\u001b]11;?\u0007\u001bP$q m\u001b\\";
1217+
const preserved = "\u001b[31m\u001b]52;c;YWJj\u0007";
12241218
const expected =
12251219
sequences.map(({ prefix, terminator }) => prefix + terminator + preserved).join("") +
12261220
"done\n";

‎apps/server/src/terminal/Manager.ts‎

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1168,7 +1168,8 @@ function findEscapeSequenceEndIndex(input: string, start: number): number | null
11681168
return isEscapeFinalByte(input.charCodeAt(cursor)) ? cursor + 1 : start + 1;
11691169
}
11701170

1171-
const HISTORY_SEQUENCE_START = /[\u001b\u0090\u009b\u009d\u009e\u009f]/;
1171+
// libghostty prints UTF-8 C1 code points (U+0080-U+009F) as text, so only ESC starts a sequence.
1172+
const HISTORY_SEQUENCE_START = /\u001b/;
11721173
const NEXT_HISTORY_SEQUENCE_START = new RegExp(HISTORY_SEQUENCE_START.source, "g");
11731174

11741175
function sanitizeTerminalHistoryChunk(
@@ -1253,47 +1254,6 @@ function sanitizeTerminalHistoryChunk(
12531254
continue;
12541255
}
12551256

1256-
if (codePoint === 0x9b) {
1257-
let cursor = index + 1;
1258-
while (cursor < input.length) {
1259-
const cancelled = isControlSequenceCancel(input.charCodeAt(cursor));
1260-
if (cancelled || isCsiFinalByte(input.charCodeAt(cursor))) {
1261-
const sequence = input.slice(index, cursor + 1);
1262-
const body = input.slice(index + 1, cursor);
1263-
if (cancelled || !shouldStripCsiSequence(body, input[cursor] ?? "")) {
1264-
append(sequence);
1265-
}
1266-
index = cursor + 1;
1267-
break;
1268-
}
1269-
cursor += 1;
1270-
}
1271-
if (cursor >= input.length) {
1272-
return { visibleText, pendingControlSequence: input.slice(index) };
1273-
}
1274-
continue;
1275-
}
1276-
1277-
if (codePoint === 0x9d || codePoint === 0x90 || codePoint === 0x9e || codePoint === 0x9f) {
1278-
const terminatorIndex = findStringTerminatorIndex(
1279-
input,
1280-
Math.max(index + 1, pendingScanStart),
1281-
);
1282-
if (terminatorIndex === null) {
1283-
return { visibleText, pendingControlSequence: input.slice(index) };
1284-
}
1285-
const sequence = input.slice(index, terminatorIndex);
1286-
const content = stripStringTerminator(input.slice(index + 1, terminatorIndex));
1287-
const strip =
1288-
(codePoint === 0x9d && shouldStripOscSequence(content)) ||
1289-
(codePoint === 0x90 && shouldStripDcsSequence(content));
1290-
if (!strip) {
1291-
append(sequence);
1292-
}
1293-
index = terminatorIndex;
1294-
continue;
1295-
}
1296-
12971257
// One slice per printable run: every chunk of coloured output reaches this loop.
12981258
NEXT_HISTORY_SEQUENCE_START.lastIndex = index + 1;
12991259
const nextSequenceStart = NEXT_HISTORY_SEQUENCE_START.exec(input)?.index ?? input.length;

0 commit comments

Comments
 (0)