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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
//! Execution-signature retrieval (WG-121).
//!
//! Inspired by APEX-EM (arXiv:2603.29093): rank episodes by execution
//! metadata (tools, errors, step structure) alongside embeddings.
//!
//! Matches query signatures against episode signatures for retrieval
//! that considers execution patterns, not just semantic similarity.

mod matcher;

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

pub use matcher::SignatureMatcher;

/// Configuration for execution-signature matching.
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct SignatureConfig {
    /// Weight for tool overlap in signature matching.
    pub tool_weight: f32,
    /// Weight for error pattern overlap.
    pub error_weight: f32,
    /// Weight for step structure similarity.
    pub structure_weight: f32,
    /// Minimum overlap threshold for matching.
    pub min_overlap_threshold: f32,
    /// Whether to normalize scores.
    pub normalize_scores: bool,
}

impl Default for SignatureConfig {
    fn default() -> Self {
        Self {
            tool_weight: 0.4,
            error_weight: 0.3,
            structure_weight: 0.3,
            min_overlap_threshold: 0.2,
            normalize_scores: true,
        }
    }
}

impl SignatureConfig {
    /// Create a config favoring tool matching.
    #[must_use]
    pub fn tool_focused() -> Self {
        Self {
            tool_weight: 0.6,
            error_weight: 0.2,
            structure_weight: 0.2,
            min_overlap_threshold: 0.3,
            normalize_scores: true,
        }
    }

    /// Create a config favoring error pattern matching.
    #[must_use]
    pub fn error_focused() -> Self {
        Self {
            tool_weight: 0.2,
            error_weight: 0.6,
            structure_weight: 0.2,
            min_overlap_threshold: 0.2,
            normalize_scores: true,
        }
    }

    /// Create a balanced config.
    #[must_use]
    pub fn balanced() -> Self {
        Self {
            tool_weight: 0.33,
            error_weight: 0.33,
            structure_weight: 0.34,
            min_overlap_threshold: 0.25,
            normalize_scores: true,
        }
    }
}

/// Execution signature capturing tool, error, and structure patterns.
///
/// Represents the execution characteristics of an episode that can
/// be matched against query signatures for retrieval.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExecutionSignature {
    /// Episode ID this signature belongs to.
    pub episode_id: Uuid,
    /// Set of tools used in execution.
    pub tools: HashSet<String>,
    /// Error types encountered (normalized).
    pub error_types: HashSet<String>,
    /// Step success pattern (sequence of success/failure).
    pub step_pattern: StepPattern,
    /// Total number of steps.
    pub total_steps: usize,
    /// Number of successful steps.
    pub successful_steps: usize,
    /// Number of failed steps.
    pub failed_steps: usize,
    /// Average latency per step (ms).
    pub avg_latency_ms: u64,
}

impl ExecutionSignature {
    /// Create a new execution signature.
    #[must_use]
    pub fn new(episode_id: Uuid) -> Self {
        Self {
            episode_id,
            tools: HashSet::new(),
            error_types: HashSet::new(),
            step_pattern: StepPattern::default(),
            total_steps: 0,
            successful_steps: 0,
            failed_steps: 0,
            avg_latency_ms: 0,
        }
    }

    /// Add a tool to the signature.
    pub fn add_tool(&mut self, tool: &str) {
        self.tools.insert(tool.to_lowercase());
    }

    /// Add an error type to the signature.
    pub fn add_error(&mut self, error_type: &str) {
        self.error_types.insert(normalize_error_type(error_type));
    }

    /// Record a step outcome (success/failure).
    pub fn record_step(&mut self, success: bool) {
        self.total_steps += 1;
        if success {
            self.successful_steps += 1;
            self.step_pattern.success_count += 1;
        } else {
            self.failed_steps += 1;
            self.step_pattern.failure_count += 1;
        }
    }

    /// Set the average latency.
    pub fn set_avg_latency(&mut self, latency_ms: u64) {
        self.avg_latency_ms = latency_ms;
    }

    /// Get success rate (0.0 to 1.0).
    #[must_use]
    pub fn success_rate(&self) -> f32 {
        if self.total_steps == 0 {
            return 0.0;
        }
        self.successful_steps as f32 / self.total_steps as f32
    }

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

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

    /// Check if signature has any tools.
    #[must_use]
    pub fn has_tools(&self) -> bool {
        !self.tools.is_empty()
    }

    /// Check if signature has any errors.
    #[must_use]
    pub fn has_errors(&self) -> bool {
        !self.error_types.is_empty()
    }
}

/// Step pattern capturing the execution structure.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct StepPattern {
    /// Number of successful steps.
    pub success_count: usize,
    /// Number of failed steps.
    pub failure_count: usize,
    /// Whether execution ended in success.
    pub final_success: bool,
    /// Pattern code: compact representation of success/failure sequence.
    pub pattern_code: String,
}

impl StepPattern {
    /// Create a step pattern from a sequence of outcomes.
    #[must_use]
    pub fn from_outcomes(outcomes: &[bool]) -> Self {
        let success_count = outcomes.iter().filter(|&s| *s).count();
        let failure_count = outcomes.len() - success_count;
        let final_success = outcomes.last().copied().unwrap_or(false);

        // Create compact pattern code: S for success, F for failure
        let pattern_code = outcomes
            .iter()
            .map(|s| if *s { 'S' } else { 'F' })
            .collect::<String>();

        Self {
            success_count,
            failure_count,
            final_success,
            pattern_code,
        }
    }

