Skip to main content

stream_reader

Function stream_reader 

Source
async fn stream_reader<R>(
    reader: R,
    kind: ToolChunkKind,
    tx: Sender<LineEvent>,
    last_activity: Arc<Mutex<Instant>>,
)
where R: AsyncRead + Unpin,
Expand description

Read raw bytes from a child stdio handle and forward chunks to the publisher with three flush triggers:

  1. Newline. As soon as a \n is in the buffer, drain everything up to and including that newline as one chunk. Loop, since a single read can contain multiple complete lines.
  2. Size threshold (FLUSH_BYTES). If the buffer accumulates without a newline (minified JSON output, long progress lines, printf without \n), flush as soon as it exceeds the threshold. Prevents unbounded memory growth and keeps streaming live for newline-free output.
  3. Quiet-window (FLUSH_QUIET_WINDOW). After this much idle time with non-empty pending bytes, flush. Surfaces slow-trickle output (one byte at a time) that would otherwise wait for the next byte to trigger a natural flush.