Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Heuristic:AGenUI AGenUI Stream Buffer Overflow Protection

From Leeroopedia
Knowledge Sources
Domains Debugging, UI_Framework
Last Updated 2026-10-24 17:21 GMT

Overview

ProtocolStreamExtractor enforces a 10 MB hard cap on its internal data buffer, silently dropping incoming chunks that would exceed the limit to prevent unbounded memory growth from malformed or runaway streams.

Description

The `ProtocolStreamExtractor` accumulates incoming text chunks in an internal `_dataBuffer` before parsing complete JSON events. Without a cap, a malformed stream (e.g. never-closing JSON braces from a misbehaving LLM, or a stuck socket that never sends delimiters) would cause the buffer to grow without bound, eventually causing OOM.

The protection is a simple size check in `appendData()`: if `_dataBuffer.size() + data.size() > MAX_BUFFER_BYTES`, the incoming chunk is dropped and a warning is logged. No error is raised to the caller — the stream continues, but data is lost. This is a deliberate trade-off: dropping data is preferable to crashing the host application.

Usage

Use this heuristic when:

  • You are debugging missing data in a streaming session — the 10 MB cap may have silently dropped chunks
  • You are sending very large protocol payloads and need to understand why some chunks disappear
  • You are designing a custom stream plugin and need to know the buffer constraints

The Insight (Rule of Thumb)

  • Action: Be aware of the 10 MB buffer cap in `ProtocolStreamExtractor::appendData()`.
  • Value: `MAX_BUFFER_BYTES = 10 * 1024 * 1024` (10 MB). Incoming data that would exceed this is dropped, not truncated.
  • Trade-off: Dropping data silently prevents OOM but can cause incomplete rendering of large streams. No error is propagated to the caller.
  • Workaround: For payloads exceeding 10 MB, break them into multiple streaming sessions or increase the constant (requires code change).

Reasoning

From `agenui_protocol_stream_extractor.cpp:11-20`:

static constexpr size_t MAX_BUFFER_BYTES = 10 * 1024 * 1024; // 10 MB

void ProtocolStreamExtractor::appendData(const std::string& data) {
    if (_dataBuffer.size() + data.size() > MAX_BUFFER_BYTES) {
        AGENUI_LOG("buffer overflow, dropping data (buffer=%zu + incoming=%zu > limit=%zu)",
            _dataBuffer.size(), data.size(), MAX_BUFFER_BYTES);
        return;
    }
    _dataBuffer += data;
}

The check is cumulative (`_dataBuffer.size() + data.size()`), not per-chunk. This means a 9 MB buffer will accept a 500 KB chunk but reject a 2 MB chunk. The buffer shrinks as complete events are parsed and consumed (via `substr` in `driveParser()`), so steady-state streaming with reasonable chunk sizes should never hit the limit. The limit is a safety net for pathological cases, not a normal operating constraint.

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment