# juliusbrussee/cavemem architecture

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

- Interactive diagram: https://gitdiagram.com/juliusbrussee/cavemem
- Repository: https://github.com/juliusbrussee/cavemem

## Overview

1. **Repository shape**
   - This is a TypeScript monorepo built with `pnpm` workspaces.
   - The code is split into a few runtime apps (`apps/*`) and reusable packages (`packages/*`), plus docs, hooks scripts, CI, and eval assets.
   - The core product is local-first: memory is stored on disk, queried locally, and only optional enrichment can touch the network.

2. **Primary runtime flow**
   - The main lifecycle is: IDE hook event → privacy filtering → compression → persistent storage → later retrieval through MCP or the CLI.
   - Hooks capture session events from supported coding assistants, compress them into a compact “caveman” form, and write to local SQLite.
   - A separate worker can backfill embeddings, maintain indexes, and serve the local viewer.

3. **Capture layer**
   - `packages/hooks` contains the generic hook runner and handlers for lifecycle events such as session start/end, prompt submission, tool use, and stop.
   - `apps/cli` wires these hooks into specific IDEs, and `packages/installers` handles IDE-specific installation mechanics.
   - OpenCode is a special case: `apps/cli/src/opencode-bridge.ts` suggests a bridge plugin that maps OpenCode’s native events into the same hook handlers used elsewhere.

4. **Memory model and storage boundary**
   - `packages/core` holds the memory-store abstraction, ID utilities, and ranking logic.
   - `packages/storage` is the persistence layer, centered on SQLite schemas and storage adapters.
   - The README indicates SQLite FTS5 is used for keyword search, and the data model includes sessions and observations as the main persisted entities.

5. **Compression and privacy boundary**
   - `packages/compress` implements deterministic text compression and expansion, including tokenization, a lexicon, counting, and privacy-aware rewriting.
   - Private content is stripped before persistence, and path/secret filtering happens before data enters the store.
   - This package is also exposed as a CLI command for standalone file compression, so compression is both a runtime concern and a user-facing tool.

6. **Search and ranking**
   - Search is hybrid: `packages/core` includes a ranker, and the README describes a blend of SQLite BM25/FTS5 keyword search with vector similarity re-ranking.
   - The search path is exposed through both the CLI and MCP server, but the data always comes from the local store.
   - The system favors progressive disclosure: short search results first, full observations only when explicitly requested.

7. **Embeddings subsystem**
   - `packages/embedding` abstracts embedding generation behind providers.
   - The provider set includes local, Ollama, and OpenAI options, so embeddings are configurable but not required to depend on a cloud service.
   - The worker is responsible for embedding backfill and vector index maintenance, which keeps capture fast while deferring heavier work.

8. **Worker and viewer**
   - `apps/worker` is the background process that auto-spawns after the first hook if enabled.
   - It handles embedding generation, idle shutdown, and the local read-only viewer.
   - The viewer is exposed on `127.0.0.1:37777` by default, and `/api/*` is protected by a locally generated bearer token plus host/origin checks, so the browser UI and API are intentionally constrained to the local machine.

9. **MCP server**
   - `apps/mcp-server` provides the model context protocol surface over stdio.
   - It exposes the repository’s retrieval interface: `search`, `timeline`, `get_observations`, `list_sessions`, and optional `enrich`.
   - This is the query boundary for query-only IDEs such as Cursor, Gemini CLI, Antigravity, and IBM Bob, which do not capture their own sessions but can still search memory captured elsewhere.

10. **CLI as orchestration layer**
   - `apps/cli` is the main user entry point and ties together installation, configuration, status, search, compression, export/import, reindexing, worker control, viewer launch, and doctor checks.
   - It also handles bridge setup and IDE-specific wiring, so the CLI is the operational control plane for the rest of the system.
   - The presence of import/export commands shows an explicit offline transfer path for moving memory between machines without relying on any backend service.

11. **Configuration and home directory resolution**
   - `packages/config` centralizes defaults, schema validation, glob handling, and settings loading.
   - Settings live under a resolved cavemem home directory, with explicit support for `CAVEMEM_HOME`, XDG-style paths, and a user-configurable `dataDir`.
   - This matters architecturally because config determines where the SQLite store, embeddings, token file, and worker state live.

12. **IDE integration boundary**
   - `packages/installers` contains per-IDE installers for Claude Code, OpenCode, Codex, Copilot, Cursor, Gemini CLI, Augment, Antigravity, IBM Bob, and others.
   - The repo distinguishes between capture-capable IDEs, which install hooks or bridge plugins, and query-only IDEs, which only get MCP wiring.
   - That split is central to the architecture: capture and query are separate concerns, and not every client participates in both.

