Condukt.SessionStore behaviour (Condukt v3.0.0)

Copy Markdown View Source

Behaviour for persisting and restoring Condukt sessions.

What this is, and what it is not

A session store is a snapshot cache: it holds the latest state of one session so the same conversation can be picked up again. It is deliberately not a repository. There is no listing, no querying, no pagination, and no tenancy, and save/2 replaces the whole snapshot rather than appending to it.

That shape suits what it is for. It does not suit a product that holds many people's sessions and needs to ask questions about them, and growing this behaviour until it does would mean rewriting a conversation's entire history on every turn, which costs more the longer the conversation gets.

A system that needs to list, search, or scope sessions should own its own persistence and use Condukt.SessionStore alongside it, or not at all. The pieces to build on are already here: Condukt.SessionID is time-ordered, Condukt.SessionStore.Snapshot is versioned and carries :id and :actor, and Condukt.Notifier reports events as they happen rather than after a save.

The journal

A snapshot is written once per run, so on its own it loses the run a process dies in. A store that also implements the optional journal callbacks, append_journal/2, load_journal/1, and clear_journal/1, makes that run durable without rewriting the history on every step: the session appends one small record per step (the run's prompt, each model response, each tool call's intent before it runs, each tool result) and only rewrites the snapshot when the run ends, after which it clears the journal.

After a restart the session folds the journal into the snapshot it loaded and holds an unfinished run as interrupted, for the caller to continue with Condukt.Session.resume/2. See the Sessions and Persistence guide.

A journaling store must keep records in the order they were appended and return them unchanged. The session serializes its own appends, so a store does not need to handle concurrent writers to one session's journal. save/2 and clear/1 must leave the journal alone, and clear_journal/1 must leave the snapshot alone: the session decides when each is cleared. Records are opaque terms to the store, like snapshots.

A journaling store is the session's durable record, so a session does not start when its snapshot or journal cannot be loaded ({:error, {:snapshot_load_failed, reason}} or {:error, {:journal_load_failed, reason}}) rather than starting over and later saving over what it could not read. A snapshot-only store is a cache: a failed load starts fresh.

Condukt.SessionStore.Conformance holds the tests a store has to pass.

Implementing one

Condukt ships memory and disk-backed stores, and callers can provide their own. Condukt.Session passes :agent_module, :cwd, an optional :id, an optional stable :key, and any configured :session_store_opts to each callback.

A store returns whatever term it was given back from load/1; the session passes it through Condukt.SessionStore.Snapshot.migrate/1, so a store never has to know which snapshot version it is holding.

Summary

Callbacks

Appends journal records after the ones already stored, in order.

Removes every journal record, leaving the snapshot in place.

Returns every journal record in append order, or {:ok, []} when there are none.

Callbacks

append_journal(list, keyword)

(optional)
@callback append_journal(
  [term()],
  keyword()
) :: :ok | {:error, term()}

Appends journal records after the ones already stored, in order.

clear(keyword)

@callback clear(keyword()) :: :ok | {:error, term()}

clear_journal(keyword)

(optional)
@callback clear_journal(keyword()) :: :ok | {:error, term()}

Removes every journal record, leaving the snapshot in place.

load(keyword)

@callback load(keyword()) :: {:ok, term()} | :not_found | {:error, term()}

load_journal(keyword)

(optional)
@callback load_journal(keyword()) :: {:ok, [term()]} | {:error, term()}

Returns every journal record in append order, or {:ok, []} when there are none.

save(term, keyword)

@callback save(
  term(),
  keyword()
) :: :ok | {:error, term()}

Functions

append_journal(store, records, default_opts \\ [])

clear(store, default_opts \\ [])

clear_journal(store, default_opts \\ [])

journal?(module)

Whether a store implements the journal callbacks.

load(store, default_opts \\ [])

load_journal(store, default_opts \\ [])

save(store, snapshot, default_opts \\ [])