mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-18 15:28:48 +03:00
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.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user