Expand description
§contextgraph-core
A versioned, git-like context store for LLM agent conversations, implemented in pure Rust. It treats the conversation window as a DAG: every turn is a content-addressed commit, branches are named pointers, and any point in history can be checked out, diffed, or rolled back to.
Contextgraph stores and versions context; it never calls a model — it’s the substrate an agent runtime checks context in and out of.
§Core concepts
- Commit — an immutable, content-addressed node representing one delta to the conversation state (a message, a tool call/result, or a compaction event). Identical content always yields the identical id.
- Branch — a named, mutable pointer to a commit (exactly like a
gitref). - Checkout — materializes the ordered message list a model would see by walking a commit’s ancestry and folding deltas (including compaction ops) in order.
- Bisect — O(log n) binary search along an ancestry path for the commit where a caller-supplied predicate flips.
§Storage
Persistence lives behind two traits: CommitStore (the content-
addressed blob graph) and RefStore (the branch pointers). The
concrete entry point ContextGraph combines them; bring your own
S: CommitStore + RefStore.
This crate ships two ready-made stores:
mem::InMemoryGraphStore— fast, no dependencies, suitable for tests or short-lived processes.sqlite::SqliteStore— SQLite (WAL mode) for durable storage, designed for an agent runtime.
§Example
use chrono::Utc;
use contextgraph_core::{
Author, Commit, ContextGraph, Delta, Metadata,
commit::{compute_commit_id, CommitId},
graph::CheckoutTarget,
mem::InMemoryGraphStore,
};
let store = InMemoryGraphStore::new();
let graph = ContextGraph::new(store);
let meta = Metadata::new(Utc::now());
let id = graph
.commit_advancing_branch("main", Author::User, Delta::Message { content: "hi".into() }, meta)
.await?;
let ctx = graph.checkout(CheckoutTarget::Branch("main".into())).await?;
assert_eq!(ctx.messages.len(), 1);Re-exports§
pub use bisect::BisectOutcome;pub use bisect::BisectResult;pub use commit::Author;pub use commit::Commit;pub use commit::CommitId;pub use commit::Delta;pub use commit::MergeStrategy;pub use commit::Metadata;pub use commit::TokenUsage;pub use diff::ContextDiff;pub use diff::DiffOp;pub use error::GraphError;pub use error::Result;pub use graph::CheckoutTarget;pub use graph::ContextGraph;pub use log::LogFilter;pub use log::LogPage;pub use materialize::MaterializedContext;pub use materialize::MaterializedMessage;pub use store::CommitStore;pub use store::RefStore;
Modules§
- bisect
bisect: git-bisect-for-agent-runs. Given agoodcommit (predicate holds) and abadcommit (predicate has flipped), finds the exact commit along their ancestry path where the predicate first goes false.- commit
- The commit model: the immutable, content-addressed unit of a contextgraph DAG.
- diff
- Structural diff between two commits’ materialized contexts: which turns
were added, removed, or held in common — never a text diff. Messages are
immutable, content-addressed atoms, so there is no “modify in place”;
a changed turn shows up as a
Removedentry for the old commit paired with anAddedentry for the new one, exactly as a line-level text diff represents an edited line. - error
- gc
gc: explicit, opt-in garbage collection. Never runs as a side effect of normal use — commits are only ever removed when a caller deliberately invokes this.- graph
ContextGraph: the embeddable library entry point combining aCommitStore+RefStore(usually the same backing object) with the guard-clause validation and transaction ordering the spec requires.- log
- History walks:
logover a commit’s ancestry (or a branch’s full history, which is just the ancestry of its head), with metadata-tag filtering and pagination. - materialize
- Materialization: turning a DAG walk into the ordered message list a model would actually see, folding compaction ops as one more delta type rather than a special case bolted onto the walk.
- mem
- In-memory fakes of
CommitStoreandRefStore, for fast unit/property tests and as a reference implementation of the storage contract. - merge
merge: reconciling two branches representing alternative continuations.- sqlite
- SQLite-backed
CommitStore+RefStore, in WAL mode. Commit blobs and metadata are stored as JSON text columns; parent/child edges are indexed in both directions so ancestry walks (log,bisect) don’t need to deserialize blobs just to traverse the graph. - store
- Storage traits.
CommitStoreowns the content-addressed blob graph;RefStoreowns branch pointers. Both are behind traits so unit tests can run against fast in-memory fakes while production code uses SQLite.