do-memory-core 0.1.34

Core episodic learning system for AI agents with pattern extraction, reward scoring, and dual storage backend
Documentation
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
//! Scope-before-search shard routing (WG-122).
//!
//! Inspired by ShardMemo (arXiv:2601.21545): route queries through
//! cheap scope filters before expensive vector search.
//!
//! Pre-filters candidates by metadata (tags, task type, timeframe)
//! to reduce the search space for embedding-based retrieval.

mod router;

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashSet;
use uuid::Uuid;

pub use router::ShardRouter;

/// Configuration for shard routing.
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct ShardConfig {
    /// Maximum candidates to pass to vector search.
    pub max_candidates: usize,
    /// Minimum candidates required before search.
    pub min_candidates: usize,
    /// Weight for tag matching in routing.
    pub tag_weight: f32,
    /// Weight for task type matching.
    pub task_type_weight: f32,
    /// Weight for timeframe matching.
    pub timeframe_weight: f32,
    /// Whether to use temporal decay for recent episodes.
    pub use_temporal_decay: bool,
    /// Days before an episode is considered stale.
    pub stale_days: u32,
}

impl Default for ShardConfig {
    fn default() -> Self {
        Self {
            max_candidates: 100,
            min_candidates: 10,
            tag_weight: 0.4,
            task_type_weight: 0.3,
            timeframe_weight: 0.3,
            use_temporal_decay: true,
            stale_days: 30,
        }
    }
}

impl ShardConfig {
    /// Create a config for large datasets.
    #[must_use]
    pub fn large_dataset() -> Self {
        Self {
            max_candidates: 500,
            min_candidates: 50,
            tag_weight: 0.5,
            task_type_weight: 0.3,
            timeframe_weight: 0.2,
            use_temporal_decay: true,
            stale_days: 60,
        }
    }

    /// Create a config for small datasets.
    #[must_use]
    pub fn small_dataset() -> Self {
        Self {
            max_candidates: 50,
            min_candidates: 5,
            tag_weight: 0.3,
            task_type_weight: 0.3,
            timeframe_weight: 0.4,
            use_temporal_decay: true,
            stale_days: 14,
        }
    }

    /// Create a config emphasizing recent episodes.
    #[must_use]
    pub fn recent_focused() -> Self {
        Self {
            max_candidates: 100,
            min_candidates: 10,
            tag_weight: 0.2,
            task_type_weight: 0.2,
            timeframe_weight: 0.6,
            use_temporal_decay: true,
            stale_days: 7,
        }
    }
}

/// Scope filter for routing queries.
///
/// Defines the metadata constraints for pre-filtering candidates.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ScopeFilter {
    /// Required tags (at least one must match).
    pub required_tags: HashSet<String>,
    /// Excluded tags (none should match).
    pub excluded_tags: HashSet<String>,
    /// Required task types.
    pub required_task_types: HashSet<String>,
    /// Time range for episodes.
    pub time_range: Option<TimeRange>,
    /// Minimum success rate threshold.
    pub min_success_rate: Option<f32>,
}

impl ScopeFilter {
    /// Create a new empty scope filter.
    #[must_use]
    pub fn new() -> Self {
        Self {
            required_tags: HashSet::new(),
            excluded_tags: HashSet::new(),
            required_task_types: HashSet::new(),
            time_range: None,
            min_success_rate: None,
        }
    }

    /// Create a scope filter from query text.
    ///
    /// Extracts tags, task types, and time hints from text.
    #[must_use]
    pub fn from_query_text(query: &str) -> Self {
        let mut filter = Self::new();

        // Extract common tags from query
        let common_tags = [
            "bug",
            "fix",
            "feature",
            "refactor",
            "test",
            "security",
            "performance",
            "documentation",
            "api",
            "cli",
            "mcp",
            "storage",
            "core",
        ];

        for tag in common_tags {
            if query.to_lowercase().contains(tag) {
                filter.required_tags.insert(tag.to_string());
            }
        }

        // Extract task types
        let task_types = ["implementation", "debugging", "testing", "review"];
        for tt in task_types {
            if query.to_lowercase().contains(tt) {
                filter.required_task_types.insert(tt.to_string());
            }
        }

        // Extract time hints
        let lower = query.to_lowercase();
        if lower.contains("recent") || lower.contains("latest") {
            filter.time_range = Some(TimeRange::recent_days(7));
        } else if lower.contains("today") {
            filter.time_range = Some(TimeRange::today());
        } else if lower.contains("this week") {
            filter.time_range = Some(TimeRange::recent_days(7));
        } else if lower.contains("this month") {
            filter.time_range = Some(TimeRange::recent_days(30));
        }

        filter
    }

    /// Add a required tag.
    pub fn require_tag(&mut self, tag: &str) {
        self.required_tags.insert(tag.to_lowercase());
    }

    /// Add an excluded tag.
    pub fn exclude_tag(&mut self, tag: &str) {
        self.excluded_tags.insert(tag.to_lowercase());
    }

    /// Add a required task type.
    pub fn require_task_type(&mut self, task_type: &str) {
        self.required_task_types.insert(task_type.to_lowercase());
    }

    /// Set time range.
    pub fn set_time_range(&mut self, range: TimeRange) {
        self.time_range = Some(range);
    }

