do-memory-core 0.1.29

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
# Regex Pattern Search Guide

**Status**: ✅ Implemented (Phase 3)  
**Date**: 2026-01-20  
**Feature**: Advanced pattern matching with regex support

## Overview

Regex search enables powerful pattern matching for complex queries. This feature includes built-in ReDoS (Regular Expression Denial of Service) protection to ensure safe operation.

## Key Features

- **Pattern Matching**: Find episodes using regular expressions
- **ReDoS Protection**: Automatic validation prevents dangerous patterns
- **Case Sensitivity**: Control with `(?i)` flag
- **Multi-Field Support**: Search across all episode fields
- **Filter Integration**: Combine regex with existing filters

## Basic Usage

### Enable Regex Search

```rust
use memory_core::{EpisodeFilter, SelfLearningMemory};
use memory_core::search::SearchMode;

let memory = SelfLearningMemory::new();

// Simple regex pattern
let filter = EpisodeFilter::builder()
    .search_text(r"error.*timeout".to_string())
    .search_mode(SearchMode::Regex)
    .build();

let results = memory.list_episodes_filtered(filter, None, None).await?;
```

## Common Patterns

### 1. Find Version Numbers

```rust
// Match semantic versions: 1.2.3, v2.0.1
let filter = EpisodeFilter::builder()
    .search_text(r"v?\d+\.\d+\.\d+".to_string())
    .search_mode(SearchMode::Regex)
    .build();
```

### 2. Find API Endpoints

```rust
// Match /users/123, /posts/456, etc.
let filter = EpisodeFilter::builder()
    .search_text(r"/\w+/\d+".to_string())
    .search_mode(SearchMode::Regex)
    .build();
```

### 3. Find File Paths

```rust
// Match src/file.rs, lib/module.ts, etc.
let filter = EpisodeFilter::builder()
    .search_text(r"src/\w+\.\w+".to_string())
    .search_mode(SearchMode::Regex)
    .build();
```

### 4. Find Error Messages with Timestamps

```rust
// Match "error" followed by time: error at 10:30:45
let filter = EpisodeFilter::builder()
    .search_text(r"error.*\d{2}:\d{2}:\d{2}".to_string())
    .search_mode(SearchMode::Regex)
    .build();
```

### 5. Case-Insensitive Search

```rust
// Match bug, Bug, BUG, error, Error, etc.
let filter = EpisodeFilter::builder()
    .search_text(r"(?i)(bug|error|fix)".to_string())
    .search_mode(SearchMode::Regex)
    .build();
```

### 6. Email Addresses

```rust
// Match email addresses
let filter = EpisodeFilter::builder()
    .search_text(r"\b[\w.-]+@[\w.-]+\.\w+\b".to_string())
    .search_mode(SearchMode::Regex)
    .build();
```

### 7. IP Addresses

```rust
// Match IPv4 addresses
let filter = EpisodeFilter::builder()
    .search_text(r"\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b".to_string())
    .search_mode(SearchMode::Regex)
    .build();
```

### 8. HTTP Status Codes

```rust
// Match 4xx and 5xx errors
let filter = EpisodeFilter::builder()
    .search_text(r"\b[45]\d{2}\b".to_string())
    .search_mode(SearchMode::Regex)
    .build();
```

## Combining with Filters

Regex search works with all existing filters:

```rust
// Regex + domain filter
let filter = EpisodeFilter::builder()
    .search_text(r"API|api".to_string())
    .search_mode(SearchMode::Regex)
    .domains(vec!["backend".to_string()])
    .build();

// Regex + date range + success filter
let filter = EpisodeFilter::builder()
    .search_text(r"deploy.*production".to_string())
    .search_mode(SearchMode::Regex)
    .date_from(Utc::now() - Duration::days(7))
    .success_only(true)
    .build();
```

## ReDoS Protection

The system automatically validates regex patterns to prevent ReDoS attacks:

### Blocked Patterns

