semantic-memory
Hybrid semantic search and durable local memory for Rust applications, backed by SQLite, FTS5, vector embeddings, and an optional recoverable HNSW sidecar.
semantic-memory is built for agents and local-first applications that need more
than a key-value cache. It stores facts, chunked documents, conversation
messages, causal episodes, and imported projection rows in SQLite, then exposes
retrieval APIs that combine lexical search, vector similarity, recency, graph
traversal, integrity checks, and repair workflows.
Contents
- What it provides
- Current package status
- Install
- Quick start
- Opening a store
- Embedders
- Facts
- Documents
- Conversations
- Episodes
- Search
- Explainable ranking
- Graph view
- Projection imports
- Configuration
- Feature flags
- Storage layout
- Integrity, repair, and re-embedding
- Concurrency model
- Operational notes
- Examples
- Minimum supported Rust version
- License
What it provides
- Durable SQLite storage for facts, documents, chunks, sessions, messages, episodes, embeddings, projection imports, and sidecar journals.
- SQLite FTS5 lexical search over facts, document chunks, messages, and episodes.
- Vector search over f32 embeddings, with optional quantized q8 storage for compact derived vectors.
- Hybrid ranking using Reciprocal Rank Fusion (RRF), configurable BM25/vector weights, optional recency weighting, and source filtering.
- Optional HNSW approximate nearest-neighbor acceleration through
hnsw_rs. - Brute-force vector search mode for exact cosine similarity or simpler deployments.
- Explainable search through
search_explained(), including BM25 rank, vector rank, exact/vector-rerank details, RRF contribution, and configured weights. - Conversation memory with session CRUD, message insertion, token budgets, and conversation-specific search.
- Document ingestion that chunks text, embeds all chunks, and commits the document plus chunks atomically.
- Causal episode storage with outcomes, confidence, verification status, and searchable episode text.
- Deterministic graph traversal over namespaces, facts, documents, chunks, sessions, messages, episodes, and semantic/temporal/causal/entity edges.
- Canonical projection import support for verified cross-crate data from
forge-memory-bridge, with scope-aware query surfaces for claims, relations, episodes, aliases, and evidence references. - Integrity verification and repair workflows for FTS state, malformed JSON, invalid enum values, embedding blob shape, quantized vectors, and HNSW sidecar drift.
- Local-first operation: SQLite is the source of truth; HNSW is only a recoverable acceleration sidecar.
Current package status
| Property | Value |
|---|---|
| Crate | semantic-memory |
| Current local version | 0.5.0 |
| Rust edition | 2021 |
| MSRV | Rust 1.75 |
| License | Apache-2.0 |
| Default vector backend | hnsw feature |
| Default embedder | Ollama /api/embed with nomic-embed-text |
| Default dimensions | 768 |
| Default storage directory | memory/ |
Install
Use the default HNSW-backed search path:
[]
= "0.5"
Use exact brute-force vector search without HNSW:
[]
= { = "0.5", = false, = ["brute-force"] }
At least one backend feature must be enabled:
hnswbrute-force
The default runtime embedder talks to Ollama. For the default configuration, start Ollama and pull the default embedding model:
For tests or custom providers, use MemoryStore::open_with_embedder() with an
implementation of the Embedder trait.
Quick start
use ;
async
A no-network test setup can use the deterministic mock embedder:
use ;
Opening a store
MemoryStore::open(config) creates the storage directory, opens SQLite, runs
migrations, validates embedding metadata, and initializes the configured vector
backend.
use ;
use PathBuf;
let config = MemoryConfig ;
let store = open?;
MemoryStore is cheap to clone. Clones share the same internal SQLite pool,
embedder, token counter, configuration, and HNSW sidecar state.
Embedders
The public Embedder trait uses boxed futures so callers can provide custom
async embedding providers without adding a crate-level async trait dependency:
Built-in embedders:
OllamaEmbedder: production default, callsPOST /api/embed, batches according toEmbeddingConfig::batch_size, validates HTTP status before parsing, and rejects non-numeric embedding values.MockEmbedder: deterministic, hash-seeded, normalized vectors for tests and offline development.
The embedder dimensions must match config.embedding.dimensions; store opening
fails with MemoryError::DimensionMismatch if they differ.
Facts
Facts are short durable knowledge records grouped by namespace.
let fact_id = store
.add_fact
.await?;
let fact = store.get_fact.await?;
let facts = store.list_facts.await?;
store
.update_fact
.await?;
store.delete_fact.await?;
Fact writes update SQLite, FTS, embeddings, optional quantized vectors, and HNSW sidecar journal entries transactionally. SQLite remains authoritative even if a sidecar flush is delayed.
Documents
Documents are split into chunks, embedded in batches, and committed atomically. If embedding any chunk fails, no document or chunk rows are written.
let doc_id = store
.ingest_document
.await?;
let docs = store.list_documents.await?;
let chunk_count = store.count_chunks_for_document.await?;
store.delete_document.await?;
Chunking uses the configured ChunkingConfig and the configured TokenCounter.
The default token counter is an estimate (text.len() / 4, minimum 1 for
non-empty text). Applications can supply their own tokenizer by setting
MemoryConfig::token_counter.
Conversations
Conversation memory is organized into sessions and messages.
use Role;
let session_id = store.create_session.await?;
store
.add_message
.await?;
store
.add_message_embedded
.await?;
let recent = store.get_recent_messages.await?;
let budgeted = store.get_messages_within_budget.await?;
let total_tokens = store.session_token_count.await?;
let hits = store.search_conversations.await?;
Message insertion can be plain, FTS-only, or embedded:
add_message(): stores the message and updates the session timestamp.add_message_fts(): stores the message and indexes it for FTS.add_message_embedded(): stores the message, FTS entry, embedding, q8 vector, and sidecar journal entry.
When token_count is None, the facade computes a token estimate with the
configured token counter before writing.
Episodes
Episodes represent causal or outcome-oriented records attached to memory state. They include cause IDs, effect type, outcome, confidence, verification status, optional experiment ID, and searchable episode text.
use ;
let meta = EpisodeMeta ;
let doc_id = store
.ingest_document
.await?;
let episode_id = store
.create_episode
.await?;
let episodes = store
.search_episodes
.await?;
Episode search participates in the same FTS/vector machinery as other search
sources when SearchSourceType::Episodes is included.
Search
The main search APIs are:
| Method | Embedding required | Searches | Use when |
|---|---|---|---|
search() |
Yes | Facts, chunks, episodes by default | General hybrid retrieval |
search_explained() |
Yes | Same as search() |
You need ranking diagnostics |
search_fts_only() |
No | FTS-backed sources | Ollama is offline or lexical search is enough |
search_vector_only() |
Yes | Embedded sources | Pure semantic similarity is desired |
search_conversations() |
Yes | Messages | Conversation recall |
search_episodes() |
No | Episode metadata | Filtered causal/outcome inspection |
Search supports namespace filtering and source-type filtering:
use SearchSourceType;
let namespaces = ;
let sources = ;
let results = store
.search
.await?;
FTS query strings are sanitized before they reach SQLite FTS5. Empty sanitized queries simply skip FTS instead of issuing an invalid FTS query.
Explainable ranking
search_explained() returns ExplainedResult, which contains the normal
SearchResult plus ScoreBreakdown.
Useful fields include:
rrf_score: final fused score.bm25_score: raw SQLite FTS5 BM25 score when present.vector_score: vector similarity used for final vector ordering.recency_score: optional recency contribution.bm25_rankandvector_rank: 1-based ranks before fusion.bm25_contributionandvector_contribution: weighted RRF components.vector_source_rankandvector_source_score: source vector retrieval data.vector_reranked_from_f32: whether HNSW hits were reranked with exact f32 cosine similarity from SQLite.
This is useful for tuning SearchConfig, explaining agent recall, debugging
stale embeddings, and verifying that source filters are doing what you expect.
Graph view
store.graph_view() exposes a deterministic graph traversal API derived from
SQLite state:
use GraphDirection;
let graph = store.graph_view;
let edges = graph.neighbors?;
let path = graph.path?;
Node IDs follow stable string forms such as:
namespace:<namespace>fact:<fact-id>document:<document-id>chunk:<chunk-id>session:<session-id>message:<message-id>episode:<episode-id>
Edge families include semantic, temporal, causal, and entity relationships.
Projection imports
The crate owns queryable projected truth for imported batches. Upstream crates own source truth and transformation:
semantic-memory-forge: evidence bundles and source export truth.forge-memory-bridge: transformation into projection import batches.semantic-memory: atomic import, authoritative importerrecorded_at, query storage, scope filtering, and temporal filtering.
Canonical import path:
let receipt = store.import_projection_batch.await?;
Supported projection read APIs:
query_projection_imports()query_projection_import_failures()latest_rebuildable_kernel_projection_import_for_scope()query_claim_versions()query_relation_versions()query_episodes()query_entity_aliases()query_evidence_refs()
ProjectionQuery carries the common filter surface:
scope- optional free-text query
- optional valid-time filter
- optional transaction-time cutoff
- optional subject/canonical entity filters
- optional claim filters
- result limit
Legacy V10 import surfaces are retained only under hidden, deprecated compatibility modules. New integrations should use the canonical projection batch import path.
Configuration
MemoryConfig is the top-level configuration:
use ;
use PathBuf;
use Duration;
let config = MemoryConfig ;
Configuration is normalized and validated on open. Examples of normalization:
embedding.batch_size == 0becomes 1.embedding.timeout_secs == 0becomes 1.search.candidate_pool_sizeis raised to at leastdefault_top_k.chunking.target_sizeis clamped into[min_size, max_size].chunking.overlapis kept belowmin_size.limits.max_embedding_concurrencyis hard-capped at 32.pool.reader_timeout_secsis capped at 300 seconds.
Feature flags
| Feature | Default | Description |
|---|---|---|
hnsw |
Yes | Enables the hnsw_rs sidecar for approximate nearest-neighbor retrieval. |
brute-force |
No | Enables exact brute-force vector retrieval. |
testing |
No | Exposes test-only helpers such as raw_execute(). |
Compile-time rule: at least one of hnsw or brute-force must be enabled.
Common build modes:
Storage layout
Given base_dir = "./memory", the crate creates and uses paths under that
directory for SQLite and, when enabled, HNSW sidecar files.
SQLite contains the durable source of truth:
sessions,messages,messages_fts,messages_rowid_mapfacts,facts_fts,facts_rowid_mapdocuments,chunks,chunks_fts,chunks_rowid_mapepisodes,episodes_fts,episodes_rowid_mapembedding_metadatahnsw_metadata,hnsw_keymap,pending_index_ops- projection import log, failures, claim versions, relation versions, entity aliases, evidence refs, and supporting projection tables
SQLite pragmas are configured for local concurrent use:
- WAL journal mode
- foreign keys enabled
- busy timeout
- normal synchronous mode
Integrity, repair, and re-embedding
Run verification:
use ;
let report = store.verify_integrity.await?;
if !report.ok
Repair FTS state:
use ReconcileAction;
let report = store.reconcile.await?;
Re-embed all embedded records after changing models or dimensions:
let dirty = store.embeddings_are_dirty.await?;
if dirty
reembed_all() covers facts, chunks, messages, and episodes. On completion it
clears the dirty flag and rebuilds HNSW when the hnsw feature is enabled.
HNSW maintenance APIs:
store.flush_hnsw?;
store.rebuild_hnsw_index.await?;
store.compact_hnsw.await?;
Concurrency model
The store uses:
- one writer connection for serialized SQLite writes,
- a configurable pool of reader connections,
- WAL mode for concurrent reads,
spawn_blockingfor SQLite work so database I/O does not block the async executor,- a semaphore around embedding calls to cap concurrent embedding requests,
- SQLite-journaled HNSW sidecar mutations so committed writes can be replayed after crashes or sidecar flush failures.
All public methods that touch SQLite route through the facade's connection helpers.
Operational notes
- SQLite is authoritative. HNSW can be rebuilt from SQLite.
- HNSW files are an acceleration sidecar, not the durable truth store.
- If the embedding model or dimensions change, the store marks embeddings dirty.
Search still works, but quality is degraded until
reembed_all()completes. search_fts_only()does not require Ollama or another embedding provider.search()and vector APIs require an embedder to be available.- Document ingestion is all-or-nothing for a document and its chunks.
- Fact, document, and message FTS updates are transactional with their source rows.
- Evidence payloads imported through projection paths remain opaque in normal retrieval; explicit query/audit paths can access evidence references.
- Compatibility import APIs are deprecated and hidden. They are retained for migration windows only.
Examples
Run the basic search example with Ollama:
Run the conversation example:
Run tests:
Run tests with the exact brute-force backend:
Minimum supported Rust version
semantic-memory declares rust-version = "1.75" and uses Rust 2021.
License
Apache-2.0. See LICENSE.