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
//! AST matching algorithms for three-way structured merge.
//!
//! Implements the matching phase from LASTMERGE (2025) and Mastery (2023):
//! - **Ordered matching**: Yang's algorithm (dynamic programming, O(n²))
//!   for children of ordered list/constructed nodes.
//! - **Unordered matching**: Bipartite maximum weight matching (O(n³))
//!   for children of unordered list nodes (imports, class members).
//!
//! The matching phase produces a set of MatchPairs linking corresponding
//! nodes across two revisions, which the amalgamation phase then uses
//! to determine what changed.

use std::collections::HashMap;

use crate::types::{CstNode, ListOrdering, MatchPair, NodeId};

/// Compute the maximum matching between children of two parent nodes.
/// Dispatches to ordered (Yang's) or unordered (bipartite) algorithm
/// based on the parent's ordering semantics.
pub fn match_children(
    left_parent: &CstNode,
    right_parent: &CstNode,
    ordering: ListOrdering,
) -> Vec<MatchPair> {
    let left_children = left_parent.children();
    let right_children = right_parent.children();

    match ordering {
        ListOrdering::Ordered => yang_match(left_children, right_children),
        ListOrdering::Unordered => bipartite_match(left_children, right_children),
    }
}

/// Recursively compute matches between two trees, returning all matched pairs.
pub fn match_trees(left: &CstNode, right: &CstNode) -> Vec<MatchPair> {
    let mut pairs = Vec::new();
    match_trees_recursive(left, right, &mut pairs);
    pairs
}

fn match_trees_recursive(left: &CstNode, right: &CstNode, pairs: &mut Vec<MatchPair>) {
    // Nodes can only match if they have the same kind (LASTMERGE rule)
    if left.kind() != right.kind() {
        return;
    }

    // Leaf-to-leaf match
    if left.is_leaf() && right.is_leaf() {
        if left.leaf_value() == right.leaf_value() {
            pairs.push(MatchPair {
                left: left.id(),
                right: right.id(),
                score: 1,
            });
        }
        return;
    }

    // Can't match leaf to non-leaf
    if left.is_leaf() != right.is_leaf() {
        return;
    }

    // Root nodes match — compute the matching score
    let similarity = tree_similarity(left, right);
    if similarity > 0 {
        pairs.push(MatchPair {
            left: left.id(),
            right: right.id(),
            score: similarity,
        });
    }

    // Determine ordering for child matching
    let ordering = match (left, right) {
        (CstNode::List { ordering: lo, .. }, CstNode::List { .. }) => *lo,
        _ => ListOrdering::Ordered,
    };

    // Compute child matches
    let child_pairs = match_children(left, right, ordering);

    // Recurse into matched children
    let left_children = left.children();
    let right_children = right.children();

    // Build lookup from matched pairs to recurse into
    let left_map: HashMap<NodeId, &CstNode> = left_children.iter().map(|c| (c.id(), c)).collect();
    let right_map: HashMap<NodeId, &CstNode> = right_children.iter().map(|c| (c.id(), c)).collect();

    for pair in child_pairs {
        if let (Some(lc), Some(rc)) = (left_map.get(&pair.left), right_map.get(&pair.right)) {
            match_trees_recursive(lc, rc, pairs);
        }
    }
}

