Decode streamed update output at character boundaries

The update popup kept the last 64 KiB of raw bytes and decoded the
whole buffer on every refresh. Cutting the buffer, or reading a chunk
that ended inside a multi-byte sequence, made the UTF-8 decode fail and
sent everything through the GB18030 fallback, garbling the display.

Clip the retained tail at a UTF-8 boundary and decode still-growing
streams with an incremental decoder that holds back an incomplete
trailing sequence instead of treating it as corruption.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
2026-10-03 03:00:09 +00:00
co-authored by Claude Fable 5.1
parent ebbd5821fd
commit 3f11734ce4
3 changed files with 60 additions and 5 deletions
+9 -4
View File
@@ -18,7 +18,7 @@ import uuid
import unicodedata
from .remotes import (
RemoteEntry, decode_remote_output, parse_remote_session_result,
RemoteEntry, clip_utf8_tail, decode_remote_output, parse_remote_session_result,
remote_control_command, remote_session_command,
)
@@ -177,13 +177,18 @@ class HerdrControl:
chunk = os.pread(output.fileno(), 65536, position)
if chunk:
position += len(chunk)
captured = (captured + chunk)[-65536:]
progress(decode_remote_output(captured).replace("\r", "\n"))
# Bound memory at a character boundary; the
# chunk may also end mid-sequence, so decode
# it as a still-growing stream.
captured = clip_utf8_tail(captured + chunk, 65536)
progress(
decode_remote_output(captured, partial=True).replace("\r", "\n")
)
last_output = time.monotonic()
now = time.monotonic()
if not finished and now - last_notice >= 1:
progress(
decode_remote_output(captured).replace("\r", "\n")
decode_remote_output(captured, partial=True).replace("\r", "\n")
+ f"\n已执行 {int(now - started)} 秒 · {int(now - last_output)} 秒无新输出"
)
last_notice = now