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
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
//! Integration tests for claw-branch using real BranchEngine + seeded SQLite.

use serde_json::json;
use sqlx::sqlite::{SqliteConnectOptions, SqliteJournalMode, SqlitePoolOptions};
use tempfile::TempDir;
use uuid::Uuid;

use claw_branch::{
    prelude::*, types::EntityType, BranchError, CherryPick, EntitySelection, MergeStrategy,
    Recommendation, SimulationScenario,
};

// ── Helpers ───────────────────────────────────────────────────────────────────

/// Creates a seeded source SQLite database with 10 memory_records, 3 sessions, 5 tool_outputs.
async fn seed_source_db(path: &std::path::Path) -> BranchResult<()> {
    let pool = SqlitePoolOptions::new()
        .max_connections(1)
        .connect_with(
            SqliteConnectOptions::new()
                .filename(path)
                .create_if_missing(true)
                .journal_mode(SqliteJournalMode::Wal),
        )
        .await?;

    sqlx::query(
        "CREATE TABLE IF NOT EXISTS memory_records (
            id TEXT PRIMARY KEY,
            content TEXT,
            metadata TEXT,
            created_at TEXT,
            updated_at TEXT
        )",
    )
    .execute(&pool)
    .await?;

    sqlx::query(
        "CREATE TABLE IF NOT EXISTS sessions (
            id TEXT PRIMARY KEY,
            name TEXT,
            metadata TEXT,
            created_at TEXT,
            updated_at TEXT
        )",
    )
    .execute(&pool)
    .await?;

    sqlx::query(
        "CREATE TABLE IF NOT EXISTS tool_outputs (
            id TEXT PRIMARY KEY,
            tool_name TEXT,
            output TEXT,
            metadata TEXT,
            created_at TEXT,
            updated_at TEXT
        )",
    )
    .execute(&pool)
    .await?;

    for i in 0..10 {
        let id = Uuid::new_v4().to_string();
        sqlx::query(
            "INSERT INTO memory_records (id, content, metadata, created_at, updated_at)
             VALUES (?, ?, ?, datetime('now'), datetime('now'))",
        )
        .bind(&id)
        .bind(format!("memory content {i}"))
        .bind(json!({"idx": i}).to_string())
        .execute(&pool)
        .await?;
    }

    for i in 0..3 {
        let id = Uuid::new_v4().to_string();
        sqlx::query(
            "INSERT INTO sessions (id, name, metadata, created_at, updated_at)
             VALUES (?, ?, ?, datetime('now'), datetime('now'))",
        )
        .bind(&id)
        .bind(format!("session {i}"))
        .bind(json!({"session_idx": i}).to_string())
        .execute(&pool)
        .await?;
    }

    for i in 0..5 {
        let id = Uuid::new_v4().to_string();
        sqlx::query(
            "INSERT INTO tool_outputs (id, tool_name, output, metadata, created_at, updated_at)
             VALUES (?, ?, ?, ?, datetime('now'), datetime('now'))",
        )
        .bind(&id)
        .bind(format!("tool_{i}"))
        .bind(format!("output_{i}"))
        .bind(json!({"tool_idx": i}).to_string())
        .execute(&pool)
        .await?;
    }

    pool.close().await;
    Ok(())
}

/// Boots a BranchEngine backed by a temp directory with a seeded source DB.
async fn make_engine() -> BranchResult<(BranchEngine, TempDir)> {
    let dir = tempfile::tempdir().map_err(BranchError::Io)?;
    let workspace_id = Uuid::new_v4();
    let trunk_db = dir.path().join("source.db");
    seed_source_db(&trunk_db).await?;

    let config = BranchConfig::builder()
        .workspace_id(workspace_id)
        .branches_dir(dir.path().join("branches"))
        .build()
        .map_err(|e: claw_branch::BranchError| e)?;

    let engine = BranchEngine::new(config, &trunk_db).await?;
    Ok((engine, dir))
}

// ── Tests ─────────────────────────────────────────────────────────────────────

#[tokio::test]
async fn engine_creates_trunk() -> BranchResult<()> {
    let (engine, _dir) = make_engine().await?;
    let trunk = engine.trunk().await?;
    assert_eq!(trunk.name, engine.config().trunk_branch_name);
    assert!(trunk.parent_id.is_none());
    Ok(())
}

#[tokio::test]
async fn fork_creates_isolated_copy() -> BranchResult<()> {
    let (engine, _dir) = make_engine().await?;
    let trunk = engine.trunk().await?;
    let feature = engine.fork(trunk.id, "feature/iso", None).await?;
    assert_eq!(feature.parent_id, Some(trunk.id));
    assert_ne!(feature.db_path, trunk.db_path);
    Ok(())
}

