semantic-memory 0.5.0

Hybrid semantic search with SQLite, FTS5, and HNSW — built for AI agents
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
# 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]#what-it-provides
- [Current package status]#current-package-status
- [Install]#install
- [Quick start]#quick-start
- [Opening a store]#opening-a-store
- [Embedders]#embedders
- [Facts]#facts
- [Documents]#documents
- [Conversations]#conversations
- [Episodes]#episodes
- [Search]#search
- [Explainable ranking]#explainable-ranking
- [Graph view]#graph-view
- [Projection imports]#projection-imports
- [Configuration]#configuration
- [Feature flags]#feature-flags
- [Storage layout]#storage-layout
- [Integrity, repair, and re-embedding]#integrity-repair-and-re-embedding
- [Concurrency model]#concurrency-model
- [Operational notes]#operational-notes
- [Examples]#examples
- [Minimum supported Rust version]#minimum-supported-rust-version
- [License]#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:

```toml
[dependencies]
semantic-memory = "0.5"
```

Use exact brute-force vector search without HNSW:

```toml
[dependencies]
semantic-memory = { version = "0.5", default-features = false, features = ["brute-force"] }
```

At least one backend feature must be enabled:

- `hnsw`
- `brute-force`

The default runtime embedder talks to Ollama. For the default configuration,
start Ollama and pull the default embedding model:

```bash
ollama pull nomic-embed-text
ollama serve
```

For tests or custom providers, use `MemoryStore::open_with_embedder()` with an
implementation of the `Embedder` trait.

## Quick start

```rust
use semantic_memory::{MemoryConfig, MemoryStore};

#[tokio::main]
async fn main() -> Result<(), semantic_memory::MemoryError> {
    let store = MemoryStore::open(MemoryConfig::default())?;

    store
        .add_fact(
            "general",
            "Rust was first released in 2015",
            Some("release-notes"),
            None,
        )
        .await?;

    let results = store
        .search("when was Rust released?", Some(5), None, None)
        .await?;

    for result in results {
        println!("{:.4} {}", result.score, result.content);
    }

    Ok(())
}
```

A no-network test setup can use the deterministic mock embedder:

```rust
use semantic_memory::{MemoryConfig, MemoryStore, MockEmbedder};

fn test_store() -> Result<MemoryStore, semantic_memory::MemoryError> {
    let config = MemoryConfig::default();
    MemoryStore::open_with_embedder(config, Box::new(MockEmbedder::new(768)))
}
```

## Opening a store

`MemoryStore::open(config)` creates the storage directory, opens SQLite, runs
migrations, validates embedding metadata, and initializes the configured vector
backend.

```rust
use semantic_memory::{MemoryConfig, MemoryStore};
use std::path::PathBuf;

let config = MemoryConfig {
    base_dir: PathBuf::from("./agent-memory"),
    ..Default::default()
};

let store = MemoryStore::open(config)?;
```

`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:

```rust
pub trait Embedder: Send + Sync {
    fn embed<'a>(&'a self, text: &'a str) -> EmbedFuture<'a>;
    fn embed_batch<'a>(&'a self, texts: Vec<String>) -> EmbedBatchFuture<'a>;
    fn model_name(&self) -> &str;
    fn dimensions(&self) -> usize;
}
```

Built-in embedders:

- `OllamaEmbedder`: production default, calls `POST /api/embed`, batches
  according to `EmbeddingConfig::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.

```rust
let fact_id = store
    .add_fact(
        "project-alpha",
        "The ingestion worker owns transcript normalization.",
        Some("architecture-notes"),
        None,
    )
    .await?;

let fact = store.get_fact(&fact_id).await?;
let facts = store.list_facts("project-alpha", 50, 0).await?;

store
    .update_fact(&fact_id, "The ingestion worker owns transcript normalization and chunk hints.")
    .await?;

store.delete_fact(&fact_id).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.

```rust
let doc_id = store
    .ingest_document(
        "Runbook",
        "Long text to chunk, embed, and search...",
        "ops",
        Some("/docs/runbook.md"),
        None,
    )
    .await?;

let docs = store.list_documents("ops", 20, 0).await?;
let chunk_count = store.count_chunks_for_document(&doc_id).await?;

store.delete_document(&doc_id).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.

```rust
use semantic_memory::Role;

let session_id = store.create_session("repl").await?;

store
    .add_message(
        &session_id,
        Role::User,
        "What should we do next?",
        None,
        None,
    )
    .await?;

store
    .add_message_embedded(
        &session_id,
        Role::Assistant,
        "Prioritize the SQLite repair path before UI work.",
        None,
        None,
    )
    .await?;

let recent = store.get_recent_messages(&session_id, 16).await?;
let budgeted = store.get_messages_within_budget(&session_id, 2_000).await?;
let total_tokens = store.session_token_count(&session_id).await?;
let hits = store.search_conversations("SQLite repair", Some(5), None).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.

```rust
use semantic_memory::{EpisodeMeta, EpisodeOutcome, VerificationStatus};

