merge-engine 0.1.0

A non-LLM merge conflict resolver using structured merge, Version Space Algebra, and search-based techniques
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
471
472
473
474
475
476
477
478
//! Three-way tree amalgamation.
//!
//! Implements the merging phase from LASTMERGE (2025) and Mastery (2023).
//! Given three CST trees (base, left, right) and their pairwise matchings,
//! the amalgamator traverses them in depth-first order, deciding for each
//! node whether to:
//! - Keep the base version (no changes)
//! - Accept the left change (only left modified)
//! - Accept the right change (only right modified)
//! - Accept both if identical changes
//! - Report a conflict if both modified differently
//!
//! For unordered list nodes, we apply the heuristic from LASTMERGE:
//! reorder children to minimize spurious conflicts.

use std::collections::{HashMap, HashSet};

use crate::matcher::{match_trees, tree_similarity};
use crate::types::{CstNode, ListOrdering, MergeResult, MergeScenario, NodeId};

/// Result of amalgamating a single tree node.
#[derive(Debug)]
pub enum AmalgamResult {
    /// Cleanly merged subtree.
    Merged(CstNode),
    /// Conflict — preserves both sides.
    Conflict {
        base: CstNode,
        left: CstNode,
        right: CstNode,
    },
}

/// Perform three-way tree amalgamation.
///
/// This is the core structured merge algorithm. It identifies actual semantic
/// conflicts vs. false positives that line-based diff3 would flag.
pub fn amalgamate(scenario: &MergeScenario<&CstNode>) -> AmalgamResult {
    // Phase 1: Compute pairwise matchings
    let bl_matches = match_trees(scenario.base, scenario.left);
    let br_matches = match_trees(scenario.base, scenario.right);
    let lr_matches = match_trees(scenario.left, scenario.right);

    // Build match maps: base_id → left_id, base_id → right_id
    let bl_map: HashMap<NodeId, NodeId> = bl_matches.iter().map(|p| (p.left, p.right)).collect();
    let br_map: HashMap<NodeId, NodeId> = br_matches.iter().map(|p| (p.left, p.right)).collect();
    let lr_map: HashMap<NodeId, NodeId> = lr_matches.iter().map(|p| (p.left, p.right)).collect();

    // Phase 2: Top-down traversal with conflict detection
    amalgamate_node(
        scenario.base,
        scenario.left,
        scenario.right,
        &bl_map,
        &br_map,
        &lr_map,
    )
}

fn amalgamate_node(
    base: &CstNode,
    left: &CstNode,
    right: &CstNode,
    bl_map: &HashMap<NodeId, NodeId>,
    br_map: &HashMap<NodeId, NodeId>,
    lr_map: &HashMap<NodeId, NodeId>,
) -> AmalgamResult {
    // Check if both sides are identical to base (no change)
    if base.structurally_equal(left) && base.structurally_equal(right) {
        return AmalgamResult::Merged(base.clone());
    }

    // Only left changed
    if base.structurally_equal(right) {
        return AmalgamResult::Merged(left.clone());
    }

    // Only right changed
    if base.structurally_equal(left) {
        return AmalgamResult::Merged(right.clone());
    }

    // Both changed identically
    if left.structurally_equal(right) {
        return AmalgamResult::Merged(left.clone());
    }

    // Both changed differently — try to merge at a finer granularity
    match (base, left, right) {
        // All are leaves — true conflict
        (CstNode::Leaf { .. }, CstNode::Leaf { .. }, CstNode::Leaf { .. }) => {
            AmalgamResult::Conflict {
                base: base.clone(),
                left: left.clone(),
                right: right.clone(),
            }
        }
        // All are non-terminal with children — try child-level merge
        _ if !base.is_leaf() && !left.is_leaf() && !right.is_leaf() => {
            amalgamate_children(base, left, right, bl_map, br_map, lr_map)
        }
        // Structure mismatch — conflict
        _ => AmalgamResult::Conflict {
            base: base.clone(),
            left: left.clone(),
            right: right.clone(),
        },
    }
}

