do-memory-core 0.1.31

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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
use super::*;
use crate::types::ComplexityLevel;

#[test]
fn test_episode_creation() {
    let context = TaskContext {
        language: Some("rust".to_string()),
        framework: Some("tokio".to_string()),
        complexity: ComplexityLevel::Moderate,
        domain: "web-api".to_string(),
        tags: vec!["async".to_string()],
    };

    let episode = Episode::new(
        "Test task".to_string(),
        context.clone(),
        TaskType::CodeGeneration,
    );

    assert!(!episode.is_complete());
    assert_eq!(episode.task_description, "Test task");
    assert_eq!(episode.context.domain, "web-api");
    assert_eq!(episode.steps.len(), 0);
}

#[test]
fn test_episode_completion() {
    let context = TaskContext::default();
    let mut episode = Episode::new("Test task".to_string(), context, TaskType::Testing);

    assert!(!episode.is_complete());

    let outcome = TaskOutcome::Success {
        verdict: "All tests passed".to_string(),
        artifacts: vec![],
    };

    episode.complete(outcome);

    assert!(episode.is_complete());
    assert!(episode.end_time.is_some());
    assert!(episode.duration().is_some());
}

#[test]
fn test_execution_step() {
    let mut step = ExecutionStep::new(1, "read_file".to_string(), "Read source file".to_string());

    assert!(!step.is_success());

    step.result = Some(ExecutionResult::Success {
        output: "File contents".to_string(),
    });

    assert!(step.is_success());
}

#[test]
fn test_add_steps() {
    let context = TaskContext::default();
    let mut episode = Episode::new("Test task".to_string(), context, TaskType::Analysis);

    for i in 0..3 {
        let mut step = ExecutionStep::new(i + 1, format!("tool_{i}"), "Action".to_string());
        step.result = Some(ExecutionResult::Success {
            output: "OK".to_string(),
        });
        episode.add_step(step);
    }

    assert_eq!(episode.steps.len(), 3);
    assert_eq!(episode.successful_steps_count(), 3);
    assert_eq!(episode.failed_steps_count(), 0);
}

#[test]
fn test_add_tag() {
    let context = TaskContext::default();
    let mut episode = Episode::new("Test task".to_string(), context, TaskType::Analysis);

    // Add valid tag
    assert!(episode.add_tag("bug-fix".to_string()).unwrap());
    assert!(episode.has_tag("bug-fix"));
    assert_eq!(episode.tags.len(), 1);

    // Add duplicate (normalized)
    assert!(!episode.add_tag("BUG-FIX".to_string()).unwrap());
    assert_eq!(episode.tags.len(), 1);

    // Add another tag
    assert!(episode.add_tag("feature".to_string()).unwrap());
    assert_eq!(episode.tags.len(), 2);
}

#[test]
fn test_tag_normalization() {
    let context = TaskContext::default();
    let mut episode = Episode::new("Test task".to_string(), context, TaskType::Analysis);

    // Test case normalization
    episode.add_tag("Feature-123".to_string()).unwrap();
    assert!(episode.has_tag("feature-123"));
    assert!(episode.has_tag("FEATURE-123"));
    assert_eq!(episode.tags[0], "feature-123");

    // Test whitespace trimming
    episode.add_tag("  refactor  ".to_string()).unwrap();
    assert!(episode.has_tag("refactor"));
    assert_eq!(episode.tags[1], "refactor");
}

#[test]
fn test_tag_validation() {
    let context = TaskContext::default();
    let mut episode = Episode::new("Test task".to_string(), context, TaskType::Analysis);

    // Empty tag
    assert!(episode.add_tag(String::new()).is_err());
    assert!(episode.add_tag("   ".to_string()).is_err());

    // Invalid characters
    assert!(episode.add_tag("bug fix".to_string()).is_err());
    assert!(episode.add_tag("bug@fix".to_string()).is_err());
    assert!(episode.add_tag("bug/fix".to_string()).is_err());

    // Valid characters
    assert!(episode.add_tag("bug-fix".to_string()).is_ok());
    assert!(episode.add_tag("bug_fix".to_string()).is_ok());
    assert!(episode.add_tag("bugfix123".to_string()).is_ok());
    assert_eq!(episode.tags.len(), 3);

    // Too long (>100 chars)
    let long_tag = "a".repeat(101);
    assert!(episode.add_tag(long_tag).is_err());
}

