use crate::bisect::{bisect as bisect_commits, BisectOutcome};
use crate::commit::{Author, Commit, CommitId, Delta, MergeStrategy, Metadata};
use crate::diff::{diff as diff_commits, ContextDiff};
use crate::error::{GraphError, Result};
use crate::gc::gc as gc_store;
use crate::log::{log_ancestors, LogFilter, LogPage};
use crate::materialize::{materialize, MaterializedContext};
use crate::merge::merge as merge_branches;
use crate::store::{CommitStore, RefStore};
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CheckoutTarget {
Commit(CommitId),
Branch(String),
}
impl CheckoutTarget {
pub fn commit(id: CommitId) -> Self {
Self::Commit(id)
}
pub fn branch(name: impl Into<String>) -> Self {
Self::Branch(name.into())
}
}
impl From<CommitId> for CheckoutTarget {
fn from(id: CommitId) -> Self {
Self::Commit(id)
}
}
pub struct ContextGraph<S> {
store: S,
}
impl<S: CommitStore + RefStore> ContextGraph<S> {
pub fn new(store: S) -> Self {
Self { store }
}
pub fn store(&self) -> &S {
&self.store
}
pub async fn resolve(&self, target: &CheckoutTarget) -> Result<CommitId> {
match target {
CheckoutTarget::Commit(id) => {
if !self.store.contains(id).await? {
return Err(GraphError::CommitNotFound(*id));
}
Ok(*id)
}
CheckoutTarget::Branch(name) => self
.store
.get_branch(name)
.await?
.ok_or_else(|| GraphError::BranchNotFound(name.clone())),
}
}
pub async fn commit(
&self,
parent: Option<CommitId>,
author: Author,
delta: Delta,
metadata: Metadata,
) -> Result<CommitId> {
if let Some(p) = parent {
if !self.store.contains(&p).await? {
return Err(GraphError::ParentNotFound(p));
}
}
let parent_ids = parent.into_iter().collect::<Vec<_>>();
let commit = Commit::new(parent_ids, author, delta, metadata);
self.store.put(commit).await
}
pub async fn commit_advancing_branch(
&self,
branch_name: &str,
author: Author,
delta: Delta,
metadata: Metadata,
) -> Result<CommitId> {
let parent = self.store.get_branch(branch_name).await?;
let id = self.commit(parent, author, delta, metadata).await?;
self.store.set_branch(branch_name, id).await?;
Ok(id)
}
pub async fn branch(&self, name: &str, at_commit_id: CommitId) -> Result<()> {
if !self.store.contains(&at_commit_id).await? {
return Err(GraphError::CommitNotFound(at_commit_id));
}
if self.store.get_branch(name).await?.is_some() {
return Err(GraphError::BranchAlreadyExists(name.to_string()));
}
self.store.set_branch(name, at_commit_id).await
}
pub async fn move_branch(&self, name: &str, to_commit_id: CommitId) -> Result<()> {
if self.store.get_branch(name).await?.is_none() {
return Err(GraphError::BranchNotFound(name.to_string()));
}
if !self.store.contains(&to_commit_id).await? {
return Err(GraphError::CommitNotFound(to_commit_id));
}
self.store.set_branch(name, to_commit_id).await
}
pub async fn delete_branch(&self, name: &str) -> Result<()> {
self.store.delete_branch(name).await
}
pub async fn list_branches(&self) -> Result<Vec<(String, CommitId)>> {
self.store.list_branches().await
}
pub async fn checkout(&self, target: CheckoutTarget) -> Result<MaterializedContext> {
let head = self.resolve(&target).await?;
materialize(&self.store, head).await
}
pub async fn fork(&self, from_commit_id: CommitId, new_branch_name: &str) -> Result<()> {
self.branch(new_branch_name, from_commit_id).await
}
pub async fn rollback(&self, branch_name: &str, to_commit_id: CommitId) -> Result<()> {
self.move_branch(branch_name, to_commit_id).await
}
pub async fn log(
&self,
target: CheckoutTarget,
filter: &LogFilter,
offset: usize,
limit: usize,
) -> Result<LogPage> {
let head = self.resolve(&target).await?;
log_ancestors(&self.store, head, filter, offset, limit).await
}
pub async fn diff(&self, from: CheckoutTarget, to: CheckoutTarget) -> Result<ContextDiff> {
let from_id = self.resolve(&from).await?;
let to_id = self.resolve(&to).await?;
diff_commits(&self.store, from_id, to_id).await
}
pub async fn merge(
&self,
branch_a: &str,
branch_b: &str,
strategy: MergeStrategy,
) -> Result<CommitId> {
merge_branches(&self.store, branch_a, branch_b, strategy).await
}
pub async fn bisect<F>(
&self,
good: CommitId,
bad: CommitId,
predicate: F,
) -> Result<BisectOutcome>
where
F: FnMut(&MaterializedContext) -> bool,
{
bisect_commits(&self.store, good, bad, predicate).await
}
pub async fn gc(&self) -> Result<Vec<CommitId>> {
gc_store(&self.store).await
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::mem::InMemoryGraphStore;
use chrono::Utc;
fn meta() -> Metadata {
Metadata::new(Utc::now())
}
fn text(s: &str) -> Delta {
Delta::Message {
content: s.to_string(),
}
}
#[tokio::test]
async fn committing_with_no_parent_creates_a_root_commit() {
let g = ContextGraph::new(InMemoryGraphStore::new());
let id = g
.commit(None, Author::User, text("hi"), meta())
.await
.unwrap();
let ctx = g.checkout(CheckoutTarget::commit(id)).await.unwrap();
assert_eq!(ctx.messages.len(), 1);
}
#[tokio::test]
async fn committing_with_nonexistent_parent_fails() {
let g = ContextGraph::new(InMemoryGraphStore::new());
let err = g
.commit(Some(CommitId::from_bytes([1; 32])), Author::User, text("hi"), meta())
.await
.unwrap_err();
assert!(matches!(err, GraphError::ParentNotFound(_)));
}
#[tokio::test]
async fn branching_at_a_nonexistent_commit_fails() {
let g = ContextGraph::new(InMemoryGraphStore::new());
let err = g.branch("main", CommitId::from_bytes([1; 32])).await.unwrap_err();
assert!(matches!(err, GraphError::CommitNotFound(_)));
}
#[tokio::test]
async fn branching_with_an_already_used_name_fails() {
let g = ContextGraph::new(InMemoryGraphStore::new());
let id = g
.commit(None, Author::User, text("hi"), meta())
.await
.unwrap();
g.branch("main", id).await.unwrap();
let err = g.branch("main", id).await.unwrap_err();
assert!(matches!(err, GraphError::BranchAlreadyExists(_)));
}
#[tokio::test]
async fn moving_a_nonexistent_branch_fails() {
let g = ContextGraph::new(InMemoryGraphStore::new());
let id = g
.commit(None, Author::User, text("hi"), meta())
.await
.unwrap();
let err = g.move_branch("nope", id).await.unwrap_err();
assert!(matches!(err, GraphError::BranchNotFound(_)));
}
#[tokio::test]
async fn moving_a_branch_to_a_nonexistent_commit_fails() {
let g = ContextGraph::new(InMemoryGraphStore::new());
let id = g
.commit(None, Author::User, text("hi"), meta())
.await
.unwrap();
g.branch("main", id).await.unwrap();
let err = g.move_branch("main", CommitId::from_bytes([9; 32])).await.unwrap_err();
assert!(matches!(err, GraphError::CommitNotFound(_)));
}
#[tokio::test]
async fn deleting_a_nonexistent_branch_fails() {
let g = ContextGraph::new(InMemoryGraphStore::new());
let err = g.delete_branch("nope").await.unwrap_err();
assert!(matches!(err, GraphError::BranchNotFound(_)));
}
#[tokio::test]
async fn checking_out_a_nonexistent_branch_fails() {
let g = ContextGraph::new(InMemoryGraphStore::new());
let err = g
.checkout(CheckoutTarget::branch("nope"))
.await
.unwrap_err();
assert!(matches!(err, GraphError::BranchNotFound(_)));
}
#[tokio::test]
async fn checking_out_a_nonexistent_commit_fails() {
let g = ContextGraph::new(InMemoryGraphStore::new());
let err = g
.checkout(CheckoutTarget::commit(CommitId::from_bytes([1; 32])))
.await
.unwrap_err();
assert!(matches!(err, GraphError::CommitNotFound(_)));
}
#[tokio::test]
async fn commit_advancing_branch_creates_commit_and_moves_pointer_atomically_in_order() {
let g = ContextGraph::new(InMemoryGraphStore::new());
let id1 = g
.commit_advancing_branch("main", Author::User, text("one"), meta())
.await
.unwrap();
assert_eq!(g.store().get_branch("main").await.unwrap(), Some(id1));
let id2 = g
.commit_advancing_branch("main", Author::Assistant, text("two"), meta())
.await
.unwrap();
assert_eq!(g.store().get_branch("main").await.unwrap(), Some(id2));
let ctx = g.checkout(CheckoutTarget::branch("main")).await.unwrap();
assert_eq!(ctx.messages.len(), 2);
}
#[tokio::test]
async fn checking_out_same_commit_twice_yields_byte_identical_materialized_context() {
let g = ContextGraph::new(InMemoryGraphStore::new());
let root = g
.commit(None, Author::User, text("hi"), meta())
.await
.unwrap();
let child = g
.commit(Some(root), Author::Assistant, text("there"), meta())
.await
.unwrap();
let a = g.checkout(CheckoutTarget::commit(child)).await.unwrap();
let b = g.checkout(CheckoutTarget::commit(child)).await.unwrap();
assert_eq!(a, b);
}
#[tokio::test]
async fn committing_identical_content_via_two_different_parents_paths_still_dedupes() {
let g = ContextGraph::new(InMemoryGraphStore::new());
let root = g
.commit(None, Author::User, text("hi"), meta())
.await
.unwrap();
g.branch("a", root).await.unwrap();
g.branch("b", root).await.unwrap();
let shared_meta = meta();
let via_a = g
.commit(
Some(root),
Author::Assistant,
text("dup"),
shared_meta.clone(),
)
.await
.unwrap();
let via_b = g
.commit(Some(root), Author::Assistant, text("dup"), shared_meta)
.await
.unwrap();
assert_eq!(via_a, via_b);
}
#[tokio::test]
async fn forking_from_an_existing_commit_creates_an_independent_branch() {
let g = ContextGraph::new(InMemoryGraphStore::new());
let root = g
.commit(None, Author::User, text("plan"), meta())
.await
.unwrap();
g.fork(root, "speculative-a").await.unwrap();
assert_eq!(
g.store().get_branch("speculative-a").await.unwrap(),
Some(root)
);
}
#[tokio::test]
async fn forking_from_a_fork_produces_a_third_independent_branch() {
let g = ContextGraph::new(InMemoryGraphStore::new());
let root = g
.commit(None, Author::User, text("plan"), meta())
.await
.unwrap();
g.fork(root, "fork-1").await.unwrap();
let child = g
.commit(Some(root), Author::Assistant, text("step"), meta())
.await
.unwrap();
g.move_branch("fork-1", child).await.unwrap();
g.fork(child, "fork-2").await.unwrap();
assert_eq!(g.store().get_branch("fork-2").await.unwrap(), Some(child));
assert_eq!(g.store().get_branch("fork-1").await.unwrap(), Some(child));
}
#[tokio::test]
async fn forking_from_a_nonexistent_commit_fails() {
let g = ContextGraph::new(InMemoryGraphStore::new());
let err = g.fork(CommitId::from_bytes([1; 32]), "x").await.unwrap_err();
assert!(matches!(err, GraphError::CommitNotFound(_)));
}
#[tokio::test]
async fn rollback_moves_branch_backward_without_deleting_forward_commits() {
let g = ContextGraph::new(InMemoryGraphStore::new());
let root = g
.commit_advancing_branch("main", Author::User, text("one"), meta())
.await
.unwrap();
let ahead = g
.commit_advancing_branch("main", Author::Assistant, text("two"), meta())
.await
.unwrap();
g.rollback("main", root).await.unwrap();
assert_eq!(g.store().get_branch("main").await.unwrap(), Some(root));
assert!(g.store().contains(&ahead).await.unwrap());
let ctx = g.checkout(CheckoutTarget::commit(ahead)).await.unwrap();
assert_eq!(ctx.messages.len(), 2);
}
#[tokio::test]
async fn rollback_of_a_nonexistent_branch_fails() {
let g = ContextGraph::new(InMemoryGraphStore::new());
let root = g
.commit(None, Author::User, text("hi"), meta())
.await
.unwrap();
let err = g.rollback("nope", root).await.unwrap_err();
assert!(matches!(err, GraphError::BranchNotFound(_)));
}
#[tokio::test]
async fn log_over_a_branch_name_resolves_its_current_head_first() {
let g = ContextGraph::new(InMemoryGraphStore::new());
g.commit_advancing_branch("main", Author::User, text("one"), meta())
.await
.unwrap();
let second = g
.commit_advancing_branch("main", Author::Assistant, text("two"), meta())
.await
.unwrap();
let page = g
.log(
CheckoutTarget::branch("main"),
&crate::log::LogFilter::new(),
0,
10,
)
.await
.unwrap();
assert_eq!(page.commits[0].id, second);
assert_eq!(page.total_matched, 2);
}
#[tokio::test]
async fn diff_between_two_branch_heads_resolves_each_branch_first() {
let g = ContextGraph::new(InMemoryGraphStore::new());
let root = g
.commit(None, Author::User, text("root"), meta())
.await
.unwrap();
g.branch("a", root).await.unwrap();
g.branch("b", root).await.unwrap();
let a_head = g
.commit(Some(root), Author::Assistant, text("a-only"), meta())
.await
.unwrap();
g.move_branch("a", a_head).await.unwrap();
let d = g
.diff(CheckoutTarget::branch("a"), CheckoutTarget::branch("b"))
.await
.unwrap();
assert_eq!(d.added().count(), 0);
assert_eq!(d.removed().count(), 1);
}
#[tokio::test]
async fn diffing_against_a_nonexistent_branch_fails() {
let g = ContextGraph::new(InMemoryGraphStore::new());
let root = g
.commit(None, Author::User, text("root"), meta())
.await
.unwrap();
g.branch("a", root).await.unwrap();
let err = g
.diff(CheckoutTarget::branch("a"), CheckoutTarget::branch("nope"))
.await
.unwrap_err();
assert!(matches!(err, GraphError::BranchNotFound(_)));
}
#[tokio::test]
async fn merging_two_branches_through_context_graph_advances_branch_a_only() {
let g = ContextGraph::new(InMemoryGraphStore::new());
let root = g
.commit(None, Author::User, text("root"), meta())
.await
.unwrap();
g.branch("a", root).await.unwrap();
g.branch("b", root).await.unwrap();
let a_head = g
.commit(Some(root), Author::Assistant, text("a-turn"), meta())
.await
.unwrap();
g.move_branch("a", a_head).await.unwrap();
let b_head = g
.commit(Some(root), Author::Assistant, text("b-turn"), meta())
.await
.unwrap();
g.move_branch("b", b_head).await.unwrap();
let merge_id = g
.merge("a", "b", crate::commit::MergeStrategy::RecordOnly)
.await
.unwrap();
assert_eq!(g.store().get_branch("a").await.unwrap(), Some(merge_id));
assert_eq!(g.store().get_branch("b").await.unwrap(), Some(b_head));
}
#[tokio::test]
async fn log_over_a_nonexistent_branch_fails() {
let g = ContextGraph::new(InMemoryGraphStore::new());
let err = g
.log(
CheckoutTarget::branch("nope"),
&crate::log::LogFilter::new(),
0,
10,
)
.await
.unwrap_err();
assert!(matches!(err, GraphError::BranchNotFound(_)));
}
}