#[tokio::test]
async fn fork_invalid_name_is_rejected() -> BranchResult<()> {
    let (engine, _dir) = make_engine().await?;
    let trunk = engine.trunk().await?;
    let result = engine.fork(trunk.id, "has spaces!", None).await;
    assert!(result.is_err(), "invalid name should be rejected");
    Ok(())
}

#[tokio::test]
async fn discard_removes_status() -> BranchResult<()> {
    let (engine, _dir) = make_engine().await?;
    let trunk = engine.trunk().await?;
    let branch = engine.fork(trunk.id, "discard-me", None).await?;
    engine.discard(branch.id).await?;
    let refreshed = engine.get(branch.id).await?;
    assert!(!refreshed.status.is_live());
    Ok(())
}

#[tokio::test]
async fn archive_branch_is_not_live() -> BranchResult<()> {
    let (engine, _dir) = make_engine().await?;
    let trunk = engine.trunk().await?;
    let branch = engine.fork(trunk.id, "archive-me", None).await?;
    engine.archive(branch.id).await?;
    let refreshed = engine.get(branch.id).await?;
    assert!(!refreshed.status.is_live());
    Ok(())
}

#[tokio::test]
async fn list_returns_forks() -> BranchResult<()> {
    let (engine, _dir) = make_engine().await?;
    let trunk = engine.trunk().await?;
    engine.fork(trunk.id, "list-a", None).await?;
    engine.fork(trunk.id, "list-b", None).await?;
    let all = engine.list(None).await?;
    assert!(
        all.len() >= 3,
        "trunk + 2 forks expected, got {}",
        all.len()
    );
    Ok(())
}

#[tokio::test]
async fn diff_identical_branches_has_zero_divergence() -> BranchResult<()> {
    let (engine, _dir) = make_engine().await?;
    let trunk = engine.trunk().await?;
    let feature = engine.fork(trunk.id, "diff-identical", None).await?;
    let diff = engine.diff(trunk.id, feature.id).await?;
    // Newly forked branch is identical to trunk.
    assert_eq!(diff.stats.added, 0);
    assert_eq!(diff.stats.removed, 0);
    assert_eq!(diff.stats.modified, 0);
    Ok(())
}

#[tokio::test]
async fn diff_detects_field_modification() -> BranchResult<()> {
    let (engine, _dir) = make_engine().await?;
    let trunk = engine.trunk().await?;
    let branch = engine.fork(trunk.id, "diff-modified", None).await?;

    // Directly write to the branch database to simulate a field modification.
    let pool = SqlitePoolOptions::new()
        .max_connections(1)
        .connect_with(
            SqliteConnectOptions::new()
                .filename(&branch.db_path)
                .create_if_missing(false)
                .journal_mode(SqliteJournalMode::Wal),
        )
        .await?;
    sqlx::query("UPDATE memory_records SET content = 'modified' WHERE rowid = 1")
        .execute(&pool)
        .await?;
    pool.close().await;

    let diff = engine.diff(trunk.id, branch.id).await?;
    assert!(
        diff.stats.modified > 0,
        "expected at least one modification"
    );
    Ok(())
}

#[tokio::test]
async fn merge_strategy_ours_prefers_source() -> BranchResult<()> {
    let (engine, _dir) = make_engine().await?;
    let trunk = engine.trunk().await?;
    let branch = engine.fork(trunk.id, "merge-ours", None).await?;

    // Modify branch so there is something to merge.
    let pool = SqlitePoolOptions::new()
        .max_connections(1)
        .connect_with(
            SqliteConnectOptions::new()
                .filename(&branch.db_path)
                .create_if_missing(false)
                .journal_mode(SqliteJournalMode::Wal),
        )
        .await?;
    sqlx::query("UPDATE memory_records SET content = 'ours_content' WHERE rowid = 1")
        .execute(&pool)
        .await?;
    pool.close().await;

    let result = engine
        .merge(branch.id, trunk.id, MergeStrategy::Ours)
        .await?;
    // Merge should produce a result without panicking.
    let _ = (result.applied, result.conflicts.len());
    Ok(())
}

#[tokio::test]
async fn merge_preview_returns_report() -> BranchResult<()> {
    let (engine, _dir) = make_engine().await?;
    let trunk = engine.trunk().await?;
    let branch = engine.fork(trunk.id, "preview-test", None).await?;
    let preview = engine.merge_preview(branch.id, trunk.id).await?;
    // Auto-resolved can be 0 for identical branches, conflicts should be 0.
    assert_eq!(preview.conflicts.len(), 0);
    Ok(())
}

