TypeScript
fix(server): keep terminal history after cancelled sequences (#25193)
mariac839797b6dce
diff --git a/apps/server/src/terminal/Manager.test.ts b/apps/server/src/terminal/Manager.test.tsindex e30a4cd3d72..5b166d8b145 100644--- a/apps/server/src/terminal/Manager.test.ts+++ b/apps/server/src/terminal/Manager.test.ts@@ -881,6 +881,35 @@ it.layer(}),);+ it.effect.each([+ { name: "OSC", sequence: "\u001b]0;unfinished" },+ { name: "DCS", sequence: "\u001bPunfinished" },+ { 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();+ yield* manager.open(openInput());+ const process = ptyAdapter.processes[0]!;+ for (const cancel of ["\u0018", "\u001a"]) {+ process.emitData(sequence);+ process.emitData(cancel);+ process.emitData("next line\n");+ process.emitData(`${sequence}${cancel}next line\n`);+ }+ yield* manager.close({ threadId: "thread-1" });++ const reopened = yield* manager.open(openInput());+ expect(reopened.history.match(/next line/g)).toHaveLength(4);+ }),+ );+it.effect("handles CSI and DCS query sequences split across output chunks", () =>Effect.gen(function* () {const { manager, ptyAdapter } = yield* createManager();diff --git a/apps/server/src/terminal/Manager.ts b/apps/server/src/terminal/Manager.tsindex 4c5229a7bae..a08b3e4afe7 100644--- a/apps/server/src/terminal/Manager.ts+++ b/apps/server/src/terminal/Manager.ts@@ -1067,6 +1067,10 @@ function isCsiFinalByte(codePoint: number): boolean {return codePoint >= 0x40 && codePoint <= 0x7e;}+function isControlSequenceCancel(codePoint: number): boolean {+ return codePoint === 0x18 || codePoint === 0x1a;+}+function shouldStripCsiSequence(body: string, finalByte: string): boolean {if (finalByte === "n") {return true;@@ -1115,7 +1119,7 @@ function stripStringTerminator(value: string): string {function findStringTerminatorIndex(input: string, start: number): number | null {for (let index = start; index < input.length; index += 1) {const codePoint = input.charCodeAt(index);- if (codePoint === 0x07 || codePoint === 0x9c) {+ if (codePoint === 0x07 || codePoint === 0x9c || isControlSequenceCancel(codePoint)) {return index + 1;}if (codePoint === 0x1b && input.charCodeAt(index + 1) === 0x5c) {@@ -1177,10 +1181,11 @@ function sanitizeTerminalHistoryChunk(if (nextCodePoint === 0x5b) {let cursor = index + 2;while (cursor < input.length) {- if (isCsiFinalByte(input.charCodeAt(cursor))) {+ const cancelled = isControlSequenceCancel(input.charCodeAt(cursor));+ if (cancelled || isCsiFinalByte(input.charCodeAt(cursor))) {const sequence = input.slice(index, cursor + 1);const body = input.slice(index + 2, cursor);- if (!shouldStripCsiSequence(body, input[cursor] ?? "")) {+ if (cancelled || !shouldStripCsiSequence(body, input[cursor] ?? "")) {append(sequence);}index = cursor + 1;@@ -1231,10 +1236,11 @@ function sanitizeTerminalHistoryChunk(if (codePoint === 0x9b) {let cursor = index + 1;while (cursor < input.length) {- if (isCsiFinalByte(input.charCodeAt(cursor))) {+ 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 (!shouldStripCsiSequence(body, input[cursor] ?? "")) {+ if (cancelled || !shouldStripCsiSequence(body, input[cursor] ?? "")) {append(sequence);}index = cursor + 1;