From 4ce66d7f4d6849cdb7d8180cb6d14170d0fb4aef Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Sun, 18 Oct 2026 16:51:37 +0300 Subject: [PATCH 1/3] feat(repo): cherry-pick and rebase on a partial clone (P37c-iv) replay_commit, the one place cherry-pick and the rebase fold reach the file merge, now uses the pull-based tree engine on a partial clone. A subtree the sides agree on is carried by id, a region both touched is read (filled by id on a lazy clone, a gap error otherwise), and the result keeps the complete wrap union when anything was carried. A stopped pick or rebase keeps its carried subtrees in the decided root and applies the same refusals as a stopped merge. Completion grafts the out-of-view part from the decided root. The final write fills the view first, and abort checks it before removing anything. Sidecar cleanup is shared and never removes a file a tree tracks, which also fixes it for rebase --continue. ADR-0049 decision 10 records the design. Co-Authored-By: Claude Sonnet 5.5 --- CLAUDE.md | 10 +- ROADMAP.md | 4 +- crates/cli/tests/partial_clone_safety.rs | 100 +++-- crates/repo/src/lib.rs | 2 + crates/repo/src/partial_merge.rs | 28 +- crates/repo/src/partial_replay.rs | 474 +++++++++++++++++++++++ crates/repo/src/replay.rs | 129 ++++-- crates/repo/src/repo.rs | 63 +-- demo/run_lazy_fetch_demo.sh | 35 +- docs/adr/0049-merge-on-partial-clones.md | 16 +- docs/adr/README.md | 2 +- 11 files changed, 748 insertions(+), 115 deletions(-) create mode 100644 crates/repo/src/partial_replay.rs diff --git a/CLAUDE.md b/CLAUDE.md index d9c1242d..ea399b18 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -106,7 +106,7 @@ the project tree if desired. ## Capability map (what's built, by phase) -All 36 phases, P37a, P37b-i, P37b-ii, P37c-ii and P37c-iii are built and tested. One line of current fact per phase; the +All 36 phases, P37a, P37b-i, P37b-ii and P37c are built and tested. One line of current fact per phase; the authoritative rationale and full semantics live in the linked ADR, the design in `ARCHITECTURE.md`. The old per-phase narrative log this table replaced is archived verbatim at `docs/archive/claude-md-phase-log-2026-07.md` — do not @@ -153,7 +153,7 @@ the code, those win. | P36 | Bucket WAL remotes (sc+wal://, sc+s3://): immutable packs + CAS'd manifest, checkpoints + log-tail cold start, bucket-backed `sc serve --store` with disposable instances | [0046](docs/adr/0046-wal-bucket-remotes.md) | | P37a | Want-by-id fetch: batched `GetObjects` verb (wire v5), opt-in `.sc/promisor-lazy`, `sc cat :` fills only the absent trees and blob on its path; `Store::get` stays network-free; sealed objects refused by kind; sparse widen and the lifted P27 refusals are P37b | [0047](docs/adr/0047-want-by-id-fetch-and-gap-fill.md) | | P37b-i | Presence-based partial clones: push, verify and gc walk what the store holds (`reachable_objects_partial`), not the fetch filter; push checks every gap id against the destination before sending; a view gap fails with `GapInView` before any write (preflight at the top of `materialize`, plus `undo` and `sparse set`); a partial clone fetches from its origin only. 37b-ii: on a lazy, online clone `ensure_view` fills the view closure by id before switch, fast-forward, undo and `sparse set`, `sparse set` may widen past the filter, a by-id reply is capped at `min(--max-pack-size, 512 MiB)` and a refused batch is split. | [0048](docs/adr/0048-presence-based-partial-clones.md) | -| P37c-ii, P37c-iii | Merge on a partial clone: a tree-recursive engine (`merge_trees`) carries a subtree by id when the sides agree and no protection rule applies, reads only where they differ or a rule intersects, and on a lazy, online clone fills what it asks for by id outside the store lock (a non-lazy clone gets the gap error with `sc backfill `); a carried protected subtree keeps the complete input wrap union; a conflict inside the view is marked and resolved like on a full clone (`sc conflicts`/`sc resolve`/`merge --abort`/`sc commit`; the out-of-view part is grafted from the decided root), while a conflict outside the view, or a result that must be sealed outside it, is refused before any ref, merge-state or working-tree change (fetched objects stay). A subtree identical on all three sides is carried even under a protection rule. Mode-only edits now merge and a file/directory collision fails before any write, on full clones too. Cherry-pick/rebase are next | [0049](docs/adr/0049-merge-on-partial-clones.md) | +| P37c | Merge, cherry-pick and rebase on a partial clone: a tree-recursive engine (`merge_trees`) carries a subtree by id when the sides agree and no protection rule applies, reads only where they differ or a rule intersects, and on a lazy, online clone fills what it asks for by id outside the store lock (a non-lazy clone gets the gap error with `sc backfill `); a carried protected subtree keeps the complete input wrap union; a conflict inside the view is marked and resolved like on a full clone (`sc conflicts`/`sc resolve`/`merge --abort`/`sc commit`; the out-of-view part is grafted from the decided root), while a conflict outside the view, or a result that must be sealed outside it, is refused before any ref, merge-state or working-tree change (fetched objects stay). A subtree identical on all three sides is carried even under a protection rule. Cherry-pick and rebase use the same engine (`replay_commit`): the fold's accumulator may hold absent subtrees, the view is filled before the final write, and a stopped pick or rebase keeps its carried subtrees in the decided root. Mode-only edits now merge and a file/directory collision fails before any write, on full clones too. Sessions are undecided | [0049](docs/adr/0049-merge-on-partial-clones.md) | ## Standing boundaries & gotchas @@ -169,10 +169,10 @@ transport-adjacent. The rest, imperatively: renderer has five typed repository commands, no general filesystem/shell capability, and no DTO field capable of carrying protected ciphertext or identity material. -- **Partial clones refuse cherry-pick/rebase, `sc ws fork`/`harvest`, +- **Partial clones refuse `sc ws fork`/`harvest`, `sc work`, `sc export`, `sparse disable`, an empty `sparse set` (and, on a non-lazy clone, one outside the filter), `sc rewrap`, - `sc grant`/`revoke`, creating, switching to or leaving a private branch, a merge whose conflict lies outside the view or needs sealing outside it (no ref, state or working-tree change), `sc push` to a git remote, and - `sc fetch` from any native remote but the promisor origin.** `sc merge` works, including conflicts inside the view (P37c). `sc backfill --all` + `sc grant`/`revoke`, creating, switching to or leaving a private branch, a merge, cherry-pick or rebase whose conflict lies outside the view or needs sealing outside it (no ref, state or working-tree change), `sc push` to a git remote, and + `sc fetch` from any native remote but the promisor origin.** `sc merge`, `sc cherry-pick` and `sc rebase` work, including conflicts inside the view (P37c). `sc backfill --all` converts to a genuine full clone and re-enables them. The filter records what a clone *asked for*, not what is present: after a fetch an in-filter object can be absent. On a lazy, online clone a switch, fast-forward, rebase, undo diff --git a/ROADMAP.md b/ROADMAP.md index 320ff2f7..c3bb1d10 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -558,11 +558,11 @@ 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; 37c-i engine, 37c-ii clean merge and 37c-iii conflicts built).** A tree-recursive three-way merge +**P37c — merge on partial clones (proposed; 37c-i engine, 37c-ii clean merge, 37c-iii conflicts and 37c-iv replay built).** 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, -the region that intersects a protection rule, and the sparse view's closure. Merges are built (`sc merge` on a partial clone): a conflict inside the view is marked, resolved with `sc conflicts`/`sc resolve`, aborted or committed like on a full clone; a conflict outside the view, or one that needs sealing outside it, is refused before any write. Cherry-pick/rebase are next; sessions +the region that intersects a protection rule, and the sparse view's closure. Merges are built (`sc merge` on a partial clone): a conflict inside the view is marked, resolved with `sc conflicts`/`sc resolve`, aborted or committed like on a full clone; a conflict outside the view, or one that needs sealing outside it, is refused before any write. Cherry-pick and rebase use the same engine and conflict contract (`sc cherry-pick`, `sc rebase` and their `--continue`/`--abort` work on a partial clone); sessions are undecided. Design in [ADR-0049](docs/adr/0049-merge-on-partial-clones.md). diff --git a/crates/cli/tests/partial_clone_safety.rs b/crates/cli/tests/partial_clone_safety.rs index b55fd1b2..3f132fc4 100644 --- a/crates/cli/tests/partial_clone_safety.rs +++ b/crates/cli/tests/partial_clone_safety.rs @@ -160,15 +160,14 @@ fn status_diff_and_gc_succeed_on_fresh_partial_clone() { /// P27 final review I2: `sc backfill --all` is the real escape hatch — /// fetches every remaining object and removes `.sc/promisor`, after which -/// a cherry-pick that was refused as unsupported on a partial clone works. +/// a sparse disable that was refused on a partial clone works. #[test] fn backfill_all_lifts_partial_clone_refusals() { let (src_root, dst_root) = make_partial_clone("backfill-all"); - // Replay is still refused while partial (a merge, clean or conflicting - // inside the view, works since P37c). Create a clashing branch (same - // file) for the refusal and a non-conflicting one for the merge after - // backfill. + // `sparse disable` is still refused while partial (merge, cherry-pick + // and rebase work since P37c). Create a clashing branch and a + // non-conflicting one for the merge after backfill. assert!(sc(&dst_root, &["branch", "clash"]).status.success()); assert!(sc(&dst_root, &["switch", "clash"]).status.success()); std::fs::write(dst_root.join("src/a.txt"), "clash-edit\n").unwrap(); @@ -182,19 +181,15 @@ fn backfill_all_lifts_partial_clone_refusals() { std::fs::write(dst_root.join("src/a.txt"), "main-edit\n").unwrap(); assert!(sc(&dst_root, &["commit", "-m", "main"]).status.success()); - let pick_before = sc(&dst_root, &["cherry-pick", "clash"]); + let disable_before = sc(&dst_root, &["sparse", "disable"]); assert!( - !pick_before.status.success(), - "a cherry-pick must still be refused before backfill --all" + !disable_before.status.success(), + "`sparse disable` must still be refused before backfill --all" ); assert!( - stderr(&pick_before).contains("backfill --all"), - "refusal must point at `sc backfill --all`: {}", - stderr(&pick_before) - ); - assert!( - !dst_root.join("src/a.txt.theirs").exists(), - "a refused cherry-pick writes nothing" + stderr(&disable_before).contains("backfill"), + "refusal must point at `sc backfill`: {}", + stderr(&disable_before) ); assert_eq!( std::fs::read_to_string(dst_root.join("src/a.txt")).unwrap(), @@ -221,22 +216,6 @@ fn backfill_all_lifts_partial_clone_refusals() { stderr(&sparse_disable) ); - // The cherry-pick is no longer refused: it reaches the ordinary - // conflict state (markers in the file), which `--abort` undoes. - let conflict_after = sc(&dst_root, &["cherry-pick", "clash"]); - assert!( - !stderr(&conflict_after).contains("partial clone"), - "a cherry-pick must no longer be refused after backfill --all: {}", - stderr(&conflict_after) - ); - assert!( - std::fs::read_to_string(dst_root.join("src/a.txt")) - .unwrap() - .contains("<<<<<<<"), - "the ordinary conflict markers are written" - ); - assert!(sc(&dst_root, &["cherry-pick", "--abort"]).status.success()); - let merge_after = sc(&dst_root, &["merge", "feature"]); assert!( merge_after.status.success(), @@ -301,6 +280,65 @@ fn conflicting_merge_on_a_partial_clone_resolves_and_commits() { std::fs::remove_dir_all(&dst_root).unwrap(); } +/// P37c-iv: cherry-pick and rebase work on a partial clone, and a conflict in +/// the view stops them with markers that `sc conflicts` and `sc resolve` find. +#[test] +fn cherry_pick_and_rebase_work_on_a_partial_clone() { + let (src_root, dst_root) = make_partial_clone("replay"); + // Two branches off the first commit, each editing the same line. + assert!(sc(&dst_root, &["branch", "side"]).status.success()); + assert!(sc(&dst_root, &["switch", "side"]).status.success()); + std::fs::write(dst_root.join("src/a.txt"), "side-edit\n").unwrap(); + assert!(sc(&dst_root, &["commit", "-m", "side"]).status.success()); + assert!(sc(&dst_root, &["switch", "main"]).status.success()); + std::fs::write(dst_root.join("src/a.txt"), "main-edit\n").unwrap(); + assert!(sc(&dst_root, &["commit", "-m", "main"]).status.success()); + + let pick = sc(&dst_root, &["cherry-pick", "side"]); + assert!(!pick.status.success(), "a conflicting pick exits nonzero"); + assert!( + !stderr(&pick).contains("partial clone"), + "not refused as unsupported: {}", + stderr(&pick) + ); + assert!(std::fs::read_to_string(dst_root.join("src/a.txt")) + .unwrap() + .contains("<<<<<<<")); + assert!( + !dst_root.join("docs").exists(), + "docs/ stays out of the view" + ); + let listed = sc(&dst_root, &["conflicts"]); + assert!(stdout(&listed).contains("src/a.txt"), "{}", stdout(&listed)); + assert!(sc(&dst_root, &["cherry-pick", "--abort"]).status.success()); + assert_eq!( + std::fs::read_to_string(dst_root.join("src/a.txt")).unwrap(), + "main-edit\n" + ); + + let rebase = sc(&dst_root, &["rebase", "side"]); + assert!(!rebase.status.success(), "a conflicting rebase stops"); + assert!( + !stderr(&rebase).contains("partial clone"), + "not refused as unsupported: {}", + stderr(&rebase) + ); + let resolved = sc(&dst_root, &["resolve", "--theirs", "src/a.txt"]); + assert!(resolved.status.success(), "{}", stderr(&resolved)); + let cont = sc(&dst_root, &["rebase", "--continue"]); + assert!(cont.status.success(), "{}", stderr(&cont)); + assert_eq!( + std::fs::read_to_string(dst_root.join("src/a.txt")).unwrap(), + "main-edit\n" + ); + assert!(!dst_root.join("docs").exists()); + let verify = sc(&dst_root, &["verify"]); + assert!(verify.status.success(), "{}", stderr(&verify)); + + std::fs::remove_dir_all(&src_root).unwrap(); + std::fs::remove_dir_all(&dst_root).unwrap(); +} + #[test] fn sparse_widen_beyond_partial_filter_is_refused() { let (src_root, dst_root) = make_partial_clone("sparse-widen"); diff --git a/crates/repo/src/lib.rs b/crates/repo/src/lib.rs index 7f25671f..42861884 100644 --- a/crates/repo/src/lib.rs +++ b/crates/repo/src/lib.rs @@ -20,6 +20,8 @@ pub mod oplog; #[cfg(test)] mod partial_merge; #[cfg(test)] +mod partial_replay; +#[cfg(test)] mod partial_walks; pub mod pick_state; pub mod private; diff --git a/crates/repo/src/partial_merge.rs b/crates/repo/src/partial_merge.rs index 38136307..6c02acd7 100644 --- a/crates/repo/src/partial_merge.rs +++ b/crates/repo/src/partial_merge.rs @@ -13,18 +13,18 @@ use crate::error::Error; use crate::refs; use crate::repo::Repo; -const SRC_A: &[u8] = b"one\ntwo\nthree\n"; +pub(crate) const SRC_A: &[u8] = b"one\ntwo\nthree\n"; -struct Fx { - origin_root: PathBuf, - clone_root: PathBuf, - origin: Repo, +pub(crate) struct Fx { + pub(crate) origin_root: PathBuf, + pub(crate) clone_root: PathBuf, + pub(crate) origin: Repo, } impl Fx { /// An origin on `main` at one commit, with `extra` written into that first /// commit by the caller-supplied closure. - fn origin(tag: &str, extra: impl FnOnce(&Path)) -> Fx { + pub(crate) fn origin(tag: &str, extra: impl FnOnce(&Path)) -> Fx { let pid = std::process::id(); let base = std::env::temp_dir(); let origin_root = base.join(format!("scl-p37c-{tag}-origin-{pid}")); @@ -52,7 +52,7 @@ impl Fx { /// Cut `name` from the origin's `main`, write `files` on it, commit, and /// return to `main`. Returns the new tip. - fn origin_branch(&self, name: &str, files: &[(&str, &[u8])]) -> ObjectId { + pub(crate) fn origin_branch(&self, name: &str, files: &[(&str, &[u8])]) -> ObjectId { self.origin.branch(name).unwrap(); self.origin.switch(name).unwrap(); for (p, bytes) in files { @@ -64,7 +64,7 @@ impl Fx { } /// A `--filter src/` clone, lazy or not, that has fetched every origin branch. - fn clone(&self, lazy: bool) -> Repo { + pub(crate) fn clone(&self, lazy: bool) -> Repo { let clone = Repo::clone_url_filtered( self.origin_root.to_str().unwrap(), &self.clone_root, @@ -76,7 +76,7 @@ impl Fx { clone } - fn cleanup(self) { + pub(crate) fn cleanup(self) { drop(self.origin); for r in [&self.origin_root, &self.clone_root] { if r.exists() { @@ -87,16 +87,16 @@ impl Fx { } } -fn has(repo: &Repo, id: &ObjectId) -> bool { +pub(crate) fn has(repo: &Repo, id: &ObjectId) -> bool { repo.vfs().store().lock().unwrap().contains(id) } -fn blob_id(bytes: &[u8]) -> ObjectId { +pub(crate) fn blob_id(bytes: &[u8]) -> ObjectId { Object::blob(bytes.to_vec()).id() } /// The tree id of directory `name` directly under `snap`'s root. -fn dir_id(repo: &Repo, snap: &ObjectId, name: &str) -> ObjectId { +pub(crate) fn dir_id(repo: &Repo, snap: &ObjectId, name: &str) -> ObjectId { let arc = repo.vfs().store(); let mut s = arc.lock().unwrap(); let root = s.get_snapshot(snap).unwrap().root; @@ -104,7 +104,7 @@ fn dir_id(repo: &Repo, snap: &ObjectId, name: &str) -> ObjectId { } /// Commit an edit to `src/a.txt` on the clone's `main`. -fn clone_edit_src(fx: &Fx, clone: &Repo, bytes: &[u8]) { +pub(crate) fn clone_edit_src(fx: &Fx, clone: &Repo, bytes: &[u8]) { std::fs::write(fx.clone_root.join("src/a.txt"), bytes).unwrap(); clone.commit("t", "ours").unwrap(); } @@ -420,7 +420,7 @@ fn a_conflict_outside_the_view_is_refused_before_any_write() { } /// An origin whose `vault/` is protected for `pk` (rule plus ciphertext). -fn protected_origin(tag: &str, pk: &scl_crypto::PublicKey) -> Fx { +pub(crate) fn protected_origin(tag: &str, pk: &scl_crypto::PublicKey) -> Fx { let fx = Fx::origin(tag, |root| { std::fs::create_dir_all(root.join("vault")).unwrap(); std::fs::write(root.join("vault/s.txt"), b"l1\nl2\nl3\n").unwrap(); diff --git a/crates/repo/src/partial_replay.rs b/crates/repo/src/partial_replay.rs new file mode 100644 index 00000000..a467570c --- /dev/null +++ b/crates/repo/src/partial_replay.rs @@ -0,0 +1,474 @@ +//! Tests for cherry-pick and rebase on a partial clone (ADR-0049, P37c-iv). +//! +//! They reuse the merge fixture: an origin with `src/`, `docs/`, `docs/api/` +//! and `lib/`, a `--filter src/` clone, and origin branches cut from the first +//! commit. Each result is compared, by root tree id, with the same operation +//! done on the origin, which is a full repository. + +use scl_core::ObjectId; + +use crate::conflicts::ResolveSide; +use crate::error::Error; +use crate::partial_merge::{clone_edit_src, dir_id, has, protected_origin, Fx, SRC_A}; +use crate::refs; +use crate::replay::{PickResult, RebaseResult}; +use crate::repo::Repo; + +const SRC_UNO: &[u8] = b"uno\ntwo\nthree\n"; +const SRC_ONE: &[u8] = b"ONE\ntwo\nthree\n"; + +fn root_of(repo: &Repo, tip: &ObjectId) -> ObjectId { + repo.snapshot(tip).unwrap().root +} + +/// The origin cherry-picks `pick` onto a side branch that first commits +/// `ours_edit`, and returns the resulting root: what a partial clone's pick +/// must equal. +fn full_pick_root(fx: &Fx, pick: &str, ours_edit: &[(&str, &[u8])], resolve: &[&str]) -> ObjectId { + fx.origin.branch("expect").unwrap(); + fx.origin.switch("expect").unwrap(); + for (p, bytes) in ours_edit { + std::fs::write(fx.origin_root.join(p), bytes).unwrap(); + } + fx.origin.commit("t", "ours").unwrap(); + let tip = match fx.origin.cherry_pick(pick, "t", None, None) { + Ok(PickResult::Picked(id)) => id, + Err(Error::PickConflicts(_)) => { + for p in resolve { + fx.origin.resolve_path(p, ResolveSide::Ours, None).unwrap(); + } + fx.origin.commit("t", "pick").unwrap() + } + other => panic!("unexpected {other:?}"), + }; + let root = root_of(&fx.origin, &tip); + fx.origin.switch("main").unwrap(); + root +} + +/// A pick whose commit touched only a subtree outside the view carries that +/// subtree by id: nothing is read, nothing is fetched, on a clone that could +/// not fetch. +#[test] +fn a_pick_carries_a_subtree_outside_the_view_without_a_fetch() { + let fx = Fx::origin("pick-carry", |_| {}); + let b1 = fx.origin_branch("b1", &[("docs/api/guide.md", b"g changed\n")]); + let theirs_docs = dir_id(&fx.origin, &b1, "docs"); + let expect = full_pick_root(&fx, "b1", &[("src/a.txt", SRC_UNO)], &[]); + let clone = fx.clone(false); + assert!(!has(&clone, &theirs_docs), "premise: docs is a gap"); + clone_edit_src(&fx, &clone, SRC_UNO); + + let PickResult::Picked(id) = clone.cherry_pick("origin/b1", "t", None, None).unwrap() else { + panic!("expected a pick"); + }; + let snap = clone.snapshot(&id).unwrap(); + assert_eq!(snap.parents.len(), 1); + assert_eq!(snap.root, expect, "same tree as the full pick"); + assert_eq!(dir_id(&clone, &id, "docs"), theirs_docs); + assert!(!has(&clone, &theirs_docs), "never read or fetched"); + assert_eq!( + std::fs::read(fx.clone_root.join("src/a.txt")).unwrap(), + SRC_UNO + ); + assert!(!fx.clone_root.join("docs").exists()); + clone.gc(std::time::Duration::from_secs(0)).unwrap(); + assert!(has(&clone, &id)); + drop(clone); + fx.cleanup(); +} + +/// Both sides changed `docs/`: a lazy clone fills it by id, a non-lazy one is +/// refused before any write, naming the path. +#[test] +fn a_pick_over_a_region_outside_the_view_fills_when_lazy_and_is_refused_otherwise() { + let fx = Fx::origin("pick-fill", |_| {}); + fx.origin_branch("o", &[("docs/b.md", b"b2\n")]); + fx.origin_branch("b1", &[("docs/a.md", b"a1\n")]); + let expect = full_pick_root(&fx, "b1", &[("docs/b.md", b"b2\n")], &[]); + let clone = fx.clone(false); + clone.merge("origin/o", "t").unwrap(); // fast-forward + let before = refs::read_branch_tip(clone.layout(), "main").unwrap(); + + let err = clone.cherry_pick("origin/b1", "t", None, None).unwrap_err(); + assert!( + matches!(&err, Error::GapOutsideFilter(p) if p == "docs"), + "got {err:?}" + ); + assert_eq!( + refs::read_branch_tip(clone.layout(), "main").unwrap(), + before + ); + assert!(!clone.pick_in_progress()); + assert!(!fx.clone_root.join("docs").exists()); + + crate::promisor::set_lazy(clone.layout(), true).unwrap(); + let PickResult::Picked(id) = clone.cherry_pick("origin/b1", "t", None, None).unwrap() else { + panic!("expected a pick"); + }; + assert_eq!(root_of(&clone, &id), expect); + assert!(!fx.clone_root.join("docs").exists()); + drop(clone); + fx.cleanup(); +} + +/// A conflict inside the view stops the pick with markers and a decided root +/// that holds the carried subtree. Resolving and committing completes it to +/// the tree a full pick gives. +#[test] +fn a_pick_conflict_in_the_view_completes_like_a_full_pick() { + let fx = Fx::origin("pick-conflict", |_| {}); + let b1 = fx.origin_branch( + "b1", + &[ + ("src/a.txt", SRC_ONE), + ("docs/api/guide.md", b"g changed\n"), + ], + ); + let theirs_docs = dir_id(&fx.origin, &b1, "docs"); + let expect = full_pick_root(&fx, "b1", &[("src/a.txt", SRC_UNO)], &["src/a.txt"]); + let clone = fx.clone(false); + clone_edit_src(&fx, &clone, SRC_UNO); + let ours = refs::read_branch_tip(clone.layout(), "main").unwrap(); + + let err = clone.cherry_pick("origin/b1", "t", None, None).unwrap_err(); + assert!(matches!(err, Error::PickConflicts(1)), "got {err:?}"); + assert!(clone.pick_in_progress()); + let marked = std::fs::read_to_string(fx.clone_root.join("src/a.txt")).unwrap(); + assert!(marked.contains("<<<<<<<"), "{marked}"); + assert!(!fx.clone_root.join("docs").exists()); + assert_eq!(refs::read_branch_tip(clone.layout(), "main").unwrap(), ours); + + clone + .resolve_path("src/a.txt", ResolveSide::Ours, None) + .unwrap(); + let id = clone.commit("t", "pick").unwrap(); + assert!(!clone.pick_in_progress()); + let snap = clone.snapshot(&id).unwrap(); + assert_eq!(Some(snap.parents[0]), ours); + assert_eq!(snap.root, expect, "same tree as the full pick"); + assert_eq!(dir_id(&clone, &id, "docs"), theirs_docs); + assert!(!has(&clone, &theirs_docs), "never fetched"); + drop(clone); + fx.cleanup(); +} + +/// Abort restores ours; an abort that hits a gap in the view changes nothing. +#[test] +fn aborting_a_stopped_pick_restores_ours_and_a_view_gap_changes_nothing() { + let fx = Fx::origin("pick-abort", |_| {}); + fx.origin_branch( + "b1", + &[ + ("src/a.txt", b"\xff\xfeTHEIRS\n"), + ("docs/api/guide.md", b"g changed\n"), + ], + ); + let clone = fx.clone(false); + clone_edit_src(&fx, &clone, b"\xff\xfeOURS\n"); + let ours = refs::read_branch_tip(clone.layout(), "main") + .unwrap() + .unwrap(); + let sidecar = fx.clone_root.join("src/a.txt.theirs"); + + assert!(matches!( + clone.cherry_pick("origin/b1", "t", None, None).unwrap_err(), + Error::PickConflicts(1) + )); + assert!(sidecar.exists()); + let src = dir_id(&clone, &ours, "src"); + clone.vfs().store().lock().unwrap().delete(&src).unwrap(); + let err = clone.cherry_pick_abort().unwrap_err(); + assert!(matches!(err, Error::GapInView { .. }), "got {err:?}"); + assert!(clone.pick_in_progress(), "still open"); + assert!(sidecar.exists(), "sidecar still there"); + drop(clone); + fx.cleanup(); + + let fx = Fx::origin("pick-abort2", |_| {}); + fx.origin_branch("b1", &[("src/a.txt", SRC_ONE)]); + let clone = fx.clone(false); + clone_edit_src(&fx, &clone, SRC_UNO); + assert!(matches!( + clone.cherry_pick("origin/b1", "t", None, None).unwrap_err(), + Error::PickConflicts(1) + )); + clone.cherry_pick_abort().unwrap(); + assert!(!clone.pick_in_progress()); + assert_eq!( + std::fs::read(fx.clone_root.join("src/a.txt")).unwrap(), + SRC_UNO + ); + drop(clone); + fx.cleanup(); +} + +/// A protected subtree no commit touched is carried through a pick, so its +/// blobs were never enumerated: the picked snapshot must keep every wrap. +#[test] +fn a_pick_keeps_the_wraps_of_a_carried_protected_subtree() { + let (sk, pk) = scl_crypto::generate_keypair(); + let fx = protected_origin("pick-wraps", &pk); + let base = fx.origin.head_tip().unwrap().unwrap(); + let base_snap = fx.origin.snapshot(&base).unwrap(); + let vault = dir_id(&fx.origin, &base, "vault"); + fx.origin_branch("b1", &[("docs/a.md", b"a1\n")]); + let clone = fx.clone(false); + assert!(!has(&clone, &vault)); + clone_edit_src(&fx, &clone, SRC_UNO); + + let PickResult::Picked(id) = clone.cherry_pick("origin/b1", "t", None, None).unwrap() else { + panic!("expected a pick"); + }; + let snap = clone.snapshot(&id).unwrap(); + assert_eq!(dir_id(&clone, &id, "vault"), vault); + assert!(!has(&clone, &vault)); + for (blob, wraps) in &base_snap.protection.wrapped { + assert_eq!(snap.protection.wrapped.get(blob), Some(wraps)); + } + let (cipher_id, _) = base_snap.protection.wrapped.iter().next().unwrap(); + let cipher = { + let arc = fx.origin.vfs().store(); + let mut s = arc.lock().unwrap(); + match s.get(cipher_id).unwrap() { + scl_core::Object::Blob(b) => b, + other => panic!("expected a blob, got {}", other.kind_name()), + } + }; + let plain = + crate::protect::decrypt_with(&cipher, cipher_id, &[&snap.protection], &sk, "vault/s.txt") + .unwrap(); + assert_eq!(&plain[..], b"l1\nl2\nl3\n"); + drop(clone); + fx.cleanup(); +} + +/// Two local commits replayed onto a target that changed `docs/` carry the +/// target's subtree through both steps and end at the full rebase's tree. +#[test] +fn a_rebase_carries_the_targets_subtree_through_every_step() { + let fx = Fx::origin("rebase-carry", |_| {}); + let b1 = fx.origin_branch("b1", &[("docs/api/guide.md", b"g changed\n")]); + let theirs_docs = dir_id(&fx.origin, &b1, "docs"); + let two = b"one\nTWO\nthree\n"; + fx.origin.branch("loc").unwrap(); + fx.origin.switch("loc").unwrap(); + std::fs::write(fx.origin_root.join("src/a.txt"), two).unwrap(); + fx.origin.commit("t", "local 1").unwrap(); + std::fs::write(fx.origin_root.join("src/n.txt"), b"n\n").unwrap(); + fx.origin.commit("t", "local 2").unwrap(); + let RebaseResult::Rebased { new_tip, .. } = fx.origin.rebase("b1", "t", None).unwrap() else { + panic!("expected a rebase"); + }; + let expect = root_of(&fx.origin, &new_tip); + fx.origin.switch("main").unwrap(); + + let clone = fx.clone(false); + std::fs::write(fx.clone_root.join("src/a.txt"), two).unwrap(); + clone.commit("t", "local 1").unwrap(); + std::fs::write(fx.clone_root.join("src/n.txt"), b"n\n").unwrap(); + clone.commit("t", "local 2").unwrap(); + + let RebaseResult::Rebased { + new_tip, + replayed, + skipped, + } = clone.rebase("origin/b1", "t", None).unwrap() + else { + panic!("expected a rebase"); + }; + assert_eq!((replayed, skipped), (2, 0)); + assert_eq!( + root_of(&clone, &new_tip), + expect, + "same tree as the full rebase" + ); + assert_eq!(dir_id(&clone, &new_tip, "docs"), theirs_docs); + assert!(!has(&clone, &theirs_docs), "never read or fetched"); + assert!(!fx.clone_root.join("docs").exists()); + assert_eq!( + std::fs::read(fx.clone_root.join("src/n.txt")).unwrap(), + b"n\n" + ); + drop(clone); + fx.cleanup(); +} + +/// A rebase stops on a conflict in the view, can be aborted, and when resolved +/// and continued ends at the tree the full rebase gives. +#[test] +fn a_rebase_stops_on_a_conflict_in_the_view_and_continues() { + let fx = Fx::origin("rebase-conflict", |_| {}); + fx.origin_branch( + "b1", + &[ + ("src/a.txt", SRC_ONE), + ("docs/api/guide.md", b"g changed\n"), + ], + ); + let two = b"one\nTWO\nthree\n"; + // Commit 1 edits line 2; commit 2 edits line 1 and so conflicts with b1. + let local2 = b"uno\nTWO\nthree\n"; + fx.origin.branch("loc").unwrap(); + fx.origin.switch("loc").unwrap(); + std::fs::write(fx.origin_root.join("src/a.txt"), two).unwrap(); + fx.origin.commit("t", "local 1").unwrap(); + std::fs::write(fx.origin_root.join("src/a.txt"), local2).unwrap(); + fx.origin.commit("t", "local 2").unwrap(); + assert!(matches!( + fx.origin.rebase("b1", "t", None).unwrap(), + RebaseResult::Stopped { .. } + )); + fx.origin + .resolve_path("src/a.txt", ResolveSide::Ours, None) + .unwrap(); + let RebaseResult::Rebased { new_tip, .. } = fx.origin.rebase_continue("t", None).unwrap() + else { + panic!("expected a rebase"); + }; + let expect = root_of(&fx.origin, &new_tip); + fx.origin.switch("main").unwrap(); + + let clone = fx.clone(false); + std::fs::write(fx.clone_root.join("src/a.txt"), two).unwrap(); + clone.commit("t", "local 1").unwrap(); + std::fs::write(fx.clone_root.join("src/a.txt"), local2).unwrap(); + clone.commit("t", "local 2").unwrap(); + let original = refs::read_branch_tip(clone.layout(), "main").unwrap(); + + let RebaseResult::Stopped { + paths, done, total, .. + } = clone.rebase("origin/b1", "t", None).unwrap() + else { + panic!("expected a stop"); + }; + assert_eq!((paths, done, total), (vec!["src/a.txt".to_string()], 1, 2)); + assert!(clone.rebase_in_progress()); + assert!(std::fs::read_to_string(fx.clone_root.join("src/a.txt")) + .unwrap() + .contains("<<<<<<<")); + assert!(!fx.clone_root.join("docs").exists()); + assert_eq!( + refs::read_branch_tip(clone.layout(), "main").unwrap(), + original, + "no ref moved" + ); + + clone.rebase_abort().unwrap(); + assert!(!clone.rebase_in_progress()); + assert_eq!( + std::fs::read(fx.clone_root.join("src/a.txt")).unwrap(), + local2 + ); + + assert!(matches!( + clone.rebase("origin/b1", "t", None).unwrap(), + RebaseResult::Stopped { .. } + )); + clone + .resolve_path("src/a.txt", ResolveSide::Ours, None) + .unwrap(); + let RebaseResult::Rebased { new_tip, .. } = clone.rebase_continue("t", None).unwrap() else { + panic!("expected a rebase"); + }; + assert_eq!( + root_of(&clone, &new_tip), + expect, + "same tree as the full rebase" + ); + assert!(!clone.rebase_in_progress()); + drop(clone); + fx.cleanup(); +} + +/// SRC_A is the first commit's content: a pick that changes nothing there is +/// already applied. +#[test] +fn a_pick_already_applied_is_a_noop_on_a_partial_clone() { + let fx = Fx::origin("pick-noop", |_| {}); + fx.origin_branch("b1", &[("src/a.txt", SRC_UNO)]); + let clone = fx.clone(false); + clone_edit_src(&fx, &clone, SRC_UNO); + let before = refs::read_branch_tip(clone.layout(), "main").unwrap(); + assert_eq!( + clone.cherry_pick("origin/b1", "t", None, None).unwrap(), + PickResult::AlreadyApplied + ); + assert_eq!( + refs::read_branch_tip(clone.layout(), "main").unwrap(), + before + ); + let _ = SRC_A; + drop(clone); + fx.cleanup(); +} + +/// A carried subtree that lies inside the view is filled before the pick or +/// the rebase lays anything out: a lazy clone fetches it, a non-lazy one fails +/// with the view-gap error and changes nothing. +#[test] +fn a_carried_subtree_inside_the_view_is_filled_before_a_pick_or_rebase_writes() { + let fx = Fx::origin("replay-view", |_| {}); + fx.origin_branch("b1", &[("docs/api/guide.md", b"g changed\n")]); + let clone = fx.clone(true); + clone + .set_sparse(&["src/".to_string(), "docs/".to_string()], None) + .unwrap(); + clone_edit_src(&fx, &clone, SRC_UNO); + let before = refs::read_branch_tip(clone.layout(), "main").unwrap(); + + crate::promisor::set_lazy(clone.layout(), false).unwrap(); + let err = clone.cherry_pick("origin/b1", "t", None, None).unwrap_err(); + assert!(matches!(err, Error::GapInView { .. }), "got {err:?}"); + assert_eq!( + refs::read_branch_tip(clone.layout(), "main").unwrap(), + before + ); + assert_eq!( + std::fs::read(fx.clone_root.join("docs/api/guide.md")).unwrap(), + b"g\n", + "nothing was laid out" + ); + let err = clone.rebase("origin/b1", "t", None).unwrap_err(); + assert!(matches!(err, Error::GapInView { .. }), "got {err:?}"); + assert_eq!( + refs::read_branch_tip(clone.layout(), "main").unwrap(), + before + ); + + crate::promisor::set_lazy(clone.layout(), true).unwrap(); + clone.cherry_pick("origin/b1", "t", None, None).unwrap(); + assert_eq!( + std::fs::read(fx.clone_root.join("docs/api/guide.md")).unwrap(), + b"g changed\n" + ); + drop(clone); + fx.cleanup(); +} + +/// The same on a lazy clone for a rebase: the carried in-view subtree is +/// fetched and laid out. +#[test] +fn a_rebase_fills_a_carried_subtree_inside_the_view() { + let fx = Fx::origin("rebase-view", |_| {}); + fx.origin_branch("b1", &[("docs/api/guide.md", b"g changed\n")]); + let clone = fx.clone(true); + clone + .set_sparse(&["src/".to_string(), "docs/".to_string()], None) + .unwrap(); + clone_edit_src(&fx, &clone, SRC_UNO); + assert!(matches!( + clone.rebase("origin/b1", "t", None).unwrap(), + RebaseResult::Rebased { .. } + )); + assert_eq!( + std::fs::read(fx.clone_root.join("docs/api/guide.md")).unwrap(), + b"g changed\n" + ); + assert_eq!( + std::fs::read(fx.clone_root.join("src/a.txt")).unwrap(), + SRC_UNO + ); + drop(clone); + fx.cleanup(); +} diff --git a/crates/repo/src/replay.rs b/crates/repo/src/replay.rs index 67dc7c09..aad3ee09 100644 --- a/crates/repo/src/replay.rs +++ b/crates/repo/src/replay.rs @@ -83,6 +83,10 @@ pub(crate) enum ReplayOutcome { files: Vec, sidecars: Vec<(String, Vec)>, paths: Vec, + /// Subtrees a partial clone's merge carried by id instead of reading + /// (ADR-0049 decision 10). They ride into the decided root. Empty on + /// a full clone. + carried: Vec<(String, scl_core::TreeEntry)>, }, } @@ -166,19 +170,6 @@ pub(crate) fn replay_commit( ), )); } - // Merge/pick completion guard (P27 Task 5, T5-I4): `three_way_files` - // below flattens the full base/onto/theirs trees; on a partial clone - // that would touch content this clone never fetched (out-of-filter), - // silently surfacing as a raw `NotFound` from deep inside the flatten. - // Refuse explicitly here instead — one choke point for both cherry-pick - // and rebase's fold, which both replay through this function — pointing - // at `sc backfill` rather than a confusing corruption-shaped error. - if repo.promisor()?.is_some() { - return Err(crate::promisor::partial_clone_unsupported( - "cherry-pick/rebase replay", - )); - } - let (onto_root, onto_prot) = onto; let base_snap = match base_override.or_else(|| snap.parents.first().copied()) { Some(p) => Some(repo.snapshot(&p)?), @@ -187,23 +178,54 @@ pub(crate) fn replay_commit( let theirs_root = snap.root; let theirs_prot = &snap.protection; - let fm = { + // A partial clone cannot flatten the trees: it merges tree by tree + // (ADR-0049), filling what it must read outside the store lock and + // carrying the rest by id. A full clone keeps the flatten merge. + let base_arg = base_snap.as_ref().map(|b| (b.root, &b.protection)); + let (fm, tree_carried) = if repo.promisor()?.is_some() { + let (done, _fill) = repo.tree_merge_filled( + base_arg, + (onto_root, onto_prot), + (theirs_root, theirs_prot), + identity, + )?; + let no_base = Protection::default(); + let wrapped_carry = done.merged_wraps( + onto_prot, + theirs_prot, + base_snap.as_ref().map_or(&no_base, |b| &b.protection), + ); + let carried_any = done.carried_any(); + let mut fm = done.merge; + fm.wrapped_carry = wrapped_carry; + (fm, Some((done.carried, carried_any))) + } else { let store_arc = repo.vfs().store(); let mut store = store_arc.lock().unwrap(); - merge::three_way_files( + let fm = merge::three_way_files( &mut store, - base_snap.as_ref().map(|b| (b.root, &b.protection)), + base_arg, (onto_root, onto_prot), (theirs_root, theirs_prot), identity, - )? + )?; + (fm, None) }; if !fm.conflicts.is_empty() { + let carried = match tree_carried { + Some((crate::merge_tree::Carried::Entries(entries), _)) => entries, + Some((crate::merge_tree::Carried::Root(_), _)) => { + debug_assert!(false, "a carried root leaves no conflict"); + Vec::new() + } + None => Vec::new(), + }; return Ok(ReplayOutcome::Conflicts { files: fm.files, sidecars: fm.sidecars, paths: fm.conflicts, + carried, }); } @@ -221,7 +243,13 @@ pub(crate) fn replay_commit( let (mut all, to_encrypt) = split_for_encryption(&fm.files, &union_prot)?; let (encrypted, fresh_wrapped) = protect::encrypt_protected(to_encrypt)?; all.extend(encrypted); - let root = repo.vfs().write_tree_with_perms(&all)?; + let root = match &tree_carried { + Some((crate::merge_tree::Carried::Root(id), _)) => *id, + Some((crate::merge_tree::Carried::Entries(entries), _)) => { + repo.vfs().write_tree_with_carried(&all, entries)? + } + None => repo.vfs().write_tree_with_perms(&all)?, + }; // Empty (P15 Task 9 extension): a tree-equal replay is a genuine no-op // only when this commit's own protection-prefix rules are ALSO @@ -251,7 +279,9 @@ pub(crate) fn replay_commit( *entry = protect::union_wraps(entry, &wks); } protect::reuse_prior_wraps(&mut wrapped, &onto_prot.wrapped); - { + // Not when a subtree was carried: its blobs were never enumerated, so a + // prune would drop their wraps for good (ADR-0049 decision 4). + if !tree_carried.as_ref().is_some_and(|(_, any)| *any) { let store_arc = repo.vfs().store(); let mut store = store_arc.lock().unwrap(); let reachable: std::collections::BTreeSet = @@ -473,6 +503,10 @@ impl Repo { )?; // Cache hoisted above the block so its save follows the ref // move below, matching commit/amend's discipline (P33 Task 7). + // The picked tree may carry a subtree that is absent inside + // the view: fill its closure before any ref, spec or file + // write (lazy partial clones only), outside the store lock. + self.ensure_view(root, &self.sparse_spec()?)?; let mut cache = self.open_protected_cache()?; { let store_arc = self.vfs().store(); @@ -480,7 +514,6 @@ impl Repo { // Protection-aware materialize: a PROTECTED entry decrypts // for `identity` when possible, else is skipped (never // writes ciphertext to disk) — same as merge's clean path. - // ensure-view-exempt: replay is refused on partial clones worktree::materialize( &self.layout, &mut store, @@ -507,6 +540,7 @@ impl Repo { files, sidecars, paths, + carried: carried_subtrees, } => { // Conflicted pick (P15 Task 7) — same worktree-direct-write // restructure as `Repo::merge_with_identity`'s conflict path @@ -546,7 +580,7 @@ impl Repo { }; let conflict_root = self.materialize_conflict_state( &carried, - &[], + &carried_subtrees, &to_encrypt, &sidecars, &conflict_prot, @@ -593,20 +627,25 @@ impl Repo { "no cherry-pick in progress — nothing to abort".into(), )); } - for path in crate::pick_state::read_conflicts(&self.layout)? { - let _ = std::fs::remove_file(self.layout.root.join(format!("{path}.theirs"))); - } let decided = crate::pick_state::read_decided_root(&self.layout)?; let ours_tip = self.head_tip()?; let mut skipped = Vec::new(); + let mut sidecar_roots: Vec = Vec::new(); if let Some(tip) = ours_tip { let snap = self.snapshot(&tip)?; + sidecar_roots.push(snap.root); + if let Some(picked) = crate::pick_state::read_pick_head(&self.layout)? { + sidecar_roots.push(self.snapshot(&picked)?.root); + } + // Fill the view's closure first (lazy partial clones only), + // before anything is removed: a view gap must leave the stopped + // pick intact and retryable (ADR-0049 decision 10). + self.ensure_view(snap.root, &self.sparse_spec()?)?; let store_arc = self.vfs().store(); let mut store = store_arc.lock().unwrap(); // No identity at abort time: nothing is decrypted, so `materialize` // records nothing — pass `None`, no cache to open or save // (P33 Task 7 item 7). - // ensure-view-exempt: replay is refused on partial clones skipped = worktree::materialize( &self.layout, &mut store, @@ -618,6 +657,10 @@ impl Repo { None, )?; } + self.remove_conflict_sidecars( + &crate::pick_state::read_conflicts(&self.layout)?, + &sidecar_roots, + )?; crate::pick_state::clear(&self.layout)?; Ok(skipped) } @@ -938,6 +981,7 @@ impl Repo { files, sidecars, paths, + carried: carried_subtrees, } => { // Stop, don't abort (P19/ADR-0029): persist the fold's // progress and put P4 markers in the working tree. The @@ -968,7 +1012,7 @@ impl Repo { }; let conflict_root = self.materialize_conflict_state( &carried, - &[], + &carried_subtrees, &to_encrypt, &sidecars, &conflict_prot, @@ -1012,6 +1056,10 @@ impl Repo { } } + // The accumulated tree may carry a subtree that is absent inside the + // view: fill its closure before any ref, spec or file write (lazy + // partial clones only), outside the store lock. + self.ensure_view(acc_root, &self.sparse_spec()?)?; // Cache hoisted above the block so its save follows the ref move // below, matching commit/amend's discipline (P33 Task 7). let mut cache = self.open_protected_cache()?; @@ -1022,7 +1070,6 @@ impl Repo { // (each step pruned its wrap map to its own merged tree): a // PROTECTED entry decrypts for `identity` when possible, else is // skipped — never writes ciphertext to disk. - // ensure-view-exempt: replay is refused on partial clones worktree::materialize( &self.layout, &mut store, @@ -1165,9 +1212,17 @@ impl Repo { // `merge_abort`'s cleanup): once resolved, they're pure // working-tree litter — never part of the tracked tree, so // `materialize` never touches them itself. - for path in crate::rebase_state::read_conflicts(&self.layout)? { - let _ = std::fs::remove_file(self.layout.root.join(format!("{path}.theirs"))); - } + // Not a file any of the trees involved tracks (that one is the + // user's own): see `remove_conflict_sidecars`. + let roots = [ + self.snapshot(&st.acc_tip)?.root, + self.snapshot(&st.conflicted)?.root, + self.snapshot(&new_tip)?.root, + ]; + self.remove_conflict_sidecars( + &crate::rebase_state::read_conflicts(&self.layout)?, + &roots, + )?; // Advance state BEFORE attempting the fold, so an error from the // fold below (on a later commit) leaves a retryable, idempotent // `--continue` rather than losing resumability. `rebase_abort` @@ -1220,9 +1275,6 @@ impl Repo { "no rebase in progress — nothing to abort".into(), )); }; - for path in crate::rebase_state::read_conflicts(&self.layout)? { - let _ = std::fs::remove_file(self.layout.root.join(format!("{path}.theirs"))); - } // The stop's own conflict-materialize wrote `REBASE_DECIDED_ROOT` // (`conflict_root` in `rebase_fold_and_finish`'s Conflicts arm) to // disk as the tree the working tree currently, actually reflects — @@ -1234,13 +1286,16 @@ impl Repo { // where the decided root is unexpectedly absent. let decided = crate::rebase_state::read_decided_root(&self.layout)?; let snap = self.snapshot(&st.original_tip)?; + // Fill the view's closure first (lazy partial clones only), before + // anything is removed: a view gap must leave the stopped rebase + // intact and retryable (ADR-0049 decision 10). + self.ensure_view(snap.root, &self.sparse_spec()?)?; let skipped = { let store_arc = self.vfs().store(); let mut store = store_arc.lock().unwrap(); // No identity at abort time: nothing is decrypted, so `materialize` // records nothing — pass `None`, no cache to open or save // (P33 Task 7 item 7). - // ensure-view-exempt: replay is refused on partial clones worktree::materialize( &self.layout, &mut store, @@ -1252,6 +1307,12 @@ impl Repo { None, )? }; + let roots = [ + snap.root, + self.snapshot(&st.conflicted)?.root, + self.snapshot(&st.acc_tip)?.root, + ]; + self.remove_conflict_sidecars(&crate::rebase_state::read_conflicts(&self.layout)?, &roots)?; crate::rebase_state::clear(&self.layout)?; Ok(skipped) } diff --git a/crates/repo/src/repo.rs b/crates/repo/src/repo.rs index 56083a4e..01487b18 100644 --- a/crates/repo/src/repo.rs +++ b/crates/repo/src/repo.rs @@ -208,6 +208,10 @@ impl Repo { author: &str, message: &str, ) -> Result { + // The parent's view closure can hold an in-view subtree no earlier step + // filled (a rebase accumulator that carried it): fill it before the + // tracked paths are read from it (lazy partial clones only). + self.ensure_view(self.snapshot(&parent)?.root, &self.sparse_spec()?)?; let files = worktree::read_worktree(&self.layout, &self.tracked_paths_at(Some(parent))?)?; // Pick/rebase completion opens its own main cache. It saves right // after `snapshot_files` (before the caller moves the ref): the sealed @@ -413,7 +417,8 @@ impl Repo { // outcome, carried subtrees included, so the out-of-view part is // grafted from it below. A merge with no decided root, and every // pick or rebase completion, still refuse. - let merge_completion = merge_head.is_some() && decided_root.is_some(); + let merge_completion = + (merge_head.is_some() || pick_head.is_some()) && decided_root.is_some(); if partial && !merge_completion && (decided_root.is_some() || merge_head.is_some()) { return Err(crate::promisor::partial_clone_unsupported( "merge/pick completion", @@ -1797,6 +1802,32 @@ impl Repo { Ok(conflict_root) } + /// Remove the `.theirs` sidecars a stopped merge, pick or rebase + /// wrote for `paths`, except a file any of `roots` tracks: that one is the + /// user's own and was just restored. A failed lookup counts as tracked: a + /// stray sidecar is harmless, deleting a tracked file is not. Call it after + /// the restore, so a restore that fails leaves the sidecars to retry. + pub(crate) fn remove_conflict_sidecars( + &self, + paths: &[String], + roots: &[ObjectId], + ) -> Result<()> { + let partial = self.promisor()?.is_some(); + let store_arc = self.vfs.store(); + let mut store = store_arc.lock().unwrap(); + for path in paths { + let sidecar = format!("{path}.theirs"); + let tracked = roots.iter().any(|root| { + worktree::tree_entry_at(&mut store, *root, &sidecar, partial) + .map_or(true, |e| e.is_some()) + }); + if !tracked { + let _ = std::fs::remove_file(self.layout.root.join(sidecar)); + } + } + Ok(()) + } + /// Abandon an in-progress merge: restore the working tree to the current tip /// and clear merge state. Errors if no merge is in progress. /// @@ -1822,16 +1853,7 @@ impl Repo { // the deletion pass drops files the conflicted merge pulled in from // theirs; materializing against ours==old would delete nothing. let mut skipped = Vec::new(); - // Sidecars (`.theirs`) this abort may remove: not a file either - // side tracks, which would be the user's own and was just restored. - // A lookup that fails is treated as tracked: a stray sidecar is - // harmless, deleting a tracked file is not. - let mut removable: Vec = Vec::new(); - let partial = self.promisor()?.is_some(); - let sidecars = crate::merge_state::read_conflicts(&self.layout)? - .into_iter() - .map(|p| format!("{p}.theirs")) - .collect::>(); + let mut sidecar_roots: Vec = Vec::new(); let ours_tip = self.head_tip()?; if let Some(ours_id) = ours_tip { let store_arc = self.vfs.store(); @@ -1840,15 +1862,7 @@ impl Repo { let ours_root = ours_snap.root; let ours_protection = ours_snap.protection; let theirs_root = store.get_snapshot(&theirs_id)?.root; - for sidecar in sidecars { - let tracked = [ours_root, theirs_root].iter().any(|root| { - worktree::tree_entry_at(&mut store, *root, &sidecar, partial) - .map_or(true, |e| e.is_some()) - }); - if !tracked { - removable.push(sidecar); - } - } + sidecar_roots = vec![ours_root, theirs_root]; // No identity at abort time: protected files in ours are skipped // (not decrypted). With nothing decrypted, `materialize` records // nothing into a cache, so pass `None` — no cache to open or save @@ -1864,12 +1878,11 @@ impl Repo { &self.sparse_spec()?, None, )?; - } else { - removable = sidecars; - } - for sidecar in removable { - let _ = std::fs::remove_file(self.layout.root.join(sidecar)); } + self.remove_conflict_sidecars( + &crate::merge_state::read_conflicts(&self.layout)?, + &sidecar_roots, + )?; crate::merge_state::clear(&self.layout)?; Ok(skipped) } diff --git a/demo/run_lazy_fetch_demo.sh b/demo/run_lazy_fetch_demo.sh index 32d19dc8..633da6ba 100755 --- a/demo/run_lazy_fetch_demo.sh +++ b/demo/run_lazy_fetch_demo.sh @@ -239,4 +239,37 @@ cmp "$CFULL/docs/api/big.bin" "$W/conflict-feature-big.bin" || fail "feature's d echo "push + full re-clone: resolution and carried docs both present ✔" echo -echo "RESULT: lazy by-id fetch verified (path-only fetch, offline/non-lazy gap errors, http v5, widen/commit/push/undo, merge, conflict)" +echo "=== 11: cherry-pick and rebase carry the other side's region unread (ADR-0049) ===" +PORIGIN="$W/porigin" +mkdir -p "$PORIGIN" +( + cd "$PORIGIN" + "$SC" init >/dev/null + mkdir -p src docs/api + printf 'one\ntwo\nthree\n' > src/a.txt + head -c 65536 /dev/urandom | od -An -tx1 | tr -d ' \n' > docs/api/big.bin + "$SC" commit -m "base" --author demo >/dev/null + "$SC" branch feature + "$SC" switch feature >/dev/null + head -c 65536 /dev/urandom | od -An -tx1 | tr -d ' \n' > docs/api/big.bin + cp docs/api/big.bin "$W/pick-feature-big.bin" + "$SC" commit -m "feature edits docs" --author demo >/dev/null + "$SC" switch main >/dev/null +) +for OP in cherry-pick rebase; do + PCLONE="$W/replay-$OP" + "$SC" clone --filter src/ --lazy "$PORIGIN" "$PCLONE" >/dev/null + printf 'one\nTWO\nthree\n' > "$PCLONE/src/a.txt" + (cd "$PCLONE" && "$SC" commit -m "local src edit" --author demo >/dev/null && "$SC" fetch origin >/dev/null) + PBEFORE=$(objects "$PCLONE") + (cd "$PCLONE" && "$SC" "$OP" origin/feature --author demo >/dev/null 2>&1) || fail "$OP on a partial clone must succeed" + [ ! -e "$PCLONE/docs" ] || fail "docs/ is outside the view and must not be written ($OP)" + [ "$(cat "$PCLONE/src/a.txt")" = "$(printf 'one\nTWO\nthree')" ] || fail "the local src edit must survive $OP" + PAFTER=$(objects "$PCLONE") + [ "$(($PAFTER - $PBEFORE))" -le 8 ] || fail "$OP must not fetch the other side's region ($PBEFORE -> $PAFTER)" + (cd "$PCLONE" && "$SC" verify >/dev/null) || fail "verify must work after $OP" + echo "$OP on a lazy partial clone: docs/ untouched and unfetched ($PBEFORE -> $PAFTER objects) ✔" +done + +echo +echo "RESULT: lazy by-id fetch verified (path-only fetch, offline/non-lazy gap errors, http v5, widen/commit/push/undo, merge, conflict, cherry-pick/rebase)" diff --git a/docs/adr/0049-merge-on-partial-clones.md b/docs/adr/0049-merge-on-partial-clones.md index 60226c38..0f00dd1d 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 (37c-i engine, 37c-ii clean merge and 37c-iii conflicts built; replay and sessions open) +- **Status:** Proposed (37c-i engine, 37c-ii clean merge, 37c-iii conflicts and 37c-iv replay built; sessions open) - **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) @@ -116,6 +116,18 @@ Flatten drops empty directories and directory metadata and rebuilds directories **`sc conflicts` and `sc resolve`** look a path up by descending its components (`entry_at`) instead of flattening a whole tree. A missing tree on the way is a gap error that names the path, never `Absent`: an absent side means the path is not in that snapshot, and a gap must not read as a deletion. On a full clone the result is identical. +### 10. Replay: cherry-pick and rebase (amended in 37c-iv) + +Replay is a three-way merge with a different base, and `replay_commit` is the only place cherry-pick and the rebase fold reach the file merge (`replay.rs:176` is the one guard that refuses a partial clone). So replay uses the engine of decisions 2 to 5 and the conflict contract of decision 8, and adds only what the fold needs. + +- **The merge.** On a partial clone `replay_commit` calls `tree_merge_filled` with base = the replayed commit's parent (or the `--mainline` parent, or none for a root commit), ours = the onto tree, theirs = the commit. It fills outside the store lock, as merge does. Registry merging is unchanged: it is snapshot-level and already runs before the file replay. +- **Clean result.** The root is the enumerated files plus the carried subtrees (or the carried root), exactly as in merge. The wrap map is `merged_wraps` (the complete input union when anything was carried, else the existing prune). An `Empty` outcome still means the root equals the onto root and the commit's own rule delta is nil. +- **The fold's accumulator** (`acc_root`, `acc_protection`) may itself contain absent subtrees, because an earlier step carried them. That needs nothing new: the next step's engine pass sees an onto tree it may not hold and asks for what it reads. What the fold must add is the view: before the final materialize, `ensure_view(acc_root)` runs outside the store lock, and no ref, spec or working-tree file is written until it and the preflight succeed. The intermediate snapshots are CAS-only, so they stay unconstrained. +- **Conflict state** is decision 8 applied: `PICK_DECIDED_ROOT` and `REBASE_DECIDED_ROOT` hold the CAS-safe entries plus the carried subtrees, nothing is sealed while the op is open, and the same three refusals fire before any write (conflict outside the view, sidecar outside the view or over a tracked file, plaintext to seal outside the view). `ReplayOutcome::Conflicts` therefore carries the subtrees. +- **Completion.** `snapshot_files` accepts a recorded decided root for a pick or a rebase step as it does for a merge, grafts the out-of-view part from it, and keeps the union of the tip's and the picked commit's wraps. `rebase --continue` reads its parent's view first, since the accumulator can hold an in-view subtree no earlier step filled. A pick or rebase completion with no decided root still refuses. +- **Abort.** `cherry-pick --abort` and `rebase --abort` check the view before they delete anything and remove only sidecars that no tree tracks, the same as `merge --abort`. The same rule now holds for the sidecar cleanup in `rebase --continue`. +- **Unchanged.** A rebase over a range that contains a merge commit still refuses before replaying anything. Sessions (`sc ws`, `sc work`) stay refused. + ### 9. Acceptance: differential tests built from real operations `merge_trees` is checked against the flatten implementation, with both using the content/mode split of decision 1. The comparison is on **decrypted file sets, modes, perms, conflict sets, sidecars, effective rules and tombstones, the recipient set each protected blob is wrapped for, and actual decryption by each relevant identity**, not on wrap-map key coverage (which can pass with a missing recipient or a wrong DEK). Carried objects compare by bytes; freshly sealed output after decryption, because sealing is randomized. Cases are generated through real operations (`protect`, `grant`, `revoke`, commit, fetch) as well as arbitrary trees, and gaps are randomized independently of the sparse view and the filter. Required named cases: @@ -137,7 +149,7 @@ A second differential runs each merge on a partial clone and a full clone. The n - **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 (built).** `merge_with_identity` uses the engine when the clone is partial: `Repo::tree_merge_filled` runs a pass under the store lock, fills what it asked for outside it (`Repo::fill_needs`, batched and split like every by-id fetch) and repeats; a conflict is refused with `PartialCloneUnsupported` before any write; the merged root is built by `Vfs::write_tree_with_carried` (enumerated files plus carried subtrees verbatim, or the carried root as is); `merged_wraps` is the wrap map, unpruned when anything was carried; `ensure_view(merged_root)` runs before the first ref, spec or working-tree write. The differing region's blobs are fetched too: the leaf merge returns file bytes even when one side wins by id, so a region both sides touched is read in full while an untouched subtree (identical on all three sides) is not. Cases from decision 9 that need real repo operations are covered by `partial_merge.rs`: disjoint carry without a fetch, the differing region (fill when lazy, gap error otherwise), a conflict refused, a carried protected subtree keeping its wraps, a keyless protected merge refusing before fetching (the merged result decrypts for the recipient and its plaintext never reaches the CAS), a carried subtree inside the view, and an interrupted fill followed by a retry. The carried wraps are checked byte for byte and by decrypting the carried ciphertext under the merged policy. Merge then gc is covered by the disjoint-carry test. Still open from decision 9: equal plaintext sealed independently, crossed revokes and regrants, an empty-directory entry on a carried side, and a randomized partial-versus-full differential. Demo: `demo/run_lazy_fetch_demo.sh` section 9. - **37c-iii, conflicts (built).** The amendment to decision 8, then markers for in-view conflicts, completion from the decided root, abort, `sc conflicts` and `sc resolve`. A conflict outside the view, or a result that needs sealing outside it, is refused before any write. Tests in `partial_merge.rs` (conflict in view completes with the same tree as a full merge, carried subtree never fetched, abort, out-of-view refusals, carried protected subtree keeps its wraps through completion) and the CLI test `conflicting_merge_on_a_partial_clone_resolves_and_commits`. Demo section 10. -- **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. +- **37c-iv, replay (built).** `cherry-pick` and `rebase` call the same engine through `replay_commit` (decision 10). Tests in `partial_replay.rs` compare each result with the same operation on a full repository by root tree id: a carried subtree never fetched, a differing region filled when lazy and refused otherwise, a conflict in the view completing like a full pick, abort and a view gap on abort, a carried protected subtree keeping its wraps, a rebase carrying the target's subtree through every step, a rebase stopping, aborting, resolving and continuing, and in-view carried subtrees filled on both paths. CLI test `cherry_pick_and_rebase_work_on_a_partial_clone`. Demo section 11. - **Sessions** (`sc ws`, `sc work`): undecided. They materialize whole checkouts per agent and auto-merge on harvest. Whether they should work on a partial clone at all is a product question. ## Consequences diff --git a/docs/adr/README.md b/docs/adr/README.md index c33f3199..9ee290aa 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-i, 37c-ii, 37c-iii built) | 37c | +| [0049](0049-merge-on-partial-clones.md) | Merge on partial clones | Proposed (37c-i to 37c-iv 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 ca2d8f2eb85e7764ddf18308e09299ed7033dec5 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Sun, 18 Oct 2026 16:56:25 +0300 Subject: [PATCH 2/3] fix(repo): refuse a partial pick or rebase completion without its decided root The guard checked the merge head and the decided root but not the pick head, so a stopped pick whose decided-root file was gone completed by grafting from the tip and silently kept the old out-of-view content. Refuse instead; --abort still clears the state. ADR-0049 records three existing full-clone defects the review found (q18 to q20). Co-Authored-By: Claude Sonnet 5.5 --- crates/repo/src/partial_replay.rs | 35 ++++++++++++++++++++++++ crates/repo/src/repo.rs | 5 +++- docs/adr/0049-merge-on-partial-clones.md | 3 ++ 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/crates/repo/src/partial_replay.rs b/crates/repo/src/partial_replay.rs index a467570c..ff7a0cf5 100644 --- a/crates/repo/src/partial_replay.rs +++ b/crates/repo/src/partial_replay.rs @@ -472,3 +472,38 @@ fn a_rebase_fills_a_carried_subtree_inside_the_view() { drop(clone); fx.cleanup(); } + +/// A stopped pick whose decided root is gone cannot be completed on a partial +/// clone: grafting from the tip would silently keep the old out-of-view +/// content. It refuses, and `--abort` still clears the state. +#[test] +fn a_stopped_pick_without_its_decided_root_refuses_to_complete() { + let fx = Fx::origin("pick-no-decided", |_| {}); + fx.origin_branch( + "b1", + &[ + ("src/a.txt", SRC_ONE), + ("docs/api/guide.md", b"g changed\n"), + ], + ); + let clone = fx.clone(false); + clone_edit_src(&fx, &clone, SRC_UNO); + assert!(matches!( + clone.cherry_pick("origin/b1", "t", None, None).unwrap_err(), + Error::PickConflicts(1) + )); + std::fs::remove_file(clone.layout().dot_sc.join("PICK_DECIDED_ROOT")).unwrap(); + clone + .resolve_path("src/a.txt", ResolveSide::Ours, None) + .unwrap(); + let err = clone.commit("t", "pick").unwrap_err(); + assert!( + matches!(err, Error::PartialCloneUnsupported(_)), + "got {err:?}" + ); + assert!(clone.pick_in_progress(), "the state is kept"); + clone.cherry_pick_abort().unwrap(); + assert!(!clone.pick_in_progress()); + drop(clone); + fx.cleanup(); +} diff --git a/crates/repo/src/repo.rs b/crates/repo/src/repo.rs index 01487b18..29eb114f 100644 --- a/crates/repo/src/repo.rs +++ b/crates/repo/src/repo.rs @@ -419,7 +419,10 @@ impl Repo { // pick or rebase completion, still refuse. let merge_completion = (merge_head.is_some() || pick_head.is_some()) && decided_root.is_some(); - if partial && !merge_completion && (decided_root.is_some() || merge_head.is_some()) { + if partial + && !merge_completion + && (decided_root.is_some() || merge_head.is_some() || pick_head.is_some()) + { return Err(crate::promisor::partial_clone_unsupported( "merge/pick completion", )); diff --git a/docs/adr/0049-merge-on-partial-clones.md b/docs/adr/0049-merge-on-partial-clones.md index 0f00dd1d..a85b80a2 100644 --- a/docs/adr/0049-merge-on-partial-clones.md +++ b/docs/adr/0049-merge-on-partial-clones.md @@ -180,6 +180,9 @@ A second differential runs each merge on a partial clone and a full clone. The n 15. **State durability.** `merge_state` renames without syncing, as the other state files do. A power loss between the decided-root write and `MERGE_HEAD` leaves a state that partial completion refuses and `merge --abort` clears. 16. **A protected delete-versus-modify conflict resolved to the absent side.** The conflict path is not in the decided root (protected conflict plaintext lives on disk), so after `sc resolve` deletes the working file, completion's absent-protected carry restores it from the first parent. The full-clone completion runs the same code (`snapshot_files`, "explicit deletion of a protected file is a future operation"), so the partial path matches it. Recording a decided deletion in the merge state would fix both. 17. **Single-tip graft keeps what the built side decided.** `graft_out_of_sparse` carries an out-of-view file standing where the view expects a directory, unless the built side already holds that name (a file replaced by a directory in an ordinary commit). +18. **Abort can leave decrypted conflict plaintext behind.** A protected delete-versus-modify conflict (base plain `src/x`, ours deleted it, theirs edits and protects it) writes the decrypted conflict text straight to disk. Neither the original root nor the decided root contains the path, so the abort's removal walk misses it and clears the state. This is the full-clone behaviour; decision 10 does not change it. +19. **Completion can drop a newly tracked ignored file.** The ignore exemptions come from the completion parent's tracked paths. A picked commit that adds a plain in-view file the parent's `.scignore` covers, and also conflicts elsewhere, loses that file at completion. Also the full-clone behaviour. +20. **A stopped pick or rebase without its decided root.** `PICK_DECIDED_ROOT` and `REBASE_DECIDED_ROOT` are written before the state's in-progress signal, so the state cannot normally be missing. If it is (a lost file, a power loss before the data reached disk), a partial clone's completion refuses, because grafting from the tip would keep the old out-of-view content; `--abort` still works. ## Alternatives considered From 2b087ecfd25e9670ae6ad94b55011ce94ea63424 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Sun, 18 Oct 2026 17:02:07 +0300 Subject: [PATCH 3/3] fix(repo): narrow abort sidecar cleanup and add replay tests Pick, rebase and merge aborts now remove only sidecars the restored tree does not track, and rebase --continue only those its completion parent does not track. Wider sets kept a sidecar the next commit would sweep in. Adds tests for a rebase abort that hits a view gap and a two-step rebase over a carried protected subtree, and fixes a stale comment and ADR reference. Co-Authored-By: Claude Sonnet 5.5 --- crates/repo/src/partial_replay.rs | 75 ++++++++++++++++++++++++ crates/repo/src/replay.rs | 25 ++++---- crates/repo/src/repo.rs | 12 ++-- docs/adr/0049-merge-on-partial-clones.md | 24 ++++---- 4 files changed, 103 insertions(+), 33 deletions(-) diff --git a/crates/repo/src/partial_replay.rs b/crates/repo/src/partial_replay.rs index ff7a0cf5..59aa6d52 100644 --- a/crates/repo/src/partial_replay.rs +++ b/crates/repo/src/partial_replay.rs @@ -507,3 +507,78 @@ fn a_stopped_pick_without_its_decided_root_refuses_to_complete() { drop(clone); fx.cleanup(); } + +/// A rebase abort that hits a gap in the view changes nothing: the rebase is +/// still open and the sidecar is still there. +#[test] +fn a_rebase_abort_that_hits_a_view_gap_changes_nothing() { + let fx = Fx::origin("rebase-abort-gap", |_| {}); + fx.origin_branch("b1", &[("src/a.txt", b"\xff\xfeTHEIRS\n")]); + let clone = fx.clone(false); + clone_edit_src(&fx, &clone, b"\xff\xfeOURS\n"); + let ours = refs::read_branch_tip(clone.layout(), "main") + .unwrap() + .unwrap(); + assert!(matches!( + clone.rebase("origin/b1", "t", None).unwrap(), + RebaseResult::Stopped { .. } + )); + let sidecar = fx.clone_root.join("src/a.txt.theirs"); + assert!(sidecar.exists()); + let src = dir_id(&clone, &ours, "src"); + clone.vfs().store().lock().unwrap().delete(&src).unwrap(); + let err = clone.rebase_abort().unwrap_err(); + assert!(matches!(err, Error::GapInView { .. }), "got {err:?}"); + assert!(clone.rebase_in_progress(), "still open"); + assert!(sidecar.exists(), "sidecar still there"); + drop(clone); + fx.cleanup(); +} + +/// Two replayed steps over a protected subtree no commit touched: it is carried +/// at every step, so the final snapshot keeps its wraps and the recipient still +/// decrypts it. +#[test] +fn a_two_step_rebase_keeps_the_wraps_of_a_carried_protected_subtree() { + let (sk, pk) = scl_crypto::generate_keypair(); + let fx = protected_origin("rebase-wraps", &pk); + let base = fx.origin.head_tip().unwrap().unwrap(); + let base_snap = fx.origin.snapshot(&base).unwrap(); + let vault = dir_id(&fx.origin, &base, "vault"); + fx.origin_branch("b1", &[("docs/a.md", b"a1\n")]); + let clone = fx.clone(false); + assert!(!has(&clone, &vault)); + std::fs::write(fx.clone_root.join("src/a.txt"), SRC_UNO).unwrap(); + clone.commit("t", "local 1").unwrap(); + std::fs::write(fx.clone_root.join("src/n.txt"), b"n\n").unwrap(); + clone.commit("t", "local 2").unwrap(); + + let RebaseResult::Rebased { + new_tip, replayed, .. + } = clone.rebase("origin/b1", "t", None).unwrap() + else { + panic!("expected a rebase"); + }; + assert_eq!(replayed, 2); + let snap = clone.snapshot(&new_tip).unwrap(); + assert_eq!(dir_id(&clone, &new_tip, "vault"), vault); + assert!(!has(&clone, &vault)); + for (blob, wraps) in &base_snap.protection.wrapped { + assert_eq!(snap.protection.wrapped.get(blob), Some(wraps)); + } + let (cipher_id, _) = base_snap.protection.wrapped.iter().next().unwrap(); + let cipher = { + let arc = fx.origin.vfs().store(); + let mut s = arc.lock().unwrap(); + match s.get(cipher_id).unwrap() { + scl_core::Object::Blob(b) => b, + other => panic!("expected a blob, got {}", other.kind_name()), + } + }; + let plain = + crate::protect::decrypt_with(&cipher, cipher_id, &[&snap.protection], &sk, "vault/s.txt") + .unwrap(); + assert_eq!(&plain[..], b"l1\nl2\nl3\n"); + drop(clone); + fx.cleanup(); +} diff --git a/crates/repo/src/replay.rs b/crates/repo/src/replay.rs index aad3ee09..fc99c775 100644 --- a/crates/repo/src/replay.rs +++ b/crates/repo/src/replay.rs @@ -633,10 +633,10 @@ impl Repo { let mut sidecar_roots: Vec = Vec::new(); if let Some(tip) = ours_tip { let snap = self.snapshot(&tip)?; + // Only the restored tree: a file it tracks is the user's own. A + // path only the picked tree tracks is gone after the restore, and + // whatever sits there is the sidecar. sidecar_roots.push(snap.root); - if let Some(picked) = crate::pick_state::read_pick_head(&self.layout)? { - sidecar_roots.push(self.snapshot(&picked)?.root); - } // Fill the view's closure first (lazy partial clones only), // before anything is removed: a view gap must leave the stopped // pick intact and retryable (ADR-0049 decision 10). @@ -1214,14 +1214,11 @@ impl Repo { // `materialize` never touches them itself. // Not a file any of the trees involved tracks (that one is the // user's own): see `remove_conflict_sidecars`. - let roots = [ - self.snapshot(&st.acc_tip)?.root, - self.snapshot(&st.conflicted)?.root, - self.snapshot(&new_tip)?.root, - ]; + // Not a file the completion's parent tracks (that one is the + // user's own): see `remove_conflict_sidecars`. self.remove_conflict_sidecars( &crate::rebase_state::read_conflicts(&self.layout)?, - &roots, + &[self.snapshot(&st.acc_tip)?.root], )?; // Advance state BEFORE attempting the fold, so an error from the // fold below (on a later commit) leaves a retryable, idempotent @@ -1307,12 +1304,10 @@ impl Repo { None, )? }; - let roots = [ - snap.root, - self.snapshot(&st.conflicted)?.root, - self.snapshot(&st.acc_tip)?.root, - ]; - self.remove_conflict_sidecars(&crate::rebase_state::read_conflicts(&self.layout)?, &roots)?; + self.remove_conflict_sidecars( + &crate::rebase_state::read_conflicts(&self.layout)?, + &[snap.root], + )?; crate::rebase_state::clear(&self.layout)?; Ok(skipped) } diff --git a/crates/repo/src/repo.rs b/crates/repo/src/repo.rs index 29eb114f..f9b501da 100644 --- a/crates/repo/src/repo.rs +++ b/crates/repo/src/repo.rs @@ -412,11 +412,11 @@ impl Repo { // (non-conflicted) pick/rebase fold still has `decided_root: None` // and keeps using the single-tip graft path, unaffected. // - // A conflicted MERGE completion is the exception (ADR-0049 decision - // 8): the conflict recorded a decided root holding the merge's whole - // outcome, carried subtrees included, so the out-of-view part is - // grafted from it below. A merge with no decided root, and every - // pick or rebase completion, still refuse. + // A conflicted merge, pick or rebase completion is the exception + // (ADR-0049 decisions 8 and 10): the conflict recorded a decided root + // holding the operation's whole outcome, carried subtrees included, + // so the out-of-view part is grafted from it below. One with no + // decided root still refuses. let merge_completion = (merge_head.is_some() || pick_head.is_some()) && decided_root.is_some(); if partial @@ -1865,7 +1865,7 @@ impl Repo { let ours_root = ours_snap.root; let ours_protection = ours_snap.protection; let theirs_root = store.get_snapshot(&theirs_id)?.root; - sidecar_roots = vec![ours_root, theirs_root]; + sidecar_roots = vec![ours_root]; // No identity at abort time: protected files in ours are skipped // (not decrypted). With nothing decrypted, `materialize` records // nothing into a cache, so pass `None` — no cache to open or save diff --git a/docs/adr/0049-merge-on-partial-clones.md b/docs/adr/0049-merge-on-partial-clones.md index a85b80a2..97443641 100644 --- a/docs/adr/0049-merge-on-partial-clones.md +++ b/docs/adr/0049-merge-on-partial-clones.md @@ -116,18 +116,6 @@ Flatten drops empty directories and directory metadata and rebuilds directories **`sc conflicts` and `sc resolve`** look a path up by descending its components (`entry_at`) instead of flattening a whole tree. A missing tree on the way is a gap error that names the path, never `Absent`: an absent side means the path is not in that snapshot, and a gap must not read as a deletion. On a full clone the result is identical. -### 10. Replay: cherry-pick and rebase (amended in 37c-iv) - -Replay is a three-way merge with a different base, and `replay_commit` is the only place cherry-pick and the rebase fold reach the file merge (`replay.rs:176` is the one guard that refuses a partial clone). So replay uses the engine of decisions 2 to 5 and the conflict contract of decision 8, and adds only what the fold needs. - -- **The merge.** On a partial clone `replay_commit` calls `tree_merge_filled` with base = the replayed commit's parent (or the `--mainline` parent, or none for a root commit), ours = the onto tree, theirs = the commit. It fills outside the store lock, as merge does. Registry merging is unchanged: it is snapshot-level and already runs before the file replay. -- **Clean result.** The root is the enumerated files plus the carried subtrees (or the carried root), exactly as in merge. The wrap map is `merged_wraps` (the complete input union when anything was carried, else the existing prune). An `Empty` outcome still means the root equals the onto root and the commit's own rule delta is nil. -- **The fold's accumulator** (`acc_root`, `acc_protection`) may itself contain absent subtrees, because an earlier step carried them. That needs nothing new: the next step's engine pass sees an onto tree it may not hold and asks for what it reads. What the fold must add is the view: before the final materialize, `ensure_view(acc_root)` runs outside the store lock, and no ref, spec or working-tree file is written until it and the preflight succeed. The intermediate snapshots are CAS-only, so they stay unconstrained. -- **Conflict state** is decision 8 applied: `PICK_DECIDED_ROOT` and `REBASE_DECIDED_ROOT` hold the CAS-safe entries plus the carried subtrees, nothing is sealed while the op is open, and the same three refusals fire before any write (conflict outside the view, sidecar outside the view or over a tracked file, plaintext to seal outside the view). `ReplayOutcome::Conflicts` therefore carries the subtrees. -- **Completion.** `snapshot_files` accepts a recorded decided root for a pick or a rebase step as it does for a merge, grafts the out-of-view part from it, and keeps the union of the tip's and the picked commit's wraps. `rebase --continue` reads its parent's view first, since the accumulator can hold an in-view subtree no earlier step filled. A pick or rebase completion with no decided root still refuses. -- **Abort.** `cherry-pick --abort` and `rebase --abort` check the view before they delete anything and remove only sidecars that no tree tracks, the same as `merge --abort`. The same rule now holds for the sidecar cleanup in `rebase --continue`. -- **Unchanged.** A rebase over a range that contains a merge commit still refuses before replaying anything. Sessions (`sc ws`, `sc work`) stay refused. - ### 9. Acceptance: differential tests built from real operations `merge_trees` is checked against the flatten implementation, with both using the content/mode split of decision 1. The comparison is on **decrypted file sets, modes, perms, conflict sets, sidecars, effective rules and tombstones, the recipient set each protected blob is wrapped for, and actual decryption by each relevant identity**, not on wrap-map key coverage (which can pass with a missing recipient or a wrong DEK). Carried objects compare by bytes; freshly sealed output after decryption, because sealing is randomized. Cases are generated through real operations (`protect`, `grant`, `revoke`, commit, fetch) as well as arbitrary trees, and gaps are randomized independently of the sparse view and the filter. Required named cases: @@ -144,6 +132,18 @@ Replay is a three-way merge with a different base, and `replay_commit` is the on A second differential runs each merge on a partial clone and a full clone. The new engine is wired only for partial clones first; full clones keep the flatten path (with the decision 1 leaf key) until the differential has run clean for a release. +### 10. Replay: cherry-pick and rebase (amended in 37c-iv) + +Replay is a three-way merge with a different base, and `replay_commit` is the only place cherry-pick and the rebase fold reach the file merge (its guard against partial clones, `partial_clone_unsupported("cherry-pick/rebase replay")`, is what this decision removes). So replay uses the engine of decisions 2 to 5 and the conflict contract of decision 8, and adds only what the fold needs. + +- **The merge.** On a partial clone `replay_commit` calls `tree_merge_filled` with base = the replayed commit's parent (or the `--mainline` parent, or none for a root commit), ours = the onto tree, theirs = the commit. It fills outside the store lock, as merge does. Registry merging is unchanged: it is snapshot-level and already runs before the file replay. +- **Clean result.** The root is the enumerated files plus the carried subtrees (or the carried root), exactly as in merge. The wrap map is `merged_wraps` (the complete input union when anything was carried, else the existing prune). An `Empty` outcome still means the root equals the onto root and the commit's own rule delta is nil. +- **The fold's accumulator** (`acc_root`, `acc_protection`) may itself contain absent subtrees, because an earlier step carried them. That needs nothing new: the next step's engine pass sees an onto tree it may not hold and asks for what it reads. What the fold must add is the view: before the final materialize, `ensure_view(acc_root)` runs outside the store lock, and no ref, spec or working-tree file is written until it and the preflight succeed. The intermediate snapshots are CAS-only, so they stay unconstrained. +- **Conflict state** is decision 8 applied: `PICK_DECIDED_ROOT` and `REBASE_DECIDED_ROOT` hold the CAS-safe entries plus the carried subtrees, nothing is sealed while the op is open, and the same three refusals fire before any write (conflict outside the view, sidecar outside the view or over a tracked file, plaintext to seal outside the view). `ReplayOutcome::Conflicts` therefore carries the subtrees. +- **Completion.** `snapshot_files` accepts a recorded decided root for a pick or a rebase step as it does for a merge, grafts the out-of-view part from it, and keeps the union of the tip's and the picked commit's wraps. `rebase --continue` reads its parent's view first, since the accumulator can hold an in-view subtree no earlier step filled. A pick or rebase completion with no decided root still refuses. +- **Abort.** `cherry-pick --abort` and `rebase --abort` check the view before they delete anything and remove only sidecars the restored tree does not track, the same as `merge --abort`. `rebase --continue` removes only sidecars its completion parent does not track. A sidecar the user left behind and a commit swept up is the existing defect of q19's kind: completion reads untracked files. +- **Unchanged.** A rebase over a range that contains a merge commit still refuses before replaying anything. Sessions (`sc ws`, `sc work`) stay refused. + ## Phasing - **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.