#[tokio::test]
async fn selective_commit_all_entities() -> BranchResult<()> {
    let (engine, _dir) = make_engine().await?;
    let trunk = engine.trunk().await?;
    let branch = engine.fork(trunk.id, "commit-all", None).await?;
    let result = engine.commit_to_trunk(branch.id).await?;
    assert_eq!(result.target_branch_id, trunk.id);
    Ok(())
}

#[tokio::test]
async fn selective_commit_specific_fields() -> BranchResult<()> {
    let (engine, _dir) = make_engine().await?;
    let trunk = engine.trunk().await?;
    let source = engine.fork(trunk.id, "commit-fields-src", None).await?;
    let target = engine.fork(trunk.id, "commit-fields-tgt", None).await?;

    use claw_branch::types::EntityType;
    use claw_branch::EntitySelection;

    let cherry = CherryPick {
        source_branch_id: source.id,
        target_branch_id: target.id,
        entity_selections: vec![EntitySelection {
            entity_type: EntityType::MemoryRecord,
            entity_ids: vec![],
            fields: Some(vec!["content".to_string()]),
        }],
        message: Some("field-level commit".to_string()),
    };

    let result = engine.commit(cherry).await?;
    assert_eq!(result.target_branch_id, target.id);
    Ok(())
}

#[tokio::test]
async fn dag_lineage_includes_trunk() -> BranchResult<()> {
    let (engine, _dir) = make_engine().await?;
    let trunk = engine.trunk().await?;
    let child = engine.fork(trunk.id, "lineage-child", None).await?;
    let lineage = engine.lineage(child.id).await?;
    // Lineage should contain at least child itself.
    assert!(!lineage.is_empty());
    Ok(())
}

#[tokio::test]
async fn dag_dot_export_is_nonempty() -> BranchResult<()> {
    let (engine, _dir) = make_engine().await?;
    let dot = engine.dag_dot().await?;
    assert!(
        dot.contains("digraph"),
        "DOT output should start with digraph"
    );
    Ok(())
}

#[tokio::test]
async fn simulation_evaluate_commit_recommendation() -> BranchResult<()> {
    let (engine, _dir) = make_engine().await?;
    let trunk = engine.trunk().await?;

    let scenario = SimulationScenario {
        name: "test-sim".to_string(),
        description: "integration test scenario".to_string(),
        max_ops: Some(10),
        timeout_secs: Some(10),
        seed_data: None,
    };

    let report = engine
        .simulate(trunk.id, scenario, |_pool| async move {
            Ok(json!({"result": "test_passed"}))
        })
        .await?;

    // The agent did nothing, so result should be Discard (identical branch).
    matches!(
        report.recommendation,
        Recommendation::Discard | Recommendation::Commit
    );
    Ok(())
}

#[tokio::test]
async fn metrics_refresh_returns_counts() -> BranchResult<()> {
    let (engine, _dir) = make_engine().await?;
    let trunk = engine.trunk().await?;
    let metrics = engine.metrics(trunk.id).await?;
    assert!(metrics.memory_record_count >= 0);
    assert!(
        metrics.bytes_on_disk > 0,
        "trunk DB should have nonzero size"
    );
    Ok(())
}

#[tokio::test]
async fn workspace_report_counts_branches() -> BranchResult<()> {
    let (engine, _dir) = make_engine().await?;
    let trunk = engine.trunk().await?;
    engine.fork(trunk.id, "report-a", None).await?;
    engine.fork(trunk.id, "report-b", None).await?;
    let report = engine.workspace_report().await?;
    assert!(
        report.branch_count >= 3,
        "trunk + 2 forks, got {}",
        report.branch_count
    );
    Ok(())
}

#[tokio::test]
async fn gc_runs_without_error() -> BranchResult<()> {
    let (engine, _dir) = make_engine().await?;
    let gc_report = engine.gc().await?;
    // On fresh workspace, nothing should be orphaned.
    assert_eq!(gc_report.branches_purged, 0);
    Ok(())
}

#[tokio::test]
async fn get_by_name_returns_branch() -> BranchResult<()> {
    let (engine, _dir) = make_engine().await?;
    let trunk = engine.trunk().await?;
    engine.fork(trunk.id, "named-branch", None).await?;
    let found = engine.get_by_name("named-branch").await?;
    assert_eq!(found.name, "named-branch");
    Ok(())
}

