From 9f49ffc2480062d9c1e2b321ade18d6d9639271a Mon Sep 17 00:00:00 2001 From: George Levis Date: Tue, 26 May 2026 12:36:42 +0300 Subject: [PATCH] fix(shell): use byte length for preview truncation Previously preview() used text.length (UTF-16 code units) which caused incorrect truncation for multi-byte characters. Changed to use Buffer.byteLength() matching the tail() function pattern. Fixes #29291 --- packages/opencode/src/tool/shell.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/opencode/src/tool/shell.ts b/packages/opencode/src/tool/shell.ts index b6a95b5c0970..8709096a0150 100644 --- a/packages/opencode/src/tool/shell.ts +++ b/packages/opencode/src/tool/shell.ts @@ -221,8 +221,11 @@ function pathArgs(list: Part[], ps: boolean, cmd = false) { } function preview(text: string) { - if (text.length <= MAX_METADATA_LENGTH) return text - return "...\n\n" + text.slice(-MAX_METADATA_LENGTH) + if (Buffer.byteLength(text, "utf-8") <= MAX_METADATA_LENGTH) return text + const buf = Buffer.from(text, "utf-8") + let start = buf.length - MAX_METADATA_LENGTH + while (start < buf.length && (buf[start] & 0xc0) === 0x80) start++ + return "...\n\n" + buf.subarray(start).toString("utf-8") } function tail(text: string, maxLines: number, maxBytes: number) {