# `AgentCall.Thread`
[🔗](https://github.com/gilbertwong96/agent_call/blob/main/lib/agent_call/thread.ex#L1)

The caller-owned thread contract.

A thread is the caller's conversation state: the model-visible messages and
the latest checkpoint, pending pause and result. The library defines the
contract and the record types it reuses; the caller owns the store, loads the
snapshot before a run and appends the records it chooses to persist. The
library never calls `load/1` or `append/2`, and ships no store, process, table
or default adapter.

`load/1` returns the thread's current snapshot. The caller derives it from its
own store; no library code reads that store. `append/2` receives the records
the caller decides to keep: the `:checkpoint`/`:approval_required`/
`:input_required` events, the terminal `Result`, or the `Checkpoint`/`Pending`
directly. The store decides how to index, retain and query them.

`thread_id` is the caller's opaque key; the library generates no id. The run's
`run_id` travels on the `Checkpoint`, `Pending` and `Result`.

A snapshot can carry a `pending` and a `checkpoint` together: the checkpoint is
the last completed tool turn and the pending is the live pause. The caller
decides which one to resume; the library neither loads nor selects a path.

# `snapshot`

```elixir
@type snapshot() :: %{
  messages: [LlmCall.Message.t()],
  checkpoint: AgentCall.Checkpoint.t() | nil,
  pending: AgentCall.Pending.t() | nil,
  result: AgentCall.Result.t() | nil
}
```

The thread's current state.

`messages` is the model-visible context. `checkpoint` is the last completed
tool turn, `pending` is the live pause, and `result` is the last outcome; each
is `nil` when the thread has no such record.

# `thread_id`

```elixir
@type thread_id() :: term()
```

The caller's opaque thread key.

The library generates no thread id and never inspects this value.

# `thread_record`

```elixir
@type thread_record() ::
  AgentCall.Checkpoint.t()
  | AgentCall.Pending.t()
  | AgentCall.Result.t()
  | AgentCall.Event.t()
```

A record the caller may append.

The contract reuses the records a run already produces. The event stream and
the terminal result are separate records: the `:finished` event is not a
`Result`.

The type is named `thread_record` because `record/0` is an Erlang built-in
type and cannot be defined.

# `append`

```elixir
@callback append(thread_id(), thread_record()) :: :ok | {:error, term()}
```

Appends a record to the thread.

The caller chooses what to append and when; the store decides how to index,
retain and query it. A failed append is returned to the caller, which owns the
recovery.

# `load`

```elixir
@callback load(thread_id()) :: {:ok, snapshot()} | {:error, term()}
```

Loads the thread's current snapshot.

The caller derives the snapshot from its own store. `{:error, reason}` leaves
the run's start to the caller.

---

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