Skip to content

Checkpoints and time travel

A checkpointer saves the graph state after every superstep, per thread. With one you get multi-turn memory, interrupts, crash recovery and time travel.

Saver Crate Notes
InMemorySaver warpra In process. Lost on exit.
SqliteSaver warpra-checkpoint-sqlite WAL mode. Schema created on open.
PostgresSaver warpra-checkpoint-postgres Blob per channel version. Migrations through setup().
use std::sync::Arc;
use warpra::*;
let app = g.compile(CompileOpts::new().checkpointer(Arc::new(InMemorySaver::new())))?;

SQLite:

use std::sync::Arc;
use warpra_checkpoint_sqlite::SqliteSaver;
let saver = Arc::new(SqliteSaver::from_path("checkpoints.db").await?); // WAL, schema created
// or SqliteSaver::in_memory().await?
let app = g.compile(CompileOpts::new().checkpointer(saver))?;

Postgres:

use warpra_checkpoint_postgres::PostgresSaver;
let saver = PostgresSaver::connect("postgres://user:pass@localhost/db").await?;
saver.setup().await?; // runs migrations 0..=9 (idempotent)

Give the savers a dedicated database, or a dedicated Postgres schema. Do not share their tables with other tools. To read checkpoints serialized with msgpack by Python clients, see Reading msgpack checkpoints.

A run’s thread comes from its config:

let cfg = RunConfig::thread("demo");
app.invoke(json!({"messages": [{"role": "user", "content": "What is 2 + 40?"}]}), cfg.clone()).await?;
app.invoke(json!({"messages": [{"role": "user", "content": "thanks"}]}), cfg.clone()).await?; // same conversation
let state = app.get_state(&cfg, false).await?; // latest snapshot
println!("next nodes: {:?}", state.next);
let history = app.get_state_history(&cfg, None, None, None).await?; // newest first
println!("checkpoints so far: {}", history.len());

A StateSnapshot has values, next, config, metadata, created_at, parent_config, tasks and interrupts. Its config addresses that exact checkpoint.

get_state_history(cfg, filter, before, limit) takes an optional metadata filter, an optional before config and an optional limit.

Invoke with () as input and a snapshot’s config. The run continues from that checkpoint. Nodes before it are not re-run.

let history = app.get_state_history(&cfg, None, None, None).await?;
let before_c = history.iter().find(|s| s.next == ["c"]).expect("checkpoint before c");
let replayed = app.invoke((), before_c.config.clone()).await?; // only c runs again

Replaying writes a fork checkpoint as a new child of the addressed one. The old branch stays intact and addressable. Replaying from the final checkpoint is a no-op. You can also address a checkpoint with RunConfig::thread("t").with_checkpoint_id(id).

update_state writes new values as if a node had returned them, and creates a new checkpoint:

// fork from the checkpoint before c, pretending node b returned v=100
let fork_cfg = app.update_state(&before_c.config, Some(json!({"v": 100})), Some("b")).await?;
let out = app.invoke((), fork_cfg).await?; // c runs with v = 100

The edge b -> c fires as if b had run. The parent of the new checkpoint is the addressed one, not the head. With None as as_node, Warpra infers the node that ran last. It is an error when that is ambiguous, for example after a fan-out. bulk_update_state applies several supersteps of updates.

All three savers implement delete_thread, plus:

  • copy_thread(source, target): copies checkpoints, parents, metadata and writes, ids preserved.
  • prune(thread_ids, PruneStrategy::KeepLatest): keeps the newest checkpoint per namespace with its writes. PruneStrategy::Delete removes everything of the threads.
  • delete_for_runs(run_ids): removes the checkpoints whose metadata run_id matches.

Next to checkpoints, a BaseStore holds long-term memory across threads: InMemoryStore, SqliteStore and PostgresStore. All three support batch operations, TTL and optional vector search. PostgresStore uses pgvector when it is available. Pass a store with CompileOpts::new().store(..) and read it in a node with ctx.store().

The saver conformance suite runs against InMemorySaver and SqliteSaver. Against a live Postgres, the saver and store conformance suites pass, and three engine-level tests (cleared channels, interrupt and resume, history and time travel) match InMemorySaver at every checkpoint. Subgraphs and Send fan-out have not been run under Postgres.

Warpra is open source under Apache-2.0. Made by Liceron.