#[test]
fn test_remove_tag() {
    let context = TaskContext::default();
    let mut episode = Episode::new("Test task".to_string(), context, TaskType::Analysis);

    episode.add_tag("bug-fix".to_string()).unwrap();
    episode.add_tag("feature".to_string()).unwrap();
    episode.add_tag("refactor".to_string()).unwrap();
    assert_eq!(episode.tags.len(), 3);

    // Remove existing tag
    assert!(episode.remove_tag("feature"));
    assert_eq!(episode.tags.len(), 2);
    assert!(!episode.has_tag("feature"));

    // Remove with different case
    assert!(episode.remove_tag("BUG-FIX"));
    assert_eq!(episode.tags.len(), 1);
    assert!(!episode.has_tag("bug-fix"));

    // Remove non-existent tag
    assert!(!episode.remove_tag("nonexistent"));
    assert_eq!(episode.tags.len(), 1);
}

#[test]
fn test_clear_tags() {
    let context = TaskContext::default();
    let mut episode = Episode::new("Test task".to_string(), context, TaskType::Analysis);

    episode.add_tag("bug-fix".to_string()).unwrap();
    episode.add_tag("feature".to_string()).unwrap();
    assert_eq!(episode.tags.len(), 2);

    episode.clear_tags();
    assert_eq!(episode.tags.len(), 0);
    assert!(!episode.has_tag("bug-fix"));
}

#[test]
fn test_get_tags() {
    let context = TaskContext::default();
    let mut episode = Episode::new("Test task".to_string(), context, TaskType::Analysis);

    episode.add_tag("bug-fix".to_string()).unwrap();
    episode.add_tag("critical".to_string()).unwrap();

    let tags = episode.get_tags();
    assert_eq!(tags.len(), 2);
    assert!(tags.contains(&"bug-fix".to_string()));
    assert!(tags.contains(&"critical".to_string()));
}

#[test]
fn test_tag_validation_error_messages() {
    let context = TaskContext::default();
    let mut episode = Episode::new("Test task".to_string(), context, TaskType::Analysis);

    // Empty tag error message
    let result = episode.add_tag(String::new());
    assert!(result.is_err());
    assert_eq!(result.unwrap_err(), "Tag cannot be empty");

    // Whitespace-only tag error message
    let result = episode.add_tag("   ".to_string());
    assert!(result.is_err());
    assert_eq!(result.unwrap_err(), "Tag cannot be empty");

    // Invalid characters error message
    let result = episode.add_tag("bug fix".to_string());
    assert!(result.is_err());
    let error_msg = result.unwrap_err();
    assert!(error_msg.contains("invalid characters"));
    assert!(error_msg.contains("bug fix"));

    // Too long tag error message
    let long_tag = "a".repeat(101);
    let result = episode.add_tag(long_tag.clone());
    assert!(result.is_err());
    assert_eq!(result.unwrap_err(), "Tag cannot exceed 100 characters");
}

#[test]
fn test_tag_minimum_length() {
    let context = TaskContext::default();
    let mut episode = Episode::new("Test task".to_string(), context, TaskType::Analysis);

    // Single character should be invalid
    let result = episode.add_tag("a".to_string());
    assert!(result.is_err());
    assert!(result.unwrap_err().contains("at least 2 characters"));

    // Two characters should be valid
    let result = episode.add_tag("ab".to_string());
    assert!(result.is_ok());
    assert!(episode.has_tag("ab"));
}

#[test]
fn test_tag_boundary_lengths() {
    let context = TaskContext::default();
    let mut episode = Episode::new("Test task".to_string(), context, TaskType::Analysis);

    // Exactly 2 characters
    assert!(episode.add_tag("ab".to_string()).is_ok());
    assert_eq!(episode.tags[0], "ab");

    // Exactly 100 characters
    let tag_100 = "a".repeat(100);
    assert!(episode.add_tag(tag_100.clone()).is_ok());
    assert_eq!(episode.tags[1].len(), 100);

    // 101 characters should fail
    let tag_101 = "b".repeat(101);
    assert!(episode.add_tag(tag_101).is_err());
}