/// Merge at the children level for non-terminal nodes.
fn amalgamate_children(
    base: &CstNode,
    left: &CstNode,
    right: &CstNode,
    bl_map: &HashMap<NodeId, NodeId>,
    br_map: &HashMap<NodeId, NodeId>,
    lr_map: &HashMap<NodeId, NodeId>,
) -> AmalgamResult {
    let base_children = base.children();
    let left_children = left.children();
    let right_children = right.children();

    // For unordered nodes (imports, class members), try the unordered merge
    if let CstNode::List {
        ordering: ListOrdering::Unordered,
        ..
    } = base
    {
        return amalgamate_unordered(base, left, right, bl_map, br_map);
    }

    // For ordered nodes, walk children in lockstep using the matchings
    let bl_child_map = build_child_match_map(base_children, left_children, bl_map);
    let br_child_map = build_child_match_map(base_children, right_children, br_map);

    let mut merged_children = Vec::new();
    let mut has_conflict = false;
    let mut conflict_base = base.clone();
    let mut conflict_left = left.clone();
    let mut conflict_right = right.clone();

    // Track which left/right children have been processed
    let mut used_left: HashSet<NodeId> = HashSet::new();
    let mut used_right: HashSet<NodeId> = HashSet::new();

    for base_child in base_children {
        let left_match = bl_child_map.get(&base_child.id());
        let right_match = br_child_map.get(&base_child.id());

        match (left_match, right_match) {
            // Both sides have a matching child → recurse
            (Some(lc), Some(rc)) => {
                used_left.insert(lc.id());
                used_right.insert(rc.id());
                match amalgamate_node(base_child, lc, rc, bl_map, br_map, lr_map) {
                    AmalgamResult::Merged(node) => merged_children.push(node),
                    AmalgamResult::Conflict {
                        base: b,
                        left: l,
                        right: r,
                    } => {
                        has_conflict = true;
                        conflict_base = b;
                        conflict_left = l;
                        conflict_right = r;
                        // Still add left's version as placeholder
                        merged_children.push((*lc).clone());
                    }
                }
            }
            // Only left has it — right deleted it
            (Some(lc), None) => {
                used_left.insert(lc.id());
                // If left didn't modify it, accept the deletion
                if base_child.structurally_equal(lc) {
                    // Right deleted, left unchanged → accept deletion
                } else {
                    // Delete/edit conflict
                    has_conflict = true;
                    conflict_base = base_child.clone();
                    conflict_left = (*lc).clone();
                    conflict_right = CstNode::Leaf {
                        id: 0,
                        kind: "deleted".into(),
                        value: String::new(),
                    };
                }
            }
            // Only right has it — left deleted it
            (None, Some(rc)) => {
                used_right.insert(rc.id());
                if base_child.structurally_equal(rc) {
                    // Left deleted, right unchanged → accept deletion
                } else {
                    has_conflict = true;
                    conflict_base = base_child.clone();
                    conflict_left = CstNode::Leaf {
                        id: 0,
                        kind: "deleted".into(),
                        value: String::new(),
                    };
                    conflict_right = (*rc).clone();
                }
            }
            // Both deleted — accept deletion
            (None, None) => {}
        }
    }

    // Add children that were inserted by left (not in base)
    for lc in left_children {
        if !used_left.contains(&lc.id()) && !bl_child_map.values().any(|v| v.id() == lc.id()) {
            merged_children.push(lc.clone());
        }
    }

    // Add children that were inserted by right (not in base)
    for rc in right_children {
        if !used_right.contains(&rc.id()) && !br_child_map.values().any(|v| v.id() == rc.id()) {
            merged_children.push(rc.clone());
        }
    }

    if has_conflict {
        AmalgamResult::Conflict {
            base: conflict_base,
            left: conflict_left,
            right: conflict_right,
        }
    } else {
        // Reconstruct node with merged children
        let merged = reconstruct_node(base, merged_children);
        AmalgamResult::Merged(merged)
    }
}

/// Amalgamation for unordered list nodes.
///
/// Per LASTMERGE heuristic: for unordered children (imports, class members),
/// we can resolve many "false conflicts" that arise from reordering.
/// Strategy: take the union of both sides' additions, and agree on deletions
/// only when both sides delete.
fn amalgamate_unordered(
    base: &CstNode,
    left: &CstNode,
    right: &CstNode,
    _bl_map: &HashMap<NodeId, NodeId>,
    _br_map: &HashMap<NodeId, NodeId>,
) -> AmalgamResult {
    let base_children = base.children();
    let left_children = left.children();
    let right_children = right.children();

    let mut result_children = Vec::new();
    let mut used_left: HashSet<usize> = HashSet::new();
    let mut used_right: HashSet<usize> = HashSet::new();

    // Match base children to left and right
    for bc in base_children {
        let left_match = left_children
            .iter()
            .enumerate()
            .find(|(idx, lc)| !used_left.contains(idx) && bc.structurally_equal(lc));
        let right_match = right_children
            .iter()
            .enumerate()
            .find(|(idx, rc)| !used_right.contains(idx) && bc.structurally_equal(rc));

        match (left_match, right_match) {
            (Some((li, _)), Some((ri, _))) => {
                // Both kept it
                used_left.insert(li);
                used_right.insert(ri);
                result_children.push(bc.clone());
            }
            (Some((li, _)), None) => {
                // Right deleted — accept deletion
                used_left.insert(li);
            }
            (None, Some((ri, _))) => {
                // Left deleted — accept deletion
                used_right.insert(ri);
            }
            (None, None) => {
                // Both deleted — accept deletion
            }
        }
    }

    // Add new items from left (not in base)
    for (i, lc) in left_children.iter().enumerate() {
        if !used_left.contains(&i) {
            result_children.push(lc.clone());
        }
    }

    // Add new items from right (not in base)
    for (i, rc) in right_children.iter().enumerate() {
        if !used_right.contains(&i) {
            // Check for duplicate with left additions
            let already_added = result_children.iter().any(|c| c.structurally_equal(rc));
            if !already_added {
                result_children.push(rc.clone());
            }
        }
    }

    AmalgamResult::Merged(reconstruct_node(base, result_children))
}

