Libra is an AI agent–native version control system written in Rust. It partially implements a Git client with full on-disk format compatibility (objects, index, pack, pack-index) while using SQLite for transactional metadata (config, HEAD, refs). It is designed for monorepo/trunk-based development with tiered cloud storage (S3/R2).
The libra code command launches an interactive TUI (with a background web server) for collaborative AI-agent and human-driven development. It also supports web-only and stdio/MCP modes for integration with AI clients like Claude Desktop.
src/
├── main.rs # Binary entry point (tokio runtime)
├── lib.rs # Library root, sync/async exec helpers
├── cli.rs # Clap CLI definition, subcommand dispatch
├── common_utils.rs # Shared utility functions
├── git_protocol.rs # Git protocol helpers
├── lfs_structs.rs # LFS data types (used by command/lfs.rs and protocol/lfs_client.rs)
├── command/ # All subcommand implementations (38 modules)
│ ├── mod.rs # Re-exports, shared helpers (load/save objects, auth)
│ ├── init.rs, clone.rs, add.rs, commit.rs, push.rs, pull.rs, fetch.rs
│ ├── status.rs, log.rs, show.rs, diff.rs, blame.rs, shortlog.rs, describe.rs
│ ├── branch.rs, tag.rs, switch.rs, checkout.rs, merge.rs, rebase.rs, cherry_pick.rs
│ ├── reset.rs, restore.rs, remove.rs, mv.rs, clean.rs, stash.rs, revert.rs
│ ├── reflog.rs, config.rs, remote.rs, worktree.rs, cloud.rs, lfs.rs
│ ├── open.rs, index_pack.rs # Browser open, pack index operations
│ └── code.rs # `libra code` — TUI/Web/MCP entry
├── internal/ # Core logic
│ ├── ai/ # AI Agent Infrastructure
│ │ ├── agent/ # Agent framework, builder, profiles, runtime
│ │ ├── providers/ # LLM backends (gemini, openai, anthropic, deepseek, zhipu, ollama)
│ │ ├── tools/ # Tool registry & handlers (ApplyPatch, Shell, ReadFile, Grep, etc.)
│ │ ├── completion/ # CompletionModel trait, request/response types
│ │ ├── mcp/ # Model Context Protocol server
│ │ ├── session/ # Session state & persistence
│ │ ├── prompt/ # Prompt engineering & templates
│ │ ├── commands/ # Agent command parsing & dispatch
│ │ └── hooks/ # Git hooks integration
│ ├── tui/ # Terminal UI (ratatui + crossterm)
│ ├── model/ # Sea-ORM data models (config, reference, reflog, object_index)
│ ├── protocol/ # Network clients (git, https, lfs, local)
│ ├── db.rs # SQLite database initialization
│ ├── branch.rs, tag.rs, config.rs, head.rs, reflog.rs
│ └── log/ # Log formatting & date parsing
└── utils/ # Shared utilities
├── client_storage.rs # Tiered storage (local + S3/R2 with LRU cache)
├── d1_client.rs # Cloudflare D1 client
├── test.rs # Test helpers (ChangeDirGuard, setup_with_new_libra_in)
└── ... # Path, object, tree, ignore, LFS utilities
tests/
├── command/ # Integration tests (one file per command)
│ ├── mod.rs # Shared test helpers
│ └── init_test.rs, add_test.rs, commit_test.rs, ...
├── objects/ # Object-level tests
├── data/ # Test fixtures (pack files, objects, indices)
├── command_test.rs # Top-level command integration test
├── e2e_mcp_flow.rs # MCP end-to-end tests
├── mcp_integration_test.rs # MCP integration tests
├── ai_agent_test.rs # AI agent tests
├── ai_chat_agent_test.rs # AI chat agent tests
├── ai_dag_tool_loop_test.rs # AI DAG tool loop tests
├── ai_storage_flow_test.rs # AI storage flow tests
├── intent_flow_test.rs # Intent flow tests
├── cloud_storage_backup_test.rs # Cloud storage backup tests
└── storage_r2_test.rs # R2 storage tests
docs/ # Community docs, contributing guide, agent specs
sql/sqlite_20260309_init.sql # SQLite schema bootstrap
template/ # Git hook templates (pre-commit, exclude)
# Format code (requires nightly toolchain)
cargo +nightly fmt --all
# Lint — all warnings must be resolved before committing
cargo clippy --all-targets --all-features -- -D warnings
# Quick compile check
cargo build
cargo check
# Run full test suite
cargo test --all
# Run specific tests
cargo test command::init_test
cargo test add_test
# Run the CLI
cargo run -- <command> # e.g. cargo run -- statusAll PRs must pass these checks:
cargo +nightly fmt --all --check— formattingcargo clippy --all-targets --all-features -- -D warnings— linting (zero warnings)- Redundancy check on
third-party/rust/crates cargo test --all— full test suite
- Rust edition 2024, 4-space indentation
- Naming:
snake_casefor modules/functions,PascalCasefor types/traits,SCREAMING_SNAKE_CASEfor constants - Imports: Grouped as Standard → External → Crate per
rustfmt.toml(group_imports = "StdExternalCrate",imports_granularity = "Crate"); avoid wildcard imports except in tests
- CLI flows: Use
anyhow::Resultfor flexible error propagation - Library code: Use
thiserrorwith domain-specific error enums (e.g.,InitError,GitError) - Command handlers:
execute(args)is the public async entry; may return early without Result for simple CLI feedback - Database operations:
_with_connsuffix for transaction-safe variants acceptingConnectionTrait - Avoid
unwrap()/expect(): Prefer returningResultand propagating errors with?, attaching human-readable context via.context("...")or.with_context(|| format!(...))so end-users see actionable messages instead of panics.unwrap()/expect()are acceptable only in unit/integration tests and where the logic is obviously infallible (e.g., compile-time-known constants) with a brief// INVARIANT:comment. All other code — including program startup and initialization — must handle errors gracefully and return actionable messages. - User-friendly error messages: All errors surfaced to the user must be human-readable and actionable. Avoid exposing raw internal errors; wrap them with context that explains what went wrong, which resource was affected (path, ref, object ID), and how to fix it.
- Command structure: Each command in
src/command/<name>.rswith anArgsstruct (clap derive) andasync fn execute(args) - Extension traits:
TreeExt,CommitExt,BlobExtadd methods to git-internal types - Builder pattern: Used for
AgentBuilder, with validation in builder methods returningResult - Guard pattern (RAII):
ChangeDirGuardfor safe directory changes in tests - Provider pattern: Each AI provider has
mod.rs+client.rs+completion.rs
- Module-level
//!doc comments explaining purpose - Function-level
///with# Arguments,# Returns,# Examplesections where helpful - Architecture notes as block comments (
/* ... */) for complex patterns like_with_conn - Add comments only when control flow is non-obvious (async handling, SQLite migrations)
- Integration tests in
tests/command/mirror real Git workflows; prefer these for new commands - Isolation: Use
tempfile::tempdir()andutils::test::ChangeDirGuardto isolate state - Serial execution: Mark tests
#[serial](fromserial_testcrate) if they mutate shared state - Async tests: Use
#[tokio::test](orflavor = "multi_thread"when needed) - Fixtures: Keep small and local in
tests/data/; reuse helpers fromtests/command/mod.rs - Coverage: Pair new commands/options with at least one end-to-end test plus a focused unit test
Use typed summaries with optional scope:
feat(status): support porcelain v2 (#82)
fix(push): record tracking reflog (#81)
refactor(ai): extract completion trait
test(merge): add three-way merge coverage
docs(readme): update provider table
- All CI checks pass (format, clippy zero-warnings, tests)
- State intent, linked issues, and tests run
- Include repro steps or sample CLI output for user-visible changes
- Keep changes small and cohesive
- Update README/CLI docs when adding flags or altering behavior
| Category | Crates |
|---|---|
| Git internals | git-internal |
| CLI | clap (derive) |
| Async runtime | tokio (multi-thread) |
| Database | sea-orm + sqlx-sqlite |
| HTTP server | axum |
| HTTP client | reqwest (rustls) |
| AI/LLM | rig-core, rmcp (MCP protocol) |
| TUI | ratatui, crossterm |
| Cloud storage | object_store (S3/R2/Azure/GCP) |
| Error handling | anyhow, thiserror |
| Serialization | serde, serde_json |
| Logging | tracing, tracing-subscriber |
| Diff/patch | similar, diffy, diffs |
SQLite database at .libra/libra.db with tables: config, reference, reflog, rebase_state, object_index. Schema bootstrap in sql/sqlite_20260309_init.sql.
| Provider | API Key Env | Base URL Override |
|---|---|---|
gemini |
GEMINI_API_KEY |
— |
openai |
OPENAI_API_KEY |
OPENAI_BASE_URL |
anthropic |
ANTHROPIC_API_KEY |
ANTHROPIC_BASE_URL |
deepseek |
DEEPSEEK_API_KEY |
— |
zhipu |
ZHIPU_API_KEY |
ZHIPU_BASE_URL |
ollama |
— | OLLAMA_BASE_URL or --api-base |
LIBRA_STORAGE_TYPE, LIBRA_STORAGE_BUCKET, LIBRA_STORAGE_ENDPOINT, LIBRA_STORAGE_REGION, LIBRA_STORAGE_ACCESS_KEY, LIBRA_STORAGE_SECRET_KEY, LIBRA_STORAGE_THRESHOLD, LIBRA_STORAGE_CACHE_SIZE, LIBRA_STORAGE_ALLOW_HTTP (set to "true" to permit non-TLS HTTP endpoints, useful for local/dev S3-compatible stores)
LIBRA_D1_ACCOUNT_ID, LIBRA_D1_API_TOKEN, LIBRA_D1_DATABASE_ID