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

Agent session lifecycle state: session pid, status, approvals.

Domain-only state (status, model, provider, session ID) lives in the
composed `MingaAgent.RuntimeState` struct at `runtime`. Presentation
state (spinner timer, buffer pid, session history) stays on this struct.

Lifecycle monitoring (Process.monitor) is handled exclusively by
`MingaAgent.SessionManager`, which broadcasts `:agent_session_stopped`
events via `Minga.Events`. The Editor subscribes to those events
instead of monitoring session PIDs directly.

UI state (scroll, prompt, focus, search, toasts) lives in `UIState`
on `EditorState.agent_ui`. This module holds only process-aware,
action-heavy fields that manage the agent session.

# `approval`

```elixir
@type approval() :: MingaAgent.ToolApproval.t()
```

Pending tool approval data.

# `status`

```elixir
@type status() :: MingaAgent.RuntimeState.status()
```

Agent status (delegated from RuntimeState).

# `t`

```elixir
@type t() :: %MingaEditor.State.Agent{
  buffer: pid() | nil,
  error: String.t() | nil,
  pending_approval: approval() | nil,
  runtime: MingaAgent.RuntimeState.t(),
  session: pid() | nil,
  session_history: [pid()],
  spinner_timer: {:ok, :timer.tref()} | nil
}
```

Agent session state.

# `all_sessions`

```elixir
@spec all_sessions(t()) :: [pid()]
```

Returns all session pids (active + history), most recent first.

# `busy?`

```elixir
@spec busy?(t()) :: boolean()
```

Returns true if the agent is actively working.

# `clear_pending_approval`

```elixir
@spec clear_pending_approval(t()) :: t()
```

Clears the pending tool approval.

# `clear_session`

```elixir
@spec clear_session(t()) :: t()
```

Clears the session reference and resets status to :idle. Lifecycle monitoring is handled by SessionManager.

# `set_buffer`

```elixir
@spec set_buffer(t(), pid()) :: t()
```

Sets the agent buffer pid.

# `set_error`

```elixir
@spec set_error(t(), String.t()) :: t()
```

Sets the agent into an error state with a message.

# `set_pending_approval`

```elixir
@spec set_pending_approval(t(), approval()) :: t()
```

Sets a pending tool approval.

# `set_session`

```elixir
@spec set_session(t(), pid()) :: t()
```

Stores the session pid and sets status to :idle. Archives the previous session. Lifecycle monitoring is handled by SessionManager.

# `set_status`

```elixir
@spec set_status(t(), status()) :: t()
```

Sets the agent status (delegates to RuntimeState).

# `start_spinner_timer`

```elixir
@spec start_spinner_timer(t()) :: t()
```

Starts the spinner timer if not already running.

# `status`

```elixir
@spec status(t()) :: status()
```

Returns the agent lifecycle status from RuntimeState.

# `stop_spinner_timer`

```elixir
@spec stop_spinner_timer(t()) :: t()
```

Stops the spinner timer if running.

# `switch_session`

```elixir
@spec switch_session(t(), pid()) :: t()
```

Switches to a session from history, moving current to history. Lifecycle monitoring is handled by SessionManager.

---

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