polypixel-memoir-core 0.4.0

Memoir memory substrate as an embeddable Rust library
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
//! Mapping extracted entity strings to canonical graph nodes.
//!
//! A [`Triple`](crate::graph::Triple)'s `subject` and `object` are bare strings
//! the extractor produced; two memories that mention the same real-world entity
//! under different surface forms ("Alice", "Alice Smith") must resolve to the
//! same graph node or the graph fragments into duplicates. [`EntityResolver`] is
//! the seam that maps a candidate string to either an existing node or a new
//! one.
//!
//! Two implementations ship: [`ExactStringResolver`] (the benchmark floor —
//! match on canonical name only) and [`EmbeddingEntityResolver`] (the
//! production impl — cosine similarity over candidate embeddings). Both partition
//! the resolution space by [`Scope`] so an entity in one tenant never merges with
//! a like-named entity in another.
//!
//! The candidates an embedding resolver compares against come from an
//! [`EntityCatalog`], a focused retrieval seam separate from
//! [`GraphStore`](crate::graph::GraphStore): the resolver needs only
//! "the entities I might merge with in this scope", not the full store surface.
//! [`InMemoryEntityCatalog`] is the always-available backing for tests and
//! benchmarks; a FalkorDB-backed catalog (reading the nodes the commit path
//! writes) is layered on in the commit ticket.

use std::collections::HashMap;
use std::future::Future;
use std::sync::Mutex;

use crate::embedding::{EmbeddingError, EmbeddingModel};
use crate::memory::Scope;

use super::cosine::cosine_similarity;

/// Minimum cosine similarity for two entity strings to be the same node.
///
/// Below this floor the surface forms are treated as distinct entities and a
/// new node is created. Mirrors the `MIN_CATEGORY_SCORE` precedent
/// (`client/categorize.rs`): a deliberately conservative default that favors a
/// new node over a wrong merge, since an over-eager merge is harder to undo than
/// a duplicate.
///
/// Chosen by measurement (`benches/resolver_accuracy.rs`, BGE-small-en-v1.5,
/// 2026-06-10): should-merge name variants scored ≥ 0.763 ("Alice Smith" vs
/// "Alice") while should-split distinct entities scored ≤ 0.718, so the floor
/// sits inside that band, biased toward the merge bound to keep favoring
/// splits. The original 0.85 guess false-split "Alice Smith" in the live 0014
/// suite.
pub const MIN_ENTITY_SIMILARITY: f32 = 0.75;

/// The outcome of resolving an entity string against the existing graph.
///
/// Tells the commit path whether to match an existing node or create one, and
/// carries the canonical name so surface-form variants collapse onto a single
/// node. A resolver that finds a match returns [`Resolution::Existing`] with the
/// matched node's stable key; otherwise [`Resolution::New`] with the name to
/// create.
#[derive(Debug, Clone, PartialEq)]
pub enum Resolution {
    /// The entity matched an existing node identified by `key`.
    Existing {
        /// The matched node's stable identity within its scope.
        key: String,
        /// The matched node's canonical name (may differ from the query string).
        name: String,
    },

    /// No existing node matched; a node should be created under `name`.
    New {
        /// The canonical name to create the node under.
        name: String,
    },
}

/// A candidate entity node the resolver compares a query against.
///
/// `key` is the node's stable identity within its scope; `name` is its canonical
/// surface form; `embedding` is the vector of `name`, used for cosine matching.
/// Yielded by an [`EntityCatalog`] for one scope.
#[derive(Debug, Clone, PartialEq)]
pub struct EntityVector {
    /// The node's stable identity within its scope.
    pub key: String,
    /// The node's canonical name.
    pub name: String,
    /// The embedding of `name`, for cosine comparison.
    pub embedding: Vec<f32>,
}

/// Yields the existing entity nodes a resolver may merge a candidate into.
///
/// A focused retrieval seam, deliberately narrower than
/// [`GraphStore`](crate::graph::GraphStore): the embedding resolver needs only
/// the candidate entities in one [`Scope`], not the full store surface. This is
/// also the seam where a FalkorDB-native vector index later replaces the linear
/// scan, behind the same trait, without changing the resolver.
pub trait EntityCatalog: Send + Sync + 'static {
    /// Returns the entity nodes within `scope` that a candidate may match.
    ///
    /// Scope-confined: implementations never yield entities from another scope,
    /// upholding the same tenant isolation the rest of memoir enforces.
    ///
    /// # Errors
    ///
    /// Returns [`ResolveError::Catalog`] when the backing store cannot be read.
    fn candidates_in_scope(
        &self,
        scope: &Scope,
    ) -> impl Future<Output = Result<Vec<EntityVector>, ResolveError>> + Send;
}

