Skip to main content

mnemo_core/storage/
mod.rs

1pub mod cold;
2pub mod duckdb;
3pub mod migrations;
4
5use crate::error::Result;
6use crate::model::acl::{Acl, Permission};
7use crate::model::agent_profile::AgentProfile;
8use crate::model::checkpoint::Checkpoint;
9use crate::model::delegation::Delegation;
10use crate::model::embedding_baseline::EmbeddingBaseline;
11use crate::model::event::AgentEvent;
12use crate::model::memory::MemoryRecord;
13use crate::model::relation::Relation;
14use uuid::Uuid;
15
16#[derive(Debug, Clone, Default)]
17pub struct MemoryFilter {
18    pub agent_id: Option<String>,
19    pub memory_type: Option<crate::model::memory::MemoryType>,
20    pub scope: Option<crate::model::memory::Scope>,
21    pub tags: Option<Vec<String>>,
22    pub min_importance: Option<f32>,
23    pub org_id: Option<String>,
24    pub thread_id: Option<String>,
25    pub include_deleted: bool,
26}
27
28#[async_trait::async_trait]
29pub trait StorageBackend: Send + Sync {
30    // Memory CRUD
31    async fn insert_memory(&self, record: &MemoryRecord) -> Result<()>;
32    async fn get_memory(&self, id: Uuid) -> Result<Option<MemoryRecord>>;
33    async fn update_memory(&self, record: &MemoryRecord) -> Result<()>;
34    async fn soft_delete_memory(&self, id: Uuid) -> Result<()>;
35    async fn hard_delete_memory(&self, id: Uuid) -> Result<()>;
36    async fn list_memories(
37        &self,
38        filter: &MemoryFilter,
39        limit: usize,
40        offset: usize,
41    ) -> Result<Vec<MemoryRecord>>;
42    async fn touch_memory(&self, id: Uuid) -> Result<()>;
43
44    // ACL
45    async fn insert_acl(&self, acl: &Acl) -> Result<()>;
46    async fn check_permission(
47        &self,
48        memory_id: Uuid,
49        principal_id: &str,
50        required: Permission,
51    ) -> Result<bool>;
52
53    // Relations
54    async fn insert_relation(&self, relation: &Relation) -> Result<()>;
55    async fn get_relations_from(&self, source_id: Uuid) -> Result<Vec<Relation>>;
56    async fn get_relations_to(&self, target_id: Uuid) -> Result<Vec<Relation>>;
57    async fn delete_relation(&self, id: Uuid) -> Result<()>;
58
59    // Chain linking
60    async fn get_latest_memory_hash(
61        &self,
62        agent_id: &str,
63        thread_id: Option<&str>,
64    ) -> Result<Option<Vec<u8>>>;
65    async fn get_latest_event_hash(
66        &self,
67        agent_id: &str,
68        thread_id: Option<&str>,
69    ) -> Result<Option<Vec<u8>>>;
70
71    // Sync watermarks
72    async fn get_sync_watermark(&self, key: &str) -> Result<Option<String>>;
73    async fn set_sync_watermark(&self, key: &str, value: &str) -> Result<()>;
74
75    // Permission-safe ANN
76    async fn list_accessible_memory_ids(&self, agent_id: &str, limit: usize) -> Result<Vec<Uuid>>;
77
78    // Events
79    async fn insert_event(&self, event: &AgentEvent) -> Result<()>;
80    async fn list_events(
81        &self,
82        agent_id: &str,
83        limit: usize,
84        offset: usize,
85    ) -> Result<Vec<AgentEvent>>;
86    async fn get_events_by_thread(&self, thread_id: &str, limit: usize) -> Result<Vec<AgentEvent>>;
87    async fn get_event(&self, id: Uuid) -> Result<Option<AgentEvent>>;
88    async fn list_child_events(
89        &self,
90        parent_event_id: Uuid,
91        limit: usize,
92    ) -> Result<Vec<AgentEvent>>;
93
94    // Ordered listing for chain verification
95    async fn list_memories_by_agent_ordered(
96        &self,
97        agent_id: &str,
98        thread_id: Option<&str>,
99        limit: usize,
100    ) -> Result<Vec<MemoryRecord>>;
101
102    // Sync support
103    async fn list_memories_since(
104        &self,
105        updated_after: &str,
106        limit: usize,
107    ) -> Result<Vec<MemoryRecord>>;
108    async fn upsert_memory(&self, record: &MemoryRecord) -> Result<()>;
109
110    // Expired memory cleanup
111    async fn cleanup_expired(&self) -> Result<usize>;
112
113    // Delegations
114    async fn insert_delegation(&self, d: &Delegation) -> Result<()>;
115    async fn list_delegations_for(&self, delegate_id: &str) -> Result<Vec<Delegation>>;
116    async fn revoke_delegation(&self, id: Uuid) -> Result<()>;
117    async fn check_delegation(
118        &self,
119        delegate_id: &str,
120        memory_id: Uuid,
121        required: Permission,
122    ) -> Result<bool>;
123
124    // Agent Profiles
125    async fn insert_or_update_agent_profile(&self, profile: &AgentProfile) -> Result<()>;
126    async fn get_agent_profile(&self, agent_id: &str) -> Result<Option<AgentProfile>>;
127
128    // Embedding baselines (v0.3.3, z-score outlier detector)
129    async fn insert_or_update_embedding_baseline(&self, baseline: &EmbeddingBaseline)
130    -> Result<()>;
131    async fn get_embedding_baseline(&self, agent_id: &str) -> Result<Option<EmbeddingBaseline>>;
132
133    // Checkpoints
134    async fn insert_checkpoint(&self, cp: &Checkpoint) -> Result<()>;
135    async fn get_checkpoint(&self, id: Uuid) -> Result<Option<Checkpoint>>;
136    async fn list_checkpoints(
137        &self,
138        thread_id: &str,
139        branch: Option<&str>,
140        limit: usize,
141    ) -> Result<Vec<Checkpoint>>;
142    async fn get_latest_checkpoint(
143        &self,
144        thread_id: &str,
145        branch: &str,
146    ) -> Result<Option<Checkpoint>>;
147}