Skip to main content

lex_vcs/
merge.rs

1//! Op-DAG three-way merge.
2//!
3//! 1. Compute LCA of src and dst heads.
4//! 2. Get ops on each side since the LCA.
5//! 3. Group by the `SigId` they touch; classify each group.
6
7use crate::op_log::OpLog;
8use crate::operation::{OpId, OperationKind, OperationRecord, SigId, StageId};
9use std::collections::{BTreeMap, BTreeSet};
10use std::io;
11
12#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
13#[serde(tag = "outcome", rename_all = "snake_case")]
14pub enum MergeOutcome {
15    /// Both sides converged on the same op_id for this sig.
16    Both { sig_id: SigId, stage_id: Option<StageId> },
17    /// Only src touched it.
18    Src  { sig_id: SigId, stage_id: Option<StageId> },
19    /// Only dst touched it.
20    Dst  { sig_id: SigId, stage_id: Option<StageId> },
21    /// Conflict: both sides touched it with different ops.
22    Conflict {
23        sig_id: SigId,
24        kind: ConflictKind,
25        base: Option<StageId>,
26        src:  Option<StageId>,
27        dst:  Option<StageId>,
28    },
29}
30
31#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
32#[serde(rename_all = "snake_case")]
33pub enum ConflictKind {
34    ModifyModify,
35    ModifyDelete,
36    DeleteModify,
37    AddAdd,
38}
39
40#[derive(Debug)]
41pub struct MergeOutput {
42    pub lca: Option<OpId>,
43    pub outcomes: Vec<MergeOutcome>,
44}
45
46pub fn merge(
47    op_log: &OpLog,
48    src_head: Option<&OpId>,
49    dst_head: Option<&OpId>,
50) -> io::Result<MergeOutput> {
51    let lca = match (src_head, dst_head) {
52        (Some(s), Some(d)) => op_log.lca(s, d)?,
53        _ => None,
54    };
55    let src_ops = match src_head {
56        Some(h) => op_log.ops_since(h, lca.as_ref())?,
57        None => Vec::new(),
58    };
59    let dst_ops = match dst_head {
60        Some(h) => op_log.ops_since(h, lca.as_ref())?,
61        None => Vec::new(),
62    };
63
64    let src_by_sig = group_by_sig(&src_ops);
65    let dst_by_sig = group_by_sig(&dst_ops);
66
67    let lca_head: BTreeMap<SigId, StageId> = match lca.as_ref() {
68        Some(id) => head_at(op_log, id)?,
69        None => BTreeMap::new(),
70    };
71
72    let mut outcomes = Vec::new();
73    let sigs: BTreeSet<&SigId> = src_by_sig.keys().chain(dst_by_sig.keys()).collect();
74    for sig in sigs {
75        let s = src_by_sig.get(sig);
76        let d = dst_by_sig.get(sig);
77        let s_stage = s.map(|recs| latest_stage(sig, recs));
78        let d_stage = d.map(|recs| latest_stage(sig, recs));
79        match (s, d) {
80            (Some(s_recs), Some(d_recs)) => {
81                let s_last = s_recs.last().map(|r| r.op_id.as_str()).unwrap_or("");
82                let d_last = d_recs.last().map(|r| r.op_id.as_str()).unwrap_or("");
83                if s_last == d_last {
84                    outcomes.push(MergeOutcome::Both {
85                        sig_id: sig.clone(),
86                        stage_id: s_stage.unwrap(),
87                    });
88                } else {
89                    let kind = classify(&s_stage.clone().unwrap(), &d_stage.clone().unwrap(), &lca_head, sig);
90                    outcomes.push(MergeOutcome::Conflict {
91                        sig_id: sig.clone(),
92                        kind,
93                        base: lca_head.get(sig).cloned(),
94                        src:  s_stage.unwrap(),
95                        dst:  d_stage.unwrap(),
96                    });
97                }
98            }
99            (Some(_), None) => {
100                outcomes.push(MergeOutcome::Src {
101                    sig_id: sig.clone(),
102                    stage_id: s_stage.unwrap(),
103                });
104            }
105            (None, Some(_)) => {
106                outcomes.push(MergeOutcome::Dst {
107                    sig_id: sig.clone(),
108                    stage_id: d_stage.unwrap(),
109                });
110            }
111            (None, None) => unreachable!(),
112        }
113    }
114
115    Ok(MergeOutput { lca, outcomes })
116}
117
118fn group_by_sig(ops: &[OperationRecord]) -> BTreeMap<SigId, Vec<&OperationRecord>> {
119    let mut out: BTreeMap<SigId, Vec<&OperationRecord>> = BTreeMap::new();
120    for r in ops {
121        for sig in touched_sigs(&r.op.kind) {
122            out.entry(sig).or_default().push(r);
123        }
124    }
125    // ops_since returned newest-first; reverse to oldest-first per sig
126    // so `latest_stage` reads the right entry.
127    for v in out.values_mut() { v.reverse(); }
128    out
129}
130
131fn touched_sigs(k: &OperationKind) -> Vec<SigId> {
132    match k {
133        OperationKind::AddFunction { sig_id, .. }
134        | OperationKind::RemoveFunction { sig_id, .. }
135        | OperationKind::ModifyBody { sig_id, .. }
136        | OperationKind::ChangeEffectSig { sig_id, .. }
137        | OperationKind::AddType { sig_id, .. }
138        | OperationKind::RemoveType { sig_id, .. }
139        | OperationKind::ModifyType { sig_id, .. }
140        | OperationKind::ReplaceMatchArm { sig_id, .. }
141        | OperationKind::RenameLocal { sig_id, .. }
142        | OperationKind::InlineLet { sig_id, .. } => vec![sig_id.clone()],
143        // A rename touches both sides — concurrent modifies on `from`
144        // must surface as a conflict, not as a disjoint set.
145        OperationKind::RenameSymbol { from, to, .. } => vec![from.clone(), to.clone()],
146        OperationKind::AddImport { .. }
147        | OperationKind::RemoveImport { .. }
148        | OperationKind::Merge { .. } => Vec::new(),
149    }
150}
151
152/// Given a chronological (oldest-first) list of ops on a sig, return
153/// the resulting stage_id (`None` if the sig was removed).
154///
155/// The `sig` parameter is used to distinguish the `from` and `to`
156/// sides of a `RenameSymbol` operation: from the `from` sig's
157/// perspective the rename removes it; from the `to` sig's perspective
158/// it produces `body_stage_id`.
159fn latest_stage(sig: &SigId, recs: &[&OperationRecord]) -> Option<StageId> {
160    use crate::operation::{OperationKind as OK, StageTransition::*};
161    let mut current: Option<StageId> = None;
162    for r in recs {
163        // For renames: distinguish which side of the rename we're on.
164        if let OK::RenameSymbol { from, to, body_stage_id } = &r.op.kind {
165            if sig == from {
166                // From this sig's perspective, the rename removed it.
167                current = None;
168            } else if sig == to {
169                current = Some(body_stage_id.clone());
170            }
171            continue;
172        }
173        match &r.produces {
174            Create { stage_id, .. } => current = Some(stage_id.clone()),
175            Replace { to, .. } => current = Some(to.clone()),
176            Remove { .. } => current = None,
177            Rename { body_stage_id, .. } => current = Some(body_stage_id.clone()),
178            ImportOnly | Merge { .. } => {}
179        }
180    }
181    current
182}
183
184fn head_at(op_log: &OpLog, head: &OpId) -> io::Result<BTreeMap<SigId, StageId>> {
185    let mut map = BTreeMap::new();
186    for r in op_log.walk_forward(head, None)? {
187        use crate::operation::StageTransition::*;
188        match &r.produces {
189            Create { sig_id, stage_id } => { map.insert(sig_id.clone(), stage_id.clone()); }
190            Replace { sig_id, to, .. } => { map.insert(sig_id.clone(), to.clone()); }
191            Remove { sig_id, .. } => { map.remove(sig_id); }
192            Rename { from, to, body_stage_id } => {
193                map.remove(from);
194                map.insert(to.clone(), body_stage_id.clone());
195            }
196            ImportOnly => {}
197            Merge { entries } => {
198                for (sig, stage) in entries {
199                    match stage {
200                        Some(s) => { map.insert(sig.clone(), s.clone()); }
201                        None    => { map.remove(sig); }
202                    }
203                }
204            }
205        }
206    }
207    Ok(map)
208}
209
210fn classify(
211    src: &Option<StageId>,
212    dst: &Option<StageId>,
213    base: &BTreeMap<SigId, StageId>,
214    sig: &SigId,
215) -> ConflictKind {
216    let in_base = base.contains_key(sig);
217    match (in_base, src.is_some(), dst.is_some()) {
218        (false, true, true)  => ConflictKind::AddAdd,
219        (true,  true, true)  => ConflictKind::ModifyModify,
220        (true,  true, false) => ConflictKind::ModifyDelete,
221        (true,  false, true) => ConflictKind::DeleteModify,
222        // Other combos shouldn't happen for a "both touched" group:
223        // both sides touched the sig, so at least one should have a
224        // result, and the (false, false, false) / (true, false, false)
225        // shapes are unreachable. Surface as a panic in debug builds
226        // so future invariant violations are loud, not silent.
227        other => {
228            debug_assert!(false, "classify: unreachable shape {other:?} for sig {sig}");
229            ConflictKind::ModifyModify
230        }
231    }
232}
233
234#[cfg(test)]
235mod tests {
236    use super::*;
237    use crate::apply::apply;
238    use crate::operation::{Operation, OperationKind, StageTransition};
239    use std::collections::BTreeSet;
240
241    fn fresh() -> (OpLog, tempfile::TempDir) {
242        let tmp = tempfile::tempdir().unwrap();
243        (OpLog::open(tmp.path()).unwrap(), tmp)
244    }
245
246    fn add_fn(log: &OpLog, parent: Option<&OpId>, sig: &str, stg: &str) -> OpId {
247        let op = Operation::new(
248            OperationKind::AddFunction {
249                sig_id: sig.into(),
250                stage_id: stg.into(),
251                effects: BTreeSet::new(),
252                budget_cost: None,
253            },
254            parent.cloned().into_iter().collect::<Vec<_>>(),
255        );
256        let t = StageTransition::Create { sig_id: sig.into(), stage_id: stg.into() };
257        apply(log, parent, op, t).unwrap().op_id
258    }
259
260    fn modify_body(log: &OpLog, parent: &OpId, sig: &str, from: &str, to: &str) -> OpId {
261        let op = Operation::new(
262            OperationKind::ModifyBody {
263                sig_id: sig.into(),
264                from_stage_id: from.into(),
265                to_stage_id: to.into(),
266                from_budget: None,
267                to_budget: None,
268            },
269            [parent.clone()],
270        );
271        let t = StageTransition::Replace {
272            sig_id: sig.into(), from: from.into(), to: to.into(),
273        };
274        apply(log, Some(parent), op, t).unwrap().op_id
275    }
276
277    #[test]
278    fn disjoint_sigs_merge_cleanly() {
279        let (log, _tmp) = fresh();
280        let root = add_fn(&log, None, "shared", "s0");
281        let s_only = add_fn(&log, Some(&root), "src-only", "src1");
282        let d_only = add_fn(&log, Some(&root), "dst-only", "dst1");
283
284        let out = merge(&log, Some(&s_only), Some(&d_only)).unwrap();
285        assert_eq!(out.lca.as_ref(), Some(&root));
286        let kinds: Vec<&str> = out.outcomes.iter().map(|o| match o {
287            MergeOutcome::Src { .. } => "src",
288            MergeOutcome::Dst { .. } => "dst",
289            MergeOutcome::Both { .. } => "both",
290            MergeOutcome::Conflict { .. } => "conflict",
291        }).collect();
292        assert!(kinds.contains(&"src") && kinds.contains(&"dst"));
293        assert!(!kinds.contains(&"conflict"));
294    }
295
296    #[test]
297    fn same_sig_divergent_is_modify_modify_conflict() {
298        let (log, _tmp) = fresh();
299        let root = add_fn(&log, None, "fac", "s0");
300        let src  = modify_body(&log, &root, "fac", "s0", "s-src");
301        let dst  = modify_body(&log, &root, "fac", "s0", "s-dst");
302
303        let out = merge(&log, Some(&src), Some(&dst)).unwrap();
304        let conflict = out.outcomes.iter().find(|o| matches!(o, MergeOutcome::Conflict { .. }));
305        assert!(conflict.is_some());
306        if let Some(MergeOutcome::Conflict { kind, .. }) = conflict {
307            assert!(matches!(kind, ConflictKind::ModifyModify));
308        }
309    }
310
311    #[test]
312    fn independent_histories_no_lca() {
313        let (log, _tmp) = fresh();
314        let a = add_fn(&log, None, "a", "sa");
315        let b = add_fn(&log, None, "b", "sb");
316        let out = merge(&log, Some(&a), Some(&b)).unwrap();
317        assert!(out.lca.is_none());
318    }
319
320    #[test]
321    fn rename_on_src_with_concurrent_modify_on_dst_conflicts() {
322        // src renames fac → fac2 (same body). dst modifies fac's body.
323        // The merge must surface a conflict on `fac` (modify-delete from
324        // dst's perspective: dst modified, src "removed" via rename),
325        // not silently report disjoint outcomes that lose dst's change.
326        let (log, _tmp) = fresh();
327        let root = add_fn(&log, None, "fac", "s0");
328
329        // src: rename fac → fac2.
330        let rename_op = Operation::new(
331            OperationKind::RenameSymbol {
332                from: "fac".into(),
333                to: "fac2".into(),
334                body_stage_id: "s0".into(),
335            },
336            [root.clone()],
337        );
338        let rename_t = StageTransition::Rename {
339            from: "fac".into(), to: "fac2".into(),
340            body_stage_id: "s0".into(),
341        };
342        let src = apply(&log, Some(&root), rename_op, rename_t).unwrap().op_id;
343
344        // dst: modify fac body.
345        let dst = modify_body(&log, &root, "fac", "s0", "s-dst");
346
347        let out = merge(&log, Some(&src), Some(&dst)).unwrap();
348
349        // The `fac` sig should produce a conflict because both sides
350        // touched it (src via rename's `from`, dst via modify).
351        let fac_outcome = out.outcomes.iter().find(|o| match o {
352            MergeOutcome::Conflict { sig_id, .. }
353            | MergeOutcome::Src { sig_id, .. }
354            | MergeOutcome::Dst { sig_id, .. }
355            | MergeOutcome::Both { sig_id, .. } => sig_id == "fac",
356        });
357        assert!(matches!(fac_outcome, Some(MergeOutcome::Conflict { .. })),
358            "expected `fac` to be a conflict, got {fac_outcome:?}");
359    }
360}