let meta = EpisodeMeta {
    cause_ids: vec!["fact:abc".to_string()],
    effect_type: "regression_fix".to_string(),
    outcome: EpisodeOutcome::Confirmed,
    confidence: 0.92,
    verification_status: VerificationStatus::Verified {
        method: "integration-test".to_string(),
        at: "2026-05-07T00:00:00Z".to_string(),
    },
    experiment_id: Some("run-42".to_string()),
};

let doc_id = store
    .ingest_document(
        "Regression note",
        "The fix held under the regression suite.",
        "engineering",
        None,
        None,
    )
    .await?;

let episode_id = store
    .create_episode("episode-1", &doc_id, &meta)
    .await?;

let episodes = store
    .search_episodes(Some("regression_fix"), Some(&EpisodeOutcome::Confirmed), 5)
    .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:

```rust
use semantic_memory::SearchSourceType;

let namespaces = ["project-alpha", "project-beta"];
let sources = [SearchSourceType::Facts, SearchSourceType::Chunks];

let results = store
    .search("deployment rollback checklist", Some(10), Some(&namespaces), Some(&sources))
    .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_rank` and `vector_rank`: 1-based ranks before fusion.
- `bm25_contribution` and `vector_contribution`: weighted RRF components.
- `vector_source_rank` and `vector_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:

```rust
use semantic_memory::GraphDirection;

let graph = store.graph_view();

let edges = graph.neighbors("namespace:project-alpha", GraphDirection::Outgoing, 2)?;
let path = graph.path("fact:abc", "episode:def", 4)?;
```

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 importer `recorded_at`, query
  storage, scope filtering, and temporal filtering.

Canonical import path:

```rust
let receipt = store.import_projection_batch(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:

```rust
use semantic_memory::{
    ChunkingConfig, EmbeddingConfig, MemoryConfig, MemoryLimits, PoolConfig, SearchConfig,
};
use std::path::PathBuf;
use std::time::Duration;

let config = MemoryConfig {
    base_dir: PathBuf::from("./memory"),
    embedding: EmbeddingConfig {
        ollama_url: "http://localhost:11434".to_string(),
        model: "nomic-embed-text".to_string(),
        dimensions: 768,
        batch_size: 32,
        timeout_secs: 30,
    },
    search: SearchConfig {
        default_top_k: 8,
        candidate_pool_size: 80,
        bm25_weight: 1.0,
        vector_weight: 1.0,
        rrf_k: 60.0,
        min_similarity: 0.3,
        recency_half_life_days: Some(30.0),
        recency_weight: 0.5,
        rerank_from_f32: true,
    },
    chunking: ChunkingConfig {
        target_size: 1000,
        min_size: 100,
        max_size: 2000,
        overlap: 200,
    },
    pool: PoolConfig {
        busy_timeout_ms: 5000,
        wal_autocheckpoint: 1000,
        enable_wal: true,
        max_read_connections: 4,
        reader_timeout_secs: 30,
    },
    limits: MemoryLimits {
        max_facts_per_namespace: 100_000,
        max_chunks_per_document: 1_000,
        max_content_bytes: 1_048_576,
        max_embedding_concurrency: 8,
        max_db_size_bytes: 0,
        embedding_timeout: Duration::from_secs(30),
    },
    token_counter: None,
    ..Default::default()
};
```

Configuration is normalized and validated on open. Examples of normalization:

- `embedding.batch_size == 0` becomes 1.
- `embedding.timeout_secs == 0` becomes 1.
- `search.candidate_pool_size` is raised to at least `default_top_k`.
- `chunking.target_size` is clamped into `[min_size, max_size]`.
- `chunking.overlap` is kept below `min_size`.
- `limits.max_embedding_concurrency` is hard-capped at 32.
- `pool.reader_timeout_secs` is 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:

```bash
cargo test
cargo test --no-default-features --features brute-force
cargo test --all-features
```

## 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_map`
- `facts`, `facts_fts`, `facts_rowid_map`
- `documents`, `chunks`, `chunks_fts`, `chunks_rowid_map`
- `episodes`, `episodes_fts`, `episodes_rowid_map`
- `embedding_metadata`
- `hnsw_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:

```rust
use semantic_memory::{VerifyMode};

let report = store.verify_integrity(VerifyMode::Full).await?;
if !report.ok {
    for issue in report.issues {
        eprintln!("{issue}");
    }
}
```

Repair FTS state:

```rust
use semantic_memory::ReconcileAction;

let report = store.reconcile(ReconcileAction::RebuildFts).await?;
```

Re-embed all embedded records after changing models or dimensions:

```rust
let dirty = store.embeddings_are_dirty().await?;
if dirty {
    let updated_rows = store.reembed_all().await?;
    println!("re-embedded {updated_rows} rows");
}
```

`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:

```rust
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_blocking` for 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:

```bash
ollama pull nomic-embed-text
cargo run --example basic_search
```

Run the conversation example:

```bash
cargo run --example conversation_memory
```

Run tests:

```bash
cargo test
```

Run tests with the exact brute-force backend:

```bash
cargo test --no-default-features --features brute-force
```

## Minimum supported Rust version

`semantic-memory` declares `rust-version = "1.75"` and uses Rust 2021.

## License

Apache-2.0. See [LICENSE](LICENSE).