vestige/tests/phase_1/send_bound_variant.rs
Jan De Landtsheer 5715f585fd feat(storage): phase 1 -- extract MemoryStore and Embedder traits (ADR 0001)
Introduce two trait boundaries that the rest of the stack now sits above,
landing Phase 1 of ADR 0001 (pluggable storage and network access).
Rebased onto v2.1.22 Sanhedrin from the original April work.

MemoryStore / LocalMemoryStore (crates/vestige-core/src/storage/memory_store.rs):
  One trait, ~25 methods, covering CRUD, hybrid / FTS / vector search,
  FSRS scheduling, graph edges, and the forthcoming domain surface.
  trait_variant::make generates a Send-bound MemoryStore alias over the
  base LocalMemoryStore so Arc<dyn MemoryStore> works under tokio/axum.
  Storage errors map through a dedicated MemoryStoreError.

Embedder / LocalEmbedder (crates/vestige-core/src/embedder/):
  Pluggable text-to-vector encoder. FastembedEmbedder wraps the existing
  EmbeddingService; storage never calls fastembed directly anymore.
  Embedder::signature() produces the ModelSignature consumed by the
  store's embedding_model registry.

SqliteMemoryStore (crates/vestige-core/src/storage/sqlite.rs):
  Storage renamed to SqliteMemoryStore; the old name lives on as a
  pub type alias so Arc<Storage> consumers in vestige-mcp stay intact.
  All existing inherent methods are untouched; the trait impl is
  purely additive and dispatches into them. The db_path field added
  by v2.1.1 portable-sync is preserved.

Migration V14 (crates/vestige-core/src/storage/migrations.rs):
  Renumbered from V12 (the original April number) to V14 to slot in
  cleanly after upstream's V12 (v2.1.1 sync_tombstones) and V13
  (v2.1.2 purge tombstones).
  - embedding_model registry table (CHECK id = 1, code enforces the
    single-row invariant).
  - knowledge_nodes.domains / domain_scores TEXT columns (JSON arrays
    default '[]' / '{}'), domains catalogue table, supporting indexes.
  Phase 4 populates these columns; Phase 1 just exposes the schema.

Consolidation and other cognitive pathways now accept a
&dyn LocalMemoryStore (sync) or Arc<dyn MemoryStore> (async) rather
than a concrete Storage.

Tests:
  - trait-method unit tests colocated in sqlite.rs and migrations.rs
  - embedder/fastembed.rs tests for name/dimension/hash stability
  - new integration crate tests/phase_1 (added to workspace members):
    trait_round_trip (8), embedding_model_registry (7),
    domain_column_migration (5), cognitive_module_isolation (4),
    send_bound_variant (2), embedder_trait (2).

Acceptance gate post-rebase:
  - cargo build --workspace --all-targets: ok
  - cargo clippy --workspace --all-targets -- -D warnings: clean
  - cargo test -p vestige-core --lib: 428 pass
  - cargo test -p vestige-phase-1-tests: 28 pass
  - cargo test -p vestige-mcp --lib: 380 pass (Storage alias preserves
    every existing call site)

Co-existence with v2.1.1 portable-sync: this trait extraction is
additive. Portable-sync's tombstone migrations (V12, V13) remain
on the concrete SqliteMemoryStore; Phase 2 (Postgres) will decide
which of those surfaces graduate into the trait.
2026-06-18 19:07:52 -05:00

99 lines
3.2 KiB
Rust

//! Phase 1 integration tests: Arc<dyn MemoryStore> moves across tokio::spawn.
//!
//! This verifies that `#[trait_variant::make(MemoryStore: Send)]` actually
//! produces a Send-bound future so Arc<dyn MemoryStore> is movable.
use chrono::Utc;
use std::sync::Arc;
use tempfile::tempdir;
use uuid::Uuid;
use vestige_core::storage::{MemoryRecord, MemoryStore, SqliteMemoryStore};
fn make_store() -> Arc<dyn MemoryStore> {
let dir = tempdir().unwrap();
let db = dir.path().join("send_test.db");
std::mem::forget(dir);
Arc::new(SqliteMemoryStore::new(Some(db)).expect("create"))
}
fn make_record(content: &str) -> MemoryRecord {
MemoryRecord {
id: Uuid::new_v4(),
domains: vec![],
domain_scores: Default::default(),
content: content.to_string(),
node_type: "fact".to_string(),
tags: vec![],
embedding: None,
created_at: Utc::now(),
updated_at: Utc::now(),
metadata: serde_json::json!({}),
}
}
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn arc_dyn_memory_store_moves_across_tokio_tasks() {
let store: Arc<dyn MemoryStore> = make_store();
let mut handles = Vec::new();
for t in 0..16usize {
let store = Arc::clone(&store);
let handle = tokio::spawn(async move {
for i in 0..10usize {
let rec = make_record(&format!("task {t} memory {i}"));
store.insert(&rec).await.expect("insert in spawned task");
}
});
handles.push(handle);
}
for h in handles {
h.await.expect("task completed without panic");
}
let count = store.count().await.expect("count");
assert_eq!(count, 160, "all 16*10 inserts must be counted");
}
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn concurrent_readers_one_writer() {
let store: Arc<dyn MemoryStore> = make_store();
// Pre-populate with some data so readers have something to find
for i in 0..10usize {
let rec = make_record(&format!("concurrent reader memory {i}"));
store.insert(&rec).await.expect("pre-insert");
}
let mut handles = Vec::new();
// 32 concurrent readers
for _ in 0..32usize {
let store = Arc::clone(&store);
let handle = tokio::spawn(async move {
let results = store.fts_search("concurrent reader", 5).await;
// Should not panic even if results vary due to concurrent writes
results.expect("fts_search in concurrent reader");
});
handles.push(handle);
}
// 1 writer inserting more records
{
let store = Arc::clone(&store);
let writer_handle = tokio::spawn(async move {
for i in 0..20usize {
let rec = make_record(&format!("writer record {i}"));
store.insert(&rec).await.expect("concurrent insert");
}
});
handles.push(writer_handle);
}
for h in handles {
h.await.expect("no panics");
}
// Eventual consistency check: total count should be at least 10 (initial)
let count = store.count().await.expect("final count");
assert!(
count >= 10,
"at least the pre-populated records must persist"
);
}