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
//! Episode management operations
//!
//! This module provides episode lifecycle management operations including
//! deletion and archival functionality.
use crate::error::{Error, Result};
use chrono::Utc;
use std::sync::Arc;
use tracing::{debug, info, warn};
use uuid::Uuid;
use super::SelfLearningMemory;
impl SelfLearningMemory {
/// Delete an episode permanently from all storage backends.
///
/// This operation removes an episode from:
/// - In-memory cache
/// - Cache storage (redb)
/// - Durable storage (Turso)
///
/// **Warning**: This operation cannot be undone. The episode and all associated
/// data will be permanently deleted.
///
/// # Arguments
///
/// * `episode_id` - UUID of the episode to delete
///
/// # Returns
///
/// `Ok(())` if deletion succeeds, or an error if the operation fails.
///
/// # Errors
///
/// Returns `Error::NotFound` if the episode doesn't exist in any storage backend.
/// Returns `Error::Storage` if deletion fails in any backend.
///
/// # Examples
///
/// ```
/// use do_memory_core::{SelfLearningMemory, TaskContext, TaskType};
///
/// # async fn example() -> anyhow::Result<()> {
/// let memory = SelfLearningMemory::new();
///
/// // Create an episode
/// let episode_id = memory.start_episode(
/// "Test task".to_string(),
/// TaskContext::default(),
/// TaskType::Testing,
/// ).await;
///
/// // Delete it
/// memory.delete_episode(episode_id).await?;
///
/// // Verify it's gone
/// assert!(memory.get_episode(episode_id).await.is_err());
/// # Ok(())
/// # }
/// ```
pub async fn delete_episode(&self, episode_id: Uuid) -> Result<()> {
debug!("Deleting episode: {}", episode_id);
// Check if episode exists first
let exists = {
let episodes = self.episodes_fallback.read().await;
episodes.contains_key(&episode_id)
};
if !exists {
// Try to load from storage to verify it doesn't exist
if self.get_episode(episode_id).await.is_err() {
return Err(Error::NotFound(episode_id));
}
}
// Delete from in-memory cache first
{
let mut episodes = self.episodes_fallback.write().await;
episodes.remove(&episode_id);
}
// Delete from step buffers if present
{
let mut buffers = self.step_buffers.write().await;
buffers.remove(&episode_id);
}
// Delete from cache storage (redb)
if let Some(cache) = &self.cache_storage {
if let Err(e) = cache.delete_episode(episode_id).await {
warn!("Failed to delete episode from cache storage: {}", e);
// Continue with deletion from other backends
}
}
// Delete from durable storage (Turso)
if let Some(turso) = &self.turso_storage {
if let Err(e) = turso.delete_episode(episode_id).await {
warn!("Failed to delete episode from durable storage: {}", e);
return Err(e);
}
}
info!("Successfully deleted episode: {}", episode_id);
Ok(())
}
/// Archive an episode by marking it as archived.
///
/// This sets the `archived_at` timestamp in both the episode's metadata
/// and the database, allowing episodes to be preserved rather than deleted.
///
/// # Arguments
///
/// * `episode_id` - UUID of the episode to archive
///
/// # Returns
///
/// `Ok(())` if archival succeeds, or an error if the operation fails.
///
/// # Errors
///
/// Returns `Error::NotFound` if the episode doesn't exist.
/// Returns `Error::Storage` if the update fails in any storage backend.
pub async fn archive_episode(&self, episode_id: Uuid) -> Result<()> {
debug!("Archiving episode: {}", episode_id);
// Get the episode first
let episode = self.get_episode(episode_id).await?;
// Set archived_at timestamp in metadata
let archived_timestamp = Utc::now().timestamp();
let mut episode = episode;
episode
.metadata
.insert("archived_at".to_string(), archived_timestamp.to_string());
// Update in all storage backends
// In-memory cache - re-insert with updated episode (can't mutate through Arc)
{
let mut episodes = self.episodes_fallback.write().await;
episodes.insert(episode_id, Arc::new(episode.clone()));
}
// Cache storage (redb)
if let Some(cache) = &self.cache_storage {
if let Err(e) = cache.store_episode(&episode).await {
warn!("Failed to update episode in cache storage: {}", e);
}
}
// Durable storage (Turso)
if let Some(turso) = &self.turso_storage {
if let Err(e) = turso.store_episode(&episode).await {
warn!("Failed to update episode in durable storage: {}", e);
return Err(e);
}
}
info!("Successfully archived episode: {}", episode_id);
Ok(())
}
/// Restore an archived episode by clearing the archived status.
///
/// This removes the `archived_at` timestamp, making the episode
/// active again for retrieval and analysis.
///
/// # Arguments
///
/// * `episode_id` - UUID of the episode to restore
///
/// # Returns
///
/// `Ok(())` if restoration succeeds, or an error if the operation fails.
///
/// # Errors
///
/// Returns `Error::NotFound` if the episode doesn't exist.
/// Returns `Error::Storage` if the episode is not archived or update fails.
pub async fn restore_episode(&self, episode_id: Uuid) -> Result<()> {
debug!("Restoring episode: {}", episode_id);
// Get the episode first
let episode = self.get_episode(episode_id).await?;
// Verify it's archived
if !episode.metadata.contains_key("archived_at") {
return Err(Error::Storage("Episode is not archived".to_string()));
}
// Remove archived_at from metadata
let mut episode = episode;
episode.metadata.remove("archived_at");
// Update in all storage backends
// In-memory cache - re-insert with updated episode (can't mutate through Arc)
{
let mut episodes = self.episodes_fallback.write().await;
episodes.insert(episode_id, Arc::new(episode.clone()));
}
if let Some(cache) = &self.cache_storage {
if let Err(e) = cache.store_episode(&episode).await {
warn!("Failed to update episode in cache storage: {}", e);
}
}
if let Some(turso) = &self.turso_storage {
if let Err(e) = turso.store_episode(&episode).await {
warn!("Failed to update episode in durable storage: {}", e);
return Err(e);
}
}
info!("Successfully restored episode: {}", episode_id);
Ok(())
}
/// Update episode fields
///
/// Updates specified fields of an episode. Only provided fields are updated.
///
/// # Arguments
///
/// * `episode_id` - Episode to update
/// * `description` - Optional new task description
/// * `metadata` - Optional metadata to merge with existing metadata
///
/// # Returns
///
/// `Ok(())` if update succeeds
///
/// # Errors
///
/// Returns `Error::NotFound` if episode doesn't exist
/// Returns `Error::Storage` if update fails
pub async fn update_episode(
&self,
episode_id: Uuid,
description: Option<String>,
metadata: Option<std::collections::HashMap<String, String>>,
) -> Result<()> {
debug!("Updating episode: {}", episode_id);
// Get the episode
let mut episode = self.get_episode(episode_id).await?;
// Track if any changes were made
let mut changes_made = false;
// Update description if provided
if let Some(new_description) = description {
if episode.task_description != new_description {
episode.task_description = new_description;
changes_made = true;
debug!("Updated task description for episode: {}", episode_id);
}
}
// Merge metadata if provided
if let Some(new_metadata) = metadata {
if !new_metadata.is_empty() {
episode.metadata.extend(new_metadata);
changes_made = true;
debug!("Updated metadata for episode: {}", episode_id);
}
}
if !changes_made {
debug!("No changes to apply for episode: {}", episode_id);
return Ok(());
}
// Update in all storage backends
self.update_episode_in_storage(&episode).await?;
info!("Successfully updated episode: {}", episode_id);
Ok(())
}
/// Helper to update episode in all storage backends
pub(super) async fn update_episode_in_storage(&self, episode: &crate::Episode) -> Result<()> {
let episode_id = episode.episode_id;
// Update in-memory cache
{
let mut episodes = self.episodes_fallback.write().await;
episodes.insert(episode_id, Arc::new(episode.clone()));
}
// Update cache storage
if let Some(cache) = &self.cache_storage {
if let Err(e) = cache.store_episode(episode).await {
warn!("Failed to update episode in cache storage: {}", e);
}
}
// Update durable storage
if let Some(turso) = &self.turso_storage {
turso.store_episode(episode).await?;
}
Ok(())
}
/// Update an episode directly with the episode struct.
///
/// This is used internally for operations like adding checkpoints.
/// Updates all storage backends.
///
/// # Arguments
///
/// * `episode` - The episode to update (will be stored with its episode_id)
///
/// # Returns
///
/// `Ok(())` if update succeeds
///
/// # Errors
///
/// Returns `Error::Storage` if update fails
pub async fn update_episode_full(&self, episode: &crate::Episode) -> Result<()> {
self.update_episode_in_storage(episode).await
}
/// Get all heuristics from memory.
///
/// Returns all stored heuristics for use in handoff packs and recommendations.
///
/// # Returns
///
/// Vector of all heuristics
pub async fn get_all_heuristics(&self) -> Result<Vec<crate::pattern::Heuristic>> {
let heuristics = self.heuristics_fallback.read().await;
Ok(heuristics.values().cloned().collect())
}
/// Search for patterns using multi-signal ranking.
///
/// This is a simplified pattern search interface for checkpoint operations.
///
/// # Arguments
///
/// * `query` - Natural language query
/// * `context` - Task context for filtering
/// * `config` - Search configuration
///
/// # Returns
///
/// Vector of pattern search results ranked by relevance
pub async fn search_patterns(
&self,
query: &str,
context: &crate::types::TaskContext,
config: super::pattern_search::SearchConfig,
) -> Result<Vec<super::pattern_search::PatternSearchResult>> {
let patterns = self.get_all_patterns().await?;
super::pattern_search::search_patterns_semantic(
query,
patterns,
context,
self.semantic_service.as_ref(),
config,
10, // default limit
)
.await
}
}