@devries-cameron20 / t3code-1 / commits / cd890ba346
Fork 0

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

fix(shared): command output no longer prints the final byte of escape sequences with intermediates (#30724)

Co-authored-by: Geezy <igor.licko@gloryck.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

TheGuyWithHAT committed Nov 6, 2026 at 05:37 UTC cd890ba3469956fac81f1989641f409c66b03d1b
2 files changed +31 -4
packages/shared/src/terminalOutput.test.ts
+18
@@ -131,6 +131,24 @@ describe("appendTerminalOutput under adversarial output", () => {
131 }
132 });
133
134 + it("drops escape sequences with intermediate bytes without printing their final byte", () => {
135 + for (const sequence of ["#8", "#6", "%G", " F", "-A", ".A", "(%5", "(B"]) {
136 + const input = `a\u001b${sequence}b`;
137 + expect(feed([input]).text).toBe("ab");
138 + for (let cut = 1; cut < input.length; cut += 1) {
139 + expect(feed([input.slice(0, cut), input.slice(cut)]).text).toBe("ab");
140 + }
141 + }
142 + for (const [interrupted, expected] of [
143 + ["a\u001b#\u001b[31mb", feed(["a\u001b[31mb"]).text],
144 + ["a\u001b#\n8b", "a\n8b"],
145 + ] as const) {
146 + for (let cut = 0; cut < interrupted.length; cut += 1) {
147 + expect(feed([interrupted.slice(0, cut), interrupted.slice(cut)]).text).toBe(expected);
148 + }
149 + }
150 + });
151 +
152 it("gives the same result however the output is split into chunks", () => {
153 const input =
154 "abcdef\rX\u001b[31mred\u001b[0m\nnext line\r\u001b[2Kdone\n" +
packages/shared/src/terminalOutput.ts
+13 -4
@@ -268,11 +268,20 @@ function escapeLength(input: string, index: number): number {
268 }
269 return -1;
270 }
271 - // Charset designations take three bytes; other sequences take two.
272 - if (next === "(" || next === ")" || next === "*" || next === "+") {
273 - return index + 2 < input.length ? 3 : -1;
271 + // ECMA-35: any intermediate bytes (0x20-0x2F, e.g. charset designations, DECALN) precede one final byte.
272 + let cursor = index + 1;
273 + while (
274 + cursor < input.length &&
275 + input.charCodeAt(cursor) >= 0x20 &&
276 + input.charCodeAt(cursor) <= 0x2f
277 + ) {
278 + cursor += 1;
279 }
275 - return 2;
280 + if (cursor >= input.length) return -1;
281 + // After intermediates, a control or ESC is not a final byte; end before it so it is still processed.
282 + const final = input.charCodeAt(cursor);
283 + if (cursor > index + 1 && (final < 0x30 || final > 0x7e)) return cursor - index;
284 + return cursor - index + 1;
285 }
286
287 const penTransitions = new Map<string, string>();