zeph-memory 0.19.1

Semantic memory with SQLite and Qdrant for Zeph agent
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
// SPDX-FileCopyrightText: 2026 Andrei G <bug-ops>
// SPDX-License-Identifier: MIT OR Apache-2.0

//! Kumiho belief revision: semantic supersession of contradicted graph edges.
//!
//! When a new fact is inserted for an entity pair, this module checks whether any existing
//! active edges for the same pair carry a semantically contradictory claim. If so, the old
//! edge is marked as superseded (via `superseded_by` pointer + `valid_to` timestamp) and the
//! new edge becomes the current belief.
//!
//! ## Contradiction heuristic
//!
//! Similarity alone is insufficient for contradiction detection: "Alice prefers Python" and
//! "Alice loves Python" are highly similar but NOT contradictory (they reinforce each other).
//! The heuristic requires:
//! 1. Same entity pair (guaranteed by the call site — `edges_exact` already filters).
//! 2. Same relation domain: the relation strings must overlap significantly (edit-distance-based
//!    normalized similarity >= 0.5, or one contains the other).
//! 3. Different fact content: cosine similarity of fact embeddings >= `similarity_threshold`.
//!
//! Reinforcing facts share a relation AND similar content → caught by exact-match dedup upstream.
//! Contradictions share a relation but differ in content → caught here.

use zeph_common::math::cosine_similarity;
use zeph_llm::any::AnyProvider;
use zeph_llm::provider::LlmProvider as _;

use crate::error::MemoryError;
use crate::graph::types::{Edge, EdgeType};

pub use zeph_common::config::memory::BeliefRevisionConfig;

/// Normalized edit-distance similarity between two strings.
///
/// Returns 1.0 for equal strings, 0.0 when distance equals max length.
/// Used to check whether two relation strings are in the same domain.
#[must_use]
fn relation_similarity(a: &str, b: &str) -> f32 {
    if a == b {
        return 1.0;
    }
    let max_len = a.len().max(b.len());
    if max_len == 0 {
        return 1.0;
    }
    let dist = edit_distance(a, b);
    // max_len <= string byte length, bounded by MAX_RELATION_BYTES (256); cast is safe.
    #[allow(clippy::cast_precision_loss)]
    let max_len_f = max_len as f32;
    #[allow(clippy::cast_precision_loss)]
    let dist_f = dist as f32;
    1.0 - dist_f / max_len_f
}

/// Simple byte-level edit distance (Levenshtein) between two strings.
fn edit_distance(a: &str, b: &str) -> usize {
    let a: Vec<char> = a.chars().collect();
    let b: Vec<char> = b.chars().collect();
    let m = a.len();
    let n = b.len();
    let mut prev: Vec<usize> = (0..=n).collect();
    let mut curr = vec![0usize; n + 1];
    for i in 1..=m {
        curr[0] = i;
        for j in 1..=n {
            curr[j] = if a[i - 1] == b[j - 1] {
                prev[j - 1]
            } else {
                1 + prev[j].min(curr[j - 1]).min(prev[j - 1])
            };
        }
        std::mem::swap(&mut prev, &mut curr);
    }
    prev[n]
}

/// Embed a fact string with the given provider.
///
/// Returns `None` on timeout (5 s) or provider error.
async fn embed_fact(provider: &AnyProvider, fact: &str) -> Option<Vec<f32>> {
    match tokio::time::timeout(std::time::Duration::from_secs(5), provider.embed(fact)).await {
        Ok(Ok(v)) => Some(v),
        Ok(Err(err)) => {
            tracing::warn!(error = %err, "belief_revision: embed failed");
            None
        }
        Err(_) => {
            tracing::warn!("belief_revision: embed timed out");
            None
        }
    }
}

/// Find edges that are semantically superseded by a new fact for the same entity pair.
///
/// An existing edge is superseded when:
/// - Its relation is in the same domain as `new_relation` (similarity >= 0.5, or containment).
/// - The fact embedding similarity to `new_fact_embedding` is >= `config.similarity_threshold`.
///
/// The new fact embedding is pre-computed by the caller so this function can reuse it across
/// multiple call sites without redundant embed calls.
///
/// Returns the list of edge IDs to supersede (may be empty).
///
/// # Errors
///
/// Returns an error if embedding calls fail in an unexpected way (beyond soft timeouts).
pub async fn find_superseded_edges(
    existing_edges: &[Edge],
    new_fact_embedding: &[f32],
    new_relation: &str,
    new_edge_type: EdgeType,
    provider: &AnyProvider,
    config: &BeliefRevisionConfig,
) -> Result<Vec<i64>, MemoryError> {
    // Filter to edges of the same type with a related relation domain.
    let candidates: Vec<&Edge> = existing_edges
        .iter()
        .filter(|e| {
            e.edge_type == new_edge_type && is_same_relation_domain(&e.relation, new_relation)
        })
        .collect();

    if candidates.is_empty() {
        return Ok(Vec::new());
    }

    let mut superseded = Vec::new();
    for edge in candidates {
        let Some(existing_emb) = embed_fact(provider, &edge.fact).await else {
            continue;
        };
        let sim = cosine_similarity(new_fact_embedding, &existing_emb);
        if sim >= config.similarity_threshold {
            tracing::debug!(
                edge_id = edge.id,
                relation = %edge.relation,
                similarity = sim,
                "belief_revision: edge superseded"
            );
            superseded.push(edge.id);
        }
    }
    Ok(superseded)
}