13. **Auxiliary tooling and quality gates**
   - `docs/` documents architecture, compression, development, and MCP behavior; `evals/` provides benchmark corpora and a bench runner.
   - The GitHub Actions workflows, changesets, and package-level tests indicate the repo is built as a release-managed library/tooling ecosystem rather than a single app.
   - The shell scripts under `hooks-scripts/` support IDE lifecycle execution outside the TypeScript packages.

14. **Overall mental model for a diagram**
   - Draw it as a local memory pipeline with three main surfaces: IDE hooks for write/capture, MCP/CLI for query/control, and a background worker for indexing/viewing.
   - The persistent center is SQLite-backed storage plus FTS/vector search, with compression and privacy filtering on the write path and ranker-assisted retrieval on the read path.
   - External services are optional and isolated: embedding providers can be local or remote, and `enrich` is opt-in; everything else is designed to stay on-device.

Main entry points: [`apps/cli/src/index.ts`](https://github.com/juliusbrussee/cavemem/blob/main/apps/cli/src/index.ts) (CLI), [`packages/storage/src/storage.ts`](https://github.com/juliusbrussee/cavemem/blob/main/packages/storage/src/storage.ts) (Storage), [`packages/hooks/src/index.ts`](https://github.com/juliusbrussee/cavemem/blob/main/packages/hooks/src/index.ts) (Hooks), [`apps/mcp-server/src/server.ts`](https://github.com/juliusbrussee/cavemem/blob/main/apps/mcp-server/src/server.ts) (MCP server)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_apps["Apps"]
  node_cli["CLI<br/>orchestrator<br/>[index.ts]"]
  node_hook_cmd["Hook cmd<br/>cli command<br/>[hook.ts]"]
  node_mcp_cmd["MCP cmd<br/>cli command<br/>[mcp.ts]"]
  node_search_cmd["Search cmd<br/>cli command<br/>[search.ts]"]
  node_compress_cmd["Compress cmd<br/>cli command<br/>[compress.ts]"]
  node_opencode_bridge["OpenCode bridge<br/>adapter<br/>[opencode-bridge.ts]"]
  node_mcp_server["MCP server<br/>stdio service<br/>[server.ts]"]
  node_enrich_tool["Enrich<br/>optional tool<br/>[enrich.ts]"]
  node_worker["Worker<br/>background service<br/>[server.ts]"]
  node_embed_loop["Embed loop<br/>job loop<br/>[embed-loop.ts]"]
  node_viewer["Viewer<br/>local UI<br/>[viewer.ts]"]
end

subgraph group_packages["Packages"]
  node_hooks_pkg["Hooks<br/>capture runtime<br/>[index.ts]"]
  node_storage[("Storage<br/>sqlite persistence<br/>[storage.ts]")]
  node_core["Core<br/>memory model<br/>[memory-store.ts]"]
  node_compress["Compress<br/>text codec<br/>[compress.ts]"]
  node_config["Config<br/>settings<br/>[loader.ts]"]
  node_embedding["Embedding<br/>provider layer<br/>[index.ts]"]
  node_installers["Installers<br/>[index.ts]"]
end

subgraph group_ops["Ops & docs"]
  node_docs["Docs & evals<br/>supporting assets<br/>[architecture.md]"]
end

node_cli -->|"dispatches"| node_hook_cmd
node_cli -->|"dispatches"| node_mcp_cmd
node_cli -->|"dispatches"| node_search_cmd
node_cli -->|"dispatches"| node_compress_cmd
node_cli -->|"orchestrates"| node_installers
node_cli -->|"loads"| node_config
node_cli -->|"controls"| node_worker
node_hook_cmd -->|"runs"| node_hooks_pkg
node_opencode_bridge -->|"adapts to"| node_hooks_pkg
node_installers -->|"installs"| node_hooks_pkg
node_hooks_pkg -->|"filters & compresses"| node_compress
node_hooks_pkg -->|"persists"| node_storage
node_storage -->|"serves"| node_core
node_core -->|"searches"| node_search_cmd
node_core -->|"retrieves"| node_mcp_server
node_mcp_server -->|"reads"| node_storage
node_mcp_server -.->|"optionally calls"| node_enrich_tool
node_worker -->|"runs"| node_embed_loop
node_embed_loop -->|"generates"| node_embedding
node_embed_loop -->|"updates"| node_storage
node_worker -->|"serves"| node_viewer
node_viewer -->|"queries"| node_storage
node_config -->|"locates"| node_storage
node_config -->|"configures"| node_worker
node_docs -.->|"describes"| node_compress
node_docs -.->|"describes"| node_mcp_server

click node_cli "https://github.com/juliusbrussee/cavemem/blob/main/apps/cli/src/index.ts"
click node_hook_cmd "https://github.com/juliusbrussee/cavemem/blob/main/apps/cli/src/commands/hook.ts"
click node_mcp_cmd "https://github.com/juliusbrussee/cavemem/blob/main/apps/cli/src/commands/mcp.ts"
click node_search_cmd "https://github.com/juliusbrussee/cavemem/blob/main/apps/cli/src/commands/search.ts"
click node_compress_cmd "https://github.com/juliusbrussee/cavemem/blob/main/apps/cli/src/commands/compress.ts"
click node_opencode_bridge "https://github.com/juliusbrussee/cavemem/blob/main/apps/cli/src/opencode-bridge.ts"
click node_mcp_server "https://github.com/juliusbrussee/cavemem/blob/main/apps/mcp-server/src/server.ts"
click node_enrich_tool "https://github.com/juliusbrussee/cavemem/blob/main/apps/mcp-server/src/enrich.ts"
click node_worker "https://github.com/juliusbrussee/cavemem/blob/main/apps/worker/src/server.ts"
click node_embed_loop "https://github.com/juliusbrussee/cavemem/blob/main/apps/worker/src/embed-loop.ts"
click node_viewer "https://github.com/juliusbrussee/cavemem/blob/main/apps/worker/src/viewer.ts"
click node_hooks_pkg "https://github.com/juliusbrussee/cavemem/blob/main/packages/hooks/src/index.ts"
click node_storage "https://github.com/juliusbrussee/cavemem/blob/main/packages/storage/src/storage.ts"
click node_core "https://github.com/juliusbrussee/cavemem/blob/main/packages/core/src/memory-store.ts"
click node_compress "https://github.com/juliusbrussee/cavemem/blob/main/packages/compress/src/compress.ts"
click node_config "https://github.com/juliusbrussee/cavemem/blob/main/packages/config/src/loader.ts"
click node_embedding "https://github.com/juliusbrussee/cavemem/blob/main/packages/embedding/src/index.ts"
click node_installers "https://github.com/juliusbrussee/cavemem/blob/main/packages/installers/src/index.ts"
click node_docs "https://github.com/juliusbrussee/cavemem/blob/main/docs/architecture.md"

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_hook_cmd,node_mcp_cmd,node_search_cmd,node_compress_cmd,node_opencode_bridge,node_mcp_server,node_enrich_tool,node_worker,node_embed_loop,node_viewer toneBlue
class node_hooks_pkg,node_storage,node_core,node_compress,node_config,node_embedding,node_installers toneAmber
class node_docs toneMint
```

## Components

### Apps

Runtime entrypoints and services

- [CLI](https://github.com/juliusbrussee/cavemem/blob/main/apps/cli/src/index.ts): orchestrator · Main user entrypoint for install, config, search, compression, export/import, reindex, and worker control · `apps/cli/src/index.ts`
- [Hook cmd](https://github.com/juliusbrussee/cavemem/blob/main/apps/cli/src/commands/hook.ts): cli command · CLI command that wires IDE hook execution and lifecycle handling · `apps/cli/src/commands/hook.ts`
- [MCP cmd](https://github.com/juliusbrussee/cavemem/blob/main/apps/cli/src/commands/mcp.ts): cli command · CLI command for launching and managing the local MCP surface · `apps/cli/src/commands/mcp.ts`
- [Search cmd](https://github.com/juliusbrussee/cavemem/blob/main/apps/cli/src/commands/search.ts): cli command · CLI search path over local memory · `apps/cli/src/commands/search.ts`
- [Compress cmd](https://github.com/juliusbrussee/cavemem/blob/main/apps/cli/src/commands/compress.ts): cli command · Standalone compression and expansion entrypoint · `apps/cli/src/commands/compress.ts`
- [OpenCode bridge](https://github.com/juliusbrussee/cavemem/blob/main/apps/cli/src/opencode-bridge.ts): adapter · Maps OpenCode-native events into shared hook handlers · `apps/cli/src/opencode-bridge.ts`
- [MCP server](https://github.com/juliusbrussee/cavemem/blob/main/apps/mcp-server/src/server.ts): stdio service · Query surface exposing search, timeline, observations, sessions, and optional enrich · `apps/mcp-server/src/server.ts`
- [Enrich](https://github.com/juliusbrussee/cavemem/blob/main/apps/mcp-server/src/enrich.ts): optional tool · Optional network-assisted enrichment path behind the MCP surface · `apps/mcp-server/src/enrich.ts`
- [Worker](https://github.com/juliusbrussee/cavemem/blob/main/apps/worker/src/server.ts): background service · Background process for embedding backfill, idle shutdown, and local viewer serving · `apps/worker/src/server.ts`
- [Embed loop](https://github.com/juliusbrussee/cavemem/blob/main/apps/worker/src/embed-loop.ts): job loop · Deferred embedding generation and index maintenance · `apps/worker/src/embed-loop.ts`
- [Viewer](https://github.com/juliusbrussee/cavemem/blob/main/apps/worker/src/viewer.ts): local UI · Read-only local browser viewer and API surface · `apps/worker/src/viewer.ts`

### Packages

Reusable subsystems

- [Hooks](https://github.com/juliusbrussee/cavemem/blob/main/packages/hooks/src/index.ts): capture runtime · Generic hook runner and lifecycle handlers for captured assistant events · `packages/hooks/src/index.ts`
- [Storage](https://github.com/juliusbrussee/cavemem/blob/main/packages/storage/src/storage.ts): sqlite persistence · SQLite-backed persistence layer and schemas for sessions and observations · `packages/storage/src/storage.ts`
- [Core](https://github.com/juliusbrussee/cavemem/blob/main/packages/core/src/memory-store.ts): memory model · Memory-store abstraction, IDs, and ranking logic · `packages/core/src/memory-store.ts`
- [Compress](https://github.com/juliusbrussee/cavemem/blob/main/packages/compress/src/compress.ts): text codec · Deterministic compression, expansion, tokenization, lexicon, and privacy rewriting · `packages/compress/src/compress.ts`
- [Config](https://github.com/juliusbrussee/cavemem/blob/main/packages/config/src/loader.ts): settings · Defaults, schema validation, glob handling, and home-directory resolution · `packages/config/src/loader.ts`
- [Embedding](https://github.com/juliusbrussee/cavemem/blob/main/packages/embedding/src/index.ts): provider layer · Pluggable local, Ollama, and OpenAI embedding providers · `packages/embedding/src/index.ts`
- [Installers](https://github.com/juliusbrussee/cavemem/blob/main/packages/installers/src/index.ts): IDE installers · Per-IDE installation mechanics and hook wiring for capture-capable clients · `packages/installers/src/index.ts`

### Ops & docs

Supporting docs, evals, and scripts

- [Docs & evals](https://github.com/juliusbrussee/cavemem/blob/main/docs/architecture.md): supporting assets · Architecture docs, compression docs, MCP docs, and benchmark corpus · `docs/architecture.md`

## Connections

- CLI → Hook cmd: dispatches · CLI exposes hook and lifecycle operations
- CLI → MCP cmd: dispatches · CLI launches the MCP server entrypoint
- CLI → Search cmd: dispatches · CLI provides local search and retrieval
- CLI → Compress cmd: dispatches · CLI exposes standalone compression tooling
- CLI → Installers: orchestrates · CLI installs and uninstalls IDE integrations
- CLI → Config: loads · CLI resolves settings and storage locations
- CLI → Worker: controls · CLI can start, stop, and inspect the worker
- Hook cmd → Hooks: runs · Hook command invokes shared lifecycle handlers
- OpenCode bridge → Hooks: adapts to · OpenCode bridge feeds native events into shared handlers
- Installers → Hooks: installs · Installers wire capture-capable IDEs into the hook runtime
- Hooks → Compress: filters & compresses · Write path performs privacy filtering and caveman compression
- Hooks → Storage: persists · Captured events are written to local storage
- Storage → Core: serves · Storage implements the memory-store boundary used by core search and retrieval
- Core → Search cmd: searches · CLI search uses core ranking over local memory
- Core → MCP server: retrieves · MCP queries are served through the shared memory model
- MCP server → Storage: reads · Retrieval comes from local persisted sessions and observations
- MCP server → Enrich: optionally calls · Optional enrichment can extend results when enabled
- Worker → Embed loop: runs · Worker delegates embedding backfill and index maintenance
- Embed loop → Embedding: generates · Embedding loop uses configured providers
- Embed loop → Storage: updates · Backfilled vectors and indexes are stored locally
- Worker → Viewer: serves · Worker hosts the local read-only viewer
- Viewer → Storage: queries · Viewer reads local memory for browsing
- Config → Storage: locates · Configuration determines the SQLite home and data directory
- Config → Worker: configures · Settings govern worker startup, token, and host constraints
- Docs & evals → Compress: describes · Documentation explains compression and privacy behavior
- Docs & evals → MCP server: describes · Documentation covers MCP query semantics
