engram-core 0.19.0

AI Memory Infrastructure - Persistent memory for AI agents with semantic search
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
//! Three-way merge implementation

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

/// Result of a three-way merge
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MergeResult {
    /// The merged content
    pub content: String,
    /// Whether the merge was successful (no conflicts)
    pub success: bool,
    /// Conflict markers in the output (if any)
    pub has_conflict_markers: bool,
    /// Lines that had conflicts
    pub conflict_lines: Vec<usize>,
    /// Statistics about the merge
    pub stats: MergeStats,
}

/// Statistics about a merge operation
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct MergeStats {
    /// Lines from base that were kept
    pub base_kept: usize,
    /// Lines added by local
    pub local_added: usize,
    /// Lines added by remote
    pub remote_added: usize,
    /// Lines deleted by local
    pub local_deleted: usize,
    /// Lines deleted by remote
    pub remote_deleted: usize,
    /// Lines with conflicts
    pub conflicts: usize,
}

/// Three-way merge implementation
pub struct ThreeWayMerge {
    /// Marker for local changes in conflict
    local_marker: String,
    /// Marker for remote changes in conflict
    remote_marker: String,
    /// Separator between versions
    separator: String,
}

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

impl ThreeWayMerge {
    /// Create a new three-way merger
    pub fn new() -> Self {
        Self {
            local_marker: "<<<<<<< LOCAL".to_string(),
            remote_marker: ">>>>>>> REMOTE".to_string(),
            separator: "=======".to_string(),
        }
    }

    /// Set custom conflict markers
    pub fn with_markers(
        mut self,
        local: impl Into<String>,
        separator: impl Into<String>,
        remote: impl Into<String>,
    ) -> Self {
        self.local_marker = local.into();
        self.separator = separator.into();
        self.remote_marker = remote.into();
        self
    }