/// Yang's algorithm for ordered sequence matching.
///
/// Uses dynamic programming to find the maximum weight matching between
/// two ordered sequences of AST nodes. This is essentially a weighted LCS
/// where the weight of matching two nodes is their subtree similarity.
///
/// Time complexity: O(n * m) where n, m are the lengths of the two sequences.
/// Reference: Yang (1991), "Identifying Syntactic Differences Between Two Programs"
fn yang_match(left: &[CstNode], right: &[CstNode]) -> Vec<MatchPair> {
    let n = left.len();
    let m = right.len();

    if n == 0 || m == 0 {
        return Vec::new();
    }

    // Build DP table: dp[i][j] = max matching score for left[0..i] and right[0..j]
    let mut dp = vec![vec![0usize; m + 1]; n + 1];
    let mut choice = vec![vec![0u8; m + 1]; n + 1]; // 0=skip, 1=match, 2=skip-left, 3=skip-right

    for i in 1..=n {
        for j in 1..=m {
            // Option 1: match left[i-1] with right[j-1] if compatible
            let match_score = if can_match(&left[i - 1], &right[j - 1]) {
                dp[i - 1][j - 1] + tree_similarity(&left[i - 1], &right[j - 1])
            } else {
                0
            };

            // Option 2: skip left[i-1]
            let skip_left = dp[i - 1][j];

            // Option 3: skip right[j-1]
            let skip_right = dp[i][j - 1];

            if match_score >= skip_left && match_score >= skip_right && match_score > 0 {
                dp[i][j] = match_score;
                choice[i][j] = 1;
            } else if skip_left >= skip_right {
                dp[i][j] = skip_left;
                choice[i][j] = 2;
            } else {
                dp[i][j] = skip_right;
                choice[i][j] = 3;
            }
        }
    }

    // Trace back to find the matching
    let mut pairs = Vec::new();
    let mut i = n;
    let mut j = m;
    while i > 0 && j > 0 {
        match choice[i][j] {
            1 => {
                pairs.push(MatchPair {
                    left: left[i - 1].id(),
                    right: right[j - 1].id(),
                    score: tree_similarity(&left[i - 1], &right[j - 1]),
                });
                i -= 1;
                j -= 1;
            }
            2 => i -= 1,
            3 => j -= 1,
            _ => break,
        }
    }

    pairs.reverse();
    pairs
}

/// Bipartite maximum weight matching for unordered children.
///
/// Uses the Hungarian algorithm (Kuhn-Munkres) to find the maximum weight
/// matching between two sets of AST nodes. This is appropriate for unordered
/// nodes like import lists or class members where position doesn't matter.
///
/// Time complexity: O(n³) where n = max(|left|, |right|).
/// Reference: LASTMERGE (2025), JDime (Apel et al.)
fn bipartite_match(left: &[CstNode], right: &[CstNode]) -> Vec<MatchPair> {
    let n = left.len();
    let m = right.len();
    if n == 0 || m == 0 {
        return Vec::new();
    }

    // Build similarity matrix
    let size = n.max(m);
    let mut weights = vec![vec![0i64; size]; size];

    for (i, l) in left.iter().enumerate() {
        for (j, r) in right.iter().enumerate() {
            if can_match(l, r) {
                weights[i][j] = tree_similarity(l, r) as i64;
            }
        }
    }

    // Hungarian algorithm for maximum weight matching
    let assignment = hungarian_max(&weights, size);

    let mut pairs = Vec::new();
    for (i, &j) in assignment.iter().enumerate() {
        if i < n && j < m && weights[i][j] > 0 {
            pairs.push(MatchPair {
                left: left[i].id(),
                right: right[j].id(),
                score: weights[i][j] as usize,
            });
        }
    }

    pairs
}

/// Check if two nodes are structurally compatible for matching.
/// Per LASTMERGE: terminal can't match non-terminal, and kinds must be identical.
fn can_match(left: &CstNode, right: &CstNode) -> bool {
    if left.is_leaf() != right.is_leaf() {
        return false;
    }
    left.kind() == right.kind()
}

/// Compute the similarity score between two subtrees.
/// Uses a simple recursive leaf-counting metric:
/// similarity = number of matching leaves in both trees.
pub fn tree_similarity(left: &CstNode, right: &CstNode) -> usize {
    if !can_match(left, right) {
        return 0;
    }

    match (left, right) {
        (CstNode::Leaf { value: v1, .. }, CstNode::Leaf { value: v2, .. }) => {
            if v1 == v2 {
                1
            } else {
                0
            }
        }
        _ => {
            // Count matching leaves between the two subtrees
            let left_leaves = left.collect_leaves();
            let right_leaves = right.collect_leaves();
            lcs_length(&left_leaves, &right_leaves)
        }
    }
}