/// Check whether two relation strings are in the same domain.
///
/// Returns `true` when:
/// - They are identical.
/// - One contains the other as a substring.
/// - Their normalized edit-distance similarity >= 0.5.
#[must_use]
fn is_same_relation_domain(existing: &str, new: &str) -> bool {
    if existing == new {
        return true;
    }
    if existing.contains(new) || new.contains(existing) {
        return true;
    }
    relation_similarity(existing, new) >= 0.5
}

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

    #[test]
    fn cosine_similarity_identical_vectors() {
        let v = vec![1.0f32, 0.0, 0.0];
        assert!((cosine_similarity(&v, &v) - 1.0).abs() < 1e-6);
    }

    #[test]
    fn cosine_similarity_orthogonal_vectors() {
        let a = vec![1.0f32, 0.0];
        let b = vec![0.0f32, 1.0];
        assert!(cosine_similarity(&a, &b).abs() < 1e-6);
    }

    #[test]
    fn cosine_similarity_zero_vector_returns_zero() {
        let a = vec![0.0f32, 0.0];
        let b = vec![1.0f32, 0.0];
        assert!((cosine_similarity(&a, &b) - 0.0).abs() < f32::EPSILON);
    }

    #[test]
    fn cosine_similarity_length_mismatch_returns_zero() {
        let a = vec![1.0f32, 0.0];
        let b = vec![1.0f32];
        assert!((cosine_similarity(&a, &b) - 0.0).abs() < f32::EPSILON);
    }

    #[test]
    fn relation_similarity_equal_strings() {
        assert!((relation_similarity("works_at", "works_at") - 1.0).abs() < 1e-6);
    }

    #[test]
    fn relation_similarity_completely_different() {
        // "abc" vs "xyz" — all 3 chars different, distance = 3, max_len = 3, sim = 0.0
        let s = relation_similarity("abc", "xyz");
        assert!(s < 0.1, "expected near-0 similarity, got {s}");
    }

    #[test]
    fn relation_similarity_partial_overlap() {
        // "works_at" vs "work_at" — 1 char diff out of 8 → ~0.875
        let s = relation_similarity("works_at", "work_at");
        assert!(s > 0.5, "expected > 0.5, got {s}");
    }

    #[test]
    fn is_same_relation_domain_identical() {
        assert!(is_same_relation_domain("works_at", "works_at"));
    }

    #[test]
    fn is_same_relation_domain_containment() {
        assert!(is_same_relation_domain("prefers", "prefers_strongly"));
    }

    #[test]
    fn is_same_relation_domain_different_domains() {
        assert!(!is_same_relation_domain("works_at", "knows"));
    }

    #[test]
    fn edit_distance_empty_strings() {
        assert_eq!(edit_distance("", ""), 0);
    }

    #[test]
    fn edit_distance_one_empty() {
        assert_eq!(edit_distance("abc", ""), 3);
        assert_eq!(edit_distance("", "xyz"), 3);
    }

    #[test]
    fn edit_distance_single_substitution() {
        assert_eq!(edit_distance("works_at", "work_at"), 1);
    }

    // --- find_superseded_edges tests (provider-free paths) ---

    fn make_edge(
        id: i64,
        relation: &str,
        fact: &str,
        edge_type: crate::graph::types::EdgeType,
    ) -> crate::graph::types::Edge {
        crate::graph::types::Edge {
            id,
            source_entity_id: 1,
            target_entity_id: 2,
            relation: relation.to_string(),
            fact: fact.to_string(),
            confidence: 1.0,
            valid_from: "2026-01-01".to_string(),
            valid_to: None,
            created_at: "2026-01-01".to_string(),
            expired_at: None,
            source_message_id: None,
            qdrant_point_id: None,
            edge_type,
            retrieval_count: 0,
            last_retrieved_at: None,
            superseded_by: None,
        }
    }

    #[tokio::test]
    async fn find_superseded_edges_empty_existing_returns_empty() {
        use zeph_llm::any::AnyProvider;
        use zeph_llm::mock::MockProvider;
        let provider = AnyProvider::Mock(MockProvider::default());
        let config = BeliefRevisionConfig {
            similarity_threshold: 0.85,
        };
        let result = find_superseded_edges(
            &[],
            &[0.5f32; 4],
            "works_at",
            crate::graph::types::EdgeType::Semantic,
            &provider,
            &config,
        )
        .await
        .unwrap();
        assert!(result.is_empty());
    }

    #[tokio::test]
    async fn find_superseded_edges_different_edge_type_not_candidate() {
        use zeph_llm::any::AnyProvider;
        use zeph_llm::mock::MockProvider;
        let provider = AnyProvider::Mock(MockProvider::default());
        let config = BeliefRevisionConfig {
            similarity_threshold: 0.85,
        };
        // Edge is Causal, new fact is Semantic → different type, must not be a candidate.
        let edge = make_edge(
            42,
            "works_at",
            "Alice works at Acme",
            crate::graph::types::EdgeType::Causal,
        );
        let result = find_superseded_edges(
            &[edge],
            &[0.5f32; 4],
            "works_at",
            crate::graph::types::EdgeType::Semantic,
            &provider,
            &config,
        )
        .await
        .unwrap();
        assert!(
            result.is_empty(),
            "different edge type must not be superseded"
        );
    }

    #[tokio::test]
    async fn find_superseded_edges_different_domain_not_candidate() {
        use zeph_llm::any::AnyProvider;
        use zeph_llm::mock::MockProvider;
        let provider = AnyProvider::Mock(MockProvider::default());
        let config = BeliefRevisionConfig {
            similarity_threshold: 0.85,
        };
        // "works_at" vs "knows" — clearly different domains (edit-distance sim < 0.5).
        let edge = make_edge(
            7,
            "works_at",
            "Alice works at Acme",
            crate::graph::types::EdgeType::Semantic,
        );
        let result = find_superseded_edges(
            &[edge],
            &[0.5f32; 4],
            "knows",
            crate::graph::types::EdgeType::Semantic,
            &provider,
            &config,
        )
        .await
        .unwrap();
        assert!(
            result.is_empty(),
            "different relation domain must not be superseded"
        );
    }

    #[tokio::test]
    async fn find_superseded_edges_high_similarity_supersedes() {
        use zeph_llm::any::AnyProvider;
        use zeph_llm::mock::MockProvider;
        // MockProvider returns the same fixed embedding for all inputs.
        // cosine_similarity(same, same) = 1.0 >= threshold → superseded.
        let mut mock = MockProvider::default();
        mock.supports_embeddings = true;
        mock.embedding = vec![1.0f32, 0.0, 0.0, 0.0];
        let provider = AnyProvider::Mock(mock);
        let config = BeliefRevisionConfig {
            similarity_threshold: 0.85,
        };
        let edge = make_edge(
            99,
            "works_at",
            "Alice works at OldCo",
            crate::graph::types::EdgeType::Semantic,
        );
        // new fact embedding is identical to mock embedding → sim = 1.0
        let new_emb = vec![1.0f32, 0.0, 0.0, 0.0];
        let result = find_superseded_edges(
            &[edge],
            &new_emb,
            "works_at",
            crate::graph::types::EdgeType::Semantic,
            &provider,
            &config,
        )
        .await
        .unwrap();
        assert_eq!(result, vec![99], "high-similarity edge must be superseded");
    }

    #[tokio::test]
    async fn find_superseded_edges_low_similarity_not_superseded() {
        use zeph_llm::any::AnyProvider;
        use zeph_llm::mock::MockProvider;
        // MockProvider returns [1,0,0,0]; new fact embedding is [0,1,0,0] → cosine = 0.0 < 0.85.
        let mut mock = MockProvider::default();
        mock.supports_embeddings = true;
        mock.embedding = vec![1.0f32, 0.0, 0.0, 0.0];
        let provider = AnyProvider::Mock(mock);
        let config = BeliefRevisionConfig {
            similarity_threshold: 0.85,
        };
        let edge = make_edge(
            55,
            "works_at",
            "Alice works at OldCo",
            crate::graph::types::EdgeType::Semantic,
        );
        // orthogonal embedding → sim = 0.0 → not superseded
        let new_emb = vec![0.0f32, 1.0, 0.0, 0.0];
        let result = find_superseded_edges(
            &[edge],
            &new_emb,
            "works_at",
            crate::graph::types::EdgeType::Semantic,
            &provider,
            &config,
        )
        .await
        .unwrap();
        assert!(
            result.is_empty(),
            "low-similarity edge (reinforcing or unrelated) must not be superseded"
        );
    }
}