Agent stdout readers have no maximum JSON/JSONL frame size
Summary
The stdout readers for all three providers append chunks until a newline is received, without limiting the size of a single JSON/JSONL frame:
buffer += chunk
while b"\n" in buffer:
line_bytes, buffer = buffer.split(b"\n", 1)
Affected readers:
ClaudeCodeAgent._read_messages()CodexAgent._read_messages()PiAgent._read_messages()- Codex session-list reader
- Codex session-info reader
If a CLI emits a truncated or newline-free frame, the buffer grows for as long
as the process continues writing. Since bytes is immutable, each
buffer += chunk also copies the accumulated data. A completed oversized frame
is then decoded, parsed, and may be logged without a size check.
The limit should apply to one encoded frame, not to the total number of frames in a response.
Steps to reproduce
This fixture does not contact a provider.
- Save this as
fake_claude.pyin a temporary directory:
#!/usr/bin/env python3
import os
import sys
import time
block = b"x" * 65536
for _ in range(64): # 4 MiB without a newline
remaining = memoryview(block)
while remaining:
remaining = remaining[os.write(sys.stdout.fileno(), remaining):]
with open(os.environ["TERMMATE_OUTPUT_DONE"], "w", encoding="ascii") as file:
file.write("done")
time.sleep(300)
- Make it executable on Linux/macOS. On Windows, create
fake_claude.cmdbeside it containing@py -3 "%~dp0fake_claude.py" %*. - Set
claude_commandto the absolute script or wrapper path. AddTERMMATE_OUTPUT_DONEto TermMate'senvsetting with an absolute output path. - Record the Python
plugin_hostRSS, start a Claude chat, wait for the marker file, and inspect the same process again.
The incomplete 4 MiB frame remains buffered. Larger finite payloads produce proportional growth; do not use a payload that risks exhausting the machine.
Close the chat and restore the previous settings after the test.
Expected behavior
Each JSON/JSONL reader enforces a documented maximum encoded frame size before decoding, parsing, logging, or queueing it. An oversized frame produces a bounded error and closes the connection without displaying partial content.
Actual behavior
A newline-free frame grows the buffer for as long as the child writes. A large completed frame is also processed without a size limit.
Suggested fix
Use a shared bounded JSONL reader in all five locations. It should:
- accumulate into a
bytearrayor equivalent; - enforce the encoded frame limit as bytes arrive;
- reject an oversized frame without decoding, logging, or truncating it;
- bound normal and error logging;
- close and await the affected subprocess when framing can no longer continue.
Choose a limit that covers the largest valid message for each provider, and make it configurable if the protocols require different limits.
Add tests for frames at the limit and one byte over it, with and without a newline, as well as split reads, malformed input, cancellation, and a large valid provider response.