How garrytan/gbrain works
GBrain is a controllable, provenance-aware memory system for AI agents. Its main workflow is an agent request entering through MCP or the CLI, passing through operation handling to a configurable persistence engine, and returning retrieved knowledge; optional capabilities include contextual retrieval, synthesis, and a typed knowledge graph. The README documents these product capabilities, while the source excerpts and index support only selected internal connections. Dashed edges mark documented product behavior or relationships not verified in sampled source.
Main entry points: engine.ts Brain engine · main.ts CLI entry · server.ts MCP server · gateway.ts AI gateway
Components 28
Agent access
- CLI entry
src/cli/main.ts - MCP server
src/mcp/server.ts - Operation contract
src/core/ops/contract.ts - CLI options
src/core/cli-options.ts
Memory and retrieval
- Context engine
src/core/context-engine.ts - Session state
src/core/context/session-state.ts - Contextual retrieval
src/core/contextual-retrieval-service.ts - Answer synthesis
src/core/think - Typed knowledge graph
src/core/derived-links.ts - Page ingestion
src/core/page-body/ingest.ts - Transcript ingestion
src/core/transcripts/ingest.ts
Core runtime
- Brain engine
src/core/engine.ts - PGlite engine
src/core/pglite-engine.ts - Postgres engine
src/core/postgres-engine.ts - Configuration
src/core/config.ts - AI gateway
src/core/ai/gateway.ts - Schema migration
src/core/schema-migrations/v248-segmented-page-storage.ts - Persistence identity
src/core/persistence/identity.ts - Database access classification
src/core/pg-access-classify.ts - Trust tiers
src/core/trust/tier.ts - Agent response formatting
src/core/agent-output.ts - Progress reporting
src/core/progress.ts - CLI shutdown
src/core/cli-force-exit.ts
Operations and administration
- Engine migration
src/commands/migrate-engine.ts - Health checks
src/commands/doctor.ts
Other components
- Agent or user
- Knowledge database
- Configured AI provider
Connections 24
- Agent or user → CLI entry: runs commands
README.md - Agent or user → MCP server: queries memory
README.md - CLI entry → Brain engine: uses engine
src/cli/main.ts - MCP server → Brain engine: calls
src/mcp/server.ts - MCP server → Operation contract: uses operations
src/mcp/server.ts - Operation contract → Brain engine: defines engine context
src/core/ops/contract.ts - Brain engine → PGlite engine: implemented by
- Brain engine → Postgres engine: implemented by
- PGlite engine → Knowledge database: reads and writes
- Postgres engine → Knowledge database: reads and writes
- CLI entry → Configuration: loads configuration
src/cli/main.ts - AI gateway → Configuration: loads settings
src/core/ai/gateway.ts - AI gateway → Configured AI provider: invokes models
- Context engine → Session state: reads session source
src/core/context-engine.ts - Contextual retrieval → Brain engine: reads projections
src/core/contextual-retrieval-service.ts - Agent or user → Answer synthesis: requests cited answers
README.md - Agent or user → Typed knowledge graph: asks relationship questions
README.md - Agent or user → Page ingestion: adds knowledge
README.md - Agent or user → Transcript ingestion: adds transcripts
README.md - Schema migration → Knowledge database: changes schema
- Engine migration → Brain engine: migrates data
src/commands/migrate-engine.ts - Health checks → Brain engine: checks engine
src/commands/doctor/checks/engine-fit.ts - CLI entry → CLI options: parses flags
src/cli/main.ts - CLI entry → CLI shutdown: manages shutdown
src/cli/main.ts