claw-branch 0.1.2

Fork, simulate, and merge engine for ClawDB agents.
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
//! In-memory DAG representation for branch lineage.

use std::{
    collections::{HashMap, HashSet, VecDeque},
    sync::Arc,
};

use chrono::{DateTime, Utc};
use parking_lot::RwLock;
use petgraph::{
    algo::is_cyclic_directed,
    graph::{DiGraph, NodeIndex},
    Direction,
};
use uuid::Uuid;

use crate::{
    config::BranchConfig,
    error::{BranchError, BranchResult},
};

/// Metadata stored on each directed edge in the branch lineage DAG.
#[derive(Debug, Clone)]
pub struct EdgeMeta {
    /// The timestamp when the child branch was forked from the parent.
    pub forked_at: DateTime<Utc>,
    /// The data-version cursor captured at fork time, if available.
    pub merge_cursor: Option<String>,
}

struct DagInner {
    graph: DiGraph<Uuid, EdgeMeta>,
    node_index: HashMap<Uuid, NodeIndex>,
}

/// Stores branch lineage as a directed acyclic graph (parent → child edges).
///
/// Uses a synchronous `parking_lot::RwLock` so that both async lifecycle code and
/// the sync [`crate::dag::traversal::DagTraversal`] API can access state without
/// blocking the async runtime.
#[derive(Clone)]
pub struct DagGraph {
    inner: Arc<RwLock<DagInner>>,
    /// Workspace configuration that owns this DAG.
    pub config: Arc<BranchConfig>,
}

impl DagGraph {
    /// Creates a new empty DAG graph for the given workspace configuration.
    pub fn new(config: Arc<BranchConfig>) -> Self {
        Self {
            inner: Arc::new(RwLock::new(DagInner {
                graph: DiGraph::new(),
                node_index: HashMap::new(),
            })),
            config,
        }
    }

    /// Ensures a branch node exists in the graph, adding it if absent.
    ///
    /// Returns the underlying petgraph `NodeIndex` for advanced callers.
    pub fn add_node(&self, branch_id: Uuid) -> BranchResult<NodeIndex> {
        let mut inner = self.inner.write();
        if let Some(&idx) = inner.node_index.get(&branch_id) {
            return Ok(idx);
        }
        let idx = inner.graph.add_node(branch_id);
        inner.node_index.insert(branch_id, idx);
        Ok(idx)
    }

    /// Removes a branch node only when it has no children (out-degree 0).
    pub fn remove_node(&self, branch_id: Uuid) -> BranchResult<()> {
        let mut inner = self.inner.write();
        let idx = *inner
            .node_index
            .get(&branch_id)
            .ok_or(BranchError::DagNodeNotFound(branch_id))?;
        let child_count = inner
            .graph
            .neighbors_directed(idx, Direction::Outgoing)
            .count();
        if child_count > 0 {
            return Err(BranchError::SandboxError(format!(
                "cannot remove branch {branch_id}: it still has {child_count} child branch(es)"
            )));
        }
        inner.graph.remove_node(idx);
        inner.node_index.remove(&branch_id);
        Ok(())
    }

    /// Adds a directed parent→child edge, stamping it with the current UTC time.
    ///
    /// Returns [`BranchError::DagCycle`] if the edge would create a cycle.
    pub fn add_edge(&self, parent_id: Uuid, child_id: Uuid) -> BranchResult<()> {
        self.add_edge_meta(
            parent_id,
            child_id,
            EdgeMeta {
                forked_at: Utc::now(),
                merge_cursor: None,
            },
        )
    }

    /// Adds a directed parent→child edge with explicit metadata.
    ///
    /// Used by [`crate::dag::serializer::DagSerializer`] when reconstructing a
    /// persisted DAG with its original timestamps.  Returns
    /// [`BranchError::DagCycle`] if the edge would create a cycle.
    pub fn add_edge_meta(
        &self,
        parent_id: Uuid,
        child_id: Uuid,
        meta: EdgeMeta,
    ) -> BranchResult<()> {
        if parent_id == child_id {
            return Err(BranchError::DagCycle {
                from: parent_id,
                to: child_id,
            });
        }
        let mut inner = self.inner.write();
        let parent_idx = ensure_or_insert(&mut inner, parent_id);
        let child_idx = ensure_or_insert(&mut inner, child_id);

        // Avoid adding duplicate edge.
        if inner.graph.find_edge(parent_idx, child_idx).is_some() {
            return Ok(());
        }

        inner.graph.add_edge(parent_idx, child_idx, meta);

        if is_cyclic_directed(&inner.graph) {
            // Undo the edge we just added.
            if let Some(e) = inner.graph.find_edge(parent_idx, child_idx) {
                inner.graph.remove_edge(e);
            }
            return Err(BranchError::DagCycle {
                from: parent_id,
                to: child_id,
            });
        }
        Ok(())
    }