#[test]
fn test_add_tag_with_combined_normalization() {
    let context = TaskContext::default();
    let mut episode = Episode::new("Test task".to_string(), context, TaskType::Analysis);

    // Test combined normalization: trim + lowercase
    let result = episode.add_tag("  FEATURE-123  ".to_string());
    assert!(result.is_ok());
    assert_eq!(episode.tags[0], "feature-123");

    // Verify case-insensitive duplicate detection
    let result = episode.add_tag("feature-123".to_string());
    assert!(result.is_ok());
    assert!(!result.unwrap(), "Should return false for duplicate");
    assert_eq!(episode.tags.len(), 1);

    let result = episode.add_tag("  FEATURE-123  ".to_string());
    assert!(result.is_ok());
    assert!(
        !result.unwrap(),
        "Should return false for duplicate with whitespace"
    );
    assert_eq!(episode.tags.len(), 1);
}

#[test]
fn test_tag_ordering() {
    let context = TaskContext::default();
    let mut episode = Episode::new("Test task".to_string(), context, TaskType::Analysis);

    // Add tags in non-alphabetical order
    episode.add_tag("zebra".to_string()).unwrap();
    episode.add_tag("alpha".to_string()).unwrap();
    episode.add_tag("beta".to_string()).unwrap();

    // Tags should maintain insertion order (not sorted)
    assert_eq!(episode.tags, vec!["zebra", "alpha", "beta"]);
}

#[test]
fn test_remove_tag_with_invalid_input() {
    let context = TaskContext::default();
    let mut episode = Episode::new("Test task".to_string(), context, TaskType::Analysis);

    episode.add_tag("bug-fix".to_string()).unwrap();
    episode.add_tag("feature".to_string()).unwrap();
    assert_eq!(episode.tags.len(), 2);

    // Try to remove invalid tag (should return false, not panic)
    assert!(!episode.remove_tag(""));
    assert!(!episode.remove_tag("   "));
    assert!(!episode.remove_tag("invalid tag"));
    assert!(!episode.remove_tag("tag@invalid"));
    assert_eq!(episode.tags.len(), 2);
}

#[test]
fn test_has_tag_with_invalid_input() {
    let context = TaskContext::default();
    let mut episode = Episode::new("Test task".to_string(), context, TaskType::Analysis);

    episode.add_tag("bug-fix".to_string()).unwrap();

    // Invalid tags should return false, not panic
    assert!(!episode.has_tag(""));
    assert!(!episode.has_tag("   "));
    assert!(!episode.has_tag("invalid tag"));
    assert!(!episode.has_tag("tag@invalid"));
    assert!(!episode.has_tag("nonexistent"));
}

#[test]
fn test_get_tags_on_empty_episode() {
    let context = TaskContext::default();
    let episode = Episode::new("Test task".to_string(), context, TaskType::Analysis);

    let tags = episode.get_tags();
    assert!(tags.is_empty());
    assert_eq!(tags.len(), 0);
}

#[test]
fn test_has_tag_on_empty_episode() {
    let context = TaskContext::default();
    let episode = Episode::new("Test task".to_string(), context, TaskType::Analysis);

    // Should return false for any tag when episode has no tags
    assert!(!episode.has_tag("bug-fix"));
    assert!(!episode.has_tag("feature"));
    assert!(!episode.has_tag("anything"));
}

#[test]
fn test_multiple_tags_with_special_characters_valid() {
    let context = TaskContext::default();
    let mut episode = Episode::new("Test task".to_string(), context, TaskType::Analysis);

    // Test all valid character combinations
    assert!(episode.add_tag("bug-fix".to_string()).is_ok());
    assert!(episode.add_tag("bug_fix".to_string()).is_ok());
    assert!(episode.add_tag("bug123".to_string()).is_ok());
    assert!(episode.add_tag("123bug".to_string()).is_ok());
    assert!(episode.add_tag("priority_high".to_string()).is_ok());
    assert!(episode.add_tag("test-123".to_string()).is_ok());
    assert!(episode.add_tag("A1B2_C3-D4".to_string()).is_ok());

    assert_eq!(episode.tags.len(), 7);
}