/// Maps an extracted entity string to a canonical graph node.
///
/// Implementations decide whether a candidate is an existing node or a new one,
/// always confined to the candidate's [`Scope`]. Swapping one implementation for
/// another (exact-string, embedding, or a future ANN-backed impl) requires no
/// caller change, which is what lets the benchmark compare them.
pub trait EntityResolver: Send + Sync + 'static {
    /// Resolves `entity` within `scope` to an existing or new node.
    ///
    /// # Errors
    ///
    /// Returns [`ResolveError::Catalog`] when candidate retrieval fails and
    /// [`ResolveError::Embed`] when the embedding model fails.
    fn resolve(&self, scope: &Scope, entity: &str) -> impl Future<Output = Result<Resolution, ResolveError>> + Send;
}

/// Failure modes for [`EntityResolver`] implementations.
#[derive(Debug, thiserror::Error)]
pub enum ResolveError {
    /// Reading candidate entities from the [`EntityCatalog`] failed.
    #[error("entity catalog read failed: {0}")]
    Catalog(String),

    /// Embedding the candidate entity string failed.
    #[error("entity embedding failed: {0}")]
    Embed(#[from] EmbeddingError),
}

/// Resolves entities by exact canonical-name match within a scope.
///
/// The benchmark floor: two surface forms of the same entity ("Alice", "Alice
/// Smith") resolve to *different* nodes because only an exact name match counts.
/// Useful as the baseline [`EmbeddingEntityResolver`] is measured against, and as
/// a zero-dependency resolver when embedding-based merging is not wanted.
pub struct ExactStringResolver<C> {
    catalog: C,
}

impl<C: EntityCatalog> ExactStringResolver<C> {
    /// Builds an exact-match resolver over `catalog`.
    pub fn new(catalog: C) -> Self {
        Self { catalog }
    }
}

impl<C: EntityCatalog> EntityResolver for ExactStringResolver<C> {
    async fn resolve(&self, scope: &Scope, entity: &str) -> Result<Resolution, ResolveError> {
        let candidates = self.catalog.candidates_in_scope(scope).await?;
        match candidates.into_iter().find(|candidate| candidate.name == entity) {
            Some(matched) => Ok(Resolution::Existing {
                key: matched.key,
                name: matched.name,
            }),
            None => Ok(Resolution::New {
                name: entity.to_string(),
            }),
        }
    }
}

/// Resolves entities by cosine similarity over candidate embeddings.
///
/// The production impl: embeds the candidate string and compares it against the
/// embeddings of existing nodes in the same scope, merging into the closest node
/// above [`MIN_ENTITY_SIMILARITY`]. This collapses surface-form variants onto one
/// node where the exact-string resolver would fragment them.
///
/// Candidate embeddings are stored as a node property and scanned exactly here;
/// a FalkorDB-native vector index can later replace the catalog's linear scan
/// behind the [`EntityCatalog`] seam without changing this resolver.
///
/// Generic over the embedder and catalog so tests inject stubs.
pub struct EmbeddingEntityResolver<E, C> {
    embedder: E,
    catalog: C,
    min_similarity: f32,
}

impl<E: EmbeddingModel, C: EntityCatalog> EmbeddingEntityResolver<E, C> {
    /// Builds a resolver over `embedder` and `catalog` with the default floor.
    pub fn new(embedder: E, catalog: C) -> Self {
        Self {
            embedder,
            catalog,
            min_similarity: MIN_ENTITY_SIMILARITY,
        }
    }

    /// Overrides the minimum cosine similarity for a match.
    #[must_use]
    pub fn with_min_similarity(mut self, min_similarity: f32) -> Self {
        self.min_similarity = min_similarity;
        self
    }
}

impl<E: EmbeddingModel, C: EntityCatalog> EntityResolver for EmbeddingEntityResolver<E, C> {
    async fn resolve(&self, scope: &Scope, entity: &str) -> Result<Resolution, ResolveError> {
        let query = self.embedder.embed(entity).await?;
        let candidates = self.catalog.candidates_in_scope(scope).await?;

        let best = candidates
            .into_iter()
            .filter_map(|candidate| cosine_similarity(&query, &candidate.embedding).map(|score| (score, candidate)))
            .filter(|(score, _)| *score >= self.min_similarity)
            .max_by(|(a, _), (b, _)| a.total_cmp(b));

        match best {
            Some((_, matched)) => Ok(Resolution::Existing {
                key: matched.key,
                name: matched.name,
            }),
            None => Ok(Resolution::New {
                name: entity.to_string(),
            }),
        }
    }
}

/// In-memory [`EntityCatalog`] for tests and benchmarks, with no live backend.
///
/// Holds entity vectors per scope so a resolver's matching logic runs against
/// real candidates rather than a faked query result. This is the catalog's
/// test/benchmark boundary, mirroring
/// [`InMemoryGraphStore`](crate::graph::InMemoryGraphStore).
///
/// # Examples
///
/// ```
/// use memoir_core::graph::{EntityCatalog, EntityVector, InMemoryEntityCatalog};
/// use memoir_core::memory::Scope;
///
/// # async fn run() -> Result<(), Box<dyn std::error::Error>> {
/// let scope = Scope { agent_id: "a".into(), org_id: "o".into(), user_id: "u".into() };
/// let catalog = InMemoryEntityCatalog::new();
/// catalog.insert(&scope, EntityVector { key: "e1".into(), name: "Alice".into(), embedding: vec![1.0, 0.0] });
///
/// let found = catalog.candidates_in_scope(&scope).await?;
/// assert_eq!(found.len(), 1);
/// # Ok(())
/// # }
/// ```
#[derive(Default)]
pub struct InMemoryEntityCatalog {
    by_scope: Mutex<HashMap<Scope, Vec<EntityVector>>>,
}

impl InMemoryEntityCatalog {
    /// Creates an empty catalog.
    pub fn new() -> Self {
        Self::default()
    }

