# garrytan/gbrain architecture

> Architecture diagram of the GitHub repository garrytan/gbrain, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-19). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/garrytan/gbrain
- Repository: https://github.com/garrytan/gbrain

## Overview

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: [`src/core/engine.ts`](https://github.com/garrytan/gbrain/blob/master/src/core/engine.ts) (Brain engine), [`src/cli/main.ts`](https://github.com/garrytan/gbrain/blob/master/src/cli/main.ts) (CLI entry), [`src/mcp/server.ts`](https://github.com/garrytan/gbrain/blob/master/src/mcp/server.ts) (MCP server), [`src/core/ai/gateway.ts`](https://github.com/garrytan/gbrain/blob/master/src/core/ai/gateway.ts) (AI gateway)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entry["Agent access"]
  node_cli["CLI entry<br/>[main.ts]"]
  node_mcp["MCP server<br/>[server.ts]"]
  node_opcontract["Operation contract<br/>[contract.ts]"]
  node_options["CLI options<br/>[cli-options.ts]"]
end

subgraph group_knowledge["Memory and retrieval"]
  node_context["Context engine<br/>[context-engine.ts]"]
  node_session["Session state<br/>[session-state.ts]"]
  node_retrieval["Contextual retrieval"]
  node_synthesis["Answer synthesis"]
  node_graph["Typed knowledge graph<br/>[derived-links.ts]"]
  node_ingest["Page ingestion<br/>[ingest.ts]"]
  node_transcripts["Transcript ingestion<br/>[ingest.ts]"]
end

subgraph group_runtime["Core runtime"]
  node_engine["Brain engine<br/>[engine.ts]"]
  node_pglite["PGlite engine<br/>[pglite-engine.ts]"]
  node_postgres["Postgres engine<br/>[postgres-engine.ts]"]
  node_config["Configuration<br/>[config.ts]"]
  node_gateway["AI gateway<br/>[gateway.ts]"]
  node_schema["Schema migration"]
  node_identity["Persistence identity<br/>[identity.ts]"]
  node_access["Database access classification"]
  node_trust["Trust tiers<br/>[tier.ts]"]
  node_agentoutput["Agent response formatting<br/>[agent-output.ts]"]
  node_progress["Progress reporting<br/>[progress.ts]"]
  node_exit["CLI shutdown<br/>[cli-force-exit.ts]"]
end

subgraph group_operations["Operations and administration"]
  node_migration["Engine migration<br/>[migrate-engine.ts]"]
  node_doctor["Health checks<br/>[doctor.ts]"]
end

node_agent(("Agent or user"))
node_database[("Knowledge database")]
node_ai_provider{{"Configured AI provider"}}

node_agent -.->|"runs commands"| node_cli
node_agent -.->|"queries memory"| node_mcp
node_cli -->|"uses engine"| node_engine
node_mcp -->|"calls"| node_engine
node_mcp -->|"uses operations"| node_opcontract
node_opcontract -->|"defines engine context"| node_engine
node_engine -.->|"implemented by"| node_pglite
node_engine -.->|"implemented by"| node_postgres
node_pglite -.->|"reads and writes"| node_database
node_postgres -.->|"reads and writes"| node_database
node_cli -->|"loads configuration"| node_config
node_gateway -->|"loads settings"| node_config
node_gateway -.->|"invokes models"| node_ai_provider
node_context -->|"reads session source"| node_session
node_retrieval -->|"reads projections"| node_engine
node_agent -.->|"requests cited answers"| node_synthesis
node_agent -.->|"asks relationship questions"| node_graph
node_agent -.->|"adds knowledge"| node_ingest
node_agent -.->|"adds transcripts"| node_transcripts
node_schema -.->|"changes schema"| node_database
node_migration -->|"migrates data"| node_engine
node_doctor -->|"checks engine"| node_engine
node_cli -->|"parses flags"| node_options
node_cli -.->|"manages shutdown"| node_exit

click node_cli "https://github.com/garrytan/gbrain/blob/master/src/cli/main.ts"
click node_mcp "https://github.com/garrytan/gbrain/blob/master/src/mcp/server.ts"
click node_opcontract "https://github.com/garrytan/gbrain/blob/master/src/core/ops/contract.ts"
click node_engine "https://github.com/garrytan/gbrain/blob/master/src/core/engine.ts"
click node_pglite "https://github.com/garrytan/gbrain/blob/master/src/core/pglite-engine.ts"
click node_postgres "https://github.com/garrytan/gbrain/blob/master/src/core/postgres-engine.ts"
click node_config "https://github.com/garrytan/gbrain/blob/master/src/core/config.ts"
click node_gateway "https://github.com/garrytan/gbrain/blob/master/src/core/ai/gateway.ts"
click node_context "https://github.com/garrytan/gbrain/blob/master/src/core/context-engine.ts"
click node_session "https://github.com/garrytan/gbrain/blob/master/src/core/context/session-state.ts"
click node_retrieval "https://github.com/garrytan/gbrain/blob/master/src/core/contextual-retrieval-service.ts"
click node_synthesis "https://github.com/garrytan/gbrain/tree/master/src/core/think"
click node_graph "https://github.com/garrytan/gbrain/blob/master/src/core/derived-links.ts"
click node_ingest "https://github.com/garrytan/gbrain/blob/master/src/core/page-body/ingest.ts"
click node_transcripts "https://github.com/garrytan/gbrain/blob/master/src/core/transcripts/ingest.ts"
click node_schema "https://github.com/garrytan/gbrain/blob/master/src/core/schema-migrations/v248-segmented-page-storage.ts"
click node_migration "https://github.com/garrytan/gbrain/blob/master/src/commands/migrate-engine.ts"
click node_doctor "https://github.com/garrytan/gbrain/blob/master/src/commands/doctor.ts"
click node_identity "https://github.com/garrytan/gbrain/blob/master/src/core/persistence/identity.ts"
click node_access "https://github.com/garrytan/gbrain/blob/master/src/core/pg-access-classify.ts"
click node_trust "https://github.com/garrytan/gbrain/blob/master/src/core/trust/tier.ts"
click node_agentoutput "https://github.com/garrytan/gbrain/blob/master/src/core/agent-output.ts"
click node_progress "https://github.com/garrytan/gbrain/blob/master/src/core/progress.ts"
click node_options "https://github.com/garrytan/gbrain/blob/master/src/core/cli-options.ts"
click node_exit "https://github.com/garrytan/gbrain/blob/master/src/core/cli-force-exit.ts"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_cli,node_mcp,node_opcontract,node_options,node_agent toneBlue
class node_context,node_session,node_retrieval,node_synthesis,node_graph,node_ingest,node_transcripts,node_database toneAmber
class node_engine,node_pglite,node_postgres,node_config,node_gateway,node_schema,node_identity,node_access,node_trust,node_agentoutput,node_progress,node_exit toneMint
class node_migration,node_doctor toneRose
class node_ai_provider toneIndigo
```

## Components

### Agent access

- [CLI entry](https://github.com/garrytan/gbrain/blob/master/src/cli/main.ts): `src/cli/main.ts`
- [MCP server](https://github.com/garrytan/gbrain/blob/master/src/mcp/server.ts): `src/mcp/server.ts`
- [Operation contract](https://github.com/garrytan/gbrain/blob/master/src/core/ops/contract.ts): `src/core/ops/contract.ts`
- [CLI options](https://github.com/garrytan/gbrain/blob/master/src/core/cli-options.ts): `src/core/cli-options.ts`

### Memory and retrieval

- [Context engine](https://github.com/garrytan/gbrain/blob/master/src/core/context-engine.ts): `src/core/context-engine.ts`
- [Session state](https://github.com/garrytan/gbrain/blob/master/src/core/context/session-state.ts): `src/core/context/session-state.ts`
- [Contextual retrieval](https://github.com/garrytan/gbrain/blob/master/src/core/contextual-retrieval-service.ts): `src/core/contextual-retrieval-service.ts`
- [Answer synthesis](https://github.com/garrytan/gbrain/tree/master/src/core/think): `src/core/think`
- [Typed knowledge graph](https://github.com/garrytan/gbrain/blob/master/src/core/derived-links.ts): `src/core/derived-links.ts`
- [Page ingestion](https://github.com/garrytan/gbrain/blob/master/src/core/page-body/ingest.ts): `src/core/page-body/ingest.ts`
- [Transcript ingestion](https://github.com/garrytan/gbrain/blob/master/src/core/transcripts/ingest.ts): `src/core/transcripts/ingest.ts`

### Core runtime

- [Brain engine](https://github.com/garrytan/gbrain/blob/master/src/core/engine.ts): `src/core/engine.ts`
- [PGlite engine](https://github.com/garrytan/gbrain/blob/master/src/core/pglite-engine.ts): `src/core/pglite-engine.ts`
- [Postgres engine](https://github.com/garrytan/gbrain/blob/master/src/core/postgres-engine.ts): `src/core/postgres-engine.ts`
- [Configuration](https://github.com/garrytan/gbrain/blob/master/src/core/config.ts): `src/core/config.ts`
- [AI gateway](https://github.com/garrytan/gbrain/blob/master/src/core/ai/gateway.ts): `src/core/ai/gateway.ts`
- [Schema migration](https://github.com/garrytan/gbrain/blob/master/src/core/schema-migrations/v248-segmented-page-storage.ts): `src/core/schema-migrations/v248-segmented-page-storage.ts`
- [Persistence identity](https://github.com/garrytan/gbrain/blob/master/src/core/persistence/identity.ts): `src/core/persistence/identity.ts`
- [Database access classification](https://github.com/garrytan/gbrain/blob/master/src/core/pg-access-classify.ts): `src/core/pg-access-classify.ts`
- [Trust tiers](https://github.com/garrytan/gbrain/blob/master/src/core/trust/tier.ts): `src/core/trust/tier.ts`
- [Agent response formatting](https://github.com/garrytan/gbrain/blob/master/src/core/agent-output.ts): `src/core/agent-output.ts`
- [Progress reporting](https://github.com/garrytan/gbrain/blob/master/src/core/progress.ts): `src/core/progress.ts`
- [CLI shutdown](https://github.com/garrytan/gbrain/blob/master/src/core/cli-force-exit.ts): `src/core/cli-force-exit.ts`

### Operations and administration

- [Engine migration](https://github.com/garrytan/gbrain/blob/master/src/commands/migrate-engine.ts): `src/commands/migrate-engine.ts`
- [Health checks](https://github.com/garrytan/gbrain/blob/master/src/commands/doctor.ts): `src/commands/doctor.ts`

### Other components

- **Agent or user**
- **Knowledge database**
- **Configured AI provider**

## Connections

- Agent or user → CLI entry: runs commands (evidence: `README.md`)
- Agent or user → MCP server: queries memory (evidence: `README.md`)
- CLI entry → Brain engine: uses engine (evidence: `src/cli/main.ts`)
- MCP server → Brain engine: calls (evidence: `src/mcp/server.ts`)
- MCP server → Operation contract: uses operations (evidence: `src/mcp/server.ts`)
- Operation contract → Brain engine: defines engine context (evidence: `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 (evidence: `src/cli/main.ts`)
- AI gateway → Configuration: loads settings (evidence: `src/core/ai/gateway.ts`)
- AI gateway → Configured AI provider: invokes models
- Context engine → Session state: reads session source (evidence: `src/core/context-engine.ts`)
- Contextual retrieval → Brain engine: reads projections (evidence: `src/core/contextual-retrieval-service.ts`)
- Agent or user → Answer synthesis: requests cited answers (evidence: `README.md`)
- Agent or user → Typed knowledge graph: asks relationship questions (evidence: `README.md`)
- Agent or user → Page ingestion: adds knowledge (evidence: `README.md`)
- Agent or user → Transcript ingestion: adds transcripts (evidence: `README.md`)
- Schema migration → Knowledge database: changes schema
- Engine migration → Brain engine: migrates data (evidence: `src/commands/migrate-engine.ts`)
- Health checks → Brain engine: checks engine (evidence: `src/commands/doctor/checks/engine-fit.ts`)
- CLI entry → CLI options: parses flags (evidence: `src/cli/main.ts`)
- CLI entry → CLI shutdown: manages shutdown (evidence: `src/cli/main.ts`)
