diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 000000000..3cf025c12 --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1 @@ +ko-fi: hungpham55178 diff --git a/README.md b/README.md index 7f4e584ef..62454c942 100644 --- a/README.md +++ b/README.md @@ -2,31 +2,149 @@ [![CI](https://github.com/hungpham10/codegraph-rs/actions/workflows/ci.yml/badge.svg)](https://github.com/hungpham10/codegraph-rs/actions/workflows/ci.yml) [![CodSpeed Badge](https://img.shields.io/endpoint?url=https://app.codspeed.io//badge.json)](https://app.codspeed.io//hungpham10/codegraph-rs?utm_source=badge) -[![codecov](https://codecov.io/gh/hungpham10/codegraph-rs/graph/badge.svg?token=PUSFMM0CM8)](https://codecov.io/gh/hungpham10/codegraph-rs) +[![codecov](https://codecov.io/gh/hungpham10/codegraph-rs/graph/badge.svg?token=PUSMFF0CM8)](https://codecov.io/gh/hungpham10/codegraph-rs) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE) -> Local-first code intelligence for AI agents. Built in Rust. +> **Local-first semantic code graph for AI agents** — tree-sitter parsing, global symbol IDs, call chains with control-flow markers, served over MCP. Single **~58 MB** static binary. -CodeGraph parses your codebase with tree‑sitter, builds a semantic graph where each symbol has a global ID and each function a call chain, and serves the graph to AI agents via the Model Context Protocol (MCP). +CodeGraph parses your codebase with tree-sitter, builds a **semantic graph** where every symbol gets a global ID and every function has a **call chain** (markers + callee IDs), stores everything under `.codegraph/` (SQLite by default), and exposes the graph to AI agents — Claude Code, Cursor, Codex CLI, opencode, Hermes, Antigravity — over the Model Context Protocol (MCP). -## Install +Agents that consult the semantic graph instead of grepping the filesystem make **fewer tool calls**, **explore faster**, and **stay within context**. -**Automatic (recommended)** - -- **Linux / macOS**: `curl -fsSL https://raw.githubusercontent.com/hungpham10/codegraph-rs/main/scripts/install.sh | sh` -- **Windows (PowerShell)**: `irm https://raw.githubusercontent.com/hungpham10/codegraph-rs/main/scripts/install.ps1 | iex` +## Why CodeGraph? -*See the full installation guide at* [docs/specs/08-installer.md](docs/specs/08-installer.md). +- **Fewer tool calls** — agents navigate call chains (`codegraph_flow`), not grep +- **Local & fast** — full re-index 139 files in ~190 ms, nothing leaves your machine +- **Works everywhere** — 14 languages, 6 storage backends, 24 MCP tools, one binary +- **Semantic, not syntactic** — symbols have global IDs; edges derived from call chains with markers (`LOOP`, `IF_TRUE`, `RETURN`, …) -## Quick start +## ⚡ Quick Start -```sh +```bash +# 1. Initialize and index your project +cd ~/code/my-project codegraph init + +# 2. Serve to your agent over MCP (stdio) codegraph serve --mcp + +# ... or over Streamable HTTP (for remote/Docker) +codegraph serve --mcp --http --addr 0.0.0.0:8123 +``` + +The agent binds the workspace with `codegraph_init {"path": ...}` and gets tools like `codegraph_search_symbol`, `codegraph_flow`, `codegraph_callers`, `codegraph_impact`, `codegraph_context` — all querying over MCP. + +## 📊 Comparison — Why Not X? + +| Tool | Type | Local-First | Semantic Graph | MCP Native | Multi-Storage | Binary Size | +|------|------|-------------|----------------|------------|---------------|-------------| +| **CodeGraph** | Code graph + MCP | ✅ | ✅ (tree-sitter semgraph) | ✅ Built-in | ✅ 6 backends | ~58 MB | +| Aider RepoMap | Repo map generator | ✅ | ❌ (ctags-based) | ❌ | ❌ | N/A | +| Sourcegraph Cody | Cloud code search | ❌ (self-host) | ✅ (CodeQL) | Via extension | ❌ | N/A | +| Bloop | Code indexer | ✅ | ❌ (search only) | ❌ | ❌ | ~30 MB | +| CodeQL | Semantic analysis | ✅/Cloud | ✅ (QL queries) | ❌ | ❌ | Heavy | +| Kythe | Code graph | ✅ | ✅ | ❌ | ❌ | Complex setup | +| LSP servers | Per-language IDE | ✅ | Per-lang only | ❌ | ❌ | Per-lang | +| ast-grep | Structural search | ✅ | ❌ (pattern match) | ❌ | ❌ | ~10 MB | +| context7 | Docs MCP | ❌ | N/A | ✅ | ❌ | N/A | + +→ [Full comparison with decision matrix](docs/comparison.md) + +## 🎯 Key Features + +- **24 MCP tools** — `search_symbol`, `flow`, `callers`, `callees`, `impact`, `search_flow`, `context`, `references`, `diff`, `sandbox`, `mermaid`, and more +- **14 languages** — TypeScript · TSX · JavaScript · Python · Go · Rust · Java · C · C++ · C# · Ruby · PHP · Scala · Swift · Lua +- **6 storage backends** — SQLite (default), LMDB, Redis, Postgres, MySQL, Memory +- **Semantic search** — opt-in fastembed (BGE-small) for hybrid KNN + keyword search +- **Behavior sandbox** — JIT compile function groups + run against Rhai mocks +- **Full re-index always** — watcher debounces changes, re-indexes completely (simpler, no stale state) + +## 📦 Install + +**Automatic (recommended)** + +```bash +# Linux / macOS +curl -fsSL https://raw.githubusercontent.com/hungpham10/codegraph-rs/main/scripts/install.sh | sh + +# Windows (PowerShell) +irm https://raw.githubusercontent.com/hungpham10/codegraph-rs/main/scripts/install.ps1 | iex ``` -## Documentation +**Other options**: [Homebrew](https://github.com/hungpham10/homebrew-codegraph) • [AUR](https://aur.archlinux.org/packages/codegraph-rs-bin) • [.deb/.rpm](https://github.com/hungpham10/codegraph-rs/releases/latest) • `cargo install --git https://github.com/hungpham10/codegraph-rs codegraph` + +[Full install guide →](docs/specs/08-installer.md) + +## 🔧 Configuration (Essentials) + +```toml +# .codegraph/config.toml +[storage] +type = "sqlite" # or lmdb, redis, postgres, mysql, memory + +[embedding] +# backend = "fastembed" # enable semantic/hybrid search +``` + +[Full config reference →](docs/configuration.md) | [Storage backends →](docs/storage-backends.md) | [Semantic search →](docs/semantic-search.md) + +## 🏗️ Architecture + +``` +files → tree-sitter (rayon) → semgraph (global IDs + chains) + → GraphIndex (2 engines + pluggable storage) + → MCP server (24 tools) → AI Agent +``` + +[Architecture deep-dive →](docs/architecture.md) + +## 📚 Documentation Map + +| Topic | File | +|-------|------| +| Architecture & Pipeline | `docs/architecture.md` | +| Extraction & Languages | `docs/specs/04-extraction.md` | +| Storage & GraphIndex | `docs/specs/03-db-layer.md` | +| MCP Server & Tools | `docs/specs/07-mcp-server.md` | +| CLI & Watcher | `docs/specs/09-cli-watcher.md` | +| Semgraph Model | `docs/specs/02-core-types.md` | +| Installer Details | `docs/specs/08-installer.md` | +| **Full Comparison** | `docs/comparison.md` | +| Configuration Reference | `docs/configuration.md` | +| Storage Backends | `docs/storage-backends.md` | +| Semantic Search | `docs/semantic-search.md` | +| Why Rust (Rewrite Story) | `docs/why-rust.md` | +| Development Guide | `docs/development.md` | + +## 🤝 Contributing + +```bash +cargo build --workspace +cargo test --workspace +cargo clippy --workspace --all-targets -- -D warnings +cargo fmt --all +``` + +See [Development Guide](docs/development.md) for feature flags, per-crate tests, and release process. + +## Sponsors + +You can buy me a coffee by sending me money by MOMO +

+ + MoMo Sponsor + +

+ +Or send to me through +[![ko-fi](https://ko-fi.com)](https://ko-fi.com) + +## License + +MIT. See [LICENSE](LICENSE). + +## Acknowledgments -- Architecture overview: [docs/architecture.md](docs/architecture.md) -- Detailed installation guide: [docs/specs/08-installer.md](docs/specs/08-installer.md) -- Full reference (configuration, CLI, MCP tools) – see the original README for comprehensive information. +- Original TypeScript implementation by [@colbymchenry](https://github.com/colbymchenry) +- `tree-sitter` and all language grammar authors +- `rusqlite`, `notify`, `clap`, `tokio`, `rayon`, `ignore`, `dashmap`, `parking_lot` diff --git a/assets/sponsor/MOMO.JPG b/assets/sponsor/MOMO.JPG new file mode 100644 index 000000000..9b02c1de6 Binary files /dev/null and b/assets/sponsor/MOMO.JPG differ diff --git a/docs/comparison.md b/docs/comparison.md new file mode 100644 index 000000000..3e9fb71cd --- /dev/null +++ b/docs/comparison.md @@ -0,0 +1,237 @@ +# Comparison — CodeGraph vs Alternatives + +This document provides a detailed comparison of CodeGraph with other tools in the code intelligence and AI agent tooling space. + +## Quick Comparison Table + +| Tool | Category | Local-First | Semantic Graph | MCP Native | Multi-Storage | Binary Size | Best For | +|------|----------|-------------|----------------|------------|---------------|-------------|----------| +| **CodeGraph** | Code graph + MCP | ✅ | ✅ (tree-sitter semgraph) | ✅ Built-in | ✅ 6 backends | ~58 MB | Local-first AI agents needing full semantic graph | +| **Aider RepoMap** | Repo map generator | ✅ | ❌ (ctags-based) | ❌ | ❌ | N/A | Aider users wanting quick repo overview | +| **Sourcegraph Cody** | Cloud code search | ❌ (self-host option) | ✅ (CodeQL) | Via extension | ❌ | N/A | Enterprise multi-repo search | +| **Bloop** | Code indexer | ✅ | ❌ (search only) | ❌ | ❌ | ~30 MB | Fast local code search | +| **CodeQL** | Semantic analysis | ✅/Cloud | ✅ (QL queries) | ❌ | ❌ | Heavy | Security auditing, variant analysis | +| **Kythe** | Code graph | ✅ | ✅ | ❌ | ❌ | Complex setup | Large-scale build-integrated graphs | +| **LSP servers** (rust-analyzer, clangd, etc.) | Per-language IDE | ✅ | Per-lang only | ❌ | ❌ | Per-lang | IDE integration per language | +| **ast-grep** | Structural search | ✅ | ❌ (pattern match) | ❌ | ❌ | ~10 MB | AST pattern matching/replace | +| **context7** | Docs MCP | ❌ | N/A | ✅ | ❌ | N/A | API/documentation context | + +--- + +## Detailed Breakdown + +### CodeGraph (This Project) + +**What it is**: A local-first semantic code graph built on tree-sitter that serves AI agents via the Model Context Protocol (MCP). + +**Strengths**: +- **True semantic graph**: Symbols have global IDs; call chains capture control flow (LOOP, IF_TRUE, RETURN, etc.) +- **MCP-native**: 24 tools exposed directly — no wrapper needed +- **Multi-storage**: SQLite (default), LMDB, Redis, Postgres, MySQL, in-memory +- **Single binary**: ~58 MB with all backends + embedding runtime bundled +- **14 languages** with full extraction: TypeScript, TSX, JavaScript, Python, Go, Rust, Java, C, C++, C#, Ruby, PHP, Scala, Swift, Lua +- **Optional semantic search**: fastembed (BGE-small) for hybrid KNN + keyword search +- **Behavior sandbox**: JIT compile functions + run against Rhai mocks +- **Full re-index always**: Simpler, no stale state; 139 files in ~190 ms + +**Trade-offs**: +- No incremental sync (by design — full re-index on change) +- Requires MCP-compatible agent (Claude Code, Cursor, Codex, opencode, Hermes, Antigravity) +- Postgres/MySQL schema applied manually (no auto-migrations) + +--- + +### Aider RepoMap + +**What it is**: A repository map generator integrated into Aider (AI pair programmer). Uses ctags + tree-sitter to create a condensed representation of the codebase for LLM context. + +**Strengths**: +- Tightly integrated with Aider's editing workflow +- Fast, lightweight +- Works with 50+ languages via ctags + +**Weaknesses vs CodeGraph**: +- Not a persistent semantic graph — regenerates per session +- No global symbol IDs or call chains +- No MCP server — only works within Aider +- No storage backends, no semantic search +- Ctags-based (less precise than tree-sitter extraction) + +**When to choose**: You use Aider exclusively and want zero-setup repo context. + +--- + +### Sourcegraph Cody + +**What it is**: Enterprise code search + AI assistant. Uses CodeQL for semantic analysis, offers cloud and self-hosted options. + +**Strengths**: +- Cross-repository search at scale +- CodeQL-powered semantic queries +- Enterprise features (RBAC, audit logs, compliance) +- IDE integrations (VS Code, JetBrains) + +**Weaknesses vs CodeGraph**: +- Cloud-first (self-host is complex) +- Heavy infrastructure (PostgreSQL, Redis, Kafka, etc.) +- Not a local-first single binary +- No native MCP server (uses proprietary protocol) +- Expensive for teams + +**When to choose**: Enterprise needing multi-repo search, compliance, and can invest in infrastructure. + +--- + +### Bloop + +**What it is**: Fast Rust-based code indexer and search tool. Focuses on regex/keyword search with some semantic awareness. + +**Strengths**: +- Very fast indexing and search +- Rust-based, single binary (~30 MB) +- Local-first + +**Weaknesses vs CodeGraph**: +- No semantic graph — no global IDs, no call chains +- No MCP server +- No semantic search (embeddings) +- Limited language extraction depth + +**When to choose**: You only need fast code search, not semantic graph for AI agents. + +--- + +### CodeQL + +**What it is**: Semantic code analysis engine from GitHub. Uses a query language (QL) to find vulnerabilities and patterns. + +**Strengths**: +- Deep semantic analysis via QL +- Industry standard for security research +- Variant analysis (find similar bugs) +- GitHub Advanced Security integration + +**Weaknesses vs CodeGraph**: +- Query-based, not graph-native — you write QL, don't traverse a graph +- Heavy (Java-based, large download) +- No MCP server +- No persistent graph storage for agent queries +- Steep learning curve (QL language) + +**When to choose**: Security auditing, variant analysis, compliance — not for AI agent context. + +--- + +### Kythe + +**What it is**: Google's language-agnostic code graph platform. Extracts facts from builds, stores in a graph. + +**Strengths**: +- Language-agnostic (supports 15+ languages) +- Build-system integrated (Bazel, Gradle, etc.) +- Scales to massive codebases (Google-scale) + +**Weaknesses vs CodeGraph**: +- Complex setup (requires build integration) +- No MCP server +- No single binary — distributed services +- Not designed for local AI agent use +- Steep operational overhead + +**When to choose**: Large org with build infrastructure wanting cross-language code graph. + +--- + +### LSP Servers (rust-analyzer, clangd, pyright, etc.) + +**What it is**: Language Server Protocol implementations per language. Provide IDE-grade semantic analysis. + +**Strengths**: +- Best-in-class per-language semantics +- IDE integration (completion, goto definition, refactor) +- Local-first + +**Weaknesses vs CodeGraph**: +- Per-language only — no cross-language graph +- No unified symbol IDs across languages +- No MCP server (though some bridges exist) +- No persistent graph storage +- Not designed for AI agent consumption + +**When to choose**: IDE development — not for AI agent context. + +--- + +### ast-grep + +**What it is**: Structural search and replace using tree-sitter patterns. Like grep but AST-aware. + +**Strengths**: +- 30+ languages via tree-sitter +- Pattern matching on AST nodes +- Fast, single binary (~10 MB) +- Local-first + +**Weaknesses vs CodeGraph**: +- No persistent graph — ephemeral pattern matching +- No global symbol IDs or call chains +- No MCP server +- No semantic search (embeddings) +- Not a queryable index + +**When to choose**: One-off structural search/replace, codemods — not for persistent AI context. + +--- + +### context7 (Upstash) + +**What it is**: MCP server for documentation and API context. Not a code graph. + +**Strengths**: +- MCP-native +- Good for API/docs lookup + +**Weaknesses vs CodeGraph**: +- No code analysis whatsoever +- Cloud-only +- Different use case entirely + +**When to choose**: Agents need API documentation context, not codebase understanding. + +--- + +## Decision Matrix + +| Your Need | Recommended Tool | +|-----------|------------------| +| Local AI agent + semantic graph + MCP | **CodeGraph** | +| Aider user, quick repo map | Aider RepoMap | +| Enterprise multi-repo search + compliance | Sourcegraph Cody | +| Fast local code search only | Bloop | +| Security auditing, variant analysis | CodeQL | +| Massive monorepo with build integration | Kythe | +| IDE development (completion, refactor) | LSP servers | +| Structural search/replace, codemods | ast-grep | +| API documentation for agents | context7 | + +--- + +## Methodology + +This comparison is based on: +- Public documentation and GitHub repos (as of 2026) +- Feature matrices from project READMEs +- Architecture descriptions (local vs cloud, graph vs search vs pattern-match) +- No hands-on benchmarking — performance claims are from respective projects + +**Missing from this table**: Greptile (cloud PR review), Continue.dev (IDE extension), Cursor indexing (IDE-tied), Glean (enterprise search), semgrep (linting), bito/DeepSource (cloud review). These serve different primary use cases. + +--- + +## See Also + +- [README](../README.md) — Quick start and overview +- [Architecture](architecture.md) — How CodeGraph works internally +- [Configuration](configuration.md) — All config options +- [Storage Backends](storage-backends.md) — SQLite, LMDB, Redis, Postgres, MySQL deep-dive +- [Semantic Search](semantic-search.md) — Embedding setup and hybrid search \ No newline at end of file diff --git a/docs/configuration.md b/docs/configuration.md new file mode 100644 index 000000000..9ff562eff --- /dev/null +++ b/docs/configuration.md @@ -0,0 +1,181 @@ +# Configuration Reference + +Complete reference for `.codegraph/config.toml`. + +## File Location + +``` +.codegraph/ + config.toml # Main configuration + .gitignore # Pre-filled (index never committed) + version # Codegraph version that created the directory +``` + +## Full Configuration Example + +```toml +# Language toggles (all 14 enabled by default) +[languages] +rust = true +go = true +python = true +typescript = true +javascript = true +java = true +c = true +cpp = true +csharp = true +ruby = true +php = true +scala = true +swift = true +lua = true + +# C/C++ header handling +# headers = "auto" # "auto" (default), "c", or "cpp" + +# Walker filters (gitignore syntax) +[walker] +include = ["**/*"] +exclude = [ + ".git/**", + ".codegraph/**", + "target/**", + "node_modules/**", + "*.min.js", + "*.lock" +] + +# Storage backend +[storage] +type = "sqlite" # "sqlite" | "lmdb" | "redis" | "memory" | "postgres" | "mysql" +# DSN override (optional; defaults derived from type) +# dsn = "sqlite:///path/to/db.sqlite" +# dsn = "lmdb:///path/to/db.lmdb" +# dsn = "redis://localhost:6379" +# For postgres/mysql: use dsns (shard list) + repo_id — see Storage Backends doc + +# Semantic search (vector KNN) — OFF by default +[embedding] +# backend = "fastembed" # "hashing"/unset = off +# model = "bge-small-en-v1.5" # 384-dim, default +# cache_dir = "~/.cache/codegraph/embeddings" +# SQLite-only: HNSW ANN via sqlite-vss +# vss_extension = "~/.cache/codegraph/embeddings/vss" +# execution_provider = "coreml" # macOS hardware acceleration +``` + +--- + +## Section Reference + +### `[languages]` + +Enable/disable individual language extractors. All 14 are enabled by default. + +| Key | Language | Tree-sitter Grammar | +|-----|----------|---------------------| +| `rust` | Rust | tree-sitter-rust | +| `go` | Go | tree-sitter-go | +| `python` | Python | tree-sitter-python | +| `typescript` | TypeScript | tree-sitter-typescript | +| `javascript` | JavaScript | tree-sitter-javascript | +| `java` | Java | tree-sitter-java | +| `c` | C | tree-sitter-c | +| `cpp` | C++ | tree-sitter-cpp | +| `csharp` | C# | tree-sitter-c-sharp | +| `ruby` | Ruby | tree-sitter-ruby | +| `php` | PHP | tree-sitter-php | +| `scala` | Scala | tree-sitter-scala | +| `swift` | Swift | tree-sitter-swift | +| `lua` | Lua | tree-sitter-lua | + +**Headers**: The `headers` key controls `.h` file parsing: +- `"auto"` (default): Detect from project — C++ if `.cpp`/`.hpp` present, C if `.c` present, inspect each `.h` for C++ syntax in mixed projects +- `"c"`: Force all `.h` as C +- `"cpp"`: Force all `.h` as C++ + +### `[walker]` + +Controls which files are indexed. Uses `ignore::WalkBuilder` (same syntax as `.gitignore`). + +| Key | Type | Description | +|-----|------|-------------| +| `include` | `Vec` | Patterns to include (default `["**/*"]`) | +| `exclude` | `Vec` | Patterns to exclude (default excludes `.git`, `.codegraph`, `target`, `node_modules`, `*.min.js`, `*.lock`) | + +**Note**: Walker filters apply *before* language detection. Excluded files are never parsed. + +### `[storage]` + +Selects the storage backend for the semantic graph. + +| `type` | Description | Default DSN | Notes | +|--------|-------------|-------------|-------| +| `sqlite` | SQLite WAL mode, single file | `sqlite:///.codegraph/db.sqlite` | Default, recommended for most uses | +| `lmdb` | Memory-mapped KV, directory | `lmdb:///.codegraph/db.lmdb` | Mmap-friendly for large indexes | +| `redis` | Redis backend | **Required** — no sensible default | Needs running Redis server | +| `memory` | Ephemeral in-process | N/A | Nothing persisted; for testing | +| `postgres` | PostgreSQL, sharded by `repo_id` | **Required** via `dsns` | Multi-tenant; see below | +| `mysql` | MySQL, sharded by `repo_id` | **Required** via `dsns` | Multi-tenant; see below | + +**DSN override**: Set `dsn` to override the default for any backend. + +**Postgres/MySQL (multi-tenant, sharded)**: +```toml +[storage] +type = "postgres" # or "mysql" +dsns = [ + "postgres://user:pass@db1:5432/codegraph", + "postgres://user:pass@db2:5432/codegraph", +] +# repo_id is auto-generated by `codegraph init` and written here +# repo_id = 14028493579208694412 +``` + +- Sharding: `shard = repo_id % len(dsns)` +- Schema **not auto-applied** — run SQL files from `sql/postgres/` or `sql/mysql/` manually before indexing +- See `sql/README.md` for full schema and sharding design + +### `[embedding]` + +Enables optional semantic search (vector KNN over symbol embeddings). + +| Key | Type | Default | Description | +|-----|------|---------|-------------| +| `backend` | `String` | unset (off) | `"fastembed"` to enable; `"hashing"` for deterministic fallback | +| `model` | `String` | `"bge-small-en-v1.5"` | ONNX model name (384-dim) | +| `cache_dir` | `String` | `"~/.cache/codegraph/embeddings"` | Global model cache directory | +| `vss_extension` | `String` | unset | SQLite-only: path to sqlite-vss extension for HNSW ANN | +| `execution_provider` | `String` | unset | `"coreml"` for macOS Apple Neural Engine/GPU | + +**Behavior**: +- **Off by default** — no embedding model runs unless `backend = "fastembed"` +- Release binary bundles fastembed (ONNX runtime) — no rebuild needed +- With embeddings enabled, `codegraph_search_symbol` gains `match` modes: `"semantic"` (vector KNN) and `"hybrid"` (RRF merge of substring + semantic) +- Vectors persisted with index — restarts reuse without re-embedding +- **Error on load failure** — if model fails to load, opening index errors out (no silent fallback) + +**Pre-download model** (for offline indexing): +```bash +codegraph embed --model bge-small-en-v1.5 +``` +Requires binary built with `--features fastembed`. + +--- + +## Environment Variable Overrides + +| Variable | Effect | +|----------|--------| +| `CODEGRAPH_CONFIG` | Path to config.toml (default: `.codegraph/config.toml`) | +| `CODEGRAPH_INSTALL_DIR` | Install script target directory (default: `~/.local/bin`) | + +--- + +## Related Docs + +- [Storage Backends](storage-backends.md) — Deep dive on each backend +- [Semantic Search](semantic-search.md) — Embedding setup, models, sqlite-vss, CoreML +- [Architecture](architecture.md) — How config maps to pipeline +- [README](../README.md) — Quick start \ No newline at end of file diff --git a/docs/development.md b/docs/development.md new file mode 100644 index 000000000..8849f11d4 --- /dev/null +++ b/docs/development.md @@ -0,0 +1,277 @@ +# Development Guide + +Building, testing, and contributing to CodeGraph. + +## Prerequisites + +- **Rust stable** ≥ 1.85 (edition 2024 used in `codegraph-graph`) +- `cargo` (from rustup) +- Optional: `clang` for some tree-sitter grammars (usually bundled) + +```bash +# Verify toolchain +rustc --version +cargo --version +``` + +--- + +## Quick Commands + +```bash +# Build everything +cargo build --workspace + +# Build release binary (what users get) +cargo build --release -p codegraph + +# Run all tests +cargo test --workspace + +# Lint (CI gate) +cargo clippy --workspace --all-targets -- -D warnings + +# Format +cargo fmt --all + +# Check all feature combinations +cargo check --workspace --features sqlite +cargo check -p codegraph-graph --features redis +cargo check -p codegraph --features rdbms +cargo check -p codegraph --features fastembed +``` + +--- + +## Crate Overview + +``` +crates/ + codegraph-core/ Error types + semgraph model (Symbol, Chain, CallRecord, markers) + codegraph-extract/ tree-sitter native + 14 LangSpec extractors + 5 hand-written + codegraph-graph/ GraphIndex: registry + 2 engines + pluggable storage + embeddings + codegraph-context/ Markdown/JSON context formatter + codegraph-api/ GraphApi wrapper on SharedGraphIndex (async queries) + codegraph-sboxes/ Behavior sandbox: Cranelift JIT + Rhai mock runtime + codegraph-mcp/ MCP server (rmcp SDK) + 24 tools + session management + codegraph-bench/ Benchmarks (criterion, codspeed, storage comparison) + codegraph/ CLI (init/deinit/embed/serve) + watcher (notify + debounce) +``` + +--- + +## Per-Crate Test Commands + +```bash +# Core model tests +cargo test -p codegraph-core + +# Extraction: 30 tests (10 lib + 16 chains + 2 cpp + 2 extract) +cargo test -p codegraph-extract + +# Graph: 60+ tests (search, storage, ingest, flow, reopen) +cargo test -p codegraph-graph + +# API layer +cargo test -p codegraph-api + +# MCP server + tools +cargo test -p codegraph-mcp + +# Sandbox JIT: control flow + end-to-end traces +cargo test -p codegraph-sboxes + +# Bench pipeline integration +cargo test -p codegraph-bench + +# Installer +cargo test -p codegraph-installer +``` + +--- + +## Feature Flags + +### `codegraph-extract` (language support) + +| Feature | Languages | +|---------|-----------| +| `all-langs` (default) | All 14 | +| `lang-rust` | Rust | +| `lang-go` | Go | +| `lang-python` | Python | +| `lang-typescript` | TypeScript | +| `lang-javascript` | JavaScript | +| `lang-java` | Java | +| `lang-c` | C | +| `lang-cpp` | C++ | +| `lang-csharp` | C# | +| `lang-ruby` | Ruby | +| `lang-php` | PHP | +| `lang-scala` | Scala | +| `lang-swift` | Swift | +| `lang-lua` | Lua | + +```bash +# Test single language +cargo test -p codegraph-extract --features lang-python +``` + +### `codegraph-graph` (storage + features) + +| Feature | Description | Default on `codegraph` | +|---------|-------------|------------------------| +| `sqlite` | SQLite storage | ✅ | +| `lmdb` | LMDB storage | ✅ | +| `redis` | Redis storage (compile verify) | ❌ | +| `postgres` | PostgreSQL storage | via `rdbms` | +| `mysql` | MySQL storage | via `rdbms` | +| `bloom-search` | Bloom filter for chain search | ✅ | +| `fastembed` | ONNX embedding backend | ✅ (via codegraph-api) | +| `apple-accel` | macOS CoreML for ONNX | ❌ (macOS only) | + +### `codegraph` binary + +| Feature | Description | Default | +|---------|-------------|---------| +| `rdbms` | Enable `postgres` + `mysql` | ✅ | +| `fastembed` | Compile `codegraph embed` CLI | ❌ | +| `apple-accel` | macOS CoreML | ❌ | + +### `codegraph-mcp` crate + +| Feature | Description | Default | +|---------|-------------|---------| +| `rdbms` | Enable `postgres` + `mysql` | ❌ | + +--- + +## Important Notes + +### `codegraph-api` enables all `codegraph-graph` features + +```bash +# This does NOT produce a slimmer binary — all storage drivers +# and embedding backend are still compiled in via codegraph-api +cargo build -p codegraph --no-default-features +``` + +To actually reduce binary size, you must build with minimal features on `codegraph-graph` AND avoid depending on `codegraph-api` (not practical for the main binary). + +### `apple-accel` is macOS-only + +```bash +# Works +cargo build --features fastembed,apple-accel --target x86_64-apple-darwin +cargo build --features fastembed,apple-accel --target aarch64-apple-darwin + +# Fails +cargo build --features fastembed,apple-accel --target x86_64-unknown-linux-gnu +``` + +--- + +## Running Benchmarks + +```bash +# Criterion benchmarks (statistical) +cargo bench -p codegraph-bench + +# Single-pass measurement (JSON output) +cargo run -p codegraph-bench -- --json + +# Storage backend comparison +cargo run -p codegraph-bench -- --storage sqlite,lmdb,memory + +# CodSpeed (CI only — see .github/workflows/codspeed.yml) +cargo codspeed build -p codegraph-bench --features codspeed +``` + +--- + +## Release Process + +Handled by CI (`.github/workflows/release.yml`): + +1. Tag pushed: `vX.Y.Z` +2. Builds for all targets: + - `x86_64-unknown-linux-musl` + - `aarch64-unknown-linux-gnu` + - `x86_64-apple-darwin` + - `aarch64-apple-darwin` + - `x86_64-pc-windows-msvc` +3. Signs with cosign (keyless, GitHub OIDC) +4. Attaches `.sig` + `.crt` to release +5. Publishes to Homebrew tap, AUR, .deb/.rpm + +**Local release build**: +```bash +cargo build --release -p codegraph +# Binary at target/release/codegraph +``` + +--- + +## Project Structure + +``` +. +├── crates/ # Workspace members +├── docs/ # Documentation (this file + others) +│ ├── architecture.md +│ ├── comparison.md +│ ├── configuration.md +│ ├── development.md # This file +│ ├── semantic-search.md +│ ├── storage-backends.md +│ ├── why-rust.md +│ └── specs/ # Detailed spec docs +├── scripts/ # Install scripts (sh/ps1) +├── sql/ # Postgres/MySQL schemas +├── packaging/ # .deb/.rpm packaging +├── .github/workflows/ # CI/CD +├── Cargo.toml # Workspace root +└── README.md # Main entry point +``` + +--- + +## Contributing + +1. Fork & branch +2. `cargo fmt --all && cargo clippy --workspace --all-targets -- -D warnings` +3. `cargo test --workspace` +4. Add tests for new functionality +5. Update relevant docs in `docs/` +6. PR with clear description + +**Commit style**: Conventional commits (`feat:`, `fix:`, `docs:`, `refactor:`, `test:`) + +--- + +## Debugging Tips + +```bash +# Verbose logging +RUST_LOG=codegraph=debug codegraph init + +# Specific crate +RUST_LOG=codegraph_graph=trace codegraph init + +# MCP server debug +RUST_LOG=codegraph_mcp=debug codegraph serve --mcp + +# Watcher debug +RUST_LOG=codegraph=debug codegraph serve --mcp +``` + +--- + +## Related Docs + +- [Architecture](architecture.md) — Pipeline and crate relationships +- [Why Rust](why-rust.md) — Rewrite rationale and benchmarks +- [Configuration](configuration.md) — Config reference +- [Storage Backends](storage-backends.md) — Backend deep-dive +- [Semantic Search](semantic-search.md) — Embedding setup +- [README](../README.md) — Quick start \ No newline at end of file diff --git a/docs/semantic-search.md b/docs/semantic-search.md new file mode 100644 index 000000000..fd63e4355 --- /dev/null +++ b/docs/semantic-search.md @@ -0,0 +1,157 @@ +# Semantic Search (Embeddings) + +Optional vector similarity search over symbol embeddings. Off by default. + +## Quick Start + +```toml +# .codegraph/config.toml +[embedding] +backend = "fastembed" +model = "bge-small-en-v1.5" +cache_dir = "~/.cache/codegraph/embeddings" +``` + +```bash +# Pre-download model (optional, for offline indexing) +codegraph embed --model bge-small-en-v1.5 + +# Re-index to generate embeddings +codegraph init +``` + +## How It Works + +1. **Model**: BGE-small-en-v1.5 (384-dim, ONNX) via fastembed +2. **Indexing**: Each symbol's name + signature → embedding vector +3. **Storage**: Vectors persisted alongside graph (in same backend) +4. **Query**: `codegraph_search_symbol` with `match = "semantic"` or `"hybrid"` +5. **Hybrid**: Reciprocal Rank Fusion (RRF) merges substring + semantic results + +## Configuration Reference + +| Key | Required | Default | Description | +|-----|----------|---------|-------------| +| `backend` | Yes* | unset (off) | `"fastembed"` to enable; `"hashing"` for deterministic fallback | +| `model` | No | `"bge-small-en-v1.5"` | ONNX model name (must be 384-dim) | +| `cache_dir` | No | `"~/.cache/codegraph/embeddings"` | Global model cache | +| `vss_extension` | No | unset | SQLite-only: path to sqlite-vss for HNSW ANN | +| `execution_provider` | No | unset | `"coreml"` for macOS Apple Neural Engine/GPU | + +*Required to enable — if unset, semantic search is completely disabled (no model loads). + +## MCP Tool Changes + +With embeddings enabled, `codegraph_search_symbol` gains: + +| Match Mode | Description | +|------------|-------------| +| `contains` (default) | Substring match on lowercase names | +| `prefix` / `suffix` / `exact` | String match variants | +| `semantic` | Vector KNN — finds symbols by semantic similarity | +| `hybrid` | RRF merge of `contains` + `semantic` | + +**Example**: +```json +// Semantic search +{ "query": "user authentication", "match": "semantic", "limit": 10 } + +// Hybrid (recommended for best recall) +{ "query": "auth user", "match": "hybrid", "limit": 10 } +``` + +## SQLite + sqlite-vss (HNSW ANN) + +For large indexes, exact KNN (brute-force) is slow. SQLite can use the `sqlite-vss` extension for HNSW approximate nearest neighbor. + +**Setup**: +1. Install sqlite-vss (see https://github.com/asg017/sqlite-vss) +2. Point `vss_extension` to the extension directory: +```toml +[embedding] +backend = "fastembed" +vss_extension = "~/.cache/codegraph/embeddings/vss" +``` +3. Re-index — vectors will be indexed in HNSW + +**Trade-offs**: +- HNSW: faster queries, approximate results, extra disk space +- Brute-force: exact, slower on >100k vectors, no extra deps + +## macOS Hardware Acceleration (CoreML) + +On macOS, run embeddings on Apple Neural Engine / GPU via CoreML execution provider. + +**Build**: +```bash +cargo build --features fastembed,apple-accel +``` +*Fails on non-macOS.* + +**Config**: +```toml +[embedding] +backend = "fastembed" +execution_provider = "coreml" +``` + +**Benefits**: 2–5× faster embedding inference on Apple Silicon. + +## Model Management + +**Pre-download** (offline indexing): +```bash +codegraph embed --model bge-small-en-v1.5 --cache-dir ~/.cache/codegraph/embeddings +``` +- Requires binary built with `--features fastembed` +- Downloads ONNX model to cache dir +- Subsequent indexing works offline + +**Cache location**: `~/.cache/codegraph/embeddings/` (configurable via `cache_dir`) + +**Model files** (~50 MB): +- `model.onnx` — the quantized BGE-small model +- `tokenizer.json` — tokenizer config + +## Error Handling + +**Critical**: If the model fails to load (no network, missing ONNX runtime, corrupted cache), **opening the index errors out**. There is no silent fallback to lexical-only search. + +This is by design — silent fallback would return misleading results. + +**Troubleshooting**: +- Verify `cache_dir` exists and is writable +- Check ONNX Runtime is available (bundled in release binary) +- Run `codegraph embed` to re-download model +- Check logs: `RUST_LOG=codegraph_graph=debug codegraph init` + +## Performance + +| Metric | Value | +|--------|-------| +| Model size | ~50 MB (ONNX, int8 quantized) | +| Dimensions | 384 | +| Embedding latency | ~2–5 ms/symbol (CPU), ~0.5–1 ms (CoreML) | +| Index overhead | 384 × 4 bytes × num_symbols (~1.5 KB/symbol) | +| Query latency (brute-force) | O(N) — ~100k vectors = ~50 ms | +| Query latency (HNSW) | O(log N) — ~100k vectors = ~2 ms | + +## When to Enable + +✅ **Enable if**: +- Agents search by concept/intent ("error handling", "database connection") +- Codebase has inconsistent naming (synonyms, abbreviations) +- You want "fuzzy" symbol discovery + +❌ **Skip if**: +- Strict name-based search is sufficient +- Indexing speed is critical (embeddings add ~2–5 ms/symbol) +- Disk space is constrained +- Offline-only with no pre-download opportunity + +## Related Docs + +- [Configuration](configuration.md) — Full config.toml reference +- [Storage Backends](storage-backends.md) — Vector storage per backend +- [MCP Tools](../README.md#mcp-tools) — `codegraph_search_symbol` reference +- [README](../README.md) — Quick start \ No newline at end of file diff --git a/docs/storage-backends.md b/docs/storage-backends.md new file mode 100644 index 000000000..aa11ab486 --- /dev/null +++ b/docs/storage-backends.md @@ -0,0 +1,248 @@ +# Storage Backends + +Deep dive on CodeGraph's pluggable storage backends. + +## Overview + +CodeGraph's `GraphIndex` uses a pluggable storage abstraction. The backend is selected via `[storage] type` in `config.toml`. + +| Backend | Type | Persistence | Concurrency | Best For | +|---------|------|-------------|-------------|----------| +| SQLite | Embedded SQL | Single file (WAL) | Single-writer, multi-reader | Default, local projects | +| LMDB | Embedded KV (mmap) | Directory | Multi-reader, single-writer | Large indexes, mmap-friendly | +| Redis | Client-server | Remote | Multi-writer | Shared index, multi-process | +| Memory | In-process | None | N/A | Testing, ephemeral | +| PostgreSQL | Client-server (sharded) | Remote | Multi-writer | Multi-tenant, production | +| MySQL | Client-server (sharded) | Remote | Multi-writer | Multi-tenant, production | + +--- + +## SQLite (Default) + +**Config**: +```toml +[storage] +type = "sqlite" +# dsn = "sqlite:///absolute/path/to/db.sqlite" # optional override +``` + +**Characteristics**: +- Single file: `.codegraph/db.sqlite` (WAL mode) +- Entities + radix streams stored in tables +- No external dependencies (bundled `rusqlite` with `bundled` feature) +- WAL mode allows concurrent readers during write +- **Default and recommended** for most local use + +**Performance** (from `crates/codegraph-bench/STORAGE_PERF.md` on `crates/` corpus): +- Open + ingest (median): ~12–14 µs (in-memory baseline), ~40–43 ms (SQLite on disk) +- On-disk size: ~590–690 KB for `crates/` workspace + +**Limitations**: +- Single-writer — not suitable for concurrent multi-process writes +- File-based — not network-accessible + +--- + +## LMDB + +**Config**: +```toml +[storage] +type = "lmdb" +# dsn = "lmdb:///absolute/path/to/db.lmdb" # optional override +``` + +**Characteristics**: +- Memory-mapped KV store (`.codegraph/db.lmdb/` directory) +- Bundled C library (`lmdb-rkv`) — no system dependency +- Zero-copy reads via mmap — excellent for read-heavy workloads +- Single-writer, multi-reader (like SQLite) +- **Smaller on-disk footprint** than SQLite (~2.2× smaller per benchmarks) + +**Performance** (same corpus): +- Open + ingest (median): ~16–28 ms +- On-disk size: ~270 KB for `crates/` workspace + +**When to choose**: +- Very large indexes where mmap helps +- Read-heavy workloads +- You want smaller disk usage + +**Limitations**: +- Single-writer +- Directory-based (not a single file) +- Map size must be configured for very large DBs (handled automatically) + +--- + +## Redis + +**Config**: +```toml +[storage] +type = "redis" +dsn = "redis://localhost:6379" # REQUIRED +``` + +**Characteristics**: +- Client-server — requires running Redis instance +- Supports multi-process / multi-machine access +- Uses Redis hashes/streams for entities and indexes +- Connection pooling via `redis` crate with `tokio-comp` + +**When to choose**: +- Multiple processes sharing one index +- Index lives on a separate server +- Need pub/sub for cache invalidation (future) + +**Limitations**: +- Network latency on every operation +- Requires Redis server management +- No embedded mode + +--- + +## Memory (Ephemeral) + +**Config**: +```toml +[storage] +type = "memory" +``` + +**Characteristics**: +- Pure in-process `DashMap` + in-memory engines +- Nothing persisted — index lost on exit +- Fastest for benchmarks/testing + +**When to choose**: +- Unit tests +- Ephemeral indexing (CI, scripting) +- Benchmarking storage overhead + +--- + +## PostgreSQL (Multi-Tenant, Sharded) + +**Config**: +```toml +[storage] +type = "postgres" +dsns = [ + "postgres://user:pass@db1:5432/codegraph", + "postgres://user:pass@db2:5432/codegraph", +] +# repo_id auto-generated and written to config +# repo_id = 14028493579208694412 +``` + +**Architecture**: +- Every table partitioned by leading `repo_id` (`u64`) +- Each project root (`.codegraph/`) → its own `repo_id` +- Sharding: `shard = repo_id % len(dsns)` +- Re-indexing/deleting one repo never touches another + +**Schema** (manual apply required): +```bash +# Run against EVERY shard +psql "$DSN" -f sql/postgres/001-initial-schema.sql +psql "$DSN" -f sql/postgres/002-add-repos-registry.sql +``` + +**Tables** (per shard): +- `repos` — registry of `repo_id` → root path +- `entities` — symbols (partitioned by `repo_id`) +- `chains` — call chains (partitioned) +- `call_records` — resolved calls (partitioned) +- `edges` — derived edges (partitioned) +- `vectors` — embeddings (partitioned, if enabled) + +**Build**: Requires `rdbms` feature (on by default for `codegraph` binary): +```bash +cargo build --features rdbms +cargo build -p codegraph-mcp --features rdbms +``` + +**When to choose**: +- Multi-tenant SaaS (each customer = one repo_id) +- Shared infrastructure, isolated data +- Need SQL tooling for analytics + +**Limitations**: +- Manual schema management +- Network latency +- More complex ops + +--- + +## MySQL (Multi-Tenant, Sharded) + +**Config**: +```toml +[storage] +type = "mysql" +dsns = [ + "mysql://user:pass@db1:3306/codegraph", + "mysql://user:pass@db2:3306/codegraph", +] +# repo_id auto-generated +``` + +**Schema** (manual apply): +```bash +mysql "$DB" < sql/mysql/001-initial-schema.sql +mysql "$DB" < sql/mysql/002-add-repos-registry.sql +``` + +Same architecture as Postgres — partitioned by `repo_id`, sharded by `repo_id % N`. + +**When to choose**: Same as Postgres, but MySQL preferred. + +--- + +## Backend Selection Guide + +| Scenario | Recommended | +|----------|-------------| +| Local development, single project | `sqlite` (default) | +| Large local index, read-heavy | `lmdb` | +| Multiple agents/processes same machine | `redis` or `lmdb` | +| Team shared index (LAN) | `redis` | +| Multi-tenant SaaS | `postgres` or `mysql` | +| CI/testing | `memory` | +| Production with SQL tooling needs | `postgres` | + +--- + +## Switching Backends + +1. Update `config.toml` `[storage] type = "..."` +2. Run `codegraph init` (or `codegraph_index` via MCP) — full re-index +3. Old index files remain but are unused (safe to delete `.codegraph/db.*`) + +**Note**: No migration between backends — always full re-index from source. + +--- + +## Performance Notes + +From `crates/codegraph-bench/STORAGE_PERF.md` (local `crates/` corpus, 3 runs median): + +| Backend | Open+Ingest | On-Disk Size | Query Latency (200 ops) | +|---------|-------------|--------------|-------------------------| +| `in_memory` | ~12 µs | N/A | ~84–90 ns/op | +| `sqlite` | ~40–43 ms | ~590–690 KB | ~84–90 ns/op | +| `lmdb` | ~16–28 ms | ~270 KB | ~84–90 ns/op | + +- Query latency dominated by in-memory engines (radix + chain search), not storage +- High variance noted in SQLite/LMDB ingest ("measurement machine was loaded") +- LMDB ~1.4–2.1× faster ingest than SQLite; ~2.2× smaller on disk + +--- + +## Related Docs + +- [Configuration](configuration.md) — Full config.toml reference +- [Architecture](architecture.md) — GraphIndex and storage abstraction +- [SQL Schema](sql/README.md) — Postgres/MySQL schema details +- [README](../README.md) — Quick start \ No newline at end of file diff --git a/docs/why-rust.md b/docs/why-rust.md new file mode 100644 index 000000000..ae2e9b56b --- /dev/null +++ b/docs/why-rust.md @@ -0,0 +1,132 @@ +# Why Rust? — The Rewrite Story + +CodeGraph is a from-scratch Rust rewrite of the previous TypeScript implementation. + +## The Old Stack (TypeScript) + +| Component | Technology | Pain Points | +|-----------|------------|-------------| +| Runtime | Node.js (embedded) | ~50 MB baseline, multi-second cold start | +| Parsing | 20+ tree-sitter WASM grammars | WASM overhead, no parallel parsing | +| Storage | Native SQLite addon (better-sqlite3) | Node-gyp builds, platform issues | +| Distribution | Single binary via `pkg` | ~140 MB, not truly static | + +**Result**: ~140 MB binary, 2–3 second startup, complex build pipeline. + +--- + +## The Rust Rewrite + +### What Changed + +| Before (TS) | After (Rust) | Impact | +|-------------|--------------|--------| +| Node runtime | **None** — static binary | -80 MB, sub-ms startup | +| WASM grammars | **Statically-linked tree-sitter C** | Native speed, rayon parallelism | +| Native SQLite addon | **Bundled `rusqlite` (bundled feature)** | No system deps, no node-gyp | +| `pkg` bundler | **`cargo build --release` + `strip`** | Standard Rust toolchain | + +### Build Optimizations + +```toml +# Cargo.toml (workspace) +[profile.release] +lto = "fat" # Cross-crate optimization +codegen-units = 1 # Maximum optimization +strip = true # Strip symbols +panic = "abort" # Smaller binary, no unwinding +``` + +### Results + +| Metric | TypeScript | Rust | Improvement | +|--------|------------|------|-------------| +| Binary size | ~140 MB | **~58 MB** | **2.4× smaller** | +| Cold start | ~2–3 s | **<100 ms** | **20–30× faster** | +| Indexing (139 files) | ~1 s | **~190 ms** | **~5× faster** | +| Memory (idle) | ~80 MB | **~15 MB** | **5× less** | +| Dependencies | 500+ npm packages | **~100 crates** | Simpler supply chain | + +--- + +## Why These Choices? + +### `tree-sitter` (C) over WASM + +- **Parallel parsing**: `rayon` thread pool across files — WASM can't do true parallelism +- **Zero-copy**: Parse trees reference source bytes directly +- **No WASM overhead**: Function calls, memory copies eliminated +- **Grammar updates**: `tree-sitter` C libs updated independently + +### `rusqlite` (bundled SQLite) over native addon + +- **Pure Rust + bundled C**: `rusqlite` with `bundled` feature compiles SQLite from source +- **No system SQLite needed**: Works on minimal containers (distroless, scratch) +- **WAL mode**: Concurrent readers during write +- **No node-gyp**: Eliminates entire class of build failures + +### Single Binary Philosophy + +``` +codegraph binary contains: + ├── tree-sitter parsers (14 languages, statically linked) + ├── SQLite (bundled, WAL mode) + ├── LMDB (bundled via lmdb-rkv) + ├── Redis client (async, tokio) + ├── Postgres/MySQL drivers (sqlx, compiled in) + ├── ONNX Runtime + fastembed (BGE-small model loader) + └── MCP server (rmcp SDK) +``` + +**No**: +- External processes +- Shared libraries (except libc) +- Runtime downloads (model cached separately) +- Daemon/background service + +--- + +## Trade-offs + +| Gain | Cost | +|------|------| +| Fast startup | Longer compile time (~3–5 min clean) | +| Small binary | Larger binary than minimal CLI (~58 MB) | +| Parallel parsing | More complex build (C dependencies) | +| No runtime deps | Can't hot-reload grammars (rebuild needed) | +| Type safety | Learning curve for contributors | + +--- + +## Verification + +```bash +# Build release +cargo build --release -p codegraph + +# Check size +ls -lh target/release/codegraph +# ~58 MB + +# Verify static linking +ldd target/release/codegraph +# Should show only libc, libdl, libpthread, libm, libgcc_s + +# Benchmark startup +time target/release/codegraph --version +# <100 ms + +# Benchmark indexing +cd /path/to/project +time target/release/codegraph init +# ~190 ms for ~139 files +``` + +--- + +## Related Docs + +- [Architecture](architecture.md) — Crate structure and pipeline +- [Development](development.md) — Build, test, feature flags +- [Configuration](configuration.md) — Storage/embedding backends +- [README](../README.md) — Quick start \ No newline at end of file