A deep-dive into the architecture of GBrain, the open-source "second brain" for personal AI agents created by Garry Tan (Y Combinator CEO). This is a reverse-engineering of code-level design decisions from the garrytan/gbrain repository, not a tutorial.
Key points
- Markdown-first: All data is plain Markdown files — no database blobs or proprietary formats. Content separates
compiled_truth(facts) fromtimeline(event sequence), letting search distinguish *what is known* from *when it was known*. Content hashes are computed over stable fields. - Six-layer ingestion funnel: (1) size gate (
MAX_FILE_SIZE = 5_000_000), (2) markdown parsing via marked lexer, (3) trust boundary — remote input hasquarantine/content_flag/embed_skipmarkers stripped, (4) content sanity gate, (5) hash + cross-slug deduplication, (6) chunking with fenced-code extraction and contextual retrieval wrapping. - Content sanity gate uses traffic-light disposition: red = quarantine (hidden from search but reviewable, never deleted), yellow = flagged as suspicious but searchable, green = pass through.
- Four-layer hybrid retrieval: pgvector HNSW → BM25 keyword → RRF fusion → knowledge graph traversal (+31.4 P@5). Each layer addresses a different failure mode: semantic similarity, exact matching, ranking fusion, and relational reasoning.
- Zero-LLM knowledge graph: entity relations (
founded,invested_at,works_at) are extracted via regex cascades against schema-declared types inferred from path prefixes (people/→person). No NER/LLM calls needed; the schema evolves with agent curation rather than a rigid ontology. - Think pipeline:
INTENT → GATHER → SYNTHESIZE → GAP. Intent is inferred from patterns (temporal/event/entity/general); synthesis is structured JSON output ({answer, citations, gaps}); missing LLM keys degrade gracefully instead of throwing — retrieval alone is still valuable. Trajectory injection (v0.40.2) handles temporal/knowledge-update questions with a 5s timeout, concurrency limit 3, andPromise.allSettledfault isolation. - Production reliability: sync locks prevent concurrent ingestion; spend caps proactively reject cost overruns; checkpoints allow resuming 10k-file syncs; identity dedup resolves overlapping import roots.
- Deployment topologies: single-machine (PGLite/Supabase), thin client over a remote brain via MCP, or split engines (code index vs. notes index) for isolation-as-security, switched via
~/.gbrain/config.jsonandGBRAIN_HOME. - Fail-closed trust model: only local trusted callers (sync/capture/dream) and the sanity gate can set content markers; remote MCP/OAuth callers cannot hide pages or inject flag text.
- Self-maintenance: 66 cron jobs handle link reconciliation, signal detection, "dream" synthesis, and contextual reindexing.
src/core/engine.ts,src/commands/sync.ts,src/commands/think.ts,src/core/think/index.ts,src/core/import-file.tsdocs/architecture/RETRIEVAL.md,docs/architecture/topologies.md,docs/ethos/ORIGIN.md,docs/what-schemas-unlock.md
Reference code
Conclusion
GBrain is not another RAG wrapper — it is a persistent memory layer for personal AI agents, built on the conviction that AI memory should be a human-readable, editable, version-controlled Markdown knowledge base rather than a black-box database.