rustbrain
The CLI for a Rust-native second brain — project-scoped Markdown knowledge graph for humans and AI coding agents.
&&
| You install… | You get… |
|---|---|
rustbrain (this crate) |
The rustbrain binary — agent-friendly commands |
rustbrain-core |
The same engine as a library (embed in tools / agents) |
Intended primary use: the CLI in a repo.
Library path is separate for engineers who wantBrain/query_ranked/context_for_promptin-process — same algorithms, no second product.
What is rustbrain?
Write ordinary notes (docs/**/*.md, Obsidian-style WikiLinks + frontmatter). Index Rust with Tree-Sitter. Search with SQLite FTS5. Expand a CSR graph (graph.mmap). Pack agent context under a token budget.
All offline, project-scoped, Git-friendly. The engine does not invent ADRs, changelogs, or history.
docs/*.md src/**/*.rs CHANGELOG.md Cargo.toml deps
│ │ │ │
▼ ▼ ▼ ▼
WikiLinks tree-sitter hub:changelog docs.rs notes
frontmatter symbols
└────────────┬─────────────┴───────────────┘
▼
.brain/db.sqlite ← disposable index (gitignore)
│
▼
.brain/graph.mmap ← CSR neighborhood cache
│
▼
setup · note · sync · query · context · graph · links · doctor
A simple analogy: most “docs for agents” dump a folder of Markdown into the prompt and hope. rustbrain is a filing system with an index and a map of the building — search the catalog, walk who links to whom, then pack only the rooms that fit the token budget.
Mental model (four jobs)
| Job | Everyday phrase | Command |
|---|---|---|
| Orient | “What does this repo already know?” | context, query, graph |
| Capture | “Write a real note, not chat residue.” | note new → edit file → sync |
| Connect | “Who links to whom? Fix pending WikiLinks.” | graph, links, links --apply |
| Health | “Is the brain usable for agents?” | doctor, sync |
Markdown on disk is the source of truth. .brain/ is a rebuildable cache (like target/ for knowledge).
Why rustbrain?
| Benefit | What you get |
|---|---|
| Agent-ready in one install | setup --yes → AGENTS.md + index + doctor; then context / query every turn |
| Truth in Git | Notes are plain Markdown; .brain/ is disposable — gitignore it |
| Code ↔ docs | symbol:Foo from notes; [[docs/adr/…]] in rustdoc → bidirectional edges |
| Graph-aware packs | context ranks seeds + hops neighbors under -m token budget (Markdown or XML) |
| Rust ecosystem hubs | Indexes root CHANGELOG.md; harvests docs.rs URLs from Cargo.toml on setup |
| HITL planning | plan notes with densified statuses (backlog → blocked); optional ROADMAP/BACKLOG |
| No cloud required | Local SQLite FTS5 + optional CSR mmap; algorithmic ranking — not neural inventiveness |
| Safe link rewrites | links --apply closes unique pending WikiLinks; optional Aho–Corasick discover (dry-run default) |
When to use rustbrain vs plain grep / RAG
| Prefer rustbrain when… | Prefer something else when… |
|---|---|
| You want structured notes (goals, ADRs, plans, analyses) that agents must respect | You only need one-off file search in a throwaway folder |
| Agents must not invent decision history | You want a chat product that “remembers” without files |
| You need code↔doc edges and neighborhood expansion | Pure vector RAG over opaque blobs is enough |
| Docs live in the repo and should be reviewable in PRs | Knowledge must live only in a SaaS second-brain app |
Rule of thumb: if the answer should still be true after the chat ends, put it in Markdown and index it with rustbrain.
Install
# pin a release:
# cargo install rustbrain --version 0.3.21 --locked
| Requirement | Notes |
|---|---|
| Rust | MSRV 1.80+ |
| C toolchain | Bundled SQLite + tree-sitter need a system C compiler |
| License | MIT OR Apache-2.0 |
Workspace flag: almost every command accepts -w /path/to/project (default .).
query / context / doctor / graph walk parent directories for .brain/ (git-style).
Getting started (5 minutes)
1. One-shot setup (recommended)
What setup --yes does:
- Creates
.brain/db.sqlite - Scaffolds
docs/(goals, ADRs, analysis, plans, …) - Writes root
AGENTS.md+docs/AGENTS.md(agent protocol for this repo) - Harvests README →
docs/goals/from-readme.md(if present) - Harvests Cargo.toml deps → docs.rs notes under
docs/references/crates/ - Optional AST module map (
docs/implementation/module-map.generated.md) sync(full index) +doctor
Common variants:
2. Capture knowledge (scaffold → edit → sync)
# Preferred: type + title only → scaffold → edit the printed path → sync
# edit docs/adr/use-local-sqlite.md (Status / Context / Decision / Consequences)
# status: backlog | in_progress | qa | done | cancelled | blocked
Agent tip: omit --body / --note so scaffolds appear; fill the file; then sync.
Pass --body only when the full text is already finished (skips scaffold).
3. Orient (search, pack, structure)
4. Everyday loop
edit docs or code → rustbrain sync → query / context / graph
│
├── pending WikiLinks? → rustbrain links
├── soft connect? → rustbrain links --auto
├── normalize links? → rustbrain links --apply --dry-run
│ rustbrain links --apply --write
└── live editing? → rustbrain watch --debounce-ms 300
Recommended sequences
Copy-paste playbooks. Prefer these over inventing your own flag soup.
A. Greenfield / first day on a repo
# read AGENTS.md (and docs/AGENTS.md) — agents should follow it every turn
B. Agent turn (HITL coding session)
# 1. Orient before large refactors
# 2. Do the work in the codebase…
# 3. Capture decisions / findings (scaffold, then edit)
# edit the file — do not invent history that did not happen
# 4. Re-index
C. Release / “what shipped?”
# Prefer a real root CHANGELOG.md (Keep a Changelog style works well)
D. Planning / backlog hygiene
# edit checkboxes + status sections; optional root ROADMAP.md / BACKLOG.md hubs
E. Link hygiene (careful rewrites)
# style: wrap (inline) | related (## Related list)
F. Portability (hand a brain to another machine)
# copy team.brainbundle …
G. Multi-brain / umbrella (optional)
# Learn ids first
# Cargo monorepo SubBrains
# Umbrella: three former mono MainBrains under one folder
# Share without merge:
# Merge a SubBrain into MainBrain:
G. CI / agents (non-interactive only)
# or stepwise:
Command reference
Full flag book (always authoritative for edge cases):
docs/CLI.md on GitHub.
Lifecycle map
setup ──► (or: init ──► bootstrap ──► sync ──► doctor)
│
├── note new ──► sync
├── query / context / graph / links / scopes
└── watch (optional live re-index)
Command index
| Command | Purpose |
|---|---|
setup |
One-shot: init + bootstrap + sync + doctor |
init |
Create empty .brain/db.sqlite only |
bootstrap |
Docs tree, AGENTS, ignore, README harvest, crate docs, module map |
sync |
Index Markdown / Canvas / Rust; bake graph.mmap |
doctor |
Health (--orphans, --json, --strict; multi-brain scope checks) |
note new |
Typed scaffold (--type + --title; optional --scope) |
query |
Ranked FTS (--scores, --type, --with-symbols, --scope) |
context |
Agent pack (-m, -F markdown|xml, --scope) |
graph |
Neighborhood ASCII/JSON or workspace stats |
scopes |
Multi-brain: list (discover ids), enable, add, attach, import, absorb, reconcile |
links |
Pending; --auto; --apply (+ --discover) |
watch |
Debounced live re-index |
export / import |
.brainbundle (export --scope ID shares one SubBrain) |
Discover SubBrain ids (before import / --scope)
| Goal | Command |
|---|---|
| List ids here | rustbrain scopes list |
| JSON for agents | rustbrain scopes list --json |
| List ids elsewhere | rustbrain scopes list -w /path/to/other |
| After Cargo discover | scopes enable --cargo then scopes list |
| New folder / foreign mono | Choose id (convention = directory name) → attach ID --root DIR or import --from DIR --as ID |
# → SubBrain ids, roots, node counts
After setup, root AGENTS.md includes the same command tables for agents.
setup
Always non-interactive. Preferred entry for agents and CI.
| Flag | Meaning |
|---|---|
--yes |
Required non-interactive mode |
--force |
Overwrite generated bootstrap files / ignore / AGENTS when regenerating |
--no-crate-docs |
Skip Cargo.toml → docs.rs notes |
--no-agents-md |
Skip writing AGENTS.md |
--agents-template PATH |
Custom cookbook content |
--no-bootstrap |
init + sync only |
--no-doctor |
Skip final doctor |
init
Creates .brain/db.sqlite and registers the workspace when possible.
Does not scaffold docs or index. Prefer setup --yes unless you are composing a custom pipeline.
bootstrap
Deterministic onboarding for existing codebases. No LLM. Never invents ADR history.
| Flag | Meaning |
|---|---|
--write |
Apply changes to disk |
--dry-run |
Plan only |
-y / --yes |
Non-interactive defaults |
--force |
Overwrite ignore, AGENTS, generated: true files |
--no-ignore |
Skip .rustbrainignore |
--import-gitignore / --no-import-gitignore |
Force / forbid .gitignore merge |
--no-agents-md |
Do not write root AGENTS.md |
--agents-template PATH |
Cookbook source |
Typical writes: docs/** scaffolds, docs/goals/from-readme.md, crate docs notes, module map, AGENTS.md, docs/AGENTS.md, .rustbrainignore, .brain/ if missing, append .brain/ to .gitignore.
AGENTS.md template order (first match wins):
--agents-templateRUSTBRAIN_AGENTS_TEMPLATE.rustbrain/AGENTS.template.mdorAGENTS.template.md- Built-in default
sync
Indexes Markdown, Canvas (if present), Rust AST symbols; resolves WikiLinks / symbol: / rustdoc [[…]]; densifies plan status + changelog summaries; bakes graph.mmap.
Reports file_errors=N when individual files fail (does not abort the whole walk).
Run after editing docs or code that should appear in search/context.
doctor
| Flag | Meaning |
|---|---|
--json |
Machine-readable report |
--strict |
Exit 1 if unhealthy or pending links |
--orphans |
Detail notes with no explicit WikiLink / symbol: edges |
status: OK means the index is usable; infos guide enrichment — they do not invent docs.
note new
Designed for AI agents and humans who want consistent structure.
# Preferred: scaffold, then edit the printed path
# Full body ready (skips scaffold)
# Options
| Flag | Meaning |
|---|---|
--type |
goal, adr, concept, analysis, plan, edge_case, reference, … |
--title |
H1 + filename slug |
--note / --body |
Body after H1 (aliases; skip scaffold when set) |
--tags / --aliases |
Comma-separated |
--no-sync |
Do not index after write |
--force |
Overwrite existing file |
--dir |
Override default folder |
query
Ranked FTS — not neural embeddings.
| Flag | Meaning |
|---|---|
--scores |
Show rank score |
-n / --limit |
Max hits (default 25) |
--with-symbols / --all-types |
Include symbol nodes (default: notes only) |
--type a,b |
Only these types |
--all-workspaces |
Merge hits across the global registry |
-w |
Workspace root |
Natural-language prompts work: stopwords drop; multi-token OR for recall.
Ranking = BM25 + title/id/tag/alias boosts + type priors + hub boosts.
context
Build an agent-oriented pack: ranked seeds + optional CSR k-hop neighbors, under a token budget. Packs body excerpts (frontmatter stripped), not titles alone.
| Flag | Meaning |
|---|---|
positional / -p / --for-prompt |
Topic string |
-m / --max-tokens |
Soft budget (~4 chars/token) |
--hops |
Graph depth (0 = seeds only) |
--with-symbols |
Include symbols as seeds (default note-first) |
--no-hop-symbols |
Also exclude symbols from neighbors |
--type a,b |
Seed type filter |
-F markdown|xml |
Output format (default markdown) |
Defaults: note-first seeds; hops may still reach useful symbols (noise consts filtered).
Empty/generic prompts fall back to README hub; release-ish prompts pull changelog; planning prompts pull roadmap / backlog when indexed.
graph
Inspect structure (who links to whom). Complements context (which packs content).
| Flag | Meaning |
|---|---|
TARGET |
Node id, path, unique title, or symbol:Name (omit = stats) |
--hops |
BFS depth (default 1) |
--direction |
both (default), out, in |
--no-auto |
Hide soft auto_* edges |
--no-symbols |
Hide symbol neighbors |
--type |
Neighbor type filter |
--limit |
Max edges shown |
--json |
Machine-readable |
--stats |
With TARGET: stats header + neighborhood |
graph: docs/concepts/raft (concept) "Raft"
├──[→ relates_to w=1.00] docs/concepts/logcompaction
├──[→ anchors w=1.00] symbol/…/storageengine
└──[← relates_to w=0.90] docs/adr/0001-use-raft
links
Three modes: list pending, soft auto-edges, apply rewrites.
# Apply (default = dry-run; --write required to mutate files)
| Mode | Mutates Markdown? | What it does |
|---|---|---|
| default list | No | Shows unresolved refs |
--auto |
No | Inserts low-weight auto_* edges in the DB |
--apply |
Only with --write |
Closes unique pending links; optional AC discover |
Apply flags: --discover, --style wrap|related, --no-graph-priors, --force, --limit N, --no-sync, --json, optional TARGET filter.
Tiers: AUTO may write; SUGGEST report-only; SKIP never writes (ambiguous, generated, limit, …).
Discover uses .brain/link_lexicon.json (invalidated when nodes/aliases change). Never invents notes.
watch
Debounced re-index while you edit. Requires the CLI build with watch support (default install includes it when enabled in the package).
export / import
Portable bundles for sharing an index snapshot. Re-sync after import if sources differ.
Note types & hubs
| Type | Default dir | Use for |
|---|---|---|
goal |
docs/goals/ |
Aims / non-goals |
adr |
docs/adr/ |
Decisions you committed to |
alternative |
docs/adr/ |
Options considered |
analysis |
docs/analysis/ |
Dated investigations (benches, options) |
plan |
docs/plans/ |
Roadmaps, backlogs, tasklists (status densified on sync) |
changelog |
root hub / docs/changelogs/ |
Ship history (prefer root CHANGELOG.md) |
concept |
docs/concepts/ |
Timeless “what is X” |
edge_case |
docs/edge_cases/ |
Traps and platform quirks |
reference |
docs/concepts/ or generated crates |
External crates / APIs |
symbol |
(from AST) | Code entities |
Root hubs (when files exist):
| File | Node id | Type |
|---|---|---|
README.md |
readme |
goal |
CHANGELOG.md |
changelog |
changelog |
ROADMAP.md |
roadmap |
plan |
BACKLOG.md |
backlog |
plan |
Plan status densification
After sync, plan notes project status into summary + FTS:
| Canonical | Surfaces (examples) |
|---|---|
backlog |
todo, open, pending |
in_progress |
wip, doing, active |
qa |
review, testing |
done |
complete, finished |
cancelled |
canceled, wontfix |
blocked |
stuck, on_hold, undone (legacy alias) |
Query tokens: status:backlog, status:in_progress, status:qa, status:done, status:cancelled, status:blocked.
Sources: frontmatter status: / state:, ## Status, section headings, checkboxes ([ ] [/] [x] [~] [?] [!]).
There is no separate tasks SQL table — Markdown is truth; the brain densifies for search.
Linking notes and code
node_type: adr
tags: [storage]
Decision: keep the brain in-process.
See [[docs/concepts/fts5]] and symbol:Database::open.
/// Primary store. Decision: [[docs/adr/use-local-sqlite]].
On sync:
| From → To | Edge |
|---|---|
Note symbol:… → code |
anchors |
Rustdoc [[note]] → note |
doc_links |
| Note WikiLinks | relates_to (and friends) |
| Soft connect | auto_filename, auto_tag |
Agent protocol (HITL)
After setup, treat AGENTS.md / docs/AGENTS.md as the project mandate:
- Orient with
rustbrain context "…"before large refactors - Search with
query/graphinstead of inventing history - Capture with
note newscaffolds + WikiLinks /symbol: syncafter doc or code changes- Never invent ADR history or changelog entries
# or commit AGENTS.template.md / .rustbrain/AGENTS.template.md
# or export RUSTBRAIN_AGENTS_TEMPLATE=/path/to/template.md
On-disk layout
| Path | Role |
|---|---|
docs/**/*.md |
Human/agent-authored knowledge (source of truth) |
CHANGELOG.md |
Optional hub changelog |
ROADMAP.md / BACKLOG.md |
Optional plan hubs |
.rustbrainignore |
Extra index skips (optional) |
.brain/db.sqlite |
Derived SQLite + FTS5 + edges |
.brain/graph.mmap |
CSR adjacency cache |
.brain/link_lexicon.json |
Optional AC lexicon cache |
.brain/workspace.json |
Marker |
AGENTS.md, docs/AGENTS.md |
Agent cookbooks |
Ignore dialect: built-in target/, .git/, .brain/, … plus optional .rustbrainignore.
Line # rustbrain: import-gitignore merges root .gitignore. Env RUSTBRAIN_IMPORT_GITIGNORE=1 forces merge.
Formats: SCHEMA.md · MMAP_FORMAT.md.
Exit codes
| Code | Meaning |
|---|---|
0 |
Success |
1 |
Error, or doctor --strict with unhealthy / pending links |
Library usage (optional)
Embed the same engine in tools and agents via rustbrain-core:
[]
= "0.3"
use ;
Full API surface, feature flags, and more examples: the rustbrain-core README and docs.rs/rustbrain-core.
Performance
Criterion harness: cargo bench -p bench --bench rustbrain_performance
Full write-up and fairness notes: docs/BENCHMARKS.md.
Numbers below are approximate medians (release, x86_64 Linux, Criterion --quick). Re-run on your machine.
Search — 500 notes, query sqlite storage
| Approach | ~Time | Notes |
|---|---|---|
rustbrain query_ranked |
~3.5 ms | FTS5 BM25 + title/tag/type boosts |
Walk all .md + substring |
~7.6 ms | No index; scales with corpus |
SQLite LIKE full scan |
~0.90 ms | Same bodies, no ranking quality |
Graph neighborhood — 500 notes, 1 hop
| Approach | ~Time | Notes |
|---|---|---|
CSR graph.mmap k-hop |
~156 ns | Topology cache used by agent context hops |
SQL neighborhood (graph CLI) |
~1.5 ms | Preserves relation types |
| Re-parse all WikiLinks + BFS | ~8.9 ms | “No edge index” baseline (~50 000× vs CSR) |
Context pack — 500 notes, ~1024 token budget
| Approach | ~Time | Notes |
|---|---|---|
rustbrain context |
~5.7 ms | Ranked seeds + CSR hops + excerpts |
| Path-order file concat | ~2.7 ms | Faster dump, unranked |
| Grep-rank then concat | ~8.1 ms | No graph hops / type packing |
Takeaway: CSR graph hops and FTS-ranked retrieval beat “walk the docs every time” as the corpus grows. Cheaper dumps (concat, naive regex) trade away ranking and correctness — see the benchmark doc for framing.
What v0.3.x claims / does not
Does: local Markdown second brain; bootstrap for mature repos; doctor; agent note new; ranked FTS with type filters; graph-aware context; first-class changelog + plan types; docs.rs harvest; portable bundles; careful links --apply.
Does not (yet): neural embeddings; full two-way Obsidian write-back; multi-brain --scope SubBrain product; inventing history for you.
Related resources
| Resource | Link |
|---|---|
| Full CLI book | docs/CLI.md |
| Performance benches | docs/BENCHMARKS.md |
| Library crate | rustbrain-core |
| Source monorepo | shan-alexander/rustbrain |
| API docs | docs.rs/rustbrain-core |
License
MIT OR Apache-2.0