    /// Adds `entity` to `scope`'s candidate set.
    pub fn insert(&self, scope: &Scope, entity: EntityVector) {
        self.by_scope
            .lock()
            .expect("entity catalog mutex poisoned")
            .entry(scope.clone())
            .or_default()
            .push(entity);
    }
}

impl EntityCatalog for InMemoryEntityCatalog {
    async fn candidates_in_scope(&self, scope: &Scope) -> Result<Vec<EntityVector>, ResolveError> {
        Ok(self
            .by_scope
            .lock()
            .expect("entity catalog mutex poisoned")
            .get(scope)
            .cloned()
            .unwrap_or_default())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn scope(user: &str) -> Scope {
        Scope {
            agent_id: "agent".to_string(),
            org_id: "org".to_string(),
            user_id: user.to_string(),
        }
    }

    /// Embeds a name to a fixed unit vector so cosine is deterministic in tests.
    ///
    /// "Alice" and "Alice Smith" map to near-identical vectors (the surface-form
    /// pair the embedding resolver must merge and the exact resolver must not);
    /// "Bob" maps to an orthogonal vector.
    struct FakeEmbedding;

    impl EmbeddingModel for FakeEmbedding {
        async fn embed(&self, text: &str) -> Result<Vec<f32>, EmbeddingError> {
            let vector = if text.starts_with("Alice") {
                vec![1.0, 0.0, 0.0]
            } else if text == "Bob" {
                vec![0.0, 1.0, 0.0]
            } else {
                vec![0.0, 0.0, 1.0]
            };
            Ok(vector)
        }

        fn dimensions(&self) -> usize {
            3
        }
    }

    async fn catalog_with_alice() -> InMemoryEntityCatalog {
        let catalog = InMemoryEntityCatalog::new();
        catalog.insert(
            &scope("u"),
            EntityVector {
                key: "alice-node".to_string(),
                name: "Alice".to_string(),
                embedding: vec![1.0, 0.0, 0.0],
            },
        );
        catalog
    }

    #[tokio::test(flavor = "current_thread")]
    async fn should_match_exact_name_with_exact_resolver() {
        let resolver = ExactStringResolver::new(catalog_with_alice().await);

        let resolution = resolver.resolve(&scope("u"), "Alice").await.unwrap();

        assert_eq!(
            resolution,
            Resolution::Existing {
                key: "alice-node".to_string(),
                name: "Alice".to_string(),
            }
        );
    }

    #[tokio::test(flavor = "current_thread")]
    async fn should_create_new_for_surface_variant_with_exact_resolver() {
        let resolver = ExactStringResolver::new(catalog_with_alice().await);

        let resolution = resolver.resolve(&scope("u"), "Alice Smith").await.unwrap();

        assert_eq!(
            resolution,
            Resolution::New {
                name: "Alice Smith".to_string(),
            }
        );
    }

    #[tokio::test(flavor = "current_thread")]
    async fn should_merge_surface_variant_with_embedding_resolver() {
        let resolver = EmbeddingEntityResolver::new(FakeEmbedding, catalog_with_alice().await);

        let resolution = resolver.resolve(&scope("u"), "Alice Smith").await.unwrap();

        assert_eq!(
            resolution,
            Resolution::Existing {
                key: "alice-node".to_string(),
                name: "Alice".to_string(),
            }
        );
    }

    #[tokio::test(flavor = "current_thread")]
    async fn should_create_new_for_dissimilar_entity_with_embedding_resolver() {
        let resolver = EmbeddingEntityResolver::new(FakeEmbedding, catalog_with_alice().await);

        let resolution = resolver.resolve(&scope("u"), "Bob").await.unwrap();

        assert_eq!(
            resolution,
            Resolution::New {
                name: "Bob".to_string()
            }
        );
    }

    #[tokio::test(flavor = "current_thread")]
    async fn should_not_merge_across_scopes() {
        let resolver = EmbeddingEntityResolver::new(FakeEmbedding, catalog_with_alice().await);

        let resolution = resolver.resolve(&scope("other"), "Alice").await.unwrap();

        assert_eq!(
            resolution,
            Resolution::New {
                name: "Alice".to_string()
            }
        );
    }
}