Files
openclaw/scripts/lib/output-tail.mjs
wings1029 796ee27b3a fix(scripts): keep format-docs output tails UTF-8 safe (#110825)
* fix(scripts): keep format-docs output tails UTF-8 safe

When formatter (oxfmt) error output exceeds the 16 KiB tail cap and the
retained byte window starts inside a multibyte UTF-8 character, the
diagnostic tail would begin with a replacement character (�).

Add a decodeUtf8Tail helper that skips leading UTF-8 continuation bytes
before decoding the byte suffix, matching the existing pattern in
scripts/run-additional-boundary-checks.mjs (#109167).

Keep the existing 16 KiB cap and ASCII behavior unchanged.

* refactor(scripts): share UTF-8-safe output tails

Co-authored-by: 陈志强0668000989 <chen.zhiqiang1@xydigit.com>

* test(scripts): prove real formatter UTF-8 failures

---------

Co-authored-by: Peter Steinberger <steipete@gmail.com>
2026-07-29 08:29:24 -04:00

32 lines
765 B
JavaScript

// Keeps child-process diagnostic tails byte-bounded without splitting UTF-8 characters.
function outputText(value) {
if (typeof value === "string") {
return value;
}
if (Buffer.isBuffer(value)) {
return value.toString("utf8");
}
return "";
}
function decodeUtf8Tail(buffer) {
let start = 0;
while (start < buffer.length && (buffer[start] & 0b1100_0000) === 0b1000_0000) {
start += 1;
}
return buffer.subarray(start).toString("utf8");
}
export function outputTail(value, maxBytes) {
const text = outputText(value).trim();
if (!text) {
return "";
}
const bytes = Buffer.from(text, "utf8");
if (bytes.byteLength <= maxBytes) {
return text;
}
return decodeUtf8Tail(bytes.subarray(bytes.byteLength - maxBytes));
}