Skip to main content

contextgraph_core/
lib.rs

1//! # contextgraph-core
2//!
3//! A versioned, git-like context store for LLM agent conversations,
4//! implemented in pure Rust. It treats the conversation window as a DAG:
5//! every turn is a content-addressed commit, branches are named pointers,
6//! and any point in history can be checked out, diffed, or rolled back to.
7//!
8//! Contextgraph stores and versions context; it **never** calls a model —
9//! it's the substrate an agent runtime checks context in and out of.
10//!
11//! ## Core concepts
12//!
13//! * **Commit** — an immutable, content-addressed node representing one
14//!   delta to the conversation state (a message, a tool call/result, or a
15//!   compaction event). Identical content always yields the identical id.
16//! * **Branch** — a named, mutable pointer to a commit (exactly like a
17//!   `git` ref).
18//! * **Checkout** — materializes the ordered message list a model would
19//!   see by walking a commit's ancestry and folding deltas (including
20//!   compaction ops) in order.
21//! * **Bisect** — O(log n) binary search along an ancestry path for the
22//!   commit where a caller-supplied predicate flips.
23//!
24//! ## Storage
25//!
26//! Persistence lives behind two traits: [`CommitStore`] (the content-
27//! addressed blob graph) and [`RefStore`] (the branch pointers). The
28//! concrete entry point [`ContextGraph`] combines them; bring your own
29//! `S: CommitStore + RefStore`.
30//!
31//! This crate ships two ready-made stores:
32//!
33//! * [`mem::InMemoryGraphStore`] — fast, no dependencies, suitable for
34//!   tests or short-lived processes.
35//! * [`sqlite::SqliteStore`] — SQLite (WAL mode) for durable storage,
36//!   designed for an agent runtime.
37//!
38//! ## Example
39//!
40//! ```
41//! use chrono::Utc;
42//! use contextgraph_core::{
43//!     Author, Commit, ContextGraph, Delta, Metadata,
44//!     commit::{compute_commit_id, CommitId},
45//!     graph::CheckoutTarget,
46//!     mem::InMemoryGraphStore,
47//! };
48//!
49//! # async fn run() -> Result<(), Box<dyn std::error::Error>> {
50//! let store = InMemoryGraphStore::new();
51//! let graph = ContextGraph::new(store);
52//!
53//! let meta = Metadata::new(Utc::now());
54//! let id = graph
55//!     .commit_advancing_branch("main", Author::User, Delta::Message { content: "hi".into() }, meta)
56//!     .await?;
57//!
58//! let ctx = graph.checkout(CheckoutTarget::Branch("main".into())).await?;
59//! assert_eq!(ctx.messages.len(), 1);
60//! # Ok(()) }
61//! ```
62
63pub mod bisect;
64pub mod commit;
65pub mod diff;
66pub mod error;
67pub mod gc;
68pub mod graph;
69pub mod log;
70pub mod materialize;
71pub mod mem;
72pub mod merge;
73pub mod sqlite;
74pub mod store;
75
76pub use bisect::{BisectOutcome, BisectResult};
77pub use commit::{Author, Commit, CommitId, Delta, MergeStrategy, Metadata, TokenUsage};
78pub use diff::{ContextDiff, DiffOp};
79pub use error::{GraphError, Result};
80pub use graph::{CheckoutTarget, ContextGraph};
81pub use log::{LogFilter, LogPage};
82pub use materialize::{MaterializedContext, MaterializedMessage};
83pub use store::{CommitStore, RefStore};