Cortex Memory Engine
by Bunk-beds
README.md
# Cortex Memory Engine
Cortex is a self-hosted memory and pre-flight safety server for coding agents. It exposes a FastAPI REST API alongside Model Context Protocol (MCP) endpoints so agents can search past fixes, store new notes, and check risky shell commands before running them.
## Why I built it this way
Coding agents start every session from scratch. They forget environment quirks, repeat container flag bugs, and can run destructive commands if nothing sits between the model and the shell. I built Cortex to fix those specific problems without turning my homelab into a fragile stack of vector databases.
### Plain Markdown on disk, disposable SQLite in front
Every memory, architecture note, and troubleshooting entry lives as a plain Markdown file with YAML frontmatter (`cortex/vault.py`). The SQLite database (`notes_meta` and `notes_fts`) is strictly a rebuildable cache. If the index breaks or needs a schema change, `wipe_index()` drops it and rebuilds from the `.md` files on disk. A `watchfiles` loop (`cortex/watcher.py`) watches the vault directory so edits made by hand in a text editor or Obsidian show up in search automatically. Nothing is locked inside a proprietary store.
### FTS5 first, Ollama embeddings optional
Exact error strings, CLI flags, and port numbers are where pure vector search tends to miss. Cortex indexes every note in SQLite FTS5 using the `porter unicode61` tokenizer in WAL mode (`cortex/search.py`), so keyword lookups stay fast and need zero GPU memory. When an Ollama server is available, Cortex computes dense embeddings (`cortex/embeddings.py`) and merges them with BM25 scores through Reciprocal Rank Fusion (RRF). If Ollama is offline or turned off (`CORTEX_ENABLE_VECTORS=false`), search falls back to FTS5 instead of failing.
### Static regex guardrails instead of an LLM judge
Asking another language model whether a shell command is dangerous adds latency and still leaves room for prompt drift. `HazardInterceptor` (`cortex/interceptor.py`) runs static regex checks timed with `perf_counter_ns`, finishing in under a millisecond. It blocks destructive patterns like `rm -rf /`, `rm -rf ~`, `git push --force main`, piped `curl | bash`, and `pct`/`qm destroy`, and returns warnings for subtle traps such as `git reset --hard` or `docker run --gpus all` missing `--runtime=nvidia` on cgroup v2.
### Superseding old notes instead of deleting them
Configs change, and stale troubleshooting notes make agents repeat outdated workarounds. Calling `/api/supersede` marks the older note with `status: superseded` and a `superseded_by` reference, then down-ranks it during retrieval. Newer decisions surface first while the old history stays readable on disk.
## Quickstart (Docker)
```bash
cp .env.example .env
docker compose up -d --build
```
Check that the server is up:
```bash
curl http://localhost:8766/health
```
Markdown notes persist under `./data/vault` and the SQLite index under `./data/cache`.
## Local development
```bash
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
export CORTEX_VAULT_PATH=./data/vault
export CORTEX_CACHE_PATH=./data/cache/index.db
export CORTEX_ENABLE_VECTORS=false
uvicorn cortex.server:app --host 0.0.0.0 --port 8766
```
## Running tests
```bash
pip install pytest
python -m pytest -v
```
## API and MCP endpoints
| Endpoint | Method | Purpose |
|---|---|---|
| `/health` | `GET` | Service status, indexed note count, and Ollama availability |
| `/api/search` | `POST` | Hybrid BM25 and vector search with optional type filtering |
| `/api/read` | `GET` | Read a Markdown note by relative path |
| `/api/store` | `POST` | Write a new memory or troubleshooting note to the vault |
| `/api/supersede` | `POST` | Mark an older note as superseded by a newer one |
| `/api/check` | `POST` | Run a pre-flight hazard check on a shell command |
| `/sse` / `/mcp` | `GET`/`POST` | Model Context Protocol endpoints for coding agents |
## Configuration
See [`.env.example`](.env.example) for all environment variables (`CORTEX_OLLAMA_URL`, `CORTEX_ENABLE_VECTORS`, `CORTEX_EMBEDDING_MODEL`, `CORTEX_VAULT_PATH`, `CORTEX_CACHE_PATH`, `CORTEX_PORT`).
## License
MIT
This server cannot be deployed
Maintenance
ActivityMaintained
ResponsivenessNo recent community issues