# `Spectre.Session`
[🔗](https://github.com/elchemista/spectre/blob/0.4.0/lib/spectre/otp/session.ex#L1)

Conversation-scoped GenServer for running a Spectre agent under supervision.

The session keeps the latest `%Spectre.State{}` in process memory and delegates
each turn to `Spectre.ask/3`. Applications that prefer durable state can
use a `state MyApp.Store` adapter in the agent DSL; sessions restore from that
adapter on start when no explicit state is supplied.

# `option`

```elixir
@type option() ::
  {:agent, module()}
  | {:state, Spectre.State.t() | map() | keyword()}
  | {:opts, keyword()}
  | {:name, GenServer.name()}
  | {:conversation_id, term()}
  | {:idle, timeout() | false | nil}
  | {:shutdown, timeout() | false | nil}
```

# `agent`

```elixir
@spec agent(GenServer.server()) :: module()
```

Returns the Agent module owned by a supervised session.

# `ask`

```elixir
@spec ask(GenServer.server(), Spectre.Input.t() | String.t() | map(), keyword()) ::
  {:ok, Spectre.Result.t()} | {:error, term()}
```

Handles one turn through a supervised session.

    {:ok, result} = Spectre.Session.ask(session, "hello")

# `child_spec`

```elixir
@spec child_spec(keyword()) :: Supervisor.child_spec()
```

Returns a child spec for a supervised session.

    children = [
      {Spectre.Session,
       agent: MyApp.Agents.ProjectAgent,
       name: MyApp.ProjectAgentSession,
       shutdown: :timer.minutes(10)}
    ]

# `execute`

```elixir
@spec execute(GenServer.server(), Spectre.Result.t(), keyword()) ::
  {:ok, Spectre.Result.t()} | {:error, term()}
```

Executes the session's current pending effect and commits its terminal state.

The state embedded in the supplied result is treated as a reference only;
the Session's current state remains authoritative.

# `reset`

```elixir
@spec reset(GenServer.server(), Spectre.State.t() | map() | keyword()) ::
  :ok | {:error, :instance_busy}
```

Replaces the current in-memory state.

    :ok = Spectre.Session.reset(session, %Spectre.State{})

# `resolve_policy`

```elixir
@spec resolve_policy(
  GenServer.server(),
  Spectre.Result.t() | {:awaitable, term()},
  Spectre.Policy.resolution(),
  keyword()
) :: {:ok, Spectre.Result.t()} | {:error, term()}
```

Resolves the session's currently open policy from a trusted host decision.

The session uses its current state rather than trusting a potentially stale
state embedded in the supplied result. `{:awaitable, id}` addresses the
current gate directly and is the preferred form for external approvals.

# `start_link`

```elixir
@spec start_link([option()]) :: GenServer.on_start()
```

Starts a conversation-scoped session process.

    {:ok, pid} = Spectre.Session.start_link(agent: MyApp.Agent)

# `state`

```elixir
@spec state(GenServer.server()) :: Spectre.State.t()
```

Returns the current in-memory Spectre state.

    %Spectre.State{} = Spectre.Session.state(session)

# `turn`

```elixir
@spec turn(GenServer.server(), Spectre.Input.t() | String.t() | map(), keyword()) ::
  {:ok, Spectre.Turn.t()} | {:error, term()}
```

Handles one turn through a supervised session and returns a `%Spectre.Turn{}`.

---

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