ContextBrain is the Knowledge Storage & RAG Service of the ContextUnity ecosystem. It owns vector search, knowledge graphs, and typed Conversation History on PostgreSQL or SQLite.
- RAG backends — store and retrieve knowledge for LLM applications
- Product catalogs — taxonomy, enrichment, and semantic search
- Memory systems — typed Conversation History and BrainCell memory for AI agents
- News aggregation — fact storage and deduplication
createdb brain
psql brain -c "CREATE EXTENSION IF NOT EXISTS vector;"
psql brain -c "CREATE EXTENSION IF NOT EXISTS ltree;"
uv run alembic upgrade headexport BRAIN_DATABASE_URL="postgres://user:pass@localhost:5432/brain"
export EMBEDDER_TYPE="openai"
export OPENAI_API_KEY="sk-..."
uv run python -m contextunity.brainuv run --package contextunity-brain pytestsrc/contextunity/brain/
├── service/ # gRPC service handlers & server
│ ├── handlers/ # Domain-specific: knowledge, memory, taxonomy, commerce, news, traces
│ └── interceptors.py # BrainPermissionInterceptor (RLS enforcement)
├── storage/postgres/ # PostgreSQL + pgvector (primary store)
│ ├── store/ # Modular mixins (search, graph, conversation history)
│ └── schema.py # DDL definitions
├── ingestion/rag/ # RAG pipeline & NLP processors
├── payloads.py # Pydantic validation models
└── core/ # Config, registry, interfaces
All modules follow the 400-Line Code Scale standard. The BrainService uses a mixin pattern for composable database operations.
| Variable | Default | Description |
|---|---|---|
BRAIN_DATABASE_URL |
— | PostgreSQL connection string (required) |
BRAIN_PORT |
50051 |
gRPC server port |
BRAIN_TENANTS |
— | Comma-separated allowed tenants |
EMBEDDER_TYPE |
auto | openai or local |
PGVECTOR_DIM |
1536 |
Vector dimensions (must match embedder) |
OPENAI_API_KEY |
— | Required for OpenAI embeddings |
REDIS_URL |
— | Embedding cache (optional, falls back to in-memory LRU) |
ContextBrain enforces multi-layer tenant isolation via PostgreSQL Row-Level Security (RLS). Every database operation sets session scope before query execution:
SET LOCAL app.current_tenant = '{tenant_id}';Only the brain_admin role can bypass RLS (used by Brain Admin / ContextForge dashboard access).
| Type | Storage | Description |
|---|---|---|
| Semantic | cells (pgvector / sqlite-vec) |
Long-term BrainCells with vector embeddings |
| Conversation History | conversation_records |
Typed bounded tenant/user/session history |
| BrainCells | cells (Postgres) |
Durable facts, preferences, and documentation |
| Embedding Cache | Redis (optional) | Avoids redundant API calls; falls back to LRU (2048 entries) |
- Full Documentation: ContextBrain on Astro Site
- Agent Boundaries & Golden Paths: AGENTS.md