contextgraph-core 0.1.0

Versioned, git-like context store for LLM agent conversations
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
//! `ContextGraph`: the embeddable library entry point combining a
//! `CommitStore` + `RefStore` (usually the same backing object) with the
//! guard-clause validation and transaction ordering the spec requires.

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};

/// What to check out: a specific commit, or the current head of a branch.
///
/// ```
/// use contextgraph_core::{CommitId, graph::CheckoutTarget};
/// let by_id: CheckoutTarget = CommitId::from_bytes([0u8; 32]).into();
/// let by_branch = CheckoutTarget::branch("main");
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CheckoutTarget {
    Commit(CommitId),
    Branch(String),
}

impl CheckoutTarget {
    /// Construct from a specific commit id.
    pub fn commit(id: CommitId) -> Self {
        Self::Commit(id)
    }

    /// Construct from a branch name.
    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)
    }
}

/// The embeddable core API: an agent runtime links this directly and calls
/// `commit`/`checkout` inline per turn.
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
    }

    /// Resolves a checkout target to a concrete commit id, without
    /// materializing.
    ///
    /// # Errors
    ///
    /// * [`GraphError::CommitNotFound`] if a commit target does not exist.
    /// * [`GraphError::BranchNotFound`] if a branch target does not exist.
    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())),
        }
    }

    /// Creates a new commit as a child of `parent` (`None` for a root
    /// commit). Content-addressed and idempotent: identical content always
    /// returns the same id without creating a duplicate node. Does not move
    /// any branch pointer — see `commit_advancing_branch` for the combined
    /// op used by normal per-turn agent loops.
    ///
    /// # Errors
    ///
    /// * [`GraphError::ParentNotFound`] if `parent` is `Some` and the
    ///   referenced commit is not in the store.
    /// * [`GraphError::Storage`] if the underlying store fails to persist.
    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
    }

    /// Commits a new turn and advances `branch_name` to point at it in one
    /// step. The commit is durably written before the branch pointer moves,
    /// so no reader can ever observe a branch pointing at a not-yet-persisted
    /// commit.
    /// Commits a new turn and advances `branch_name` to point at it in one
    /// step. The commit is durably written before the branch pointer moves,
    /// so no reader can ever observe a branch pointing at a not-yet-persisted
    /// commit.
    ///
    /// # Errors
    ///
    /// Any error returned by [`commit`](Self::commit) (e.g. parent not
    /// found) or [`store.set_branch`](CommitStore::put).
    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)
    }

    /// Creates a new branch pointer at an existing commit. Fails if the
    /// branch already exists or the commit does not.
    ///
    /// # Errors
    ///
    /// * [`GraphError::CommitNotFound`] if `at_commit_id` is not in the store.
    /// * [`GraphError::BranchAlreadyExists`] if a branch named `name` is
    ///   already defined.
    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
    }

    /// Moves an existing branch pointer to a different (existing) commit.
    ///
    /// # Errors
    ///
    /// * [`GraphError::BranchNotFound`] if no branch named `name` exists.
    /// * [`GraphError::CommitNotFound`] if `to_commit_id` is not in the store.
    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
    }

    /// Materializes the ordered message list a model would see at `target`.
    pub async fn checkout(&self, target: CheckoutTarget) -> Result<MaterializedContext> {
        let head = self.resolve(&target).await?;
        materialize(&self.store, head).await
    }

    /// Explicit speculative-branch creation: "try two continuations from the
    /// same decision point" in one call. Semantically identical to `branch`
    /// (a fork *is* a branch at an existing commit) but exposed under its
    /// own name since it's the primary developer-facing verb for A/B'ing a
    /// decision point — including forking again from an existing fork.
    pub async fn fork(&self, from_commit_id: CommitId, new_branch_name: &str) -> Result<()> {
        self.branch(new_branch_name, from_commit_id).await
    }

    /// Moves `branch_name` backward to `to_commit_id`. Matches git's `reset`
    /// semantics: commits "ahead" of the new position are not deleted — they
    /// remain reachable by hash, or by any other branch still pointing past
    /// them.
    pub async fn rollback(&self, branch_name: &str, to_commit_id: CommitId) -> Result<()> {
        self.move_branch(branch_name, to_commit_id).await
    }

    /// Walks the ancestry of `target` (a commit, or a branch's current
    /// head) newest-first, applying `filter` and then offset/limit
    /// pagination.
    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
    }

    /// Structural diff between two materialized contexts. `from`/`to` can
    /// each independently be a commit or a branch's current head, so this
    /// covers both the commit-pair and branch-pair forms.
    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
    }

    /// Merges `branch_b` into `branch_a`, creating an explicit merge commit
    /// and advancing `branch_a` to it. See `crate::merge` for why this never
    /// silently merges divergent conversation content.
    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
    }

    /// Binary-searches the ancestry path between `good` and `bad` for the
    /// exact commit where `predicate` flips from true to false.
    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
    }

    /// Explicit, opt-in garbage collection: removes every commit not
    /// reachable from any branch. Never runs implicitly.
    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() {
        // Two branches both commit the exact same text on top of the same
        // parent: this must produce one commit id, referenced from both
        // branches (free storage dedupe across forks).
        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();

        // Forking again, from the fork's current head.
        g.fork(child, "fork-2").await.unwrap();
        assert_eq!(g.store().get_branch("fork-2").await.unwrap(), Some(child));
        // The original fork is untouched by the new one.
        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));

        // The commit that was "ahead" is still reachable by hash.
        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(_)));
    }
}