mentedb 0.32.0

A purpose-built database engine for AI agent memory
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
//! Embedder self-calibration for cosine-based cognitive thresholds.
//!
//! Absolute cosine thresholds are embedder-coupled: the same paraphrase pair
//! lands near 0.67 on one embedder and 0.80 on another, so a constant like
//! 0.97 silently disables deduplication on every real embedder (measured in
//! production, and the same failure previously disabled value-update
//! supersession at 0.88). Instead of hand-tuning constants per embedder, this
//! module embeds a small built-in probe set once per embedder and derives the
//! operating thresholds from the measured score distributions. Calibration is
//! persisted next to the database keyed by model and dimension, so it runs
//! once per embedder and self-heals on any embedder swap.
//!
//! Derivation is distribution-relative, never absolute: the dedup cosine sits
//! just below the paraphrase distribution while staying above measured
//! unrelated noise, and the topical floor (the loosest gate the value-update
//! rule uses) is the unrelated noise ceiling itself. When an embedder cannot
//! separate paraphrase from unrelated (a hash or degenerate provider),
//! calibration reports failure and callers keep their configured defaults.

use std::path::{Path, PathBuf};

use mentedb_core::error::{MenteError, MenteResult};
use mentedb_embedding::provider::EmbeddingProvider;
use serde::{Deserialize, Serialize};
use tracing::info;

/// Version of the built-in probe set. Bump when the probe sentences change so
/// stored calibrations recompute instead of describing an older probe set.
pub const PROBE_VERSION: u32 = 1;

/// Minimum separation between the paraphrase p25 and unrelated p95 for the
/// embedder to count as calibratable. Below this the distributions overlap and
/// derived thresholds would be noise.
const MIN_SEPARATION: f32 = 0.05;

/// How far below the paraphrase p25 the dedup cosine sits, so the bulk of the
/// paraphrase distribution clears the gate rather than only its upper half.
const PARAPHRASE_MARGIN: f32 = 0.05;

/// How far above the unrelated p95 the dedup cosine must stay, so measured
/// noise never counts as a paraphrase even when the margins collide.
const NOISE_MARGIN: f32 = 0.02;

/// Sentence pairs that state the same fact in different words. Dedup must
/// catch these.
pub(crate) const PARAPHRASE_PAIRS: &[(&str, &str)] = &[
    (
        "The user prefers dark mode in every editor",
        "The user likes their editors set to dark mode",
    ),
    (
        "Standup is at 9:30 every weekday morning",
        "Every weekday the standup happens at 9:30 in the morning",
    ),
    (
        "The production database runs Postgres 16",
        "Postgres 16 is what the production database runs",
    ),
    (
        "Sarah leads the payments team",
        "The payments team is led by Sarah",
    ),
    (
        "Deploys go out on Friday afternoons",
        "The team ships deploys Friday afternoon",
    ),
    (
        "The user is allergic to peanuts",
        "Peanuts trigger the user's allergy",
    ),
    (
        "The API rate limit is one hundred requests per minute",
        "Clients may send at most one hundred requests a minute to the API",
    ),
    (
        "The user's favorite coffee is a latte",
        "A latte is the user's coffee of choice",
    ),
    (
        "Error budgets reset on the first of the month",
        "On the first of each month the error budget resets",
    ),
    (
        "The mobile app is written in Kotlin",
        "Kotlin is the language of the mobile app",
    ),
    (
        "Meetings are documented in the shared wiki",
        "The shared wiki holds the meeting notes",
    ),
    (
        "The user commutes by bicycle on dry days",
        "On dry days the user bikes to work",
    ),
];

/// Sentence pairs with no semantic relationship. Their cosine distribution is
/// the embedder's noise floor.
pub(crate) const UNRELATED_PAIRS: &[(&str, &str)] = &[
    (
        "The user prefers dark mode in every editor",
        "The quarterly tax filing is due in April",
    ),
    (
        "Standup is at 9:30 every weekday morning",
        "The cat needs a rabies booster next year",
    ),
    (
        "Postgres 16 is what the production database runs",
        "Flights to Lisbon are cheapest in February",
    ),
    (
        "Sarah leads the payments team",
        "The garage door opener needs new batteries",
    ),
    (
        "Deploys go out on Friday afternoons",
        "Basil grows best with six hours of sunlight",
    ),
    (
        "The user is allergic to peanuts",
        "The staging cluster autoscaled to four nodes",
    ),
    (
        "The API rate limit is one hundred requests per minute",
        "Grandma's birthday dinner is at the Italian place",
    ),
    (
        "The user's favorite coffee is a latte",
        "The linter forbids unused imports in CI",
    ),
    (
        "Error budgets reset on the first of the month",
        "The marathon route crosses two bridges",
    ),
    (
        "The mobile app is written in Kotlin",
        "Rent is due on the third of the month",
    ),
    (
        "Meetings are documented in the shared wiki",
        "The aquarium filter hums when it needs cleaning",
    ),
    (
        "The user commutes by bicycle on dry days",
        "Invoices over five thousand need a second approval",
    ),
    (
        "The retro board uses three columns",
        "The user's daughter plays goalkeeper on Saturdays",
    ),
    (
        "Feature flags live in the settings service",
        "The soup needs more salt near the end of cooking",
    ),
];

