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
//! Public API methods for `SelfLearningMemory`
//!
//! This module contains all public methods that users interact with,
//! organized by functionality area.
use super::SelfLearningMemory;
use crate::error::Result;
use crate::memory::attribution::{
RecommendationFeedback, RecommendationSession, RecommendationStats,
};
use crate::monitoring::AgentMetrics;
use crate::procedural::ProceduralMemory;
use std::time::Duration;
// ============================================================================
// Monitoring and Statistics
// ============================================================================
impl SelfLearningMemory {
/// Get statistics about the memory system
pub async fn get_stats(&self) -> (usize, usize, usize) {
super::monitoring::get_stats(&self.episodes_fallback, &self.patterns_fallback).await
}
/// Record an agent execution for monitoring
pub async fn record_agent_execution(
&self,
agent_name: &str,
success: bool,
duration: Duration,
) -> Result<()> {
super::monitoring::record_agent_execution(
&self.agent_monitor,
agent_name,
success,
duration,
)
.await
}
/// Record detailed agent execution information
pub async fn record_agent_execution_detailed(
&self,
agent_name: &str,
success: bool,
duration: Duration,
task_description: Option<String>,
error_message: Option<String>,
) -> Result<()> {
super::monitoring::record_agent_execution_detailed(
&self.agent_monitor,
agent_name,
success,
duration,
task_description,
error_message,
)
.await
}
/// Get performance metrics for a specific agent
pub async fn get_agent_metrics(&self, agent_name: &str) -> Option<AgentMetrics> {
super::monitoring::get_agent_metrics(&self.agent_monitor, agent_name).await
}
/// Get metrics for all tracked agents
pub async fn get_all_agent_metrics(&self) -> std::collections::HashMap<String, AgentMetrics> {
super::monitoring::get_all_agent_metrics(&self.agent_monitor).await
}
/// Get monitoring system summary statistics
pub async fn get_monitoring_summary(&self) -> crate::monitoring::MonitoringSummary {
super::monitoring::get_monitoring_summary(&self.agent_monitor).await
}
/// Get query cache metrics (v0.1.12)
#[must_use]
pub fn get_cache_metrics(&self) -> crate::retrieval::CacheMetrics {
super::monitoring::get_cache_metrics(&self.query_cache)
}
/// Clear query cache metrics (v0.1.12)
pub fn clear_cache_metrics(&self) {
super::monitoring::clear_cache_metrics(&self.query_cache);
}
/// Clear all cached query results (v0.1.12)
pub fn clear_cache(&self) {
super::monitoring::clear_cache(&self.query_cache);
}
}
// ============================================================================
// Recommendation Attribution (ADR-044 Feature 2)
// ============================================================================
impl SelfLearningMemory {
/// Record a recommendation session when patterns/playbooks are suggested.
///
/// Call this when the system recommends patterns or playbooks to an agent.
/// This creates a record that can later be correlated with feedback.
///
/// # Arguments
///
/// * `session` - The recommendation session to record
///
/// # Example
///
/// ```no_run
/// use do_memory_core::SelfLearningMemory;
/// use do_memory_core::memory::attribution::RecommendationSession;
/// use uuid::Uuid;
///
/// # #[tokio::main]
/// # async fn main() {
/// let memory = SelfLearningMemory::new();
///
/// let session = RecommendationSession {
/// session_id: Uuid::new_v4(),
/// episode_id: Uuid::new_v4(),
/// timestamp: chrono::Utc::now(),
/// recommended_pattern_ids: vec!["pattern-123".to_string()],
/// recommended_playbook_ids: vec![],
/// };
///
/// memory.record_recommendation_session(session).await;
/// # }
/// ```
pub async fn record_recommendation_session(&self, session: RecommendationSession) {
self.recommendation_tracker
.record_session(session.clone())
.await;
self.persist_recommendation_session(&session).await;
}
/// Record feedback about a recommendation session.
///
/// Call this after an agent completes or abandons a task to indicate
/// which recommendations were used and the outcome.
///
/// # Arguments
///
/// * `feedback` - The feedback to record
///
/// # Returns
///
/// Returns `Ok(())` on success, or an error if the session doesn't exist.
///
/// # Example
///
/// ```no_run
/// use do_memory_core::SelfLearningMemory;
/// use do_memory_core::memory::attribution::RecommendationFeedback;
/// use do_memory_core::TaskOutcome;
/// use uuid::Uuid;
///
/// # #[tokio::main]
/// # async fn main() -> anyhow::Result<()> {
/// let memory = SelfLearningMemory::new();
///
/// let feedback = RecommendationFeedback {
/// session_id: Uuid::new_v4(),
/// applied_pattern_ids: vec!["pattern-123".to_string()],
/// consulted_episode_ids: vec![],
/// outcome: TaskOutcome::Success {
/// verdict: "Done".to_string(),
/// artifacts: vec![],
/// },
/// agent_rating: Some(0.9),
/// };
///
/// memory.record_recommendation_feedback(feedback).await?;
/// # Ok(())
/// # }
/// ```
pub async fn record_recommendation_feedback(
&self,
feedback: RecommendationFeedback,
) -> Result<()> {
self.recommendation_tracker
.record_feedback(feedback.clone())
.await?;
self.persist_recommendation_feedback(&feedback).await;
Ok(())
}
/// Get the recommendation session for an episode.
///
/// Returns the session that was recorded when recommendations were made
/// for the specified episode.
pub async fn get_recommendation_session_for_episode(
&self,
episode_id: uuid::Uuid,
) -> Option<RecommendationSession> {
if let Some(session) = self
.recommendation_tracker
.get_session_for_episode(episode_id)
.await
{
return Some(session);
}
self.fetch_session_for_episode_from_storage(episode_id)
.await
}
/// Get feedback for a recommendation session.
pub async fn get_recommendation_feedback(
&self,
session_id: uuid::Uuid,
) -> Option<RecommendationFeedback> {
if let Some(feedback) = self.recommendation_tracker.get_feedback(session_id).await {
return Some(feedback);
}
self.fetch_feedback_from_storage(session_id).await
}
/// Get overall recommendation effectiveness statistics.
///
/// Returns aggregate metrics about recommendation adoption rates,
/// success rates, and agent ratings.
pub async fn get_recommendation_stats(&self) -> RecommendationStats {
if let Some(stats) = self.fetch_recommendation_stats_from_storage().await {
return stats;
}
self.recommendation_tracker.get_stats().await
}
}
// ============================================================================
// Procedural Memory API
// ============================================================================
impl SelfLearningMemory {
/// Store a procedural memory
pub async fn store_procedural_memory(&self, procedural: ProceduralMemory) -> Result<()> {
// Store in durable storage
if let Some(storage) = &self.turso_storage {
storage.store_procedural(&procedural).await?;
}
// Store in cache
if let Some(storage) = &self.cache_storage {
let _ = storage.store_procedural(&procedural).await;
}
// Always store in fallback for in-memory access
let mut procedural_fallback = self.procedural_fallback.write().await;
procedural_fallback.insert(procedural.id, procedural.clone());
Ok(())
}
/// Retrieve a procedural memory by ID
pub async fn get_procedural_memory(&self, id: uuid::Uuid) -> Result<Option<ProceduralMemory>> {
// Try cache first
if let Some(storage) = &self.cache_storage {
if let Ok(Some(procedural)) = storage.get_procedural(id).await {
return Ok(Some(procedural));
}
}
// Try durable storage
if let Some(storage) = &self.turso_storage {
match storage.get_procedural(id).await {
Ok(Some(procedural)) => {
// Update cache
if let Some(cache) = &self.cache_storage {
let _ = cache.store_procedural(&procedural).await;
}
return Ok(Some(procedural));
}
Ok(None) => {}
Err(e) => return Err(e),
}
}
// Fallback to in-memory
let procedural_fallback = self.procedural_fallback.read().await;
Ok(procedural_fallback.get(&id).cloned())
}
/// Delete a procedural memory by ID
pub async fn delete_procedural_memory(&self, id: uuid::Uuid) -> Result<()> {
// Delete from durable storage
if let Some(storage) = &self.turso_storage {
storage.delete_procedural(id).await?;
}
// Delete from cache
if let Some(storage) = &self.cache_storage {
let _ = storage.delete_procedural(id).await;
}
// Delete from fallback
let mut procedural_fallback = self.procedural_fallback.write().await;
procedural_fallback.remove(&id);
Ok(())
}
/// Query procedural memories
pub async fn query_procedural_memory(
&self,
limit: Option<usize>,
) -> Result<Vec<ProceduralMemory>> {
// Try durable storage first (source of truth)
if let Some(storage) = &self.turso_storage {
return storage.query_procedural(limit).await;
}
// Try cache storage if no durable storage available
if let Some(storage) = &self.cache_storage {
if let Ok(results) = storage.query_procedural(limit).await {
return Ok(results);
}
}
// Fallback to in-memory
let procedural = self.procedural_fallback.read().await;
let mut results: Vec<ProceduralMemory> = procedural.values().cloned().collect();
results.sort_by_key(|b| std::cmp::Reverse(b.updated_at));
if let Some(l) = limit {
results.truncate(l);
}
Ok(results)
}
}