The best way to control Claude Code, Codex and more from any computer or phone
fix(server): keep terminal history after cancelled sequences (#25193)
Co-authored-by: Aditya Garud <153842990+yashranaway@users.noreply.github.com>
maria committed
Oct 29, 2026 at 17:48 UTC
c839797b6dceaa28cc61b7044fcae36bd7a11462
2 files changed
+40
-5
apps/server/src/terminal/Manager.test.ts
+29
@@ -881,6 +881,35 @@ it.layer(
881
}),
882
);
883
884
+ it.effect.each([
885
+ { name: "OSC", sequence: "\u001b]0;unfinished" },
886
+ { name: "DCS", sequence: "\u001bPunfinished" },
887
+ { name: "PM", sequence: "\u001b^unfinished" },
888
+ { name: "APC", sequence: "\u001b_unfinished" },
889
+ { name: "CSI", sequence: "\u001b[5" },
890
+ { name: "C1 OSC", sequence: "\u009d0;unfinished" },
891
+ { name: "C1 DCS", sequence: "\u0090unfinished" },
892
+ { name: "C1 PM", sequence: "\u009eunfinished" },
893
+ { name: "C1 APC", sequence: "\u009funfinished" },
894
+ { name: "C1 CSI", sequence: "\u009b5" },
895
+ ])("keeps output after a cancelled $name sequence in history", ({ sequence }) =>
896
+ Effect.gen(function* () {
897
+ const { manager, ptyAdapter } = yield* createManager();
898
+ yield* manager.open(openInput());
899
+ const process = ptyAdapter.processes[0]!;
900
+ for (const cancel of ["\u0018", "\u001a"]) {
901
+ process.emitData(sequence);
902
+ process.emitData(cancel);
903
+ process.emitData("next line\n");
904
+ process.emitData(`${sequence}${cancel}next line\n`);
905
+ }
906
+ yield* manager.close({ threadId: "thread-1" });
907
+
908
+ const reopened = yield* manager.open(openInput());
909
+ expect(reopened.history.match(/next line/g)).toHaveLength(4);
910
+ }),
911
+ );
912
+
913
it.effect("handles CSI and DCS query sequences split across output chunks", () =>
914
Effect.gen(function* () {
915
const { manager, ptyAdapter } = yield* createManager();
apps/server/src/terminal/Manager.ts
+11
-5
@@ -1067,6 +1067,10 @@ function isCsiFinalByte(codePoint: number): boolean {
1067
return codePoint >= 0x40 && codePoint <= 0x7e;
1068
}
1069
1070
+function isControlSequenceCancel(codePoint: number): boolean {
1071
+ return codePoint === 0x18 || codePoint === 0x1a;
1072
+}
1073
+
1074
function shouldStripCsiSequence(body: string, finalByte: string): boolean {
1075
if (finalByte === "n") {
1076
return true;
@@ -1115,7 +1119,7 @@ function stripStringTerminator(value: string): string {
1119
function findStringTerminatorIndex(input: string, start: number): number | null {
1120
for (let index = start; index < input.length; index += 1) {
1121
const codePoint = input.charCodeAt(index);
1118
- if (codePoint === 0x07 || codePoint === 0x9c) {
1122
+ if (codePoint === 0x07 || codePoint === 0x9c || isControlSequenceCancel(codePoint)) {
1123
return index + 1;
1124
}
1125
if (codePoint === 0x1b && input.charCodeAt(index + 1) === 0x5c) {
@@ -1177,10 +1181,11 @@ function sanitizeTerminalHistoryChunk(
1181
if (nextCodePoint === 0x5b) {
1182
let cursor = index + 2;
1183
while (cursor < input.length) {
1180
- if (isCsiFinalByte(input.charCodeAt(cursor))) {
1184
+ const cancelled = isControlSequenceCancel(input.charCodeAt(cursor));
1185
+ if (cancelled || isCsiFinalByte(input.charCodeAt(cursor))) {
1186
const sequence = input.slice(index, cursor + 1);
1187
const body = input.slice(index + 2, cursor);
1183
- if (!shouldStripCsiSequence(body, input[cursor] ?? "")) {
1188
+ if (cancelled || !shouldStripCsiSequence(body, input[cursor] ?? "")) {
1189
append(sequence);
1190
}
1191
index = cursor + 1;
@@ -1231,10 +1236,11 @@ function sanitizeTerminalHistoryChunk(
1236
if (codePoint === 0x9b) {
1237
let cursor = index + 1;
1238
while (cursor < input.length) {
1234
- if (isCsiFinalByte(input.charCodeAt(cursor))) {
1239
+ const cancelled = isControlSequenceCancel(input.charCodeAt(cursor));
1240
+ if (cancelled || isCsiFinalByte(input.charCodeAt(cursor))) {
1241
const sequence = input.slice(index, cursor + 1);
1242
const body = input.slice(index + 1, cursor);
1237
- if (!shouldStripCsiSequence(body, input[cursor] ?? "")) {
1243
+ if (cancelled || !shouldStripCsiSequence(body, input[cursor] ?? "")) {
1244
append(sequence);
1245
}
1246
index = cursor + 1;