    /// Perform three-way merge
    pub fn merge(&self, base: &str, local: &str, remote: &str) -> MergeResult {
        let base_lines: Vec<&str> = base.lines().collect();
        let local_lines: Vec<&str> = local.lines().collect();
        let remote_lines: Vec<&str> = remote.lines().collect();

        let mut result = Vec::new();
        let mut stats = MergeStats::default();
        let mut conflict_lines = Vec::new();
        let mut has_conflicts = false;

        let max_len = base_lines
            .len()
            .max(local_lines.len())
            .max(remote_lines.len());
        let mut base_idx = 0;
        let mut local_idx = 0;
        let mut remote_idx = 0;

        while base_idx < base_lines.len()
            || local_idx < local_lines.len()
            || remote_idx < remote_lines.len()
        {
            let base_line = base_lines.get(base_idx);
            let local_line = local_lines.get(local_idx);
            let remote_line = remote_lines.get(remote_idx);

            match (base_line, local_line, remote_line) {
                // All three match - keep it
                (Some(b), Some(l), Some(r)) if b == l && l == r => {
                    result.push((*l).to_string());
                    stats.base_kept += 1;
                    base_idx += 1;
                    local_idx += 1;
                    remote_idx += 1;
                }

                // Local changed, remote unchanged - take local
                (Some(b), Some(l), Some(r)) if b == r && b != l => {
                    result.push((*l).to_string());
                    stats.local_added += 1;
                    base_idx += 1;
                    local_idx += 1;
                    remote_idx += 1;
                }

                // Remote changed, local unchanged - take remote
                (Some(b), Some(l), Some(r)) if b == l && b != r => {
                    result.push((*r).to_string());
                    stats.remote_added += 1;
                    base_idx += 1;
                    local_idx += 1;
                    remote_idx += 1;
                }

                // Both changed differently - conflict!
                (Some(_), Some(l), Some(r)) if l != r => {
                    has_conflicts = true;
                    stats.conflicts += 1;
                    conflict_lines.push(result.len());

                    result.push(self.local_marker.clone());
                    result.push((*l).to_string());
                    result.push(self.separator.clone());
                    result.push((*r).to_string());
                    result.push(self.remote_marker.clone());

                    base_idx += 1;
                    local_idx += 1;
                    remote_idx += 1;
                }

                // Both changed to same - take it
                (Some(_), Some(l), Some(r)) if l == r => {
                    result.push((*l).to_string());
                    stats.local_added += 1;
                    base_idx += 1;
                    local_idx += 1;
                    remote_idx += 1;
                }

                // Local added line (past base)
                (None, Some(l), None) => {
                    result.push((*l).to_string());
                    stats.local_added += 1;
                    local_idx += 1;
                }

                // Remote added line (past base)
                (None, None, Some(r)) => {
                    result.push((*r).to_string());
                    stats.remote_added += 1;
                    remote_idx += 1;
                }

                // Both added different lines - conflict
                (None, Some(l), Some(r)) if l != r => {
                    has_conflicts = true;
                    stats.conflicts += 1;
                    conflict_lines.push(result.len());

                    result.push(self.local_marker.clone());
                    result.push((*l).to_string());
                    result.push(self.separator.clone());
                    result.push((*r).to_string());
                    result.push(self.remote_marker.clone());

                    local_idx += 1;
                    remote_idx += 1;
                }

                // Both added same line
                (None, Some(l), Some(r)) if l == r => {
                    result.push((*l).to_string());
                    stats.local_added += 1;
                    local_idx += 1;
                    remote_idx += 1;
                }

                // Local deleted (remote unchanged)
                (Some(_), None, Some(r)) if base_lines.get(base_idx) == Some(r) => {
                    stats.local_deleted += 1;
                    base_idx += 1;
                    remote_idx += 1;
                }

                // Remote deleted (local unchanged)
                (Some(_), Some(l), None) if base_lines.get(base_idx) == Some(l) => {
                    stats.remote_deleted += 1;
                    base_idx += 1;
                    local_idx += 1;
                }

                // Fallback: take what we have
                _ => {
                    if let Some(l) = local_line {
                        result.push((*l).to_string());
                    }
                    if let Some(r) = remote_line {
                        if local_line.map(|l| l != r).unwrap_or(true) {
                            result.push((*r).to_string());
                        }
                    }
                    break; // Prevent infinite loop
                }
            }

            // Safety: prevent infinite loops
            if base_idx + local_idx + remote_idx > max_len * 3 + 10 {
                break;
            }
        }

        MergeResult {
            content: result.join("\n"),
            success: !has_conflicts,
            has_conflict_markers: has_conflicts,
            conflict_lines,
            stats,
        }
    }

    /// Merge tags by taking union
    pub fn merge_tags(&self, base: &[String], local: &[String], remote: &[String]) -> Vec<String> {
        let mut result: HashSet<String> = HashSet::new();

        // Start with base
        result.extend(base.iter().cloned());

        // Add local additions
        for tag in local {
            result.insert(tag.clone());
        }

        // Add remote additions
        for tag in remote {
            result.insert(tag.clone());
        }

        // Remove tags deleted by both
        let local_set: HashSet<_> = local.iter().collect();
        let remote_set: HashSet<_> = remote.iter().collect();

        result
            .into_iter()
            .filter(|tag| {
                // Keep if: in local OR in remote OR (in base AND (in local OR in remote))
                local_set.contains(tag) || remote_set.contains(tag)
            })
            .collect()
    }