    /// Get pattern similarity with another pattern.
    ///
    /// Compares the ratio of matching positions.
    #[must_use]
    pub fn similarity(&self, other: &StepPattern) -> f32 {
        if self.pattern_code.is_empty() || other.pattern_code.is_empty() {
            return 0.0;
        }

        // Compare lengths first - similar lengths score higher
        let len_ratio = if self.pattern_code.len() > other.pattern_code.len() {
            other.pattern_code.len() as f32 / self.pattern_code.len() as f32
        } else {
            self.pattern_code.len() as f32 / other.pattern_code.len() as f32
        };

        // Compare success/failure ratios
        let self_success_ratio =
            self.success_count as f32 / (self.success_count + self.failure_count).max(1) as f32;
        let other_success_ratio =
            other.success_count as f32 / (other.success_count + other.failure_count).max(1) as f32;
        let ratio_similarity = 1.0 - (self_success_ratio - other_success_ratio).abs();

        // Compare final outcome
        let final_match = if self.final_success == other.final_success {
            1.0
        } else {
            0.0
        };

        // Combine factors
        len_ratio * 0.3 + ratio_similarity * 0.5 + final_match * 0.2
    }
}

/// Query signature for matching against episode signatures.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QuerySignature {
    /// Tools expected/needed in the query.
    pub expected_tools: HashSet<String>,
    /// Error types to avoid or handle.
    pub relevant_errors: HashSet<String>,
    /// Expected step pattern (if known).
    pub expected_pattern: Option<StepPattern>,
}

impl QuerySignature {
    /// Create a new query signature.
    #[must_use]
    pub fn new() -> Self {
        Self {
            expected_tools: HashSet::new(),
            relevant_errors: HashSet::new(),
            expected_pattern: None,
        }
    }

    /// Create a query signature from a query string.
    ///
    /// Extracts tool names and error patterns from text.
    #[must_use]
    pub fn from_query_text(query: &str) -> Self {
        let mut sig = Self::new();

        // Extract common tool names from query
        let common_tools = [
            "read",
            "write",
            "bash",
            "grep",
            "edit",
            "glob",
            "lsp",
            "webfetch",
            "websearch",
            "skill",
            "agent",
            "cargo",
            "rustc",
            "git",
            "npm",
            "pytest",
        ];

        for tool in common_tools {
            if query.to_lowercase().contains(tool) {
                sig.expected_tools.insert(tool.to_string());
            }
        }

        // Extract error patterns
        let error_patterns = [
            "timeout",
            "error",
            "failure",
            "panic",
            "exception",
            " deadlock",
            "race",
            "null",
            "missing",
            "invalid",
        ];

        for pattern in error_patterns {
            if query.to_lowercase().contains(pattern) {
                sig.relevant_errors.insert(normalize_error_type(pattern));
            }
        }

        sig
    }

    /// Add an expected tool.
    pub fn add_tool(&mut self, tool: &str) {
        self.expected_tools.insert(tool.to_lowercase());
    }

    /// Add a relevant error.
    pub fn add_error(&mut self, error: &str) {
        self.relevant_errors.insert(normalize_error_type(error));
    }

    /// Set expected pattern.
    pub fn set_pattern(&mut self, pattern: StepPattern) {
        self.expected_pattern = Some(pattern);
    }

    /// Check if query has any tool expectations.
    #[must_use]
    pub fn has_tool_expectations(&self) -> bool {
        !self.expected_tools.is_empty()
    }

    /// Check if query has any error expectations.
    #[must_use]
    pub fn has_error_expectations(&self) -> bool {
        !self.relevant_errors.is_empty()
    }
}

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

/// Result of signature matching.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SignatureMatch {
    /// Episode ID that matched.
    pub episode_id: Uuid,
    /// Overall match score (0.0 to 1.0).
    pub score: f32,
    /// Tool overlap score component.
    pub tool_score: f32,
    /// Error overlap score component.
    pub error_score: f32,
    /// Structure similarity score component.
    pub structure_score: f32,
    /// Which components contributed to the match.
    pub contributing_components: Vec<String>,
}

impl SignatureMatch {
    /// Check if this is a strong match (score >= 0.5).
    #[must_use]
    pub fn is_strong_match(&self) -> bool {
        self.score >= 0.5
    }

    /// Check if this is a weak match (score >= 0.2).
    #[must_use]
    pub fn is_weak_match(&self) -> bool {
        self.score >= 0.2
    }
}

/// Normalize an error type for matching.
///
/// Maps similar error descriptions to canonical forms.
#[cfg(test)]
pub(crate) fn normalize_error_type(error: &str) -> String {
    let lower = error.to_lowercase();

    // Map common patterns to canonical forms
    let canonical = match lower.as_str() {
        "timeout" | "timed out" | "time out" => "timeout",
        "panic" | "panicked" => "panic",
        "deadlock" | "dead lock" => "deadlock",
        "race" | "race condition" => "race_condition",
        "null" | "null pointer" | "nullptr" => "null_pointer",
        "missing" | "not found" | "does not exist" => "missing",
        "invalid" | "invalid input" | "invalid argument" => "invalid",
        e => e.trim(),
    };

    canonical.to_string()
}

#[cfg(not(test))]
fn normalize_error_type(error: &str) -> String {
    let lower = error.to_lowercase();

    let canonical = match lower.as_str() {
        "timeout" | "timed out" | "time out" => "timeout",
        "panic" | "panicked" => "panic",
        "deadlock" | "dead lock" => "deadlock",
        "race" | "race condition" => "race_condition",
        "null" | "null pointer" | "nullptr" => "null_pointer",
        "missing" | "not found" | "does not exist" => "missing",
        "invalid" | "invalid input" | "invalid argument" => "invalid",
        e => e.trim(),
    };

    canonical.to_string()
}

#[cfg(test)]
mod tests;