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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
// SPDX-FileCopyrightText: 2026 Andrei G <bug-ops>
// SPDX-License-Identifier: MIT OR Apache-2.0

//! Thin trait abstraction over [`crate::semantic::SemanticMemory`].
//!
//! `MemoryFacade` is a narrow interface covering the four operations the agent loop
//! depends on: `remember`, `recall`, `summarize`, and `compact`. It exists for two
//! reasons:
//!
//! 1. **Unit testing** — `InMemoryFacade` implements the trait using in-process
//!    storage so tests that exercise memory-dependent agent logic do not need `SQLite`
//!    or `Qdrant`.
//!
//! 2. **Future migration path** — the agent can eventually hold
//!    `Arc<dyn MemoryFacade>` instead of `Arc<SemanticMemory>`. Phase 2 will
//!    require making the trait object-safe (e.g. via `#[trait_variant::make]`
//!    or boxed-future signatures). This PR implements Phase 1 only.
//!
//! # Design constraints
//!
//! `MemoryEntry.parts` uses `Vec<serde_json::Value>` (opaque JSON) rather than
//! `Vec<zeph_llm::MessagePart>` to keep `zeph-memory` free of `zeph-llm` dependencies.

use std::collections::BTreeMap;
use std::sync::Mutex;

use crate::error::MemoryError;
use crate::types::{ConversationId, MessageId};

// ── Supporting types ─────────────────────────────────────────────────────────

/// An entry to persist in memory.
///
/// The `parts` field is opaque `serde_json::Value` — callers are responsible for
/// serializing their content model. This avoids a `zeph-memory` → `zeph-llm`
/// dependency at the trait boundary.
///
/// # Examples
///
/// ```
/// use zeph_memory::facade::MemoryEntry;
/// use zeph_memory::ConversationId;
///
/// let entry = MemoryEntry {
///     conversation_id: ConversationId(1),
///     role: "user".into(),
///     content: "Hello".into(),
///     parts: vec![],
///     metadata: None,
/// };
/// assert_eq!(entry.role, "user");
/// ```
#[derive(Debug, Clone)]
pub struct MemoryEntry {
    /// Conversation this entry belongs to.
    pub conversation_id: ConversationId,
    /// Role string (`"user"`, `"assistant"`, `"system"`).
    pub role: String,
    /// Flat text content of the message.
    pub content: String,
    /// Structured content parts as opaque JSON values.
    pub parts: Vec<serde_json::Value>,
    /// Optional per-entry metadata as opaque JSON.
    pub metadata: Option<serde_json::Value>,
}

/// A matching entry returned by a recall query.
///
/// # Examples
///
/// ```
/// use zeph_memory::facade::{MemoryMatch, MemorySource};
///
/// let m = MemoryMatch {
///     content: "Rust is a systems language.".into(),
///     score: 0.92,
///     source: MemorySource::Semantic,
/// };
/// assert!(m.score > 0.0);
/// ```
#[derive(Debug, Clone)]
pub struct MemoryMatch {
    /// Matching message content.
    pub content: String,
    /// Relevance score in `[0.0, 1.0]`.
    pub score: f32,
    /// Backend that produced this match.
    pub source: MemorySource,
}

/// Which memory backend produced a [`MemoryMatch`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MemorySource {
    /// Qdrant vector search.
    Semantic,
    /// Timestamp-filtered `SQLite` FTS5.
    Episodic,
    /// Graph traversal from extracted entities.
    Graph,
    /// `SQLite` FTS5 keyword search.
    Keyword,
}

/// Input context for a compaction run.
///
/// # Examples
///
/// ```
/// use zeph_memory::facade::CompactionContext;
/// use zeph_memory::ConversationId;
///
/// let ctx = CompactionContext { conversation_id: ConversationId(1), token_budget: 4096 };
/// assert_eq!(ctx.token_budget, 4096);
/// ```
#[derive(Debug, Clone)]
pub struct CompactionContext {
    /// Conversation to compact.
    pub conversation_id: ConversationId,
    /// Target token budget; the compactor aims to bring context below this limit.
    pub token_budget: usize,
}