#[tokio::test]
async fn engine_open_reads_existing_workspace() -> BranchResult<()> {
    let dir = tempfile::tempdir().map_err(BranchError::Io)?;
    let workspace_id = Uuid::new_v4();
    let trunk_db = dir.path().join("source.db");
    seed_source_db(&trunk_db).await?;

    let config1 = BranchConfig::builder()
        .workspace_id(workspace_id)
        .branches_dir(dir.path().join("branches"))
        .build()
        .map_err(|e: claw_branch::BranchError| e)?;

    let engine1 = BranchEngine::new(config1, &trunk_db).await?;
    let trunk = engine1.trunk().await?;
    engine1.fork(trunk.id, "persist-check", None).await?;
    drop(engine1);

    let config2 = BranchConfig::builder()
        .workspace_id(workspace_id)
        .branches_dir(dir.path().join("branches"))
        .build()
        .map_err(|e: claw_branch::BranchError| e)?;

    let engine2 = BranchEngine::open(config2).await?;
    let all = engine2.list(None).await?;
    assert!(all.len() >= 2, "should see trunk + persist-check");
    Ok(())
}

#[tokio::test]
async fn fork_then_diff_then_merge_roundtrip() -> BranchResult<()> {
    let (engine, _dir) = make_engine().await?;
    let trunk = engine.trunk().await?;
    let branch = engine.fork(trunk.id, "roundtrip", None).await?;

    // Mutate branch.
    let pool = SqlitePoolOptions::new()
        .max_connections(1)
        .connect_with(
            SqliteConnectOptions::new()
                .filename(&branch.db_path)
                .create_if_missing(false)
                .journal_mode(SqliteJournalMode::Wal),
        )
        .await?;
    sqlx::query("UPDATE memory_records SET content = 'roundtrip_val' WHERE rowid = 2")
        .execute(&pool)
        .await?;
    pool.close().await;

    let diff = engine.diff(trunk.id, branch.id).await?;
    assert!(diff.stats.modified > 0);

    let result = engine
        .merge(branch.id, trunk.id, MergeStrategy::Theirs)
        .await?;
    // Should succeed without error.
    let _ = (result.applied, result.conflicts.len());
    Ok(())
}

#[tokio::test]
async fn simulation_teardown_discards_branch() -> BranchResult<()> {
    use claw_branch::sandbox::environment::SimulationEnvironment;
    use std::sync::Arc;

    let (engine, _dir) = make_engine().await?;
    let trunk = engine.trunk().await?;

    let scenario = SimulationScenario {
        name: "teardown-test".to_string(),
        description: "teardown test".to_string(),
        max_ops: None,
        timeout_secs: None,
        seed_data: None,
    };

    let config_arc = Arc::new(engine.config().clone());
    let mut env = SimulationEnvironment::setup(&trunk, scenario, config_arc, engine.lifecycle())
        .await
        .unwrap_or_else(|_| {
            panic!("SimulationEnvironment::setup failed in teardown test");
        });

    let sandbox_id = env.branch.id;
    env.teardown(engine.lifecycle()).await?;

    let refreshed = engine.get(sandbox_id).await?;
    assert!(
        !refreshed.status.is_live(),
        "sandbox should be discarded after teardown"
    );
    Ok(())
}

#[tokio::test]
async fn compare_branches_matches_diff_result_shape() -> BranchResult<()> {
    let (engine, _dir) = make_engine().await?;
    let trunk = engine.trunk().await?;
    let branch = engine.fork(trunk.id, "compare-shape", None).await?;

    let left = engine.diff(trunk.id, branch.id).await?;
    let right = engine.compare_branches(trunk.id, branch.id).await?;

    assert_eq!(left.stats.total_entities, right.stats.total_entities);
    assert_eq!(left.stats.modified, right.stats.modified);
    Ok(())
}

#[tokio::test]
async fn cherry_pick_api_executes_commit_path() -> BranchResult<()> {
    let (engine, _dir) = make_engine().await?;
    let trunk = engine.trunk().await?;
    let source = engine.fork(trunk.id, "cp-source", None).await?;
    let target = engine.fork(trunk.id, "cp-target", None).await?;

    let result = engine
        .cherry_pick(
            source.id,
            target.id,
            vec![EntitySelection {
                entity_type: EntityType::MemoryRecord,
                entity_ids: vec![],
                fields: Some(vec!["content".to_string()]),
            }],
            Some("integration cherry-pick".to_string()),
        )
        .await?;

    assert_eq!(result.target_branch_id, target.id);
    Ok(())
}

#[tokio::test]
async fn gc_scheduler_start_stop_is_joinable() -> BranchResult<()> {
    let (engine, _dir) = make_engine().await?;

    engine.start_gc_scheduler().await?;
    engine.start_gc_scheduler().await?;
    tokio::time::sleep(std::time::Duration::from_millis(10)).await;
    engine.stop_gc_scheduler().await?;
    engine.shutdown().await?;
    Ok(())
}