```rust
use memory_core::search::validate_regex_pattern;

// Nested quantifiers (catastrophic backtracking)
assert!(validate_regex_pattern("(a+)+").is_err());
assert!(validate_regex_pattern("(a*)*").is_err());
assert!(validate_regex_pattern("(a+)*").is_err());

// Excessive repetitions
assert!(validate_regex_pattern("a{1000}").is_err());

// Too long patterns (>1000 chars)
assert!(validate_regex_pattern(&"a".repeat(1001)).is_err());
```

### Safe Patterns

```rust
// These patterns are safe and allowed
assert!(validate_regex_pattern("error.*timeout").is_ok());
assert!(validate_regex_pattern(r"\d+\.\d+\.\d+").is_ok());
assert!(validate_regex_pattern("(?i)bug|fix").is_ok());
```

## CLI Usage

### Basic Regex Search

```bash
# Find API endpoints
do-memory-cli episode search "/users/\d+" --regex

# Find version numbers
do-memory-cli episode search "v?\d+\.\d+\.\d+" --regex

# Case-insensitive OR
do-memory-cli episode search "(?i)(bug|error|fix)" --regex
```

### Combine with Other Options

```bash
# Regex + limit
do-memory-cli episode search "error.*timeout" --regex --limit 20

# Regex + domain filter
do-memory-cli episode search "API|api" --regex --search-fields description,outcome

# Regex with field selection
do-memory-cli episode search "database.*connection" --regex --search-fields steps,outcome
```

## Performance Considerations

### Pattern Complexity

| Pattern Type | Performance | Recommendation |
|--------------|-------------|----------------|
| Simple string | Fast | Use when possible |
| Anchored (^$) | Fast | Preferred for exact matches |
| Character classes | Fast | Good for validation |
| Alternation (a\|b) | Moderate | Limit alternatives |
| Wildcards (.*) | Moderate | Avoid at start |
| Lookahead/behind | Slow | Use sparingly |

### Optimization Tips

1. **Anchor patterns** when possible: `^error` vs `error`
2. **Avoid leading wildcards**: `error.*` vs `.*error`
3. **Use specific patterns**: `\d{3}` vs `\d+`
4. **Limit alternations**: `bug|fix` vs `(bug|fix|error|warning|...)`

## Regex Syntax Reference

### Basic Patterns

| Pattern | Description | Example |
|---------|-------------|---------|
| `.` | Any character | `a.b` matches "aab", "acb" |
| `\d` | Digit (0-9) | `\d+` matches "123" |
| `\w` | Word char (a-z, A-Z, 0-9, _) | `\w+` matches "test_123" |
| `\s` | Whitespace | `\s+` matches "   " |
| `^` | Start of line | `^error` matches "error: ..." |
| `$` | End of line | `end$` matches "... end" |

### Quantifiers

| Pattern | Description | Example |
|---------|-------------|---------|
| `*` | 0 or more | `a*` matches "", "a", "aa" |
| `+` | 1 or more | `a+` matches "a", "aa" |
| `?` | 0 or 1 | `a?` matches "", "a" |
| `{n}` | Exactly n | `\d{3}` matches "123" |
| `{n,}` | n or more | `\d{3,}` matches "123", "1234" |
| `{n,m}` | Between n and m | `\d{3,5}` matches "123" to "12345" |

### Character Classes

| Pattern | Description | Example |
|---------|-------------|---------|
| `[abc]` | Any of a, b, c | `[aeiou]` matches vowels |
| `[^abc]` | Not a, b, c | `[^0-9]` matches non-digits |
| `[a-z]` | Range a to z | `[A-Z]` matches uppercase |
| `\|` | Alternation (OR) | `cat\|dog` matches "cat" or "dog" |

### Groups

| Pattern | Description | Example |
|---------|-------------|---------|
| `(...)` | Capture group | `(\d+)-(\d+)` captures "123-456" |
| `(?:...)` | Non-capture group | `(?:http\|https)://` |
| `(?i)` | Case-insensitive | `(?i)error` matches "ERROR" |