    /// Set minimum success rate.
    pub fn set_min_success_rate(&mut self, rate: f32) {
        self.min_success_rate = Some(rate);
    }

    /// Check if filter has any constraints.
    #[must_use]
    pub fn has_constraints(&self) -> bool {
        !self.required_tags.is_empty()
            || !self.excluded_tags.is_empty()
            || !self.required_task_types.is_empty()
            || self.time_range.is_some()
            || self.min_success_rate.is_some()
    }

    /// Get constraint count.
    #[must_use]
    pub fn constraint_count(&self) -> usize {
        self.required_tags.len()
            + self.excluded_tags.len()
            + self.required_task_types.len()
            + self.time_range.map_or(0, |_| 1)
            + self.min_success_rate.map_or(0, |_| 1)
    }
}

impl Default for ScopeFilter {
    fn default() -> Self {
        Self::new()
    }
}

/// Time range for filtering episodes.
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct TimeRange {
    /// Start of range.
    pub start: DateTime<Utc>,
    /// End of range.
    pub end: DateTime<Utc>,
}

impl TimeRange {
    /// Create a time range from start to end.
    #[must_use]
    pub fn new(start: DateTime<Utc>, end: DateTime<Utc>) -> Self {
        Self { start, end }
    }

    /// Create a range for recent days.
    #[must_use]
    pub fn recent_days(days: u32) -> Self {
        let end = Utc::now();
        let start = end - chrono::Duration::days(i64::from(days));
        Self { start, end }
    }

    /// Create a range for today.
    #[must_use]
    pub fn today() -> Self {
        let now = Utc::now();
        let start = now.date_naive().and_hms_opt(0, 0, 0).unwrap();
        Self {
            start: DateTime::from_naive_utc_and_offset(start, Utc),
            end: now,
        }
    }

    /// Create a range for this week.
    #[must_use]
    pub fn this_week() -> Self {
        Self::recent_days(7)
    }

    /// Check if a timestamp is within the range.
    #[must_use]
    pub fn contains(&self, timestamp: DateTime<Utc>) -> bool {
        timestamp >= self.start && timestamp <= self.end
    }

    /// Get duration in days.
    #[must_use]
    pub fn duration_days(&self) -> u32 {
        ((self.end - self.start).num_days()).max(0) as u32
    }
}

/// Episode metadata for shard routing.
///
/// Lightweight metadata extracted from episodes for efficient filtering.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EpisodeMetadata {
    /// Episode ID.
    pub episode_id: Uuid,
    /// Tags associated with episode.
    pub tags: HashSet<String>,
    /// Task type.
    pub task_type: String,
    /// Creation timestamp.
    pub created_at: DateTime<Utc>,
    /// Success rate of execution.
    pub success_rate: f32,
    /// Whether episode is complete.
    pub is_complete: bool,
}

impl EpisodeMetadata {
    /// Create new episode metadata.
    #[must_use]
    pub fn new(
        episode_id: Uuid,
        tags: HashSet<String>,
        task_type: String,
        created_at: DateTime<Utc>,
    ) -> Self {
        Self {
            episode_id,
            tags,
            task_type,
            created_at,
            success_rate: 0.0,
            is_complete: false,
        }
    }

    /// Set success rate.
    pub fn set_success_rate(&mut self, rate: f32) {
        self.success_rate = rate;
    }

    /// Mark as complete.
    pub fn mark_complete(&mut self) {
        self.is_complete = true;
    }

    /// Check if has a specific tag.
    #[must_use]
    pub fn has_tag(&self, tag: &str) -> bool {
        self.tags.contains(&tag.to_lowercase())
    }

    /// Check if matches task type.
    #[must_use]
    pub fn matches_task_type(&self, task_type: &str) -> bool {
        self.task_type.to_lowercase() == task_type.to_lowercase()
    }

    /// Get age in days.
    #[must_use]
    pub fn age_days(&self) -> u32 {
        ((Utc::now() - self.created_at).num_days()).max(0) as u32
    }
}

/// Routing result containing filtered candidates.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RoutingResult {
    /// Candidate episode IDs for vector search.
    pub candidates: Vec<Uuid>,
    /// Original candidate count before filtering.
    pub original_count: usize,
    /// Filtered candidate count.
    pub filtered_count: usize,
    /// Whether max_candidates was reached.
    pub capped: bool,
    /// Applied scope filter.
    pub filter: ScopeFilter,
    /// Routing scores for each candidate.
    pub scores: Vec<f32>,
}

impl RoutingResult {
    /// Create an empty routing result.
    #[must_use]
    pub fn empty(filter: ScopeFilter) -> Self {
        Self {
            candidates: Vec::new(),
            original_count: 0,
            filtered_count: 0,
            capped: false,
            filter,
            scores: Vec::new(),
        }
    }

    /// Get candidate count.
    #[must_use]
    pub fn len(&self) -> usize {
        self.candidates.len()
    }

    /// Check if empty.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.candidates.is_empty()
    }

    /// Get reduction ratio (filtered / original).
    #[must_use]
    pub fn reduction_ratio(&self) -> f32 {
        if self.original_count == 0 {
            return 0.0;
        }
        1.0 - (self.filtered_count as f32 / self.original_count as f32)
    }
}

#[cfg(test)]
mod tests;