/// Result of a compaction run.
#[derive(Debug, Clone)]
pub struct CompactionResult {
    /// Summary text replacing the compacted messages.
    pub summary: String,
    /// Number of messages that were replaced by the summary.
    pub messages_compacted: usize,
}

// ── MemoryFacade trait ────────────────────────────────────────────────────────

/// Narrow read/write interface over a memory backend.
///
/// Implement this trait to provide an alternative backend for unit testing
/// (see [`InMemoryFacade`]) or future agent refactoring.
///
/// # Contract
///
/// - `remember` stores a message and returns its stable ID.
/// - `recall` performs a best-effort similarity search; empty results are valid.
/// - `summarize` returns a textual summary of the conversation so far.
/// - `compact` reduces context size to within `ctx.token_budget`.
///
/// Implementations must be `Send + Sync` to support `Arc<dyn MemoryFacade>` usage.
// The `Send` bound on returned futures is guaranteed by the `Send + Sync` supertrait
// requirement on all implementors. `async_fn_in_trait` fires because auto-trait bounds
// on the implicit future type cannot be declared without #[trait_variant::make], which
// is deferred to Phase 2 when the trait becomes object-safe.
#[allow(async_fn_in_trait)]
pub trait MemoryFacade: Send + Sync {
    /// Store a memory entry and return its ID.
    ///
    /// # Errors
    ///
    /// Returns [`MemoryError`] if the backend fails to persist the entry.
    async fn remember(&self, entry: MemoryEntry) -> Result<MessageId, MemoryError>;

    /// Retrieve the most relevant entries for `query`, up to `limit` results.
    ///
    /// # Errors
    ///
    /// Returns [`MemoryError`] if the recall query fails.
    async fn recall(&self, query: &str, limit: usize) -> Result<Vec<MemoryMatch>, MemoryError>;

    /// Produce a textual summary of `conv_id`.
    ///
    /// # Errors
    ///
    /// Returns [`MemoryError`] if summarization fails.
    async fn summarize(&self, conv_id: ConversationId) -> Result<String, MemoryError>;

    /// Compact a conversation to fit within the token budget.
    ///
    /// # Errors
    ///
    /// Returns [`MemoryError`] if compaction fails.
    async fn compact(&self, ctx: &CompactionContext) -> Result<CompactionResult, MemoryError>;
}

// ── InMemoryFacade ────────────────────────────────────────────────────────────

/// In-process test double for [`MemoryFacade`].
///
/// Stores entries in a `Vec` and uses substring matching for recall.
/// No `SQLite` or `Qdrant` required — suitable for unit tests that exercise
/// memory-dependent agent logic without external infrastructure.
///
/// # Examples
///
/// ```no_run
/// # use zeph_memory::facade::{InMemoryFacade, MemoryEntry, MemoryFacade};
/// # use zeph_memory::ConversationId;
/// # #[tokio::main] async fn main() {
/// let facade = InMemoryFacade::new();
/// let entry = MemoryEntry {
///     conversation_id: ConversationId(1),
///     role: "user".into(),
///     content: "Rust borrow checker".into(),
///     parts: vec![],
///     metadata: None,
/// };
/// let id = facade.remember(entry).await.unwrap();
/// let matches = facade.recall("borrow", 10).await.unwrap();
/// assert!(!matches.is_empty());
/// # }
/// ```
#[derive(Debug, Default)]
pub struct InMemoryFacade {
    entries: Mutex<BTreeMap<i64, MemoryEntry>>,
    next_id: Mutex<i64>,
}

impl InMemoryFacade {
    /// Create a new empty facade.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Return the number of stored entries.
    #[must_use]
    pub fn len(&self) -> usize {
        self.entries.lock().map_or(0, |g| g.len())
    }

    /// Return `true` if no entries have been stored.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }
}