## Common Use Cases

### Software Development

```rust
// Find commits
search_text: r"commit\s+[a-f0-9]{7,40}"

// Find function calls
search_text: r"\w+\([^)]*\)"

// Find TODO comments
search_text: r"(?i)TODO:.*"

// Find import statements
search_text: r"(?:import|from|require)\s+[\w.]+"
```

### DevOps & Infrastructure

```rust
// Find deployment logs
search_text: r"deploy.*(?:staging|production)"

// Find error codes
search_text: r"\b[45]\d{2}\b"

// Find resource names
search_text: r"(?i)(cpu|memory|disk).*\d+%"

// Find service names
search_text: r"service-\w+-\d+"
```

### Data Analysis

```rust
// Find JSON keys
search_text: r#""[^"]+"\s*:"#

// Find URLs
search_text: r"https?://[\w.-]+/[\w/.-]*"

// Find timestamps
search_text: r"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}"

// Find durations
search_text: r"\d+(?:ms|s|m|h)"
```

## API Reference

### Pattern Validation

```rust
pub fn validate_regex_pattern(pattern: &str) -> Result<(), String>
```

Validates a regex pattern before use. Returns `Ok(())` if safe, `Err` with description if not.

### Search Functions

```rust
pub fn regex_search(text: &str, pattern: &str) -> Result<Vec<(usize, String)>, String>
```

Search for regex pattern in text. Returns vector of `(position, matched_text)`.

```rust
pub fn regex_search_case_insensitive(text: &str, pattern: &str) -> Result<Vec<(usize, String)>, String>
```

Case-insensitive regex search.

```rust
pub fn regex_matches(text: &str, pattern: &str) -> Result<bool, String>
```

Check if text matches pattern (returns boolean).

## Examples

See the complete working example:

```bash
cargo run --example regex_search_demo
```

## Testing

Run regex search tests:

```bash
# Unit tests
cargo test search::regex

# Integration tests
cargo test regex_search
```

## Migration from Other Search Modes

### From Exact Search

```rust
// Before (exact)
.search_text("error timeout")
.search_mode(SearchMode::Exact)

// After (regex for flexibility)
.search_text(r"error.*timeout")
.search_mode(SearchMode::Regex)
```

### From Fuzzy Search

```rust
// Before (fuzzy for typos)
.search_text("databse")
.search_mode(SearchMode::Fuzzy { threshold: 0.8 })

// After (regex for patterns)
.search_text(r"data.{0,2}base")  // Match database, databse, databae
.search_mode(SearchMode::Regex)
```

## Troubleshooting

### Pattern Not Matching

1. **Test pattern separately**: Use `validate_regex_pattern()` first
2. **Check case sensitivity**: Add `(?i)` flag if needed
3. **Escape special chars**: Use `\` before `.`, `*`, `+`, etc.
4. **Check field selection**: Ensure searching correct fields

### Performance Issues

1. **Avoid leading wildcards**: `.*error` is slow
2. **Use anchors**: `^error` is faster than `error`
3. **Limit alternations**: Too many `|` operators slow down
4. **Check pattern complexity**: Simplify if possible

### ReDoS Protection Triggered

If your pattern is blocked:
1. **Simplify nested groups**: Avoid `(a+)+` patterns
2. **Reduce repetitions**: Keep `{n}` values reasonable
3. **Use character classes**: `[abc]` instead of `(a|b|c)`

## Security Best Practices

1. **Always validate** user-provided patterns
2. **Set timeouts** for regex execution in production
3. **Log blocked patterns** for security monitoring
4. **Educate users** about safe pattern construction
5. **Review patterns** before deploying to production

## See Also

- [Fuzzy Search Guide]FUZZY_SEARCH_GUIDE.md - Typo-tolerant search
- [Episode Filtering Guide]EPISODE_FILTERING.md - Filter API reference
- [Pattern Search Feature]PATTERN_SEARCH_FEATURE.md - Pattern extraction