codetree 0.1.2

AI-native version control system for autonomous agents
Documentation
//! Benchmark: codetree native engine vs git CLI on the agent operation loop.
//!
//! Framing: agents drive codetree through library/MCP calls (no subprocess),
//! while agents driving git must shell out to the `git` CLI. This benchmark
//! therefore compares codetree library calls against `git` subprocesses —
//! the costs an agent actually pays with each tool.
//!
//! Run: cargo run --release --example bench_vs_git
//!
//! Sections:
//!   save    — stage + commit after editing 2 of N files (the dominant agent op)
//!   status  — working-tree status
//!   branch  — create an operation branch and switch to it
//!   undo    — revert the last save

use std::fs;
use std::path::Path;
use std::process::Command;
use std::time::Instant;

use codetree::commands::{init_command, start_command, undo_command};
use codetree::vcs::VcsManager;

const N_FILES: usize = 50;
const ROUNDS: usize = 15;
const BRANCH_ROUNDS: usize = 10;
const UNDO_ROUNDS: usize = 10;

fn file_content(round: usize, i: usize) -> String {
    format!("round {round} file {i}\n{}", "x".repeat(1400 + (i * 37) % 200))
}

fn write_round(dir: &Path, round: usize, hot: usize) {
    for i in 0..hot {
        fs::write(dir.join(format!("f{i:03}.txt")), file_content(round, i)).unwrap();
    }
}

fn seed_files(dir: &Path) {
    for i in 0..N_FILES {
        fs::write(dir.join(format!("f{i:03}.txt")), file_content(0, i)).unwrap();
    }
}

fn git(dir: &Path, args: &[&str]) {
    let out = Command::new("git")
        .current_dir(dir)
        // Disable machine-local hooks (e.g. corporate secret scanners) so we
        // measure git's engine, not the local environment.
        .arg("-c").arg("core.hooksPath=/dev/null")
        .args(args)
        .env("GIT_AUTHOR_NAME", "bench")
        .env("GIT_AUTHOR_EMAIL", "bench@example")
        .env("GIT_COMMITTER_NAME", "bench")
        .env("GIT_COMMITTER_EMAIL", "bench@example")
        .output()
        .expect("git not available");
    assert!(
        out.status.success(),
        "git {:?} failed: {}",
        args,
        String::from_utf8_lossy(&out.stderr)
    );
}

