zeph-memory 0.18.2

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
426
427
428
429
430
431
432
433
434
// SPDX-FileCopyrightText: 2026 Andrei G <bug-ops>
// SPDX-License-Identifier: MIT OR Apache-2.0

use std::sync::Arc;
use std::sync::atomic::AtomicU64;

use zeph_llm::any::AnyProvider;
use zeph_llm::mock::MockProvider;

use crate::store::SqliteStore;
use crate::token_counter::TokenCounter;
use crate::types::{ConversationId, MessageId};

use super::super::*;
use super::test_semantic_memory;

#[tokio::test]
async fn unsummarized_count_decreases_after_summary() {
    let memory = test_semantic_memory(false).await;
    let cid = memory.sqlite().create_conversation().await.unwrap();

    for i in 0..10 {
        memory
            .remember(cid, "user", &format!("msg{i}"), None)
            .await
            .unwrap();
    }
    assert_eq!(memory.unsummarized_message_count(cid).await.unwrap(), 10);

    memory.summarize(cid, 5).await.unwrap();

    assert!(memory.unsummarized_message_count(cid).await.unwrap() < 10);
    assert_eq!(memory.message_count(cid).await.unwrap(), 10);
}

#[tokio::test]
async fn load_summaries_empty() {
    let memory = test_semantic_memory(false).await;
    let cid = memory.sqlite().create_conversation().await.unwrap();

    let summaries = memory.load_summaries(cid).await.unwrap();
    assert!(summaries.is_empty());
}

#[tokio::test]
async fn load_summaries_ordered() {
    let memory = test_semantic_memory(false).await;
    let cid = memory.sqlite().create_conversation().await.unwrap();

    let msg_id1 = memory
        .remember(cid, "user", "m1", None)
        .await
        .unwrap()
        .unwrap();
    let msg_id2 = memory
        .remember(cid, "assistant", "m2", None)
        .await
        .unwrap()
        .unwrap();
    let msg_id3 = memory
        .remember(cid, "user", "m3", None)
        .await
        .unwrap()
        .unwrap();

    let s1 = memory
        .sqlite()
        .save_summary(cid, "summary1", Some(msg_id1), Some(msg_id2), 3)
        .await
        .unwrap();
    let s2 = memory
        .sqlite()
        .save_summary(cid, "summary2", Some(msg_id2), Some(msg_id3), 3)
        .await
        .unwrap();

    let summaries = memory.load_summaries(cid).await.unwrap();
    assert_eq!(summaries.len(), 2);
    assert_eq!(summaries[0].id, s1);
    assert_eq!(summaries[0].content, "summary1");
    assert_eq!(summaries[1].id, s2);
    assert_eq!(summaries[1].content, "summary2");
}

#[tokio::test]
async fn summarize_below_threshold() {
    let memory = test_semantic_memory(false).await;
    let cid = memory.sqlite().create_conversation().await.unwrap();

    memory
        .remember(cid, "user", "hello", None)
        .await
        .unwrap()
        .unwrap();

    let result = memory.summarize(cid, 10).await.unwrap();
    assert!(result.is_none());
}

#[tokio::test]
async fn summarize_stores_summary() {
    let memory = test_semantic_memory(false).await;
    let cid = memory.sqlite().create_conversation().await.unwrap();

    for i in 0..5 {
        memory
            .remember(cid, "user", &format!("message {i}"), None)
            .await
            .unwrap();
    }

    let summary_id = memory.summarize(cid, 3).await.unwrap();
    assert!(summary_id.is_some());

    let summaries = memory.load_summaries(cid).await.unwrap();
    assert_eq!(summaries.len(), 1);
    assert_eq!(summaries[0].id, summary_id.unwrap());
    assert!(!summaries[0].content.is_empty());
}

#[tokio::test]
async fn summarize_respects_previous_summaries() {
    let memory = test_semantic_memory(false).await;
    let cid = memory.sqlite().create_conversation().await.unwrap();

    for i in 0..10 {
        memory
            .remember(cid, "user", &format!("message {i}"), None)
            .await
            .unwrap();
    }

    let s1 = memory.summarize(cid, 3).await.unwrap();
    assert!(s1.is_some());

    let s2 = memory.summarize(cid, 3).await.unwrap();
    assert!(s2.is_some());

    let summaries = memory.load_summaries(cid).await.unwrap();
    assert_eq!(summaries.len(), 2);
    assert!(summaries[0].last_message_id < summaries[1].first_message_id);
}

