termmate icon indicating copy to clipboard operation
termmate copied to clipboard

Agent stdout readers have no maximum JSON/JSONL frame size

Open thiblizz opened this issue 4 months ago • 0 comments

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:

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.

  1. Save this as fake_claude.py in 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)
  1. Make it executable on Linux/macOS. On Windows, create fake_claude.cmd beside it containing @py -3 "%~dp0fake_claude.py" %*.
  2. Set claude_command to the absolute script or wrapper path. Add TERMMATE_OUTPUT_DONE to TermMate's env setting with an absolute output path.
  3. Record the Python plugin_host RSS, 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 bytearray or 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.

thiblizz avatar Jul 26 '26 21:07 thiblizz