org.llm4s.agent.graph.sqlite

Members list

Type members

Classlikes

final class SqliteCheckpointer extends Checkpointer, AutoCloseable

A durable org.llm4s.agent.graph.Checkpointer in one SQLite database file: threads, their checkpoint histories, pending writes, event logs and run claims survive a restart, and several runtimes - in this process or in others on the same host - can share the file, each through a store of its own.

A durable org.llm4s.agent.graph.Checkpointer in one SQLite database file: threads, their checkpoint histories, pending writes, event logs and run claims survive a restart, and several runtimes - in this process or in others on the same host - can share the file, each through a store of its own.

Open it with SqliteCheckpointer.open and close it with close. It meets the Checkpointer contract (and passes CheckpointerContract from llm4s-agent-testkit):

  • '''Atomic, fenced commits.''' Every call that writes is one BEGIN IMMEDIATE transaction: a Commit's checkpoint, pending writes and events land together or not at all, and the claim's token and the thread's version are checked inside the same transaction, so two stores over one file cannot both pass the checks; so is a Commit.fence's claim on another thread.
  • '''Claims expire by clock''', the clock this store was opened with: the clock of the process that grants or renews a claim decides when it lapses. Stores in processes on different hosts sharing one file therefore judge expiry by different clocks, and skew between them changes a claim's effective ttl (see docs/guide/agents/durable-checkpointers.md); fencing keeps writes safe regardless.
  • '''History and retention.''' Every checkpoint a commit supersedes stays in the file, readable with history (newest first, paged through the (thread, position) primary key) and checkpoint, until prune, a commit's retractTurn or deleteThread removes it; each of those is one write transaction too.
  • '''Fencing tokens''' come from one counter for the whole file, stored in it, so they keep increasing across restarts and deleteThread, and are never issued twice.
  • '''Fenced deletes, numbered incarnations.''' deleteThread checks the deleter's token in its own write transaction, as a commit does. It keeps the thread's next event number in the file, so a new thread of the same id numbers its events past the deleted one's, in this process and every other, and a read from inside the deleted log is refused with GraphError.ThreadDeleted - also once a sweep has reclaimed the tombstone, through the file's reclamation mark (design 4.53, sweep).
  • '''Data only.''' Checkpoints, pending writes and events are stored as JSON - the checkpoint through Checkpoint.toJson, which records its format version - and decoded on read, so nothing executable is stored.
  • '''Change notification by polling.''' SQLite cannot tell a connection that another has committed, so this store keeps Checkpointer.awaitEventsAfter's default: a subscription watching it reads the event log once per WatchPolicy.pollInterval, and sees commits made through other stores on the file that long after they land.

The database runs in WAL mode with synchronous = FULL: readers never block the writer, and a commit that returned survives a crash or a power loss. SQLite admits one writer at a time per file; a write that finds another connection writing waits up to SqliteCheckpointerConfig.busyTimeout, then fails with a ProcessingError, changing nothing. WAL needs shared memory between the processes that open the file, so keep the file on a local disk - not on a network file system.

'''Fair writes within a process.''' SQLite does not queue a waiting writer: it sleeps and tries again, and wins only if it wakes while no other connection is writing, so a store beside one that writes back to back can wait out its whole busy timeout although every write is short (#1951). The stores in one JVM that opened the same path therefore take turns through a fair, first-come-first-served lock for the file before they begin a write (and an opener before its migration): each waits for the writes queued ahead of it, up to the busy timeout, and only then for SQLite's lock, which another process may hold - up to the busy timeout again. Between processes SQLite's polling still decides the order, so a process writing steadily can starve another's write; raise busyTimeout there.

One store is one connection, and its calls are serialised: it is safe to call from any number of threads. Every failure of the database itself is a ProcessingError, and so is any call after close.

'''Every call returns within twice the busy timeout''', plus the time its own SQL takes, as the Checkpointer contract requires (#1919). A call waits for this store's earlier calls, and a write then for its turn at the file, at most busyTimeout from the call's start; a write then waits for SQLite's own lock, which another process may hold, at most busyTimeout again. A call that reaches either limit fails with a ProcessingError, changing nothing. A read never waits for a writer in WAL mode, so it waits only for this store's earlier calls. awaitEventsAfter polls, so it returns within its timeout plus that bound. What no limit here covers is the SQL itself once it holds the connection: sqlite-jdbc's native calls cannot be timed out or interrupted, so a disk that stalls inside a write - synchronous = FULL waits for the disk to sync every commit - holds that call, and the store's later calls fail once they have waited busyTimeout for it. Keep the bound well inside ClaimPolicy.ttl - ClaimPolicy.renewEvery: with the default 5 second busy timeout and 30 second ttl, it is.

Attributes

Companion
object
Supertypes
trait AutoCloseable
trait Checkpointer
class Object
trait Matchable
class Any

Attributes

Companion
class
Supertypes
class Object
trait Matchable
class Any
Self type
@Experimental

Registers SqliteCheckpointer for llm4s.agent.checkpointer.kind = sqlite (design 4.54).

Registers SqliteCheckpointer for llm4s.agent.checkpointer.kind = sqlite (design 4.54).

Declared in this module's META-INF/services/org.llm4s.agent.graph.spi.CheckpointerBackend, so depending on llm4s-agent-checkpoint-sqlite is all it takes. It reads llm4s.agent.checkpointer.sqlite, whose defaults and bindings are in this module's reference.conf:

  • path (LLM4S_CHECKPOINTER_SQLITE_PATH): the database file, created if it does not exist (its directory must); required;
  • busyTimeout (5 seconds): SqliteCheckpointerConfig.busyTimeout.

Claims expire by the system clock. A class rather than an object because java.util.ServiceLoader instantiates it through a public no-arg constructor.

Attributes

Companion
object
Supertypes
class Object
trait Matchable
class Any

Attributes

Companion
class
Supertypes
class Object
trait Matchable
class Any
Self type
final case class SqliteCheckpointerConfig

How a SqliteCheckpointer uses its database connection.

How a SqliteCheckpointer uses its database connection.

busyTimeout is how long a write waits for the database's write lock while another connection - another store over the same file, in this process or another - holds it, before the call fails. SQLite allows one writer at a time per file, and every write here is short (one transaction per call), so a wait is brief; a call that still times out returns a ProcessingError and changes nothing. The stores in one process that write the same file take turns, first come first served, and a write waits up to busyTimeout for the writes queued ahead of it, then up to busyTimeout again for SQLite's lock, which another process may hold. A call also waits for the store's own earlier calls - one store is one connection - within the same first busyTimeout, so every call returns within twice busyTimeout plus the time its own SQL takes: the bound the Checkpointer contract asks a store to document (#1919). Keep that well inside ClaimPolicy.ttl - ClaimPolicy.renewEvery. It is rounded up to whole milliseconds, and must be positive and at most Int.MaxValue milliseconds: apply and the with* setters throw IllegalArgumentException otherwise, and SqliteCheckpointerConfig.of returns a ValidationError.

The journal mode and sync level are not configurable: the store always runs in WAL mode with synchronous = FULL, so readers never block the writer and a commit that returned survives a power loss.

Attributes

Companion
object
Supertypes
trait Serializable
trait Product
trait Equals
class Object
trait Matchable
class Any
Show all

Attributes

Companion
class
Supertypes
trait Product
trait Mirror
class Object
trait Matchable
class Any
Self type