/// Thresholds derived from an embedder's measured score distributions.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EmbedderCalibration {
    /// Embedder model the calibration describes.
    pub model: String,
    /// Embedding dimension the calibration describes.
    pub dimensions: usize,
    /// Probe set version the calibration was computed against.
    pub probe_version: u32,
    /// 25th percentile of paraphrase-pair cosines.
    pub paraphrase_p25: f32,
    /// 95th percentile of unrelated-pair cosines (the noise ceiling).
    pub unrelated_p95: f32,
    /// Operating cosine gate for paraphrase deduplication.
    pub dedup_cosine: f32,
    /// Loosest topical gate: text less similar than this is noise. Used as
    /// the value-update similarity floor.
    pub topical_floor: f32,
}

/// Nearest-rank percentile of an unsorted sample.
fn percentile(values: &mut [f32], q: f32) -> f32 {
    if values.is_empty() {
        return 0.0;
    }
    values.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
    let idx = ((values.len() - 1) as f32 * q).round() as usize;
    values[idx.min(values.len() - 1)]
}

fn cosine(a: &[f32], b: &[f32]) -> f32 {
    if a.len() != b.len() || a.is_empty() {
        return 0.0;
    }
    let dot: f32 = a.iter().zip(b).map(|(x, y)| x * y).sum();
    let na: f32 = a.iter().map(|x| x * x).sum::<f32>().sqrt();
    let nb: f32 = b.iter().map(|x| x * x).sum::<f32>().sqrt();
    if na == 0.0 || nb == 0.0 {
        return 0.0;
    }
    dot / (na * nb)
}

/// Embed the probe set and derive operating thresholds. Errors when the
/// embedder fails or cannot separate paraphrase from unrelated noise; callers
/// keep their configured defaults in that case.
pub fn calibrate(embedder: &dyn EmbeddingProvider) -> MenteResult<EmbedderCalibration> {
    let texts: Vec<&str> = PARAPHRASE_PAIRS
        .iter()
        .chain(UNRELATED_PAIRS.iter())
        .flat_map(|(a, b)| [*a, *b])
        .collect();
    let vectors = embedder.embed_batch(&texts)?;
    if vectors.len() != texts.len() {
        return Err(MenteError::Index(format!(
            "calibration embed returned {} vectors for {} probes",
            vectors.len(),
            texts.len()
        )));
    }

    let pair_cosines = |offset: usize, count: usize| -> Vec<f32> {
        (0..count)
            .map(|i| cosine(&vectors[offset + 2 * i], &vectors[offset + 2 * i + 1]))
            .collect()
    };
    let mut para = pair_cosines(0, PARAPHRASE_PAIRS.len());
    let mut unrel = pair_cosines(PARAPHRASE_PAIRS.len() * 2, UNRELATED_PAIRS.len());

    let paraphrase_p25 = percentile(&mut para, 0.25);
    let unrelated_p95 = percentile(&mut unrel, 0.95);

    if paraphrase_p25 < unrelated_p95 + MIN_SEPARATION {
        return Err(MenteError::Index(format!(
            "embedder does not separate paraphrase from noise (paraphrase p25 {paraphrase_p25:.3}, unrelated p95 {unrelated_p95:.3}); keeping configured thresholds"
        )));
    }

    let dedup_cosine = (paraphrase_p25 - PARAPHRASE_MARGIN).max(unrelated_p95 + NOISE_MARGIN);
    Ok(EmbedderCalibration {
        model: embedder.model_name().to_string(),
        dimensions: embedder.dimensions(),
        probe_version: PROBE_VERSION,
        paraphrase_p25,
        unrelated_p95,
        dedup_cosine,
        topical_floor: unrelated_p95,
    })
}

/// File the calibration persists to, keyed by probe version, model, and
/// dimension so an embedder swap recomputes instead of reusing stale numbers.
fn calibration_path(dir: &Path, model: &str, dimensions: usize) -> PathBuf {
    let slug: String = model
        .chars()
        .map(|c| if c.is_ascii_alphanumeric() { c } else { '_' })
        .collect();
    dir.join(format!(
        ".embedder_calibration_v{PROBE_VERSION}_{slug}_{dimensions}.json"
    ))
}

