# `MingaEditor.Agent.BufferSync`
[🔗](https://github.com/jsmestad/minga/blob/main/lib/minga_editor/agent/buffer_sync.ex#L1)

Syncs agent conversation messages into a `*Agent*` Buffer.

Converts the session's message list into markdown text and writes
it into the buffer. The buffer provides a vim-navigable view of
the conversation, with tree-sitter markdown highlighting.

# `line_type`

```elixir
@type line_type() :: :text | :code | :tool | :thinking | :usage | :system | :empty
```

Line type for buffer-line-to-message mapping.

# `line_message_index`

```elixir
@spec line_message_index([term()]) :: [{non_neg_integer(), line_type()}]
```

Builds a per-buffer-line index mapping buffer lines to message indices and types.

Returns a list where each element corresponds to a buffer line and contains
`{message_index, line_type}`. The list is indexed by buffer line number.

Prefer reading the cached index from `UIState.cached_line_index` when
available (populated by `sync/3`). This function is the fallback for when
the cache is empty (e.g., before the first sync).

# `message_start_line`

```elixir
@spec message_start_line([term()], non_neg_integer()) :: non_neg_integer() | nil
```

Returns the buffer line number where the given message index starts.

Returns nil if the message index is not found in the current layout.

# `start_buffer`

```elixir
@spec start_buffer() :: pid() | nil
```

Starts the `*Agent*` buffer.

Returns the buffer pid. The buffer is nofile (no save), read-only
(no user edits), unlisted (hidden from buffer picker), and persistent
(survives buffer kill).

# `sync`

```elixir
@spec sync(pid(), [term()], keyword()) :: [{non_neg_integer(), line_type()}]
```

Syncs messages into the agent buffer as markdown text.

Each message is rendered as a markdown section with a header.
The buffer cursor is moved to the end (auto-scroll).

Returns the line-to-message index computed from the synced content,
so the caller can cache it in state for later lookups without
recomputing.

---

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