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, .. } => vec![sig_id.clone()],
140        // A rename touches both sides — concurrent modifies on `from`
141        // must surface as a conflict, not as a disjoint set.
142        OperationKind::RenameSymbol { from, to, .. } => vec![from.clone(), to.clone()],
143        OperationKind::AddImport { .. }
144        | OperationKind::RemoveImport { .. }
145        | OperationKind::Merge { .. } => Vec::new(),
146    }
147}
148
149/// Given a chronological (oldest-first) list of ops on a sig, return
150/// the resulting stage_id (`None` if the sig was removed).
151///
152/// The `sig` parameter is used to distinguish the `from` and `to`
153/// sides of a `RenameSymbol` operation: from the `from` sig's
154/// perspective the rename removes it; from the `to` sig's perspective
155/// it produces `body_stage_id`.
156fn latest_stage(sig: &SigId, recs: &[&OperationRecord]) -> Option<StageId> {
157    use crate::operation::{OperationKind as OK, StageTransition::*};
158    let mut current: Option<StageId> = None;
159    for r in recs {
160        // For renames: distinguish which side of the rename we're on.
161        if let OK::RenameSymbol { from, to, body_stage_id } = &r.op.kind {
162            if sig == from {
163                // From this sig's perspective, the rename removed it.
164                current = None;
165            } else if sig == to {
166                current = Some(body_stage_id.clone());
167            }
168            continue;
169        }
170        match &r.produces {
171            Create { stage_id, .. } => current = Some(stage_id.clone()),
172            Replace { to, .. } => current = Some(to.clone()),
173            Remove { .. } => current = None,
174            Rename { body_stage_id, .. } => current = Some(body_stage_id.clone()),
175            ImportOnly | Merge { .. } => {}
176        }
177    }
178    current
179}
180
181fn head_at(op_log: &OpLog, head: &OpId) -> io::Result<BTreeMap<SigId, StageId>> {
182    let mut map = BTreeMap::new();
183    for r in op_log.walk_forward(head, None)? {
184        use crate::operation::StageTransition::*;
185        match &r.produces {
186            Create { sig_id, stage_id } => { map.insert(sig_id.clone(), stage_id.clone()); }
187            Replace { sig_id, to, .. } => { map.insert(sig_id.clone(), to.clone()); }
188            Remove { sig_id, .. } => { map.remove(sig_id); }
189            Rename { from, to, body_stage_id } => {
190                map.remove(from);
191                map.insert(to.clone(), body_stage_id.clone());
192            }
193            ImportOnly => {}
194            Merge { entries } => {
195                for (sig, stage) in entries {
196                    match stage {
197                        Some(s) => { map.insert(sig.clone(), s.clone()); }
198                        None    => { map.remove(sig); }
199                    }
200                }
201            }
202        }
203    }
204    Ok(map)
205}
206
207fn classify(
208    src: &Option<StageId>,
209    dst: &Option<StageId>,
210    base: &BTreeMap<SigId, StageId>,
211    sig: &SigId,
212) -> ConflictKind {
213    let in_base = base.contains_key(sig);
214    match (in_base, src.is_some(), dst.is_some()) {
215        (false, true, true)  => ConflictKind::AddAdd,
216        (true,  true, true)  => ConflictKind::ModifyModify,
217        (true,  true, false) => ConflictKind::ModifyDelete,
218        (true,  false, true) => ConflictKind::DeleteModify,
219        // Other combos shouldn't happen for a "both touched" group:
220        // both sides touched the sig, so at least one should have a
221        // result, and the (false, false, false) / (true, false, false)
222        // shapes are unreachable. Surface as a panic in debug builds
223        // so future invariant violations are loud, not silent.
224        other => {
225            debug_assert!(false, "classify: unreachable shape {other:?} for sig {sig}");
226            ConflictKind::ModifyModify
227        }
228    }
229}
230
231#[cfg(test)]
232mod tests {
233    use super::*;
234    use crate::apply::apply;
235    use crate::operation::{Operation, OperationKind, StageTransition};
236    use std::collections::BTreeSet;
237
238    fn fresh() -> (OpLog, tempfile::TempDir) {
239        let tmp = tempfile::tempdir().unwrap();
240        (OpLog::open(tmp.path()).unwrap(), tmp)
241    }
242
243    fn add_fn(log: &OpLog, parent: Option<&OpId>, sig: &str, stg: &str) -> OpId {
244        let op = Operation::new(
245            OperationKind::AddFunction {
246                sig_id: sig.into(),
247                stage_id: stg.into(),
248                effects: BTreeSet::new(),
249            },
250            parent.cloned().into_iter().collect::<Vec<_>>(),
251        );
252        let t = StageTransition::Create { sig_id: sig.into(), stage_id: stg.into() };
253        apply(log, parent, op, t).unwrap().op_id
254    }
255
256    fn modify_body(log: &OpLog, parent: &OpId, sig: &str, from: &str, to: &str) -> OpId {
257        let op = Operation::new(
258            OperationKind::ModifyBody {
259                sig_id: sig.into(),
260                from_stage_id: from.into(),
261                to_stage_id: to.into(),
262            },
263            [parent.clone()],
264        );
265        let t = StageTransition::Replace {
266            sig_id: sig.into(), from: from.into(), to: to.into(),
267        };
268        apply(log, Some(parent), op, t).unwrap().op_id
269    }
270
271    #[test]
272    fn disjoint_sigs_merge_cleanly() {
273        let (log, _tmp) = fresh();
274        let root = add_fn(&log, None, "shared", "s0");
275        let s_only = add_fn(&log, Some(&root), "src-only", "src1");
276        let d_only = add_fn(&log, Some(&root), "dst-only", "dst1");
277
278        let out = merge(&log, Some(&s_only), Some(&d_only)).unwrap();
279        assert_eq!(out.lca.as_ref(), Some(&root));
280        let kinds: Vec<&str> = out.outcomes.iter().map(|o| match o {
281            MergeOutcome::Src { .. } => "src",
282            MergeOutcome::Dst { .. } => "dst",
283            MergeOutcome::Both { .. } => "both",
284            MergeOutcome::Conflict { .. } => "conflict",
285        }).collect();
286        assert!(kinds.contains(&"src") && kinds.contains(&"dst"));
287        assert!(!kinds.contains(&"conflict"));
288    }
289
290    #[test]
291    fn same_sig_divergent_is_modify_modify_conflict() {
292        let (log, _tmp) = fresh();
293        let root = add_fn(&log, None, "fac", "s0");
294        let src  = modify_body(&log, &root, "fac", "s0", "s-src");
295        let dst  = modify_body(&log, &root, "fac", "s0", "s-dst");
296
297        let out = merge(&log, Some(&src), Some(&dst)).unwrap();
298        let conflict = out.outcomes.iter().find(|o| matches!(o, MergeOutcome::Conflict { .. }));
299        assert!(conflict.is_some());
300        if let Some(MergeOutcome::Conflict { kind, .. }) = conflict {
301            assert!(matches!(kind, ConflictKind::ModifyModify));
302        }
303    }
304
305    #[test]
306    fn independent_histories_no_lca() {
307        let (log, _tmp) = fresh();
308        let a = add_fn(&log, None, "a", "sa");
309        let b = add_fn(&log, None, "b", "sb");
310        let out = merge(&log, Some(&a), Some(&b)).unwrap();
311        assert!(out.lca.is_none());
312    }
313
314    #[test]
315    fn rename_on_src_with_concurrent_modify_on_dst_conflicts() {
316        // src renames fac → fac2 (same body). dst modifies fac's body.
317        // The merge must surface a conflict on `fac` (modify-delete from
318        // dst's perspective: dst modified, src "removed" via rename),
319        // not silently report disjoint outcomes that lose dst's change.
320        let (log, _tmp) = fresh();
321        let root = add_fn(&log, None, "fac", "s0");
322
323        // src: rename fac → fac2.
324        let rename_op = Operation::new(
325            OperationKind::RenameSymbol {
326                from: "fac".into(),
327                to: "fac2".into(),
328                body_stage_id: "s0".into(),
329            },
330            [root.clone()],
331        );
332        let rename_t = StageTransition::Rename {
333            from: "fac".into(), to: "fac2".into(),
334            body_stage_id: "s0".into(),
335        };
336        let src = apply(&log, Some(&root), rename_op, rename_t).unwrap().op_id;
337
338        // dst: modify fac body.
339        let dst = modify_body(&log, &root, "fac", "s0", "s-dst");
340
341        let out = merge(&log, Some(&src), Some(&dst)).unwrap();
342
343        // The `fac` sig should produce a conflict because both sides
344        // touched it (src via rename's `from`, dst via modify).
345        let fac_outcome = out.outcomes.iter().find(|o| match o {
346            MergeOutcome::Conflict { sig_id, .. }
347            | MergeOutcome::Src { sig_id, .. }
348            | MergeOutcome::Dst { sig_id, .. }
349            | MergeOutcome::Both { sig_id, .. } => sig_id == "fac",
350        });
351        assert!(matches!(fac_outcome, Some(MergeOutcome::Conflict { .. })),
352            "expected `fac` to be a conflict, got {fac_outcome:?}");
353    }
354}