#[tokio::test]
async fn summarize_exact_threshold_returns_none() {
    let memory = test_semantic_memory(false).await;
    let cid = memory.sqlite().create_conversation().await.unwrap();

    for i in 0..3 {
        memory
            .remember(cid, "user", &format!("msg {i}"), None)
            .await
            .unwrap();
    }

    let result = memory.summarize(cid, 3).await.unwrap();
    assert!(result.is_none());
}

#[tokio::test]
async fn summarize_one_above_threshold_produces_summary() {
    let memory = test_semantic_memory(false).await;
    let cid = memory.sqlite().create_conversation().await.unwrap();

    for i in 0..4 {
        memory
            .remember(cid, "user", &format!("msg {i}"), None)
            .await
            .unwrap();
    }

    let result = memory.summarize(cid, 3).await.unwrap();
    assert!(result.is_some());
}

#[tokio::test]
async fn summary_fields_populated() {
    let memory = test_semantic_memory(false).await;
    let cid = memory.sqlite().create_conversation().await.unwrap();

    for i in 0..5 {
        memory
            .remember(cid, "user", &format!("msg {i}"), None)
            .await
            .unwrap();
    }

    memory.summarize(cid, 3).await.unwrap();
    let summaries = memory.load_summaries(cid).await.unwrap();
    let s = &summaries[0];

    assert_eq!(s.conversation_id, cid);
    assert!(s.first_message_id > Some(MessageId(0)));
    assert!(s.last_message_id >= s.first_message_id);
    assert!(s.token_estimate >= 0);
    assert!(!s.content.is_empty());
}

#[tokio::test]
async fn summarize_empty_messages_range_returns_none() {
    let memory = test_semantic_memory(false).await;
    let cid = memory.sqlite().create_conversation().await.unwrap();

    for i in 0..6 {
        memory
            .remember(cid, "user", &format!("msg {i}"), None)
            .await
            .unwrap();
    }

    memory.summarize(cid, 3).await.unwrap();
    memory.summarize(cid, 3).await.unwrap();

    let summaries = memory.load_summaries(cid).await.unwrap();
    assert_eq!(summaries.len(), 2);
}

#[tokio::test]
async fn summarize_token_estimate_populated() {
    let memory = test_semantic_memory(false).await;
    let cid = memory.sqlite().create_conversation().await.unwrap();

    for i in 0..5 {
        memory
            .remember(cid, "user", &format!("message {i}"), None)
            .await
            .unwrap();
    }

    memory.summarize(cid, 3).await.unwrap();
    let summaries = memory.load_summaries(cid).await.unwrap();
    let token_est = summaries[0].token_estimate;
    assert!(token_est > 0);
}

#[tokio::test]
async fn summarize_fails_when_provider_chat_fails() {
    let sqlite = SqliteStore::new(":memory:").await.unwrap();
    let provider = AnyProvider::Ollama(zeph_llm::ollama::OllamaProvider::new(
        "http://127.0.0.1:1",
        "test".into(),
        "embed".into(),
    ));
    let memory = super::super::SemanticMemory {
        sqlite,
        qdrant: None,
        provider,
        embedding_model: "test".into(),
        vector_weight: 0.7,
        keyword_weight: 0.3,
        temporal_decay_enabled: false,
        temporal_decay_half_life_days: 30,
        mmr_enabled: false,
        mmr_lambda: 0.7,
        importance_enabled: false,
        importance_weight: 0.15,
        token_counter: Arc::new(TokenCounter::new()),
        graph_store: None,
        community_detection_failures: Arc::new(AtomicU64::new(0)),
        graph_extraction_count: Arc::new(AtomicU64::new(0)),
        graph_extraction_failures: Arc::new(AtomicU64::new(0)),
        tier_boost_semantic: 1.3,
        admission_control: None,
    };
    let cid = memory.sqlite().create_conversation().await.unwrap();

    for i in 0..5 {
        memory
            .remember(cid, "user", &format!("msg {i}"), None)
            .await
            .unwrap();
    }

    let result = memory.summarize(cid, 3).await;
    assert!(result.is_err());
}

#[tokio::test]
async fn summarize_message_range_bounds() {
    let memory = test_semantic_memory(false).await;
    let cid = memory.sqlite().create_conversation().await.unwrap();

    for i in 0..8 {
        memory
            .remember(cid, "user", &format!("msg {i}"), None)
            .await
            .unwrap();
    }

    let summary_id = memory.summarize(cid, 4).await.unwrap().unwrap();
    let summaries = memory.load_summaries(cid).await.unwrap();
    assert_eq!(summaries.len(), 1);
    assert_eq!(summaries[0].id, summary_id);
    assert!(summaries[0].first_message_id >= Some(MessageId(1)));
    assert!(summaries[0].last_message_id >= summaries[0].first_message_id);
}

