Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

fix(terminal): decode POSIX PTY output incrementally so split UTF-8 glyphs survive

rubenluetke10-beep262be7482a80
diff --git a/jarvis/terminal/backend.py b/jarvis/terminal/backend.py
index bbde68c484..b0ba03f256 100644
--- a/jarvis/terminal/backend.py
+++ b/jarvis/terminal/backend.py
@@ -18,7 +18,9 @@ read-loop in ``pty_manager.py`` keeps working verbatim once it talks to a
``RuntimeError("pywinpty not installed ...")`` degrade.
* ``ptyprocess.PtyProcess`` deals in ``bytes`` (``.read`` returns bytes,
``.write`` takes bytes), so ``UnixPtyBackend`` decodes ``utf-8`` with
- ``errors="replace"`` on read and encodes on write.
+ ``errors="replace"`` on read and encodes on write. The decode is
+ INCREMENTAL: a read can end in the middle of a multi-byte sequence, and
+ a per-chunk ``bytes.decode`` would turn both halves into U+FFFD.
Import-cleanliness contract (HN-7): neither ``winpty`` nor ``ptyprocess`` is
imported at module scope. Both imports are lazy, inside ``spawn`` (the same
@@ -29,6 +31,7 @@ installed.
from __future__ import annotations
+import codecs
from collections.abc import Mapping
from typing import Protocol, runtime_checkable
@@ -167,10 +170,16 @@ class WinptyBackend:
class _UnixPtyHandle:
"""``PtyHandle`` over a ``ptyprocess.PtyProcess``, normalizing str<->bytes."""
- __slots__ = ("_proc",)
+ __slots__ = ("_proc", "_decoder")
def __init__(self, proc: object) -> None:
self._proc = proc
+ # One incremental decoder per handle: a fixed-size read can split a
+ # multi-byte UTF-8 sequence at the chunk boundary (every box-drawing
+ # glyph a TUI repaints is 3 bytes), and decoding each chunk in
+ # isolation turns both halves into U+FFFD. The incremental decoder
+ # holds the partial tail until the next read completes it.
+ self._decoder = codecs.getincrementaldecoder("utf-8")(errors="replace")
@property
def pid(self) -> int:
@@ -209,7 +218,7 @@ class _UnixPtyHandle:
# ptyprocess.PtyProcess.read returns bytes; decode at the seam.
data = self._proc.read(size) # type: ignore[attr-defined]
if isinstance(data, bytes):
- return data.decode("utf-8", errors="replace")
+ return self._decoder.decode(data)
return str(data)
def isalive(self) -> bool:
diff --git a/tests/unit/terminal/test_unix_pty.py b/tests/unit/terminal/test_unix_pty.py
index b6504b63f7..1f3a1eaf4a 100644
--- a/tests/unit/terminal/test_unix_pty.py
+++ b/tests/unit/terminal/test_unix_pty.py
@@ -163,6 +163,28 @@ def test_unix_backend_decodes_bytes_to_str_on_read():
assert out == "héllo"
+def test_unix_backend_reassembles_multibyte_utf8_split_across_reads():
+ """A fixed-size read can cut a multi-byte UTF-8 sequence at the boundary.
+
+ ptyprocess hands back raw byte chunks with no regard for character
+ boundaries; decoding each chunk in isolation turned both halves of a split
+ glyph into U+FFFD (every box-drawing glyph a TUI repaints is 3 bytes, every
+ umlaut 2). The handle must hold the partial tail until the next read.
+ """
+ from jarvis.terminal.backend import _UnixPtyHandle
+
+ fake_proc = FakeBytesPtyProcess()
+ text = "bé─e" # 2-byte e-acute and 3-byte box-drawing glyph
+ payload = text.encode()
+ fake_proc.queue_output(payload[:2]) # ends inside the 2-byte sequence
+ fake_proc.queue_output(payload[2:4]) # completes it, ends inside the 3-byte one
+ fake_proc.queue_output(payload[4:]) # completes the 3-byte sequence
+ h = _UnixPtyHandle(fake_proc)
+ out = h.read(4096) + h.read(4096) + h.read(4096)
+ assert out == text
+ assert "�" not in out
+
+
def test_unix_backend_encodes_str_to_bytes_on_write():
"""handle.write takes str -> ptyprocess.write must receive bytes."""
from jarvis.terminal.backend import _UnixPtyHandle