# `MingaClaudeBridge.LineBuffer`
[🔗](https://github.com/jsmestad/minga-claude-bridge/blob/main/lib/minga_claude_bridge/line_buffer.ex#L1)

Joins Port line fragments into complete lines.

A Port opened with `{:line, max}` delivers each output line as one or more `{:noeol, chunk}` fragments followed by one `{:eol, chunk}` fragment. `push/2` accepts one fragment at a time and returns a complete line when it receives `{:eol, chunk}`. Lines never include the newline.

The buffer enforces a byte cap on a single line (default 33554432 bytes, 32 MiB). When the bytes of the current line exceed the cap, `push/2` returns `{:error, :line_too_long, buffer}` once and drops the fragments it holds. The buffer then discards the remaining fragments of that line up to and including its `{:eol, chunk}`, and starts the next line clean. This keeps the tail of an oversized line from being reported as a separate, truncated line.

The module is pure and holds no payload outside the returned struct.

# `fragment`

```elixir
@type fragment() :: {:eol | :noeol, binary()}
```

One fragment as delivered by a `{:line, max}` Port.

# `phase`

```elixir
@type phase() :: {:collecting, [binary()], non_neg_integer()} | :discarding
```

Phase of the line in progress.

`{:collecting, chunks, size}` holds the reversed fragments of the current line and their total byte size. `:discarding` drops fragments until the end of an oversized line.

# `push_result`

```elixir
@type push_result() ::
  {:line, binary(), t()} | {:more, t()} | {:error, :line_too_long, t()}
```

Result of pushing one fragment.

# `t`

```elixir
@type t() :: %MingaClaudeBridge.LineBuffer{max_bytes: pos_integer(), phase: phase()}
```

# `buffered_bytes`

```elixir
@spec buffered_bytes(t()) :: non_neg_integer()
```

Returns the number of bytes held for the line in progress.

# `new`

```elixir
@spec new(keyword()) :: t()
```

Returns an empty buffer.

Options:

  * `:max_bytes` - the largest line in bytes, excluding the newline. Defaults to 32 MiB.

# `push`

```elixir
@spec push(t(), fragment()) :: push_result()
```

Adds one Port fragment to the buffer.

Returns `{:line, line, buffer}` when the fragment completes a line, `{:more, buffer}` when the line continues, and `{:error, :line_too_long, buffer}` when the line exceeds the cap.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
