From 4221df8cf4477963ca35b4e7d901599593e48a22 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Sat, 17 Oct 2026 12:18:48 +0300 Subject: [PATCH 1/4] feat(repo): merge content and mode as separate dimensions; reject file/dir collisions (P37c-i, ADR-0049) Co-Authored-By: Claude Sonnet 5.5 --- crates/repo/src/error.rs | 6 + crates/repo/src/merge.rs | 569 ++++++++++++++++++++++++++------------- 2 files changed, 384 insertions(+), 191 deletions(-) diff --git a/crates/repo/src/error.rs b/crates/repo/src/error.rs index 763f184b..2eae82e1 100644 --- a/crates/repo/src/error.rs +++ b/crates/repo/src/error.rs @@ -76,6 +76,12 @@ pub enum Error { /// full clone (or a full backfill of every prefix), not one path. #[error("{0} is not supported on a partial clone; run `sc backfill --all` to convert to a full clone first")] PartialCloneUnsupported(String), + /// A merge result would make `{0}` both a file and a directory (one side + /// has a file where the other has a directory, or a file where the merged + /// result needs a directory). Detected before any output is written + /// (ADR-0049). + #[error("merge would make {0} both a file and a directory; resolve the collision on one branch first")] + PathCollision(String), #[error("already up to date")] UpToDate, /// The named branch is private and the caller either provided no identity diff --git a/crates/repo/src/merge.rs b/crates/repo/src/merge.rs index 8b934739..3ef14574 100644 --- a/crates/repo/src/merge.rs +++ b/crates/repo/src/merge.rs @@ -213,229 +213,314 @@ pub(crate) fn three_way_files( paths.extend(ours_f.keys().cloned()); paths.extend(theirs_f.keys().cloned()); - let mut files: Vec = Vec::new(); - let mut sidecars = Vec::new(); - let mut conflicts = Vec::new(); - let mut wrapped_carry: BTreeMap> = BTreeMap::new(); - + let mut acc = MergeAcc::default(); for path in paths { - let b = base_f.get(&path).copied(); - let o = ours_f.get(&path).copied(); - let t = theirs_f.get(&path).copied(); - - if ![b, o, t].iter().flatten().any(|e| e.2 & PROTECTED != 0) { - // ---- all-plain arm: pre-P15 logic verbatim ---- - - // Resolve by blob id first (cheap, covers unchanged/one-sided/delete). - let bo = b.map(|x| x.0); - let oo = o.map(|x| x.0); - let to = t.map(|x| x.0); - - if oo == to { - // same on both sides (including both-deleted) — take ours if present - if let Some((id, mode, _)) = o { - files.push(plain(path, mode, blob_bytes(store, id)?)); - } - continue; - } - if oo == bo { - // only theirs changed -> take theirs (present or deleted) - if let Some((id, mode, _)) = t { - files.push(plain(path, mode, blob_bytes(store, id)?)); - } - continue; - } - if to == bo { - // only ours changed -> take ours - if let Some((id, mode, _)) = o { - files.push(plain(path, mode, blob_bytes(store, id)?)); - } - continue; - } + let (b, o, t) = ( + base_f.get(&path).copied(), + ours_f.get(&path).copied(), + theirs_f.get(&path).copied(), + ); + merge_path(store, path, b, o, t, &prots, identity, &mut acc)?; + } + check_path_collisions(acc.files.iter().map(|f| f.path.as_str()))?; + Ok(acc.into()) +} - // Both sides changed differently. - match (o, t) { - (Some((oid, omode, _)), Some((tid, tmode, _))) => { - // Mode resolves like Git: executable if either side is. - let mode = if omode == FileMode::EXEC || tmode == FileMode::EXEC { - FileMode::EXEC - } else { - FileMode::FILE - }; - let ob = blob_bytes(store, oid)?; - let tb = blob_bytes(store, tid)?; - let bb = match b { - Some((bid, _, _)) => blob_bytes(store, bid)?, - None => Vec::new(), - }; - match (std::str::from_utf8(&ob), std::str::from_utf8(&tb)) { - (Ok(os), Ok(ts)) => { - // A non-UTF-8 base falls back to an empty base (rare - // encoding-change case): yields a conservative conflict, - // never corruption. - let base_text = std::str::from_utf8(&bb).unwrap_or(""); - let m = diff3::merge_lines(base_text, os, ts); - if m.conflicted { - conflicts.push(path.clone()); - } - files.push(plain(path, mode, m.text.into_bytes())); - } - _ => { - // binary conflict: keep ours, write theirs sidecar - conflicts.push(path.clone()); - sidecars.push((format!("{path}.theirs"), tb)); - files.push(plain(path, mode, ob)); - } - } - } - // delete/modify: one side deleted, the other modified -> conflict; - // keep the surviving (modified) content and mark conflicted. - (Some((oid, omode, _)), None) => { - conflicts.push(path.clone()); - files.push(plain(path, omode, blob_bytes(store, oid)?)); - } - (None, Some((tid, tmode, _))) => { - conflicts.push(path.clone()); - files.push(plain(path, tmode, blob_bytes(store, tid)?)); - } - (None, None) => unreachable!("oo==to already handled the both-absent case"), - } - continue; +/// Accumulated result of per-path resolution. +#[derive(Default)] +pub(crate) struct MergeAcc { + pub files: Vec, + pub sidecars: Vec<(String, Vec)>, + pub conflicts: Vec, + pub wrapped_carry: BTreeMap>, +} + +impl From for FileMerge { + fn from(a: MergeAcc) -> FileMerge { + FileMerge { + files: a.files, + sidecars: a.sidecars, + conflicts: a.conflicts, + wrapped_carry: a.wrapped_carry, } + } +} - // ---- protected arm ---- - // Fast paths compare (raw blob id, PROTECTED bit): the id logic of the - // plain arm transfers directly to ciphertext ids, but a PROTECTED-bit - // flip alone is also a change, so the bit joins the comparison key. Any - // resolution here carries the winner's raw bytes verbatim — ciphertext - // is never decrypted on a fast path, so no identity is needed. - let key = |e: Option<(ObjectId, FileMode, u8)>| e.map(|(id, _, p)| (id, p & PROTECTED)); - let (bk, ok, tk) = (key(b), key(o), key(t)); +/// A tree entry viewed as a merge input: blob id, mode, perms. +pub(crate) type FileEntry = (ObjectId, FileMode, u8); - let winner = if ok == tk { +/// Merge one path's three versions into `acc`. Content resolves by blob id +/// (and the `PROTECTED` bit for protected paths) exactly as before; the mode +/// of the result is merged separately by [`merge_mode`] (ADR-0049). The same +/// function serves the flatten merge and the tree-recursive engine, so the two +/// cannot disagree on a leaf. +pub(crate) fn merge_path( + store: &mut Store, + path: String, + b: Option, + o: Option, + t: Option, + prots: &[&Protection; 3], + identity: Option<&scl_crypto::SecretKey>, + acc: &mut MergeAcc, +) -> Result<()> { + let MergeAcc { + files, + sidecars, + conflicts, + wrapped_carry, + } = acc; + let prots = *prots; + let (bm, om, tm) = (b.map(|e| e.1), o.map(|e| e.1), t.map(|e| e.1)); + + if ![b, o, t].iter().flatten().any(|e| e.2 & PROTECTED != 0) { + // ---- all-plain arm: pre-P15 logic verbatim ---- + + // Resolve by blob id first (cheap, covers unchanged/one-sided/delete). + let bo = b.map(|x| x.0); + let oo = o.map(|x| x.0); + let to = t.map(|x| x.0); + + if oo == to { // same on both sides (including both-deleted) — take ours if present - Some(o) - } else if ok == bk { - // only theirs changed -> take theirs (present or cleanly deleted) - Some(t) - } else if tk == bk { + if let Some((id, _, _)) = o { + files.push(plain(path, merge_mode(bm, om, tm), blob_bytes(store, id)?)); + } + return Ok(()); + } + if oo == bo { + // only theirs changed -> take theirs (present or deleted) + if let Some((id, _, _)) = t { + files.push(plain(path, merge_mode(bm, om, tm), blob_bytes(store, id)?)); + } + return Ok(()); + } + if to == bo { // only ours changed -> take ours - Some(o) - } else { - None - }; - if let Some(w) = winner { - if let Some((id, mode, perms)) = w { - if perms & PROTECTED != 0 { - // Unioning wraps across ours/theirs/base means a wrap - // revoked on only ONE side is resurrected for an unchanged - // blob — consistent with P11's revoke-is-metadata-only - // stance (ADR-0019): the old DEK was never secret from a - // past recipient anyway; a cryptographic cutover requires - // `secret rotate` / re-wrap, not a merge. - let wraps = known_wraps(&prots, &id); - if !wraps.is_empty() { - wrapped_carry.insert(id, wraps); - } - // An empty union (no input protection knows this blob) - // gets no wrapped_carry entry, not an error: the input was - // already undecryptable on every side — the merge - // preserves that state, it never worsens it. - } - files.push(MergedFile { - path, - mode, - bytes: blob_bytes(store, id)?, - perms, - needs_encrypt: false, - }); + if let Some((id, _, _)) = o { + files.push(plain(path, merge_mode(bm, om, tm), blob_bytes(store, id)?)); } - continue; + return Ok(()); } - // Content-divergent (both differ from base and each other; delete-vs- - // modify; or the PROTECTED bit itself differs between sides): protected - // inputs must be decrypted, which requires an identity — enforced - // inside `plain_input`, so a case that only touches plain inputs (e.g. - // delete-vs-modify with a plain survivor) still resolves without one. + // Both sides changed differently. match (o, t) { - (Some(oe), Some(te)) => { - // Mode resolves like Git: executable if either side is. - let mode = if oe.1 == FileMode::EXEC || te.1 == FileMode::EXEC { - FileMode::EXEC - } else { - FileMode::FILE - }; - // The PROTECTED bit resolves like the exec bit: protected if - // either side is. (A protected base with two plain sides means - // the rule was removed on both — the output stays plain.) - let perms = (oe.2 | te.2) & PROTECTED; - let needs_encrypt = perms != 0; - // Decrypted inputs stay in `Zeroizing` until the final output - // copy; `bb` never leaves it — the base plaintext is only read - // for diff3 and is wiped when it drops. - let ob = plain_input(store, oe, &prots, identity, &path)?; - let tb = plain_input(store, te, &prots, identity, &path)?; + (Some((oid, _, _)), Some((tid, _, _))) => { + // Mode is merged separately from content (ADR-0049). + let mode = merge_mode(bm, om, tm); + let ob = blob_bytes(store, oid)?; + let tb = blob_bytes(store, tid)?; let bb = match b { - Some(be) => plain_input(store, be, &prots, identity, &path)?, - None => scl_crypto::Zeroizing::new(Vec::new()), + Some((bid, _, _)) => blob_bytes(store, bid)?, + None => Vec::new(), }; match (std::str::from_utf8(&ob), std::str::from_utf8(&tb)) { (Ok(os), Ok(ts)) => { - // Same non-UTF-8-base fallback as the plain arm. + // A non-UTF-8 base falls back to an empty base (rare + // encoding-change case): yields a conservative conflict, + // never corruption. let base_text = std::str::from_utf8(&bb).unwrap_or(""); let m = diff3::merge_lines(base_text, os, ts); if m.conflicted { conflicts.push(path.clone()); } - files.push(MergedFile { - path, - mode, - bytes: m.text.into_bytes(), - perms, - needs_encrypt, - }); + files.push(plain(path, mode, m.text.into_bytes())); } _ => { - // binary conflict: keep ours, write theirs sidecar — - // both in plaintext, so the user can resolve. + // binary conflict: keep ours, write theirs sidecar conflicts.push(path.clone()); - sidecars.push((format!("{path}.theirs"), tb.to_vec())); - files.push(MergedFile { - path, - mode, - bytes: ob.to_vec(), - perms, - needs_encrypt, - }); + sidecars.push((format!("{path}.theirs"), tb)); + files.push(plain(path, mode, ob)); } } } // delete/modify: one side deleted, the other modified -> conflict; - // keep the surviving (modified) content — decrypted to plaintext - // when protected, so conflict resolution sees the real content. - (Some(oe), None) | (None, Some(oe)) => { + // keep the surviving (modified) content and mark conflicted. + (Some((oid, omode, _)), None) => { + conflicts.push(path.clone()); + files.push(plain(path, omode, blob_bytes(store, oid)?)); + } + (None, Some((tid, tmode, _))) => { conflicts.push(path.clone()); - let perms = oe.2 & PROTECTED; - let bytes = plain_input(store, oe, &prots, identity, &path)?; - files.push(MergedFile { - path, - mode: oe.1, - bytes: bytes.to_vec(), - perms, - needs_encrypt: perms != 0, - }); + files.push(plain(path, tmode, blob_bytes(store, tid)?)); } - (None, None) => unreachable!("ok==tk already handled the both-absent case"), + (None, None) => unreachable!("oo==to already handled the both-absent case"), } + return Ok(()); } - Ok(FileMerge { - files, - sidecars, - conflicts, - wrapped_carry, - }) + // ---- protected arm ---- + // Fast paths compare (raw blob id, PROTECTED bit): the id logic of the + // plain arm transfers directly to ciphertext ids, but a PROTECTED-bit + // flip alone is also a change, so the bit joins the comparison key. Any + // resolution here carries the winner's raw bytes verbatim — ciphertext + // is never decrypted on a fast path, so no identity is needed. + let key = |e: Option<(ObjectId, FileMode, u8)>| e.map(|(id, _, p)| (id, p & PROTECTED)); + let (bk, ok, tk) = (key(b), key(o), key(t)); + + let winner = if ok == tk { + // same on both sides (including both-deleted) — take ours if present + Some(o) + } else if ok == bk { + // only theirs changed -> take theirs (present or cleanly deleted) + Some(t) + } else if tk == bk { + // only ours changed -> take ours + Some(o) + } else { + None + }; + if let Some(w) = winner { + if let Some((id, _, perms)) = w { + let mode = merge_mode(bm, om, tm); + if perms & PROTECTED != 0 { + // Unioning wraps across ours/theirs/base means a wrap + // revoked on only ONE side is resurrected for an unchanged + // blob — consistent with P11's revoke-is-metadata-only + // stance (ADR-0019): the old DEK was never secret from a + // past recipient anyway; a cryptographic cutover requires + // `secret rotate` / re-wrap, not a merge. + let wraps = known_wraps(&prots, &id); + if !wraps.is_empty() { + wrapped_carry.insert(id, wraps); + } + // An empty union (no input protection knows this blob) + // gets no wrapped_carry entry, not an error: the input was + // already undecryptable on every side — the merge + // preserves that state, it never worsens it. + } + files.push(MergedFile { + path, + mode, + bytes: blob_bytes(store, id)?, + perms, + needs_encrypt: false, + }); + } + return Ok(()); + } + + // Content-divergent (both differ from base and each other; delete-vs- + // modify; or the PROTECTED bit itself differs between sides): protected + // inputs must be decrypted, which requires an identity — enforced + // inside `plain_input`, so a case that only touches plain inputs (e.g. + // delete-vs-modify with a plain survivor) still resolves without one. + match (o, t) { + (Some(oe), Some(te)) => { + // Mode is merged separately from content (ADR-0049). + let mode = merge_mode(bm, om, tm); + // The PROTECTED bit resolves like the exec bit: protected if + // either side is. (A protected base with two plain sides means + // the rule was removed on both — the output stays plain.) + let perms = (oe.2 | te.2) & PROTECTED; + let needs_encrypt = perms != 0; + // Decrypted inputs stay in `Zeroizing` until the final output + // copy; `bb` never leaves it — the base plaintext is only read + // for diff3 and is wiped when it drops. + let ob = plain_input(store, oe, &prots, identity, &path)?; + let tb = plain_input(store, te, &prots, identity, &path)?; + let bb = match b { + Some(be) => plain_input(store, be, &prots, identity, &path)?, + None => scl_crypto::Zeroizing::new(Vec::new()), + }; + match (std::str::from_utf8(&ob), std::str::from_utf8(&tb)) { + (Ok(os), Ok(ts)) => { + // Same non-UTF-8-base fallback as the plain arm. + let base_text = std::str::from_utf8(&bb).unwrap_or(""); + let m = diff3::merge_lines(base_text, os, ts); + if m.conflicted { + conflicts.push(path.clone()); + } + files.push(MergedFile { + path, + mode, + bytes: m.text.into_bytes(), + perms, + needs_encrypt, + }); + } + _ => { + // binary conflict: keep ours, write theirs sidecar — + // both in plaintext, so the user can resolve. + conflicts.push(path.clone()); + sidecars.push((format!("{path}.theirs"), tb.to_vec())); + files.push(MergedFile { + path, + mode, + bytes: ob.to_vec(), + perms, + needs_encrypt, + }); + } + } + } + // delete/modify: one side deleted, the other modified -> conflict; + // keep the surviving (modified) content — decrypted to plaintext + // when protected, so conflict resolution sees the real content. + (Some(oe), None) | (None, Some(oe)) => { + conflicts.push(path.clone()); + let perms = oe.2 & PROTECTED; + let bytes = plain_input(store, oe, &prots, identity, &path)?; + files.push(MergedFile { + path, + mode: oe.1, + bytes: bytes.to_vec(), + perms, + needs_encrypt: perms != 0, + }); + } + (None, None) => unreachable!("ok==tk already handled the both-absent case"), + } + Ok(()) +} + +/// Mode of a merged entry, merged as its own dimension (ADR-0049): content +/// resolution keeps deciding which blob survives, and this decides the mode. +/// Only entries that exist vote: a lone survivor (delete/modify, one-sided +/// add) keeps its own mode; with two entries, equal modes win, a side equal to +/// base yields to the side that changed, and when both changed differently +/// (or both were added differently) executable wins, as it always has. +pub(crate) fn merge_mode( + b: Option, + o: Option, + t: Option, +) -> FileMode { + match (o, t) { + (Some(om), Some(tm)) => { + if om == tm { + om + } else if Some(om) == b { + tm + } else if Some(tm) == b { + om + } else if om == FileMode::EXEC || tm == FileMode::EXEC { + FileMode::EXEC + } else { + FileMode::FILE + } + } + (Some(m), None) | (None, Some(m)) => m, + (None, None) => FileMode::FILE, + } +} + +/// Reject a merge whose resolved paths make a name both a file and a +/// directory (`x` and `x/child`): the tree writer would emit a blob and a tree +/// with the same name and materialization would fail after changing files. +/// Checked before any output is written (ADR-0049). +pub(crate) fn check_path_collisions<'a>(paths: impl Iterator) -> Result<()> { + let set: BTreeSet<&str> = paths.collect(); + for p in &set { + let mut idx = 0; + while let Some(i) = p[idx..].find('/') { + let ancestor = &p[..idx + i]; + if set.contains(ancestor) { + return Err(Error::PathCollision(ancestor.to_string())); + } + idx += i + 1; + } + } + Ok(()) } /// Shorthand for an unprotected [`MergedFile`]. @@ -1462,4 +1547,106 @@ mod tests { assert!(m.files.iter().all(|f| !f.needs_encrypt && f.perms == 0)); assert!(m.sidecars.is_empty()); } + + // ---- ADR-0049: content and mode merge as separate dimensions ---------- + + fn mode_root(repo: &VfsRepo, files: &[(&str, &[u8], FileMode)]) -> ObjectId { + let v: Vec<(String, Vec, FileMode)> = files + .iter() + .map(|(p, b, m)| (p.to_string(), b.to_vec(), *m)) + .collect(); + repo.write_tree(&v).unwrap() + } + + fn merged_mode(fm: &FileMerge, path: &str) -> FileMode { + fm.files.iter().find(|f| f.path == path).unwrap().mode + } + + #[test] + fn mode_only_change_on_one_side_merges() { + let repo = VfsRepo::new(Store::with_budget(1 << 20)); + let base = mode_root(&repo, &[("run.sh", b"#!/bin/sh\n", FileMode::FILE)]); + let theirs = mode_root(&repo, &[("run.sh", b"#!/bin/sh\n", FileMode::EXEC)]); + let p = Protection::default(); + let arc = repo.store(); + let mut s = arc.lock().unwrap(); + // Theirs made it executable; ours left it alone: the chmod is kept. + let fm = three_way_files(&mut s, Some((base, &p)), (base, &p), (theirs, &p), None).unwrap(); + assert!(fm.conflicts.is_empty()); + assert_eq!(merged_mode(&fm, "run.sh"), FileMode::EXEC); + // And symmetrically when ours made the change. + let fm = three_way_files(&mut s, Some((base, &p)), (theirs, &p), (base, &p), None).unwrap(); + assert_eq!(merged_mode(&fm, "run.sh"), FileMode::EXEC); + } + + #[test] + fn removing_the_executable_bit_survives_a_content_change_on_the_other_side() { + let repo = VfsRepo::new(Store::with_budget(1 << 20)); + let base = mode_root(&repo, &[("run.sh", b"one\n", FileMode::EXEC)]); + let ours = mode_root(&repo, &[("run.sh", b"one\n", FileMode::FILE)]); + let theirs = mode_root(&repo, &[("run.sh", b"two\n", FileMode::EXEC)]); + let p = Protection::default(); + let arc = repo.store(); + let mut s = arc.lock().unwrap(); + let fm = three_way_files(&mut s, Some((base, &p)), (ours, &p), (theirs, &p), None).unwrap(); + assert!(fm.conflicts.is_empty()); + let f = fm.files.iter().find(|f| f.path == "run.sh").unwrap(); + assert_eq!(f.bytes, b"two\n", "theirs' content wins"); + assert_eq!(f.mode, FileMode::FILE, "ours' executable removal is kept"); + } + + #[test] + fn identical_binary_content_with_different_modes_is_not_a_conflict() { + let repo = VfsRepo::new(Store::with_budget(1 << 20)); + let bin_a: &[u8] = &[0xff, 0xfe, 0x00, 0x01]; + let bin_b: &[u8] = &[0xff, 0xfe, 0x00, 0x02]; + let base = mode_root(&repo, &[("x.bin", bin_a, FileMode::FILE)]); + let ours = mode_root(&repo, &[("x.bin", bin_b, FileMode::FILE)]); + let theirs = mode_root(&repo, &[("x.bin", bin_b, FileMode::EXEC)]); + let p = Protection::default(); + let arc = repo.store(); + let mut s = arc.lock().unwrap(); + let fm = three_way_files(&mut s, Some((base, &p)), (ours, &p), (theirs, &p), None).unwrap(); + assert!( + fm.conflicts.is_empty(), + "same bytes on both sides: no conflict" + ); + assert!(fm.sidecars.is_empty()); + assert_eq!(merged_mode(&fm, "x.bin"), FileMode::EXEC); + } + + #[test] + fn delete_modify_keeps_the_survivors_mode() { + let repo = VfsRepo::new(Store::with_budget(1 << 20)); + let base = mode_root(&repo, &[("f", b"one\n", FileMode::FILE)]); + let theirs = mode_root(&repo, &[("f", b"two\n", FileMode::FILE)]); + let ours = mode_root(&repo, &[("other", b"x\n", FileMode::FILE)]); + let p = Protection::default(); + let arc = repo.store(); + let mut s = arc.lock().unwrap(); + let fm = three_way_files(&mut s, Some((base, &p)), (ours, &p), (theirs, &p), None).unwrap(); + assert_eq!(fm.conflicts, vec!["f"]); + assert_eq!(merged_mode(&fm, "f"), FileMode::FILE); + } + + #[test] + fn a_file_and_a_directory_with_the_same_name_is_rejected_before_any_output() { + let repo = VfsRepo::new(Store::with_budget(1 << 20)); + let ours = mode_root(&repo, &[("x", b"file\n", FileMode::FILE)]); + let theirs = mode_root(&repo, &[("x/child", b"nested\n", FileMode::FILE)]); + let p = Protection::default(); + let arc = repo.store(); + let mut s = arc.lock().unwrap(); + let err = three_way_files(&mut s, None, (ours, &p), (theirs, &p), None).unwrap_err(); + assert!( + matches!(&err, Error::PathCollision(x) if x == "x"), + "got {err:?}" + ); + } + + #[test] + fn path_collision_check_finds_nested_ancestors_and_ignores_siblings() { + assert!(check_path_collisions(["a/b/c", "a/b", "z"].into_iter()).is_err()); + assert!(check_path_collisions(["a", "a-b", "a.b/c", "ab/c"].into_iter()).is_ok()); + } } From d12bda5f4adac3e3ddf23ea3c43419890f443c09 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Sat, 17 Oct 2026 12:27:40 +0300 Subject: [PATCH 2/4] feat(repo): tree-recursive merge engine with differential tests (P37c-i, ADR-0049) merge_trees carries a subtree by id only where the sides agree and no protection rule intersects it (or it is identical on all three), reuses the flatten merge's per-path function for leaves, and is pull-based: it returns Needs((id, path)) for objects the store lacks. Nothing calls it yet outside its tests: a 4000-seed differential against the flatten merge (including runs on stores with objects removed and refilled on demand, and a check that no object under a carried subtree is ever requested), plus named cases from the ADR. ADR-0012 gets an amendment for the mode and collision fixes. Co-Authored-By: Claude Sonnet 5.5 --- ROADMAP.md | 2 +- crates/repo/src/lib.rs | 4 + crates/repo/src/merge_tree.rs | 963 +++++++++++++++++++++++ docs/adr/0012-three-way-merge.md | 18 + docs/adr/0049-merge-on-partial-clones.md | 2 +- docs/adr/README.md | 2 +- 6 files changed, 988 insertions(+), 3 deletions(-) create mode 100644 crates/repo/src/merge_tree.rs diff --git a/ROADMAP.md b/ROADMAP.md index 3c368f6f..89ddfab4 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -558,7 +558,7 @@ clone: the view closure is filled by id before switch, fast-forward, undo and `sparse disable` stay refused. Design and open questions in [ADR-0048](docs/adr/0048-presence-based-partial-clones.md). -**P37c — merge on partial clones (proposed).** A tree-recursive three-way merge +**P37c — merge on partial clones (proposed; 37c-i engine built, not yet wired).** A tree-recursive three-way merge that carries a subtree by id where the three sides agree, checks protection rules before carrying, never drops wraps for subtrees it does not enumerate, and fills (lazy) or refuses (otherwise) the region where the sides differ, diff --git a/crates/repo/src/lib.rs b/crates/repo/src/lib.rs index f39c816d..47bfe770 100644 --- a/crates/repo/src/lib.rs +++ b/crates/repo/src/lib.rs @@ -14,7 +14,11 @@ pub mod ignore; pub mod layout; pub mod lock; pub mod merge; +// Wired into `merge` on partial clones in P37c-ii (ADR-0049); until then only +// its differential tests call it. pub mod merge_state; +#[cfg_attr(not(test), allow(dead_code))] +pub(crate) mod merge_tree; pub mod oplog; #[cfg(test)] mod partial_walks; diff --git a/crates/repo/src/merge_tree.rs b/crates/repo/src/merge_tree.rs new file mode 100644 index 00000000..33b87af1 --- /dev/null +++ b/crates/repo/src/merge_tree.rs @@ -0,0 +1,963 @@ +//! Tree-recursive three-way merge (ADR-0049, P37c). +//! +//! The flatten merge ([`crate::merge::three_way_files`]) reads every file of +//! all three trees. On a partial clone most of those objects are absent, and +//! most of them do not matter: where the three sides agree on a subtree by id, +//! the answer is known without reading below it. [`merge_trees`] merges one +//! directory level at a time and *carries* such a subtree by id, unread. +//! +//! A subtree is carried only when that is provably the same answer the flatten +//! merge gives: +//! +//! - it is identical on all three sides, or +//! - the sides do not all differ (`ours == theirs`, `base == theirs` or +//! `base == ours`, by tree id) **and** it is policy-disjoint: no protection +//! rule covers it or lies inside it. A side can already hold plaintext under +//! its own rule (`protect` records the rule before it encrypts), and the full +//! merge re-encrypts every plain survivor under a union rule, so a subtree +//! that intersects a rule is read, not carried. +//! +//! Everything else recurses, and at the leaves the per-path rules are the very +//! same function the flatten merge uses ([`crate::merge::merge_path`]), so the +//! two engines cannot disagree on a leaf. +//! +//! The engine is pull-based. It never fetches: when it must read an object the +//! store lacks it returns [`TreeMerge::Needs`] with the `(id, path)` pairs, the +//! caller fills them (outside the store lock) and calls again. + +use std::collections::{BTreeMap, BTreeSet}; + +use scl_core::{EntryKind, FileMode, ObjectId, Protection, Store, TreeEntry}; + +use crate::error::{Error, Result}; +use crate::merge::{check_path_collisions, merge_path, FileEntry, FileMerge, MergeAcc}; + +/// What the merge carried by id instead of reading. +#[derive(Debug, PartialEq, Eq)] +pub(crate) enum Carried { + /// The whole merged root is this tree, as is (the three roots agreed). + Root(ObjectId), + /// Subtrees to overlay by id onto the tree built from the enumerated + /// files, as `(path, entry)` where the entry's name is the last component. + Entries(Vec<(String, TreeEntry)>), +} + +/// A finished tree merge. +#[derive(Debug)] +pub(crate) struct TreeMergeDone { + /// The resolved files of every region the merge read. + pub merge: FileMerge, + /// The subtrees it did not read. + pub carried: Carried, +} + +/// The outcome of one [`merge_trees`] pass. +#[derive(Debug)] +pub(crate) enum TreeMerge { + /// The pass needs these objects (subtrees or blobs the store lacks), each + /// with the path at which the traversal found it. Fill them and call again. + Needs(Vec<(ObjectId, String)>), + Done(TreeMergeDone), +} + +struct Walk<'a> { + prots: [&'a Protection; 3], + rules: Vec, + identity: Option<&'a scl_crypto::SecretKey>, + acc: MergeAcc, + carried: Vec<(String, TreeEntry)>, + carried_root: Option, + needs: Vec<(ObjectId, String)>, + needs_seen: BTreeSet, +} + +impl Walk<'_> { + fn need(&mut self, id: ObjectId, path: &str) { + if self.needs_seen.insert(id) { + self.needs.push((id, path.to_string())); + } + } + + /// Does any rule cover `path` or lie inside it? Checked in both + /// directions at `/` boundaries. Every rule counts, including rules present + /// on only one side and rules whose recipients have all been revoked + /// (tombstones gate future seals; they do not remove the prefix). + fn intersects_rule(&self, path: &str) -> bool { + self.rules.iter().any(|bare| { + path.is_empty() + || path == bare + || path.starts_with(&format!("{bare}/")) + || bare.starts_with(&format!("{path}/")) + }) + } +} + +fn join(path: &str, name: &str) -> String { + if path.is_empty() { + name.to_string() + } else { + format!("{path}/{name}") + } +} + +fn file_entry(e: &TreeEntry) -> FileEntry { + (e.id, e.mode, e.perms) +} + +/// Merge `base`/`ours`/`theirs` (root tree ids with their snapshots' +/// protection policies; `base: None` is the empty base) one directory level at +/// a time, carrying subtrees by id where that is provably safe. See the module +/// docs. On a full store this never returns [`TreeMerge::Needs`]. +pub(crate) fn merge_trees( + store: &mut Store, + base: Option<(ObjectId, &Protection)>, + ours: (ObjectId, &Protection), + theirs: (ObjectId, &Protection), + identity: Option<&scl_crypto::SecretKey>, +) -> Result { + let default_prot = Protection::default(); + let (base_root, base_prot) = match base { + Some((r, p)) => (Some(r), p), + None => (None, &default_prot), + }; + let prots = [ours.1, theirs.1, base_prot]; + let mut rules: BTreeSet = BTreeSet::new(); + for p in prots { + for r in &p.prefixes { + rules.insert(r.prefix.trim_end_matches('/').to_string()); + } + } + let mut w = Walk { + prots, + rules: rules.into_iter().collect(), + identity, + acc: MergeAcc::default(), + carried: Vec::new(), + carried_root: None, + needs: Vec::new(), + needs_seen: BTreeSet::new(), + }; + let root_entry = |id: ObjectId| TreeEntry { + name: String::new(), + kind: EntryKind::Tree, + id, + mode: FileMode::FILE, + perms: 0, + }; + tree_node( + store, + &mut w, + "", + [ + base_root.map(root_entry), + Some(root_entry(ours.0)), + Some(root_entry(theirs.0)), + ], + )?; + if !w.needs.is_empty() { + return Ok(TreeMerge::Needs(w.needs)); + } + let carried = match w.carried_root { + Some(id) => Carried::Root(id), + None => Carried::Entries(w.carried), + }; + // Reject `x` as both file and directory before anything is written. The + // carried entries count as directories at their paths. + check_path_collisions( + w.acc + .files + .iter() + .map(|f| f.path.as_str()) + .chain(match &carried { + Carried::Entries(v) => v.iter().map(|(p, _)| p.as_str()).collect::>(), + Carried::Root(_) => Vec::new(), + }), + )?; + Ok(TreeMerge::Done(TreeMergeDone { + merge: w.acc.into(), + carried, + })) +} + +/// Visit one name at `path` with its (up to three) entries. +fn visit(store: &mut Store, w: &mut Walk, path: &str, ents: [Option; 3]) -> Result<()> { + let present: Vec<&TreeEntry> = ents.iter().flatten().collect(); + if present.iter().all(|e| e.kind == EntryKind::Blob) { + let [b, o, t] = &ents; + match merge_path( + store, + path.to_string(), + b.as_ref().map(file_entry), + o.as_ref().map(file_entry), + t.as_ref().map(file_entry), + &w.prots, + w.identity, + &mut w.acc, + ) { + Ok(()) => Ok(()), + // A blob the leaf must read is absent: ask for it. + Err(Error::Core(scl_core::Error::NotFound(id))) => { + w.need(id, path); + Ok(()) + } + Err(e) => Err(e), + } + } else if present.iter().all(|e| e.kind == EntryKind::Tree) { + tree_node(store, w, path, ents) + } else { + enumerate_mixed(store, w, path, ents) + } +} + +/// All present entries at `path` are trees. +fn tree_node( + store: &mut Store, + w: &mut Walk, + path: &str, + ents: [Option; 3], +) -> Result<()> { + let id = |e: &Option| e.as_ref().map(|x| x.id); + let (bi, oi, ti) = (id(&ents[0]), id(&ents[1]), id(&ents[2])); + + // Which entry would be carried, and is carrying it safe? + let carry_from: Option<&Option> = if oi == ti || bi == ti { + // ours == theirs, or only ours changed: ours is the answer. + Some(&ents[1]) + } else if bi == oi { + // only theirs changed. + Some(&ents[2]) + } else { + None + }; + if let Some(src) = carry_from { + match src { + // The surviving result is a deletion: nothing to carry or read. + None => return Ok(()), + Some(entry) => { + let identical = bi == oi && oi == ti; + if identical || !w.intersects_rule(path) { + carry(w, path, entry); + return Ok(()); + } + } + } + } + + // Recurse: read each present node (asking for the ones we lack) and visit + // the union of their names. + let mut trees: [Option; 3] = [None, None, None]; + let mut missing = false; + for e in ents.iter().flatten() { + if !store.contains(&e.id) { + w.need(e.id, path); + missing = true; + } + } + if missing { + return Ok(()); + } + for (i, e) in ents.iter().enumerate() { + if let Some(e) = e { + trees[i] = Some(store.get_tree(&e.id)?); + } + } + let names: BTreeSet = trees + .iter() + .flatten() + .flat_map(|t| t.entries.iter().map(|e| e.name.clone())) + .collect(); + for name in names { + let child = |t: &Option| t.as_ref().and_then(|t| t.get(&name).cloned()); + let kids = [child(&trees[0]), child(&trees[1]), child(&trees[2])]; + visit(store, w, &join(path, &name), kids)?; + } + Ok(()) +} + +fn carry(w: &mut Walk, path: &str, entry: &TreeEntry) { + if path.is_empty() { + w.carried_root = Some(entry.id); + } else { + let mut e = entry.clone(); + e.name = path.rsplit('/').next().unwrap_or(path).to_string(); + w.carried.push((path.to_string(), e)); + } +} + +/// A name is a blob on some sides and a tree on others. Flatten every side +/// that has a tree under this name (asking for what is absent), add the blob +/// sides as the path itself, and resolve per path with the shared leaf rules. +fn enumerate_mixed( + store: &mut Store, + w: &mut Walk, + path: &str, + ents: [Option; 3], +) -> Result<()> { + let mut maps: [BTreeMap; 3] = Default::default(); + for (i, e) in ents.iter().enumerate() { + let Some(e) = e else { continue }; + match e.kind { + EntryKind::Blob => { + maps[i].insert(path.to_string(), file_entry(e)); + } + EntryKind::Tree => flatten_present(store, w, e.id, path, &mut maps[i])?, + } + } + let paths: BTreeSet = maps.iter().flat_map(|m| m.keys().cloned()).collect(); + for p in paths { + let [b, o, t] = [&maps[0], &maps[1], &maps[2]].map(|m| m.get(&p).copied()); + match merge_path(store, p.clone(), b, o, t, &w.prots, w.identity, &mut w.acc) { + Ok(()) => {} + Err(Error::Core(scl_core::Error::NotFound(id))) => w.need(id, &p), + Err(e) => return Err(e), + } + } + Ok(()) +} + +/// Flatten a subtree to leaf entries, asking for any tree the store lacks. +fn flatten_present( + store: &mut Store, + w: &mut Walk, + root: ObjectId, + prefix: &str, + out: &mut BTreeMap, +) -> Result<()> { + let mut stack = vec![(root, prefix.to_string())]; + while let Some((id, p)) = stack.pop() { + if !store.contains(&id) { + w.need(id, &p); + continue; + } + let tree = store.get_tree(&id)?; + for e in tree.entries { + let child = join(&p, &e.name); + match e.kind { + EntryKind::Blob => { + out.insert(child, file_entry(&e)); + } + EntryKind::Tree => stack.push((e.id, child)), + } + } + } + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::merge::three_way_files; + use scl_core::{ProtectPrefix, PROTECTED}; + use scl_vfs::Repo as VfsRepo; + use std::collections::HashSet; + + // ---- a tiny deterministic generator (no external RNG dependency) ------- + + struct Rng(u64); + impl Rng { + fn next(&mut self) -> u64 { + // xorshift64* + self.0 ^= self.0 >> 12; + self.0 ^= self.0 << 25; + self.0 ^= self.0 >> 27; + self.0.wrapping_mul(0x2545F4914F6CDD1D) + } + fn below(&mut self, n: u64) -> u64 { + self.next() % n + } + fn chance(&mut self, num: u64, den: u64) -> bool { + self.below(den) < num + } + } + + #[derive(Clone, PartialEq, Eq, Debug)] + struct Leaf { + content: u8, + exec: bool, + prot: bool, + } + type Files = BTreeMap; + + fn leaf_bytes(l: &Leaf) -> Vec { + let k = l.content; + match (l.prot, k >= 4) { + (true, _) => format!("cipher{k}").into_bytes(), + (false, false) => format!("text line {k}\n").into_bytes(), + (false, true) => vec![0xff, 0xfe, k], + } + } + + fn rand_path(rng: &mut Rng) -> String { + const NAMES: [&str; 4] = ["a", "b", "c", "f"]; + let depth = 1 + rng.below(3) as usize; + (0..depth) + .map(|_| NAMES[rng.below(4) as usize]) + .collect::>() + .join("/") + } + + fn collides(files: &Files, path: &str) -> bool { + files.keys().any(|k| { + k == path || k.starts_with(&format!("{path}/")) || path.starts_with(&format!("{k}/")) + }) + } + + fn rand_leaf(rng: &mut Rng) -> Leaf { + Leaf { + content: rng.below(6) as u8, + exec: rng.chance(1, 4), + prot: rng.chance(1, 4), + } + } + + fn gen_base(rng: &mut Rng) -> Files { + let mut f = Files::new(); + for _ in 0..rng.below(9) { + let p = rand_path(rng); + if !collides(&f, &p) { + f.insert(p, rand_leaf(rng)); + } + } + f + } + + /// A side derived from `base`: each file kept, deleted, edited or chmod'd, + /// plus a few additions (which may collide with the other side's names). + fn mutate(rng: &mut Rng, base: &Files, aggression: u64) -> Files { + let mut f = Files::new(); + for (p, l) in base { + if rng.chance(aggression, 10) { + match rng.below(4) { + 0 => continue, // delete + 1 => { + let mut l = l.clone(); + l.content = rng.below(6) as u8; + f.insert(p.clone(), l); + } + 2 => { + let mut l = l.clone(); + l.exec = !l.exec; + f.insert(p.clone(), l); + } + _ => { + let mut l = l.clone(); + l.prot = !l.prot; + f.insert(p.clone(), l); + } + } + } else { + f.insert(p.clone(), l.clone()); + } + } + for _ in 0..rng.below(3) { + let p = rand_path(rng); + if !collides(&f, &p) { + f.insert(p, rand_leaf(rng)); + } + } + f + } + + fn build(repo: &VfsRepo, files: &Files) -> ObjectId { + let v: Vec<(String, Vec, FileMode, u8)> = files + .iter() + .map(|(p, l)| { + ( + p.clone(), + leaf_bytes(l), + if l.exec { + FileMode::EXEC + } else { + FileMode::FILE + }, + if l.prot { PROTECTED } else { 0 }, + ) + }) + .collect(); + repo.write_tree_with_perms(&v).unwrap() + } + + fn rand_policy(rng: &mut Rng) -> Protection { + const RULES: [&str; 5] = ["a", "b/a", "c", "f", "b/c/f"]; + let mut p = Protection::default(); + for r in RULES { + if rng.chance(1, 4) { + p.prefixes.push(ProtectPrefix { + prefix: if rng.chance(1, 2) { + format!("{r}/") + } else { + r.to_string() + }, + recipients: Vec::new(), + }); + } + } + p + } + + type Flat = BTreeMap, bool)>; + + fn flat_of_files(files: &[crate::merge::MergedFile]) -> Flat { + files + .iter() + .map(|f| { + ( + f.path.clone(), + (f.mode, f.perms, f.bytes.clone(), f.needs_encrypt), + ) + }) + .collect() + } + + fn expand(store: &mut Store, done: &TreeMergeDone) -> Flat { + let mut flat = flat_of_files(&done.merge.files); + let mut add = |store: &mut Store, prefix: &str, id: ObjectId| { + for (p, (bid, mode, perms)) in + crate::worktree::tree_file_entries_with_perms(store, id).unwrap() + { + let path = if prefix.is_empty() { + p + } else { + format!("{prefix}/{p}") + }; + let bytes = match store.get(&bid).unwrap() { + scl_core::Object::Blob(b) => b.to_vec(), + _ => panic!("expected blob"), + }; + assert!( + flat.insert(path.clone(), (mode, perms, bytes, false)) + .is_none(), + "carried {path} also enumerated" + ); + } + }; + match &done.carried { + Carried::Root(id) => add(store, "", *id), + Carried::Entries(v) => { + for (path, e) in v { + add(store, path, e.id); + } + } + } + flat + } + + fn sorted(mut v: Vec) -> Vec { + v.sort(); + v + } + + fn sorted_sidecars(v: &[(String, Vec)]) -> Vec<(String, Vec)> { + let mut v = v.to_vec(); + v.sort(); + v + } + + /// Tree id at `path` under `root` (None if absent or not a tree). + fn tree_at(store: &mut Store, root: ObjectId, path: &str) -> Option { + let mut cur = root; + for comp in path.split('/').filter(|c| !c.is_empty()) { + let t = store.get_tree(&cur).ok()?; + let e = t.get(comp)?; + if e.kind != EntryKind::Tree { + return None; + } + cur = e.id; + } + Some(cur) + } + + /// Every node (tree or blob) reachable from `root`, with its path. + fn all_nodes(store: &mut Store, root: ObjectId) -> Vec<(String, ObjectId)> { + let mut out = vec![(String::new(), root)]; + let mut stack = vec![(root, String::new())]; + while let Some((id, p)) = stack.pop() { + let t = store.get_tree(&id).unwrap(); + for e in t.entries { + let child = join(&p, &e.name); + out.push((child.clone(), e.id)); + if e.kind == EntryKind::Tree { + stack.push((e.id, child)); + } + } + } + out + } + + fn carried_paths(c: &Carried) -> Vec { + match c { + Carried::Root(_) => vec![String::new()], + Carried::Entries(v) => v.iter().map(|(p, _)| p.clone()).collect(), + } + } + + fn under(path: &str, prefix: &str) -> bool { + prefix.is_empty() || path == prefix || path.starts_with(&format!("{prefix}/")) + } + + fn rule_intersects(prots: &[&Protection], path: &str) -> bool { + prots.iter().flat_map(|p| p.prefixes.iter()).any(|r| { + let bare = r.prefix.trim_end_matches('/'); + path.is_empty() + || path == bare + || path.starts_with(&format!("{bare}/")) + || bare.starts_with(&format!("{path}/")) + }) + } + + #[derive(Default, Debug)] + struct Coverage { + merged: u32, + carried_some: u32, + carried_root: u32, + conflicts: u32, + sidecars: u32, + needed_fill: u32, + refused_collision: u32, + refused_identity: u32, + } + + /// One random case: the engine (on the full store, and on a store with + /// objects removed and refilled on demand) against the flatten merge. + fn run_case(seed: u64, cov: &mut Coverage) { + let mut rng = Rng(seed.wrapping_mul(0x9E3779B97F4A7C15) | 1); + let base = gen_base(&mut rng); + let aggr = 1 + rng.below(6); + let ours = mutate(&mut rng, &base, aggr); + let theirs = mutate(&mut rng, &base, aggr); + let bp = rand_policy(&mut rng); + let op = if rng.chance(2, 3) { + bp.clone() + } else { + rand_policy(&mut rng) + }; + let tp = if rng.chance(2, 3) { + bp.clone() + } else { + rand_policy(&mut rng) + }; + + let full_repo = VfsRepo::new(Store::with_budget(1 << 22)); + let (rb, ro, rt) = ( + build(&full_repo, &base), + build(&full_repo, &ours), + build(&full_repo, &theirs), + ); + let arc = full_repo.store(); + let mut full = arc.lock().unwrap(); + + let flat_res = three_way_files(&mut full, Some((rb, &bp)), (ro, &op), (rt, &tp), None); + let eng_res = merge_trees(&mut full, Some((rb, &bp)), (ro, &op), (rt, &tp), None); + + match (flat_res, eng_res) { + (Ok(fm), Ok(TreeMerge::Done(done))) => { + cov.merged += 1; + match &done.carried { + Carried::Root(_) => cov.carried_root += 1, + Carried::Entries(v) if !v.is_empty() => cov.carried_some += 1, + _ => {} + } + if !fm.conflicts.is_empty() { + cov.conflicts += 1; + } + if !fm.sidecars.is_empty() { + cov.sidecars += 1; + } + let want = flat_of_files(&fm.files); + let got = expand(&mut full, &done); + assert_eq!(got, want, "seed {seed}: file sets differ"); + assert_eq!( + sorted(done.merge.conflicts.clone()), + sorted(fm.conflicts.clone()), + "seed {seed}: conflicts differ" + ); + assert_eq!( + sorted_sidecars(&done.merge.sidecars), + sorted_sidecars(&fm.sidecars), + "seed {seed}: sidecars differ" + ); + + // Policy: a carried subtree is policy-disjoint, or identical + // on all three sides. + let prots = [&op, &tp, &bp]; + for path in carried_paths(&done.carried) { + let ids = [rb, ro, rt].map(|r| tree_at(&mut full, r, &path)); + let identical = ids[0].is_some() && ids[0] == ids[1] && ids[1] == ids[2]; + assert!( + identical || !rule_intersects(&prots, &path), + "seed {seed}: carried {path:?} intersects a rule and is not identical" + ); + } + + // The same case on a store with objects missing: the engine + // asks for what it needs, never for anything under a carried + // subtree, and ends with the same answer. + let part_repo = VfsRepo::new(Store::with_budget(1 << 22)); + let _ = ( + build(&part_repo, &base), + build(&part_repo, &ours), + build(&part_repo, &theirs), + ); + let part_arc = part_repo.store(); + let mut part = part_arc.lock().unwrap(); + let mut universe: HashSet = HashSet::new(); + for r in [rb, ro, rt] { + for (_, id) in all_nodes(&mut full, r) { + universe.insert(id); + } + } + let mut drop_rng = Rng(seed ^ 0xDEADBEEF | 1); + for id in &universe { + if drop_rng.chance(1, 2) { + let _ = part.delete(id); + } + } + // Objects that appear at an uncarried path are fair game. + let carried = carried_paths(&done.carried); + let mut allowed: HashSet = HashSet::new(); + for r in [rb, ro, rt] { + for (p, id) in all_nodes(&mut full, r) { + if !carried + .iter() + .any(|c| under(&p, c) && !c.is_empty() || c.is_empty() && !p.is_empty()) + { + allowed.insert(id); + } + } + allowed.insert(r); + } + let mut rounds = 0; + let done2 = loop { + rounds += 1; + assert!(rounds < 64, "seed {seed}: fill loop did not converge"); + match merge_trees(&mut part, Some((rb, &bp)), (ro, &op), (rt, &tp), None) + .unwrap_or_else(|e| panic!("seed {seed}: partial engine error {e:?}")) + { + TreeMerge::Done(d) => break d, + TreeMerge::Needs(list) => { + if rounds == 1 { + cov.needed_fill += 1; + } + assert!(!list.is_empty()); + for (id, _path) in list { + assert!( + allowed.contains(&id), + "seed {seed}: asked for {id} under a carried subtree" + ); + let obj = full.get(&id).unwrap(); + part.put(obj).unwrap(); + } + } + } + }; + // Carried subtrees may be absent on the partial store, so + // compare against the full-store expansion via the full store. + assert_eq!( + expand(&mut full, &done2), + want, + "seed {seed}: partial run differs" + ); + } + (Err(a), Err(b)) => { + // Both refuse. The first error may differ (traversal order), but + // both must be one of the deliberate refusals. + for e in [&a, &b] { + match e { + Error::PathCollision(_) => cov.refused_collision += 1, + Error::ProtectedMergeNeedsIdentity(_) => cov.refused_identity += 1, + _ => {} + } + } + let ok = |e: &Error| { + matches!( + e, + Error::ProtectedMergeNeedsIdentity(_) | Error::PathCollision(_) + ) + }; + assert!( + ok(&a) && ok(&b), + "seed {seed}: unexpected errors {a:?} / {b:?}" + ); + } + (Ok(_), Ok(TreeMerge::Needs(n))) => { + panic!("seed {seed}: engine asked for {n:?} on a full store") + } + (Ok(_), Err(e)) => panic!("seed {seed}: engine refused ({e:?}) but flatten merged"), + (Err(e), Ok(_)) => panic!("seed {seed}: flatten refused ({e:?}) but engine merged"), + } + } + + #[test] + fn engine_matches_the_flatten_merge_on_random_trees() { + let mut cov = Coverage::default(); + for seed in 0..4000u64 { + run_case(seed, &mut cov); + } + eprintln!("coverage: {cov:?}"); + // The generator must actually reach the interesting paths, or a green + // run proves nothing. + assert!(cov.merged > 1000, "{cov:?}"); + assert!(cov.carried_some > 300, "{cov:?}"); + assert!(cov.carried_root > 5, "{cov:?}"); + assert!(cov.conflicts > 100, "{cov:?}"); + assert!(cov.sidecars > 10, "{cov:?}"); + assert!(cov.needed_fill > 300, "{cov:?}"); + assert!(cov.refused_collision > 50, "{cov:?}"); + assert!(cov.refused_identity > 50, "{cov:?}"); + } + + // ---- named cases ------------------------------------------------------ + + fn files(items: &[(&str, u8, bool)]) -> Files { + items + .iter() + .map(|(p, c, prot)| { + ( + p.to_string(), + Leaf { + content: *c, + exec: false, + prot: *prot, + }, + ) + }) + .collect() + } + + fn rule(prefix: &str) -> Protection { + let mut p = Protection::default(); + p.prefixes.push(ProtectPrefix { + prefix: prefix.to_string(), + recipients: Vec::new(), + }); + p + } + + fn done(m: TreeMerge) -> TreeMergeDone { + match m { + TreeMerge::Done(d) => d, + TreeMerge::Needs(n) => panic!("unexpected Needs {n:?}"), + } + } + + #[test] + fn disjoint_edits_are_carried_unread() { + let repo = VfsRepo::new(Store::with_budget(1 << 20)); + let base = build(&repo, &files(&[("docs/x", 1, false), ("src/y", 1, false)])); + let ours = build(&repo, &files(&[("docs/x", 2, false), ("src/y", 1, false)])); + let theirs = build(&repo, &files(&[("docs/x", 1, false), ("src/y", 3, false)])); + let p = rule("secret"); + let arc = repo.store(); + let mut s = arc.lock().unwrap(); + let d = + done(merge_trees(&mut s, Some((base, &p)), (ours, &p), (theirs, &p), None).unwrap()); + assert!(d.merge.files.is_empty(), "nothing was read"); + match d.carried { + Carried::Entries(v) => { + let paths: Vec<&str> = v.iter().map(|(p, _)| p.as_str()).collect(); + assert_eq!(paths, vec!["docs", "src"]); + } + other => panic!("expected entries, got {other:?}"), + } + } + + #[test] + fn plaintext_under_a_sides_own_rule_is_enumerated_not_carried() { + // Ours edits a file under `secret/`, which is ruled on ours, and the + // file is plaintext (the state `protect` leaves before it encrypts). + // A carry would keep the plaintext; the full merge would encrypt it. + let repo = VfsRepo::new(Store::with_budget(1 << 20)); + let base = build( + &repo, + &files(&[("secret/f", 1, false), ("other/g", 1, false)]), + ); + let ours = build( + &repo, + &files(&[("secret/f", 2, false), ("other/g", 1, false)]), + ); + let p = rule("secret"); + let arc = repo.store(); + let mut s = arc.lock().unwrap(); + let d = done(merge_trees(&mut s, Some((base, &p)), (ours, &p), (base, &p), None).unwrap()); + let f = d.merge.files.iter().find(|f| f.path == "secret/f").unwrap(); + assert_eq!( + f.perms & PROTECTED, + 0, + "still plaintext, so the repo layer encrypts it" + ); + match d.carried { + Carried::Entries(v) => { + let paths: Vec<&str> = v.iter().map(|(p, _)| p.as_str()).collect(); + assert_eq!( + paths, + vec!["other"], + "only the policy-disjoint subtree is carried" + ); + } + other => panic!("expected entries, got {other:?}"), + } + } + + #[test] + fn identical_subtree_under_a_rule_is_carried() { + let repo = VfsRepo::new(Store::with_budget(1 << 20)); + let all = build(&repo, &files(&[("secret/f", 1, false), ("a", 1, false)])); + let ours = build(&repo, &files(&[("secret/f", 1, false), ("a", 2, false)])); + let p = rule("secret"); + let arc = repo.store(); + let mut s = arc.lock().unwrap(); + let d = done(merge_trees(&mut s, Some((all, &p)), (ours, &p), (all, &p), None).unwrap()); + match d.carried { + Carried::Entries(v) => { + assert!( + v.iter().any(|(p, _)| p == "secret"), + "identical on all three: carried" + ); + } + other => panic!("expected entries, got {other:?}"), + } + } + + #[test] + fn identical_roots_carry_the_root() { + let repo = VfsRepo::new(Store::with_budget(1 << 20)); + let r = build(&repo, &files(&[("a/b", 1, false)])); + let p = Protection::default(); + let arc = repo.store(); + let mut s = arc.lock().unwrap(); + let d = done(merge_trees(&mut s, Some((r, &p)), (r, &p), (r, &p), None).unwrap()); + assert_eq!(d.carried, Carried::Root(r)); + assert!(d.merge.files.is_empty()); + } + + #[test] + fn a_missing_subtree_in_the_differing_region_is_requested_with_its_path() { + let repo = VfsRepo::new(Store::with_budget(1 << 20)); + let base = build(&repo, &files(&[("d/x", 1, false)])); + let ours = build(&repo, &files(&[("d/x", 2, false)])); + let theirs = build(&repo, &files(&[("d/x", 3, false)])); + let p = Protection::default(); + let arc = repo.store(); + let mut s = arc.lock().unwrap(); + let d_ours = tree_at(&mut s, ours, "d").unwrap(); + s.delete(&d_ours).unwrap(); + match merge_trees(&mut s, Some((base, &p)), (ours, &p), (theirs, &p), None).unwrap() { + TreeMerge::Needs(n) => assert_eq!(n, vec![(d_ours, "d".to_string())]), + other => panic!("expected Needs, got {other:?}"), + } + } + + #[test] + fn a_file_and_a_directory_collide_across_the_carry_boundary() { + // Ours adds file `x`; theirs adds directory `x/` (carried, since the + // base lacks it and ours lacks it as a tree: a mixed-kind name). + let repo = VfsRepo::new(Store::with_budget(1 << 20)); + let ours = build(&repo, &files(&[("x", 1, false)])); + let theirs = build(&repo, &files(&[("x/y", 1, false)])); + let p = Protection::default(); + let arc = repo.store(); + let mut s = arc.lock().unwrap(); + let err = merge_trees(&mut s, None, (ours, &p), (theirs, &p), None).unwrap_err(); + assert!(matches!(err, Error::PathCollision(_)), "got {err:?}"); + } +} diff --git a/docs/adr/0012-three-way-merge.md b/docs/adr/0012-three-way-merge.md index d3ef41da..fdd8e1ec 100644 --- a/docs/adr/0012-three-way-merge.md +++ b/docs/adr/0012-three-way-merge.md @@ -53,6 +53,24 @@ merge-aware `commit` that records both parents; fast-forward and already-up-to-date are short-circuits; binary conflicts keep ours and write a `.theirs` sidecar; a secret-registry conflict aborts the merge atomically. +## Amendment (P37c-i, ADR-0049) + +Two behaviors changed, both fixes: + +- **Mode is merged separately from content.** Content still resolves by blob id + (and the `PROTECTED` bit for protected paths). The mode of the result is then + a three-way on the modes of the entries that exist: equal wins, a side equal + to base yields to the side that changed, a lone survivor (delete/modify) + keeps its own mode, and when both sides changed differently executable wins. + Previously the mode followed whichever side's entry was kept, so a mode-only + change on one side was silently dropped, and identical content with different + modes could be reported as a conflict. +- **A file/directory collision is rejected before any output is written.** + When the merged result would make a name both a file and a directory (`x` and + `x/child`), the merge fails with `PathCollision` naming the path. Previously + the tree writer emitted both a blob and a tree with that name and + materialization failed after changing working files. + ## Alternatives considered - **Two-way (no base) merge.** Simpler but produces far more spurious conflicts; diff --git a/docs/adr/0049-merge-on-partial-clones.md b/docs/adr/0049-merge-on-partial-clones.md index 0793484c..14cb43eb 100644 --- a/docs/adr/0049-merge-on-partial-clones.md +++ b/docs/adr/0049-merge-on-partial-clones.md @@ -1,6 +1,6 @@ # ADR-0049: Merge on partial clones (P37c) -- **Status:** Proposed +- **Status:** Proposed (37c-i built: engine and differential tests; not yet wired into `sc merge`) - **Date:** 2026-10-17 - **Phase:** 37c - **Builds on:** ADR-0048 (presence-based partial clones), ADR-0047 (want-by-id fetch), ADR-0012 (three-way merge), ADR-0025 (protected merge and replay), ADR-0043 (randomized sealing) diff --git a/docs/adr/README.md b/docs/adr/README.md index 8769ce48..8961222a 100644 --- a/docs/adr/README.md +++ b/docs/adr/README.md @@ -59,7 +59,7 @@ supersedes the old one and update the **Status** line of the superseded record. | [0046](0046-wal-bucket-remotes.md) | Bucket remotes via an immutable-pack WAL + one CAS'd manifest | Accepted | 36 | | [0047](0047-want-by-id-fetch-and-gap-fill.md) | Want-by-id fetch and explicit gap fill for partial clones | Accepted (37a built) | 37 | | [0048](0048-presence-based-partial-clones.md) | Presence-based walks for partial clones | Accepted (37b-i, 37b-ii built) | 37b | -| [0049](0049-merge-on-partial-clones.md) | Merge on partial clones | Proposed | 37c | +| [0049](0049-merge-on-partial-clones.md) | Merge on partial clones | Proposed (37c-i built) | 37c | See [`ROADMAP.md`](../../ROADMAP.md) for the phase sequence. ADR numbers are assigned in creation order, so 0017 — the secret scanner — sequences as phase P5, From e13b3b1e39473114338ac484222c4daebf99c0d8 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Sat, 17 Oct 2026 12:37:17 +0300 Subject: [PATCH 3/4] fix(repo): wrap union helper, harvest fallback on collision, stronger differential (P37c-i review) - TreeMergeDone::merged_wraps: the complete input wrap union whenever anything was carried (ADR-0049 decision 4); the differential now generates wraps and checks the engine keeps every wrap the full merge keeps and drops none the inputs held for a surviving protected blob. TreeMergeDone documents that wrapped_carry alone is incomplete when anything was carried. - ws harvest: a file/directory collision falls back to work- instead of aborting the harvest. - differential also checks conflicts, sidecars and the Needs paths on the partial-store run; the misleading boundary-collision test is renamed. - ADR-0049 absence table, scope of 37c-i, new open questions (ciphertext fetched before a refusal, fill progress, mode vs delete, old collisions); ADR-0012 amendment wording corrected. Co-Authored-By: Claude Sonnet 5.5 --- crates/repo/src/merge_tree.rs | 138 +++++++++++++++++++++-- crates/repo/src/ws.rs | 38 ++++++- docs/adr/0012-three-way-merge.md | 9 +- docs/adr/0049-merge-on-partial-clones.md | 18 +-- 4 files changed, 184 insertions(+), 19 deletions(-) diff --git a/crates/repo/src/merge_tree.rs b/crates/repo/src/merge_tree.rs index 33b87af1..2dc478d5 100644 --- a/crates/repo/src/merge_tree.rs +++ b/crates/repo/src/merge_tree.rs @@ -43,6 +43,13 @@ pub(crate) enum Carried { } /// A finished tree merge. +/// +/// **`merge.wrapped_carry` is incomplete whenever anything was carried**: it +/// holds wraps only for the protected blobs of the regions the merge read, and a +/// carried subtree's protected blobs are never enumerated. Building the merged +/// snapshot's `protection.wrapped` from it, then pruning to reachable blobs as +/// the full merge does, would permanently drop every recipient's access to +/// those blobs. Use [`TreeMergeDone::merged_wraps`] instead. #[derive(Debug)] pub(crate) struct TreeMergeDone { /// The resolved files of every region the merge read. @@ -51,6 +58,41 @@ pub(crate) struct TreeMergeDone { pub carried: Carried, } +impl TreeMergeDone { + /// Did the merge carry any subtree by id? + pub fn carried_any(&self) -> bool { + match &self.carried { + Carried::Root(_) => true, + Carried::Entries(v) => !v.is_empty(), + } + } + + /// The wraps the merged snapshot must start from (ADR-0049 decision 4). + /// If anything was carried, the complete union of the input wraps (ours, + /// theirs, base; an id seen as deleted at one path may survive under a + /// carried subtree elsewhere, so no per-id pruning is sound). Otherwise + /// just the enumerated regions' wraps, which the caller prunes to + /// reachable blobs exactly as the full merge does. + pub fn merged_wraps( + &self, + ours: &Protection, + theirs: &Protection, + base: &Protection, + ) -> BTreeMap> { + let mut out = self.merge.wrapped_carry.clone(); + if !self.carried_any() { + return out; + } + for p in [ours, theirs, base] { + for (id, wks) in &p.wrapped { + let entry = out.entry(*id).or_default(); + *entry = crate::protect::union_wraps(entry, wks); + } + } + out + } +} + /// The outcome of one [`merge_trees`] pass. #[derive(Debug)] pub(crate) enum TreeMerge { @@ -154,6 +196,10 @@ pub(crate) fn merge_trees( Some(root_entry(theirs.0)), ], )?; + // A pass that needs objects discards everything it accumulated: leaves + // push into `acc` before some reads that can come back NotFound, so a + // partial `acc` must never survive into the next round. The caller fills + // and calls again from scratch. if !w.needs.is_empty() { return Ok(TreeMerge::Needs(w.needs)); } @@ -636,6 +682,25 @@ mod tests { } else { rand_policy(&mut rng) }; + // Give each side wraps for (some of) its protected blobs, for a random + // recipient subset, so the wrap union can be checked. + let (mut bp, mut op, mut tp) = (bp, op, tp); + for (files, prot) in [(&base, &mut bp), (&ours, &mut op), (&theirs, &mut tp)] { + for l in files.values().filter(|l| l.prot) { + let id = scl_core::Object::blob(leaf_bytes(l)).id(); + for r in ["r1", "r2", "r3"] { + if rng.chance(1, 2) { + prot.wrapped + .entry(id) + .or_default() + .push(scl_core::WrappedKey { + recipient_id: r.to_string(), + wrapped_dek: vec![rng.below(256) as u8], + }); + } + } + } + } let full_repo = VfsRepo::new(Store::with_budget(1 << 22)); let (rb, ro, rt) = ( @@ -677,6 +742,39 @@ mod tests { "seed {seed}: sidecars differ" ); + // Wraps: the merged snapshot must keep every wrap the full merge + // keeps, and lose none the inputs held for a protected blob that + // survives in the result. + let recips = |ws: Option<&Vec>| -> BTreeSet { + ws.map(|v| v.iter().map(|w| w.recipient_id.clone()).collect()) + .unwrap_or_default() + }; + let merged_wraps = done.merged_wraps(&op, &tp, &bp); + for (id, wks) in &fm.wrapped_carry { + let want: BTreeSet = + wks.iter().map(|w| w.recipient_id.clone()).collect(); + assert!( + recips(merged_wraps.get(id)).is_superset(&want), + "seed {seed}: engine lost wraps the full merge keeps for {id}" + ); + } + for (path, (_, perms, bytes, _)) in &got { + if perms & PROTECTED == 0 { + continue; + } + let id = scl_core::Object::blob(bytes.clone()).id(); + let held: BTreeSet = [&op, &tp, &bp] + .iter() + .flat_map(|p| recips(p.wrapped.get(&id))) + .collect(); + if done.carried_any() { + assert!( + recips(merged_wraps.get(&id)).is_superset(&held), + "seed {seed}: carried merge dropped input wraps for protected {path}" + ); + } + } + // Policy: a carried subtree is policy-disjoint, or identical // on all three sides. let prots = [&op, &tp, &bp]; @@ -717,15 +815,24 @@ mod tests { let mut allowed: HashSet = HashSet::new(); for r in [rb, ro, rt] { for (p, id) in all_nodes(&mut full, r) { - if !carried - .iter() - .any(|c| under(&p, c) && !c.is_empty() || c.is_empty() && !p.is_empty()) - { + if !carried.iter().any(|c| { + if c.is_empty() { + !p.is_empty() + } else { + under(&p, c) + } + }) { allowed.insert(id); } } allowed.insert(r); } + let mut node_paths: HashSet<(String, ObjectId)> = HashSet::new(); + for r in [rb, ro, rt] { + for (p, id) in all_nodes(&mut full, r) { + node_paths.insert((p, id)); + } + } let mut rounds = 0; let done2 = loop { rounds += 1; @@ -739,11 +846,15 @@ mod tests { cov.needed_fill += 1; } assert!(!list.is_empty()); - for (id, _path) in list { + for (id, path) in list { assert!( allowed.contains(&id), "seed {seed}: asked for {id} under a carried subtree" ); + assert!( + node_paths.contains(&(path.clone(), id)), + "seed {seed}: Needs reported {path:?} for {id}, which is not where it lives" + ); let obj = full.get(&id).unwrap(); part.put(obj).unwrap(); } @@ -757,6 +868,16 @@ mod tests { want, "seed {seed}: partial run differs" ); + assert_eq!( + sorted(done2.merge.conflicts.clone()), + sorted(fm.conflicts.clone()), + "seed {seed}: partial run conflicts differ" + ); + assert_eq!( + sorted_sidecars(&done2.merge.sidecars), + sorted_sidecars(&fm.sidecars), + "seed {seed}: partial run sidecars differ" + ); } (Err(a), Err(b)) => { // Both refuse. The first error may differ (traversal order), but @@ -948,9 +1069,10 @@ mod tests { } #[test] - fn a_file_and_a_directory_collide_across_the_carry_boundary() { - // Ours adds file `x`; theirs adds directory `x/` (carried, since the - // base lacks it and ours lacks it as a tree: a mixed-kind name). + fn a_file_and_a_directory_with_the_same_name_collide() { + // Ours adds file `x`; theirs adds directory `x/`: a mixed-kind name, + // which the engine enumerates (it is never carried), so the collision + // check sees both `x` and `x/y`. let repo = VfsRepo::new(Store::with_budget(1 << 20)); let ours = build(&repo, &files(&[("x", 1, false)])); let theirs = build(&repo, &files(&[("x/y", 1, false)])); diff --git a/crates/repo/src/ws.rs b/crates/repo/src/ws.rs index 90ea5269..9c379c63 100644 --- a/crates/repo/src/ws.rs +++ b/crates/repo/src/ws.rs @@ -548,7 +548,11 @@ impl Repo { Err(e) => Err(e), } } - Err(Error::ProtectedMergeNeedsIdentity(_)) | Err(Error::NotAuthorized(_)) => Ok(false), + // A file/directory collision (ADR-0049) is not a clean merge: this + // workspace falls back to a `work-` branch like any conflict. + Err(Error::ProtectedMergeNeedsIdentity(_)) + | Err(Error::NotAuthorized(_)) + | Err(Error::PathCollision(_)) => Ok(false), Err(e) => Err(e), } } @@ -1573,6 +1577,38 @@ mod tests { std::fs::remove_dir_all(&root).unwrap(); } + #[test] + fn harvest_file_directory_collision_falls_back_instead_of_aborting() { + // ws-1 adds a FILE `n`; ws-2 adds a DIRECTORY `n/` with a file in it. + // Landing ws-1 first makes ws-2's merge a file/directory collision + // (ADR-0049): it falls back to `work-2` like any conflict, and the + // harvest of the other workspaces is not aborted. + let root = tmp_root("harvest-collision"); + let repo = init(&root); + std::fs::write(root.join("keep.txt"), "k\n").unwrap(); + repo.commit("t", "base").unwrap(); + + let session = repo.ws_fork(2, "t", None).unwrap(); + std::fs::write(session.workspaces[0].dir.join("n"), "file\n").unwrap(); + std::fs::create_dir_all(session.workspaces[1].dir.join("n")).unwrap(); + std::fs::write(session.workspaces[1].dir.join("n/child"), "nested\n").unwrap(); + + let outcomes = repo.ws_harvest(None, "t", None).unwrap(); + assert_eq!(outcomes.len(), 2); + assert!( + matches!(&outcomes[0], WsHarvestOutcome::Landed { index: 1, .. }), + "got {:?}", + outcomes[0] + ); + assert!( + matches!(&outcomes[1], WsHarvestOutcome::FallbackBranch { index: 2, branch } if branch == "work-2"), + "got {:?}", + outcomes[1] + ); + assert_eq!(std::fs::read_to_string(root.join("n")).unwrap(), "file\n"); + std::fs::remove_dir_all(&root).unwrap(); + } + #[test] fn harvest_conflict_falls_back_without_touching_anything() { let root = tmp_root("harvest-conflict"); diff --git a/docs/adr/0012-three-way-merge.md b/docs/adr/0012-three-way-merge.md index fdd8e1ec..fca942a5 100644 --- a/docs/adr/0012-three-way-merge.md +++ b/docs/adr/0012-three-way-merge.md @@ -63,11 +63,14 @@ Two behaviors changed, both fixes: to base yields to the side that changed, a lone survivor (delete/modify) keeps its own mode, and when both sides changed differently executable wins. Previously the mode followed whichever side's entry was kept, so a mode-only - change on one side was silently dropped, and identical content with different - modes could be reported as a conflict. + change on one side was silently dropped (the unchanged side's blob id equalled + the changed side's, so the unchanged side's entry won). - **A file/directory collision is rejected before any output is written.** When the merged result would make a name both a file and a directory (`x` and - `x/child`), the merge fails with `PathCollision` naming the path. Previously + `x/child`), the merge fails with `PathCollision` naming the path. This + covers every caller of the file merge: `sc merge`, cherry-pick, rebase, the + merge into a private branch and the `sc ws harvest` auto-merge check (a + colliding workspace falls back to a `work-` branch). Previously the tree writer emitted both a blob and a tree with that name and materialization failed after changing working files. diff --git a/docs/adr/0049-merge-on-partial-clones.md b/docs/adr/0049-merge-on-partial-clones.md index 14cb43eb..eab8a4e2 100644 --- a/docs/adr/0049-merge-on-partial-clones.md +++ b/docs/adr/0049-merge-on-partial-clones.md @@ -44,7 +44,7 @@ Everything else is **enumerated**: read, flattened for that subtree, and resolve | Situation at a name | Result | |---|---| | tree on all three sides, all differ | recurse | -| tree on some sides, absent (or a blob) on others | enumerate the subtrees that exist and apply the per-path rules, so delete/modify and add/add keep their flatten meaning | +| tree on some sides, absent on others | the carry rules above apply first (a one-sided add or delete carries or drops the subtree); when all three differ it recurses with the missing side as an empty tree, so delete/modify and add/add keep their flatten meaning | | blob on one side and a tree on another | enumerate both and apply the per-path rules, then apply the collision check below | | blobs on all three, all differ | the existing leaf merge | @@ -116,7 +116,7 @@ A second differential runs each merge on a partial clone and a full clone. The n ## Phasing -- **37c-i, engine.** The content/mode split and the collision check on the flatten merge (with the ADR-0012 amendment and tests), `merge_trees` returning `TreeMerge` or `Needs`, the policy-intersection test, the wrap-union rule, and the differential tests. Nothing calls the new engine yet except tests. +- **37c-i, engine (built).** The content/mode split and the collision check on the flatten merge (with the ADR-0012 amendment and tests), `merge_trees` returning `TreeMerge` or `Needs`, the policy-intersection test, `TreeMergeDone::merged_wraps` (the wrap-union rule; assembling and pruning the merged snapshot's `protection.wrapped` from it is 37c-ii), and the differential tests (file sets with modes and perms, conflicts, sidecars, wraps, carried-subtree policy, and fills on a store with objects removed). Nothing calls the new engine yet except tests. Decision 9's cases built from real operations (protect, grant, revoke, with decryption by each identity), equal plaintext sealed independently, an empty-directory entry on a carried side, merge then gc, and an interrupted fill then retry need the repo layer and land with 37c-ii. - **37c-ii, clean merge on a partial clone.** `merge_with_identity` uses the engine when the clone is partial: fill loop outside the lock, `ensure_view(merged_root)`, then the existing pipeline. A conflict is refused up front. Demo: two branches touching disjoint directories merge on a `--filter` clone without fetching either side's out-of-view content; a merge whose differing region is outside the view fills it on a lazy clone and is refused on a non-lazy one. - **37c-iii, conflicts.** First an amendment to this record that fixes decision 8's contract, then markers for present in-view conflicts, completion from the decided root, abort, `sc conflicts` and `sc resolve`. - **37c-iv, replay.** `cherry-pick` and `rebase` call the same engine; `replay.rs:176` is the only guard that blocks them, and `three_way_files` is already their merge. Replay adds protected re-encryption under the replayed rules and the resumable-rebase state. @@ -132,11 +132,15 @@ A second differential runs each merge on a partial clone and a full clone. The n ## Open questions -1. **A `sc verify` check for plaintext under a rule** (decision 3's accepted divergence). -2. **Wrap growth.** Measure before designing a prune; `sc rewrap` on a partial clone is refused today. -3. **Switching full clones to the new engine.** Out of scope; decide after the differential has soaked. -4. **Merging a public branch INTO a private one** routes through `merge_into_private` and stays refused on a partial clone (ADR-0048 q10). -5. **Empty-directory normalization.** Decision 7 accepts the divergence; revisit if it shows up in real histories. +1. **Ciphertext fetched before a refusal it cannot avoid.** `plain_input` reads a protected blob before it checks for an identity (`merge.rs:565`, `:569`), so a lazy partial merge of a content-divergent protected path would fetch the ciphertext and then refuse with `ProtectedMergeNeedsIdentity`. 37c-ii should check the identity requirement before reporting `Needs`. +2. **Fill progress.** `merge_trees` detects an absent tree with `store.contains` but an absent blob through a `NotFound` from `get`. If those ever disagree (a stale pack index entry), a round asks for an id the filler thinks is present. The 37c-ii fill loop must detect a round that makes no progress and fail. +3. **Mode change against a delete.** Ours changes only the mode and theirs deletes the file: the delete wins silently (content decides, as before). Git reports modify/delete. Unchanged by this work; decide separately. +4. **Pre-existing collisions.** Before the collision check, a clean cherry-pick or rebase that produced a file and a directory with the same name may already have committed such a snapshot. The fix prevents new ones and does not repair old ones. +5. **A `sc verify` check for plaintext under a rule** (decision 3's accepted divergence). +6. **Wrap growth.** Measure before designing a prune; `sc rewrap` on a partial clone is refused today. +7. **Switching full clones to the new engine.** Out of scope; decide after the differential has soaked. +8. **Merging a public branch INTO a private one** routes through `merge_into_private` and stays refused on a partial clone (ADR-0048 q10). +9. **Empty-directory normalization.** Decision 7 accepts the divergence; revisit if it shows up in real histories. ## Alternatives considered From d12af6bdcc4994830c3979182e8cd80b8aae6437 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Sat, 17 Oct 2026 12:42:52 +0300 Subject: [PATCH 4/4] test(repo): unique temp root for the harvest collision test Co-Authored-By: Claude Sonnet 5.5 --- crates/repo/src/ws.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/repo/src/ws.rs b/crates/repo/src/ws.rs index 9c379c63..7f708dbc 100644 --- a/crates/repo/src/ws.rs +++ b/crates/repo/src/ws.rs @@ -1583,7 +1583,7 @@ mod tests { // Landing ws-1 first makes ws-2's merge a file/directory collision // (ADR-0049): it falls back to `work-2` like any conflict, and the // harvest of the other workspaces is not aborted. - let root = tmp_root("harvest-collision"); + let root = tmp_root("harvest-file-dir-collision"); let repo = init(&root); std::fs::write(root.join("keep.txt"), "k\n").unwrap(); repo.commit("t", "base").unwrap();