rustbrain-core
Core engine for rustbrain: a project-scoped knowledge graph for software repositories — humans and AI agents.
Index Markdown notes and Rust symbols into SQLite (FTS5 + edges), bake an optional CSR graph.mmap, then serve ranked search and graph-aware context packs under a token budget. No cloud, no invented ADRs — algorithmic and Git-friendly.
Markdown notes Rust sources
WikiLinks / YAML tree-sitter symbols
│ │
└───────────┬────────────┘
▼
Brain::sync()
│
┌───────────┴───────────┐
▼ ▼
.brain/db.sqlite .brain/graph.mmap
nodes · FTS · edges CSR neighborhoods
│ │
└───────────┬───────────┘
▼
query_ranked · context_for_prompt · graph · doctor
CLI: rustbrain (same engine, agent-friendly commands)
Repo / architecture: github.com/shan-alexander/rustbrain
Why embed rustbrain-core?
| Benefit | For your tool / agent |
|---|---|
| Structured retrieval | Ranked FTS + type filters + tag/alias boosts — not bag-of-files grep alone |
| Graph hops | From a note, expand CSR neighbors (ADRs → symbols, plans → concepts) |
| Token-budget packs | context_for_prompt* returns Markdown or XML sized for LLM prompts |
| Scaffolded capture | create_note writes type-aware templates agents can fill |
| Project hubs | README, CHANGELOG, optional ROADMAP/BACKLOG as stable node ids |
| Deps → docs.rs | Bootstrap harvests crates.io deps into reference notes with docs.rs URLs |
| Disposable index | Rebuild .brain/ anytime; truth stays in Markdown |
Install
[]
= "0.3"
# Smaller compile (no tree-sitter):
# rustbrain-core = { version = "0.3", default-features = false, features = ["obsidian", "mmap"] }
# Live re-index API:
# rustbrain-core = { version = "0.3", features = ["watch"] }
MSRV: 1.80 · License: MIT OR Apache-2.0 · C toolchain required (bundled SQLite; optional AST).
Getting started
Minimal: open, sync, search, pack
use ;
Bootstrap a mature repo (docs tree + AGENTS + crate docs)
use ;
Capture a note (scaffold), then re-index
use ;
Graph neighborhood & plan status search
use ;
Apply safe WikiLink rewrites (optional)
use ;
Core API surface
| Area | Entry points |
|---|---|
| Lifecycle | Brain::create · open · open_exact · open_or_create · sync |
| Search | query · query_ranked + QueryOptions::{human, no_symbols, include_types, type_boosts} |
| Context | context_for_prompt · context_for_prompt_with + ContextOptions · ContextBundle::{to_markdown, to_xml} |
| Graph | graph_neighborhood · graph_stats + GraphOptions / GraphDirection |
| Notes | create_note / Brain::note_new · NoteNewOptions · NodeType |
| Bootstrap | bootstrap_workspace · bootstrap_noninteractive · BootstrapOptions · agents templates |
| Crate docs | collect_crate_deps · write_crate_docs_notes · docs_rs_url |
| Health | run_doctor / run_doctor_with · DoctorReport |
| Links | list_orphan_notes · run_auto_link · apply_links · ApplyOptions |
| Portability | export / import brainbundles |
| Watch | Brain::watch (feature watch) |
| Hubs | detect_project_hub · HUB_CHANGELOG · is_release_intent · is_planning_intent |
| Plan status | densify_plan · PlanStatus · enrich_plan_index_fields |
Node types
NodeType |
String | Typical path / hub |
|---|---|---|
Goal |
goal |
docs/goals/, hub readme |
Adr |
adr |
docs/adr/ |
Alternative |
alternative |
docs/adr/ |
Concept |
concept |
docs/concepts/ |
Analysis |
analysis |
docs/analysis/ (dated digs) |
Plan |
plan |
docs/plans/, hubs roadmap / backlog |
Changelog |
changelog |
hub changelog ← root CHANGELOG.md |
Reference |
reference |
crate docs notes, external refs |
EdgeCase |
edge_case |
docs/edge_cases/ |
Symbol |
symbol |
AST-extracted code entities |
NodeType::parse accepts aliases such as roadmap / todo → Plan, analyses → Analysis.
Plan status densification (on sync)
For plan notes, Markdown status is projected 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) |
Sources: frontmatter status: / state:, ## Status, section headings, checkboxes ([ ] [/] [x] [~] [?] [!]).
Query & context nuances
QueryOptions::human()—no_symbols = trueso code noise does not drown notes.- Natural-language FTS — stopwords stripped; multi-token OR for recall (
why X not Y). ContextOptions— note-first seeds, body excerpts, ADR-first pack rank;hop_to_symbols(default true) still allows ADR → code.- Hub inject — empty/generic prompts pull README / harvest; release intent pulls
changelog; planning intent pullsroadmap/backlogwhen indexed. Brain::open— walks parent directories for.brain(useopen_exactfor a fixed path).
use ;
// Only ADRs and goals
let mut q = human;
q.include_types = vec!;
q.limit = 10;
// Larger pack, deeper hops, symbols as seeds too
let ctx_opts = ContextOptions ;
Bootstrap options
use ;
let report = bootstrap_workspace?;
for a in report.actions
Generated layout (typical):
| Path | Role |
|---|---|
docs/goals/, docs/adr/, docs/analysis/, docs/plans/, … |
Note trees |
AGENTS.md, docs/AGENTS.md |
Agent cookbooks |
docs/goals/from-readme.md |
Algorithmic README harvest |
docs/references/crates/*.md |
docs.rs URLs per crates.io dep |
docs/implementation/module-map.generated.md |
AST map (ast feature) |
.rustbrainignore |
Extra index skips |
Feature flags
| Feature | Default | Description |
|---|---|---|
ast |
✓ | Tree-sitter Rust symbol extraction |
obsidian |
✓ | WikiLinks, YAML frontmatter, Canvas |
mmap |
✓ | CSR graph.mmap reader/compiler |
watch |
Debounced filesystem watcher (notify) |
|
jshift |
Optional sparse JSON path helpers (not a serde_json replacement) | |
full |
All optional features |
JSON stack policy (when to use serde vs jshift): repo docs/JSON_STACK.md.
On-disk layout
| Path | Role |
|---|---|
.brain/db.sqlite |
Source of truth (nodes, edges, FTS5, pending links) |
.brain/graph.mmap |
CSR adjacency cache for hops |
.brain/link_lexicon.json |
Optional AC lexicon cache (apply --discover) |
docs/**/*.md |
Human/agent-authored knowledge (source of truth for prose) |
CHANGELOG.md |
Optional hub changelog |
Cargo.toml / Cargo.lock |
Input to docs.rs harvest on bootstrap |
Ignore: built-in target/, .git/, .brain/, … plus optional .rustbrainignore.
Line # rustbrain: import-gitignore merges root .gitignore. Env RUSTBRAIN_IMPORT_GITIGNORE=1 forces merge.
Schema / mmap details: docs/SCHEMA.md, docs/MMAP_FORMAT.md.
Mental model (storage)
Markdown / Rust on disk = source of truth (Git)
.brain/* = disposable index + densified projections
nodes.summary = skimmable (e.g. plan status line, latest changelog heading)
node_fts.content = full body + dense tokens (status:…, versions, …)
edges = WikiLinks, anchors, doc_links, auto_*
graph.mmap = hop topology only
Status checkboxes and changelog headings are not separate SQL task tables — they are densified into FTS/summary on sync so agents can query without a kanban product.
CLI companion
Install the same engine as a binary:
See rustbrain for the full command reference.
Related crates / docs
| Resource | Link |
|---|---|
| CLI crate | rustbrain |
| Source | shan-alexander/rustbrain |
| CLI book | docs/CLI.md |
| API docs | docs.rs/rustbrain-core |
License
MIT OR Apache-2.0