fix(exec): preserve UTF-8 across streaming output chunks
This commit is contained in:
1 parent
8ea683679d
commit
dfed706bc0
2 files changed
+32
-3
No files matched your search
@@ -3,6 +3,7 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
|
import codecs
|
||||||
import time
|
import time
|
||||||
import uuid
|
import uuid
|
||||||
from collections import deque
|
from collections import deque
|
||||||
@@ -149,13 +150,14 @@ class _ExecSession:
|
|||||||
) -> None:
|
) -> None:
|
||||||
if stream is None:
|
if stream is None:
|
||||||
return
|
return
|
||||||
|
decoder = codecs.getincrementaldecoder("utf-8")(errors="replace")
|
||||||
while True:
|
while True:
|
||||||
chunk = await stream.read(4096)
|
chunk = await stream.read(4096)
|
||||||
if not chunk:
|
text = decoder.decode(chunk, final=not chunk)
|
||||||
break
|
|
||||||
text = chunk.decode("utf-8", errors="replace")
|
|
||||||
async with self._lock:
|
async with self._lock:
|
||||||
buffer.append(text)
|
buffer.append(text)
|
||||||
|
if not chunk:
|
||||||
|
break
|
||||||
|
|
||||||
async def write(self, chars: str) -> str | None:
|
async def write(self, chars: str) -> str | None:
|
||||||
if self.process.returncode is not None:
|
if self.process.returncode is not None:
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ from nanobot.agent.tools.exec_session import (
|
|||||||
ExecSessionTool,
|
ExecSessionTool,
|
||||||
ListExecSessionsTool,
|
ListExecSessionsTool,
|
||||||
_BoundedOutputBuffer,
|
_BoundedOutputBuffer,
|
||||||
|
_ExecSession,
|
||||||
_SessionPoll,
|
_SessionPoll,
|
||||||
_truncate_output,
|
_truncate_output,
|
||||||
)
|
)
|
||||||
@@ -151,6 +152,32 @@ def test_bounded_output_buffer_keeps_head_tail_and_exact_drop_count():
|
|||||||
assert buffer.retained_chars == 0
|
assert buffer.retained_chars == 0
|
||||||
|
|
||||||
|
|
||||||
|
async def test_exec_session_preserves_utf8_across_stdout_and_stderr_chunks():
|
||||||
|
stdout = asyncio.StreamReader()
|
||||||
|
stderr = asyncio.StreamReader()
|
||||||
|
stdout_bytes = b"a" * 4095 + "你好🙂".encode() + b"\xe2\x82"
|
||||||
|
stderr_bytes = b"b" * 4094 + "🙂é".encode() + b"\xff"
|
||||||
|
stdout.feed_data(stdout_bytes[:4096])
|
||||||
|
stderr.feed_data(stderr_bytes[:4096])
|
||||||
|
session = _ExecSession(
|
||||||
|
session_id="utf8-output",
|
||||||
|
process=SimpleNamespace(stdout=stdout, stderr=stderr),
|
||||||
|
command="test",
|
||||||
|
cwd=".",
|
||||||
|
timeout=None,
|
||||||
|
)
|
||||||
|
|
||||||
|
await asyncio.sleep(0)
|
||||||
|
stdout.feed_data(stdout_bytes[4096:])
|
||||||
|
stderr.feed_data(stderr_bytes[4096:])
|
||||||
|
stdout.feed_eof()
|
||||||
|
stderr.feed_eof()
|
||||||
|
await asyncio.gather(session._stdout_task, session._stderr_task)
|
||||||
|
|
||||||
|
assert session._stdout.drain() == ("a" * 4095 + "你好🙂\ufffd", 0)
|
||||||
|
assert session._stderr.drain() == ("b" * 4094 + "🙂é\ufffd", 0)
|
||||||
|
|
||||||
|
|
||||||
def test_exec_session_bounds_unpolled_stdout_and_stderr(tmp_path):
|
def test_exec_session_bounds_unpolled_stdout_and_stderr(tmp_path):
|
||||||
async def run() -> tuple[int, int, str, int]:
|
async def run() -> tuple[int, int, str, int]:
|
||||||
manager = ExecSessionManager()
|
manager = ExecSessionManager()
|
||||||
|
|||||||
Reference in new issue
Block a user