From a94944e4d2b0c4f2412aecc255b8216a35b2f5de Mon Sep 17 00:00:00 2001 From: Kit Langton Date: Fri, 6 Nov 2026 17:10:56 -0800 Subject: [PATCH] fix(core): keep PTY replay encodable across chunk and buffer boundaries Output was sliced by UTF-16 index, so a chunk or buffer trim could keep only one half of a surrogate pair. Frames travel as UTF-8, where a lone surrogate encodes as U+FFFD, so replayed output showed a replacement character instead of the emoji that straddled the boundary. --- packages/core/src/pty.ts | 5 ++++- packages/core/src/pty/protocol.ts | 9 ++++++++- packages/core/test/pty/protocol.test.ts | 11 +++++++++++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/packages/core/src/pty.ts b/packages/core/src/pty.ts index 5af2f902f19..8eff0516e29 100644 --- a/packages/core/src/pty.ts +++ b/packages/core/src/pty.ts @@ -216,7 +216,10 @@ const layer = Layer.effect( } session.buffer += chunk if (session.buffer.length <= BUFFER_LIMIT) return - session.buffer = session.buffer.slice(session.buffer.length - BUFFER_LIMIT) + const excess = session.buffer.length - BUFFER_LIMIT + const first = session.buffer.charCodeAt(excess) + // Keep both halves of a surrogate pair so replay stays encodable. + session.buffer = session.buffer.slice(first >= 0xdc00 && first <= 0xdfff ? excess - 1 : excess) }), proc.onExit(({ exitCode }) => { if (session.info.status === "exited") return diff --git a/packages/core/src/pty/protocol.ts b/packages/core/src/pty/protocol.ts index e9b1741e38c..be5b31754d6 100644 --- a/packages/core/src/pty/protocol.ts +++ b/packages/core/src/pty/protocol.ts @@ -22,7 +22,14 @@ export function metaFrame(cursor: number) { export function chunks(data: string) { const out: string[] = [] - for (let i = 0; i < data.length; i += REPLAY_CHUNK) out.push(data.slice(i, i + REPLAY_CHUNK)) + for (let start = 0; start < data.length; ) { + let end = Math.min(start + REPLAY_CHUNK, data.length) + // A split between the halves of a surrogate pair encodes as U+FFFD, so keep the pair together. + const last = data.charCodeAt(end - 1) + if (end < data.length && last >= 0xd800 && last <= 0xdbff) end-- + out.push(data.slice(start, end)) + start = end + } return out } diff --git a/packages/core/test/pty/protocol.test.ts b/packages/core/test/pty/protocol.test.ts index 85090d44387..0fed0962173 100644 --- a/packages/core/test/pty/protocol.test.ts +++ b/packages/core/test/pty/protocol.test.ts @@ -24,4 +24,15 @@ describe("pty protocol", () => { expect(frames[0].length).toBe(PtyProtocol.REPLAY_CHUNK) expect(frames.join("")).toBe(big) }) + + test("keeps both halves of a surrogate pair in one frame", () => { + // Frames travel as UTF-8, where a lone surrogate encodes as U+FFFD. Here the emoji straddles the + // first chunk boundary, because the prefix is one UTF-16 unit short of it. + const data = `${"a".repeat(PtyProtocol.REPLAY_CHUNK - 1)}😀${"b".repeat(8)}` + const decode = (value: string) => new TextDecoder().decode(new TextEncoder().encode(value)) + const frames = PtyProtocol.chunks(data) + expect(frames.length).toBe(2) + expect(frames.join("")).toBe(data) + for (const frame of frames) expect(decode(frame)).toBe(frame) + }) })