How juliusbrussee/cavemem works
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: src/index.ts CLI · storage.ts Storage · src/index.ts Hooks · server.ts MCP server
Components 19
Apps
Runtime entrypoints and services
- CLI · Main user entrypoint for install, config, search, compression, export/import, reindex, and worker control
apps/cli/src/index.ts - Hook cmd · CLI command that wires IDE hook execution and lifecycle handling
apps/cli/src/commands/hook.ts - MCP cmd · CLI command for launching and managing the local MCP surface
apps/cli/src/commands/mcp.ts - Search cmd · CLI search path over local memory
apps/cli/src/commands/search.ts - Compress cmd · Standalone compression and expansion entrypoint
apps/cli/src/commands/compress.ts - OpenCode bridge · Maps OpenCode-native events into shared hook handlers
apps/cli/src/opencode-bridge.ts - MCP server · Query surface exposing search, timeline, observations, sessions, and optional enrich
apps/mcp-server/src/server.ts - Enrich · Optional network-assisted enrichment path behind the MCP surface
apps/mcp-server/src/enrich.ts - Worker · Background process for embedding backfill, idle shutdown, and local viewer serving
apps/worker/src/server.ts - Embed loop · Deferred embedding generation and index maintenance
apps/worker/src/embed-loop.ts - Viewer · Read-only local browser viewer and API surface
apps/worker/src/viewer.ts
Packages
Reusable subsystems
- Hooks · Generic hook runner and lifecycle handlers for captured assistant events
packages/hooks/src/index.ts - Storage · SQLite-backed persistence layer and schemas for sessions and observations
packages/storage/src/storage.ts - Core · Memory-store abstraction, IDs, and ranking logic
packages/core/src/memory-store.ts - Compress · Deterministic compression, expansion, tokenization, lexicon, and privacy rewriting
packages/compress/src/compress.ts - Config · Defaults, schema validation, glob handling, and home-directory resolution
packages/config/src/loader.ts - Embedding · Pluggable local, Ollama, and OpenAI embedding providers
packages/embedding/src/index.ts - 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 · Architecture docs, compression docs, MCP docs, and benchmark corpus
docs/architecture.md
Connections 26
- 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