impl MemoryFacade for InMemoryFacade {
    async fn remember(&self, entry: MemoryEntry) -> Result<MessageId, MemoryError> {
        let mut id_guard = self
            .next_id
            .lock()
            .map_err(|e| MemoryError::Other(format!("InMemoryFacade lock poisoned: {e}")))?;
        *id_guard += 1;
        let id = *id_guard;
        let mut entries = self
            .entries
            .lock()
            .map_err(|e| MemoryError::Other(format!("InMemoryFacade lock poisoned: {e}")))?;
        entries.insert(id, entry);
        Ok(MessageId(id))
    }

    async fn recall(&self, query: &str, limit: usize) -> Result<Vec<MemoryMatch>, MemoryError> {
        let entries = self
            .entries
            .lock()
            .map_err(|e| MemoryError::Other(format!("InMemoryFacade lock poisoned: {e}")))?;
        let query_lower = query.to_lowercase();
        let mut matches: Vec<MemoryMatch> = entries
            .values()
            .filter(|e| e.content.to_lowercase().contains(&query_lower))
            .map(|e| MemoryMatch {
                content: e.content.clone(),
                score: 1.0,
                source: MemorySource::Keyword,
            })
            .take(limit)
            .collect();
        // Stable order for deterministic tests
        matches.sort_by(|a, b| a.content.cmp(&b.content));
        Ok(matches)
    }

    async fn summarize(&self, conv_id: ConversationId) -> Result<String, MemoryError> {
        let entries = self
            .entries
            .lock()
            .map_err(|e| MemoryError::Other(format!("InMemoryFacade lock poisoned: {e}")))?;
        let texts: Vec<&str> = entries
            .values()
            .filter(|e| e.conversation_id == conv_id)
            .map(|e| e.content.as_str())
            .collect();
        Ok(texts.join("\n"))
    }

    async fn compact(&self, ctx: &CompactionContext) -> Result<CompactionResult, MemoryError> {
        let mut entries = self
            .entries
            .lock()
            .map_err(|e| MemoryError::Other(format!("InMemoryFacade lock poisoned: {e}")))?;
        let ids_to_remove: Vec<i64> = entries
            .iter()
            .filter(|(_, e)| e.conversation_id == ctx.conversation_id)
            .map(|(&id, _)| id)
            .collect();
        let count = ids_to_remove.len();
        let summary: Vec<String> = ids_to_remove
            .iter()
            .filter_map(|id| entries.get(id).map(|e| e.content.clone()))
            .collect();
        for id in &ids_to_remove {
            entries.remove(id);
        }
        Ok(CompactionResult {
            summary: summary.join("\n"),
            messages_compacted: count,
        })
    }
}

// ── SemanticMemory impl ───────────────────────────────────────────────────────

impl MemoryFacade for crate::semantic::SemanticMemory {
    async fn remember(&self, entry: MemoryEntry) -> Result<MessageId, MemoryError> {
        let parts_json = serde_json::to_string(&entry.parts)
            .map_err(|e| MemoryError::Other(format!("parts serialization failed: {e}")))?;
        let (id_opt, _embedded) = self
            .remember_with_parts(
                entry.conversation_id,
                &entry.role,
                &entry.content,
                &parts_json,
                None,
            )
            .await?;
        id_opt.ok_or_else(|| MemoryError::Other("message rejected by admission control".into()))
    }

    async fn recall(&self, query: &str, limit: usize) -> Result<Vec<MemoryMatch>, MemoryError> {
        let recalled = self.recall(query, limit, None).await?;
        Ok(recalled
            .into_iter()
            .map(|r| MemoryMatch {
                content: r.message.content,
                score: r.score,
                source: MemorySource::Semantic,
            })
            .collect())
    }

    async fn summarize(&self, conv_id: ConversationId) -> Result<String, MemoryError> {
        let summaries = self.load_summaries(conv_id).await?;
        Ok(summaries
            .into_iter()
            .map(|s| s.content)
            .collect::<Vec<_>>()
            .join("\n"))
    }

