nano-brain — Persistent Memory + Code Intelligence
A read-mostly knowledge graph + vector DB. Three call surfaces: HTTP, CLI (npx nano-brain ...), MCP tools. All operations require a workspace_hash that maps to one project root path.
TL;DR
# Bootstrap once per shell:
eval "$(npx nano-brain workspaces current --export)"
# Then query:
npx nano-brain query "how did we solve auth caching"
All CLI commands read $NANO_BRAIN_WORKSPACE so you do not need to pass --workspace=... after bootstrap. For HTTP/MCP direct calls, see Phase 3.
Phase 1 — DISCOVER (bootstrap)
Run these steps once per shell session. They are idempotent — safe to re-run.
1.1 Verify server is reachable
From a container (most agents):
curl -fsS http://host.docker.internal:3100/api/status | jq -r .pg_status
From the host directly:
curl -fsS http://localhost:3100/api/status | jq -r .pg_status
Success criterion: prints healthy.
Failure recovery: If curl fails, the server is not running. Recovery in order:
- On host:
npx nano-brain docker start - Ask the user to start the server. Do not try to start the server yourself inside a container — see "nano-brain Server Rule" in the project AGENTS.md.
1.2 Resolve the current workspace hash
Recommended — CLI bootstrap one-liner:
eval "$(npx nano-brain workspaces current --export)"
This sets NANO_BRAIN_WORKSPACE for the rest of the shell. All subsequent npx nano-brain ... commands pick it up implicitly.
HTTP fallback (for non-CLI agents):
HASH=$(curl -fsS -X POST http://host.docker.internal:3100/api/v1/workspaces/resolve \
-H 'Content-Type: application/json' \
-d "{\"path\":\"$PWD\"}" | jq -r .workspace_hash)
export NANO_BRAIN_WORKSPACE="$HASH"
The /api/v1/workspaces/resolve endpoint is read-only: it computes the deterministic SHA-256 hash from your absolute path and reports registered: true|false. It does NOT create the workspace.
1.3 Ensure the workspace is registered
npx nano-brain workspaces current --check 2>/dev/null \
|| npx nano-brain init --root "$PWD"
workspaces current --check exits 2 if the workspace is not yet registered. init --root <path> (space-separated flag; omit --yes, which only writes config and ignores --root) is idempotent — safe to run multiple times, and registers default collections (code, memory, sessions) + attaches the async file watcher (#161).
Success criterion: $NANO_BRAIN_WORKSPACE is set AND npx nano-brain workspaces current --check exits 0.
Phase 2 — SELECT (decision tree)
Map the user's intent to the right call:
| User intent | Operation | CLI | HTTP endpoint |
|---|---|---|---|
| "What did we decide about X?" | hybrid recall | query "X" | POST /api/v1/query |
| "Find exact term/identifier" | BM25 only | search "X" | POST /api/v1/search |
| "Semantic concept (no exact words)" | vector only | vsearch "X" | POST /api/v1/vsearch |
| "Across all my projects" | cross-workspace | query --scope=all "X" | POST /api/v1/query body {"workspace":"all", ...} |
| "Filter by tag (e.g. decisions)" | tagged search | query --tags=decision "X" | body adds "tags":["decision"] |
| "Save a decision/summary" | persist | write | POST /api/v1/write |
| "What does my workspace contain?" | briefing | wake-up | GET /api/v1/wake-up?workspace=$NANO_BRAIN_WORKSPACE |
| "Find function/type definition" | symbol lookup | get + code-intelligence | GET /api/v1/symbols?workspace=...&query=... |
| "Who calls FunctionName?" | graph traversal | (HTTP/MCP) | POST /api/v1/graph/query |
| "What breaks if I change Y?" | impact analysis | (HTTP/MCP) | POST /api/v1/graph/impact |
| "Trace call chain from entry" | call trace | (HTTP/MCP) | POST /api/v1/graph/trace |
| "List all tags with counts" | tag inventory | tags | GET /api/v1/tags?workspace=... |
Default for ambiguous "find X": use query (hybrid). It combines BM25 + vector + RRF + recency decay and gives the best results.
Phase 3 — EXECUTE
Every example below assumes Phase 1 has set $NANO_BRAIN_WORKSPACE.
3.1 Hybrid query — `POST /api/v1/query`
# CLI:
npx nano-brain query "redis caching for inv compression" --json
# HTTP:
curl -fsS -X POST http://host.docker.internal:3100/api/v1/query \
-H 'Content-Type: application/json' \
-d "{\"workspace\":\"$NANO_BRAIN_WORKSPACE\",\"query\":\"redis caching\",\"max_results\":10}"
Response: {"results":[{"source_path","excerpt","score","tags",...}, ...]}.
| Error | Meaning | Recovery |
|---|---|---|
| 400 workspace_required | Missing workspace in body | Re-run Phase 1.2 |
| 404 workspace_not_found / 400 workspace_not_registered | Hash valid but not in DB (#309) | Re-run Phase 1.3 (init --root "$PWD" or POST /api/v1/init) |
| 500 | Server error | Read nano-brain logs -n 50, retry once |
3.2 BM25 keyword search — `POST /api/v1/search`
Use when the query is an exact identifier / error message / function name.
npx nano-brain search "EmbedQueue.PendingCount"
3.3 Vector semantic search — `POST /api/v1/vsearch`
Use when the user describes a concept without exact terms.
npx nano-brain vsearch "ways to keep the embedding queue from overflowing"
3.4 Cross-workspace search
# CLI:
npx nano-brain query --scope=all "rate limiter pattern"
# HTTP:
curl -fsS -X POST http://host.docker.internal:3100/api/v1/query \
-H 'Content-Type: application/json' \
-d '{"workspace":"all","query":"rate limiter pattern"}'
The literal string "all" is allowed for read-only endpoints (query/search/vsearch). It is rejected by write endpoints (/api/v1/write, /api/v1/update) — those require a specific registered hash.
3.5 Persist a decision — `POST /api/v1/write`
# CLI:
npx nano-brain write \
--title="Decision: use RRF k=60 for hybrid search" \
--tags=decision,search \
--content='## Context\n...\n## Decision\n...'
# HTTP:
curl -fsS -X POST http://host.docker.internal:3100/api/v1/write \
-H 'Content-Type: application/json' \
-d "$(jq -n --arg ws "$NANO_BRAIN_WORKSPACE" \
'{workspace:$ws, source_path:"notes/decision-rrf.md", content:"# Decision\nUse k=60.", tags:["decision"]}')"
3.6 Workspace briefing — `wake-up`
Use at start of a new task to summarize what's in the workspace.
npx nano-brain wake-up
Returns {summary, recent_memories} — summary is a natural-language string with doc/collection counts and last-activity timestamp; recent_memories is an array of {id, title, snippet, tags, date} for the most recently written documents.