/// Load a stored calibration for this embedder, or compute and persist one.
/// A failed save still returns the computed calibration; a failed computation
/// propagates so the caller can fall back to configured defaults.
pub fn load_or_calibrate(
    dir: &Path,
    embedder: &dyn EmbeddingProvider,
) -> MenteResult<EmbedderCalibration> {
    let path = calibration_path(dir, embedder.model_name(), embedder.dimensions());
    if let Ok(raw) = std::fs::read_to_string(&path)
        && let Ok(cal) = serde_json::from_str::<EmbedderCalibration>(&raw)
        && cal.probe_version == PROBE_VERSION
        && cal.dimensions == embedder.dimensions()
    {
        return Ok(cal);
    }
    let cal = calibrate(embedder)?;
    match serde_json::to_string_pretty(&cal) {
        Ok(json) => {
            if let Err(e) = std::fs::write(&path, json) {
                tracing::warn!(error = %e, "calibration computed but not persisted");
            }
        }
        Err(e) => tracing::warn!(error = %e, "calibration serialize failed"),
    }
    info!(
        model = %cal.model,
        dedup_cosine = cal.dedup_cosine,
        topical_floor = cal.topical_floor,
        "embedder calibration computed"
    );
    Ok(cal)
}

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

    /// Deterministic provider mapping each probe text to a designed vector:
    /// both halves of a paraphrase pair share one basis vector, unrelated
    /// halves get mutually orthogonal vectors.
    struct DesignedProvider {
        vectors: HashMap<String, Vec<f32>>,
        dims: usize,
    }

    impl DesignedProvider {
        fn separable() -> Self {
            let dims = 2 * (PARAPHRASE_PAIRS.len() + 2 * UNRELATED_PAIRS.len());
            let mut vectors = HashMap::new();
            let basis = |i: usize| -> Vec<f32> {
                let mut v = vec![0.0; dims];
                v[i] = 1.0;
                v
            };
            for (i, (a, b)) in PARAPHRASE_PAIRS.iter().enumerate() {
                // Same direction with a slight tilt: cosine near but below 1.
                let va = basis(i);
                let mut vb = basis(i);
                vb[dims - 1] = 0.3;
                vectors.insert((*a).to_string(), va);
                vectors.entry((*b).to_string()).or_insert(vb);
            }
            let base = PARAPHRASE_PAIRS.len();
            for (j, (a, b)) in UNRELATED_PAIRS.iter().enumerate() {
                vectors
                    .entry((*a).to_string())
                    .or_insert_with(|| basis(base + 2 * j));
                vectors
                    .entry((*b).to_string())
                    .or_insert_with(|| basis(base + 2 * j + 1));
            }
            Self { vectors, dims }
        }

        /// Every text maps to the same vector: paraphrase and unrelated
        /// cosines are all 1.0, which no threshold can separate.
        fn degenerate() -> Self {
            let mut vectors = HashMap::new();
            for (a, b) in PARAPHRASE_PAIRS.iter().chain(UNRELATED_PAIRS.iter()) {
                vectors.insert((*a).to_string(), vec![1.0, 0.0]);
                vectors.insert((*b).to_string(), vec![1.0, 0.0]);
            }
            Self { vectors, dims: 2 }
        }
    }

    impl EmbeddingProvider for DesignedProvider {
        fn embed(&self, text: &str) -> MenteResult<Vec<f32>> {
            Ok(self
                .vectors
                .get(text)
                .cloned()
                .unwrap_or_else(|| vec![0.0; self.dims]))
        }

        fn embed_batch(&self, texts: &[&str]) -> MenteResult<Vec<Vec<f32>>> {
            texts.iter().map(|t| self.embed(t)).collect()
        }

        fn dimensions(&self) -> usize {
            self.dims
        }

        fn model_name(&self) -> &str {
            "designed-test-provider"
        }
    }

    #[test]
    fn separable_provider_calibrates() {
        let cal = calibrate(&DesignedProvider::separable()).expect("calibratable");
        // Paraphrase pairs sit near 1.0, unrelated near 0.0.
        assert!(cal.paraphrase_p25 > 0.9);
        assert!(cal.unrelated_p95 < 0.1);
        assert!(cal.dedup_cosine > cal.topical_floor);
        assert!(cal.dedup_cosine < cal.paraphrase_p25);
        assert_eq!(cal.probe_version, PROBE_VERSION);
    }

    #[test]
    fn degenerate_provider_fails_closed() {
        assert!(calibrate(&DesignedProvider::degenerate()).is_err());
    }

    #[test]
    fn calibration_roundtrips_through_file() {
        let dir = tempfile::tempdir().expect("tempdir");
        let provider = DesignedProvider::separable();
        let first = load_or_calibrate(dir.path(), &provider).expect("calibrate");
        // Second load must come from the file, not a recompute; equality of
        // every derived field is the observable.
        let second = load_or_calibrate(dir.path(), &provider).expect("load");
        assert_eq!(first.dedup_cosine, second.dedup_cosine);
        assert_eq!(first.topical_floor, second.topical_floor);
        assert!(
            calibration_path(dir.path(), provider.model_name(), provider.dimensions()).exists()
        );
    }

    #[test]
    fn percentile_nearest_rank() {
        let mut v = vec![0.9, 0.1, 0.5, 0.3, 0.7];
        assert!((percentile(&mut v, 0.5) - 0.5).abs() < f32::EPSILON);
        assert!((percentile(&mut v.clone(), 0.0) - 0.1).abs() < f32::EPSILON);
        assert!((percentile(&mut v.clone(), 1.0) - 0.9).abs() < f32::EPSILON);
        assert_eq!(percentile(&mut [], 0.5), 0.0);
    }
}