Please read this first
Describe the bug
collect_pty_output decodes each collection window with errors="replace". PTY output is not collected once, it is collected in repeated windows over one persistent chunk deque: pty_exec_start takes a window, then every pty_write_stdin takes another from the same entry.output_chunks.
So a multi-byte character whose bytes land either side of a window boundary is decoded as two separate partial sequences, and both halves become U+FFFD. The bytes are destroyed at that first decode, so no later window can reassemble the character. There is no error and no warning; the text is silently wrong.
This is not backend-specific. collect_pty_output is the shared helper behind the unix_local, docker, blaxel, daytona and modal PTY paths.
Debug information
- Agents SDK version:
main at 89c02c82, and v0.22.0
- Related library versions: none
- Python version: 3.14
- Operating system: macOS arm64; no platform-specific code involved
- Model and model provider: none, this is pure session-layer code
- Does the issue reproduce with the latest Agents SDK release? Yes
- Does the issue occur consistently or intermittently? Consistently for a given split position
Repro steps
import asyncio
from collections import deque
from agents.sandbox.session.pty_output import collect_pty_output
TEXT = "héllo wörld"
RAW = TEXT.encode("utf-8")
async def main() -> None:
# One persistent deque, exactly as _UnixPtyProcessEntry.output_chunks is reused
# across pty_exec_start and each pty_write_stdin.
chunks: deque[bytes] = deque()
lock = asyncio.Lock()
notify = asyncio.Event()
done = {"value": False}
async def window() -> bytes:
notify.set()
collected, _ = await collect_pty_output(
output_chunks=chunks,
output_lock=lock,
output_notify=notify,
is_done=lambda: done["value"],
yield_time_ms=1,
max_output_tokens=None,
)
return collected
split = 2 # after "h" and the first byte of the two-byte "e" with acute
chunks.append(RAW[:split])
first = await window() # producer still running
chunks.append(RAW[split:])
done["value"] = True
second = await window()
print("window 1:", first)
print("window 2:", second)
print("joined :", (first + second).decode("utf-8"))
print("matches :", (first + second).decode("utf-8") == TEXT)
asyncio.run(main())
Output on main:
window 1: b'h\xef\xbf\xbd'
window 2: b'\xef\xbf\xbdllo w\xc3\xb6rld'
joined : h\ufffd\ufffdllo wörld
matches : False
The two U+FFFD replacement characters are where a single é should be. The ö later in the string is intact, because it happens to fall inside one window.
Expected behavior
A character split across two collection windows should survive, the same way #4707 made a character split across two SSE chunks survive. The joined output should equal the original text.
I opened #4724 for this and closed it myself, because the shape I chose was wrong rather than incomplete. Recording the reproduction here so the bug is not lost with the PR.
What I learned there, in case it saves someone time. Holding the unfinished bytes back on the shared deque races with entry teardown: _watch_process_exit sets output_closed only after gathering the pump tasks, while _finalize_pty_update pops and terminates the entry as soon as process.returncode is set, so a carried byte can land in a deque that is discarded and be lost entirely, which is worse than the replacement character. Hand-rolling the incremental UTF-8 validation was also a mistake; codecs.getincrementaldecoder("utf-8")("replace") plus len(decoder.getstate()[0]) gives the pending prefix length and rejects malformed prefixes correctly. My read is that a correct fix wants decoder state owned by the PTY entry and flushed on exit, which is per-entry state across five backend wrappers.
I'm a freshman in college, so I would rather leave the design call to you than guess at whether that spread is worth it. Happy to attempt it if you want it.
Please read this first
Describe the bug
collect_pty_outputdecodes each collection window witherrors="replace". PTY output is not collected once, it is collected in repeated windows over one persistent chunk deque:pty_exec_starttakes a window, then everypty_write_stdintakes another from the sameentry.output_chunks.So a multi-byte character whose bytes land either side of a window boundary is decoded as two separate partial sequences, and both halves become U+FFFD. The bytes are destroyed at that first decode, so no later window can reassemble the character. There is no error and no warning; the text is silently wrong.
This is not backend-specific.
collect_pty_outputis the shared helper behind theunix_local,docker,blaxel,daytonaandmodalPTY paths.Debug information
mainat89c02c82, and v0.22.0Repro steps
Output on
main:The two U+FFFD replacement characters are where a single
éshould be. Theölater in the string is intact, because it happens to fall inside one window.Expected behavior
A character split across two collection windows should survive, the same way #4707 made a character split across two SSE chunks survive. The joined output should equal the original text.
I opened #4724 for this and closed it myself, because the shape I chose was wrong rather than incomplete. Recording the reproduction here so the bug is not lost with the PR.
What I learned there, in case it saves someone time. Holding the unfinished bytes back on the shared deque races with entry teardown:
_watch_process_exitsetsoutput_closedonly after gathering the pump tasks, while_finalize_pty_updatepops and terminates the entry as soon asprocess.returncodeis set, so a carried byte can land in a deque that is discarded and be lost entirely, which is worse than the replacement character. Hand-rolling the incremental UTF-8 validation was also a mistake;codecs.getincrementaldecoder("utf-8")("replace")pluslen(decoder.getstate()[0])gives the pending prefix length and rejects malformed prefixes correctly. My read is that a correct fix wants decoder state owned by the PTY entry and flushed on exit, which is per-entry state across five backend wrappers.I'm a freshman in college, so I would rather leave the design call to you than guess at whether that spread is worth it. Happy to attempt it if you want it.