@devries-cameron20 / t3code-1 / commits / 594532a73f
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(server): terminal history keeps output after an unterminated OSC (#29604)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Alex Southwell committed Nov 3, 2026 at 23:15 UTC 594532a73fc960981a43aebc65ae84ce41a3af8c
2 files changed +18 -8
apps/server/src/terminal/Manager.test.ts
+10 -6
@@ -1142,14 +1142,17 @@ it.layer(
1142 process.emitData("\u0090$q m\u009c\u00901$r0m\u009c");
1143 process.emitData("\u0090+q544e\u009c\u00901+r544e=1b\u009c");
1144 process.emitData('\u001b[!p\u001b["p\u001b[4 q\u001b[u');
1145 + process.emitData("\u001b]0;x\u001b\u001b[?2026$p");
1146 process.emitData("done\n");
1147
1148 yield* manager.close({ threadId: "thread-1" });
1149
1150 const reopened = yield* manager.open(openInput());
1150 - expect(reopened.history.startsWith('prompt \u001b[!p\u001b["p\u001b[4 q\u001b[udone\n')).toBe(
1151 - true,
1152 - );
1151 + expect(
1152 + reopened.history.startsWith(
1153 + 'prompt \u001b[!p\u001b["p\u001b[4 q\u001b[u\u001b]0;x\u001bdone\n',
1154 + ),
1155 + ).toBe(true);
1156 }),
1157 );
1158
@@ -1166,10 +1169,11 @@ it.layer(
1169 { name: "C1 CSI", sequence: "\u009b5" },
1170 ])("keeps output after a cancelled $name sequence in history", ({ sequence }) =>
1171 Effect.gen(function* () {
1169 - const { manager, ptyAdapter } = yield* createManager();
1172 + const { manager, ptyAdapter } = yield* createManager(10);
1173 yield* manager.open(openInput());
1174 const process = ptyAdapter.processes[0]!;
1172 - for (const cancel of ["\u0018", "\u001a"]) {
1175 + // A new escape sequence also aborts one, as in xterm.js; keep it last so CAN cannot end it.
1176 + for (const cancel of ["\u0018", "\u001a", "\u001b[0m"]) {
1177 process.emitData(sequence);
1178 process.emitData(cancel);
1179 process.emitData("next line\n");
@@ -1178,7 +1182,7 @@ it.layer(
1182 yield* manager.close({ threadId: "thread-1" });
1183
1184 const reopened = yield* manager.open(openInput());
1181 - expect(reopened.history.match(/next line/g)).toHaveLength(4);
1185 + expect(reopened.history.match(/next line/g)).toHaveLength(6);
1186 }),
1187 );
1188
apps/server/src/terminal/Manager.ts
+8 -2
@@ -1142,8 +1142,10 @@ function findStringTerminatorIndex(input: string, start: number): number | null
1142 if (codePoint === 0x07 || codePoint === 0x9c || isControlSequenceCancel(codePoint)) {
1143 return index + 1;
1144 }
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;
1149 }
1150 }
1151 return null;
@@ -1165,6 +1167,10 @@ function findEscapeSequenceEndIndex(input: string, start: number): number | null
1167 if (cursor >= input.length) {
1168 return null;
1169 }
1170 + // A second ESC restarts the sequence, so leave it for the next pass.
1171 + if (input.charCodeAt(cursor) === 0x1b) {
1172 + return cursor;
1173 + }
1174 return isEscapeFinalByte(input.charCodeAt(cursor)) ? cursor + 1 : start + 1;
1175 }
1176