async fn bench_codetree(dir: &Path) -> Vec<(&'static str, f64)> {
    init_command(false).await.unwrap();
    let mgr = VcsManager::new(dir.to_path_buf()).unwrap();

    let mut results: Vec<(&'static str, f64)> = Vec::new();

    // save: the full user-facing save command (status + stage + commit + logs)
    let mut total = std::time::Duration::ZERO;
    for round in 1..=ROUNDS {
        write_round(dir, round, 2);
        let t = Instant::now();
        codetree::commands::save_command(vec![format!("save {round}")], false, false, None)
            .await
            .unwrap();
        total += t.elapsed();
    }
    results.push(("save", ms_per_op(total, ROUNDS)));

    // status
    let mut total = std::time::Duration::ZERO;
    for round in ROUNDS..ROUNDS * 2 {
        write_round(dir, round, 2);
        let t = Instant::now();
        let _ = mgr.get_status().await.unwrap();
        total += t.elapsed();
    }
    results.push(("status", ms_per_op(total, ROUNDS)));

    // branch: start an operation with checkout (registers metadata + ops log)
    let mut total = std::time::Duration::ZERO;
    for i in 0..BRANCH_ROUNDS {
        let t = Instant::now();
        start_command(
            format!("bench branch {i}"),
            false, // checkout the new branch
            false,
            false,
            None,
            None,
            None,
            None,
            Some("bench-agent".to_string()),
            false,
        )
        .await
        .unwrap();
        total += t.elapsed();
    }
    results.push(("branch", ms_per_op(total, BRANCH_ROUNDS)));

    // undo: revert last save (includes redo stack bookkeeping)
    let mut total = std::time::Duration::ZERO;
    for round in 0..UNDO_ROUNDS {
        write_round(dir, 100 + round, 2);
        mgr.add_all().await.unwrap();
        mgr.commit(&format!("pre-undo {round}")).await.unwrap();
        let t = Instant::now();
        undo_command(None, false).await.unwrap();
        total += t.elapsed();
    }
    results.push(("undo", ms_per_op(total, UNDO_ROUNDS)));

    results
}

fn bench_git(dir: &Path) -> Vec<(&'static str, f64)> {
    git(dir, &["init", "-q"]);
    git(dir, &["checkout", "-q", "-b", "main"]);
    seed_files(dir);
    git(dir, &["add", "-A"]);
    git(dir, &["commit", "-q", "-m", "initial"]);

    let mut results: Vec<(&'static str, f64)> = Vec::new();

    let mut total = std::time::Duration::ZERO;
    for round in 1..=ROUNDS {
        write_round(dir, round, 2);
        let t = Instant::now();
        git(dir, &["add", "-A"]);
        git(dir, &["commit", "-q", "-m", &format!("save {round}")]);
        total += t.elapsed();
    }
    results.push(("save", ms_per_op(total, ROUNDS)));

    let mut total = std::time::Duration::ZERO;
    for round in ROUNDS..ROUNDS * 2 {
        write_round(dir, round, 2);
        let t = Instant::now();
        git(dir, &["status", "--porcelain"]);
        total += t.elapsed();
    }
    results.push(("status", ms_per_op(total, ROUNDS)));

    let mut total = std::time::Duration::ZERO;
    for i in 0..BRANCH_ROUNDS {
        let t = Instant::now();
        git(dir, &["checkout", "-q", "-b", &format!("bench{i}")]);
        total += t.elapsed();
    }
    results.push(("branch", ms_per_op(total, BRANCH_ROUNDS)));

    let mut total = std::time::Duration::ZERO;
    for round in 0..UNDO_ROUNDS {
        write_round(dir, 100 + round, 2);
        git(dir, &["add", "-A"]);
        git(dir, &["commit", "-q", "-m", &format!("pre-undo {round}")]);
        let t = Instant::now();
        git(dir, &["reset", "-q", "--soft", "HEAD~1"]);
        total += t.elapsed();
    }
    results.push(("undo", ms_per_op(total, UNDO_ROUNDS)));

    results
}

fn ms_per_op(total: std::time::Duration, ops: usize) -> f64 {
    total.as_secs_f64() * 1000.0 / ops as f64
}

#[tokio::main]
async fn main() {
    let tmp = tempfile::TempDir::new().unwrap();
    let ct_dir = tmp.path().join("codetree-repo");
    let git_dir = tmp.path().join("git-repo");
    fs::create_dir_all(&ct_dir).unwrap();
    fs::create_dir_all(&git_dir).unwrap();

    seed_files(&ct_dir);
    let old = std::env::current_dir().unwrap();
    std::env::set_current_dir(&ct_dir).unwrap();
    let ct = bench_codetree(&ct_dir).await;
    std::env::set_current_dir(old).unwrap();

    let has_git = Command::new("git").arg("--version").output().is_ok();
    let gt = if has_git {
        bench_git(&git_dir)
    } else {
        eprintln!("git not found — skipping git comparison");
        Vec::new()
    };

    println!();
    println!(
        "codetree vs git — {N_FILES} files, {} saves, {} branches, {} undos",
        ROUNDS, BRANCH_ROUNDS, UNDO_ROUNDS
    );
    println!("codetree: library calls (MCP-style, no subprocess)");
    println!("git:      CLI subprocess per operation (agent-style)");
    println!();
    println!("{:<10} {:>14} {:>14} {:>10}", "op", "codetree ms/op", "git ms/op", "speedup");
    println!("{}", "-".repeat(52));
    for (op, ct_ms) in &ct {
        let git_ms = gt.iter().find(|(o, _)| o == op).map(|(_, m)| *m);
        match git_ms {
            Some(g) => {
                let speedup = g / ct_ms;
                println!("{:<10} {:>14.2} {:>14.2} {:>9.1}x", op, ct_ms, g, speedup);
            }
            None => println!("{:<10} {:>14.2} {:>14} {:>10}", op, ct_ms, "n/a", ""),
        }
    }
    println!();
    println!("status* measures MCP get_status (status without CLI printing).");
}