# `Condukt.Session.View`
[🔗](https://github.com/tuist/tuist/blob/condukt@3.0.0/condukt/lib/condukt/session/view.ex#L1)

What a viewer of a session sees: its current state, kept by folding changes.

A viewer that attaches to a session mid-turn, or reconnects, starts from the
session's view and then applies only the changes after it. The view holds:

  * `messages` - the committed history, newest last
  * `run` - the turn in flight, or `nil` when the session is idle. Its
    `items` are the turn's steps in order, starting with its input:
    `%{type: :user, message: message}`,
    `%{type: :assistant, text: text, thinking: thinking}`, or
    `%{type: :tool, id: id, name: name, args: args, status: status, result: result, output: output, subagent: run}`.
    A tool's `status` is `:running` until its result arrives, then `:ok` or
    `:error`. `output` is what the call streamed while it ran, as
    `{:tool_output, tool_call_id, chunk}` events, up to its last 64 KiB. `subagent` holds a foreground sub-agent's own run, folded from
    the `{:subagent, tool_call_id, event}` events of that call. Its `error`
    holds an `{:error, reason}` the turn reported.
  * `error` - why the last finished turn failed, or `nil`
  * `queue` - input waiting to start or join a turn, in the order the session
    takes it, each `%{kind: kind, message: message}` with `kind` one of
    `:prompt` (a caller's `run` or `stream` waiting), `:steer`, `:follow_up`,
    `:input` (submitted by id with `Condukt.Sessions.submit/3`), or `:report`
    (a background sub-agent's answer)
  * `background` - background sub-agents by name, each
    `%{name: name, role: role, session_id: id, call_id: id, status: status, run: run}`.
    `call_id` is the tool call that spawned it, `status` is `:working` or
    `:idle`, and `run` holds its current or last turn, the same shape as the
    session's own run: the message the turn started from, steering that
    joined it, and its steps. It is `nil` before the first turn starts.
  * `agent` - the agent module, model name, thinking level, tool names, and
    runtime module

Nothing in the view carries a secret. User input and tool results, in the
history, the run, the queue, and the changes, pass through the session's
redactors, `Condukt.Secrets` included, as they do on their way to the model.
The configuration leaves out keys, URLs, options, and the system prompt.

## Changes

The session's view process numbers each change. It is linked to the
session and lives as long as it does. `epoch` is drawn when the session
starts, so a restarted session's numbers are not mistaken for the old
one's, and `seq` counts its changes; the view records the position of the
last one it holds.
A change is an event the session published, with tool results redacted, or
one of:

  * `{:run_started, message}` - a turn started from this input
  * `{:input, message}` - steering joined the running turn
  * `{:run_finished, error, messages}` - the turn ended, adding `messages` to
    the history; `error` is why it failed, or `nil`
  * `{:messages, :append | :replace, messages}` - the history changed outside
    a turn ending, such as by compaction or `Condukt.clear/1`
  * `{:queue, items}` - queued input changed
  * `{:background, name, entry}` - a background sub-agent started or changed
    status, or, with `nil`, is gone
  * `{:background_event, name, event}` - a background sub-agent's turn
    changed: `event` is one of its events, or `{:run_started, message}` and
    `{:input, message}` as above

`advance/4` applies a numbered change and tells a viewer when it missed one.
Changes are what a remote viewer needs; the view itself is only sent once.

# `advance`

Applies the change numbered `seq` in `epoch`.

Returns `{:ok, view}` for the next change, `:stale` for one the view already
holds, which a viewer drops, and `:gap` when the viewer missed changes or the
session restarted. On a gap, fetch the view again with
`Condukt.Session.view/1` and keep applying from there.

# `running?`

Whether a turn is in flight.

---

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