#[test]
fn test_tags_with_only_numbers() {
    let context = TaskContext::default();
    let mut episode = Episode::new("Test task".to_string(), context, TaskType::Analysis);

    // Tags with only numbers should be valid
    assert!(episode.add_tag("123".to_string()).is_ok());
    assert!(episode.add_tag("456".to_string()).is_ok());
    assert!(episode.has_tag("123"));
    assert!(episode.has_tag("456"));
    assert_eq!(episode.tags.len(), 2);
}

#[test]
fn test_tag_serialization() {
    let context = TaskContext::default();
    let mut episode = Episode::new("Test task".to_string(), context, TaskType::Analysis);

    episode.add_tag("bug-fix".to_string()).unwrap();
    episode.add_tag("feature".to_string()).unwrap();
    episode.add_tag("priority_high".to_string()).unwrap();

    // Serialize to JSON
    let json = serde_json::to_string(&episode).unwrap();
    assert!(json.contains("bug-fix"));
    assert!(json.contains("feature"));
    assert!(json.contains("priority_high"));

    // Deserialize from JSON
    let deserialized: Episode = serde_json::from_str(&json).unwrap();
    assert_eq!(deserialized.tags.len(), 3);
    assert!(deserialized.has_tag("bug-fix"));
    assert!(deserialized.has_tag("feature"));
    assert!(deserialized.has_tag("priority_high"));
}

#[test]
fn test_tag_operations_chain() {
    let context = TaskContext::default();
    let mut episode = Episode::new("Test task".to_string(), context, TaskType::Analysis);

    // Add multiple tags
    episode.add_tag("tag1".to_string()).unwrap();
    episode.add_tag("tag2".to_string()).unwrap();
    episode.add_tag("tag3".to_string()).unwrap();
    assert_eq!(episode.tags.len(), 3);

    // Check presence
    assert!(episode.has_tag("tag1"));
    assert!(episode.has_tag("tag2"));
    assert!(episode.has_tag("tag3"));

    // Remove middle tag
    assert!(episode.remove_tag("tag2"));
    assert_eq!(episode.tags.len(), 2);
    assert!(!episode.has_tag("tag2"));

    // Add new tag
    episode.add_tag("tag4".to_string()).unwrap();
    assert_eq!(episode.tags.len(), 3);

    // Clear all
    episode.clear_tags();
    assert_eq!(episode.tags.len(), 0);
    assert!(!episode.has_tag("tag1"));
    assert!(!episode.has_tag("tag3"));
    assert!(!episode.has_tag("tag4"));
}

#[test]
fn test_tag_persistence_after_operations() {
    let context = TaskContext::default();
    let mut episode = Episode::new("Test task".to_string(), context, TaskType::Analysis);

    // Add tags with various operations
    let mut added = episode.add_tag("persistent".to_string()).unwrap();
    assert!(added);

    added = episode.add_tag("PERSISTENT".to_string()).unwrap();
    assert!(!added, "Duplicate should return false");

    // Verify tag is still there after failed add
    assert!(episode.has_tag("persistent"));

    // Try to remove with wrong case (should still work due to normalization)
    let removed = episode.remove_tag("PERSISTENT");
    assert!(removed);
    assert!(!episode.has_tag("persistent"));
}

#[test]
fn test_tags_are_case_insensitive() {
    let context = TaskContext::default();
    let mut episode = Episode::new("Test task".to_string(), context, TaskType::Analysis);

    // Add tag in uppercase
    episode.add_tag("BUG-FIX".to_string()).unwrap();

    // Should find it with various cases
    assert!(episode.has_tag("bug-fix"));
    assert!(episode.has_tag("BUG-FIX"));
    assert!(episode.has_tag("Bug-Fix"));
    assert!(episode.has_tag("  bug-fix  "));

    // Should be able to remove with different case
    assert!(episode.remove_tag("BuG-FiX"));
    assert!(!episode.has_tag("bug-fix"));
}