/// Compute LCS length between two sequences.
fn lcs_length<T: PartialEq>(a: &[T], b: &[T]) -> usize {
    let n = a.len();
    let m = b.len();
    let mut dp = vec![vec![0usize; m + 1]; n + 1];
    for i in 1..=n {
        for j in 1..=m {
            dp[i][j] = if a[i - 1] == b[j - 1] {
                dp[i - 1][j - 1] + 1
            } else {
                dp[i - 1][j].max(dp[i][j - 1])
            };
        }
    }
    dp[n][m]
}

/// Simple Hungarian algorithm for maximum weight matching.
/// Converts to minimum cost by negating, then uses the standard algorithm.
fn hungarian_max(weights: &[Vec<i64>], n: usize) -> Vec<usize> {
    if n == 0 {
        return Vec::new();
    }

    // Convert to cost minimization by finding max and subtracting
    let max_w = weights
        .iter()
        .flat_map(|row| row.iter())
        .copied()
        .max()
        .unwrap_or(0);

    let mut cost = vec![vec![0i64; n]; n];
    for i in 0..n {
        for j in 0..n {
            cost[i][j] = max_w - weights[i][j];
        }
    }

    // Kuhn-Munkres algorithm
    let mut u = vec![0i64; n + 1];
    let mut v = vec![0i64; n + 1];
    let mut p = vec![0usize; n + 1]; // p[j] = row assigned to col j
    let mut way = vec![0usize; n + 1];

    for i in 1..=n {
        p[0] = i;
        let mut j0 = 0usize;
        let mut minv = vec![i64::MAX; n + 1];
        let mut used = vec![false; n + 1];

        loop {
            used[j0] = true;
            let i0 = p[j0];
            let mut delta = i64::MAX;
            let mut j1 = 0usize;

            for j in 1..=n {
                if !used[j] {
                    let cur = cost[i0 - 1][j - 1] - u[i0] - v[j];
                    if cur < minv[j] {
                        minv[j] = cur;
                        way[j] = j0;
                    }
                    if minv[j] < delta {
                        delta = minv[j];
                        j1 = j;
                    }
                }
            }

            for j in 0..=n {
                if used[j] {
                    u[p[j]] += delta;
                    v[j] -= delta;
                } else {
                    minv[j] -= delta;
                }
            }

            j0 = j1;
            if p[j0] == 0 {
                break;
            }
        }

        loop {
            let j1 = way[j0];
            p[j0] = p[j1];
            j0 = j1;
            if j0 == 0 {
                break;
            }
        }
    }

    // Extract assignment: result[i] = column assigned to row i
    let mut result = vec![0usize; n];
    for j in 1..=n {
        if p[j] > 0 {
            result[p[j] - 1] = j - 1;
        }
    }
    result
}

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

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

    #[test]
    fn test_yang_match_identical() {
        let left = vec![leaf(1, "a"), leaf(2, "b"), leaf(3, "c")];
        let right = vec![leaf(4, "a"), leaf(5, "b"), leaf(6, "c")];
        let pairs = yang_match(&left, &right);
        assert_eq!(pairs.len(), 3);
    }

    #[test]
    fn test_yang_match_partial() {
        let left = vec![leaf(1, "a"), leaf(2, "b"), leaf(3, "c")];
        let right = vec![leaf(4, "a"), leaf(5, "c")];
        let pairs = yang_match(&left, &right);
        assert_eq!(pairs.len(), 2);
    }

    #[test]
    fn test_bipartite_match() {
        let left = vec![leaf(1, "a"), leaf(2, "b")];
        let right = vec![leaf(3, "b"), leaf(4, "a")];
        let pairs = bipartite_match(&left, &right);
        assert_eq!(pairs.len(), 2);
    }

    #[test]
    fn test_hungarian_simple() {
        let weights = vec![vec![3, 1], vec![1, 3]];
        let assignment = hungarian_max(&weights, 2);
        // Optimal: row 0→col 0 (3), row 1→col 1 (3) = 6
        assert_eq!(assignment[0], 0);
        assert_eq!(assignment[1], 1);
    }

    #[test]
    fn test_tree_similarity() {
        let a = leaf(1, "hello");
        let b = leaf(2, "hello");
        let c = leaf(3, "world");
        assert_eq!(tree_similarity(&a, &b), 1);
        assert_eq!(tree_similarity(&a, &c), 0);
    }
}