Skip to main content

zeph_common/
memory.rs

1// SPDX-FileCopyrightText: 2026 Andrei G <bug-ops>
2// SPDX-License-Identifier: MIT OR Apache-2.0
3
4//! Shared memory interface types used by both `zeph-memory` (Layer 1) and
5//! `zeph-context` (Layer 1) without a cross-layer dependency.
6//!
7//! Moving these pure interface types here resolves the same-layer violation
8//! `zeph-context → zeph-memory` (issue #3665).
9
10use std::fmt;
11use std::str::FromStr;
12
13use serde::{Deserialize, Serialize};
14
15// ── MemoryRoute ───────────────────────────────────────────────────────────────
16
17/// Classification of which memory backend(s) to query.
18///
19/// Used in routing configuration and at runtime to dispatch memory operations.
20/// Serialises with `snake_case` names (`keyword`, `semantic`, `hybrid`, `graph`, `episodic`).
21#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, serde::Serialize, serde::Deserialize)]
22#[serde(rename_all = "snake_case")]
23#[non_exhaustive]
24pub enum MemoryRoute {
25    /// Full-text search only (`SQLite` FTS5). Fast, good for keyword/exact queries.
26    Keyword,
27    /// Vector search only (Qdrant). Good for semantic/conceptual queries.
28    Semantic,
29    /// Both backends, results merged by reciprocal rank fusion.
30    #[default]
31    Hybrid,
32    /// Graph-based retrieval via BFS traversal.
33    Graph,
34    /// FTS5 search with a timestamp-range filter. Used for temporal/episodic queries.
35    Episodic,
36}
37
38/// Routing decision with confidence and optional LLM reasoning.
39#[derive(Debug, Clone)]
40pub struct RoutingDecision {
41    pub route: MemoryRoute,
42    /// Confidence in `[0, 1]`. `1.0` = certain, `0.5` = ambiguous.
43    pub confidence: f32,
44    /// Only populated when an LLM classifier was used.
45    pub reasoning: Option<String>,
46}
47
48/// Decides which memory backend(s) to query for a given input.
49pub trait MemoryRouter: Send + Sync {
50    /// Route a query to the appropriate backend(s).
51    fn route(&self, query: &str) -> MemoryRoute;
52
53    /// Route with a confidence signal. Default implementation wraps `route()` with confidence 1.0.
54    fn route_with_confidence(&self, query: &str) -> RoutingDecision {
55        RoutingDecision {
56            route: self.route(query),
57            confidence: 1.0,
58            reasoning: None,
59        }
60    }
61}
62
63/// Async extension for LLM-capable routers.
64pub trait AsyncMemoryRouter: MemoryRouter {
65    fn route_async<'a>(
66        &'a self,
67        query: &'a str,
68    ) -> std::pin::Pin<Box<dyn std::future::Future<Output = RoutingDecision> + Send + 'a>>;
69}
70
71// ── RecallView ────────────────────────────────────────────────────────────────
72
73/// Enrichment level for view-aware graph recall.
74///
75/// # Examples
76///
77/// ```
78/// use zeph_common::memory::RecallView;
79///
80/// assert_eq!(RecallView::default(), RecallView::Head);
81/// ```
82#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
83#[non_exhaustive]
84pub enum RecallView {
85    /// Standard retrieval — no enrichment beyond what the base method provides.
86    #[default]
87    Head,
88    /// Retrieval + source-message provenance.
89    ZoomIn,
90    /// Retrieval + 1-hop neighbor expansion.
91    ZoomOut,
92}
93
94// ── CompressionLevel ─────────────────────────────────────────────────────────
95
96/// The three abstraction levels in the compression spectrum.
97#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
98#[non_exhaustive]
99pub enum CompressionLevel {
100    /// Raw episodic messages — full fidelity, high token cost.
101    Episodic,
102    /// Abstracted procedural knowledge (how-to, tool patterns).
103    Procedural,
104    /// Stable declarative facts and reference material.
105    Declarative,
106}
107
108impl CompressionLevel {
109    /// A relative token-cost factor for budgeting purposes.
110    ///
111    /// `Episodic = 1.0` (baseline), `Procedural = 0.6`, `Declarative = 0.3`.
112    #[must_use]
113    pub const fn cost_factor(self) -> f32 {
114        match self {
115            Self::Episodic => 1.0,
116            Self::Procedural => 0.6,
117            Self::Declarative => 0.3,
118        }
119    }
120}
121
122// ── AnchoredSummary ───────────────────────────────────────────────────────────
123
124/// Structured compaction summary with anchored sections.
125///
126/// Produced by the structured summarization path during hard compaction.
127/// Replaces the free-form 9-section prose when `[memory] structured_summaries = true`.
128#[derive(Debug, Clone, Serialize, Deserialize)]
129#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
130pub struct AnchoredSummary {
131    /// What the user is ultimately trying to accomplish in this session.
132    pub session_intent: String,
133    /// File paths, function names, structs/enums touched or referenced.
134    pub files_modified: Vec<String>,
135    /// Architectural or implementation decisions made, with rationale.
136    pub decisions_made: Vec<String>,
137    /// Unresolved questions, ambiguities, or blocked items.
138    pub open_questions: Vec<String>,
139    /// Concrete next actions the agent should take immediately.
140    pub next_steps: Vec<String>,
141}
142
143impl AnchoredSummary {
144    /// Returns true if the mandatory sections (`session_intent`, `next_steps`) are populated.
145    #[must_use]
146    pub fn is_complete(&self) -> bool {
147        !self.session_intent.trim().is_empty() && !self.next_steps.is_empty()
148    }
149
150    /// Render as Markdown for context injection into the LLM.
151    #[must_use]
152    pub fn to_markdown(&self) -> String {
153        let mut out = String::with_capacity(512);
154        out.push_str("[anchored summary]\n");
155        out.push_str("## Session Intent\n");
156        out.push_str(&self.session_intent);
157        out.push('\n');
158
159        if !self.files_modified.is_empty() {
160            out.push_str("\n## Files Modified\n");
161            for entry in &self.files_modified {
162                let clean = entry.trim_start_matches("- ");
163                out.push_str("- ");
164                out.push_str(clean);
165                out.push('\n');
166            }
167        }
168
169        if !self.decisions_made.is_empty() {
170            out.push_str("\n## Decisions Made\n");
171            for entry in &self.decisions_made {
172                let clean = entry.trim_start_matches("- ");
173                out.push_str("- ");
174                out.push_str(clean);
175                out.push('\n');
176            }
177        }
178
179        if !self.open_questions.is_empty() {
180            out.push_str("\n## Open Questions\n");
181            for entry in &self.open_questions {
182                let clean = entry.trim_start_matches("- ");
183                out.push_str("- ");
184                out.push_str(clean);
185                out.push('\n');
186            }
187        }
188
189        if !self.next_steps.is_empty() {
190            out.push_str("\n## Next Steps\n");
191            for entry in &self.next_steps {
192                let clean = entry.trim_start_matches("- ");
193                out.push_str("- ");
194                out.push_str(clean);
195                out.push('\n');
196            }
197        }
198
199        out
200    }
201
202    /// Validate per-field length limits to guard against bloated LLM output.
203    ///
204    /// # Errors
205    ///
206    /// Returns `Err` with a descriptive message if any field exceeds its limit.
207    pub fn validate(&self) -> Result<(), String> {
208        const MAX_INTENT: usize = 2_000;
209        const MAX_ENTRY: usize = 500;
210        const MAX_VEC_LEN: usize = 50;
211
212        if self.session_intent.len() > MAX_INTENT {
213            return Err(format!(
214                "session_intent exceeds {MAX_INTENT} chars (got {})",
215                self.session_intent.len()
216            ));
217        }
218        for (field, entries) in [
219            ("files_modified", &self.files_modified),
220            ("decisions_made", &self.decisions_made),
221            ("open_questions", &self.open_questions),
222            ("next_steps", &self.next_steps),
223        ] {
224            if entries.len() > MAX_VEC_LEN {
225                return Err(format!(
226                    "{field} has {} entries (max {MAX_VEC_LEN})",
227                    entries.len()
228                ));
229            }
230            for entry in entries {
231                if entry.len() > MAX_ENTRY {
232                    return Err(format!(
233                        "{field} entry exceeds {MAX_ENTRY} chars (got {})",
234                        entry.len()
235                    ));
236                }
237            }
238        }
239        Ok(())
240    }
241
242    /// Serialize to JSON for storage in `summaries.content`.
243    ///
244    /// # Panics
245    ///
246    /// Panics if serialization fails. Since all fields are `String`/`Vec<String>`,
247    /// serialization is infallible in practice.
248    #[must_use]
249    pub fn to_json(&self) -> String {
250        serde_json::to_string(self).expect("AnchoredSummary serialization is infallible")
251    }
252}
253
254// ── SpreadingActivationParams ─────────────────────────────────────────────────
255
256/// Parameters for spreading activation graph retrieval.
257#[derive(Debug, Clone)]
258pub struct SpreadingActivationParams {
259    pub decay_lambda: f32,
260    pub max_hops: u32,
261    pub activation_threshold: f32,
262    pub inhibition_threshold: f32,
263    pub max_activated_nodes: usize,
264    pub temporal_decay_rate: f64,
265    /// Weight of structural score in hybrid seed ranking. Range: `[0.0, 1.0]`. Default: `0.4`.
266    pub seed_structural_weight: f32,
267    /// Maximum seeds per community ID. `0` = unlimited. Default: `3`.
268    pub seed_community_cap: usize,
269}
270
271// ── EdgeType ──────────────────────────────────────────────────────────────────
272
273/// MAGMA edge type: the semantic category of a relationship between two entities.
274#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
275#[serde(rename_all = "snake_case")]
276#[non_exhaustive]
277pub enum EdgeType {
278    #[default]
279    Semantic,
280    Temporal,
281    Causal,
282    Entity,
283}
284
285impl EdgeType {
286    /// Return the canonical lowercase string for this edge type.
287    ///
288    /// # Examples
289    ///
290    /// ```
291    /// use zeph_common::memory::EdgeType;
292    ///
293    /// assert_eq!(EdgeType::Causal.as_str(), "causal");
294    /// ```
295    #[must_use]
296    pub const fn as_str(self) -> &'static str {
297        match self {
298            Self::Semantic => "semantic",
299            Self::Temporal => "temporal",
300            Self::Causal => "causal",
301            Self::Entity => "entity",
302        }
303    }
304}
305
306impl fmt::Display for EdgeType {
307    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
308        f.write_str(self.as_str())
309    }
310}
311
312impl FromStr for EdgeType {
313    type Err = String;
314
315    fn from_str(s: &str) -> Result<Self, Self::Err> {
316        match s {
317            "semantic" => Ok(Self::Semantic),
318            "temporal" => Ok(Self::Temporal),
319            "causal" => Ok(Self::Causal),
320            "entity" => Ok(Self::Entity),
321            other => Err(format!("unknown edge type: {other}")),
322        }
323    }
324}
325
326// ── Marker constants ──────────────────────────────────────────────────────────
327
328/// MAGMA causal edge markers used by `classify_graph_subgraph`.
329pub const CAUSAL_MARKERS: &[&str] = &[
330    "why",
331    "because",
332    "caused",
333    "cause",
334    "reason",
335    "result",
336    "led to",
337    "consequence",
338    "trigger",
339    "effect",
340    "blame",
341    "fault",
342];
343
344/// MAGMA temporal edge markers for subgraph classification.
345pub const TEMPORAL_MARKERS: &[&str] = &[
346    "before", "after", "first", "then", "timeline", "sequence", "preceded", "followed", "started",
347    "ended", "during", "prior",
348];
349
350/// MAGMA entity/structural markers.
351pub const ENTITY_MARKERS: &[&str] = &[
352    "is a",
353    "type of",
354    "kind of",
355    "part of",
356    "instance",
357    "same as",
358    "alias",
359    "subtype",
360    "subclass",
361    "belongs to",
362];
363
364/// Single-word temporal tokens that require word-boundary checking.
365pub const WORD_BOUNDARY_TEMPORAL: &[&str] = &["ago"];
366
367/// Classify a query into MAGMA edge types to use for subgraph-scoped BFS retrieval.
368///
369/// Pure heuristic, zero latency — no LLM call. Returns a prioritised list of [`EdgeType`]s.
370///
371/// # Example
372///
373/// ```
374/// use zeph_common::memory::{classify_graph_subgraph, EdgeType};
375///
376/// let types = classify_graph_subgraph("why did X happen");
377/// assert!(types.contains(&EdgeType::Causal));
378/// assert!(types.contains(&EdgeType::Semantic));
379/// ```
380#[must_use]
381pub fn classify_graph_subgraph(query: &str) -> Vec<EdgeType> {
382    let lower = query.to_ascii_lowercase();
383    let mut types: Vec<EdgeType> = Vec::new();
384
385    if CAUSAL_MARKERS.iter().any(|m| lower.contains(m)) {
386        types.push(EdgeType::Causal);
387    }
388    if TEMPORAL_MARKERS.iter().any(|m| lower.contains(m)) {
389        types.push(EdgeType::Temporal);
390    }
391    if ENTITY_MARKERS.iter().any(|m| lower.contains(m)) {
392        types.push(EdgeType::Entity);
393    }
394
395    if !types.contains(&EdgeType::Semantic) {
396        types.push(EdgeType::Semantic);
397    }
398
399    types
400}
401
402/// Parse a route name string into a [`MemoryRoute`], falling back to `fallback` on unknown values.
403///
404/// # Examples
405///
406/// ```
407/// use zeph_common::memory::{parse_route_str, MemoryRoute};
408///
409/// assert_eq!(parse_route_str("semantic", MemoryRoute::Hybrid), MemoryRoute::Semantic);
410/// assert_eq!(parse_route_str("unknown", MemoryRoute::Hybrid), MemoryRoute::Hybrid);
411/// ```
412#[must_use]
413pub fn parse_route_str(s: &str, fallback: MemoryRoute) -> MemoryRoute {
414    match s {
415        "keyword" => MemoryRoute::Keyword,
416        "semantic" => MemoryRoute::Semantic,
417        "hybrid" => MemoryRoute::Hybrid,
418        "graph" => MemoryRoute::Graph,
419        "episodic" => MemoryRoute::Episodic,
420        _ => fallback,
421    }
422}
423
424// ── TokenCounting trait ───────────────────────────────────────────────────────
425
426/// Minimal token-counting interface used by `zeph-context` for budget enforcement.
427///
428/// Defined here in Layer 0 so `zeph-context` can accept a `&dyn TokenCounting`
429/// without importing `zeph-memory`. `zeph-memory::TokenCounter` implements this trait.
430pub trait TokenCounting: Send + Sync {
431    /// Count tokens in a plain text string.
432    fn count_tokens(&self, text: &str) -> usize;
433    /// Count tokens for a JSON schema value (tool definitions).
434    fn count_tool_schema_tokens(&self, schema: &serde_json::Value) -> usize;
435}
436
437// ── Context memory DTOs ───────────────────────────────────────────────────────
438//
439// Plain data-transfer structs used by `ContextMemoryBackend`. They mirror the
440// fields that `zeph-context::assembler` actually reads from `zeph-memory` row
441// types. Keeping them here (Layer 0) allows `zeph-context` (Layer 1) to depend
442// only on `zeph-common` rather than `zeph-memory`.
443
444/// A persona fact row projection used by context assembly.
445#[derive(Debug, Clone)]
446pub struct MemPersonaFact {
447    /// Fact category label (e.g. `"preference"`, `"domain"`).
448    pub category: String,
449    /// Fact content injected into the system prompt.
450    pub content: String,
451}
452
453/// A memory tree node projection used by context assembly.
454#[derive(Debug, Clone)]
455pub struct MemTreeNode {
456    /// Node content injected into the system prompt.
457    pub content: String,
458}
459
460/// A conversation summary projection used by context assembly.
461#[derive(Debug, Clone)]
462pub struct MemSummary {
463    /// Row ID of the first message covered by this summary, if known.
464    pub first_message_id: Option<i64>,
465    /// Row ID of the last message covered by this summary, if known.
466    pub last_message_id: Option<i64>,
467    /// Summary text.
468    pub content: String,
469}
470
471/// A reasoning strategy projection used by context assembly.
472#[derive(Debug, Clone)]
473pub struct MemReasoningStrategy {
474    /// Unique strategy identifier (used by `mark_reasoning_used`).
475    pub id: String,
476    /// Outcome label (e.g. `"success"`, `"failure"`).
477    pub outcome: String,
478    /// Distilled strategy summary injected into the system prompt.
479    pub summary: String,
480}
481
482/// A user correction projection used by context assembly.
483#[derive(Debug, Clone)]
484pub struct MemCorrection {
485    /// The correction text to inject into the system prompt.
486    pub correction_text: String,
487}
488
489/// A recalled message projection used by context assembly.
490#[derive(Debug, Clone)]
491pub struct MemRecalledMessage {
492    /// Message role: `"user"`, `"assistant"`, or `"system"`.
493    pub role: String,
494    /// Message content.
495    pub content: String,
496    /// Similarity score in `[0, 1]`.
497    pub score: f32,
498}
499
500/// A neighbor fact in a graph recall result.
501#[derive(Debug, Clone)]
502pub struct MemGraphNeighbor {
503    /// Neighbor fact text.
504    pub fact: String,
505    /// Confidence score in `[0, 1]`.
506    pub confidence: f32,
507}
508
509/// A graph fact projection used by context assembly.
510#[derive(Debug, Clone)]
511pub struct MemGraphFact {
512    /// Fact text.
513    pub fact: String,
514    /// Confidence score in `[0, 1]`.
515    pub confidence: f32,
516    /// Spreading-activation score, if applicable.
517    pub activation_score: Option<f32>,
518    /// `ZoomOut` 1-hop neighbors, if view-aware expansion was requested.
519    pub neighbors: Vec<MemGraphNeighbor>,
520    /// `ZoomIn` provenance snippet, if view-aware provenance was requested.
521    pub provenance_snippet: Option<String>,
522}
523
524/// A cross-session summary search result used by context assembly.
525#[derive(Debug, Clone)]
526pub struct MemSessionSummary {
527    /// Summary text from the matched session.
528    pub summary_text: String,
529    /// Similarity score in `[0, 1]`.
530    pub score: f32,
531}
532
533/// A document chunk search result used by context assembly.
534#[derive(Debug, Clone)]
535pub struct MemDocumentChunk {
536    /// Chunk text extracted from the `"text"` payload key.
537    pub text: String,
538}
539
540/// A trajectory entry projection used by context assembly.
541#[derive(Debug, Clone)]
542pub struct MemTrajectoryEntry {
543    /// Intent description for the trajectory entry.
544    pub intent: String,
545    /// Outcome description.
546    pub outcome: String,
547    /// Confidence score in `[0, 1]`.
548    pub confidence: f64,
549}
550
551// ── GraphRecallParams ─────────────────────────────────────────────────────────
552
553/// Parameters for a graph-view recall call, used by [`ContextMemoryBackend::recall_graph_facts`].
554#[derive(Debug)]
555pub struct GraphRecallParams<'a> {
556    /// Maximum number of graph facts to return.
557    pub limit: usize,
558    /// Enrichment view (head, zoom-in, zoom-out).
559    pub view: RecallView,
560    /// Cap on `ZoomOut` neighbor expansion.
561    pub zoom_out_neighbor_cap: usize,
562    /// Maximum BFS hops during graph traversal.
563    pub max_hops: u32,
564    /// Rate at which older facts are downweighted.
565    pub temporal_decay_rate: f64,
566    /// Edge type filters for subgraph-scoped BFS.
567    pub edge_types: &'a [EdgeType],
568    /// Spreading activation parameters. `None` disables spreading activation.
569    pub spreading_activation: Option<SpreadingActivationParams>,
570}
571
572// ── ContextMemoryBackend trait ────────────────────────────────────────────────
573
574/// Abstraction over `SemanticMemory` that `zeph-context` uses for all memory
575/// operations during context assembly.
576///
577/// Defined in Layer 0 (`zeph-common`) so that `zeph-context` (Layer 1) can hold
578/// `Option<Arc<dyn ContextMemoryBackend>>` without importing `zeph-memory`.
579/// `zeph-core` (Layer 4) provides the concrete implementation that wraps
580/// `SemanticMemory`.
581///
582/// All async methods use `Pin<Box<dyn Future<...>>>` for dyn-compatibility.
583#[allow(clippy::type_complexity)]
584pub trait ContextMemoryBackend: Send + Sync {
585    /// Load persona facts with at least `min_confidence`.
586    fn load_persona_facts<'a>(
587        &'a self,
588        min_confidence: f64,
589    ) -> std::pin::Pin<
590        Box<
591            dyn std::future::Future<
592                    Output = Result<Vec<MemPersonaFact>, Box<dyn std::error::Error + Send + Sync>>,
593                > + Send
594                + 'a,
595        >,
596    >;
597
598    /// Load `top_k` trajectory entries for the given `tier` filter (e.g. `"procedural"`).
599    fn load_trajectory_entries<'a>(
600        &'a self,
601        tier: Option<&'a str>,
602        top_k: usize,
603    ) -> std::pin::Pin<
604        Box<
605            dyn std::future::Future<
606                    Output = Result<
607                        Vec<MemTrajectoryEntry>,
608                        Box<dyn std::error::Error + Send + Sync>,
609                    >,
610                > + Send
611                + 'a,
612        >,
613    >;
614
615    /// Load `top_k` memory tree nodes at the given level.
616    fn load_tree_nodes<'a>(
617        &'a self,
618        level: u32,
619        top_k: usize,
620    ) -> std::pin::Pin<
621        Box<
622            dyn std::future::Future<
623                    Output = Result<Vec<MemTreeNode>, Box<dyn std::error::Error + Send + Sync>>,
624                > + Send
625                + 'a,
626        >,
627    >;
628
629    /// Load all summaries for the given conversation (raw row ID).
630    fn load_summaries<'a>(
631        &'a self,
632        conversation_id: i64,
633    ) -> std::pin::Pin<
634        Box<
635            dyn std::future::Future<
636                    Output = Result<Vec<MemSummary>, Box<dyn std::error::Error + Send + Sync>>,
637                > + Send
638                + 'a,
639        >,
640    >;
641
642    /// Retrieve the top-`top_k` reasoning strategies for `query`.
643    fn retrieve_reasoning_strategies<'a>(
644        &'a self,
645        query: &'a str,
646        top_k: usize,
647    ) -> std::pin::Pin<
648        Box<
649            dyn std::future::Future<
650                    Output = Result<
651                        Vec<MemReasoningStrategy>,
652                        Box<dyn std::error::Error + Send + Sync>,
653                    >,
654                > + Send
655                + 'a,
656        >,
657    >;
658
659    /// Mark reasoning strategies as used (fire-and-forget; best-effort).
660    fn mark_reasoning_used<'a>(
661        &'a self,
662        ids: &'a [String],
663    ) -> std::pin::Pin<
664        Box<
665            dyn std::future::Future<Output = Result<(), Box<dyn std::error::Error + Send + Sync>>>
666                + Send
667                + 'a,
668        >,
669    >;
670
671    /// Retrieve corrections similar to `query`, up to `limit` with `min_score`.
672    fn retrieve_corrections<'a>(
673        &'a self,
674        query: &'a str,
675        limit: usize,
676        min_score: f32,
677    ) -> std::pin::Pin<
678        Box<
679            dyn std::future::Future<
680                    Output = Result<Vec<MemCorrection>, Box<dyn std::error::Error + Send + Sync>>,
681                > + Send
682                + 'a,
683        >,
684    >;
685
686    /// Recall semantically similar messages for `query`, up to `limit`.
687    fn recall<'a>(
688        &'a self,
689        query: &'a str,
690        limit: usize,
691        router: Option<&'a dyn AsyncMemoryRouter>,
692    ) -> std::pin::Pin<
693        Box<
694            dyn std::future::Future<
695                    Output = Result<
696                        Vec<MemRecalledMessage>,
697                        Box<dyn std::error::Error + Send + Sync>,
698                    >,
699                > + Send
700                + 'a,
701        >,
702    >;
703
704    /// Recall graph facts for `query` with view-aware enrichment.
705    fn recall_graph_facts<'a>(
706        &'a self,
707        query: &'a str,
708        params: GraphRecallParams<'a>,
709    ) -> std::pin::Pin<
710        Box<
711            dyn std::future::Future<
712                    Output = Result<Vec<MemGraphFact>, Box<dyn std::error::Error + Send + Sync>>,
713                > + Send
714                + 'a,
715        >,
716    >;
717
718    /// Search cross-session summaries for `query`, excluding `current_conversation_id`.
719    fn search_session_summaries<'a>(
720        &'a self,
721        query: &'a str,
722        limit: usize,
723        current_conversation_id: Option<i64>,
724    ) -> std::pin::Pin<
725        Box<
726            dyn std::future::Future<
727                    Output = Result<
728                        Vec<MemSessionSummary>,
729                        Box<dyn std::error::Error + Send + Sync>,
730                    >,
731                > + Send
732                + 'a,
733        >,
734    >;
735
736    /// Search a named document collection for `query`, returning `top_k` chunks.
737    fn search_document_collection<'a>(
738        &'a self,
739        collection: &'a str,
740        query: &'a str,
741        top_k: usize,
742    ) -> std::pin::Pin<
743        Box<
744            dyn std::future::Future<
745                    Output = Result<
746                        Vec<MemDocumentChunk>,
747                        Box<dyn std::error::Error + Send + Sync>,
748                    >,
749                > + Send
750                + 'a,
751        >,
752    >;
753}
754
755#[cfg(test)]
756mod tests {
757    use super::MemoryRoute;
758
759    #[test]
760    fn memory_route_serde_roundtrip() {
761        let cases = [
762            ("\"keyword\"", MemoryRoute::Keyword),
763            ("\"semantic\"", MemoryRoute::Semantic),
764            ("\"hybrid\"", MemoryRoute::Hybrid),
765            ("\"graph\"", MemoryRoute::Graph),
766            ("\"episodic\"", MemoryRoute::Episodic),
767        ];
768        for (json_str, expected) in cases {
769            let got: MemoryRoute = serde_json::from_str(json_str).unwrap();
770            assert_eq!(got, expected);
771            let serialized = serde_json::to_string(&got).unwrap();
772            let roundtrip: MemoryRoute = serde_json::from_str(&serialized).unwrap();
773            assert_eq!(roundtrip, expected);
774        }
775    }
776
777    #[test]
778    fn memory_route_default_is_hybrid() {
779        assert_eq!(MemoryRoute::default(), MemoryRoute::Hybrid);
780    }
781}