    async fn compact(&self, ctx: &CompactionContext) -> Result<CompactionResult, MemoryError> {
        let before = self.message_count(ctx.conversation_id).await?;
        let messages_compacted = usize::try_from(before).unwrap_or(0);
        // Trigger a summarization pass to reduce context below the token budget.
        // The message_count parameter drives how many messages to summarize at once.
        // Approximate: 4 chars per token; produce a target message count that
        // keeps the resulting context under the token budget.
        let target_msgs = ctx.token_budget.checked_div(4).unwrap_or(512);
        let _ = self.summarize(ctx.conversation_id, target_msgs).await?;
        let summary = self
            .load_summaries(ctx.conversation_id)
            .await?
            .into_iter()
            .map(|s| s.content)
            .collect::<Vec<_>>()
            .join("\n");
        Ok(CompactionResult {
            summary,
            messages_compacted,
        })
    }
}

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

    #[tokio::test]
    async fn remember_and_recall() {
        let facade = InMemoryFacade::new();
        let entry = MemoryEntry {
            conversation_id: ConversationId(1),
            role: "user".into(),
            content: "Rust ownership model".into(),
            parts: vec![],
            metadata: None,
        };
        let id = facade.remember(entry).await.unwrap();
        assert_eq!(id, MessageId(1));

        let matches = facade.recall("ownership", 10).await.unwrap();
        assert_eq!(matches.len(), 1);
        assert_eq!(matches[0].content, "Rust ownership model");
        assert_eq!(matches[0].source, MemorySource::Keyword);
    }

    #[tokio::test]
    async fn recall_no_match() {
        let facade = InMemoryFacade::new();
        let entry = MemoryEntry {
            conversation_id: ConversationId(1),
            role: "user".into(),
            content: "Rust ownership model".into(),
            parts: vec![],
            metadata: None,
        };
        facade.remember(entry).await.unwrap();
        let matches = facade.recall("Python", 10).await.unwrap();
        assert!(matches.is_empty());
    }

    #[tokio::test]
    async fn summarize_joins_content() {
        let facade = InMemoryFacade::new();
        for content in ["Hello", "World"] {
            facade
                .remember(MemoryEntry {
                    conversation_id: ConversationId(1),
                    role: "user".into(),
                    content: content.into(),
                    parts: vec![],
                    metadata: None,
                })
                .await
                .unwrap();
        }
        let summary = facade.summarize(ConversationId(1)).await.unwrap();
        assert!(summary.contains("Hello") && summary.contains("World"));
    }

    #[tokio::test]
    async fn compact_removes_conversation_entries() {
        let facade = InMemoryFacade::new();
        facade
            .remember(MemoryEntry {
                conversation_id: ConversationId(1),
                role: "user".into(),
                content: "entry 1".into(),
                parts: vec![],
                metadata: None,
            })
            .await
            .unwrap();
        facade
            .remember(MemoryEntry {
                conversation_id: ConversationId(2),
                role: "user".into(),
                content: "other conv".into(),
                parts: vec![],
                metadata: None,
            })
            .await
            .unwrap();

        let result = facade
            .compact(&CompactionContext {
                conversation_id: ConversationId(1),
                token_budget: 100,
            })
            .await
            .unwrap();

        assert_eq!(result.messages_compacted, 1);
        assert!(result.summary.contains("entry 1"));
        // Other conversation untouched
        assert_eq!(facade.len(), 1);
    }

    #[tokio::test]
    async fn recall_respects_limit() {
        let facade = InMemoryFacade::new();
        for i in 0..5 {
            facade
                .remember(MemoryEntry {
                    conversation_id: ConversationId(1),
                    role: "user".into(),
                    content: format!("memory item {i}"),
                    parts: vec![],
                    metadata: None,
                })
                .await
                .unwrap();
        }
        let matches = facade.recall("memory", 3).await.unwrap();
        assert_eq!(matches.len(), 3);
    }
}