# `Condukt.Sessions`
[🔗](https://github.com/tuist/tuist/blob/condukt@3.0.0/condukt/lib/condukt/sessions.ex#L1)

Starts sessions under supervision and finds them again by id.

Before this existed a session was a bare process held by whoever called
`start_link/1`. That is workable for one caller running one agent, and it is
the wrong shape for anything holding sessions on behalf of other people: with
no registry there is no way to answer "is this session running?", reattach a
reconnecting viewer, count what a tenant is using, or shut a set of them down
in an orderly way.

Sessions started here are registered under their own id, so the id a caller
already has is the only handle it needs.

    {:ok, session} = Condukt.Sessions.start(MyApp.Agent, actor: user_id)
    id = Condukt.Session.id(session)

    Condukt.Sessions.whereis(id)
    Condukt.Sessions.alive?(id)
    Condukt.run(via(id), "hello")

Sessions are `:temporary`: a conversation that has crashed is not silently
restarted underneath the person having it. Restarting is the caller's
decision. With a store that keeps a journal (see `Condukt.SessionStore`), the
turn it died in survives too: start the session again under the same id and
store, and `Condukt.Session.interrupted/1` reports the run, which
`Condukt.Session.resume/2` continues where it stopped.

## Registries

Where ids are registered is an adapter, a `Condukt.Sessions.Registry`. The
default, `Condukt.Sessions.Registry.Local`, sees the local node only.
`Condukt.Sessions.Registry.PG` and `Condukt.Sessions.Registry.Horde` see
every node of a cluster, so `whereis/1`,
`list/0`, `stop/1`, and the session API by id reach a session wherever it
runs. Configure one before `:condukt` starts:

    config :condukt, session_registry: Condukt.Sessions.Registry.PG

## Child sessions and input by id

A session started with `parent: parent_id` is that session's child.
`children/1` lists a session's children wherever the registry sees them,
and `abort/2` reaches them. `submit/3` delivers input to a session by id at
most once, whether it runs on this node, another one, or nowhere yet, and
tells the sender when the turn it started has settled. A host builds its
own notion of a thread, with its own storage and tools, on these; see the
[Child Sessions](child-sessions.md) guide.

# `abort`

Aborts a session's running turn, as `Condukt.abort/1` does, and by default
its children's.

Returns `:ok`, or `{:error, :not_found}` when no session runs under `id`.

Options:

  * `:children` - also abort the turns of its children, and theirs,
    wherever the registry sees them (see `children/1`). Defaults to
    `true`. Each child keeps running and takes new input afterwards.

# `alive?`

Whether a session is running under this id.

# `children`

The running children of the session `id`, as `{child_id, pid}` pairs sorted
by id, wherever the registry sees them: on this node with the default
`Condukt.Sessions.Registry.Local`, on every connected node with
`Condukt.Sessions.Registry.PG` or `Condukt.Sessions.Registry.Horde`.

A child is listed from when it starts until it exits, whatever started it
and whether or not `id` itself runs. With `Condukt.Sessions.Registry.Horde`
a child that has just started may take a moment to be listed, as its ids
do.

# `count`

How many sessions are running.

# `drain`

Tells the running session `id` to take its stored input.

`submit/3` does this itself. A host calls it when an input was stored while
the session looked stopped but runs, for example from work its
`Condukt.Sessions.Resumer` scheduled. Returns `:ok`, or
`{:error, :not_found}` when no session runs under `id`.

# `list`

Every running session id the registry sees.

Ordering is the registry's and carries no meaning; sort by id for a
time-ordered list, since `Condukt.SessionID` is time-ordered by construction.

# `start`

Starts a supervised session and registers it under its id.

Takes the same options as an agent's `start_link/1`. When no `:id` is given
one is generated, which is also the name the session is registered under.
Also takes:

  * `:parent` - the id of the session this one is a child of. The link is
    recorded while the child runs; a host that restarts a child passes it
    again.
  * `:inbox` - the `Condukt.Sessions.Inbox` the session takes stored input
    from. Defaults to the configured one.

# `stop`

Stops a session.

Returns `:ok` whether or not one was running, so a caller cleaning up does
not have to race the session's own exit. The running turn is aborted first,
so tool calls parked in it run their `c:Condukt.Tool.cancel/2`, and the
inputs it holds settle as `:aborted`.

Options:

  * `:children` - also stop its children, and theirs, wherever the
    registry sees them (see `children/1`). Defaults to `false`: a child is
    a session of its own, which a host may keep running after its parent's
    session stops.
  * `:drain` - hand the session off instead of ending its work, as a
    deploy does. The turn is not aborted, parked tool calls are not
    cancelled, and the sandbox processes they follow, such as a parked
    `Condukt.Tools.Bash` command, keep running for the node that starts
    the session again. The session there does not get the parked calls
    back yet: that needs a durable turn journal (see the Waiting for
    Events guide). Until then, those commands run until their own
    timeout with nothing reading or stopping them, and their process
    state stays in the sandbox. Defaults to `false`.

# `submit`

Submits `text` to the session `id` as input, at most once per request id.

The session takes the input as a user message that waits behind its running
turn and any queued input, and starts a turn when the session is idle. The
input **settles** when that turn ends, with `{:ok, answer}`,
`{:error, reason}`, or `:aborted` when the turn is aborted or the input is
withdrawn by an abort before it starts. Only the native runtime takes it.

Options:

  * `:request_id` - required. A string that identifies this submission, such
    as the id of the tool call making it. A retry with the same one is
    answered `{:error, :duplicate}` and delivers nothing.
  * `:from` - the id of the session submitting, which `:notify` and
    `:report` answer.
  * `:notify` - a key. When the input settles, the session `:from` receives
    a `Condukt.Sessions.Settled` signal under it, which wakes a tool call
    parked on `{Condukt.Sessions.Settled, key}`.
  * `:report` - when `true`, an outcome other than `:aborted` that no call
    took through `:notify` is submitted to `:from` as input, so the sender
    hears the answer after it stopped waiting, or while it is not running.
  * `:inbox` - the `Condukt.Sessions.Inbox` to store the input in, or `nil`
    for none. Defaults to the configured one.
  * `:resumer` - the `Condukt.Sessions.Resumer` that starts the session when
    the input was stored and no node runs it, or a function of the session
    id. Defaults to the configured one.

Without an inbox the input reaches only a running session, and a request id
is remembered by the session process, for its last 1,000 inputs. With one,
the input is stored first, wherever the session runs, and the inbox
remembers request ids. A session that is not running is then started by the
resumer, and takes the input as it starts.

Returns `:ok` once the input is stored or queued, or `{:error, reason}`:

  * `:duplicate` - the request id was submitted before. Treat it as success.
  * `:not_found` - no inbox, and no session runs under `id`.
  * `{:invalid_option, key}`, or another validation error, for bad
    arguments.

# `via`

The `:via` tuple for a session id.

Accepted anywhere a session is: `Condukt.run/3`, `Condukt.Session.stream/3`,
and the rest of the session API.

# `whereis`

The session's pid, or `nil` when the registry knows no session under that id.

With a cluster-wide registry the pid may be on another node.

---

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