mempalace — Memory and Context
Palace Hierarchy
Palace (palace_path)
└── Wings (people, projects — namespace level)
└── Rooms (topics within a wing)
└── Drawers (verbatim content units)
Drawers are the atomic storage unit. Each drawer contains the original verbatim text — no summarization, no paraphrasing.
Storage Backends
ChromaDB (default): Two collections per palace:
mempalace_drawers — primary drawer storage with embeddings
mempalace_closets — searchable index layer
Pluggable via BaseBackend ABC: Any backend that implements get_collection(palace_path, collection_name, create) can replace ChromaDB. backends/registry.py provides the factory lookup.
SQLite: ~/.mempalace/knowledge_graph.sqlite3 — temporal entity-relationship graph.
Plain text: ~/.mempalace/identity.txt — Layer 0 identity (~100 tokens).
Embedding Model
Default: embeddinggemma-300m — multilingual, 100+ languages, ~300 MB. Recommended via onboarding.
Alternative: all-MiniLM-L6-v2 — English-only, ~30 MB.
Local inference via mempalace/embedding.py. No API key required. ONNX int8 quantization for inference speed.
4-Layer Memory Stack
Defined in layers.py:
| Layer |
Tokens |
Loaded |
Source |
| L0 — Identity |
~100 |
Always |
~/.mempalace/identity.txt |
| L1 — Essential Story |
~500–800 |
Always |
Top moments from mempalace_drawers |
| L2 — On-Demand |
~200–500 each |
When topic arises |
Wing/room-scoped drawer retrieval |
| L3 — Deep Search |
Unlimited |
On request |
Full ChromaDB semantic search |
Wake-up cost (L0+L1): ~600–900 tokens total. Leaves 95%+ of context free.
Temporal Knowledge Graph
knowledge_graph.py — entity-relation triples with validity windows:
add_triple(entity, relation, target, valid_from, valid_to=None) — add a fact
query_entity(entity, as_of=None, direction="both") — query point-in-time or current
invalidate(entity, relation, target, ended) — mark fact as no longer valid
kg_timeline(entity) — full history of entity facts
Default path: ~/.mempalace/knowledge_graph.sqlite3. No external database required.
Tunnel System
Cross-wing shortcuts: mempalace_create_tunnel(from_wing, from_room, to_wing, to_room) creates a bidirectional navigation shortcut. mempalace_find_tunnels discovers relevant tunnels from any node. mempalace_follow_tunnels navigates via them.
Tunnels enable cross-project context: a drawer about JWT auth in the myapp wing can tunnel to the security-patterns wing without duplicating content.
Mining Sources
Project files: source code, docs, configs. Skips: .git, node_modules, pycache, .venv, dist, build, .next, coverage, and 15+ other build artifact directories.
Claude Code JSONL transcripts (--mode convos): Reads ~/.claude/projects/ JSONL files. v2 normalization strips system tags and hook chrome. Scope per project with --wing <project_name>.
Sweep (per-message): mempalace sweep <transcript_dir> — one drawer per user/assistant message. More granular than convo mining. Idempotent.
Agent Diaries
Each specialist agent gets a private diary in the palace. mempalace_diary_write appends entries; mempalace_diary_read retrieves them. Diaries are scoped per agent identity and persist across sessions. mempalace_list_agents discovers all agents with diaries — agents are not pre-declared in configuration.
Persistence Scope
Global: knowledge graph, identity, agent diaries.
Project-scoped: palace collections at palace_path.
Multiple palaces can be maintained simultaneously (e.g., one per project).
Normalization Version
NORMALIZE_VERSION = 2. Drawers mined with earlier normalization are rebuilt transparently on the next mine pass — no manual migration required. The version is stored in drawer metadata.