    /// Returns the direct parent of a branch, if any.
    pub fn parent_of(&self, branch_id: Uuid) -> BranchResult<Option<Uuid>> {
        let inner = self.inner.read();
        let idx = *inner
            .node_index
            .get(&branch_id)
            .ok_or(BranchError::DagNodeNotFound(branch_id))?;
        let parent = inner
            .graph
            .neighbors_directed(idx, Direction::Incoming)
            .next()
            .map(|n| inner.graph[n]);
        Ok(parent)
    }

    /// Returns the direct children of a branch.
    pub fn children_of(&self, branch_id: Uuid) -> BranchResult<Vec<Uuid>> {
        let inner = self.inner.read();
        let idx = *inner
            .node_index
            .get(&branch_id)
            .ok_or(BranchError::DagNodeNotFound(branch_id))?;
        Ok(inner
            .graph
            .neighbors_directed(idx, Direction::Outgoing)
            .map(|n| inner.graph[n])
            .collect())
    }

    /// Returns all ancestors of a branch, walking upward to the root (closest first).
    pub fn ancestors_of(&self, branch_id: Uuid) -> BranchResult<Vec<Uuid>> {
        let inner = self.inner.read();
        let start = *inner
            .node_index
            .get(&branch_id)
            .ok_or(BranchError::DagNodeNotFound(branch_id))?;
        let mut output = Vec::new();
        let mut stack = vec![start];
        let mut seen = HashSet::new();
        while let Some(node) = stack.pop() {
            for parent in inner.graph.neighbors_directed(node, Direction::Incoming) {
                if seen.insert(parent) {
                    output.push(inner.graph[parent]);
                    stack.push(parent);
                }
            }
        }
        Ok(output)
    }

    /// Returns the lowest common ancestor of two branches by intersecting their ancestor chains.
    ///
    /// Returns `None` when the branches share no common ancestor (disconnected subgraphs).
    pub fn find_merge_base(&self, a: Uuid, b: Uuid) -> BranchResult<Option<Uuid>> {
        if a == b {
            return Ok(Some(a));
        }
        let a_ancestors = self.ancestors_of(a)?;
        let a_set: HashSet<Uuid> = a_ancestors
            .iter()
            .copied()
            .chain(std::iter::once(a))
            .collect();
        if a_set.contains(&b) {
            return Ok(Some(b));
        }
        for anc in self.ancestors_of(b)? {
            if a_set.contains(&anc) {
                return Ok(Some(anc));
            }
        }
        Ok(None)
    }

    /// Returns `true` if `ancestor_id` is an ancestor (or equal to) `descendant_id`.
    pub fn is_ancestor(&self, ancestor_id: Uuid, descendant_id: Uuid) -> BranchResult<bool> {
        if ancestor_id == descendant_id {
            return Ok(true);
        }
        let ancestors = self.ancestors_of(descendant_id)?;
        Ok(ancestors.contains(&ancestor_id))
    }

    /// Returns the root node (in-degree 0) of the graph, if one exists.
    pub fn root(&self) -> BranchResult<Option<Uuid>> {
        let inner = self.inner.read();
        let root = inner
            .graph
            .node_indices()
            .filter(|&n| {
                inner
                    .graph
                    .neighbors_directed(n, Direction::Incoming)
                    .count()
                    == 0
            })
            .map(|n| inner.graph[n])
            .min(); // deterministic: smallest UUID
        Ok(root)
    }

    /// Returns the total number of branch nodes in the graph.
    pub fn node_count(&self) -> usize {
        self.inner.read().graph.node_count()
    }

    /// Returns the hop distance from the nearest root node to `branch_id`.
    pub fn depth_of(&self, branch_id: Uuid) -> BranchResult<usize> {
        let inner = self.inner.read();
        let target = *inner
            .node_index
            .get(&branch_id)
            .ok_or(BranchError::DagNodeNotFound(branch_id))?;

        // BFS from all root nodes.
        let mut depth_map: HashMap<NodeIndex, usize> = HashMap::new();
        let mut queue: VecDeque<NodeIndex> = VecDeque::new();
        for idx in inner.graph.node_indices() {
            if inner
                .graph
                .neighbors_directed(idx, Direction::Incoming)
                .count()
                == 0
            {
                depth_map.insert(idx, 0);
                queue.push_back(idx);
            }
        }
        while let Some(node) = queue.pop_front() {
            let d = depth_map[&node];
            for child in inner.graph.neighbors_directed(node, Direction::Outgoing) {
                if let std::collections::hash_map::Entry::Vacant(entry) = depth_map.entry(child) {
                    entry.insert(d + 1);
                    queue.push_back(child);
                }
            }
        }
        depth_map
            .get(&target)
            .copied()
            .ok_or(BranchError::DagNodeNotFound(branch_id))
    }