    /// Merge metadata by combining with local preference for conflicts
    /// Now works with HashMap directly
    pub fn merge_metadata_map(
        &self,
        base: Option<&HashMap<String, serde_json::Value>>,
        local: &HashMap<String, serde_json::Value>,
        remote: &HashMap<String, serde_json::Value>,
    ) -> HashMap<String, serde_json::Value> {
        let mut result = HashMap::new();

        // If local and remote are the same, return local
        if local == remote {
            return local.clone();
        }

        // Start with base if present, otherwise start fresh
        if let Some(base_map) = base {
            result = base_map.clone();
        }

        // Apply local changes
        for (k, v) in local {
            let base_value = base.and_then(|b| b.get(k));
            if base_value != Some(v) {
                result.insert(k.clone(), v.clone());
            }
        }

        // Apply remote changes (if not conflicting with local)
        for (k, v) in remote {
            let base_value = base.and_then(|b| b.get(k));
            let local_value = local.get(k);
            // Only apply remote change if local didn't change this key from base
            if base_value != Some(v) && local_value == base_value {
                result.insert(k.clone(), v.clone());
            }
        }

        // Add any keys that are only in local or remote (not in base)
        for (k, v) in local {
            if !result.contains_key(k) {
                result.insert(k.clone(), v.clone());
            }
        }
        for (k, v) in remote {
            if !result.contains_key(k) {
                result.insert(k.clone(), v.clone());
            }
        }

        result
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_merge_no_conflict() {
        let merger = ThreeWayMerge::new();

        let base = "Line 1\nLine 2\nLine 3";
        let local = "Line 1 modified\nLine 2\nLine 3";
        let remote = "Line 1\nLine 2\nLine 3 modified";

        let result = merger.merge(base, local, remote);
        assert!(result.success);
        assert!(!result.has_conflict_markers);
        assert!(result.content.contains("Line 1 modified"));
        assert!(result.content.contains("Line 3 modified"));
    }

    #[test]
    fn test_merge_with_conflict() {
        let merger = ThreeWayMerge::new();

        let base = "Line 1\nLine 2";
        let local = "Local change\nLine 2";
        let remote = "Remote change\nLine 2";

        let result = merger.merge(base, local, remote);
        assert!(!result.success);
        assert!(result.has_conflict_markers);
        assert!(result.content.contains("<<<<<<< LOCAL"));
        assert!(result.content.contains("Local change"));
        assert!(result.content.contains("======="));
        assert!(result.content.contains("Remote change"));
        assert!(result.content.contains(">>>>>>> REMOTE"));
    }

    #[test]
    fn test_merge_both_same_change() {
        let merger = ThreeWayMerge::new();

        let base = "Original";
        let local = "Same change";
        let remote = "Same change";

        let result = merger.merge(base, local, remote);
        assert!(result.success);
        assert_eq!(result.content, "Same change");
    }

    #[test]
    fn test_merge_tags() {
        let merger = ThreeWayMerge::new();

        let base = vec!["tag1".to_string(), "tag2".to_string()];
        let local = vec![
            "tag1".to_string(),
            "tag2".to_string(),
            "local_tag".to_string(),
        ];
        let remote = vec![
            "tag1".to_string(),
            "tag2".to_string(),
            "remote_tag".to_string(),
        ];

        let result = merger.merge_tags(&base, &local, &remote);
        assert!(result.contains(&"tag1".to_string()));
        assert!(result.contains(&"tag2".to_string()));
        assert!(result.contains(&"local_tag".to_string()));
        assert!(result.contains(&"remote_tag".to_string()));
    }

    #[test]
    fn test_merge_metadata_map() {
        let merger = ThreeWayMerge::new();

        let mut base = HashMap::new();
        base.insert("a".to_string(), serde_json::json!(1));
        base.insert("b".to_string(), serde_json::json!(2));

        let mut local = HashMap::new();
        local.insert("a".to_string(), serde_json::json!(10)); // Changed a
        local.insert("b".to_string(), serde_json::json!(2));

        let mut remote = HashMap::new();
        remote.insert("a".to_string(), serde_json::json!(1));
        remote.insert("b".to_string(), serde_json::json!(20)); // Changed b

        let result = merger.merge_metadata_map(Some(&base), &local, &remote);
        assert_eq!(result.get("a"), Some(&serde_json::json!(10))); // Local's change
        assert_eq!(result.get("b"), Some(&serde_json::json!(20))); // Remote's change
    }
}