|
| 1 | +# Contributing to Brain |
| 2 | + |
| 3 | +Thanks for considering a contribution. Brain has strong design |
| 4 | +constraints — read this end-to-end before you start. |
| 5 | + |
| 6 | +## TL;DR |
| 7 | + |
| 8 | +1. The [spec](spec/) is authoritative. Code disagreements get |
| 9 | + fixed in the code, not the spec. Spec changes go through |
| 10 | + the maintainer. |
| 11 | +2. Every sub-task: **read the spec → read the phase doc → write |
| 12 | + a plan in `.claude/plans/phase-NN-task-MM.md` → wait for |
| 13 | + approval → implement → verify → commit**. |
| 14 | +3. No `unwrap()` outside tests. Use `expect("invariant: |
| 15 | + <reason>")` for unreachable. |
| 16 | +4. Run `just verify` (or `cargo zigbuild --target |
| 17 | + x86_64-unknown-linux-gnu --workspace --tests` on macOS) |
| 18 | + before opening a PR. |
| 19 | + |
| 20 | +See [`AUTONOMY.md`](AUTONOMY.md) for the full operating |
| 21 | +contract Brain's autonomous mode runs under — much of it |
| 22 | +applies to human contributors too. |
| 23 | + |
| 24 | +## Architecture in one paragraph |
| 25 | + |
| 26 | +Linux server. Connection layer (Tokio) accepts TCP; each |
| 27 | +request dispatches to one of N **shards**. Each shard runs a |
| 28 | +**Glommio** executor (thread-per-core, io_uring) and owns its |
| 29 | +data: a memory-mapped **arena** for vectors, a **WAL** with |
| 30 | +O_DIRECT + `pwritev2(RWF_DSYNC)` group commit, a **redb** |
| 31 | +B-tree for metadata, an **HNSW** index in RAM. |
| 32 | +Single-writer-per-shard, lock-free reads via |
| 33 | +**ArcSwap** + **crossbeam-epoch**. When a schema is declared, |
| 34 | +the same shard additionally owns entity / statement HNSWs, |
| 35 | +two **tantivy** indexes, an LLM extractor cache, and runs the |
| 36 | +three-tier extractor pipeline. |
| 37 | + |
| 38 | +## Where to start reading |
| 39 | + |
| 40 | +- [`README.md`](README.md) — what Brain is + capability tour. |
| 41 | +- [`spec/00_master_overview/`](spec/00_master_overview/) — design |
| 42 | + start. |
| 43 | +- [`ROADMAP.md`](ROADMAP.md) — phase index. |
| 44 | +- [`CLAUDE.md`](CLAUDE.md) — operating rules + invariants. |
| 45 | +- [`docs/development/`](docs/development/) — contributor |
| 46 | + workflow. |
| 47 | + |
| 48 | +## Core invariants — DO NOT violate |
| 49 | + |
| 50 | +Code that violates these is wrong regardless of test results: |
| 51 | + |
| 52 | +1. **WAL-before-acknowledge.** No operation returns success |
| 53 | + until its WAL record is fsynced. |
| 54 | +2. **Single writer per shard.** No locks needed; the discipline |
| 55 | + enforces it. |
| 56 | +3. **CRC everywhere.** Every WAL record + arena slot. |
| 57 | +4. **Slot version on `MemoryId`.** Stale references → |
| 58 | + `NotFound`. |
| 59 | +5. **Idempotency by `RequestId`.** 24h TTL. Same params → |
| 60 | + cached response. Different params → `Conflict`. |
| 61 | +6. **Tombstone grace before reclamation.** Default 7 days. Hard |
| 62 | + FORGET zeroes immediately. |
| 63 | +7. **No silent corruption.** Fail-stop and alert. |
| 64 | + |
| 65 | +## Anti-patterns |
| 66 | + |
| 67 | +- Don't add Tokio inside a shard. Shards use Glommio. |
| 68 | +- Don't hold a lock across `.await`. |
| 69 | +- Don't allocate in the hot path (encode/recall serving). |
| 70 | +- Don't add `Send + Sync` to per-shard types. |
| 71 | +- Don't use `tokio::fs` in shard code. |
| 72 | +- Don't introduce a thread pool for parallel work. Sharding is |
| 73 | + the parallelism. |
| 74 | +- Don't trust user input. All wire input is untrusted. |
| 75 | +- Don't `panic!` on user-input errors. |
| 76 | + |
| 77 | +## Workflow |
| 78 | + |
| 79 | +### 1. Pick a sub-task |
| 80 | + |
| 81 | +The lowest-numbered unfinished sub-task in the active phase |
| 82 | +doc. Use `/next-task` if you're in Claude Code. |
| 83 | + |
| 84 | +### 2. Read the spec |
| 85 | + |
| 86 | +The spec section that section governs the work. Don't infer |
| 87 | +from the code if the spec covers it — read the spec. |
| 88 | + |
| 89 | +### 3. Plan |
| 90 | + |
| 91 | +Write `.claude/plans/phase-NN-task-MM.md` with: |
| 92 | +- Scope. |
| 93 | +- Spec references. |
| 94 | +- Architecture sketch. |
| 95 | +- Trade-offs considered. |
| 96 | +- Risks / open questions. |
| 97 | +- Test plan. |
| 98 | +- Commit shape. |
| 99 | +- Confirmation questions. |
| 100 | + |
| 101 | +Wait for approval before coding. This isn't ceremony — most |
| 102 | +mistakes are caught at the plan step. |
| 103 | + |
| 104 | +### 4. Implement |
| 105 | + |
| 106 | +Follow the plan. Deviations go back through plan → approval. |
| 107 | + |
| 108 | +### 5. Verify |
| 109 | + |
| 110 | +```bash |
| 111 | +just verify |
| 112 | +# or, on macOS: |
| 113 | +cargo zigbuild --target x86_64-unknown-linux-gnu --workspace --tests |
| 114 | +cargo clippy --workspace --all-targets -- -D warnings |
| 115 | +cargo fmt --check |
| 116 | +``` |
| 117 | + |
| 118 | +### 6. Commit |
| 119 | + |
| 120 | +One commit per sub-task. Commit subject: |
| 121 | + |
| 122 | +``` |
| 123 | +<type>(<scope>): <NN.MM> — <summary> |
| 124 | +``` |
| 125 | + |
| 126 | +Types: `feat`, `fix`, `refactor`, `test`, `docs`, `chore`, |
| 127 | +`perf`. |
| 128 | + |
| 129 | +**Never** add a `Co-Authored-By: Claude` trailer. The user is |
| 130 | +the sole author of these commits. |
| 131 | + |
| 132 | +## Code conventions |
| 133 | + |
| 134 | +- Edition: Rust 2021. MSRV: stable latest minus one. |
| 135 | +- Errors: `thiserror` for libs; `anyhow` for binaries. Stable |
| 136 | + error taxonomy per spec §03/10. |
| 137 | +- No `unwrap()` outside tests. Use `expect("invariant: |
| 138 | + <reason>")` for unreachable. |
| 139 | +- Public APIs: rustdoc + at least one example for non-trivial. |
| 140 | +- No `unsafe` outside `crates/brain-storage`. That crate needs |
| 141 | + it for mmap. Every `unsafe` block: `// SAFETY:` comment, |
| 142 | + smallest scope. |
| 143 | +- Formatting: rustfmt defaults. |
| 144 | +- Lints: clippy default warnings as errors in CI. Pedantic is |
| 145 | + aspirational; not enforced on stubs. |
| 146 | +- Naming: snake_case items, CamelCase types — Rust standard. |
| 147 | + |
| 148 | +## Testing |
| 149 | + |
| 150 | +- Unit tests colocated. |
| 151 | +- Integration tests in `tests/` per crate. |
| 152 | +- Property tests with `proptest` for parsers, allocators, |
| 153 | + recovery. |
| 154 | +- Fuzz with `cargo-fuzz` for the wire protocol. |
| 155 | +- Loom for concurrency-critical paths. |
| 156 | +- Miri for `crates/brain-storage`'s unsafe. |
| 157 | +- Chaos tests for recovery (kill-during-operation). |
| 158 | +- Benchmarks with `criterion` per phase. |
| 159 | + |
| 160 | +New behaviour → new test. Spec change → corresponding test |
| 161 | +change. |
| 162 | + |
| 163 | +## Reporting bugs / security issues |
| 164 | + |
| 165 | +- Functional bugs: open a GitHub issue with a reproducer. |
| 166 | +- Security issues: see [`SECURITY.md`](SECURITY.md). |
| 167 | + |
| 168 | +## License |
| 169 | + |
| 170 | +By contributing, you agree your contribution is licensed under |
| 171 | +the project's [Apache 2.0 license](LICENSE). |
0 commit comments