A selective memory layer for coding agents: distilled notes in a pgvector store and addressed messages between sessions, served through a single REST API and an MCP server.
Only distilled content is embedded. Agent notes arrive already distilled by the writing agent and pass deterministic checks (format, credentials such as API keys, tokens, PINs, and one-time codes, near duplicates); transcripts are not notes: the session record keeps Claude Code transcripts verbatim, read on demand and never ranked. Agent-authored messages — one-time signals and handoff snapshots — are stored as canonical Markdown, never embedded, listed while pending and consumed by claiming instead of search.
┌──────────────────────────────────────────────────────────────────────────────┐
│ CONSUMERS coding agents · scripts │
└───────┬──────────────────────────────────────────────────────────────────────┘
│ MCP (stdio | streamable HTTP :8765)
▼
┌─────────────┐ 18 tools: search_memory / save_memory / list_notes /
│ mcp_server │ read_notes / archive_notes / delete_notes /
└──────┬──────┘ send_message / list_messages / claim_message /
│ HTTP cancel_message / record_decision / retire_decision /
│ list_decisions / get_decision / list_sessions /
│ read_session / search_transcript / query_sql
│ (thin proxy, no logic)
▼
╔═══════════════════════════════════════════════════════════╗
║ REST API :8010 (the only backend) ║
╚═╤═══════════════════╤═══════════════════╤═════════════════╝
│ WRITE │ READ │ LIFECYCLE
▼ ▼ ▼
notes/store search/ notes/curation · messages/
│ │ │
└───────────────────┴─────────┬─────────┘
▼
Postgres 17 + pgvector + pg_textsearch :5439
memory_chunks · messages · namespaces · api_keys · decisions · session_events
side services: vLLM (embedding / rerank)
Every consumer reaches stored notes through the REST API, never through the database
directly. memory_chunks feeds search, messages holds addressed, once-claimed signals
that are never embedded and never searched
(ADR-0002). decisions holds each
author's numbered decisions per repository; their own routes and /recall read the caller's
decisions, and /sql reads every author's
(ADR-0016). session_events holds
each author's Claude Code transcript events; the session routes and their MCP tools read the
caller's events, /sql reads every author's, and /search and /recall never read them
(ADR-0017).
POST /sql runs one read-only statement as the database role memory_ro, which reads
memory_chunks, decisions, namespaces, session_events, and the messages columns other than
content, sender_key, and idempotency_key, never api_keys, across every author
(ADR-0018).
How notes and messages move through the system — the write pipeline, the hybrid-search read path, the archive lifecycle, and the storage schema — is documented in docs/data-flow.md.
| method | path | purpose |
|---|---|---|
GET |
/health |
liveness — 200 {status} whenever the process serves HTTP; reaches nothing outside it, and backs the container healthcheck |
GET |
/health/services |
dependency health — {status, checks:{db, embedding, rerank}}; 503 when any of them is down |
POST |
/search |
hybrid note search — query, top_k, min_score, kind, tags, author, namespaces, since/until, include_archived; the reranked results are capped at 10 before top_k applies; hits are headers (no note text); author omitted returns the calling author's own and shared notes, all every author's, a list of slugs exactly those authors' |
POST |
/save_memory |
store a distilled note, attributed to the X-Author header — content (at most 1,750 characters, its first line at most 160: the note's title), the required tags, the required kind (personal or work, a label for search and listing), the optional shared (a note every author's default read scope returns; set at creation only), the optional id of a prior note to archive (400 when the save would leave no active note), and an optional occurred_at (ISO 8601, the event's date, stored beside the save time); refused with 409 when a near-identical active note in the caller's default scope exists unless supersedes names it or allow_similar is set, and before embedding when the content or a tag carries a credential (the error names the credential type only) |
GET |
/notes |
list agent note headers (title, no text) newest-first without a query or embedding call — repeated tags and namespace params, kind (personal or work), repeated author (omitted: the caller's own and shared notes; all: every author; slugs: exactly those authors), since/until, include_archived, limit (default 50, max 200) |
GET |
/notes/read |
the full text of 1 to 5 agent notes — repeated id; each row is a /notes row plus text, in request order; any author and namespace, archived notes included; 400 for 0 or more than 5 ids, 404 naming the ids that are not agent notes |
POST |
/recall |
notes and the caller's active decisions for one query, scored separately — query (at most 4000 chars), optional scope (repo:<origin>; without it no decisions are scored), and notes / decisions objects each with top_k (at most 5; defaults 3 and 2) and min_score (defaults 0.5 and 0.5); the notes are /search hits, the decisions are headers carrying number, title, decided_on, and score; a failing lane is named in errors and the other still answers, both failing is 503 |
POST |
/session_events |
store a batch of Claude Code transcript events, attributed to the X-Author header — events (1 to 500 objects, the request body at most 2,000,000 bytes); each event has id (UUID), session_id, role (user, assistant, tool_call, or tool_result), content (at most 64,000 chars), optional tool_name and tool_use_id, event_at (ISO 8601), optional repo, and the required host and cwd; 200 {stored, known, conflicts}, where an id the caller already stored with the same payload is known and one stored with another payload is listed in conflicts; each author stores its own copy of an id; a body with author is a 400 |
GET |
/sessions |
the caller's sessions newest first by their last event — repo, since, until, limit (default 20, max 100); rows hold session_id, repo, host, cwd, started_at, last_at, events, and title (the first user message that does not open with a tag such as <command-name>, at most 120 chars) |
GET |
/sessions/{session_id} |
one of the caller's sessions in seq order — from_seq (default 1), limit (default 50, max 200), role, and contains (a case-insensitive substring of at least 2 chars, which returns each match with its neighbour on either side, the matches marked match: true); rows hold seq, role, tool_name, event_at, and content; a page also stops before 200,000 characters of content (it always holds one event), and next_from_seq names the seq to continue from while events remain, else null; 404 when the caller has no such session |
GET |
/session_events/search |
the caller's events whose content holds q (required, at least 2 chars) as a case-insensitive substring, newest first — repo, role, limit (default 20, max 50); each hit has session_id, seq, role, event_at, repo, and a 300-char window around the first match |
POST |
/decisions |
record a decision in a repository scope, attributed to the X-Author header — scope (repo:<origin>), title (at most 200 chars), decision, optional why, links (at most 10 strings of at most 500 chars), decided_on (ISO date, default today in UTC), and supersedes (the number of an active decision of the caller that it replaces); decision and why together are at most 1500 chars; 201 {number, superseded}; a supersedes that is not an active decision of the caller is a 400 and takes no number |
POST |
/decisions/retire |
retire an active decision of the caller — scope, number, reason; any other state is a 400 |
GET |
/decisions |
the caller's decisions in a scope, newest number first — scope, status (active, the default, or all), limit (default 200, max 200); rows hold number, title, decided_on, status |
GET |
/decisions/{number} |
one of the caller's decisions in full — scope; with supersedes, superseded_by, and retired_reason; 404 when the caller has no such number in the scope |
POST |
/messages |
send an addressed message (status info, no scope) or, with a scope (repo:<origin> or project:<organization>/<project>), a handoff snapshot (status in_progress|blocked|completed) — subject, result, optional next_step/verification/refs, optional idempotency_key and expires_at; attributed to the X-Author header; rendered to canonical Markdown, rejected past 16 KiB; an identical replay returns 200 |
GET |
/messages |
pending, unexpired messages newest-first without a query or embedding call — repeated namespace, purpose, scope, subject (case-insensitive substring of the normalized subject), limit (default 50, max 100) |
POST |
/messages/{id}/claim |
claim a pending message at most once — the loser of a race gets 409; a stale superseded or expired id gets 409, an unknown id a 404 |
DELETE |
/messages/{id} |
cancel a pending message — the 200 reply is the header row (id, namespace, purpose, scope, subject, status, author, created_at, expires_at), without content |
POST |
/sql |
run one read-only SQL statement as the memory_ro role — sql (one statement; any other field is a 400); 200 {columns, rows, truncated} with at most 200 rows and 50,000 characters in the whole reply, truncated true when rows were left out; non-JSON values (timestamps, UUIDs, numerics, vectors, bytes, NaN, ±Infinity) come back as strings; a write, a second statement, a privilege error, the 5 s statement timeout, a first row that alone exceeds the size cap, and a result type the client cannot decode are a 400 naming the reason; an unreachable database is a 503 database unavailable; not author-scoped |
POST |
/namespaces |
register a namespace — name (^[a-z0-9_-]{1,64}$) |
GET |
/namespaces |
list the registered namespaces |
DELETE |
/namespaces/{name} |
unregister a namespace with no notes or messages; the reserved default namespace cannot be deleted |
POST |
/admin/notes/delete |
preview (rows of id, title, date, archived), or delete with confirm; an id that is not an agent note makes the whole call a 400 that lists them as missing |
POST |
/admin/archive |
preview the agent notes named by ids (notes_to_archive), then archive them with confirm, stamping the header author on every row archived; a body without ids, or an id that is not an active note, is a 400 (the id case lists them as missing) |
DELETE |
/admin/sessions/{session_id} |
delete the caller's rows of one session; returns {deleted} |
POST |
/admin/messages/purge |
preview claimed, cancelled, superseded, and expired messages (messages_to_delete), then delete them with confirm (deleted); deletion is permanent and releases their idempotency keys |
The /admin/* routes are maintenance routes; any valid key may call them.
since and until are ISO 8601 dates or datetimes bounding a row's occurred_at, or
ts_last_active when none is recorded: a bare date covers that whole day, and naive values
are read as UTC.
A search hit is a header: the stored row's id (the note id supersedes takes), title, kind, and
tags, and shared: true when the note is shared; a note saved with supersedes carries the archived note's id as supersedes,
in hits and in GET /notes rows; a supersede save whose content matches an active note
archives the target without recording a pointer on the active note. The archived note
records the id of the note that replaced it as replaced_by. GET /notes rows also carry the lineage
fields they record: supersedes, archived_by, and replaced_by. date is the note's
occurred_at when recorded, else its save time. GET /notes rows carry the same date.
Each MCP tool is a thin proxy over the REST API, defined in its feature package's
tools.py (or, for query_sql, in the feature module serve/sql.py) and registered by mcp_server.py, which holds no logic of its own. Transport is
stdio by default; MCP_TRANSPORT=sse|streamable-http with MCP_HOST/MCP_PORT serves
over HTTP (Docker serves streamable HTTP on :8765/mcp).
search_memory · save_memory · list_notes · read_notes · archive_notes · delete_notes ·
send_message · list_messages · claim_message · cancel_message ·
record_decision · retire_decision · list_decisions · get_decision ·
list_sessions · read_session · search_transcript · query_sql
search_memory takes include_archived, kind, tags, since/until, and author.
list_notes reads note headers by filters alone — no query, no embedding call — for
deterministic reads like every note carrying one subject tag or a time window.
read_notes returns the full text of 1 to 5 notes by id, whatever their author or
namespace.
The author argument of search_memory and list_notes is three-way:
omitted returns the caller's own notes and shared notes, all every agent's notes, a slug
that agent's notes only.
Curation runs over the same tools: archive_notes (ids required; archived rows are
stamped with the header author) and delete_notes preview by default and act only with
confirm. Archiving records its author; deleting is permanent and records nothing.
The message lane is addressed, not searched: list_messages reads pending messages at
the start of a session and claim_message takes exclusive delivery of each one acted
on — never automatically. send_message publishes a general message or, with a scope,
a handoff snapshot; cancel_message withdraws a sender's own pending message. A
general message is operational and expires after MESSAGE_TTL_DAYS; a completed
handoff expires after 3 days and an in_progress or blocked one stays pending until it
is claimed, superseded, or cancelled, unless its sender gives an expires_at; a note
is durable knowledge.
The decision lane is separate from notes: record_decision (a supersede included) and
retire_decision run only when the owner asks; list_decisions and get_decision read a
repository's decisions of the caller's author. Note search never returns them.
The session record is read the same way: list_sessions lists the caller's sessions
(repo, since, until), read_session reads one in seq order or the matches of contains,
and search_transcript finds a literal substring in the caller's events. None of them is
memory and none is ranked.
uv sync
docker compose up -d --build db # pgvector + pg_textsearch on :5439
docker compose up -d --build api # REST backend on :8010
docker compose up -d --build mcp # MCP server, streamable HTTP on :8765
uv run python -m memory_base.serve.access.keys new <label> # prints a client's API key once| client | integration |
|---|---|
| Claude Code | the plugin in integrations/claude_code: /plugin marketplace add yw0nam/memory_layer, then /plugin install memory-base@memory_layer; it brings the SessionStart hook (the repo's handoff headers, its scope line and newest decisions), the UserPromptSubmit recall and Stop hooks, the session record upload on Stop, SessionEnd, and SessionStart (the transcript of each session goes to /session_events), the MCP server, the init and connect-agent skills, and the save-notes, send-handoff and record-decision skills |
| Hermes | an mcp_servers entry for the MCP server with the X-API-Key and X-Author headers |
The memory schema is created on first write (ensure_schema).
scripts/deploy.sh fast-forwards the main checkout, rebuilds the api and mcp
services, and waits until they report healthy. It refuses a checkout that is not on
main. An MCP client reconnects after the mcp container restarts.
scripts/backup.sh <backup-dir> [keep] writes a gzipped pg_dump of the database and
keeps the newest keep dumps (default 14). A daily dump is one crontab line:
0 4 * * * /path/to/memory_base/scripts/backup.sh /path/to/backups >> /path/to/backup.log 2>&1The database is the only state that needs backing up.
Every route except /health and /health/services requires an X-API-Key header
(ApiKeyAuthMiddleware in serve/access/auth.py); a missing, unknown, or revoked key gets a
fail-closed 401. The same routes require an X-Author header: the author slug the
client declares, matching ^[a-z0-9][a-z0-9-]{0,39}$; a missing, malformed, or reserved
(all) value gets a 400.
A key is a credential for the network boundary and grants nothing else: any valid key may
call every route, and every key is equal. Each client holds its own key, so one host or
agent is cut off by revoking its key alone. The label of a key names the client that
holds it.
Keys are provisioned with an operator CLI, not through the API:
uv run python -m memory_base.serve.access.keys new <label>
uv run python -m memory_base.serve.access.keys list
uv run python -m memory_base.serve.access.keys revoke <key-hash-prefix>new prints the plaintext key once, on its last line — only its sha256 hash is stored.
revoke takes an 8+ character prefix of the stored hash, as shown by list, and
revokes every active key matching it.
Every write — saved note, archive, message, decision, session event — is
attributed to the X-Author header's author. A note read returns the caller's own notes
and the notes marked shared unless it names an author
(ADR-0015).
A namespace is a registered label for filtering:
a write without namespace lands in default, a read without a namespace filter covers
every namespace, and nothing in a namespace is private.
Each agent gets its own key and author slug, sent as the X-Author header. The connect-agent skill
(integrations/claude_code/skills/connect-agent/SKILL.md)
walks through connecting and disconnecting an agent.
The MCP server needs the same headers: over streamable HTTP it forwards the caller's own
X-API-Key and X-Author, and over stdio (no inbound HTTP request to read them from) it
reads the MEMORY_API_KEY and MEMORY_AUTHOR environment variables instead.
.env (gitignored) holds every endpoint and credential. Required to boot: DB_URL,
POSTGRES_PASSWORD, DATA_ROOT, and the embedding/rerank vLLM endpoints
(EMB_URL/EMB_MODEL, RERANK_URL/RERANK_MODEL). The full variable reference and
optional tuning knobs are documented in docs/configuration.md.
uv run pytest # unit + integration (throwaway Postgres container, needs docker)
uv run pytest -m "not integration" # what CI runs
uv run ruff format --check . && uv run ruff check .
uv run python -m memory_base.eval.sandbox # real REST API on a throwaway Postgres; prints URL and key (runtime evidence)Work happens in a git worktree and lands via PR; main requires a PR and green CI (lint,
unit tests, test-guard, docs-guard, PR title). The docs-guard job and the PostToolUse edit
hook share scripts/ci/docs-guard.sh, which flags change-narrative vocabulary in markdown.
See AGENTS.md for the full contributor contract.