Skip to content

Commit e863548

Browse files
saphidclaude
authored andcommitted
fix(server): terminal history keeps output after an unterminated OSC
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 60aec28 commit e863548

2 files changed

Lines changed: 8 additions & 5 deletions

File tree

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1161,10 +1161,11 @@ it.layer(
11611161
{ name: "C1 CSI", sequence: "\u009b5" },
11621162
])("keeps output after a cancelled $name sequence in history", ({ sequence }) =>
11631163
Effect.gen(function* () {
1164-
const { manager, ptyAdapter } = yield* createManager();
1164+
const { manager, ptyAdapter } = yield* createManager(10);
11651165
yield* manager.open(openInput());
11661166
const process = ptyAdapter.processes[0]!;
1167-
for (const cancel of ["\u0018", "\u001a"]) {
1167+
// A new escape sequence also aborts one, as in xterm.js; keep it last so CAN cannot end it.
1168+
for (const cancel of ["\u0018", "\u001a", "\u001b[0m"]) {
11681169
process.emitData(sequence);
11691170
process.emitData(cancel);
11701171
process.emitData("next line\n");
@@ -1173,7 +1174,7 @@ it.layer(
11731174
yield* manager.close({ threadId: "thread-1" });
11741175

11751176
const reopened = yield* manager.open(openInput());
1176-
expect(reopened.history.match(/next line/g)).toHaveLength(4);
1177+
expect(reopened.history.match(/next line/g)).toHaveLength(6);
11771178
}),
11781179
);
11791180

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,8 +1142,10 @@ function findStringTerminatorIndex(input: string, start: number): number | null
11421142
if (codePoint === 0x07 || codePoint === 0x9c || isControlSequenceCancel(codePoint)) {
11431143
return index + 1;
11441144
}
1145-
if (codePoint === 0x1b && input.charCodeAt(index + 1) === 0x5c) {
1146-
return index + 2;
1145+
// Like xterm.js, an ESC that does not start ST ends the string and begins the next sequence.
1146+
if (codePoint === 0x1b) {
1147+
if (index + 1 >= input.length) return null;
1148+
return input.charCodeAt(index + 1) === 0x5c ? index + 2 : index;
11471149
}
11481150
}
11491151
return null;

0 commit comments

Comments
 (0)