    /// Returns all edges as `(parent_id, child_id, meta)` triples.
    pub fn all_edges(&self) -> Vec<(Uuid, Uuid, EdgeMeta)> {
        let inner = self.inner.read();
        inner
            .graph
            .edge_indices()
            .filter_map(|e| {
                let (src, dst) = inner.graph.edge_endpoints(e)?;
                let meta = inner.graph[e].clone();
                Some((inner.graph[src], inner.graph[dst], meta))
            })
            .collect()
    }

    /// Returns all branch node UUIDs currently registered in the graph.
    pub fn all_nodes(&self) -> Vec<Uuid> {
        let inner = self.inner.read();
        inner.graph.node_weights().copied().collect()
    }

    /// Exposes a read-only snapshot of the inner petgraph `DiGraph` and node index
    /// for use by [`crate::dag::traversal::DagTraversal`].
    pub(crate) fn with_inner<F, T>(&self, f: F) -> T
    where
        F: FnOnce(&DiGraph<Uuid, EdgeMeta>, &HashMap<Uuid, NodeIndex>) -> T,
    {
        let inner = self.inner.read();
        f(&inner.graph, &inner.node_index)
    }
}

/// Inserts a node if absent and returns its index (internal helper, no locking).
fn ensure_or_insert(inner: &mut DagInner, id: Uuid) -> NodeIndex {
    if let Some(&idx) = inner.node_index.get(&id) {
        idx
    } else {
        let idx = inner.graph.add_node(id);
        inner.node_index.insert(id, idx);
        idx
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::path::Path;

    fn test_config() -> Arc<BranchConfig> {
        Arc::new(BranchConfig::default_for_workspace(
            Uuid::new_v4(),
            Path::new("/tmp"),
        ))
    }

    #[test]
    fn test_add_node_idempotent() -> BranchResult<()> {
        let g = DagGraph::new(test_config());
        let id = Uuid::new_v4();
        let idx1 = g.add_node(id)?;
        let idx2 = g.add_node(id)?;
        assert_eq!(idx1, idx2);
        assert_eq!(g.node_count(), 1);
        Ok(())
    }

    #[test]
    fn test_cycle_detection_linear() -> BranchResult<()> {
        let g = DagGraph::new(test_config());
        let a = Uuid::new_v4();
        let b = Uuid::new_v4();
        let c = Uuid::new_v4();
        g.add_edge(a, b)?;
        g.add_edge(b, c)?;
        let result = g.add_edge(c, a);
        assert!(
            matches!(result, Err(BranchError::DagCycle { .. })),
            "c→a should be rejected as it would cycle"
        );
        // Original edges must still be intact.
        assert_eq!(g.children_of(a)?, vec![b]);
        Ok(())
    }

    #[test]
    fn test_self_edge_rejected() -> BranchResult<()> {
        let g = DagGraph::new(test_config());
        let a = Uuid::new_v4();
        let result = g.add_edge(a, a);
        assert!(matches!(result, Err(BranchError::DagCycle { .. })));
        Ok(())
    }

    #[test]
    fn test_depth_linear() -> BranchResult<()> {
        let g = DagGraph::new(test_config());
        let a = Uuid::new_v4();
        let b = Uuid::new_v4();
        let c = Uuid::new_v4();
        g.add_edge(a, b)?;
        g.add_edge(b, c)?;
        assert_eq!(g.depth_of(a)?, 0);
        assert_eq!(g.depth_of(b)?, 1);
        assert_eq!(g.depth_of(c)?, 2);
        Ok(())
    }

    #[test]
    fn test_remove_node_with_children_fails() -> BranchResult<()> {
        let g = DagGraph::new(test_config());
        let a = Uuid::new_v4();
        let b = Uuid::new_v4();
        g.add_edge(a, b)?;
        let result = g.remove_node(a);
        assert!(result.is_err());
        Ok(())
    }

    #[test]
    fn test_ancestors_of() -> BranchResult<()> {
        let g = DagGraph::new(test_config());
        let a = Uuid::new_v4();
        let b = Uuid::new_v4();
        let c = Uuid::new_v4();
        g.add_edge(a, b)?;
        g.add_edge(b, c)?;
        let ancestors = g.ancestors_of(c)?;
        assert!(ancestors.contains(&a));
        assert!(ancestors.contains(&b));
        assert!(!ancestors.contains(&c));
        Ok(())
    }

    #[test]
    fn test_find_merge_base() -> BranchResult<()> {
        let g = DagGraph::new(test_config());
        let root = Uuid::new_v4();
        let left = Uuid::new_v4();
        let right = Uuid::new_v4();
        g.add_edge(root, left)?;
        g.add_edge(root, right)?;
        assert_eq!(g.find_merge_base(left, right)?, Some(root));
        Ok(())
    }
}