The best way to control Claude Code, Codex and more from any computer or phone
fix(server): terminal history keeps text after a CSI cut off by non-ASCII output (#36355)
Julius Marminge committed
Nov 13, 2026 at 13:01 UTC
d772115e0dfdd2840ba82fe3ce990fba4f5d44d6
2 files changed
+136
-25
apps/server/src/terminal/Manager.test.ts
+22
@@ -1426,6 +1426,15 @@ it.layer(
1426
{ name: "NUL in DECRQM", data: "A\u001b[?2026\u0000$pB", history: "AB" },
1427
{ name: "CR in DSR", data: "AB\u001b[5\rnC", history: "AB\rC" },
1428
{ name: "DLE in DSR", data: "AB\u001b[5\u0010nC", history: "ABC" },
1429
+ // Ghostty reads non-ASCII there as UTF-8 bytes: it drops é and runs 0x80-0x9F as C1 controls.
1430
+ { name: "é in DA", data: "A\u001b[0écB", history: "AB" },
1431
+ { name: "DECID byte in a CSI", data: "A\u001b[嚠B", history: "A\ufffdB" },
1432
+ {
1433
+ name: "C1 DCS then C1 CSI in DSR",
1434
+ data: "A\u001b[\u0090\u009b6nB",
1435
+ history: "A\u001bP\u009cB",
1436
+ },
1437
+ { name: "ESC in DSR", data: "A\u001b[5\u001b[6nB", history: "AB" },
1438
])(
1439
"strips a query with an embedded control but keeps what the terminal ran ($name)",
1440
({ data, history }) =>
@@ -1441,6 +1450,19 @@ it.layer(
1450
}),
1451
);
1452
1453
+ // Ghostty aborts a CSI at a non-ASCII code point and prints it and what follows.
1454
+ it.effect("keeps a CSI interrupted by non-ASCII text in history", () =>
1455
+ Effect.gen(function* () {
1456
+ const { manager, ptyAdapter } = yield* createManager();
1457
+ yield* manager.open(openInput());
1458
+ ptyAdapter.processes[0]!.emitData("A\u001b[界nB");
1459
+ yield* manager.close({ threadId: "thread-1" });
1460
+
1461
+ const reopened = yield* manager.open(openInput());
1462
+ expect(reopened.history.split("\u001b[?1049h")[0]).toBe("A\u001b[界nB");
1463
+ }),
1464
+ );
1465
+
1466
it.effect.each([
1467
{ name: "OSC", sequence: "\u001b]0;unfinished" },
1468
{ name: "DCS", sequence: "\u001bPunfinished" },
apps/server/src/terminal/Manager.ts
+114
-25
@@ -1194,18 +1194,70 @@ function stripStringTerminator(value: string): string {
1194
return value;
1195
}
1196
1197
-// BEL ends only OSC; inside DCS, PM and APC it is payload. A decoded U+009C is the
1198
-// reverse: libghostty keeps it inside OSC but ends the other strings there.
1199
-function findStringTerminatorIndex(input: string, start: number, isOsc: boolean): number | null {
1197
+interface C1Effect {
1198
+ /** ESC form of the sequence the code point starts, or "" when it returns to ground. */
1199
+ readonly next: string;
1200
+ /** Its 0x9A byte ran DECID, which replies with device attributes. */
1201
+ readonly query: boolean;
1202
+ /** What replays its visible effect from ground. */
1203
+ readonly safe: string;
1204
+}
1205
+
1206
+// Outside ground and OSC, libghostty parses a code point's UTF-8 bytes one at a time: 0x80-0x9F
1207
+// act as C1 controls, the rest are dropped. Bytes left over after a return to ground print U+FFFD.
1208
+function c1Effect(codePoint: number): C1Effect | undefined {
1209
+ // A lone surrogate is encoded as U+FFFD, whose bytes are all dropped.
1210
+ if (codePoint >= 0xd800 && codePoint <= 0xdfff) return undefined;
1211
+ let next: string | undefined;
1212
+ let query = false;
1213
+ let safe = "";
1214
+ for (let shift = codePoint > 0xffff ? 12 : codePoint > 0x7ff ? 6 : 0; shift >= 0; shift -= 6) {
1215
+ const byte = 0x80 | ((codePoint >> shift) & 0x3f);
1216
+ if (next === "") {
1217
+ safe += "\ufffd";
1218
+ } else if (next?.startsWith("\u001b]")) {
1219
+ next += "\ufffd";
1220
+ } else if (byte === 0x9b) {
1221
+ next = "\u001b[";
1222
+ } else if (byte === 0x9d) {
1223
+ next = "\u001b]";
1224
+ } else if (byte === 0x90) {
1225
+ next = "\u001bP";
1226
+ } else if (byte === 0x98 || byte === 0x9e || byte === 0x9f) {
1227
+ next = "\u001b_";
1228
+ } else if (byte < 0xa0) {
1229
+ next = "";
1230
+ query = byte === 0x9a;
1231
+ // Any other C1 control but ST runs as ESC plus the byte less 0x40.
1232
+ safe = query || byte === 0x9c ? "" : `\u001b${String.fromCharCode(byte - 0x40)}`;
1233
+ }
1234
+ }
1235
+ return next === undefined ? undefined : { next, query, safe };
1236
+}
1237
+
1238
+// BEL ends only OSC; inside DCS, PM and APC it is payload. Non-ASCII is the reverse: libghostty
1239
+// keeps it inside OSC, while a C1 byte in it ends the other strings.
1240
+function findStringEnd(
1241
+ input: string,
1242
+ start: number,
1243
+ isOsc: boolean,
1244
+): { end: number; c1?: { effect: C1Effect; index: number } } | null {
1245
for (let index = start; index < input.length; index += 1) {
1246
const codePoint = input.charCodeAt(index);
1202
- if ((isOsc ? codePoint === 0x07 : codePoint === 0x9c) || isControlSequenceCancel(codePoint)) {
1203
- return index + 1;
1247
+ if ((isOsc && codePoint === 0x07) || isControlSequenceCancel(codePoint)) {
1248
+ return { end: index + 1 };
1249
}
1250
// Like xterm.js, an ESC that does not start ST ends the string and begins the next sequence.
1251
if (codePoint === 0x1b) {
1252
if (index + 1 >= input.length) return null;
1208
- return input.charCodeAt(index + 1) === 0x5c ? index + 2 : index;
1253
+ return { end: input.charCodeAt(index + 1) === 0x5c ? index + 2 : index };
1254
+ }
1255
+ if (codePoint >= 0x80 && !isOsc) {
1256
+ const fullCodePoint = input.codePointAt(index) ?? codePoint;
1257
+ const end = index + (fullCodePoint > 0xffff ? 2 : 1);
1258
+ const effect = c1Effect(fullCodePoint);
1259
+ if (effect) return { end, c1: { effect, index } };
1260
+ index = end - 1;
1261
}
1262
}
1263
return null;
@@ -1251,6 +1303,8 @@ function sanitizeTerminalHistoryChunk(
1303
const pendingScanStart = Math.max(0, pendingControlSequence.length - 1);
1304
let visibleText = "";
1305
let index = 0;
1306
+ // Set when a C1 code point began the sequence at `index`: the ESC form that stands in for it.
1307
+ let c1Introducer = "";
1308
1309
const append = (value: string) => {
1310
visibleText += value;
@@ -1259,14 +1313,18 @@ function sanitizeTerminalHistoryChunk(
1313
while (index < input.length) {
1314
const codePoint = input.charCodeAt(index);
1315
1262
- if (codePoint === 0x1b) {
1263
- const nextCodePoint = input.charCodeAt(index + 1);
1316
+ if (c1Introducer.length > 0 || codePoint === 0x1b) {
1317
+ // Empty for a sequence the input introduces itself, which starts with ESC at `index`.
1318
+ const prefix = c1Introducer;
1319
+ c1Introducer = "";
1320
+ const contentStart = prefix.length > 0 ? index : index + 2;
1321
+ const nextCodePoint = (prefix || input).charCodeAt(prefix.length > 0 ? 1 : index + 1);
1322
if (Number.isNaN(nextCodePoint)) {
1323
return { visibleText, pendingControlSequence: input.slice(index) };
1324
}
1325
1326
if (nextCodePoint === 0x5b) {
1269
- let cursor = index + 2;
1327
+ let cursor = contentStart;
1328
// Inside a CSI Ghostty executes 0x00-0x0F and ignores DEL and 0x10-0x1F, which it would
1329
// print in ground state. None join the parameters; only executed ones survive a strip.
1330
let body = "";
@@ -1276,25 +1334,47 @@ function sanitizeTerminalHistoryChunk(
1334
const cancelled = isControlSequenceCancel(cursorCodePoint);
1335
if (cancelled || isCsiFinalByte(cursorCodePoint)) {
1336
if (cancelled || !shouldStripCsiSequence(body, input[cursor] ?? "")) {
1279
- append(input.slice(index, cursor + 1));
1337
+ append(prefix + input.slice(index, cursor + 1));
1338
} else {
1339
append(executedControls);
1340
}
1341
index = cursor + 1;
1342
break;
1343
}
1344
+ if (cursorCodePoint >= 0x80) {
1345
+ const fullCodePoint = input.codePointAt(cursor) ?? cursorCodePoint;
1346
+ const end = cursor + (fullCodePoint > 0xffff ? 2 : 1);
1347
+ const c1 = c1Effect(fullCodePoint);
1348
+ if (c1) {
1349
+ // The CSI ends here undispatched, so it is kept only when the terminal is back in
1350
+ // ground with nothing to answer.
1351
+ if (c1.next.length === 0 && !c1.query) {
1352
+ append(prefix + input.slice(index, end));
1353
+ } else {
1354
+ append(executedControls + c1.safe);
1355
+ }
1356
+ c1Introducer = c1.next;
1357
+ index = end;
1358
+ break;
1359
+ }
1360
+ cursor = end;
1361
+ continue;
1362
+ }
1363
+ // An ESC abandons the CSI and starts the next sequence, which may be stripped.
1364
+ if (cursorCodePoint === 0x1b) {
1365
+ append(executedControls);
1366
+ index = cursor;
1367
+ break;
1368
+ }
1369
if (cursorCodePoint < 0x10) {
1370
executedControls += input[cursor];
1288
- } else if (
1289
- (cursorCodePoint >= 0x20 || cursorCodePoint === 0x1b) &&
1290
- cursorCodePoint !== 0x7f
1291
- ) {
1371
+ } else if (cursorCodePoint >= 0x20 && cursorCodePoint !== 0x7f) {
1372
body += input[cursor];
1373
}
1374
cursor += 1;
1375
}
1376
if (cursor >= input.length) {
1297
- return { visibleText, pendingControlSequence: input.slice(index) };
1377
+ return { visibleText, pendingControlSequence: prefix + input.slice(index) };
1378
}
1379
continue;
1380
}
@@ -1305,23 +1385,32 @@ function sanitizeTerminalHistoryChunk(
1385
nextCodePoint === 0x5e ||
1386
nextCodePoint === 0x5f
1387
) {
1308
- const terminatorIndex = findStringTerminatorIndex(
1388
+ const stringEnd = findStringEnd(
1389
input,
1310
- Math.max(index + 2, pendingScanStart),
1390
+ Math.max(contentStart, pendingScanStart),
1391
nextCodePoint === 0x5d,
1392
);
1313
- if (terminatorIndex === null) {
1314
- return { visibleText, pendingControlSequence: input.slice(index) };
1393
+ if (stringEnd === null) {
1394
+ return { visibleText, pendingControlSequence: prefix + input.slice(index) };
1395
}
1316
- const sequence = input.slice(index, terminatorIndex);
1317
- const content = stripStringTerminator(input.slice(index + 2, terminatorIndex));
1396
+ const { end, c1 } = stringEnd;
1397
+ const content = stripStringTerminator(
1398
+ prefix.slice(2) + input.slice(contentStart, c1?.index ?? end),
1399
+ );
1400
const strip =
1401
(nextCodePoint === 0x5d && shouldStripOscSequence(content)) ||
1402
(nextCodePoint === 0x50 && shouldStripDcsSequence(content));
1321
- if (!strip) {
1322
- append(sequence);
1403
+ if (c1 === undefined || (!strip && c1.effect.next.length === 0 && !c1.effect.query)) {
1404
+ if (!strip) {
1405
+ append(prefix + input.slice(index, end));
1406
+ }
1407
+ } else {
1408
+ // ST stands in for a code point that cannot be replayed as it came.
1409
+ const closed = strip ? "" : `${prefix}${input.slice(index, c1.index)}\u009c`;
1410
+ append(closed + c1.effect.safe);
1411
+ c1Introducer = c1.effect.next;
1412
}
1324
- index = terminatorIndex;
1413
+ index = end;
1414
continue;
1415
}
1416
@@ -1344,7 +1433,7 @@ function sanitizeTerminalHistoryChunk(
1433
index = nextSequenceStart;
1434
}
1435
1347
- return { visibleText, pendingControlSequence: "" };
1436
+ return { visibleText, pendingControlSequence: c1Introducer };
1437
}
1438
1439
function legacySafeThreadId(threadId: string): string {