zeph_memory/facade.rs
1// SPDX-FileCopyrightText: 2026 Andrei G <bug-ops>
2// SPDX-License-Identifier: MIT OR Apache-2.0
3
4//! Thin trait abstraction over [`crate::semantic::SemanticMemory`].
5//!
6//! `MemoryFacade` is a narrow interface covering the four operations the agent loop
7//! depends on: `remember`, `recall`, `summarize`, and `compact`. It exists for two
8//! reasons:
9//!
10//! 1. **Unit testing** — `InMemoryFacade` implements the trait using in-process
11//! storage so tests that exercise memory-dependent agent logic do not need `SQLite`
12//! or `Qdrant`.
13//!
14//! 2. **Future migration path** — the agent can eventually hold
15//! `Arc<dyn MemoryFacade>` instead of `Arc<SemanticMemory>`. Phase 2 will
16//! require making the trait object-safe (e.g. via `#[trait_variant::make]`
17//! or boxed-future signatures). This PR implements Phase 1 only.
18//!
19//! # Design constraints
20//!
21//! `MemoryEntry.parts` uses `Vec<serde_json::Value>` (opaque JSON) rather than
22//! `Vec<zeph_llm::MessagePart>` to keep `zeph-memory` free of `zeph-llm` dependencies.
23
24use std::collections::BTreeMap;
25use std::sync::Mutex;
26
27use crate::error::MemoryError;
28use crate::types::{ConversationId, MessageId};
29
30// ── Supporting types ─────────────────────────────────────────────────────────
31
32/// An entry to persist in memory.
33///
34/// The `parts` field is opaque `serde_json::Value` — callers are responsible for
35/// serializing their content model. This avoids a `zeph-memory` → `zeph-llm`
36/// dependency at the trait boundary.
37///
38/// # Examples
39///
40/// ```
41/// use zeph_memory::facade::MemoryEntry;
42/// use zeph_memory::ConversationId;
43///
44/// let entry = MemoryEntry {
45/// conversation_id: ConversationId(1),
46/// role: "user".into(),
47/// content: "Hello".into(),
48/// parts: vec![],
49/// metadata: None,
50/// };
51/// assert_eq!(entry.role, "user");
52/// ```
53#[derive(Debug, Clone)]
54pub struct MemoryEntry {
55 /// Conversation this entry belongs to.
56 pub conversation_id: ConversationId,
57 /// Role string (`"user"`, `"assistant"`, `"system"`).
58 pub role: String,
59 /// Flat text content of the message.
60 pub content: String,
61 /// Structured content parts as opaque JSON values.
62 pub parts: Vec<serde_json::Value>,
63 /// Optional per-entry metadata as opaque JSON.
64 pub metadata: Option<serde_json::Value>,
65}
66
67/// A matching entry returned by a recall query.
68///
69/// # Examples
70///
71/// ```
72/// use zeph_memory::facade::{MemoryMatch, MemorySource};
73///
74/// let m = MemoryMatch {
75/// content: "Rust is a systems language.".into(),
76/// score: 0.92,
77/// source: MemorySource::Semantic,
78/// };
79/// assert!(m.score > 0.0);
80/// ```
81#[derive(Debug, Clone)]
82pub struct MemoryMatch {
83 /// Matching message content.
84 pub content: String,
85 /// Relevance score in `[0.0, 1.0]`.
86 pub score: f32,
87 /// Backend that produced this match.
88 pub source: MemorySource,
89}
90
91/// Which memory backend produced a [`MemoryMatch`].
92#[derive(Debug, Clone, Copy, PartialEq, Eq)]
93pub enum MemorySource {
94 /// Qdrant vector search.
95 Semantic,
96 /// Timestamp-filtered `SQLite` FTS5.
97 Episodic,
98 /// Graph traversal from extracted entities.
99 Graph,
100 /// `SQLite` FTS5 keyword search.
101 Keyword,
102}
103
104/// Input context for a compaction run.
105///
106/// # Examples
107///
108/// ```
109/// use zeph_memory::facade::CompactionContext;
110/// use zeph_memory::ConversationId;
111///
112/// let ctx = CompactionContext { conversation_id: ConversationId(1), token_budget: 4096 };
113/// assert_eq!(ctx.token_budget, 4096);
114/// ```
115#[derive(Debug, Clone)]
116pub struct CompactionContext {
117 /// Conversation to compact.
118 pub conversation_id: ConversationId,
119 /// Target token budget; the compactor aims to bring context below this limit.
120 pub token_budget: usize,
121}
122
123/// Result of a compaction run.
124#[derive(Debug, Clone)]
125pub struct CompactionResult {
126 /// Summary text replacing the compacted messages.
127 pub summary: String,
128 /// Number of messages that were replaced by the summary.
129 pub messages_compacted: usize,
130}
131
132// ── MemoryFacade trait ────────────────────────────────────────────────────────
133
134/// Narrow read/write interface over a memory backend.
135///
136/// Implement this trait to provide an alternative backend for unit testing
137/// (see [`InMemoryFacade`]) or future agent refactoring.
138///
139/// # Contract
140///
141/// - `remember` stores a message and returns its stable ID.
142/// - `recall` performs a best-effort similarity search; empty results are valid.
143/// - `summarize` returns a textual summary of the conversation so far.
144/// - `compact` reduces context size to within `ctx.token_budget`.
145///
146/// Implementations must be `Send + Sync` to support `Arc<dyn MemoryFacade>` usage.
147// The `Send` bound on returned futures is guaranteed by the `Send + Sync` supertrait
148// requirement on all implementors. `async_fn_in_trait` fires because auto-trait bounds
149// on the implicit future type cannot be declared without #[trait_variant::make], which
150// is deferred to Phase 2 when the trait becomes object-safe.
151#[allow(async_fn_in_trait)]
152pub trait MemoryFacade: Send + Sync {
153 /// Store a memory entry and return its ID.
154 ///
155 /// # Errors
156 ///
157 /// Returns [`MemoryError`] if the backend fails to persist the entry.
158 async fn remember(&self, entry: MemoryEntry) -> Result<MessageId, MemoryError>;
159
160 /// Retrieve the most relevant entries for `query`, up to `limit` results.
161 ///
162 /// # Errors
163 ///
164 /// Returns [`MemoryError`] if the recall query fails.
165 async fn recall(&self, query: &str, limit: usize) -> Result<Vec<MemoryMatch>, MemoryError>;
166
167 /// Produce a textual summary of `conv_id`.
168 ///
169 /// # Errors
170 ///
171 /// Returns [`MemoryError`] if summarization fails.
172 async fn summarize(&self, conv_id: ConversationId) -> Result<String, MemoryError>;
173
174 /// Compact a conversation to fit within the token budget.
175 ///
176 /// # Errors
177 ///
178 /// Returns [`MemoryError`] if compaction fails.
179 async fn compact(&self, ctx: &CompactionContext) -> Result<CompactionResult, MemoryError>;
180}
181
182// ── InMemoryFacade ────────────────────────────────────────────────────────────
183
184/// In-process test double for [`MemoryFacade`].
185///
186/// Stores entries in a `Vec` and uses substring matching for recall.
187/// No `SQLite` or `Qdrant` required — suitable for unit tests that exercise
188/// memory-dependent agent logic without external infrastructure.
189///
190/// # Examples
191///
192/// ```no_run
193/// # use zeph_memory::facade::{InMemoryFacade, MemoryEntry, MemoryFacade};
194/// # use zeph_memory::ConversationId;
195/// # #[tokio::main] async fn main() {
196/// let facade = InMemoryFacade::new();
197/// let entry = MemoryEntry {
198/// conversation_id: ConversationId(1),
199/// role: "user".into(),
200/// content: "Rust borrow checker".into(),
201/// parts: vec![],
202/// metadata: None,
203/// };
204/// let id = facade.remember(entry).await.unwrap();
205/// let matches = facade.recall("borrow", 10).await.unwrap();
206/// assert!(!matches.is_empty());
207/// # }
208/// ```
209#[derive(Debug, Default)]
210pub struct InMemoryFacade {
211 entries: Mutex<BTreeMap<i64, MemoryEntry>>,
212 next_id: Mutex<i64>,
213}
214
215impl InMemoryFacade {
216 /// Create a new empty facade.
217 #[must_use]
218 pub fn new() -> Self {
219 Self::default()
220 }
221
222 /// Return the number of stored entries.
223 #[must_use]
224 pub fn len(&self) -> usize {
225 self.entries.lock().map_or(0, |g| g.len())
226 }
227
228 /// Return `true` if no entries have been stored.
229 #[must_use]
230 pub fn is_empty(&self) -> bool {
231 self.len() == 0
232 }
233}
234
235impl MemoryFacade for InMemoryFacade {
236 async fn remember(&self, entry: MemoryEntry) -> Result<MessageId, MemoryError> {
237 let mut id_guard = self
238 .next_id
239 .lock()
240 .map_err(|e| MemoryError::LockPoisoned(format!("InMemoryFacade lock poisoned: {e}")))?;
241 *id_guard += 1;
242 let id = *id_guard;
243 let mut entries = self
244 .entries
245 .lock()
246 .map_err(|e| MemoryError::LockPoisoned(format!("InMemoryFacade lock poisoned: {e}")))?;
247 entries.insert(id, entry);
248 Ok(MessageId(id))
249 }
250
251 async fn recall(&self, query: &str, limit: usize) -> Result<Vec<MemoryMatch>, MemoryError> {
252 let entries = self
253 .entries
254 .lock()
255 .map_err(|e| MemoryError::LockPoisoned(format!("InMemoryFacade lock poisoned: {e}")))?;
256 let query_lower = query.to_lowercase();
257 let mut matches: Vec<MemoryMatch> = entries
258 .values()
259 .filter(|e| e.content.to_lowercase().contains(&query_lower))
260 .map(|e| MemoryMatch {
261 content: e.content.clone(),
262 score: 1.0,
263 source: MemorySource::Keyword,
264 })
265 .take(limit)
266 .collect();
267 // Stable order for deterministic tests
268 matches.sort_by(|a, b| a.content.cmp(&b.content));
269 Ok(matches)
270 }
271
272 async fn summarize(&self, conv_id: ConversationId) -> Result<String, MemoryError> {
273 let entries = self
274 .entries
275 .lock()
276 .map_err(|e| MemoryError::LockPoisoned(format!("InMemoryFacade lock poisoned: {e}")))?;
277 let texts: Vec<&str> = entries
278 .values()
279 .filter(|e| e.conversation_id == conv_id)
280 .map(|e| e.content.as_str())
281 .collect();
282 Ok(texts.join("\n"))
283 }
284
285 async fn compact(&self, ctx: &CompactionContext) -> Result<CompactionResult, MemoryError> {
286 let mut entries = self
287 .entries
288 .lock()
289 .map_err(|e| MemoryError::LockPoisoned(format!("InMemoryFacade lock poisoned: {e}")))?;
290 let ids_to_remove: Vec<i64> = entries
291 .iter()
292 .filter(|(_, e)| e.conversation_id == ctx.conversation_id)
293 .map(|(&id, _)| id)
294 .collect();
295 let count = ids_to_remove.len();
296 let summary: Vec<String> = ids_to_remove
297 .iter()
298 .filter_map(|id| entries.get(id).map(|e| e.content.clone()))
299 .collect();
300 for id in &ids_to_remove {
301 entries.remove(id);
302 }
303 Ok(CompactionResult {
304 summary: summary.join("\n"),
305 messages_compacted: count,
306 })
307 }
308}
309
310// ── SemanticMemory impl ───────────────────────────────────────────────────────
311
312impl MemoryFacade for crate::semantic::SemanticMemory {
313 async fn remember(&self, entry: MemoryEntry) -> Result<MessageId, MemoryError> {
314 let parts_json = serde_json::to_string(&entry.parts).map_err(MemoryError::Json)?;
315 let (id_opt, _embedded) = self
316 .remember_with_parts(
317 entry.conversation_id,
318 &entry.role,
319 &entry.content,
320 &parts_json,
321 None,
322 )
323 .await?;
324 id_opt.ok_or_else(|| {
325 MemoryError::InvalidInput("message rejected by admission control".into())
326 })
327 }
328
329 async fn recall(&self, query: &str, limit: usize) -> Result<Vec<MemoryMatch>, MemoryError> {
330 let recalled = self.recall(query, limit, None).await?;
331 Ok(recalled
332 .into_iter()
333 .map(|r| MemoryMatch {
334 content: r.message.content,
335 score: r.score,
336 source: MemorySource::Semantic,
337 })
338 .collect())
339 }
340
341 async fn summarize(&self, conv_id: ConversationId) -> Result<String, MemoryError> {
342 let summaries = self.load_summaries(conv_id).await?;
343 Ok(summaries
344 .into_iter()
345 .map(|s| s.content)
346 .collect::<Vec<_>>()
347 .join("\n"))
348 }
349
350 async fn compact(&self, ctx: &CompactionContext) -> Result<CompactionResult, MemoryError> {
351 let before = self.message_count(ctx.conversation_id).await?;
352 let messages_compacted = usize::try_from(before).unwrap_or(0);
353 // Trigger a summarization pass to reduce context below the token budget.
354 // The message_count parameter drives how many messages to summarize at once.
355 // Approximate: 4 chars per token; produce a target message count that
356 // keeps the resulting context under the token budget.
357 let target_msgs = ctx.token_budget.checked_div(4).unwrap_or(512);
358 let _ = self.summarize(ctx.conversation_id, target_msgs).await?;
359 let summary = self
360 .load_summaries(ctx.conversation_id)
361 .await?
362 .into_iter()
363 .map(|s| s.content)
364 .collect::<Vec<_>>()
365 .join("\n");
366 Ok(CompactionResult {
367 summary,
368 messages_compacted,
369 })
370 }
371}
372
373#[cfg(test)]
374mod tests {
375 use super::*;
376
377 #[tokio::test]
378 async fn remember_and_recall() {
379 let facade = InMemoryFacade::new();
380 let entry = MemoryEntry {
381 conversation_id: ConversationId(1),
382 role: "user".into(),
383 content: "Rust ownership model".into(),
384 parts: vec![],
385 metadata: None,
386 };
387 let id = facade.remember(entry).await.unwrap();
388 assert_eq!(id, MessageId(1));
389
390 let matches = facade.recall("ownership", 10).await.unwrap();
391 assert_eq!(matches.len(), 1);
392 assert_eq!(matches[0].content, "Rust ownership model");
393 assert_eq!(matches[0].source, MemorySource::Keyword);
394 }
395
396 #[tokio::test]
397 async fn recall_no_match() {
398 let facade = InMemoryFacade::new();
399 let entry = MemoryEntry {
400 conversation_id: ConversationId(1),
401 role: "user".into(),
402 content: "Rust ownership model".into(),
403 parts: vec![],
404 metadata: None,
405 };
406 facade.remember(entry).await.unwrap();
407 let matches = facade.recall("Python", 10).await.unwrap();
408 assert!(matches.is_empty());
409 }
410
411 #[tokio::test]
412 async fn summarize_joins_content() {
413 let facade = InMemoryFacade::new();
414 for content in ["Hello", "World"] {
415 facade
416 .remember(MemoryEntry {
417 conversation_id: ConversationId(1),
418 role: "user".into(),
419 content: content.into(),
420 parts: vec![],
421 metadata: None,
422 })
423 .await
424 .unwrap();
425 }
426 let summary = facade.summarize(ConversationId(1)).await.unwrap();
427 assert!(summary.contains("Hello") && summary.contains("World"));
428 }
429
430 #[tokio::test]
431 async fn compact_removes_conversation_entries() {
432 let facade = InMemoryFacade::new();
433 facade
434 .remember(MemoryEntry {
435 conversation_id: ConversationId(1),
436 role: "user".into(),
437 content: "entry 1".into(),
438 parts: vec![],
439 metadata: None,
440 })
441 .await
442 .unwrap();
443 facade
444 .remember(MemoryEntry {
445 conversation_id: ConversationId(2),
446 role: "user".into(),
447 content: "other conv".into(),
448 parts: vec![],
449 metadata: None,
450 })
451 .await
452 .unwrap();
453
454 let result = facade
455 .compact(&CompactionContext {
456 conversation_id: ConversationId(1),
457 token_budget: 100,
458 })
459 .await
460 .unwrap();
461
462 assert_eq!(result.messages_compacted, 1);
463 assert!(result.summary.contains("entry 1"));
464 // Other conversation untouched
465 assert_eq!(facade.len(), 1);
466 }
467
468 #[tokio::test]
469 async fn recall_respects_limit() {
470 let facade = InMemoryFacade::new();
471 for i in 0..5 {
472 facade
473 .remember(MemoryEntry {
474 conversation_id: ConversationId(1),
475 role: "user".into(),
476 content: format!("memory item {i}"),
477 parts: vec![],
478 metadata: None,
479 })
480 .await
481 .unwrap();
482 }
483 let matches = facade.recall("memory", 3).await.unwrap();
484 assert_eq!(matches.len(), 3);
485 }
486}