#[tokio::test]
async fn summarize_fallback_to_plain_text_when_structured_fails() {
    let sqlite = SqliteStore::new(":memory:").await.unwrap();
    let mut mock = MockProvider::default();
    mock.default_response = "plain text summary".into();
    let provider = AnyProvider::Mock(mock);

    let memory = super::super::SemanticMemory {
        sqlite,
        qdrant: None,
        provider,
        embedding_model: "test".into(),
        vector_weight: 0.7,
        keyword_weight: 0.3,
        temporal_decay_enabled: false,
        temporal_decay_half_life_days: 30,
        mmr_enabled: false,
        mmr_lambda: 0.7,
        importance_enabled: false,
        importance_weight: 0.15,
        token_counter: Arc::new(TokenCounter::new()),
        graph_store: None,
        community_detection_failures: Arc::new(AtomicU64::new(0)),
        graph_extraction_count: Arc::new(AtomicU64::new(0)),
        graph_extraction_failures: Arc::new(AtomicU64::new(0)),
        tier_boost_semantic: 1.3,
        admission_control: None,
    };

    let cid = memory.sqlite().create_conversation().await.unwrap();
    for i in 0..5 {
        memory
            .remember(cid, "user", &format!("msg {i}"), None)
            .await
            .unwrap();
    }

    let result = memory.summarize(cid, 3).await;
    assert!(result.is_ok());
    let summaries = memory.load_summaries(cid).await.unwrap();
    assert_eq!(summaries.len(), 1);
    assert!(!summaries[0].content.is_empty());
}

#[test]
fn build_summarization_prompt_format() {
    let messages = vec![
        (MessageId(1), "user".into(), "Hello".into()),
        (MessageId(2), "assistant".into(), "Hi there".into()),
    ];
    let prompt = build_summarization_prompt(&messages);
    assert!(prompt.contains("user: Hello"));
    assert!(prompt.contains("assistant: Hi there"));
    assert!(prompt.contains("key_facts"));
}

#[test]
fn build_summarization_prompt_empty() {
    let messages: Vec<(MessageId, String, String)> = vec![];
    let prompt = build_summarization_prompt(&messages);
    assert!(prompt.contains("key_facts"));
}

#[test]
fn build_summarization_prompt_preserves_order() {
    let messages = vec![
        (MessageId(1), "user".into(), "first".into()),
        (MessageId(2), "assistant".into(), "second".into()),
        (MessageId(3), "user".into(), "third".into()),
    ];
    let prompt = build_summarization_prompt(&messages);
    let first_pos = prompt.find("user: first").unwrap();
    let second_pos = prompt.find("assistant: second").unwrap();
    let third_pos = prompt.find("user: third").unwrap();
    assert!(first_pos < second_pos);
    assert!(second_pos < third_pos);
}

#[test]
fn structured_summary_deserialize() {
    let json = r#"{"summary":"s","key_facts":["f1","f2"],"entities":["e1"]}"#;
    let ss: StructuredSummary = serde_json::from_str(json).unwrap();
    assert_eq!(ss.summary, "s");
    assert_eq!(ss.key_facts.len(), 2);
    assert_eq!(ss.entities.len(), 1);
}

#[test]
fn structured_summary_empty_facts() {
    let json = r#"{"summary":"s","key_facts":[],"entities":[]}"#;
    let ss: StructuredSummary = serde_json::from_str(json).unwrap();
    assert!(ss.key_facts.is_empty());
    assert!(ss.entities.is_empty());
}

#[test]
fn summary_clone() {
    let summary = Summary {
        id: 1,
        conversation_id: ConversationId(2),
        content: "test summary".into(),
        first_message_id: Some(MessageId(1)),
        last_message_id: Some(MessageId(5)),
        token_estimate: 10,
    };
    let cloned = summary.clone();
    assert_eq!(summary.id, cloned.id);
    assert_eq!(summary.content, cloned.content);
}

#[test]
fn summary_debug() {
    let summary = Summary {
        id: 1,
        conversation_id: ConversationId(2),
        content: "test".into(),
        first_message_id: Some(MessageId(1)),
        last_message_id: Some(MessageId(5)),
        token_estimate: 10,
    };
    let dbg = format!("{summary:?}");
    assert!(dbg.contains("Summary"));
}

#[tokio::test]
async fn search_key_facts_no_qdrant_empty() {
    let memory = test_semantic_memory(false).await;
    let facts = memory.search_key_facts("query", 5).await.unwrap();
    assert!(facts.is_empty());
}

#[tokio::test]
async fn load_summaries_nonexistent_conversation() {
    let memory = test_semantic_memory(false).await;
    let summaries = memory.load_summaries(ConversationId(999)).await.unwrap();
    assert!(summaries.is_empty());
}