/// Build a map from base child IDs to their matched counterparts.
fn build_child_match_map<'a>(
    base_children: &[CstNode],
    other_children: &'a [CstNode],
    match_map: &HashMap<NodeId, NodeId>,
) -> HashMap<NodeId, &'a CstNode> {
    let other_by_id: HashMap<NodeId, &CstNode> =
        other_children.iter().map(|c| (c.id(), c)).collect();

    let mut result = HashMap::new();
    for bc in base_children {
        if let Some(&other_id) = match_map.get(&bc.id())
            && let Some(other_node) = other_by_id.get(&other_id)
        {
            result.insert(bc.id(), *other_node);
        }
    }

    // Fallback: match by structural similarity if ID matching fails
    let matched_other: HashSet<NodeId> = result.values().map(|n| n.id()).collect();
    for bc in base_children {
        if result.contains_key(&bc.id()) {
            continue;
        }
        // Find best unmatched other child by similarity
        let best = other_children
            .iter()
            .filter(|oc| !matched_other.contains(&oc.id()))
            .filter(|oc| oc.kind() == bc.kind())
            .max_by_key(|oc| tree_similarity(bc, oc));
        if let Some(matched) = best
            && tree_similarity(bc, matched) > 0
        {
            result.insert(bc.id(), matched);
        }
    }

    result
}

/// Reconstruct a node with new children, preserving the original's kind and type.
fn reconstruct_node(template: &CstNode, children: Vec<CstNode>) -> CstNode {
    match template {
        CstNode::Leaf { .. } => template.clone(),
        CstNode::Constructed { id, kind, .. } => CstNode::Constructed {
            id: *id,
            kind: kind.clone(),
            children,
        },
        CstNode::List {
            id, kind, ordering, ..
        } => CstNode::List {
            id: *id,
            kind: kind.clone(),
            ordering: *ordering,
            children,
        },
    }
}

/// Convert an AmalgamResult to a MergeResult (text-level).
pub fn amalgam_to_merge_result(result: &AmalgamResult) -> MergeResult {
    match result {
        AmalgamResult::Merged(node) => MergeResult::Resolved(node.to_source()),
        AmalgamResult::Conflict { base, left, right } => MergeResult::Conflict {
            base: base.to_source(),
            left: left.to_source(),
            right: right.to_source(),
        },
    }
}

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

    fn leaf(id: usize, val: &str) -> CstNode {
        CstNode::Leaf {
            id,
            kind: "ident".into(),
            value: val.into(),
        }
    }

    #[allow(dead_code)]
    fn list(id: usize, children: Vec<CstNode>) -> CstNode {
        CstNode::List {
            id,
            kind: "block".into(),
            ordering: ListOrdering::Ordered,
            children,
        }
    }

    fn unordered_list(id: usize, children: Vec<CstNode>) -> CstNode {
        CstNode::List {
            id,
            kind: "import_list".into(),
            ordering: ListOrdering::Unordered,
            children,
        }
    }

    #[test]
    fn test_no_change() {
        let base = leaf(1, "x");
        let left = leaf(2, "x");
        let right = leaf(3, "x");
        let scenario = MergeScenario::new(&base, &left, &right);
        let result = amalgamate(&scenario);
        assert!(matches!(result, AmalgamResult::Merged(_)));
    }

    #[test]
    fn test_left_only_change() {
        let base = leaf(1, "x");
        let left = leaf(2, "y");
        let right = leaf(3, "x");
        let scenario = MergeScenario::new(&base, &left, &right);
        let result = amalgamate(&scenario);
        match result {
            AmalgamResult::Merged(node) => assert_eq!(node.leaf_value(), Some("y")),
            _ => panic!("expected merged"),
        }
    }

    #[test]
    fn test_both_same_change() {
        let base = leaf(1, "x");
        let left = leaf(2, "z");
        let right = leaf(3, "z");
        let scenario = MergeScenario::new(&base, &left, &right);
        let result = amalgamate(&scenario);
        match result {
            AmalgamResult::Merged(node) => assert_eq!(node.leaf_value(), Some("z")),
            _ => panic!("expected merged"),
        }
    }

    #[test]
    fn test_true_conflict() {
        let base = leaf(1, "x");
        let left = leaf(2, "y");
        let right = leaf(3, "z");
        let scenario = MergeScenario::new(&base, &left, &right);
        let result = amalgamate(&scenario);
        assert!(matches!(result, AmalgamResult::Conflict { .. }));
    }

    #[test]
    fn test_unordered_merge_additions() {
        let base = unordered_list(1, vec![leaf(2, "a"), leaf(3, "b")]);
        let left = unordered_list(4, vec![leaf(5, "a"), leaf(6, "b"), leaf(7, "c")]);
        let right = unordered_list(8, vec![leaf(9, "a"), leaf(10, "b"), leaf(11, "d")]);

        let scenario = MergeScenario::new(&base, &left, &right);
        let result = amalgamate(&scenario);
        match result {
            AmalgamResult::Merged(node) => {
                // Should have a, b, c, d (union of both additions)
                assert_eq!(node.children().len(), 4);
            }
            _ => panic!("expected unordered merge to succeed"),
        }
    }
}